2004-11-17 Atsushi Enomoto <atsushi@ximian.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
19 #include "mini-arch.h"
20 #include "regalloc.h"
21 #include "declsec.h"
22
23 #define MONO_USE_AOT_COMPILER
24
25 /* for 32 bit systems */
26 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
27 #define MINI_LS_WORD_OFFSET 0
28 #define MINI_MS_WORD_OFFSET 4
29 #define inst_ls_word data.op[0].const_val
30 #define inst_ms_word data.op[1].const_val
31 #else
32 #define MINI_LS_WORD_OFFSET 4
33 #define MINI_MS_WORD_OFFSET 0
34 #define inst_ls_word data.op[1].const_val
35 #define inst_ms_word data.op[0].const_val
36 #endif
37
38 /* Version number of the AOT file format */
39 #define MONO_AOT_FILE_VERSION "9"
40
41 #if 1
42 #define mono_bitset_test_fast(set,n) (((guint32*)set)[2+(n)/32] & (1 << ((n) % 32)))
43 #else
44 #define mono_bitset_test_fast(set,n) mono_bitset_test(set,n)
45 #endif
46
47 #if 0
48 #define mono_bitset_foreach_bit(set,b,n) \
49         for (b = 0; b < n; b++)\
50                 if (mono_bitset_test_fast(set,b))
51 #define mono_bitset_foreach_bit_rev(set,b,n) \
52         for (b = n - 1; b >= 0; b--)\
53                 if (mono_bitset_test_fast(set,b))
54 #else
55 #define mono_bitset_foreach_bit(set,b,n) \
56         for (b = mono_bitset_find_first (set, -1); b < n && b >= 0; b = mono_bitset_find_first (set, b))
57 #define mono_bitset_foreach_bit_rev(set,b,n) \
58         for (b = mono_bitset_find_last (set, n - 1); b >= 0; b = b ? mono_bitset_find_last (set, b) : -1)
59  
60 #endif
61
62 /*
63  * Pull the list of opcodes
64  */
65 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
66         a = i,
67
68 enum {
69 #include "mono/cil/opcode.def"
70         CEE_LASTOP
71 };
72 #undef OPDEF
73
74 #define MONO_VARINFO(cfg,varnum) ((cfg)->vars [varnum])
75
76 #define MONO_INST_NEW(cfg,dest,op) do { \
77                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
78                 (dest)->opcode = (op);  \
79         } while (0)
80
81 #define MONO_INST_NEW_CALL(cfg,dest,op) do {    \
82                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoCallInst));   \
83                 (dest)->inst.opcode = (op);     \
84         } while (0)
85
86 #define MONO_ADD_INS(b,inst) do {       \
87                 if ((b)->last_ins) {    \
88                         (b)->last_ins->next = (inst);   \
89                         (b)->last_ins = (inst); \
90                 } else {        \
91                         (b)->code = (b)->last_ins = (inst);     \
92                 }       \
93         } while (0)
94
95 typedef struct MonoInst MonoInst;
96 typedef struct MonoCallInst MonoCallInst;
97 typedef struct MonoEdge MonoEdge;
98 typedef struct MonoMethodVar MonoMethodVar;
99 typedef struct MonoBasicBlock MonoBasicBlock;
100 typedef struct MonoLMF MonoLMF;
101 typedef struct MonoSpillInfo MonoSpillInfo;
102 typedef struct MonoTraceSpec MonoTraceSpec;
103
104 extern guint32 mono_jit_tls_id;
105 extern MonoTraceSpec *mono_jit_trace_calls;
106 extern gboolean mono_break_on_exc;
107 extern int mono_exc_esp_offset;
108 extern gboolean mono_compile_aot;
109
110 struct MonoEdge {
111         MonoEdge *next;
112         MonoBasicBlock *bb;
113         /* add edge type? */
114 };
115
116 struct MonoSpillInfo {
117         MonoSpillInfo *next;
118         int offset;
119 };
120
121 /*
122  * The IR-level basic block.  
123  *
124  * A basic block can have multiple exits just fine, as long as the point of
125  * 'departure' is the last instruction in the basic block. Extended basic
126  * blocks, on the other hand, may have instructions that leave the block
127  * midstream. The important thing is that they cannot be _entered_
128  * midstream, ie, execution of a basic block (or extened bb) always start
129  * at the beginning of the block, never in the middle.
130  */
131 struct MonoBasicBlock {
132         MonoInst *last_ins;
133
134         /* Points to the start of the CIL code that initiated this BB */
135         unsigned char* cil_code;
136
137         /* Length of the CIL block */
138         gint32 cil_length;
139
140         /* The address of the generated code, used for fixups */
141         int native_offset;
142         int max_offset;
143         
144         gint32 dfn;
145
146         /* unique block number identification */
147         gint32 block_num;
148
149         /* Visited and reachable flags */
150         guint32 flags;
151
152         /* Basic blocks: incoming and outgoing counts and pointers */
153         gint16 out_count, in_count;
154         MonoBasicBlock **in_bb;
155         MonoBasicBlock **out_bb;
156
157         /* the next basic block in the order it appears in IL */
158         MonoBasicBlock *next_bb;
159
160         /*
161          * Before instruction selection it is the first tree in the
162          * forest and the first item in the list of trees. After
163          * instruction selection it is the first instruction and the
164          * first item in the list of instructions.
165          */
166         MonoInst *code;
167
168         /*
169          * SSA and loop based flags
170          */
171         MonoBitSet *dominators;
172         MonoBitSet *dfrontier;
173         MonoBasicBlock *idom;
174         GList *dominated;
175         /* fast dominator algorithm */
176         MonoBasicBlock *df_parent, *ancestor, *child, *label;
177         MonoEdge *bucket;
178         int size, sdom, idomn;
179         
180         /* loop nesting and recognition */
181         GList *loop_blocks;
182         gint8  nesting;
183         gint8  loop_body_start;
184
185         /* use for liveness analysis */
186         MonoBitSet *gen_set;
187         MonoBitSet *kill_set;
188         MonoBitSet *live_in_set;
189         MonoBitSet *live_out_set;
190
191         /* fields to deal with non-empty stack slots at bb boundary */
192         guint16 out_scount, in_scount;
193         MonoInst **out_stack;
194         MonoInst **in_stack;
195
196         /* we use that to prevent merging of bblock covered by different clauses*/
197         guint real_offset;
198
199         /*
200          * The region encodes whether the basic block is inside
201          * a finally, catch, filter or none of thoese.
202          *
203          * If the value is -1, then it is neither finally, catch nor filter
204          *
205          * Otherwise the format is:
206          *
207          *  Bits: |     0-3      |       4-7      |     8-31
208          *        |              |                |
209          *        | clause-flags |   MONO_REGION  | clause-index 
210          *
211          */
212         guint region;
213
214         /* The current symbolic register number, used in local register allocation. */
215         guint32 max_ireg, max_freg;
216 };
217
218 /* BBlock flags */
219 #define BB_VISITED 1
220 #define BB_REACHABLE 2
221
222 struct MonoInst {
223         union {
224                 union {
225                         MonoInst *src;
226                         MonoMethodVar *var;
227                         gssize const_val;
228                         gpointer p;
229                         MonoMethod *method;
230                         MonoMethodSignature *signature;
231                         MonoBasicBlock **many_blocks;
232                         MonoBasicBlock *target_block;
233                         MonoInst **args;
234                         MonoType *vtype;
235                         MonoClass *klass;
236                         int *phi_args;
237                 } op [2];
238                 gint64 i8const;
239                 double r8const;
240         } data;
241         guint16 opcode;
242         guint8  type; /* stack type */
243         guint   ssa_op : 3;
244         guint8  flags  : 5;
245         
246         /* used by the register allocator */
247         gint32 dreg, sreg1, sreg2, unused;
248         
249         MonoInst *next;
250         MonoClass *klass;
251         const unsigned char* cil_code; /* for debugging and bblock splitting */
252 };
253         
254 struct MonoCallInst {
255         MonoInst inst;
256         MonoMethodSignature *signature;
257         MonoMethod *method;
258         MonoInst **args;
259         MonoInst *out_args;
260         gconstpointer fptr;
261         guint stack_usage;
262         gboolean virtual;
263         regmask_t used_iregs;
264         regmask_t used_fregs;
265 #ifdef __x86_64__
266         GSList *out_ireg_args;
267         GSList *out_freg_args;
268 #endif
269 };
270
271 /* 
272  * flags for MonoInst
273  * Note: some of the values overlap, because they can't appear
274  * in the same MonoInst.
275  */
276 enum {
277         MONO_INST_HAS_METHOD = 1,
278         /* temp local created by a DUP: used only within a BB */
279         MONO_INST_IS_TEMP    = 1,
280         MONO_INST_INIT       = 1, /* in localloc */
281         MONO_INST_IS_DEAD    = 2,
282         MONO_INST_TAILCALL   = 4,
283         MONO_INST_VOLATILE   = 4,
284         MONO_INST_BRLABEL    = 4,
285         MONO_INST_NOTYPECHECK    = 4,
286         MONO_INST_UNALIGNED  = 8,
287         /* the address of the variable has been taken */
288         MONO_INST_INDIRECT   = 16,
289         MONO_INST_NORANGECHECK   = 16
290 };
291
292 #define inst_c0 data.op[0].const_val
293 #define inst_c1 data.op[1].const_val
294 #define inst_i0 data.op[0].src
295 #define inst_i1 data.op[1].src
296 #define inst_p0 data.op[0].p
297 #define inst_p1 data.op[1].p
298 #define inst_l  data.i8const
299 #define inst_r  data.r8const
300 #define inst_left  data.op[0].src
301 #define inst_right data.op[1].src
302
303 #define inst_newa_len   data.op[0].src
304 #define inst_newa_class data.op[1].klass
305
306 #define inst_var    data.op[0].var
307 #define inst_vtype  data.op[1].vtype
308 /* in branch instructions */
309 #define inst_many_bb   data.op[1].many_blocks
310 #define inst_target_bb data.op[0].target_block
311 #define inst_true_bb   data.op[1].many_blocks[0]
312 #define inst_false_bb  data.op[1].many_blocks[1]
313
314 #define inst_basereg sreg1
315 #define inst_indexreg sreg2
316 #define inst_destbasereg dreg
317 #define inst_offset data.op[0].const_val
318 #define inst_imm    data.op[1].const_val
319
320 #define inst_phi_args   data.op[1].phi_args
321
322 /* instruction description for use in regalloc/scheduling */
323 enum {
324         MONO_INST_DEST,
325         MONO_INST_SRC1,
326         MONO_INST_SRC2,
327         MONO_INST_FLAGS,
328         MONO_INST_CLOB,
329         MONO_INST_COST,
330         MONO_INST_DELAY,
331         MONO_INST_RES,
332         MONO_INST_LEN,
333         MONO_INST_MAX
334 };
335
336 typedef union {
337         struct {
338                 guint16 tid; /* tree number */
339                 guint16 bid; /* block number */
340         } pos ;
341         guint32 abs_pos; 
342 } MonoPosition;
343
344 typedef struct {
345         MonoPosition first_use, last_use;
346 } MonoLiveRange;
347
348 /*
349  * Additional information about a variable
350  */
351 struct MonoMethodVar {
352         guint           idx; /* inside cfg->varinfo, cfg->vars */
353         guint           last_name;
354         MonoBitSet     *dfrontier;
355         MonoLiveRange   range; /* generated by liveness analysis */
356         int             reg; /* != -1 if allocated into a register */
357         int             spill_costs;
358         MonoBitSet     *def_in; /* used by SSA */
359         MonoInst       *def;    /* used by SSA */
360         MonoBasicBlock *def_bb; /* used by SSA */
361         GList          *uses;   /* used by SSA */
362         char            cpstate;  /* used by SSA conditional  constant propagation */
363 };
364
365 typedef struct {
366         gpointer          end_of_stack;
367         guint32           stack_size;
368         MonoLMF          *lmf;
369         MonoLMF          *first_lmf;
370         gpointer         signal_stack;
371         guint32          signal_stack_size;
372         void            (*abort_func) (MonoObject *object);
373 } MonoJitTlsData;
374
375 typedef enum {
376         MONO_PATCH_INFO_BB,
377         MONO_PATCH_INFO_ABS,
378         MONO_PATCH_INFO_LABEL,
379         MONO_PATCH_INFO_METHOD,
380         MONO_PATCH_INFO_METHOD_JUMP,
381         MONO_PATCH_INFO_METHOD_REL,
382         MONO_PATCH_INFO_METHODCONST,
383         MONO_PATCH_INFO_INTERNAL_METHOD,
384         MONO_PATCH_INFO_SWITCH,
385         MONO_PATCH_INFO_EXC,
386         MONO_PATCH_INFO_EXC_NAME,
387         MONO_PATCH_INFO_CLASS,
388         MONO_PATCH_INFO_IMAGE,
389         MONO_PATCH_INFO_FIELD,
390         MONO_PATCH_INFO_VTABLE,
391         MONO_PATCH_INFO_CLASS_INIT,
392         MONO_PATCH_INFO_SFLDA,
393         MONO_PATCH_INFO_LDSTR,
394         MONO_PATCH_INFO_LDTOKEN,
395         MONO_PATCH_INFO_TYPE_FROM_HANDLE,
396         MONO_PATCH_INFO_R4,
397         MONO_PATCH_INFO_R8,
398         MONO_PATCH_INFO_IP,
399         MONO_PATCH_INFO_IID,
400         MONO_PATCH_INFO_BB_OVF,
401         MONO_PATCH_INFO_EXC_OVF,
402         MONO_PATCH_INFO_WRAPPER
403 } MonoJumpInfoType;
404
405 /*
406  * We need to store the image which the token refers to along with the token,
407  * since the image might not be the same as the image of the method which
408  * contains the relocation, because of inlining.
409  */
410 typedef struct MonoJumpInfoToken {
411         MonoImage *image;
412         guint32 token;
413 } MonoJumpInfoToken;
414
415 typedef struct MonoJumpInfo MonoJumpInfo;
416 struct MonoJumpInfo {
417         MonoJumpInfo *next;
418         union {
419                 int i;
420                 guint8 *p;
421                 MonoInst *label;
422         } ip;
423
424         MonoJumpInfoType type;
425         union {
426                 gconstpointer   target;
427 #if SIZEOF_VOID_P == 8
428                 gint64          offset;
429 #else
430                 int             offset;
431 #endif
432                 MonoBasicBlock *bb;
433                 MonoBasicBlock **table;
434                 MonoInst       *inst;
435                 MonoMethod     *method;
436                 MonoClass      *klass;
437                 MonoClassField *field;
438                 MonoImage      *image;
439                 MonoVTable     *vtable;
440                 const char     *name;
441                 MonoJumpInfoToken  *token;
442         } data;
443
444         int table_size; /* use by switch */
445 };
446
447 /* optimization flags: keep up to date with the name array in driver.c */
448 enum {
449         MONO_OPT_PEEPHOLE = 1 << 0,
450         MONO_OPT_BRANCH   = 1 << 1,
451         MONO_OPT_INLINE   = 1 << 2,
452         MONO_OPT_CFOLD    = 1 << 3,
453         MONO_OPT_CONSPROP = 1 << 4,
454         MONO_OPT_COPYPROP = 1 << 5,
455         MONO_OPT_DEADCE   = 1 << 6,
456         MONO_OPT_LINEARS  = 1 << 7,
457         MONO_OPT_CMOV     = 1 << 8,
458         MONO_OPT_SHARED   = 1 << 9,
459         MONO_OPT_SCHED    = 1 << 10,
460         MONO_OPT_INTRINS  = 1 << 11,
461         MONO_OPT_TAILC    = 1 << 12,
462         MONO_OPT_LOOP     = 1 << 13,
463         MONO_OPT_FCMOV    = 1 << 14,
464         MONO_OPT_LEAF     = 1 << 15,
465         MONO_OPT_AOT      = 1 << 16,
466         MONO_OPT_PRECOMP  = 1 << 17,
467         MONO_OPT_ABCREM   = 1 << 18
468 };
469
470 /* Bit-fields in the MonoBasicBlock.region */
471 #define MONO_REGION_FINALLY  16
472 #define MONO_REGION_CATCH    32
473 #define MONO_REGION_FAULT    64         /* Currently unused */
474 #define MONO_REGION_FILTER  128
475
476 /*
477  * Control Flow Graph and compilation unit information
478  */
479 typedef struct {
480         MonoMethod      *method;
481         MonoMemPool     *mempool;
482         MonoInst       **varinfo;
483         MonoMethodVar  **vars;
484         MonoInst        *ret;
485         MonoBasicBlock  *bb_entry;
486         MonoBasicBlock  *bb_exit;
487         MonoBasicBlock  *bb_init;
488         MonoBasicBlock **bblocks;
489         GHashTable      *bb_hash;
490         MonoMemPool     *state_pool; /* used by instruction selection */
491         MonoBasicBlock  *cbb;        /* used by instruction selection */
492         MonoInst        *prev_ins;   /* in decompose */
493         MonoJumpInfo    *patch_info;
494         MonoJitInfo     *jit_info;
495         MonoJitDynamicMethodInfo *dynamic_info;
496         guint            num_bblocks;
497         guint            locals_start;
498         guint            num_varinfo; /* used items in varinfo */
499         guint            varinfo_count; /* total storage in varinfo */
500         gint             stack_offset;
501         gint             max_ireg;
502         MonoRegState    *rs;
503         MonoSpillInfo   *spill_info; /* machine register spills */
504         MonoSpillInfo   *spill_info_float; /* fp register spills */
505         gint             spill_count;
506         /* unsigned char   *cil_code; */
507         MonoMethod      *inlined_method; /* the method which is currently inlined */
508
509         /* the exception object passed to catch/filter blocks */
510         MonoInst        *exvar;
511         
512         MonoInst        *domainvar; /* a cache for the current domain */
513
514         /* A hashtable of region ID-> SP var mappings */
515         /* An SP var is a place to store the stack pointer (used by handlers)*/
516         GHashTable      *spvars;
517
518         GList           *ldstr_list; /* used by AOT */
519         
520         MonoDomain      *domain;
521
522         unsigned char   *native_code;
523         guint            code_size;
524         guint            code_len;
525         guint            prolog_end;
526         guint            epilog_begin;
527         regmask_t        used_int_regs;
528         guint32          opt;
529         guint32          prof_options;
530         guint32          flags;
531         guint32          comp_done;
532         guint32          verbose_level;
533         guint32          stack_usage;
534         guint32          param_area;
535         guint32          frame_reg;
536         gint32           sig_cookie;
537         gboolean         disable_aot;
538         gboolean         disable_ssa;
539         gboolean         run_cctors;
540         gboolean         need_lmf_area;
541         gpointer         debug_info;
542         guint32          lmf_offset;
543         guint16          *intvars;
544         MonoProfileCoverageInfo *coverage_info;
545         MonoCompileArch  arch;
546 #ifdef __ia64
547         guint8           ins, locals, outs; /* reg stack region sizes */
548 #endif /* __ia64 */
549 } MonoCompile;
550
551 typedef enum {
552         MONO_CFG_HAS_ALLOCA = 1 << 0,
553         MONO_CFG_HAS_CALLS  = 1 << 1,
554         MONO_CFG_HAS_LDELEMA  = 1 << 2,
555         MONO_CFG_HAS_VARARGS  = 1 << 3,
556         MONO_CFG_HAS_TAIL     = 1 << 4
557 } MonoCompileFlags;
558
559 typedef struct {
560         gulong methods_compiled;
561         gulong methods_aot;
562         gulong methods_lookups;
563         gulong method_trampolines;
564         gulong allocate_var;
565         gulong analyze_stack_repeat;
566         gulong cil_code_size;
567         gulong native_code_size;
568         gulong code_reallocs;
569         gulong max_code_size_ratio;
570         gulong biggest_method_size;
571         gulong allocated_code_size;
572         gulong inlineable_methods;
573         gulong inlined_methods;
574         gulong basic_blocks;
575         gulong max_basic_blocks;
576         MonoMethod *max_ratio_method;
577         MonoMethod *biggest_method;
578         gboolean enabled;
579 } MonoJitStats;
580
581 extern MonoJitStats mono_jit_stats;
582
583 /* values for MonoInst.ssa_op */
584 enum {
585         MONO_SSA_NOP,
586         MONO_SSA_LOAD,
587         MONO_SSA_STORE,
588         MONO_SSA_MAYBE_LOAD,
589         MONO_SSA_MAYBE_STORE
590 };
591
592 #define OP_CEQ    (256+CEE_CEQ)
593 #define OP_CLT    (256+CEE_CLT)
594 #define OP_CLT_UN (256+CEE_CLT_UN)
595 #define OP_CGT    (256+CEE_CGT)
596 #define OP_CGT_UN (256+CEE_CGT_UN)
597 #define OP_LOCALLOC (256+CEE_LOCALLOC)
598
599 /* opcodes: value assigned after all the CIL opcodes */
600 #ifdef MINI_OP
601 #undef MINI_OP
602 #endif
603 #define MINI_OP(a,b) a,
604 enum {
605         OP_START = MONO_CEE_LAST,
606 #include "mini-ops.h"
607         OP_LAST
608 };
609 #undef MINI_OP
610
611 #if SIZEOF_VOID_P == 8
612 #define OP_PADD OP_LADD
613 #define OP_PNEG OP_LNEG
614 #define OP_PCONV_TO_U2 OP_LCONV_TO_U2
615 #define OP_PCONV_TO_OVF_I1_UN OP_LCONV_TO_OVF_I1_UN
616 #define OP_PCONV_TO_OVF_I1 OP_LCONV_TO_OVF_I1
617 #define OP_PCEQ CEE_CEQ
618 #define OP_STOREP_MEMBASE_REG OP_STOREI8_MEMBASE_REG
619 #define OP_STOREP_MEMBASE_IMM OP_STOREI8_MEMBASE_IMM
620 #else
621 #define OP_PADD CEE_ADD
622 #define OP_PNEG CEE_NEG
623 #define OP_PCONV_TO_U2 CEE_CONV_U2
624 #define OP_PCONV_TO_OVF_I1_UN CEE_CONV_OVF_I1_UN
625 #define OP_PCONV_TO_OVF_I1 CEE_CONV_OVF_I1
626 #define OP_PCEQ CEE_CEQ
627 #define OP_STOREP_MEMBASE_REG OP_STOREI4_MEMBASE_REG
628 #define OP_STOREP_MEMBASE_IMM OP_STOREI4_MEMBASE_IMM
629 #endif
630
631 typedef enum {
632         STACK_INV,
633         STACK_I4,
634         STACK_I8,
635         STACK_PTR,
636         STACK_R8,
637         STACK_MP,
638         STACK_OBJ,
639         STACK_VTYPE,
640         STACK_MAX
641 } MonoStackType;
642
643 typedef struct {
644         union {
645                 double   r8;
646                 gint32   i4;
647                 gint64   i8;
648                 gpointer p;
649                 MonoClass *klass;
650         } data;
651         int type;
652 } StackSlot;
653
654 enum {
655         MONO_COMP_DOM = 1,
656         MONO_COMP_IDOM = 2,
657         MONO_COMP_DFRONTIER = 4,
658         MONO_COMP_DOM_REV = 8,
659         MONO_COMP_LIVENESS = 16,
660         MONO_COMP_SSA = 32,
661         MONO_COMP_SSA_DEF_USE = 64,
662         MONO_COMP_REACHABILITY = 128,
663         MONO_COMP_LOOPS = 256
664 };
665
666 typedef enum {
667         MONO_GRAPH_CFG = 1,
668         MONO_GRAPH_DTREE = 2,
669         MONO_GRAPH_CFG_CODE = 4,
670         MONO_GRAPH_CFG_SSA = 8,
671         MONO_GRAPH_CFG_OPTCODE = 16
672 } MonoGraphOptions;
673
674 typedef struct {
675         guint16 size;
676         guint16 offset;
677         guint8  pad;
678 } MonoJitArgumentInfo;
679
680 typedef void (*MonoInstFunc) (MonoInst *tree, gpointer data);
681
682 /* main function */
683 int         mono_main                      (int argc, char* argv[]);
684 void        mono_set_defaults              (int verbose_level, guint32 opts);
685 MonoDomain* mini_init                      (const char *filename);
686 void        mini_cleanup                   (MonoDomain *domain);
687
688 /* helper methods */
689 MonoJumpInfoToken * mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token);
690 MonoInst* mono_find_spvar_for_region        (MonoCompile *cfg, int region);
691 void      mono_precompile_assemblies        (void);
692 int       mono_parse_default_optimizations  (const char* p);
693 void      mono_bblock_add_inst              (MonoBasicBlock *bb, MonoInst *inst);
694 void      mono_constant_fold                (MonoCompile *cfg);
695 void      mono_constant_fold_inst           (MonoInst *inst, gpointer data);
696 int       mono_is_power_of_two              (guint32 val);
697 void      mono_cprop_local                  (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size);
698 MonoInst* mono_compile_create_var           (MonoCompile *cfg, MonoType *type, int opcode);
699 void      mono_blockset_print               (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom);
700 void      mono_print_tree                   (MonoInst *tree);
701 void      mono_print_tree_nl                (MonoInst *tree);
702 void      mono_print_code                   (MonoCompile *cfg);
703 void      mono_print_method_from_ip         (void *ip);
704 void      mono_select_instructions          (MonoCompile *cfg);
705 const char* mono_inst_name                  (int op);
706 void      mono_inst_foreach                 (MonoInst *tree, MonoInstFunc func, gpointer data);
707 void      mono_disassemble_code             (guint8 *code, int size, char *id);
708 guint     mono_type_to_ldind                (MonoType *t);
709 guint     mono_type_to_stind                (MonoType *t);
710 void      mono_add_patch_info               (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target);
711 void      mono_remove_patch_info            (MonoCompile *cfg, int ip);
712 gpointer  mono_resolve_patch_target         (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors);
713 MonoLMF** mono_get_lmf_addr                 (void);
714 GList    *mono_varlist_insert_sorted        (MonoCompile *cfg, GList *list, MonoMethodVar *mv, gboolean sort_end);
715 GList    *mono_varlist_sort                 (MonoCompile *cfg, GList *list, int sort_type);
716 void      mono_analyze_liveness             (MonoCompile *cfg);
717 void      mono_linear_scan                  (MonoCompile *cfg, GList *vars, GList *regs, regmask_t *used_mask);
718 void      mono_create_jump_table            (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks);
719 int       mono_compile_assembly             (MonoAssembly *ass, guint32 opts, const char *aot_options);
720 MonoCompile *mini_method_compile            (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts);
721 void      mono_destroy_compile              (MonoCompile *cfg);
722 void      mono_aot_init                     (void);
723 MonoJitInfo*  mono_aot_get_method           (MonoDomain *domain,
724                                                                                          MonoMethod *method);
725 gboolean  mono_method_blittable             (MonoMethod *method);
726 gboolean  mono_method_same_domain           (MonoJitInfo *caller, MonoJitInfo *callee);
727 void      mono_register_opcode_emulation    (int opcode, const char* name, MonoMethodSignature *sig, gpointer func, gboolean no_throw);
728 void      mono_arch_register_lowlevel_calls (void);
729 void      mono_draw_graph                   (MonoCompile *cfg, MonoGraphOptions draw_options);
730 void      mono_add_varcopy_to_end           (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest);
731
732 int               mono_find_method_opcode      (MonoMethod *method);
733 MonoJitICallInfo *mono_find_jit_icall_by_name  (const char *name);
734 MonoJitICallInfo *mono_find_jit_icall_by_addr  (gconstpointer addr);
735 MonoJitICallInfo *mono_register_jit_icall      (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save);
736 gconstpointer     mono_icall_get_wrapper       (MonoJitICallInfo* callinfo);
737
738 gpointer          mono_create_jump_trampoline (MonoDomain *domain, 
739                                                                                            MonoMethod *method, 
740                                                                                            gboolean add_sync_wrapper);
741 gpointer          mono_create_class_init_trampoline (MonoVTable *vtable);
742 gpointer          mono_create_jit_trampoline (MonoMethod *method);
743 MonoVTable*       mono_find_class_init_trampoline_by_addr (gconstpointer addr);
744 gboolean          mono_running_on_valgrind (void);
745
746 /* methods that must be provided by the arch-specific port */
747 void      mono_arch_cpu_init                    (void);
748 guint32   mono_arch_cpu_optimizazions           (guint32 *exclude_mask);
749 void      mono_arch_instrument_mem_needs        (MonoMethod *method, int *stack, int *code);
750 void     *mono_arch_instrument_prolog           (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments);
751 void     *mono_arch_instrument_epilog           (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments);
752 MonoCallInst *mono_arch_call_opcode             (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual);
753 gint      mono_arch_get_opcode_for_method       (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args);
754 void      mono_codegen                          (MonoCompile *cfg);
755 const char *mono_arch_regname                   (int reg);
756 gpointer  mono_arch_get_throw_exception         (void);
757 gpointer  mono_arch_get_rethrow_exception       (void);
758 gpointer  mono_arch_get_throw_exception_by_name (void);
759 gpointer  mono_arch_create_jit_trampoline       (MonoMethod *method);
760 MonoJitInfo *mono_arch_create_jump_trampoline      (MonoMethod *method);
761 gpointer  mono_arch_create_class_init_trampoline(MonoVTable *vtable);
762 GList    *mono_arch_get_allocatable_int_vars    (MonoCompile *cfg);
763 GList    *mono_arch_get_global_int_regs         (MonoCompile *cfg);
764 guint32   mono_arch_regalloc_cost               (MonoCompile *cfg, MonoMethodVar *vmv);
765 void      mono_arch_patch_code                  (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors);
766 void      mono_arch_flush_icache                (guint8 *code, gint size);
767 int       mono_arch_max_epilog_size             (MonoCompile *cfg);
768 guint8   *mono_arch_emit_prolog                 (MonoCompile *cfg);
769 void      mono_arch_emit_epilog                 (MonoCompile *cfg);
770 void      mono_arch_local_regalloc              (MonoCompile *cfg, MonoBasicBlock *bb);
771 void      mono_arch_output_basic_block          (MonoCompile *cfg, MonoBasicBlock *bb);
772 gboolean  mono_arch_has_unwind_info             (gconstpointer addr);
773 void      mono_arch_setup_jit_tls_data          (MonoJitTlsData *tls);
774 void      mono_arch_free_jit_tls_data           (MonoJitTlsData *tls);
775 void      mono_arch_emit_this_vret_args         (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg);
776 void      mono_arch_allocate_vars               (MonoCompile *m);
777 int       mono_arch_get_argument_info           (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info);
778 gboolean  mono_arch_print_tree                  (MonoInst *tree, int arity);
779 MonoJitInfo *mono_arch_find_jit_info            (MonoDomain *domain, 
780                                                  MonoJitTlsData *jit_tls, 
781                                                  MonoJitInfo *res, 
782                                                  MonoJitInfo *prev_ji, 
783                                                  MonoContext *ctx, 
784                                                  MonoContext *new_ctx, 
785                                                  char **trace, 
786                                                  MonoLMF **lmf, 
787                                                  int *native_offset,
788                                                  gboolean *managed);
789 gpointer mono_arch_get_call_filter              (void);
790 gpointer mono_arch_get_restore_context          (void);
791 gboolean mono_arch_handle_exception             (void *sigctx, gpointer obj, gboolean test_only);
792 gpointer mono_arch_ip_from_context              (void *sigctx);
793 void     mono_arch_flush_register_windows       (void);
794 gboolean mono_arch_is_inst_imm                  (gint64 imm);
795 MonoInst* mono_arch_get_domain_intrinsic        (MonoCompile* cfg);
796 MonoInst* mono_arch_get_thread_intrinsic        (MonoCompile* cfg);
797 gboolean mono_arch_is_int_overflow              (void *sigctx, void *info);
798 void     mono_arch_invalidate_method            (MonoJitInfo *ji, void *func, gpointer func_arg);
799
800 /* Exception handling */
801 gboolean mono_handle_exception                  (MonoContext *ctx, gpointer obj,
802                                                  gpointer original_ip, gboolean test_only);
803 void      mono_jit_walk_stack                   (MonoStackWalk func, gboolean do_il_offset, gpointer user_data);
804 MonoArray *ves_icall_get_trace                  (MonoException *exc, gint32 skip, MonoBoolean need_file_info);
805 MonoBoolean ves_icall_get_frame_info            (gint32 skip, MonoBoolean need_file_info, 
806                                                  MonoReflectionMethod **method, 
807                                                  gint32 *iloffset, gint32 *native_offset,
808                                                  MonoString **file, gint32 *line, gint32 *column);
809
810 /* Dominator/SSA methods */
811 void        mono_compile_dominator_info         (MonoCompile *cfg, int dom_flags);
812 void        mono_compute_natural_loops          (MonoCompile *cfg);
813 MonoBitSet* mono_compile_iterated_dfrontier     (MonoCompile *cfg, MonoBitSet *set);
814 void        mono_ssa_compute                    (MonoCompile *cfg);
815 void        mono_ssa_remove                     (MonoCompile *cfg);
816 void        mono_ssa_cprop                      (MonoCompile *cfg);
817 void        mono_ssa_deadce                     (MonoCompile *cfg);
818 void        mono_ssa_strength_reduction         (MonoCompile *cfg);
819 void        mono_free_loop_info                 (MonoCompile *cfg);
820
821 /* debugging support */
822 void      mono_debug_init_method                (MonoCompile *cfg, MonoBasicBlock *start_block,
823                                                  guint32 breakpoint_id);
824 void      mono_debug_open_method                (MonoCompile *cfg);
825 void      mono_debug_close_method               (MonoCompile *cfg);
826 void      mono_debug_open_block                 (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address);
827 void      mono_debug_record_line_number         (MonoCompile *cfg, MonoInst *ins, guint32 address);
828 void      mono_debug_serialize_debug_info       (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len);
829 void      mono_debug_add_aot_method             (MonoDomain *domain,
830                                                                                                 MonoMethod *method, guint8 *code_start, 
831                                                                                                 guint8 *debug_info, guint32 debug_info_len);
832 void      mono_debug_add_icall_wrapper          (MonoMethod *method, MonoJitICallInfo* info);
833
834
835 /* Tracing */
836 MonoTraceSpec *mono_trace_parse_options         (char *options);
837 void           mono_trace_set_assembly          (MonoAssembly *assembly);
838 gboolean       mono_trace_eval                  (MonoMethod *method);
839
840 extern void
841 mono_perform_abc_removal (MonoCompile *cfg);
842
843 #endif /* __MONO_MINI_H__ */