1 #ifndef __MONO_MINI_H__
2 #define __MONO_MINI_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/profiler-private.h>
16 #include "mini-arch.h"
19 #define MONO_USE_AOT_COMPILER
21 /* for 32 bit systems */
22 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
23 #define MINI_LS_WORD_OFFSET 0
24 #define MINI_MS_WORD_OFFSET 4
25 #define inst_ls_word data.op[0].const_val
26 #define inst_ms_word data.op[1].const_val
28 #define MINI_LS_WORD_OFFSET 4
29 #define MINI_MS_WORD_OFFSET 0
30 #define inst_ls_word data.op[1].const_val
31 #define inst_ms_word data.op[0].const_val
34 /* Version number of the AOT file format */
35 #define MONO_AOT_FILE_VERSION "7"
38 #define mono_bitset_test_fast(set,n) (((guint32*)set)[2+(n)/32] & (1 << ((n) % 32)))
40 #define mono_bitset_test_fast(set,n) mono_bitset_test(set,n)
44 #define mono_bitset_foreach_bit(set,b,n) \
45 for (b = 0; b < n; b++)\
46 if (mono_bitset_test_fast(set,b))
47 #define mono_bitset_foreach_bit_rev(set,b,n) \
48 for (b = n - 1; b >= 0; b--)\
49 if (mono_bitset_test_fast(set,b))
51 #define mono_bitset_foreach_bit(set,b,n) \
52 for (b = mono_bitset_find_first (set, -1); b < n && b >= 0; b = mono_bitset_find_first (set, b))
53 #define mono_bitset_foreach_bit_rev(set,b,n) \
54 for (b = mono_bitset_find_last (set, n - 1); b >= 0; b = b ? mono_bitset_find_last (set, b) : -1)
59 * Pull the list of opcodes
61 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
65 #include "mono/cil/opcode.def"
70 #define MONO_VARINFO(cfg,varnum) ((cfg)->vars [varnum])
72 #define MONO_INST_NEW(cfg,dest,op) do { \
73 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
74 (dest)->opcode = (op); \
77 #define MONO_INST_NEW_CALL(cfg,dest,op) do { \
78 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoCallInst)); \
79 (dest)->inst.opcode = (op); \
82 #define MONO_ADD_INS(b,inst) do { \
83 if ((b)->last_ins) { \
84 (b)->last_ins->next = (inst); \
85 (b)->last_ins = (inst); \
87 (b)->code = (b)->last_ins = (inst); \
91 typedef struct MonoInst MonoInst;
92 typedef struct MonoCallInst MonoCallInst;
93 typedef struct MonoEdge MonoEdge;
94 typedef struct MonoMethodVar MonoMethodVar;
95 typedef struct MonoBasicBlock MonoBasicBlock;
96 typedef struct MonoLMF MonoLMF;
97 typedef struct MonoSpillInfo MonoSpillInfo;
98 typedef struct MonoTraceSpec MonoTraceSpec;
100 extern guint32 mono_jit_tls_id;
101 extern MonoTraceSpec *mono_jit_trace_calls;
102 extern gboolean mono_break_on_exc;
103 extern int mono_exc_esp_offset;
104 extern gboolean mono_compile_aot;
112 struct MonoSpillInfo {
118 * The IR-level basic block.
120 * A basic block can have multiple exits just fine, as long as the point of
121 * 'departure' is the last instruction in the basic block. Extended basic
122 * blocks, on the other hand, may have instructions that leave the block
123 * midstream. The important thing is that they cannot be _entered_
124 * midstream, ie, execution of a basic block (or extened bb) always start
125 * at the beginning of the block, never in the middle.
127 struct MonoBasicBlock {
130 /* Points to the start of the CIL code that initiated this BB */
131 unsigned char* cil_code;
133 /* Length of the CIL block */
136 /* The address of the generated code, used for fixups */
142 /* unique block number identification */
145 /* Visited and reachable flags */
148 /* Basic blocks: incoming and outgoing counts and pointers */
149 gint16 out_count, in_count;
150 MonoBasicBlock **in_bb;
151 MonoBasicBlock **out_bb;
153 /* the next basic block in the order it appears in IL */
154 MonoBasicBlock *next_bb;
157 * Before instruction selection it is the first tree in the
158 * forest and the first item in the list of trees. After
159 * instruction selection it is the first instruction and the
160 * first item in the list of instructions.
165 * SSA and loop based flags
167 MonoBitSet *dominators;
168 MonoBitSet *dfrontier;
169 MonoBasicBlock *idom;
171 /* fast dominator algorithm */
172 MonoBasicBlock *df_parent, *ancestor, *child, *label;
174 int size, sdom, idomn;
176 /* loop nesting and recognition */
180 /* use for liveness analysis */
182 MonoBitSet *kill_set;
183 MonoBitSet *live_in_set;
184 MonoBitSet *live_out_set;
186 /* fields to deal with non-empty stack slots at bb boundary */
187 guint16 out_scount, in_scount;
188 MonoInst **out_stack;
191 /* we use that to prevent merging of bblock covered by different clauses*/
195 /* The current symbolic register number, used in local register allocation. */
196 guint32 max_ireg, max_freg;
201 #define BB_REACHABLE 2
211 MonoMethodSignature *signature;
212 MonoBasicBlock **many_blocks;
213 MonoBasicBlock *target_block;
223 guint8 type; /* stack type */
227 /* used by the register allocator */
228 gint32 dreg, sreg1, sreg2, unused;
232 const unsigned char* cil_code; /* for debugging and bblock splitting */
235 struct MonoCallInst {
237 MonoMethodSignature *signature;
243 regmask_t used_iregs;
244 regmask_t used_fregs;
249 * Note: some of the values overlap, because they can't appear
250 * in the same MonoInst.
253 MONO_INST_HAS_METHOD = 1,
254 /* temp local created by a DUP: used only within a BB */
255 MONO_INST_IS_TEMP = 1,
256 MONO_INST_INIT = 1, /* in localloc */
257 MONO_INST_IS_DEAD = 2,
258 MONO_INST_TAILCALL = 4,
259 MONO_INST_VOLATILE = 4,
260 MONO_INST_BRLABEL = 4,
261 MONO_INST_NOTYPECHECK = 4,
262 MONO_INST_UNALIGNED = 8,
263 /* the address of the variable has been taken */
264 MONO_INST_INDIRECT = 16,
265 MONO_INST_NORANGECHECK = 16
268 #define inst_c0 data.op[0].const_val
269 #define inst_c1 data.op[1].const_val
270 #define inst_i0 data.op[0].src
271 #define inst_i1 data.op[1].src
272 #define inst_p0 data.op[0].p
273 #define inst_p1 data.op[1].p
274 #define inst_l data.i8const
275 #define inst_r data.r8const
276 #define inst_left data.op[0].src
277 #define inst_right data.op[1].src
279 #define inst_newa_len data.op[0].src
280 #define inst_newa_class data.op[1].klass
282 #define inst_var data.op[0].var
283 #define inst_vtype data.op[1].vtype
284 /* in branch instructions */
285 #define inst_many_bb data.op[1].many_blocks
286 #define inst_target_bb data.op[0].target_block
287 #define inst_true_bb data.op[1].many_blocks[0]
288 #define inst_false_bb data.op[1].many_blocks[1]
290 #define inst_basereg sreg1
291 #define inst_indexreg sreg2
292 #define inst_destbasereg dreg
293 #define inst_offset data.op[0].const_val
294 #define inst_imm data.op[1].const_val
296 #define inst_phi_args data.op[1].phi_args
298 /* instruction description for use in regalloc/scheduling */
314 guint16 tid; /* tree number */
315 guint16 bid; /* block number */
321 MonoPosition first_use, last_use;
325 * Additional information about a variable
327 struct MonoMethodVar {
328 guint idx; /* inside cfg->varinfo, cfg->vars */
330 MonoBitSet *dfrontier;
331 MonoLiveRange range; /* generated by liveness analysis */
332 int reg; /* != -1 if allocated into a register */
334 MonoBitSet *def_in; /* used by SSA */
335 MonoInst *def; /* used by SSA */
336 MonoBasicBlock *def_bb; /* used by SSA */
337 GList *uses; /* used by SSA */
338 char cpstate; /* used by SSA conditional constant propagation */
342 gpointer end_of_stack;
345 void (*abort_func) (MonoObject *object);
351 MONO_PATCH_INFO_LABEL,
352 MONO_PATCH_INFO_METHOD,
353 MONO_PATCH_INFO_METHOD_JUMP,
354 MONO_PATCH_INFO_METHOD_REL,
355 MONO_PATCH_INFO_METHODCONST,
356 MONO_PATCH_INFO_INTERNAL_METHOD,
357 MONO_PATCH_INFO_SWITCH,
359 MONO_PATCH_INFO_EXC_NAME,
360 MONO_PATCH_INFO_CLASS,
361 MONO_PATCH_INFO_IMAGE,
362 MONO_PATCH_INFO_FIELD,
363 MONO_PATCH_INFO_VTABLE,
364 MONO_PATCH_INFO_CLASS_INIT,
365 MONO_PATCH_INFO_SFLDA,
366 MONO_PATCH_INFO_LDSTR,
367 MONO_PATCH_INFO_LDTOKEN,
368 MONO_PATCH_INFO_TYPE_FROM_HANDLE,
373 MONO_PATCH_INFO_BB_OVF,
374 MONO_PATCH_INFO_WRAPPER
378 * We need to store the image which the token refers to along with the token,
379 * since the image might not be the same as the image of the method which
380 * contains the relocation, because of inlining.
382 typedef struct MonoJumpInfoToken {
387 typedef struct MonoJumpInfo MonoJumpInfo;
388 struct MonoJumpInfo {
396 MonoJumpInfoType type;
398 gconstpointer target;
401 MonoBasicBlock **table;
405 MonoClassField *field;
409 MonoJumpInfoToken *token;
412 int table_size; /* use by switch */
415 /* optimization flags: keep up to date with the name array in driver.c */
417 MONO_OPT_PEEPHOLE = 1 << 0,
418 MONO_OPT_BRANCH = 1 << 1,
419 MONO_OPT_INLINE = 1 << 2,
420 MONO_OPT_CFOLD = 1 << 3,
421 MONO_OPT_CONSPROP = 1 << 4,
422 MONO_OPT_COPYPROP = 1 << 5,
423 MONO_OPT_DEADCE = 1 << 6,
424 MONO_OPT_LINEARS = 1 << 7,
425 MONO_OPT_CMOV = 1 << 8,
426 MONO_OPT_SHARED = 1 << 9,
427 MONO_OPT_SCHED = 1 << 10,
428 MONO_OPT_INTRINS = 1 << 11,
429 MONO_OPT_TAILC = 1 << 12,
430 MONO_OPT_LOOP = 1 << 13,
431 MONO_OPT_FCMOV = 1 << 14,
432 MONO_OPT_LEAF = 1 << 15,
433 MONO_OPT_AOT = 1 << 16,
434 MONO_OPT_PRECOMP = 1 << 17
437 #define MONO_REGION_FINALLY 16
438 #define MONO_REGION_CATCH 32
439 #define MONO_REGION_FAULT 64
440 #define MONO_REGION_FILTER 128
443 * Control Flow Graph and compilation unit information
447 MonoMemPool *mempool;
449 MonoMethodVar **vars;
451 MonoBasicBlock *bb_entry;
452 MonoBasicBlock *bb_exit;
453 MonoBasicBlock *bb_init;
454 MonoBasicBlock **bblocks;
456 MonoMemPool *state_pool; /* used by instruction selection */
457 MonoBasicBlock *cbb; /* used by instruction selection */
458 MonoInst *prev_ins; /* in decompose */
459 MonoJumpInfo *patch_info;
460 MonoJitInfo *jit_info;
463 guint num_varinfo; /* used items in varinfo */
464 guint varinfo_count; /* total storage in varinfo */
467 MonoSpillInfo *spill_info; /* machine register spills */
468 MonoSpillInfo *spill_info_float; /* fp register spills */
470 // unsigned char *cil_code;
472 MonoInst *exvar; /* the exception object passed to catch/filter blocks */
473 MonoInst *domainvar; /* a cache for the current domain */
474 /* A hashtable of region ID-> SP var mappings */
475 /* An SP var is a place to store the stack pointer (used by handlers) */
478 GList *ldstr_list; /* used by AOT */
482 unsigned char *native_code;
487 regmask_t used_int_regs;
489 guint32 prof_options;
492 guint32 verbose_level;
497 gboolean disable_aot;
498 gboolean disable_ssa;
502 MonoProfileCoverageInfo *coverage_info;
504 guint8 ins, locals, outs; /* reg stack region sizes */
509 MONO_CFG_HAS_ALLOCA = 1 << 0,
510 MONO_CFG_HAS_CALLS = 1 << 1
514 gulong methods_compiled;
516 gulong methods_lookups;
517 gulong method_trampolines;
519 gulong analyze_stack_repeat;
520 gulong cil_code_size;
521 gulong native_code_size;
522 gulong code_reallocs;
523 gulong max_code_size_ratio;
524 gulong biggest_method_size;
525 gulong allocated_code_size;
526 gulong inlineable_methods;
527 gulong inlined_methods;
529 gulong max_basic_blocks;
530 MonoMethod *max_ratio_method;
531 MonoMethod *biggest_method;
535 extern MonoJitStats mono_jit_stats;
537 /* values for MonoInst.ssa_op */
546 #define OP_CEQ (256+CEE_CEQ)
547 #define OP_CLT (256+CEE_CLT)
548 #define OP_CLT_UN (256+CEE_CLT_UN)
549 #define OP_CGT (256+CEE_CGT)
550 #define OP_CGT_UN (256+CEE_CGT_UN)
551 #define OP_LOCALLOC (256+CEE_LOCALLOC)
553 /* opcodes: value assigned after all the CIL opcodes */
557 #define MINI_OP(a,b) a,
559 OP_START = MONO_CEE_LAST,
560 #include "mini-ops.h"
565 /* make this depend on 32bit platform (use OP_LADD otherwise) */
566 #define OP_PADD CEE_ADD
567 #define OP_PNEG CEE_NEG
568 #define OP_PCONV_TO_U2 CEE_CONV_U2
569 #define OP_PCONV_TO_OVF_I1_UN CEE_CONV_OVF_I1_UN
570 #define OP_PCONV_TO_OVF_I1 CEE_CONV_OVF_I1
571 #define OP_PCEQ CEE_CEQ
599 MONO_COMP_DFRONTIER = 4,
600 MONO_COMP_DOM_REV = 8,
601 MONO_COMP_LIVENESS = 16,
603 MONO_COMP_SSA_DEF_USE = 64,
604 MONO_COMP_REACHABILITY = 128,
605 MONO_COMP_LOOPS = 256
610 MONO_GRAPH_DTREE = 2,
611 MONO_GRAPH_CFG_CODE = 4,
612 MONO_GRAPH_CFG_SSA = 8,
613 MONO_GRAPH_CFG_OPTCODE = 16
619 gconstpointer wrapper;
620 MonoMethodSignature *sig;
623 typedef void (*MonoInstFunc) (MonoInst *tree, gpointer data);
626 int mono_main (int argc, char* argv[]);
627 void mono_set_defaults (int verbose_level, guint32 opts);
628 MonoDomain* mini_init (const char *filename);
629 void mini_cleanup (MonoDomain *domain);
632 MonoJumpInfoToken * mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token);
633 MonoInst* mono_find_spvar_for_region (MonoCompile *cfg, int region);
634 void mono_precompile_assemblies (void);
635 int mono_parse_default_optimizations (const char* p);
636 void mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst);
637 void mono_constant_fold (MonoCompile *cfg);
638 void mono_constant_fold_inst (MonoInst *inst, gpointer data);
639 void mono_cprop_local (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size);
640 MonoInst* mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode);
641 void mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom);
642 void mono_print_tree (MonoInst *tree);
643 void mono_print_tree_nl (MonoInst *tree);
644 void mono_print_code (MonoCompile *cfg);
645 void mono_select_instructions (MonoCompile *cfg);
646 const char* mono_inst_name (int op);
647 void mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data);
648 void mono_disassemble_code (guint8 *code, int size, char *id);
649 guint mono_type_to_ldind (MonoType *t);
650 guint mono_type_to_stind (MonoType *t);
651 void mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target);
652 void mono_remove_patch_info (MonoCompile *cfg, int ip);
653 MonoLMF** mono_get_lmf_addr (void);
654 GList *mono_varlist_insert_sorted (MonoCompile *cfg, GList *list, MonoMethodVar *mv, gboolean sort_end);
655 GList *mono_varlist_sort (MonoCompile *cfg, GList *list, int sort_type);
656 void mono_analyze_liveness (MonoCompile *cfg);
657 void mono_linear_scan (MonoCompile *cfg, GList *vars, GList *regs, regmask_t *used_mask);
658 void mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks);
659 int mono_compile_assembly (MonoAssembly *ass, guint32 opts);
660 MonoCompile *mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts);
661 void mono_destroy_compile (MonoCompile *cfg);
662 void mono_aot_init (void);
663 MonoJitInfo* mono_aot_get_method (MonoDomain *domain,
665 gboolean mono_method_blittable (MonoMethod *method);
666 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee);
667 void mono_register_opcode_emulation (int opcode, const char* name, MonoMethodSignature *sig, gpointer func, gboolean no_throw);
668 void mono_arch_register_lowlevel_calls (void);
669 void mono_draw_graph (MonoCompile *cfg, MonoGraphOptions draw_options);
670 void mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest);
672 int mono_find_method_opcode (MonoMethod *method);
673 MonoJitICallInfo *mono_find_jit_icall_by_name (const char *name);
674 MonoJitICallInfo *mono_find_jit_icall_by_addr (gconstpointer addr);
675 MonoJitICallInfo *mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save);
676 gconstpointer mono_icall_get_wrapper (MonoJitICallInfo* callinfo);
678 gpointer mono_create_class_init_trampoline (MonoVTable *vtable);
679 MonoVTable* mono_find_class_init_trampoline_by_addr (gconstpointer addr);
681 /* methods that must be provided by the arch-specific port */
682 void mono_arch_cpu_init (void);
683 guint32 mono_arch_cpu_optimizazions (guint32 *exclude_mask);
684 void mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code);
685 void *mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments);
686 void *mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments);
687 MonoCallInst *mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual);
688 gint mono_arch_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args);
689 void mono_codegen (MonoCompile *cfg);
690 const char *mono_arch_regname (int reg);
691 gpointer mono_arch_get_throw_exception (void);
692 gpointer mono_arch_get_throw_exception_by_name (void);
693 gpointer mono_arch_create_jit_trampoline (MonoMethod *method);
694 gpointer mono_arch_create_jump_trampoline (MonoMethod *method);
695 gpointer mono_arch_create_class_init_trampoline(MonoVTable *vtable);
696 GList *mono_arch_get_allocatable_int_vars (MonoCompile *cfg);
697 GList *mono_arch_get_global_int_regs (MonoCompile *cfg);
698 void mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors);
699 void mono_arch_flush_icache (guint8 *code, gint size);
700 int mono_arch_max_epilog_size (MonoCompile *cfg);
701 guint8 *mono_arch_emit_prolog (MonoCompile *cfg);
702 void mono_arch_emit_epilog (MonoCompile *cfg);
703 void mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb);
704 void mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb);
705 gboolean mono_arch_has_unwind_info (gconstpointer addr);
706 void mono_arch_setup_jit_tls_data (MonoJitTlsData *tls);
707 void mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg);
708 void mono_arch_allocate_vars (MonoCompile *m);
709 void mono_jit_walk_stack (MonoStackWalk func, gpointer user_data);
710 MonoArray *ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info);
711 MonoBoolean ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
712 MonoReflectionMethod **method,
713 gint32 *iloffset, gint32 *native_offset,
714 MonoString **file, gint32 *line, gint32 *column);
716 /* Dominator/SSA methods */
717 void mono_compile_dominator_info (MonoCompile *cfg, int dom_flags);
718 void mono_compute_natural_loops (MonoCompile *cfg);
719 MonoBitSet* mono_compile_iterated_dfrontier (MonoCompile *cfg, MonoBitSet *set);
720 void mono_ssa_compute (MonoCompile *cfg);
721 void mono_ssa_remove (MonoCompile *cfg);
722 void mono_ssa_cprop (MonoCompile *cfg);
723 void mono_ssa_deadce (MonoCompile *cfg);
724 void mono_ssa_strength_reduction (MonoCompile *cfg);
726 /* debugging support */
727 void mono_debug_init_method (MonoCompile *cfg, MonoBasicBlock *start_block,
728 guint32 breakpoint_id);
729 void mono_debug_open_method (MonoCompile *cfg);
730 void mono_debug_close_method (MonoCompile *cfg);
731 void mono_debug_open_block (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address);
732 void mono_debug_record_line_number (MonoCompile *cfg, MonoInst *ins, guint32 address);
733 void mono_debug_serialize_debug_info (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len);
734 void mono_debug_add_aot_method (MonoDomain *domain,
735 MonoMethod *method, guint8 *code_start,
736 guint8 *debug_info, guint32 debug_info_len);
739 MonoTraceSpec *mono_trace_parse_options (MonoAssembly *assembly, char *options);
740 gboolean mono_trace_eval (MonoMethod *method);
742 #endif /* __MONO_MINI_H__ */