2008-08-28 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini.h
1 #ifndef __MONO_MINI_H__
2 #define __MONO_MINI_H__
3
4 #include "config.h"
5 #include <glib.h>
6 #include <signal.h>
7 #include <mono/metadata/loader.h>
8 #include <mono/metadata/mempool.h>
9 #include <mono/utils/monobitset.h>
10 #include <mono/metadata/class.h>
11 #include <mono/metadata/object.h>
12 #include <mono/metadata/opcodes.h>
13 #include <mono/metadata/tabledefs.h>
14 #include <mono/metadata/domain-internals.h>
15 #include "mono/metadata/class-internals.h"
16 #include "mono/metadata/object-internals.h"
17 #include <mono/metadata/profiler-private.h>
18 #include <mono/metadata/debug-helpers.h>
19 #include <mono/utils/mono-compiler.h>
20
21 #define MONO_BREAKPOINT_ARRAY_SIZE 64
22
23 #include "mini-arch.h"
24 #include "regalloc.h"
25 #include "declsec.h"
26
27 #ifndef G_LIKELY
28 #define G_LIKELY(a) (a)
29 #define G_UNLIKELY(a) (a)
30 #endif
31
32 #ifndef G_MAXINT32
33 #define G_MAXINT32 2147483647
34 #endif
35
36 #ifndef G_MININT32
37 #define G_MININT32 (-G_MAXINT32 - 1)
38 #endif
39
40 #if DISABLE_LOGGING
41 #define MINI_DEBUG(level,limit,code)
42 #else
43 #define MINI_DEBUG(level,limit,code) do {if (G_UNLIKELY ((level) >= (limit))) code} while (0)
44 #endif
45
46 #define NOT_IMPLEMENTED do { g_assert_not_reached (); } while (0)
47
48 #ifndef DISABLE_AOT
49 #define MONO_USE_AOT_COMPILER
50 #endif
51
52 /* for 32 bit systems */
53 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
54 #define MINI_LS_WORD_IDX 0
55 #define MINI_MS_WORD_IDX 1
56 #else
57 #define MINI_LS_WORD_IDX 1
58 #define MINI_MS_WORD_IDX 0
59 #endif
60 #define MINI_LS_WORD_OFFSET (MINI_LS_WORD_IDX * 4)
61 #define MINI_MS_WORD_OFFSET (MINI_MS_WORD_IDX * 4)
62 #define inst_ls_word data.op[MINI_LS_WORD_IDX].const_val
63 #define inst_ms_word data.op[MINI_MS_WORD_IDX].const_val
64
65 #define MONO_FAKE_IMT_METHOD ((MonoMethod*)GINT_TO_POINTER(-1))
66 #define MONO_FAKE_VTABLE_METHOD ((MonoMethod*)GINT_TO_POINTER(-2))
67
68 /* Version number of the AOT file format */
69 #define MONO_AOT_FILE_VERSION "38"
70  
71 /* Per-domain information maintained by the JIT */
72 typedef struct
73 {
74         /* Maps MonoMethod's to a GSList of GOT slot addresses pointing to its code */
75         GHashTable *jump_target_got_slot_hash;
76 } MonoJitDomainInfo;
77
78 #define jit_domain_info(domain) ((MonoJitDomainInfo*)((domain)->runtime_info))
79
80 #if 0
81 #define mono_bitset_foreach_bit(set,b,n) \
82         for (b = 0; b < n; b++)\
83                 if (mono_bitset_test_fast(set,b))
84 #define mono_bitset_foreach_bit_rev(set,b,n) \
85         for (b = n - 1; b >= 0; b--)\
86                 if (mono_bitset_test_fast(set,b))
87 #else
88 #define mono_bitset_foreach_bit(set,b,n) \
89         for (b = mono_bitset_find_start (set); b < n && b >= 0; b = mono_bitset_find_first (set, b))
90 #define mono_bitset_foreach_bit_rev(set,b,n) \
91         for (b = mono_bitset_find_last (set, n - 1); b >= 0; b = b ? mono_bitset_find_last (set, b) : -1)
92  
93 #endif
94
95 /*
96  * Pull the list of opcodes
97  */
98 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
99         a = i,
100
101 enum {
102 #include "mono/cil/opcode.def"
103         CEE_LASTOP
104 };
105 #undef OPDEF
106
107 #define MONO_VARINFO(cfg,varnum) (&(cfg)->vars [varnum])
108
109 #define MONO_INST_NEW(cfg,dest,op) do { \
110                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
111                 (dest)->opcode = (op);  \
112         (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1;  \
113         (dest)->cil_code = (cfg)->ip;  \
114         (dest)->cil_code = (cfg)->ip; \
115         } while (0)
116
117 #define MONO_INST_NEW_CALL(cfg,dest,op) do {    \
118                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoCallInst));   \
119                 (dest)->inst.opcode = (op);     \
120         (dest)->inst.dreg = (dest)->inst.sreg1 = (dest)->inst.sreg2 = -1;  \
121         (dest)->inst.cil_code = (cfg)->ip;  \
122         } while (0)
123
124 #define MONO_INST_NEW_CALL_ARG(cfg,dest,op) do {        \
125                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoCallArgParm));        \
126                 (dest)->ins.opcode = (op);      \
127         } while (0)
128
129 #define MONO_ADD_INS(b,inst) do {       \
130                 if ((b)->last_ins) {    \
131                         (b)->last_ins->next = (inst);   \
132             (inst)->prev = (b)->last_ins;   \
133                         (b)->last_ins = (inst); \
134                 } else {        \
135                         (b)->code = (b)->last_ins = (inst);     \
136                 }       \
137         } while (0)
138
139 #define NULLIFY_INS(ins) do { \
140         (ins)->opcode = OP_NOP; \
141         (ins)->dreg = (ins)->sreg1 = (ins)->sreg2 = -1; \
142                 (ins)->ssa_op = MONO_SSA_NOP; \
143     } while (0)
144
145 /* Remove INS from BB */
146 #define MONO_REMOVE_INS(bb,ins) do { \
147         if ((ins)->prev) \
148             (ins)->prev->next = (ins)->next; \
149         if ((ins)->next) \
150             (ins)->next->prev = (ins)->prev; \
151         if ((bb)->code == (ins)) \
152             (bb)->code = (ins)->next; \
153         if ((bb)->last_ins == (ins)) \
154             (bb)->last_ins = (ins)->prev; \
155     } while (0)
156
157 /* Remove INS from BB and nullify it */
158 #define MONO_DELETE_INS(bb,ins) do { \
159         MONO_REMOVE_INS ((bb), (ins)); \
160         NULLIFY_INS ((ins)); \
161     } while (0)
162
163 /* 
164  * this is used to determine when some branch optimizations are possible: we exclude FP compares
165  * because they have weird semantics with NaNs.
166  */
167 #define MONO_IS_COND_BRANCH_OP(ins) (((ins)->opcode >= CEE_BEQ && (ins)->opcode <= CEE_BLT_UN) || ((ins)->opcode >= OP_LBEQ && (ins)->opcode <= OP_LBLT_UN) || ((ins)->opcode >= OP_FBEQ && (ins)->opcode <= OP_FBLT_UN) || ((ins)->opcode >= OP_IBEQ && (ins)->opcode <= OP_IBLT_UN))
168 #define MONO_IS_COND_BRANCH_NOFP(ins) (MONO_IS_COND_BRANCH_OP(ins) && !(((ins)->opcode >= OP_FBEQ) && ((ins)->opcode <= OP_FBLT_UN)) && (!(ins)->inst_left || (ins)->inst_left->inst_left->type != STACK_R8))
169
170 #define MONO_IS_BRANCH_OP(ins) (MONO_IS_COND_BRANCH_OP(ins) || ((ins)->opcode == OP_BR) || ((ins)->opcode == OP_BR_REG) || ((ins)->opcode == OP_SWITCH))
171
172 #define MONO_IS_COND_EXC(ins) ((((ins)->opcode >= OP_COND_EXC_EQ) && ((ins)->opcode <= OP_COND_EXC_LT_UN)) || (((ins)->opcode >= OP_COND_EXC_IEQ) && ((ins)->opcode <= OP_COND_EXC_ILT_UN)))
173
174 #define MONO_IS_SETCC(ins) ((((ins)->opcode >= OP_CEQ) && ((ins)->opcode <= OP_CLT_UN)) || (((ins)->opcode >= OP_ICEQ) && ((ins)->opcode <= OP_ICLT_UN)) || (((ins)->opcode >= OP_LCEQ) && ((ins)->opcode <= OP_LCLT_UN)) || (((ins)->opcode >= OP_FCEQ) && ((ins)->opcode <= OP_FCLT_UN)))
175
176 #define MONO_IS_PHI(ins) (((ins)->opcode == OP_PHI) || ((ins)->opcode == OP_FPHI) || ((ins)->opcode == OP_VPHI))
177 #define MONO_IS_MOVE(ins) (((ins)->opcode == OP_MOVE) || ((ins)->opcode == OP_FMOVE) || ((ins)->opcode == OP_VMOVE))
178
179 #define MONO_IS_LOAD_MEMBASE(ins) (((ins)->opcode >= OP_LOAD_MEMBASE) && ((ins)->opcode <= OP_LOADV_MEMBASE))
180 #define MONO_IS_STORE_MEMBASE(ins) (((ins)->opcode >= OP_STORE_MEMBASE_REG) && ((ins)->opcode <= OP_STOREV_MEMBASE))
181 #define MONO_IS_STORE_MEMINDEX(ins) (((ins)->opcode >= OP_STORE_MEMINDEX) && ((ins)->opcode <= OP_STORER8_MEMINDEX))
182
183 #define MONO_IS_CALL(ins) (((ins->opcode >= OP_VOIDCALL) && (ins->opcode <= OP_VOIDCALL_MEMBASE)) || ((ins->opcode >= OP_FCALL) && (ins->opcode <= OP_FCALL_MEMBASE)) || ((ins->opcode >= OP_LCALL) && (ins->opcode <= OP_LCALL_MEMBASE)) || ((ins->opcode >= OP_VCALL) && (ins->opcode <= OP_VCALL_MEMBASE)) || ((ins->opcode >= OP_CALL) && (ins->opcode <= OP_CALL_MEMBASE)) || ((ins->opcode >= OP_VCALL2) && (ins->opcode <= OP_VCALL2_MEMBASE)) || (ins->opcode == OP_TAILCALL))
184
185 /* FIXME: Handle OP_GOT_ENTRY too */
186 #define MONO_IS_JUMP_TABLE(ins) (((ins)->opcode == OP_JUMP_TABLE) ? TRUE : ((((ins)->opcode == OP_AOTCONST) && (ins->inst_i1 == (gpointer)MONO_PATCH_INFO_SWITCH)) ? TRUE : ((ins)->opcode == OP_SWITCH) ? TRUE : FALSE))
187
188 #define MONO_JUMP_TABLE_FROM_INS(ins) (((ins)->opcode == OP_JUMP_TABLE) ? (ins)->inst_p0 : (((ins)->opcode == OP_AOTCONST) && (ins->inst_i1 == (gpointer)MONO_PATCH_INFO_SWITCH) ? (ins)->inst_p0 : (((ins)->opcode == OP_SWITCH) ? (ins)->inst_p0 : NULL)))
189
190 /* FIXME: Add more instructions */
191 #define MONO_INS_HAS_NO_SIDE_EFFECT(ins) (MONO_IS_MOVE (ins) || (ins->opcode == OP_ICONST) || (ins->opcode == OP_I8CONST) || (ins->opcode == OP_VZERO) || (ins->opcode == OP_ADD_IMM) || (ins->opcode == OP_R8CONST) || (ins->opcode == OP_LADD_IMM) || (ins->opcode == OP_ISUB_IMM) || (ins->opcode == OP_IADD_IMM) || (ins->opcode == OP_INEG) || (ins->opcode == OP_LNEG) || (ins->opcode == OP_ISUB) || (ins->opcode == OP_CMOV_IGE) || (ins->opcode == OP_ISHL_IMM) || (ins->opcode == OP_ISHR_IMM) || (ins->opcode == OP_ISHR_UN_IMM) || (ins->opcode == OP_IAND_IMM) || (ins->opcode == OP_ICONV_TO_U1) || (ins->opcode == OP_ICONV_TO_I1) || (ins->opcode == OP_SEXT_I4) || (ins->opcode == OP_LCONV_TO_U1) || (ins->opcode == OP_ICONV_TO_U2) || (ins->opcode == OP_ICONV_TO_I2) || (ins->opcode == OP_LCONV_TO_I2))
192
193 typedef struct MonoInstList MonoInstList;
194 typedef struct MonoInst MonoInst;
195 typedef struct MonoCallInst MonoCallInst;
196 typedef struct MonoCallArgParm MonoCallArgParm;
197 typedef struct MonoEdge MonoEdge;
198 typedef struct MonoMethodVar MonoMethodVar;
199 typedef struct MonoBasicBlock MonoBasicBlock;
200 typedef struct MonoLMF MonoLMF;
201 typedef struct MonoSpillInfo MonoSpillInfo;
202 typedef struct MonoTraceSpec MonoTraceSpec;
203
204 extern guint32 mono_jit_tls_id;
205 extern MonoTraceSpec *mono_jit_trace_calls;
206 extern gboolean mono_break_on_exc;
207 extern int mono_exc_esp_offset;
208 #ifdef DISABLE_AOT
209 #define mono_compile_aot 0
210 #else
211 extern gboolean mono_compile_aot;
212 #endif
213 extern gboolean mono_aot_only;
214 extern gboolean mono_use_imt;
215 extern MonoMethodDesc *mono_inject_async_exc_method;
216 extern int mono_inject_async_exc_pos;
217 extern MonoMethodDesc *mono_break_at_bb_method;
218 extern int mono_break_at_bb_bb_num;
219 extern gboolean check_for_pending_exc;
220 extern gboolean disable_vtypes_in_regs;
221 extern gboolean mono_verify_all;
222
223 #define INS_INFO(opcode) (&ins_info [((opcode) - OP_START - 1) * 3])
224
225 extern const char ins_info[];
226
227 #define MONO_BB_FOR_EACH_INS(bb, ins) for ((ins) = (bb)->code; (ins); (ins) = (ins)->next)
228
229 #define MONO_BB_FOR_EACH_INS_SAFE(bb, n, ins) for ((ins) = (bb)->code, n = (ins) ? (ins)->next : NULL; (ins); (ins) = (n), (n) = (ins) ? (ins)->next : NULL)
230
231 #define MONO_BB_FOR_EACH_INS_REVERSE_SAFE(bb, p, ins) for ((ins) = (bb)->last_ins, p = (ins) ? (ins)->prev : NULL; (ins); (ins) = (p), (p) = (ins) ? (ins)->prev : NULL)
232
233 #define mono_bb_first_ins(bb) (bb)->code
234
235 #if 0
236
237 static inline void
238 MONO_INST_LIST_ADD_TAIL (MonoInstList *new, MonoInstList *head)
239 {
240         __MONO_INST_LIST_ADD (new, head->prev, head);
241 }
242
243 static inline void
244 __MONO_INST_LIST_DEL (MonoInstList *prev, MonoInstList *next)
245 {
246         next->prev = prev;
247         prev->next = next;
248 }
249
250 static inline void
251 __MONO_INST_LIST_SPLICE (MonoInstList *list, MonoInstList *head)
252 {
253         MonoInstList *first = list->next;
254         MonoInstList *last = list->prev;
255         MonoInstList *at = head->next;
256
257         first->prev = head;
258         head->next = first;
259
260         last->next = at;
261         at->prev = last;
262 }
263
264 static inline void
265 MONO_INST_LIST_SPLICE (MonoInstList *list, MonoInstList *head) 
266 {
267         if (!MONO_INST_LIST_EMPTY (list))
268                 __MONO_INST_LIST_SPLICE (list, head);
269 }
270
271 static inline void
272 MONO_INST_LIST_SPLICE_TAIL (MonoInstList *list, MonoInstList *head) 
273 {
274         if (!MONO_INST_LIST_EMPTY (list))
275                 __MONO_INST_LIST_SPLICE (list, head->prev);
276 }
277
278 static inline void
279 MONO_INST_LIST_SPLICE_INIT (MonoInstList *list, MonoInstList *head)
280 {
281         if (!MONO_INST_LIST_EMPTY (list)) {
282                 __MONO_INST_LIST_SPLICE (list, head);
283                 MONO_INST_LIST_INIT (list);
284         }
285 }
286
287 static inline void
288 MONO_INST_LIST_SPLICE_TAIL_INIT (MonoInstList *list, MonoInstList *head)
289 {
290         if (!MONO_INST_LIST_EMPTY (list)) {
291                 __MONO_INST_LIST_SPLICE (list, head->prev);
292                 MONO_INST_LIST_INIT (list);
293         }
294 }
295
296 /*#define mono_container_of(ptr, type, member) ({                       \
297         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
298         (type *)( (char *)__mptr - offsetof(type,member) );})
299
300 #define MONO_INST_LIST_ENTRY(ptr, type, member) \
301         mono_container_of(ptr, type, member)*/
302
303 #define MONO_INST_LIST_ENTRY(ptr, type, member) \
304         ((type *)(gpointer)(ptr))
305
306 #define MONO_INST_LIST_FIRST_ENTRY(ptr, type, member) \
307         MONO_INST_LIST_ENTRY((ptr)->next, type, member)
308
309 #define MONO_INST_LIST_LAST_ENTRY(ptr, type, member) \
310         MONO_INST_LIST_ENTRY((ptr)->prev, type, member)
311
312 #define MONO_INST_LIST_FOR_EACH(pos, head) \
313         for (pos = (head)->next; pos != (head); pos = pos->next)
314
315 #define MONO_INST_LIST_FOR_EACH_PREV(pos, head) \
316         for (pos = (head)->prev; pos != (head); pos = pos->prev)
317
318 #define MONO_INST_LIST_FOR_EACH_SAFE(pos, n, head) \
319         for (pos = (head)->next, n = pos->next; pos != (head); \
320                 pos = n, n = pos->next)
321
322 #define MONO_INST_LIST_FOR_EACH_PREV_SAFE(pos, n, head) \
323         for (pos = (head)->prev, n = pos->prev; pos != (head); \
324                 pos = n, n = pos->prev)
325
326 #define MONO_INST_LIST_FOR_EACH_ENTRY(pos, head, member) \
327         for (pos = MONO_INST_LIST_ENTRY ((head)->next, MonoInst, member);\
328              &pos->member != (head);\
329              pos = MONO_INST_LIST_ENTRY (pos->member.next, MonoInst, member))
330
331 #define MONO_INST_LIST_FOR_EACH_ENTRY_REVERSE(pos, head, member) \
332         for (pos = MONO_INST_LIST_ENTRY ((head)->prev, MonoInst, member);\
333              &pos->member != (head);\
334              pos = MONO_INST_LIST_ENTRY (pos->member.prev, MonoInst, member))
335
336 #define MONO_INST_LIST_FOR_EACH_ENTRY_SAFE(pos, n, head, member) \
337         for (pos = MONO_INST_LIST_ENTRY ((head)->next, MonoInst, member),\
338                 n = MONO_INST_LIST_ENTRY (pos->member.next, MonoInst, member);\
339              &pos->member != (head);                                    \
340              pos = n, n = MONO_INST_LIST_ENTRY (n->member.next, MonoInst, member))
341
342 #define MONO_BB_FOR_EACH_INS(bb, ins) MONO_INST_LIST_FOR_EACH_ENTRY ((ins), &((bb)->ins_list), node)
343
344 #define MONO_BB_FOR_EACH_INS_SAFE(bb, next, ins) MONO_INST_LIST_FOR_EACH_ENTRY_SAFE ((ins), (next), &((bb)->ins_list), node)
345
346 #define MONO_BB_FOR_EACH_INS_REVERSE(bb, ins) MONO_INST_LIST_FOR_EACH_ENTRY_REVERSE ((ins), &((bb)->ins_list), node)
347
348 #endif
349
350 struct MonoEdge {
351         MonoEdge *next;
352         MonoBasicBlock *bb;
353         /* add edge type? */
354 };
355
356 struct MonoSpillInfo {
357         int offset;
358 };
359
360 /*
361  * The IR-level extended basic block.  
362  *
363  * A basic block can have multiple exits just fine, as long as the point of
364  * 'departure' is the last instruction in the basic block. Extended basic
365  * blocks, on the other hand, may have instructions that leave the block
366  * midstream. The important thing is that they cannot be _entered_
367  * midstream, ie, execution of a basic block (or extened bb) always start
368  * at the beginning of the block, never in the middle.
369  */
370 struct MonoBasicBlock {
371         MonoInst *last_ins;
372
373         /* the next basic block in the order it appears in IL */
374         MonoBasicBlock *next_bb;
375
376         /*
377          * Before instruction selection it is the first tree in the
378          * forest and the first item in the list of trees. After
379          * instruction selection it is the first instruction and the
380          * first item in the list of instructions.
381          */
382         MonoInst *code;
383
384         /* unique block number identification */
385         gint32 block_num;
386         
387         gint32 dfn;
388
389         /* Basic blocks: incoming and outgoing counts and pointers */
390         /* Each bb should only appear once in each array */
391         gint16 out_count, in_count;
392         MonoBasicBlock **in_bb;
393         MonoBasicBlock **out_bb;
394
395         /* Points to the start of the CIL code that initiated this BB */
396         unsigned char* cil_code;
397
398         /* Length of the CIL block */
399         gint32 cil_length;
400
401         /* The address of the generated code, used for fixups */
402         int native_offset;
403         int max_offset;
404
405         /* Visited and reachable flags */
406         guint32 flags;
407
408         /*
409          * SSA and loop based flags
410          */
411         MonoBitSet *dominators;
412         MonoBitSet *dfrontier;
413         MonoBasicBlock *idom;
414         GSList *dominated;
415         /* fast dominator algorithm */
416         MonoBasicBlock *df_parent, *ancestor, *child, *label;
417         MonoEdge *bucket;
418         int size, sdom, idomn;
419         
420         /* loop nesting and recognition */
421         GList *loop_blocks;
422         gint8  nesting;
423         gint8  loop_body_start;
424
425         /* 
426          * Whenever the bblock is rarely executed so it should be emitted after
427          * the function epilog.
428          */
429         guint out_of_line : 1;
430         /* Caches the result of uselessness calculation during optimize_branches */
431         guint not_useless : 1;
432         /* Whenever the decompose_array_access_opts () pass needs to process this bblock */
433         guint has_array_access : 1;
434         /* Whenever this bblock is extended, ie. it has branches inside it */
435         guint extended : 1;
436         
437         /* use for liveness analysis */
438         MonoBitSet *gen_set;
439         MonoBitSet *kill_set;
440         MonoBitSet *live_in_set;
441         MonoBitSet *live_out_set;
442
443         /* fields to deal with non-empty stack slots at bb boundary */
444         guint16 out_scount, in_scount;
445         MonoInst **out_stack;
446         MonoInst **in_stack;
447
448         /* we use that to prevent merging of bblocks covered by different clauses*/
449         guint real_offset;
450
451         /*
452          * The region encodes whether the basic block is inside
453          * a finally, catch, filter or none of these.
454          *
455          * If the value is -1, then it is neither finally, catch nor filter
456          *
457          * Otherwise the format is:
458          *
459          *  Bits: |     0-3      |       4-7      |     8-31
460          *        |              |                |
461          *        | clause-flags |   MONO_REGION  | clause-index 
462          *
463          */
464         guint region;
465
466         /* The current symbolic register number, used in local register allocation. */
467         guint32 max_vreg;
468 };
469
470 /* BBlock flags */
471 enum {
472         BB_VISITED            = 1 << 0,
473         BB_REACHABLE          = 1 << 1,
474         BB_EXCEPTION_DEAD_OBJ = 1 << 2,
475         BB_EXCEPTION_UNSAFE   = 1 << 3,
476         BB_EXCEPTION_HANDLER  = 1 << 4
477 };
478
479 typedef struct MonoMemcpyArgs {
480         int size, align;
481 } MonoMemcpyArgs;
482
483 struct MonoInst {
484         guint16 opcode;
485         guint8  type; /* stack type */
486         guint   ssa_op : 3;
487         guint8  flags  : 5;
488         
489         /* used by the register allocator */
490         gint32 dreg, sreg1, sreg2;
491
492         MonoInst *next, *prev;
493
494         union {
495                 union {
496                         MonoInst *src;
497                         MonoMethodVar *var;
498                         gssize const_val;
499                         gpointer p;
500                         MonoMethod *method;
501                         MonoMethodSignature *signature;
502                         MonoBasicBlock **many_blocks;
503                         MonoBasicBlock *target_block;
504                         MonoInst **args;
505                         MonoType *vtype;
506                         MonoClass *klass;
507                         int *phi_args;
508                         MonoCallInst *call_inst;
509                 } op [2];
510                 gint64 i8const;
511                 double r8const;
512         } data;
513
514         const unsigned char* cil_code; /* for debugging and bblock splitting */
515
516         /* used mostly by the backend to store additional info it may need */
517         union {
518                 gint32 reg3;
519                 gint32 arg_info;
520                 gint32 size;
521                 MonoMemcpyArgs *memcpy_args; /* in OP_MEMSET and OP_MEMCPY */
522                 gpointer data;
523                 gint shift_amount;
524                 gboolean is_pinvoke; /* for variables in the unmanaged marshal format */
525                 gboolean record_cast_details; /* For CEE_CASTCLASS */
526         } backend;
527         
528         MonoClass *klass;
529 };
530         
531 struct MonoCallInst {
532         MonoInst inst;
533         MonoMethodSignature *signature;
534         MonoMethod *method;
535         MonoInst **args;
536         MonoInst *out_args;
537         MonoInst *vret_var;
538         gconstpointer fptr;
539         guint stack_usage;
540         guint virtual : 1;
541         guint tail_call : 1;
542         /* If this is TRUE, 'fptr' points to a MonoJumpInfo instead of an address. */
543         guint fptr_is_patch : 1;
544         /*
545          * If this is true, then the call returns a vtype in a register using the same 
546          * calling convention as OP_CALL.
547          */
548         guint vret_in_reg : 1;
549         /* Whenever there is an IMT argument and it is dynamic */
550         guint dynamic_imt_arg : 1;
551         regmask_t used_iregs;
552         regmask_t used_fregs;
553         GSList *out_ireg_args;
554         GSList *out_freg_args;
555 };
556
557 struct MonoCallArgParm {
558         MonoInst ins;
559         gint32 size;
560         gint32 offset;
561         gint32 offPrm;
562 };
563
564 /* 
565  * flags for MonoInst
566  * Note: some of the values overlap, because they can't appear
567  * in the same MonoInst.
568  */
569 enum {
570         MONO_INST_HAS_METHOD = 1,
571         /* temp local created by a DUP: used only within a BB */
572         MONO_INST_IS_TEMP    = 1,
573         MONO_INST_INIT       = 1, /* in localloc */
574         MONO_INST_IS_DEAD    = 2,
575         MONO_INST_TAILCALL   = 4,
576         MONO_INST_VOLATILE   = 4,
577         MONO_INST_BRLABEL    = 4,
578         MONO_INST_NOTYPECHECK    = 4,
579         MONO_INST_UNALIGNED  = 8,
580     MONO_INST_CFOLD_TAKEN = 8, /* On branches */
581     MONO_INST_CFOLD_NOT_TAKEN = 16, /* On branches */
582         MONO_INST_DEFINITION_HAS_SIDE_EFFECTS = 8,
583         /* the address of the variable has been taken */
584         MONO_INST_INDIRECT   = 16,
585         MONO_INST_NORANGECHECK   = 16
586 };
587
588 #define inst_c0 data.op[0].const_val
589 #define inst_c1 data.op[1].const_val
590 #define inst_i0 data.op[0].src
591 #define inst_i1 data.op[1].src
592 #define inst_p0 data.op[0].p
593 #define inst_p1 data.op[1].p
594 #define inst_l  data.i8const
595 #define inst_r  data.r8const
596 #define inst_left  data.op[0].src
597 #define inst_right data.op[1].src
598
599 #define inst_newa_len   data.op[0].src
600 #define inst_newa_class data.op[1].klass
601
602 #define inst_var    data.op[0].var
603 #define inst_vtype  data.op[1].vtype
604 /* in branch instructions */
605 #define inst_many_bb   data.op[1].many_blocks
606 #define inst_target_bb data.op[0].target_block
607 #define inst_true_bb   data.op[1].many_blocks[0]
608 #define inst_false_bb  data.op[1].many_blocks[1]
609
610 #define inst_basereg sreg1
611 #define inst_indexreg sreg2
612 #define inst_destbasereg dreg
613 #define inst_offset data.op[0].const_val
614 #define inst_imm    data.op[1].const_val
615 #define inst_call   data.op[1].call_inst
616
617 #define inst_phi_args   data.op[1].phi_args
618
619 /* instruction description for use in regalloc/scheduling */
620 enum {
621         MONO_INST_DEST,
622         MONO_INST_SRC1,
623         MONO_INST_SRC2,
624         MONO_INST_LEN,
625         MONO_INST_CLOB,
626         /* Unused, commented out to reduce the size of the mdesc tables
627         MONO_INST_FLAGS,
628         MONO_INST_COST,
629         MONO_INST_DELAY,
630         MONO_INST_RES,
631         */
632         MONO_INST_MAX
633 };
634
635 typedef union {
636         struct {
637                 guint16 tid; /* tree number */
638                 guint16 bid; /* block number */
639         } pos ;
640         guint32 abs_pos; 
641 } MonoPosition;
642
643 typedef struct {
644         MonoPosition first_use, last_use;
645 } MonoLiveRange;
646
647 typedef struct MonoLiveRange2 MonoLiveRange2;
648
649 struct MonoLiveRange2 {
650         int from, to;
651         MonoLiveRange2 *next;
652 };
653
654 typedef struct {
655         /* List of live ranges sorted by 'from' */
656         MonoLiveRange2 *range;
657         MonoLiveRange2 *last_range;
658 } MonoLiveInterval;
659
660 /*
661  * Additional information about a variable
662  */
663 struct MonoMethodVar {
664         guint           idx; /* inside cfg->varinfo, cfg->vars */
665         MonoLiveRange   range; /* generated by liveness analysis */
666         MonoLiveInterval *interval; /* generated by liveness analysis */
667         int             reg; /* != -1 if allocated into a register */
668         int             spill_costs;
669         MonoBitSet     *def_in; /* used by SSA */
670         MonoInst       *def;    /* used by SSA */
671         MonoBasicBlock *def_bb; /* used by SSA */
672         GList          *uses;   /* used by SSA */
673         char            cpstate;  /* used by SSA conditional  constant propagation */
674 };
675
676 typedef struct {
677         gpointer          end_of_stack;
678         guint32           stack_size;
679 #if !defined(HAVE_KW_THREAD) || !defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
680         MonoLMF          *lmf;
681 #endif
682         MonoLMF          *first_lmf;
683         gpointer         signal_stack;
684         guint32          signal_stack_size;
685         gpointer         stack_ovf_guard_base;
686         guint32          stack_ovf_guard_size;
687         void            (*abort_func) (MonoObject *object);
688         /* Used to implement --debug=casts */
689         MonoClass       *class_cast_from, *class_cast_to;
690 } MonoJitTlsData;
691
692 typedef enum {
693 #define PATCH_INFO(a,b) MONO_PATCH_INFO_ ## a,
694 #include "patch-info.h"
695 #undef PATCH_INFO
696         MONO_PATCH_INFO_NUM
697 } MonoJumpInfoType;
698
699 /*
700  * We need to store the image which the token refers to along with the token,
701  * since the image might not be the same as the image of the method which
702  * contains the relocation, because of inlining.
703  */
704 typedef struct MonoJumpInfoToken {
705         MonoImage *image;
706         guint32 token;
707 } MonoJumpInfoToken;
708
709 typedef struct MonoJumpInfoBBTable {
710         MonoBasicBlock **table;
711         int table_size;
712 } MonoJumpInfoBBTable;
713
714 typedef struct MonoJumpInfoRgctxEntry MonoJumpInfoRgctxEntry;
715
716 typedef struct MonoJumpInfo MonoJumpInfo;
717 struct MonoJumpInfo {
718         MonoJumpInfo *next;
719         union {
720                 int i;
721                 guint8 *p;
722                 MonoInst *label;
723         } ip;
724
725         MonoJumpInfoType type;
726         union {
727                 gconstpointer   target;
728 #if SIZEOF_VOID_P == 8
729                 gint64          offset;
730 #else
731                 int             offset;
732 #endif
733                 MonoBasicBlock *bb;
734                 MonoInst       *inst;
735                 MonoMethod     *method;
736                 MonoClass      *klass;
737                 MonoClassField *field;
738                 MonoImage      *image;
739                 MonoVTable     *vtable;
740                 const char     *name;
741                 MonoJumpInfoToken  *token;
742                 MonoJumpInfoBBTable *table;
743                 MonoJumpInfoRgctxEntry *rgctx_entry;
744         } data;
745 };
746  
747 /* Contains information describing an rgctx entry */
748 struct MonoJumpInfoRgctxEntry {
749         MonoMethod *method;
750         gboolean in_mrgctx;
751         MonoJumpInfo *data; /* describes the data to be loaded */
752         int info_type;
753 };
754
755 typedef enum {
756         MONO_TRAMPOLINE_JIT,
757         MONO_TRAMPOLINE_JUMP,
758         MONO_TRAMPOLINE_CLASS_INIT,
759         MONO_TRAMPOLINE_GENERIC_CLASS_INIT,
760         MONO_TRAMPOLINE_RGCTX_LAZY_FETCH,
761         MONO_TRAMPOLINE_AOT,
762         MONO_TRAMPOLINE_AOT_PLT,
763         MONO_TRAMPOLINE_DELEGATE,
764         MONO_TRAMPOLINE_NUM
765 } MonoTrampolineType;
766
767 /* optimization flags */
768 #define OPTFLAG(id,shift,name,descr) MONO_OPT_ ## id = 1 << shift,
769 enum {
770 #include "optflags-def.h"
771         MONO_OPT_LAST
772 };
773
774 /* Bit-fields in the MonoBasicBlock.region */
775 #define MONO_REGION_TRY       0
776 #define MONO_REGION_FINALLY  16
777 #define MONO_REGION_CATCH    32
778 #define MONO_REGION_FAULT    64         /* Currently unused */
779 #define MONO_REGION_FILTER  128
780
781 #define MONO_BBLOCK_IS_IN_REGION(bblock, regtype) (((bblock)->region & (0xf << 4)) == (regtype))
782
783 #define get_vreg_to_inst(cfg, vreg) ((vreg) < (cfg)->vreg_to_inst_len ? (cfg)->vreg_to_inst [(vreg)] : NULL)
784
785 #define vreg_is_volatile(cfg, vreg) (G_UNLIKELY (get_vreg_to_inst ((cfg), (vreg)) && (get_vreg_to_inst ((cfg), (vreg))->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))))
786
787 /*
788  * Control Flow Graph and compilation unit information
789  */
790 typedef struct {
791         MonoMethod      *method;
792         MonoMemPool     *mempool;
793         MonoInst       **varinfo;
794         MonoMethodVar   *vars;
795         MonoInst        *ret;
796         MonoBasicBlock  *bb_entry;
797         MonoBasicBlock  *bb_exit;
798         MonoBasicBlock  *bb_init;
799         MonoBasicBlock **bblocks;
800         MonoBasicBlock **cil_offset_to_bb;
801         MonoMemPool     *state_pool; /* used by instruction selection */
802         MonoBasicBlock  *cbb;        /* used by instruction selection */
803         MonoInst        *prev_ins;   /* in decompose */
804         MonoJumpInfo    *patch_info;
805         MonoJitInfo     *jit_info;
806         MonoJitDynamicMethodInfo *dynamic_info;
807         guint            num_bblocks;
808         guint            locals_start;
809         guint            num_varinfo; /* used items in varinfo */
810         guint            varinfo_count; /* total storage in varinfo */
811         gint             stack_offset;
812         gint             max_ireg;
813         gint             cil_offset_to_bb_len;
814         MonoRegState    *rs;
815         MonoSpillInfo   *spill_info; /* machine register spills */
816         MonoSpillInfo   *spill_info_float; /* fp register spills */
817         gint             spill_count;
818         gint             spill_info_len, spill_info_float_len;
819         /* unsigned char   *cil_code; */
820         MonoMethod      *inlined_method; /* the method which is currently inlined */
821         MonoInst        *domainvar; /* a cache for the current domain */
822         MonoInst        *got_var; /* Global Offset Table variable */
823         MonoInst        **locals;
824         MonoInst        *rgctx_var; /* Runtime generic context variable (for static generic methods) */
825         MonoInst        **args;
826         MonoType        **arg_types;
827         MonoMethod      *current_method; /* The method currently processed by method_to_ir () */
828         MonoGenericContext *generic_context;
829
830         /* 
831          * This variable represents the hidden argument holding the vtype
832          * return address. If the method returns something other than a vtype, or
833          * the vtype is returned in registers this is NULL.
834          */
835         MonoInst        *vret_addr;
836
837         /*
838          * This is used to initialize the cil_code field of MonoInst's.
839          */
840         const unsigned char *ip;
841         
842         struct MonoAliasingInformation *aliasing_info;
843
844         /* A hashtable of region ID-> SP var mappings */
845         /* An SP var is a place to store the stack pointer (used by handlers)*/
846         GHashTable      *spvars;
847
848         /* A hashtable of region ID -> EX var mappings */
849         /* An EX var stores the exception object passed to catch/filter blocks */
850         GHashTable      *exvars;
851
852         GList           *ldstr_list; /* used by AOT */
853         
854         MonoDomain      *domain;
855
856         guint            real_offset;
857         GHashTable      *cbb_hash;
858
859         /* The current virtual register number */
860         guint32 next_vreg;
861
862         MonoGenericSharingContext *generic_sharing_context;
863
864         unsigned char   *cil_start;
865         unsigned char   *native_code;
866         guint            code_size;
867         guint            code_len;
868         guint            prolog_end;
869         guint            epilog_begin;
870         regmask_t        used_int_regs;
871         guint32          opt;
872         guint32          prof_options;
873         guint32          flags;
874         guint32          comp_done;
875         guint32          verbose_level;
876         guint32          stack_usage;
877         guint32          param_area;
878         guint32          frame_reg;
879         gint32           sig_cookie;
880         guint            disable_aot : 1;
881         guint            disable_ssa : 1;
882         guint            enable_extended_bblocks : 1;
883         guint            run_cctors : 1;
884         guint            need_lmf_area : 1;
885         guint            compile_aot : 1;
886         guint            got_var_allocated : 1;
887         guint            ret_var_is_local : 1;
888         guint            ret_var_set : 1;
889         guint            new_ir : 1;
890         guint            globalra : 1;
891         guint            unverifiable : 1;
892         guint            skip_visibility : 1;
893         guint            disable_reuse_registers : 1;
894         guint            disable_reuse_stack_slots : 1;
895         guint            disable_initlocals_opt : 1;
896         guint            disable_omit_fp : 1;
897         guint            has_got_slots : 1;
898         guint            uses_rgctx_reg : 1;
899         guint            uses_vtable_reg : 1;
900         gpointer         debug_info;
901         guint32          lmf_offset;
902     guint16          *intvars;
903         MonoProfileCoverageInfo *coverage_info;
904         GHashTable       *token_info_hash;
905         MonoCompileArch  arch;
906         guint32          exception_type;        /* MONO_EXCEPTION_* */
907         guint32          exception_data;
908         char*            exception_message;
909         gpointer         exception_ptr;
910
911         /* Fields used by the local reg allocator */
912         void*            reginfo;
913         int              reginfo_len;
914
915         /* Maps vregs to their associated MonoInst's */
916         /* vregs with an associated MonoInst are 'global' while others are 'local' */
917         MonoInst **vreg_to_inst;
918
919         /* Size of above array */
920         guint32 vreg_to_inst_len;
921
922         /* 
923          * The original method to compile, differs from 'method' when doing generic
924          * sharing.
925          */
926         MonoMethod *orig_method;
927
928         /* Patches which describe absolute addresses embedded into the native code */
929         GHashTable *abs_patches;
930 } MonoCompile;
931
932 typedef enum {
933         MONO_CFG_HAS_ALLOCA = 1 << 0,
934         MONO_CFG_HAS_CALLS  = 1 << 1,
935         MONO_CFG_HAS_LDELEMA  = 1 << 2,
936         MONO_CFG_HAS_VARARGS  = 1 << 3,
937         MONO_CFG_HAS_TAIL     = 1 << 4,
938         MONO_CFG_HAS_FPOUT    = 1 << 5, /* there are fp values passed in int registers */
939         MONO_CFG_HAS_SPILLUP  = 1 << 6, /* spill var slots are allocated from bottom to top */
940         MONO_CFG_HAS_CHECK_THIS  = 1 << 7,
941         MONO_CFG_HAS_ARRAY_ACCESS = 1 << 8
942 } MonoCompileFlags;
943
944 typedef struct {
945         gulong methods_compiled;
946         gulong methods_aot;
947         gulong methods_lookups;
948         gulong method_trampolines;
949         gulong allocate_var;
950         gulong analyze_stack_repeat;
951         gulong cil_code_size;
952         gulong native_code_size;
953         gulong code_reallocs;
954         gulong max_code_size_ratio;
955         gulong biggest_method_size;
956         gulong allocated_code_size;
957         gulong inlineable_methods;
958         gulong inlined_methods;
959         gulong basic_blocks;
960         gulong max_basic_blocks;
961         gulong locals_stack_size;
962         gulong regvars;
963         gulong cas_declsec_check;
964         gulong cas_linkdemand_icall;
965         gulong cas_linkdemand_pinvoke;
966         gulong cas_linkdemand_aptc;
967         gulong cas_linkdemand;
968         gulong cas_demand_generation;
969         gulong generic_virtual_invocations;
970         char *max_ratio_method;
971         char *biggest_method;
972         gboolean enabled;
973 } MonoJitStats;
974
975 extern MonoJitStats mono_jit_stats;
976
977 /* values for MonoInst.ssa_op */
978 enum {
979         MONO_SSA_NOP = 0,
980         MONO_SSA_ADDRESS_TAKEN = 1,
981         MONO_SSA_LOAD = 2,
982         MONO_SSA_STORE = 4,
983         MONO_SSA_LOAD_STORE = MONO_SSA_LOAD|MONO_SSA_STORE,
984         MONO_SSA_INDIRECT_LOAD = MONO_SSA_LOAD|MONO_SSA_ADDRESS_TAKEN,
985         MONO_SSA_INDIRECT_STORE = MONO_SSA_STORE|MONO_SSA_ADDRESS_TAKEN,
986         MONO_SSA_INDIRECT_LOAD_STORE =
987         MONO_SSA_LOAD|MONO_SSA_STORE|MONO_SSA_ADDRESS_TAKEN
988 };
989
990 /* opcodes: value assigned after all the CIL opcodes */
991 #ifdef MINI_OP
992 #undef MINI_OP
993 #endif
994 #define MINI_OP(a,b,dest,src1,src2) a,
995 enum {
996         OP_START = MONO_CEE_LAST - 1,
997 #include "mini-ops.h"
998         OP_LAST
999 };
1000 #undef MINI_OP
1001
1002 /* Can't use the same with both JITs since that would break the burg rules */
1003 #if defined(NEW_IR)
1004
1005 #if SIZEOF_VOID_P == 8
1006 #define OP_PCONST OP_I8CONST
1007 #define OP_PADD OP_LADD
1008 #define OP_PADD_IMM OP_LADD_IMM
1009 #define OP_PSUB OP_LSUB
1010 #define OP_PMUL OP_LMUL
1011 #define OP_PMUL_IMM OP_LMUL_IMM
1012 #define OP_PNEG OP_LNEG
1013 #define OP_PCONV_TO_I1 OP_LCONV_TO_I1
1014 #define OP_PCONV_TO_U1 OP_LCONV_TO_U1
1015 #define OP_PCONV_TO_I2 OP_LCONV_TO_I2
1016 #define OP_PCONV_TO_U2 OP_LCONV_TO_U2
1017 #define OP_PCONV_TO_OVF_I1_UN OP_LCONV_TO_OVF_I1_UN
1018 #define OP_PCONV_TO_OVF_I1 OP_LCONV_TO_OVF_I1
1019 #define OP_PBEQ OP_LBEQ
1020 #define OP_PCEQ OP_LCEQ
1021 #define OP_PBNE_UN OP_LBNE_UN
1022 #define OP_PBGE_UN OP_LBGE_UN
1023 #define OP_PBLT_UN OP_LBLT_UN
1024 #define OP_PBGE OP_LBGE
1025 #define OP_STOREP_MEMBASE_REG OP_STOREI8_MEMBASE_REG
1026 #define OP_STOREP_MEMBASE_IMM OP_STOREI8_MEMBASE_IMM
1027 #else
1028 #define OP_PCONST OP_ICONST
1029 #define OP_PADD OP_IADD
1030 #define OP_PADD_IMM OP_IADD_IMM
1031 #define OP_PSUB OP_ISUB
1032 #define OP_PMUL OP_IMUL
1033 #define OP_PMUL_IMM OP_IMUL_IMM
1034 #define OP_PNEG OP_INEG
1035 #define OP_PCONV_TO_U2 OP_ICONV_TO_U2
1036 #define OP_PCONV_TO_OVF_I1_UN OP_ICONV_TO_OVF_I1_UN
1037 #define OP_PCONV_TO_OVF_I1 OP_ICONV_TO_OVF_I1
1038 #define OP_PBEQ OP_IBEQ
1039 #define OP_PCEQ OP_ICEQ
1040 #define OP_PBNE_UN OP_IBNE_UN
1041 #define OP_PBGE_UN OP_IBGE_UN
1042 #define OP_PBLT_UN OP_IBLT_UN
1043 #define OP_PBGE OP_IBGE
1044 #define OP_STOREP_MEMBASE_REG OP_STOREI4_MEMBASE_REG
1045 #define OP_STOREP_MEMBASE_IMM OP_STOREI4_MEMBASE_IMM
1046 #endif
1047
1048 #else
1049
1050 #if SIZEOF_VOID_P == 8
1051 #define OP_PCONST OP_I8CONST
1052 #define OP_PADD OP_LADD
1053 #define OP_PADD_IMM OP_LADD_IMM
1054 #define OP_PNEG OP_LNEG
1055 #define OP_PCONV_TO_I1 OP_LCONV_TO_I1
1056 #define OP_PCONV_TO_U1 OP_LCONV_TO_U1
1057 #define OP_PCONV_TO_I2 OP_LCONV_TO_I2
1058 #define OP_PCONV_TO_U2 OP_LCONV_TO_U2
1059 #define OP_PCONV_TO_OVF_I1_UN OP_LCONV_TO_OVF_I1_UN
1060 #define OP_PCONV_TO_OVF_I1 OP_LCONV_TO_OVF_I1
1061 #define OP_PBEQ OP_LBEQ
1062 #define OP_PCEQ CEE_CEQ
1063 #define OP_STOREP_MEMBASE_REG OP_STOREI8_MEMBASE_REG
1064 #define OP_STOREP_MEMBASE_IMM OP_STOREI8_MEMBASE_IMM
1065 #else
1066 #define OP_PCONST OP_ICONST
1067 #define OP_PADD CEE_ADD
1068 #define OP_PADD2 OP_IADD
1069 #define OP_PNEG CEE_NEG
1070 #define OP_PCONV_TO_I1 OP_ICONV_TO_I1
1071 #define OP_PCONV_TO_U1 OP_ICONV_TO_U1
1072 #define OP_PCONV_TO_I2 OP_ICONV_TO_I2
1073 #define OP_PCONV_TO_U2 CEE_CONV_U2
1074 #define OP_PCONV_TO_OVF_I1_UN CEE_CONV_OVF_I1_UN
1075 #define OP_PCONV_TO_OVF_I1 CEE_CONV_OVF_I1
1076 #define OP_PBEQ OP_IBEQ
1077 #define OP_PCEQ CEE_CEQ
1078 #define OP_STOREP_MEMBASE_REG OP_STOREI4_MEMBASE_REG
1079 #define OP_STOREP_MEMBASE_IMM OP_STOREI4_MEMBASE_IMM
1080 #endif
1081
1082 #endif
1083
1084 typedef enum {
1085         STACK_INV,
1086         STACK_I4,
1087         STACK_I8,
1088         STACK_PTR,
1089         STACK_R8,
1090         STACK_MP,
1091         STACK_OBJ,
1092         STACK_VTYPE,
1093         STACK_MAX
1094 } MonoStackType;
1095
1096 typedef struct {
1097         union {
1098                 double   r8;
1099                 gint32   i4;
1100                 gint64   i8;
1101                 gpointer p;
1102                 MonoClass *klass;
1103         } data;
1104         int type;
1105 } StackSlot;
1106
1107 #if HAVE_ARRAY_ELEM_INIT
1108 extern const guint8 mono_burg_arity [];
1109 #else
1110 extern guint8 mono_burg_arity [];
1111 #endif
1112
1113 extern const char MONO_ARCH_CPU_SPEC [] MONO_INTERNAL;
1114 #define MONO_ARCH_CPU_SPEC_IDX_COMBINE(a) a ## _idx
1115 #define MONO_ARCH_CPU_SPEC_IDX(a) MONO_ARCH_CPU_SPEC_IDX_COMBINE(a)
1116 extern const guint16 MONO_ARCH_CPU_SPEC_IDX(MONO_ARCH_CPU_SPEC) [] MONO_INTERNAL;
1117 #define ins_get_spec(op) ((const char*)&MONO_ARCH_CPU_SPEC + MONO_ARCH_CPU_SPEC_IDX(MONO_ARCH_CPU_SPEC)[(op)])
1118
1119 enum {
1120         MONO_COMP_DOM = 1,
1121         MONO_COMP_IDOM = 2,
1122         MONO_COMP_DFRONTIER = 4,
1123         MONO_COMP_DOM_REV = 8,
1124         MONO_COMP_LIVENESS = 16,
1125         MONO_COMP_SSA = 32,
1126         MONO_COMP_SSA_DEF_USE = 64,
1127         MONO_COMP_REACHABILITY = 128,
1128         MONO_COMP_LOOPS = 256
1129 };
1130
1131 typedef enum {
1132         MONO_GRAPH_CFG = 1,
1133         MONO_GRAPH_DTREE = 2,
1134         MONO_GRAPH_CFG_CODE = 4,
1135         MONO_GRAPH_CFG_SSA = 8,
1136         MONO_GRAPH_CFG_OPTCODE = 16
1137 } MonoGraphOptions;
1138
1139 typedef struct {
1140         guint16 size;
1141         guint16 offset;
1142         guint8  pad;
1143 } MonoJitArgumentInfo;
1144
1145 typedef struct {
1146         gboolean handle_sigint;
1147         gboolean keep_delegates;
1148         gboolean collect_pagefault_stats;
1149         gboolean break_on_unverified;
1150         gboolean better_cast_details;
1151         gboolean mdb_optimizations;
1152         gboolean no_gdb_backtrace;
1153 } MonoDebugOptions;
1154
1155 enum {
1156         BRANCH_NOT_TAKEN,
1157         BRANCH_TAKEN,
1158         BRANCH_UNDEF
1159 };
1160
1161 typedef enum {
1162         CMP_EQ,
1163         CMP_NE,
1164         CMP_LE,
1165         CMP_GE,
1166         CMP_LT,
1167         CMP_GT,
1168         CMP_LE_UN,
1169         CMP_GE_UN,
1170         CMP_LT_UN,
1171         CMP_GT_UN
1172 } CompRelation;
1173
1174 typedef enum {
1175         CMP_TYPE_L,
1176         CMP_TYPE_I,
1177         CMP_TYPE_F
1178 } CompType;
1179
1180 /* Implicit exceptions */
1181 enum {
1182         MONO_EXC_INDEX_OUT_OF_RANGE,
1183         MONO_EXC_OVERFLOW,
1184         MONO_EXC_ARITHMETIC,
1185         MONO_EXC_DIVIDE_BY_ZERO,
1186         MONO_EXC_INVALID_CAST,
1187         MONO_EXC_NULL_REF,
1188         MONO_EXC_ARRAY_TYPE_MISMATCH,
1189         MONO_EXC_INTRINS_NUM
1190 };
1191
1192 enum {
1193         MINI_TOKEN_SOURCE_CLASS,
1194         MINI_TOKEN_SOURCE_METHOD,
1195         MINI_TOKEN_SOURCE_FIELD
1196 };
1197
1198 typedef void (*MonoInstFunc) (MonoInst *tree, gpointer data);
1199
1200 /* main function */
1201 int         mono_main                      (int argc, char* argv[]);
1202 void        mono_set_defaults              (int verbose_level, guint32 opts);
1203 MonoDomain* mini_init                      (const char *filename, const char *runtime_version) MONO_INTERNAL;
1204 void        mini_cleanup                   (MonoDomain *domain) MONO_INTERNAL;
1205 MonoDebugOptions *mini_get_debug_options   (void) MONO_INTERNAL;
1206
1207 /* helper methods */
1208 MonoJumpInfoToken * mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token) MONO_INTERNAL;
1209 MonoInst* mono_find_spvar_for_region        (MonoCompile *cfg, int region) MONO_INTERNAL;
1210 void      mono_precompile_assemblies        (void) MONO_INTERNAL;
1211 int       mono_parse_default_optimizations  (const char* p);
1212 void      mono_bblock_add_inst              (MonoBasicBlock *bb, MonoInst *inst) MONO_INTERNAL;
1213 void      mono_bblock_insert_after_ins      (MonoBasicBlock *bb, MonoInst *ins, MonoInst *ins_to_insert) MONO_INTERNAL;
1214 void      mono_bblock_insert_before_ins     (MonoBasicBlock *bb, MonoInst *ins, MonoInst *ins_to_insert) MONO_INTERNAL;
1215 void      mono_verify_bblock                (MonoBasicBlock *bb) MONO_INTERNAL;
1216 void      mono_verify_cfg                   (MonoCompile *cfg) MONO_INTERNAL;
1217 void      mono_constant_fold                (MonoCompile *cfg) MONO_INTERNAL;
1218 void      mono_constant_fold_inst           (MonoInst *inst, gpointer data) MONO_INTERNAL;
1219 MonoInst* mono_constant_fold_ins2           (MonoCompile *cfg, MonoInst *ins, MonoInst *arg1, MonoInst *arg2, gboolean overwrite) MONO_INTERNAL;
1220 int       mono_eval_cond_branch             (MonoInst *branch) MONO_INTERNAL;
1221 int       mono_is_power_of_two              (guint32 val) MONO_INTERNAL;
1222 void      mono_cprop_local                  (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size) MONO_INTERNAL;
1223 MonoInst* mono_compile_create_var           (MonoCompile *cfg, MonoType *type, int opcode) MONO_INTERNAL;
1224 MonoInst* mono_compile_create_var_for_vreg  (MonoCompile *cfg, MonoType *type, int opcode, int vreg) MONO_INTERNAL;
1225 void      mono_compile_make_var_load        (MonoCompile *cfg, MonoInst *dest, gssize var_index) MONO_INTERNAL;
1226 MonoInst* mono_compile_create_var_load      (MonoCompile *cfg, gssize var_index) MONO_INTERNAL;
1227 MonoInst* mono_compile_create_var_store     (MonoCompile *cfg, gssize var_index, MonoInst *value) MONO_INTERNAL;
1228 MonoType* mono_type_from_stack_type         (MonoInst *ins) MONO_INTERNAL;
1229 guint32   mono_alloc_ireg                   (MonoCompile *cfg) MONO_INTERNAL;
1230 guint32   mono_alloc_freg                   (MonoCompile *cfg) MONO_INTERNAL;
1231 guint32   mono_alloc_preg                   (MonoCompile *cfg) MONO_INTERNAL;
1232 guint32   mono_alloc_dreg                   (MonoCompile *cfg, MonoStackType stack_type) MONO_INTERNAL;
1233
1234 void      mono_link_bblock                  (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to) MONO_INTERNAL;
1235 void      mono_unlink_bblock                (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to) MONO_INTERNAL;
1236 gboolean  mono_bblocks_linked               (MonoBasicBlock *bb1, MonoBasicBlock *bb2) MONO_INTERNAL;
1237 void      mono_remove_bblock                (MonoCompile *cfg, MonoBasicBlock *bb) MONO_INTERNAL;
1238 void      mono_nullify_basic_block          (MonoBasicBlock *bb) MONO_INTERNAL;
1239 void      mono_merge_basic_blocks           (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *bbn) MONO_INTERNAL;
1240 void      mono_optimize_branches            (MonoCompile *cfg) MONO_INTERNAL;
1241
1242 void      mono_blockset_print               (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom) MONO_INTERNAL;
1243 void      mono_print_tree                   (MonoInst *tree) MONO_INTERNAL;
1244 void      mono_print_tree_nl                (MonoInst *tree) MONO_INTERNAL;
1245 void      mono_print_ins_index              (int i, MonoInst *ins) MONO_INTERNAL;
1246 void      mono_print_ins                    (MonoInst *ins) MONO_INTERNAL;
1247 void      mono_print_bb                     (MonoBasicBlock *bb, const char *msg) MONO_INTERNAL;
1248 void      mono_print_code                   (MonoCompile *cfg, const char *msg) MONO_INTERNAL;
1249 void      mono_print_method_from_ip         (void *ip);
1250 char     *mono_pmip                         (void *ip);
1251 void      mono_select_instructions          (MonoCompile *cfg) MONO_INTERNAL;
1252 const char* mono_inst_name                  (int op);
1253 int       mono_op_to_op_imm                 (int opcode) MONO_INTERNAL;
1254 int       mono_op_imm_to_op                 (int opcode) MONO_INTERNAL;
1255 int       mono_load_membase_to_load_mem     (int opcode) MONO_INTERNAL;
1256 guint     mono_type_to_load_membase         (MonoCompile *cfg, MonoType *type) MONO_INTERNAL;
1257 guint     mono_type_to_store_membase        (MonoCompile *cfg, MonoType *type) MONO_INTERNAL;
1258 guint     mini_type_to_stind                (MonoCompile* cfg, MonoType *type) MONO_INTERNAL;
1259 guint32   mono_reverse_branch_op            (guint32 opcode) MONO_INTERNAL;
1260 void      mono_inst_foreach                 (MonoInst *tree, MonoInstFunc func, gpointer data) MONO_INTERNAL;
1261 void      mono_disassemble_code             (MonoCompile *cfg, guint8 *code, int size, char *id) MONO_INTERNAL;
1262 void      mono_add_patch_info               (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target) MONO_INTERNAL;
1263 void      mono_remove_patch_info            (MonoCompile *cfg, int ip) MONO_INTERNAL;
1264 MonoJumpInfo* mono_patch_info_dup_mp        (MonoMemPool *mp, MonoJumpInfo *patch_info) MONO_INTERNAL;
1265 guint     mono_patch_info_hash (gconstpointer data) MONO_INTERNAL;
1266 gint      mono_patch_info_equal (gconstpointer ka, gconstpointer kb) MONO_INTERNAL;
1267 MonoJumpInfo *mono_patch_info_list_prepend  (MonoJumpInfo *list, int ip, MonoJumpInfoType type, gconstpointer target) MONO_INTERNAL;
1268 gpointer  mono_resolve_patch_target         (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors) MONO_INTERNAL;
1269 gpointer  mono_jit_find_compiled_method     (MonoDomain *domain, MonoMethod *method) MONO_INTERNAL;
1270 MonoLMF * mono_get_lmf                      (void) MONO_INTERNAL;
1271 MonoLMF** mono_get_lmf_addr                 (void) MONO_INTERNAL;
1272 void      mono_jit_thread_attach            (MonoDomain *domain);
1273 guint32   mono_get_jit_tls_key              (void) MONO_INTERNAL;
1274 gint32    mono_get_jit_tls_offset           (void) MONO_INTERNAL;
1275 gint32    mono_get_lmf_tls_offset           (void) MONO_INTERNAL;
1276 gint32    mono_get_lmf_addr_tls_offset      (void) MONO_INTERNAL;
1277 MonoInst* mono_get_jit_tls_intrinsic        (MonoCompile *cfg) MONO_INTERNAL;
1278 GList    *mono_varlist_insert_sorted        (MonoCompile *cfg, GList *list, MonoMethodVar *mv, gboolean sort_end) MONO_INTERNAL;
1279 GList    *mono_varlist_sort                 (MonoCompile *cfg, GList *list, int sort_type) MONO_INTERNAL;
1280 void      mono_analyze_liveness             (MonoCompile *cfg) MONO_INTERNAL;
1281 void      mono_linear_scan                  (MonoCompile *cfg, GList *vars, GList *regs, regmask_t *used_mask) MONO_INTERNAL;
1282 void      mono_global_regalloc              (MonoCompile *cfg) MONO_INTERNAL;
1283 void      mono_create_jump_table            (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks) MONO_INTERNAL;
1284 int       mono_compile_assembly             (MonoAssembly *ass, guint32 opts, const char *aot_options) MONO_INTERNAL;
1285 MonoCompile *mini_method_compile            (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, gboolean compile_aot, int parts) MONO_INTERNAL;
1286 void      mono_destroy_compile              (MonoCompile *cfg) MONO_INTERNAL;
1287 MonoJitICallInfo *mono_find_jit_opcode_emulation (int opcode) MONO_INTERNAL;
1288 void      mono_print_ins_index (int i, MonoInst *ins) MONO_INTERNAL;
1289 void      mono_print_ins (MonoInst *ins) MONO_INTERNAL;
1290 gboolean  mini_assembly_can_skip_verification (MonoDomain *domain, MonoMethod *method) MONO_INTERNAL;
1291 gboolean  mini_method_verify (MonoCompile *cfg, MonoMethod *method) MONO_INTERNAL;
1292
1293 gboolean  mini_class_is_system_array (MonoClass *klass) MONO_INTERNAL;
1294 MonoMethodSignature *mono_get_element_address_signature (int arity) MONO_INTERNAL;
1295 MonoJitICallInfo    *mono_get_element_address_icall (int rank) MONO_INTERNAL;
1296 MonoJitICallInfo    *mono_get_array_new_va_icall (int rank) MONO_INTERNAL;
1297
1298 void      mono_linterval_add_range          (MonoCompile *cfg, MonoLiveInterval *interval, int from, int to) MONO_INTERNAL;
1299 void      mono_linterval_print              (MonoLiveInterval *interval) MONO_INTERNAL;
1300 void      mono_linterval_print_nl (MonoLiveInterval *interval) MONO_INTERNAL;
1301 gboolean  mono_linterval_covers             (MonoLiveInterval *interval, int pos) MONO_INTERNAL;
1302 gint32    mono_linterval_get_intersect_pos  (MonoLiveInterval *i1, MonoLiveInterval *i2) MONO_INTERNAL;
1303 void      mono_linterval_split              (MonoCompile *cfg, MonoLiveInterval *interval, MonoLiveInterval **i1, MonoLiveInterval **i2, int pos) MONO_INTERNAL;
1304 void      mono_liveness_handle_exception_clauses (MonoCompile *cfg) MONO_INTERNAL;
1305
1306 /* AOT */
1307 void      mono_aot_init                     (void) MONO_INTERNAL;
1308 gpointer  mono_aot_get_method               (MonoDomain *domain,
1309                                                                                          MonoMethod *method) MONO_INTERNAL;
1310 gpointer  mono_aot_get_method_from_token    (MonoDomain *domain, MonoImage *image, guint32 token) MONO_INTERNAL;
1311 gboolean  mono_aot_is_got_entry             (guint8 *code, guint8 *addr) MONO_INTERNAL;
1312 guint8*   mono_aot_get_plt_entry            (guint8 *code) MONO_INTERNAL;
1313 guint32   mono_aot_get_plt_info_offset      (gssize *regs, guint8 *code) MONO_INTERNAL;
1314 gboolean  mono_aot_get_cached_class_info    (MonoClass *klass, MonoCachedClassInfo *res) MONO_INTERNAL;
1315 gboolean  mono_aot_get_class_from_name      (MonoImage *image, const char *name_space, const char *name, MonoClass **klass) MONO_INTERNAL;
1316 MonoJitInfo* mono_aot_find_jit_info         (MonoDomain *domain, MonoImage *image, gpointer addr) MONO_INTERNAL;
1317 void mono_aot_set_make_unreadable           (gboolean unreadable) MONO_INTERNAL;
1318 gboolean mono_aot_is_pagefault              (void *ptr) MONO_INTERNAL;
1319 void mono_aot_handle_pagefault              (void *ptr) MONO_INTERNAL;
1320 guint32 mono_aot_get_n_pagefaults           (void) MONO_INTERNAL;
1321 gpointer mono_aot_plt_resolve               (gpointer aot_module, guint32 plt_info_offset, guint8 *code) MONO_INTERNAL;
1322 gpointer mono_aot_get_method_from_vt_slot   (MonoDomain *domain, MonoVTable *vtable, int slot) MONO_INTERNAL;
1323 gpointer mono_aot_create_specific_trampoline   (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len) MONO_INTERNAL;
1324 gpointer mono_aot_get_named_code            (const char *name) MONO_INTERNAL;
1325 gpointer mono_aot_get_unbox_trampoline      (MonoMethod *method) MONO_INTERNAL;
1326 gpointer mono_aot_get_lazy_fetch_trampoline (guint32 slot) MONO_INTERNAL;
1327 /* This is an exported function */
1328 void     mono_aot_register_globals          (gpointer *globals);
1329 /* This too */
1330 void     mono_aot_register_module           (gpointer *aot_info);
1331
1332 gboolean  mono_method_blittable             (MonoMethod *method) MONO_INTERNAL;
1333 gboolean  mono_method_same_domain           (MonoJitInfo *caller, MonoJitInfo *callee) MONO_INTERNAL;
1334
1335 void      mono_register_opcode_emulation    (int opcode, const char* name, const char *sigstr, gpointer func, gboolean no_throw) MONO_INTERNAL;
1336 void      mono_draw_graph                   (MonoCompile *cfg, MonoGraphOptions draw_options) MONO_INTERNAL;
1337 void      mono_add_varcopy_to_end           (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest) MONO_INTERNAL;
1338 void      mono_add_ins_to_end               (MonoBasicBlock *bb, MonoInst *inst) MONO_INTERNAL;
1339 gpointer  mono_create_ftnptr                (MonoDomain *domain, gpointer addr) MONO_INTERNAL;
1340
1341 void      mono_replace_ins                  (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **prev, MonoBasicBlock *first_bb, MonoBasicBlock *last_bb);
1342
1343 int               mono_find_method_opcode      (MonoMethod *method) MONO_INTERNAL;
1344 MonoJitICallInfo *mono_find_jit_icall_by_name  (const char *name) MONO_INTERNAL;
1345 MonoJitICallInfo *mono_find_jit_icall_by_addr  (gconstpointer addr) MONO_INTERNAL;
1346 MonoJitICallInfo *mono_register_jit_icall      (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save) MONO_INTERNAL;
1347 gconstpointer     mono_icall_get_wrapper       (MonoJitICallInfo* callinfo) MONO_INTERNAL;
1348
1349 void              mono_trampolines_init (void) MONO_INTERNAL;
1350 void              mono_trampolines_cleanup (void) MONO_INTERNAL;
1351 guint8 *          mono_get_trampoline_code (MonoTrampolineType tramp_type) MONO_INTERNAL;
1352 gpointer          mono_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len) MONO_INTERNAL;
1353 gpointer          mono_create_jump_trampoline (MonoDomain *domain, 
1354                                                                                            MonoMethod *method, 
1355                                                                                            gboolean add_sync_wrapper) MONO_INTERNAL;
1356 gpointer          mono_create_class_init_trampoline (MonoVTable *vtable) MONO_INTERNAL;
1357 gpointer          mono_create_generic_class_init_trampoline (void) MONO_INTERNAL;
1358 gpointer          mono_create_jit_trampoline (MonoMethod *method) MONO_INTERNAL;
1359 gpointer          mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token) MONO_INTERNAL;
1360 gpointer          mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method) MONO_INTERNAL;
1361 gpointer          mono_create_delegate_trampoline (MonoClass *klass) MONO_INTERNAL;
1362 gpointer          mono_create_rgctx_lazy_fetch_trampoline (guint32 offset) MONO_INTERNAL;
1363 MonoVTable*       mono_find_class_init_trampoline_by_addr (gconstpointer addr) MONO_INTERNAL;
1364 MonoClass*        mono_find_delegate_trampoline_by_addr (gconstpointer addr) MONO_INTERNAL;
1365 guint32           mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr) MONO_INTERNAL;
1366 gpointer          mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp) MONO_INTERNAL;
1367 gpointer          mono_delegate_trampoline (gssize *regs, guint8 *code, gpointer *tramp_data, guint8* tramp) MONO_INTERNAL;
1368 gpointer          mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info, 
1369                                                                            guint8* tramp) MONO_INTERNAL;
1370 gpointer          mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *token_info, 
1371                                                                                    guint8* tramp) MONO_INTERNAL;
1372 void              mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp) MONO_INTERNAL;
1373 void              mono_generic_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp) MONO_INTERNAL;
1374 gconstpointer     mono_get_trampoline_func (MonoTrampolineType tramp_type);
1375 gpointer          mini_get_vtable_trampoline (void) MONO_INTERNAL;
1376
1377 gboolean          mono_running_on_valgrind (void) MONO_INTERNAL;
1378 void*             mono_global_codeman_reserve (int size) MONO_INTERNAL;
1379 const char       *mono_regname_full (int reg, gboolean fp) MONO_INTERNAL;
1380 gint32*           mono_allocate_stack_slots_full (MonoCompile *cfg, gboolean backward, guint32 *stack_size, guint32 *stack_align) MONO_INTERNAL;
1381 gint32*           mono_allocate_stack_slots (MonoCompile *cfg, guint32 *stack_size, guint32 *stack_align) MONO_INTERNAL;
1382 void              mono_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb) MONO_INTERNAL;
1383 MonoInst         *mono_branch_optimize_exception_target (MonoCompile *cfg, MonoBasicBlock *bb, const char * exname) MONO_INTERNAL;
1384 void              mono_remove_critical_edges (MonoCompile *cfg) MONO_INTERNAL;
1385 gboolean          mono_is_regsize_var (MonoType *t) MONO_INTERNAL;
1386 void              mini_emit_memcpy2 (MonoCompile *cfg, int destreg, int doffset, int srcreg, int soffset, int size, int align) MONO_INTERNAL;
1387 CompRelation      mono_opcode_to_cond (int opcode) MONO_INTERNAL;
1388 CompType          mono_opcode_to_type (int opcode, int cmp_opcode) MONO_INTERNAL;
1389 CompRelation      mono_negate_cond (CompRelation cond) MONO_INTERNAL;
1390 int               mono_op_imm_to_op (int opcode) MONO_INTERNAL;
1391 void              mono_decompose_op_imm (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins) MONO_INTERNAL;
1392 void              mono_peephole_ins (MonoBasicBlock *bb, MonoInst *ins) MONO_INTERNAL;
1393
1394 void              mono_decompose_opcode (MonoCompile *cfg, MonoInst *ins) MONO_INTERNAL;
1395 void              mono_decompose_long_opts (MonoCompile *cfg) MONO_INTERNAL;
1396 void              mono_decompose_vtype_opts (MonoCompile *cfg) MONO_INTERNAL;
1397 void              mono_decompose_array_access_opts (MonoCompile *cfg) MONO_INTERNAL;
1398 void              mono_handle_soft_float (MonoCompile *cfg) MONO_INTERNAL;
1399 void              mono_handle_global_vregs (MonoCompile *cfg) MONO_INTERNAL;
1400 void              mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts) MONO_INTERNAL;
1401 void              mono_if_conversion (MonoCompile *cfg) MONO_INTERNAL;
1402
1403 /* methods that must be provided by the arch-specific port */
1404 void      mono_arch_init                        (void) MONO_INTERNAL;
1405 void      mono_arch_cleanup                     (void) MONO_INTERNAL;
1406 void      mono_arch_cpu_init                    (void) MONO_INTERNAL;
1407 guint32   mono_arch_cpu_optimizazions           (guint32 *exclude_mask) MONO_INTERNAL;
1408 void      mono_arch_instrument_mem_needs        (MonoMethod *method, int *stack, int *code) MONO_INTERNAL;
1409 void     *mono_arch_instrument_prolog           (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments) MONO_INTERNAL;
1410 void     *mono_arch_instrument_epilog           (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments) MONO_INTERNAL;
1411 MonoCallInst *mono_arch_call_opcode             (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) MONO_INTERNAL;
1412 MonoInst *mono_arch_get_inst_for_method         (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args) MONO_INTERNAL;
1413 void      mono_codegen                          (MonoCompile *cfg) MONO_INTERNAL;
1414 void      mono_call_inst_add_outarg_reg         (MonoCompile *cfg, MonoCallInst *call, int vreg, int hreg, gboolean fp) MONO_INTERNAL;
1415 const char *mono_arch_regname                   (int reg) MONO_INTERNAL;
1416 const char *mono_arch_fregname                  (int reg) MONO_INTERNAL;
1417 gpointer  mono_arch_get_throw_exception         (void) MONO_INTERNAL;
1418 gpointer  mono_arch_get_rethrow_exception       (void) MONO_INTERNAL;
1419 gpointer  mono_arch_get_throw_exception_by_name (void) MONO_INTERNAL;
1420 gpointer  mono_arch_get_throw_corlib_exception  (void) MONO_INTERNAL;
1421 guchar*   mono_arch_create_trampoline_code      (MonoTrampolineType tramp_type) MONO_INTERNAL;
1422 guchar*   mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *code_size, MonoJumpInfo **ji, gboolean aot) MONO_INTERNAL;
1423 gpointer  mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot) MONO_INTERNAL;
1424 gpointer  mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_size, MonoJumpInfo **ji, gboolean aot) MONO_INTERNAL;
1425 gpointer  mono_arch_create_generic_class_init_trampoline (void) MONO_INTERNAL;
1426 gpointer  mono_arch_get_nullified_class_init_trampoline (guint32 *code_len) MONO_INTERNAL;
1427 GList    *mono_arch_get_allocatable_int_vars    (MonoCompile *cfg) MONO_INTERNAL;
1428 GList    *mono_arch_get_global_int_regs         (MonoCompile *cfg) MONO_INTERNAL;
1429 GList    *mono_arch_get_global_fp_regs          (MonoCompile *cfg) MONO_INTERNAL;
1430 GList    *mono_arch_get_iregs_clobbered_by_call (MonoCallInst *call) MONO_INTERNAL;
1431 GList    *mono_arch_get_fregs_clobbered_by_call (MonoCallInst *call) MONO_INTERNAL;
1432 guint32   mono_arch_regalloc_cost               (MonoCompile *cfg, MonoMethodVar *vmv) MONO_INTERNAL;
1433 void      mono_arch_patch_code                  (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors) MONO_INTERNAL;
1434 void      mono_arch_flush_icache                (guint8 *code, gint size) MONO_INTERNAL;
1435 int       mono_arch_max_epilog_size             (MonoCompile *cfg) MONO_INTERNAL;
1436 guint8   *mono_arch_emit_prolog                 (MonoCompile *cfg) MONO_INTERNAL;
1437 void      mono_arch_emit_epilog                 (MonoCompile *cfg) MONO_INTERNAL;
1438 void      mono_arch_emit_exceptions             (MonoCompile *cfg) MONO_INTERNAL;
1439 void      mono_arch_lowering_pass               (MonoCompile *cfg, MonoBasicBlock *bb) MONO_INTERNAL;
1440 void      mono_arch_peephole_pass_1             (MonoCompile *cfg, MonoBasicBlock *bb) MONO_INTERNAL;
1441 void      mono_arch_peephole_pass_2             (MonoCompile *cfg, MonoBasicBlock *bb) MONO_INTERNAL;
1442 void      mono_arch_output_basic_block          (MonoCompile *cfg, MonoBasicBlock *bb) MONO_INTERNAL;
1443 gboolean  mono_arch_has_unwind_info             (gconstpointer addr) MONO_INTERNAL;
1444 void      mono_arch_setup_jit_tls_data          (MonoJitTlsData *tls) MONO_INTERNAL;
1445 void      mono_arch_free_jit_tls_data           (MonoJitTlsData *tls) MONO_INTERNAL;
1446 void      mono_arch_emit_this_vret_args         (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg) MONO_INTERNAL;
1447 void      mono_arch_fill_argument_info          (MonoCompile *cfg) MONO_INTERNAL;
1448 void      mono_arch_allocate_vars               (MonoCompile *m) MONO_INTERNAL;
1449 int       mono_arch_get_argument_info           (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info) MONO_INTERNAL;
1450 gboolean  mono_arch_print_tree                  (MonoInst *tree, int arity) MONO_INTERNAL;
1451 void      mono_arch_emit_call                   (MonoCompile *cfg, MonoCallInst *call) MONO_INTERNAL;
1452 void      mono_arch_emit_outarg_vt              (MonoCompile *cfg, MonoInst *ins, MonoInst *src) MONO_INTERNAL;
1453 void      mono_arch_emit_setret                 (MonoCompile *cfg, MonoMethod *method, MonoInst *val) MONO_INTERNAL;
1454 MonoInst *mono_arch_emit_inst_for_method        (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args) MONO_INTERNAL;
1455 void      mono_arch_decompose_opts              (MonoCompile *cfg, MonoInst *ins) MONO_INTERNAL;
1456 void      mono_arch_decompose_long_opts         (MonoCompile *cfg, MonoInst *ins) MONO_INTERNAL;
1457
1458 MonoJitInfo *mono_arch_find_jit_info            (MonoDomain *domain, 
1459                                                  MonoJitTlsData *jit_tls, 
1460                                                  MonoJitInfo *res, 
1461                                                  MonoJitInfo *prev_ji, 
1462                                                  MonoContext *ctx, 
1463                                                  MonoContext *new_ctx, 
1464                                                  MonoLMF **lmf, 
1465                                                  gboolean *managed) MONO_INTERNAL;
1466 gpointer mono_arch_get_call_filter              (void) MONO_INTERNAL;
1467 gpointer mono_arch_get_restore_context          (void) MONO_INTERNAL;
1468 gpointer mono_arch_get_call_filter_full         (guint32 *code_size, MonoJumpInfo **ji, gboolean aot) MONO_INTERNAL;
1469 gpointer mono_arch_get_restore_context_full     (guint32 *code_size, MonoJumpInfo **ji, gboolean aot) MONO_INTERNAL;
1470 gpointer  mono_arch_get_throw_exception_full    (guint32 *code_size, MonoJumpInfo **ji, gboolean aot) MONO_INTERNAL;
1471 gpointer  mono_arch_get_rethrow_exception_full  (guint32 *code_size, MonoJumpInfo **ji, gboolean aot) MONO_INTERNAL;
1472 gpointer  mono_arch_get_throw_exception_by_name_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot) MONO_INTERNAL;
1473 gpointer  mono_arch_get_throw_corlib_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot) MONO_INTERNAL;
1474 gboolean mono_arch_handle_exception             (void *sigctx, gpointer obj, gboolean test_only) MONO_INTERNAL;
1475 void     mono_arch_handle_altstack_exception    (void *sigctx, gpointer fault_addr, gboolean stack_ovf) MONO_INTERNAL;
1476 gpointer mono_arch_ip_from_context              (void *sigctx) MONO_INTERNAL;
1477 void     mono_arch_sigctx_to_monoctx            (void *sigctx, MonoContext *ctx) MONO_INTERNAL;
1478 void     mono_arch_monoctx_to_sigctx            (MonoContext *mctx, void *ctx) MONO_INTERNAL;
1479 gpointer mono_arch_context_get_int_reg          (MonoContext *ctx, int reg) MONO_INTERNAL;
1480 void     mono_arch_flush_register_windows       (void) MONO_INTERNAL;
1481 gboolean mono_arch_is_inst_imm                  (gint64 imm) MONO_INTERNAL;
1482 MonoInst* mono_arch_get_domain_intrinsic        (MonoCompile* cfg) MONO_INTERNAL;
1483 MonoInst* mono_arch_get_thread_intrinsic        (MonoCompile* cfg) MONO_INTERNAL;
1484 gboolean mono_arch_is_int_overflow              (void *sigctx, void *info) MONO_INTERNAL;
1485 void     mono_arch_invalidate_method            (MonoJitInfo *ji, void *func, gpointer func_arg) MONO_INTERNAL;
1486 guint32  mono_arch_get_patch_offset             (guint8 *code) MONO_INTERNAL;
1487 gpointer*mono_arch_get_vcall_slot_addr          (guint8* code, gpointer *regs) MONO_INTERNAL;
1488 gpointer mono_arch_get_vcall_slot               (guint8 *code, gpointer *regs, int *displacement) MONO_INTERNAL;
1489 gpointer*mono_arch_get_delegate_method_ptr_addr (guint8* code, gpointer *regs) MONO_INTERNAL;
1490 void     mono_arch_create_vars                  (MonoCompile *cfg) MONO_INTERNAL;
1491 void     mono_arch_save_unwind_info             (MonoCompile *cfg) MONO_INTERNAL;
1492 void     mono_arch_register_lowlevel_calls      (void) MONO_INTERNAL;
1493 gpointer mono_arch_get_unbox_trampoline         (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr) MONO_INTERNAL;
1494 void     mono_arch_patch_callsite               (guint8 *method_start, guint8 *code, guint8 *addr) MONO_INTERNAL;
1495 void     mono_arch_patch_plt_entry              (guint8 *code, guint8 *addr) MONO_INTERNAL;
1496 void     mono_arch_nullify_class_init_trampoline(guint8 *code, gssize *regs) MONO_INTERNAL;
1497 void     mono_arch_nullify_plt_entry            (guint8 *code) MONO_INTERNAL;
1498 int      mono_arch_get_this_arg_reg             (MonoMethodSignature *sig, MonoGenericSharingContext *gsctx, guint8 *code) MONO_INTERNAL;
1499 gpointer mono_arch_get_this_arg_from_call       (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, gssize *regs, guint8 *code) MONO_INTERNAL;
1500 MonoObject* mono_arch_find_this_argument        (gpointer *regs, MonoMethod *method, MonoGenericSharingContext *gsctx) MONO_INTERNAL;
1501 gpointer mono_arch_get_delegate_invoke_impl     (MonoMethodSignature *sig, gboolean has_target) MONO_INTERNAL;
1502 gpointer mono_arch_create_specific_trampoline   (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len) MONO_INTERNAL;
1503 void        mono_arch_emit_imt_argument         (MonoCompile *cfg, MonoCallInst *call, MonoInst *imt_arg) MONO_INTERNAL;
1504 MonoMethod* mono_arch_find_imt_method           (gpointer *regs, guint8 *code) MONO_INTERNAL;
1505 MonoVTable* mono_arch_find_static_call_vtable   (gpointer *regs, guint8 *code) MONO_INTERNAL;
1506 gpointer    mono_arch_build_imt_thunk           (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count) MONO_INTERNAL;
1507 void    mono_arch_notify_pending_exc            (void) MONO_INTERNAL;
1508 void    mono_arch_fixup_jinfo                   (MonoCompile *cfg) MONO_INTERNAL;
1509
1510 /* Exception handling */
1511 void     mono_exceptions_init                   (void) MONO_INTERNAL;
1512 gboolean mono_handle_exception                  (MonoContext *ctx, gpointer obj,
1513                                                  gpointer original_ip, gboolean test_only) MONO_INTERNAL;
1514 void     mono_handle_native_sigsegv             (int signal, void *sigctx) MONO_INTERNAL;
1515 void     mono_print_thread_dump                 (void *sigctx);
1516 void     mono_jit_walk_stack                    (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) MONO_INTERNAL;
1517 void     mono_jit_walk_stack_from_ctx           (MonoStackWalk func, MonoContext *ctx, gboolean do_il_offset, gpointer user_data) MONO_INTERNAL;
1518 void     mono_setup_altstack                    (MonoJitTlsData *tls) MONO_INTERNAL;
1519 void     mono_free_altstack                     (MonoJitTlsData *tls) MONO_INTERNAL;
1520 MonoJitInfo * mono_find_jit_info                (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx, MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset, gboolean *managed) MONO_INTERNAL;
1521
1522 gpointer mono_get_throw_exception               (void) MONO_INTERNAL;
1523 gpointer mono_get_rethrow_exception             (void) MONO_INTERNAL;
1524 gpointer mono_get_call_filter                   (void) MONO_INTERNAL;
1525 gpointer mono_get_restore_context               (void) MONO_INTERNAL;
1526 gpointer mono_get_throw_exception_by_name       (void) MONO_INTERNAL;
1527 gpointer mono_get_throw_corlib_exception        (void) MONO_INTERNAL;
1528
1529 /* the new function to do stack walks */
1530 typedef gboolean (*MonoStackFrameWalk)          (MonoDomain *domain, MonoContext *ctx, MonoJitInfo *ji, gpointer data);
1531 void      mono_walk_stack                       (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoContext *start_ctx, MonoStackFrameWalk func, gpointer user_data);
1532
1533 MonoArray *ves_icall_get_trace                  (MonoException *exc, gint32 skip, MonoBoolean need_file_info) MONO_INTERNAL;
1534 MonoBoolean ves_icall_get_frame_info            (gint32 skip, MonoBoolean need_file_info, 
1535                                                  MonoReflectionMethod **method, 
1536                                                  gint32 *iloffset, gint32 *native_offset,
1537                                                  MonoString **file, gint32 *line, gint32 *column) MONO_INTERNAL;
1538 MonoString *ves_icall_System_Exception_get_trace (MonoException *exc) MONO_INTERNAL;
1539
1540 /* Dominator/SSA methods */
1541 void        mono_compile_dominator_info         (MonoCompile *cfg, int dom_flags) MONO_INTERNAL;
1542 void        mono_compute_natural_loops          (MonoCompile *cfg) MONO_INTERNAL;
1543 MonoBitSet* mono_compile_iterated_dfrontier     (MonoCompile *cfg, MonoBitSet *set) MONO_INTERNAL;
1544 void        mono_ssa_compute                    (MonoCompile *cfg) MONO_INTERNAL;
1545 void        mono_ssa_remove                     (MonoCompile *cfg) MONO_INTERNAL;
1546 void        mono_ssa_cprop                      (MonoCompile *cfg) MONO_INTERNAL;
1547 void        mono_ssa_deadce                     (MonoCompile *cfg) MONO_INTERNAL;
1548 void        mono_ssa_strength_reduction         (MonoCompile *cfg) MONO_INTERNAL;
1549 void        mono_free_loop_info                 (MonoCompile *cfg) MONO_INTERNAL;
1550
1551 void        mono_ssa_compute2                   (MonoCompile *cfg);
1552 void        mono_ssa_remove2                    (MonoCompile *cfg);
1553 void        mono_ssa_cprop2                     (MonoCompile *cfg);
1554 void        mono_ssa_deadce2                    (MonoCompile *cfg);
1555
1556 /* debugging support */
1557 void      mono_debug_init_method                (MonoCompile *cfg, MonoBasicBlock *start_block,
1558                                                  guint32 breakpoint_id) MONO_INTERNAL;
1559 void      mono_debug_open_method                (MonoCompile *cfg) MONO_INTERNAL;
1560 void      mono_debug_close_method               (MonoCompile *cfg) MONO_INTERNAL;
1561 void      mono_debug_open_block                 (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address) MONO_INTERNAL;
1562 void      mono_debug_record_line_number         (MonoCompile *cfg, MonoInst *ins, guint32 address) MONO_INTERNAL;
1563 void      mono_debug_serialize_debug_info       (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len) MONO_INTERNAL;
1564 void      mono_debug_add_aot_method             (MonoDomain *domain,
1565                                                  MonoMethod *method, guint8 *code_start, 
1566                                                  guint8 *debug_info, guint32 debug_info_len) MONO_INTERNAL;
1567 void      mono_debug_add_icall_wrapper          (MonoMethod *method, MonoJitICallInfo* info) MONO_INTERNAL;
1568 void      mono_debug_print_vars                 (gpointer ip, gboolean only_arguments);
1569 void      mono_debugger_run_finally             (MonoContext *start_ctx);
1570
1571 extern gssize mono_breakpoint_info_index [MONO_BREAKPOINT_ARRAY_SIZE];
1572
1573 gboolean mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size);
1574
1575 /* Mono Debugger support */
1576 void      mono_debugger_init                    (void);
1577 int       mono_debugger_main                    (MonoDomain *domain, MonoAssembly *assembly, int argc, char **argv);
1578
1579
1580 /* Tracing */
1581 MonoTraceSpec *mono_trace_parse_options         (const char *options) MONO_INTERNAL;
1582 void           mono_trace_set_assembly          (MonoAssembly *assembly) MONO_INTERNAL;
1583 gboolean       mono_trace_eval                  (MonoMethod *method) MONO_INTERNAL;
1584
1585 extern void
1586 mono_perform_abc_removal (MonoCompile *cfg) MONO_INTERNAL;
1587 extern void
1588 mono_perform_abc_removal2 (MonoCompile *cfg) MONO_INTERNAL;
1589 extern void
1590 mono_perform_ssapre (MonoCompile *cfg) MONO_INTERNAL;
1591 extern void
1592 mono_local_cprop (MonoCompile *cfg) MONO_INTERNAL;
1593 extern void
1594 mono_local_cprop2 (MonoCompile *cfg);
1595 extern void
1596 mono_local_deadce (MonoCompile *cfg);
1597
1598 /* CAS - stack walk */
1599 MonoSecurityFrame* ves_icall_System_Security_SecurityFrame_GetSecurityFrame (gint32 skip) MONO_INTERNAL;
1600 MonoArray* ves_icall_System_Security_SecurityFrame_GetSecurityStack (gint32 skip) MONO_INTERNAL;
1601
1602 /* Generic sharing */
1603
1604 MonoGenericSharingContext* mono_get_generic_context_from_code (guint8 *code) MONO_INTERNAL;
1605
1606 MonoGenericContext* mini_method_get_context (MonoMethod *method) MONO_INTERNAL;
1607
1608 int mono_method_check_context_used (MonoMethod *method) MONO_INTERNAL;
1609
1610 gboolean mono_generic_context_equal_deep (MonoGenericContext *context1, MonoGenericContext *context2) MONO_INTERNAL;
1611
1612 gboolean mono_generic_context_is_sharable (MonoGenericContext *context, gboolean allow_type_vars) MONO_INTERNAL;
1613
1614 gboolean mono_method_is_generic_impl (MonoMethod *method) MONO_INTERNAL;
1615 gboolean mono_method_is_generic_sharable_impl (MonoMethod *method, gboolean allow_type_vars) MONO_INTERNAL;
1616
1617 gpointer mono_helper_get_rgctx_other_ptr (MonoClass *caller_class, MonoVTable *vtable,
1618                                           guint32 token, guint32 token_source, guint32 rgctx_type,
1619                                           gint32 rgctx_index) MONO_INTERNAL;
1620
1621 void mono_generic_sharing_init (void) MONO_INTERNAL;
1622
1623 MonoClass* mini_class_get_container_class (MonoClass *class) MONO_INTERNAL;
1624 MonoGenericContext* mini_class_get_context (MonoClass *class) MONO_INTERNAL;
1625
1626 MonoType* mini_get_basic_type_from_generic (MonoGenericSharingContext *gsctx, MonoType *type) MONO_INTERNAL;
1627 MonoType* mini_type_get_underlying_type (MonoGenericSharingContext *gsctx, MonoType *type) MONO_INTERNAL;
1628
1629 int mini_type_stack_size (MonoGenericSharingContext *gsctx, MonoType *t, int *align) MONO_INTERNAL;
1630 int mini_type_stack_size_full (MonoGenericSharingContext *gsctx, MonoType *t, guint32 *align, gboolean pinvoke) MONO_INTERNAL;
1631
1632 /* wapihandles.c */
1633 int mini_wapi_hps (int argc, char **argv) MONO_INTERNAL;
1634
1635 int mini_wapi_semdel (int argc, char **argv) MONO_INTERNAL;
1636
1637 int mini_wapi_seminfo (int argc, char **argv) MONO_INTERNAL;
1638
1639 #endif /* __MONO_MINI_H__ */