Treat CEE_BREAK the same as Debugger:Break (), i.e. route it through sdb.
[mono-project/dkf.git] / mono / mini / ir-emit.h
blob7545ddca36d6cb6946c41de2ada6103eb197d274
1 /*
2 * ir-emit.h: IR Creation/Emission Macros
4 * Author:
5 * Zoltan Varga (vargaz@gmail.com)
7 * (C) 2002 Ximian, Inc.
8 */
10 #ifndef __MONO_IR_EMIT_H__
11 #define __MONO_IR_EMIT_H__
13 #include "mini.h"
15 G_BEGIN_DECLS
17 static inline guint32
18 alloc_ireg (MonoCompile *cfg)
20 return cfg->next_vreg ++;
23 static inline guint32
24 alloc_preg (MonoCompile *cfg)
26 return alloc_ireg (cfg);
29 static inline guint32
30 alloc_lreg (MonoCompile *cfg)
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;
38 cfg->next_vreg += 3;
40 return res;
41 #endif
44 static inline guint32
45 alloc_freg (MonoCompile *cfg)
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
56 static inline guint32
57 alloc_ireg_ref (MonoCompile *cfg)
59 int vreg = alloc_ireg (cfg);
61 if (cfg->compute_gc_maps)
62 mono_mark_vreg_as_ref (cfg, vreg);
64 return vreg;
67 static inline guint32
68 alloc_ireg_mp (MonoCompile *cfg)
70 int vreg = alloc_ireg (cfg);
72 if (cfg->compute_gc_maps)
73 mono_mark_vreg_as_mp (cfg, vreg);
75 return vreg;
78 static inline guint32
79 alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
81 switch (stack_type) {
82 case STACK_I4:
83 case STACK_PTR:
84 return alloc_ireg (cfg);
85 case STACK_MP:
86 return alloc_ireg_mp (cfg);
87 case STACK_OBJ:
88 return alloc_ireg_ref (cfg);
89 case STACK_R8:
90 return alloc_freg (cfg);
91 case STACK_I8:
92 return alloc_lreg (cfg);
93 case STACK_VTYPE:
94 return alloc_ireg (cfg);
95 default:
96 g_warning ("Unknown stack type %x\n", stack_type);
97 g_assert_not_reached ();
98 return -1;
102 #undef MONO_INST_NEW
104 * FIXME: zeroing out some fields is not needed with the new IR, but the old
105 * JIT code still uses the left and right fields, so it has to stay.
107 #define MONO_INST_NEW(cfg,dest,op) do { \
108 (dest) = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoInst)); \
109 (dest)->inst_c0 = (dest)->inst_c1 = 0; \
110 (dest)->next = (dest)->prev = NULL; \
111 (dest)->opcode = (op); \
112 (dest)->flags = 0; \
113 (dest)->type = 0; \
114 (dest)->dreg = -1; \
115 MONO_INST_NULLIFY_SREGS ((dest)); \
116 (dest)->cil_code = (cfg)->ip; \
117 } while (0)
120 * Variants which take a dest argument and don't do an emit
122 #define NEW_ICONST(cfg,dest,val) do { \
123 MONO_INST_NEW ((cfg), (dest), OP_ICONST); \
124 (dest)->inst_c0 = (val); \
125 (dest)->type = STACK_I4; \
126 (dest)->dreg = alloc_dreg ((cfg), STACK_I4); \
127 } while (0)
130 * Avoid using this with a non-NULL val if possible as it is not AOT
131 * compatible. Use one of the NEW_xxxCONST variants instead.
133 #define NEW_PCONST(cfg,dest,val) do { \
134 MONO_INST_NEW ((cfg), (dest), OP_PCONST); \
135 (dest)->inst_p0 = (val); \
136 (dest)->type = STACK_PTR; \
137 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
138 } while (0)
140 #define NEW_I8CONST(cfg,dest,val) do { \
141 MONO_INST_NEW ((cfg), (dest), OP_I8CONST); \
142 (dest)->dreg = alloc_lreg ((cfg)); \
143 (dest)->type = STACK_I8; \
144 (dest)->inst_l = (val); \
145 } while (0)
147 #define NEW_STORE_MEMBASE(cfg,dest,op,base,offset,sr) do { \
148 MONO_INST_NEW ((cfg), (dest), (op)); \
149 (dest)->sreg1 = sr; \
150 (dest)->inst_destbasereg = base; \
151 (dest)->inst_offset = offset; \
152 } while (0)
154 #define NEW_LOAD_MEMBASE(cfg,dest,op,dr,base,offset) do { \
155 MONO_INST_NEW ((cfg), (dest), (op)); \
156 (dest)->dreg = (dr); \
157 (dest)->inst_basereg = (base); \
158 (dest)->inst_offset = (offset); \
159 (dest)->type = STACK_I4; \
160 } while (0)
162 #define NEW_LOAD_MEM(cfg,dest,op,dr,mem) do { \
163 MONO_INST_NEW ((cfg), (dest), (op)); \
164 (dest)->dreg = (dr); \
165 (dest)->inst_p0 = (gpointer)(gssize)(mem); \
166 (dest)->type = STACK_I4; \
167 } while (0)
169 #define NEW_UNALU(cfg,dest,op,dr,sr1) do { \
170 MONO_INST_NEW ((cfg), (dest), (op)); \
171 (dest)->dreg = dr; \
172 (dest)->sreg1 = sr1; \
173 } while (0)
175 #define NEW_BIALU(cfg,dest,op,dr,sr1,sr2) do { \
176 MONO_INST_NEW ((cfg), (dest), (op)); \
177 (dest)->dreg = (dr); \
178 (dest)->sreg1 = (sr1); \
179 (dest)->sreg2 = (sr2); \
180 } while (0)
182 #define NEW_BIALU_IMM(cfg,dest,op,dr,sr,imm) do { \
183 MONO_INST_NEW ((cfg), (dest), (op)); \
184 (dest)->dreg = dr; \
185 (dest)->sreg1 = sr; \
186 (dest)->inst_imm = (imm); \
187 } while (0)
189 #ifdef MONO_ARCH_NEED_GOT_VAR
191 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do { \
192 MONO_INST_NEW ((cfg), (dest), OP_PATCH_INFO); \
193 (dest)->inst_left = (gpointer)(el1); \
194 (dest)->inst_right = (gpointer)(el2); \
195 } while (0)
197 /* FIXME: Add the PUSH_GOT_ENTRY optimizations */
198 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
199 MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST); \
200 if (cfg->compile_aot) { \
201 MonoInst *group, *got_loc; \
202 got_loc = mono_get_got_var (cfg); \
203 NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
204 (dest)->inst_basereg = got_loc->dreg; \
205 (dest)->inst_p1 = group; \
206 } else { \
207 (dest)->inst_p0 = (cons); \
208 (dest)->inst_i1 = (gpointer)(patch_type); \
210 (dest)->type = STACK_PTR; \
211 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
212 } while (0)
214 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
215 MonoInst *group, *got_loc; \
216 MONO_INST_NEW ((cfg), (dest), OP_GOT_ENTRY); \
217 got_loc = mono_get_got_var (cfg); \
218 NEW_PATCH_INFO ((cfg), group, NULL, patch_type); \
219 group->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
220 (dest)->inst_basereg = got_loc->dreg; \
221 (dest)->inst_p1 = group; \
222 (dest)->type = (stack_type); \
223 (dest)->klass = (stack_class); \
224 (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
225 } while (0)
227 #else
229 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
230 MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
231 (dest)->inst_p0 = (cons); \
232 (dest)->inst_i1 = (gpointer)(patch_type); \
233 (dest)->type = STACK_PTR; \
234 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
235 } while (0)
237 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
238 MONO_INST_NEW ((cfg), (dest), OP_AOTCONST); \
239 (dest)->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
240 (dest)->inst_p1 = (gpointer)(patch_type); \
241 (dest)->type = (stack_type); \
242 (dest)->klass = (stack_class); \
243 (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
244 } while (0)
246 #endif
248 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
250 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
252 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
254 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
256 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
258 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
260 #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)
262 #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)
264 #define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), NULL, STACK_PTR, NULL)
266 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
267 if (cfg->compile_aot) { \
268 NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, NULL, STACK_OBJ, NULL); \
269 } else { \
270 NEW_PCONST (cfg, args [0], (entry).blob); \
272 } while (0)
274 #define NEW_METHOD_RGCTX_CONST(cfg,dest,method) do { \
275 if (cfg->compile_aot) { \
276 NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHOD_RGCTX, (method)); \
277 } else { \
278 MonoMethodRuntimeGenericContext *mrgctx; \
279 mrgctx = mono_method_lookup_rgctx (mono_class_vtable ((cfg)->domain, (method)->klass), mini_method_get_context ((method))->method_inst); \
280 NEW_PCONST ((cfg), (dest), (mrgctx)); \
282 } while (0)
284 #define NEW_DOMAINCONST(cfg,dest) do { \
285 if ((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) { \
286 /* avoid depending on undefined C behavior in sequence points */ \
287 MonoInst* __domain_var = mono_get_domainvar (cfg); \
288 NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
289 } else { \
290 NEW_PCONST (cfg, dest, (cfg)->domain); \
292 } while (0)
294 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
296 #define NEW_VARLOAD(cfg,dest,var,vartype) do { \
297 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
298 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
299 type_to_eval_stack_type ((cfg), (vartype), (dest)); \
300 (dest)->klass = var->klass; \
301 (dest)->sreg1 = var->dreg; \
302 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
303 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
304 } while (0)
306 #ifdef MONO_ARCH_SOFT_FLOAT
307 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8 || (stack_type) == STACK_R8)
308 #else
309 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8)
310 #endif
312 #define NEW_VARLOADA(cfg,dest,var,vartype) do { \
313 MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
314 (dest)->inst_p0 = (var); \
315 (var)->flags |= MONO_INST_INDIRECT; \
316 (dest)->type = STACK_MP; \
317 (dest)->klass = (var)->klass; \
318 (dest)->dreg = alloc_dreg ((cfg), STACK_MP); \
319 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; } \
320 } while (0)
322 #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do { \
323 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
324 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
325 (dest)->klass = (var)->klass; \
326 (dest)->sreg1 = (inst)->dreg; \
327 (dest)->dreg = (var)->dreg; \
328 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
329 } while (0)
331 #define NEW_TEMPLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype)
333 #define NEW_TEMPLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), cfg->varinfo [(num)], cfg->varinfo [(num)]->inst_vtype)
335 #define NEW_TEMPSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype, (inst))
337 #define NEW_ARGLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
339 #define NEW_LOCLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
341 #define NEW_LOCSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
343 #define NEW_ARGSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
345 #define NEW_LOCLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype)
347 #define NEW_RETLOADA(cfg,dest) do { \
348 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
349 (dest)->type = STACK_MP; \
350 (dest)->klass = cfg->ret->klass; \
351 (dest)->sreg1 = cfg->vret_addr->dreg; \
352 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
353 } while (0)
355 #define NEW_ARGLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), arg_array [(num)], param_types [(num)])
357 /* Promote the vreg to a variable so its address can be taken */
358 #define NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { \
359 MonoInst *var = get_vreg_to_inst ((cfg), (vreg)); \
360 if (!var) \
361 var = mono_compile_create_var_for_vreg ((cfg), (ltype), OP_LOCAL, (vreg)); \
362 NEW_VARLOADA ((cfg), (dest), (var), (ltype)); \
363 } while (0)
365 #define NEW_DUMMY_USE(cfg,dest,var) do { \
366 MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
367 (dest)->sreg1 = var->dreg; \
368 } while (0)
370 /* Variants which take a type argument and handle vtypes as well */
371 #define NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { \
372 NEW_LOAD_MEMBASE ((cfg), (dest), mono_type_to_load_membase ((cfg), (ltype)), 0, (base), (offset)); \
373 type_to_eval_stack_type ((cfg), (ltype), (dest)); \
374 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
375 } while (0)
377 #define NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { \
378 MONO_INST_NEW ((cfg), (dest), mono_type_to_store_membase ((cfg), (ltype))); \
379 (dest)->sreg1 = sr; \
380 (dest)->inst_destbasereg = base; \
381 (dest)->inst_offset = offset; \
382 type_to_eval_stack_type ((cfg), (ltype), (dest)); \
383 (dest)->klass = mono_class_from_mono_type (ltype); \
384 } while (0)
386 #define NEW_SEQ_POINT(cfg,dest,il_offset,ss_loc) do { \
387 MONO_INST_NEW ((cfg), (dest), OP_SEQ_POINT); \
388 (dest)->inst_imm = (il_offset); \
389 (dest)->flags = ss_loc ? MONO_INST_SINGLE_STEP_LOC : 0; \
390 } while (0)
392 #define NEW_GC_PARAM_SLOT_LIVENESS_DEF(cfg,dest,offset,type) do { \
393 MONO_INST_NEW ((cfg), (dest), OP_GC_PARAM_SLOT_LIVENESS_DEF); \
394 (dest)->inst_offset = (offset); \
395 (dest)->inst_vtype = (type); \
396 } while (0)
399 * Variants which do an emit as well.
401 #define EMIT_NEW_ICONST(cfg,dest,val) do { NEW_ICONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
403 #define EMIT_NEW_PCONST(cfg,dest,val) do { NEW_PCONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
405 #define EMIT_NEW_I8CONST(cfg,dest,val) do { NEW_I8CONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
407 #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)
409 #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)
411 #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)
413 #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)
415 #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)
417 #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)
419 #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)
421 #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)
423 #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)
425 #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)
427 #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)
429 #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
431 #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
433 #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)
435 #define EMIT_NEW_VARLOAD(cfg,dest,var,vartype) do { NEW_VARLOAD ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
437 #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)
439 #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
442 #ifdef MONO_ARCH_SOFT_FLOAT
445 * Since the IL stack (and our vregs) contain double values, we have to do a conversion
446 * when loading/storing args/locals of type R4.
449 #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
450 if (COMPILE_SOFT_FLOAT ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
451 MonoInst *iargs [1]; \
452 EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
453 (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
454 } else { \
455 EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
457 } while (0)
459 #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do { \
460 if (COMPILE_SOFT_FLOAT ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
461 MonoInst *iargs [2]; \
462 iargs [0] = (inst); \
463 EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
464 mono_emit_jit_icall (cfg, mono_fstore_r4, iargs); \
465 } else { \
466 EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
468 } while (0)
470 #define EMIT_NEW_ARGLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
472 #define EMIT_NEW_LOCLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
474 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
476 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
478 #else
480 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { NEW_ARGLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
482 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { NEW_LOCLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
484 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { NEW_LOCSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
486 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
488 #endif
490 #define EMIT_NEW_TEMPLOAD(cfg,dest,num) do { NEW_TEMPLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
492 #define EMIT_NEW_TEMPLOADA(cfg,dest,num) do { NEW_TEMPLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
494 #define EMIT_NEW_LOCLOADA(cfg,dest,num) do { NEW_LOCLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
496 #define EMIT_NEW_ARGLOADA(cfg,dest,num) do { NEW_ARGLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
498 #define EMIT_NEW_RETLOADA(cfg,dest) do { NEW_RETLOADA ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
500 #define EMIT_NEW_TEMPSTORE(cfg,dest,num,inst) do { NEW_TEMPSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
502 #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)
504 #define EMIT_NEW_DUMMY_USE(cfg,dest,var) do { NEW_DUMMY_USE ((cfg), (dest), (var)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
506 #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)
508 #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)
510 #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)
512 #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)
514 #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)
516 #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)
518 #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)
520 #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)
522 * Variants which do not take an dest argument, but take a dreg argument.
524 #define MONO_EMIT_NEW_ICONST(cfg,dr,imm) do { \
525 MonoInst *inst; \
526 MONO_INST_NEW ((cfg), (inst), OP_ICONST); \
527 inst->dreg = dr; \
528 inst->inst_c0 = imm; \
529 MONO_ADD_INS ((cfg)->cbb, inst); \
530 } while (0)
532 #define MONO_EMIT_NEW_PCONST(cfg,dr,val) do { \
533 MonoInst *inst; \
534 MONO_INST_NEW ((cfg), (inst), OP_PCONST); \
535 inst->dreg = dr; \
536 (inst)->inst_p0 = (val); \
537 (inst)->type = STACK_PTR; \
538 MONO_ADD_INS ((cfg)->cbb, inst); \
539 } while (0)
541 #define MONO_EMIT_NEW_I8CONST(cfg,dr,imm) do { \
542 MonoInst *inst; \
543 MONO_INST_NEW ((cfg), (inst), OP_I8CONST); \
544 inst->dreg = dr; \
545 inst->inst_l = imm; \
546 MONO_ADD_INS ((cfg)->cbb, inst); \
547 } while (0)
549 #ifdef MONO_ARCH_NEED_GOT_VAR
551 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
552 MonoInst *inst; \
553 NEW_AOTCONST ((cfg), (inst), (patch_type), (cons)); \
554 inst->dreg = (dr); \
555 MONO_ADD_INS ((cfg)->cbb, inst); \
556 } while (0)
558 #else
560 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,type) do { \
561 MonoInst *inst; \
562 MONO_INST_NEW ((cfg), (inst), OP_AOTCONST); \
563 inst->dreg = dr; \
564 inst->inst_p0 = imm; \
565 inst->inst_c1 = type; \
566 MONO_ADD_INS ((cfg)->cbb, inst); \
567 } while (0)
569 #endif
571 #define MONO_EMIT_NEW_CLASSCONST(cfg,dr,imm) MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,MONO_PATCH_INFO_CLASS)
572 #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)
574 #define MONO_EMIT_NEW_VZERO(cfg,dr,kl) do { \
575 MonoInst *inst; \
576 MONO_INST_NEW ((cfg), (inst), MONO_CLASS_IS_SIMD (cfg, kl) ? OP_XZERO : OP_VZERO); \
577 inst->dreg = dr; \
578 (inst)->type = STACK_VTYPE; \
579 (inst)->klass = (kl); \
580 MONO_ADD_INS ((cfg)->cbb, inst); \
581 } while (0)
583 #define MONO_EMIT_NEW_UNALU(cfg,op,dr,sr1) do { \
584 MonoInst *inst; \
585 EMIT_NEW_UNALU ((cfg), (inst), (op), (dr), (sr1)); \
586 } while (0)
588 #define MONO_EMIT_NEW_BIALU(cfg,op,dr,sr1,sr2) do { \
589 MonoInst *inst; \
590 MONO_INST_NEW ((cfg), (inst), (op)); \
591 inst->dreg = dr; \
592 inst->sreg1 = sr1; \
593 inst->sreg2 = sr2; \
594 MONO_ADD_INS (cfg->cbb, inst); \
595 } while (0)
597 #define MONO_EMIT_NEW_BIALU_IMM(cfg,op,dr,sr,imm) do { \
598 MonoInst *inst; \
599 MONO_INST_NEW ((cfg), (inst), (op)); \
600 inst->dreg = dr; \
601 inst->sreg1 = sr; \
602 inst->inst_imm = (mgreg_t)(imm); \
603 MONO_ADD_INS (cfg->cbb, inst); \
604 } while (0)
606 #define MONO_EMIT_NEW_COMPARE_IMM(cfg,sr1,imm) do { \
607 MonoInst *inst; \
608 MONO_INST_NEW ((cfg), (inst), (OP_COMPARE_IMM)); \
609 inst->sreg1 = sr1; \
610 inst->inst_imm = (imm); \
611 MONO_ADD_INS ((cfg)->cbb, inst); \
612 } while (0)
614 #define MONO_EMIT_NEW_ICOMPARE_IMM(cfg,sr1,imm) do { \
615 MonoInst *inst; \
616 MONO_INST_NEW ((cfg), (inst), sizeof (mgreg_t) == 8 ? OP_ICOMPARE_IMM : OP_COMPARE_IMM); \
617 inst->sreg1 = sr1; \
618 inst->inst_imm = (imm); \
619 MONO_ADD_INS ((cfg)->cbb, inst); \
620 } while (0)
622 /* This is used on 32 bit machines too when running with LLVM */
623 #define MONO_EMIT_NEW_LCOMPARE_IMM(cfg,sr1,imm) do { \
624 MonoInst *inst; \
625 MONO_INST_NEW ((cfg), (inst), (OP_LCOMPARE_IMM)); \
626 inst->sreg1 = sr1; \
627 if (SIZEOF_REGISTER == 4 && COMPILE_LLVM (cfg)) { \
628 guint64 _l = (imm); \
629 inst->inst_imm = _l & 0xffffffff; \
630 inst->inst_offset = _l >> 32; \
631 } else { \
632 inst->inst_imm = (imm); \
634 MONO_ADD_INS ((cfg)->cbb, inst); \
635 } while (0)
637 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg,op,dr,base,offset) do { \
638 MonoInst *inst; \
639 MONO_INST_NEW ((cfg), (inst), (op)); \
640 inst->dreg = dr; \
641 inst->inst_basereg = base; \
642 inst->inst_offset = offset; \
643 MONO_ADD_INS (cfg->cbb, inst); \
644 } while (0)
646 #define MONO_EMIT_NEW_LOAD_MEMBASE(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
648 #define MONO_EMIT_NEW_STORE_MEMBASE(cfg,op,base,offset,sr) do { \
649 MonoInst *inst; \
650 MONO_INST_NEW ((cfg), (inst), (op)); \
651 (inst)->sreg1 = sr; \
652 (inst)->inst_destbasereg = base; \
653 (inst)->inst_offset = offset; \
654 MONO_ADD_INS (cfg->cbb, inst); \
655 } while (0)
657 #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
658 MonoInst *inst; \
659 MONO_INST_NEW ((cfg), (inst), (op)); \
660 inst->inst_destbasereg = base; \
661 inst->inst_offset = offset; \
662 inst->inst_imm = (mgreg_t)(imm); \
663 MONO_ADD_INS ((cfg)->cbb, inst); \
664 } while (0)
666 #define MONO_EMIT_NEW_COND_EXC(cfg,cond,name) do { \
667 MonoInst *inst; \
668 MONO_INST_NEW ((cfg), (inst), (OP_COND_EXC_##cond)); \
669 inst->inst_p1 = (char*)name; \
670 MONO_ADD_INS ((cfg)->cbb, inst); \
671 } while (0)
673 /* Branch support */
676 * Basic blocks have two numeric identifiers:
677 * dfn: Depth First Number
678 * block_num: unique ID assigned at bblock creation
680 #define NEW_BBLOCK(cfg,bblock) do { \
681 (bblock) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
682 (bblock)->block_num = cfg->num_bblocks++; \
683 } while (0)
685 #define ADD_BBLOCK(cfg,b) do { \
686 if ((b)->cil_code) {\
687 cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b); \
689 (b)->real_offset = cfg->real_offset; \
690 } while (0)
692 /* Emit a one-way conditional branch */
694 * The inst_false_bb field of the cond branch will not be set, the JIT code should be
695 * prepared to deal with this.
697 #ifdef DEBUG_EXTENDED_BBLOCKS
698 static int ccount = 0;
699 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
700 MonoInst *ins; \
701 MonoBasicBlock *falsebb; \
702 MONO_INST_NEW ((cfg), (ins), (op)); \
703 if ((op) == OP_BR) { \
704 NEW_BBLOCK ((cfg), falsebb); \
705 ins->inst_target_bb = (truebb); \
706 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
707 MONO_ADD_INS ((cfg)->cbb, ins); \
708 MONO_START_BB ((cfg), falsebb); \
709 } else { \
710 ccount ++; \
711 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
712 ins->inst_true_bb = (truebb); \
713 ins->inst_false_bb = NULL; \
714 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
715 MONO_ADD_INS ((cfg)->cbb, ins); \
716 if (getenv ("COUNT2") && ccount == atoi (getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
717 if (getenv ("COUNT2") && ccount < atoi (getenv ("COUNT2"))) { \
718 cfg->cbb->extended = TRUE; \
719 } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
721 } while (0)
722 #else
723 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
724 MonoInst *ins; \
725 MonoBasicBlock *falsebb; \
726 MONO_INST_NEW ((cfg), (ins), (op)); \
727 if ((op) == OP_BR) { \
728 NEW_BBLOCK ((cfg), falsebb); \
729 ins->inst_target_bb = (truebb); \
730 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
731 MONO_ADD_INS ((cfg)->cbb, ins); \
732 MONO_START_BB ((cfg), falsebb); \
733 } else { \
734 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
735 ins->inst_true_bb = (truebb); \
736 ins->inst_false_bb = NULL; \
737 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
738 MONO_ADD_INS ((cfg)->cbb, ins); \
739 if (!cfg->enable_extended_bblocks) { \
740 NEW_BBLOCK ((cfg), falsebb); \
741 ins->inst_false_bb = falsebb; \
742 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
743 MONO_START_BB ((cfg), falsebb); \
744 } else { \
745 cfg->cbb->extended = TRUE; \
748 } while (0)
749 #endif
751 /* Emit a two-way conditional branch */
752 #define MONO_EMIT_NEW_BRANCH_BLOCK2(cfg,op,truebb,falsebb) do { \
753 MonoInst *ins; \
754 MONO_INST_NEW ((cfg), (ins), (op)); \
755 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
756 ins->inst_true_bb = (truebb); \
757 ins->inst_false_bb = (falsebb); \
758 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
759 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
760 MONO_ADD_INS ((cfg)->cbb, ins); \
761 } while (0)
763 #define MONO_START_BB(cfg, bblock) do { \
764 ADD_BBLOCK ((cfg), (bblock)); \
765 if (cfg->cbb->last_ins && MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins) && !cfg->cbb->last_ins->inst_false_bb) { \
766 cfg->cbb->last_ins->inst_false_bb = (bblock); \
767 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
768 } 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)))) \
769 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
770 (cfg)->cbb->next_bb = (bblock); \
771 (cfg)->cbb = (bblock); \
772 } while (0)
774 /* This marks a place in code where an implicit exception could be thrown */
775 #define MONO_EMIT_NEW_IMPLICIT_EXCEPTION(cfg) do { \
776 if (COMPILE_LLVM ((cfg))) { \
777 MONO_EMIT_NEW_UNALU (cfg, OP_IMPLICIT_EXCEPTION, -1, -1); \
779 } while (0)
781 /* Loads/Stores which can fault are handled correctly by the LLVM mono branch */
782 #define MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE(cfg) do { \
783 if (COMPILE_LLVM (cfg) && !IS_LLVM_MONO_BRANCH) \
784 MONO_EMIT_NEW_IMPLICIT_EXCEPTION ((cfg)); \
785 } while (0)
787 /* Emit an explicit null check which doesn't depend on SIGSEGV signal handling */
788 #define MONO_EMIT_NULL_CHECK(cfg, reg) do { \
789 if (cfg->explicit_null_checks) { \
790 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, (reg), 0); \
791 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException"); \
792 } else { \
793 MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE (cfg); \
795 } while (0)
797 #define MONO_EMIT_NEW_CHECK_THIS(cfg, sreg) do { \
798 cfg->flags |= MONO_CFG_HAS_CHECK_THIS; \
799 if (cfg->explicit_null_checks) { \
800 MONO_EMIT_NULL_CHECK (cfg, sreg); \
801 } else { \
802 MONO_EMIT_NEW_UNALU (cfg, OP_CHECK_THIS, -1, sreg); \
803 MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE (cfg); \
805 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, sreg); \
806 } while (0)
808 #define NEW_LOAD_MEMBASE_FLAGS(cfg,dest,op,dr,base,offset,ins_flags) do { \
809 int __ins_flags = ins_flags; \
810 if (__ins_flags & MONO_INST_FAULT) { \
811 MONO_EMIT_NULL_CHECK ((cfg), (base)); \
812 if (cfg->explicit_null_checks) \
813 __ins_flags &= ~MONO_INST_FAULT; \
815 NEW_LOAD_MEMBASE ((cfg), (dest), (op), (dr), (base), (offset)); \
816 (dest)->flags = (__ins_flags); \
817 } while (0)
819 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS(cfg,op,dr,base,offset,ins_flags) do { \
820 MonoInst *inst; \
821 int __ins_flags = ins_flags; \
822 if (__ins_flags & MONO_INST_FAULT) { \
823 MONO_EMIT_NULL_CHECK ((cfg), (base)); \
824 if (cfg->explicit_null_checks) \
825 __ins_flags &= ~MONO_INST_FAULT; \
827 NEW_LOAD_MEMBASE ((cfg), (inst), (op), (dr), (base), (offset)); \
828 inst->flags = (__ins_flags); \
829 MONO_ADD_INS (cfg->cbb, inst); \
830 } while (0)
832 #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))
834 /* A load which can cause a nullref */
835 #define NEW_LOAD_MEMBASE_FAULT(cfg,dest,op,dr,base,offset) NEW_LOAD_MEMBASE_FLAGS ((cfg), (dest), (op), (dr), (base), (offset), MONO_INST_FAULT)
837 #define EMIT_NEW_LOAD_MEMBASE_FAULT(cfg,dest,op,dr,base,offset) do { \
838 NEW_LOAD_MEMBASE_FAULT ((cfg), (dest), (op), (dr), (base), (offset)); \
839 MONO_ADD_INS ((cfg)->cbb, (dest)); \
840 } while (0)
842 #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)
844 #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))
846 /*Object Model related macros*/
848 /* Default bounds check implementation for most architectures + llvm */
849 #define MONO_EMIT_DEFAULT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg, fault) do { \
850 int _length_reg = alloc_ireg (cfg); \
851 if (fault) \
852 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset); \
853 else \
854 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset, MONO_INST_CONSTANT_LOAD); \
855 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, _length_reg, index_reg); \
856 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
857 } while (0)
859 #ifndef MONO_ARCH_EMIT_BOUNDS_CHECK
860 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) MONO_EMIT_DEFAULT_BOUNDS_CHECK ((cfg), (array_reg), (offset), (index_reg), TRUE)
861 #endif
863 /* cfg is the MonoCompile been used
864 * array_reg is the vreg holding the array object
865 * array_type is a struct (usually MonoArray or MonoString)
866 * array_length_field is the field in the previous struct with the length
867 * index_reg is the vreg holding the index
869 #define MONO_EMIT_BOUNDS_CHECK(cfg, array_reg, array_type, array_length_field, index_reg) do { \
870 if (!(cfg->opt & MONO_OPT_UNSAFE)) { \
871 if (!(cfg->opt & MONO_OPT_ABCREM)) { \
872 MONO_EMIT_NULL_CHECK (cfg, array_reg); \
873 if (COMPILE_LLVM (cfg)) \
874 MONO_EMIT_DEFAULT_BOUNDS_CHECK ((cfg), (array_reg), G_STRUCT_OFFSET (array_type, array_length_field), (index_reg), TRUE); \
875 else \
876 MONO_ARCH_EMIT_BOUNDS_CHECK ((cfg), (array_reg), G_STRUCT_OFFSET (array_type, array_length_field), (index_reg)); \
877 } else { \
878 MonoInst *ins; \
879 MONO_INST_NEW ((cfg), ins, OP_BOUNDS_CHECK); \
880 ins->sreg1 = array_reg; \
881 ins->sreg2 = index_reg; \
882 ins->inst_imm = G_STRUCT_OFFSET (array_type, array_length_field); \
883 ins->flags |= MONO_INST_FAULT; \
884 MONO_ADD_INS ((cfg)->cbb, ins); \
885 (cfg)->flags |= MONO_CFG_HAS_ARRAY_ACCESS; \
886 (cfg)->cbb->has_array_access = TRUE; \
889 } while (0)
891 G_END_DECLS
893 #endif