Merge pull request #1861 from saper/home-override
[mono-project.git] / mono / mini / ir-emit.h
blobfe05ecca2d36a5569abfd30b622b71bf3868d86b
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_R4:
90 case STACK_R8:
91 return alloc_freg (cfg);
92 case STACK_I8:
93 return alloc_lreg (cfg);
94 case STACK_VTYPE:
95 return alloc_ireg (cfg);
96 default:
97 g_warning ("Unknown stack type %x\n", stack_type);
98 g_assert_not_reached ();
99 return -1;
103 #undef MONO_INST_NEW
105 * FIXME: zeroing out some fields is not needed with the new IR, but the old
106 * JIT code still uses the left and right fields, so it has to stay.
108 #define MONO_INST_NEW(cfg,dest,op) do { \
109 (dest) = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoInst)); \
110 (dest)->inst_c0 = (dest)->inst_c1 = 0; \
111 (dest)->next = (dest)->prev = NULL; \
112 (dest)->opcode = (op); \
113 (dest)->flags = 0; \
114 (dest)->type = 0; \
115 (dest)->dreg = -1; \
116 MONO_INST_NULLIFY_SREGS ((dest)); \
117 (dest)->cil_code = (cfg)->ip; \
118 } while (0)
121 * Variants which take a dest argument and don't do an emit
123 #define NEW_ICONST(cfg,dest,val) do { \
124 MONO_INST_NEW ((cfg), (dest), OP_ICONST); \
125 (dest)->inst_c0 = (val); \
126 (dest)->type = STACK_I4; \
127 (dest)->dreg = alloc_dreg ((cfg), STACK_I4); \
128 } while (0)
131 * Avoid using this with a non-NULL val if possible as it is not AOT
132 * compatible. Use one of the NEW_xxxCONST variants instead.
134 #define NEW_PCONST(cfg,dest,val) do { \
135 MONO_INST_NEW ((cfg), (dest), OP_PCONST); \
136 (dest)->inst_p0 = (val); \
137 (dest)->type = STACK_PTR; \
138 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
139 } while (0)
141 #define NEW_I8CONST(cfg,dest,val) do { \
142 MONO_INST_NEW ((cfg), (dest), OP_I8CONST); \
143 (dest)->dreg = alloc_lreg ((cfg)); \
144 (dest)->type = STACK_I8; \
145 (dest)->inst_l = (val); \
146 } while (0)
148 #define NEW_STORE_MEMBASE(cfg,dest,op,base,offset,sr) do { \
149 MONO_INST_NEW ((cfg), (dest), (op)); \
150 (dest)->sreg1 = sr; \
151 (dest)->inst_destbasereg = base; \
152 (dest)->inst_offset = offset; \
153 } while (0)
155 #define NEW_LOAD_MEMBASE(cfg,dest,op,dr,base,offset) do { \
156 MONO_INST_NEW ((cfg), (dest), (op)); \
157 (dest)->dreg = (dr); \
158 (dest)->inst_basereg = (base); \
159 (dest)->inst_offset = (offset); \
160 (dest)->type = STACK_I4; \
161 } while (0)
163 #define NEW_LOAD_MEM(cfg,dest,op,dr,mem) do { \
164 MONO_INST_NEW ((cfg), (dest), (op)); \
165 (dest)->dreg = (dr); \
166 (dest)->inst_p0 = (gpointer)(gssize)(mem); \
167 (dest)->type = STACK_I4; \
168 } while (0)
170 #define NEW_UNALU(cfg,dest,op,dr,sr1) do { \
171 MONO_INST_NEW ((cfg), (dest), (op)); \
172 (dest)->dreg = dr; \
173 (dest)->sreg1 = sr1; \
174 } while (0)
176 #define NEW_BIALU(cfg,dest,op,dr,sr1,sr2) do { \
177 MONO_INST_NEW ((cfg), (dest), (op)); \
178 (dest)->dreg = (dr); \
179 (dest)->sreg1 = (sr1); \
180 (dest)->sreg2 = (sr2); \
181 } while (0)
183 #define NEW_BIALU_IMM(cfg,dest,op,dr,sr,imm) do { \
184 MONO_INST_NEW ((cfg), (dest), (op)); \
185 (dest)->dreg = dr; \
186 (dest)->sreg1 = sr; \
187 (dest)->inst_imm = (imm); \
188 } while (0)
190 #ifdef MONO_ARCH_NEED_GOT_VAR
192 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do { \
193 MONO_INST_NEW ((cfg), (dest), OP_PATCH_INFO); \
194 (dest)->inst_left = (gpointer)(el1); \
195 (dest)->inst_right = (gpointer)(el2); \
196 } while (0)
198 /* FIXME: Add the PUSH_GOT_ENTRY optimizations */
199 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
200 MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST); \
201 if (cfg->compile_aot) { \
202 MonoInst *group, *got_loc; \
203 got_loc = mono_get_got_var (cfg); \
204 NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
205 (dest)->inst_basereg = got_loc->dreg; \
206 (dest)->inst_p1 = group; \
207 } else { \
208 (dest)->inst_p0 = (cons); \
209 (dest)->inst_i1 = (gpointer)(patch_type); \
211 (dest)->type = STACK_PTR; \
212 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
213 } while (0)
215 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
216 MonoInst *group, *got_loc; \
217 MONO_INST_NEW ((cfg), (dest), OP_GOT_ENTRY); \
218 got_loc = mono_get_got_var (cfg); \
219 NEW_PATCH_INFO ((cfg), group, NULL, patch_type); \
220 group->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
221 (dest)->inst_basereg = got_loc->dreg; \
222 (dest)->inst_p1 = group; \
223 (dest)->type = (stack_type); \
224 (dest)->klass = (stack_class); \
225 (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
226 } while (0)
228 #else
230 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
231 MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
232 (dest)->inst_p0 = (cons); \
233 (dest)->inst_i1 = (gpointer)(patch_type); \
234 (dest)->type = STACK_PTR; \
235 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
236 } while (0)
238 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
239 MONO_INST_NEW ((cfg), (dest), OP_AOTCONST); \
240 (dest)->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
241 (dest)->inst_p1 = (gpointer)(patch_type); \
242 (dest)->type = (stack_type); \
243 (dest)->klass = (stack_class); \
244 (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
245 } while (0)
247 #endif
249 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
251 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
253 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
255 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
257 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
259 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
261 #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)
263 #define NEW_LDSTRLITCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_LDSTR_LIT, (val))
265 #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)
267 #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)
269 #define NEW_TLS_OFFSETCONST(cfg,dest,key) do { \
270 if (cfg->compile_aot) { \
271 NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (key)); \
272 } else { \
273 int _offset = mini_get_tls_offset ((key)); \
274 NEW_PCONST ((cfg), (dest), GINT_TO_POINTER (_offset)); \
276 } while (0)
278 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
279 if (cfg->compile_aot) { \
280 NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, NULL, STACK_OBJ, NULL); \
281 } else { \
282 NEW_PCONST (cfg, args [0], (entry).blob); \
284 } while (0)
286 #define NEW_METHOD_RGCTX_CONST(cfg,dest,method) do { \
287 if (cfg->compile_aot) { \
288 NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHOD_RGCTX, (method)); \
289 } else { \
290 MonoMethodRuntimeGenericContext *mrgctx; \
291 mrgctx = mono_method_lookup_rgctx (mono_class_vtable ((cfg)->domain, (method)->klass), mini_method_get_context ((method))->method_inst); \
292 NEW_PCONST ((cfg), (dest), (mrgctx)); \
294 } while (0)
296 #define NEW_DOMAINCONST(cfg,dest) do { \
297 if ((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) { \
298 /* avoid depending on undefined C behavior in sequence points */ \
299 MonoInst* __domain_var = mono_get_domainvar (cfg); \
300 NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
301 } else { \
302 NEW_PCONST (cfg, dest, (cfg)->domain); \
304 } while (0)
306 #define NEW_JIT_ICALL_ADDRCONST(cfg,dest,name) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_JIT_ICALL_ADDR, (name))
308 #define NEW_VARLOAD(cfg,dest,var,vartype) do { \
309 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
310 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
311 type_to_eval_stack_type ((cfg), (vartype), (dest)); \
312 (dest)->klass = var->klass; \
313 (dest)->sreg1 = var->dreg; \
314 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
315 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
316 } while (0)
318 #define DECOMPOSE_INTO_REGPAIR(stack_type) (mono_arch_is_soft_float () ? ((stack_type) == STACK_I8 || (stack_type) == STACK_R8) : ((stack_type) == STACK_I8))
320 static inline void
321 handle_gsharedvt_ldaddr (MonoCompile *cfg)
323 /* The decomposition of ldaddr makes use of these two variables, so add uses for them */
324 MonoInst *use;
326 MONO_INST_NEW (cfg, use, OP_DUMMY_USE);
327 use->sreg1 = cfg->gsharedvt_info_var->dreg;
328 MONO_ADD_INS (cfg->cbb, use);
329 MONO_INST_NEW (cfg, use, OP_DUMMY_USE);
330 use->sreg1 = cfg->gsharedvt_locals_var->dreg;
331 MONO_ADD_INS (cfg->cbb, use);
334 #define NEW_VARLOADA(cfg,dest,var,vartype) do { \
335 MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
336 (dest)->inst_p0 = (var); \
337 (var)->flags |= MONO_INST_INDIRECT; \
338 (dest)->type = STACK_MP; \
339 (dest)->klass = (var)->klass; \
340 (dest)->dreg = alloc_dreg ((cfg), STACK_MP); \
341 (cfg)->has_indirection = TRUE; \
342 if (G_UNLIKELY (cfg->gsharedvt) && mini_is_gsharedvt_variable_type ((cfg), (var)->inst_vtype)) { handle_gsharedvt_ldaddr ((cfg)); } \
343 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; } \
344 } while (0)
346 #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do { \
347 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
348 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
349 (dest)->klass = (var)->klass; \
350 (dest)->sreg1 = (inst)->dreg; \
351 (dest)->dreg = (var)->dreg; \
352 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
353 } while (0)
355 #define NEW_TEMPLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype)
357 #define NEW_TEMPLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), cfg->varinfo [(num)], cfg->varinfo [(num)]->inst_vtype)
359 #define NEW_TEMPSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype, (inst))
361 #define NEW_ARGLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
363 #define NEW_LOCLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
365 #define NEW_LOCSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
367 #define NEW_ARGSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
369 #define NEW_LOCLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype)
371 #define NEW_RETLOADA(cfg,dest) do { \
372 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
373 (dest)->type = STACK_MP; \
374 (dest)->klass = cfg->ret->klass; \
375 (dest)->sreg1 = cfg->vret_addr->dreg; \
376 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
377 } while (0)
379 #define NEW_ARGLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), arg_array [(num)], param_types [(num)])
381 /* Promote the vreg to a variable so its address can be taken */
382 #define NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { \
383 MonoInst *var = get_vreg_to_inst ((cfg), (vreg)); \
384 if (!var) \
385 var = mono_compile_create_var_for_vreg ((cfg), (ltype), OP_LOCAL, (vreg)); \
386 NEW_VARLOADA ((cfg), (dest), (var), (ltype)); \
387 } while (0)
389 #define NEW_DUMMY_USE(cfg,dest,var) do { \
390 MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
391 (dest)->sreg1 = var->dreg; \
392 } while (0)
394 /* Variants which take a type argument and handle vtypes as well */
395 #define NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { \
396 NEW_LOAD_MEMBASE ((cfg), (dest), mono_type_to_load_membase ((cfg), (ltype)), 0, (base), (offset)); \
397 type_to_eval_stack_type ((cfg), (ltype), (dest)); \
398 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
399 } while (0)
401 #define NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { \
402 MONO_INST_NEW ((cfg), (dest), mono_type_to_store_membase ((cfg), (ltype))); \
403 (dest)->sreg1 = sr; \
404 (dest)->inst_destbasereg = base; \
405 (dest)->inst_offset = offset; \
406 type_to_eval_stack_type ((cfg), (ltype), (dest)); \
407 (dest)->klass = mono_class_from_mono_type (ltype); \
408 } while (0)
410 #define NEW_SEQ_POINT(cfg,dest,il_offset,intr_loc) do { \
411 MONO_INST_NEW ((cfg), (dest), cfg->gen_sdb_seq_points ? OP_SEQ_POINT : OP_IL_SEQ_POINT); \
412 (dest)->inst_imm = (il_offset); \
413 (dest)->flags = intr_loc ? MONO_INST_SINGLE_STEP_LOC : 0; \
414 } while (0)
416 #define NEW_GC_PARAM_SLOT_LIVENESS_DEF(cfg,dest,offset,type) do { \
417 MONO_INST_NEW ((cfg), (dest), OP_GC_PARAM_SLOT_LIVENESS_DEF); \
418 (dest)->inst_offset = (offset); \
419 (dest)->inst_vtype = (type); \
420 } while (0)
423 * Variants which do an emit as well.
425 #define EMIT_NEW_ICONST(cfg,dest,val) do { NEW_ICONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
427 #define EMIT_NEW_PCONST(cfg,dest,val) do { NEW_PCONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
429 #define EMIT_NEW_I8CONST(cfg,dest,val) do { NEW_I8CONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
431 #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)
433 #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)
435 #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)
437 #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)
439 #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)
441 #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)
443 #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)
445 #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)
447 #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)
449 #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)
451 #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)
453 #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)
455 #define EMIT_NEW_TLS_OFFSETCONST(cfg,dest,key) do { NEW_TLS_OFFSETCONST ((cfg), (dest), (key)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
457 #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
459 #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
461 #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)
463 #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)
465 #define EMIT_NEW_VARLOAD(cfg,dest,var,vartype) do { NEW_VARLOAD ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
467 #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)
469 #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
471 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
474 * Since the IL stack (and our vregs) contain double values, we have to do a conversion
475 * when loading/storing args/locals of type R4.
478 #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
479 if (!COMPILE_LLVM ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
480 MonoInst *iargs [1]; \
481 EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
482 (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
483 } else { \
484 EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
486 } while (0)
488 #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do { \
489 if (COMPILE_SOFT_FLOAT ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
490 MonoInst *iargs [2]; \
491 iargs [0] = (inst); \
492 EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
493 (dest) = mono_emit_jit_icall (cfg, mono_fstore_r4, iargs); \
494 } else { \
495 EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
497 } while (0)
499 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { \
500 if (mono_arch_is_soft_float ()) { \
501 EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)]); \
502 } else { \
503 NEW_ARGLOAD ((cfg), (dest), (num)); \
504 MONO_ADD_INS ((cfg)->cbb, (dest)); \
506 } while (0)
508 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { \
509 if (mono_arch_is_soft_float ()) { \
510 EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)]); \
511 } else { \
512 NEW_LOCLOAD ((cfg), (dest), (num)); \
513 MONO_ADD_INS ((cfg)->cbb, (dest)); \
515 } while (0)
517 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { \
518 if (mono_arch_is_soft_float ()) { \
519 EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst)); \
520 } else { \
521 NEW_LOCSTORE ((cfg), (dest), (num), (inst)); \
522 MONO_ADD_INS ((cfg)->cbb, (dest)); \
524 } while (0)
526 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { \
527 if (mono_arch_is_soft_float ()) { \
528 EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst)); \
529 } else { \
530 NEW_ARGSTORE ((cfg), (dest), (num), (inst)); \
531 MONO_ADD_INS ((cfg)->cbb, (dest)); \
533 } while (0)
535 #else
537 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { NEW_ARGLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
539 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { NEW_LOCLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
541 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { NEW_LOCSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
543 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
545 #endif
547 #define EMIT_NEW_TEMPLOAD(cfg,dest,num) do { NEW_TEMPLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
549 #define EMIT_NEW_TEMPLOADA(cfg,dest,num) do { NEW_TEMPLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
551 #define EMIT_NEW_LOCLOADA(cfg,dest,num) do { NEW_LOCLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
553 #define EMIT_NEW_ARGLOADA(cfg,dest,num) do { NEW_ARGLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
555 #define EMIT_NEW_RETLOADA(cfg,dest) do { NEW_RETLOADA ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
557 #define EMIT_NEW_TEMPSTORE(cfg,dest,num,inst) do { NEW_TEMPSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
559 #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)
561 #define EMIT_NEW_DUMMY_USE(cfg,dest,var) do { NEW_DUMMY_USE ((cfg), (dest), (var)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
563 #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)
565 #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)
567 #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)
569 #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)
571 #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)
573 #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)
575 #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)
577 #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)
579 * Variants which do not take an dest argument, but take a dreg argument.
581 #define MONO_EMIT_NEW_ICONST(cfg,dr,imm) do { \
582 MonoInst *inst; \
583 MONO_INST_NEW ((cfg), (inst), OP_ICONST); \
584 inst->dreg = dr; \
585 inst->inst_c0 = imm; \
586 MONO_ADD_INS ((cfg)->cbb, inst); \
587 } while (0)
589 #define MONO_EMIT_NEW_PCONST(cfg,dr,val) do { \
590 MonoInst *inst; \
591 MONO_INST_NEW ((cfg), (inst), OP_PCONST); \
592 inst->dreg = dr; \
593 (inst)->inst_p0 = (val); \
594 (inst)->type = STACK_PTR; \
595 MONO_ADD_INS ((cfg)->cbb, inst); \
596 } while (0)
598 #define MONO_EMIT_NEW_I8CONST(cfg,dr,imm) do { \
599 MonoInst *inst; \
600 MONO_INST_NEW ((cfg), (inst), OP_I8CONST); \
601 inst->dreg = dr; \
602 inst->inst_l = imm; \
603 MONO_ADD_INS ((cfg)->cbb, inst); \
604 } while (0)
606 #define MONO_EMIT_NEW_DUMMY_INIT(cfg,dr,op) do { \
607 MonoInst *inst; \
608 MONO_INST_NEW ((cfg), (inst), (op)); \
609 inst->dreg = dr; \
610 MONO_ADD_INS ((cfg)->cbb, inst); \
611 } while (0)
613 #ifdef MONO_ARCH_NEED_GOT_VAR
615 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
616 MonoInst *inst; \
617 NEW_AOTCONST ((cfg), (inst), (patch_type), (cons)); \
618 inst->dreg = (dr); \
619 MONO_ADD_INS ((cfg)->cbb, inst); \
620 } while (0)
622 #else
624 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,type) do { \
625 MonoInst *inst; \
626 MONO_INST_NEW ((cfg), (inst), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
627 inst->dreg = dr; \
628 inst->inst_p0 = imm; \
629 inst->inst_c1 = type; \
630 MONO_ADD_INS ((cfg)->cbb, inst); \
631 } while (0)
633 #endif
635 #define MONO_EMIT_NEW_CLASSCONST(cfg,dr,imm) MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,MONO_PATCH_INFO_CLASS)
636 #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)
637 #define MONO_EMIT_NEW_SIGNATURECONST(cfg,dr,sig) MONO_EMIT_NEW_AOTCONST ((cfg), (dr), (sig), MONO_PATCH_INFO_SIGNATURE)
639 #define MONO_EMIT_NEW_VZERO(cfg,dr,kl) do { \
640 MonoInst *inst; \
641 MONO_INST_NEW ((cfg), (inst), MONO_CLASS_IS_SIMD (cfg, kl) ? OP_XZERO : OP_VZERO); \
642 inst->dreg = dr; \
643 (inst)->type = STACK_VTYPE; \
644 (inst)->klass = (kl); \
645 MONO_ADD_INS ((cfg)->cbb, inst); \
646 } while (0)
648 #define MONO_EMIT_NEW_UNALU(cfg,op,dr,sr1) do { \
649 MonoInst *inst; \
650 EMIT_NEW_UNALU ((cfg), (inst), (op), (dr), (sr1)); \
651 } while (0)
653 #define MONO_EMIT_NEW_BIALU(cfg,op,dr,sr1,sr2) do { \
654 MonoInst *inst; \
655 MONO_INST_NEW ((cfg), (inst), (op)); \
656 inst->dreg = dr; \
657 inst->sreg1 = sr1; \
658 inst->sreg2 = sr2; \
659 MONO_ADD_INS (cfg->cbb, inst); \
660 } while (0)
662 #define MONO_EMIT_NEW_BIALU_IMM(cfg,op,dr,sr,imm) do { \
663 MonoInst *inst; \
664 MONO_INST_NEW ((cfg), (inst), (op)); \
665 inst->dreg = dr; \
666 inst->sreg1 = sr; \
667 inst->inst_imm = (mgreg_t)(imm); \
668 MONO_ADD_INS (cfg->cbb, inst); \
669 } while (0)
671 #define MONO_EMIT_NEW_COMPARE_IMM(cfg,sr1,imm) do { \
672 MonoInst *inst; \
673 MONO_INST_NEW ((cfg), (inst), (OP_COMPARE_IMM)); \
674 inst->sreg1 = sr1; \
675 inst->inst_imm = (imm); \
676 MONO_ADD_INS ((cfg)->cbb, inst); \
677 } while (0)
679 #define MONO_EMIT_NEW_ICOMPARE_IMM(cfg,sr1,imm) do { \
680 MonoInst *inst; \
681 MONO_INST_NEW ((cfg), (inst), sizeof (mgreg_t) == 8 ? OP_ICOMPARE_IMM : OP_COMPARE_IMM); \
682 inst->sreg1 = sr1; \
683 inst->inst_imm = (imm); \
684 MONO_ADD_INS ((cfg)->cbb, inst); \
685 } while (0)
687 /* This is used on 32 bit machines too when running with LLVM */
688 #define MONO_EMIT_NEW_LCOMPARE_IMM(cfg,sr1,imm) do { \
689 MonoInst *inst; \
690 MONO_INST_NEW ((cfg), (inst), (OP_LCOMPARE_IMM)); \
691 inst->sreg1 = sr1; \
692 if (SIZEOF_REGISTER == 4 && COMPILE_LLVM (cfg)) { \
693 guint64 _l = (imm); \
694 inst->inst_imm = _l & 0xffffffff; \
695 inst->inst_offset = _l >> 32; \
696 } else { \
697 inst->inst_imm = (imm); \
699 MONO_ADD_INS ((cfg)->cbb, inst); \
700 } while (0)
702 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg,op,dr,base,offset) do { \
703 MonoInst *inst; \
704 MONO_INST_NEW ((cfg), (inst), (op)); \
705 inst->dreg = dr; \
706 inst->inst_basereg = base; \
707 inst->inst_offset = offset; \
708 MONO_ADD_INS (cfg->cbb, inst); \
709 } while (0)
711 #define MONO_EMIT_NEW_LOAD_MEMBASE(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
713 #define MONO_EMIT_NEW_STORE_MEMBASE(cfg,op,base,offset,sr) do { \
714 MonoInst *inst; \
715 MONO_INST_NEW ((cfg), (inst), (op)); \
716 (inst)->sreg1 = sr; \
717 (inst)->inst_destbasereg = base; \
718 (inst)->inst_offset = offset; \
719 MONO_ADD_INS (cfg->cbb, inst); \
720 } while (0)
722 #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
723 MonoInst *inst; \
724 MONO_INST_NEW ((cfg), (inst), (op)); \
725 inst->inst_destbasereg = base; \
726 inst->inst_offset = offset; \
727 inst->inst_imm = (mgreg_t)(imm); \
728 MONO_ADD_INS ((cfg)->cbb, inst); \
729 } while (0)
731 #define MONO_EMIT_NEW_COND_EXC(cfg,cond,name) do { \
732 MonoInst *inst; \
733 MONO_INST_NEW ((cfg), (inst), (OP_COND_EXC_##cond)); \
734 inst->inst_p1 = (char*)name; \
735 MONO_ADD_INS ((cfg)->cbb, inst); \
736 } while (0)
738 /* Branch support */
741 * Basic blocks have two numeric identifiers:
742 * dfn: Depth First Number
743 * block_num: unique ID assigned at bblock creation
745 #define NEW_BBLOCK(cfg,bblock) do { \
746 (bblock) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
747 (bblock)->block_num = cfg->num_bblocks++; \
748 } while (0)
750 #define ADD_BBLOCK(cfg,b) do { \
751 if ((b)->cil_code) {\
752 cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b); \
754 (b)->real_offset = cfg->real_offset; \
755 } while (0)
757 /* Emit a one-way conditional branch */
759 * The inst_false_bb field of the cond branch will not be set, the JIT code should be
760 * prepared to deal with this.
762 #ifdef DEBUG_EXTENDED_BBLOCKS
763 static int ccount = 0;
764 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
765 MonoInst *ins; \
766 MonoBasicBlock *falsebb; \
767 MONO_INST_NEW ((cfg), (ins), (op)); \
768 if ((op) == OP_BR) { \
769 NEW_BBLOCK ((cfg), falsebb); \
770 ins->inst_target_bb = (truebb); \
771 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
772 MONO_ADD_INS ((cfg)->cbb, ins); \
773 MONO_START_BB ((cfg), falsebb); \
774 } else { \
775 ccount ++; \
776 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
777 ins->inst_true_bb = (truebb); \
778 ins->inst_false_bb = NULL; \
779 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
780 MONO_ADD_INS ((cfg)->cbb, ins); \
781 if (g_getenv ("COUNT2") && ccount == atoi (g_getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
782 if (g_getenv ("COUNT2") && ccount < atoi (g_getenv ("COUNT2"))) { \
783 cfg->cbb->extended = TRUE; \
784 } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
786 } while (0)
787 #else
788 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
789 MonoInst *ins; \
790 MonoBasicBlock *falsebb; \
791 MONO_INST_NEW ((cfg), (ins), (op)); \
792 if ((op) == OP_BR) { \
793 NEW_BBLOCK ((cfg), falsebb); \
794 ins->inst_target_bb = (truebb); \
795 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
796 MONO_ADD_INS ((cfg)->cbb, ins); \
797 MONO_START_BB ((cfg), falsebb); \
798 } else { \
799 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
800 ins->inst_true_bb = (truebb); \
801 ins->inst_false_bb = NULL; \
802 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
803 MONO_ADD_INS ((cfg)->cbb, ins); \
804 if (!cfg->enable_extended_bblocks) { \
805 NEW_BBLOCK ((cfg), falsebb); \
806 ins->inst_false_bb = falsebb; \
807 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
808 MONO_START_BB ((cfg), falsebb); \
809 } else { \
810 cfg->cbb->extended = TRUE; \
813 } while (0)
814 #endif
816 /* Emit a two-way conditional branch */
817 #define MONO_EMIT_NEW_BRANCH_BLOCK2(cfg,op,truebb,falsebb) do { \
818 MonoInst *ins; \
819 MONO_INST_NEW ((cfg), (ins), (op)); \
820 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
821 ins->inst_true_bb = (truebb); \
822 ins->inst_false_bb = (falsebb); \
823 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
824 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
825 MONO_ADD_INS ((cfg)->cbb, ins); \
826 } while (0)
828 #define MONO_START_BB(cfg, bblock) do { \
829 ADD_BBLOCK ((cfg), (bblock)); \
830 if (cfg->cbb->last_ins && MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins) && !cfg->cbb->last_ins->inst_false_bb) { \
831 cfg->cbb->last_ins->inst_false_bb = (bblock); \
832 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
833 } 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)))) \
834 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
835 (cfg)->cbb->next_bb = (bblock); \
836 (cfg)->cbb = (bblock); \
837 } while (0)
839 /* This marks a place in code where an implicit exception could be thrown */
840 #define MONO_EMIT_NEW_IMPLICIT_EXCEPTION(cfg) do { \
841 if (COMPILE_LLVM ((cfg))) { \
842 MONO_EMIT_NEW_UNALU (cfg, OP_IMPLICIT_EXCEPTION, -1, -1); \
844 } while (0)
846 /* Loads/Stores which can fault are handled correctly by the LLVM mono branch */
847 #define MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE(cfg) do { \
848 } while (0)
850 /* Emit an explicit null check which doesn't depend on SIGSEGV signal handling */
851 #define MONO_EMIT_NULL_CHECK(cfg, reg) do { \
852 if (cfg->explicit_null_checks) { \
853 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, (reg), 0); \
854 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException"); \
855 } else { \
856 MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE (cfg); \
858 } while (0)
860 #define MONO_EMIT_NEW_CHECK_THIS(cfg, sreg) do { \
861 cfg->flags |= MONO_CFG_HAS_CHECK_THIS; \
862 if (cfg->explicit_null_checks) { \
863 MONO_EMIT_NULL_CHECK (cfg, sreg); \
864 } else { \
865 MONO_EMIT_NEW_UNALU (cfg, OP_CHECK_THIS, -1, sreg); \
866 MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE (cfg); \
868 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, sreg); \
869 } while (0)
871 #define NEW_LOAD_MEMBASE_FLAGS(cfg,dest,op,dr,base,offset,ins_flags) do { \
872 int __ins_flags = ins_flags; \
873 if (__ins_flags & MONO_INST_FAULT) { \
874 MONO_EMIT_NULL_CHECK ((cfg), (base)); \
876 NEW_LOAD_MEMBASE ((cfg), (dest), (op), (dr), (base), (offset)); \
877 (dest)->flags = (__ins_flags); \
878 } while (0)
880 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS(cfg,op,dr,base,offset,ins_flags) do { \
881 MonoInst *inst; \
882 int __ins_flags = ins_flags; \
883 if (__ins_flags & MONO_INST_FAULT) { \
884 MONO_EMIT_NULL_CHECK ((cfg), (base)); \
886 NEW_LOAD_MEMBASE ((cfg), (inst), (op), (dr), (base), (offset)); \
887 inst->flags = (__ins_flags); \
888 MONO_ADD_INS (cfg->cbb, inst); \
889 } while (0)
891 #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))
893 /* A load which can cause a nullref */
894 #define NEW_LOAD_MEMBASE_FAULT(cfg,dest,op,dr,base,offset) NEW_LOAD_MEMBASE_FLAGS ((cfg), (dest), (op), (dr), (base), (offset), MONO_INST_FAULT)
896 #define EMIT_NEW_LOAD_MEMBASE_FAULT(cfg,dest,op,dr,base,offset) do { \
897 NEW_LOAD_MEMBASE_FAULT ((cfg), (dest), (op), (dr), (base), (offset)); \
898 MONO_ADD_INS ((cfg)->cbb, (dest)); \
899 } while (0)
901 #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)
903 #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))
905 #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)
907 #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)
909 #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))
911 /*Object Model related macros*/
913 /* Default bounds check implementation for most architectures + llvm */
914 #define MONO_EMIT_DEFAULT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg, fault) do { \
915 int _length_reg = alloc_ireg (cfg); \
916 if (fault) \
917 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset); \
918 else \
919 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset, MONO_INST_INVARIANT_LOAD); \
920 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, _length_reg, index_reg); \
921 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
922 } while (0)
924 #ifndef MONO_ARCH_EMIT_BOUNDS_CHECK
925 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) MONO_EMIT_DEFAULT_BOUNDS_CHECK ((cfg), (array_reg), (offset), (index_reg), TRUE)
926 #endif
928 /* cfg is the MonoCompile been used
929 * array_reg is the vreg holding the array object
930 * array_type is a struct (usually MonoArray or MonoString)
931 * array_length_field is the field in the previous struct with the length
932 * index_reg is the vreg holding the index
934 #define MONO_EMIT_BOUNDS_CHECK(cfg, array_reg, array_type, array_length_field, index_reg) do { \
935 if (!(cfg->opt & MONO_OPT_UNSAFE)) { \
936 if (!(cfg->opt & MONO_OPT_ABCREM)) { \
937 MONO_EMIT_NULL_CHECK (cfg, array_reg); \
938 if (COMPILE_LLVM (cfg)) \
939 MONO_EMIT_DEFAULT_BOUNDS_CHECK ((cfg), (array_reg), MONO_STRUCT_OFFSET (array_type, array_length_field), (index_reg), TRUE); \
940 else \
941 MONO_ARCH_EMIT_BOUNDS_CHECK ((cfg), (array_reg), MONO_STRUCT_OFFSET (array_type, array_length_field), (index_reg)); \
942 } else { \
943 MonoInst *ins; \
944 MONO_INST_NEW ((cfg), ins, OP_BOUNDS_CHECK); \
945 ins->sreg1 = array_reg; \
946 ins->sreg2 = index_reg; \
947 ins->inst_imm = MONO_STRUCT_OFFSET (array_type, array_length_field); \
948 ins->flags |= MONO_INST_FAULT; \
949 MONO_ADD_INS ((cfg)->cbb, ins); \
950 (cfg)->flags |= MONO_CFG_HAS_ARRAY_ACCESS; \
951 (cfg)->cbb->has_array_access = TRUE; \
954 } while (0)
956 G_END_DECLS
958 #endif