2009-11-23 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_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_imm = (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 #define NEW_SEQ_POINT(cfg,dest,il_offset,ss_loc) do {    \
363         MONO_INST_NEW ((cfg), (dest), OP_SEQ_POINT); \
364         (dest)->inst_imm = (il_offset); \
365         (dest)->flags = ss_loc ? MONO_INST_SINGLE_STEP_LOC : 0; \
366         } while (0)
367
368 /*
369  * Variants which do an emit as well.
370  */
371 #define EMIT_NEW_ICONST(cfg,dest,val) do { NEW_ICONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
372
373 #define EMIT_NEW_PCONST(cfg,dest,val) do { NEW_PCONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
374
375 #define EMIT_NEW_I8CONST(cfg,dest,val) do { NEW_I8CONST ((cfg), (dest), (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
376
377 #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)
378
379 #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)
380
381 #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)
382
383 #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)
384
385 #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)
386
387 #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)
388
389 #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)
390
391 #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)
392
393 #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)
394
395 #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)
396
397 #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)
398
399 #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
400
401 #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
402
403 #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)
404
405 #define EMIT_NEW_VARLOAD(cfg,dest,var,vartype) do { NEW_VARLOAD ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
406
407 #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)
408
409 #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
410
411
412 #ifdef MONO_ARCH_SOFT_FLOAT
413
414 /*
415  * Since the IL stack (and our vregs) contain double values, we have to do a conversion
416  * when loading/storing args/locals of type R4.
417  */
418
419 #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
420         if (!(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
421              MonoInst *iargs [1]; \
422              EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
423              (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
424         } else { \
425              EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
426         } \
427     } while (0)
428
429 #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do {        \
430         if (!(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
431              MonoInst *iargs [2]; \
432              iargs [0] = (inst); \
433              EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
434              mono_emit_jit_icall (cfg, mono_fstore_r4, iargs); \
435         } else { \
436              EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
437         } \
438     } while (0)
439
440 #define EMIT_NEW_ARGLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
441
442 #define EMIT_NEW_LOCLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
443
444 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
445
446 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
447
448 #else
449
450 #define EMIT_NEW_ARGLOAD(cfg,dest,num) do { NEW_ARGLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
451
452 #define EMIT_NEW_LOCLOAD(cfg,dest,num) do { NEW_LOCLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
453
454 #define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do { NEW_LOCSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
455
456 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
457
458 #endif
459
460 #define EMIT_NEW_TEMPLOAD(cfg,dest,num) do { NEW_TEMPLOAD ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
461
462 #define EMIT_NEW_TEMPLOADA(cfg,dest,num) do { NEW_TEMPLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
463
464 #define EMIT_NEW_LOCLOADA(cfg,dest,num) do { NEW_LOCLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
465
466 #define EMIT_NEW_ARGLOADA(cfg,dest,num) do { NEW_ARGLOADA ((cfg), (dest), (num)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
467
468 #define EMIT_NEW_RETLOADA(cfg,dest) do { NEW_RETLOADA ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
469
470 #define EMIT_NEW_TEMPSTORE(cfg,dest,num,inst) do { NEW_TEMPSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
471
472 #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)
473
474 #define EMIT_NEW_DUMMY_USE(cfg,dest,var) do { NEW_DUMMY_USE ((cfg), (dest), (var)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
475
476 #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)
477
478 #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)
479
480 #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)
481
482 #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)
483
484 #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)
485
486 #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)
487
488 #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)
489
490 /*
491  * Variants which do not take an dest argument, but take a dreg argument.
492  */
493 #define MONO_EMIT_NEW_ICONST(cfg,dr,imm) do { \
494         MonoInst *inst; \
495         MONO_INST_NEW ((cfg), (inst), OP_ICONST); \
496         inst->dreg = dr; \
497         inst->inst_c0 = imm; \
498             MONO_ADD_INS ((cfg)->cbb, inst); \
499         } while (0)
500
501 #define MONO_EMIT_NEW_PCONST(cfg,dr,val) do {   \
502         MonoInst *inst; \
503         MONO_INST_NEW ((cfg), (inst), OP_PCONST); \
504         inst->dreg = dr; \
505                 (inst)->inst_p0 = (val);        \
506                 (inst)->type = STACK_PTR;       \
507             MONO_ADD_INS ((cfg)->cbb, inst); \
508         } while (0)
509
510 #define MONO_EMIT_NEW_I8CONST(cfg,dr,imm) do { \
511         MonoInst *inst; \
512         MONO_INST_NEW ((cfg), (inst), OP_I8CONST); \
513         inst->dreg = dr; \
514         inst->inst_l = imm; \
515             MONO_ADD_INS ((cfg)->cbb, inst); \
516         } while (0)
517
518 #ifdef MONO_ARCH_NEED_GOT_VAR
519
520 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
521         MonoInst *inst; \
522         NEW_AOTCONST ((cfg), (inst), (patch_type), (cons)); \
523         inst->dreg = (dr); \
524         MONO_ADD_INS ((cfg)->cbb, inst); \
525     } while (0)
526
527 #else
528
529 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,type) do { \
530         MonoInst *inst; \
531         MONO_INST_NEW ((cfg), (inst), OP_AOTCONST); \
532         inst->dreg = dr; \
533         inst->inst_p0 = imm; \
534         inst->inst_c1 = type; \
535                 MONO_ADD_INS ((cfg)->cbb, inst); \
536         } while (0)
537
538 #endif
539
540 #define MONO_EMIT_NEW_CLASSCONST(cfg,dr,imm) MONO_EMIT_NEW_AOTCONST(cfg,dr,imm,MONO_PATCH_INFO_CLASS)
541 #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)
542
543 #define MONO_EMIT_NEW_VZERO(cfg,dr,kl) do {     \
544         MonoInst *inst; \
545         MONO_INST_NEW ((cfg), (inst), MONO_CLASS_IS_SIMD (cfg, kl) ? OP_XZERO : OP_VZERO); \
546         inst->dreg = dr; \
547                 (inst)->type = STACK_VTYPE;     \
548         (inst)->klass = (kl); \
549             MONO_ADD_INS ((cfg)->cbb, inst); \
550         } while (0)
551
552 #define MONO_EMIT_NEW_UNALU(cfg,op,dr,sr1) do { \
553         MonoInst *inst; \
554         EMIT_NEW_UNALU ((cfg), (inst), (op), (dr), (sr1)); \
555         } while (0)
556
557 #define MONO_EMIT_NEW_BIALU(cfg,op,dr,sr1,sr2) do { \
558         MonoInst *inst; \
559         MONO_INST_NEW ((cfg), (inst), (op)); \
560         inst->dreg = dr; \
561         inst->sreg1 = sr1; \
562         inst->sreg2 = sr2; \
563             MONO_ADD_INS (cfg->cbb, inst); \
564         } while (0)
565
566 #define MONO_EMIT_NEW_BIALU_IMM(cfg,op,dr,sr,imm) do { \
567         MonoInst *inst; \
568         MONO_INST_NEW ((cfg), (inst), (op)); \
569         inst->dreg = dr; \
570         inst->sreg1 = sr; \
571         inst->inst_imm = (mgreg_t)(imm);                \
572             MONO_ADD_INS (cfg->cbb, inst); \
573         } while (0)
574
575 #define MONO_EMIT_NEW_COMPARE_IMM(cfg,sr1,imm) do { \
576         MonoInst *inst; \
577         MONO_INST_NEW ((cfg), (inst), (OP_COMPARE_IMM)); \
578         inst->sreg1 = sr1; \
579         inst->inst_imm = (imm);                  \
580             MONO_ADD_INS ((cfg)->cbb, inst); \
581         } while (0)
582
583 #define MONO_EMIT_NEW_ICOMPARE_IMM(cfg,sr1,imm) do { \
584         MonoInst *inst; \
585         MONO_INST_NEW ((cfg), (inst), sizeof (void*) == 8 ? OP_ICOMPARE_IMM : OP_COMPARE_IMM); \
586         inst->sreg1 = sr1; \
587         inst->inst_imm = (imm);                  \
588             MONO_ADD_INS ((cfg)->cbb, inst); \
589         } while (0)
590
591 #define MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg,op,dr,base,offset) do { \
592         MonoInst *inst; \
593         MONO_INST_NEW ((cfg), (inst), (op)); \
594         inst->dreg = dr; \
595         inst->inst_basereg = base; \
596         inst->inst_offset = offset; \
597             MONO_ADD_INS (cfg->cbb, inst); \
598     } while (0)
599
600 #define MONO_EMIT_NEW_LOAD_MEMBASE(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
601
602 #define MONO_EMIT_NEW_STORE_MEMBASE(cfg,op,base,offset,sr) do { \
603         MonoInst *inst; \
604         MONO_INST_NEW ((cfg), (inst), (op)); \
605         (inst)->sreg1 = sr; \
606         (inst)->inst_destbasereg = base; \
607         (inst)->inst_offset = offset; \
608             MONO_ADD_INS (cfg->cbb, inst); \
609         } while (0)
610
611 #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
612         MonoInst *inst; \
613         MONO_INST_NEW ((cfg), (inst), (op)); \
614         inst->inst_destbasereg = base; \
615         inst->inst_offset = offset; \
616         inst->inst_imm = (mgreg_t)(imm); \
617         MONO_ADD_INS ((cfg)->cbb, inst); \
618         } while (0)
619
620 #define MONO_EMIT_NEW_COND_EXC(cfg,cond,name) do { \
621         MonoInst *inst; \
622         MONO_INST_NEW ((cfg), (inst), (OP_COND_EXC_##cond)); \
623         inst->inst_p1 = (char*)name; \
624             MONO_ADD_INS ((cfg)->cbb, inst); \
625         } while (0)
626
627 /* Branch support */
628
629 /*
630  * Basic blocks have two numeric identifiers:
631  * dfn: Depth First Number
632  * block_num: unique ID assigned at bblock creation
633  */
634 #define NEW_BBLOCK(cfg,bblock) do { \
635         (bblock) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
636         (bblock)->block_num = cfg->num_bblocks++; \
637     } while (0)
638
639 #define ADD_BBLOCK(cfg,b) do {  \
640         if ((b)->cil_code)  {\
641                         cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b);   \
642         } \
643                 (b)->real_offset = cfg->real_offset;    \
644         } while (0)
645
646 /* Emit a one-way conditional branch */
647 /* 
648  * The inst_false_bb field of the cond branch will not be set, the JIT code should be
649  * prepared to deal with this.
650  */
651 #ifdef DEBUG_EXTENDED_BBLOCKS
652 static int ccount = 0;
653 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
654         MonoInst *ins; \
655         MonoBasicBlock *falsebb; \
656         MONO_INST_NEW ((cfg), (ins), (op)); \
657         if ((op) == OP_BR) { \
658                 NEW_BBLOCK ((cfg), falsebb); \
659             ins->inst_target_bb = (truebb); \
660             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
661             MONO_ADD_INS ((cfg)->cbb, ins); \
662             MONO_START_BB ((cfg), falsebb); \
663         } else { \
664             ccount ++; \
665                     ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);  \
666             ins->inst_true_bb = (truebb); \
667             ins->inst_false_bb = NULL; \
668             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
669             MONO_ADD_INS ((cfg)->cbb, ins); \
670             if (getenv ("COUNT2") && ccount == atoi (getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
671             if (getenv ("COUNT2") && ccount < atoi (getenv ("COUNT2"))) { \
672                  cfg->cbb->extended = TRUE; \
673             } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
674         } \
675         } while (0)
676 #else
677 #define MONO_EMIT_NEW_BRANCH_BLOCK(cfg,op,truebb) do { \
678         MonoInst *ins; \
679         MonoBasicBlock *falsebb; \
680         MONO_INST_NEW ((cfg), (ins), (op)); \
681         if ((op) == OP_BR) { \
682                 NEW_BBLOCK ((cfg), falsebb); \
683             ins->inst_target_bb = (truebb); \
684             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
685             MONO_ADD_INS ((cfg)->cbb, ins); \
686             MONO_START_BB ((cfg), falsebb); \
687         } else { \
688                     ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);  \
689             ins->inst_true_bb = (truebb); \
690             ins->inst_false_bb = NULL; \
691             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
692             MONO_ADD_INS ((cfg)->cbb, ins); \
693             if (!cfg->enable_extended_bblocks) { \
694                 NEW_BBLOCK ((cfg), falsebb); \
695                 ins->inst_false_bb = falsebb; \
696                 mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
697                 MONO_START_BB ((cfg), falsebb); \
698             } else { \
699                                 cfg->cbb->extended = TRUE; \
700                         } \
701         } \
702         } while (0)
703 #endif
704
705 /* Emit a two-way conditional branch */
706 #define MONO_EMIT_NEW_BRANCH_BLOCK2(cfg,op,truebb,falsebb) do { \
707         MonoInst *ins; \
708         MONO_INST_NEW ((cfg), (ins), (op)); \
709                 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);      \
710         ins->inst_true_bb = (truebb); \
711         ins->inst_false_bb = (falsebb); \
712         mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
713         mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); \
714         MONO_ADD_INS ((cfg)->cbb, ins); \
715         } while (0)
716
717 #define MONO_START_BB(cfg, bblock) do { \
718         ADD_BBLOCK ((cfg), (bblock)); \
719         if (cfg->cbb->last_ins && MONO_IS_COND_BRANCH_OP (cfg->cbb->last_ins) && !cfg->cbb->last_ins->inst_false_bb) { \
720             cfg->cbb->last_ins->inst_false_bb = (bblock); \
721             mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
722         } 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)))) \
723             mono_link_bblock ((cfg), (cfg)->cbb, (bblock)); \
724             (cfg)->cbb->next_bb = (bblock); \
725             (cfg)->cbb = (bblock); \
726     } while (0)
727
728 /*Object Model related macros*/
729
730 #ifndef MONO_ARCH_EMIT_BOUNDS_CHECK
731 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) do { \
732                         int _length_reg = alloc_ireg (cfg); \
733                         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset); \
734                         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, _length_reg, index_reg); \
735                         MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
736         } while (0)
737 #endif
738
739 /* cfg is the MonoCompile been used
740  * array_reg is the vreg holding the array object
741  * array_type is a struct (usually MonoArray or MonoString)
742  * array_length_field is the field in the previous struct with the length
743  * index_reg is the vreg holding the index
744  */
745 #define MONO_EMIT_BOUNDS_CHECK(cfg, array_reg, array_type, array_length_field, index_reg) do { \
746             if (!(cfg->opt & MONO_OPT_ABCREM)) { \
747                 MONO_ARCH_EMIT_BOUNDS_CHECK ((cfg), (array_reg), G_STRUCT_OFFSET (array_type, array_length_field), (index_reg)); \
748             } else { \
749                 MonoInst *ins; \
750                 MONO_INST_NEW ((cfg), ins, OP_BOUNDS_CHECK); \
751                 ins->sreg1 = array_reg; \
752                 ins->sreg2 = index_reg; \
753                 ins->inst_imm = G_STRUCT_OFFSET (array_type, array_length_field); \
754                 MONO_ADD_INS ((cfg)->cbb, ins); \
755                             (cfg)->flags |= MONO_CFG_HAS_ARRAY_ACCESS; \
756                 (cfg)->cbb->has_array_access = TRUE; \
757             } \
758     } while (0)
759
760 G_END_DECLS
761
762 #endif