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