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