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