Make sure x86 ATOMIC_CAS doesn't overwrite its own operands.
[mono-debugger.git] / mono / mini / ir-emit.h
blob460d653bc0ff496cf4fa1f0be7ffe35aa610d246
1 /*
2 * ir-emit.h: IR Creation/Emission Macros
4 * Author:
5 * Zoltan Varga (vargaz@gmail.com)
7 * (C) 2002 Ximian, Inc.
8 */
10 #ifndef __MONO_IR_EMIT_H__
11 #define __MONO_IR_EMIT_H__
13 #include "mini.h"
15 G_BEGIN_DECLS
17 static inline guint32
18 alloc_ireg (MonoCompile *cfg)
20 return cfg->next_vreg ++;
23 static inline guint32
24 alloc_preg (MonoCompile *cfg)
26 return alloc_ireg (cfg);
29 static inline guint32
30 alloc_lreg (MonoCompile *cfg)
32 #if SIZEOF_REGISTER == 8
33 return cfg->next_vreg ++;
34 #else
35 /* Use a pair of consecutive vregs */
36 guint32 res = cfg->next_vreg;
38 cfg->next_vreg += 3;
40 return res;
41 #endif
44 static inline guint32
45 alloc_freg (MonoCompile *cfg)
47 #ifdef MONO_ARCH_SOFT_FLOAT
48 /* Allocate an lvreg so float ops can be decomposed into long ops */
49 return alloc_lreg (cfg);
50 #else
51 /* Allocate these from the same pool as the int regs */
52 return cfg->next_vreg ++;
53 #endif
56 static inline guint32
57 alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
59 switch (stack_type) {
60 case STACK_I4:
61 case STACK_PTR:
62 case STACK_MP:
63 case STACK_OBJ:
64 return alloc_ireg (cfg);
65 case STACK_R8:
66 return alloc_freg (cfg);
67 case STACK_I8:
68 return alloc_lreg (cfg);
69 case STACK_VTYPE:
70 return alloc_ireg (cfg);
71 default:
72 g_warning ("Unknown stack type %x\n", stack_type);
73 g_assert_not_reached ();
77 #undef MONO_INST_NEW
78 /*
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); \
87 (dest)->flags = 0; \
88 (dest)->type = 0; \
89 (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1; \
90 (dest)->cil_code = (cfg)->ip; \
91 } while (0)
94 * Variants which take a dest argument and don't do an emit
96 #define NEW_ICONST(cfg,dest,val) do { \
97 MONO_INST_NEW ((cfg), (dest), OP_ICONST); \
98 (dest)->inst_c0 = (val); \
99 (dest)->type = STACK_I4; \
100 (dest)->dreg = alloc_dreg ((cfg), STACK_I4); \
101 } while (0)
104 * Avoid using this with a non-NULL val if possible as it is not AOT
105 * compatible. Use one of the NEW_xxxCONST variants instead.
107 #define NEW_PCONST(cfg,dest,val) do { \
108 MONO_INST_NEW ((cfg), (dest), OP_PCONST); \
109 (dest)->inst_p0 = (val); \
110 (dest)->type = STACK_PTR; \
111 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
112 } while (0)
114 #define NEW_I8CONST(cfg,dest,val) do { \
115 MONO_INST_NEW ((cfg), (dest), OP_I8CONST); \
116 (dest)->dreg = alloc_lreg ((cfg)); \
117 (dest)->type = STACK_I8; \
118 (dest)->inst_l = (val); \
119 } while (0)
121 #define NEW_STORE_MEMBASE(cfg,dest,op,base,offset,sr) do { \
122 MONO_INST_NEW ((cfg), (dest), (op)); \
123 (dest)->sreg1 = sr; \
124 (dest)->inst_destbasereg = base; \
125 (dest)->inst_offset = offset; \
126 } while (0)
128 #define NEW_LOAD_MEMBASE(cfg,dest,op,dr,base,offset) do { \
129 MONO_INST_NEW ((cfg), (dest), (op)); \
130 (dest)->dreg = (dr); \
131 (dest)->inst_basereg = (base); \
132 (dest)->inst_offset = (offset); \
133 (dest)->type = STACK_I4; \
134 } while (0)
136 #define NEW_LOAD_MEM(cfg,dest,op,dr,mem) do { \
137 MONO_INST_NEW ((cfg), (dest), (op)); \
138 (dest)->dreg = (dr); \
139 (dest)->inst_p0 = (gpointer)(gssize)(mem); \
140 (dest)->type = STACK_I4; \
141 } while (0)
143 #define NEW_UNALU(cfg,dest,op,dr,sr1) do { \
144 MONO_INST_NEW ((cfg), (dest), (op)); \
145 (dest)->dreg = dr; \
146 (dest)->sreg1 = sr1; \
147 } while (0)
149 #define NEW_BIALU(cfg,dest,op,dr,sr1,sr2) do { \
150 MONO_INST_NEW ((cfg), (dest), (op)); \
151 (dest)->dreg = (dr); \
152 (dest)->sreg1 = (sr1); \
153 (dest)->sreg2 = (sr2); \
154 } while (0)
156 #define NEW_BIALU_IMM(cfg,dest,op,dr,sr,imm) do { \
157 MONO_INST_NEW ((cfg), (dest), (op)); \
158 (dest)->dreg = dr; \
159 (dest)->sreg1 = sr; \
160 (dest)->inst_p1 = (gpointer)(gssize)(imm); \
161 } while (0)
163 #ifdef MONO_ARCH_NEED_GOT_VAR
165 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do { \
166 MONO_INST_NEW ((cfg), (dest), OP_PATCH_INFO); \
167 (dest)->inst_left = (gpointer)(el1); \
168 (dest)->inst_right = (gpointer)(el2); \
169 } while (0)
171 /* FIXME: Add the PUSH_GOT_ENTRY optimizations */
172 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
173 MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST); \
174 if (cfg->compile_aot) { \
175 MonoInst *group, *got_loc; \
176 got_loc = mono_get_got_var (cfg); \
177 NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
178 (dest)->inst_basereg = got_loc->dreg; \
179 (dest)->inst_p1 = group; \
180 } else { \
181 (dest)->inst_p0 = (cons); \
182 (dest)->inst_i1 = (gpointer)(patch_type); \
184 (dest)->type = STACK_PTR; \
185 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
186 } while (0)
188 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
189 MonoInst *group, *got_loc; \
190 MONO_INST_NEW ((cfg), (dest), OP_GOT_ENTRY); \
191 got_loc = mono_get_got_var (cfg); \
192 NEW_PATCH_INFO ((cfg), group, NULL, patch_type); \
193 group->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
194 (dest)->inst_basereg = got_loc->dreg; \
195 (dest)->inst_p1 = group; \
196 (dest)->type = (stack_type); \
197 (dest)->klass = (stack_class); \
198 (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
199 } while (0)
201 #else
203 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
204 MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
205 (dest)->inst_p0 = (cons); \
206 (dest)->inst_i1 = (gpointer)(patch_type); \
207 (dest)->type = STACK_PTR; \
208 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR); \
209 } while (0)
211 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,generic_context,stack_type,stack_class) do { \
212 MONO_INST_NEW ((cfg), (dest), OP_AOTCONST); \
213 (dest)->inst_p0 = mono_jump_info_token_new2 ((cfg)->mempool, (image), (token), (generic_context)); \
214 (dest)->inst_p1 = (gpointer)(patch_type); \
215 (dest)->type = (stack_type); \
216 (dest)->klass = (stack_class); \
217 (dest)->dreg = alloc_dreg ((cfg), (stack_type)); \
218 } while (0)
220 #endif
222 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
224 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
226 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
228 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
230 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
232 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
234 #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)
236 #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)
238 #define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), NULL, STACK_PTR, NULL)
240 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
241 if (cfg->compile_aot) { \
242 NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, NULL, STACK_OBJ, NULL); \
243 } else { \
244 NEW_PCONST (cfg, args [0], (entry).blob); \
246 } while (0)
248 #define NEW_METHOD_RGCTX_CONST(cfg,dest,method) do { \
249 if (cfg->compile_aot) { \
250 NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHOD_RGCTX, (method)); \
251 } else { \
252 MonoMethodRuntimeGenericContext *mrgctx; \
253 mrgctx = mono_method_lookup_rgctx (mono_class_vtable ((cfg)->domain, (method)->klass), mini_method_get_context ((method))->method_inst); \
254 NEW_PCONST ((cfg), (dest), (mrgctx)); \
256 } while (0)
258 #define NEW_DOMAINCONST(cfg,dest) do { \
259 if ((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) { \
260 /* avoid depending on undefined C behavior in sequence points */ \
261 MonoInst* __domain_var = mono_get_domainvar (cfg); \
262 NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
263 } else { \
264 NEW_PCONST (cfg, dest, (cfg)->domain); \
266 } while (0)
268 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
270 #define NEW_VARLOAD(cfg,dest,var,vartype) do { \
271 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
272 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
273 type_to_eval_stack_type ((cfg), (vartype), (dest)); \
274 (dest)->klass = var->klass; \
275 (dest)->sreg1 = var->dreg; \
276 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
277 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
278 } while (0)
280 #ifdef MONO_ARCH_SOFT_FLOAT
281 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8 || (stack_type) == STACK_R8)
282 #else
283 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8)
284 #endif
286 #define NEW_VARLOADA(cfg,dest,var,vartype) do { \
287 MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
288 (dest)->inst_p0 = (var); \
289 (var)->flags |= MONO_INST_INDIRECT; \
290 (dest)->type = STACK_MP; \
291 (dest)->klass = (var)->klass; \
292 (dest)->dreg = alloc_dreg ((cfg), STACK_MP); \
293 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; } \
294 } while (0)
296 #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do { \
297 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
298 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
299 (dest)->klass = (var)->klass; \
300 (dest)->sreg1 = (inst)->dreg; \
301 (dest)->dreg = (var)->dreg; \
302 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
303 } while (0)
305 #define NEW_TEMPLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype)
307 #define NEW_TEMPLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), cfg->varinfo [(num)], cfg->varinfo [(num)]->inst_vtype)
309 #define NEW_TEMPSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype, (inst))
311 #define NEW_ARGLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
313 #define NEW_LOCLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
315 #define NEW_LOCSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
317 #define NEW_ARGSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
319 #define NEW_LOCLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype)
321 #define NEW_RETLOADA(cfg,dest) do { \
322 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
323 (dest)->type = STACK_MP; \
324 (dest)->klass = cfg->ret->klass; \
325 (dest)->sreg1 = cfg->vret_addr->dreg; \
326 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
327 } while (0)
329 #define NEW_ARGLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), arg_array [(num)], param_types [(num)])
331 /* Promote the vreg to a variable so its address can be taken */
332 #define NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { \
333 MonoInst *var = get_vreg_to_inst ((cfg), (vreg)); \
334 if (!var) \
335 var = mono_compile_create_var_for_vreg ((cfg), (ltype), OP_LOCAL, (vreg)); \
336 NEW_VARLOADA ((cfg), (dest), (var), (ltype)); \
337 } while (0)
339 #define NEW_DUMMY_USE(cfg,dest,var) do { \
340 MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
341 (dest)->sreg1 = var->dreg; \
342 } while (0)
344 /* Variants which take a type argument and handle vtypes as well */
345 #define NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { \
346 NEW_LOAD_MEMBASE ((cfg), (dest), mono_type_to_load_membase ((cfg), (ltype)), 0, (base), (offset)); \
347 type_to_eval_stack_type ((cfg), (ltype), (dest)); \
348 (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
349 } while (0)
351 #define NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { \
352 MONO_INST_NEW ((cfg), (dest), mono_type_to_store_membase ((cfg), (ltype))); \
353 (dest)->sreg1 = sr; \
354 (dest)->inst_destbasereg = base; \
355 (dest)->inst_offset = offset; \
356 type_to_eval_stack_type ((cfg), (ltype), (dest)); \
357 (dest)->klass = mono_class_from_mono_type (ltype); \
358 } while (0)
361 * Variants which do an emit as well.
363 #define EMIT_NEW_ICONST(cfg,dest,val) do { NEW_ICONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
365 #define EMIT_NEW_PCONST(cfg,dest,val) do { NEW_PCONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
367 #define EMIT_NEW_I8CONST(cfg,dest,val) do { NEW_I8CONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
369 #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)
371 #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)
373 #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)
375 #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)
377 #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)
379 #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)
381 #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)
383 #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)
385 #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)
387 #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)
389 #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)
391 #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
393 #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
395 #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)
397 #define EMIT_NEW_VARLOAD(cfg,dest,var,vartype) do { NEW_VARLOAD ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
399 #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)
401 #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
404 #ifdef MONO_ARCH_SOFT_FLOAT
407 * Since the IL stack (and our vregs) contain double values, we have to do a conversion
408 * when loading/storing args/locals of type R4.
411 #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
412 if (!(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
413 MonoInst *iargs [1]; \
414 EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
415 (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
416 } else { \
417 EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
419 } while (0)
421 #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do { \
422 if (!(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
423 MonoInst *iargs [2]; \
424 iargs [0] = (inst); \
425 EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
426 mono_emit_jit_icall (cfg, mono_fstore_r4, iargs); \
427 } else { \
428 EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
430 } while (0)
432 #define EMIT_NEW_ARGLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
434 #define EMIT_NEW_LOCLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
436 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
438 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
440 #else
442 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { NEW_ARGLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
444 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { NEW_LOCLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
446 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { NEW_LOCSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
448 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
450 #endif
452 #define EMIT_NEW_TEMPLOAD(cfg,dest,num) do { NEW_TEMPLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
454 #define EMIT_NEW_TEMPLOADA(cfg,dest,num) do { NEW_TEMPLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
456 #define EMIT_NEW_LOCLOADA(cfg,dest,num) do { NEW_LOCLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
458 #define EMIT_NEW_ARGLOADA(cfg,dest,num) do { NEW_ARGLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
460 #define EMIT_NEW_RETLOADA(cfg,dest) do { NEW_RETLOADA ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
462 #define EMIT_NEW_TEMPSTORE(cfg,dest,num,inst) do { NEW_TEMPSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
464 #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)
466 #define EMIT_NEW_DUMMY_USE(cfg,dest,var) do { NEW_DUMMY_USE ((cfg), (dest), (var)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
468 #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)
470 #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)
472 #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)
474 #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)
476 #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)
478 #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)
480 #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)
483 * Variants which do not take an dest argument, but take a dreg argument.
485 #define MONO_EMIT_NEW_ICONST(cfg,dr,imm) do { \
486 MonoInst *inst; \
487 MONO_INST_NEW ((cfg), (inst), OP_ICONST); \
488 inst->dreg = dr; \
489 inst->inst_c0 = imm; \
490 MONO_ADD_INS ((cfg)->cbb, inst); \
491 } while (0)
493 #define MONO_EMIT_NEW_PCONST(cfg,dr,val) do { \
494 MonoInst *inst; \
495 MONO_INST_NEW ((cfg), (inst), OP_PCONST); \
496 inst->dreg = dr; \
497 (inst)->inst_p0 = (val); \
498 (inst)->type = STACK_PTR; \
499 MONO_ADD_INS ((cfg)->cbb, inst); \
500 } while (0)
502 #define MONO_EMIT_NEW_I8CONST(cfg,dr,imm) do { \
503 MonoInst *inst; \
504 MONO_INST_NEW ((cfg), (inst), OP_I8CONST); \
505 inst->dreg = dr; \
506 inst->inst_l = imm; \
507 MONO_ADD_INS ((cfg)->cbb, inst); \
508 } while (0)
510 #ifdef MONO_ARCH_NEED_GOT_VAR
512 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
513 MonoInst *inst; \
514 NEW_AOTCONST ((cfg), (inst), (patch_type), (cons)); \
515 inst->dreg = (dr); \
516 MONO_ADD_INS ((cfg)->cbb, inst); \
517 } while (0)
519 #else
521 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,type) do { \
522 MonoInst *inst; \
523 MONO_INST_NEW ((cfg), (inst), OP_AOTCONST); \
524 inst->dreg = dr; \
525 inst->inst_p0 = imm; \
526 inst->inst_c1 = type; \
527 MONO_ADD_INS ((cfg)->cbb, inst); \
528 } while (0)
530 #endif
532 #define MONO_EMIT_NEW_CLASSCONST(cfg,dr,imm) MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,MONO_PATCH_INFO_CLASS)
533 #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)
535 #define MONO_EMIT_NEW_VZERO(cfg,dr,kl) do { \
536 MonoInst *inst; \
537 MONO_INST_NEW ((cfg), (inst), MONO_CLASS_IS_SIMD (cfg, kl) ? OP_XZERO : OP_VZERO); \
538 inst->dreg = dr; \
539 (inst)->type = STACK_VTYPE; \
540 (inst)->klass = (kl); \
541 MONO_ADD_INS ((cfg)->cbb, inst); \
542 } while (0)
544 #define MONO_EMIT_NEW_UNALU(cfg,op,dr,sr1) do { \
545 MonoInst *inst; \
546 EMIT_NEW_UNALU ((cfg), (inst), (op), (dr), (sr1)); \
547 } while (0)
549 #define MONO_EMIT_NEW_BIALU(cfg,op,dr,sr1,sr2) do { \
550 MonoInst *inst; \
551 MONO_INST_NEW ((cfg), (inst), (op)); \
552 inst->dreg = dr; \
553 inst->sreg1 = sr1; \
554 inst->sreg2 = sr2; \
555 MONO_ADD_INS (cfg->cbb, inst); \
556 } while (0)
558 #define MONO_EMIT_NEW_BIALU_IMM(cfg,op,dr,sr,imm) do { \
559 MonoInst *inst; \
560 MONO_INST_NEW ((cfg), (inst), (op)); \
561 inst->dreg = dr; \
562 inst->sreg1 = sr; \
563 inst->inst_p1 = (gpointer)(gssize)(imm); \
564 MONO_ADD_INS (cfg->cbb, inst); \
565 } while (0)
567 #define MONO_EMIT_NEW_COMPARE_IMM(cfg,sr1,imm) do { \
568 MonoInst *inst; \
569 MONO_INST_NEW ((cfg), (inst), (OP_COMPARE_IMM)); \
570 inst->sreg1 = sr1; \
571 inst->inst_p1 = (gpointer)imm; \
572 MONO_ADD_INS ((cfg)->cbb, inst); \
573 } while (0)
575 #define MONO_EMIT_NEW_ICOMPARE_IMM(cfg,sr1,imm) do { \
576 MonoInst *inst; \
577 MONO_INST_NEW ((cfg), (inst), sizeof (void*) == 8 ? OP_ICOMPARE_IMM : OP_COMPARE_IMM); \
578 inst->sreg1 = sr1; \
579 inst->inst_p1 = (gpointer)imm; \
580 MONO_ADD_INS ((cfg)->cbb, inst); \
581 } while (0)
583 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg,op,dr,base,offset) do { \
584 MonoInst *inst; \
585 MONO_INST_NEW ((cfg), (inst), (op)); \
586 inst->dreg = dr; \
587 inst->inst_basereg = base; \
588 inst->inst_offset = offset; \
589 MONO_ADD_INS (cfg->cbb, inst); \
590 } while (0)
592 #define MONO_EMIT_NEW_LOAD_MEMBASE(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
594 #define MONO_EMIT_NEW_STORE_MEMBASE(cfg,op,base,offset,sr) do { \
595 MonoInst *inst; \
596 MONO_INST_NEW ((cfg), (inst), (op)); \
597 (inst)->sreg1 = sr; \
598 (inst)->inst_destbasereg = base; \
599 (inst)->inst_offset = offset; \
600 MONO_ADD_INS (cfg->cbb, inst); \
601 } while (0)
603 #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
604 MonoInst *inst; \
605 MONO_INST_NEW ((cfg), (inst), (op)); \
606 inst->inst_destbasereg = base; \
607 inst->inst_offset = offset; \
608 inst->inst_p1 = (gpointer)(gssize)imm; \
609 MONO_ADD_INS ((cfg)->cbb, inst); \
610 } while (0)
612 #define MONO_EMIT_NEW_COND_EXC(cfg,cond,name) do { \
613 MonoInst *inst; \
614 MONO_INST_NEW ((cfg), (inst), (OP_COND_EXC_##cond)); \
615 inst->inst_p1 = (char*)name; \
616 MONO_ADD_INS ((cfg)->cbb, inst); \
617 } while (0)
619 /* Branch support */
622 * Basic blocks have two numeric identifiers:
623 * dfn: Depth First Number
624 * block_num: unique ID assigned at bblock creation
626 #define NEW_BBLOCK(cfg,bblock) do { \
627 (bblock) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
628 (bblock)->block_num = cfg->num_bblocks++; \
629 } while (0)
631 #define ADD_BBLOCK(cfg,b) do { \
632 if ((b)->cil_code) {\
633 cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b); \
635 (b)->real_offset = cfg->real_offset; \
636 } while (0)
638 /* Emit a one-way conditional branch */
640 * The inst_false_bb field of the cond branch will not be set, the JIT code should be
641 * prepared to deal with this.
643 #ifdef DEBUG_EXTENDED_BBLOCKS
644 static int ccount = 0;
645 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
646 MonoInst *ins; \
647 MonoBasicBlock *falsebb; \
648 MONO_INST_NEW ((cfg), (ins), (op)); \
649 if ((op) == OP_BR) { \
650 NEW_BBLOCK ((cfg), falsebb); \
651 ins->inst_target_bb = (truebb); \
652 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
653 MONO_ADD_INS ((cfg)->cbb, ins); \
654 MONO_START_BB ((cfg), falsebb); \
655 } else { \
656 ccount ++; \
657 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
658 ins->inst_true_bb = (truebb); \
659 ins->inst_false_bb = NULL; \
660 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
661 MONO_ADD_INS ((cfg)->cbb, ins); \
662 if (getenv ("COUNT2") && ccount == atoi (getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
663 if (getenv ("COUNT2") && ccount < atoi (getenv ("COUNT2"))) { \
664 cfg->cbb->extended = TRUE; \
665 } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
667 } while (0)
668 #else
669 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
670 MonoInst *ins; \
671 MonoBasicBlock *falsebb; \
672 MONO_INST_NEW ((cfg), (ins), (op)); \
673 if ((op) == OP_BR) { \
674 NEW_BBLOCK ((cfg), falsebb); \
675 ins->inst_target_bb = (truebb); \
676 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
677 MONO_ADD_INS ((cfg)->cbb, ins); \
678 MONO_START_BB ((cfg), falsebb); \
679 } else { \
680 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
681 ins->inst_true_bb = (truebb); \
682 ins->inst_false_bb = NULL; \
683 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
684 MONO_ADD_INS ((cfg)->cbb, ins); \
685 if (!cfg->enable_extended_bblocks) { \
686 NEW_BBLOCK ((cfg), falsebb); \
687 ins->inst_false_bb = falsebb; \
688 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
689 MONO_START_BB ((cfg), falsebb); \
690 } else { \
691 cfg->cbb->extended = TRUE; \
694 } while (0)
695 #endif
697 /* Emit a two-way conditional branch */
698 #define MONO_EMIT_NEW_BRANCH_BLOCK2(cfg,op,truebb,falsebb) do { \
699 MonoInst *ins; \
700 MONO_INST_NEW ((cfg), (ins), (op)); \
701 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
702 ins->inst_true_bb = (truebb); \
703 ins->inst_false_bb = (falsebb); \
704 mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
705 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
706 MONO_ADD_INS ((cfg)->cbb, ins); \
707 } while (0)
709 #define MONO_START_BB(cfg, bblock) do { \
710 ADD_BBLOCK ((cfg), (bblock)); \
711 if (cfg->cbb->last_ins && MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins) && !cfg->cbb->last_ins->inst_false_bb) { \
712 cfg->cbb->last_ins->inst_false_bb = (bblock); \
713 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
714 } 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)))) \
715 mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
716 (cfg)->cbb->next_bb = (bblock); \
717 (cfg)->cbb = (bblock); \
718 } while (0)
720 /*Object Model related macros*/
722 #ifndef MONO_ARCH_EMIT_BOUNDS_CHECK
723 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) do { \
724 int _length_reg = alloc_ireg (cfg); \
725 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset); \
726 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, _length_reg, index_reg); \
727 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
728 } while (0)
729 #endif
731 /* cfg is the MonoCompile been used
732 * array_reg is the vreg holding the array object
733 * array_type is a struct (usually MonoArray or MonoString)
734 * array_length_field is the field in the previous struct with the length
735 * index_reg is the vreg holding the index
737 #define MONO_EMIT_BOUNDS_CHECK(cfg, array_reg, array_type, array_length_field, index_reg) do { \
738 if (!(cfg->opt & MONO_OPT_ABCREM)) { \
739 MONO_ARCH_EMIT_BOUNDS_CHECK ((cfg), (array_reg), G_STRUCT_OFFSET (array_type, array_length_field), (index_reg)); \
740 } else { \
741 MonoInst *ins; \
742 MONO_INST_NEW ((cfg), ins, OP_BOUNDS_CHECK); \
743 ins->sreg1 = array_reg; \
744 ins->sreg2 = index_reg; \
745 ins->inst_imm = G_STRUCT_OFFSET (array_type, array_length_field); \
746 MONO_ADD_INS ((cfg)->cbb, ins); \
747 (cfg)->flags |= MONO_CFG_HAS_ARRAY_ACCESS; \
748 (cfg)->cbb->has_array_access = TRUE; \
750 } while (0)
752 G_END_DECLS
754 #endif