Fri Jun 18 18:24:28 CEST 2004 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / mini / mini.h
index bfec035c9932da061c78fd40c081cc70cd5bae6f..117b1f7e97a0e78bc694641efd3c1817e64d77ad 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef __MONO_MINI_H__
 #define __MONO_MINI_H__
 
+#include "config.h"
 #include <glib.h>
 #include <signal.h>
 #include <mono/metadata/loader.h>
@@ -10,6 +11,9 @@
 #include <mono/metadata/object.h>
 #include <mono/metadata/opcodes.h>
 #include <mono/metadata/tabledefs.h>
+#include <mono/metadata/domain-internals.h>
+#include "mono/metadata/class-internals.h"
+#include "mono/metadata/object-internals.h"
 #include <mono/metadata/profiler-private.h>
 
 #include "mini-arch.h"
 
 #define MONO_USE_AOT_COMPILER
 
+/* for 32 bit systems */
+#if G_BYTE_ORDER == G_LITTLE_ENDIAN
+#define MINI_LS_WORD_OFFSET 0
+#define MINI_MS_WORD_OFFSET 4
+#define inst_ls_word data.op[0].const_val
+#define inst_ms_word data.op[1].const_val
+#else
+#define MINI_LS_WORD_OFFSET 4
+#define MINI_MS_WORD_OFFSET 0
+#define inst_ls_word data.op[1].const_val
+#define inst_ms_word data.op[0].const_val
+#endif
+
 /* Version number of the AOT file format */
-#define MONO_AOT_FILE_VERSION "5"
+#define MONO_AOT_FILE_VERSION "7"
 
 #if 1
 #define mono_bitset_test_fast(set,n) (((guint32*)set)[2+(n)/32] & (1 << ((n) % 32)))
@@ -81,9 +98,10 @@ typedef struct MonoMethodVar MonoMethodVar;
 typedef struct MonoBasicBlock MonoBasicBlock;
 typedef struct MonoLMF MonoLMF;
 typedef struct MonoSpillInfo MonoSpillInfo;
+typedef struct MonoTraceSpec MonoTraceSpec;
 
 extern guint32 mono_jit_tls_id;
-extern gboolean mono_jit_trace_calls;
+extern MonoTraceSpec *mono_jit_trace_calls;
 extern gboolean mono_break_on_exc;
 extern int mono_exc_esp_offset;
 extern gboolean mono_compile_aot;
