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 ();
80 * FIXME: zeroing out some fields is not needed with the new IR, but the old
81 * JIT code still uses the left and right fields, so it has to stay.
83 #define MONO_INST_NEW(cfg,dest,op) do { \
84 (dest) = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoInst)); \
85 (dest)->inst_c0 = (dest)->inst_c1 = 0; \
86 (dest)->next = (dest)->prev = NULL; \
87 (dest)->opcode = (op); \
91 MONO_INST_NULLIFY_SREGS ((dest)); \
92 (dest)->cil_code = (cfg)->ip; \
96 * Variants which take a dest argument and don't do an emit
98 #define NEW_ICONST(cfg,dest,val) do { \
99 MONO_INST_NEW ((cfg), (dest), OP_ICONST); \
100 (dest)->inst_c0 = (val); \
101 (dest)->type = STACK_I4; \
102 (dest)->dreg = alloc_dreg ((cfg), STACK_I4); \
106 * Avoid using this with a non-NULL val if possible as it is not AOT
107 * compatible. Use one of the NEW_xxxCONST variants instead.
109 #define NEW_PCONST(cfg,dest,val) do { \
110 MONO_INST_NEW ((cfg), (dest), OP_PCONST); \
111 (dest)->inst_p0 = (val); \
112 (dest)->type = STACK_PTR; \
113 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
116 #define NEW_I8CONST(cfg,dest,val) do { \
117 MONO_INST_NEW ((cfg), (dest), OP_I8CONST); \
118 (dest)->dreg = alloc_lreg ((cfg)); \
119 (dest)->type = STACK_I8; \
120 (dest)->inst_l = (val); \
123 #define NEW_STORE_MEMBASE(cfg,dest,op,base,offset,sr) do { \
124 MONO_INST_NEW ((cfg), (dest), (op)); \
125 (dest)->sreg1 = sr; \
126 (dest)->inst_destbasereg = base; \
127 (dest)->inst_offset = offset; \
130 #define NEW_LOAD_MEMBASE(cfg,dest,op,dr,base,offset) do { \
131 MONO_INST_NEW ((cfg), (dest), (op)); \
132 (dest)->dreg = (dr); \
133 (dest)->inst_basereg = (base); \
134 (dest)->inst_offset = (offset); \
135 (dest)->type = STACK_I4; \
138 #define NEW_LOAD_MEM(cfg,dest,op,dr,mem) do { \
139 MONO_INST_NEW ((cfg), (dest), (op)); \
140 (dest)->dreg = (dr); \
141 (dest)->inst_p0 = (gpointer)(gssize)(mem); \
142 (dest)->type = STACK_I4; \
145 #define NEW_UNALU(cfg,dest,op,dr,sr1) do { \
146 MONO_INST_NEW ((cfg), (dest), (op)); \
148 (dest)->sreg1 = sr1; \
151 #define NEW_BIALU(cfg,dest,op,dr,sr1,sr2) do { \
152 MONO_INST_NEW ((cfg), (dest), (op)); \
153 (dest)->dreg = (dr); \
154 (dest)->sreg1 = (sr1); \
155 (dest)->sreg2 = (sr2); \
158 #define NEW_BIALU_IMM(cfg,dest,op,dr,sr,imm) do { \
159 MONO_INST_NEW ((cfg), (dest), (op)); \
161 (dest)->sreg1 = sr; \
162 (dest)->inst_p1 = (gpointer)(gssize)(imm); \
165 #ifdef MONO_ARCH_NEED_GOT_VAR
167 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do { \
168 MONO_INST_NEW ((cfg), (dest), OP_PATCH_INFO); \
169 (dest)->inst_left = (gpointer)(el1); \
170 (dest)->inst_right = (gpointer)(el2); \
173 /* FIXME: Add the PUSH_GOT_ENTRY optimizations */
174 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
175 MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST); \
176 if (cfg->compile_aot) { \
177 MonoInst *group, *got_loc; \
178 got_loc = mono_get_got_var (cfg); \
179 NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
180 (dest)->inst_basereg = got_loc->dreg; \
181 (dest)->inst_p1 = group; \
183 (dest)->inst_p0 = (cons); \
184 (dest)->inst_i1 = (gpointer)(patch_type); \
186 (dest)->type = STACK_PTR; \
187 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
190 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
191 MonoInst *group, *got_loc; \
192 MONO_INST_NEW ((cfg), (dest), OP_GOT_ENTRY); \
193 got_loc = mono_get_got_var (cfg); \
194 NEW_PATCH_INFO ((cfg), group, NULL, patch_type); \
195 group->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
196 (dest)->inst_basereg = got_loc->dreg; \
197 (dest)->inst_p1 = group; \
198 (dest)->type = (stack_type); \
199 (dest)->klass = (stack_class); \
200 (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
205 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
206 MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
207 (dest)->inst_p0 = (cons); \
208 (dest)->inst_i1 = (gpointer)(patch_type); \
209 (dest)->type = STACK_PTR; \
210 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
213 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
214 MONO_INST_NEW ((cfg), (dest), OP_AOTCONST); \
215 (dest)->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
216 (dest)->inst_p1 = (gpointer)(patch_type); \
217 (dest)->type = (stack_type); \
218 (dest)->klass = (stack_class); \
219 (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
224 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
226 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
228 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
230 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
232 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
234 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
236 #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)
238 #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)
240 #define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), NULL, STACK_PTR, NULL)
242 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
243 if (cfg->compile_aot) { \
244 NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, NULL, STACK_OBJ, NULL); \
246 NEW_PCONST (cfg, args [0], (entry).blob); \
250 #define NEW_METHOD_RGCTX_CONST(cfg,dest,method) do { \
251 if (cfg->compile_aot) { \
252 NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHOD_RGCTX, (method)); \
254 MonoMethodRuntimeGenericContext *mrgctx; \
255 mrgctx = mono_method_lookup_rgctx (mono_class_vtable ((cfg)->domain, (method)->klass), mini_method_get_context ((method))->method_inst); \
256 NEW_PCONST ((cfg), (dest), (mrgctx)); \
260 #define NEW_DOMAINCONST(cfg,dest) do { \
261 if ((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) { \
262 /* avoid depending on undefined C behavior in sequence points */ \
263 MonoInst* __domain_var = mono_get_domainvar (cfg); \
264 NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
266 NEW_PCONST (cfg, dest, (cfg)->domain); \
270 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
272 #define NEW_VARLOAD(cfg,dest,var,vartype) do { \
273 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
274 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
275 type_to_eval_stack_type ((cfg), (vartype), (dest)); \
276 (dest)->klass = var->klass; \
277 (dest)->sreg1 = var->dreg; \
278 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
279 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
282 #ifdef MONO_ARCH_SOFT_FLOAT
283 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8 || (stack_type) == STACK_R8)
285 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8)
288 #define NEW_VARLOADA(cfg,dest,var,vartype) do { \
289 MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
290 (dest)->inst_p0 = (var); \
291 (var)->flags |= MONO_INST_INDIRECT; \
292 (dest)->type = STACK_MP; \
293 (dest)->klass = (var)->klass; \
294 (dest)->dreg = alloc_dreg ((cfg), STACK_MP); \
295 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; } \
298 #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do { \
299 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
300 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
301 (dest)->klass = (var)->klass; \
302 (dest)->sreg1 = (inst)->dreg; \
303 (dest)->dreg = (var)->dreg; \
304 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
307 #define NEW_TEMPLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype)
309 #define NEW_TEMPLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), cfg->varinfo [(num)], cfg->varinfo [(num)]->inst_vtype)
311 #define NEW_TEMPSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype, (inst))
313 #define NEW_ARGLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
315 #define NEW_LOCLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
317 #define NEW_LOCSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
319 #define NEW_ARGSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
321 #define NEW_LOCLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype)
323 #define NEW_RETLOADA(cfg,dest) do { \
324 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
325 (dest)->type = STACK_MP; \
326 (dest)->klass = cfg->ret->klass; \
327 (dest)->sreg1 = cfg->vret_addr->dreg; \
328 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
331 #define NEW_ARGLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), arg_array [(num)], param_types [(num)])
333 /* Promote the vreg to a variable so its address can be taken */
334 #define NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { \
335 MonoInst *var = get_vreg_to_inst ((cfg), (vreg)); \
337 var = mono_compile_create_var_for_vreg ((cfg), (ltype), OP_LOCAL, (vreg)); \
338 NEW_VARLOADA ((cfg), (dest), (var), (ltype)); \
341 #define NEW_DUMMY_USE(cfg,dest,var) do { \
342 MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
343 (dest)->sreg1 = var->dreg; \
346 /* Variants which take a type argument and handle vtypes as well */
347 #define NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { \
348 NEW_LOAD_MEMBASE ((cfg), (dest), mono_type_to_load_membase ((cfg), (ltype)), 0, (base), (offset)); \
349 type_to_eval_stack_type ((cfg), (ltype), (dest)); \
350 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
353 #define NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { \
354 MONO_INST_NEW ((cfg), (dest), mono_type_to_store_membase ((cfg), (ltype))); \
355 (dest)->sreg1 = sr; \
356 (dest)->inst_destbasereg = base; \
357 (dest)->inst_offset = offset; \
358 type_to_eval_stack_type ((cfg), (ltype), (dest)); \
359 (dest)->klass = mono_class_from_mono_type (ltype); \
363 * Variants which do an emit as well.
365 #define EMIT_NEW_ICONST(cfg,dest,val) do { NEW_ICONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
367 #define EMIT_NEW_PCONST(cfg,dest,val) do { NEW_PCONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
369 #define EMIT_NEW_I8CONST(cfg,dest,val) do { NEW_I8CONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
371 #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)
373 #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)
375 #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)
377 #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)
379 #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)
381 #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)
383 #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)
385 #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)
387 #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)
389 #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)
391 #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)
393 #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
395 #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
397 #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)
399 #define EMIT_NEW_VARLOAD(cfg,dest,var,vartype) do { NEW_VARLOAD ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
401 #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)
403 #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
406 #ifdef MONO_ARCH_SOFT_FLOAT
409 * Since the IL stack (and our vregs) contain double values, we have to do a conversion
410 * when loading/storing args/locals of type R4.
413 #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
414 if (!(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
415 MonoInst *iargs [1]; \
416 EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
417 (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
419 EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
423 #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do { \
424 if (!(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
425 MonoInst *iargs [2]; \
426 iargs [0] = (inst); \
427 EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
428 mono_emit_jit_icall (cfg, mono_fstore_r4, iargs); \
430 EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
434 #define EMIT_NEW_ARGLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
436 #define EMIT_NEW_LOCLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
438 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
440 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
444 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { NEW_ARGLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
446 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { NEW_LOCLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
448 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { NEW_LOCSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
450 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
454 #define EMIT_NEW_TEMPLOAD(cfg,dest,num) do { NEW_TEMPLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
456 #define EMIT_NEW_TEMPLOADA(cfg,dest,num) do { NEW_TEMPLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
458 #define EMIT_NEW_LOCLOADA(cfg,dest,num) do { NEW_LOCLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
460 #define EMIT_NEW_ARGLOADA(cfg,dest,num) do { NEW_ARGLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
462 #define EMIT_NEW_RETLOADA(cfg,dest) do { NEW_RETLOADA ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
464 #define EMIT_NEW_TEMPSTORE(cfg,dest,num,inst) do { NEW_TEMPSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
466 #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)
468 #define EMIT_NEW_DUMMY_USE(cfg,dest,var) do { NEW_DUMMY_USE ((cfg), (dest), (var)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
470 #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)
472 #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)
474 #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)
476 #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)
478 #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)
480 #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)
482 #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)
485 * Variants which do not take an dest argument, but take a dreg argument.
487 #define MONO_EMIT_NEW_ICONST(cfg,dr,imm) do { \
489 MONO_INST_NEW ((cfg), (inst), OP_ICONST); \
491 inst->inst_c0 = imm; \
492 MONO_ADD_INS ((cfg)->cbb, inst); \
495 #define MONO_EMIT_NEW_PCONST(cfg,dr,val) do { \
497 MONO_INST_NEW ((cfg), (inst), OP_PCONST); \
499 (inst)->inst_p0 = (val); \
500 (inst)->type = STACK_PTR; \
501 MONO_ADD_INS ((cfg)->cbb, inst); \
504 #define MONO_EMIT_NEW_I8CONST(cfg,dr,imm) do { \
506 MONO_INST_NEW ((cfg), (inst), OP_I8CONST); \
508 inst->inst_l = imm; \
509 MONO_ADD_INS ((cfg)->cbb, inst); \
512 #ifdef MONO_ARCH_NEED_GOT_VAR
514 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
516 NEW_AOTCONST ((cfg), (inst), (patch_type), (cons)); \
518 MONO_ADD_INS ((cfg)->cbb, inst); \
523 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,type) do { \
525 MONO_INST_NEW ((cfg), (inst), OP_AOTCONST); \
527 inst->inst_p0 = imm; \
528 inst->inst_c1 = type; \
529 MONO_ADD_INS ((cfg)->cbb, inst); \
534 #define MONO_EMIT_NEW_CLASSCONST(cfg,dr,imm) MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,MONO_PATCH_INFO_CLASS)
535 #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)
537 #define MONO_EMIT_NEW_VZERO(cfg,dr,kl) do { \
539 MONO_INST_NEW ((cfg), (inst), MONO_CLASS_IS_SIMD (cfg, kl) ? OP_XZERO : OP_VZERO); \
541 (inst)->type = STACK_VTYPE; \
542 (inst)->klass = (kl); \
543 MONO_ADD_INS ((cfg)->cbb, inst); \
546 #define MONO_EMIT_NEW_UNALU(cfg,op,dr,sr1) do { \
548 EMIT_NEW_UNALU ((cfg), (inst), (op), (dr), (sr1)); \
551 #define MONO_EMIT_NEW_BIALU(cfg,op,dr,sr1,sr2) do { \
553 MONO_INST_NEW ((cfg), (inst), (op)); \
557 MONO_ADD_INS (cfg->cbb, inst); \
560 #define MONO_EMIT_NEW_BIALU_IMM(cfg,op,dr,sr,imm) do { \
562 MONO_INST_NEW ((cfg), (inst), (op)); \
565 inst->inst_p1 = (gpointer)(gssize)(imm); \
566 MONO_ADD_INS (cfg->cbb, inst); \
569 #define MONO_EMIT_NEW_COMPARE_IMM(cfg,sr1,imm) do { \
571 MONO_INST_NEW ((cfg), (inst), (OP_COMPARE_IMM)); \
573 inst->inst_p1 = (gpointer)imm; \
574 MONO_ADD_INS ((cfg)->cbb, inst); \
577 #define MONO_EMIT_NEW_ICOMPARE_IMM(cfg,sr1,imm) do { \
579 MONO_INST_NEW ((cfg), (inst), sizeof (void*) == 8 ? OP_ICOMPARE_IMM : OP_COMPARE_IMM); \
581 inst->inst_p1 = (gpointer)imm; \
582 MONO_ADD_INS ((cfg)->cbb, inst); \
585 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg,op,dr,base,offset) do { \
587 MONO_INST_NEW ((cfg), (inst), (op)); \
589 inst->inst_basereg = base; \
590 inst->inst_offset = offset; \
591 MONO_ADD_INS (cfg->cbb, inst); \
594 #define MONO_EMIT_NEW_LOAD_MEMBASE(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
596 #define MONO_EMIT_NEW_STORE_MEMBASE(cfg,op,base,offset,sr) do { \
598 MONO_INST_NEW ((cfg), (inst), (op)); \
599 (inst)->sreg1 = sr; \
600 (inst)->inst_destbasereg = base; \
601 (inst)->inst_offset = offset; \
602 MONO_ADD_INS (cfg->cbb, inst); \
605 #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
607 MONO_INST_NEW ((cfg), (inst), (op)); \
608 inst->inst_destbasereg = base; \
609 inst->inst_offset = offset; \
610 inst->inst_p1 = (gpointer)(gssize)imm; \
611 MONO_ADD_INS ((cfg)->cbb, inst); \
614 #define MONO_EMIT_NEW_COND_EXC(cfg,cond,name) do { \
616 MONO_INST_NEW ((cfg), (inst), (OP_COND_EXC_##cond)); \
617 inst->inst_p1 = (char*)name; \
618 MONO_ADD_INS ((cfg)->cbb, inst); \
624 * Basic blocks have two numeric identifiers:
625 * dfn: Depth First Number
626 * block_num: unique ID assigned at bblock creation
628 #define NEW_BBLOCK(cfg,bblock) do { \
629 (bblock) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
630 (bblock)->block_num = cfg->num_bblocks++; \
633 #define ADD_BBLOCK(cfg,b) do { \
634 if ((b)->cil_code) {\
635 cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b); \
637 (b)->real_offset = cfg->real_offset; \
640 /* Emit a one-way conditional branch */
642 * The inst_false_bb field of the cond branch will not be set, the JIT code should be
643 * prepared to deal with this.
645 #ifdef DEBUG_EXTENDED_BBLOCKS
646 static int ccount = 0;
647 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
649 MonoBasicBlock *falsebb; \
650 MONO_INST_NEW ((cfg), (ins), (op)); \
651 if ((op) == OP_BR) { \
652 NEW_BBLOCK ((cfg), falsebb); \
653 ins->inst_target_bb = (truebb); \
654 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
655 MONO_ADD_INS ((cfg)->cbb, ins); \
656 MONO_START_BB ((cfg), falsebb); \
659 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
660 ins->inst_true_bb = (truebb); \
661 ins->inst_false_bb = NULL; \
662 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
663 MONO_ADD_INS ((cfg)->cbb, ins); \
664 if (getenv ("COUNT2") && ccount == atoi (getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
665 if (getenv ("COUNT2") && ccount < atoi (getenv ("COUNT2"))) { \
666 cfg->cbb->extended = TRUE; \
667 } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
671 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
673 MonoBasicBlock *falsebb; \
674 MONO_INST_NEW ((cfg), (ins), (op)); \
675 if ((op) == OP_BR) { \
676 NEW_BBLOCK ((cfg), falsebb); \
677 ins->inst_target_bb = (truebb); \
678 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
679 MONO_ADD_INS ((cfg)->cbb, ins); \
680 MONO_START_BB ((cfg), falsebb); \
682 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
683 ins->inst_true_bb = (truebb); \
684 ins->inst_false_bb = NULL; \
685 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
686 MONO_ADD_INS ((cfg)->cbb, ins); \
687 if (!cfg->enable_extended_bblocks) { \
688 NEW_BBLOCK ((cfg), falsebb); \
689 ins->inst_false_bb = falsebb; \
690 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
691 MONO_START_BB ((cfg), falsebb); \
693 cfg->cbb->extended = TRUE; \
699 /* Emit a two-way conditional branch */
700 #define MONO_EMIT_NEW_BRANCH_BLOCK2(cfg,op,truebb,falsebb) do { \
702 MONO_INST_NEW ((cfg), (ins), (op)); \
703 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
704 ins->inst_true_bb = (truebb); \
705 ins->inst_false_bb = (falsebb); \
706 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
707 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
708 MONO_ADD_INS ((cfg)->cbb, ins); \
711 #define MONO_START_BB(cfg, bblock) do { \
712 ADD_BBLOCK ((cfg), (bblock)); \
713 if (cfg->cbb->last_ins && MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins) && !cfg->cbb->last_ins->inst_false_bb) { \
714 cfg->cbb->last_ins->inst_false_bb = (bblock); \
715 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
716 } 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)))) \
717 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
718 (cfg)->cbb->next_bb = (bblock); \
719 (cfg)->cbb = (bblock); \
722 /*Object Model related macros*/
724 #ifndef MONO_ARCH_EMIT_BOUNDS_CHECK
725 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) do { \
726 int _length_reg = alloc_ireg (cfg); \
727 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset); \
728 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, _length_reg, index_reg); \
729 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
733 /* cfg is the MonoCompile been used
734 * array_reg is the vreg holding the array object
735 * array_type is a struct (usually MonoArray or MonoString)
736 * array_length_field is the field in the previous struct with the length
737 * index_reg is the vreg holding the index
739 #define MONO_EMIT_BOUNDS_CHECK(cfg, array_reg, array_type, array_length_field, index_reg) do { \
740 if (!(cfg->opt & MONO_OPT_ABCREM)) { \
741 MONO_ARCH_EMIT_BOUNDS_CHECK ((cfg), (array_reg), G_STRUCT_OFFSET (array_type, array_length_field), (index_reg)); \
744 MONO_INST_NEW ((cfg), ins, OP_BOUNDS_CHECK); \
745 ins->sreg1 = array_reg; \
746 ins->sreg2 = index_reg; \
747 ins->inst_imm = G_STRUCT_OFFSET (array_type, array_length_field); \
748 MONO_ADD_INS ((cfg)->cbb, ins); \
749 (cfg)->flags |= MONO_CFG_HAS_ARRAY_ACCESS; \
750 (cfg)->cbb->has_array_access = TRUE; \