2008-08-05 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_DOMAINCONST(cfg,dest) do { \
243                 if (cfg->opt & MONO_OPT_SHARED) { \
244                         /* avoid depending on undefined C behavior in sequence points */ \
245                         MonoInst* __domain_var = mono_get_domainvar (cfg); \
246                         NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
247                 } else { \
248                         NEW_PCONST (cfg, dest, (cfg)->domain); \
249                 } \
250         } while (0)
251
252 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
253
254 #define NEW_VARLOAD(cfg,dest,var,vartype) do { \
255         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
256                 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype));  \
257                 type_to_eval_stack_type ((cfg), (vartype), (dest));     \
258                 (dest)->klass = var->klass;     \
259                 (dest)->sreg1 = var->dreg;   \
260         (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
261         if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
262         } while (0)
263
264 #ifdef MONO_ARCH_SOFT_FLOAT
265 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8 || (stack_type) == STACK_R8)
266 #else
267 #define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8)
268 #endif
269
270 #define NEW_VARLOADA(cfg,dest,var,vartype) do { \
271         MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
272                 (dest)->inst_p0 = (var); \
273                 (var)->flags |= MONO_INST_INDIRECT;     \
274                 (dest)->type = STACK_MP;        \
275                 (dest)->klass = (var)->klass;   \
276         (dest)->dreg = alloc_dreg ((cfg), STACK_MP); \
277                 if (SIZEOF_VOID_P == 4 && DECOMPOSE_INTO_REGPAIR ((var)->type)) { MonoInst *var1 = get_vreg_to_inst (cfg, (var)->dreg + 1); MonoInst *var2 = get_vreg_to_inst (cfg, (var)->dreg + 2); g_assert (var1); g_assert (var2); var1->flags |= MONO_INST_INDIRECT; var2->flags |= MONO_INST_INDIRECT; } \
278         } while (0)
279
280 #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do {    \
281         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
282                 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype));    \
283                 (dest)->klass = (var)->klass;   \
284         (dest)->sreg1 = (inst)->dreg; \
285                 (dest)->dreg = (var)->dreg;   \
286         if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
287         } while (0)
288
289 #define NEW_TEMPLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype)
290
291 #define NEW_TEMPLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), cfg->varinfo [(num)], cfg->varinfo [(num)]->inst_vtype)
292
293 #define NEW_TEMPSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->varinfo [(num)], (cfg)->varinfo [(num)]->inst_vtype, (inst))
294
295 #define NEW_ARGLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
296
297 #define NEW_LOCLOAD(cfg,dest,num) NEW_VARLOAD ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
298
299 #define NEW_LOCSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
300
301 #define NEW_ARGSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), arg_array [(num)], param_types [(num)], (inst))
302
303 #define NEW_LOCLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype)
304
305 #define NEW_RETLOADA(cfg,dest) do {     \
306         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
307         (dest)->type = STACK_MP; \
308             (dest)->klass = cfg->ret->klass;    \
309             (dest)->sreg1 = cfg->vret_addr->dreg;   \
310         (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
311         } while (0)
312
313 #define NEW_ARGLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), arg_array [(num)], param_types [(num)])
314
315 /* Promote the vreg to a variable so its address can be taken */
316 #define NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { \
317         MonoInst *var = get_vreg_to_inst ((cfg), (vreg)); \
318         if (!var) \
319                     var = mono_compile_create_var_for_vreg ((cfg), (ltype), OP_LOCAL, (vreg)); \
320         NEW_VARLOADA ((cfg), (dest), (var), (ltype)); \
321     } while (0)
322
323 #define NEW_DUMMY_USE(cfg,dest,var) do { \
324         MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
325                 (dest)->sreg1 = var->dreg; \
326     } while (0)
327
328 /* Variants which take a type argument and handle vtypes as well */
329 #define NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { \
330             NEW_LOAD_MEMBASE ((cfg), (dest), mono_type_to_load_membase ((cfg), (ltype)), 0, (base), (offset)); \
331             type_to_eval_stack_type ((cfg), (ltype), (dest)); \
332             (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
333     } while (0)
334
335 #define NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { \
336         MONO_INST_NEW ((cfg), (dest), mono_type_to_store_membase ((cfg), (ltype))); \
337         (dest)->sreg1 = sr; \
338         (dest)->inst_destbasereg = base; \
339         (dest)->inst_offset = offset; \
340             type_to_eval_stack_type ((cfg), (ltype), (dest)); \
341         (dest)->klass = mono_class_from_mono_type (ltype); \
342         } while (0)
343
344 /*
345  * Variants which do an emit as well.
346  */
347 #define EMIT_NEW_ICONST(cfg,dest,val) do { NEW_ICONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
348
349 #define EMIT_NEW_PCONST(cfg,dest,val) do { NEW_PCONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
350
351 #define EMIT_NEW_I8CONST(cfg,dest,val) do { NEW_I8CONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
352
353 #define EMIT_NEW_AOTCONST(cfg,dest,patch_type,cons) do { NEW_AOTCONST ((cfg), (dest), (patch_type), (cons)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
354
355 #define EMIT_NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { NEW_AOTCONST_TOKEN ((cfg), (dest), (patch_type), (image), (token), (stack_type), (stack_class)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
356
357 #define EMIT_NEW_CLASSCONST(cfg,dest,val) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
358
359 #define EMIT_NEW_IMAGECONST(cfg,dest,val) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
360
361 #define EMIT_NEW_FIELDCONST(cfg,dest,val) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
362
363 #define EMIT_NEW_METHODCONST(cfg,dest,val) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
364
365 #define EMIT_NEW_VTABLECONST(cfg,dest,vtable) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
366
367 #define EMIT_NEW_SFLDACONST(cfg,dest,val) EMIT_NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
368
369 #define EMIT_NEW_LDSTRCONST(cfg,dest,image,token) EMIT_NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), STACK_OBJ, mono_defaults.string_class)
370
371 #define EMIT_NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) EMIT_NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_TYPE_FROM_HANDLE, (image), (token), STACK_OBJ, mono_defaults.monotype_class)
372
373 #define EMIT_NEW_LDTOKENCONST(cfg,dest,image,token) EMIT_NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), STACK_PTR, NULL)
374
375 #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
376
377 #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
378
379 #define EMIT_NEW_VARLOAD(cfg,dest,var,vartype) do { NEW_VARLOAD ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
380
381 #define EMIT_NEW_VARSTORE(cfg,dest,var,vartype,inst) do { NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
382
383 #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
384
385
386 #ifdef MONO_ARCH_SOFT_FLOAT
387
388 /*
389  * Since the IL stack (and our vregs) contain double values, we have to do a conversion
390  * when loading/storing args/locals of type R4.
391  */
392
393 #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
394         if (!(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
395              MonoInst *iargs [1]; \
396              EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
397              (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
398         } else { \
399              EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
400         } \
401     } while (0)
402
403 #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do {        \
404         if (!(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
405              MonoInst *iargs [2]; \
406              iargs [0] = (inst); \
407              EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
408              mono_emit_jit_icall (cfg, mono_fstore_r4, iargs); \
409         } else { \
410              EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
411         } \
412     } while (0)
413
414 #define EMIT_NEW_ARGLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), arg_array [(num)], param_types [(num)])
415
416 #define EMIT_NEW_LOCLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
417
418 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
419
420 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), arg_array [(num)], param_types [(num)], (inst))
421
422 #else
423
424 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { NEW_ARGLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
425
426 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { NEW_LOCLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
427
428 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { NEW_LOCSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
429
430 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
431
432 #endif
433
434 #define EMIT_NEW_TEMPLOAD(cfg,dest,num) do { NEW_TEMPLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
435
436 #define EMIT_NEW_TEMPLOADA(cfg,dest,num) do { NEW_TEMPLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
437
438 #define EMIT_NEW_LOCLOADA(cfg,dest,num) do { NEW_LOCLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
439
440 #define EMIT_NEW_ARGLOADA(cfg,dest,num) do { NEW_ARGLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
441
442 #define EMIT_NEW_RETLOADA(cfg,dest) do { NEW_RETLOADA ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
443
444 #define EMIT_NEW_TEMPSTORE(cfg,dest,num,inst) do { NEW_TEMPSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
445
446 #define EMIT_NEW_VARLOADA_VREG(cfg,dest,vreg,ltype) do { NEW_VARLOADA_VREG ((cfg), (dest), (vreg), (ltype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
447
448 #define EMIT_NEW_DUMMY_USE(cfg,dest,var) do { NEW_DUMMY_USE ((cfg), (dest), (var)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
449
450 #define EMIT_NEW_UNALU(cfg,dest,op,dr,sr1) do { NEW_UNALU ((cfg), (dest), (op), (dr), (sr1)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
451
452 #define EMIT_NEW_BIALU(cfg,dest,op,dr,sr1,sr2) do { NEW_BIALU ((cfg), (dest), (op), (dr), (sr1), (sr2)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
453
454 #define EMIT_NEW_BIALU_IMM(cfg,dest,op,dr,sr,imm) do { NEW_BIALU_IMM ((cfg), (dest), (op), (dr), (sr), (imm)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
455
456 #define EMIT_NEW_LOAD_MEMBASE(cfg,dest,op,dr,base,offset) do { NEW_LOAD_MEMBASE ((cfg), (dest), (op), (dr), (base), (offset)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
457
458 #define EMIT_NEW_STORE_MEMBASE(cfg,dest,op,base,offset,sr) do { NEW_STORE_MEMBASE ((cfg), (dest), (op), (base), (offset), (sr)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
459
460 #define EMIT_NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { NEW_LOAD_MEMBASE_TYPE ((cfg), (dest), (ltype), (base), (offset)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
461
462 #define EMIT_NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { NEW_STORE_MEMBASE_TYPE ((cfg), (dest), (ltype), (base), (offset), (sr)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
463
464 /*
465  * Variants which do not take an dest argument, but take a dreg argument.
466  */
467 #define MONO_EMIT_NEW_ICONST(cfg,dr,imm) do { \
468         MonoInst *inst; \
469         MONO_INST_NEW ((cfg), (inst), OP_ICONST); \
470         inst->dreg = dr; \
471         inst->inst_c0 = imm; \
472             MONO_ADD_INS ((cfg)->cbb, inst); \
473         } while (0)
474
475 #define MONO_EMIT_NEW_PCONST(cfg,dr,val) do {   \
476         MonoInst *inst; \
477         MONO_INST_NEW ((cfg), (inst), OP_PCONST); \
478         inst->dreg = dr; \
479                 (inst)->inst_p0 = (val);        \
480                 (inst)->type = STACK_PTR;       \
481             MONO_ADD_INS ((cfg)->cbb, inst); \
482         } while (0)
483
484 #define MONO_EMIT_NEW_I8CONST(cfg,dr,imm) do { \
485         MonoInst *inst; \
486         MONO_INST_NEW ((cfg), (inst), OP_I8CONST); \
487         inst->dreg = dr; \
488         inst->inst_l = imm; \
489             MONO_ADD_INS ((cfg)->cbb, inst); \
490         } while (0)
491
492 #ifdef MONO_ARCH_NEED_GOT_VAR
493
494 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
495         MonoInst *inst; \
496         NEW_AOTCONST ((cfg), (inst), (patch_type), (cons)); \
497         inst->dreg = (dr); \
498         MONO_ADD_INS ((cfg)->cbb, inst); \
499     } while (0)
500
501 #else
502
503 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,type) do { \
504         MonoInst *inst; \
505         MONO_INST_NEW ((cfg), (inst), OP_AOTCONST); \
506         inst->dreg = dr; \
507         inst->inst_p0 = imm; \
508         inst->inst_c1 = type; \
509                 MONO_ADD_INS ((cfg)->cbb, inst); \
510         } while (0)
511
512 #endif
513
514 #define MONO_EMIT_NEW_CLASSCONST(cfg,dr,imm) MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,MONO_PATCH_INFO_CLASS)
515 #define MONO_EMIT_NEW_VTABLECONST(cfg,dest,vtable) MONO_EMIT_NEW_AOTCONST ((cfg), (dest), (cfg)->compile_aot ? (gpointer)((vtable)->klass) : (vtable), MONO_PATCH_INFO_VTABLE)
516
517 #define MONO_EMIT_NEW_VZERO(cfg,dr,kl) do {     \
518         MonoInst *inst; \
519         MONO_INST_NEW ((cfg), (inst), OP_VZERO); \
520         inst->dreg = dr; \
521                 (inst)->type = STACK_VTYPE;     \
522         (inst)->klass = (kl); \
523             MONO_ADD_INS ((cfg)->cbb, inst); \
524         } while (0)
525
526 #define MONO_EMIT_NEW_UNALU(cfg,op,dr,sr1) do { \
527         MonoInst *inst; \
528         EMIT_NEW_UNALU ((cfg), (inst), (op), (dr), (sr1)); \
529         } while (0)
530
531 #define MONO_EMIT_NEW_BIALU(cfg,op,dr,sr1,sr2) do { \
532         MonoInst *inst; \
533         MONO_INST_NEW ((cfg), (inst), (op)); \
534         inst->dreg = dr; \
535         inst->sreg1 = sr1; \
536         inst->sreg2 = sr2; \
537             MONO_ADD_INS (cfg->cbb, inst); \
538         } while (0)
539
540 #define MONO_EMIT_NEW_BIALU_IMM(cfg,op,dr,sr,imm) do { \
541         MonoInst *inst; \
542         MONO_INST_NEW ((cfg), (inst), (op)); \
543         inst->dreg = dr; \
544         inst->sreg1 = sr; \
545         inst->inst_p1 = (gpointer)(gssize)(imm); \
546             MONO_ADD_INS (cfg->cbb, inst); \
547         } while (0)
548
549 #define MONO_EMIT_NEW_COMPARE_IMM(cfg,sr1,imm) do { \
550         MonoInst *inst; \
551         MONO_INST_NEW ((cfg), (inst), (OP_COMPARE_IMM)); \
552         inst->sreg1 = sr1; \
553         inst->inst_p1 = (gpointer)imm; \
554             MONO_ADD_INS ((cfg)->cbb, inst); \
555         } while (0)
556
557 #define MONO_EMIT_NEW_ICOMPARE_IMM(cfg,sr1,imm) do { \
558         MonoInst *inst; \
559         MONO_INST_NEW ((cfg), (inst), sizeof (void*) == 8 ? OP_ICOMPARE_IMM : OP_COMPARE_IMM); \
560         inst->sreg1 = sr1; \
561         inst->inst_p1 = (gpointer)imm; \
562             MONO_ADD_INS ((cfg)->cbb, inst); \
563         } while (0)
564
565 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg,op,dr,base,offset) do { \
566         MonoInst *inst; \
567         MONO_INST_NEW ((cfg), (inst), (op)); \
568         inst->dreg = dr; \
569         inst->inst_basereg = base; \
570         inst->inst_offset = offset; \
571             MONO_ADD_INS (cfg->cbb, inst); \
572     } while (0)
573
574 #define MONO_EMIT_NEW_LOAD_MEMBASE(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
575
576 #define MONO_EMIT_NEW_STORE_MEMBASE(cfg,op,base,offset,sr) do { \
577         MonoInst *inst; \
578         MONO_INST_NEW ((cfg), (inst), (op)); \
579         (inst)->sreg1 = sr; \
580         (inst)->inst_destbasereg = base; \
581         (inst)->inst_offset = offset; \
582             MONO_ADD_INS (cfg->cbb, inst); \
583         } while (0)
584
585 #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
586         MonoInst *inst; \
587         MONO_INST_NEW ((cfg), (inst), (op)); \
588         inst->inst_destbasereg = base; \
589         inst->inst_offset = offset; \
590         inst->inst_p1 = (gpointer)(gssize)imm; \
591         MONO_ADD_INS ((cfg)->cbb, inst); \
592         } while (0)
593
594 #define MONO_EMIT_NEW_COND_EXC(cfg,cond,name) do { \
595         MonoInst *inst; \
596         MONO_INST_NEW ((cfg), (inst), (OP_COND_EXC_##cond)); \
597         inst->inst_p1 = (char*)name; \
598             MONO_ADD_INS ((cfg)->cbb, inst); \
599         } while (0)
600
601 /* Branch support */
602
603 /*
604  * Basic blocks have two numeric identifiers:
605  * dfn: Depth First Number
606  * block_num: unique ID assigned at bblock creation
607  */
608 #define NEW_BBLOCK(cfg,bblock) do { \
609         (bblock) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
610         (bblock)->block_num = cfg->num_bblocks++; \
611     } while (0)
612
613 #define ADD_BBLOCK(cfg,b) do {  \
614         if ((b)->cil_code)  {\
615                         cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b);   \
616         } \
617                 (b)->real_offset = cfg->real_offset;    \
618         } while (0)
619
620 /* Emit a one-way conditional branch */
621 /* 
622  * The inst_false_bb field of the cond branch will not be set, the JIT code should be
623  * prepared to deal with this.
624  */
625 #ifdef DEBUG_EXTENDED_BBLOCKS
626 static int ccount = 0;
627 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
628         MonoInst *ins; \
629         MonoBasicBlock *falsebb; \
630         MONO_INST_NEW ((cfg), (ins), (op)); \
631         if ((op) == OP_BR) { \
632                 NEW_BBLOCK ((cfg), falsebb); \
633             ins->inst_target_bb = (truebb); \
634             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
635             MONO_ADD_INS ((cfg)->cbb, ins); \
636             MONO_START_BB ((cfg), falsebb); \
637         } else { \
638             ccount ++; \
639                     ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);  \
640             ins->inst_true_bb = (truebb); \
641             ins->inst_false_bb = NULL; \
642             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
643             MONO_ADD_INS ((cfg)->cbb, ins); \
644             if (getenv ("COUNT2") && ccount == atoi (getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
645             if (getenv ("COUNT2") && ccount < atoi (getenv ("COUNT2"))) { \
646                  cfg->cbb->extended = TRUE; \
647             } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
648         } \
649         } while (0)
650 #else
651 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
652         MonoInst *ins; \
653         MonoBasicBlock *falsebb; \
654         MONO_INST_NEW ((cfg), (ins), (op)); \
655         if ((op) == OP_BR) { \
656                 NEW_BBLOCK ((cfg), falsebb); \
657             ins->inst_target_bb = (truebb); \
658             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
659             MONO_ADD_INS ((cfg)->cbb, ins); \
660             MONO_START_BB ((cfg), falsebb); \
661         } else { \
662                     ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);  \
663             ins->inst_true_bb = (truebb); \
664             ins->inst_false_bb = NULL; \
665             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
666             MONO_ADD_INS ((cfg)->cbb, ins); \
667             if (!cfg->enable_extended_bblocks) { \
668                 NEW_BBLOCK ((cfg), falsebb); \
669                 ins->inst_false_bb = falsebb; \
670                 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
671                 MONO_START_BB ((cfg), falsebb); \
672             } else { \
673                                 cfg->cbb->extended = TRUE; \
674                         } \
675         } \
676         } while (0)
677 #endif
678
679 /* Emit a two-way conditional branch */
680 #define MONO_EMIT_NEW_BRANCH_BLOCK2(cfg,op,truebb,falsebb) do { \
681         MonoInst *ins; \
682         MONO_INST_NEW ((cfg), (ins), (op)); \
683                 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);      \
684         ins->inst_true_bb = (truebb); \
685         ins->inst_false_bb = (falsebb); \
686         mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
687         mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
688         MONO_ADD_INS ((cfg)->cbb, ins); \
689         } while (0)
690
691 #define MONO_START_BB(cfg, bblock) do { \
692         ADD_BBLOCK ((cfg), (bblock)); \
693         if (cfg->cbb->last_ins && MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins) && !cfg->cbb->last_ins->inst_false_bb) { \
694             cfg->cbb->last_ins->inst_false_bb = (bblock); \
695             mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
696         } else if (! (cfg->cbb->last_ins && ((cfg->cbb->last_ins->opcode == OP_BR) || (cfg->cbb->last_ins->opcode == OP_BR_REG) || MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins)))) \
697             mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
698             (cfg)->cbb->next_bb = (bblock); \
699             (cfg)->cbb = (bblock); \
700     } while (0)
701
702 G_END_DECLS
703
704 #endif