@@ -175,6 +193,20 @@ struct MonoBasicBlock {
 
        /* we use that to prevent merging of bblock covered by different clauses*/
        guint real_offset;
+
+       /*
+        * The region encodes whether the basic block is inside
+        * a finally, catch, filter or none of thoese.
+        *
+        * If the value is -1, then it is neither finally, catch nor filter
+        *
+        * Otherwise the format is:
+        *
+        *  Bits: |     0-3      |       4-7      |     8-31
+        *        |              |                |
+        *        | clause-flags |   MONO_REGION  | clause-index 
+        *
+        */
         guint region;
 
        /* The current symbolic register number, used in local register allocation. */
@@ -225,6 +257,7 @@ struct MonoCallInst {
        MonoInst *out_args;
        gconstpointer fptr;
        guint stack_usage;
+       gboolean virtual;
        regmask_t used_iregs;
        regmask_t used_fregs;
 };
@@ -264,7 +297,6 @@ enum {
 #define inst_newa_len   data.op[0].src
 #define inst_newa_class data.op[1].klass
 
-#define inst_switch data.op[0].switch_blocks
 #define inst_var    data.op[0].var
 #define inst_vtype  data.op[1].vtype
 /* in branch instructions */
@@ -326,8 +358,11 @@ struct MonoMethodVar {
 
 typedef struct {
        gpointer          end_of_stack;
+       guint32           stack_size;
        MonoLMF          *lmf;
        MonoLMF          *first_lmf;
+       gpointer         signal_stack;
+       guint32          signal_stack_size;
        void            (*abort_func) (MonoObject *object);
 } MonoJitTlsData;
 
@@ -356,9 +391,21 @@ typedef enum {
        MONO_PATCH_INFO_R8,
        MONO_PATCH_INFO_IP,
        MONO_PATCH_INFO_IID,
+       MONO_PATCH_INFO_BB_OVF,
+       MONO_PATCH_INFO_EXC_OVF,
        MONO_PATCH_INFO_WRAPPER
 } MonoJumpInfoType;
 
+/*
+ * We need to store the image which the token refers to along with the token,
+ * since the image might not be the same as the image of the method which
+ * contains the relocation, because of inlining.
+ */
+typedef struct MonoJumpInfoToken {
+       MonoImage *image;
+       guint32 token;
+} MonoJumpInfoToken;
+
 typedef struct MonoJumpInfo MonoJumpInfo;
 struct MonoJumpInfo {
        MonoJumpInfo *next;
@@ -381,7 +428,7 @@ struct MonoJumpInfo {
                MonoImage      *image;
                MonoVTable     *vtable;
                const char     *name;
-               guint32        token;
+               MonoJumpInfoToken  *token;
        } data;
 
        int table_size; /* use by switch */
@@ -405,12 +452,15 @@ enum {
        MONO_OPT_LOOP     = 1 << 13,
        MONO_OPT_FCMOV    = 1 << 14,
        MONO_OPT_LEAF     = 1 << 15,
-       MONO_OPT_AOT      = 1 << 16 
+       MONO_OPT_AOT      = 1 << 16,
+       MONO_OPT_PRECOMP  = 1 << 17,
+       MONO_OPT_ABCREM   = 1 << 18
 };
 
+/* Bit-fields in the MonoBasicBlock.region */
 #define MONO_REGION_FINALLY  16
 #define MONO_REGION_CATCH    32
-#define MONO_REGION_FAULT    64
+#define MONO_REGION_FAULT    64         /* Currently unused */
 #define MONO_REGION_FILTER  128
 
 /*
@@ -438,13 +488,19 @@ typedef struct {
        guint            varinfo_count; /* total storage in varinfo */
        gint             stack_offset;
        MonoRegState    *rs;
-       MonoSpillInfo   *spill_info;
+       MonoSpillInfo   *spill_info; /* machine register spills */
+       MonoSpillInfo   *spill_info_float; /* fp register spills */
        gint             spill_count;
-       // unsigned char   *cil_code;
+       /* unsigned char   *cil_code; */
 
-       MonoInst        *exvar; /* the exception object passed to catch/filter blocks */
+       /* the exception object passed to catch/filter blocks */
+       MonoInst        *exvar;
+       
        MonoInst        *domainvar; /* a cache for the current domain */
-       MonoInst        *spvar; /* a place to store the stack pointer (used by handlers) */
+
+       /* A hashtable of region ID-> SP var mappings */
+       /* An SP var is a place to store the stack pointer (used by handlers)*/
+       GHashTable      *spvars;
 
        GList           *ldstr_list; /* used by AOT */
        
@@ -467,9 +523,13 @@ typedef struct {
        gint32           sig_cookie;
        gboolean         disable_aot;
        gboolean         disable_ssa;
+       gboolean         run_cctors;
+       gboolean         need_lmf_area;
        gpointer         debug_info;
+       guint32          lmf_offset;
        guint16          *intvars;
        MonoProfileCoverageInfo *coverage_info;
+       MonoCompileArch  arch;
 #ifdef __ia64
        guint8           ins, locals, outs; /* reg stack region sizes */
 #endif /* __ia64 */
@@ -477,7 +537,9 @@ typedef struct {
 
 typedef enum {
        MONO_CFG_HAS_ALLOCA = 1 << 0,
-       MONO_CFG_HAS_CALLS  = 1 << 1
+       MONO_CFG_HAS_CALLS  = 1 << 1,
+       MONO_CFG_HAS_LDELEMA  = 1 << 2,
+       MONO_CFG_HAS_VARARGS  = 1 << 3
 } MonoCompileFlags;
 
 typedef struct {
@@ -590,6 +652,12 @@ typedef struct {
        MonoMethodSignature *sig;
 } MonoJitICallInfo;
 
+typedef struct {
+       guint16 size;
+       guint16 offset;
+       guint8  pad;
+} MonoJitArgumentInfo;
+
 typedef void (*MonoInstFunc) (MonoInst *tree, gpointer data);
 
 /* main function */
@@ -599,17 +667,20 @@ MonoDomain* mini_init                      (const char *filename);
 void        mini_cleanup                   (MonoDomain *domain);
 
 /* helper methods */
+MonoJumpInfoToken * mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token);
+MonoInst* mono_find_spvar_for_region        (MonoCompile *cfg, int region);
+void      mono_precompile_assemblies        (void);
 int       mono_parse_default_optimizations  (const char* p);
 void      mono_bblock_add_inst              (MonoBasicBlock *bb, MonoInst *inst);
 void      mono_constant_fold                (MonoCompile *cfg);
 void      mono_constant_fold_inst           (MonoInst *inst, gpointer data);
+int       mono_is_power_of_two              (guint32 val);
 void      mono_cprop_local                  (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size);
 MonoInst* mono_compile_create_var           (MonoCompile *cfg, MonoType *type, int opcode);
 void      mono_blockset_print               (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom);
 void      mono_print_tree                   (MonoInst *tree);
 void      mono_print_tree_nl                (MonoInst *tree);
 void      mono_print_code                   (MonoCompile *cfg);
-int       mono_spillvar_offset              (MonoCompile *cfg, int spillvar);
 void      mono_select_instructions          (MonoCompile *cfg);
 const char* mono_inst_name                  (int op);
 void      mono_inst_foreach                 (MonoInst *tree, MonoInstFunc func, gpointer data);
@@ -618,18 +689,21 @@ guint     mono_type_to_ldind                (MonoType *t);
 guint     mono_type_to_stind                (MonoType *t);
 void      mono_add_patch_info               (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target);
 void      mono_remove_patch_info            (MonoCompile *cfg, int ip);
-gpointer  mono_get_lmf_addr                 (void);
+gpointer  mono_resolve_patch_target         (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors);
+MonoLMF** mono_get_lmf_addr                 (void);
 GList    *mono_varlist_insert_sorted        (MonoCompile *cfg, GList *list, MonoMethodVar *mv, gboolean sort_end);
+GList    *mono_varlist_sort                 (MonoCompile *cfg, GList *list, int sort_type);
 void      mono_analyze_liveness             (MonoCompile *cfg);
 void      mono_linear_scan                  (MonoCompile *cfg, GList *vars, GList *regs, regmask_t *used_mask);
 void      mono_create_jump_table            (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks);
 int       mono_compile_assembly             (MonoAssembly *ass, guint32 opts);
-MonoCompile *mini_method_compile            (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts);
+MonoCompile *mini_method_compile            (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts);
 void      mono_destroy_compile              (MonoCompile *cfg);
 void      mono_aot_init                     (void);
 MonoJitInfo*  mono_aot_get_method           (MonoDomain *domain,
                                                                                         MonoMethod *method);
 gboolean  mono_method_blittable             (MonoMethod *method);
+gboolean  mono_method_same_domain           (MonoJitInfo *caller, MonoJitInfo *callee);
 void      mono_register_opcode_emulation    (int opcode, const char* name, MonoMethodSignature *sig, gpointer func, gboolean no_throw);
 void      mono_arch_register_lowlevel_calls (void);
 void      mono_draw_graph                   (MonoCompile *cfg, MonoGraphOptions draw_options);
@@ -641,8 +715,12 @@ MonoJitICallInfo *mono_find_jit_icall_by_addr  (gconstpointer addr);
 MonoJitICallInfo *mono_register_jit_icall      (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save);
 gconstpointer     mono_icall_get_wrapper       (MonoJitICallInfo* callinfo);
 
+gpointer          mono_create_jump_trampoline (MonoDomain *domain, 
+                                                                                          MonoMethod *method, 
+                                                                                          gboolean add_sync_wrapper);
 gpointer          mono_create_class_init_trampoline (MonoVTable *vtable);
 MonoVTable*       mono_find_class_init_trampoline_by_addr (gconstpointer addr);
+gboolean          mono_running_on_valgrind (void);
 
 /* methods that must be provided by the arch-specific port */
 void      mono_arch_cpu_init                    (void);
@@ -651,16 +729,18 @@ void      mono_arch_instrument_mem_needs        (MonoMethod *method, int *stack,
 void     *mono_arch_instrument_prolog           (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments);
 void     *mono_arch_instrument_epilog           (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments);
 MonoCallInst *mono_arch_call_opcode             (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual);
+gint      mono_arch_get_opcode_for_method       (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args);
 void      mono_codegen                          (MonoCompile *cfg);
 const char *mono_arch_regname                   (int reg);
 gpointer  mono_arch_get_throw_exception         (void);
 gpointer  mono_arch_get_throw_exception_by_name (void);
 gpointer  mono_arch_create_jit_trampoline       (MonoMethod *method);
-gpointer  mono_arch_create_jump_trampoline      (MonoMethod *method);
+MonoJitInfo *mono_arch_create_jump_trampoline      (MonoMethod *method);
 gpointer  mono_arch_create_class_init_trampoline(MonoVTable *vtable);
 GList    *mono_arch_get_allocatable_int_vars    (MonoCompile *cfg);
 GList    *mono_arch_get_global_int_regs         (MonoCompile *cfg);
-void      mono_arch_patch_code                  (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji);
+guint32   mono_arch_regalloc_cost               (MonoCompile *cfg, MonoMethodVar *vmv);
+void      mono_arch_patch_code                  (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors);
 void      mono_arch_flush_icache                (guint8 *code, gint size);
 int       mono_arch_max_epilog_size             (MonoCompile *cfg);
 guint8   *mono_arch_emit_prolog                 (MonoCompile *cfg);
@@ -668,7 +748,30 @@ void      mono_arch_emit_epilog                 (MonoCompile *cfg);
 void      mono_arch_local_regalloc              (MonoCompile *cfg, MonoBasicBlock *bb);
 void      mono_arch_output_basic_block          (MonoCompile *cfg, MonoBasicBlock *bb);
 gboolean  mono_arch_has_unwind_info             (gconstpointer addr);
+void      mono_arch_setup_jit_tls_data          (MonoJitTlsData *tls);
+void      mono_arch_free_jit_tls_data           (MonoJitTlsData *tls);
+void      mono_arch_emit_this_vret_args         (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg);
 void      mono_arch_allocate_vars               (MonoCompile *m);
+int       mono_arch_get_argument_info           (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info);
+gboolean  mono_arch_print_tree                 (MonoInst *tree, int arity);
+MonoJitInfo *mono_arch_find_jit_info            (MonoDomain *domain, 
+                                                MonoJitTlsData *jit_tls, 
+                                                MonoJitInfo *res, 
+                                                MonoJitInfo *prev_ji, 
+                                                MonoContext *ctx, 
+                                                MonoContext *new_ctx, 
+                                                char **trace, 
+                                                MonoLMF **lmf, 
+                                                int *native_offset,
+                                                gboolean *managed);
+gpointer mono_arch_get_call_filter              (void);
+gpointer mono_arch_get_restore_context          (void);
+gboolean mono_arch_handle_exception             (void *sigctx, gpointer obj, gboolean test_only);
+gpointer mono_arch_ip_from_context              (void *sigctx);
+void     mono_arch_flush_register_windows       (void);
+
+/* Exception handling */
+gboolean mono_handle_exception                  (MonoContext *ctx, gpointer obj, gboolean test_only);
 void      mono_jit_walk_stack                   (MonoStackWalk func, gpointer user_data);
 MonoArray *ves_icall_get_trace                  (MonoException *exc, gint32 skip, MonoBoolean need_file_info);
 MonoBoolean ves_icall_get_frame_info            (gint32 skip, MonoBoolean need_file_info, 
@@ -685,6 +788,7 @@ void        mono_ssa_remove                     (MonoCompile *cfg);
 void        mono_ssa_cprop                      (MonoCompile *cfg);
 void        mono_ssa_deadce                     (MonoCompile *cfg);
 void        mono_ssa_strength_reduction         (MonoCompile *cfg);
+void        mono_free_loop_info                 (MonoCompile *cfg);
 
 /* debugging support */
 void      mono_debug_init_method                (MonoCompile *cfg, MonoBasicBlock *start_block,
@@ -693,5 +797,16 @@ void      mono_debug_open_method                (MonoCompile *cfg);
 void      mono_debug_close_method               (MonoCompile *cfg);
 void      mono_debug_open_block                 (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address);
 void      mono_debug_record_line_number         (MonoCompile *cfg, MonoInst *ins, guint32 address);
+void      mono_debug_serialize_debug_info       (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len);
+void      mono_debug_add_aot_method            (MonoDomain *domain,
+                                                                                               MonoMethod *method, guint8 *code_start, 
+                                                                                               guint8 *debug_info, guint32 debug_info_len);
+
+/* Tracing */
+MonoTraceSpec *mono_trace_parse_options         (MonoAssembly *assembly, char *options);
+gboolean       mono_trace_eval                  (MonoMethod *method);
+
+extern void
+mono_perform_abc_removal (MonoCompile *cfg);
 
 #endif /* __MONO_MINI_H__ */