Fix some AOT memory leaks.
[mono.git] / mono / mini / mini.c
index ecf6707764c860ef563f80139e6d18d6c96339fc..fcb2ca1109df19729f605f5da8eff44f6ca8d844 100644 (file)
@@ -1,13 +1,15 @@
 /*
  * mini.c: The new Mono code generator.
  *
- * Author:
+ * Authors:
  *   Paolo Molaro (lupus@ximian.com)
  *   Dietmar Maurer (dietmar@ximian.com)
  *
- * (C) 2002 Ximian, Inc.
+ * Copyright 2002-2003 Ximian, Inc.
+ * Coprygith 2003-2010 Novell, Inc.
  */
 
+#define MONO_LLVM_IN_MINI 1
 #include <config.h>
 #include <signal.h>
 #ifdef HAVE_ALLOCA_H
@@ -45,6 +47,7 @@
 #include <mono/metadata/verify-internals.h>
 #include <mono/metadata/mempool-internals.h>
 #include <mono/metadata/attach.h>
+#include <mono/metadata/runtime.h>
 #include <mono/utils/mono-math.h>
 #include <mono/utils/mono-compiler.h>
 #include <mono/utils/mono-counters.h>
@@ -53,6 +56,7 @@
 #include <mono/utils/dtrace.h>
 
 #include "mini.h"
+#include "mini-llvm.h"
 #include "tasklets.h"
 #include <string.h>
 #include <ctype.h>
@@ -72,10 +76,16 @@ static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 op
 MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
 MonoMethodSignature *helper_sig_domain_get = NULL;
 MonoMethodSignature *helper_sig_generic_class_init_trampoline = NULL;
+MonoMethodSignature *helper_sig_generic_class_init_trampoline_llvm = NULL;
 MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline = NULL;
 MonoMethodSignature *helper_sig_monitor_enter_exit_trampoline = NULL;
 MonoMethodSignature *helper_sig_monitor_enter_exit_trampoline_llvm = NULL;
 
+#ifdef __native_client_codegen__
+/* Default alignment for Native Client is 32-byte. */
+guint8 nacl_align_byte = 0xe0;
+#endif
+
 static guint32 default_opt = 0;
 static gboolean default_opt_set = FALSE;
 
@@ -106,26 +116,11 @@ gboolean mono_do_signal_chaining;
 static gboolean        mono_using_xdebug;
 static int mini_verbose = 0;
 
-/* Statistics */
-#ifdef ENABLE_LLVM
-static int methods_with_llvm, methods_without_llvm;
-#endif
-
 /*
- * This flag controls whenever the runtime uses LLVM compiled code.
- * Enabling this causes different/slower code paths to be used, which is why it
- * defaults to FALSE if ENABLE_LLVM is not defined, i.e. the runtime is only capable of
- * running AOT code compiled by LLVM.
- * Changes when this flag is set include:
- * - a per method vtable trampoline is used to handle virtual calls, instead of only
- *   one trampoline.
- * - fast generic virtual calls are not supported.
+ * This flag controls whenever the runtime uses LLVM for JIT compilation, and whenever
+ * it can load AOT code compiled by LLVM.
  */
-#ifdef ENABLE_LLVM
-gboolean mono_use_llvm = TRUE;
-#else
 gboolean mono_use_llvm = FALSE;
-#endif
 
 #define mono_jit_lock() EnterCriticalSection (&jit_mutex)
 #define mono_jit_unlock() LeaveCriticalSection (&jit_mutex)
@@ -157,6 +152,82 @@ gboolean disable_vtypes_in_regs = FALSE;
 
 gboolean mono_dont_free_global_codeman;
 
+#ifdef __native_client_codegen__
+
+/* Prevent instructions from straddling a 32-byte alignment boundary.   */
+/* Instructions longer than 32 bytes must be aligned internally.        */
+/* IN: pcode, instlen                                                   */
+/* OUT: pcode                                                           */
+void mono_nacl_align_inst(guint8 **pcode, int instlen) {
+  int space_in_block;
+
+  space_in_block = kNaClAlignment - ((uintptr_t)(*pcode) & kNaClAlignmentMask);
+
+  if (G_UNLIKELY (instlen >= kNaClAlignment)) {
+    g_assert_not_reached();
+  } else if (instlen > space_in_block) {
+    *pcode = mono_arch_nacl_pad(*pcode, space_in_block);
+  }
+}
+
+/* Move emitted call sequence to the end of a kNaClAlignment-byte block.  */
+/* IN: start    pointer to start of call sequence                         */
+/* IN: pcode    pointer to end of call sequence (current "IP")            */
+/* OUT: start   pointer to the start of the call sequence after padding   */
+/* OUT: pcode   pointer to the end of the call sequence after padding     */
+void mono_nacl_align_call(guint8 **start, guint8 **pcode) {
+  const size_t MAX_NACL_CALL_LENGTH = kNaClAlignment;
+  guint8 copy_of_call[MAX_NACL_CALL_LENGTH];
+  guint8 *temp;
+
+  const size_t length = (size_t)((*pcode)-(*start));
+  g_assert(length < MAX_NACL_CALL_LENGTH);
+
+  memcpy(copy_of_call, *start, length);
+  temp = mono_nacl_pad_call(*start, (guint8)length);
+  memcpy(temp, copy_of_call, length);
+  (*start) = temp;
+  (*pcode) = temp + length;
+}
+
+/* mono_nacl_pad_call(): Insert padding for Native Client call instructions */
+/*    code     pointer to buffer for emitting code                          */
+/*    ilength  length of call instruction                                   */
+guint8 *mono_nacl_pad_call(guint8 *code, guint8 ilength) {
+  int freeSpaceInBlock = kNaClAlignment - ((uintptr_t)code & kNaClAlignmentMask);
+  int padding = freeSpaceInBlock - ilength;
+
+  if (padding < 0) {
+    /* There isn't enough space in this block for the instruction. */
+    /* Fill this block and start a new one.                        */
+    code = mono_arch_nacl_pad(code, freeSpaceInBlock);
+    freeSpaceInBlock = kNaClAlignment;
+    padding = freeSpaceInBlock - ilength;
+  }
+  g_assert(ilength > 0);
+  g_assert(padding >= 0);
+  g_assert(padding < kNaClAlignment);
+  if (0 == padding) return code;
+  return mono_arch_nacl_pad(code, padding);
+}
+
+guint8 *mono_nacl_align(guint8 *code) {
+  int padding = kNaClAlignment - ((uintptr_t)code & kNaClAlignmentMask);
+  if (padding != kNaClAlignment) code = mono_arch_nacl_pad(code, padding);
+  return code;
+}
+
+void mono_nacl_fix_patches(const guint8 *code, MonoJumpInfo *ji)
+{
+  MonoJumpInfo *patch_info;
+  for (patch_info = ji; patch_info; patch_info = patch_info->next) {
+    unsigned char *ip = patch_info->ip.i + code;
+    ip = mono_arch_nacl_skip_nops(ip);
+    patch_info->ip.i = ip - code;
+  }
+}
+#endif  /* __native_client_codegen__ */
+
 gboolean
 mono_running_on_valgrind (void)
 {
@@ -169,6 +240,12 @@ mono_running_on_valgrind (void)
                return FALSE;
 }
 
