Skip to content
This repository
Newer
Older
100644 346 lines (310 sloc) 11.034 kb
cfd7b4d8 » Laurent Sansonetti
2009-05-23 added licensing information on the new files
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
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
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
3432fd19 » Laurent Sansonetti
2009-07-10 a better exception-based implementation for return-from-block
33 RoxorCompiler(void);
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
34 virtual ~RoxorCompiler(void) { }
35
3432fd19 » Laurent Sansonetti
2009-07-10 a better exception-based implementation for return-from-block
36 void set_fname(const char *_fname) {
37 fname = _fname;
38 }
39
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
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);
c1c347fc » Laurent Sansonetti
2009-07-12 AOT compiler: now supporting objc stubs and super dispatch
52 Function *compile_objc_stub(Function *ruby_func, IMP ruby_imp,
53 const rb_vm_arity_t &arity, const char *types);
208a965e » Laurent Sansonetti
2009-06-15 a quick implementation for #define_method (not yet 100% complete)
54 Function *compile_block_caller(rb_vm_block_t *block);
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
55
56 const Type *convert_type(const char *type);
57
c219e864 » Laurent Sansonetti
2009-06-19 don't slow ivars used inside an eval string
58 bool is_inside_eval(void) { return inside_eval; }
59 void set_inside_eval(bool flag) { inside_eval = flag; }
60
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
61 protected:
62 const char *fname;
c219e864 » Laurent Sansonetti
2009-06-19 don't slow ivars used inside an eval string
63 bool inside_eval;
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
64
65 std::map<ID, Value *> lvars;
66 std::vector<ID> dvars;
85b7cc25 » Laurent Sansonetti
2009-07-02 AOT compiler: ivar slots support
67 std::map<ID, Instruction *> ivar_slots_cache;
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
68 std::map<std::string, GlobalVariable *> static_strings;
e3796866 » Laurent Sansonetti
2009-07-20 some work on unicode support (still in progress)
69 std::map<CFHashCode, GlobalVariable *> static_ustrings;
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
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;
83 bool current_instance_method;
84 ID self_id;
85 Value *current_self;
86 bool current_block;
1961daae » Laurent Sansonetti
2009-07-29 better fix than last one
87 bool current_block_chain;
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
88 Value *current_var_uses;
89 Value *running_block;
90 BasicBlock *begin_bb;
91 BasicBlock *rescue_bb;
92 BasicBlock *ensure_bb;
93 bool current_rescue;
94 NODE *current_block_node;
95 Function *current_block_func;
96 GlobalVariable *current_opened_class;
97 bool current_module;
98 BasicBlock *current_loop_begin_bb;
99 BasicBlock *current_loop_body_bb;
100 BasicBlock *current_loop_end_bb;
101 Value *current_loop_exit_val;
3432fd19 » Laurent Sansonetti
2009-07-10 a better exception-based implementation for return-from-block
102 int return_from_block;
103 int return_from_block_ids;
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
104
105 Function *dispatcherFunc;
106 Function *fastEqqFunc;
107 Function *whenSplatFunc;
108 Function *prepareBlockFunc;
109 Function *pushBindingFunc;
110 Function *getBlockFunc;
111 Function *currentBlockObjectFunc;
112 Function *getConstFunc;
113 Function *setConstFunc;
114 Function *prepareMethodFunc;
115 Function *singletonClassFunc;
116 Function *defineClassFunc;
117 Function *prepareIvarSlotFunc;
118 Function *getIvarFunc;
119 Function *setIvarFunc;
120 Function *definedFunc;
121 Function *undefFunc;
122 Function *aliasFunc;
123 Function *valiasFunc;
124 Function *newHashFunc;
125 Function *toAFunc;
126 Function *toAryFunc;
127 Function *catArrayFunc;
128 Function *dupArrayFunc;
129 Function *newArrayFunc;
130 Function *newStructFunc;
131 Function *newOpaqueFunc;
ff75d601 » Laurent Sansonetti
2009-06-12 added support for overwriting an ObjC method that accepts a pointer
132 Function *newPointerFunc;
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
133 Function *getStructFieldsFunc;
134 Function *getOpaqueDataFunc;
135 Function *getPointerPtrFunc;
136 Function *checkArityFunc;
137 Function *setStructFunc;
138 Function *newRangeFunc;
139 Function *newRegexpFunc;
140 Function *strInternFunc;
141 Function *keepVarsFunc;
142 Function *masgnGetElemBeforeSplatFunc;
143 Function *masgnGetElemAfterSplatFunc;
144 Function *masgnGetSplatFunc;
145 Function *newStringFunc;
913687b8 » Laurent Sansonetti
2009-07-02 fixed a bug in the way we compile literal strings
146 Function *newString2Func;
147 Function *newString3Func;
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
148 Function *yieldFunc;
208a965e » Laurent Sansonetti
2009-06-15 a quick implementation for #define_method (not yet 100% complete)
149 Function *blockEvalFunc;
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
150 Function *gvarSetFunc;
151 Function *gvarGetFunc;
152 Function *cvarSetFunc;
153 Function *cvarGetFunc;
154 Function *currentExceptionFunc;
155 Function *popExceptionFunc;
156 Function *getSpecialFunc;
157 Function *breakFunc;
6c5b476f » Laurent Sansonetti
2009-07-10 a new implementation for return-from-block which uses an exception in…
158 Function *returnFromBlockFunc;
3432fd19 » Laurent Sansonetti
2009-07-10 a better exception-based implementation for return-from-block
159 Function *checkReturnFromBlockFunc;
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
160 Function *longjmpFunc;
161 Function *setjmpFunc;
162 Function *popBrokenValue;
163
164 Constant *zeroVal;
165 Constant *oneVal;
166 Constant *twoVal;
01528a72 » Laurent Sansonetti
2009-06-18 floating point arithmetic optimization: Float objects are now immedia…
167 Constant *threeVal;
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
168 Constant *nilVal;
169 Constant *trueVal;
170 Constant *falseVal;
171 Constant *undefVal;
172 Constant *splatArgFollowsVal;
173 Constant *cObject;
174 const Type *RubyObjTy;
175 const Type *RubyObjPtrTy;
176 const Type *RubyObjPtrPtrTy;
177 const Type *PtrTy;
178 const Type *PtrPtrTy;
179 const Type *IntTy;
85b7cc25 » Laurent Sansonetti
2009-07-02 AOT compiler: ivar slots support
180 const Type *Int32PtrTy;
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
181
182 void compile_node_error(const char *msg, NODE *node);
183
184 virtual Instruction *
85b7cc25 » Laurent Sansonetti
2009-07-02 AOT compiler: ivar slots support
185 compile_const_pointer(void *ptr, const Type *type=NULL,
186 bool insert_to_bb=true) {
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
187 Value *ptrint = ConstantInt::get(IntTy, (long)ptr);
85b7cc25 » Laurent Sansonetti
2009-07-02 AOT compiler: ivar slots support
188 if (type == NULL) {
189 type = PtrTy;
190 }
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
191 return insert_to_bb
85b7cc25 » Laurent Sansonetti
2009-07-02 AOT compiler: ivar slots support
192 ? new IntToPtrInst(ptrint, type, "", bb)
193 : new IntToPtrInst(ptrint, type, "");
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
194 }
195
196 Instruction *
197 compile_const_pointer_to_pointer(void *ptr, bool insert_to_bb=true) {
85b7cc25 » Laurent Sansonetti
2009-07-02 AOT compiler: ivar slots support
198 return compile_const_pointer(ptr, PtrPtrTy, insert_to_bb);
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
199 }
200
201 Value *compile_protected_call(Function *func,
202 std::vector<Value *> &params);
203 void compile_dispatch_arguments(NODE *args,
204 std::vector<Value *> &arguments, int *pargc);
205 Function::ArgumentListType::iterator compile_optional_arguments(
206 Function::ArgumentListType::iterator iter, NODE *node);
207 void compile_boolean_test(Value *condVal, BasicBlock *ifTrueBB,
208 BasicBlock *ifFalseBB);
209 void compile_when_arguments(NODE *args, Value *comparedToVal,
210 BasicBlock *thenBB);
211 void compile_single_when_argument(NODE *arg, Value *comparedToVal,
212 BasicBlock *thenBB);
213 virtual void compile_prepare_method(Value *classVal, Value *sel,
214 Function *new_function, rb_vm_arity_t &arity, NODE *body);
215 Value *compile_dispatch_call(std::vector<Value *> &params);
216 Value *compile_when_splat(Value *comparedToVal, Value *splatVal);
217 Value *compile_fast_eqq_call(Value *selfVal, Value *comparedToVal);
218 Value *compile_attribute_assign(NODE *node, Value *extra_val);
2c54dbd2 » Laurent Sansonetti
2009-07-01 AOT compiler: added support for blocks
219 virtual Value *compile_prepare_block_args(Function *func, int *flags);
220 Value *compile_block_create(NODE *node);
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
221 Value *compile_binding(void);
222 Value *compile_optimized_dispatch_call(SEL sel, int argc,
223 std::vector<Value *> &params);
224 Value *compile_ivar_read(ID vid);
225 Value *compile_ivar_assignment(ID vid, Value *val);
226 Value *compile_cvar_assignment(ID vid, Value *val);
42e4a2a2 » Laurent Sansonetti
2009-07-03 AOT compiler: added support for global variables
227 Value *compile_gvar_assignment(NODE *node, Value *val);
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
228 Value *compile_constant_declaration(NODE *node, Value *val);
229 Value *compile_multiple_assignment(NODE *node, Value *val);
230 void compile_multiple_assignment_element(NODE *node, Value *val);
231 Value *compile_current_class(void);
57d80850 » Laurent Sansonetti
2009-06-30 AOT compiler: added support for NSObject and ID compilation
232 virtual Value *compile_nsobject(void);
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
233 Value *compile_class_path(NODE *node);
234 Value *compile_const(ID id, Value *outer);
235 Value *compile_singleton_class(Value *obj);
236 Value *compile_defined_expression(NODE *node);
237 Value *compile_dstr(NODE *node);
238 Value *compile_dvar_slot(ID name);
239 void compile_break_val(Value *val);
3432fd19 » Laurent Sansonetti
2009-07-10 a better exception-based implementation for return-from-block
240 void compile_return_from_block(Value *val, int id);
241 void compile_return_from_block_handler(int id);
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
242 Value *compile_jump(NODE *node);
243 virtual Value *compile_mcache(SEL sel, bool super);
244 virtual Value *compile_ccache(ID id);
245 virtual Instruction *compile_sel(SEL sel, bool add_to_bb=true) {
85b7cc25 » Laurent Sansonetti
2009-07-02 AOT compiler: ivar slots support
246 return compile_const_pointer(sel, PtrTy, add_to_bb);
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
247 }
57d80850 » Laurent Sansonetti
2009-06-30 AOT compiler: added support for NSObject and ID compilation
248 virtual Value *compile_id(ID id);
913687b8 » Laurent Sansonetti
2009-07-02 fixed a bug in the way we compile literal strings
249 GlobalVariable *compile_const_global_string(const char *str,
250 const size_t str_len);
251 GlobalVariable *compile_const_global_string(const char *str) {
252 return compile_const_global_string(str, strlen(str));
253 }
e3796866 » Laurent Sansonetti
2009-07-20 some work on unicode support (still in progress)
254 GlobalVariable *compile_const_global_ustring(const UniChar *str,
255 const size_t str_len, CFHashCode hash);
256
2c54dbd2 » Laurent Sansonetti
2009-07-01 AOT compiler: added support for blocks
257 Value *compile_arity(rb_vm_arity_t &arity);
913687b8 » Laurent Sansonetti
2009-07-02 fixed a bug in the way we compile literal strings
258 Value *compile_literal(VALUE val);
907bd816 » Laurent Sansonetti
2009-07-04 AOT compiler: added support for some immutable literals
259 virtual Value *compile_immutable_literal(VALUE val);
42e4a2a2 » Laurent Sansonetti
2009-07-03 AOT compiler: added support for global variables
260 virtual Value *compile_global_entry(NODE *node);
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
261
3432fd19 » Laurent Sansonetti
2009-07-10 a better exception-based implementation for return-from-block
262 Value *compile_landing_pad_header(void);
263 Value *compile_landing_pad_header(const std::type_info &eh_type);
6c5b476f » Laurent Sansonetti
2009-07-10 a new implementation for return-from-block which uses an exception in…
264 void compile_landing_pad_footer(bool pop_exception=true);
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
265 void compile_rethrow_exception(void);
266 void compile_pop_exception(void);
267 Value *compile_lvar_slot(ID name);
268 bool compile_lvars(ID *tbl);
269 Value *compile_new_struct(Value *klass, std::vector<Value *> &fields);
270 Value *compile_new_opaque(Value *klass, Value *val);
ff75d601 » Laurent Sansonetti
2009-06-12 added support for overwriting an ObjC method that accepts a pointer
271 Value *compile_new_pointer(const char *type, Value *val);
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
272 void compile_get_struct_fields(Value *val, Value *buf,
273 rb_vm_bs_boxed_t *bs_boxed);
274 Value *compile_get_opaque_data(Value *val, rb_vm_bs_boxed_t *bs_boxed,
275 Value *slot);
276 Value *compile_get_cptr(Value *val, const char *type, Value *slot);
277 void compile_check_arity(Value *given, Value *requested);
278 void compile_set_struct(Value *rcv, int field, Value *val);
279
280 Value *compile_conversion_to_c(const char *type, Value *val,
281 Value *slot);
282 Value *compile_conversion_to_ruby(const char *type,
283 const Type *llvm_type, Value *val);
284
85b7cc25 » Laurent Sansonetti
2009-07-02 AOT compiler: ivar slots support
285 Instruction *compile_slot_cache(ID id);
286 virtual Instruction *gen_slot_cache(ID id);
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
287 ICmpInst *is_value_a_fixnum(Value *val);
288 void compile_ivar_slots(Value *klass, BasicBlock::InstListType &list,
289 BasicBlock::InstListType::iterator iter);
290 bool unbox_ruby_constant(Value *val, VALUE *rval);
01528a72 » Laurent Sansonetti
2009-06-18 floating point arithmetic optimization: Float objects are now immedia…
291 Value *optimized_immediate_op(SEL sel, Value *leftVal, Value *rightVal,
292 bool float_op, bool *is_predicate);
293 Value *compile_double_coercion(Value *val, Value *mask,
294 BasicBlock *fallback_bb, Function *f);
295
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
296 SEL mid_to_sel(ID mid, int arity);
297 };
298
299 class RoxorAOTCompiler : public RoxorCompiler {
300 public:
3432fd19 » Laurent Sansonetti
2009-07-10 a better exception-based implementation for return-from-block
301 RoxorAOTCompiler(void);
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
302
303 Function *compile_main_function(NODE *node);
304
305 private:
306 std::map<SEL, GlobalVariable *> mcaches;
307 std::map<ID, GlobalVariable *> ccaches;
308 std::map<SEL, GlobalVariable *> sels;
57d80850 » Laurent Sansonetti
2009-06-30 AOT compiler: added support for NSObject and ID compilation
309 std::map<ID, GlobalVariable *> ids;
42e4a2a2 » Laurent Sansonetti
2009-07-03 AOT compiler: added support for global variables
310 std::map<ID, GlobalVariable *> global_entries;
85b7cc25 » Laurent Sansonetti
2009-07-02 AOT compiler: ivar slots support
311 std::vector<GlobalVariable *> ivar_slots;
907bd816 » Laurent Sansonetti
2009-07-04 AOT compiler: added support for some immutable literals
312 std::map<VALUE, GlobalVariable *> literals;
313
57d80850 » Laurent Sansonetti
2009-06-30 AOT compiler: added support for NSObject and ID compilation
314 GlobalVariable *cObject_gvar;
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
315
907bd816 » Laurent Sansonetti
2009-07-04 AOT compiler: added support for some immutable literals
316 Function *name2symFunc;
317
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
318 Value *compile_mcache(SEL sel, bool super);
319 Value *compile_ccache(ID id);
320 Instruction *compile_sel(SEL sel, bool add_to_bb=true);
321 void compile_prepare_method(Value *classVal, Value *sel,
322 Function *new_function, rb_vm_arity_t &arity, NODE *body);
2c54dbd2 » Laurent Sansonetti
2009-07-01 AOT compiler: added support for blocks
323 Value *compile_prepare_block_args(Function *func, int *flags);
57d80850 » Laurent Sansonetti
2009-06-30 AOT compiler: added support for NSObject and ID compilation
324 Value *compile_nsobject(void);
325 Value *compile_id(ID id);
907bd816 » Laurent Sansonetti
2009-07-04 AOT compiler: added support for some immutable literals
326 Value *compile_immutable_literal(VALUE val);
42e4a2a2 » Laurent Sansonetti
2009-07-03 AOT compiler: added support for global variables
327 Value *compile_global_entry(NODE *node);
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
328
85b7cc25 » Laurent Sansonetti
2009-07-02 AOT compiler: ivar slots support
329 Instruction *gen_slot_cache(ID id);
330
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
331 Instruction *
85b7cc25 » Laurent Sansonetti
2009-07-02 AOT compiler: ivar slots support
332 compile_const_pointer(void *ptr, const Type *type=NULL,
333 bool insert_to_bb=true) {
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
334 if (ptr == NULL) {
85b7cc25 » Laurent Sansonetti
2009-07-02 AOT compiler: ivar slots support
335 return RoxorCompiler::compile_const_pointer(ptr, type,
336 insert_to_bb);
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
337 }
338 printf("compile_const_pointer() called with a non-NULL pointer " \
339 "on the AOT compiler - leaving the ship!\n");
340 abort();
341 }
342 };
343
344 #endif /* __cplusplus */
345
346 #endif /* __COMPILER_H_ */
Something went wrong with that request. Please try again.