Skip to content
Newer
Older
100644 315 lines (282 sloc) 9.72 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
33 RoxorCompiler(const char *fname);
34 virtual ~RoxorCompiler(void) { }
35
36 Value *compile_node(NODE *node);
37
38 virtual Function *compile_main_function(NODE *node);
39 Function *compile_read_attr(ID name);
40 Function *compile_write_attr(ID name);
41 Function *compile_stub(const char *types, int argc, bool is_objc);
42 Function *compile_bs_struct_new(rb_vm_bs_boxed_t *bs_boxed);
43 Function *compile_bs_struct_writer(rb_vm_bs_boxed_t *bs_boxed,
44 int field);
45 Function *compile_ffi_function(void *stub, void *imp, int argc);
46 Function *compile_to_rval_convertor(const char *type);
47 Function *compile_to_ocval_convertor(const char *type);
48 Function *compile_objc_stub(Function *ruby_func, const char *types);
208a965 a quick implementation for #define_method (not yet 100% complete)
Laurent Sansonetti authored
49 Function *compile_block_caller(rb_vm_block_t *block);
cb65416 the great schism, part I
Laurent Sansonetti authored
50
51 const Type *convert_type(const char *type);
52
c219e86 don't slow ivars used inside an eval string
Laurent Sansonetti authored
53 bool is_inside_eval(void) { return inside_eval; }
54 void set_inside_eval(bool flag) { inside_eval = flag; }
55
cb65416 the great schism, part I
Laurent Sansonetti authored
56 protected:
57 const char *fname;
c219e86 don't slow ivars used inside an eval string
Laurent Sansonetti authored
58 bool inside_eval;
cb65416 the great schism, part I
Laurent Sansonetti authored
59
60 std::map<ID, Value *> lvars;
61 std::vector<ID> dvars;
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
62 std::map<ID, Instruction *> ivar_slots_cache;
cb65416 the great schism, part I
Laurent Sansonetti authored
63 std::map<std::string, GlobalVariable *> static_strings;
64
65 #if ROXOR_COMPILER_DEBUG
66 int level;
67 # define DEBUG_LEVEL_INC() (level++)
68 # define DEBUG_LEVEL_DEC() (level--)
69 #else
70 # define DEBUG_LEVEL_INC()
71 # define DEBUG_LEVEL_DEC()
72 #endif
73
74 BasicBlock *bb;
75 BasicBlock *entry_bb;
76 ID current_mid;
77 bool current_instance_method;
78 ID self_id;
79 Value *current_self;
80 bool current_block;
81 Value *current_var_uses;
82 Value *running_block;
83 BasicBlock *begin_bb;
84 BasicBlock *rescue_bb;
85 BasicBlock *ensure_bb;
86 bool current_rescue;
87 NODE *current_block_node;
88 Function *current_block_func;
89 jmp_buf *return_from_block_jmpbuf;
90 GlobalVariable *current_opened_class;
91 bool current_module;
92 BasicBlock *current_loop_begin_bb;
93 BasicBlock *current_loop_body_bb;
94 BasicBlock *current_loop_end_bb;
95 Value *current_loop_exit_val;
96
97 Function *dispatcherFunc;
98 Function *fastEqqFunc;
99 Function *whenSplatFunc;
100 Function *prepareBlockFunc;
101 Function *pushBindingFunc;
102 Function *getBlockFunc;
103 Function *currentBlockObjectFunc;
104 Function *getConstFunc;
105 Function *setConstFunc;
106 Function *prepareMethodFunc;
107 Function *singletonClassFunc;
108 Function *defineClassFunc;
109 Function *prepareIvarSlotFunc;
110 Function *getIvarFunc;
111 Function *setIvarFunc;
112 Function *definedFunc;
113 Function *undefFunc;
114 Function *aliasFunc;
115 Function *valiasFunc;
116 Function *newHashFunc;
117 Function *toAFunc;
118 Function *toAryFunc;
119 Function *catArrayFunc;
120 Function *dupArrayFunc;
121 Function *newArrayFunc;
122 Function *newStructFunc;
123 Function *newOpaqueFunc;
ff75d60 added support for overwriting an ObjC method that accepts a pointer
Laurent Sansonetti authored
124 Function *newPointerFunc;
cb65416 the great schism, part I
Laurent Sansonetti authored
125 Function *getStructFieldsFunc;
126 Function *getOpaqueDataFunc;
127 Function *getPointerPtrFunc;
128 Function *checkArityFunc;
129 Function *setStructFunc;
130 Function *newRangeFunc;
131 Function *newRegexpFunc;
132 Function *strInternFunc;
133 Function *keepVarsFunc;
134 Function *masgnGetElemBeforeSplatFunc;
135 Function *masgnGetElemAfterSplatFunc;
136 Function *masgnGetSplatFunc;
137 Function *newStringFunc;
138 Function *yieldFunc;
208a965 a quick implementation for #define_method (not yet 100% complete)
Laurent Sansonetti authored
139 Function *blockEvalFunc;
cb65416 the great schism, part I
Laurent Sansonetti authored
140 Function *gvarSetFunc;
141 Function *gvarGetFunc;
142 Function *cvarSetFunc;
143 Function *cvarGetFunc;
144 Function *currentExceptionFunc;
145 Function *popExceptionFunc;
146 Function *getSpecialFunc;
147 Function *breakFunc;
148 Function *longjmpFunc;
149 Function *setjmpFunc;
150 Function *popBrokenValue;
151
152 Constant *zeroVal;
153 Constant *oneVal;
154 Constant *twoVal;
01528a7 floating point arithmetic optimization: Float objects are now immedia…
Laurent Sansonetti authored
155 Constant *threeVal;
cb65416 the great schism, part I
Laurent Sansonetti authored
156 Constant *nilVal;
157 Constant *trueVal;
158 Constant *falseVal;
159 Constant *undefVal;
160 Constant *splatArgFollowsVal;
161 Constant *cObject;
162 const Type *RubyObjTy;
163 const Type *RubyObjPtrTy;
164 const Type *RubyObjPtrPtrTy;
165 const Type *PtrTy;
166 const Type *PtrPtrTy;
167 const Type *IntTy;
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
168 const Type *Int32PtrTy;
cb65416 the great schism, part I
Laurent Sansonetti authored
169
170 void compile_node_error(const char *msg, NODE *node);
171
172 virtual Instruction *
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
173 compile_const_pointer(void *ptr, const Type *type=NULL,
174 bool insert_to_bb=true) {
cb65416 the great schism, part I
Laurent Sansonetti authored
175 Value *ptrint = ConstantInt::get(IntTy, (long)ptr);
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
176 if (type == NULL) {
177 type = PtrTy;
178 }
cb65416 the great schism, part I
Laurent Sansonetti authored
179 return insert_to_bb
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
180 ? new IntToPtrInst(ptrint, type, "", bb)
181 : new IntToPtrInst(ptrint, type, "");
cb65416 the great schism, part I
Laurent Sansonetti authored
182 }
183
184 Instruction *
185 compile_const_pointer_to_pointer(void *ptr, bool insert_to_bb=true) {
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
186 return compile_const_pointer(ptr, PtrPtrTy, insert_to_bb);
cb65416 the great schism, part I
Laurent Sansonetti authored
187 }
188
189 Value *compile_protected_call(Function *func,
190 std::vector<Value *> &params);
191 void compile_dispatch_arguments(NODE *args,
192 std::vector<Value *> &arguments, int *pargc);
193 Function::ArgumentListType::iterator compile_optional_arguments(
194 Function::ArgumentListType::iterator iter, NODE *node);
195 void compile_boolean_test(Value *condVal, BasicBlock *ifTrueBB,
196 BasicBlock *ifFalseBB);
197 void compile_when_arguments(NODE *args, Value *comparedToVal,
198 BasicBlock *thenBB);
199 void compile_single_when_argument(NODE *arg, Value *comparedToVal,
200 BasicBlock *thenBB);
201 virtual void compile_prepare_method(Value *classVal, Value *sel,
202 Function *new_function, rb_vm_arity_t &arity, NODE *body);
203 Value *compile_dispatch_call(std::vector<Value *> &params);
204 Value *compile_when_splat(Value *comparedToVal, Value *splatVal);
205 Value *compile_fast_eqq_call(Value *selfVal, Value *comparedToVal);
206 Value *compile_attribute_assign(NODE *node, Value *extra_val);
2c54dbd AOT compiler: added support for blocks
Laurent Sansonetti authored
207 virtual Value *compile_prepare_block_args(Function *func, int *flags);
208 Value *compile_block_create(NODE *node);
cb65416 the great schism, part I
Laurent Sansonetti authored
209 Value *compile_binding(void);
210 Value *compile_optimized_dispatch_call(SEL sel, int argc,
211 std::vector<Value *> &params);
212 Value *compile_ivar_read(ID vid);
213 Value *compile_ivar_assignment(ID vid, Value *val);
214 Value *compile_cvar_assignment(ID vid, Value *val);
215 Value *compile_gvar_assignment(struct global_entry *entry, Value *val);
216 Value *compile_constant_declaration(NODE *node, Value *val);
217 Value *compile_multiple_assignment(NODE *node, Value *val);
218 void compile_multiple_assignment_element(NODE *node, Value *val);
219 Value *compile_current_class(void);
57d8085 AOT compiler: added support for NSObject and ID compilation
Laurent Sansonetti authored
220 virtual Value *compile_nsobject(void);
cb65416 the great schism, part I
Laurent Sansonetti authored
221 Value *compile_class_path(NODE *node);
222 Value *compile_const(ID id, Value *outer);
223 Value *compile_singleton_class(Value *obj);
224 Value *compile_defined_expression(NODE *node);
225 Value *compile_dstr(NODE *node);
226 Value *compile_dvar_slot(ID name);
227 void compile_break_val(Value *val);
228 Value *compile_jump(NODE *node);
229 virtual Value *compile_mcache(SEL sel, bool super);
230 virtual Value *compile_ccache(ID id);
231 virtual Instruction *compile_sel(SEL sel, bool add_to_bb=true) {
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
232 return compile_const_pointer(sel, PtrTy, add_to_bb);
cb65416 the great schism, part I
Laurent Sansonetti authored
233 }
57d8085 AOT compiler: added support for NSObject and ID compilation
Laurent Sansonetti authored
234 virtual Value *compile_id(ID id);
cb65416 the great schism, part I
Laurent Sansonetti authored
235 GlobalVariable *compile_const_global_string(const char *str);
2c54dbd AOT compiler: added support for blocks
Laurent Sansonetti authored
236 Value *compile_arity(rb_vm_arity_t &arity);
cb65416 the great schism, part I
Laurent Sansonetti authored
237
238 void compile_landing_pad_header(void);
239 void compile_landing_pad_footer(void);
240 void compile_rethrow_exception(void);
241 void compile_pop_exception(void);
242 Value *compile_lvar_slot(ID name);
243 bool compile_lvars(ID *tbl);
244 Value *compile_new_struct(Value *klass, std::vector<Value *> &fields);
245 Value *compile_new_opaque(Value *klass, Value *val);
ff75d60 added support for overwriting an ObjC method that accepts a pointer
Laurent Sansonetti authored
246 Value *compile_new_pointer(const char *type, Value *val);
cb65416 the great schism, part I
Laurent Sansonetti authored
247 void compile_get_struct_fields(Value *val, Value *buf,
248 rb_vm_bs_boxed_t *bs_boxed);
249 Value *compile_get_opaque_data(Value *val, rb_vm_bs_boxed_t *bs_boxed,
250 Value *slot);
251 Value *compile_get_cptr(Value *val, const char *type, Value *slot);
252 void compile_check_arity(Value *given, Value *requested);
253 void compile_set_struct(Value *rcv, int field, Value *val);
254
255 Value *compile_conversion_to_c(const char *type, Value *val,
256 Value *slot);
257 Value *compile_conversion_to_ruby(const char *type,
258 const Type *llvm_type, Value *val);
259
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
260 Instruction *compile_slot_cache(ID id);
261 virtual Instruction *gen_slot_cache(ID id);
cb65416 the great schism, part I
Laurent Sansonetti authored
262 ICmpInst *is_value_a_fixnum(Value *val);
263 void compile_ivar_slots(Value *klass, BasicBlock::InstListType &list,
264 BasicBlock::InstListType::iterator iter);
265 bool unbox_ruby_constant(Value *val, VALUE *rval);
01528a7 floating point arithmetic optimization: Float objects are now immedia…
Laurent Sansonetti authored
266 Value *optimized_immediate_op(SEL sel, Value *leftVal, Value *rightVal,
267 bool float_op, bool *is_predicate);
268 Value *compile_double_coercion(Value *val, Value *mask,
269 BasicBlock *fallback_bb, Function *f);
270
cb65416 the great schism, part I
Laurent Sansonetti authored
271 SEL mid_to_sel(ID mid, int arity);
272 };
273
274 class RoxorAOTCompiler : public RoxorCompiler {
275 public:
57d8085 AOT compiler: added support for NSObject and ID compilation
Laurent Sansonetti authored
276 RoxorAOTCompiler(const char *fname);
cb65416 the great schism, part I
Laurent Sansonetti authored
277
278 Function *compile_main_function(NODE *node);
279
280 private:
281 std::map<SEL, GlobalVariable *> mcaches;
282 std::map<ID, GlobalVariable *> ccaches;
283 std::map<SEL, GlobalVariable *> sels;
57d8085 AOT compiler: added support for NSObject and ID compilation
Laurent Sansonetti authored
284 std::map<ID, GlobalVariable *> ids;
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
285 std::vector<GlobalVariable *> ivar_slots;
57d8085 AOT compiler: added support for NSObject and ID compilation
Laurent Sansonetti authored
286 GlobalVariable *cObject_gvar;
cb65416 the great schism, part I
Laurent Sansonetti authored
287
288 Value *compile_mcache(SEL sel, bool super);
289 Value *compile_ccache(ID id);
290 Instruction *compile_sel(SEL sel, bool add_to_bb=true);
291 void compile_prepare_method(Value *classVal, Value *sel,
292 Function *new_function, rb_vm_arity_t &arity, NODE *body);
2c54dbd AOT compiler: added support for blocks
Laurent Sansonetti authored
293 Value *compile_prepare_block_args(Function *func, int *flags);
57d8085 AOT compiler: added support for NSObject and ID compilation
Laurent Sansonetti authored
294 Value *compile_nsobject(void);
295 Value *compile_id(ID id);
cb65416 the great schism, part I
Laurent Sansonetti authored
296
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
297 Instruction *gen_slot_cache(ID id);
298
cb65416 the great schism, part I
Laurent Sansonetti authored
299 Instruction *
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
300 compile_const_pointer(void *ptr, const Type *type=NULL,
301 bool insert_to_bb=true) {
cb65416 the great schism, part I
Laurent Sansonetti authored
302 if (ptr == NULL) {
85b7cc2 AOT compiler: ivar slots support
Laurent Sansonetti authored
303 return RoxorCompiler::compile_const_pointer(ptr, type,
304 insert_to_bb);
cb65416 the great schism, part I
Laurent Sansonetti authored
305 }
306 printf("compile_const_pointer() called with a non-NULL pointer " \
307 "on the AOT compiler - leaving the ship!\n");
308 abort();
309 }
310 };
311
312 #endif /* __cplusplus */
313
314 #endif /* __COMPILER_H_ */
Something went wrong with that request. Please try again.