1 #ifndef __MONO_MINI_H__
2 #define __MONO_MINI_H__
6 #include <mono/metadata/loader.h>
7 #include <mono/metadata/mempool.h>
8 #include <mono/utils/monobitset.h>
9 #include <mono/metadata/class.h>
10 #include <mono/metadata/object.h>
11 #include <mono/metadata/opcodes.h>
12 #include <mono/metadata/tabledefs.h>
13 #include <mono/metadata/profiler-private.h>
16 /* fixme: configure should set this */
17 #define SIZEOF_VOID_P 4
19 #define MONO_USE_AOT_COMPILER
22 #define mono_bitset_test_fast(set,n) (((guint32*)set)[2+(n)/32] & (1 << ((n) % 32)))
24 #define mono_bitset_test_fast(set,n) mono_bitset_test(set,n)
28 #define mono_bitset_foreach_bit(set,b,n) \
29 for (b = 0; b < n; b++)\
30 if (mono_bitset_test_fast(set,b))
31 #define mono_bitset_foreach_bit_rev(set,b,n) \
32 for (b = n - 1; b >= 0; b--)\
33 if (mono_bitset_test_fast(set,b))
35 #define mono_bitset_foreach_bit(set,b,n) \
36 for (b = mono_bitset_find_first (set, -1); b < n && b >= 0; b = mono_bitset_find_first (set, b))
37 #define mono_bitset_foreach_bit_rev(set,b,n) \
38 for (b = mono_bitset_find_last (set, n - 1); b >= 0; b = b ? mono_bitset_find_last (set, b) : -1)
43 * Pull the list of opcodes
45 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
49 #include "mono/cil/opcode.def"
54 #define MONO_VARINFO(cfg,varnum) ((cfg)->vars [varnum])
56 #define MONO_INST_NEW(cfg,dest,op) do { \
57 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
58 (dest)->opcode = (op); \
61 #define MONO_INST_NEW_CALL(cfg,dest,op) do { \
62 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoCallInst)); \
63 (dest)->inst.opcode = (op); \
66 #define MONO_ADD_INS(b,inst) do { \
67 if ((b)->last_ins) { \
68 (b)->last_ins->next = (inst); \
69 (b)->last_ins = (inst); \
71 (b)->code = (b)->last_ins = (inst); \
75 typedef struct MonoInst MonoInst;
76 typedef struct MonoCallInst MonoCallInst;
77 typedef struct MonoEdge MonoEdge;
78 typedef struct MonoMethodVar MonoMethodVar;
79 typedef struct MonoBasicBlock MonoBasicBlock;
80 typedef struct MonoLMF MonoLMF;
81 typedef struct MonoSpillInfo MonoSpillInfo;
83 extern guint32 mono_jit_tls_id;
84 extern gboolean mono_jit_trace_calls;
85 extern gboolean mono_break_on_exc;
86 extern int mono_exc_esp_offset;
87 extern gboolean mono_compile_aot;
89 extern CRITICAL_SECTION *metadata_section;
97 struct MonoSpillInfo {
103 * The IR-level basic block.
105 * A basic block can have multiple exits just fine, as long as the point of
106 * 'departure' is the last instruction in the basic block. Extended basic
107 * blocks, on the other hand, may have instructions that leave the block
108 * midstream. The important thing is that they cannot be _entered_
109 * midstream, ie, execution of a basic block (or extened bb) always start
110 * at the beginning of the block, never in the middle.
112 struct MonoBasicBlock {
115 /* Points to the start of the CIL code that initiated this BB */
116 unsigned char* cil_code;
118 /* Length of the CIL block */
121 /* The address of the generated code, used for fixups */
127 /* unique block number identification */
130 /* Visited and reachable flags */
133 /* Basic blocks: incoming and outgoing counts and pointers */
134 gint16 out_count, in_count;
135 MonoBasicBlock **in_bb;
136 MonoBasicBlock **out_bb;
138 /* the next basic block in the order it appears in IL */
139 MonoBasicBlock *next_bb;
142 * Before instruction selection it is the first tree in the
143 * forest and the first item in the list of trees. After
144 * instruction selection it is the first instruction and the
145 * first item in the list of instructions.
150 * SSA and loop based flags
152 MonoBitSet *dominators;
153 MonoBitSet *dfrontier;
154 MonoBasicBlock *idom;
156 /* fast dominator algorithm */
157 MonoBasicBlock *df_parent, *ancestor, *child, *label;
159 int size, sdom, idomn;
161 /* loop nesting and recognition */
165 /* use for liveness analysis */
167 MonoBitSet *kill_set;
168 MonoBitSet *live_in_set;
169 MonoBitSet *live_out_set;
171 /* fields to deal with non-empty stack slots at bb boundary */
172 guint16 out_scount, in_scount;
173 MonoInst **out_stack;
176 /* we use that to prevent merging of bblock covered by different clauses*/
180 /* The current symbolic register number, used in local register allocation. */
181 guint32 max_ireg, max_freg;
186 #define BB_REACHABLE 2
196 MonoMethodSignature *signature;
197 MonoBasicBlock **many_blocks;
198 MonoBasicBlock *target_block;
208 guint8 type; /* stack type */
212 /* used by the register allocator */
213 gint32 dreg, sreg1, sreg2, unused;
217 const unsigned char* cil_code; /* for debugging and bblock splitting */
220 struct MonoCallInst {
222 MonoMethodSignature *signature;
234 * Note: some of the values overlap, because they can't appear
235 * in the same MonoInst.
238 MONO_INST_HAS_METHOD = 1,
239 /* temp local created by a DUP: used only within a BB */
240 MONO_INST_IS_TEMP = 1,
241 MONO_INST_INIT = 1, /* in localloc */
242 MONO_INST_IS_DEAD = 2,
243 MONO_INST_TAILCALL = 4,
244 MONO_INST_VOLATILE = 4,
245 MONO_INST_BRLABEL = 4,
246 MONO_INST_UNALIGNED = 8,
247 /* the address of the variable has been taken */
248 MONO_INST_INDIRECT = 16
251 #define inst_c0 data.op[0].const_val
252 #define inst_c1 data.op[1].const_val
253 #define inst_i0 data.op[0].src
254 #define inst_i1 data.op[1].src
255 #define inst_p0 data.op[0].p
256 #define inst_p1 data.op[1].p
257 #define inst_l data.i8const
258 #define inst_r data.r8const
259 #define inst_left data.op[0].src
260 #define inst_right data.op[1].src
262 #define inst_newa_len data.op[0].src
263 #define inst_newa_class data.op[1].klass
265 #define inst_switch data.op[0].switch_blocks
266 #define inst_var data.op[0].var
267 #define inst_vtype data.op[1].vtype
268 /* in branch instructions */
269 #define inst_many_bb data.op[1].many_blocks
270 #define inst_target_bb data.op[0].target_block
271 #define inst_true_bb data.op[1].many_blocks[0]
272 #define inst_false_bb data.op[1].many_blocks[1]
274 #define inst_basereg sreg1
275 #define inst_indexreg sreg2
276 #define inst_destbasereg dreg
277 #define inst_offset data.op[0].const_val
278 #define inst_imm data.op[1].const_val
280 #define inst_phi_args data.op[1].phi_args
282 /* instruction description for use in regalloc/scheduling */
298 guint16 tid; /* tree number */
299 guint16 bid; /* block number */
305 MonoPosition first_use, last_use;
309 * Additional information about a variable
311 struct MonoMethodVar {
312 guint idx; /* inside cfg->varinfo, cfg->vars */
314 MonoBitSet *dfrontier;
315 MonoLiveRange range; /* generated by liveness analysis */
316 int reg; /* != -1 if allocated into a register */
318 MonoBitSet *def_in; /* used by SSA */
319 MonoInst *def; /* used by SSA */
320 MonoBasicBlock *def_bb; /* used by SSA */
321 GList *uses; /* used by SSA */
322 char cpstate; /* used by SSA conditional constant propagation */
326 gpointer end_of_stack;
328 void (*abort_func) (MonoObject *object);
334 MONO_PATCH_INFO_LABEL,
335 MONO_PATCH_INFO_METHOD,
336 MONO_PATCH_INFO_METHOD_JUMP,
337 MONO_PATCH_INFO_METHODCONST,
338 MONO_PATCH_INFO_INTERNAL_METHOD,
339 MONO_PATCH_INFO_SWITCH,
341 MONO_PATCH_INFO_CLASS,
342 MONO_PATCH_INFO_IMAGE,
343 MONO_PATCH_INFO_FIELD,
349 typedef struct MonoJumpInfo MonoJumpInfo;
350 struct MonoJumpInfo {
358 MonoJumpInfoType type;
360 gconstpointer target;
363 MonoBasicBlock **table;
367 MonoClassField *field;
372 int table_size; /* use by switch */
375 /* optimization flags: keep up to date with the name array in mini.c */
377 MONO_OPT_PEEPHOLE = 1 << 0,
378 MONO_OPT_BRANCH = 1 << 1,
379 MONO_OPT_INLINE = 1 << 2,
380 MONO_OPT_CFOLD = 1 << 3,
381 MONO_OPT_CONSPROP = 1 << 4,
382 MONO_OPT_COPYPROP = 1 << 5,
383 MONO_OPT_DEADCE = 1 << 6,
384 MONO_OPT_LINEARS = 1 << 7,
385 MONO_OPT_CMOV = 1 << 8,
386 MONO_OPT_SHARED = 1 << 9,
387 MONO_OPT_SCHED = 1 << 10,
388 MONO_OPT_INTRINS = 1 << 11,
389 MONO_OPT_TAILC = 1 << 12,
390 MONO_OPT_LOOP = 1 << 13,
391 MONO_OPT_FCMOV = 1 << 14,
392 MONO_OPT_LEAF = 1 << 15
395 #define MONO_REGION_FINALLY 16
396 #define MONO_REGION_CATCH 32
397 #define MONO_REGION_FAULT 64
398 #define MONO_REGION_FILTER 128
401 * Control Flow Graph and compilation unit information
405 MonoMemPool *mempool;
407 MonoMethodVar **vars;
409 MonoBasicBlock *bb_entry;
410 MonoBasicBlock *bb_exit;
411 MonoBasicBlock *bb_init;
412 MonoBasicBlock **bblocks;
414 MonoMemPool *state_pool; /* used by instruction selection */
415 MonoBasicBlock *cbb; /* used by instruction selection */
416 MonoInst *prev_ins; /* in decompose */
417 MonoJumpInfo *patch_info;
420 guint num_varinfo; /* used items in varinfo */
421 guint varinfo_count; /* total storage in varinfo */
424 MonoSpillInfo *spill_info;
426 // unsigned char *cil_code;
428 MonoInst *exvar; /* the exception object passed to catch/filter blocks */
429 MonoInst *domainvar; /* a cache for the current domain */
430 MonoInst *spvar; /* a place to store the stack pointer (used by handlers) */
432 GList *ldstr_list; /* used by AOT */
436 unsigned char *native_code;
441 guint32 used_int_regs;
443 guint32 prof_options;
446 guint32 verbose_level;
451 gboolean disable_aot;
452 gboolean disable_ssa;
455 MonoProfileCoverageInfo *coverage_info;
459 MONO_CFG_HAS_ALLOCA = 1 << 0,
460 MONO_CFG_HAS_CALLS = 1 << 1
464 gulong methods_compiled;
466 gulong methods_lookups;
467 gulong method_trampolines;
469 gulong analyze_stack_repeat;
470 gulong cil_code_size;
471 gulong native_code_size;
472 gulong code_reallocs;
473 gulong max_code_size_ratio;
474 gulong biggest_method_size;
475 gulong allocated_code_size;
476 gulong inlineable_methods;
477 gulong inlined_methods;
479 gulong max_basic_blocks;
480 MonoMethod *max_ratio_method;
481 MonoMethod *biggest_method;
485 extern MonoJitStats mono_jit_stats;
487 /* values for MonoInst.ssa_op */
496 #define OP_CEQ (256+CEE_CEQ)
497 #define OP_CLT (256+CEE_CLT)
498 #define OP_CLT_UN (256+CEE_CLT_UN)
499 #define OP_CGT (256+CEE_CGT)
500 #define OP_CGT_UN (256+CEE_CGT_UN)
501 #define OP_LOCALLOC (256+CEE_LOCALLOC)
503 /* opcodes: value assigned after all the CIL opcodes */
507 #define MINI_OP(a,b) a,
509 OP_START = MONO_CEE_LAST,
510 #include "mini-ops.h"
515 /* make this depend on 32bit platform (use OP_LADD otherwise) */
516 #define OP_PADD CEE_ADD
517 #define OP_PNEG CEE_NEG
518 #define OP_PCONV_TO_U2 CEE_CONV_U2
519 #define OP_PCONV_TO_OVF_I1_UN CEE_CONV_OVF_I1_UN
520 #define OP_PCONV_TO_OVF_I1 CEE_CONV_OVF_I1
521 #define OP_PCEQ CEE_CEQ
549 MONO_COMP_DFRONTIER = 4,
550 MONO_COMP_DOM_REV = 8,
551 MONO_COMP_LIVENESS = 16,
553 MONO_COMP_SSA_DEF_USE = 64,
554 MONO_COMP_REACHABILITY = 128,
555 MONO_COMP_LOOPS = 256
560 MONO_GRAPH_DTREE = 2,
561 MONO_GRAPH_CFG_CODE = 4,
562 MONO_GRAPH_CFG_SSA = 8,
563 MONO_GRAPH_CFG_OPTCODE = 16
569 gconstpointer wrapper;
570 MonoMethodSignature *sig;
573 typedef void (*MonoInstFunc) (MonoInst *tree, gpointer data);
576 int mono_main (int argc, char* argv[]);
577 void mono_set_defaults (int verbose_level, guint32 opts);
578 MonoDomain* mini_init (const char *filename);
579 void mini_cleanup (MonoDomain *domain);
582 int mono_parse_default_optimizations (const char* p);
583 void mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst);
584 void mono_constant_fold (MonoCompile *cfg);
585 void mono_constant_fold_inst (MonoInst *inst, gpointer data);
586 void mono_cprop_local (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size);
587 MonoInst* mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode);
588 void mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom);
589 void mono_print_tree (MonoInst *tree);
590 int mono_spillvar_offset (MonoCompile *cfg, int spillvar);
591 void mono_select_instructions (MonoCompile *cfg);
592 const char* mono_inst_name (int op);
593 void mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data);
594 void mono_disassemble_code (guint8 *code, int size, char *id);
595 guint mono_type_to_ldind (MonoType *t);
596 guint mono_type_to_stind (MonoType *t);
597 void mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target);
598 void mono_remove_patch_info (MonoCompile *cfg, int ip);
599 gpointer mono_get_lmf_addr (void);
600 GList *mono_varlist_insert_sorted (MonoCompile *cfg, GList *list, MonoMethodVar *mv, gboolean sort_end);
601 void mono_analyze_liveness (MonoCompile *cfg);
602 void mono_linear_scan (MonoCompile *cfg, GList *vars, GList *regs, guint32 *used_mask);
603 void mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks);
604 int mono_compile_assembly (MonoAssembly *ass, guint32 opts);
605 MonoCompile *mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts);
606 void mono_destroy_compile (MonoCompile *cfg);
607 gpointer mono_aot_get_method (MonoMethod *method);
608 gboolean mono_method_blittable (MonoMethod *method);
609 void mono_register_opcode_emulation (int opcode, const char* name, MonoMethodSignature *sig, gpointer func, gboolean no_throw);
610 void mono_arch_register_lowlevel_calls (void);
611 void mono_draw_graph (MonoCompile *cfg, MonoGraphOptions draw_options);
612 void mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest);
614 int mono_find_method_opcode (MonoMethod *method);
615 MonoJitICallInfo *mono_find_jit_icall_by_name (const char *name);
616 MonoJitICallInfo *mono_find_jit_icall_by_addr (gconstpointer addr);
617 MonoJitICallInfo *mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save);
618 gconstpointer mono_icall_get_wrapper (MonoJitICallInfo* callinfo);
620 /* methods that must be provided by the arch-specific port */
621 void mono_arch_cpu_init (void);
622 guint32 mono_arch_cpu_optimizazions (guint32 *exclude_mask);
623 void mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code);
624 void *mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments);
625 void *mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments);
626 MonoCallInst *mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual);
627 void mono_codegen (MonoCompile *cfg);
628 const char *mono_arch_regname (int reg);
629 gpointer mono_arch_get_throw_exception (void);
630 gpointer mono_arch_get_throw_exception_by_name (void);
631 gpointer mono_arch_create_jit_trampoline (MonoMethod *method);
632 gpointer mono_arch_create_jump_trampoline (MonoMethod *method);
633 GList *mono_arch_get_allocatable_int_vars (MonoCompile *cfg);
634 GList *mono_arch_get_global_int_regs (MonoCompile *cfg);
635 void mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji);
636 void mono_arch_flush_icache (guint8 *code, gint size);
637 int mono_arch_max_epilog_size (MonoCompile *cfg);
638 guint8 *mono_arch_emit_prolog (MonoCompile *cfg);
639 void mono_arch_emit_epilog (MonoCompile *cfg);
640 void mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb);
641 void mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb);
642 gboolean mono_arch_has_unwind_info (gconstpointer addr);
643 void mono_arch_allocate_vars (MonoCompile *m);
644 void mono_jit_walk_stack (MonoStackWalk func, gpointer user_data);
645 MonoArray *ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info);
646 MonoBoolean ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
647 MonoReflectionMethod **method,
648 gint32 *iloffset, gint32 *native_offset,
649 MonoString **file, gint32 *line, gint32 *column);
651 /* Dominator/SSA methods */
652 void mono_compile_dominator_info (MonoCompile *cfg, int dom_flags);
653 void mono_compute_natural_loops (MonoCompile *cfg);
654 MonoBitSet* mono_compile_iterated_dfrontier (MonoCompile *cfg, MonoBitSet *set);
655 void mono_ssa_compute (MonoCompile *cfg);
656 void mono_ssa_remove (MonoCompile *cfg);
657 void mono_ssa_cprop (MonoCompile *cfg);
658 void mono_ssa_deadce (MonoCompile *cfg);
659 void mono_ssa_strength_reduction (MonoCompile *cfg);
661 /* debugging support */
662 void mono_debug_init_method (MonoCompile *cfg, MonoBasicBlock *start_block,
663 guint32 breakpoint_id);
664 void mono_debug_open_method (MonoCompile *cfg);
665 void mono_debug_close_method (MonoCompile *cfg);
666 void mono_debug_open_block (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address);
667 void mono_debug_record_line_number (MonoCompile *cfg, MonoInst *ins, guint32 address);
669 #endif /* __MONO_MINI_H__ */