Merge pull request #631 from kebby/master
[mono.git] / mono / mini / ir-emit.h
1 /*
2  * ir-emit.h: IR Creation/Emission Macros
3  *
4  * Author:
5  *   Zoltan Varga (vargaz@gmail.com)
6  *
7  * (C) 2002 Ximian, Inc.
8  */
9
10 #ifndef __MONO_IR_EMIT_H__
11 #define __MONO_IR_EMIT_H__
12
13 #include "mini.h"
14
15 G_BEGIN_DECLS
16
17 static inline guint32
18 alloc_ireg (MonoCompile *cfg)
19 {
20         return cfg->next_vreg ++;
21 }
22
23 static inline guint32
24 alloc_preg (MonoCompile *cfg)
25 {
26         return alloc_ireg (cfg);
27 }
28
29 static inline guint32
30 alloc_lreg (MonoCompile *cfg)
31 {
32 #if SIZEOF_REGISTER == 8
33         return cfg->next_vreg ++;
34 #else
35         /* Use a pair of consecutive vregs */
36         guint32 res = cfg->next_vreg;
37
38         cfg->next_vreg += 3;
39
40         return res;
41 #endif
42 }
43
44 static inline guint32
45 alloc_freg (MonoCompile *cfg)
46 {
47 #ifdef MONO_ARCH_SOFT_FLOAT
48         /* Allocate an lvreg so float ops can be decomposed into long ops */
49         return alloc_lreg (cfg);
50 #else
51         /* Allocate these from the same pool as the int regs */
52         return cfg->next_vreg ++;
53 #endif
54 }
55
56 static inline guint32
57 alloc_ireg_ref (MonoCompile *cfg)
58 {
59         int vreg = alloc_ireg (cfg);
60
61         if (cfg->compute_gc_maps)
62                 mono_mark_vreg_as_ref (cfg, vreg);
63
64         return vreg;
65 }
66
67 static inline guint32
68 alloc_ireg_mp (MonoCompile *cfg)
69 {
70         int vreg = alloc_ireg (cfg);
71
72         if (cfg->compute_gc_maps)
73                 mono_mark_vreg_as_mp (cfg, vreg);
74
75         return vreg;
76 }
77
78 static inline guint32
79 alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
80 {
81         switch (stack_type) {
82         case STACK_I4:
83         case STACK_PTR:
84                 return alloc_ireg (cfg);
85         case STACK_MP:
86                 return alloc_ireg_mp (cfg);
87         case STACK_OBJ:
88                 return alloc_ireg_ref (cfg);
89         case STACK_R8:
90                 return alloc_freg (cfg);
91         case STACK_I8:
92                 return alloc_lreg (cfg);
93         case STACK_VTYPE:
94                 return alloc_ireg (cfg);
95         default:
96                 g_warning ("Unknown stack type %x\n", stack_type);
97                 g_assert_not_reached ();
98                 return -1;
99         }
100 }
101
102 #undef MONO_INST_NEW
103 /* 
104  * FIXME: zeroing out some fields is not needed with the new IR, but the old 
105  * JIT code still uses the left and right fields, so it has to stay.
106  */
107 #define MONO_INST_NEW(cfg,dest,op) do { \
108                 (dest) = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoInst));        \
109                 (dest)->inst_c0 = (dest)->inst_c1 = 0; \
110                 (dest)->next = (dest)->prev = NULL;    \
111                 (dest)->opcode = (op);  \
112         (dest)->flags = 0; \
113         (dest)->type = 0; \
114         (dest)->dreg = -1;  \
115         MONO_INST_NULLIFY_SREGS ((dest));                   \
116         (dest)->cil_code = (cfg)->ip;  \
117         } while (0)
118
119 /*
120  * Variants which take a dest argument and don't do an emit
121  */
122 #define NEW_ICONST(cfg,dest,val) do {   \
123         MONO_INST_NEW ((cfg), (dest), OP_ICONST); \
124                 (dest)->inst_c0 = (val);        \
125                 (dest)->type = STACK_I4;        \
126                 (dest)->dreg = alloc_dreg ((cfg), STACK_I4);    \
127         } while (0)
128
129 /* 
130  * Avoid using this with a non-NULL val if possible as it is not AOT 
131  * compatible. Use one of the NEW_xxxCONST variants instead.
132  */
133 #define NEW_PCONST(cfg,dest,val) do {   \
134         MONO_INST_NEW ((cfg), (dest), OP_PCONST); \
135                 (dest)->inst_p0 = (val);        \
136                 (dest)->type = STACK_PTR;       \
137                 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR);   \
138         } while (0)
139
140 #define NEW_I8CONST(cfg,dest,val) do {  \
141         MONO_INST_NEW ((cfg), (dest), OP_I8CONST); \
142         (dest)->dreg = alloc_lreg ((cfg)); \
143         (dest)->type = STACK_I8; \
144         (dest)->inst_l = (val); \
145         } while (0)
146
147 #define NEW_STORE_MEMBASE(cfg,dest,op,base,offset,sr) do { \
148         MONO_INST_NEW ((cfg), (dest), (op)); \
149         (dest)->sreg1 = sr; \
150         (dest)->inst_destbasereg = base; \
151         (dest)->inst_offset = offset; \
152         } while (0)
153
154 #define NEW_LOAD_MEMBASE(cfg,dest,op,dr,base,offset) do { \
155         MONO_INST_NEW ((cfg), (dest), (op)); \
156         (dest)->dreg = (dr); \
157         (dest)->inst_basereg = (base); \
158         (dest)->inst_offset = (offset); \
159         (dest)->type = STACK_I4; \
160         } while (0)
161
162 #define NEW_LOAD_MEM(cfg,dest,op,dr,mem) do { \
163         MONO_INST_NEW ((cfg), (dest), (op)); \
164         (dest)->dreg = (dr); \
165         (dest)->inst_p0 = (gpointer)(gssize)(mem); \
166         (dest)->type = STACK_I4; \
167         } while (0)
168
169 #define NEW_UNALU(cfg,dest,op,dr,sr1) do { \
170         MONO_INST_NEW ((cfg), (dest), (op)); \
171         (dest)->dreg = dr; \
172         (dest)->sreg1 = sr1; \
173     } while (0)        
174
175 #define NEW_BIALU(cfg,dest,op,dr,sr1,sr2) do { \
176         MONO_INST_NEW ((cfg), (dest), (op)); \
177         (dest)->dreg = (dr); \
178         (dest)->sreg1 = (sr1); \
179         (dest)->sreg2 = (sr2); \
180         } while (0)
181
182 #define NEW_BIALU_IMM(cfg,dest,op,dr,sr,imm) do { \
183         MONO_INST_NEW ((cfg), (dest), (op)); \
184         (dest)->dreg = dr; \
185         (dest)->sreg1 = sr; \
186         (dest)->inst_imm = (imm); \
187         } while (0)
188
189 #ifdef MONO_ARCH_NEED_GOT_VAR
190
191 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do {   \
192         MONO_INST_NEW ((cfg), (dest), OP_PATCH_INFO); \
193                 (dest)->inst_left = (gpointer)(el1);    \
194                 (dest)->inst_right = (gpointer)(el2);   \
195         } while (0)
196
197 /* FIXME: Add the PUSH_GOT_ENTRY optimizations */
198 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do {                     \
199         MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST); \
200                 if (cfg->compile_aot) {                                 \
201                         MonoInst *group, *got_loc;              \
202                         got_loc = mono_get_got_var (cfg);               \
203                         NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
204                         (dest)->inst_basereg = got_loc->dreg;                   \
205                         (dest)->inst_p1 = group;                        \
206                 } else {                                                \
207                         (dest)->inst_p0 = (cons);                       \
208                         (dest)->inst_i1 = (gpointer)(patch_type);       \
209                 }                                                       \
210                 (dest)->type = STACK_PTR;                               \
211                 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR);   \
212         } while (0)
213
214 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
215                 MonoInst *group, *got_loc;                      \
216         MONO_INST_NEW ((cfg), (dest), OP_GOT_ENTRY); \
217                 got_loc = mono_get_got_var (cfg);                       \
218                 NEW_PATCH_INFO ((cfg), group, NULL, patch_type);        \
219                 group->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
220                 (dest)->inst_basereg = got_loc->dreg;                           \
221                 (dest)->inst_p1 = group;                                \
222                 (dest)->type = (stack_type);                            \
223         (dest)->klass = (stack_class);          \
224                 (dest)->dreg = alloc_dreg ((cfg), (stack_type));        \
225         } while (0)
226
227 #else
228
229 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do {    \
230         MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
231                 (dest)->inst_p0 = (cons);       \
232                 (dest)->inst_i1 = (gpointer)(patch_type); \
233                 (dest)->type = STACK_PTR;       \
234                 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR);   \
235     } while (0)
236
237 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
238         MONO_INST_NEW ((cfg), (dest), OP_AOTCONST); \
239                 (dest)->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
240                 (dest)->inst_p1 = (gpointer)(patch_type); \
241                 (dest)->type = (stack_type);    \
242         (dest)->klass = (stack_class);          \
243                 (dest)->dreg = alloc_dreg ((cfg), (stack_type));        \
244     } while (0)
245
246 #endif
247
248 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
249
250 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
251
252 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
253
254 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
255
256 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
257
258 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
259
260 #define NEW_LDSTRCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), NULL, STACK_OBJ, mono_defaults.string_class)
261
262 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token,generic_context) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_TYPE_FROM_HANDLE, (image), (token), (generic_context), STACK_OBJ, mono_defaults.monotype_class)
263
264 #define NEW_LDTOKENCONST(cfg,dest,image,token,generic_context) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), (generic_context), STACK_PTR, NULL)
265
266 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
267                 if (cfg->compile_aot) { \
268                         NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, NULL, STACK_OBJ, NULL); \
269                 } else { \
270                         NEW_PCONST (cfg, args [0], (entry).blob); \
271                 } \
272         } while (0)
273
274 #define NEW_METHOD_RGCTX_CONST(cfg,dest,method) do { \
275                 if (cfg->compile_aot) {                                                                                 \
276                         NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHOD_RGCTX, (method)); \
277                 } else {                                                                                                                \
278                         MonoMethodRuntimeGenericContext *mrgctx;                                        \
279                         mrgctx = mono_method_lookup_rgctx (mono_class_vtable ((cfg)->domain, (method)->klass), mini_method_get_context ((method))->method_inst); \
280                         NEW_PCONST ((cfg), (dest), (mrgctx));                                           \
281                 }                                                                                                                               \
282         } while (0)
283
284 #define NEW_DOMAINCONST(cfg,dest) do { \
285                 if ((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) {                          \
286                         /* avoid depending on undefined C behavior in sequence points */ \
287                         MonoInst* __domain_var = mono_get_domainvar (cfg); \
288                         NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
289                 } else { \
290                         NEW_PCONST (cfg, dest, (cfg)->domain); \
291                 } \
292         } while (0)
293
294 #define NEW_JIT_ICALL_ADDRCONST(cfg,dest,name) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_JIT_ICALL_ADDR, (name))
295
296 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
297
298 #define NEW_VARLOAD(cfg,dest,var,vartype) do { \
299         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
300                 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype));  \
301                 type_to_eval_stack_type ((cfg), (vartype), (dest));     \
302                 (dest)->klass = var->klass;     \
303                 (dest)->sreg1 = var->dreg;   \
304         (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
305         if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
306         } while (0)
307
308 #ifdef MONO_ARCH_SOFT_FLOAT
309 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8 || (stack_type) == STACK_R8)
310 #else
311 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8)
312 #endif
313
314 static inline void
315 handle_gsharedvt_ldaddr (MonoCompile *cfg)
316 {
317         /* The decomposition of ldaddr makes use of these two variables, so add uses for them */
318         MonoInst *use;
319
320         MONO_INST_NEW (cfg, use, OP_DUMMY_USE);
321         use->sreg1 = cfg->gsharedvt_info_var->dreg;
322         MONO_ADD_INS (cfg->cbb, use);
323         MONO_INST_NEW (cfg, use, OP_DUMMY_USE);
324         use->sreg1 = cfg->gsharedvt_locals_var->dreg;
325         MONO_ADD_INS (cfg->cbb, use);
326 }
327
328 #define NEW_VARLOADA(cfg,dest,var,vartype) do { \
329         MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
330                 (dest)->inst_p0 = (var); \
331                 (var)->flags |= MONO_INST_INDIRECT;     \
332                 (dest)->type = STACK_MP;        \
333                 (dest)->klass = (var)->klass;   \
334         (dest)->dreg = alloc_dreg ((cfg), STACK_MP); \
335                           if (G_UNLIKELY (cfg->gsharedvt) && mini_is_gsharedvt_variable_type ((cfg), (var)->inst_vtype)) { handle_gsharedvt_ldaddr ((cfg)); } \
336                 if (SIZEOF_REGISTER == 4 && DECOMPOSE_INTO_REGPAIR ((var)->type)) { MonoInst *var1 = get_vreg_to_inst (cfg, (var)->dreg + 1); MonoInst *var2 = get_vreg_to_inst (cfg, (var)->dreg + 2); g_assert (var1); g_assert (var2); var1->flags |= MONO_INST_INDIRECT; var2->flags |= MONO_INST_INDIRECT; } \
337         } while (0)
338
339 #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do {    \
340         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
341                 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype));    \
342                 (dest)->klass = (var)->klass;   \
343         (dest)->sreg1 = (inst)->dreg; \
344                 (dest)->dreg = (var)->dreg;   \
345         if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
346         } while (0)
347
348 #define NEW_TEMPLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype)
349
350 #define NEW_TEMPLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), cfg->varinfo [(num)], cfg->varinfo [(num)]->inst_vtype)
351
352 #define NEW_TEMPSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype, (inst))
353
354 #define NEW_ARGLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
355
356 #define NEW_LOCLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
357
358 #define NEW_LOCSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
359
360 #define NEW_ARGSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
361
362 #define NEW_LOCLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype)
363
364 #define NEW_RETLOADA(cfg,dest) do {     \
365         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
366         (dest)->type = STACK_MP; \
367             (dest)->klass = cfg->ret->klass;    \
368             (dest)->sreg1 = cfg->vret_addr->dreg;   \
369         (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
370         } while (0)
371
372 #define NEW_ARGLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), arg_array [(num)], param_types [(num)])
373
374 /* Promote the vreg to a variable so its address can be taken */
375 #define NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { \
376         MonoInst *var = get_vreg_to_inst ((cfg), (vreg)); \
377         if (!var) \
378                     var = mono_compile_create_var_for_vreg ((cfg), (ltype), OP_LOCAL, (vreg)); \
379         NEW_VARLOADA ((cfg), (dest), (var), (ltype)); \
380     } while (0)
381
382 #define NEW_DUMMY_USE(cfg,dest,var) do { \
383         MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
384                 (dest)->sreg1 = var->dreg; \
385     } while (0)
386
387 /* Variants which take a type argument and handle vtypes as well */
388 #define NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { \
389             NEW_LOAD_MEMBASE ((cfg), (dest), mono_type_to_load_membase ((cfg), (ltype)), 0, (base), (offset)); \
390             type_to_eval_stack_type ((cfg), (ltype), (dest)); \
391             (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
392     } while (0)
393
394 #define NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { \
395         MONO_INST_NEW ((cfg), (dest), mono_type_to_store_membase ((cfg), (ltype))); \
396         (dest)->sreg1 = sr; \
397         (dest)->inst_destbasereg = base; \
398         (dest)->inst_offset = offset; \
399             type_to_eval_stack_type ((cfg), (ltype), (dest)); \
400         (dest)->klass = mono_class_from_mono_type (ltype); \
401         } while (0)
402
403 #define NEW_SEQ_POINT(cfg,dest,il_offset,intr_loc) do {  \
404         MONO_INST_NEW ((cfg), (dest), OP_SEQ_POINT); \
405         (dest)->inst_imm = (il_offset); \
406         (dest)->flags = intr_loc ? MONO_INST_SINGLE_STEP_LOC : 0; \
407         } while (0)
408
409 #define NEW_GC_PARAM_SLOT_LIVENESS_DEF(cfg,dest,offset,type) do { \
410         MONO_INST_NEW ((cfg), (dest), OP_GC_PARAM_SLOT_LIVENESS_DEF); \
411         (dest)->inst_offset = (offset); \
412         (dest)->inst_vtype = (type); \
413         } while (0)
414
415 /*
416  * Variants which do an emit as well.
417  */
418 #define EMIT_NEW_ICONST(cfg,dest,val) do { NEW_ICONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
419
420 #define EMIT_NEW_PCONST(cfg,dest,val) do { NEW_PCONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
421
422 #define EMIT_NEW_I8CONST(cfg,dest,val) do { NEW_I8CONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
423
424 #define EMIT_NEW_AOTCONST(cfg,dest,patch_type,cons) do { NEW_AOTCONST ((cfg), (dest), (patch_type), (cons)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
425
426 #define EMIT_NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { NEW_AOTCONST_TOKEN ((cfg), (dest), (patch_type), (image), (token), NULL, (stack_type), (stack_class)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
427
428 #define EMIT_NEW_CLASSCONST(cfg,dest,val) do { NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
429
430 #define EMIT_NEW_IMAGECONST(cfg,dest,val) do { NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
431
432 #define EMIT_NEW_FIELDCONST(cfg,dest,val) do { NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
433
434 #define EMIT_NEW_METHODCONST(cfg,dest,val) do { NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
435
436 #define EMIT_NEW_VTABLECONST(cfg,dest,vtable) do { NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
437
438 #define EMIT_NEW_SFLDACONST(cfg,dest,val) do { NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
439
440 #define EMIT_NEW_LDSTRCONST(cfg,dest,image,token) do { NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), NULL, STACK_OBJ, mono_defaults.string_class); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
441
442 #define EMIT_NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token,generic_context) do { NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_TYPE_FROM_HANDLE, (image), (token), (generic_context), STACK_OBJ, mono_defaults.monotype_class); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
443
444 #define EMIT_NEW_LDTOKENCONST(cfg,dest,image,token,generic_context) do { NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), (generic_context), STACK_PTR, NULL); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
445
446 #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
447
448 #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
449
450 #define EMIT_NEW_METHOD_RGCTX_CONST(cfg,dest,method) do { NEW_METHOD_RGCTX_CONST ((cfg), (dest), (method)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
451
452 #define EMIT_NEW_JIT_ICALL_ADDRCONST(cfg,dest,name) do { NEW_JIT_ICALL_ADDRCONST ((cfg), (dest), (name)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
453
454 #define EMIT_NEW_VARLOAD(cfg,dest,var,vartype) do { NEW_VARLOAD ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
455
456 #define EMIT_NEW_VARSTORE(cfg,dest,var,vartype,inst) do { NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
457
458 #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
459
460 #ifdef MONO_ARCH_SOFT_FLOAT
461
462 /*
463  * Since the IL stack (and our vregs) contain double values, we have to do a conversion
464  * when loading/storing args/locals of type R4.
465  */
466
467 #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
468                 if (COMPILE_SOFT_FLOAT ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
469              MonoInst *iargs [1]; \
470              EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
471              (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
472         } else { \
473              EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
474         } \
475     } while (0)
476
477 #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do {        \
478                 if (COMPILE_SOFT_FLOAT ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
479              MonoInst *iargs [2]; \
480              iargs [0] = (inst); \
481              EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
482              mono_emit_jit_icall (cfg, mono_fstore_r4, iargs); \
483         } else { \
484              EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
485         } \
486     } while (0)
487
488 #define EMIT_NEW_ARGLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
489
490 #define EMIT_NEW_LOCLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
491
492 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
493
494 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
495
496 #else
497
498 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { NEW_ARGLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
499
500 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { NEW_LOCLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
501
502 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { NEW_LOCSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
503
504 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
505
506 #endif
507
508 #define EMIT_NEW_TEMPLOAD(cfg,dest,num) do { NEW_TEMPLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
509
510 #define EMIT_NEW_TEMPLOADA(cfg,dest,num) do { NEW_TEMPLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
511
512 #define EMIT_NEW_LOCLOADA(cfg,dest,num) do { NEW_LOCLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
513
514 #define EMIT_NEW_ARGLOADA(cfg,dest,num) do { NEW_ARGLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
515
516 #define EMIT_NEW_RETLOADA(cfg,dest) do { NEW_RETLOADA ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
517
518 #define EMIT_NEW_TEMPSTORE(cfg,dest,num,inst) do { NEW_TEMPSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
519
520 #define EMIT_NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { NEW_VARLOADA_VREG ((cfg), (dest), (vreg), (ltype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
521
522 #define EMIT_NEW_DUMMY_USE(cfg,dest,var) do { NEW_DUMMY_USE ((cfg), (dest), (var)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
523
524 #define EMIT_NEW_UNALU(cfg,dest,op,dr,sr1) do { NEW_UNALU ((cfg), (dest), (op), (dr), (sr1)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
525
526 #define EMIT_NEW_BIALU(cfg,dest,op,dr,sr1,sr2) do { NEW_BIALU ((cfg), (dest), (op), (dr), (sr1), (sr2)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
527
528 #define EMIT_NEW_BIALU_IMM(cfg,dest,op,dr,sr,imm) do { NEW_BIALU_IMM ((cfg), (dest), (op), (dr), (sr), (imm)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
529
530 #define EMIT_NEW_LOAD_MEMBASE(cfg,dest,op,dr,base,offset) do { NEW_LOAD_MEMBASE ((cfg), (dest), (op), (dr), (base), (offset)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
531
532 #define EMIT_NEW_STORE_MEMBASE(cfg,dest,op,base,offset,sr) do { NEW_STORE_MEMBASE ((cfg), (dest), (op), (base), (offset), (sr)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
533
534 #define EMIT_NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { NEW_LOAD_MEMBASE_TYPE ((cfg), (dest), (ltype), (base), (offset)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
535
536 #define EMIT_NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { NEW_STORE_MEMBASE_TYPE ((cfg), (dest), (ltype), (base), (offset), (sr)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
537
538 #define EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF(cfg,dest,offset,type) do { NEW_GC_PARAM_SLOT_LIVENESS_DEF ((cfg), (dest), (offset), (type)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
539 /*
540  * Variants which do not take an dest argument, but take a dreg argument.
541  */
542 #define MONO_EMIT_NEW_ICONST(cfg,dr,imm) do { \
543         MonoInst *inst; \
544         MONO_INST_NEW ((cfg), (inst), OP_ICONST); \
545         inst->dreg = dr; \
546         inst->inst_c0 = imm; \
547             MONO_ADD_INS ((cfg)->cbb, inst); \
548         } while (0)
549
550 #define MONO_EMIT_NEW_PCONST(cfg,dr,val) do {   \
551         MonoInst *inst; \
552         MONO_INST_NEW ((cfg), (inst), OP_PCONST); \
553         inst->dreg = dr; \
554                 (inst)->inst_p0 = (val);        \
555                 (inst)->type = STACK_PTR;       \
556             MONO_ADD_INS ((cfg)->cbb, inst); \
557         } while (0)
558
559 #define MONO_EMIT_NEW_I8CONST(cfg,dr,imm) do { \
560         MonoInst *inst; \
561         MONO_INST_NEW ((cfg), (inst), OP_I8CONST); \
562         inst->dreg = dr; \
563         inst->inst_l = imm; \
564             MONO_ADD_INS ((cfg)->cbb, inst); \
565         } while (0)
566
567 #ifdef MONO_ARCH_NEED_GOT_VAR
568
569 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
570         MonoInst *inst; \
571         NEW_AOTCONST ((cfg), (inst), (patch_type), (cons)); \
572         inst->dreg = (dr); \
573         MONO_ADD_INS ((cfg)->cbb, inst); \
574     } while (0)
575
576 #else
577
578 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,type) do { \
579         MonoInst *inst; \
580         MONO_INST_NEW ((cfg), (inst), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
581         inst->dreg = dr; \
582         inst->inst_p0 = imm; \
583         inst->inst_c1 = type; \
584                 MONO_ADD_INS ((cfg)->cbb, inst); \
585         } while (0)
586
587 #endif
588
589 #define MONO_EMIT_NEW_CLASSCONST(cfg,dr,imm) MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,MONO_PATCH_INFO_CLASS)
590 #define MONO_EMIT_NEW_VTABLECONST(cfg,dest,vtable) MONO_EMIT_NEW_AOTCONST ((cfg), (dest), (cfg)->compile_aot ? (gpointer)((vtable)->klass) : (vtable), MONO_PATCH_INFO_VTABLE)
591 #define MONO_EMIT_NEW_SIGNATURECONST(cfg,dr,sig) MONO_EMIT_NEW_AOTCONST ((cfg), (dr), (sig), MONO_PATCH_INFO_SIGNATURE)
592
593 #define MONO_EMIT_NEW_VZERO(cfg,dr,kl) do {     \
594         MonoInst *inst; \
595         MONO_INST_NEW ((cfg), (inst), MONO_CLASS_IS_SIMD (cfg, kl) ? OP_XZERO : OP_VZERO); \
596         inst->dreg = dr; \
597                 (inst)->type = STACK_VTYPE;     \
598         (inst)->klass = (kl); \
599             MONO_ADD_INS ((cfg)->cbb, inst); \
600         } while (0)
601
602 #define MONO_EMIT_NEW_UNALU(cfg,op,dr,sr1) do { \
603         MonoInst *inst; \
604         EMIT_NEW_UNALU ((cfg), (inst), (op), (dr), (sr1)); \
605         } while (0)
606
607 #define MONO_EMIT_NEW_BIALU(cfg,op,dr,sr1,sr2) do { \
608         MonoInst *inst; \
609         MONO_INST_NEW ((cfg), (inst), (op)); \
610         inst->dreg = dr; \
611         inst->sreg1 = sr1; \
612         inst->sreg2 = sr2; \
613             MONO_ADD_INS (cfg->cbb, inst); \
614         } while (0)
615
616 #define MONO_EMIT_NEW_BIALU_IMM(cfg,op,dr,sr,imm) do { \
617         MonoInst *inst; \
618         MONO_INST_NEW ((cfg), (inst), (op)); \
619         inst->dreg = dr; \
620         inst->sreg1 = sr; \
621         inst->inst_imm = (mgreg_t)(imm);                \
622             MONO_ADD_INS (cfg->cbb, inst); \
623         } while (0)
624
625 #define MONO_EMIT_NEW_COMPARE_IMM(cfg,sr1,imm) do { \
626         MonoInst *inst; \
627         MONO_INST_NEW ((cfg), (inst), (OP_COMPARE_IMM)); \
628         inst->sreg1 = sr1; \
629         inst->inst_imm = (imm);                  \
630             MONO_ADD_INS ((cfg)->cbb, inst); \
631         } while (0)
632
633 #define MONO_EMIT_NEW_ICOMPARE_IMM(cfg,sr1,imm) do { \
634         MonoInst *inst; \
635         MONO_INST_NEW ((cfg), (inst), sizeof (mgreg_t) == 8 ? OP_ICOMPARE_IMM : OP_COMPARE_IMM); \
636         inst->sreg1 = sr1; \
637         inst->inst_imm = (imm);                  \
638             MONO_ADD_INS ((cfg)->cbb, inst); \
639         } while (0)
640
641 /* This is used on 32 bit machines too when running with LLVM */
642 #define MONO_EMIT_NEW_LCOMPARE_IMM(cfg,sr1,imm) do { \
643         MonoInst *inst; \
644         MONO_INST_NEW ((cfg), (inst), (OP_LCOMPARE_IMM)); \
645         inst->sreg1 = sr1;                                                                      \
646         if (SIZEOF_REGISTER == 4 && COMPILE_LLVM (cfg))  {      \
647                         guint64 _l = (imm);                                                             \
648                         inst->inst_imm = _l & 0xffffffff;                               \
649                         inst->inst_offset = _l >> 32;                                           \
650                 } else { \
651                         inst->inst_imm = (imm);          \
652                 }                                                                \
653             MONO_ADD_INS ((cfg)->cbb, inst); \
654         } while (0)
655
656 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg,op,dr,base,offset) do { \
657         MonoInst *inst; \
658         MONO_INST_NEW ((cfg), (inst), (op)); \
659         inst->dreg = dr; \
660         inst->inst_basereg = base; \
661         inst->inst_offset = offset; \
662             MONO_ADD_INS (cfg->cbb, inst); \
663     } while (0)
664
665 #define MONO_EMIT_NEW_LOAD_MEMBASE(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
666
667 #define MONO_EMIT_NEW_STORE_MEMBASE(cfg,op,base,offset,sr) do { \
668         MonoInst *inst; \
669         MONO_INST_NEW ((cfg), (inst), (op)); \
670         (inst)->sreg1 = sr; \
671         (inst)->inst_destbasereg = base; \
672         (inst)->inst_offset = offset; \
673             MONO_ADD_INS (cfg->cbb, inst); \
674         } while (0)
675
676 #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
677         MonoInst *inst; \
678         MONO_INST_NEW ((cfg), (inst), (op)); \
679         inst->inst_destbasereg = base; \
680         inst->inst_offset = offset; \
681         inst->inst_imm = (mgreg_t)(imm); \
682         MONO_ADD_INS ((cfg)->cbb, inst); \
683         } while (0)
684
685 #define MONO_EMIT_NEW_COND_EXC(cfg,cond,name) do { \
686         MonoInst *inst; \
687         MONO_INST_NEW ((cfg), (inst), (OP_COND_EXC_##cond)); \
688         inst->inst_p1 = (char*)name; \
689             MONO_ADD_INS ((cfg)->cbb, inst); \
690         } while (0)
691
692 /* Branch support */
693
694 /*
695  * Basic blocks have two numeric identifiers:
696  * dfn: Depth First Number
697  * block_num: unique ID assigned at bblock creation
698  */
699 #define NEW_BBLOCK(cfg,bblock) do { \
700         (bblock) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
701         (bblock)->block_num = cfg->num_bblocks++; \
702     } while (0)
703
704 #define ADD_BBLOCK(cfg,b) do {  \
705         if ((b)->cil_code)  {\
706                         cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b);   \
707         } \
708                 (b)->real_offset = cfg->real_offset;    \
709         } while (0)
710
711 /* Emit a one-way conditional branch */
712 /* 
713  * The inst_false_bb field of the cond branch will not be set, the JIT code should be
714  * prepared to deal with this.
715  */
716 #ifdef DEBUG_EXTENDED_BBLOCKS
717 static int ccount = 0;
718 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
719         MonoInst *ins; \
720         MonoBasicBlock *falsebb; \
721         MONO_INST_NEW ((cfg), (ins), (op)); \
722         if ((op) == OP_BR) { \
723                 NEW_BBLOCK ((cfg), falsebb); \
724             ins->inst_target_bb = (truebb); \
725             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
726             MONO_ADD_INS ((cfg)->cbb, ins); \
727             MONO_START_BB ((cfg), falsebb); \
728         } else { \
729             ccount ++; \
730                     ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);  \
731             ins->inst_true_bb = (truebb); \
732             ins->inst_false_bb = NULL; \
733             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
734             MONO_ADD_INS ((cfg)->cbb, ins); \
735             if (getenv ("COUNT2") && ccount == atoi (getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
736             if (getenv ("COUNT2") && ccount < atoi (getenv ("COUNT2"))) { \
737                  cfg->cbb->extended = TRUE; \
738             } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
739         } \
740         } while (0)
741 #else
742 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
743         MonoInst *ins; \
744         MonoBasicBlock *falsebb; \
745         MONO_INST_NEW ((cfg), (ins), (op)); \
746         if ((op) == OP_BR) { \
747                 NEW_BBLOCK ((cfg), falsebb); \
748             ins->inst_target_bb = (truebb); \
749             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
750             MONO_ADD_INS ((cfg)->cbb, ins); \
751             MONO_START_BB ((cfg), falsebb); \
752         } else { \
753                     ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);  \
754             ins->inst_true_bb = (truebb); \
755             ins->inst_false_bb = NULL; \
756             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
757             MONO_ADD_INS ((cfg)->cbb, ins); \
758             if (!cfg->enable_extended_bblocks) { \
759                 NEW_BBLOCK ((cfg), falsebb); \
760                 ins->inst_false_bb = falsebb; \
761                 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
762                 MONO_START_BB ((cfg), falsebb); \
763             } else { \
764                                 cfg->cbb->extended = TRUE; \
765                         } \
766         } \
767         } while (0)
768 #endif
769
770 /* Emit a two-way conditional branch */
771 #define MONO_EMIT_NEW_BRANCH_BLOCK2(cfg,op,truebb,falsebb) do { \
772         MonoInst *ins; \
773         MONO_INST_NEW ((cfg), (ins), (op)); \
774                 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);      \
775         ins->inst_true_bb = (truebb); \
776         ins->inst_false_bb = (falsebb); \
777         mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
778         mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
779         MONO_ADD_INS ((cfg)->cbb, ins); \
780         } while (0)
781
782 #define MONO_START_BB(cfg, bblock) do { \
783         ADD_BBLOCK ((cfg), (bblock)); \
784         if (cfg->cbb->last_ins && MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins) && !cfg->cbb->last_ins->inst_false_bb) { \
785             cfg->cbb->last_ins->inst_false_bb = (bblock); \
786             mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
787         } else if (! (cfg->cbb->last_ins && ((cfg->cbb->last_ins->opcode == OP_BR) || (cfg->cbb->last_ins->opcode == OP_BR_REG) || MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins)))) \
788             mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
789             (cfg)->cbb->next_bb = (bblock); \
790             (cfg)->cbb = (bblock); \
791     } while (0)
792
793 /* This marks a place in code where an implicit exception could be thrown */
794 #define MONO_EMIT_NEW_IMPLICIT_EXCEPTION(cfg) do { \
795                 if (COMPILE_LLVM ((cfg))) {                                                                     \
796                         MONO_EMIT_NEW_UNALU (cfg, OP_IMPLICIT_EXCEPTION, -1, -1);       \
797                 } \
798         } while (0)
799
800 /* Loads/Stores which can fault are handled correctly by the LLVM mono branch */
801 #define MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE(cfg) do { \
802         if (COMPILE_LLVM (cfg) && !IS_LLVM_MONO_BRANCH)                 \
803                 MONO_EMIT_NEW_IMPLICIT_EXCEPTION ((cfg));                       \
804     } while (0)
805
806 /* Emit an explicit null check which doesn't depend on SIGSEGV signal handling */
807 #define MONO_EMIT_NULL_CHECK(cfg, reg) do { \
808                 if (cfg->explicit_null_checks) {                                                          \
809                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, (reg), 0); \
810                         MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException"); \
811                 } else {                        \
812                         MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE (cfg);                                              \
813                 }                                                                                                                               \
814         } while (0)
815
816 #define MONO_EMIT_NEW_CHECK_THIS(cfg, sreg) do { \
817                 cfg->flags |= MONO_CFG_HAS_CHECK_THIS;   \
818                 if (cfg->explicit_null_checks) {                 \
819                         MONO_EMIT_NULL_CHECK (cfg, sreg);                               \
820                 } else {                                                                                        \
821                         MONO_EMIT_NEW_UNALU (cfg, OP_CHECK_THIS, -1, sreg);                     \
822                         MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE (cfg);                      \
823                 }                                                                                                                               \
824                 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, sreg);                               \
825         } while (0)
826
827 #define NEW_LOAD_MEMBASE_FLAGS(cfg,dest,op,dr,base,offset,ins_flags) do {       \
828                 int __ins_flags = ins_flags; \
829                 if (__ins_flags & MONO_INST_FAULT) {                                                            \
830                         MONO_EMIT_NULL_CHECK ((cfg), (base));                                           \
831                         if (cfg->explicit_null_checks)                                                          \
832                                 __ins_flags &= ~MONO_INST_FAULT;                                                        \
833                 }                                                                                                                               \
834                 NEW_LOAD_MEMBASE ((cfg), (dest), (op), (dr), (base), (offset)); \
835                 (dest)->flags = (__ins_flags);                                                                  \
836         } while (0)
837
838 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS(cfg,op,dr,base,offset,ins_flags) do { \
839         MonoInst *inst;                                                                                                 \
840                 int __ins_flags = ins_flags; \
841             if (__ins_flags & MONO_INST_FAULT) {                                                                        \
842                         MONO_EMIT_NULL_CHECK ((cfg), (base));                                           \
843                         if (cfg->explicit_null_checks)                                                          \
844                                 __ins_flags &= ~MONO_INST_FAULT;                                                        \
845                 }                                                                                                                               \
846                 NEW_LOAD_MEMBASE ((cfg), (inst), (op), (dr), (base), (offset)); \
847                 inst->flags = (__ins_flags); \
848             MONO_ADD_INS (cfg->cbb, inst); \
849     } while (0)
850
851 #define MONO_EMIT_NEW_LOAD_MEMBASE_FLAGS(cfg,dr,base,offset,ins_flags) MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset),(ins_flags))
852
853 /* A load which can cause a nullref */
854 #define NEW_LOAD_MEMBASE_FAULT(cfg,dest,op,dr,base,offset) NEW_LOAD_MEMBASE_FLAGS ((cfg), (dest), (op), (dr), (base), (offset), MONO_INST_FAULT)
855
856 #define EMIT_NEW_LOAD_MEMBASE_FAULT(cfg,dest,op,dr,base,offset) do { \
857                 NEW_LOAD_MEMBASE_FAULT ((cfg), (dest), (op), (dr), (base), (offset)); \
858                 MONO_ADD_INS ((cfg)->cbb, (dest)); \
859         } while (0)
860
861 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT(cfg,op,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS ((cfg), (op), (dr), (base), (offset), MONO_INST_FAULT)
862
863 #define MONO_EMIT_NEW_LOAD_MEMBASE_FAULT(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
864
865 /*Object Model related macros*/
866
867 /* Default bounds check implementation for most architectures + llvm */
868 #define MONO_EMIT_DEFAULT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg, fault) do { \
869                         int _length_reg = alloc_ireg (cfg); \
870                         if (fault) \
871                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset); \
872                         else \
873                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset, MONO_INST_CONSTANT_LOAD); \
874                         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, _length_reg, index_reg); \
875                         MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
876         } while (0)
877
878 #ifndef MONO_ARCH_EMIT_BOUNDS_CHECK
879 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) MONO_EMIT_DEFAULT_BOUNDS_CHECK ((cfg), (array_reg), (offset), (index_reg), TRUE)
880 #endif
881
882 /* cfg is the MonoCompile been used
883  * array_reg is the vreg holding the array object
884  * array_type is a struct (usually MonoArray or MonoString)
885  * array_length_field is the field in the previous struct with the length
886  * index_reg is the vreg holding the index
887  */
888 #define MONO_EMIT_BOUNDS_CHECK(cfg, array_reg, array_type, array_length_field, index_reg) do { \
889                 if (!(cfg->opt & MONO_OPT_UNSAFE)) {                                                    \
890                 if (!(cfg->opt & MONO_OPT_ABCREM)) {                                                    \
891                         MONO_EMIT_NULL_CHECK (cfg, array_reg);                                          \
892                         if (COMPILE_LLVM (cfg)) \
893                                 MONO_EMIT_DEFAULT_BOUNDS_CHECK ((cfg), (array_reg), G_STRUCT_OFFSET (array_type, array_length_field), (index_reg), TRUE); \
894                         else \
895                                 MONO_ARCH_EMIT_BOUNDS_CHECK ((cfg), (array_reg), G_STRUCT_OFFSET (array_type, array_length_field), (index_reg)); \
896                 } else {                                                                                                                \
897                         MonoInst *ins;                                                                                          \
898                         MONO_INST_NEW ((cfg), ins, OP_BOUNDS_CHECK);                            \
899                         ins->sreg1 = array_reg;                                                                         \
900                         ins->sreg2 = index_reg;                                                                         \
901                         ins->inst_imm = G_STRUCT_OFFSET (array_type, array_length_field); \
902                         ins->flags |= MONO_INST_FAULT; \
903                         MONO_ADD_INS ((cfg)->cbb, ins);                                                         \
904                         (cfg)->flags |= MONO_CFG_HAS_ARRAY_ACCESS;                                      \
905                         (cfg)->cbb->has_array_access = TRUE;                                            \
906                 }                                                                                                                               \
907                 }                                                                                                                               \
908     } while (0)
909
910 G_END_DECLS
911
912 #endif