2010-03-12 Jb Evain <jbevain@novell.com>
[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 #ifdef MONO_ARCH_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 #endif
54 }
55
56 static inline guint32
57 alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
58 {
59         switch (stack_type) {
60         case STACK_I4:
61         case STACK_PTR:
62         case STACK_MP:
63         case STACK_OBJ:
64                 return alloc_ireg (cfg);
65         case STACK_R8:
66                 return alloc_freg (cfg);
67         case STACK_I8:
68                 return alloc_lreg (cfg);
69         case STACK_VTYPE:
70                 return alloc_ireg (cfg);
71         default:
72                 g_warning ("Unknown stack type %x\n", stack_type);
73                 g_assert_not_reached ();
74                 return -1;
75         }
76 }
77
78 #undef MONO_INST_NEW
79 /* 
80  * FIXME: zeroing out some fields is not needed with the new IR, but the old 
81  * JIT code still uses the left and right fields, so it has to stay.
82  */
83 #define MONO_INST_NEW(cfg,dest,op) do { \
84                 (dest) = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoInst));        \
85                 (dest)->inst_c0 = (dest)->inst_c1 = 0; \
86                 (dest)->next = (dest)->prev = NULL;    \
87                 (dest)->opcode = (op);  \
88         (dest)->flags = 0; \
89         (dest)->type = 0; \
90         (dest)->dreg = -1;  \
91         MONO_INST_NULLIFY_SREGS ((dest));                   \
92         (dest)->cil_code = (cfg)->ip;  \
93         } while (0)
94
95 /*
96  * Variants which take a dest argument and don't do an emit
97  */
98 #define NEW_ICONST(cfg,dest,val) do {   \
99         MONO_INST_NEW ((cfg), (dest), OP_ICONST); \
100                 (dest)->inst_c0 = (val);        \
101                 (dest)->type = STACK_I4;        \
102                 (dest)->dreg = alloc_dreg ((cfg), STACK_I4);    \
103         } while (0)
104
105 /* 
106  * Avoid using this with a non-NULL val if possible as it is not AOT 
107  * compatible. Use one of the NEW_xxxCONST variants instead.
108  */
109 #define NEW_PCONST(cfg,dest,val) do {   \
110         MONO_INST_NEW ((cfg), (dest), OP_PCONST); \
111                 (dest)->inst_p0 = (val);        \
112                 (dest)->type = STACK_PTR;       \
113                 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR);   \
114         } while (0)
115
116 #define NEW_I8CONST(cfg,dest,val) do {  \
117         MONO_INST_NEW ((cfg), (dest), OP_I8CONST); \
118         (dest)->dreg = alloc_lreg ((cfg)); \
119         (dest)->type = STACK_I8; \
120         (dest)->inst_l = (val); \
121         } while (0)
122
123 #define NEW_STORE_MEMBASE(cfg,dest,op,base,offset,sr) do { \
124         MONO_INST_NEW ((cfg), (dest), (op)); \
125         (dest)->sreg1 = sr; \
126         (dest)->inst_destbasereg = base; \
127         (dest)->inst_offset = offset; \
128         } while (0)
129
130 #define NEW_LOAD_MEMBASE(cfg,dest,op,dr,base,offset) do { \
131         MONO_INST_NEW ((cfg), (dest), (op)); \
132         (dest)->dreg = (dr); \
133         (dest)->inst_basereg = (base); \
134         (dest)->inst_offset = (offset); \
135         (dest)->type = STACK_I4; \
136         } while (0)
137
138 #define NEW_LOAD_MEMBASE_FLAGS(cfg,dest,op,dr,base,offset,ins_flags) do {       \
139         MONO_INST_NEW ((cfg), (dest), (op)); \
140         (dest)->dreg = (dr); \
141         (dest)->inst_basereg = (base); \
142         (dest)->inst_offset = (offset); \
143         (dest)->type = STACK_I4; \
144                 (dest)->flags = (ins_flags); \
145         } while (0)
146
147 #define NEW_LOAD_MEM(cfg,dest,op,dr,mem) do { \
148         MONO_INST_NEW ((cfg), (dest), (op)); \
149         (dest)->dreg = (dr); \
150         (dest)->inst_p0 = (gpointer)(gssize)(mem); \
151         (dest)->type = STACK_I4; \
152         } while (0)
153
154 #define NEW_UNALU(cfg,dest,op,dr,sr1) do { \
155         MONO_INST_NEW ((cfg), (dest), (op)); \
156         (dest)->dreg = dr; \
157         (dest)->sreg1 = sr1; \
158     } while (0)        
159
160 #define NEW_BIALU(cfg,dest,op,dr,sr1,sr2) do { \
161         MONO_INST_NEW ((cfg), (dest), (op)); \
162         (dest)->dreg = (dr); \
163         (dest)->sreg1 = (sr1); \
164         (dest)->sreg2 = (sr2); \
165         } while (0)
166
167 #define NEW_BIALU_IMM(cfg,dest,op,dr,sr,imm) do { \
168         MONO_INST_NEW ((cfg), (dest), (op)); \
169         (dest)->dreg = dr; \
170         (dest)->sreg1 = sr; \
171         (dest)->inst_imm = (imm); \
172         } while (0)
173
174 #ifdef MONO_ARCH_NEED_GOT_VAR
175
176 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do {   \
177         MONO_INST_NEW ((cfg), (dest), OP_PATCH_INFO); \
178                 (dest)->inst_left = (gpointer)(el1);    \
179                 (dest)->inst_right = (gpointer)(el2);   \
180         } while (0)
181
182 /* FIXME: Add the PUSH_GOT_ENTRY optimizations */
183 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do {                     \
184         MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST); \
185                 if (cfg->compile_aot) {                                 \
186                         MonoInst *group, *got_loc;              \
187                         got_loc = mono_get_got_var (cfg);               \
188                         NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
189                         (dest)->inst_basereg = got_loc->dreg;                   \
190                         (dest)->inst_p1 = group;                        \
191                 } else {                                                \
192                         (dest)->inst_p0 = (cons);                       \
193                         (dest)->inst_i1 = (gpointer)(patch_type);       \
194                 }                                                       \
195                 (dest)->type = STACK_PTR;                               \
196                 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR);   \
197         } while (0)
198
199 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
200                 MonoInst *group, *got_loc;                      \
201         MONO_INST_NEW ((cfg), (dest), OP_GOT_ENTRY); \
202                 got_loc = mono_get_got_var (cfg);                       \
203                 NEW_PATCH_INFO ((cfg), group, NULL, patch_type);        \
204                 group->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
205                 (dest)->inst_basereg = got_loc->dreg;                           \
206                 (dest)->inst_p1 = group;                                \
207                 (dest)->type = (stack_type);                            \
208         (dest)->klass = (stack_class);          \
209                 (dest)->dreg = alloc_dreg ((cfg), (stack_type));        \
210         } while (0)
211
212 #else
213
214 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do {    \
215         MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
216                 (dest)->inst_p0 = (cons);       \
217                 (dest)->inst_i1 = (gpointer)(patch_type); \
218                 (dest)->type = STACK_PTR;       \
219                 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR);   \
220     } while (0)
221
222 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
223         MONO_INST_NEW ((cfg), (dest), OP_AOTCONST); \
224                 (dest)->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
225                 (dest)->inst_p1 = (gpointer)(patch_type); \
226                 (dest)->type = (stack_type);    \
227         (dest)->klass = (stack_class);          \
228                 (dest)->dreg = alloc_dreg ((cfg), (stack_type));        \
229     } while (0)
230
231 #endif
232
233 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
234
235 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
236
237 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
238
239 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
240
241 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
242
243 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
244
245 #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)
246
247 #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.monotype_class)
248
249 #define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), NULL, STACK_PTR, NULL)
250
251 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
252                 if (cfg->compile_aot) { \
253                         NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, NULL, STACK_OBJ, NULL); \
254                 } else { \
255                         NEW_PCONST (cfg, args [0], (entry).blob); \
256                 } \
257         } while (0)
258
259 #define NEW_METHOD_RGCTX_CONST(cfg,dest,method) do { \
260                 if (cfg->compile_aot) {                                                                                 \
261                         NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHOD_RGCTX, (method)); \
262                 } else {                                                                                                                \
263                         MonoMethodRuntimeGenericContext *mrgctx;                                        \
264                         mrgctx = mono_method_lookup_rgctx (mono_class_vtable ((cfg)->domain, (method)->klass), mini_method_get_context ((method))->method_inst); \
265                         NEW_PCONST ((cfg), (dest), (mrgctx));                                           \
266                 }                                                                                                                               \
267         } while (0)
268
269 #define NEW_DOMAINCONST(cfg,dest) do { \
270                 if ((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) {                          \
271                         /* avoid depending on undefined C behavior in sequence points */ \
272                         MonoInst* __domain_var = mono_get_domainvar (cfg); \
273                         NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
274                 } else { \
275                         NEW_PCONST (cfg, dest, (cfg)->domain); \
276                 } \
277         } while (0)
278
279 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
280
281 #define NEW_VARLOAD(cfg,dest,var,vartype) do { \
282         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
283                 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype));  \
284                 type_to_eval_stack_type ((cfg), (vartype), (dest));     \
285                 (dest)->klass = var->klass;     \
286                 (dest)->sreg1 = var->dreg;   \
287         (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
288         if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
289         } while (0)
290
291 #ifdef MONO_ARCH_SOFT_FLOAT
292 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8 || (stack_type) == STACK_R8)
293 #else
294 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8)
295 #endif
296
297 #define NEW_VARLOADA(cfg,dest,var,vartype) do { \
298         MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
299                 (dest)->inst_p0 = (var); \
300                 (var)->flags |= MONO_INST_INDIRECT;     \
301                 (dest)->type = STACK_MP;        \
302                 (dest)->klass = (var)->klass;   \
303         (dest)->dreg = alloc_dreg ((cfg), STACK_MP); \
304                 if (SIZEOF_REGISTER == 4 && DECOMPOSE_INTO_REGPAIR ((var)->type)) { MonoInst *var1 = get_vreg_to_inst (cfg, (var)->dreg + 1); MonoInst *var2 = get_vreg_to_inst (cfg, (var)->dreg + 2); g_assert (var1); g_assert (var2); var1->flags |= MONO_INST_INDIRECT; var2->flags |= MONO_INST_INDIRECT; } \
305         } while (0)
306
307 #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do {    \
308         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
309                 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype));    \
310                 (dest)->klass = (var)->klass;   \
311         (dest)->sreg1 = (inst)->dreg; \
312                 (dest)->dreg = (var)->dreg;   \
313         if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
314         } while (0)
315
316 #define NEW_TEMPLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype)
317
318 #define NEW_TEMPLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), cfg->varinfo [(num)], cfg->varinfo [(num)]->inst_vtype)
319
320 #define NEW_TEMPSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype, (inst))
321
322 #define NEW_ARGLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
323
324 #define NEW_LOCLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
325
326 #define NEW_LOCSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
327
328 #define NEW_ARGSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
329
330 #define NEW_LOCLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype)
331
332 #define NEW_RETLOADA(cfg,dest) do {     \
333         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
334         (dest)->type = STACK_MP; \
335             (dest)->klass = cfg->ret->klass;    \
336             (dest)->sreg1 = cfg->vret_addr->dreg;   \
337         (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
338         } while (0)
339
340 #define NEW_ARGLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), arg_array [(num)], param_types [(num)])
341
342 /* Promote the vreg to a variable so its address can be taken */
343 #define NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { \
344         MonoInst *var = get_vreg_to_inst ((cfg), (vreg)); \
345         if (!var) \
346                     var = mono_compile_create_var_for_vreg ((cfg), (ltype), OP_LOCAL, (vreg)); \
347         NEW_VARLOADA ((cfg), (dest), (var), (ltype)); \
348     } while (0)
349
350 #define NEW_DUMMY_USE(cfg,dest,var) do { \
351         MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
352                 (dest)->sreg1 = var->dreg; \
353     } while (0)
354
355 /* Variants which take a type argument and handle vtypes as well */
356 #define NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { \
357             NEW_LOAD_MEMBASE ((cfg), (dest), mono_type_to_load_membase ((cfg), (ltype)), 0, (base), (offset)); \
358             type_to_eval_stack_type ((cfg), (ltype), (dest)); \
359             (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
360     } while (0)
361
362 #define NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { \
363         MONO_INST_NEW ((cfg), (dest), mono_type_to_store_membase ((cfg), (ltype))); \
364         (dest)->sreg1 = sr; \
365         (dest)->inst_destbasereg = base; \
366         (dest)->inst_offset = offset; \
367             type_to_eval_stack_type ((cfg), (ltype), (dest)); \
368         (dest)->klass = mono_class_from_mono_type (ltype); \
369         } while (0)
370
371 #define NEW_SEQ_POINT(cfg,dest,il_offset,ss_loc) do {    \
372         MONO_INST_NEW ((cfg), (dest), OP_SEQ_POINT); \
373         (dest)->inst_imm = (il_offset); \
374         (dest)->flags = ss_loc ? MONO_INST_SINGLE_STEP_LOC : 0; \
375         } while (0)
376
377 /*
378  * Variants which do an emit as well.
379  */
380 #define EMIT_NEW_ICONST(cfg,dest,val) do { NEW_ICONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
381
382 #define EMIT_NEW_PCONST(cfg,dest,val) do { NEW_PCONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
383
384 #define EMIT_NEW_I8CONST(cfg,dest,val) do { NEW_I8CONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
385
386 #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)
387
388 #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)
389
390 #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)
391
392 #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)
393
394 #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)
395
396 #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)
397
398 #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)
399
400 #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)
401
402 #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)
403
404 #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.monotype_class); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
405
406 #define EMIT_NEW_LDTOKENCONST(cfg,dest,image,token) do { NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), NULL, STACK_PTR, NULL); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
407
408 #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
409
410 #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
411
412 #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)
413
414 #define EMIT_NEW_VARLOAD(cfg,dest,var,vartype) do { NEW_VARLOAD ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
415
416 #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)
417
418 #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
419
420
421 #ifdef MONO_ARCH_SOFT_FLOAT
422
423 /*
424  * Since the IL stack (and our vregs) contain double values, we have to do a conversion
425  * when loading/storing args/locals of type R4.
426  */
427
428 #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
429                 if (COMPILE_SOFT_FLOAT ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
430              MonoInst *iargs [1]; \
431              EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
432              (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
433         } else { \
434              EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
435         } \
436     } while (0)
437
438 #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do {        \
439                 if (COMPILE_SOFT_FLOAT ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
440              MonoInst *iargs [2]; \
441              iargs [0] = (inst); \
442              EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
443              mono_emit_jit_icall (cfg, mono_fstore_r4, iargs); \
444         } else { \
445              EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
446         } \
447     } while (0)
448
449 #define EMIT_NEW_ARGLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
450
451 #define EMIT_NEW_LOCLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
452
453 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
454
455 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
456
457 #else
458
459 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { NEW_ARGLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
460
461 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { NEW_LOCLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
462
463 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { NEW_LOCSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
464
465 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
466
467 #endif
468
469 #define EMIT_NEW_TEMPLOAD(cfg,dest,num) do { NEW_TEMPLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
470
471 #define EMIT_NEW_TEMPLOADA(cfg,dest,num) do { NEW_TEMPLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
472
473 #define EMIT_NEW_LOCLOADA(cfg,dest,num) do { NEW_LOCLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
474
475 #define EMIT_NEW_ARGLOADA(cfg,dest,num) do { NEW_ARGLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
476
477 #define EMIT_NEW_RETLOADA(cfg,dest) do { NEW_RETLOADA ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
478
479 #define EMIT_NEW_TEMPSTORE(cfg,dest,num,inst) do { NEW_TEMPSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
480
481 #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)
482
483 #define EMIT_NEW_DUMMY_USE(cfg,dest,var) do { NEW_DUMMY_USE ((cfg), (dest), (var)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
484
485 #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)
486
487 #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)
488
489 #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)
490
491 #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)
492
493 #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)
494
495 #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)
496
497 #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)
498
499 /*
500  * Variants which do not take an dest argument, but take a dreg argument.
501  */
502 #define MONO_EMIT_NEW_ICONST(cfg,dr,imm) do { \
503         MonoInst *inst; \
504         MONO_INST_NEW ((cfg), (inst), OP_ICONST); \
505         inst->dreg = dr; \
506         inst->inst_c0 = imm; \
507             MONO_ADD_INS ((cfg)->cbb, inst); \
508         } while (0)
509
510 #define MONO_EMIT_NEW_PCONST(cfg,dr,val) do {   \
511         MonoInst *inst; \
512         MONO_INST_NEW ((cfg), (inst), OP_PCONST); \
513         inst->dreg = dr; \
514                 (inst)->inst_p0 = (val);        \
515                 (inst)->type = STACK_PTR;       \
516             MONO_ADD_INS ((cfg)->cbb, inst); \
517         } while (0)
518
519 #define MONO_EMIT_NEW_I8CONST(cfg,dr,imm) do { \
520         MonoInst *inst; \
521         MONO_INST_NEW ((cfg), (inst), OP_I8CONST); \
522         inst->dreg = dr; \
523         inst->inst_l = imm; \
524             MONO_ADD_INS ((cfg)->cbb, inst); \
525         } while (0)
526
527 #ifdef MONO_ARCH_NEED_GOT_VAR
528
529 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
530         MonoInst *inst; \
531         NEW_AOTCONST ((cfg), (inst), (patch_type), (cons)); \
532         inst->dreg = (dr); \
533         MONO_ADD_INS ((cfg)->cbb, inst); \
534     } while (0)
535
536 #else
537
538 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,type) do { \
539         MonoInst *inst; \
540         MONO_INST_NEW ((cfg), (inst), OP_AOTCONST); \
541         inst->dreg = dr; \
542         inst->inst_p0 = imm; \
543         inst->inst_c1 = type; \
544                 MONO_ADD_INS ((cfg)->cbb, inst); \
545         } while (0)
546
547 #endif
548
549 #define MONO_EMIT_NEW_CLASSCONST(cfg,dr,imm) MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,MONO_PATCH_INFO_CLASS)
550 #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)
551
552 #define MONO_EMIT_NEW_VZERO(cfg,dr,kl) do {     \
553         MonoInst *inst; \
554         MONO_INST_NEW ((cfg), (inst), MONO_CLASS_IS_SIMD (cfg, kl) ? OP_XZERO : OP_VZERO); \
555         inst->dreg = dr; \
556                 (inst)->type = STACK_VTYPE;     \
557         (inst)->klass = (kl); \
558             MONO_ADD_INS ((cfg)->cbb, inst); \
559         } while (0)
560
561 #define MONO_EMIT_NEW_UNALU(cfg,op,dr,sr1) do { \
562         MonoInst *inst; \
563         EMIT_NEW_UNALU ((cfg), (inst), (op), (dr), (sr1)); \
564         } while (0)
565
566 #define MONO_EMIT_NEW_BIALU(cfg,op,dr,sr1,sr2) do { \
567         MonoInst *inst; \
568         MONO_INST_NEW ((cfg), (inst), (op)); \
569         inst->dreg = dr; \
570         inst->sreg1 = sr1; \
571         inst->sreg2 = sr2; \
572             MONO_ADD_INS (cfg->cbb, inst); \
573         } while (0)
574
575 #define MONO_EMIT_NEW_BIALU_IMM(cfg,op,dr,sr,imm) do { \
576         MonoInst *inst; \
577         MONO_INST_NEW ((cfg), (inst), (op)); \
578         inst->dreg = dr; \
579         inst->sreg1 = sr; \
580         inst->inst_imm = (mgreg_t)(imm);                \
581             MONO_ADD_INS (cfg->cbb, inst); \
582         } while (0)
583
584 #define MONO_EMIT_NEW_COMPARE_IMM(cfg,sr1,imm) do { \
585         MonoInst *inst; \
586         MONO_INST_NEW ((cfg), (inst), (OP_COMPARE_IMM)); \
587         inst->sreg1 = sr1; \
588         inst->inst_imm = (imm);                  \
589             MONO_ADD_INS ((cfg)->cbb, inst); \
590         } while (0)
591
592 #define MONO_EMIT_NEW_ICOMPARE_IMM(cfg,sr1,imm) do { \
593         MonoInst *inst; \
594         MONO_INST_NEW ((cfg), (inst), sizeof (mgreg_t) == 8 ? OP_ICOMPARE_IMM : OP_COMPARE_IMM); \
595         inst->sreg1 = sr1; \
596         inst->inst_imm = (imm);                  \
597             MONO_ADD_INS ((cfg)->cbb, inst); \
598         } while (0)
599
600 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg,op,dr,base,offset) do { \
601         MonoInst *inst; \
602         MONO_INST_NEW ((cfg), (inst), (op)); \
603         inst->dreg = dr; \
604         inst->inst_basereg = base; \
605         inst->inst_offset = offset; \
606             MONO_ADD_INS (cfg->cbb, inst); \
607     } while (0)
608
609 #define MONO_EMIT_NEW_LOAD_MEMBASE(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
610
611 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS(cfg,op,dr,base,offset,ins_flags) do { \
612         MonoInst *inst; \
613         MONO_INST_NEW ((cfg), (inst), (op)); \
614         inst->dreg = dr; \
615         inst->inst_basereg = base; \
616         inst->inst_offset = offset; \
617                 inst->flags = (ins_flags); \
618             MONO_ADD_INS (cfg->cbb, inst); \
619     } while (0)
620
621 #define MONO_EMIT_NEW_STORE_MEMBASE(cfg,op,base,offset,sr) do { \
622         MonoInst *inst; \
623         MONO_INST_NEW ((cfg), (inst), (op)); \
624         (inst)->sreg1 = sr; \
625         (inst)->inst_destbasereg = base; \
626         (inst)->inst_offset = offset; \
627             MONO_ADD_INS (cfg->cbb, inst); \
628         } while (0)
629
630 #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
631         MonoInst *inst; \
632         MONO_INST_NEW ((cfg), (inst), (op)); \
633         inst->inst_destbasereg = base; \
634         inst->inst_offset = offset; \
635         inst->inst_imm = (mgreg_t)(imm); \
636         MONO_ADD_INS ((cfg)->cbb, inst); \
637         } while (0)
638
639 #define MONO_EMIT_NEW_COND_EXC(cfg,cond,name) do { \
640         MonoInst *inst; \
641         MONO_INST_NEW ((cfg), (inst), (OP_COND_EXC_##cond)); \
642         inst->inst_p1 = (char*)name; \
643             MONO_ADD_INS ((cfg)->cbb, inst); \
644         } while (0)
645
646 /* Branch support */
647
648 /*
649  * Basic blocks have two numeric identifiers:
650  * dfn: Depth First Number
651  * block_num: unique ID assigned at bblock creation
652  */
653 #define NEW_BBLOCK(cfg,bblock) do { \
654         (bblock) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
655         (bblock)->block_num = cfg->num_bblocks++; \
656     } while (0)
657
658 #define ADD_BBLOCK(cfg,b) do {  \
659         if ((b)->cil_code)  {\
660                         cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b);   \
661         } \
662                 (b)->real_offset = cfg->real_offset;    \
663         } while (0)
664
665 /* Emit a one-way conditional branch */
666 /* 
667  * The inst_false_bb field of the cond branch will not be set, the JIT code should be
668  * prepared to deal with this.
669  */
670 #ifdef DEBUG_EXTENDED_BBLOCKS
671 static int ccount = 0;
672 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
673         MonoInst *ins; \
674         MonoBasicBlock *falsebb; \
675         MONO_INST_NEW ((cfg), (ins), (op)); \
676         if ((op) == OP_BR) { \
677                 NEW_BBLOCK ((cfg), falsebb); \
678             ins->inst_target_bb = (truebb); \
679             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
680             MONO_ADD_INS ((cfg)->cbb, ins); \
681             MONO_START_BB ((cfg), falsebb); \
682         } else { \
683             ccount ++; \
684                     ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);  \
685             ins->inst_true_bb = (truebb); \
686             ins->inst_false_bb = NULL; \
687             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
688             MONO_ADD_INS ((cfg)->cbb, ins); \
689             if (getenv ("COUNT2") && ccount == atoi (getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
690             if (getenv ("COUNT2") && ccount < atoi (getenv ("COUNT2"))) { \
691                  cfg->cbb->extended = TRUE; \
692             } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
693         } \
694         } while (0)
695 #else
696 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
697         MonoInst *ins; \
698         MonoBasicBlock *falsebb; \
699         MONO_INST_NEW ((cfg), (ins), (op)); \
700         if ((op) == OP_BR) { \
701                 NEW_BBLOCK ((cfg), falsebb); \
702             ins->inst_target_bb = (truebb); \
703             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
704             MONO_ADD_INS ((cfg)->cbb, ins); \
705             MONO_START_BB ((cfg), falsebb); \
706         } else { \
707                     ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);  \
708             ins->inst_true_bb = (truebb); \
709             ins->inst_false_bb = NULL; \
710             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
711             MONO_ADD_INS ((cfg)->cbb, ins); \
712             if (!cfg->enable_extended_bblocks) { \
713                 NEW_BBLOCK ((cfg), falsebb); \
714                 ins->inst_false_bb = falsebb; \
715                 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
716                 MONO_START_BB ((cfg), falsebb); \
717             } else { \
718                                 cfg->cbb->extended = TRUE; \
719                         } \
720         } \
721         } while (0)
722 #endif
723
724 /* Emit a two-way conditional branch */
725 #define MONO_EMIT_NEW_BRANCH_BLOCK2(cfg,op,truebb,falsebb) do { \
726         MonoInst *ins; \
727         MONO_INST_NEW ((cfg), (ins), (op)); \
728                 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);      \
729         ins->inst_true_bb = (truebb); \
730         ins->inst_false_bb = (falsebb); \
731         mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
732         mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
733         MONO_ADD_INS ((cfg)->cbb, ins); \
734         } while (0)
735
736 #define MONO_START_BB(cfg, bblock) do { \
737         ADD_BBLOCK ((cfg), (bblock)); \
738         if (cfg->cbb->last_ins && MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins) && !cfg->cbb->last_ins->inst_false_bb) { \
739             cfg->cbb->last_ins->inst_false_bb = (bblock); \
740             mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
741         } 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)))) \
742             mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
743             (cfg)->cbb->next_bb = (bblock); \
744             (cfg)->cbb = (bblock); \
745     } while (0)
746
747 /* This marks a place in code where an implicit exception could be thrown */
748 #define MONO_EMIT_NEW_IMPLICIT_EXCEPTION(cfg) do { \
749                 if (COMPILE_LLVM ((cfg))) {                                                                     \
750                         MONO_EMIT_NEW_UNALU (cfg, OP_IMPLICIT_EXCEPTION, -1, -1);       \
751                 } \
752         } while (0)
753
754 /* Emit an explicit null check which doesn't depend on SIGSEGV signal handling */
755 #define MONO_EMIT_NULL_CHECK(cfg, reg) do { \
756                 if (cfg->explicit_null_checks) {                                                          \
757                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, (reg), 0); \
758                         MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException"); \
759                 } else {                                                                                                                \
760                         MONO_EMIT_NEW_IMPLICIT_EXCEPTION (cfg);                                         \
761                 }                                                                                                                               \
762         } while (0)
763
764 #define MONO_EMIT_NEW_CHECK_THIS(cfg, sreg) do { \
765         cfg->flags |= MONO_CFG_HAS_CHECK_THIS; \
766         if (cfg->explicit_null_checks) {           \
767                 MONO_EMIT_NULL_CHECK (cfg, sreg); \
768         } else {                                                                                                \
769                 MONO_EMIT_NEW_UNALU (cfg, OP_CHECK_THIS, -1, sreg); \
770                 MONO_EMIT_NEW_IMPLICIT_EXCEPTION (cfg);                         \
771                 } \
772         MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, sreg);       \
773         } while (0)
774
775 /* A load which can cause a nullref */
776 #define NEW_LOAD_MEMBASE_FAULT(cfg,dest,op,dr,base,offset) do { \
777                 MONO_EMIT_NULL_CHECK ((cfg), (base));                                                   \
778                 NEW_LOAD_MEMBASE_FLAGS ((cfg), (dest), (op), (dr), (base), (offset), MONO_INST_FAULT); \
779         } while (0)
780
781 #define EMIT_NEW_LOAD_MEMBASE_FAULT(cfg,dest,op,dr,base,offset) do { \
782                 MONO_EMIT_NULL_CHECK ((cfg), (base));                                                   \
783                 NEW_LOAD_MEMBASE_FLAGS ((cfg), (dest), (op), (dr), (base), (offset), MONO_INST_FAULT); \
784                 MONO_ADD_INS ((cfg)->cbb, (dest)); \
785         } while (0)
786
787 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT(cfg,op,dr,base,offset) do { \
788                 MONO_EMIT_NULL_CHECK (cfg, base);                                                               \
789                 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS ((cfg), (op), (dr), (base), (offset), MONO_INST_FAULT); \
790         } while (0)
791
792 #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))
793
794 /*Object Model related macros*/
795
796 #ifndef MONO_ARCH_EMIT_BOUNDS_CHECK
797 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) do { \
798                         int _length_reg = alloc_ireg (cfg); \
799                         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset); \
800                         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, _length_reg, index_reg); \
801                         MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
802         } while (0)
803 #endif
804
805 /* cfg is the MonoCompile been used
806  * array_reg is the vreg holding the array object
807  * array_type is a struct (usually MonoArray or MonoString)
808  * array_length_field is the field in the previous struct with the length
809  * index_reg is the vreg holding the index
810  */
811 #define MONO_EMIT_BOUNDS_CHECK(cfg, array_reg, array_type, array_length_field, index_reg) do { \
812                 if (!(cfg->opt & MONO_OPT_ABCREM)) {                                                    \
813                         MONO_EMIT_NULL_CHECK (cfg, array_reg);                                          \
814                         MONO_ARCH_EMIT_BOUNDS_CHECK ((cfg), (array_reg), G_STRUCT_OFFSET (array_type, array_length_field), (index_reg)); \
815                 } else {                                                                                                                \
816                         MonoInst *ins;                                                                                          \
817                         MONO_INST_NEW ((cfg), ins, OP_BOUNDS_CHECK);                            \
818                         ins->sreg1 = array_reg;                                                                         \
819                         ins->sreg2 = index_reg;                                                                         \
820                         ins->inst_imm = G_STRUCT_OFFSET (array_type, array_length_field); \
821                         MONO_ADD_INS ((cfg)->cbb, ins);                                                         \
822                         (cfg)->flags |= MONO_CFG_HAS_ARRAY_ACCESS;                                      \
823                         (cfg)->cbb->has_array_access = TRUE;                                            \
824                 }                                                                                                                               \
825     } while (0)
826
827 G_END_DECLS
828
829 #endif