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