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