Merge pull request #4621 from alexanderkyte/strdup_env
[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 #ifdef MONO_ARCH_NEED_GOT_VAR
235
236 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do {   \
237         MONO_INST_NEW ((cfg), (dest), OP_PATCH_INFO); \
238                 (dest)->inst_left = (gpointer)(el1);    \
239                 (dest)->inst_right = (gpointer)(el2);   \
240         } while (0)
241
242 /* FIXME: Add the PUSH_GOT_ENTRY optimizations */
243 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do {                     \
244         MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST); \
245                 if (cfg->compile_aot) {                                 \
246                         MonoInst *group, *got_loc;              \
247                         got_loc = mono_get_got_var (cfg);               \
248                         NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
249                         (dest)->inst_basereg = got_loc->dreg;                   \
250                         (dest)->inst_p1 = group;                        \
251                 } else {                                                \
252                         (dest)->inst_p0 = (cons);                       \
253                         (dest)->inst_i1 = (gpointer)(patch_type);       \
254                 }                                                       \
255                 (dest)->type = STACK_PTR;                               \
256                 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR);   \
257         } while (0)
258
259 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
260                 MonoInst *group, *got_loc;                      \
261         MONO_INST_NEW ((cfg), (dest), OP_GOT_ENTRY); \
262                 got_loc = mono_get_got_var (cfg);                       \
263                 NEW_PATCH_INFO ((cfg), group, NULL, patch_type);        \
264                 group->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
265                 (dest)->inst_basereg = got_loc->dreg;                           \
266                 (dest)->inst_p1 = group;                                \
267                 (dest)->type = (stack_type);                            \
268         (dest)->klass = (stack_class);          \
269                 (dest)->dreg = alloc_dreg ((cfg), (stack_type));        \
270         } while (0)
271
272 #else
273
274 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do {    \
275         MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
276                 (dest)->inst_p0 = (cons);       \
277                 (dest)->inst_i1 = (MonoInst *)(patch_type); \
278                 (dest)->type = STACK_PTR;       \
279                 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR);   \
280     } while (0)
281
282 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
283         MONO_INST_NEW ((cfg), (dest), OP_AOTCONST); \
284                 (dest)->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
285                 (dest)->inst_p1 = (gpointer)(patch_type); \
286                 (dest)->type = (stack_type);    \
287         (dest)->klass = (stack_class);          \
288                 (dest)->dreg = alloc_dreg ((cfg), (stack_type));        \
289     } while (0)
290
291 #endif
292
293 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
294
295 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
296
297 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
298
299 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
300
301 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
302
303 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
304
305 #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)
306
307 #define NEW_LDSTRLITCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_LDSTR_LIT, (val))
308
309 #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)
310
311 #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)
312
313 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
314                 if (cfg->compile_aot) { \
315                         NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, NULL, STACK_OBJ, NULL); \
316                 } else { \
317                         NEW_PCONST (cfg, args [0], (entry).blob); \
318                 } \
319         } while (0)
320
321 #define NEW_METHOD_RGCTX_CONST(cfg,dest,method) do { \
322                 if (cfg->compile_aot) {                                                                                 \
323                         NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHOD_RGCTX, (method)); \
324                 } else {                                                                                                                \
325                         MonoMethodRuntimeGenericContext *mrgctx;                                        \
326                         mrgctx = mono_method_lookup_rgctx (mono_class_vtable ((cfg)->domain, (method)->klass), mini_method_get_context ((method))->method_inst); \
327                         NEW_PCONST ((cfg), (dest), (mrgctx));                                           \
328                 }                                                                                                                               \
329         } while (0)
330
331 #define NEW_DOMAINCONST(cfg,dest) do { \
332                 if ((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) {                          \
333                         /* avoid depending on undefined C behavior in sequence points */ \
334                         MonoInst* __domain_var = mono_get_domainvar (cfg); \
335                         NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
336                 } else { \
337                         NEW_PCONST (cfg, dest, (cfg)->domain); \
338                 } \
339         } while (0)
340
341 #define NEW_JIT_ICALL_ADDRCONST(cfg,dest,name) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_JIT_ICALL_ADDR, (name))
342
343 #define NEW_VARLOAD(cfg,dest,var,vartype) do { \
344         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
345                 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype));  \
346                 type_to_eval_stack_type ((cfg), (vartype), (dest));     \
347                 (dest)->klass = var->klass;     \
348                 (dest)->sreg1 = var->dreg;   \
349         (dest)->dreg = alloc_dreg ((cfg), (MonoStackType)(dest)->type); \
350         if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
351         } while (0)
352
353 #define DECOMPOSE_INTO_REGPAIR(stack_type) (mono_arch_is_soft_float () ? ((stack_type) == STACK_I8 || (stack_type) == STACK_R8) : ((stack_type) == STACK_I8))
354
355 static inline void
356 handle_gsharedvt_ldaddr (MonoCompile *cfg)
357 {
358         /* The decomposition of ldaddr makes use of these two variables, so add uses for them */
359         MonoInst *use;
360
361         MONO_INST_NEW (cfg, use, OP_DUMMY_USE);
362         use->sreg1 = cfg->gsharedvt_info_var->dreg;
363         MONO_ADD_INS (cfg->cbb, use);
364         MONO_INST_NEW (cfg, use, OP_DUMMY_USE);
365         use->sreg1 = cfg->gsharedvt_locals_var->dreg;
366         MONO_ADD_INS (cfg->cbb, use);
367 }
368
369 #define NEW_VARLOADA(cfg,dest,var,vartype) do { \
370         MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
371                 (dest)->inst_p0 = (var); \
372                 (var)->flags |= MONO_INST_INDIRECT;     \
373                 (dest)->type = STACK_MP;        \
374                 (dest)->klass = (var)->klass;   \
375         (dest)->dreg = alloc_dreg ((cfg), STACK_MP); \
376                 (cfg)->has_indirection = TRUE;  \
377                           if (G_UNLIKELY (cfg->gsharedvt) && mini_is_gsharedvt_variable_type ((var)->inst_vtype)) { handle_gsharedvt_ldaddr ((cfg)); } \
378                 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; } \
379         } while (0)
380
381 #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do {    \
382         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
383                 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype));    \
384                 (dest)->klass = (var)->klass;   \
385         (dest)->sreg1 = (inst)->dreg; \
386                 (dest)->dreg = (var)->dreg;   \
387         if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
388         } while (0)
389
390 #define NEW_TEMPLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype)
391
392 #define NEW_TEMPLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), cfg->varinfo [(num)], cfg->varinfo [(num)]->inst_vtype)
393
394 #define NEW_TEMPSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype, (inst))
395
396 #define NEW_ARGLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
397
398 #define NEW_LOCLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
399
400 #define NEW_LOCSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
401
402 #define NEW_ARGSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
403
404 #define NEW_LOCLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype)
405
406 #define NEW_RETLOADA(cfg,dest) do {     \
407         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
408         (dest)->type = STACK_MP; \
409             (dest)->klass = cfg->ret->klass;    \
410             (dest)->sreg1 = cfg->vret_addr->dreg;   \
411         (dest)->dreg = alloc_dreg ((cfg), (MonoStackType)(dest)->type); \
412         } while (0)
413
414 #define NEW_ARGLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), arg_array [(num)], param_types [(num)])
415
416 /* Promote the vreg to a variable so its address can be taken */
417 #define NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { \
418         MonoInst *var = get_vreg_to_inst ((cfg), (vreg)); \
419         if (!var) \
420                     var = mono_compile_create_var_for_vreg ((cfg), (ltype), OP_LOCAL, (vreg)); \
421         NEW_VARLOADA ((cfg), (dest), (var), (ltype)); \
422     } while (0)
423
424 #define NEW_DUMMY_USE(cfg,dest,var) do { \
425         MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
426                 (dest)->sreg1 = var->dreg; \
427     } while (0)
428
429 /* Variants which take a type argument and handle vtypes as well */
430 #define NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { \
431             NEW_LOAD_MEMBASE ((cfg), (dest), mono_type_to_load_membase ((cfg), (ltype)), 0, (base), (offset)); \
432             type_to_eval_stack_type ((cfg), (ltype), (dest)); \
433             (dest)->dreg = alloc_dreg ((cfg), (MonoStackType)(dest)->type); \
434     } while (0)
435
436 #define NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { \
437         MONO_INST_NEW ((cfg), (dest), mono_type_to_store_membase ((cfg), (ltype))); \
438         (dest)->sreg1 = sr; \
439         (dest)->inst_destbasereg = base; \
440         (dest)->inst_offset = offset; \
441             type_to_eval_stack_type ((cfg), (ltype), (dest)); \
442         (dest)->klass = mono_class_from_mono_type (ltype); \
443         } while (0)
444
445 #define NEW_SEQ_POINT(cfg,dest,il_offset,intr_loc) do {  \
446         MONO_INST_NEW ((cfg), (dest), cfg->gen_sdb_seq_points ? OP_SEQ_POINT : OP_IL_SEQ_POINT); \
447         (dest)->inst_imm = (il_offset); \
448         (dest)->flags = intr_loc ? MONO_INST_SINGLE_STEP_LOC : 0; \
449         } while (0)
450
451 #define NEW_GC_PARAM_SLOT_LIVENESS_DEF(cfg,dest,offset,type) do { \
452         MONO_INST_NEW ((cfg), (dest), OP_GC_PARAM_SLOT_LIVENESS_DEF); \
453         (dest)->inst_offset = (offset); \
454         (dest)->inst_vtype = (type); \
455         } while (0)
456
457 /*
458  * Variants which do an emit as well.
459  */
460 #define EMIT_NEW_ICONST(cfg,dest,val) do { NEW_ICONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
461
462 #define EMIT_NEW_PCONST(cfg,dest,val) do { NEW_PCONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
463
464 #define EMIT_NEW_I8CONST(cfg,dest,val) do { NEW_I8CONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
465
466 #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)
467
468 #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)
469
470 #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)
471
472 #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)
473
474 #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)
475
476 #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)
477
478 #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)
479
480 #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)
481
482 #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)
483
484 #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)
485
486 #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)
487
488 #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)
489
490 #define EMIT_NEW_TLS_OFFSETCONST(cfg,dest,key) do { NEW_TLS_OFFSETCONST ((cfg), (dest), (key)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
491
492 #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
493
494 #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
495
496 #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)
497
498 #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)
499
500 #define EMIT_NEW_VARLOAD(cfg,dest,var,vartype) do { NEW_VARLOAD ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
501
502 #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)
503
504 #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
505
506 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
507
508 /*
509  * Since the IL stack (and our vregs) contain double values, we have to do a conversion
510  * when loading/storing args/locals of type R4.
511  */
512
513 #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
514                 if (!COMPILE_LLVM ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
515                         MonoInst *iargs [1]; \
516                         EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
517                         (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
518                 } else { \
519                         EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
520                 } \
521         } while (0)
522
523 #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do {        \
524                 if (COMPILE_SOFT_FLOAT ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
525                         MonoInst *iargs [2]; \
526                         iargs [0] = (inst); \
527                         EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
528                         (dest) = mono_emit_jit_icall (cfg, mono_fstore_r4, iargs);      \
529                 } else { \
530                         EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
531                 } \
532         } while (0)
533
534 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do {     \
535                 if (mono_arch_is_soft_float ()) {       \
536                         EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)]);     \
537                 } else {        \
538                         NEW_ARGLOAD ((cfg), (dest), (num));     \
539                         MONO_ADD_INS ((cfg)->cbb, (dest));      \
540                 }       \
541         } while (0)
542
543 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do {     \
544                 if (mono_arch_is_soft_float ()) {       \
545                         EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)]);   \
546                 } else {        \
547                         NEW_LOCLOAD ((cfg), (dest), (num));     \
548                         MONO_ADD_INS ((cfg)->cbb, (dest));      \
549                 }       \
550         } while (0)
551
552 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do {       \
553                 if (mono_arch_is_soft_float ()) {       \
554                         EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst));     \
555                 } else {        \
556                         NEW_LOCSTORE ((cfg), (dest), (num), (inst));    \
557                         MONO_ADD_INS ((cfg)->cbb, (dest));      \
558                 }       \
559         } while (0)
560
561 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do {       \
562                 if (mono_arch_is_soft_float ()) {       \
563                         EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst));    \
564                 } else {        \
565                         NEW_ARGSTORE ((cfg), (dest), (num), (inst));    \
566                         MONO_ADD_INS ((cfg)->cbb, (dest));      \
567                 }       \
568         } while (0)
569
570 #else
571
572 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { NEW_ARGLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
573
574 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { NEW_LOCLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
575
576 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { NEW_LOCSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
577
578 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
579
580 #endif
581
582 #define EMIT_NEW_TEMPLOAD(cfg,dest,num) do { NEW_TEMPLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
583
584 #define EMIT_NEW_TEMPLOADA(cfg,dest,num) do { NEW_TEMPLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
585
586 #define EMIT_NEW_LOCLOADA(cfg,dest,num) do { NEW_LOCLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
587
588 #define EMIT_NEW_ARGLOADA(cfg,dest,num) do { NEW_ARGLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
589
590 #define EMIT_NEW_RETLOADA(cfg,dest) do { NEW_RETLOADA ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
591
592 #define EMIT_NEW_TEMPSTORE(cfg,dest,num,inst) do { NEW_TEMPSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
593
594 #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)
595
596 #define EMIT_NEW_DUMMY_USE(cfg,dest,var) do { NEW_DUMMY_USE ((cfg), (dest), (var)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
597
598 #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)
599
600 #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)
601
602 #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)
603
604 #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)
605
606 #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)
607
608 #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)
609
610 #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)
611
612 #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)
613 /*
614  * Variants which do not take an dest argument, but take a dreg argument.
615  */
616 #define MONO_EMIT_NEW_ICONST(cfg,dr,imm) do { \
617         MonoInst *inst; \
618         MONO_INST_NEW ((cfg), (inst), OP_ICONST); \
619         inst->dreg = dr; \
620         inst->inst_c0 = imm; \
621             MONO_ADD_INS ((cfg)->cbb, inst); \
622         } while (0)
623
624 #define MONO_EMIT_NEW_PCONST(cfg,dr,val) do {   \
625         MonoInst *inst; \
626         MONO_INST_NEW ((cfg), (inst), OP_PCONST); \
627         inst->dreg = dr; \
628                 (inst)->inst_p0 = (val);        \
629                 (inst)->type = STACK_PTR;       \
630             MONO_ADD_INS ((cfg)->cbb, inst); \
631         } while (0)
632
633 #define MONO_EMIT_NEW_I8CONST(cfg,dr,imm) do { \
634         MonoInst *inst; \
635         MONO_INST_NEW ((cfg), (inst), OP_I8CONST); \
636         inst->dreg = dr; \
637         inst->inst_l = imm; \
638             MONO_ADD_INS ((cfg)->cbb, inst); \
639         } while (0)
640
641 #define MONO_EMIT_NEW_DUMMY_INIT(cfg,dr,op) do {                          \
642                 MonoInst *inst;                                                                           \
643                 MONO_INST_NEW ((cfg), (inst), (op));                              \
644                 inst->dreg = dr;                                                                          \
645                 MONO_ADD_INS ((cfg)->cbb, inst);                                          \
646         } while (0)
647
648 #ifdef MONO_ARCH_NEED_GOT_VAR
649
650 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
651         MonoInst *inst; \
652         NEW_AOTCONST ((cfg), (inst), (patch_type), (cons)); \
653         inst->dreg = (dr); \
654         MONO_ADD_INS ((cfg)->cbb, inst); \
655     } while (0)
656
657 #else
658
659 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,type) do { \
660         MonoInst *inst; \
661         MONO_INST_NEW ((cfg), (inst), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
662         inst->dreg = dr; \
663         inst->inst_p0 = imm; \
664         inst->inst_c1 = type; \
665                 MONO_ADD_INS ((cfg)->cbb, inst); \
666         } while (0)
667
668 #endif
669
670 #define MONO_EMIT_NEW_CLASSCONST(cfg,dr,imm) MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,MONO_PATCH_INFO_CLASS)
671 #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)
672 #define MONO_EMIT_NEW_SIGNATURECONST(cfg,dr,sig) MONO_EMIT_NEW_AOTCONST ((cfg), (dr), (sig), MONO_PATCH_INFO_SIGNATURE)
673
674 #define MONO_EMIT_NEW_VZERO(cfg,dr,kl) do {     \
675         MonoInst *inst; \
676         MONO_INST_NEW ((cfg), (inst), MONO_CLASS_IS_SIMD (cfg, kl) ? OP_XZERO : OP_VZERO); \
677         inst->dreg = dr; \
678                 (inst)->type = STACK_VTYPE;     \
679         (inst)->klass = (kl); \
680             MONO_ADD_INS ((cfg)->cbb, inst); \
681         } while (0)
682
683 #define MONO_EMIT_NEW_UNALU(cfg,op,dr,sr1) do { \
684         MonoInst *inst; \
685         EMIT_NEW_UNALU ((cfg), (inst), (op), (dr), (sr1)); \
686         } while (0)
687
688 #define MONO_EMIT_NEW_BIALU(cfg,op,dr,sr1,sr2) do { \
689         MonoInst *inst; \
690         MONO_INST_NEW ((cfg), (inst), (op)); \
691         inst->dreg = dr; \
692         inst->sreg1 = sr1; \
693         inst->sreg2 = sr2; \
694             MONO_ADD_INS (cfg->cbb, inst); \
695         } while (0)
696
697 #define MONO_EMIT_NEW_BIALU_IMM(cfg,op,dr,sr,imm) do { \
698         MonoInst *inst; \
699         MONO_INST_NEW ((cfg), (inst), (op)); \
700         inst->dreg = dr; \
701         inst->sreg1 = sr; \
702         inst->inst_imm = (mgreg_t)(imm);                \
703             MONO_ADD_INS (cfg->cbb, inst); \
704         } while (0)
705
706 #define MONO_EMIT_NEW_COMPARE_IMM(cfg,sr1,imm) do { \
707         MonoInst *inst; \
708         MONO_INST_NEW ((cfg), (inst), (OP_COMPARE_IMM)); \
709         inst->sreg1 = sr1; \
710         inst->inst_imm = (imm);                  \
711             MONO_ADD_INS ((cfg)->cbb, inst); \
712         } while (0)
713
714 #define MONO_EMIT_NEW_ICOMPARE_IMM(cfg,sr1,imm) do { \
715         MonoInst *inst; \
716         MONO_INST_NEW ((cfg), (inst), sizeof (mgreg_t) == 8 ? OP_ICOMPARE_IMM : OP_COMPARE_IMM); \
717         inst->sreg1 = sr1; \
718         inst->inst_imm = (imm);                  \
719             MONO_ADD_INS ((cfg)->cbb, inst); \
720         } while (0)
721
722 /* This is used on 32 bit machines too when running with LLVM */
723 #define MONO_EMIT_NEW_LCOMPARE_IMM(cfg,sr1,imm) do { \
724         MonoInst *inst; \
725         MONO_INST_NEW ((cfg), (inst), (OP_LCOMPARE_IMM)); \
726         inst->sreg1 = sr1;                                                                      \
727         if (SIZEOF_REGISTER == 4 && COMPILE_LLVM (cfg))  {      \
728                         guint64 _l = (imm);                                                             \
729                         inst->inst_imm = _l & 0xffffffff;                               \
730                         inst->inst_offset = _l >> 32;                                           \
731                 } else { \
732                         inst->inst_imm = (imm);          \
733                 }                                                                \
734             MONO_ADD_INS ((cfg)->cbb, inst); \
735         } while (0)
736
737 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg,op,dr,base,offset) do { \
738         MonoInst *inst; \
739         MONO_INST_NEW ((cfg), (inst), (op)); \
740         inst->dreg = dr; \
741         inst->inst_basereg = base; \
742         inst->inst_offset = offset; \
743             MONO_ADD_INS (cfg->cbb, inst); \
744     } while (0)
745
746 #define MONO_EMIT_NEW_LOAD_MEMBASE(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
747
748 #define MONO_EMIT_NEW_STORE_MEMBASE(cfg,op,base,offset,sr) do { \
749         MonoInst *inst; \
750         MONO_INST_NEW ((cfg), (inst), (op)); \
751         (inst)->sreg1 = sr; \
752         (inst)->inst_destbasereg = base; \
753         (inst)->inst_offset = offset; \
754             MONO_ADD_INS (cfg->cbb, inst); \
755         } while (0)
756
757 #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
758         MonoInst *inst; \
759         MONO_INST_NEW ((cfg), (inst), (op)); \
760         inst->inst_destbasereg = base; \
761         inst->inst_offset = offset; \
762         inst->inst_imm = (mgreg_t)(imm); \
763         MONO_ADD_INS ((cfg)->cbb, inst); \
764         } while (0)
765
766 #define MONO_EMIT_NEW_COND_EXC(cfg,cond,name) do { \
767         MonoInst *inst; \
768         MONO_INST_NEW ((cfg), (inst), (OP_COND_EXC_##cond)); \
769         inst->inst_p1 = (char*)name; \
770             MONO_ADD_INS ((cfg)->cbb, inst); \
771         } while (0)
772
773 /* Branch support */
774
775 /*
776  * Basic blocks have two numeric identifiers:
777  * dfn: Depth First Number
778  * block_num: unique ID assigned at bblock creation
779  */
780 #define NEW_BBLOCK(cfg,bblock) do { \
781         (bblock) = (MonoBasicBlock *)mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
782         (bblock)->block_num = cfg->num_bblocks++; \
783     } while (0)
784
785 #define ADD_BBLOCK(cfg,b) do {  \
786         if ((b)->cil_code)  {\
787                         cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b);   \
788         } \
789                 (b)->real_offset = cfg->real_offset;    \
790         } while (0)
791
792 /*
793  * Emit a one-way conditional branch and start a new bblock.
794  * The inst_false_bb field of the cond branch will not be set, the JIT code should be
795  * prepared to deal with this.
796  */
797 #ifdef DEBUG_EXTENDED_BBLOCKS
798 static int ccount = 0;
799 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
800         MonoInst *ins; \
801         MonoBasicBlock *falsebb; \
802         MONO_INST_NEW ((cfg), (ins), (op)); \
803         if ((op) == OP_BR) { \
804                 NEW_BBLOCK ((cfg), falsebb); \
805             ins->inst_target_bb = (truebb); \
806             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
807             MONO_ADD_INS ((cfg)->cbb, ins); \
808             MONO_START_BB ((cfg), falsebb); \
809         } else { \
810             ccount ++; \
811                     ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);  \
812             ins->inst_true_bb = (truebb); \
813             ins->inst_false_bb = NULL; \
814             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
815             MONO_ADD_INS ((cfg)->cbb, ins); \
816             char *count2 = g_getenv ("COUNT2"); \
817             if (count2 && ccount == atoi (count2) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
818             if (count2 && ccount < atoi (count2)) { \
819                  cfg->cbb->extended = TRUE; \
820             } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
821             if (count2) g_free (count2); \
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