3 * IR Creation/Emission Macros
6 * Zoltan Varga (vargaz@gmail.com)
8 * (C) 2002 Ximian, Inc.
11 #ifndef __MONO_IR_EMIT_H__
12 #define __MONO_IR_EMIT_H__
19 alloc_ireg (MonoCompile
*cfg
)
21 return cfg
->next_vreg
++;
25 alloc_preg (MonoCompile
*cfg
)
27 return alloc_ireg (cfg
);
31 alloc_lreg (MonoCompile
*cfg
)
33 #if SIZEOF_REGISTER == 8
34 return cfg
->next_vreg
++;
36 /* Use a pair of consecutive vregs */
37 guint32 res
= cfg
->next_vreg
;
46 alloc_freg (MonoCompile
*cfg
)
48 if (mono_arch_is_soft_float ()) {
49 /* Allocate an lvreg so float ops can be decomposed into long ops */
50 return alloc_lreg (cfg
);
52 /* Allocate these from the same pool as the int regs */
53 return cfg
->next_vreg
++;
58 alloc_ireg_ref (MonoCompile
*cfg
)
60 int vreg
= alloc_ireg (cfg
);
62 if (cfg
->compute_gc_maps
)
63 mono_mark_vreg_as_ref (cfg
, vreg
);
69 alloc_ireg_mp (MonoCompile
*cfg
)
71 int vreg
= alloc_ireg (cfg
);
73 if (cfg
->compute_gc_maps
)
74 mono_mark_vreg_as_mp (cfg
, vreg
);
80 alloc_xreg (MonoCompile
*cfg
)
82 return alloc_ireg (cfg
);
86 alloc_dreg (MonoCompile
*cfg
, MonoStackType stack_type
)
91 return alloc_ireg (cfg
);
93 return alloc_ireg_mp (cfg
);
95 return alloc_ireg_ref (cfg
);
98 return alloc_freg (cfg
);
100 return alloc_lreg (cfg
);
102 return alloc_ireg (cfg
);
104 g_warning ("Unknown stack type %x\n", stack_type
);
105 g_assert_not_reached ();
111 * Macros used to generate intermediate representation macros
113 * The macros use a `MonoConfig` object as its context, and among other
114 * things it is used to associate instructions with the memory pool with
117 * The macros come in three variations with slightly different
118 * features, the patter is: NEW_OP, EMIT_NEW_OP, MONO_EMIT_NEW_OP,
119 * the differences are as follows:
121 * `NEW_OP`: these are the basic macros to setup an instruction that is
122 * passed as an argument.
124 * `EMIT_NEW_OP`: these macros in addition to creating the instruction
125 * add the instruction to the current basic block in the `MonoConfig`
126 * object passed. Usually these are used when further customization of
127 * the `inst` parameter is desired before the instruction is added to the
128 * MonoConfig current basic block.
130 * `MONO_EMIT_NEW_OP`: These variations of the instructions are used when
131 * you are merely interested in emitting the instruction into the `MonoConfig`
136 * FIXME: zeroing out some fields is not needed with the new IR, but the old
137 * JIT code still uses the left and right fields, so it has to stay.
141 * MONO_INST_NEW: create a new MonoInst instance that is allocated on the MonoConfig pool.
143 * @cfg: the MonoConfig object that will be used as the context for the
145 * @dest: this is the place where the instance of the `MonoInst` is stored.
146 * @op: the value that should be stored in the MonoInst.opcode field
148 * This initializes an empty MonoInst that has been nulled out, it is allocated
149 * from the memory pool associated with the MonoConfig, but it is not linked anywhere.
150 * the cil_code is set to the cfg->ip address.
152 #define MONO_INST_NEW(cfg,dest,op) do { \
153 (dest) = (MonoInst *)mono_mempool_alloc ((cfg)->mempool, sizeof (MonoInst)); \
154 (dest)->inst_i0 = (dest)->inst_i1 = 0; \
155 (dest)->next = (dest)->prev = NULL; \
156 (dest)->opcode = (op); \
160 MONO_INST_NULLIFY_SREGS ((dest)); \
161 (dest)->cil_code = (cfg)->ip; \
165 * Variants which take a dest argument and don't do an emit
167 #define NEW_ICONST(cfg,dest,val) do { \
168 MONO_INST_NEW ((cfg), (dest), OP_ICONST); \
169 (dest)->inst_c0 = (val); \
170 (dest)->type = STACK_I4; \
171 (dest)->dreg = alloc_dreg ((cfg), STACK_I4); \
175 * Avoid using this with a non-NULL val if possible as it is not AOT
176 * compatible. Use one of the NEW_xxxCONST variants instead.
178 #define NEW_PCONST(cfg,dest,val) do { \
179 MONO_INST_NEW ((cfg), (dest), OP_PCONST); \
180 (dest)->inst_p0 = (val); \
181 (dest)->type = STACK_PTR; \
182 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
185 #define NEW_I8CONST(cfg,dest,val) do { \
186 MONO_INST_NEW ((cfg), (dest), OP_I8CONST); \
187 (dest)->dreg = alloc_lreg ((cfg)); \
188 (dest)->type = STACK_I8; \
189 (dest)->inst_l = (val); \
192 #define NEW_STORE_MEMBASE(cfg,dest,op,base,offset,sr) do { \
193 MONO_INST_NEW ((cfg), (dest), (op)); \
194 (dest)->sreg1 = sr; \
195 (dest)->inst_destbasereg = base; \
196 (dest)->inst_offset = offset; \
199 #define NEW_LOAD_MEMBASE(cfg,dest,op,dr,base,offset) do { \
200 MONO_INST_NEW ((cfg), (dest), (op)); \
201 (dest)->dreg = (dr); \
202 (dest)->inst_basereg = (base); \
203 (dest)->inst_offset = (offset); \
204 (dest)->type = STACK_I4; \
207 #define NEW_LOAD_MEM(cfg,dest,op,dr,mem) do { \
208 MONO_INST_NEW ((cfg), (dest), (op)); \
209 (dest)->dreg = (dr); \
210 (dest)->inst_p0 = (gpointer)(gssize)(mem); \
211 (dest)->type = STACK_I4; \
214 #define NEW_UNALU(cfg,dest,op,dr,sr1) do { \
215 MONO_INST_NEW ((cfg), (dest), (op)); \
217 (dest)->sreg1 = sr1; \
220 #define NEW_BIALU(cfg,dest,op,dr,sr1,sr2) do { \
221 MONO_INST_NEW ((cfg), (dest), (op)); \
222 (dest)->dreg = (dr); \
223 (dest)->sreg1 = (sr1); \
224 (dest)->sreg2 = (sr2); \
227 #define NEW_BIALU_IMM(cfg,dest,op,dr,sr,imm) do { \
228 MONO_INST_NEW ((cfg), (dest), (op)); \
230 (dest)->sreg1 = sr; \
231 (dest)->inst_imm = (imm); \
234 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do { \
235 MONO_INST_NEW ((cfg), (dest), OP_PATCH_INFO); \
236 (dest)->inst_left = (gpointer)(el1); \
237 (dest)->inst_right = (gpointer)(el2); \
240 #define NEW_AOTCONST_GOT_VAR(cfg,dest,patch_type,cons) do { \
241 MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST); \
242 if (cfg->compile_aot) { \
243 MonoInst *group, *got_loc; \
244 got_loc = mono_get_got_var (cfg); \
245 NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
246 (dest)->inst_basereg = got_loc->dreg; \
247 (dest)->inst_p1 = group; \
249 (dest)->inst_p0 = (cons); \
250 (dest)->inst_i1 = (gpointer)(patch_type); \
252 (dest)->type = STACK_PTR; \
253 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
256 #define NEW_AOTCONST_TOKEN_GOT_VAR(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
257 MonoInst *group, *got_loc; \
258 MONO_INST_NEW ((cfg), (dest), OP_GOT_ENTRY); \
259 got_loc = mono_get_got_var (cfg); \
260 NEW_PATCH_INFO ((cfg), group, NULL, patch_type); \
261 group->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
262 (dest)->inst_basereg = got_loc->dreg; \
263 (dest)->inst_p1 = group; \
264 (dest)->type = (stack_type); \
265 (dest)->klass = (stack_class); \
266 (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
269 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
270 if (cfg->backend->need_got_var && !cfg->llvm_only) { \
271 NEW_AOTCONST_GOT_VAR ((cfg), (dest), (patch_type), (cons)); \
273 MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
274 (dest)->inst_p0 = (cons); \
275 (dest)->inst_p1 = GUINT_TO_POINTER (patch_type); \
276 (dest)->type = STACK_PTR; \
277 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
281 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
282 if (cfg->backend->need_got_var && !cfg->llvm_only) { \
283 NEW_AOTCONST_TOKEN_GOT_VAR ((cfg), (dest), (patch_type), (image), (token), (generic_context), (stack_type), (stack_class)); \
285 MONO_INST_NEW ((cfg), (dest), OP_AOTCONST); \
286 (dest)->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
287 (dest)->inst_p1 = (gpointer)(patch_type); \
288 (dest)->type = (stack_type); \
289 (dest)->klass = (stack_class); \
290 (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
294 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
296 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
298 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
300 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
302 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
304 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
306 #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)
308 #define NEW_LDSTRLITCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_LDSTR_LIT, (val))
310 #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)
312 #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)
314 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
315 if (cfg->compile_aot) { \
316 NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, NULL, STACK_OBJ, NULL); \
318 NEW_PCONST (cfg, args [0], (entry).blob); \
322 #define NEW_METHOD_RGCTX_CONST(cfg,dest,method) do { \
323 if (cfg->compile_aot) { \
324 NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHOD_RGCTX, (method)); \
326 MonoMethodRuntimeGenericContext *mrgctx; \
327 mrgctx = mini_method_get_rgctx ((method)); \
328 NEW_PCONST ((cfg), (dest), (mrgctx)); \
332 #define NEW_DOMAINCONST(cfg,dest) do { \
333 if ((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) { \
334 /* avoid depending on undefined C behavior in sequence points */ \
335 MonoInst* __domain_var = mono_get_domainvar (cfg); \
336 NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
338 NEW_PCONST (cfg, dest, (cfg)->domain); \
342 #define NEW_JIT_ICALL_ADDRCONST(cfg,dest,name) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_JIT_ICALL_ADDR, (name))
344 #define NEW_VARLOAD(cfg,dest,var,vartype) do { \
345 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
346 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
347 mini_type_to_eval_stack_type ((cfg), (vartype), (dest)); \
348 (dest)->klass = var->klass; \
349 (dest)->sreg1 = var->dreg; \
350 (dest)->dreg = alloc_dreg ((cfg), (MonoStackType)(dest)->type); \
351 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
354 #define DECOMPOSE_INTO_REGPAIR(stack_type) (mono_arch_is_soft_float () ? ((stack_type) == STACK_I8 || (stack_type) == STACK_R8) : ((stack_type) == STACK_I8))
357 handle_gsharedvt_ldaddr (MonoCompile
*cfg
)
359 /* The decomposition of ldaddr makes use of these two variables, so add uses for them */
362 MONO_INST_NEW (cfg
, use
, OP_DUMMY_USE
);
363 use
->sreg1
= cfg
->gsharedvt_info_var
->dreg
;
364 MONO_ADD_INS (cfg
->cbb
, use
);
365 MONO_INST_NEW (cfg
, use
, OP_DUMMY_USE
);
366 use
->sreg1
= cfg
->gsharedvt_locals_var
->dreg
;
367 MONO_ADD_INS (cfg
->cbb
, use
);
370 #define NEW_VARLOADA(cfg,dest,var,vartype) do { \
371 MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
372 (dest)->inst_p0 = (var); \
373 (var)->flags |= MONO_INST_INDIRECT; \
374 (dest)->type = STACK_MP; \
375 (dest)->klass = (var)->klass; \
376 (dest)->dreg = alloc_dreg ((cfg), STACK_MP); \
377 (cfg)->has_indirection = TRUE; \
378 if (G_UNLIKELY (cfg->gsharedvt) && mini_is_gsharedvt_variable_type ((var)->inst_vtype)) { handle_gsharedvt_ldaddr ((cfg)); } \
379 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; } \
382 #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do { \
383 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
384 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
385 (dest)->klass = (var)->klass; \
386 (dest)->sreg1 = (inst)->dreg; \
387 (dest)->dreg = (var)->dreg; \
388 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
391 #define NEW_TEMPLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype)
393 #define NEW_TEMPLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), cfg->varinfo [(num)], cfg->varinfo [(num)]->inst_vtype)
395 #define NEW_TEMPSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype, (inst))
397 #define NEW_ARGLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
399 #define NEW_LOCLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
401 #define NEW_LOCSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
403 #define NEW_ARGSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
405 #define NEW_LOCLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype)
407 #define NEW_RETLOADA(cfg,dest) do { \
408 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
409 (dest)->type = STACK_MP; \
410 (dest)->klass = cfg->ret->klass; \
411 (dest)->sreg1 = cfg->vret_addr->dreg; \
412 (dest)->dreg = alloc_dreg ((cfg), (MonoStackType)(dest)->type); \
415 #define NEW_ARGLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), arg_array [(num)], param_types [(num)])
417 /* Promote the vreg to a variable so its address can be taken */
418 #define NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { \
419 MonoInst *var = get_vreg_to_inst ((cfg), (vreg)); \
421 var = mono_compile_create_var_for_vreg ((cfg), (ltype), OP_LOCAL, (vreg)); \
422 NEW_VARLOADA ((cfg), (dest), (var), (ltype)); \
425 #define NEW_DUMMY_USE(cfg,dest,var) do { \
426 MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
427 (dest)->sreg1 = var->dreg; \
430 /* Variants which take a type argument and handle vtypes as well */
431 #define NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { \
432 NEW_LOAD_MEMBASE ((cfg), (dest), mono_type_to_load_membase ((cfg), (ltype)), 0, (base), (offset)); \
433 mini_type_to_eval_stack_type ((cfg), (ltype), (dest)); \
434 (dest)->dreg = alloc_dreg ((cfg), (MonoStackType)(dest)->type); \
437 #define NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { \
438 MONO_INST_NEW ((cfg), (dest), mono_type_to_store_membase ((cfg), (ltype))); \
439 (dest)->sreg1 = sr; \
440 (dest)->inst_destbasereg = base; \
441 (dest)->inst_offset = offset; \
442 mini_type_to_eval_stack_type ((cfg), (ltype), (dest)); \
443 (dest)->klass = mono_class_from_mono_type (ltype); \
446 #define NEW_SEQ_POINT(cfg,dest,il_offset,intr_loc) do { \
447 MONO_INST_NEW ((cfg), (dest), cfg->gen_sdb_seq_points ? OP_SEQ_POINT : OP_IL_SEQ_POINT); \
448 (dest)->inst_imm = (il_offset); \
449 (dest)->flags = intr_loc ? MONO_INST_SINGLE_STEP_LOC : 0; \
452 #define NEW_GC_PARAM_SLOT_LIVENESS_DEF(cfg,dest,offset,type) do { \
453 MONO_INST_NEW ((cfg), (dest), OP_GC_PARAM_SLOT_LIVENESS_DEF); \
454 (dest)->inst_offset = (offset); \
455 (dest)->inst_vtype = (type); \
459 * Variants which do an emit as well.
461 #define EMIT_NEW_ICONST(cfg,dest,val) do { NEW_ICONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
463 #define EMIT_NEW_PCONST(cfg,dest,val) do { NEW_PCONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
465 #define EMIT_NEW_I8CONST(cfg,dest,val) do { NEW_I8CONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
467 #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)
469 #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)
471 #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)
473 #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)
475 #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)
477 #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)
479 #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)
481 #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)
483 #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)
485 #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)
487 #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)
489 #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)
491 #define EMIT_NEW_TLS_OFFSETCONST(cfg,dest,key) do { NEW_TLS_OFFSETCONST ((cfg), (dest), (key)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
493 #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
495 #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
497 #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)
499 #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)
501 #define EMIT_NEW_VARLOAD(cfg,dest,var,vartype) do { NEW_VARLOAD ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
503 #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)
505 #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
507 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
510 * Since the IL stack (and our vregs) contain double values, we have to do a conversion
511 * when loading/storing args/locals of type R4.
514 #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
515 if (!COMPILE_LLVM ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
516 MonoInst *iargs [1]; \
517 EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
518 (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
520 EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
524 #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do { \
525 if (COMPILE_SOFT_FLOAT ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
526 MonoInst *iargs [2]; \
527 iargs [0] = (inst); \
528 EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
529 (dest) = mono_emit_jit_icall (cfg, mono_fstore_r4, iargs); \
531 EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
535 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { \
536 if (mono_arch_is_soft_float ()) { \
537 EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)]); \
539 NEW_ARGLOAD ((cfg), (dest), (num)); \
540 MONO_ADD_INS ((cfg)->cbb, (dest)); \
544 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { \
545 if (mono_arch_is_soft_float ()) { \
546 EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)]); \
548 NEW_LOCLOAD ((cfg), (dest), (num)); \
549 MONO_ADD_INS ((cfg)->cbb, (dest)); \
553 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { \
554 if (mono_arch_is_soft_float ()) { \
555 EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst)); \
557 NEW_LOCSTORE ((cfg), (dest), (num), (inst)); \
558 MONO_ADD_INS ((cfg)->cbb, (dest)); \
562 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { \
563 if (mono_arch_is_soft_float ()) { \
564 EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst)); \
566 NEW_ARGSTORE ((cfg), (dest), (num), (inst)); \
567 MONO_ADD_INS ((cfg)->cbb, (dest)); \
573 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { NEW_ARGLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
575 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { NEW_LOCLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
577 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { NEW_LOCSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
579 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
583 #define EMIT_NEW_TEMPLOAD(cfg,dest,num) do { NEW_TEMPLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
585 #define EMIT_NEW_TEMPLOADA(cfg,dest,num) do { NEW_TEMPLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
587 #define EMIT_NEW_LOCLOADA(cfg,dest,num) do { NEW_LOCLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
589 #define EMIT_NEW_ARGLOADA(cfg,dest,num) do { NEW_ARGLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
591 #define EMIT_NEW_RETLOADA(cfg,dest) do { NEW_RETLOADA ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
593 #define EMIT_NEW_TEMPSTORE(cfg,dest,num,inst) do { NEW_TEMPSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
595 #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)
597 #define EMIT_NEW_DUMMY_USE(cfg,dest,var) do { NEW_DUMMY_USE ((cfg), (dest), (var)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
599 #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)
601 #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)
603 #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)
605 #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)
607 #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)
609 #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)
611 #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)
613 #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)
615 * Variants which do not take an dest argument, but take a dreg argument.
617 #define MONO_EMIT_NEW_ICONST(cfg,dr,imm) do { \
619 MONO_INST_NEW ((cfg), (inst), OP_ICONST); \
621 inst->inst_c0 = imm; \
622 MONO_ADD_INS ((cfg)->cbb, inst); \
625 #define MONO_EMIT_NEW_PCONST(cfg,dr,val) do { \
627 MONO_INST_NEW ((cfg), (inst), OP_PCONST); \
629 (inst)->inst_p0 = (val); \
630 (inst)->type = STACK_PTR; \
631 MONO_ADD_INS ((cfg)->cbb, inst); \
634 #define MONO_EMIT_NEW_I8CONST(cfg,dr,imm) do { \
636 MONO_INST_NEW ((cfg), (inst), OP_I8CONST); \
638 inst->inst_l = imm; \
639 MONO_ADD_INS ((cfg)->cbb, inst); \
642 #define MONO_EMIT_NEW_DUMMY_INIT(cfg,dr,op) do { \
644 MONO_INST_NEW ((cfg), (inst), (op)); \
646 MONO_ADD_INS ((cfg)->cbb, inst); \
649 #ifdef MONO_ARCH_NEED_GOT_VAR
651 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
653 NEW_AOTCONST ((cfg), (inst), (patch_type), (cons)); \
655 MONO_ADD_INS ((cfg)->cbb, inst); \
660 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,type) do { \
662 MONO_INST_NEW ((cfg), (inst), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
664 inst->inst_p0 = imm; \
665 inst->inst_c1 = type; \
666 MONO_ADD_INS ((cfg)->cbb, inst); \
671 #define MONO_EMIT_NEW_CLASSCONST(cfg,dr,imm) MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,MONO_PATCH_INFO_CLASS)
672 #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)
673 #define MONO_EMIT_NEW_SIGNATURECONST(cfg,dr,sig) MONO_EMIT_NEW_AOTCONST ((cfg), (dr), (sig), MONO_PATCH_INFO_SIGNATURE)
675 #define MONO_EMIT_NEW_VZERO(cfg,dr,kl) do { \
677 MONO_INST_NEW ((cfg), (inst), MONO_CLASS_IS_SIMD (cfg, kl) ? OP_XZERO : OP_VZERO); \
679 (inst)->type = STACK_VTYPE; \
680 (inst)->klass = (kl); \
681 MONO_ADD_INS ((cfg)->cbb, inst); \
684 #define MONO_EMIT_NEW_UNALU(cfg,op,dr,sr1) do { \
686 EMIT_NEW_UNALU ((cfg), (inst), (op), (dr), (sr1)); \
689 #define MONO_EMIT_NEW_BIALU(cfg,op,dr,sr1,sr2) do { \
691 MONO_INST_NEW ((cfg), (inst), (op)); \
695 MONO_ADD_INS (cfg->cbb, inst); \
698 #define MONO_EMIT_NEW_BIALU_IMM(cfg,op,dr,sr,imm) do { \
700 MONO_INST_NEW ((cfg), (inst), (op)); \
703 inst->inst_imm = (mgreg_t)(imm); \
704 MONO_ADD_INS (cfg->cbb, inst); \
707 #define MONO_EMIT_NEW_COMPARE_IMM(cfg,sr1,imm) do { \
709 MONO_INST_NEW ((cfg), (inst), (OP_COMPARE_IMM)); \
711 inst->inst_imm = (imm); \
712 MONO_ADD_INS ((cfg)->cbb, inst); \
715 #define MONO_EMIT_NEW_ICOMPARE_IMM(cfg,sr1,imm) do { \
717 MONO_INST_NEW ((cfg), (inst), sizeof (mgreg_t) == 8 ? OP_ICOMPARE_IMM : OP_COMPARE_IMM); \
719 inst->inst_imm = (imm); \
720 MONO_ADD_INS ((cfg)->cbb, inst); \
723 /* This is used on 32 bit machines too when running with LLVM */
724 #define MONO_EMIT_NEW_LCOMPARE_IMM(cfg,sr1,imm) do { \
726 MONO_INST_NEW ((cfg), (inst), (OP_LCOMPARE_IMM)); \
728 if (SIZEOF_REGISTER == 4 && COMPILE_LLVM (cfg)) { \
729 guint64 _l = (imm); \
730 inst->inst_imm = _l & 0xffffffff; \
731 inst->inst_offset = _l >> 32; \
733 inst->inst_imm = (imm); \
735 MONO_ADD_INS ((cfg)->cbb, inst); \
738 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg,op,dr,base,offset) do { \
740 MONO_INST_NEW ((cfg), (inst), (op)); \
742 inst->inst_basereg = base; \
743 inst->inst_offset = offset; \
744 MONO_ADD_INS (cfg->cbb, inst); \
747 #define MONO_EMIT_NEW_LOAD_MEMBASE(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
749 #define MONO_EMIT_NEW_STORE_MEMBASE(cfg,op,base,offset,sr) do { \
751 MONO_INST_NEW ((cfg), (inst), (op)); \
752 (inst)->sreg1 = sr; \
753 (inst)->inst_destbasereg = base; \
754 (inst)->inst_offset = offset; \
755 MONO_ADD_INS (cfg->cbb, inst); \
758 #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
760 MONO_INST_NEW ((cfg), (inst), (op)); \
761 inst->inst_destbasereg = base; \
762 inst->inst_offset = offset; \
763 inst->inst_imm = (mgreg_t)(imm); \
764 MONO_ADD_INS ((cfg)->cbb, inst); \
767 #define MONO_EMIT_NEW_COND_EXC(cfg,cond,name) do { \
769 MONO_INST_NEW ((cfg), (inst), (OP_COND_EXC_##cond)); \
770 inst->inst_p1 = (char*)name; \
771 MONO_ADD_INS ((cfg)->cbb, inst); \
777 * Basic blocks have two numeric identifiers:
778 * dfn: Depth First Number
779 * block_num: unique ID assigned at bblock creation
781 #define NEW_BBLOCK(cfg,bblock) do { \
782 (bblock) = (MonoBasicBlock *)mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
783 (bblock)->block_num = cfg->num_bblocks++; \
786 #define ADD_BBLOCK(cfg,b) do { \
787 if ((b)->cil_code) {\
788 cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b); \
790 (b)->real_offset = cfg->real_offset; \
794 * Emit a one-way conditional branch and start a new bblock.
795 * The inst_false_bb field of the cond branch will not be set, the JIT code should be
796 * prepared to deal with this.
798 #ifdef DEBUG_EXTENDED_BBLOCKS
799 static int ccount
= 0;
800 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
802 MonoBasicBlock *falsebb; \
803 MONO_INST_NEW ((cfg), (ins), (op)); \
804 if ((op) == OP_BR) { \
805 NEW_BBLOCK ((cfg), falsebb); \
806 ins->inst_target_bb = (truebb); \
807 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
808 MONO_ADD_INS ((cfg)->cbb, ins); \
809 MONO_START_BB ((cfg), falsebb); \
812 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
813 ins->inst_true_bb = (truebb); \
814 ins->inst_false_bb = NULL; \
815 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
816 MONO_ADD_INS ((cfg)->cbb, ins); \
817 char *count2 = g_getenv ("COUNT2"); \
818 if (count2 && ccount == atoi (count2) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
819 if (count2 && ccount < atoi (count2)) { \
820 cfg->cbb->extended = TRUE; \
821 } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
822 if (count2) g_free (count2); \
826 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
828 MonoBasicBlock *falsebb; \
829 MONO_INST_NEW ((cfg), (ins), (op)); \
830 if ((op) == OP_BR) { \
831 NEW_BBLOCK ((cfg), falsebb); \
832 ins->inst_target_bb = (truebb); \
833 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
834 MONO_ADD_INS ((cfg)->cbb, ins); \
835 MONO_START_BB ((cfg), falsebb); \
837 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
838 ins->inst_true_bb = (truebb); \
839 ins->inst_false_bb = NULL; \
840 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
841 MONO_ADD_INS ((cfg)->cbb, ins); \
842 if (!cfg->enable_extended_bblocks) { \
843 NEW_BBLOCK ((cfg), falsebb); \
844 ins->inst_false_bb = falsebb; \
845 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
846 MONO_START_BB ((cfg), falsebb); \
848 cfg->cbb->extended = TRUE; \
854 /* Emit a two-way conditional branch */
855 #define MONO_EMIT_NEW_BRANCH_BLOCK2(cfg,op,truebb,falsebb) do { \
857 MONO_INST_NEW ((cfg), (ins), (op)); \
858 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
859 ins->inst_true_bb = (truebb); \
860 ins->inst_false_bb = (falsebb); \
861 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
862 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
863 MONO_ADD_INS ((cfg)->cbb, ins); \
866 #define MONO_START_BB(cfg, bblock) do { \
867 ADD_BBLOCK ((cfg), (bblock)); \
868 if (cfg->cbb->last_ins && MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins) && !cfg->cbb->last_ins->inst_false_bb) { \
869 cfg->cbb->last_ins->inst_false_bb = (bblock); \
870 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
871 } 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)))) \
872 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
873 (cfg)->cbb->next_bb = (bblock); \
874 (cfg)->cbb = (bblock); \
877 /* This marks a place in code where an implicit exception could be thrown */
878 #define MONO_EMIT_NEW_IMPLICIT_EXCEPTION(cfg) do { \
879 if (COMPILE_LLVM ((cfg))) { \
880 MONO_EMIT_NEW_UNALU (cfg, OP_IMPLICIT_EXCEPTION, -1, -1); \
884 /* Loads/Stores which can fault are handled correctly by the LLVM mono branch */
885 #define MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE(cfg) do { \
888 /* Emit an explicit null check which doesn't depend on SIGSEGV signal handling */
889 #define MONO_EMIT_NULL_CHECK(cfg, reg, out_of_page) do { \
890 if (cfg->explicit_null_checks || (out_of_page)) { \
891 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, (reg), 0); \
892 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException"); \
894 MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE (cfg); \
898 #define MONO_EMIT_NEW_CHECK_THIS(cfg, sreg) do { \
899 cfg->flags |= MONO_CFG_HAS_CHECK_THIS; \
900 if (cfg->explicit_null_checks) { \
901 MONO_EMIT_NULL_CHECK (cfg, sreg, FALSE); \
903 MONO_EMIT_NEW_UNALU (cfg, OP_CHECK_THIS, -1, sreg); \
904 MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE (cfg); \
906 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, sreg); \
909 #define NEW_LOAD_MEMBASE_FLAGS(cfg,dest,op,dr,base,offset,ins_flags) do { \
910 int __ins_flags = ins_flags; \
911 if (__ins_flags & MONO_INST_FAULT) { \
912 gboolean __out_of_page = offset > mono_target_pagesize (); \
913 MONO_EMIT_NULL_CHECK ((cfg), (base), __out_of_page); \
915 NEW_LOAD_MEMBASE ((cfg), (dest), (op), (dr), (base), (offset)); \
916 (dest)->flags = (__ins_flags); \
919 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS(cfg,op,dr,base,offset,ins_flags) do { \
921 int __ins_flags = ins_flags; \
922 if (__ins_flags & MONO_INST_FAULT) { \
923 int __out_of_page = offset > mono_target_pagesize (); \
924 MONO_EMIT_NULL_CHECK ((cfg), (base), __out_of_page); \
926 NEW_LOAD_MEMBASE ((cfg), (inst), (op), (dr), (base), (offset)); \
927 inst->flags = (__ins_flags); \
928 MONO_ADD_INS (cfg->cbb, inst); \
931 #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))
933 /* A load which can cause a nullref */
934 #define NEW_LOAD_MEMBASE_FAULT(cfg,dest,op,dr,base,offset) NEW_LOAD_MEMBASE_FLAGS ((cfg), (dest), (op), (dr), (base), (offset), MONO_INST_FAULT)
936 #define EMIT_NEW_LOAD_MEMBASE_FAULT(cfg,dest,op,dr,base,offset) do { \
937 NEW_LOAD_MEMBASE_FAULT ((cfg), (dest), (op), (dr), (base), (offset)); \
938 MONO_ADD_INS ((cfg)->cbb, (dest)); \
941 #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)
943 #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))
945 #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)
947 #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)
949 #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))
951 /*Object Model related macros*/
953 /* Default bounds check implementation for most architectures + llvm */
954 #define MONO_EMIT_DEFAULT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg, fault) do { \
955 int _length_reg = alloc_ireg (cfg); \
957 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset); \
959 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset, MONO_INST_INVARIANT_LOAD); \
960 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, _length_reg, index_reg); \
961 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
964 #ifndef MONO_ARCH_EMIT_BOUNDS_CHECK
965 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) MONO_EMIT_DEFAULT_BOUNDS_CHECK ((cfg), (array_reg), (offset), (index_reg), TRUE)
968 #define MONO_EMIT_BOUNDS_CHECK_OFFSET(cfg, array_reg, array_length_offset, index_reg) do { \
969 if (!(cfg->opt & MONO_OPT_UNSAFE)) { \
970 if (!(cfg->opt & MONO_OPT_ABCREM)) { \
971 MONO_EMIT_NULL_CHECK (cfg, array_reg, FALSE); \
972 if (COMPILE_LLVM (cfg)) \
973 MONO_EMIT_DEFAULT_BOUNDS_CHECK ((cfg), (array_reg), (array_length_offset), (index_reg), TRUE); \
975 MONO_ARCH_EMIT_BOUNDS_CHECK ((cfg), (array_reg), (array_length_offset), (index_reg)); \
978 MONO_INST_NEW ((cfg), ins, OP_BOUNDS_CHECK); \
979 ins->sreg1 = array_reg; \
980 ins->sreg2 = index_reg; \
981 ins->inst_imm = (array_length_offset); \
982 ins->flags |= MONO_INST_FAULT; \
983 MONO_ADD_INS ((cfg)->cbb, ins); \
984 (cfg)->flags |= MONO_CFG_NEEDS_DECOMPOSE; \
985 (cfg)->cbb->needs_decompose = TRUE; \
990 /* cfg is the MonoCompile been used
991 * array_reg is the vreg holding the array object
992 * array_type is a struct (usually MonoArray or MonoString)
993 * array_length_field is the field in the previous struct with the length
994 * index_reg is the vreg holding the index
996 #define MONO_EMIT_BOUNDS_CHECK(cfg, array_reg, array_type, array_length_field, index_reg) do { \
997 MONO_EMIT_BOUNDS_CHECK_OFFSET ((cfg), (array_reg), MONO_STRUCT_OFFSET (array_type, array_length_field), (index_reg)); \