Merge pull request #3988 from lambdageek/dev-handles-srmodule
[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_TLS_OFFSETCONST(cfg,dest,key) do { \
313         if (cfg->compile_aot) { \
314                 NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (key)); \
315         } else {                                                                                                                        \
316                 int _offset = mini_get_tls_offset ((key));                                                      \
317                 NEW_PCONST ((cfg), (dest), GINT_TO_POINTER (_offset)); \
318                 } \
319         } while (0)
320
321 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
322                 if (cfg->compile_aot) { \
323                         NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, NULL, STACK_OBJ, NULL); \
324                 } else { \
325                         NEW_PCONST (cfg, args [0], (entry).blob); \
326                 } \
327         } while (0)
328
329 #define NEW_METHOD_RGCTX_CONST(cfg,dest,method) do { \
330                 if (cfg->compile_aot) {                                                                                 \
331                         NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHOD_RGCTX, (method)); \
332                 } else {                                                                                                                \
333                         MonoMethodRuntimeGenericContext *mrgctx;                                        \
334                         mrgctx = mono_method_lookup_rgctx (mono_class_vtable ((cfg)->domain, (method)->klass), mini_method_get_context ((method))->method_inst); \
335                         NEW_PCONST ((cfg), (dest), (mrgctx));                                           \
336                 }                                                                                                                               \
337         } while (0)
338
339 #define NEW_DOMAINCONST(cfg,dest) do { \
340                 if ((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) {                          \
341                         /* avoid depending on undefined C behavior in sequence points */ \
342                         MonoInst* __domain_var = mono_get_domainvar (cfg); \
343                         NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
344                 } else { \
345                         NEW_PCONST (cfg, dest, (cfg)->domain); \
346                 } \
347         } while (0)
348
349 #define NEW_JIT_ICALL_ADDRCONST(cfg,dest,name) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_JIT_ICALL_ADDR, (name))
350
351 #define NEW_VARLOAD(cfg,dest,var,vartype) do { \
352         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
353                 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype));  \
354                 type_to_eval_stack_type ((cfg), (vartype), (dest));     \
355                 (dest)->klass = var->klass;     \
356                 (dest)->sreg1 = var->dreg;   \
357         (dest)->dreg = alloc_dreg ((cfg), (MonoStackType)(dest)->type); \
358         if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
359         } while (0)
360
361 #define DECOMPOSE_INTO_REGPAIR(stack_type) (mono_arch_is_soft_float () ? ((stack_type) == STACK_I8 || (stack_type) == STACK_R8) : ((stack_type) == STACK_I8))
362
363 static inline void
364 handle_gsharedvt_ldaddr (MonoCompile *cfg)
365 {
366         /* The decomposition of ldaddr makes use of these two variables, so add uses for them */
367         MonoInst *use;
368
369         MONO_INST_NEW (cfg, use, OP_DUMMY_USE);
370         use->sreg1 = cfg->gsharedvt_info_var->dreg;
371         MONO_ADD_INS (cfg->cbb, use);
372         MONO_INST_NEW (cfg, use, OP_DUMMY_USE);
373         use->sreg1 = cfg->gsharedvt_locals_var->dreg;
374         MONO_ADD_INS (cfg->cbb, use);
375 }
376
377 #define NEW_VARLOADA(cfg,dest,var,vartype) do { \
378         MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
379                 (dest)->inst_p0 = (var); \
380                 (var)->flags |= MONO_INST_INDIRECT;     \
381                 (dest)->type = STACK_MP;        \
382                 (dest)->klass = (var)->klass;   \
383         (dest)->dreg = alloc_dreg ((cfg), STACK_MP); \
384                 (cfg)->has_indirection = TRUE;  \
385                           if (G_UNLIKELY (cfg->gsharedvt) && mini_is_gsharedvt_variable_type ((var)->inst_vtype)) { handle_gsharedvt_ldaddr ((cfg)); } \
386                 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; } \
387         } while (0)
388
389 #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do {    \
390         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
391                 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype));    \
392                 (dest)->klass = (var)->klass;   \
393         (dest)->sreg1 = (inst)->dreg; \
394                 (dest)->dreg = (var)->dreg;   \
395         if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
396         } while (0)
397
398 #define NEW_TEMPLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype)
399
400 #define NEW_TEMPLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), cfg->varinfo [(num)], cfg->varinfo [(num)]->inst_vtype)
401
402 #define NEW_TEMPSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype, (inst))
403
404 #define NEW_ARGLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
405
406 #define NEW_LOCLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
407
408 #define NEW_LOCSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
409
410 #define NEW_ARGSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
411
412 #define NEW_LOCLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype)
413
414 #define NEW_RETLOADA(cfg,dest) do {     \
415         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
416         (dest)->type = STACK_MP; \
417             (dest)->klass = cfg->ret->klass;    \
418             (dest)->sreg1 = cfg->vret_addr->dreg;   \
419         (dest)->dreg = alloc_dreg ((cfg), (MonoStackType)(dest)->type); \
420         } while (0)
421
422 #define NEW_ARGLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), arg_array [(num)], param_types [(num)])
423
424 /* Promote the vreg to a variable so its address can be taken */
425 #define NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { \
426         MonoInst *var = get_vreg_to_inst ((cfg), (vreg)); \
427         if (!var) \
428                     var = mono_compile_create_var_for_vreg ((cfg), (ltype), OP_LOCAL, (vreg)); \
429         NEW_VARLOADA ((cfg), (dest), (var), (ltype)); \
430     } while (0)
431
432 #define NEW_DUMMY_USE(cfg,dest,var) do { \
433         MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
434                 (dest)->sreg1 = var->dreg; \
435     } while (0)
436
437 /* Variants which take a type argument and handle vtypes as well */
438 #define NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { \
439             NEW_LOAD_MEMBASE ((cfg), (dest), mono_type_to_load_membase ((cfg), (ltype)), 0, (base), (offset)); \
440             type_to_eval_stack_type ((cfg), (ltype), (dest)); \
441             (dest)->dreg = alloc_dreg ((cfg), (MonoStackType)(dest)->type); \
442     } while (0)
443
444 #define NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { \
445         MONO_INST_NEW ((cfg), (dest), mono_type_to_store_membase ((cfg), (ltype))); \
446         (dest)->sreg1 = sr; \
447         (dest)->inst_destbasereg = base; \
448         (dest)->inst_offset = offset; \
449             type_to_eval_stack_type ((cfg), (ltype), (dest)); \
450         (dest)->klass = mono_class_from_mono_type (ltype); \
451         } while (0)
452
453 #define NEW_SEQ_POINT(cfg,dest,il_offset,intr_loc) do {  \
454         MONO_INST_NEW ((cfg), (dest), cfg->gen_sdb_seq_points ? OP_SEQ_POINT : OP_IL_SEQ_POINT); \
455         (dest)->inst_imm = (il_offset); \
456         (dest)->flags = intr_loc ? MONO_INST_SINGLE_STEP_LOC : 0; \
457         } while (0)
458
459 #define NEW_GC_PARAM_SLOT_LIVENESS_DEF(cfg,dest,offset,type) do { \
460         MONO_INST_NEW ((cfg), (dest), OP_GC_PARAM_SLOT_LIVENESS_DEF); \
461         (dest)->inst_offset = (offset); \
462         (dest)->inst_vtype = (type); \
463         } while (0)
464
465 /*
466  * Variants which do an emit as well.
467  */
468 #define EMIT_NEW_ICONST(cfg,dest,val) do { NEW_ICONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
469
470 #define EMIT_NEW_PCONST(cfg,dest,val) do { NEW_PCONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
471
472 #define EMIT_NEW_I8CONST(cfg,dest,val) do { NEW_I8CONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
473
474 #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)
475
476 #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)
477
478 #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)
479
480 #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)
481
482 #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)
483
484 #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)
485
486 #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)
487
488 #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)
489
490 #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)
491
492 #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)
493
494 #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)
495
496 #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)
497
498 #define EMIT_NEW_TLS_OFFSETCONST(cfg,dest,key) do { NEW_TLS_OFFSETCONST ((cfg), (dest), (key)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
499
500 #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
501
502 #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
503
504 #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)
505
506 #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)
507
508 #define EMIT_NEW_VARLOAD(cfg,dest,var,vartype) do { NEW_VARLOAD ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
509
510 #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)
511
512 #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
513
514 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
515
516 /*
517  * Since the IL stack (and our vregs) contain double values, we have to do a conversion
518  * when loading/storing args/locals of type R4.
519  */
520
521 #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
522                 if (!COMPILE_LLVM ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
523                         MonoInst *iargs [1]; \
524                         EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
525                         (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
526                 } else { \
527                         EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
528                 } \
529         } while (0)
530
531 #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do {        \
532                 if (COMPILE_SOFT_FLOAT ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
533                         MonoInst *iargs [2]; \
534                         iargs [0] = (inst); \
535                         EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
536                         (dest) = mono_emit_jit_icall (cfg, mono_fstore_r4, iargs);      \
537                 } else { \
538                         EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
539                 } \
540         } while (0)
541
542 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do {     \
543                 if (mono_arch_is_soft_float ()) {       \
544                         EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)]);     \
545                 } else {        \
546                         NEW_ARGLOAD ((cfg), (dest), (num));     \
547                         MONO_ADD_INS ((cfg)->cbb, (dest));      \
548                 }       \
549         } while (0)
550
551 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do {     \
552                 if (mono_arch_is_soft_float ()) {       \
553                         EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)]);   \
554                 } else {        \
555                         NEW_LOCLOAD ((cfg), (dest), (num));     \
556                         MONO_ADD_INS ((cfg)->cbb, (dest));      \
557                 }       \
558         } while (0)
559
560 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do {       \
561                 if (mono_arch_is_soft_float ()) {       \
562                         EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst));     \
563                 } else {        \
564                         NEW_LOCSTORE ((cfg), (dest), (num), (inst));    \
565                         MONO_ADD_INS ((cfg)->cbb, (dest));      \
566                 }       \
567         } while (0)
568
569 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do {       \
570                 if (mono_arch_is_soft_float ()) {       \
571                         EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst));    \
572                 } else {        \
573                         NEW_ARGSTORE ((cfg), (dest), (num), (inst));    \
574                         MONO_ADD_INS ((cfg)->cbb, (dest));      \
575                 }       \
576         } while (0)
577
578 #else
579
580 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { NEW_ARGLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
581
582 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { NEW_LOCLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
583
584 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { NEW_LOCSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
585
586 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
587
588 #endif
589
590 #define EMIT_NEW_TEMPLOAD(cfg,dest,num) do { NEW_TEMPLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
591
592 #define EMIT_NEW_TEMPLOADA(cfg,dest,num) do { NEW_TEMPLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
593
594 #define EMIT_NEW_LOCLOADA(cfg,dest,num) do { NEW_LOCLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
595
596 #define EMIT_NEW_ARGLOADA(cfg,dest,num) do { NEW_ARGLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
597
598 #define EMIT_NEW_RETLOADA(cfg,dest) do { NEW_RETLOADA ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
599
600 #define EMIT_NEW_TEMPSTORE(cfg,dest,num,inst) do { NEW_TEMPSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
601
602 #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)
603
604 #define EMIT_NEW_DUMMY_USE(cfg,dest,var) do { NEW_DUMMY_USE ((cfg), (dest), (var)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
605
606 #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)
607
608 #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)
609
610 #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)
611
612 #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)
613
614 #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)
615
616 #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)
617
618 #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)
619
620 #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)
621 /*
622  * Variants which do not take an dest argument, but take a dreg argument.
623  */
624 #define MONO_EMIT_NEW_ICONST(cfg,dr,imm) do { \
625         MonoInst *inst; \
626         MONO_INST_NEW ((cfg), (inst), OP_ICONST); \
627         inst->dreg = dr; \
628         inst->inst_c0 = imm; \
629             MONO_ADD_INS ((cfg)->cbb, inst); \
630         } while (0)
631
632 #define MONO_EMIT_NEW_PCONST(cfg,dr,val) do {   \
633         MonoInst *inst; \
634         MONO_INST_NEW ((cfg), (inst), OP_PCONST); \
635         inst->dreg = dr; \
636                 (inst)->inst_p0 = (val);        \
637                 (inst)->type = STACK_PTR;       \
638             MONO_ADD_INS ((cfg)->cbb, inst); \
639         } while (0)
640
641 #define MONO_EMIT_NEW_I8CONST(cfg,dr,imm) do { \
642         MonoInst *inst; \
643         MONO_INST_NEW ((cfg), (inst), OP_I8CONST); \
644         inst->dreg = dr; \
645         inst->inst_l = imm; \
646             MONO_ADD_INS ((cfg)->cbb, inst); \
647         } while (0)
648
649 #define MONO_EMIT_NEW_DUMMY_INIT(cfg,dr,op) do {                          \
650                 MonoInst *inst;                                                                           \
651                 MONO_INST_NEW ((cfg), (inst), (op));                              \
652                 inst->dreg = dr;                                                                          \
653                 MONO_ADD_INS ((cfg)->cbb, inst);                                          \
654         } while (0)
655
656 #ifdef MONO_ARCH_NEED_GOT_VAR
657
658 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
659         MonoInst *inst; \
660         NEW_AOTCONST ((cfg), (inst), (patch_type), (cons)); \
661         inst->dreg = (dr); \
662         MONO_ADD_INS ((cfg)->cbb, inst); \
663     } while (0)
664
665 #else
666
667 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,type) do { \
668         MonoInst *inst; \
669         MONO_INST_NEW ((cfg), (inst), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
670         inst->dreg = dr; \
671         inst->inst_p0 = imm; \
672         inst->inst_c1 = type; \
673                 MONO_ADD_INS ((cfg)->cbb, inst); \
674         } while (0)
675
676 #endif
677
678 #define MONO_EMIT_NEW_CLASSCONST(cfg,dr,imm) MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,MONO_PATCH_INFO_CLASS)
679 #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)
680 #define MONO_EMIT_NEW_SIGNATURECONST(cfg,dr,sig) MONO_EMIT_NEW_AOTCONST ((cfg), (dr), (sig), MONO_PATCH_INFO_SIGNATURE)
681
682 #define MONO_EMIT_NEW_VZERO(cfg,dr,kl) do {     \
683         MonoInst *inst; \
684         MONO_INST_NEW ((cfg), (inst), MONO_CLASS_IS_SIMD (cfg, kl) ? OP_XZERO : OP_VZERO); \
685         inst->dreg = dr; \
686                 (inst)->type = STACK_VTYPE;     \
687         (inst)->klass = (kl); \
688             MONO_ADD_INS ((cfg)->cbb, inst); \
689         } while (0)
690
691 #define MONO_EMIT_NEW_UNALU(cfg,op,dr,sr1) do { \
692         MonoInst *inst; \
693         EMIT_NEW_UNALU ((cfg), (inst), (op), (dr), (sr1)); \
694         } while (0)
695
696 #define MONO_EMIT_NEW_BIALU(cfg,op,dr,sr1,sr2) do { \
697         MonoInst *inst; \
698         MONO_INST_NEW ((cfg), (inst), (op)); \
699         inst->dreg = dr; \
700         inst->sreg1 = sr1; \
701         inst->sreg2 = sr2; \
702             MONO_ADD_INS (cfg->cbb, inst); \
703         } while (0)
704
705 #define MONO_EMIT_NEW_BIALU_IMM(cfg,op,dr,sr,imm) do { \
706         MonoInst *inst; \
707         MONO_INST_NEW ((cfg), (inst), (op)); \
708         inst->dreg = dr; \
709         inst->sreg1 = sr; \
710         inst->inst_imm = (mgreg_t)(imm);                \
711             MONO_ADD_INS (cfg->cbb, inst); \
712         } while (0)
713
714 #define MONO_EMIT_NEW_COMPARE_IMM(cfg,sr1,imm) do { \
715         MonoInst *inst; \
716         MONO_INST_NEW ((cfg), (inst), (OP_COMPARE_IMM)); \
717         inst->sreg1 = sr1; \
718         inst->inst_imm = (imm);                  \
719             MONO_ADD_INS ((cfg)->cbb, inst); \
720         } while (0)
721
722 #define MONO_EMIT_NEW_ICOMPARE_IMM(cfg,sr1,imm) do { \
723         MonoInst *inst; \
724         MONO_INST_NEW ((cfg), (inst), sizeof (mgreg_t) == 8 ? OP_ICOMPARE_IMM : OP_COMPARE_IMM); \
725         inst->sreg1 = sr1; \
726         inst->inst_imm = (imm);                  \
727             MONO_ADD_INS ((cfg)->cbb, inst); \
728         } while (0)
729
730 /* This is used on 32 bit machines too when running with LLVM */
731 #define MONO_EMIT_NEW_LCOMPARE_IMM(cfg,sr1,imm) do { \
732         MonoInst *inst; \
733         MONO_INST_NEW ((cfg), (inst), (OP_LCOMPARE_IMM)); \
734         inst->sreg1 = sr1;                                                                      \
735         if (SIZEOF_REGISTER == 4 && COMPILE_LLVM (cfg))  {      \
736                         guint64 _l = (imm);                                                             \
737                         inst->inst_imm = _l & 0xffffffff;                               \
738                         inst->inst_offset = _l >> 32;                                           \
739                 } else { \
740                         inst->inst_imm = (imm);          \
741                 }                                                                \
742             MONO_ADD_INS ((cfg)->cbb, inst); \
743         } while (0)
744
745 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg,op,dr,base,offset) do { \
746         MonoInst *inst; \
747         MONO_INST_NEW ((cfg), (inst), (op)); \
748         inst->dreg = dr; \
749         inst->inst_basereg = base; \
750         inst->inst_offset = offset; \
751             MONO_ADD_INS (cfg->cbb, inst); \
752     } while (0)
753
754 #define MONO_EMIT_NEW_LOAD_MEMBASE(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
755
756 #define MONO_EMIT_NEW_STORE_MEMBASE(cfg,op,base,offset,sr) do { \
757         MonoInst *inst; \
758         MONO_INST_NEW ((cfg), (inst), (op)); \
759         (inst)->sreg1 = sr; \
760         (inst)->inst_destbasereg = base; \
761         (inst)->inst_offset = offset; \
762             MONO_ADD_INS (cfg->cbb, inst); \
763         } while (0)
764
765 #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
766         MonoInst *inst; \
767         MONO_INST_NEW ((cfg), (inst), (op)); \
768         inst->inst_destbasereg = base; \
769         inst->inst_offset = offset; \
770         inst->inst_imm = (mgreg_t)(imm); \
771         MONO_ADD_INS ((cfg)->cbb, inst); \
772         } while (0)
773
774 #define MONO_EMIT_NEW_COND_EXC(cfg,cond,name) do { \
775         MonoInst *inst; \
776         MONO_INST_NEW ((cfg), (inst), (OP_COND_EXC_##cond)); \
777         inst->inst_p1 = (char*)name; \
778             MONO_ADD_INS ((cfg)->cbb, inst); \
779         } while (0)
780
781 /* Branch support */
782
783 /*
784  * Basic blocks have two numeric identifiers:
785  * dfn: Depth First Number
786  * block_num: unique ID assigned at bblock creation
787  */
788 #define NEW_BBLOCK(cfg,bblock) do { \
789         (bblock) = (MonoBasicBlock *)mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
790         (bblock)->block_num = cfg->num_bblocks++; \
791     } while (0)
792
793 #define ADD_BBLOCK(cfg,b) do {  \
794         if ((b)->cil_code)  {\
795                         cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b);   \
796         } \
797                 (b)->real_offset = cfg->real_offset;    \
798         } while (0)
799
800 /*
801  * Emit a one-way conditional branch and start a new bblock.
802  * The inst_false_bb field of the cond branch will not be set, the JIT code should be
803  * prepared to deal with this.
804  */
805 #ifdef DEBUG_EXTENDED_BBLOCKS
806 static int ccount = 0;
807 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
808         MonoInst *ins; \
809         MonoBasicBlock *falsebb; \
810         MONO_INST_NEW ((cfg), (ins), (op)); \
811         if ((op) == OP_BR) { \
812                 NEW_BBLOCK ((cfg), falsebb); \
813             ins->inst_target_bb = (truebb); \
814             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
815             MONO_ADD_INS ((cfg)->cbb, ins); \
816             MONO_START_BB ((cfg), falsebb); \
817         } else { \
818             ccount ++; \
819                     ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);  \
820             ins->inst_true_bb = (truebb); \
821             ins->inst_false_bb = NULL; \
822             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
823             MONO_ADD_INS ((cfg)->cbb, ins); \
824             if (g_getenv ("COUNT2") && ccount == atoi (g_getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
825             if (g_getenv ("COUNT2") && ccount < atoi (g_getenv ("COUNT2"))) { \
826                  cfg->cbb->extended = TRUE; \
827             } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
828         } \
829         } while (0)
830 #else
831 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
832         MonoInst *ins; \
833         MonoBasicBlock *falsebb; \
834         MONO_INST_NEW ((cfg), (ins), (op)); \
835         if ((op) == OP_BR) { \
836                 NEW_BBLOCK ((cfg), falsebb); \
837             ins->inst_target_bb = (truebb); \
838             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
839             MONO_ADD_INS ((cfg)->cbb, ins); \
840             MONO_START_BB ((cfg), falsebb); \
841         } else { \
842                     ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);       \
843             ins->inst_true_bb = (truebb); \
844             ins->inst_false_bb = NULL; \
845             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
846             MONO_ADD_INS ((cfg)->cbb, ins); \
847             if (!cfg->enable_extended_bblocks) { \
848                 NEW_BBLOCK ((cfg), falsebb); \
849                 ins->inst_false_bb = falsebb; \
850                 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
851                 MONO_START_BB ((cfg), falsebb); \
852             } else { \
853                                 cfg->cbb->extended = TRUE; \
854                         } \
855         } \
856         } while (0)
857 #endif
858
859 /* Emit a two-way conditional branch */
860 #define MONO_EMIT_NEW_BRANCH_BLOCK2(cfg,op,truebb,falsebb) do { \
861         MonoInst *ins; \
862         MONO_INST_NEW ((cfg), (ins), (op)); \
863                 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);   \
864         ins->inst_true_bb = (truebb); \
865         ins->inst_false_bb = (falsebb); \
866         mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
867         mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
868         MONO_ADD_INS ((cfg)->cbb, ins); \
869         } while (0)
870
871 #define MONO_START_BB(cfg, bblock) do { \
872         ADD_BBLOCK ((cfg), (bblock)); \
873         if (cfg->cbb->last_ins && MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins) && !cfg->cbb->last_ins->inst_false_bb) { \
874             cfg->cbb->last_ins->inst_false_bb = (bblock); \
875             mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
876         } 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)))) \
877             mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
878             (cfg)->cbb->next_bb = (bblock); \
879             (cfg)->cbb = (bblock); \
880     } while (0)
881
882 /* This marks a place in code where an implicit exception could be thrown */
883 #define MONO_EMIT_NEW_IMPLICIT_EXCEPTION(cfg) do { \
884                 if (COMPILE_LLVM ((cfg))) {                                                                     \
885                         MONO_EMIT_NEW_UNALU (cfg, OP_IMPLICIT_EXCEPTION, -1, -1);       \
886                 } \
887         } while (0)
888
889 /* Loads/Stores which can fault are handled correctly by the LLVM mono branch */
890 #define MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE(cfg) do { \
891     } while (0)
892
893 /* Emit an explicit null check which doesn't depend on SIGSEGV signal handling */
894 #define MONO_EMIT_NULL_CHECK(cfg, reg) do { \
895                 if (cfg->explicit_null_checks) {                                                          \
896                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, (reg), 0); \
897                         MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException"); \
898                 } else {                        \
899                         MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE (cfg);                                              \
900                 }                                                                                                                               \
901         } while (0)
902
903 #define MONO_EMIT_NEW_CHECK_THIS(cfg, sreg) do { \
904                 cfg->flags |= MONO_CFG_HAS_CHECK_THIS;   \
905                 if (cfg->explicit_null_checks) {                 \
906                         MONO_EMIT_NULL_CHECK (cfg, sreg);                               \
907                 } else {                                                                                        \
908                         MONO_EMIT_NEW_UNALU (cfg, OP_CHECK_THIS, -1, sreg);                     \
909                         MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE (cfg);                      \
910                 }                                                                                                                               \
911                 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, sreg);                               \
912         } while (0)
913
914 #define NEW_LOAD_MEMBASE_FLAGS(cfg,dest,op,dr,base,offset,ins_flags) do {       \
915                 int __ins_flags = ins_flags; \
916                 if (__ins_flags & MONO_INST_FAULT) {                                                            \
917                         MONO_EMIT_NULL_CHECK ((cfg), (base));                                           \
918                 }                                                                                                                               \
919                 NEW_LOAD_MEMBASE ((cfg), (dest), (op), (dr), (base), (offset)); \
920                 (dest)->flags = (__ins_flags);                                                                  \
921         } while (0)
922
923 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS(cfg,op,dr,base,offset,ins_flags) do { \
924         MonoInst *inst;                                                                                                 \
925                 int __ins_flags = ins_flags; \
926             if (__ins_flags & MONO_INST_FAULT) {                                                                        \
927                         MONO_EMIT_NULL_CHECK ((cfg), (base));                                           \
928                 }                                                                                                                               \
929                 NEW_LOAD_MEMBASE ((cfg), (inst), (op), (dr), (base), (offset)); \
930                 inst->flags = (__ins_flags); \
931             MONO_ADD_INS (cfg->cbb, inst); \
932     } while (0)
933
934 #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))
935
936 /* A load which can cause a nullref */
937 #define NEW_LOAD_MEMBASE_FAULT(cfg,dest,op,dr,base,offset) NEW_LOAD_MEMBASE_FLAGS ((cfg), (dest), (op), (dr), (base), (offset), MONO_INST_FAULT)
938
939 #define EMIT_NEW_LOAD_MEMBASE_FAULT(cfg,dest,op,dr,base,offset) do { \
940                 NEW_LOAD_MEMBASE_FAULT ((cfg), (dest), (op), (dr), (base), (offset)); \
941                 MONO_ADD_INS ((cfg)->cbb, (dest)); \
942         } while (0)
943
944 #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)
945
946 #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))
947
948 #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)
949
950 #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)
951
952 #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))
953
954 /*Object Model related macros*/
955
956 /* Default bounds check implementation for most architectures + llvm */
957 #define MONO_EMIT_DEFAULT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg, fault) do { \
958                         int _length_reg = alloc_ireg (cfg); \
959                         if (fault) \
960                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset); \
961                         else \
962                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset, MONO_INST_INVARIANT_LOAD); \
963                         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, _length_reg, index_reg); \
964                         MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
965         } while (0)
966
967 #ifndef MONO_ARCH_EMIT_BOUNDS_CHECK
968 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) MONO_EMIT_DEFAULT_BOUNDS_CHECK ((cfg), (array_reg), (offset), (index_reg), TRUE)
969 #endif
970
971 /* cfg is the MonoCompile been used
972  * array_reg is the vreg holding the array object
973  * array_type is a struct (usually MonoArray or MonoString)
974  * array_length_field is the field in the previous struct with the length
975  * index_reg is the vreg holding the index
976  */
977 #define MONO_EMIT_BOUNDS_CHECK(cfg, array_reg, array_type, array_length_field, index_reg) do { \
978                 if (!(cfg->opt & MONO_OPT_UNSAFE)) {                                                    \
979                 if (!(cfg->opt & MONO_OPT_ABCREM)) {                                                    \
980                         MONO_EMIT_NULL_CHECK (cfg, array_reg);                                          \
981                         if (COMPILE_LLVM (cfg)) \
982                                 MONO_EMIT_DEFAULT_BOUNDS_CHECK ((cfg), (array_reg), MONO_STRUCT_OFFSET (array_type, array_length_field), (index_reg), TRUE); \
983                         else \
984                                 MONO_ARCH_EMIT_BOUNDS_CHECK ((cfg), (array_reg), MONO_STRUCT_OFFSET (array_type, array_length_field), (index_reg)); \
985                 } else {                                                                                                                \
986                         MonoInst *ins;                                                                                          \
987                         MONO_INST_NEW ((cfg), ins, OP_BOUNDS_CHECK);                            \
988                         ins->sreg1 = array_reg;                                                                         \
989                         ins->sreg2 = index_reg;                                                                         \
990                         ins->inst_imm = MONO_STRUCT_OFFSET (array_type, array_length_field); \
991                         ins->flags |= MONO_INST_FAULT; \
992                         MONO_ADD_INS ((cfg)->cbb, ins);                                                         \
993                         (cfg)->flags |= MONO_CFG_HAS_ARRAY_ACCESS;                                      \
994                         (cfg)->cbb->has_array_access = TRUE;                                            \
995                 }                                                                                                                               \
996                 }                                                                                                                               \
997     } while (0)
998
999 G_END_DECLS
1000
1001 #endif