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