2008-08-18 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / ir-emit.h
1 /*
2  * ir-emit.h: IR Creation/Emission Macros
3  *
4  * Author:
5  *   Zoltan Varga (vargaz@gmail.com)
6  *
7  * (C) 2002 Ximian, Inc.
8  */
9
10 #ifndef __MONO_IR_EMIT_H__
11 #define __MONO_IR_EMIT_H__
12
13 #include "mini.h"
14
15 G_BEGIN_DECLS
16
17 static inline guint32
18 alloc_ireg (MonoCompile *cfg)
19 {
20         return cfg->next_vreg ++;
21 }
22
23 static inline guint32
24 alloc_preg (MonoCompile *cfg)
25 {
26         return alloc_ireg (cfg);
27 }
28
29 static inline guint32
30 alloc_lreg (MonoCompile *cfg)
31 {
32 #if SIZEOF_VOID_P == 8
33         return cfg->next_vreg ++;
34 #else
35         /* Use a pair of consecutive vregs */
36         guint32 res = cfg->next_vreg;
37
38         cfg->next_vreg += 3;
39
40         return res;
41 #endif
42 }
43
44 static inline guint32
45 alloc_freg (MonoCompile *cfg)
46 {
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
54 }
55
56 static inline guint32
57 alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
58 {
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_assert_not_reached ();
73         }
74 }
75
76 #undef MONO_INST_NEW
77 /* 
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.
80  */
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);  \
85         (dest)->flags = 0; \
86         (dest)->type = 0; \
87         (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1;  \
88         (dest)->cil_code = (cfg)->ip;  \
89         } while (0)
90
91 /*
92  * Variants which take a dest argument and don't do an emit
93  */
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);    \
99         } while (0)
100
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);   \
106         } while (0)
107
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); \
113         } while (0)
114
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; \
120         } while (0)
121
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; \
128         } while (0)
129
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; \
135         } while (0)
136
137 #define NEW_UNALU(cfg,dest,op,dr,sr1) do { \
138         MONO_INST_NEW ((cfg), (dest), (op)); \
139         (dest)->dreg = dr; \
140         (dest)->sreg1 = sr1; \
141     } while (0)        
142
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); \
148         } while (0)
149
150 #define NEW_BIALU_IMM(cfg,dest,op,dr,sr,imm) do { \
151         MONO_INST_NEW ((cfg), (dest), (op)); \
152         (dest)->dreg = dr; \
153         (dest)->sreg1 = sr; \
154         (dest)->inst_p1 = (gpointer)(gssize)(imm); \
155         } while (0)
156
157 #ifdef MONO_ARCH_NEED_GOT_VAR
158
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);   \
163         } while (0)
164
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;                        \
174                 } else {                                                \
175                         (dest)->inst_p0 = (cons);                       \
176                         (dest)->inst_i1 = (gpointer)(patch_type);       \
177                 }                                                       \
178                 (dest)->type = STACK_PTR;                               \
179                 (dest)->dreg = alloc_dreg ((cfg), STACK_PTR);   \
180         } while (0)
181
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));        \
193         } while (0)
194
195 #else
196
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);   \
203     } while (0)
204
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));        \
212     } while (0)
213
214 #endif
215
216 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
217
218 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
219
220 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
221
222 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
223
224 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
225
226 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
227
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)
229
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)
231
232 #define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), STACK_PTR, NULL)
233
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); \
237                 } else { \
238                         NEW_PCONST (cfg, args [0], (entry).blob); \
239                 } \
240         } while (0)
241
242 #define NEW_METHOD_RGCTX_CONST(cfg,dest,method) do { \
243                 if (cfg->compile_aot) {                                                                                 \
244                         NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHOD_RGCTX, (method)); \
245                 } else {                                                                                                                \
246                         MonoMethodRuntimeGenericContext *mrgctx;                                        \
247                         mrgctx = mono_method_lookup_rgctx (mono_class_vtable ((cfg)->domain, (method)->klass), mini_method_get_context ((method))->method_inst); \
248                         NEW_PCONST ((cfg), (dest), (mrgctx));                                           \
249                 }                                                                                                                               \
250         } while (0)
251
252 #define NEW_DOMAINCONST(cfg,dest) do { \
253                 if (cfg->opt & MONO_OPT_SHARED) { \
254                         /* avoid depending on undefined C behavior in sequence points */ \
255                         MonoInst* __domain_var = mono_get_domainvar (cfg); \
256                         NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
257                 } else { \
258                         NEW_PCONST (cfg, dest, (cfg)->domain); \
259                 } \
260         } while (0)
261
262 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
263
264 #define NEW_VARLOAD(cfg,dest,var,vartype) do { \
265         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
266                 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype));  \
267                 type_to_eval_stack_type ((cfg), (vartype), (dest));     \
268                 (dest)->klass = var->klass;     \
269                 (dest)->sreg1 = var->dreg;   \
270         (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
271         if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
272         } while (0)
273
274 #ifdef MONO_ARCH_SOFT_FLOAT
275 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8 || (stack_type) == STACK_R8)
276 #else
277 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8)
278 #endif
279
280 #define NEW_VARLOADA(cfg,dest,var,vartype) do { \
281         MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
282                 (dest)->inst_p0 = (var); \
283                 (var)->flags |= MONO_INST_INDIRECT;     \
284                 (dest)->type = STACK_MP;        \
285                 (dest)->klass = (var)->klass;   \
286         (dest)->dreg = alloc_dreg ((cfg), STACK_MP); \
287                 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; } \
288         } while (0)
289
290 #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do {    \
291         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
292                 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype));    \
293                 (dest)->klass = (var)->klass;   \
294         (dest)->sreg1 = (inst)->dreg; \
295                 (dest)->dreg = (var)->dreg;   \
296         if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
297         } while (0)
298
299 #define NEW_TEMPLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype)
300
301 #define NEW_TEMPLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), cfg->varinfo [(num)], cfg->varinfo [(num)]->inst_vtype)
302
303 #define NEW_TEMPSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype, (inst))
304
305 #define NEW_ARGLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
306
307 #define NEW_LOCLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
308
309 #define NEW_LOCSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
310
311 #define NEW_ARGSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), arg_array [(num)], param_types [(num)], (inst))
312
313 #define NEW_LOCLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype)
314
315 #define NEW_RETLOADA(cfg,dest) do {     \
316         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
317         (dest)->type = STACK_MP; \
318             (dest)->klass = cfg->ret->klass;    \
319             (dest)->sreg1 = cfg->vret_addr->dreg;   \
320         (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
321         } while (0)
322
323 #define NEW_ARGLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), arg_array [(num)], param_types [(num)])
324
325 /* Promote the vreg to a variable so its address can be taken */
326 #define NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { \
327         MonoInst *var = get_vreg_to_inst ((cfg), (vreg)); \
328         if (!var) \
329                     var = mono_compile_create_var_for_vreg ((cfg), (ltype), OP_LOCAL, (vreg)); \
330         NEW_VARLOADA ((cfg), (dest), (var), (ltype)); \
331     } while (0)
332
333 #define NEW_DUMMY_USE(cfg,dest,var) do { \
334         MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
335                 (dest)->sreg1 = var->dreg; \
336     } while (0)
337
338 /* Variants which take a type argument and handle vtypes as well */
339 #define NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { \
340             NEW_LOAD_MEMBASE ((cfg), (dest), mono_type_to_load_membase ((cfg), (ltype)), 0, (base), (offset)); \
341             type_to_eval_stack_type ((cfg), (ltype), (dest)); \
342             (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
343     } while (0)
344
345 #define NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { \
346         MONO_INST_NEW ((cfg), (dest), mono_type_to_store_membase ((cfg), (ltype))); \
347         (dest)->sreg1 = sr; \
348         (dest)->inst_destbasereg = base; \
349         (dest)->inst_offset = offset; \
350             type_to_eval_stack_type ((cfg), (ltype), (dest)); \
351         (dest)->klass = mono_class_from_mono_type (ltype); \
352         } while (0)
353
354 /*
355  * Variants which do an emit as well.
356  */
357 #define EMIT_NEW_ICONST(cfg,dest,val) do { NEW_ICONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
358
359 #define EMIT_NEW_PCONST(cfg,dest,val) do { NEW_PCONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
360
361 #define EMIT_NEW_I8CONST(cfg,dest,val) do { NEW_I8CONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
362
363 #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)
364
365 #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)
366
367 #define EMIT_NEW_CLASSCONST(cfg,dest,val) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
368
369 #define EMIT_NEW_IMAGECONST(cfg,dest,val) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
370
371 #define EMIT_NEW_FIELDCONST(cfg,dest,val) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
372
373 #define EMIT_NEW_METHODCONST(cfg,dest,val) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
374
375 #define EMIT_NEW_VTABLECONST(cfg,dest,vtable) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
376
377 #define EMIT_NEW_SFLDACONST(cfg,dest,val) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
378
379 #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)
380
381 #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)
382
383 #define EMIT_NEW_LDTOKENCONST(cfg,dest,image,token) EMIT_NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), STACK_PTR, NULL)
384
385 #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
386
387 #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
388
389 #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)
390
391 #define EMIT_NEW_VARLOAD(cfg,dest,var,vartype) do { NEW_VARLOAD ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
392
393 #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)
394
395 #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
396
397
398 #ifdef MONO_ARCH_SOFT_FLOAT
399
400 /*
401  * Since the IL stack (and our vregs) contain double values, we have to do a conversion
402  * when loading/storing args/locals of type R4.
403  */
404
405 #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
406         if (!(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
407              MonoInst *iargs [1]; \
408              EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
409              (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
410         } else { \
411              EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
412         } \
413     } while (0)
414
415 #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do {        \
416         if (!(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
417              MonoInst *iargs [2]; \
418              iargs [0] = (inst); \
419              EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
420              mono_emit_jit_icall (cfg, mono_fstore_r4, iargs); \
421         } else { \
422              EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
423         } \
424     } while (0)
425
426 #define EMIT_NEW_ARGLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
427
428 #define EMIT_NEW_LOCLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
429
430 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
431
432 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
433
434 #else
435
436 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { NEW_ARGLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
437
438 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { NEW_LOCLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
439
440 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { NEW_LOCSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
441
442 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
443
444 #endif
445
446 #define EMIT_NEW_TEMPLOAD(cfg,dest,num) do { NEW_TEMPLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
447
448 #define EMIT_NEW_TEMPLOADA(cfg,dest,num) do { NEW_TEMPLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
449
450 #define EMIT_NEW_LOCLOADA(cfg,dest,num) do { NEW_LOCLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
451
452 #define EMIT_NEW_ARGLOADA(cfg,dest,num) do { NEW_ARGLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
453
454 #define EMIT_NEW_RETLOADA(cfg,dest) do { NEW_RETLOADA ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
455
456 #define EMIT_NEW_TEMPSTORE(cfg,dest,num,inst) do { NEW_TEMPSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
457
458 #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)
459
460 #define EMIT_NEW_DUMMY_USE(cfg,dest,var) do { NEW_DUMMY_USE ((cfg), (dest), (var)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
461
462 #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)
463
464 #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)
465
466 #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)
467
468 #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)
469
470 #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)
471
472 #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)
473
474 #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)
475
476 /*
477  * Variants which do not take an dest argument, but take a dreg argument.
478  */
479 #define MONO_EMIT_NEW_ICONST(cfg,dr,imm) do { \
480         MonoInst *inst; \
481         MONO_INST_NEW ((cfg), (inst), OP_ICONST); \
482         inst->dreg = dr; \
483         inst->inst_c0 = imm; \
484             MONO_ADD_INS ((cfg)->cbb, inst); \
485         } while (0)
486
487 #define MONO_EMIT_NEW_PCONST(cfg,dr,val) do {   \
488         MonoInst *inst; \
489         MONO_INST_NEW ((cfg), (inst), OP_PCONST); \
490         inst->dreg = dr; \
491                 (inst)->inst_p0 = (val);        \
492                 (inst)->type = STACK_PTR;       \
493             MONO_ADD_INS ((cfg)->cbb, inst); \
494         } while (0)
495
496 #define MONO_EMIT_NEW_I8CONST(cfg,dr,imm) do { \
497         MonoInst *inst; \
498         MONO_INST_NEW ((cfg), (inst), OP_I8CONST); \
499         inst->dreg = dr; \
500         inst->inst_l = imm; \
501             MONO_ADD_INS ((cfg)->cbb, inst); \
502         } while (0)
503
504 #ifdef MONO_ARCH_NEED_GOT_VAR
505
506 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
507         MonoInst *inst; \
508         NEW_AOTCONST ((cfg), (inst), (patch_type), (cons)); \
509         inst->dreg = (dr); \
510         MONO_ADD_INS ((cfg)->cbb, inst); \
511     } while (0)
512
513 #else
514
515 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,type) do { \
516         MonoInst *inst; \
517         MONO_INST_NEW ((cfg), (inst), OP_AOTCONST); \
518         inst->dreg = dr; \
519         inst->inst_p0 = imm; \
520         inst->inst_c1 = type; \
521                 MONO_ADD_INS ((cfg)->cbb, inst); \
522         } while (0)
523
524 #endif
525
526 #define MONO_EMIT_NEW_CLASSCONST(cfg,dr,imm) MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,MONO_PATCH_INFO_CLASS)
527 #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)
528
529 #define MONO_EMIT_NEW_VZERO(cfg,dr,kl) do {     \
530         MonoInst *inst; \
531         MONO_INST_NEW ((cfg), (inst), OP_VZERO); \
532         inst->dreg = dr; \
533                 (inst)->type = STACK_VTYPE;     \
534         (inst)->klass = (kl); \
535             MONO_ADD_INS ((cfg)->cbb, inst); \
536         } while (0)
537
538 #define MONO_EMIT_NEW_UNALU(cfg,op,dr,sr1) do { \
539         MonoInst *inst; \
540         EMIT_NEW_UNALU ((cfg), (inst), (op), (dr), (sr1)); \
541         } while (0)
542
543 #define MONO_EMIT_NEW_BIALU(cfg,op,dr,sr1,sr2) do { \
544         MonoInst *inst; \
545         MONO_INST_NEW ((cfg), (inst), (op)); \
546         inst->dreg = dr; \
547         inst->sreg1 = sr1; \
548         inst->sreg2 = sr2; \
549             MONO_ADD_INS (cfg->cbb, inst); \
550         } while (0)
551
552 #define MONO_EMIT_NEW_BIALU_IMM(cfg,op,dr,sr,imm) do { \
553         MonoInst *inst; \
554         MONO_INST_NEW ((cfg), (inst), (op)); \
555         inst->dreg = dr; \
556         inst->sreg1 = sr; \
557         inst->inst_p1 = (gpointer)(gssize)(imm); \
558             MONO_ADD_INS (cfg->cbb, inst); \
559         } while (0)
560
561 #define MONO_EMIT_NEW_COMPARE_IMM(cfg,sr1,imm) do { \
562         MonoInst *inst; \
563         MONO_INST_NEW ((cfg), (inst), (OP_COMPARE_IMM)); \
564         inst->sreg1 = sr1; \
565         inst->inst_p1 = (gpointer)imm; \
566             MONO_ADD_INS ((cfg)->cbb, inst); \
567         } while (0)
568
569 #define MONO_EMIT_NEW_ICOMPARE_IMM(cfg,sr1,imm) do { \
570         MonoInst *inst; \
571         MONO_INST_NEW ((cfg), (inst), sizeof (void*) == 8 ? OP_ICOMPARE_IMM : OP_COMPARE_IMM); \
572         inst->sreg1 = sr1; \
573         inst->inst_p1 = (gpointer)imm; \
574             MONO_ADD_INS ((cfg)->cbb, inst); \
575         } while (0)
576
577 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg,op,dr,base,offset) do { \
578         MonoInst *inst; \
579         MONO_INST_NEW ((cfg), (inst), (op)); \
580         inst->dreg = dr; \
581         inst->inst_basereg = base; \
582         inst->inst_offset = offset; \
583             MONO_ADD_INS (cfg->cbb, inst); \
584     } while (0)
585
586 #define MONO_EMIT_NEW_LOAD_MEMBASE(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
587
588 #define MONO_EMIT_NEW_STORE_MEMBASE(cfg,op,base,offset,sr) do { \
589         MonoInst *inst; \
590         MONO_INST_NEW ((cfg), (inst), (op)); \
591         (inst)->sreg1 = sr; \
592         (inst)->inst_destbasereg = base; \
593         (inst)->inst_offset = offset; \
594             MONO_ADD_INS (cfg->cbb, inst); \
595         } while (0)
596
597 #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
598         MonoInst *inst; \
599         MONO_INST_NEW ((cfg), (inst), (op)); \
600         inst->inst_destbasereg = base; \
601         inst->inst_offset = offset; \
602         inst->inst_p1 = (gpointer)(gssize)imm; \
603         MONO_ADD_INS ((cfg)->cbb, inst); \
604         } while (0)
605
606 #define MONO_EMIT_NEW_COND_EXC(cfg,cond,name) do { \
607         MonoInst *inst; \
608         MONO_INST_NEW ((cfg), (inst), (OP_COND_EXC_##cond)); \
609         inst->inst_p1 = (char*)name; \
610             MONO_ADD_INS ((cfg)->cbb, inst); \
611         } while (0)
612
613 /* Branch support */
614
615 /*
616  * Basic blocks have two numeric identifiers:
617  * dfn: Depth First Number
618  * block_num: unique ID assigned at bblock creation
619  */
620 #define NEW_BBLOCK(cfg,bblock) do { \
621         (bblock) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
622         (bblock)->block_num = cfg->num_bblocks++; \
623     } while (0)
624
625 #define ADD_BBLOCK(cfg,b) do {  \
626         if ((b)->cil_code)  {\
627                         cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b);   \
628         } \
629                 (b)->real_offset = cfg->real_offset;    \
630         } while (0)
631
632 /* Emit a one-way conditional branch */
633 /* 
634  * The inst_false_bb field of the cond branch will not be set, the JIT code should be
635  * prepared to deal with this.
636  */
637 #ifdef DEBUG_EXTENDED_BBLOCKS
638 static int ccount = 0;
639 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
640         MonoInst *ins; \
641         MonoBasicBlock *falsebb; \
642         MONO_INST_NEW ((cfg), (ins), (op)); \
643         if ((op) == OP_BR) { \
644                 NEW_BBLOCK ((cfg), falsebb); \
645             ins->inst_target_bb = (truebb); \
646             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
647             MONO_ADD_INS ((cfg)->cbb, ins); \
648             MONO_START_BB ((cfg), falsebb); \
649         } else { \
650             ccount ++; \
651                     ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);  \
652             ins->inst_true_bb = (truebb); \
653             ins->inst_false_bb = NULL; \
654             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
655             MONO_ADD_INS ((cfg)->cbb, ins); \
656             if (getenv ("COUNT2") && ccount == atoi (getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
657             if (getenv ("COUNT2") && ccount < atoi (getenv ("COUNT2"))) { \
658                  cfg->cbb->extended = TRUE; \
659             } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
660         } \
661         } while (0)
662 #else
663 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
664         MonoInst *ins; \
665         MonoBasicBlock *falsebb; \
666         MONO_INST_NEW ((cfg), (ins), (op)); \
667         if ((op) == OP_BR) { \
668                 NEW_BBLOCK ((cfg), falsebb); \
669             ins->inst_target_bb = (truebb); \
670             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
671             MONO_ADD_INS ((cfg)->cbb, ins); \
672             MONO_START_BB ((cfg), falsebb); \
673         } else { \
674                     ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);  \
675             ins->inst_true_bb = (truebb); \
676             ins->inst_false_bb = NULL; \
677             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
678             MONO_ADD_INS ((cfg)->cbb, ins); \
679             if (!cfg->enable_extended_bblocks) { \
680                 NEW_BBLOCK ((cfg), falsebb); \
681                 ins->inst_false_bb = falsebb; \
682                 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
683                 MONO_START_BB ((cfg), falsebb); \
684             } else { \
685                                 cfg->cbb->extended = TRUE; \
686                         } \
687         } \
688         } while (0)
689 #endif
690
691 /* Emit a two-way conditional branch */
692 #define MONO_EMIT_NEW_BRANCH_BLOCK2(cfg,op,truebb,falsebb) do { \
693         MonoInst *ins; \
694         MONO_INST_NEW ((cfg), (ins), (op)); \
695                 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);      \
696         ins->inst_true_bb = (truebb); \
697         ins->inst_false_bb = (falsebb); \
698         mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
699         mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
700         MONO_ADD_INS ((cfg)->cbb, ins); \
701         } while (0)
702
703 #define MONO_START_BB(cfg, bblock) do { \
704         ADD_BBLOCK ((cfg), (bblock)); \
705         if (cfg->cbb->last_ins && MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins) && !cfg->cbb->last_ins->inst_false_bb) { \
706             cfg->cbb->last_ins->inst_false_bb = (bblock); \
707             mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
708         } 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)))) \
709             mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
710             (cfg)->cbb->next_bb = (bblock); \
711             (cfg)->cbb = (bblock); \
712     } while (0)
713
714 G_END_DECLS
715
716 #endif