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