+typedef struct {
+       MonoExceptionClause *clause;
+       MonoBasicBlock *basic_block;
+       int start_offset;
+} TryBlockHole;
+
 typedef struct {
        void *ip;
        MonoMethod *method;
@@ -396,6 +473,40 @@ mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
 {
        return mono_jump_info_token_new2 (mp, image, token, NULL);
 }
+/*
+ * mono_tramp_info_create:
+ *
+ *   Create a MonoTrampInfo structure from the arguments. This function assumes ownership
+ * of NAME, JI, and UNWIND_OPS.
+ */
+MonoTrampInfo*
+mono_tramp_info_create (const char *name, guint8 *code, guint32 code_size, MonoJumpInfo *ji, GSList *unwind_ops)
+{
+       MonoTrampInfo *info = g_new0 (MonoTrampInfo, 1);
+
+       info->name = (char*)name;
+       info->code = code;
+       info->code_size = code_size;
+       info->ji = ji;
+       info->unwind_ops = unwind_ops;
+
+       return info;
+}
+
+void
+mono_tramp_info_free (MonoTrampInfo *info)
+{
+       GSList *l;
+
+       g_free (info->name);
+
+       // FIXME: ji
+       for (l = info->unwind_ops; l; l = l->next)
+               g_free (l->data);
+       g_slist_free (info->unwind_ops);
+       g_free (info);
+}
 
 #define MONO_INIT_VARINFO(vi,id) do { \
        (vi)->range.first_use.pos.bid = 0xffff; \
@@ -470,8 +581,7 @@ mono_bblocks_linked (MonoBasicBlock *bb1, MonoBasicBlock *bb2)
 static int
 mono_find_block_region_notry (MonoCompile *cfg, int offset)
 {
-       MonoMethod *method = cfg->method;
-       MonoMethodHeader *header = mono_method_get_header (method);
+       MonoMethodHeader *header = cfg->header;
        MonoExceptionClause *clause;
        int i;
 
@@ -504,7 +614,7 @@ int
 mono_get_block_region_notry (MonoCompile *cfg, int region)
 {
        if ((region & (0xf << 4)) == MONO_REGION_TRY) {
-               MonoMethodHeader *header = mono_method_get_header (cfg->method);
+               MonoMethodHeader *header = cfg->header;
                
                /*
                 * This can happen if a try clause is nested inside a finally clause.
@@ -1218,18 +1328,6 @@ mini_class_is_system_array (MonoClass *klass)
                return FALSE;
 }
 
-static MonoJitICallInfo **emul_opcode_map = NULL;
-
-MonoJitICallInfo *
-mono_find_jit_opcode_emulation (int opcode)
-{
-       g_assert (opcode >= 0 && opcode <= OP_LAST);
-       if  (emul_opcode_map)
-               return emul_opcode_map [opcode];
-       else
-               return NULL;
-}
-
 gboolean
 mini_assembly_can_skip_verification (MonoDomain *domain, MonoMethod *method)
 {
@@ -1323,6 +1421,7 @@ create_helper_signature (void)
        helper_sig_domain_get = mono_create_icall_signature ("ptr");
        helper_sig_class_init_trampoline = mono_create_icall_signature ("void");
        helper_sig_generic_class_init_trampoline = mono_create_icall_signature ("void");
+       helper_sig_generic_class_init_trampoline_llvm = mono_create_icall_signature ("void ptr");
        helper_sig_rgctx_lazy_fetch_trampoline = mono_create_icall_signature ("ptr ptr");
        helper_sig_monitor_enter_exit_trampoline = mono_create_icall_signature ("void");
        helper_sig_monitor_enter_exit_trampoline_llvm = mono_create_icall_signature ("void object");
@@ -1540,12 +1639,11 @@ mono_allocate_stack_slots_full2 (MonoCompile *cfg, gboolean backward, guint32 *s
 #else
                case MONO_TYPE_I8:
 #endif
-#ifdef HAVE_SGEN_GC
-                       slot_info = &scalar_stack_slots [MONO_TYPE_I];
-                       break;
-#else
+                       if (cfg->disable_ref_noref_stack_slot_share) {
+                               slot_info = &scalar_stack_slots [MONO_TYPE_I];
+                               break;
+                       }
                        /* Fall through */
-#endif
 
                case MONO_TYPE_CLASS:
                case MONO_TYPE_OBJECT:
@@ -1830,12 +1928,11 @@ mono_allocate_stack_slots_full (MonoCompile *cfg, gboolean backward, guint32 *st
 #else
                        case MONO_TYPE_I8:
 #endif
-#ifdef HAVE_SGEN_GC
-                               slot_info = &scalar_stack_slots [MONO_TYPE_I];
-                               break;
-#else
+                               if (cfg->disable_ref_noref_stack_slot_share) {
+                                       slot_info = &scalar_stack_slots [MONO_TYPE_I];
+                                       break;
+                               }
                                /* Fall through */
-#endif
 
                        case MONO_TYPE_CLASS:
                        case MONO_TYPE_OBJECT:
@@ -1974,21 +2071,50 @@ mono_allocate_stack_slots (MonoCompile *m, guint32 *stack_size, guint32 *stack_a
        return mono_allocate_stack_slots_full (m, TRUE, stack_size, stack_align);
 }
 
+#define EMUL_HIT_SHIFT 3
+#define EMUL_HIT_MASK ((1 << EMUL_HIT_SHIFT) - 1)
+/* small hit bitmap cache */
+static mono_byte emul_opcode_hit_cache [(OP_LAST>>EMUL_HIT_SHIFT) + 1] = {0};
+static short emul_opcode_num = 0;
+static short emul_opcode_alloced = 0;
+static short *emul_opcode_opcodes = NULL;
+static MonoJitICallInfo **emul_opcode_map = NULL;
+
+MonoJitICallInfo *
+mono_find_jit_opcode_emulation (int opcode)
+{
+       g_assert (opcode >= 0 && opcode <= OP_LAST);
+       if (emul_opcode_hit_cache [opcode >> (EMUL_HIT_SHIFT + 3)] & (1 << (opcode & EMUL_HIT_MASK))) {
+               int i;
+               for (i = 0; i < emul_opcode_num; ++i) {
+                       if (emul_opcode_opcodes [i] == opcode)
+                               return emul_opcode_map [i];
+               }
+       }
+       return NULL;
+}
+
 void
 mono_register_opcode_emulation (int opcode, const char *name, const char *sigstr, gpointer func, gboolean no_throw)
 {
        MonoJitICallInfo *info;
        MonoMethodSignature *sig = mono_create_icall_signature (sigstr);
 
-       if (!emul_opcode_map)
-               emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
-
        g_assert (!sig->hasthis);
        g_assert (sig->param_count < 3);
 
        info = mono_register_jit_icall (func, name, sig, no_throw);
 
-       emul_opcode_map [opcode] = info;
+       if (emul_opcode_num >= emul_opcode_alloced) {
+               int incr = emul_opcode_alloced? emul_opcode_alloced/2: 16;
+               emul_opcode_alloced += incr;
+               emul_opcode_map = g_realloc (emul_opcode_map, sizeof (emul_opcode_map [0]) * emul_opcode_alloced);
+               emul_opcode_opcodes = g_realloc (emul_opcode_opcodes, sizeof (emul_opcode_opcodes [0]) * emul_opcode_alloced);
+       }
+       emul_opcode_map [emul_opcode_num] = info;
+       emul_opcode_opcodes [emul_opcode_num] = opcode;
+       emul_opcode_num++;
+       emul_opcode_hit_cache [opcode >> (EMUL_HIT_SHIFT + 3)] |= (1 << (opcode & EMUL_HIT_MASK));
 }
 
 static void
@@ -2152,6 +2278,10 @@ mono_verify_cfg (MonoCompile *cfg)
 void
 mono_destroy_compile (MonoCompile *cfg)
 {
+       GSList *l;
+
+       if (cfg->header)
+               mono_metadata_free_mh (cfg->header);
        //mono_mempool_stats (cfg->mempool);
        mono_free_loop_info (cfg);
        if (cfg->rs)
@@ -2160,11 +2290,13 @@ mono_destroy_compile (MonoCompile *cfg)
                g_hash_table_destroy (cfg->spvars);
        if (cfg->exvars)
                g_hash_table_destroy (cfg->exvars);
-       mono_mempool_destroy (cfg->mempool);
+       for (l = cfg->headers_to_free; l; l = l->next)
+               mono_metadata_free_mh (l->data);
        g_list_free (cfg->ldstr_list);
        g_hash_table_destroy (cfg->token_info_hash);
        if (cfg->abs_patches)
                g_hash_table_destroy (cfg->abs_patches);
+       mono_mempool_destroy (cfg->mempool);
 
        g_free (cfg->varinfo);
        g_free (cfg->vars);
@@ -2443,18 +2575,19 @@ static MonoInst*
 mono_create_tls_get (MonoCompile *cfg, int offset)
 {
 #ifdef MONO_ARCH_HAVE_TLS_GET
-       MonoInst* ins;
-       
-       if (offset == -1)
-               return NULL;
-       
-       MONO_INST_NEW (cfg, ins, OP_TLS_GET);
-       ins->dreg = mono_alloc_preg (cfg);
-       ins->inst_offset = offset;
-       return ins;
-#else
-       return NULL;
+       if (MONO_ARCH_HAVE_TLS_GET) {
+               MonoInst* ins;
+
+               if (offset == -1)
+                       return NULL;
+
+               MONO_INST_NEW (cfg, ins, OP_TLS_GET);
+               ins->dreg = mono_alloc_preg (cfg);
+               ins->inst_offset = offset;
+               return ins;
+       }
 #endif
+       return NULL;
 }
 
 MonoInst*
@@ -2730,7 +2863,7 @@ mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code,
                MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
 
                if (mono_class_field_is_special_static (patch_info->data.field)) {
-                       gpointer addr;
+                       gpointer addr = NULL;
 
                        mono_domain_lock (domain);
                        if (domain->special_static_fields)
@@ -2913,7 +3046,7 @@ mono_compile_create_vars (MonoCompile *cfg)
        MonoMethodHeader *header;
        int i;
 
-       header = mono_method_get_header (cfg->method);
+       header = cfg->header;
 
        sig = mono_method_signature (cfg->method);
        
@@ -3226,6 +3359,7 @@ mono_codegen (MonoCompile *cfg)
                bb->native_offset = cfg->code_len;
                //if ((bb == cfg->bb_entry) || !(bb->region == -1 && !bb->dfn))
                        mono_arch_output_basic_block (cfg, bb);
+               bb->native_length = cfg->code_len - bb->native_offset;
 
                if (bb == cfg->bb_exit) {
                        cfg->epilog_begin = cfg->code_len;
@@ -3273,7 +3407,17 @@ mono_codegen (MonoCompile *cfg)
        }
 
        memcpy (code, cfg->native_code, cfg->code_len);
+#ifdef __native_client_codegen__
+       if (cfg->native_code_alloc) {
+               g_free (cfg->native_code_alloc);
+               cfg->native_code_alloc = 0;
+       }
+       else if (cfg->native_code) {
+               g_free (cfg->native_code);
+       }
+#else
        g_free (cfg->native_code);
+#endif
        cfg->native_code = code;
        code = cfg->native_code + cfg->code_len;
   
@@ -3312,6 +3456,10 @@ if (valgrind_register){
        mono_arch_save_unwind_info (cfg);
 #endif
        
+#ifdef __native_client_codegen__
+       mono_nacl_fix_patches (cfg->native_code, cfg->patch_info);
+#endif
+
        mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
 
        if (cfg->method->dynamic) {
@@ -3345,18 +3493,39 @@ compute_reachable (MonoBasicBlock *bb)
 static MonoJitInfo*
 create_jit_info (MonoCompile *cfg, MonoMethod *method_to_compile)
 {
+       GSList *tmp;
        MonoMethodHeader *header;
        MonoJitInfo *jinfo;
        int num_clauses;
        int generic_info_size;
+       int holes_size = 0, num_holes = 0;
 
-       header = mono_method_get_header (method_to_compile);
+       g_assert (method_to_compile == cfg->method);
+       header = cfg->header;
 
        if (cfg->generic_sharing_context)
                generic_info_size = sizeof (MonoGenericJitInfo);
        else
                generic_info_size = 0;
 
+       if (cfg->try_block_holes) {
+               for (tmp = cfg->try_block_holes; tmp; tmp = tmp->next) {
+                       TryBlockHole *hole = tmp->data;
+                       MonoExceptionClause *ec = hole->clause;
+                       int hole_end = hole->basic_block->native_offset + hole->basic_block->native_length;
+                       MonoBasicBlock *clause_last_bb = cfg->cil_offset_to_bb [ec->try_offset + ec->try_len];
+                       g_assert (clause_last_bb);
+
+                       /* Holes at the end of a try region can be represented by simply reducing the size of the block itself.*/
+                       if (clause_last_bb->native_offset != hole_end)
+                               ++num_holes;
+               }
+               if (num_holes)
+                       holes_size = sizeof (MonoTryBlockHoleTableJitInfo) + num_holes * sizeof (MonoTryBlockHoleJitInfo);
+               if (G_UNLIKELY (cfg->verbose_level >= 4))
+                       printf ("Number of try block holes %d\n", num_holes);
+       }
+
        if (COMPILE_LLVM (cfg))
                num_clauses = cfg->llvm_ex_info_len;
        else
@@ -3364,11 +3533,11 @@ create_jit_info (MonoCompile *cfg, MonoMethod *method_to_compile)
 
        if (cfg->method->dynamic) {
                jinfo = g_malloc0 (MONO_SIZEOF_JIT_INFO + (num_clauses * sizeof (MonoJitExceptionInfo)) +
-                               generic_info_size);
+                               generic_info_size + holes_size);
        } else {
                jinfo = mono_domain_alloc0 (cfg->domain, MONO_SIZEOF_JIT_INFO +
                                (num_clauses * sizeof (MonoJitExceptionInfo)) +
-                               generic_info_size);
+                               generic_info_size + holes_size);
        }
 
        jinfo->method = cfg->method_to_register;
@@ -3404,12 +3573,18 @@ create_jit_info (MonoCompile *cfg, MonoMethod *method_to_compile)
                                mini_method_get_context (method_to_compile)->method_inst ||
                                method_to_compile->klass->valuetype) {
                        inst = cfg->rgctx_var;
-                       g_assert (inst->opcode == OP_REGOFFSET);
+                       if (!COMPILE_LLVM (cfg))
+                               g_assert (inst->opcode == OP_REGOFFSET);
                } else {
                        inst = cfg->args [0];
                }
 
-               if (inst->opcode == OP_REGVAR) {
+               if (COMPILE_LLVM (cfg)) {
+                       g_assert (cfg->llvm_this_reg != -1);
+                       gi->this_in_reg = 0;
+                       gi->this_reg = cfg->llvm_this_reg;
+                       gi->this_offset = cfg->llvm_this_offset;
+               } else if (inst->opcode == OP_REGVAR) {
                        gi->this_in_reg = 1;
                        gi->this_reg = inst->dreg;
                } else {
@@ -3427,6 +3602,39 @@ create_jit_info (MonoCompile *cfg, MonoMethod *method_to_compile)
                }
        }
 
+       if (num_holes) {
+               MonoTryBlockHoleTableJitInfo *table;
+               int i;
+
+               jinfo->has_try_block_holes = 1;
+               table = mono_jit_info_get_try_block_hole_table_info (jinfo);
+               table->num_holes = (guint16)num_holes;
+               i = 0;
+               for (tmp = cfg->try_block_holes; tmp; tmp = tmp->next) {
+                       guint32 start_bb_offset;
+                       MonoTryBlockHoleJitInfo *hole;
+                       TryBlockHole *hole_data = tmp->data;
+                       MonoExceptionClause *ec = hole_data->clause;
+                       int hole_end = hole_data->basic_block->native_offset + hole_data->basic_block->native_length;
+                       MonoBasicBlock *clause_last_bb = cfg->cil_offset_to_bb [ec->try_offset + ec->try_len];
+                       g_assert (clause_last_bb);
+
+                       /* Holes at the end of a try region can be represented by simply reducing the size of the block itself.*/
+                       if (clause_last_bb->native_offset == hole_end)
+                               continue;
+
+                       start_bb_offset = hole_data->start_offset - hole_data->basic_block->native_offset;
+                       hole = &table->holes [i++];
+                       hole->clause = hole_data->clause - &header->clauses [0];
+                       hole->offset = (guint32)hole_data->start_offset;
+                       hole->length = (guint16)(hole_data->basic_block->native_length - start_bb_offset);
+
+                       if (G_UNLIKELY (cfg->verbose_level >= 4))
+                               printf ("\tTry block hole at eh clause %d offset %x length %x\n", hole->clause, hole->offset, hole->length);
+               }
+               g_assert (i == num_holes);
+       }
+
        if (COMPILE_LLVM (cfg)) {
                if (num_clauses)
                        memcpy (&jinfo->clauses [0], &cfg->llvm_ex_info [0], num_clauses * sizeof (MonoJitExceptionInfo));
@@ -3463,6 +3671,42 @@ create_jit_info (MonoCompile *cfg, MonoMethod *method_to_compile)
                        tblock = cfg->cil_offset_to_bb [ec->handler_offset];
                        g_assert (tblock);
                        ei->handler_start = cfg->native_code + tblock->native_offset;
+
+                       for (tmp = cfg->try_block_holes; tmp; tmp = tmp->next) {
+                               TryBlockHole *hole = tmp->data;
+                               gpointer hole_end = cfg->native_code + (hole->basic_block->native_offset + hole->basic_block->native_length);
+                               if (hole->clause == ec && hole_end == ei->try_end) {
+                                       if (G_UNLIKELY (cfg->verbose_level >= 4))
+                                               printf ("\tShortening try block %d from %x to %x\n", i, (int)((guint8*)ei->try_end - cfg->native_code), hole->start_offset);
+
+                                       ei->try_end = cfg->native_code + hole->start_offset;
+                                       break;
+                               }
+                       }
+
+                       if (ec->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
+                               int end_offset;
+                               if (ec->handler_offset + ec->handler_len < header->code_size) {
+                                       tblock = cfg->cil_offset_to_bb [ec->handler_offset + ec->handler_len];
+                                       g_assert (tblock);
+                                       end_offset = tblock->native_offset;
+                               } else {
+                                       end_offset = cfg->epilog_begin;
+                               }
+                               ei->data.handler_end = cfg->native_code + end_offset;
+                       }
+               }
+       }
+
+       if (G_UNLIKELY (cfg->verbose_level >= 4)) {
+               int i;
+               for (i = 0; i < jinfo->num_clauses; i++) {
+                       MonoJitExceptionInfo *ei = &jinfo->clauses [i];
+                       int start = (guint8*)ei->try_start - cfg->native_code;
+                       int end = (guint8*)ei->try_end - cfg->native_code;
+                       int handler = (guint8*)ei->handler_start - cfg->native_code;
+
+                       printf ("JitInfo EH clause %d flags %x try %x-%x handler %x\n", i, ei->flags, start, end, handler);
                }
        }
 
@@ -3485,7 +3729,80 @@ create_jit_info (MonoCompile *cfg, MonoMethod *method_to_compile)
 
        return jinfo;
 }
+#endif
+
+/*
+ * mini_get_shared_method:
+ *
+ *   Return the method which is actually compiled/registered when doing generic sharing.
+ */
+MonoMethod*
+mini_get_shared_method (MonoMethod *method)
+{
+       MonoGenericContext shared_context;
+       MonoMethod *declaring_method, *res;
+       int i;
+       gboolean partial = FALSE;
+
+       if (method->is_generic || method->klass->generic_container)
+               declaring_method = method;
+       else
+               declaring_method = mono_method_get_declaring_generic_method (method);
+
+       if (declaring_method->is_generic)
+               shared_context = mono_method_get_generic_container (declaring_method)->context;
+       else
+               shared_context = declaring_method->klass->generic_container->context;
+
+       /* Handle partial sharing */
+       if (method != declaring_method && method->is_inflated && !mono_method_is_generic_sharable_impl_full (method, FALSE, FALSE)) {
+               MonoGenericContext *context = mono_method_get_context (method);
+               MonoGenericInst *inst;
+               MonoType **type_argv;
+
+               /* 
+                * Create the shared context by replacing the ref type arguments with
+                * type parameters, and keeping the rest.
+                */
+               partial = TRUE;
+               inst = context->class_inst;
+               if (inst) {
+                       type_argv = g_new0 (MonoType*, inst->type_argc);
+                       for (i = 0; i < inst->type_argc; ++i) {
+                               if (MONO_TYPE_IS_REFERENCE (inst->type_argv [i]) || inst->type_argv [i]->type == MONO_TYPE_VAR || inst->type_argv [i]->type == MONO_TYPE_MVAR)
+                                       type_argv [i] = shared_context.class_inst->type_argv [i];
+                               else
+                                       type_argv [i] = inst->type_argv [i];
+                       }
+
+                       shared_context.class_inst = mono_metadata_get_generic_inst (inst->type_argc, type_argv);
+                       g_free (type_argv);
+               }
+
+               inst = context->method_inst;
+               if (inst) {
+                       type_argv = g_new0 (MonoType*, inst->type_argc);
+                       for (i = 0; i < inst->type_argc; ++i) {
+                               if (MONO_TYPE_IS_REFERENCE (inst->type_argv [i]) || inst->type_argv [i]->type == MONO_TYPE_VAR || inst->type_argv [i]->type == MONO_TYPE_MVAR)
+                                       type_argv [i] = shared_context.method_inst->type_argv [i];
+                               else
+                                       type_argv [i] = inst->type_argv [i];
+                       }
+
+                       shared_context.method_inst = mono_metadata_get_generic_inst (inst->type_argc, type_argv);
+                       g_free (type_argv);
+               }
+       }
+
+    res = mono_class_inflate_generic_method (declaring_method, &shared_context);
+       if (!partial) {
+               /* The result should be an inflated method whose parent is not inflated */
+               g_assert (!res->klass->is_inflated);
+       }
+       return res;
+}
 
+#ifndef DISABLE_JIT
 /*
  * mini_method_compile:
  * @method: the method to compile
@@ -3502,11 +3819,13 @@ MonoCompile*
 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, gboolean compile_aot, int parts)
 {
        MonoMethodHeader *header;
+       MonoMethodSignature *sig;
+       MonoError err;
        guint8 *ip;
        MonoCompile *cfg;
        int dfn, i, code_size_ratio;
        gboolean deadce_has_run = FALSE;
-       gboolean try_generic_shared, try_llvm;
+       gboolean try_generic_shared, try_llvm = FALSE;
        MonoMethod *method_to_compile, *method_to_register;
 
        mono_jit_stats.methods_compiled++;
@@ -3516,9 +3835,13 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
                MONO_PROBE_METHOD_COMPILE_BEGIN (method);
  
        if (compile_aot)
-               /* We are passed the original generic method definition */
+               /* 
+                * We might get passed the original generic method definition or
+                * instances with type parameters.
+                * FIXME: Remove the method->klass->generic_class limitation.
+                */
                try_generic_shared = mono_class_generic_sharing_enabled (method->klass) &&
-                       (opts & MONO_OPT_GSHARED) && (method->is_generic || method->klass->generic_container);
+                       (opts & MONO_OPT_GSHARED) && ((method->is_generic || method->klass->generic_container) || (!method->klass->generic_class && mono_method_is_generic_sharable_impl (method, TRUE)));
        else
                try_generic_shared = mono_class_generic_sharing_enabled (method->klass) &&
                        (opts & MONO_OPT_GSHARED) && mono_method_is_generic_sharable_impl (method, FALSE);
@@ -3530,33 +3853,13 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
                        mono_stats.generics_unsharable_methods++;
        }
 
-       try_llvm = TRUE;
-
-#ifndef ENABLE_LLVM
-       try_llvm = FALSE;
+#ifdef ENABLE_LLVM
+       try_llvm = mono_use_llvm;
 #endif
 
  restart_compile:
        if (try_generic_shared) {
-               MonoMethod *declaring_method;
-               MonoGenericContext *shared_context;
-
-               if (compile_aot) {
-                       declaring_method = method;
-               } else {
-                       declaring_method = mono_method_get_declaring_generic_method (method);
-                       if (method->klass->generic_class)
-                               g_assert (method->klass->generic_class->container_class == declaring_method->klass);
-                       else
-                               g_assert (method->klass == declaring_method->klass);
-               }
-
-               if (declaring_method->is_generic)
-                       shared_context = &(mono_method_get_generic_container (declaring_method)->context);
-               else
-                       shared_context = &declaring_method->klass->generic_container->context;
-
-               method_to_compile = mono_class_inflate_generic_method (declaring_method, shared_context);
+               method_to_compile = mini_get_shared_method (method);
                g_assert (method_to_compile);
        } else {
                method_to_compile = method;
@@ -3564,6 +3867,7 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
 
        cfg = g_new0 (MonoCompile, 1);
        cfg->method = method_to_compile;
+       cfg->header = mono_method_get_header (cfg->method);
        cfg->mempool = mono_mempool_new ();
        cfg->opt = opts;
        cfg->prof_options = mono_profiler_get_events ();
@@ -3589,35 +3893,25 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
        }
 
        if (cfg->generic_sharing_context) {
-               MonoGenericContext object_context = mono_method_construct_object_context (method_to_compile);
-
-               method_to_register = mono_class_inflate_generic_method (method_to_compile, &object_context);
+               method_to_register = method_to_compile;
        } else {
                g_assert (method == method_to_compile);
                method_to_register = method;
        }
        cfg->method_to_register = method_to_register;
 
-       if (cfg->compile_llvm) {
-               /* No way to obtain the location info for 'this' */
-               if (try_generic_shared) {
-                       cfg->exception_message = g_strdup ("gshared");
-                       cfg->disable_llvm = TRUE;
-               }
-
-               if (cfg->method->save_lmf) {
-                       cfg->exception_message = g_strdup ("lmf");
-                       cfg->disable_llvm = TRUE;
-               }
-
-               /* FIXME: */
-               if (cfg->method->dynamic) {
-                       cfg->exception_message = g_strdup ("dynamic.");
-                       cfg->disable_llvm = TRUE;
-               }
+       mono_error_init (&err);
+       sig = mono_method_signature_checked (cfg->method, &err);        
+       if (!sig) {
+               cfg->exception_type = MONO_EXCEPTION_TYPE_LOAD;
+               cfg->exception_message = g_strdup (mono_error_get_message (&err));
+               mono_error_cleanup (&err);
+               if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
+                       MONO_PROBE_METHOD_COMPILE_END (method, FALSE);
+               return cfg;
        }
 
-       header = mono_method_get_header (method_to_compile);
+       header = cfg->header;
        if (!header) {
                MonoLoaderError *error;
 
@@ -3632,40 +3926,30 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
                return cfg;
        }
 
-       if (FALSE && header->clauses) {
-               /* 
-                * Cannot be enabled until LLVM supports implicit exceptions, or we use
-                * explicit checks, or we disable this for methods which might throw implicit
-                * exceptions inside clauses.
-                */
-               cfg->exception_message = g_strdup ("clauses");
-               cfg->disable_llvm = TRUE;
-       }
-
 #ifdef ENABLE_LLVM
        {
                static gboolean inited;
 
                if (!inited) {
-                       mono_counters_register ("Methods JITted using LLVM", MONO_COUNTER_JIT | MONO_COUNTER_INT, &methods_with_llvm);  
-                       mono_counters_register ("Methods JITted using mono JIT", MONO_COUNTER_JIT | MONO_COUNTER_INT, &methods_without_llvm);
                        inited = TRUE;
                }
-       
+
                /* 
                 * Check for methods which cannot be compiled by LLVM early, to avoid
                 * the extra compilation pass.
                 */
-               if (COMPILE_LLVM (cfg) && cfg->disable_llvm) {
-                       if (cfg->verbose_level >= 1) {
-                               //nm = mono_method_full_name (cfg->method, TRUE);
-                               printf ("LLVM failed for '%s': %s\n", method->name, cfg->exception_message);
-                               //g_free (nm);
+               if (COMPILE_LLVM (cfg)) {
+                       mono_llvm_check_method_supported (cfg);
+                       if (cfg->disable_llvm) {
+                               if (cfg->verbose_level >= 1) {
+                                       //nm = mono_method_full_name (cfg->method, TRUE);
+                                       printf ("LLVM failed for '%s': %s\n", method->name, cfg->exception_message);
+                                       //g_free (nm);
+                               }
+                               mono_destroy_compile (cfg);
+                               try_llvm = FALSE;
+                               goto restart_compile;
                        }
-                       InterlockedIncrement (&methods_without_llvm);
-                       mono_destroy_compile (cfg);
-                       try_llvm = FALSE;
-                       goto restart_compile;
                }
        }
 #endif
@@ -3716,7 +4000,7 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
                cfg->compute_precise_live_ranges = TRUE;
        }
 
-       mini_gc_init_gc_map (cfg);
+       mini_gc_init_cfg (cfg);
 
        if (COMPILE_LLVM (cfg)) {
                cfg->opt |= MONO_OPT_ABCREM;
@@ -3725,7 +4009,7 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
        if (getenv ("MONO_VERBOSE_METHOD")) {
                char *name = getenv ("MONO_VERBOSE_METHOD");
 
-               if (strchr (name, '.') || strchr (name, ':')) {
+               if ((strchr (name, '.') > name) || strchr (name, ':')) {
                        MonoMethodDesc *desc;
                        
                        desc = mono_method_desc_new (name, TRUE);
@@ -3747,7 +4031,7 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
                if (COMPILE_LLVM (cfg))
                        g_print ("converting llvm method %s\n", mono_method_full_name (method, TRUE));
                else if (cfg->generic_sharing_context)
-                       g_print ("converting shared method %s\n", mono_method_full_name (method, TRUE));
+                       g_print ("converting shared method %s\n", mono_method_full_name (method_to_compile, TRUE));
                else
                        g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
        }
@@ -4063,7 +4347,8 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
 #endif
 
        if (cfg->comp_done & MONO_COMP_SSA && COMPILE_LLVM (cfg)) {
-               if ((cfg->flags & (MONO_CFG_HAS_LDELEMA|MONO_CFG_HAS_CHECK_THIS)) && (cfg->opt & MONO_OPT_ABCREM))
+               /* This removes MONO_INST_FAULT flags too so perform it unconditionally */
+               if (cfg->opt & MONO_OPT_ABCREM)
                        mono_perform_abc_removal (cfg);
        }
 
@@ -4084,7 +4369,10 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
                mono_decompose_array_access_opts (cfg);
 
        if (cfg->got_var) {
+#ifndef MONO_ARCH_GOT_REG
                GList *regs;
+#endif
+               int got_reg;
 
                g_assert (cfg->got_var_allocated);
 
@@ -4095,13 +4383,17 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
                 * branches problem. Testcase: mcs crash in 
                 * System.MonoCustomAttrs:GetCustomAttributes.
                 */
+#ifdef MONO_ARCH_GOT_REG
+               got_reg = MONO_ARCH_GOT_REG;
+#else
                regs = mono_arch_get_global_int_regs (cfg);
                g_assert (regs);
+               got_reg = GPOINTER_TO_INT (regs->data);
+               g_list_free (regs);
+#endif
                cfg->got_var->opcode = OP_REGVAR;
-               cfg->got_var->dreg = GPOINTER_TO_INT (regs->data);
+               cfg->got_var->dreg = got_reg;
                cfg->used_int_regs |= 1LL << cfg->got_var->dreg;
-               
-               g_list_free (regs);
        }
 
        /*
@@ -4120,7 +4412,7 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
        }
 
        if ((cfg->opt & MONO_OPT_LINEARS) && !cfg->globalra) {
-               GList *vars, *regs;
+               GList *vars, *regs, *l;
                
                /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
                cfg->comp_done &= ~MONO_COMP_LIVENESS;
@@ -4129,8 +4421,15 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
 
                if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
                        regs = mono_arch_get_global_int_regs (cfg);
-                       if (cfg->got_var)
-                               regs = g_list_delete_link (regs, regs);
+                       /* Remove the reg reserved for holding the GOT address */
+                       if (cfg->got_var) {
+                               for (l = regs; l; l = l->next) {
+                                       if (GPOINTER_TO_UINT (l->data) == cfg->got_var->dreg) {
+                                               regs = g_list_delete_link (regs, l);
+                                               break;
+                                       }
+                               }
+                       }
                        mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
                }
        }
@@ -4222,14 +4521,11 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
                                printf ("LLVM failed for '%s': %s\n", method->name, cfg->exception_message);
                                //g_free (nm);
                        }
-                       InterlockedIncrement (&methods_without_llvm);
                        mono_destroy_compile (cfg);
                        try_llvm = FALSE;
                        goto restart_compile;
                }
 
-               InterlockedIncrement (&methods_with_llvm);
-
                if (cfg->verbose_level > 0 && !cfg->compile_aot) {
                        nm = mono_method_full_name (cfg->method, TRUE);
                        g_print ("LLVM Method %s emitted at %p to %p (code length %d) [%s]\n", 
@@ -4242,6 +4538,11 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
                mono_codegen (cfg);
        }
 
+       if (COMPILE_LLVM (cfg))
+               InterlockedIncrement (&mono_jit_stats.methods_with_llvm);
+       else
+               InterlockedIncrement (&mono_jit_stats.methods_without_llvm);
+
        if (cfg->verbose_level >= 2) {
                char *id =  mono_method_full_name (cfg->method, FALSE);
                mono_disassemble_code (cfg, cfg->native_code, cfg->code_len, id + 3);
@@ -4283,7 +4584,7 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
                g_free (mono_jit_stats.biggest_method);
                mono_jit_stats.biggest_method = g_strdup_printf ("%s::%s)", method->klass->name, method->name);
        }
-       code_size_ratio = (code_size_ratio * 100) / mono_method_get_header (method)->code_size;
+       code_size_ratio = (code_size_ratio * 100) / header->code_size;
        if (code_size_ratio > mono_jit_stats.max_code_size_ratio && mono_jit_stats.enabled) {
                mono_jit_stats.max_code_size_ratio = code_size_ratio;
                g_free (mono_jit_stats.max_ratio_method);
@@ -4308,17 +4609,29 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
 
 #endif /* DISABLE_JIT */
 
-static MonoJitInfo*
-lookup_generic_method (MonoDomain *domain, MonoMethod *method)
+MonoJitInfo*
+mono_domain_lookup_shared_generic (MonoDomain *domain, MonoMethod *method)
 {
-       MonoMethod *open_method;
+       static gboolean inited = FALSE;
+       static int lookups = 0;
+       static int failed_lookups = 0;
+       MonoJitInfo *ji;
 
-       if (!mono_method_is_generic_sharable_impl (method, FALSE))
-               return NULL;
+       ji = mono_internal_hash_table_lookup (&domain->jit_code_hash, mini_get_shared_method (method));
+       if (ji && !ji->has_generic_jit_info)
+               ji = NULL;
 
-       open_method = mono_method_get_declaring_generic_method (method);
+       if (!inited) {
+               mono_counters_register ("Shared generic lookups", MONO_COUNTER_INT|MONO_COUNTER_GENERICS, &lookups);
+               mono_counters_register ("Failed shared generic lookups", MONO_COUNTER_INT|MONO_COUNTER_GENERICS, &failed_lookups);
+               inited = TRUE;
+       }
+
+       ++lookups;
+       if (!ji)
+               ++failed_lookups;
 
-       return mono_domain_lookup_shared_generic (domain, open_method);
+       return ji;
 }
 
 /*
@@ -4332,7 +4645,9 @@ lookup_method_inner (MonoDomain *domain, MonoMethod *method)
        if (ji)
                return ji;
 
-       return lookup_generic_method (domain, method);
+       if (!mono_method_is_generic_sharable_impl (method, FALSE))
+               return NULL;
+       return mono_domain_lookup_shared_generic (domain, method);
 }
 
 static MonoJitInfo*
@@ -4349,6 +4664,38 @@ lookup_method (MonoDomain *domain, MonoMethod *method)
        return info;
 }
 
+#if ENABLE_JIT_MAP
+static FILE* perf_map_file = NULL;
+
+void
+mono_enable_jit_map (void)
+{
+       if (!perf_map_file) {
+               char name [64];
+               g_snprintf (name, sizeof (name), "/tmp/perf-%d.map", getpid ());
+               unlink (name);
+               perf_map_file = fopen (name, "w");
+       }
+}
+
+void
+mono_emit_jit_tramp (void *start, int size, const char *desc)
+{
+       if (perf_map_file)
+               fprintf (perf_map_file, "%llx %x %s\n", (long long unsigned int)(gsize)start, size, desc);
+}
+
+void
+mono_emit_jit_map (MonoJitInfo *jinfo)
+{
+       if (perf_map_file) {
+               char *name = mono_method_full_name (jinfo->method, TRUE);
+               mono_emit_jit_tramp (jinfo->code_start, jinfo->code_size, name);
+               g_free (name);
+       }
+}
+#endif
+
 static gpointer
 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, int opt, MonoException **jit_ex)
 {
@@ -4399,6 +4746,7 @@ mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, in
                //mono_debug_add_wrapper (method, nm);
        } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
                const char *name = method->name;
+               char *full_name, *msg;
                MonoMethod *nm;
 
                if (method->klass->parent == mono_defaults.multicastdelegate_class) {
@@ -4431,6 +4779,12 @@ mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, in
                                return mono_get_addr_from_ftnptr (mono_compile_method (nm));
                        }
                }
+
+               full_name = mono_method_full_name (method, TRUE);
+               msg = g_strdup_printf ("Unrecognizable runtime implemented method '%s'", full_name);
+               *jit_ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", msg);
+               g_free (full_name);
+               g_free (msg);
                return NULL;
        }
 
@@ -4560,6 +4914,7 @@ mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, in
 
        mono_destroy_compile (cfg);
 
+#ifndef DISABLE_JIT
        if (domain_jit_info (target_domain)->jump_target_hash) {
                MonoJumpInfo patch_info;
                GSList *list, *tmp;
@@ -4576,26 +4931,36 @@ mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, in
                g_slist_free (list);
        }
 
+       mono_emit_jit_map (jinfo);
+#endif
        mono_domain_unlock (target_domain);
        mono_loader_unlock ();
 
        vtable = mono_class_vtable (target_domain, method->klass);
        if (!vtable) {
-               MonoException *exc;
-               exc = mono_class_get_exception_for_failure (method->klass);
-               g_assert (exc);
-               mono_raise_exception (exc);
+               ex = mono_class_get_exception_for_failure (method->klass);
+               g_assert (ex);
+               *jit_ex = ex;
+               return NULL;
        }
 
        if (prof_options & MONO_PROFILE_JIT_COMPILATION) {
-               if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
-                       /* The profiler doesn't know about wrappers, so pass the original icall method */
-                       mono_profiler_method_end_jit (mono_marshal_method_from_wrapper (method), jinfo, MONO_PROFILE_OK);
-               else
+               if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
+                       if (mono_marshal_method_from_wrapper (method)) {
+                               /* Native func wrappers have no method */
+                               /* The profiler doesn't know about wrappers, so pass the original icall method */
+                               mono_profiler_method_end_jit (mono_marshal_method_from_wrapper (method), jinfo, MONO_PROFILE_OK);
+                       }
+               } else {
                        mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
+               }
        }
 
-       mono_runtime_class_init (vtable);
+       ex = mono_runtime_class_init_full (vtable, FALSE);
+       if (ex) {
+               *jit_ex = ex;
+               return NULL;
+       }
        return code;
 }
 
@@ -4947,7 +5312,17 @@ mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObjec
         * We need this here because mono_marshal_get_runtime_invoke can place 
         * the helper method in System.Object and not the target class.
         */
-       mono_runtime_class_init (info->vtable);
+       if (exc) {
+               *exc = (MonoObject*)mono_runtime_class_init_full (info->vtable, FALSE);
+               if (*exc)
+                       return NULL;
+       } else {
+               mono_runtime_class_init (info->vtable);
+       }
+
+       /* The wrappers expect this to be initialized to NULL */
+       if (exc)
+               *exc = NULL;
 
 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
        if (info->dyn_call_info) {
@@ -5040,18 +5415,19 @@ SIG_HANDLER_SIGNATURE (mono_sigill_signal_handler)
        mono_arch_handle_exception (ctx, exc, FALSE);
 }
 
+#if defined(MONO_ARCH_USE_SIGACTION) || defined(HOST_WIN32)
+#define HAVE_SIG_INFO
+#endif
+
 void
 SIG_HANDLER_SIGNATURE (mono_sigsegv_signal_handler)
 {
-#ifndef MONO_ARCH_SIGSEGV_ON_ALTSTACK
-       MonoException *exc = NULL;
-#endif
        MonoJitInfo *ji;
        MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
 
        GET_CONTEXT;
 
-#ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
+#if defined(MONO_ARCH_SOFT_DEBUG_SUPPORTED) && defined(HAVE_SIG_INFO)
        if (mono_arch_is_single_step_event (info, ctx)) {
                mono_debugger_agent_single_step_event (ctx);
                return;
@@ -5061,7 +5437,7 @@ SIG_HANDLER_SIGNATURE (mono_sigsegv_signal_handler)
        }
 #endif
 
-#ifndef HOST_WIN32
+#if !defined(HOST_WIN32) && defined(HAVE_SIG_INFO)
        if (mono_aot_is_pagefault (info->si_addr)) {
                mono_aot_handle_pagefault (info->si_addr);
                return;
@@ -5111,7 +5487,7 @@ SIG_HANDLER_SIGNATURE (mono_sigsegv_signal_handler)
                mono_handle_native_sigsegv (SIGSEGV, ctx);
        }
                        
-       mono_arch_handle_exception (ctx, exc, FALSE);
+       mono_arch_handle_exception (ctx, NULL, FALSE);
 #endif
 }
 
@@ -5155,25 +5531,45 @@ mono_jit_create_remoting_trampoline (MonoDomain *domain, MonoMethod *method, Mon
        return mono_get_addr_from_ftnptr (addr);
 }
 
-#ifdef MONO_ARCH_HAVE_IMT
-static G_GNUC_UNUSED gpointer
-mini_get_imt_trampoline (void)
-{
-       static gpointer tramp = NULL;
-       if (!tramp)
-               tramp = mono_create_specific_trampoline (MONO_FAKE_IMT_METHOD, MONO_TRAMPOLINE_JIT, mono_get_root_domain (), NULL);
-       return tramp;
-}
-#endif
+static gpointer *vtable_trampolines;
+static int vtable_trampolines_size;
 
 gpointer
 mini_get_vtable_trampoline (int slot_index)
 {
-       static gpointer tramp = NULL;
+       int index = slot_index + MONO_IMT_SIZE;
 
-       if (!tramp)
-               tramp = mono_create_specific_trampoline (MONO_FAKE_VTABLE_METHOD, MONO_TRAMPOLINE_JIT, mono_get_root_domain (), NULL);
-       return tramp;
+       g_assert (slot_index >= - MONO_IMT_SIZE);
+       if (!vtable_trampolines || slot_index + MONO_IMT_SIZE >= vtable_trampolines_size) {
+               mono_jit_lock ();
+               if (!vtable_trampolines || index >= vtable_trampolines_size) {
+                       int new_size;
+                       gpointer new_table;
+
+                       new_size = vtable_trampolines_size ? vtable_trampolines_size * 2 : 128;
+                       while (new_size <= index)
+                               new_size *= 2;
+                       new_table = g_new0 (gpointer, new_size);
+
+                       if (vtable_trampolines)
+                               memcpy (new_table, vtable_trampolines, vtable_trampolines_size * sizeof (gpointer));
+                       g_free (vtable_trampolines);
+                       mono_memory_barrier ();
+                       vtable_trampolines = new_table;
+                       vtable_trampolines_size = new_size;
+               }
+               mono_jit_unlock ();
+       }
+
+       if (!vtable_trampolines [index])
+               vtable_trampolines [index] = mono_create_specific_trampoline (GUINT_TO_POINTER (slot_index), MONO_TRAMPOLINE_VCALL, mono_get_root_domain (), NULL);
+       return vtable_trampolines [index];
+}
+
+static gpointer
+mini_get_imt_trampoline (int slot_index)
+{
+       return mini_get_vtable_trampoline (slot_index - MONO_IMT_SIZE);
 }
 
 static void
@@ -5194,6 +5590,8 @@ mini_parse_debug_options (void)
                        debug_options.handle_sigint = TRUE;
                else if (!strcmp (arg, "keep-delegates"))
                        debug_options.keep_delegates = TRUE;
+               else if (!strcmp (arg, "reverse-pinvoke-exceptions"))
+                       debug_options.reverse_pinvoke_exceptions = TRUE;
                else if (!strcmp (arg, "collect-pagefault-stats"))
                        debug_options.collect_pagefault_stats = TRUE;
                else if (!strcmp (arg, "break-on-unverified"))
@@ -5214,9 +5612,11 @@ mini_parse_debug_options (void)
                        debug_options.gen_seq_points = TRUE;
                else if (!strcmp (arg, "init-stacks"))
                        debug_options.init_stacks = TRUE;
+               else if (!strcmp (arg, "casts"))
+                       debug_options.better_cast_details = TRUE;
                else {
                        fprintf (stderr, "Invalid option for the MONO_DEBUG env variable: %s\n", arg);
-                       fprintf (stderr, "Available options: 'handle-sigint', 'keep-delegates', 'collect-pagefault-stats', 'break-on-unverified', 'no-gdb-backtrace', 'dont-free-domains', 'suspend-on-sigsegv', 'dyn-runtime-invoke', 'gdb', 'explicit-null-checks', 'init-stacks'\n");
+                       fprintf (stderr, "Available options: 'handle-sigint', 'keep-delegates', 'reverse-pinvoke-exceptions', 'collect-pagefault-stats', 'break-on-unverified', 'no-gdb-backtrace', 'dont-free-domains', 'suspend-on-sigsegv', 'dyn-runtime-invoke', 'gdb', 'explicit-null-checks', 'init-stacks'\n");
                        exit (1);
                }
        }
@@ -5267,6 +5667,15 @@ mini_get_addr_from_ftnptr (gpointer descr)
 #endif
 }      
 
+static void
+register_jit_stats (void)
+{
+       mono_counters_register ("Compiled methods", MONO_COUNTER_JIT | MONO_COUNTER_LONG, &mono_jit_stats.methods_compiled);
+       mono_counters_register ("Methods from AOT", MONO_COUNTER_JIT | MONO_COUNTER_LONG, &mono_jit_stats.methods_aot);
+       mono_counters_register ("Methods JITted using LLVM", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.methods_with_llvm);   
+       mono_counters_register ("Methods JITted using mono JIT", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.methods_without_llvm);
+}
+
 static void runtime_invoke_info_free (gpointer value);
  
 static void
@@ -5357,7 +5766,7 @@ mini_init (const char *filename, const char *runtime_version)
 
        MONO_PROBE_VES_INIT_BEGIN ();
 
-#ifdef __linux__
+#if defined(__linux__) && !defined(__native_client__)
        if (access ("/proc/self/maps", F_OK) != 0) {
                g_print ("Mono requires /proc to be mounted.\n");
                exit (1);
@@ -5376,7 +5785,7 @@ mini_init (const char *filename, const char *runtime_version)
 #endif
 
 #ifdef MONO_ARCH_HAVE_TLS_GET
-       mono_runtime_set_has_tls_get (TRUE);
+       mono_runtime_set_has_tls_get (MONO_ARCH_HAVE_TLS_GET);
 #else
        mono_runtime_set_has_tls_get (FALSE);
 #endif
@@ -5392,10 +5801,8 @@ mini_init (const char *filename, const char *runtime_version)
 
 #ifdef MONO_ARCH_HAVE_IMT
        if (mono_use_imt) {
-               if (!mono_use_llvm) {
-                       /* LLVM needs a per-method vtable trampoline */
-                       callbacks.get_vtable_trampoline = mini_get_vtable_trampoline;
-               }
+               callbacks.get_vtable_trampoline = mini_get_vtable_trampoline;
+               callbacks.get_imt_trampoline = mini_get_imt_trampoline;
        }
 #endif
 
@@ -5425,7 +5832,12 @@ mini_init (const char *filename, const char *runtime_version)
        }
 
 #ifdef ENABLE_LLVM
-       mono_llvm_init ();
+       if (!mono_llvm_load (NULL)) {
+               mono_use_llvm = FALSE;
+               fprintf (stderr, "Mono Warning: llvm support could not be loaded.\n");
+       }
+       if (mono_use_llvm)
+               mono_llvm_init ();
 #endif
 
        mono_trampolines_init ();
@@ -5433,8 +5845,6 @@ mini_init (const char *filename, const char *runtime_version)
        if (!g_thread_supported ())
                g_thread_init (NULL);
 
-       mono_gc_base_init ();
-
        mono_jit_tls_id = TlsAlloc ();
        setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
 
@@ -5466,15 +5876,7 @@ mini_init (const char *filename, const char *runtime_version)
 #ifdef JIT_TRAMPOLINES_WORK
        mono_install_compile_method (mono_jit_compile_method);
        mono_install_free_method (mono_jit_free_method);
-#ifdef MONO_ARCH_LLVM_SUPPORTED
-       if (mono_use_llvm)
-               /* The runtime currently only uses this for filling out vtables */
-               mono_install_trampoline (mono_create_llvm_vcall_trampoline);
-       else
-               mono_install_trampoline (mono_create_jit_trampoline);
-#else
        mono_install_trampoline (mono_create_jit_trampoline);
-#endif
        mono_install_jump_trampoline (mono_create_jump_trampoline);
        mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
        mono_install_delegate_trampoline (mono_create_delegate_trampoline);
@@ -5510,13 +5912,6 @@ mini_init (const char *filename, const char *runtime_version)
                        mono_install_imt_thunk_builder (mono_aot_get_imt_thunk);
                else
                        mono_install_imt_thunk_builder (mono_arch_build_imt_thunk);
-               if (!mono_use_llvm) {
-                       /* 
-                        * The imt code in mono_magic_trampoline () can't handle LLVM code. By disabling
-                        * this, we force iface calls to go through the llvm vcall trampoline.
-                        */
-                       mono_install_imt_trampoline (mini_get_imt_trampoline ());
-               }
        }
 #endif
 
@@ -5542,6 +5937,8 @@ mini_init (const char *filename, const char *runtime_version)
 
        create_helper_signature ();
 
+       register_jit_stats ();
+
 #define JIT_CALLS_WORK
 #ifdef JIT_CALLS_WORK
        /* Needs to be called here since register_jit_icall depends on it */
@@ -5558,9 +5955,6 @@ mini_init (const char *filename, const char *runtime_version)
 
        register_icall (mono_get_throw_exception (), "mono_arch_throw_exception", "void object", TRUE);
        register_icall (mono_get_rethrow_exception (), "mono_arch_rethrow_exception", "void object", TRUE);
-#ifdef MONO_ARCH_HAVE_THROW_EXCEPTION_BY_NAME
-       register_icall (mono_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name", "void ptr", TRUE); 
-#endif
 #if MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION
        register_icall (mono_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception", 
                                 "void ptr", TRUE);
@@ -5686,6 +6080,10 @@ mini_init (const char *filename, const char *runtime_version)
        register_icall (mono_isfinite, "mono_isfinite", "uint32 double", FALSE);
 #endif
 
+#ifdef COMPRESSED_INTERFACE_BITMAP
+       register_icall (mono_class_interface_match, "mono_class_interface_match", "uint32 ptr int32", TRUE);
+#endif
+
 #if SIZEOF_REGISTER == 4
        mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4, TRUE);
 #endif
@@ -5722,6 +6120,10 @@ mini_init (const char *filename, const char *runtime_version)
        register_icall (mono_array_new_2, "mono_array_new_2", "object ptr int int", FALSE);
        register_icall (mono_array_new_3, "mono_array_new_3", "object ptr int int int", FALSE);
        register_icall (mono_get_native_calli_wrapper, "mono_get_native_calli_wrapper", "ptr ptr ptr ptr", FALSE);
+       register_icall (mono_resume_unwind, "mono_resume_unwind", "void", TRUE);
+
+       register_icall (mono_gc_wbarrier_value_copy_bitmap, "mono_gc_wbarrier_value_copy_bitmap", "void ptr ptr int int", FALSE);
+
 #endif
 
        mono_generic_sharing_init ();
@@ -5762,10 +6164,7 @@ print_jit_stats (void)
 {
        if (mono_jit_stats.enabled) {
                g_print ("Mono Jit statistics\n");
-               g_print ("Compiled methods:       %ld\n", mono_jit_stats.methods_compiled);
-               g_print ("Methods from AOT:       %ld\n", mono_jit_stats.methods_aot);
                g_print ("Methods cache lookup:   %ld\n", mono_jit_stats.methods_lookups);
-               g_print ("Method trampolines:     %ld\n", mono_jit_stats.method_trampolines);
                g_print ("Basic blocks:           %ld\n", mono_jit_stats.basic_blocks);
                g_print ("Max basic blocks:       %ld\n", mono_jit_stats.max_basic_blocks);
                g_print ("Allocated vars:         %ld\n", mono_jit_stats.allocate_var);
@@ -5818,13 +6217,9 @@ print_jit_stats (void)
                g_print ("JIT info table lookups: %ld\n", mono_stats.jit_info_table_lookup_count);
 
                g_print ("Hazardous pointers:     %ld\n", mono_stats.hazardous_pointer_count);
-#ifdef HAVE_SGEN_GC
                g_print ("Minor GC collections:   %ld\n", mono_stats.minor_gc_count);
-#endif
                g_print ("Major GC collections:   %ld\n", mono_stats.major_gc_count);
-#ifdef HAVE_SGEN_GC
                g_print ("Minor GC time in msecs: %lf\n", (double)mono_stats.minor_gc_time_usecs / 1000.0);
-#endif
                g_print ("Major GC time in msecs: %lf\n", (double)mono_stats.major_gc_time_usecs / 1000.0);
                if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
                        g_print ("\nDecl security check   : %ld\n", mono_jit_stats.cas_declsec_check);
@@ -5852,6 +6247,7 @@ mini_cleanup (MonoDomain *domain)
 #endif
 
 #ifndef MONO_CROSS_COMPILE     
+       mono_runtime_shutdown ();
        /* 
         * mono_runtime_cleanup() and mono_domain_finalize () need to
         * be called early since they need the execution engine still
@@ -5879,9 +6275,12 @@ mini_cleanup (MonoDomain *domain)
        mono_debugger_cleanup ();
 
 #ifdef ENABLE_LLVM
-       mono_llvm_cleanup ();
+       if (mono_use_llvm)
+               mono_llvm_cleanup ();
 #endif
 
+       mono_aot_cleanup ();
+
        mono_trampolines_cleanup ();
 
        mono_unwind_cleanup ();
@@ -5890,6 +6289,8 @@ mini_cleanup (MonoDomain *domain)
                mono_code_manager_destroy (global_codeman);
        g_hash_table_destroy (jit_icall_name_hash);
        g_free (emul_opcode_map);
+       g_free (emul_opcode_opcodes);
+       g_free (vtable_trampolines);
 
        mono_arch_cleanup ();
 
@@ -6000,4 +6401,15 @@ mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean ena
        return mono_arch_instrument_epilog_full (cfg, func, p, enable_arguments, FALSE);
 }
 
+void
+mono_cfg_add_try_hole (MonoCompile *cfg, MonoExceptionClause *clause, guint8 *start, MonoBasicBlock *bb)
+{
+       TryBlockHole *hole = mono_mempool_alloc (cfg->mempool, sizeof (TryBlockHole));
+       hole->clause = clause;
+       hole->start_offset = start - cfg->native_code;
+       hole->basic_block = bb;
+
+       cfg->try_block_holes = g_slist_append_mempool (cfg->mempool, cfg->try_block_holes, hole);
+}
+
 #endif