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_assert_not_reached ();
78 * FIXME: zeroing out some fields is not needed with the new IR, but the old
79 * JIT code still uses the left and right fields, so it has to stay.
81 #define MONO_INST_NEW(cfg,dest,op) do { \
82 (dest) = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoInst)); \
83 (dest)->inst_p0 = (dest)->inst_p1 = (dest)->next = (dest)->prev = NULL; \
84 (dest)->opcode = (op); \
87 (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1; \
88 (dest)->cil_code = (cfg)->ip; \
92 * Variants which take a dest argument and don't do an emit
94 #define NEW_ICONST(cfg,dest,val) do { \
95 MONO_INST_NEW ((cfg), (dest), OP_ICONST); \
96 (dest)->inst_c0 = (val); \
97 (dest)->type = STACK_I4; \
98 (dest)->dreg = alloc_dreg ((cfg), STACK_I4); \
101 #define NEW_PCONST(cfg,dest,val) do { \
102 MONO_INST_NEW ((cfg), (dest), OP_PCONST); \
103 (dest)->inst_p0 = (val); \
104 (dest)->type = STACK_PTR; \
105 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
108 #define NEW_I8CONST(cfg,dest,val) do { \
109 MONO_INST_NEW ((cfg), (dest), OP_I8CONST); \
110 (dest)->dreg = alloc_lreg ((cfg)); \
111 (dest)->type = STACK_I8; \
112 (dest)->inst_l = (val); \
115 #define NEW_STORE_MEMBASE(cfg,dest,op,base,offset,sr) do { \
116 MONO_INST_NEW ((cfg), (dest), (op)); \
117 (dest)->sreg1 = sr; \
118 (dest)->inst_destbasereg = base; \
119 (dest)->inst_offset = offset; \
122 #define NEW_LOAD_MEMBASE(cfg,dest,op,dr,base,offset) do { \
123 MONO_INST_NEW ((cfg), (dest), (op)); \
124 (dest)->dreg = (dr); \
125 (dest)->inst_basereg = (base); \
126 (dest)->inst_offset = (offset); \
127 (dest)->type = STACK_I4; \
130 #define NEW_LOAD_MEM(cfg,dest,op,dr,mem) do { \
131 MONO_INST_NEW ((cfg), (dest), (op)); \
132 (dest)->dreg = (dr); \
133 (dest)->inst_p0 = (gpointer)(gssize)(mem); \
134 (dest)->type = STACK_I4; \
137 #define NEW_UNALU(cfg,dest,op,dr,sr1) do { \
138 MONO_INST_NEW ((cfg), (dest), (op)); \
140 (dest)->sreg1 = sr1; \
143 #define NEW_BIALU(cfg,dest,op,dr,sr1,sr2) do { \
144 MONO_INST_NEW ((cfg), (dest), (op)); \
145 (dest)->dreg = (dr); \
146 (dest)->sreg1 = (sr1); \
147 (dest)->sreg2 = (sr2); \
150 #define NEW_BIALU_IMM(cfg,dest,op,dr,sr,imm) do { \
151 MONO_INST_NEW ((cfg), (dest), (op)); \
153 (dest)->sreg1 = sr; \
154 (dest)->inst_p1 = (gpointer)(gssize)(imm); \
157 #ifdef MONO_ARCH_NEED_GOT_VAR
159 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do { \
160 MONO_INST_NEW ((cfg), (dest), OP_PATCH_INFO); \
161 (dest)->inst_left = (gpointer)(el1); \
162 (dest)->inst_right = (gpointer)(el2); \
165 /* FIXME: Add the PUSH_GOT_ENTRY optimizations */
166 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
167 MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST); \
168 if (cfg->compile_aot) { \
169 MonoInst *group, *got_loc; \
170 got_loc = mono_get_got_var (cfg); \
171 NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
172 (dest)->inst_basereg = got_loc->dreg; \
173 (dest)->inst_p1 = group; \
175 (dest)->inst_p0 = (cons); \
176 (dest)->inst_i1 = (gpointer)(patch_type); \
178 (dest)->type = STACK_PTR; \
179 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
182 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { \
183 MonoInst *group, *got_loc; \
184 MONO_INST_NEW ((cfg), (dest), OP_GOT_ENTRY); \
185 got_loc = mono_get_got_var (cfg); \
186 NEW_PATCH_INFO ((cfg), group, NULL, patch_type); \
187 group->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
188 (dest)->inst_basereg = got_loc->dreg; \
189 (dest)->inst_p1 = group; \
190 (dest)->type = (stack_type); \
191 (dest)->klass = (stack_class); \
192 (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
197 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
198 MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
199 (dest)->inst_p0 = (cons); \
200 (dest)->inst_i1 = (gpointer)(patch_type); \
201 (dest)->type = STACK_PTR; \
202 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
205 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { \
206 MONO_INST_NEW ((cfg), (dest), OP_AOTCONST); \
207 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
208 (dest)->inst_p1 = (gpointer)(patch_type); \
209 (dest)->type = (stack_type); \
210 (dest)->klass = (stack_class); \
211 (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
216 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
218 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
220 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
222 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
224 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
226 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
228 #define NEW_LDSTRCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), STACK_OBJ, mono_defaults.string_class)
230 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_TYPE_FROM_HANDLE, (image), (token), STACK_OBJ, mono_defaults.monotype_class)
232 #define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), STACK_PTR, NULL)
234 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
235 if (cfg->compile_aot) { \
236 NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, STACK_OBJ, NULL); \
238 NEW_PCONST (cfg, args [0], (entry).blob); \
242 #define NEW_DOMAINCONST(cfg,dest) do { \
243 if (cfg->opt & MONO_OPT_SHARED) { \
244 /* avoid depending on undefined C behavior in sequence points */ \
245 MonoInst* __domain_var = mono_get_domainvar (cfg); \
246 NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
248 NEW_PCONST (cfg, dest, (cfg)->domain); \
252 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
254 #define NEW_VARLOAD(cfg,dest,var,vartype) do { \
255 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
256 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
257 type_to_eval_stack_type ((cfg), (vartype), (dest)); \
258 (dest)->klass = var->klass; \
259 (dest)->sreg1 = var->dreg; \
260 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
261 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
264 #ifdef MONO_ARCH_SOFT_FLOAT
265 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8 || (stack_type) == STACK_R8)
267 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8)
270 #define NEW_VARLOADA(cfg,dest,var,vartype) do { \
271 MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
272 (dest)->inst_p0 = (var); \
273 (var)->flags |= MONO_INST_INDIRECT; \
274 (dest)->type = STACK_MP; \
275 (dest)->klass = (var)->klass; \
276 (dest)->dreg = alloc_dreg ((cfg), STACK_MP); \
277 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; } \
280 #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do { \
281 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
282 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
283 (dest)->klass = (var)->klass; \
284 (dest)->sreg1 = (inst)->dreg; \
285 (dest)->dreg = (var)->dreg; \
286 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
289 #define NEW_TEMPLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype)
291 #define NEW_TEMPLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), cfg->varinfo [(num)], cfg->varinfo [(num)]->inst_vtype)
293 #define NEW_TEMPSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype, (inst))
295 #define NEW_ARGLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
297 #define NEW_LOCLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
299 #define NEW_LOCSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
301 #define NEW_ARGSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), arg_array [(num)], param_types [(num)], (inst))
303 #define NEW_LOCLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype)
305 #define NEW_RETLOADA(cfg,dest) do { \
306 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
307 (dest)->type = STACK_MP; \
308 (dest)->klass = cfg->ret->klass; \
309 (dest)->sreg1 = cfg->vret_addr->dreg; \
310 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
313 #define NEW_ARGLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), arg_array [(num)], param_types [(num)])
315 /* Promote the vreg to a variable so its address can be taken */
316 #define NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { \
317 MonoInst *var = get_vreg_to_inst ((cfg), (vreg)); \
319 var = mono_compile_create_var_for_vreg ((cfg), (ltype), OP_LOCAL, (vreg)); \
320 NEW_VARLOADA ((cfg), (dest), (var), (ltype)); \
323 #define NEW_DUMMY_USE(cfg,dest,var) do { \
324 MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
325 (dest)->sreg1 = var->dreg; \
328 /* Variants which take a type argument and handle vtypes as well */
329 #define NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { \
330 NEW_LOAD_MEMBASE ((cfg), (dest), mono_type_to_load_membase ((cfg), (ltype)), 0, (base), (offset)); \
331 type_to_eval_stack_type ((cfg), (ltype), (dest)); \
332 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
335 #define NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { \
336 MONO_INST_NEW ((cfg), (dest), mono_type_to_store_membase ((cfg), (ltype))); \
337 (dest)->sreg1 = sr; \
338 (dest)->inst_destbasereg = base; \
339 (dest)->inst_offset = offset; \
340 type_to_eval_stack_type ((cfg), (ltype), (dest)); \
341 (dest)->klass = mono_class_from_mono_type (ltype); \
345 * Variants which do an emit as well.
347 #define EMIT_NEW_ICONST(cfg,dest,val) do { NEW_ICONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
349 #define EMIT_NEW_PCONST(cfg,dest,val) do { NEW_PCONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
351 #define EMIT_NEW_I8CONST(cfg,dest,val) do { NEW_I8CONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
353 #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)
355 #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), (stack_type), (stack_class)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
357 #define EMIT_NEW_CLASSCONST(cfg,dest,val) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
359 #define EMIT_NEW_IMAGECONST(cfg,dest,val) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
361 #define EMIT_NEW_FIELDCONST(cfg,dest,val) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
363 #define EMIT_NEW_METHODCONST(cfg,dest,val) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
365 #define EMIT_NEW_VTABLECONST(cfg,dest,vtable) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
367 #define EMIT_NEW_SFLDACONST(cfg,dest,val) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
369 #define EMIT_NEW_LDSTRCONST(cfg,dest,image,token) EMIT_NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), STACK_OBJ, mono_defaults.string_class)
371 #define EMIT_NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) EMIT_NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_TYPE_FROM_HANDLE, (image), (token), STACK_OBJ, mono_defaults.monotype_class)
373 #define EMIT_NEW_LDTOKENCONST(cfg,dest,image,token) EMIT_NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), STACK_PTR, NULL)
375 #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
377 #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
379 #define EMIT_NEW_VARLOAD(cfg,dest,var,vartype) do { NEW_VARLOAD ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
381 #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)
383 #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
386 #ifdef MONO_ARCH_SOFT_FLOAT
389 * Since the IL stack (and our vregs) contain double values, we have to do a conversion
390 * when loading/storing args/locals of type R4.
393 #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
394 if (!(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
395 MonoInst *iargs [1]; \
396 EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
397 (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
399 EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
403 #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do { \
404 if (!(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
405 MonoInst *iargs [2]; \
406 iargs [0] = (inst); \
407 EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
408 mono_emit_jit_icall (cfg, mono_fstore_r4, iargs); \
410 EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
414 #define EMIT_NEW_ARGLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), arg_array [(num)], param_types [(num)])
416 #define EMIT_NEW_LOCLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
418 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
420 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), arg_array [(num)], param_types [(num)], (inst))
424 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { NEW_ARGLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
426 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { NEW_LOCLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
428 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { NEW_LOCSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
430 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
434 #define EMIT_NEW_TEMPLOAD(cfg,dest,num) do { NEW_TEMPLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
436 #define EMIT_NEW_TEMPLOADA(cfg,dest,num) do { NEW_TEMPLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
438 #define EMIT_NEW_LOCLOADA(cfg,dest,num) do { NEW_LOCLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
440 #define EMIT_NEW_ARGLOADA(cfg,dest,num) do { NEW_ARGLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
442 #define EMIT_NEW_RETLOADA(cfg,dest) do { NEW_RETLOADA ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
444 #define EMIT_NEW_TEMPSTORE(cfg,dest,num,inst) do { NEW_TEMPSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
446 #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)
448 #define EMIT_NEW_DUMMY_USE(cfg,dest,var) do { NEW_DUMMY_USE ((cfg), (dest), (var)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
450 #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)
452 #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)
454 #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)
456 #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)
458 #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)
460 #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)
462 #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)
465 * Variants which do not take an dest argument, but take a dreg argument.
467 #define MONO_EMIT_NEW_ICONST(cfg,dr,imm) do { \
469 MONO_INST_NEW ((cfg), (inst), OP_ICONST); \
471 inst->inst_c0 = imm; \
472 MONO_ADD_INS ((cfg)->cbb, inst); \
475 #define MONO_EMIT_NEW_PCONST(cfg,dr,val) do { \
477 MONO_INST_NEW ((cfg), (inst), OP_PCONST); \
479 (inst)->inst_p0 = (val); \
480 (inst)->type = STACK_PTR; \
481 MONO_ADD_INS ((cfg)->cbb, inst); \
484 #define MONO_EMIT_NEW_I8CONST(cfg,dr,imm) do { \
486 MONO_INST_NEW ((cfg), (inst), OP_I8CONST); \
488 inst->inst_l = imm; \
489 MONO_ADD_INS ((cfg)->cbb, inst); \
492 #ifdef MONO_ARCH_NEED_GOT_VAR
494 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
496 NEW_AOTCONST ((cfg), (inst), (patch_type), (cons)); \
498 MONO_ADD_INS ((cfg)->cbb, inst); \
503 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,type) do { \
505 MONO_INST_NEW ((cfg), (inst), OP_AOTCONST); \
507 inst->inst_p0 = imm; \
508 inst->inst_c1 = type; \
509 MONO_ADD_INS ((cfg)->cbb, inst); \
514 #define MONO_EMIT_NEW_CLASSCONST(cfg,dr,imm) MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,MONO_PATCH_INFO_CLASS)
515 #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)
517 #define MONO_EMIT_NEW_VZERO(cfg,dr,kl) do { \
519 MONO_INST_NEW ((cfg), (inst), OP_VZERO); \
521 (inst)->type = STACK_VTYPE; \
522 (inst)->klass = (kl); \
523 MONO_ADD_INS ((cfg)->cbb, inst); \
526 #define MONO_EMIT_NEW_UNALU(cfg,op,dr,sr1) do { \
528 EMIT_NEW_UNALU ((cfg), (inst), (op), (dr), (sr1)); \
531 #define MONO_EMIT_NEW_BIALU(cfg,op,dr,sr1,sr2) do { \
533 MONO_INST_NEW ((cfg), (inst), (op)); \
537 MONO_ADD_INS (cfg->cbb, inst); \
540 #define MONO_EMIT_NEW_BIALU_IMM(cfg,op,dr,sr,imm) do { \
542 MONO_INST_NEW ((cfg), (inst), (op)); \
545 inst->inst_p1 = (gpointer)(gssize)(imm); \
546 MONO_ADD_INS (cfg->cbb, inst); \
549 #define MONO_EMIT_NEW_COMPARE_IMM(cfg,sr1,imm) do { \
551 MONO_INST_NEW ((cfg), (inst), (OP_COMPARE_IMM)); \
553 inst->inst_p1 = (gpointer)imm; \
554 MONO_ADD_INS ((cfg)->cbb, inst); \
557 #define MONO_EMIT_NEW_ICOMPARE_IMM(cfg,sr1,imm) do { \
559 MONO_INST_NEW ((cfg), (inst), sizeof (void*) == 8 ? OP_ICOMPARE_IMM : OP_COMPARE_IMM); \
561 inst->inst_p1 = (gpointer)imm; \
562 MONO_ADD_INS ((cfg)->cbb, inst); \
565 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg,op,dr,base,offset) do { \
567 MONO_INST_NEW ((cfg), (inst), (op)); \
569 inst->inst_basereg = base; \
570 inst->inst_offset = offset; \
571 MONO_ADD_INS (cfg->cbb, inst); \
574 #define MONO_EMIT_NEW_LOAD_MEMBASE(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
576 #define MONO_EMIT_NEW_STORE_MEMBASE(cfg,op,base,offset,sr) do { \
578 MONO_INST_NEW ((cfg), (inst), (op)); \
579 (inst)->sreg1 = sr; \
580 (inst)->inst_destbasereg = base; \
581 (inst)->inst_offset = offset; \
582 MONO_ADD_INS (cfg->cbb, inst); \
585 #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
587 MONO_INST_NEW ((cfg), (inst), (op)); \
588 inst->inst_destbasereg = base; \
589 inst->inst_offset = offset; \
590 inst->inst_p1 = (gpointer)(gssize)imm; \
591 MONO_ADD_INS ((cfg)->cbb, inst); \
594 #define MONO_EMIT_NEW_COND_EXC(cfg,cond,name) do { \
596 MONO_INST_NEW ((cfg), (inst), (OP_COND_EXC_##cond)); \
597 inst->inst_p1 = (char*)name; \
598 MONO_ADD_INS ((cfg)->cbb, inst); \
604 * Basic blocks have two numeric identifiers:
605 * dfn: Depth First Number
606 * block_num: unique ID assigned at bblock creation
608 #define NEW_BBLOCK(cfg,bblock) do { \
609 (bblock) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
610 (bblock)->block_num = cfg->num_bblocks++; \
613 #define ADD_BBLOCK(cfg,b) do { \
614 if ((b)->cil_code) {\
615 cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b); \
617 (b)->real_offset = cfg->real_offset; \
620 /* Emit a one-way conditional branch */
622 * The inst_false_bb field of the cond branch will not be set, the JIT code should be
623 * prepared to deal with this.
625 #ifdef DEBUG_EXTENDED_BBLOCKS
626 static int ccount = 0;
627 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
629 MonoBasicBlock *falsebb; \
630 MONO_INST_NEW ((cfg), (ins), (op)); \
631 if ((op) == OP_BR) { \
632 NEW_BBLOCK ((cfg), falsebb); \
633 ins->inst_target_bb = (truebb); \
634 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
635 MONO_ADD_INS ((cfg)->cbb, ins); \
636 MONO_START_BB ((cfg), falsebb); \
639 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
640 ins->inst_true_bb = (truebb); \
641 ins->inst_false_bb = NULL; \
642 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
643 MONO_ADD_INS ((cfg)->cbb, ins); \
644 if (getenv ("COUNT2") && ccount == atoi (getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
645 if (getenv ("COUNT2") && ccount < atoi (getenv ("COUNT2"))) { \
646 cfg->cbb->extended = TRUE; \
647 } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
651 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
653 MonoBasicBlock *falsebb; \
654 MONO_INST_NEW ((cfg), (ins), (op)); \
655 if ((op) == OP_BR) { \
656 NEW_BBLOCK ((cfg), falsebb); \
657 ins->inst_target_bb = (truebb); \
658 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
659 MONO_ADD_INS ((cfg)->cbb, ins); \
660 MONO_START_BB ((cfg), falsebb); \
662 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
663 ins->inst_true_bb = (truebb); \
664 ins->inst_false_bb = NULL; \
665 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
666 MONO_ADD_INS ((cfg)->cbb, ins); \
667 if (!cfg->enable_extended_bblocks) { \
668 NEW_BBLOCK ((cfg), falsebb); \
669 ins->inst_false_bb = falsebb; \
670 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
671 MONO_START_BB ((cfg), falsebb); \
673 cfg->cbb->extended = TRUE; \
679 /* Emit a two-way conditional branch */
680 #define MONO_EMIT_NEW_BRANCH_BLOCK2(cfg,op,truebb,falsebb) do { \
682 MONO_INST_NEW ((cfg), (ins), (op)); \
683 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
684 ins->inst_true_bb = (truebb); \
685 ins->inst_false_bb = (falsebb); \
686 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
687 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
688 MONO_ADD_INS ((cfg)->cbb, ins); \
691 #define MONO_START_BB(cfg, bblock) do { \
692 ADD_BBLOCK ((cfg), (bblock)); \
693 if (cfg->cbb->last_ins && MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins) && !cfg->cbb->last_ins->inst_false_bb) { \
694 cfg->cbb->last_ins->inst_false_bb = (bblock); \
695 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
696 } 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)))) \
697 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
698 (cfg)->cbb->next_bb = (bblock); \
699 (cfg)->cbb = (bblock); \