2008-08-22 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         regmask_t used_iregs;
550         regmask_t used_fregs;
551         GSList *out_ireg_args;
552         GSList *out_freg_args;
553 };
554
555 struct MonoCallArgParm {
556         MonoInst ins;
557         gint32 size;
558         gint32 offset;
559         gint32 offPrm;
560 };
561
562 /* 
563  * flags for MonoInst
564  * Note: some of the values overlap, because they can't appear
565  * in the same MonoInst.
566  */
567 enum {
568         MONO_INST_HAS_METHOD = 1,
569         /* temp local created by a DUP: used only within a BB */
570         MONO_INST_IS_TEMP    = 1,
571         MONO_INST_INIT       = 1, /* in localloc */
572         MONO_INST_IS_DEAD    = 2,
573         MONO_INST_TAILCALL   = 4,
574         MONO_INST_VOLATILE   = 4,
575         MONO_INST_BRLABEL    = 4,
576         MONO_INST_NOTYPECHECK    = 4,
577         MONO_INST_UNALIGNED  = 8,
578     MONO_INST_CFOLD_TAKEN = 8, /* On branches */
579     MONO_INST_CFOLD_NOT_TAKEN = 16, /* On branches */
580         MONO_INST_DEFINITION_HAS_SIDE_EFFECTS = 8,
581         /* the address of the variable has been taken */
582         MONO_INST_INDIRECT   = 16,
583         MONO_INST_NORANGECHECK   = 16
584 };
585
586 #define inst_c0 data.op[0].const_val
587 #define inst_c1 data.op[1].const_val
588 #define inst_i0 data.op[0].src
589 #define inst_i1 data.op[1].src
590 #define inst_p0 data.op[0].p
591 #define inst_p1 data.op[1].p
592 #define inst_l  data.i8const
593 #define inst_r  data.r8const
594 #define inst_left  data.op[0].src
595 #define inst_right data.op[1].src
596
597 #define inst_newa_len   data.op[0].src
598 #define inst_newa_class data.op[1].klass
599
600 #define inst_var    data.op[0].var
601 #define inst_vtype  data.op[1].vtype
602 /* in branch instructions */
603 #define inst_many_bb   data.op[1].many_blocks
604 #define inst_target_bb data.op[0].target_block
605 #define inst_true_bb   data.op[1].many_blocks[0]
606 #define inst_false_bb  data.op[1].many_blocks[1]
607
608 #define inst_basereg sreg1
609 #define inst_indexreg sreg2
610 #define inst_destbasereg dreg
611 #define inst_offset data.op[0].const_val
612 #define inst_imm    data.op[1].const_val
613 #define inst_call   data.op[1].call_inst
614
615 #define inst_phi_args   data.op[1].phi_args
616
617 /* instruction description for use in regalloc/scheduling */
618 enum {
619         MONO_INST_DEST,
620         MONO_INST_SRC1,
621         MONO_INST_SRC2,
622         MONO_INST_LEN,
623         MONO_INST_CLOB,
624         /* Unused, commented out to reduce the size of the mdesc tables
625         MONO_INST_FLAGS,
626         MONO_INST_COST,
627         MONO_INST_DELAY,
628         MONO_INST_RES,
629         */
630         MONO_INST_MAX
631 };
632
633 typedef union {
634         struct {
635                 guint16 tid; /* tree number */
636                 guint16 bid; /* block number */
637         } pos ;
638         guint32 abs_pos; 
639 } MonoPosition;
640
641 typedef struct {
642         MonoPosition first_use, last_use;
643 } MonoLiveRange;
644
645 typedef struct MonoLiveRange2 MonoLiveRange2;
646
647 struct MonoLiveRange2 {
648         int from, to;
649         MonoLiveRange2 *next;
650 };
651
652 typedef struct {
653         /* List of live ranges sorted by 'from' */
654         MonoLiveRange2 *range;
655         MonoLiveRange2 *last_range;
656 } MonoLiveInterval;
657
658 /*
659  * Additional information about a variable
660  */
661 struct MonoMethodVar {
662         guint           idx; /* inside cfg->varinfo, cfg->vars */
663         MonoLiveRange   range; /* generated by liveness analysis */
664         MonoLiveInterval *interval; /* generated by liveness analysis */
665         int             reg; /* != -1 if allocated into a register */
666         int             spill_costs;
667         MonoBitSet     *def_in; /* used by SSA */
668         MonoInst       *def;    /* used by SSA */
669         MonoBasicBlock *def_bb; /* used by SSA */
670         GList          *uses;   /* used by SSA */
671         char            cpstate;  /* used by SSA conditional  constant propagation */
672 };
673
674 typedef struct {
675         gpointer          end_of_stack;
676         guint32           stack_size;
677 #if !defined(HAVE_KW_THREAD) || !defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
678         MonoLMF          *lmf;
679 #endif
680         MonoLMF          *first_lmf;
681         gpointer         signal_stack;
682         guint32          signal_stack_size;
683         gpointer         stack_ovf_guard_base;
684         guint32          stack_ovf_guard_size;
685         void            (*abort_func) (MonoObject *object);
686         /* Used to implement --debug=casts */
687         MonoClass       *class_cast_from, *class_cast_to;
688 } MonoJitTlsData;
689
690 typedef enum {
691 #define PATCH_INFO(a,b) MONO_PATCH_INFO_ ## a,
692 #include "patch-info.h"
693 #undef PATCH_INFO
694         MONO_PATCH_INFO_NUM
695 } MonoJumpInfoType;
696
697 /*
698  * We need to store the image which the token refers to along with the token,
699  * since the image might not be the same as the image of the method which
700  * contains the relocation, because of inlining.
701  */
702 typedef struct MonoJumpInfoToken {
703         MonoImage *image;
704         guint32 token;
705 } MonoJumpInfoToken;
706
707 typedef struct MonoJumpInfoBBTable {
708         MonoBasicBlock **table;
709         int table_size;
710 } MonoJumpInfoBBTable;
711
712 typedef struct MonoJumpInfoRgctxEntry MonoJumpInfoRgctxEntry;
713
714 typedef struct MonoJumpInfo MonoJumpInfo;
715 struct MonoJumpInfo {
716         MonoJumpInfo *next;
717         union {
718                 int i;
719                 guint8 *p;
720                 MonoInst *label;
721         } ip;
722
723         MonoJumpInfoType type;
724         union {
725                 gconstpointer   target;
726 #if SIZEOF_VOID_P == 8
727                 gint64          offset;
728 #else
729                 int             offset;
730 #endif
731                 MonoBasicBlock *bb;
732                 MonoInst       *inst;
733                 MonoMethod     *method;
734                 MonoClass      *klass;
735                 MonoClassField *field;
736                 MonoImage      *image;
737                 MonoVTable     *vtable;
738                 const char     *name;
739                 MonoJumpInfoToken  *token;
740                 MonoJumpInfoBBTable *table;
741                 MonoJumpInfoRgctxEntry *rgctx_entry;
742         } data;
743 };
744  
745 /* Contains information describing an rgctx entry */
746 struct MonoJumpInfoRgctxEntry {
747         MonoMethod *method;
748         gboolean in_mrgctx;
749         MonoJumpInfo *data; /* describes the data to be loaded */
750         int info_type;
751 };
752
753 typedef enum {
754         MONO_TRAMPOLINE_JIT,
755         MONO_TRAMPOLINE_JUMP,
756         MONO_TRAMPOLINE_CLASS_INIT,
757         MONO_TRAMPOLINE_GENERIC_CLASS_INIT,
758         MONO_TRAMPOLINE_RGCTX_LAZY_FETCH,
759         MONO_TRAMPOLINE_AOT,
760         MONO_TRAMPOLINE_AOT_PLT,
761         MONO_TRAMPOLINE_DELEGATE,
762         MONO_TRAMPOLINE_NUM
763 } MonoTrampolineType;
764
765 /* optimization flags */
766 #define OPTFLAG(id,shift,name,descr) MONO_OPT_ ## id = 1 << shift,
767 enum {
768 #include "optflags-def.h"
769         MONO_OPT_LAST
770 };
771
772 /* Bit-fields in the MonoBasicBlock.region */
773 #define MONO_REGION_TRY       0
774 #define MONO_REGION_FINALLY  16
775 #define MONO_REGION_CATCH    32
776 #define MONO_REGION_FAULT    64         /* Currently unused */
777 #define MONO_REGION_FILTER  128
778
779 #define MONO_BBLOCK_IS_IN_REGION(bblock, regtype) (((bblock)->region & (0xf << 4)) == (regtype))
780
781 #define get_vreg_to_inst(cfg, vreg) ((vreg) < (cfg)->vreg_to_inst_len ? (cfg)->vreg_to_inst [(vreg)] : NULL)
782
783 #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))))
784
785 /*
786  * Control Flow Graph and compilation unit information
787  */
788 typedef struct {
789         MonoMethod      *method;
790         MonoMemPool     *mempool;
791         MonoInst       **varinfo;
792         MonoMethodVar   *vars;
793         MonoInst        *ret;
794         MonoBasicBlock  *bb_entry;
795         MonoBasicBlock  *bb_exit;
796         MonoBasicBlock  *bb_init;
797         MonoBasicBlock **bblocks;
798         MonoBasicBlock **cil_offset_to_bb;
799         MonoMemPool     *state_pool; /* used by instruction selection */
800         MonoBasicBlock  *cbb;        /* used by instruction selection */
801         MonoInst        *prev_ins;   /* in decompose */
802         MonoJumpInfo    *patch_info;
803         MonoJitInfo     *jit_info;
804         MonoJitDynamicMethodInfo *dynamic_info;
805         guint            num_bblocks;
806         guint            locals_start;
807         guint            num_varinfo; /* used items in varinfo */
808         guint            varinfo_count; /* total storage in varinfo */
809         gint             stack_offset;
810         gint             max_ireg;
811         gint             cil_offset_to_bb_len;
812         MonoRegState    *rs;
813         MonoSpillInfo   *spill_info; /* machine register spills */
814         MonoSpillInfo   *spill_info_float; /* fp register spills */
815         gint             spill_count;
816         gint             spill_info_len, spill_info_float_len;
817         /* unsigned char   *cil_code; */
818         MonoMethod      *inlined_method; /* the method which is currently inlined */
819         MonoInst        *domainvar; /* a cache for the current domain */
820         MonoInst        *got_var; /* Global Offset Table variable */
821         MonoInst        **locals;
822         MonoInst        *rgctx_var; /* Runtime generic context variable (for static generic methods) */
823         MonoInst        **args;
824         MonoType        **arg_types;
825         MonoMethod      *current_method; /* The method currently processed by method_to_ir () */
826         MonoGenericContext *generic_context;
827
828         /* 
829          * This variable represents the hidden argument holding the vtype
830          * return address. If the method returns something other than a vtype, or
831          * the vtype is returned in registers this is NULL.
832          */
833         MonoInst        *vret_addr;
834
835         /*
836          * This is used to initialize the cil_code field of MonoInst's.
837          */
838         const unsigned char *ip;
839         
840         struct MonoAliasingInformation *aliasing_info;
841
842         /* A hashtable of region ID-> SP var mappings */
843         /* An SP var is a place to store the stack pointer (used by handlers)*/
844         GHashTable      *spvars;
845
846         /* A hashtable of region ID -> EX var mappings */
847         /* An EX var stores the exception object passed to catch/filter blocks */
848         GHashTable      *exvars;
849
850         GList           *ldstr_list; /* used by AOT */
851         
852         MonoDomain      *domain;
853
854         guint            real_offset;
855         GHashTable      *cbb_hash;
856
857         /* The current virtual register number */
858         guint32 next_vreg;
859
860         MonoGenericSharingContext *generic_sharing_context;
861
862         unsigned char   *cil_start;
863         unsigned char   *native_code;
864         guint            code_size;
865         guint            code_len;
866         guint            prolog_end;
867         guint            epilog_begin;
868         regmask_t        used_int_regs;
869         guint32          opt;
870         guint32          prof_options;
871         guint32          flags;
872         guint32          comp_done;
873         guint32          verbose_level;
874         guint32          stack_usage;
875         guint32          param_area;
876         guint32          frame_reg;
877         gint32           sig_cookie;
878         guint            disable_aot : 1;
879         guint            disable_ssa : 1;
880         guint            enable_extended_bblocks : 1;
881         guint            run_cctors : 1;
882         guint            need_lmf_area : 1;
883         guint            compile_aot : 1;
884         guint            got_var_allocated : 1;
885         guint            ret_var_is_local : 1;
886         guint            ret_var_set : 1;
887         guint            new_ir : 1;
888         guint            globalra : 1;
889         guint            unverifiable : 1;
890         guint            skip_visibility : 1;
891         guint            disable_reuse_registers : 1;
892         guint            disable_reuse_stack_slots : 1;
893         guint            disable_initlocals_opt : 1;
894         guint            disable_omit_fp : 1;
895         guint            has_got_slots : 1;
896         gpointer         debug_info;
897         guint32          lmf_offset;
898     guint16          *intvars;
899         MonoProfileCoverageInfo *coverage_info;
900         GHashTable       *token_info_hash;
901         MonoCompileArch  arch;
902         guint32          exception_type;        /* MONO_EXCEPTION_* */
903         guint32          exception_data;
904         char*            exception_message;
905         gpointer         exception_ptr;
906
907         /* Fields used by the local reg allocator */
908         void*            reginfo;
909         int              reginfo_len;
910
911         /* Maps vregs to their associated MonoInst's */
912         /* vregs with an associated MonoInst are 'global' while others are 'local' */
913         MonoInst **vreg_to_inst;
914
915         /* Size of above array */
916         guint32 vreg_to_inst_len;
917
918         /* 
919          * The original method to compile, differs from 'method' when doing generic
920          * sharing.
921          */
922         MonoMethod *orig_method;
923
924         /* Patches which describe absolute addresses embedded into the native code */
925         GHashTable *abs_patches;
926 } MonoCompile;
927
928 typedef enum {
929         MONO_CFG_HAS_ALLOCA = 1 << 0,
930         MONO_CFG_HAS_CALLS  = 1 << 1,
931         MONO_CFG_HAS_LDELEMA  = 1 << 2,
932         MONO_CFG_HAS_VARARGS  = 1 << 3,
933         MONO_CFG_HAS_TAIL     = 1 << 4,
934         MONO_CFG_HAS_FPOUT    = 1 << 5, /* there are fp values passed in int registers */
935         MONO_CFG_HAS_SPILLUP  = 1 << 6, /* spill var slots are allocated from bottom to top */
936         MONO_CFG_HAS_CHECK_THIS  = 1 << 7,
937         MONO_CFG_HAS_ARRAY_ACCESS = 1 << 8
938 } MonoCompileFlags;
939
940 typedef struct {
941         gulong methods_compiled;
942         gulong methods_aot;
943         gulong methods_lookups;
944         gulong method_trampolines;
945         gulong allocate_var;
946         gulong analyze_stack_repeat;
947         gulong cil_code_size;
948         gulong native_code_size;
949         gulong code_reallocs;
950         gulong max_code_size_ratio;
951         gulong biggest_method_size;
952         gulong allocated_code_size;
953         gulong inlineable_methods;
954         gulong inlined_methods;
955         gulong basic_blocks;
956         gulong max_basic_blocks;
957         gulong locals_stack_size;
958         gulong regvars;
959         gulong cas_declsec_check;
960         gulong cas_linkdemand_icall;
961         gulong cas_linkdemand_pinvoke;
962         gulong cas_linkdemand_aptc;
963         gulong cas_linkdemand;
964         gulong cas_demand_generation;
965         gulong generic_virtual_invocations;
966         char *max_ratio_method;
967         char *biggest_method;
968         gboolean enabled;
969 } MonoJitStats;
970
971 extern MonoJitStats mono_jit_stats;
972
973 /* values for MonoInst.ssa_op */
974 enum {
975         MONO_SSA_NOP = 0,
976         MONO_SSA_ADDRESS_TAKEN = 1,
977         MONO_SSA_LOAD = 2,
978         MONO_SSA_STORE = 4,
979         MONO_SSA_LOAD_STORE = MONO_SSA_LOAD|MONO_SSA_STORE,
980         MONO_SSA_INDIRECT_LOAD = MONO_SSA_LOAD|MONO_SSA_ADDRESS_TAKEN,
981         MONO_SSA_INDIRECT_STORE = MONO_SSA_STORE|MONO_SSA_ADDRESS_TAKEN,
982         MONO_SSA_INDIRECT_LOAD_STORE =
983         MONO_SSA_LOAD|MONO_SSA_STORE|MONO_SSA_ADDRESS_TAKEN
984 };
985
986 /* opcodes: value assigned after all the CIL opcodes */
987 #ifdef MINI_OP
988 #undef MINI_OP
989 #endif
990 #define MINI_OP(a,b,dest,src1,src2) a,
991 enum {
992         OP_START = MONO_CEE_LAST - 1,
993 #include "mini-ops.h"
994         OP_LAST
995 };
996 #undef MINI_OP
997
998 /* Can't use the same with both JITs since that would break the burg rules */
999 #if defined(NEW_IR)
1000
1001 #if SIZEOF_VOID_P == 8
1002 #define OP_PCONST OP_I8CONST
1003 #define OP_PADD OP_LADD
1004 #define OP_PADD_IMM OP_LADD_IMM
1005 #define OP_PSUB OP_LSUB
1006 #define OP_PMUL OP_LMUL
1007 #define OP_PMUL_IMM OP_LMUL_IMM
1008 #define OP_PNEG OP_LNEG
1009 #define OP_PCONV_TO_I1 OP_LCONV_TO_I1
1010 #define OP_PCONV_TO_U1 OP_LCONV_TO_U1
1011 #define OP_PCONV_TO_I2 OP_LCONV_TO_I2
1012 #define OP_PCONV_TO_U2 OP_LCONV_TO_U2
1013 #define OP_PCONV_TO_OVF_I1_UN OP_LCONV_TO_OVF_I1_UN
1014 #define OP_PCONV_TO_OVF_I1 OP_LCONV_TO_OVF_I1
1015 #define OP_PBEQ OP_LBEQ
1016 #define OP_PCEQ OP_LCEQ
1017 #define OP_PBNE_UN OP_LBNE_UN
1018 #define OP_PBGE_UN OP_LBGE_UN
1019 #define OP_PBLT_UN OP_LBLT_UN
1020 #define OP_PBGE OP_LBGE
1021 #define OP_STOREP_MEMBASE_REG OP_STOREI8_MEMBASE_REG
1022 #define OP_STOREP_MEMBASE_IMM OP_STOREI8_MEMBASE_IMM
1023 #else
1024 #define OP_PCONST OP_ICONST
1025 #define OP_PADD OP_IADD
1026 #define OP_PADD_IMM OP_IADD_IMM
1027 #define OP_PSUB OP_ISUB
1028 #define OP_PMUL OP_IMUL
1029 #define OP_PMUL_IMM OP_IMUL_IMM
1030 #define OP_PNEG OP_INEG
1031 #define OP_PCONV_TO_U2 OP_ICONV_TO_U2
1032 #define OP_PCONV_TO_OVF_I1_UN OP_ICONV_TO_OVF_I1_UN
1033 #define OP_PCONV_TO_OVF_I1 OP_ICONV_TO_OVF_I1
1034 #define OP_PBEQ OP_IBEQ
1035 #define OP_PCEQ OP_ICEQ
1036 #define OP_PBNE_UN OP_IBNE_UN
1037 #define OP_PBGE_UN OP_IBGE_UN
1038 #define OP_PBLT_UN OP_IBLT_UN
1039 #define OP_PBGE OP_IBGE
1040 #define OP_STOREP_MEMBASE_REG OP_STOREI4_MEMBASE_REG
1041 #define OP_STOREP_MEMBASE_IMM OP_STOREI4_MEMBASE_IMM
1042 #endif
1043
1044 #else
1045
1046 #if SIZEOF_VOID_P == 8
1047 #define OP_PCONST OP_I8CONST
1048 #define OP_PADD OP_LADD
1049 #define OP_PADD_IMM OP_LADD_IMM
1050 #define OP_PNEG OP_LNEG
1051 #define OP_PCONV_TO_I1 OP_LCONV_TO_I1
1052 #define OP_PCONV_TO_U1 OP_LCONV_TO_U1
1053 #define OP_PCONV_TO_I2 OP_LCONV_TO_I2
1054 #define OP_PCONV_TO_U2 OP_LCONV_TO_U2
1055 #define OP_PCONV_TO_OVF_I1_UN OP_LCONV_TO_OVF_I1_UN
1056 #define OP_PCONV_TO_OVF_I1 OP_LCONV_TO_OVF_I1
1057 #define OP_PBEQ OP_LBEQ
1058 #define OP_PCEQ CEE_CEQ
1059 #define OP_STOREP_MEMBASE_REG OP_STOREI8_MEMBASE_REG
1060 #define OP_STOREP_MEMBASE_IMM OP_STOREI8_MEMBASE_IMM
1061 #else
1062 #define OP_PCONST OP_ICONST
1063 #define OP_PADD CEE_ADD
1064 #define OP_PADD2 OP_IADD
1065 #define OP_PNEG CEE_NEG
1066 #define OP_PCONV_TO_I1 OP_ICONV_TO_I1
1067 #define OP_PCONV_TO_U1 OP_ICONV_TO_U1
1068 #define OP_PCONV_TO_I2 OP_ICONV_TO_I2
1069 #define OP_PCONV_TO_U2 CEE_CONV_U2
1070 #define OP_PCONV_TO_OVF_I1_UN CEE_CONV_OVF_I1_UN
1071 #define OP_PCONV_TO_OVF_I1 CEE_CONV_OVF_I1
1072 #define OP_PBEQ OP_IBEQ
1073 #define OP_PCEQ CEE_CEQ
1074 #define OP_STOREP_MEMBASE_REG OP_STOREI4_MEMBASE_REG
1075 #define OP_STOREP_MEMBASE_IMM OP_STOREI4_MEMBASE_IMM
1076 #endif
1077
1078 #endif
1079
1080 typedef enum {
1081         STACK_INV,
1082         STACK_I4,
1083         STACK_I8,
1084         STACK_PTR,
1085         STACK_R8,
1086         STACK_MP,
1087         STACK_OBJ,
1088         STACK_VTYPE,
1089         STACK_MAX
1090 } MonoStackType;
1091
1092 typedef struct {
1093         union {
1094                 double   r8;
1095                 gint32   i4;
1096                 gint64   i8;
1097                 gpointer p;
1098                 MonoClass *klass;
1099         } data;
1100         int type;
1101 } StackSlot;
1102
1103 #if HAVE_ARRAY_ELEM_INIT
1104 extern const guint8 mono_burg_arity [];
1105 #else
1106 extern guint8 mono_burg_arity [];
1107 #endif
1108
1109 extern const char MONO_ARCH_CPU_SPEC [] MONO_INTERNAL;
1110 #define MONO_ARCH_CPU_SPEC_IDX_COMBINE(a) a ## _idx
1111 #define MONO_ARCH_CPU_SPEC_IDX(a) MONO_ARCH_CPU_SPEC_IDX_COMBINE(a)
1112 extern const guint16 MONO_ARCH_CPU_SPEC_IDX(MONO_ARCH_CPU_SPEC) [] MONO_INTERNAL;
1113 #define ins_get_spec(op) ((const char*)&MONO_ARCH_CPU_SPEC + MONO_ARCH_CPU_SPEC_IDX(MONO_ARCH_CPU_SPEC)[(op)])
1114
1115 enum {
1116         MONO_COMP_DOM = 1,
1117         MONO_COMP_IDOM = 2,
1118         MONO_COMP_DFRONTIER = 4,
1119         MONO_COMP_DOM_REV = 8,
1120         MONO_COMP_LIVENESS = 16,
1121         MONO_COMP_SSA = 32,
1122         MONO_COMP_SSA_DEF_USE = 64,
1123         MONO_COMP_REACHABILITY = 128,
1124         MONO_COMP_LOOPS = 256
1125 };
1126
1127 typedef enum {
1128         MONO_GRAPH_CFG = 1,
1129         MONO_GRAPH_DTREE = 2,
1130         MONO_GRAPH_CFG_CODE = 4,
1131         MONO_GRAPH_CFG_SSA = 8,
1132         MONO_GRAPH_CFG_OPTCODE = 16
1133 } MonoGraphOptions;
1134
1135 typedef struct {
1136         guint16 size;
1137         guint16 offset;
1138         guint8  pad;
1139 } MonoJitArgumentInfo;
1140
1141 typedef struct {
1142         gboolean handle_sigint;
1143         gboolean keep_delegates;
1144         gboolean collect_pagefault_stats;
1145         gboolean break_on_unverified;
1146         gboolean better_cast_details;
1147         gboolean mdb_optimizations;
1148         gboolean no_gdb_backtrace;
1149 } MonoDebugOptions;
1150
1151 enum {
1152         BRANCH_NOT_TAKEN,
1153         BRANCH_TAKEN,
1154         BRANCH_UNDEF
1155 };
1156
1157 typedef enum {
1158         CMP_EQ,
1159         CMP_NE,
1160         CMP_LE,
1161         CMP_GE,
1162         CMP_LT,
1163         CMP_GT,
1164         CMP_LE_UN,
1165         CMP_GE_UN,
1166         CMP_LT_UN,
1167         CMP_GT_UN
1168 } CompRelation;
1169
1170 typedef enum {
1171         CMP_TYPE_L,
1172         CMP_TYPE_I,
1173         CMP_TYPE_F
1174 } CompType;
1175
1176 /* Implicit exceptions */
1177 enum {
1178         MONO_EXC_INDEX_OUT_OF_RANGE,
1179         MONO_EXC_OVERFLOW,
1180         MONO_EXC_ARITHMETIC,
1181         MONO_EXC_DIVIDE_BY_ZERO,
1182         MONO_EXC_INVALID_CAST,
1183         MONO_EXC_NULL_REF,
1184         MONO_EXC_ARRAY_TYPE_MISMATCH,
1185         MONO_EXC_INTRINS_NUM
1186 };
1187
1188 enum {
1189         MINI_TOKEN_SOURCE_CLASS,
1190         MINI_TOKEN_SOURCE_METHOD,
1191         MINI_TOKEN_SOURCE_FIELD
1192 };
1193
1194 typedef void (*MonoInstFunc) (MonoInst *tree, gpointer data);
1195
1196 /* main function */
1197 int         mono_main                      (int argc, char* argv[]);
1198 void        mono_set_defaults              (int verbose_level, guint32 opts);
1199 MonoDomain* mini_init                      (const char *filename, const char *runtime_version) MONO_INTERNAL;
1200 void        mini_cleanup                   (MonoDomain *domain) MONO_INTERNAL;
1201 MonoDebugOptions *mini_get_debug_options   (void) MONO_INTERNAL;
1202
1203 /* helper methods */
1204 MonoJumpInfoToken * mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token) MONO_INTERNAL;
1205 MonoInst* mono_find_spvar_for_region        (MonoCompile *cfg, int region) MONO_INTERNAL;
1206 void      mono_precompile_assemblies        (void) MONO_INTERNAL;
1207 int       mono_parse_default_optimizations  (const char* p);
1208 void      mono_bblock_add_inst              (MonoBasicBlock *bb, MonoInst *inst) MONO_INTERNAL;
1209 void      mono_bblock_insert_after_ins      (MonoBasicBlock *bb, MonoInst *ins, MonoInst *ins_to_insert) MONO_INTERNAL;
1210 void      mono_bblock_insert_before_ins     (MonoBasicBlock *bb, MonoInst *ins, MonoInst *ins_to_insert) MONO_INTERNAL;
1211 void      mono_verify_bblock                (MonoBasicBlock *bb) MONO_INTERNAL;
1212 void      mono_verify_cfg                   (MonoCompile *cfg) MONO_INTERNAL;
1213 void      mono_constant_fold                (MonoCompile *cfg) MONO_INTERNAL;
1214 void      mono_constant_fold_inst           (MonoInst *inst, gpointer data) MONO_INTERNAL;
1215 MonoInst* mono_constant_fold_ins2           (MonoCompile *cfg, MonoInst *ins, MonoInst *arg1, MonoInst *arg2, gboolean overwrite) MONO_INTERNAL;
1216 int       mono_eval_cond_branch             (MonoInst *branch) MONO_INTERNAL;
1217 int       mono_is_power_of_two              (guint32 val) MONO_INTERNAL;
1218 void      mono_cprop_local                  (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size) MONO_INTERNAL;
1219 MonoInst* mono_compile_create_var           (MonoCompile *cfg, MonoType *type, int opcode) MONO_INTERNAL;
1220 MonoInst* mono_compile_create_var_for_vreg  (MonoCompile *cfg, MonoType *type, int opcode, int vreg) MONO_INTERNAL;
1221 void      mono_compile_make_var_load        (MonoCompile *cfg, MonoInst *dest, gssize var_index) MONO_INTERNAL;
1222 MonoInst* mono_compile_create_var_load      (MonoCompile *cfg, gssize var_index) MONO_INTERNAL;
1223 MonoInst* mono_compile_create_var_store     (MonoCompile *cfg, gssize var_index, MonoInst *value) MONO_INTERNAL;
1224 MonoType* mono_type_from_stack_type         (MonoInst *ins) MONO_INTERNAL;
1225 guint32   mono_alloc_ireg                   (MonoCompile *cfg) MONO_INTERNAL;
1226 guint32   mono_alloc_freg                   (MonoCompile *cfg) MONO_INTERNAL;
1227 guint32   mono_alloc_preg                   (MonoCompile *cfg) MONO_INTERNAL;
1228 guint32   mono_alloc_dreg                   (MonoCompile *cfg, MonoStackType stack_type) MONO_INTERNAL;
1229
1230 void      mono_link_bblock                  (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to) MONO_INTERNAL;
1231 void      mono_unlink_bblock                (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to) MONO_INTERNAL;
1232 gboolean  mono_bblocks_linked               (MonoBasicBlock *bb1, MonoBasicBlock *bb2) MONO_INTERNAL;
1233 void      mono_remove_bblock                (MonoCompile *cfg, MonoBasicBlock *bb) MONO_INTERNAL;
1234 void      mono_nullify_basic_block          (MonoBasicBlock *bb) MONO_INTERNAL;
1235 void      mono_merge_basic_blocks           (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *bbn) MONO_INTERNAL;
1236 void      mono_optimize_branches            (MonoCompile *cfg) MONO_INTERNAL;
1237
1238 void      mono_blockset_print               (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom) MONO_INTERNAL;
1239 void      mono_print_tree                   (MonoInst *tree) MONO_INTERNAL;
1240 void      mono_print_tree_nl                (MonoInst *tree) MONO_INTERNAL;
1241 void      mono_print_ins_index              (int i, MonoInst *ins) MONO_INTERNAL;
1242 void      mono_print_ins                    (MonoInst *ins) MONO_INTERNAL;
1243 void      mono_print_bb                     (MonoBasicBlock *bb, const char *msg) MONO_INTERNAL;
1244 void      mono_print_code                   (MonoCompile *cfg, const char *msg) MONO_INTERNAL;
1245 void      mono_print_method_from_ip         (void *ip);
1246 char     *mono_pmip                         (void *ip);
1247 void      mono_select_instructions          (MonoCompile *cfg) MONO_INTERNAL;
1248 const char* mono_inst_name                  (int op);
1249 int       mono_op_to_op_imm                 (int opcode) MONO_INTERNAL;
1250 int       mono_op_imm_to_op                 (int opcode) MONO_INTERNAL;
1251 int       mono_load_membase_to_load_mem     (int opcode) MONO_INTERNAL;
1252 guint     mono_type_to_load_membase         (MonoCompile *cfg, MonoType *type) MONO_INTERNAL;
1253 guint     mono_type_to_store_membase        (MonoCompile *cfg, MonoType *type) MONO_INTERNAL;
1254 guint     mini_type_to_stind                (MonoCompile* cfg, MonoType *type) MONO_INTERNAL;
1255 guint32   mono_reverse_branch_op            (guint32 opcode) MONO_INTERNAL;
1256 void      mono_inst_foreach                 (MonoInst *tree, MonoInstFunc func, gpointer data) MONO_INTERNAL;
1257 void      mono_disassemble_code             (MonoCompile *cfg, guint8 *code, int size, char *id) MONO_INTERNAL;
1258 void      mono_add_patch_info               (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target) MONO_INTERNAL;
1259 void      mono_remove_patch_info            (MonoCompile *cfg, int ip) MONO_INTERNAL;
1260 MonoJumpInfo* mono_patch_info_dup_mp        (MonoMemPool *mp, MonoJumpInfo *patch_info) MONO_INTERNAL;
1261 guint     mono_patch_info_hash (gconstpointer data) MONO_INTERNAL;
1262 gint      mono_patch_info_equal (gconstpointer ka, gconstpointer kb) MONO_INTERNAL;
1263 MonoJumpInfo *mono_patch_info_list_prepend  (MonoJumpInfo *list, int ip, MonoJumpInfoType type, gconstpointer target) MONO_INTERNAL;
1264 gpointer  mono_resolve_patch_target         (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors) MONO_INTERNAL;
1265 gpointer  mono_jit_find_compiled_method     (MonoDomain *domain, MonoMethod *method) MONO_INTERNAL;
1266 MonoLMF * mono_get_lmf                      (void) MONO_INTERNAL;
1267 MonoLMF** mono_get_lmf_addr                 (void) MONO_INTERNAL;
1268 void      mono_jit_thread_attach            (MonoDomain *domain);
1269 guint32   mono_get_jit_tls_key              (void) MONO_INTERNAL;
1270 gint32    mono_get_jit_tls_offset           (void) MONO_INTERNAL;
1271 gint32    mono_get_lmf_tls_offset           (void) MONO_INTERNAL;
1272 gint32    mono_get_lmf_addr_tls_offset      (void) MONO_INTERNAL;
1273 MonoInst* mono_get_jit_tls_intrinsic        (MonoCompile *cfg) MONO_INTERNAL;
1274 GList    *mono_varlist_insert_sorted        (MonoCompile *cfg, GList *list, MonoMethodVar *mv, gboolean sort_end) MONO_INTERNAL;
1275 GList    *mono_varlist_sort                 (MonoCompile *cfg, GList *list, int sort_type) MONO_INTERNAL;
1276 void      mono_analyze_liveness             (MonoCompile *cfg) MONO_INTERNAL;
1277 void      mono_linear_scan                  (MonoCompile *cfg, GList *vars, GList *regs, regmask_t *used_mask) MONO_INTERNAL;
1278 void      mono_global_regalloc              (MonoCompile *cfg) MONO_INTERNAL;
1279 void      mono_create_jump_table            (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks) MONO_INTERNAL;
1280 int       mono_compile_assembly             (MonoAssembly *ass, guint32 opts, const char *aot_options) MONO_INTERNAL;
1281 MonoCompile *mini_method_compile            (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, gboolean compile_aot, int parts) MONO_INTERNAL;
1282 void      mono_destroy_compile              (MonoCompile *cfg) MONO_INTERNAL;
1283 MonoJitICallInfo *mono_find_jit_opcode_emulation (int opcode) MONO_INTERNAL;
1284 void      mono_print_ins_index (int i, MonoInst *ins) MONO_INTERNAL;
1285 void      mono_print_ins (MonoInst *ins) MONO_INTERNAL;
1286 gboolean  mini_assembly_can_skip_verification (MonoDomain *domain, MonoMethod *method) MONO_INTERNAL;
1287 gboolean  mini_method_verify (MonoCompile *cfg, MonoMethod *method) MONO_INTERNAL;
1288
1289 gboolean  mini_class_is_system_array (MonoClass *klass) MONO_INTERNAL;
1290 MonoMethodSignature *mono_get_element_address_signature (int arity) MONO_INTERNAL;
1291 MonoJitICallInfo    *mono_get_element_address_icall (int rank) MONO_INTERNAL;
1292 MonoJitICallInfo    *mono_get_array_new_va_icall (int rank) MONO_INTERNAL;
1293
1294 void      mono_linterval_add_range          (MonoCompile *cfg, MonoLiveInterval *interval, int from, int to) MONO_INTERNAL;
1295 void      mono_linterval_print              (MonoLiveInterval *interval) MONO_INTERNAL;
1296 void      mono_linterval_print_nl (MonoLiveInterval *interval) MONO_INTERNAL;
1297 gboolean  mono_linterval_covers             (MonoLiveInterval *interval, int pos) MONO_INTERNAL;
1298 gint32    mono_linterval_get_intersect_pos  (MonoLiveInterval *i1, MonoLiveInterval *i2) MONO_INTERNAL;
1299 void      mono_linterval_split              (MonoCompile *cfg, MonoLiveInterval *interval, MonoLiveInterval **i1, MonoLiveInterval **i2, int pos) MONO_INTERNAL;
1300 void      mono_liveness_handle_exception_clauses (MonoCompile *cfg) MONO_INTERNAL;
1301
1302 /* AOT */
1303 void      mono_aot_init                     (void) MONO_INTERNAL;
1304 gpointer  mono_aot_get_method               (MonoDomain *domain,
1305                                                                                          MonoMethod *method) MONO_INTERNAL;
1306 gpointer  mono_aot_get_method_from_token    (MonoDomain *domain, MonoImage *image, guint32 token) MONO_INTERNAL;
1307 gboolean  mono_aot_is_got_entry             (guint8 *code, guint8 *addr) MONO_INTERNAL;
1308 guint8*   mono_aot_get_plt_entry            (guint8 *code) MONO_INTERNAL;
1309 guint32   mono_aot_get_plt_info_offset      (gssize *regs, guint8 *code) MONO_INTERNAL;
1310 gboolean  mono_aot_get_cached_class_info    (MonoClass *klass, MonoCachedClassInfo *res) MONO_INTERNAL;
1311 gboolean  mono_aot_get_class_from_name      (MonoImage *image, const char *name_space, const char *name, MonoClass **klass) MONO_INTERNAL;
1312 MonoJitInfo* mono_aot_find_jit_info         (MonoDomain *domain, MonoImage *image, gpointer addr) MONO_INTERNAL;
1313 void mono_aot_set_make_unreadable           (gboolean unreadable) MONO_INTERNAL;
1314 gboolean mono_aot_is_pagefault              (void *ptr) MONO_INTERNAL;
1315 void mono_aot_handle_pagefault              (void *ptr) MONO_INTERNAL;
1316 guint32 mono_aot_get_n_pagefaults           (void) MONO_INTERNAL;
1317 gpointer mono_aot_plt_resolve               (gpointer aot_module, guint32 plt_info_offset, guint8 *code) MONO_INTERNAL;
1318 gpointer mono_aot_get_method_from_vt_slot   (MonoDomain *domain, MonoVTable *vtable, int slot) MONO_INTERNAL;
1319 gpointer mono_aot_create_specific_trampoline   (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len) MONO_INTERNAL;
1320 gpointer mono_aot_get_named_code            (const char *name) MONO_INTERNAL;
1321 gpointer mono_aot_get_unbox_trampoline      (MonoMethod *method) MONO_INTERNAL;
1322 /* This is an exported function */
1323 void     mono_aot_register_globals          (gpointer *globals);
1324 /* This too */
1325 void     mono_aot_register_module           (gpointer *aot_info);
1326
1327 gboolean  mono_method_blittable             (MonoMethod *method) MONO_INTERNAL;
1328 gboolean  mono_method_same_domain           (MonoJitInfo *caller, MonoJitInfo *callee) MONO_INTERNAL;
1329
1330 void      mono_register_opcode_emulation    (int opcode, const char* name, const char *sigstr, gpointer func, gboolean no_throw) MONO_INTERNAL;
1331 void      mono_draw_graph                   (MonoCompile *cfg, MonoGraphOptions draw_options) MONO_INTERNAL;
1332 void      mono_add_varcopy_to_end           (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest) MONO_INTERNAL;
1333 void      mono_add_ins_to_end               (MonoBasicBlock *bb, MonoInst *inst) MONO_INTERNAL;
1334 gpointer  mono_create_ftnptr                (MonoDomain *domain, gpointer addr) MONO_INTERNAL;
1335
1336 void      mono_replace_ins                  (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **prev, MonoBasicBlock *first_bb, MonoBasicBlock *last_bb);
1337
1338 int               mono_find_method_opcode      (MonoMethod *method) MONO_INTERNAL;
1339 MonoJitICallInfo *mono_find_jit_icall_by_name  (const char *name) MONO_INTERNAL;
1340 MonoJitICallInfo *mono_find_jit_icall_by_addr  (gconstpointer addr) MONO_INTERNAL;
1341 MonoJitICallInfo *mono_register_jit_icall      (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save) MONO_INTERNAL;
1342 gconstpointer     mono_icall_get_wrapper       (MonoJitICallInfo* callinfo) MONO_INTERNAL;
1343
1344 void              mono_trampolines_init (void) MONO_INTERNAL;
1345 void              mono_trampolines_cleanup (void) MONO_INTERNAL;
1346 guint8 *          mono_get_trampoline_code (MonoTrampolineType tramp_type) MONO_INTERNAL;
1347 gpointer          mono_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len) MONO_INTERNAL;
1348 gpointer          mono_create_jump_trampoline (MonoDomain *domain, 
1349                                                                                            MonoMethod *method, 
1350                                                                                            gboolean add_sync_wrapper) MONO_INTERNAL;
1351 gpointer          mono_create_class_init_trampoline (MonoVTable *vtable) MONO_INTERNAL;
1352 gpointer          mono_create_generic_class_init_trampoline (void) MONO_INTERNAL;
1353 gpointer          mono_create_jit_trampoline (MonoMethod *method) MONO_INTERNAL;
1354 gpointer          mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token) MONO_INTERNAL;
1355 gpointer          mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method) MONO_INTERNAL;
1356 gpointer          mono_create_delegate_trampoline (MonoClass *klass) MONO_INTERNAL;
1357 gpointer          mono_create_rgctx_lazy_fetch_trampoline (guint32 offset) MONO_INTERNAL;
1358 MonoVTable*       mono_find_class_init_trampoline_by_addr (gconstpointer addr) MONO_INTERNAL;
1359 MonoClass*        mono_find_delegate_trampoline_by_addr (gconstpointer addr) MONO_INTERNAL;
1360 guint32           mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr) MONO_INTERNAL;
1361 gpointer          mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp) MONO_INTERNAL;
1362 gpointer          mono_delegate_trampoline (gssize *regs, guint8 *code, gpointer *tramp_data, guint8* tramp) MONO_INTERNAL;
1363 gpointer          mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info, 
1364                                                                            guint8* tramp) MONO_INTERNAL;
1365 gpointer          mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *token_info, 
1366                                                                                    guint8* tramp) MONO_INTERNAL;
1367 void              mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp) MONO_INTERNAL;
1368 void              mono_generic_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp) MONO_INTERNAL;
1369 gconstpointer     mono_get_trampoline_func (MonoTrampolineType tramp_type);
1370 gpointer          mini_get_vtable_trampoline (void) MONO_INTERNAL;
1371
1372 gboolean          mono_running_on_valgrind (void) MONO_INTERNAL;
1373 void*             mono_global_codeman_reserve (int size) MONO_INTERNAL;
1374 const char       *mono_regname_full (int reg, gboolean fp) MONO_INTERNAL;
1375 gint32*           mono_allocate_stack_slots_full (MonoCompile *cfg, gboolean backward, guint32 *stack_size, guint32 *stack_align) MONO_INTERNAL;
1376 gint32*           mono_allocate_stack_slots (MonoCompile *cfg, guint32 *stack_size, guint32 *stack_align) MONO_INTERNAL;
1377 void              mono_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb) MONO_INTERNAL;
1378 MonoInst         *mono_branch_optimize_exception_target (MonoCompile *cfg, MonoBasicBlock *bb, const char * exname) MONO_INTERNAL;
1379 void              mono_remove_critical_edges (MonoCompile *cfg) MONO_INTERNAL;
1380 gboolean          mono_is_regsize_var (MonoType *t) MONO_INTERNAL;
1381 void              mini_emit_memcpy2 (MonoCompile *cfg, int destreg, int doffset, int srcreg, int soffset, int size, int align) MONO_INTERNAL;
1382 CompRelation      mono_opcode_to_cond (int opcode) MONO_INTERNAL;
1383 CompType          mono_opcode_to_type (int opcode, int cmp_opcode) MONO_INTERNAL;
1384 CompRelation      mono_negate_cond (CompRelation cond) MONO_INTERNAL;
1385 int               mono_op_imm_to_op (int opcode) MONO_INTERNAL;
1386 void              mono_decompose_op_imm (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins) MONO_INTERNAL;
1387 void              mono_peephole_ins (MonoBasicBlock *bb, MonoInst *ins) MONO_INTERNAL;
1388
1389 void              mono_decompose_opcode (MonoCompile *cfg, MonoInst *ins) MONO_INTERNAL;
1390 void              mono_decompose_long_opts (MonoCompile *cfg) MONO_INTERNAL;
1391 void              mono_decompose_vtype_opts (MonoCompile *cfg) MONO_INTERNAL;
1392 void              mono_decompose_array_access_opts (MonoCompile *cfg) MONO_INTERNAL;
1393 void              mono_handle_soft_float (MonoCompile *cfg) MONO_INTERNAL;
1394 void              mono_handle_global_vregs (MonoCompile *cfg) MONO_INTERNAL;
1395 void              mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts) MONO_INTERNAL;
1396 void              mono_if_conversion (MonoCompile *cfg) MONO_INTERNAL;
1397
1398 /* methods that must be provided by the arch-specific port */
1399 void      mono_arch_init                        (void) MONO_INTERNAL;
1400 void      mono_arch_cleanup                     (void) MONO_INTERNAL;
1401 void      mono_arch_cpu_init                    (void) MONO_INTERNAL;
1402 guint32   mono_arch_cpu_optimizazions           (guint32 *exclude_mask) MONO_INTERNAL;
1403 void      mono_arch_instrument_mem_needs        (MonoMethod *method, int *stack, int *code) MONO_INTERNAL;
1404 void     *mono_arch_instrument_prolog           (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments) MONO_INTERNAL;
1405 void     *mono_arch_instrument_epilog           (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments) MONO_INTERNAL;
1406 MonoCallInst *mono_arch_call_opcode             (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) MONO_INTERNAL;
1407 MonoInst *mono_arch_get_inst_for_method         (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args) MONO_INTERNAL;
1408 void      mono_codegen                          (MonoCompile *cfg) MONO_INTERNAL;
1409 void      mono_call_inst_add_outarg_reg         (MonoCompile *cfg, MonoCallInst *call, int vreg, int hreg, gboolean fp) MONO_INTERNAL;
1410 const char *mono_arch_regname                   (int reg) MONO_INTERNAL;
1411 const char *mono_arch_fregname                  (int reg) MONO_INTERNAL;
1412 gpointer  mono_arch_get_throw_exception         (void) MONO_INTERNAL;
1413 gpointer  mono_arch_get_rethrow_exception       (void) MONO_INTERNAL;
1414 gpointer  mono_arch_get_throw_exception_by_name (void) MONO_INTERNAL;
1415 gpointer  mono_arch_get_throw_corlib_exception  (void) MONO_INTERNAL;
1416 guchar*   mono_arch_create_trampoline_code      (MonoTrampolineType tramp_type) MONO_INTERNAL;
1417 guchar*   mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *code_size, MonoJumpInfo **ji, gboolean aot) MONO_INTERNAL;
1418 gpointer  mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot) MONO_INTERNAL;
1419 gpointer  mono_arch_create_generic_class_init_trampoline (void) MONO_INTERNAL;
1420 gpointer  mono_arch_get_nullified_class_init_trampoline (guint32 *code_len) MONO_INTERNAL;
1421 GList    *mono_arch_get_allocatable_int_vars    (MonoCompile *cfg) MONO_INTERNAL;
1422 GList    *mono_arch_get_global_int_regs         (MonoCompile *cfg) MONO_INTERNAL;
1423 GList    *mono_arch_get_global_fp_regs          (MonoCompile *cfg) MONO_INTERNAL;
1424 GList    *mono_arch_get_iregs_clobbered_by_call (MonoCallInst *call) MONO_INTERNAL;
1425 GList    *mono_arch_get_fregs_clobbered_by_call (MonoCallInst *call) MONO_INTERNAL;
1426 guint32   mono_arch_regalloc_cost               (MonoCompile *cfg, MonoMethodVar *vmv) MONO_INTERNAL;
1427 void      mono_arch_patch_code                  (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors) MONO_INTERNAL;
1428 void      mono_arch_flush_icache                (guint8 *code, gint size) MONO_INTERNAL;
1429 int       mono_arch_max_epilog_size             (MonoCompile *cfg) MONO_INTERNAL;
1430 guint8   *mono_arch_emit_prolog                 (MonoCompile *cfg) MONO_INTERNAL;
1431 void      mono_arch_emit_epilog                 (MonoCompile *cfg) MONO_INTERNAL;
1432 void      mono_arch_emit_exceptions             (MonoCompile *cfg) MONO_INTERNAL;
1433 void      mono_arch_lowering_pass               (MonoCompile *cfg, MonoBasicBlock *bb) MONO_INTERNAL;
1434 void      mono_arch_peephole_pass_1             (MonoCompile *cfg, MonoBasicBlock *bb) MONO_INTERNAL;
1435 void      mono_arch_peephole_pass_2             (MonoCompile *cfg, MonoBasicBlock *bb) MONO_INTERNAL;
1436 void      mono_arch_output_basic_block          (MonoCompile *cfg, MonoBasicBlock *bb) MONO_INTERNAL;
1437 gboolean  mono_arch_has_unwind_info             (gconstpointer addr) MONO_INTERNAL;
1438 void      mono_arch_setup_jit_tls_data          (MonoJitTlsData *tls) MONO_INTERNAL;
1439 void      mono_arch_free_jit_tls_data           (MonoJitTlsData *tls) MONO_INTERNAL;
1440 void      mono_arch_emit_this_vret_args         (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg) MONO_INTERNAL;
1441 void      mono_arch_fill_argument_info          (MonoCompile *cfg) MONO_INTERNAL;
1442 void      mono_arch_allocate_vars               (MonoCompile *m) MONO_INTERNAL;
1443 int       mono_arch_get_argument_info           (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info) MONO_INTERNAL;
1444 gboolean  mono_arch_print_tree                  (MonoInst *tree, int arity) MONO_INTERNAL;
1445 void      mono_arch_emit_call                   (MonoCompile *cfg, MonoCallInst *call) MONO_INTERNAL;
1446 void      mono_arch_emit_outarg_vt              (MonoCompile *cfg, MonoInst *ins, MonoInst *src) MONO_INTERNAL;
1447 void      mono_arch_emit_setret                 (MonoCompile *cfg, MonoMethod *method, MonoInst *val) MONO_INTERNAL;
1448 MonoInst *mono_arch_emit_inst_for_method        (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args) MONO_INTERNAL;
1449 void      mono_arch_decompose_opts              (MonoCompile *cfg, MonoInst *ins) MONO_INTERNAL;
1450 void      mono_arch_decompose_long_opts         (MonoCompile *cfg, MonoInst *ins) MONO_INTERNAL;
1451
1452 MonoJitInfo *mono_arch_find_jit_info            (MonoDomain *domain, 
1453                                                  MonoJitTlsData *jit_tls, 
1454                                                  MonoJitInfo *res, 
1455                                                  MonoJitInfo *prev_ji, 
1456                                                  MonoContext *ctx, 
1457                                                  MonoContext *new_ctx, 
1458                                                  MonoLMF **lmf, 
1459                                                  gboolean *managed) MONO_INTERNAL;
1460 gpointer mono_arch_get_call_filter              (void) MONO_INTERNAL;
1461 gpointer mono_arch_get_restore_context          (void) MONO_INTERNAL;
1462 gpointer mono_arch_get_call_filter_full         (guint32 *code_size, MonoJumpInfo **ji, gboolean aot) MONO_INTERNAL;
1463 gpointer mono_arch_get_restore_context_full     (guint32 *code_size, MonoJumpInfo **ji, gboolean aot) MONO_INTERNAL;
1464 gpointer  mono_arch_get_throw_exception_full    (guint32 *code_size, MonoJumpInfo **ji, gboolean aot) MONO_INTERNAL;
1465 gpointer  mono_arch_get_rethrow_exception_full  (guint32 *code_size, MonoJumpInfo **ji, gboolean aot) MONO_INTERNAL;
1466 gpointer  mono_arch_get_throw_exception_by_name_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot) MONO_INTERNAL;
1467 gpointer  mono_arch_get_throw_corlib_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot) MONO_INTERNAL;
1468 gboolean mono_arch_handle_exception             (void *sigctx, gpointer obj, gboolean test_only) MONO_INTERNAL;
1469 void     mono_arch_handle_altstack_exception    (void *sigctx, gpointer fault_addr, gboolean stack_ovf) MONO_INTERNAL;
1470 gpointer mono_arch_ip_from_context              (void *sigctx) MONO_INTERNAL;
1471 void     mono_arch_sigctx_to_monoctx            (void *sigctx, MonoContext *ctx) MONO_INTERNAL;
1472 void     mono_arch_monoctx_to_sigctx            (MonoContext *mctx, void *ctx) MONO_INTERNAL;
1473 gpointer mono_arch_context_get_int_reg          (MonoContext *ctx, int reg) MONO_INTERNAL;
1474 void     mono_arch_flush_register_windows       (void) MONO_INTERNAL;
1475 gboolean mono_arch_is_inst_imm                  (gint64 imm) MONO_INTERNAL;
1476 MonoInst* mono_arch_get_domain_intrinsic        (MonoCompile* cfg) MONO_INTERNAL;
1477 MonoInst* mono_arch_get_thread_intrinsic        (MonoCompile* cfg) MONO_INTERNAL;
1478 gboolean mono_arch_is_int_overflow              (void *sigctx, void *info) MONO_INTERNAL;
1479 void     mono_arch_invalidate_method            (MonoJitInfo *ji, void *func, gpointer func_arg) MONO_INTERNAL;
1480 guint32  mono_arch_get_patch_offset             (guint8 *code) MONO_INTERNAL;
1481 gpointer*mono_arch_get_vcall_slot_addr          (guint8* code, gpointer *regs) MONO_INTERNAL;
1482 gpointer mono_arch_get_vcall_slot               (guint8 *code, gpointer *regs, int *displacement) MONO_INTERNAL;
1483 gpointer*mono_arch_get_delegate_method_ptr_addr (guint8* code, gpointer *regs) MONO_INTERNAL;
1484 void     mono_arch_create_vars                  (MonoCompile *cfg) MONO_INTERNAL;
1485 void     mono_arch_save_unwind_info             (MonoCompile *cfg) MONO_INTERNAL;
1486 void     mono_arch_register_lowlevel_calls      (void) MONO_INTERNAL;
1487 gpointer mono_arch_get_unbox_trampoline         (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr) MONO_INTERNAL;
1488 void     mono_arch_patch_callsite               (guint8 *method_start, guint8 *code, guint8 *addr) MONO_INTERNAL;
1489 void     mono_arch_patch_plt_entry              (guint8 *code, guint8 *addr) MONO_INTERNAL;
1490 void     mono_arch_nullify_class_init_trampoline(guint8 *code, gssize *regs) MONO_INTERNAL;
1491 void     mono_arch_nullify_plt_entry            (guint8 *code) MONO_INTERNAL;
1492 int      mono_arch_get_this_arg_reg             (MonoMethodSignature *sig, MonoGenericSharingContext *gsctx, guint8 *code) MONO_INTERNAL;
1493 gpointer mono_arch_get_this_arg_from_call       (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, gssize *regs, guint8 *code) MONO_INTERNAL;
1494 MonoObject* mono_arch_find_this_argument        (gpointer *regs, MonoMethod *method, MonoGenericSharingContext *gsctx) MONO_INTERNAL;
1495 gpointer mono_arch_get_delegate_invoke_impl     (MonoMethodSignature *sig, gboolean has_target) MONO_INTERNAL;
1496 gpointer mono_arch_create_specific_trampoline   (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len) MONO_INTERNAL;
1497 void        mono_arch_emit_imt_argument         (MonoCompile *cfg, MonoCallInst *call) MONO_INTERNAL;
1498 MonoMethod* mono_arch_find_imt_method           (gpointer *regs, guint8 *code) MONO_INTERNAL;
1499 MonoVTable* mono_arch_find_static_call_vtable   (gpointer *regs, guint8 *code) MONO_INTERNAL;
1500 gpointer    mono_arch_build_imt_thunk           (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count) MONO_INTERNAL;
1501 void    mono_arch_notify_pending_exc            (void) MONO_INTERNAL;
1502 void    mono_arch_fixup_jinfo                   (MonoCompile *cfg) MONO_INTERNAL;
1503
1504 /* Exception handling */
1505 void     mono_exceptions_init                   (void) MONO_INTERNAL;
1506 gboolean mono_handle_exception                  (MonoContext *ctx, gpointer obj,
1507                                                  gpointer original_ip, gboolean test_only) MONO_INTERNAL;
1508 void     mono_handle_native_sigsegv             (int signal, void *sigctx) MONO_INTERNAL;
1509 void     mono_print_thread_dump                 (void *sigctx);
1510 void     mono_jit_walk_stack                    (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) MONO_INTERNAL;
1511 void     mono_jit_walk_stack_from_ctx           (MonoStackWalk func, MonoContext *ctx, gboolean do_il_offset, gpointer user_data) MONO_INTERNAL;
1512 void     mono_setup_altstack                    (MonoJitTlsData *tls) MONO_INTERNAL;
1513 void     mono_free_altstack                     (MonoJitTlsData *tls) MONO_INTERNAL;
1514 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;
1515
1516 gpointer mono_get_throw_exception               (void) MONO_INTERNAL;
1517 gpointer mono_get_rethrow_exception             (void) MONO_INTERNAL;
1518 gpointer mono_get_call_filter                   (void) MONO_INTERNAL;
1519 gpointer mono_get_restore_context               (void) MONO_INTERNAL;
1520 gpointer mono_get_throw_exception_by_name       (void) MONO_INTERNAL;
1521 gpointer mono_get_throw_corlib_exception        (void) MONO_INTERNAL;
1522
1523 /* the new function to do stack walks */
1524 typedef gboolean (*MonoStackFrameWalk)          (MonoDomain *domain, MonoContext *ctx, MonoJitInfo *ji, gpointer data);
1525 void      mono_walk_stack                       (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoContext *start_ctx, MonoStackFrameWalk func, gpointer user_data);
1526
1527 MonoArray *ves_icall_get_trace                  (MonoException *exc, gint32 skip, MonoBoolean need_file_info) MONO_INTERNAL;
1528 MonoBoolean ves_icall_get_frame_info            (gint32 skip, MonoBoolean need_file_info, 
1529                                                  MonoReflectionMethod **method, 
1530                                                  gint32 *iloffset, gint32 *native_offset,
1531                                                  MonoString **file, gint32 *line, gint32 *column) MONO_INTERNAL;
1532 MonoString *ves_icall_System_Exception_get_trace (MonoException *exc) MONO_INTERNAL;
1533
1534 /* Dominator/SSA methods */
1535 void        mono_compile_dominator_info         (MonoCompile *cfg, int dom_flags) MONO_INTERNAL;
1536 void        mono_compute_natural_loops          (MonoCompile *cfg) MONO_INTERNAL;
1537 MonoBitSet* mono_compile_iterated_dfrontier     (MonoCompile *cfg, MonoBitSet *set) MONO_INTERNAL;
1538 void        mono_ssa_compute                    (MonoCompile *cfg) MONO_INTERNAL;
1539 void        mono_ssa_remove                     (MonoCompile *cfg) MONO_INTERNAL;
1540 void        mono_ssa_cprop                      (MonoCompile *cfg) MONO_INTERNAL;
1541 void        mono_ssa_deadce                     (MonoCompile *cfg) MONO_INTERNAL;
1542 void        mono_ssa_strength_reduction         (MonoCompile *cfg) MONO_INTERNAL;
1543 void        mono_free_loop_info                 (MonoCompile *cfg) MONO_INTERNAL;
1544
1545 void        mono_ssa_compute2                   (MonoCompile *cfg);
1546 void        mono_ssa_remove2                    (MonoCompile *cfg);
1547 void        mono_ssa_cprop2                     (MonoCompile *cfg);
1548 void        mono_ssa_deadce2                    (MonoCompile *cfg);
1549
1550 /* debugging support */
1551 void      mono_debug_init_method                (MonoCompile *cfg, MonoBasicBlock *start_block,
1552                                                  guint32 breakpoint_id) MONO_INTERNAL;
1553 void      mono_debug_open_method                (MonoCompile *cfg) MONO_INTERNAL;
1554 void      mono_debug_close_method               (MonoCompile *cfg) MONO_INTERNAL;
1555 void      mono_debug_open_block                 (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address) MONO_INTERNAL;
1556 void      mono_debug_record_line_number         (MonoCompile *cfg, MonoInst *ins, guint32 address) MONO_INTERNAL;
1557 void      mono_debug_serialize_debug_info       (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len) MONO_INTERNAL;
1558 void      mono_debug_add_aot_method             (MonoDomain *domain,
1559                                                  MonoMethod *method, guint8 *code_start, 
1560                                                  guint8 *debug_info, guint32 debug_info_len) MONO_INTERNAL;
1561 void      mono_debug_add_icall_wrapper          (MonoMethod *method, MonoJitICallInfo* info) MONO_INTERNAL;
1562 void      mono_debug_print_vars                 (gpointer ip, gboolean only_arguments);
1563 void      mono_debugger_run_finally             (MonoContext *start_ctx);
1564
1565 extern gssize mono_breakpoint_info_index [MONO_BREAKPOINT_ARRAY_SIZE];
1566
1567 gboolean mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size);
1568
1569 /* Mono Debugger support */
1570 void      mono_debugger_init                    (void);
1571 int       mono_debugger_main                    (MonoDomain *domain, MonoAssembly *assembly, int argc, char **argv);
1572
1573
1574 /* Tracing */
1575 MonoTraceSpec *mono_trace_parse_options         (const char *options) MONO_INTERNAL;
1576 void           mono_trace_set_assembly          (MonoAssembly *assembly) MONO_INTERNAL;
1577 gboolean       mono_trace_eval                  (MonoMethod *method) MONO_INTERNAL;
1578
1579 extern void
1580 mono_perform_abc_removal (MonoCompile *cfg) MONO_INTERNAL;
1581 extern void
1582 mono_perform_abc_removal2 (MonoCompile *cfg) MONO_INTERNAL;
1583 extern void
1584 mono_perform_ssapre (MonoCompile *cfg) MONO_INTERNAL;
1585 extern void
1586 mono_local_cprop (MonoCompile *cfg) MONO_INTERNAL;
1587 extern void
1588 mono_local_cprop2 (MonoCompile *cfg);
1589 extern void
1590 mono_local_deadce (MonoCompile *cfg);
1591
1592 /* CAS - stack walk */
1593 MonoSecurityFrame* ves_icall_System_Security_SecurityFrame_GetSecurityFrame (gint32 skip) MONO_INTERNAL;
1594 MonoArray* ves_icall_System_Security_SecurityFrame_GetSecurityStack (gint32 skip) MONO_INTERNAL;
1595
1596 /* Generic sharing */
1597
1598 MonoGenericSharingContext* mono_get_generic_context_from_code (guint8 *code) MONO_INTERNAL;
1599
1600 MonoGenericContext* mini_method_get_context (MonoMethod *method) MONO_INTERNAL;
1601
1602 int mono_method_check_context_used (MonoMethod *method) MONO_INTERNAL;
1603
1604 gboolean mono_generic_context_equal_deep (MonoGenericContext *context1, MonoGenericContext *context2) MONO_INTERNAL;
1605
1606 gboolean mono_generic_context_is_sharable (MonoGenericContext *context, gboolean allow_type_vars) MONO_INTERNAL;
1607
1608 gboolean mono_method_is_generic_impl (MonoMethod *method) MONO_INTERNAL;
1609 gboolean mono_method_is_generic_sharable_impl (MonoMethod *method, gboolean allow_type_vars) MONO_INTERNAL;
1610
1611 gpointer mono_helper_get_rgctx_other_ptr (MonoClass *caller_class, MonoVTable *vtable,
1612                                           guint32 token, guint32 token_source, guint32 rgctx_type,
1613                                           gint32 rgctx_index) MONO_INTERNAL;
1614
1615 void mono_generic_sharing_init (void) MONO_INTERNAL;
1616
1617 MonoClass* mini_class_get_container_class (MonoClass *class) MONO_INTERNAL;
1618 MonoGenericContext* mini_class_get_context (MonoClass *class) MONO_INTERNAL;
1619
1620 MonoType* mini_get_basic_type_from_generic (MonoGenericSharingContext *gsctx, MonoType *type) MONO_INTERNAL;
1621
1622 int mini_type_stack_size (MonoGenericSharingContext *gsctx, MonoType *t, int *align) MONO_INTERNAL;
1623
1624 /* wapihandles.c */
1625 int mini_wapi_hps (int argc, char **argv) MONO_INTERNAL;
1626
1627 int mini_wapi_semdel (int argc, char **argv) MONO_INTERNAL;
1628
1629 int mini_wapi_seminfo (int argc, char **argv) MONO_INTERNAL;
1630
1631 #endif /* __MONO_MINI_H__ */