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