2 * ir-emit.h: IR Creation/Emission Macros
5 * Zoltan Varga (vargaz@gmail.com)
7 * (C) 2002 Ximian, Inc.
10 #ifndef __MONO_IR_EMIT_H__
11 #define __MONO_IR_EMIT_H__
18 alloc_ireg (MonoCompile *cfg)
20 return cfg->next_vreg ++;
24 alloc_preg (MonoCompile *cfg)
26 return alloc_ireg (cfg);
30 alloc_lreg (MonoCompile *cfg)
32 #if SIZEOF_VOID_P == 8
33 return cfg->next_vreg ++;
35 /* Use a pair of consecutive vregs */
36 guint32 res = cfg->next_vreg;
45 alloc_freg (MonoCompile *cfg)
47 #ifdef MONO_ARCH_SOFT_FLOAT
48 /* Allocate an lvreg so float ops can be decomposed into long ops */
49 return alloc_lreg (cfg);
51 /* Allocate these from the same pool as the int regs */
52 return cfg->next_vreg ++;
57 alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
64 return alloc_ireg (cfg);
66 return alloc_freg (cfg);
68 return alloc_lreg (cfg);
70 return alloc_ireg (cfg);
72 g_warning ("Unknown stack type %x\n", stack_type);
73 g_assert_not_reached ();
79 * FIXME: zeroing out some fields is not needed with the new IR, but the old
80 * JIT code still uses the left and right fields, so it has to stay.
82 #define MONO_INST_NEW(cfg,dest,op) do { \
83 (dest) = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoInst)); \
84 (dest)->inst_p0 = (dest)->inst_p1 = (dest)->next = (dest)->prev = NULL; \
85 (dest)->opcode = (op); \
88 (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1; \
89 (dest)->cil_code = (cfg)->ip; \
93 * Variants which take a dest argument and don't do an emit
95 #define NEW_ICONST(cfg,dest,val) do { \
96 MONO_INST_NEW ((cfg), (dest), OP_ICONST); \
97 (dest)->inst_c0 = (val); \
98 (dest)->type = STACK_I4; \
99 (dest)->dreg = alloc_dreg ((cfg), STACK_I4); \
103 * Avoid using this with a non-NULL val if possible as it is not AOT
104 * compatible. Use one of the NEW_xxxCONST variants instead.
106 #define NEW_PCONST(cfg,dest,val) do { \
107 MONO_INST_NEW ((cfg), (dest), OP_PCONST); \
108 (dest)->inst_p0 = (val); \
109 (dest)->type = STACK_PTR; \
110 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
113 #define NEW_I8CONST(cfg,dest,val) do { \
114 MONO_INST_NEW ((cfg), (dest), OP_I8CONST); \
115 (dest)->dreg = alloc_lreg ((cfg)); \
116 (dest)->type = STACK_I8; \
117 (dest)->inst_l = (val); \
120 #define NEW_STORE_MEMBASE(cfg,dest,op,base,offset,sr) do { \
121 MONO_INST_NEW ((cfg), (dest), (op)); \
122 (dest)->sreg1 = sr; \
123 (dest)->inst_destbasereg = base; \
124 (dest)->inst_offset = offset; \
127 #define NEW_LOAD_MEMBASE(cfg,dest,op,dr,base,offset) do { \
128 MONO_INST_NEW ((cfg), (dest), (op)); \
129 (dest)->dreg = (dr); \
130 (dest)->inst_basereg = (base); \
131 (dest)->inst_offset = (offset); \
132 (dest)->type = STACK_I4; \
135 #define NEW_LOAD_MEM(cfg,dest,op,dr,mem) do { \
136 MONO_INST_NEW ((cfg), (dest), (op)); \
137 (dest)->dreg = (dr); \
138 (dest)->inst_p0 = (gpointer)(gssize)(mem); \
139 (dest)->type = STACK_I4; \
142 #define NEW_UNALU(cfg,dest,op,dr,sr1) do { \
143 MONO_INST_NEW ((cfg), (dest), (op)); \
145 (dest)->sreg1 = sr1; \
148 #define NEW_BIALU(cfg,dest,op,dr,sr1,sr2) do { \
149 MONO_INST_NEW ((cfg), (dest), (op)); \
150 (dest)->dreg = (dr); \
151 (dest)->sreg1 = (sr1); \
152 (dest)->sreg2 = (sr2); \
155 #define NEW_BIALU_IMM(cfg,dest,op,dr,sr,imm) do { \
156 MONO_INST_NEW ((cfg), (dest), (op)); \
158 (dest)->sreg1 = sr; \
159 (dest)->inst_p1 = (gpointer)(gssize)(imm); \
162 #ifdef MONO_ARCH_NEED_GOT_VAR
164 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do { \
165 MONO_INST_NEW ((cfg), (dest), OP_PATCH_INFO); \
166 (dest)->inst_left = (gpointer)(el1); \
167 (dest)->inst_right = (gpointer)(el2); \
170 /* FIXME: Add the PUSH_GOT_ENTRY optimizations */
171 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
172 MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST); \
173 if (cfg->compile_aot) { \
174 MonoInst *group, *got_loc; \
175 got_loc = mono_get_got_var (cfg); \
176 NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
177 (dest)->inst_basereg = got_loc->dreg; \
178 (dest)->inst_p1 = group; \
180 (dest)->inst_p0 = (cons); \
181 (dest)->inst_i1 = (gpointer)(patch_type); \
183 (dest)->type = STACK_PTR; \
184 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
187 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
188 MonoInst *group, *got_loc; \
189 MONO_INST_NEW ((cfg), (dest), OP_GOT_ENTRY); \
190 got_loc = mono_get_got_var (cfg); \
191 NEW_PATCH_INFO ((cfg), group, NULL, patch_type); \
192 group->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
193 (dest)->inst_basereg = got_loc->dreg; \
194 (dest)->inst_p1 = group; \
195 (dest)->type = (stack_type); \
196 (dest)->klass = (stack_class); \
197 (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
202 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
203 MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
204 (dest)->inst_p0 = (cons); \
205 (dest)->inst_i1 = (gpointer)(patch_type); \
206 (dest)->type = STACK_PTR; \
207 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
210 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
211 MONO_INST_NEW ((cfg), (dest), OP_AOTCONST); \
212 (dest)->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
213 (dest)->inst_p1 = (gpointer)(patch_type); \
214 (dest)->type = (stack_type); \
215 (dest)->klass = (stack_class); \
216 (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
221 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
223 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
225 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
227 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
229 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
231 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
233 #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)
235 #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)
237 #define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), NULL, STACK_PTR, NULL)
239 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
240 if (cfg->compile_aot) { \
241 NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, NULL, STACK_OBJ, NULL); \
243 NEW_PCONST (cfg, args [0], (entry).blob); \
247 #define NEW_METHOD_RGCTX_CONST(cfg,dest,method) do { \
248 if (cfg->compile_aot) { \
249 NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHOD_RGCTX, (method)); \
251 MonoMethodRuntimeGenericContext *mrgctx; \
252 mrgctx = mono_method_lookup_rgctx (mono_class_vtable ((cfg)->domain, (method)->klass), mini_method_get_context ((method))->method_inst); \
253 NEW_PCONST ((cfg), (dest), (mrgctx)); \
257 #define NEW_DOMAINCONST(cfg,dest) do { \
258 if ((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) { \
259 /* avoid depending on undefined C behavior in sequence points */ \
260 MonoInst* __domain_var = mono_get_domainvar (cfg); \
261 NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
263 NEW_PCONST (cfg, dest, (cfg)->domain); \
267 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
269 #define NEW_VARLOAD(cfg,dest,var,vartype) do { \
270 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
271 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
272 type_to_eval_stack_type ((cfg), (vartype), (dest)); \
273 (dest)->klass = var->klass; \
274 (dest)->sreg1 = var->dreg; \
275 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
276 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
279 #ifdef MONO_ARCH_SOFT_FLOAT
280 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8 || (stack_type) == STACK_R8)
282 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8)
285 #define NEW_VARLOADA(cfg,dest,var,vartype) do { \
286 MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
287 (dest)->inst_p0 = (var); \
288 (var)->flags |= MONO_INST_INDIRECT; \
289 (dest)->type = STACK_MP; \
290 (dest)->klass = (var)->klass; \
291 (dest)->dreg = alloc_dreg ((cfg), STACK_MP); \
292 if (SIZEOF_VOID_P == 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; } \
295 #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do { \
296 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
297 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
298 (dest)->klass = (var)->klass; \
299 (dest)->sreg1 = (inst)->dreg; \
300 (dest)->dreg = (var)->dreg; \
301 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
304 #define NEW_TEMPLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype)
306 #define NEW_TEMPLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), cfg->varinfo [(num)], cfg->varinfo [(num)]->inst_vtype)
308 #define NEW_TEMPSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype, (inst))
310 #define NEW_ARGLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
312 #define NEW_LOCLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
314 #define NEW_LOCSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
316 #define NEW_ARGSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
318 #define NEW_LOCLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype)
320 #define NEW_RETLOADA(cfg,dest) do { \
321 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
322 (dest)->type = STACK_MP; \
323 (dest)->klass = cfg->ret->klass; \
324 (dest)->sreg1 = cfg->vret_addr->dreg; \
325 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
328 #define NEW_ARGLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), arg_array [(num)], param_types [(num)])
330 /* Promote the vreg to a variable so its address can be taken */
331 #define NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { \
332 MonoInst *var = get_vreg_to_inst ((cfg), (vreg)); \
334 var = mono_compile_create_var_for_vreg ((cfg), (ltype), OP_LOCAL, (vreg)); \
335 NEW_VARLOADA ((cfg), (dest), (var), (ltype)); \
338 #define NEW_DUMMY_USE(cfg,dest,var) do { \
339 MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
340 (dest)->sreg1 = var->dreg; \
343 /* Variants which take a type argument and handle vtypes as well */
344 #define NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { \
345 NEW_LOAD_MEMBASE ((cfg), (dest), mono_type_to_load_membase ((cfg), (ltype)), 0, (base), (offset)); \
346 type_to_eval_stack_type ((cfg), (ltype), (dest)); \
347 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
350 #define NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { \
351 MONO_INST_NEW ((cfg), (dest), mono_type_to_store_membase ((cfg), (ltype))); \
352 (dest)->sreg1 = sr; \
353 (dest)->inst_destbasereg = base; \
354 (dest)->inst_offset = offset; \
355 type_to_eval_stack_type ((cfg), (ltype), (dest)); \
356 (dest)->klass = mono_class_from_mono_type (ltype); \
360 * Variants which do an emit as well.
362 #define EMIT_NEW_ICONST(cfg,dest,val) do { NEW_ICONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
364 #define EMIT_NEW_PCONST(cfg,dest,val) do { NEW_PCONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
366 #define EMIT_NEW_I8CONST(cfg,dest,val) do { NEW_I8CONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
368 #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)
370 #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)
372 #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)
374 #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)
376 #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)
378 #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)
380 #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)
382 #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)
384 #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)
386 #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)
388 #define EMIT_NEW_LDTOKENCONST(cfg,dest,image,token) do { NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), NULL, STACK_PTR, NULL); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
390 #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
392 #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
394 #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)
396 #define EMIT_NEW_VARLOAD(cfg,dest,var,vartype) do { NEW_VARLOAD ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
398 #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)
400 #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
403 #ifdef MONO_ARCH_SOFT_FLOAT
406 * Since the IL stack (and our vregs) contain double values, we have to do a conversion
407 * when loading/storing args/locals of type R4.
410 #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
411 if (!(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
412 MonoInst *iargs [1]; \
413 EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
414 (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
416 EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
420 #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do { \
421 if (!(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
422 MonoInst *iargs [2]; \
423 iargs [0] = (inst); \
424 EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
425 mono_emit_jit_icall (cfg, mono_fstore_r4, iargs); \
427 EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
431 #define EMIT_NEW_ARGLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
433 #define EMIT_NEW_LOCLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
435 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
437 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
441 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { NEW_ARGLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
443 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { NEW_LOCLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
445 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { NEW_LOCSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
447 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
451 #define EMIT_NEW_TEMPLOAD(cfg,dest,num) do { NEW_TEMPLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
453 #define EMIT_NEW_TEMPLOADA(cfg,dest,num) do { NEW_TEMPLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
455 #define EMIT_NEW_LOCLOADA(cfg,dest,num) do { NEW_LOCLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
457 #define EMIT_NEW_ARGLOADA(cfg,dest,num) do { NEW_ARGLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
459 #define EMIT_NEW_RETLOADA(cfg,dest) do { NEW_RETLOADA ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
461 #define EMIT_NEW_TEMPSTORE(cfg,dest,num,inst) do { NEW_TEMPSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
463 #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)
465 #define EMIT_NEW_DUMMY_USE(cfg,dest,var) do { NEW_DUMMY_USE ((cfg), (dest), (var)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
467 #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)
469 #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)
471 #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)
473 #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)
475 #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)
477 #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)
479 #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)
482 * Variants which do not take an dest argument, but take a dreg argument.
484 #define MONO_EMIT_NEW_ICONST(cfg,dr,imm) do { \
486 MONO_INST_NEW ((cfg), (inst), OP_ICONST); \
488 inst->inst_c0 = imm; \
489 MONO_ADD_INS ((cfg)->cbb, inst); \
492 #define MONO_EMIT_NEW_PCONST(cfg,dr,val) do { \
494 MONO_INST_NEW ((cfg), (inst), OP_PCONST); \
496 (inst)->inst_p0 = (val); \
497 (inst)->type = STACK_PTR; \
498 MONO_ADD_INS ((cfg)->cbb, inst); \
501 #define MONO_EMIT_NEW_I8CONST(cfg,dr,imm) do { \
503 MONO_INST_NEW ((cfg), (inst), OP_I8CONST); \
505 inst->inst_l = imm; \
506 MONO_ADD_INS ((cfg)->cbb, inst); \
509 #ifdef MONO_ARCH_NEED_GOT_VAR
511 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
513 NEW_AOTCONST ((cfg), (inst), (patch_type), (cons)); \
515 MONO_ADD_INS ((cfg)->cbb, inst); \
520 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,type) do { \
522 MONO_INST_NEW ((cfg), (inst), OP_AOTCONST); \
524 inst->inst_p0 = imm; \
525 inst->inst_c1 = type; \
526 MONO_ADD_INS ((cfg)->cbb, inst); \
531 #define MONO_EMIT_NEW_CLASSCONST(cfg,dr,imm) MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,MONO_PATCH_INFO_CLASS)
532 #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)
534 #define MONO_EMIT_NEW_VZERO(cfg,dr,kl) do { \
536 MONO_INST_NEW ((cfg), (inst), MONO_CLASS_IS_SIMD (cfg, kl) ? OP_XZERO : OP_VZERO); \
538 (inst)->type = STACK_VTYPE; \
539 (inst)->klass = (kl); \
540 MONO_ADD_INS ((cfg)->cbb, inst); \
543 #define MONO_EMIT_NEW_UNALU(cfg,op,dr,sr1) do { \
545 EMIT_NEW_UNALU ((cfg), (inst), (op), (dr), (sr1)); \
548 #define MONO_EMIT_NEW_BIALU(cfg,op,dr,sr1,sr2) do { \
550 MONO_INST_NEW ((cfg), (inst), (op)); \
554 MONO_ADD_INS (cfg->cbb, inst); \
557 #define MONO_EMIT_NEW_BIALU_IMM(cfg,op,dr,sr,imm) do { \
559 MONO_INST_NEW ((cfg), (inst), (op)); \
562 inst->inst_p1 = (gpointer)(gssize)(imm); \
563 MONO_ADD_INS (cfg->cbb, inst); \
566 #define MONO_EMIT_NEW_COMPARE_IMM(cfg,sr1,imm) do { \
568 MONO_INST_NEW ((cfg), (inst), (OP_COMPARE_IMM)); \
570 inst->inst_p1 = (gpointer)imm; \
571 MONO_ADD_INS ((cfg)->cbb, inst); \
574 #define MONO_EMIT_NEW_ICOMPARE_IMM(cfg,sr1,imm) do { \
576 MONO_INST_NEW ((cfg), (inst), sizeof (void*) == 8 ? OP_ICOMPARE_IMM : OP_COMPARE_IMM); \
578 inst->inst_p1 = (gpointer)imm; \
579 MONO_ADD_INS ((cfg)->cbb, inst); \
582 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg,op,dr,base,offset) do { \
584 MONO_INST_NEW ((cfg), (inst), (op)); \
586 inst->inst_basereg = base; \
587 inst->inst_offset = offset; \
588 MONO_ADD_INS (cfg->cbb, inst); \
591 #define MONO_EMIT_NEW_LOAD_MEMBASE(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
593 #define MONO_EMIT_NEW_STORE_MEMBASE(cfg,op,base,offset,sr) do { \
595 MONO_INST_NEW ((cfg), (inst), (op)); \
596 (inst)->sreg1 = sr; \
597 (inst)->inst_destbasereg = base; \
598 (inst)->inst_offset = offset; \
599 MONO_ADD_INS (cfg->cbb, inst); \
602 #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
604 MONO_INST_NEW ((cfg), (inst), (op)); \
605 inst->inst_destbasereg = base; \
606 inst->inst_offset = offset; \
607 inst->inst_p1 = (gpointer)(gssize)imm; \
608 MONO_ADD_INS ((cfg)->cbb, inst); \
611 #define MONO_EMIT_NEW_COND_EXC(cfg,cond,name) do { \
613 MONO_INST_NEW ((cfg), (inst), (OP_COND_EXC_##cond)); \
614 inst->inst_p1 = (char*)name; \
615 MONO_ADD_INS ((cfg)->cbb, inst); \
621 * Basic blocks have two numeric identifiers:
622 * dfn: Depth First Number
623 * block_num: unique ID assigned at bblock creation
625 #define NEW_BBLOCK(cfg,bblock) do { \
626 (bblock) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
627 (bblock)->block_num = cfg->num_bblocks++; \
630 #define ADD_BBLOCK(cfg,b) do { \
631 if ((b)->cil_code) {\
632 cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b); \
634 (b)->real_offset = cfg->real_offset; \
637 /* Emit a one-way conditional branch */
639 * The inst_false_bb field of the cond branch will not be set, the JIT code should be
640 * prepared to deal with this.
642 #ifdef DEBUG_EXTENDED_BBLOCKS
643 static int ccount = 0;
644 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
646 MonoBasicBlock *falsebb; \
647 MONO_INST_NEW ((cfg), (ins), (op)); \
648 if ((op) == OP_BR) { \
649 NEW_BBLOCK ((cfg), falsebb); \
650 ins->inst_target_bb = (truebb); \
651 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
652 MONO_ADD_INS ((cfg)->cbb, ins); \
653 MONO_START_BB ((cfg), falsebb); \
656 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
657 ins->inst_true_bb = (truebb); \
658 ins->inst_false_bb = NULL; \
659 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
660 MONO_ADD_INS ((cfg)->cbb, ins); \
661 if (getenv ("COUNT2") && ccount == atoi (getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
662 if (getenv ("COUNT2") && ccount < atoi (getenv ("COUNT2"))) { \
663 cfg->cbb->extended = TRUE; \
664 } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
668 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
670 MonoBasicBlock *falsebb; \
671 MONO_INST_NEW ((cfg), (ins), (op)); \
672 if ((op) == OP_BR) { \
673 NEW_BBLOCK ((cfg), falsebb); \
674 ins->inst_target_bb = (truebb); \
675 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
676 MONO_ADD_INS ((cfg)->cbb, ins); \
677 MONO_START_BB ((cfg), falsebb); \
679 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
680 ins->inst_true_bb = (truebb); \
681 ins->inst_false_bb = NULL; \
682 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
683 MONO_ADD_INS ((cfg)->cbb, ins); \
684 if (!cfg->enable_extended_bblocks) { \
685 NEW_BBLOCK ((cfg), falsebb); \
686 ins->inst_false_bb = falsebb; \
687 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
688 MONO_START_BB ((cfg), falsebb); \
690 cfg->cbb->extended = TRUE; \
696 /* Emit a two-way conditional branch */
697 #define MONO_EMIT_NEW_BRANCH_BLOCK2(cfg,op,truebb,falsebb) do { \
699 MONO_INST_NEW ((cfg), (ins), (op)); \
700 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
701 ins->inst_true_bb = (truebb); \
702 ins->inst_false_bb = (falsebb); \
703 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
704 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
705 MONO_ADD_INS ((cfg)->cbb, ins); \
708 #define MONO_START_BB(cfg, bblock) do { \
709 ADD_BBLOCK ((cfg), (bblock)); \
710 if (cfg->cbb->last_ins && MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins) && !cfg->cbb->last_ins->inst_false_bb) { \
711 cfg->cbb->last_ins->inst_false_bb = (bblock); \
712 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
713 } 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)))) \
714 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
715 (cfg)->cbb->next_bb = (bblock); \
716 (cfg)->cbb = (bblock); \
719 /*Object Model related macros*/
721 #ifndef MONO_ARCH_EMIT_BOUNDS_CHECK
722 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) do { \
723 int _length_reg = alloc_ireg (cfg); \
724 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset); \
725 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, _length_reg, index_reg); \
726 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
730 /* cfg is the MonoCompile been used
731 * array_reg is the vreg holding the array object
732 * array_type is a struct (usually MonoArray or MonoString)
733 * array_length_field is the field in the previous struct with the length
734 * index_reg is the vreg holding the index
736 #define MONO_EMIT_BOUNDS_CHECK(cfg, array_reg, array_type, array_length_field, index_reg) do { \
737 if (!(cfg->opt & MONO_OPT_ABCREM)) { \
738 MONO_ARCH_EMIT_BOUNDS_CHECK ((cfg), (array_reg), G_STRUCT_OFFSET (array_type, array_length_field), (index_reg)); \
741 MONO_INST_NEW ((cfg), ins, OP_BOUNDS_CHECK); \
742 ins->sreg1 = array_reg; \
743 ins->sreg2 = index_reg; \
744 ins->inst_imm = G_STRUCT_OFFSET (array_type, array_length_field); \
745 MONO_ADD_INS ((cfg)->cbb, ins); \
746 (cfg)->flags |= MONO_CFG_HAS_ARRAY_ACCESS; \
747 (cfg)->cbb->has_array_access = TRUE; \