[amd64] Remove the callee saved registers from MonoLMF, save/restore them normally...
[mono-project.git] / mono / mini / ir-emit.h
blob9ba92bf3765bb3fb377aba0cdfe547a704c3adb6
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 if (mono_arch_is_soft_float ()) {
48 /* Allocate an lvreg so float ops can be decomposed into long ops */
49 return alloc_lreg (cfg);
50 } else {
51 /* Allocate these from the same pool as the int regs */
52 return cfg->next_vreg ++;
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,generic_context) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), (generic_context), STACK_PTR, NULL)
266 #define NEW_TLS_OFFSETCONST(cfg,dest,key) do { \
267 if (cfg->compile_aot) { \
268 NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (key)); \
269 } else { \
270 int _offset = mini_get_tls_offset ((key)); \
271 NEW_PCONST ((cfg), (dest), GINT_TO_POINTER (_offset)); \
273 } while (0)
275 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
276 if (cfg->compile_aot) { \
277 NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, NULL, STACK_OBJ, NULL); \
278 } else { \
279 NEW_PCONST (cfg, args [0], (entry).blob); \
281 } while (0)
283 #define NEW_METHOD_RGCTX_CONST(cfg,dest,method) do { \
284 if (cfg->compile_aot) { \
285 NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHOD_RGCTX, (method)); \
286 } else { \
287 MonoMethodRuntimeGenericContext *mrgctx; \
288 mrgctx = mono_method_lookup_rgctx (mono_class_vtable ((cfg)->domain, (method)->klass), mini_method_get_context ((method))->method_inst); \
289 NEW_PCONST ((cfg), (dest), (mrgctx)); \
291 } while (0)
293 #define NEW_DOMAINCONST(cfg,dest) do { \
294 if ((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) { \
295 /* avoid depending on undefined C behavior in sequence points */ \
296 MonoInst* __domain_var = mono_get_domainvar (cfg); \
297 NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
298 } else { \
299 NEW_PCONST (cfg, dest, (cfg)->domain); \
301 } while (0)
303 #define NEW_JIT_ICALL_ADDRCONST(cfg,dest,name) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_JIT_ICALL_ADDR, (name))
305 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
307 #define NEW_VARLOAD(cfg,dest,var,vartype) do { \
308 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
309 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
310 type_to_eval_stack_type ((cfg), (vartype), (dest)); \
311 (dest)->klass = var->klass; \
312 (dest)->sreg1 = var->dreg; \
313 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
314 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
315 } while (0)
317 #define DECOMPOSE_INTO_REGPAIR(stack_type) (mono_arch_is_soft_float () ? ((stack_type) == STACK_I8 || (stack_type) == STACK_R8) : ((stack_type) == STACK_I8))
319 static inline void
320 handle_gsharedvt_ldaddr (MonoCompile *cfg)
322 /* The decomposition of ldaddr makes use of these two variables, so add uses for them */
323 MonoInst *use;
325 MONO_INST_NEW (cfg, use, OP_DUMMY_USE);
326 use->sreg1 = cfg->gsharedvt_info_var->dreg;
327 MONO_ADD_INS (cfg->cbb, use);
328 MONO_INST_NEW (cfg, use, OP_DUMMY_USE);
329 use->sreg1 = cfg->gsharedvt_locals_var->dreg;
330 MONO_ADD_INS (cfg->cbb, use);
333 #define NEW_VARLOADA(cfg,dest,var,vartype) do { \
334 MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
335 (dest)->inst_p0 = (var); \
336 (var)->flags |= MONO_INST_INDIRECT; \
337 (dest)->type = STACK_MP; \
338 (dest)->klass = (var)->klass; \
339 (dest)->dreg = alloc_dreg ((cfg), STACK_MP); \
340 (cfg)->has_indirection = TRUE; \
341 if (G_UNLIKELY (cfg->gsharedvt) && mini_is_gsharedvt_variable_type ((cfg), (var)->inst_vtype)) { handle_gsharedvt_ldaddr ((cfg)); } \
342 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; } \
343 } while (0)
345 #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do { \
346 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
347 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
348 (dest)->klass = (var)->klass; \
349 (dest)->sreg1 = (inst)->dreg; \
350 (dest)->dreg = (var)->dreg; \
351 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
352 } while (0)
354 #define NEW_TEMPLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype)
356 #define NEW_TEMPLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), cfg->varinfo [(num)], cfg->varinfo [(num)]->inst_vtype)
358 #define NEW_TEMPSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype, (inst))
360 #define NEW_ARGLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
362 #define NEW_LOCLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
364 #define NEW_LOCSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
366 #define NEW_ARGSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
368 #define NEW_LOCLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype)
370 #define NEW_RETLOADA(cfg,dest) do { \
371 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
372 (dest)->type = STACK_MP; \
373 (dest)->klass = cfg->ret->klass; \
374 (dest)->sreg1 = cfg->vret_addr->dreg; \
375 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
376 } while (0)
378 #define NEW_ARGLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), arg_array [(num)], param_types [(num)])
380 /* Promote the vreg to a variable so its address can be taken */
381 #define NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { \
382 MonoInst *var = get_vreg_to_inst ((cfg), (vreg)); \
383 if (!var) \
384 var = mono_compile_create_var_for_vreg ((cfg), (ltype), OP_LOCAL, (vreg)); \
385 NEW_VARLOADA ((cfg), (dest), (var), (ltype)); \
386 } while (0)
388 #define NEW_DUMMY_USE(cfg,dest,var) do { \
389 MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
390 (dest)->sreg1 = var->dreg; \
391 } while (0)
393 /* Variants which take a type argument and handle vtypes as well */
394 #define NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { \
395 NEW_LOAD_MEMBASE ((cfg), (dest), mono_type_to_load_membase ((cfg), (ltype)), 0, (base), (offset)); \
396 type_to_eval_stack_type ((cfg), (ltype), (dest)); \
397 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
398 } while (0)
400 #define NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { \
401 MONO_INST_NEW ((cfg), (dest), mono_type_to_store_membase ((cfg), (ltype))); \
402 (dest)->sreg1 = sr; \
403 (dest)->inst_destbasereg = base; \
404 (dest)->inst_offset = offset; \
405 type_to_eval_stack_type ((cfg), (ltype), (dest)); \
406 (dest)->klass = mono_class_from_mono_type (ltype); \
407 } while (0)
409 #define NEW_SEQ_POINT(cfg,dest,il_offset,intr_loc) do { \
410 MONO_INST_NEW ((cfg), (dest), OP_SEQ_POINT); \
411 (dest)->inst_imm = (il_offset); \
412 (dest)->flags = intr_loc ? MONO_INST_SINGLE_STEP_LOC : 0; \
413 } while (0)
415 #define NEW_GC_PARAM_SLOT_LIVENESS_DEF(cfg,dest,offset,type) do { \
416 MONO_INST_NEW ((cfg), (dest), OP_GC_PARAM_SLOT_LIVENESS_DEF); \
417 (dest)->inst_offset = (offset); \
418 (dest)->inst_vtype = (type); \
419 } while (0)
422 * Variants which do an emit as well.
424 #define EMIT_NEW_ICONST(cfg,dest,val) do { NEW_ICONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
426 #define EMIT_NEW_PCONST(cfg,dest,val) do { NEW_PCONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
428 #define EMIT_NEW_I8CONST(cfg,dest,val) do { NEW_I8CONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
430 #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)
432 #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)
434 #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)
436 #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)
438 #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)
440 #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)
442 #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)
444 #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)
446 #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)
448 #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)
450 #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)
452 #define EMIT_NEW_TLS_OFFSETCONST(cfg,dest,key) do { NEW_TLS_OFFSETCONST ((cfg), (dest), (key)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
454 #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
456 #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
458 #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)
460 #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)
462 #define EMIT_NEW_VARLOAD(cfg,dest,var,vartype) do { NEW_VARLOAD ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
464 #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)
466 #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
468 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
471 * Since the IL stack (and our vregs) contain double values, we have to do a conversion
472 * when loading/storing args/locals of type R4.
475 #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
476 if (!COMPILE_LLVM ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
477 MonoInst *iargs [1]; \
478 EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
479 (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
480 } else { \
481 EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
483 } while (0)
485 #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do { \
486 if (COMPILE_SOFT_FLOAT ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
487 MonoInst *iargs [2]; \
488 iargs [0] = (inst); \
489 EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
490 (dest) = mono_emit_jit_icall (cfg, mono_fstore_r4, iargs); \
491 } else { \
492 EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
494 } while (0)
496 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { \
497 if (mono_arch_is_soft_float ()) { \
498 EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)]); \
499 } else { \
500 NEW_ARGLOAD ((cfg), (dest), (num)); \
501 MONO_ADD_INS ((cfg)->cbb, (dest)); \
503 } while (0)
505 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { \
506 if (mono_arch_is_soft_float ()) { \
507 EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)]); \
508 } else { \
509 NEW_LOCLOAD ((cfg), (dest), (num)); \
510 MONO_ADD_INS ((cfg)->cbb, (dest)); \
512 } while (0)
514 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { \
515 if (mono_arch_is_soft_float ()) { \
516 EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst)); \
517 } else { \
518 NEW_LOCSTORE ((cfg), (dest), (num), (inst)); \
519 MONO_ADD_INS ((cfg)->cbb, (dest)); \
521 } while (0)
523 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { \
524 if (mono_arch_is_soft_float ()) { \
525 EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst)); \
526 } else { \
527 NEW_ARGSTORE ((cfg), (dest), (num), (inst)); \
528 MONO_ADD_INS ((cfg)->cbb, (dest)); \
530 } while (0)
532 #else
534 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { NEW_ARGLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
536 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { NEW_LOCLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
538 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { NEW_LOCSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
540 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
542 #endif
544 #define EMIT_NEW_TEMPLOAD(cfg,dest,num) do { NEW_TEMPLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
546 #define EMIT_NEW_TEMPLOADA(cfg,dest,num) do { NEW_TEMPLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
548 #define EMIT_NEW_LOCLOADA(cfg,dest,num) do { NEW_LOCLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
550 #define EMIT_NEW_ARGLOADA(cfg,dest,num) do { NEW_ARGLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
552 #define EMIT_NEW_RETLOADA(cfg,dest) do { NEW_RETLOADA ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
554 #define EMIT_NEW_TEMPSTORE(cfg,dest,num,inst) do { NEW_TEMPSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
556 #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)
558 #define EMIT_NEW_DUMMY_USE(cfg,dest,var) do { NEW_DUMMY_USE ((cfg), (dest), (var)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
560 #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)
562 #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)
564 #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)
566 #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)
568 #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)
570 #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)
572 #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)
574 #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)
576 * Variants which do not take an dest argument, but take a dreg argument.
578 #define MONO_EMIT_NEW_ICONST(cfg,dr,imm) do { \
579 MonoInst *inst; \
580 MONO_INST_NEW ((cfg), (inst), OP_ICONST); \
581 inst->dreg = dr; \
582 inst->inst_c0 = imm; \
583 MONO_ADD_INS ((cfg)->cbb, inst); \
584 } while (0)
586 #define MONO_EMIT_NEW_PCONST(cfg,dr,val) do { \
587 MonoInst *inst; \
588 MONO_INST_NEW ((cfg), (inst), OP_PCONST); \
589 inst->dreg = dr; \
590 (inst)->inst_p0 = (val); \
591 (inst)->type = STACK_PTR; \
592 MONO_ADD_INS ((cfg)->cbb, inst); \
593 } while (0)
595 #define MONO_EMIT_NEW_I8CONST(cfg,dr,imm) do { \
596 MonoInst *inst; \
597 MONO_INST_NEW ((cfg), (inst), OP_I8CONST); \
598 inst->dreg = dr; \
599 inst->inst_l = imm; \
600 MONO_ADD_INS ((cfg)->cbb, inst); \
601 } while (0)
603 #define MONO_EMIT_NEW_DUMMY_INIT(cfg,dr,op) do { \
604 MonoInst *inst; \
605 MONO_INST_NEW ((cfg), (inst), (op)); \
606 inst->dreg = dr; \
607 MONO_ADD_INS ((cfg)->cbb, inst); \
608 } while (0)
610 #ifdef MONO_ARCH_NEED_GOT_VAR
612 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
613 MonoInst *inst; \
614 NEW_AOTCONST ((cfg), (inst), (patch_type), (cons)); \
615 inst->dreg = (dr); \
616 MONO_ADD_INS ((cfg)->cbb, inst); \
617 } while (0)
619 #else
621 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,type) do { \
622 MonoInst *inst; \
623 MONO_INST_NEW ((cfg), (inst), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
624 inst->dreg = dr; \
625 inst->inst_p0 = imm; \
626 inst->inst_c1 = type; \
627 MONO_ADD_INS ((cfg)->cbb, inst); \
628 } while (0)
630 #endif
632 #define MONO_EMIT_NEW_CLASSCONST(cfg,dr,imm) MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,MONO_PATCH_INFO_CLASS)
633 #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)
634 #define MONO_EMIT_NEW_SIGNATURECONST(cfg,dr,sig) MONO_EMIT_NEW_AOTCONST ((cfg), (dr), (sig), MONO_PATCH_INFO_SIGNATURE)
636 #define MONO_EMIT_NEW_VZERO(cfg,dr,kl) do { \
637 MonoInst *inst; \
638 MONO_INST_NEW ((cfg), (inst), MONO_CLASS_IS_SIMD (cfg, kl) ? OP_XZERO : OP_VZERO); \
639 inst->dreg = dr; \
640 (inst)->type = STACK_VTYPE; \
641 (inst)->klass = (kl); \
642 MONO_ADD_INS ((cfg)->cbb, inst); \
643 } while (0)
645 #define MONO_EMIT_NEW_UNALU(cfg,op,dr,sr1) do { \
646 MonoInst *inst; \
647 EMIT_NEW_UNALU ((cfg), (inst), (op), (dr), (sr1)); \
648 } while (0)
650 #define MONO_EMIT_NEW_BIALU(cfg,op,dr,sr1,sr2) do { \
651 MonoInst *inst; \
652 MONO_INST_NEW ((cfg), (inst), (op)); \
653 inst->dreg = dr; \
654 inst->sreg1 = sr1; \
655 inst->sreg2 = sr2; \
656 MONO_ADD_INS (cfg->cbb, inst); \
657 } while (0)
659 #define MONO_EMIT_NEW_BIALU_IMM(cfg,op,dr,sr,imm) do { \
660 MonoInst *inst; \
661 MONO_INST_NEW ((cfg), (inst), (op)); \
662 inst->dreg = dr; \
663 inst->sreg1 = sr; \
664 inst->inst_imm = (mgreg_t)(imm); \
665 MONO_ADD_INS (cfg->cbb, inst); \
666 } while (0)
668 #define MONO_EMIT_NEW_COMPARE_IMM(cfg,sr1,imm) do { \
669 MonoInst *inst; \
670 MONO_INST_NEW ((cfg), (inst), (OP_COMPARE_IMM)); \
671 inst->sreg1 = sr1; \
672 inst->inst_imm = (imm); \
673 MONO_ADD_INS ((cfg)->cbb, inst); \
674 } while (0)
676 #define MONO_EMIT_NEW_ICOMPARE_IMM(cfg,sr1,imm) do { \
677 MonoInst *inst; \
678 MONO_INST_NEW ((cfg), (inst), sizeof (mgreg_t) == 8 ? OP_ICOMPARE_IMM : OP_COMPARE_IMM); \
679 inst->sreg1 = sr1; \
680 inst->inst_imm = (imm); \
681 MONO_ADD_INS ((cfg)->cbb, inst); \
682 } while (0)
684 /* This is used on 32 bit machines too when running with LLVM */
685 #define MONO_EMIT_NEW_LCOMPARE_IMM(cfg,sr1,imm) do { \
686 MonoInst *inst; \
687 MONO_INST_NEW ((cfg), (inst), (OP_LCOMPARE_IMM)); \
688 inst->sreg1 = sr1; \
689 if (SIZEOF_REGISTER == 4 && COMPILE_LLVM (cfg)) { \
690 guint64 _l = (imm); \
691 inst->inst_imm = _l & 0xffffffff; \
692 inst->inst_offset = _l >> 32; \
693 } else { \
694 inst->inst_imm = (imm); \
696 MONO_ADD_INS ((cfg)->cbb, inst); \
697 } while (0)
699 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg,op,dr,base,offset) do { \
700 MonoInst *inst; \
701 MONO_INST_NEW ((cfg), (inst), (op)); \
702 inst->dreg = dr; \
703 inst->inst_basereg = base; \
704 inst->inst_offset = offset; \
705 MONO_ADD_INS (cfg->cbb, inst); \
706 } while (0)
708 #define MONO_EMIT_NEW_LOAD_MEMBASE(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
710 #define MONO_EMIT_NEW_STORE_MEMBASE(cfg,op,base,offset,sr) do { \
711 MonoInst *inst; \
712 MONO_INST_NEW ((cfg), (inst), (op)); \
713 (inst)->sreg1 = sr; \
714 (inst)->inst_destbasereg = base; \
715 (inst)->inst_offset = offset; \
716 MONO_ADD_INS (cfg->cbb, inst); \
717 } while (0)
719 #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
720 MonoInst *inst; \
721 MONO_INST_NEW ((cfg), (inst), (op)); \
722 inst->inst_destbasereg = base; \
723 inst->inst_offset = offset; \
724 inst->inst_imm = (mgreg_t)(imm); \
725 MONO_ADD_INS ((cfg)->cbb, inst); \
726 } while (0)
728 #define MONO_EMIT_NEW_COND_EXC(cfg,cond,name) do { \
729 MonoInst *inst; \
730 MONO_INST_NEW ((cfg), (inst), (OP_COND_EXC_##cond)); \
731 inst->inst_p1 = (char*)name; \
732 MONO_ADD_INS ((cfg)->cbb, inst); \
733 } while (0)
735 /* Branch support */
738 * Basic blocks have two numeric identifiers:
739 * dfn: Depth First Number
740 * block_num: unique ID assigned at bblock creation
742 #define NEW_BBLOCK(cfg,bblock) do { \
743 (bblock) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
744 (bblock)->block_num = cfg->num_bblocks++; \
745 } while (0)
747 #define ADD_BBLOCK(cfg,b) do { \
748 if ((b)->cil_code) {\
749 cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b); \
751 (b)->real_offset = cfg->real_offset; \
752 } while (0)
754 /* Emit a one-way conditional branch */
756 * The inst_false_bb field of the cond branch will not be set, the JIT code should be
757 * prepared to deal with this.
759 #ifdef DEBUG_EXTENDED_BBLOCKS
760 static int ccount = 0;
761 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
762 MonoInst *ins; \
763 MonoBasicBlock *falsebb; \
764 MONO_INST_NEW ((cfg), (ins), (op)); \
765 if ((op) == OP_BR) { \
766 NEW_BBLOCK ((cfg), falsebb); \
767 ins->inst_target_bb = (truebb); \
768 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
769 MONO_ADD_INS ((cfg)->cbb, ins); \
770 MONO_START_BB ((cfg), falsebb); \
771 } else { \
772 ccount ++; \
773 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
774 ins->inst_true_bb = (truebb); \
775 ins->inst_false_bb = NULL; \
776 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
777 MONO_ADD_INS ((cfg)->cbb, ins); \
778 if (g_getenv ("COUNT2") && ccount == atoi (g_getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
779 if (g_getenv ("COUNT2") && ccount < atoi (g_getenv ("COUNT2"))) { \
780 cfg->cbb->extended = TRUE; \
781 } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
783 } while (0)
784 #else
785 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
786 MonoInst *ins; \
787 MonoBasicBlock *falsebb; \
788 MONO_INST_NEW ((cfg), (ins), (op)); \
789 if ((op) == OP_BR) { \
790 NEW_BBLOCK ((cfg), falsebb); \
791 ins->inst_target_bb = (truebb); \
792 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
793 MONO_ADD_INS ((cfg)->cbb, ins); \
794 MONO_START_BB ((cfg), falsebb); \
795 } else { \
796 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
797 ins->inst_true_bb = (truebb); \
798 ins->inst_false_bb = NULL; \
799 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
800 MONO_ADD_INS ((cfg)->cbb, ins); \
801 if (!cfg->enable_extended_bblocks) { \
802 NEW_BBLOCK ((cfg), falsebb); \
803 ins->inst_false_bb = falsebb; \
804 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
805 MONO_START_BB ((cfg), falsebb); \
806 } else { \
807 cfg->cbb->extended = TRUE; \
810 } while (0)
811 #endif
813 /* Emit a two-way conditional branch */
814 #define MONO_EMIT_NEW_BRANCH_BLOCK2(cfg,op,truebb,falsebb) do { \
815 MonoInst *ins; \
816 MONO_INST_NEW ((cfg), (ins), (op)); \
817 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
818 ins->inst_true_bb = (truebb); \
819 ins->inst_false_bb = (falsebb); \
820 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
821 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
822 MONO_ADD_INS ((cfg)->cbb, ins); \
823 } while (0)
825 #define MONO_START_BB(cfg, bblock) do { \
826 ADD_BBLOCK ((cfg), (bblock)); \
827 if (cfg->cbb->last_ins && MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins) && !cfg->cbb->last_ins->inst_false_bb) { \
828 cfg->cbb->last_ins->inst_false_bb = (bblock); \
829 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
830 } 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)))) \
831 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
832 (cfg)->cbb->next_bb = (bblock); \
833 (cfg)->cbb = (bblock); \
834 } while (0)
836 /* This marks a place in code where an implicit exception could be thrown */
837 #define MONO_EMIT_NEW_IMPLICIT_EXCEPTION(cfg) do { \
838 if (COMPILE_LLVM ((cfg))) { \
839 MONO_EMIT_NEW_UNALU (cfg, OP_IMPLICIT_EXCEPTION, -1, -1); \
841 } while (0)
843 /* Loads/Stores which can fault are handled correctly by the LLVM mono branch */
844 #define MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE(cfg) do { \
845 } while (0)
847 /* Emit an explicit null check which doesn't depend on SIGSEGV signal handling */
848 #define MONO_EMIT_NULL_CHECK(cfg, reg) do { \
849 if (cfg->explicit_null_checks) { \
850 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, (reg), 0); \
851 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException"); \
852 } else { \
853 MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE (cfg); \
855 } while (0)
857 #define MONO_EMIT_NEW_CHECK_THIS(cfg, sreg) do { \
858 cfg->flags |= MONO_CFG_HAS_CHECK_THIS; \
859 if (cfg->explicit_null_checks) { \
860 MONO_EMIT_NULL_CHECK (cfg, sreg); \
861 } else { \
862 MONO_EMIT_NEW_UNALU (cfg, OP_CHECK_THIS, -1, sreg); \
863 MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE (cfg); \
865 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, sreg); \
866 } while (0)
868 #define NEW_LOAD_MEMBASE_FLAGS(cfg,dest,op,dr,base,offset,ins_flags) do { \
869 int __ins_flags = ins_flags; \
870 if (__ins_flags & MONO_INST_FAULT) { \
871 MONO_EMIT_NULL_CHECK ((cfg), (base)); \
873 NEW_LOAD_MEMBASE ((cfg), (dest), (op), (dr), (base), (offset)); \
874 (dest)->flags = (__ins_flags); \
875 } while (0)
877 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS(cfg,op,dr,base,offset,ins_flags) do { \
878 MonoInst *inst; \
879 int __ins_flags = ins_flags; \
880 if (__ins_flags & MONO_INST_FAULT) { \
881 MONO_EMIT_NULL_CHECK ((cfg), (base)); \
883 NEW_LOAD_MEMBASE ((cfg), (inst), (op), (dr), (base), (offset)); \
884 inst->flags = (__ins_flags); \
885 MONO_ADD_INS (cfg->cbb, inst); \
886 } while (0)
888 #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))
890 /* A load which can cause a nullref */
891 #define NEW_LOAD_MEMBASE_FAULT(cfg,dest,op,dr,base,offset) NEW_LOAD_MEMBASE_FLAGS ((cfg), (dest), (op), (dr), (base), (offset), MONO_INST_FAULT)
893 #define EMIT_NEW_LOAD_MEMBASE_FAULT(cfg,dest,op,dr,base,offset) do { \
894 NEW_LOAD_MEMBASE_FAULT ((cfg), (dest), (op), (dr), (base), (offset)); \
895 MONO_ADD_INS ((cfg)->cbb, (dest)); \
896 } while (0)
898 #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)
900 #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))
902 #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)
904 #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)
906 #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))
908 /*Object Model related macros*/
910 /* Default bounds check implementation for most architectures + llvm */
911 #define MONO_EMIT_DEFAULT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg, fault) do { \
912 int _length_reg = alloc_ireg (cfg); \
913 if (fault) \
914 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset); \
915 else \
916 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset, MONO_INST_INVARIANT_LOAD); \
917 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, _length_reg, index_reg); \
918 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
919 } while (0)
921 #ifndef MONO_ARCH_EMIT_BOUNDS_CHECK
922 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) MONO_EMIT_DEFAULT_BOUNDS_CHECK ((cfg), (array_reg), (offset), (index_reg), TRUE)
923 #endif
925 /* cfg is the MonoCompile been used
926 * array_reg is the vreg holding the array object
927 * array_type is a struct (usually MonoArray or MonoString)
928 * array_length_field is the field in the previous struct with the length
929 * index_reg is the vreg holding the index
931 #define MONO_EMIT_BOUNDS_CHECK(cfg, array_reg, array_type, array_length_field, index_reg) do { \
932 if (!(cfg->opt & MONO_OPT_UNSAFE)) { \
933 if (!(cfg->opt & MONO_OPT_ABCREM)) { \
934 MONO_EMIT_NULL_CHECK (cfg, array_reg); \
935 if (COMPILE_LLVM (cfg)) \
936 MONO_EMIT_DEFAULT_BOUNDS_CHECK ((cfg), (array_reg), G_STRUCT_OFFSET (array_type, array_length_field), (index_reg), TRUE); \
937 else \
938 MONO_ARCH_EMIT_BOUNDS_CHECK ((cfg), (array_reg), G_STRUCT_OFFSET (array_type, array_length_field), (index_reg)); \
939 } else { \
940 MonoInst *ins; \
941 MONO_INST_NEW ((cfg), ins, OP_BOUNDS_CHECK); \
942 ins->sreg1 = array_reg; \
943 ins->sreg2 = index_reg; \
944 ins->inst_imm = G_STRUCT_OFFSET (array_type, array_length_field); \
945 ins->flags |= MONO_INST_FAULT; \
946 MONO_ADD_INS ((cfg)->cbb, ins); \
947 (cfg)->flags |= MONO_CFG_HAS_ARRAY_ACCESS; \
948 (cfg)->cbb->has_array_access = TRUE; \
951 } while (0)
953 G_END_DECLS
955 #endif