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