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