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