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_REGISTER == 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_c0 = (dest)->inst_c1 = 0; \
85 (dest)->next = (dest)->prev = NULL; \
86 (dest)->opcode = (op); \
90 MONO_INST_NULLIFY_SREGS ((dest)); \
91 (dest)->cil_code = (cfg)->ip; \
95 * Variants which take a dest argument and don't do an emit
97 #define NEW_ICONST(cfg,dest,val) do { \
98 MONO_INST_NEW ((cfg), (dest), OP_ICONST); \
99 (dest)->inst_c0 = (val); \
100 (dest)->type = STACK_I4; \
101 (dest)->dreg = alloc_dreg ((cfg), STACK_I4); \
105 * Avoid using this with a non-NULL val if possible as it is not AOT
106 * compatible. Use one of the NEW_xxxCONST variants instead.
108 #define NEW_PCONST(cfg,dest,val) do { \
109 MONO_INST_NEW ((cfg), (dest), OP_PCONST); \
110 (dest)->inst_p0 = (val); \
111 (dest)->type = STACK_PTR; \
112 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
115 #define NEW_I8CONST(cfg,dest,val) do { \
116 MONO_INST_NEW ((cfg), (dest), OP_I8CONST); \
117 (dest)->dreg = alloc_lreg ((cfg)); \
118 (dest)->type = STACK_I8; \
119 (dest)->inst_l = (val); \
122 #define NEW_STORE_MEMBASE(cfg,dest,op,base,offset,sr) do { \
123 MONO_INST_NEW ((cfg), (dest), (op)); \
124 (dest)->sreg1 = sr; \
125 (dest)->inst_destbasereg = base; \
126 (dest)->inst_offset = offset; \
129 #define NEW_LOAD_MEMBASE(cfg,dest,op,dr,base,offset) do { \
130 MONO_INST_NEW ((cfg), (dest), (op)); \
131 (dest)->dreg = (dr); \
132 (dest)->inst_basereg = (base); \
133 (dest)->inst_offset = (offset); \
134 (dest)->type = STACK_I4; \
137 #define NEW_LOAD_MEM(cfg,dest,op,dr,mem) do { \
138 MONO_INST_NEW ((cfg), (dest), (op)); \
139 (dest)->dreg = (dr); \
140 (dest)->inst_p0 = (gpointer)(gssize)(mem); \
141 (dest)->type = STACK_I4; \
144 #define NEW_UNALU(cfg,dest,op,dr,sr1) do { \
145 MONO_INST_NEW ((cfg), (dest), (op)); \
147 (dest)->sreg1 = sr1; \
150 #define NEW_BIALU(cfg,dest,op,dr,sr1,sr2) do { \
151 MONO_INST_NEW ((cfg), (dest), (op)); \
152 (dest)->dreg = (dr); \
153 (dest)->sreg1 = (sr1); \
154 (dest)->sreg2 = (sr2); \
157 #define NEW_BIALU_IMM(cfg,dest,op,dr,sr,imm) do { \
158 MONO_INST_NEW ((cfg), (dest), (op)); \
160 (dest)->sreg1 = sr; \
161 (dest)->inst_p1 = (gpointer)(gssize)(imm); \
164 #ifdef MONO_ARCH_NEED_GOT_VAR
166 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do { \
167 MONO_INST_NEW ((cfg), (dest), OP_PATCH_INFO); \
168 (dest)->inst_left = (gpointer)(el1); \
169 (dest)->inst_right = (gpointer)(el2); \
172 /* FIXME: Add the PUSH_GOT_ENTRY optimizations */
173 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
174 MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST); \
175 if (cfg->compile_aot) { \
176 MonoInst *group, *got_loc; \
177 got_loc = mono_get_got_var (cfg); \
178 NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
179 (dest)->inst_basereg = got_loc->dreg; \
180 (dest)->inst_p1 = group; \
182 (dest)->inst_p0 = (cons); \
183 (dest)->inst_i1 = (gpointer)(patch_type); \
185 (dest)->type = STACK_PTR; \
186 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
189 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
190 MonoInst *group, *got_loc; \
191 MONO_INST_NEW ((cfg), (dest), OP_GOT_ENTRY); \
192 got_loc = mono_get_got_var (cfg); \
193 NEW_PATCH_INFO ((cfg), group, NULL, patch_type); \
194 group->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
195 (dest)->inst_basereg = got_loc->dreg; \
196 (dest)->inst_p1 = group; \
197 (dest)->type = (stack_type); \
198 (dest)->klass = (stack_class); \
199 (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
204 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
205 MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
206 (dest)->inst_p0 = (cons); \
207 (dest)->inst_i1 = (gpointer)(patch_type); \
208 (dest)->type = STACK_PTR; \
209 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
212 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
213 MONO_INST_NEW ((cfg), (dest), OP_AOTCONST); \
214 (dest)->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
215 (dest)->inst_p1 = (gpointer)(patch_type); \
216 (dest)->type = (stack_type); \
217 (dest)->klass = (stack_class); \
218 (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
223 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
225 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
227 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
229 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
231 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
233 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
235 #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)
237 #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)
239 #define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), NULL, STACK_PTR, NULL)
241 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
242 if (cfg->compile_aot) { \
243 NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, NULL, STACK_OBJ, NULL); \
245 NEW_PCONST (cfg, args [0], (entry).blob); \
249 #define NEW_METHOD_RGCTX_CONST(cfg,dest,method) do { \
250 if (cfg->compile_aot) { \
251 NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHOD_RGCTX, (method)); \
253 MonoMethodRuntimeGenericContext *mrgctx; \
254 mrgctx = mono_method_lookup_rgctx (mono_class_vtable ((cfg)->domain, (method)->klass), mini_method_get_context ((method))->method_inst); \
255 NEW_PCONST ((cfg), (dest), (mrgctx)); \
259 #define NEW_DOMAINCONST(cfg,dest) do { \
260 if ((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) { \
261 /* avoid depending on undefined C behavior in sequence points */ \
262 MonoInst* __domain_var = mono_get_domainvar (cfg); \
263 NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
265 NEW_PCONST (cfg, dest, (cfg)->domain); \
269 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
271 #define NEW_VARLOAD(cfg,dest,var,vartype) do { \
272 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
273 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
274 type_to_eval_stack_type ((cfg), (vartype), (dest)); \
275 (dest)->klass = var->klass; \
276 (dest)->sreg1 = var->dreg; \
277 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
278 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
281 #ifdef MONO_ARCH_SOFT_FLOAT
282 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8 || (stack_type) == STACK_R8)
284 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8)
287 #define NEW_VARLOADA(cfg,dest,var,vartype) do { \
288 MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
289 (dest)->inst_p0 = (var); \
290 (var)->flags |= MONO_INST_INDIRECT; \
291 (dest)->type = STACK_MP; \
292 (dest)->klass = (var)->klass; \
293 (dest)->dreg = alloc_dreg ((cfg), STACK_MP); \
294 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; } \
297 #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do { \
298 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
299 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
300 (dest)->klass = (var)->klass; \
301 (dest)->sreg1 = (inst)->dreg; \
302 (dest)->dreg = (var)->dreg; \
303 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
306 #define NEW_TEMPLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype)
308 #define NEW_TEMPLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), cfg->varinfo [(num)], cfg->varinfo [(num)]->inst_vtype)
310 #define NEW_TEMPSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype, (inst))
312 #define NEW_ARGLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
314 #define NEW_LOCLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
316 #define NEW_LOCSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
318 #define NEW_ARGSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
320 #define NEW_LOCLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype)
322 #define NEW_RETLOADA(cfg,dest) do { \
323 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
324 (dest)->type = STACK_MP; \
325 (dest)->klass = cfg->ret->klass; \
326 (dest)->sreg1 = cfg->vret_addr->dreg; \
327 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
330 #define NEW_ARGLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), arg_array [(num)], param_types [(num)])
332 /* Promote the vreg to a variable so its address can be taken */
333 #define NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { \
334 MonoInst *var = get_vreg_to_inst ((cfg), (vreg)); \
336 var = mono_compile_create_var_for_vreg ((cfg), (ltype), OP_LOCAL, (vreg)); \
337 NEW_VARLOADA ((cfg), (dest), (var), (ltype)); \
340 #define NEW_DUMMY_USE(cfg,dest,var) do { \
341 MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
342 (dest)->sreg1 = var->dreg; \
345 /* Variants which take a type argument and handle vtypes as well */
346 #define NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { \
347 NEW_LOAD_MEMBASE ((cfg), (dest), mono_type_to_load_membase ((cfg), (ltype)), 0, (base), (offset)); \
348 type_to_eval_stack_type ((cfg), (ltype), (dest)); \
349 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
352 #define NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { \
353 MONO_INST_NEW ((cfg), (dest), mono_type_to_store_membase ((cfg), (ltype))); \
354 (dest)->sreg1 = sr; \
355 (dest)->inst_destbasereg = base; \
356 (dest)->inst_offset = offset; \
357 type_to_eval_stack_type ((cfg), (ltype), (dest)); \
358 (dest)->klass = mono_class_from_mono_type (ltype); \
362 * Variants which do an emit as well.
364 #define EMIT_NEW_ICONST(cfg,dest,val) do { NEW_ICONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
366 #define EMIT_NEW_PCONST(cfg,dest,val) do { NEW_PCONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
368 #define EMIT_NEW_I8CONST(cfg,dest,val) do { NEW_I8CONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
370 #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)
372 #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)
374 #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)
376 #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)
378 #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)
380 #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)
382 #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)
384 #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)
386 #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)
388 #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)
390 #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)
392 #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
394 #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
396 #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)
398 #define EMIT_NEW_VARLOAD(cfg,dest,var,vartype) do { NEW_VARLOAD ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
400 #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)
402 #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
405 #ifdef MONO_ARCH_SOFT_FLOAT
408 * Since the IL stack (and our vregs) contain double values, we have to do a conversion
409 * when loading/storing args/locals of type R4.
412 #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
413 if (!(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
414 MonoInst *iargs [1]; \
415 EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
416 (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
418 EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
422 #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do { \
423 if (!(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
424 MonoInst *iargs [2]; \
425 iargs [0] = (inst); \
426 EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
427 mono_emit_jit_icall (cfg, mono_fstore_r4, iargs); \
429 EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
433 #define EMIT_NEW_ARGLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
435 #define EMIT_NEW_LOCLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
437 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
439 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
443 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { NEW_ARGLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
445 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { NEW_LOCLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
447 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { NEW_LOCSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
449 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
453 #define EMIT_NEW_TEMPLOAD(cfg,dest,num) do { NEW_TEMPLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
455 #define EMIT_NEW_TEMPLOADA(cfg,dest,num) do { NEW_TEMPLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
457 #define EMIT_NEW_LOCLOADA(cfg,dest,num) do { NEW_LOCLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
459 #define EMIT_NEW_ARGLOADA(cfg,dest,num) do { NEW_ARGLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
461 #define EMIT_NEW_RETLOADA(cfg,dest) do { NEW_RETLOADA ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
463 #define EMIT_NEW_TEMPSTORE(cfg,dest,num,inst) do { NEW_TEMPSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
465 #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)
467 #define EMIT_NEW_DUMMY_USE(cfg,dest,var) do { NEW_DUMMY_USE ((cfg), (dest), (var)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
469 #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)
471 #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)
473 #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)
475 #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)
477 #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)
479 #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)
481 #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)
484 * Variants which do not take an dest argument, but take a dreg argument.
486 #define MONO_EMIT_NEW_ICONST(cfg,dr,imm) do { \
488 MONO_INST_NEW ((cfg), (inst), OP_ICONST); \
490 inst->inst_c0 = imm; \
491 MONO_ADD_INS ((cfg)->cbb, inst); \
494 #define MONO_EMIT_NEW_PCONST(cfg,dr,val) do { \
496 MONO_INST_NEW ((cfg), (inst), OP_PCONST); \
498 (inst)->inst_p0 = (val); \
499 (inst)->type = STACK_PTR; \
500 MONO_ADD_INS ((cfg)->cbb, inst); \
503 #define MONO_EMIT_NEW_I8CONST(cfg,dr,imm) do { \
505 MONO_INST_NEW ((cfg), (inst), OP_I8CONST); \
507 inst->inst_l = imm; \
508 MONO_ADD_INS ((cfg)->cbb, inst); \
511 #ifdef MONO_ARCH_NEED_GOT_VAR
513 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
515 NEW_AOTCONST ((cfg), (inst), (patch_type), (cons)); \
517 MONO_ADD_INS ((cfg)->cbb, inst); \
522 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,type) do { \
524 MONO_INST_NEW ((cfg), (inst), OP_AOTCONST); \
526 inst->inst_p0 = imm; \
527 inst->inst_c1 = type; \
528 MONO_ADD_INS ((cfg)->cbb, inst); \
533 #define MONO_EMIT_NEW_CLASSCONST(cfg,dr,imm) MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,MONO_PATCH_INFO_CLASS)
534 #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)
536 #define MONO_EMIT_NEW_VZERO(cfg,dr,kl) do { \
538 MONO_INST_NEW ((cfg), (inst), MONO_CLASS_IS_SIMD (cfg, kl) ? OP_XZERO : OP_VZERO); \
540 (inst)->type = STACK_VTYPE; \
541 (inst)->klass = (kl); \
542 MONO_ADD_INS ((cfg)->cbb, inst); \
545 #define MONO_EMIT_NEW_UNALU(cfg,op,dr,sr1) do { \
547 EMIT_NEW_UNALU ((cfg), (inst), (op), (dr), (sr1)); \
550 #define MONO_EMIT_NEW_BIALU(cfg,op,dr,sr1,sr2) do { \
552 MONO_INST_NEW ((cfg), (inst), (op)); \
556 MONO_ADD_INS (cfg->cbb, inst); \
559 #define MONO_EMIT_NEW_BIALU_IMM(cfg,op,dr,sr,imm) do { \
561 MONO_INST_NEW ((cfg), (inst), (op)); \
564 inst->inst_p1 = (gpointer)(gssize)(imm); \
565 MONO_ADD_INS (cfg->cbb, inst); \
568 #define MONO_EMIT_NEW_COMPARE_IMM(cfg,sr1,imm) do { \
570 MONO_INST_NEW ((cfg), (inst), (OP_COMPARE_IMM)); \
572 inst->inst_p1 = (gpointer)imm; \
573 MONO_ADD_INS ((cfg)->cbb, inst); \
576 #define MONO_EMIT_NEW_ICOMPARE_IMM(cfg,sr1,imm) do { \
578 MONO_INST_NEW ((cfg), (inst), sizeof (void*) == 8 ? OP_ICOMPARE_IMM : OP_COMPARE_IMM); \
580 inst->inst_p1 = (gpointer)imm; \
581 MONO_ADD_INS ((cfg)->cbb, inst); \
584 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg,op,dr,base,offset) do { \
586 MONO_INST_NEW ((cfg), (inst), (op)); \
588 inst->inst_basereg = base; \
589 inst->inst_offset = offset; \
590 MONO_ADD_INS (cfg->cbb, inst); \
593 #define MONO_EMIT_NEW_LOAD_MEMBASE(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
595 #define MONO_EMIT_NEW_STORE_MEMBASE(cfg,op,base,offset,sr) do { \
597 MONO_INST_NEW ((cfg), (inst), (op)); \
598 (inst)->sreg1 = sr; \
599 (inst)->inst_destbasereg = base; \
600 (inst)->inst_offset = offset; \
601 MONO_ADD_INS (cfg->cbb, inst); \
604 #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
606 MONO_INST_NEW ((cfg), (inst), (op)); \
607 inst->inst_destbasereg = base; \
608 inst->inst_offset = offset; \
609 inst->inst_p1 = (gpointer)(gssize)imm; \
610 MONO_ADD_INS ((cfg)->cbb, inst); \
613 #define MONO_EMIT_NEW_COND_EXC(cfg,cond,name) do { \
615 MONO_INST_NEW ((cfg), (inst), (OP_COND_EXC_##cond)); \
616 inst->inst_p1 = (char*)name; \
617 MONO_ADD_INS ((cfg)->cbb, inst); \
623 * Basic blocks have two numeric identifiers:
624 * dfn: Depth First Number
625 * block_num: unique ID assigned at bblock creation
627 #define NEW_BBLOCK(cfg,bblock) do { \
628 (bblock) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
629 (bblock)->block_num = cfg->num_bblocks++; \
632 #define ADD_BBLOCK(cfg,b) do { \
633 if ((b)->cil_code) {\
634 cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b); \
636 (b)->real_offset = cfg->real_offset; \
639 /* Emit a one-way conditional branch */
641 * The inst_false_bb field of the cond branch will not be set, the JIT code should be
642 * prepared to deal with this.
644 #ifdef DEBUG_EXTENDED_BBLOCKS
645 static int ccount = 0;
646 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
648 MonoBasicBlock *falsebb; \
649 MONO_INST_NEW ((cfg), (ins), (op)); \
650 if ((op) == OP_BR) { \
651 NEW_BBLOCK ((cfg), falsebb); \
652 ins->inst_target_bb = (truebb); \
653 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
654 MONO_ADD_INS ((cfg)->cbb, ins); \
655 MONO_START_BB ((cfg), falsebb); \
658 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
659 ins->inst_true_bb = (truebb); \
660 ins->inst_false_bb = NULL; \
661 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
662 MONO_ADD_INS ((cfg)->cbb, ins); \
663 if (getenv ("COUNT2") && ccount == atoi (getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
664 if (getenv ("COUNT2") && ccount < atoi (getenv ("COUNT2"))) { \
665 cfg->cbb->extended = TRUE; \
666 } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
670 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
672 MonoBasicBlock *falsebb; \
673 MONO_INST_NEW ((cfg), (ins), (op)); \
674 if ((op) == OP_BR) { \
675 NEW_BBLOCK ((cfg), falsebb); \
676 ins->inst_target_bb = (truebb); \
677 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
678 MONO_ADD_INS ((cfg)->cbb, ins); \
679 MONO_START_BB ((cfg), falsebb); \
681 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
682 ins->inst_true_bb = (truebb); \
683 ins->inst_false_bb = NULL; \
684 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
685 MONO_ADD_INS ((cfg)->cbb, ins); \
686 if (!cfg->enable_extended_bblocks) { \
687 NEW_BBLOCK ((cfg), falsebb); \
688 ins->inst_false_bb = falsebb; \
689 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
690 MONO_START_BB ((cfg), falsebb); \
692 cfg->cbb->extended = TRUE; \
698 /* Emit a two-way conditional branch */
699 #define MONO_EMIT_NEW_BRANCH_BLOCK2(cfg,op,truebb,falsebb) do { \
701 MONO_INST_NEW ((cfg), (ins), (op)); \
702 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
703 ins->inst_true_bb = (truebb); \
704 ins->inst_false_bb = (falsebb); \
705 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
706 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
707 MONO_ADD_INS ((cfg)->cbb, ins); \
710 #define MONO_START_BB(cfg, bblock) do { \
711 ADD_BBLOCK ((cfg), (bblock)); \
712 if (cfg->cbb->last_ins && MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins) && !cfg->cbb->last_ins->inst_false_bb) { \
713 cfg->cbb->last_ins->inst_false_bb = (bblock); \
714 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
715 } 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)))) \
716 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
717 (cfg)->cbb->next_bb = (bblock); \
718 (cfg)->cbb = (bblock); \
721 /*Object Model related macros*/
723 #ifndef MONO_ARCH_EMIT_BOUNDS_CHECK
724 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) do { \
725 int _length_reg = alloc_ireg (cfg); \
726 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset); \
727 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, _length_reg, index_reg); \
728 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
732 /* cfg is the MonoCompile been used
733 * array_reg is the vreg holding the array object
734 * array_type is a struct (usually MonoArray or MonoString)
735 * array_length_field is the field in the previous struct with the length
736 * index_reg is the vreg holding the index
738 #define MONO_EMIT_BOUNDS_CHECK(cfg, array_reg, array_type, array_length_field, index_reg) do { \
739 if (!(cfg->opt & MONO_OPT_ABCREM)) { \
740 MONO_ARCH_EMIT_BOUNDS_CHECK ((cfg), (array_reg), G_STRUCT_OFFSET (array_type, array_length_field), (index_reg)); \
743 MONO_INST_NEW ((cfg), ins, OP_BOUNDS_CHECK); \
744 ins->sreg1 = array_reg; \
745 ins->sreg2 = index_reg; \
746 ins->inst_imm = G_STRUCT_OFFSET (array_type, array_length_field); \
747 MONO_ADD_INS ((cfg)->cbb, ins); \
748 (cfg)->flags |= MONO_CFG_HAS_ARRAY_ACCESS; \
749 (cfg)->cbb->has_array_access = TRUE; \