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