Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 360 lines (323 sloc) 11.437 kb
cfd7b4d added licensing information on the new files
Laurent Sansonetti authored
1 /*
2 * MacRuby compiler.
3 *
4 * This file is covered by the Ruby license. See COPYING for more details.
5 *
6 * Copyright (C) 2008-2009, Apple Inc. All rights reserved.
7 */
8
cb65416 the great schism, part I
Laurent Sansonetti authored
9 #ifndef __COMPILER_H_
10 #define __COMPILER_H_
11
12 #if defined(__cplusplus)
13
14 // For the dispatcher.
15 #define DISPATCH_VCALL 1
16 #define DISPATCH_SUPER 2
17 #define SPLAT_ARG_FOLLOWS 0xdeadbeef
18
19 // For defined?
20 #define DEFINED_IVAR 1
21 #define DEFINED_GVAR 2
22 #define DEFINED_CVAR 3
23 #define DEFINED_CONST 4
24 #define DEFINED_LCONST 5
25 #define DEFINED_SUPER 6
26 #define DEFINED_METHOD 7
27
28 class RoxorCompiler {
29 public:
30 static llvm::Module *module;
31 static RoxorCompiler *shared;
32
3432fd1 a better exception-based implementation for return-from-block
Laurent Sansonetti authored
33 RoxorCompiler(void);
cb65416 the great schism, part I
Laurent Sansonetti authored
34 virtual ~RoxorCompiler(void) { }
35
3432fd1 a better exception-based implementation for return-from-block
Laurent Sansonetti authored
36 void set_fname(const char *_fname) {
37 fname = _fname;
38 }
39
cb65416 the great schism, part I
Laurent Sansonetti authored
40 Value *compile_node(NODE *node);
41
42 virtual Function *compile_main_function(NODE *node);
43 Function *compile_read_attr(ID name);
44 Function *compile_write_attr(ID name);
45 Function *compile_stub(const char *types, int argc, bool is_objc);
46 Function *compile_bs_struct_new(rb_vm_bs_boxed_t *bs_boxed);
47 Function *compile_bs_struct_writer(rb_vm_bs_boxed_t *bs_boxed,
48 int field);
49 Function *compile_ffi_function(void *stub, void *imp, int argc);
50 Function *compile_to_rval_convertor(const char *type);
51 Function *compile_to_ocval_convertor(const char *type);
c1c347f AOT compiler: now supporting objc stubs and super dispatch
Laurent Sansonetti authored
52 Function *compile_objc_stub(Function *ruby_func, IMP ruby_imp,
53 const rb_vm_arity_t &arity, const char *types);
208a965 a quick implementation for #define_method (not yet 100% complete)
Laurent Sansonetti authored
54 Function *compile_block_caller(rb_vm_block_t *block);
cb65416 the great schism, part I
Laurent Sansonetti authored
55
56 const Type *convert_type(const char *type);
57
c219e86 don't slow ivars used inside an eval string
Laurent Sansonetti authored
58 bool is_inside_eval(void) { return inside_eval; }
59 void set_inside_eval(bool flag) { inside_eval = flag; }
60
cb65416 the great schism, part I
Laurent Sansonetti authored
61 protected:
62 const char *fname;
c219e86 don't slow ivars used inside an eval string
Laurent Sansonetti authored
63 bool inside_eval;
cb65416 the great schism, part I
Laurent Sansonetti authored
64
65 std::map<ID, Value *> lvars;
66 std::vector<ID> dvars;
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
67 std::map<ID, Instruction *> ivar_slots_cache;
cb65416 the great schism, part I
Laurent Sansonetti authored
68 std::map<std::string, GlobalVariable *> static_strings;
e379686 some work on unicode support (still in progress)
Laurent Sansonetti authored
69 std::map<CFHashCode, GlobalVariable *> static_ustrings;
cb65416 the great schism, part I
Laurent Sansonetti authored
70
71 #if ROXOR_COMPILER_DEBUG
72 int level;
73 # define DEBUG_LEVEL_INC() (level++)
74 # define DEBUG_LEVEL_DEC() (level--)
75 #else
76 # define DEBUG_LEVEL_INC()
77 # define DEBUG_LEVEL_DEC()
78 #endif
79
80 BasicBlock *bb;
81 BasicBlock *entry_bb;
82 ID current_mid;
164ca93 fixed super within a method that has a splat argument
Laurent Sansonetti authored
83 rb_vm_arity_t current_arity;
cb65416 the great schism, part I
Laurent Sansonetti authored
84 bool current_instance_method;
85 ID self_id;
86 Value *current_self;
87 bool current_block;
1961daa better fix than last one
Laurent Sansonetti authored
88 bool current_block_chain;
cb65416 the great schism, part I
Laurent Sansonetti authored
89 Value *current_var_uses;
90 Value *running_block;
91 BasicBlock *begin_bb;
92 BasicBlock *rescue_bb;
93 BasicBlock *ensure_bb;
94 bool current_rescue;
95 NODE *current_block_node;
96 Function *current_block_func;
97 GlobalVariable *current_opened_class;
98 bool current_module;
99 BasicBlock *current_loop_begin_bb;
100 BasicBlock *current_loop_body_bb;
101 BasicBlock *current_loop_end_bb;
102 Value *current_loop_exit_val;
3432fd1 a better exception-based implementation for return-from-block
Laurent Sansonetti authored
103 int return_from_block;
104 int return_from_block_ids;
cb65416 the great schism, part I
Laurent Sansonetti authored
105
106 Function *dispatcherFunc;
38456a4 most fast ops in place
Laurent Sansonetti authored
107 Function *fastPlusFunc;
108 Function *fastMinusFunc;
109 Function *fastMultFunc;
110 Function *fastDivFunc;
111 Function *fastLtFunc;
112 Function *fastLeFunc;
113 Function *fastGtFunc;
114 Function *fastGeFunc;
944d624 optimize #== and #!= for non fixnum/float types
Laurent Sansonetti authored
115 Function *fastEqFunc;
116 Function *fastNeqFunc;
cb65416 the great schism, part I
Laurent Sansonetti authored
117 Function *fastEqqFunc;
118 Function *whenSplatFunc;
119 Function *prepareBlockFunc;
120 Function *pushBindingFunc;
121 Function *getBlockFunc;
122 Function *currentBlockObjectFunc;
123 Function *getConstFunc;
124 Function *setConstFunc;
125 Function *prepareMethodFunc;
126 Function *singletonClassFunc;
127 Function *defineClassFunc;
128 Function *prepareIvarSlotFunc;
129 Function *getIvarFunc;
130 Function *setIvarFunc;
131 Function *definedFunc;
132 Function *undefFunc;
133 Function *aliasFunc;
134 Function *valiasFunc;
135 Function *newHashFunc;
136 Function *toAFunc;
137 Function *toAryFunc;
138 Function *catArrayFunc;
139 Function *dupArrayFunc;
140 Function *newArrayFunc;
141 Function *newStructFunc;
142 Function *newOpaqueFunc;
ff75d60 added support for overwriting an ObjC method that accepts a pointer
Laurent Sansonetti authored
143 Function *newPointerFunc;
cb65416 the great schism, part I
Laurent Sansonetti authored
144 Function *getStructFieldsFunc;
145 Function *getOpaqueDataFunc;
146 Function *getPointerPtrFunc;
147 Function *checkArityFunc;
148 Function *setStructFunc;
149 Function *newRangeFunc;
150 Function *newRegexpFunc;
151 Function *strInternFunc;
152 Function *keepVarsFunc;
153 Function *masgnGetElemBeforeSplatFunc;
154 Function *masgnGetElemAfterSplatFunc;
155 Function *masgnGetSplatFunc;
156 Function *newStringFunc;
913687b fixed a bug in the way we compile literal strings
Laurent Sansonetti authored
157 Function *newString2Func;
158 Function *newString3Func;
cb65416 the great schism, part I
Laurent Sansonetti authored
159 Function *yieldFunc;
208a965 a quick implementation for #define_method (not yet 100% complete)
Laurent Sansonetti authored
160 Function *blockEvalFunc;
cb65416 the great schism, part I
Laurent Sansonetti authored
161 Function *gvarSetFunc;
162 Function *gvarGetFunc;
163 Function *cvarSetFunc;
164 Function *cvarGetFunc;
165 Function *currentExceptionFunc;
166 Function *popExceptionFunc;
167 Function *getSpecialFunc;
168 Function *breakFunc;
6c5b476 a new implementation for return-from-block which uses an exception inste...
Laurent Sansonetti authored
169 Function *returnFromBlockFunc;
3432fd1 a better exception-based implementation for return-from-block
Laurent Sansonetti authored
170 Function *checkReturnFromBlockFunc;
cb65416 the great schism, part I
Laurent Sansonetti authored
171 Function *longjmpFunc;
172 Function *setjmpFunc;
173 Function *popBrokenValue;
174
175 Constant *zeroVal;
176 Constant *oneVal;
177 Constant *twoVal;
01528a7 floating point arithmetic optimization: Float objects are now immediate ...
Laurent Sansonetti authored
178 Constant *threeVal;
cb65416 the great schism, part I
Laurent Sansonetti authored
179 Constant *nilVal;
180 Constant *trueVal;
181 Constant *falseVal;
182 Constant *undefVal;
183 Constant *splatArgFollowsVal;
184 Constant *cObject;
185 const Type *RubyObjTy;
186 const Type *RubyObjPtrTy;
187 const Type *RubyObjPtrPtrTy;
188 const Type *PtrTy;
189 const Type *PtrPtrTy;
190 const Type *IntTy;
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
191 const Type *Int32PtrTy;
cb65416 the great schism, part I
Laurent Sansonetti authored
192
193 void compile_node_error(const char *msg, NODE *node);
194
195 virtual Instruction *
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
196 compile_const_pointer(void *ptr, const Type *type=NULL,
197 bool insert_to_bb=true) {
cb65416 the great schism, part I
Laurent Sansonetti authored
198 Value *ptrint = ConstantInt::get(IntTy, (long)ptr);
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
199 if (type == NULL) {
200 type = PtrTy;
201 }
cb65416 the great schism, part I
Laurent Sansonetti authored
202 return insert_to_bb
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
203 ? new IntToPtrInst(ptrint, type, "", bb)
204 : new IntToPtrInst(ptrint, type, "");
cb65416 the great schism, part I
Laurent Sansonetti authored
205 }
206
207 Instruction *
208 compile_const_pointer_to_pointer(void *ptr, bool insert_to_bb=true) {
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
209 return compile_const_pointer(ptr, PtrPtrTy, insert_to_bb);
cb65416 the great schism, part I
Laurent Sansonetti authored
210 }
211
212 Value *compile_protected_call(Function *func,
213 std::vector<Value *> &params);
214 void compile_dispatch_arguments(NODE *args,
215 std::vector<Value *> &arguments, int *pargc);
216 Function::ArgumentListType::iterator compile_optional_arguments(
217 Function::ArgumentListType::iterator iter, NODE *node);
218 void compile_boolean_test(Value *condVal, BasicBlock *ifTrueBB,
219 BasicBlock *ifFalseBB);
220 void compile_when_arguments(NODE *args, Value *comparedToVal,
221 BasicBlock *thenBB);
222 void compile_single_when_argument(NODE *arg, Value *comparedToVal,
223 BasicBlock *thenBB);
224 virtual void compile_prepare_method(Value *classVal, Value *sel,
225 Function *new_function, rb_vm_arity_t &arity, NODE *body);
226 Value *compile_dispatch_call(std::vector<Value *> &params);
227 Value *compile_when_splat(Value *comparedToVal, Value *splatVal);
944d624 optimize #== and #!= for non fixnum/float types
Laurent Sansonetti authored
228 Value *compile_fast_op_call(SEL sel, Value *selfVal, Value *comparedToVal);
cb65416 the great schism, part I
Laurent Sansonetti authored
229 Value *compile_attribute_assign(NODE *node, Value *extra_val);
2c54dbd AOT compiler: added support for blocks
Laurent Sansonetti authored
230 virtual Value *compile_prepare_block_args(Function *func, int *flags);
231 Value *compile_block_create(NODE *node);
cb65416 the great schism, part I
Laurent Sansonetti authored
232 Value *compile_binding(void);
233 Value *compile_optimized_dispatch_call(SEL sel, int argc,
234 std::vector<Value *> &params);
235 Value *compile_ivar_read(ID vid);
236 Value *compile_ivar_assignment(ID vid, Value *val);
237 Value *compile_cvar_assignment(ID vid, Value *val);
eeb7a10 added support for @@foo||=42, a lot of changes for a very stupid functio...
Laurent Sansonetti authored
238 Value *compile_cvar_get(ID vid, bool check);
42e4a2a AOT compiler: added support for global variables
Laurent Sansonetti authored
239 Value *compile_gvar_assignment(NODE *node, Value *val);
cb65416 the great schism, part I
Laurent Sansonetti authored
240 Value *compile_constant_declaration(NODE *node, Value *val);
241 Value *compile_multiple_assignment(NODE *node, Value *val);
242 void compile_multiple_assignment_element(NODE *node, Value *val);
243 Value *compile_current_class(void);
57d8085 AOT compiler: added support for NSObject and ID compilation
Laurent Sansonetti authored
244 virtual Value *compile_nsobject(void);
aca37ff fixed const lookup inside a module defined under an explicit (using ::) ...
Laurent Sansonetti authored
245 Value *compile_class_path(NODE *node, bool *outer);
cb65416 the great schism, part I
Laurent Sansonetti authored
246 Value *compile_const(ID id, Value *outer);
247 Value *compile_singleton_class(Value *obj);
248 Value *compile_defined_expression(NODE *node);
249 Value *compile_dstr(NODE *node);
250 Value *compile_dvar_slot(ID name);
251 void compile_break_val(Value *val);
3432fd1 a better exception-based implementation for return-from-block
Laurent Sansonetti authored
252 void compile_return_from_block(Value *val, int id);
253 void compile_return_from_block_handler(int id);
cb65416 the great schism, part I
Laurent Sansonetti authored
254 Value *compile_jump(NODE *node);
255 virtual Value *compile_mcache(SEL sel, bool super);
256 virtual Value *compile_ccache(ID id);
257 virtual Instruction *compile_sel(SEL sel, bool add_to_bb=true) {
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
258 return compile_const_pointer(sel, PtrTy, add_to_bb);
cb65416 the great schism, part I
Laurent Sansonetti authored
259 }
57d8085 AOT compiler: added support for NSObject and ID compilation
Laurent Sansonetti authored
260 virtual Value *compile_id(ID id);
913687b fixed a bug in the way we compile literal strings
Laurent Sansonetti authored
261 GlobalVariable *compile_const_global_string(const char *str,
262 const size_t str_len);
263 GlobalVariable *compile_const_global_string(const char *str) {
264 return compile_const_global_string(str, strlen(str));
265 }
e379686 some work on unicode support (still in progress)
Laurent Sansonetti authored
266 GlobalVariable *compile_const_global_ustring(const UniChar *str,
267 const size_t str_len, CFHashCode hash);
268
2c54dbd AOT compiler: added support for blocks
Laurent Sansonetti authored
269 Value *compile_arity(rb_vm_arity_t &arity);
913687b fixed a bug in the way we compile literal strings
Laurent Sansonetti authored
270 Value *compile_literal(VALUE val);
907bd81 AOT compiler: added support for some immutable literals
Laurent Sansonetti authored
271 virtual Value *compile_immutable_literal(VALUE val);
42e4a2a AOT compiler: added support for global variables
Laurent Sansonetti authored
272 virtual Value *compile_global_entry(NODE *node);
cb65416 the great schism, part I
Laurent Sansonetti authored
273
3432fd1 a better exception-based implementation for return-from-block
Laurent Sansonetti authored
274 Value *compile_landing_pad_header(void);
275 Value *compile_landing_pad_header(const std::type_info &eh_type);
6c5b476 a new implementation for return-from-block which uses an exception inste...
Laurent Sansonetti authored
276 void compile_landing_pad_footer(bool pop_exception=true);
cb65416 the great schism, part I
Laurent Sansonetti authored
277 void compile_rethrow_exception(void);
278 void compile_pop_exception(void);
279 Value *compile_lvar_slot(ID name);
280 bool compile_lvars(ID *tbl);
281 Value *compile_new_struct(Value *klass, std::vector<Value *> &fields);
282 Value *compile_new_opaque(Value *klass, Value *val);
ff75d60 added support for overwriting an ObjC method that accepts a pointer
Laurent Sansonetti authored
283 Value *compile_new_pointer(const char *type, Value *val);
cb65416 the great schism, part I
Laurent Sansonetti authored
284 void compile_get_struct_fields(Value *val, Value *buf,
285 rb_vm_bs_boxed_t *bs_boxed);
286 Value *compile_get_opaque_data(Value *val, rb_vm_bs_boxed_t *bs_boxed,
287 Value *slot);
288 Value *compile_get_cptr(Value *val, const char *type, Value *slot);
289 void compile_check_arity(Value *given, Value *requested);
290 void compile_set_struct(Value *rcv, int field, Value *val);
291
292 Value *compile_conversion_to_c(const char *type, Value *val,
293 Value *slot);
294 Value *compile_conversion_to_ruby(const char *type,
295 const Type *llvm_type, Value *val);
296
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
297 Instruction *compile_slot_cache(ID id);
298 virtual Instruction *gen_slot_cache(ID id);
cb65416 the great schism, part I
Laurent Sansonetti authored
299 ICmpInst *is_value_a_fixnum(Value *val);
300 void compile_ivar_slots(Value *klass, BasicBlock::InstListType &list,
301 BasicBlock::InstListType::iterator iter);
302 bool unbox_ruby_constant(Value *val, VALUE *rval);
01528a7 floating point arithmetic optimization: Float objects are now immediate ...
Laurent Sansonetti authored
303 Value *optimized_immediate_op(SEL sel, Value *leftVal, Value *rightVal,
304 bool float_op, bool *is_predicate);
305 Value *compile_double_coercion(Value *val, Value *mask,
306 BasicBlock *fallback_bb, Function *f);
2a1240c @vincentisambart some refactoring
vincentisambart authored
307 void compile_keep_vars(BasicBlock *startBB, BasicBlock *mergeBB);
01528a7 floating point arithmetic optimization: Float objects are now immediate ...
Laurent Sansonetti authored
308
cb65416 the great schism, part I
Laurent Sansonetti authored
309 SEL mid_to_sel(ID mid, int arity);
310 };
311
312 class RoxorAOTCompiler : public RoxorCompiler {
313 public:
3432fd1 a better exception-based implementation for return-from-block
Laurent Sansonetti authored
314 RoxorAOTCompiler(void);
cb65416 the great schism, part I
Laurent Sansonetti authored
315
316 Function *compile_main_function(NODE *node);
317
318 private:
319 std::map<SEL, GlobalVariable *> mcaches;
320 std::map<ID, GlobalVariable *> ccaches;
321 std::map<SEL, GlobalVariable *> sels;
57d8085 AOT compiler: added support for NSObject and ID compilation
Laurent Sansonetti authored
322 std::map<ID, GlobalVariable *> ids;
42e4a2a AOT compiler: added support for global variables
Laurent Sansonetti authored
323 std::map<ID, GlobalVariable *> global_entries;
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
324 std::vector<GlobalVariable *> ivar_slots;
907bd81 AOT compiler: added support for some immutable literals
Laurent Sansonetti authored
325 std::map<VALUE, GlobalVariable *> literals;
326
57d8085 AOT compiler: added support for NSObject and ID compilation
Laurent Sansonetti authored
327 GlobalVariable *cObject_gvar;
cb65416 the great schism, part I
Laurent Sansonetti authored
328
907bd81 AOT compiler: added support for some immutable literals
Laurent Sansonetti authored
329 Function *name2symFunc;
330
cb65416 the great schism, part I
Laurent Sansonetti authored
331 Value *compile_mcache(SEL sel, bool super);
332 Value *compile_ccache(ID id);
333 Instruction *compile_sel(SEL sel, bool add_to_bb=true);
334 void compile_prepare_method(Value *classVal, Value *sel,
335 Function *new_function, rb_vm_arity_t &arity, NODE *body);
2c54dbd AOT compiler: added support for blocks
Laurent Sansonetti authored
336 Value *compile_prepare_block_args(Function *func, int *flags);
57d8085 AOT compiler: added support for NSObject and ID compilation
Laurent Sansonetti authored
337 Value *compile_nsobject(void);
338 Value *compile_id(ID id);
907bd81 AOT compiler: added support for some immutable literals
Laurent Sansonetti authored
339 Value *compile_immutable_literal(VALUE val);
42e4a2a AOT compiler: added support for global variables
Laurent Sansonetti authored
340 Value *compile_global_entry(NODE *node);
cb65416 the great schism, part I
Laurent Sansonetti authored
341
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
342 Instruction *gen_slot_cache(ID id);
343
cb65416 the great schism, part I
Laurent Sansonetti authored
344 Instruction *
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
345 compile_const_pointer(void *ptr, const Type *type=NULL,
346 bool insert_to_bb=true) {
cb65416 the great schism, part I
Laurent Sansonetti authored
347 if (ptr == NULL) {
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
348 return RoxorCompiler::compile_const_pointer(ptr, type,
349 insert_to_bb);
cb65416 the great schism, part I
Laurent Sansonetti authored
350 }
351 printf("compile_const_pointer() called with a non-NULL pointer " \
352 "on the AOT compiler - leaving the ship!\n");
353 abort();
354 }
355 };
356
357 #endif /* __cplusplus */
358
359 #endif /* __COMPILER_H_ */
Something went wrong with that request. Please try again.