Merged in rest of Native Client changes, untested
authorElijah Taylor <elijahtaylor@google.com>
Fri, 17 Dec 2010 00:26:34 +0000 (16:26 -0800)
committerElijah Taylor <elijahtaylor@google.com>
Fri, 17 Dec 2010 00:26:34 +0000 (16:26 -0800)
37 files changed:
libgc/configure.in
libgc/gc_dlopen.c
libgc/include/gc_pthread_redirects.h
libgc/include/private/gc_priv.h
libgc/include/private/gcconfig.h
libgc/include/private/pthread_stop_world.h
libgc/include/private/pthread_support.h
libgc/os_dep.c
libgc/pthread_stop_world.c
libgc/pthread_support.c
mono/mini/aot-compiler.c
mono/mini/aot-runtime.c
mono/mini/branch-opts.c
mono/mini/cpu-amd64.md
mono/mini/cpu-x86.md
mono/mini/dominators.c
mono/mini/driver.c
mono/mini/exceptions-amd64.c
mono/mini/exceptions-x86.c
mono/mini/genmdesc.pl
mono/mini/image-writer.c
mono/mini/jit-icalls.c
mono/mini/jit-icalls.h
mono/mini/method-to-ir.c
mono/mini/mini-amd64.c
mono/mini/mini-amd64.h
mono/mini/mini-ops.h
mono/mini/mini-x86.c
mono/mini/mini-x86.h
mono/mini/mini.c
mono/mini/mini.h
mono/mini/tramp-amd64.c
mono/mini/tramp-x86.c
mono/utils/mono-codeman.c
mono/utils/mono-codeman.h
mono/utils/mono-path.c
runtime/mono-wrapper.in

index c7ce110c6c2a490f4ce36c6f8a14f8f8d34a4f35..e68d7382883db2b9bbbc3aab9cab64e61f18c387 100644 (file)
@@ -94,7 +94,7 @@ case "$THREADS" in
        fi
        AC_DEFINE(THREAD_LOCAL_ALLOC)
        ;;
-     *-*-linux*)
+     *-*-linux* | *-*-nacl*)
        AC_DEFINE(GC_LINUX_THREADS)
        AC_DEFINE(_REENTRANT)
        ;;
@@ -340,6 +340,9 @@ case "$host" in
      machdep="mach_dep.lo ia64_save_regs_in_stack.lo"
        target_ia64=true
     ;;
+ *-*-nacl*)
+    AC_DEFINE(NO_EXECUTE_PERMISSION)
+    ;;
 esac
 if test x"$machdep" = x; then
 AC_MSG_RESULT($machdep)
index 4c690edcfe49c3c923722a34349aba2bb7576978..6ca9e996fb297584209c43c2dbfbc4445d1c75ea 100644 (file)
@@ -25,7 +25,7 @@
 
 #include "private/gc_priv.h"
 
-# if (defined(GC_PTHREADS) && !defined(GC_DARWIN_THREADS)) \
+# if defined(DYNAMIC_LOADING) && (defined(GC_PTHREADS) && !defined(GC_DARWIN_THREADS)) \
       || defined(GC_SOLARIS_THREADS)
 
 # if defined(dlopen) && !defined(GC_USE_LD_WRAP)
index bedcc26ac7ed202343f47092ad857676be00b6d7..520a36bf66c961af02a69a329ed48dd08ce0e664 100644 (file)
 #endif
   int GC_pthread_join(pthread_t thread, void **retval);
   int GC_pthread_detach(pthread_t thread);
+#if defined(__native_client__) || defined(NACL)
+  void GC_pthread_exit(void *status);
+# undef pthread_exit
+# define pthread_exit GC_pthread_exit
+#endif
 
 #if defined(GC_OSF1_THREADS) \
     && defined(_PTHREAD_USE_MANGLED_NAMES_) && !defined(_PTHREAD_USE_PTDNAM_)
index 2efb4732f79f725e60a7ab8db9593580df344f32..5c4c5146ba208b75fefd0a55002a176c3992780e 100644 (file)
@@ -1962,6 +1962,8 @@ void GC_err_puts GC_PROTO((GC_CONST char *s));
        /* SPARC/Linux doesn't properly define SIGPWR in <signal.h>.
         * It is aliased to SIGLOST in asm/signal.h, though.            */
 #      define SIG_SUSPEND SIGLOST
+#    elif defined(NACL)
+#      define SIG_SUSPEND 0
 #    else
        /* Linuxthreads itself uses SIGUSR1 and SIGUSR2.                        */
 #      define SIG_SUSPEND SIGPWR
index d1c46944bfecd13034bed7f817d6d5869abccf6f..8276079d91edb183ca642fe67c08459ec58d4996 100644 (file)
 # endif
 
 /* Determine the machine type: */
+# if defined(__native_client__)
+#    define NACL
+#    define I386
+#    define mach_type_known
+# endif
 # if defined(__arm__) || defined(__thumb__)
 #    define ARM32
 #    if !defined(LINUX) && !defined(NETBSD) && !defined(DARWIN)
 # endif
 
 # ifdef I386
-#   define MACH_TYPE "I386"
-#   if defined(__LP64__) || defined(_WIN64)
-#     define CPP_WORDSZ 64
-#     define ALIGNMENT 8
-#   else
+#   if defined( NACL )
+#     define MACH_TYPE "NACL"
 #     define CPP_WORDSZ 32
 #     define ALIGNMENT 4
+#   else
+#     define MACH_TYPE "I386"
+#     if defined(__LP64__) || defined(_WIN64)
+#       define CPP_WORDSZ 64
+#       define ALIGNMENT 8
+#     else
+#       define CPP_WORDSZ 32
+#       define ALIGNMENT 4
+#     endif
                        /* Appears to hold for all "32 bit" compilers   */
                        /* except Borland.  The -a4 option fixes        */
                        /* Borland.                                     */
 #        define HEAP_START DATAEND
 #      endif /* USE_MMAP */
 #   endif /* DGUX */
-
+#   ifdef NACL
+#      define OS_TYPE "NACL"
+       extern int etext[];
+#      define DATASTART ((ptr_t)((((word) (etext)) + 0xfff) & ~0xfff))
+       extern int _end[];
+#      define DATAEND (_end)
+#      ifdef STACK_GRAN
+#        undef STACK_GRAN
+#      endif /* STACK_GRAN */
+#      define STACK_GRAN 0x10000
+#      define HEURISTIC1
+#      ifdef USE_MMAP
+#        undef USE_MMAP
+#      endif
+#      ifdef USE_MUNMAP
+#        undef USE_MUNMAP
+#      endif
+#      ifdef USE_MMAP_ANON
+#        undef USE_MMAP_ANON
+#      endif
+#      ifdef USE_MMAP_FIXED
+#        undef USE_MMAP_FIXED
+#      endif
+#      define GETPAGESIZE() 65536
+#      define MAX_NACL_GC_THREADS 1024
+#   endif
 #   ifdef LINUX
 #      ifndef __GNUC__
          /* The Intel compiler doesn't like inline assembly */
 # if defined(GC_IRIX_THREADS) && !defined(IRIX5)
        --> inconsistent configuration
 # endif
-# if defined(GC_LINUX_THREADS) && !defined(LINUX)
+# if defined(GC_LINUX_THREADS) && !(defined(LINUX) || defined(NACL))
        --> inconsistent configuration
 # endif
 # if defined(GC_SOLARIS_THREADS) && !defined(SUNOS5)
index 054c7a0eacd7c14d6c0cd40e6082c4a1d547afb4..bd72739f580bb71c87c71ab39671a8f5ee6a01c2 100644 (file)
@@ -7,6 +7,15 @@ struct thread_stop_info {
                                /* last successfully handled a suspend  */
                                /* signal.                              */
     ptr_t stack_ptr;           /* Valid only when stopped.             */
+#ifdef NACL
+/* Grab NACL_GC_REG_STORAGE_SIZE pointers off the stack when going into */
+/* a syscall.  20 is more than we need, but it's an overestimate in case*/
+/* the instrumented function uses any callee saved registers, they may  */
+/* be pushed to the stack much earlier.  Also, on amd64 'push' puts 8   */
+/* bytes on the stack even though our pointers are 4 bytes.             */
+#define NACL_GC_REG_STORAGE_SIZE 20
+    ptr_t reg_storage[NACL_GC_REG_STORAGE_SIZE];
+#endif
 };
     
 #endif
index 852d3815bc61ef1c9bc1c973dc9dc184d0362fe3..dbd6dbcde8987dfba9ebf7928cf9d85e6f74ecf3 100644 (file)
@@ -92,6 +92,9 @@ typedef struct GC_Thread_Rep {
 
 # define THREAD_TABLE_SZ 128   /* Must be power of 2   */
 extern volatile GC_thread GC_threads[THREAD_TABLE_SZ];
+#ifdef NACL
+extern __thread GC_thread gc_thread_self;
+#endif
 
 extern GC_bool GC_thr_initialized;
 
index ee2e409ad42ca3b3d3973e14000330c20d716426..ecaa27c7d825318756c853bba76ca0e82d6e19ed 100644 (file)
 # include <errno.h>
 #endif
 
-#ifdef UNIX_LIKE
+#if defined( UNIX_LIKE ) || defined(NACL)
 # include <fcntl.h>
 #endif
 
@@ -618,6 +618,12 @@ void GC_enable_signals(void)
          /* longjmp implementations.  Most systems appear not to have  */
          /* a signal 32.                                               */
 #      define SIGSETMASK(old, new) (old) = sigsetmask(new)
+#   elif defined(NACL)
+       /* We don't use signals in NaCl. */
+#      define SIGSET_T int
+#      define SIG_DEL(set, signal)
+#      define SIG_FILL(set)
+#      define SIGSETMASK(old, new)
 #   else
        /* Use POSIX/SYSV interface     */
 #      define SIGSET_T sigset_t
@@ -2067,8 +2073,21 @@ void GC_remap(ptr_t start, word bytes)
       int result; 
 
       if (0 == start_addr) return;
+#ifdef NACL
+      {
+       /* NaCl doesn't expose mprotect, but mmap should work fine */
+       void * mmap_result;
+        mmap_result = mmap(start_addr, len, PROT_READ | PROT_WRITE | OPT_PROT_EXEC,
+                     MAP_PRIVATE | MAP_FIXED | OPT_MAP_ANON,
+                     zero_fd, 0/* offset */);
+        if (mmap_result != (void *)start_addr) ABORT("mmap as mprotect failed");
+        /* Fake the return value as if mprotect succeeded. */
+        result = 0;
+      }
+#else /* NACL */
       result = mprotect(start_addr, len,
                        PROT_READ | PROT_WRITE | OPT_PROT_EXEC);
+#endif /* NACL */
       if (result != 0) {
          GC_err_printf3(
                "Mprotect failed at 0x%lx (length %ld) with errno %ld\n",
index bf2faafe3d60dd4e1933b20178b950bd40723727..b1f98099d1e79e60937a2edebd00630d55df6216 100644 (file)
@@ -9,6 +9,7 @@
 #include <semaphore.h>
 #include <errno.h>
 #include <unistd.h>
+#include <sys/time.h>
 
 /* work around a dlopen issue (bug #75390), undefs to avoid warnings with redefinitions */
 #undef PACKAGE_BUGREPORT
 #include "include/libgc-mono-debugger.h"
 #endif
 
+#ifdef NACL
+int nacl_park_threads_now = 0;
+pthread_t nacl_thread_parker = -1;
+
+int nacl_thread_parked[MAX_NACL_GC_THREADS];
+int nacl_thread_used[MAX_NACL_GC_THREADS];
+int nacl_thread_parking_inited = 0;
+int nacl_num_gc_threads = 0;
+pthread_mutex_t nacl_thread_alloc_lock = PTHREAD_MUTEX_INITIALIZER;
+__thread int nacl_thread_idx = -1;
+__thread GC_thread nacl_gc_thread_self = NULL;
+#endif
+
 #if DEBUG_THREADS
 
 #ifndef NSIG
@@ -36,6 +50,7 @@
 # endif
 #endif
 
+#ifndef NACL
 void GC_print_sig_mask()
 {
     sigset_t blocked;
@@ -49,7 +64,7 @@ void GC_print_sig_mask()
     }
     GC_printf0("\n");
 }
-
+#endif /* NACL */
 #endif
 
 /* Remove the signals that we want to allow in thread stopping         */
@@ -116,6 +131,7 @@ sem_t GC_suspend_ack_sem;
 
 static void _GC_suspend_handler(int sig)
 {
+#ifndef NACL
     int dummy;
     pthread_t my_thread = pthread_self();
     GC_thread me;
@@ -185,6 +201,8 @@ static void _GC_suspend_handler(int sig)
 #if DEBUG_THREADS
     GC_printf1("Continuing 0x%lx\n", my_thread);
 #endif
+
+#endif /* NACL */
 }
 
 void GC_suspend_handler(int sig)
@@ -278,6 +296,10 @@ static void pthread_push_all_stacks()
 #       else
           GC_push_all_stack(lo, hi);
 #      endif
+#       ifdef NACL
+         /* Push reg_storage as roots, this will cover the reg context */
+          GC_push_all_stack(p -> stop_info.reg_storage, p -> stop_info.reg_storage + NACL_GC_REG_STORAGE_SIZE);
+#       endif
 #      ifdef IA64
 #         if DEBUG_THREADS
             GC_printf3("Reg stack for thread 0x%lx = [%lx,%lx)\n",
@@ -337,6 +359,7 @@ int android_thread_kill(pid_t tid, int sig)
 /* were sent. */
 int GC_suspend_all()
 {
+#ifndef NACL
     int n_live_threads = 0;
     int i;
     GC_thread p;
@@ -375,11 +398,15 @@ int GC_suspend_all()
       }
     }
     return n_live_threads;
+#else /* NACL */
+    return 0;
+#endif
 }
 
 /* Caller holds allocation lock.       */
 static void pthread_stop_world()
 {
+#ifndef NACL
     int i;
     int n_live_threads;
     int code;
@@ -431,8 +458,128 @@ static void pthread_stop_world()
       GC_printf1("World stopped from 0x%lx\n", pthread_self());
     #endif
     GC_stopping_thread = 0;  /* debugging only */
+#else /* NACL */
+    GC_thread p;
+    int i;
+
+    #if DEBUG_THREADS
+    GC_printf1("pthread_stop_world: num_threads %d\n", nacl_num_gc_threads - 1);
+    #endif
+    nacl_thread_parker = pthread_self();
+    nacl_park_threads_now = 1;
+    
+    while (1) {
+       #define NACL_PARK_WAIT_NANOSECONDS 100000
+        int num_threads_parked = 0;
+        struct timespec ts;
+        int num_used = 0;
+       /* Check the 'parked' flag for each thread the GC knows about */
+        for (i = 0; i < MAX_NACL_GC_THREADS && num_used < nacl_num_gc_threads; i++) {
+            if (nacl_thread_used[i] == 1) {
+                num_used++;
+                if (nacl_thread_parked[i] == 1) {
+                    num_threads_parked++;
+                }
+            }
+        }
+       /* -1 for the current thread */
+        if (num_threads_parked >= nacl_num_gc_threads - 1)
+            break;
+        ts.tv_sec = 0;
+        ts.tv_nsec = NACL_PARK_WAIT_NANOSECONDS;
+        #if DEBUG_THREADS
+        GC_printf1("sleeping waiting for %d threads to park...\n", nacl_num_gc_threads - num_threads_parked - 1);
+        #endif
+        nanosleep(&ts, 0);
+    }
+
+#endif /* NACL */
 }
 
+
+#ifdef NACL
+
+#if __x86_64__
+
+#define NACL_STORE_REGS()  \
+    do {                  \
+       asm("push %rbx");\
+       asm("push %rbp");\
+       asm("push %r12");\
+       asm("push %r13");\
+       asm("push %r14");\
+       asm("push %r15");\
+       asm("mov %%esp, %0" : "=m" (nacl_gc_thread_self->stop_info.stack_ptr));\
+        memcpy(nacl_gc_thread_self->stop_info.reg_storage, nacl_gc_thread_self->stop_info.stack_ptr, NACL_GC_REG_STORAGE_SIZE * sizeof(ptr_t));\
+       asm("add $48, %esp");\
+        asm("add %r15, %rsp");\
+    } while (0)
+
+#elif __i386__
+
+#define NACL_STORE_REGS()  \
+    do {                  \
+       asm("push %ebx");\
+       asm("push %ebp");\
+       asm("push %esi");\
+       asm("push %edi");\
+       asm("mov %%esp, %0" : "=m" (nacl_gc_thread_self->stop_info.stack_ptr));\
+        memcpy(nacl_gc_thread_self->stop_info.reg_storage, nacl_gc_thread_self->stop_info.stack_ptr, NACL_GC_REG_STORAGE_SIZE * sizeof(ptr_t));\
+       asm("add $16, %esp");\
+    } while (0)
+
+#endif
+
+void nacl_pre_syscall_hook()
+{
+    int local_dummy = 0;
+    if (nacl_thread_idx != -1) {
+       NACL_STORE_REGS();
+        nacl_gc_thread_self->stop_info.stack_ptr = (ptr_t)(&local_dummy);
+        nacl_thread_parked[nacl_thread_idx] = 1;
+    }
+}
+
+void nacl_post_syscall_hook()
+{
+    /* Calling __nacl_suspend_thread_if_needed() right away should guarantee we don't mutate the GC set. */
+    __nacl_suspend_thread_if_needed();
+    if (nacl_thread_idx != -1) {
+        nacl_thread_parked[nacl_thread_idx] = 0;
+    }
+}
+
+void __nacl_suspend_thread_if_needed() {
+    if (nacl_park_threads_now) {
+        pthread_t self = pthread_self();
+        int local_dummy = 0;
+        /* Don't try to park the thread parker. */
+        if (nacl_thread_parker == self)
+            return;
+
+        /* This can happen when a thread is created   */
+        /* outside of the GC system (wthread mostly). */
+        if (nacl_thread_idx < 0)
+            return;
+
+        /* If it was already 'parked', we're returning from a syscall, */
+        /* so don't bother storing registers again, the GC has a set.  */
+        if (!nacl_thread_parked[nacl_thread_idx]) {
+            NACL_STORE_REGS();
+            nacl_gc_thread_self->stop_info.stack_ptr = (ptr_t)(&local_dummy);
+        }
+        nacl_thread_parked[nacl_thread_idx] = 1;
+        while (nacl_park_threads_now)
+            ; /* spin */
+        nacl_thread_parked[nacl_thread_idx] = 0;
+
+        /* Clear out the reg storage for next suspend. */
+        memset(nacl_gc_thread_self->stop_info.reg_storage, 0, NACL_GC_REG_STORAGE_SIZE * sizeof(ptr_t));
+    }
+}
+
+#endif /* NACL */
+
 /* Caller holds allocation lock.       */
 void GC_stop_world()
 {
@@ -465,6 +612,7 @@ void GC_stop_world()
 /* the world stopped.                                                  */
 static void pthread_start_world()
 {
+#ifndef NACL
     pthread_t my_thread = pthread_self();
     register int i;
     register GC_thread p;
@@ -525,6 +673,12 @@ static void pthread_start_world()
     #if DEBUG_THREADS
       GC_printf0("World started\n");
     #endif
+#else /* NACL */
+#   if DEBUG_THREADS
+    GC_printf0("World starting\n");
+#   endif
+    nacl_park_threads_now = 0;
+#endif /* NACL */
 }
 
 void GC_start_world()
@@ -538,6 +692,7 @@ void GC_start_world()
 }
 
 static void pthread_stop_init() {
+#ifndef NACL
     struct sigaction act;
     
     if (sem_init(&GC_suspend_ack_sem, 0, 0) != 0)
@@ -578,6 +733,7 @@ static void pthread_stop_init() {
               GC_printf0("Will retry suspend signal if necessary.\n");
          }
 #     endif
+#endif /* NACL */
 }
 
 /* We hold the allocation lock.        */
index c307ac0eec588a5ec2eb000a77bf05a8d9f83e6f..3e588ace211abdcd0ed62b104f94f2a57d8b336b 100644 (file)
 #   endif
 #   undef pthread_join
 #   undef pthread_detach
+#   if defined(NACL)
+#     undef pthread_exit
+#   endif
 #   if defined(GC_OSF1_THREADS) && defined(_PTHREAD_USE_MANGLED_NAMES_) \
        && !defined(_PTHREAD_USE_PTDNAM_)
 /* Restore the original mangled names on Tru64 UNIX.  */
@@ -676,6 +679,52 @@ void GC_mark_thread_local_free_lists(void)
 
 static struct GC_Thread_Rep first_thread;
 
+#ifdef NACL
+extern int nacl_thread_parked[MAX_NACL_GC_THREADS];
+extern int nacl_thread_used[MAX_NACL_GC_THREADS];
+extern int nacl_thread_parking_inited;
+extern int nacl_num_gc_threads;
+extern pthread_mutex_t nacl_thread_alloc_lock;
+extern __thread int nacl_thread_idx;
+extern __thread GC_thread nacl_gc_thread_self;
+
+void nacl_initialize_gc_thread()
+{
+    int i;
+    pthread_mutex_lock(&nacl_thread_alloc_lock);
+    if (!nacl_thread_parking_inited)
+    {
+        for (i = 0; i < MAX_NACL_GC_THREADS; i++) {
+            nacl_thread_used[i] = 0;
+            nacl_thread_parked[i] = 0;
+        }
+        nacl_thread_parking_inited = 1;
+    }
+    GC_ASSERT(nacl_num_gc_threads <= MAX_NACL_GC_THREADS);
+    for (i = 0; i < MAX_NACL_GC_THREADS; i++) {
+        if (nacl_thread_used[i] == 0) {
+            nacl_thread_used[i] = 1;
+            nacl_thread_idx = i;
+            nacl_num_gc_threads++;
+            break;
+        }
+    }
+    pthread_mutex_unlock(&nacl_thread_alloc_lock);
+}
+
+void nacl_shutdown_gc_thread()
+{
+    pthread_mutex_lock(&nacl_thread_alloc_lock);
+    GC_ASSERT(nacl_thread_idx >= 0 && nacl_thread_idx < MAX_NACL_GC_THREADS);
+    GC_ASSERT(nacl_thread_used[nacl_thread_idx] != 0);
+    nacl_thread_used[nacl_thread_idx] = 0;
+    nacl_thread_idx = -1;
+    nacl_num_gc_threads--;
+    pthread_mutex_unlock(&nacl_thread_alloc_lock);
+}
+
+#endif /* NACL */
+
 /* Add a thread to GC_threads.  We assume it wasn't already there.     */
 /* Caller holds allocation lock.                                       */
 GC_thread GC_new_thread(pthread_t id)
@@ -698,6 +747,10 @@ GC_thread GC_new_thread(pthread_t id)
 #endif
     result -> next = GC_threads[hv];
     GC_threads[hv] = result;
+#ifdef NACL
+    nacl_gc_thread_self = result;
+    nacl_initialize_gc_thread();
+#endif
     GC_ASSERT(result -> flags == 0 && result -> thread_blocked == 0);
     return(result);
 }
@@ -711,6 +764,11 @@ void GC_delete_thread(pthread_t id)
     register GC_thread p = GC_threads[hv];
     register GC_thread prev = 0;
     
+#ifdef NACL
+    nacl_shutdown_gc_thread();
+    nacl_gc_thread_self = NULL;
+#endif
+
     while (!pthread_equal(p -> id, id)) {
         prev = p;
         p = p -> next;
@@ -1118,6 +1176,7 @@ void GC_init_parallel()
 
 
 #if !defined(GC_DARWIN_THREADS) && !defined(GC_OPENBSD_THREADS)
+#ifndef NACL
 int WRAP_FUNC(pthread_sigmask)(int how, const sigset_t *set, sigset_t *oset)
 {
     sigset_t fudged_set;
@@ -1129,6 +1188,7 @@ int WRAP_FUNC(pthread_sigmask)(int how, const sigset_t *set, sigset_t *oset)
     }
     return(REAL_FUNC(pthread_sigmask)(how, set, oset));
 }
+#endif
 #endif /* !GC_DARWIN_THREADS */
 
 /* Wrappers for functions that are likely to block for an appreciable  */
@@ -1259,6 +1319,17 @@ int WRAP_FUNC(pthread_join)(pthread_t thread, void **retval)
     return result;
 }
 
+#ifdef NACL
+/* Native Client doesn't support pthread cleanup functions, */
+/* so wrap pthread_exit and manually cleanup the thread.    */
+void
+WRAP_FUNC(pthread_exit)(void *status)
+{
+    GC_thread_exit_proc(0); 
+    REAL_FUNC(pthread_exit)(status);
+}
+#endif
+
 int
 WRAP_FUNC(pthread_detach)(pthread_t thread)
 {
index 8fe73c81e27b08b0c947380e5b79e770e5af20db..24bf9af5623aedf9fa59a4a4ef8baa2d9bd51dde 100644 (file)
@@ -474,7 +474,7 @@ encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
 #else
 #define AOT_FUNC_ALIGNMENT 16
 #endif
-#if defined(TARGET_X86) && defined(__native_client_codegen__)
+#if (defined(TARGET_X86) || defined(TARGET_AMD64)) && defined(__native_client_codegen__)
 #undef AOT_FUNC_ALIGNMENT
 #define AOT_FUNC_ALIGNMENT 32
 #endif
@@ -678,8 +678,14 @@ arch_emit_plt_entry (MonoAotCompile *acfg, int index)
 {
 #if defined(TARGET_X86)
                guint32 offset = (acfg->plt_got_offset_base + index) * sizeof (gpointer);
-
-#ifdef __native_client_codegen__
+#if defined(__default_codegen__)
+               /* jmp *<offset>(%ebx) */
+               emit_byte (acfg, 0xff);
+               emit_byte (acfg, 0xa3);
+               emit_int32 (acfg, offset);
+               /* Used by mono_aot_get_plt_info_offset */
+               emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
+#elif defined(__native_client_codegen__)
                const guint8 kSizeOfNaClJmp = 11;
                guint8 bytes[kSizeOfNaClJmp];
                guint8 *pbytes = &bytes[0];
@@ -687,19 +693,13 @@ arch_emit_plt_entry (MonoAotCompile *acfg, int index)
                x86_jump_membase32 (pbytes, X86_EBX, offset);
                emit_bytes (acfg, bytes, kSizeOfNaClJmp);
                /* four bytes of data, used by mono_arch_patch_plt_entry              */
-               /* For Native Client, make this work with data embedded in push.      */
+               /* TODO(bradchen): make this work with data embedded in push.         */
                emit_byte (acfg, 0x68);  /* hide data in a push */
                emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
                emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
-#else
-               /* jmp *<offset>(%ebx) */
-               emit_byte (acfg, 0xff);
-               emit_byte (acfg, 0xa3);
-               emit_int32 (acfg, offset);
-               /* Used by mono_aot_get_plt_info_offset */
-               emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
-#endif  /* __native_client_codegen__ */
+#endif /*__native_client_codegen__*/
 #elif defined(TARGET_AMD64)
+#if defined(__default_codegen__)
                /*
                 * We can't emit jumps because they are 32 bits only so they can't be patched.
                 * So we make indirect calls through GOT entries which are patched by the AOT 
@@ -711,6 +711,27 @@ arch_emit_plt_entry (MonoAotCompile *acfg, int index)
                emit_symbol_diff (acfg, acfg->got_symbol, ".", ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) -4);
                /* Used by mono_aot_get_plt_info_offset */
                emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
+#elif defined(__native_client_codegen__)
+               guint8 buf [256];
+               guint8 *buf_aligned = ALIGN_TO(buf, kNaClAlignment);
+               guint8 *code = buf_aligned;
+
+               /* mov <OFFSET>(%rip), %r11d */
+               emit_byte (acfg, '\x45');
+               emit_byte (acfg, '\x8b');
+               emit_byte (acfg, '\x1d');
+               emit_symbol_diff (acfg, acfg->got_symbol, ".", ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) -4);
+
+               amd64_jump_reg (code, AMD64_R11);
+               /* This should be constant for the plt patch */
+               g_assert ((size_t)(code-buf_aligned) == 10);
+               emit_bytes (acfg, buf_aligned, code - buf_aligned);
+
+               /* Hide data in a push imm32 so it passes validation */
+               emit_byte (acfg, 0x68);  /* push */
+               emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
+               emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
+#endif /*__native_client_codegen__*/
 #elif defined(TARGET_ARM)
                guint8 buf [256];
                guint8 *code;
@@ -789,6 +810,7 @@ arch_emit_specific_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size
         * - all the trampolines should be of the same length.
         */
 #if defined(TARGET_AMD64)
+#if defined(__default_codegen__)
        /* This should be exactly 16 bytes long */
        *tramp_size = 16;
        /* call *<offset>(%rip) */
@@ -797,8 +819,61 @@ arch_emit_specific_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size
        emit_byte (acfg, '\x15');
        emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
        /* This should be relative to the start of the trampoline */
-       emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 19);
+       emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset+1) * sizeof (gpointer)) + 7);
        emit_zero_bytes (acfg, 5);
+#elif defined(__native_client_codegen__)
+       guint8 buf [256];
+       guint8 *buf_aligned = ALIGN_TO(buf, kNaClAlignment);
+       guint8 *code = buf_aligned;
+       guint8 *call_start;
+       size_t call_len;
+       int got_offset;
+
+       /* Emit this call in 'code' so we can find out how long it is. */
+       amd64_call_reg (code, AMD64_R11);
+       call_start = mono_arch_nacl_skip_nops (buf_aligned);
+       call_len = code - call_start;
+
+       /* The tramp_size is twice the NaCl alignment because it starts with */ 
+       /* a call which needs to be aligned to the end of the boundary.      */
+       *tramp_size = kNaClAlignment*2;
+       {
+               /* Emit nops to align call site below which is 7 bytes plus */
+               /* the length of the call sequence emitted above.           */
+               /* Note: this requires the specific trampoline starts on a  */
+               /* kNaclAlignedment aligned address, which it does because  */
+               /* it's its own function that is aligned.                   */
+               guint8 nop_buf[256];
+               guint8 *nopbuf_aligned = ALIGN_TO (nop_buf, kNaClAlignment);
+               guint8 *nopbuf_end = mono_arch_nacl_pad (nopbuf_aligned, kNaClAlignment - 7 - (call_len));
+               emit_bytes (acfg, nopbuf_aligned, nopbuf_end - nopbuf_aligned);
+       }
+       /* The trampoline is stored at the offset'th pointer, the -4 is  */
+       /* present because RIP relative addressing starts at the end of  */
+       /* the current instruction, while the label "." is relative to   */
+       /* the beginning of the current asm location, which in this case */
+       /* is not the mov instruction, but the offset itself, due to the */
+       /* way the bytes and ints are emitted here.                      */
+       got_offset = (offset * sizeof(gpointer)) - 4;
+
+       /* mov <OFFSET>(%rip), %r11d */
+       emit_byte (acfg, '\x45');
+       emit_byte (acfg, '\x8b');
+       emit_byte (acfg, '\x1d');
+       emit_symbol_diff (acfg, acfg->got_symbol, ".", got_offset);
+
+       /* naclcall %r11 */
+       emit_bytes (acfg, call_start, call_len);
+
+       /* The arg is stored at the offset+1 pointer, relative to beginning */
+       /* of trampoline: 7 for mov, plus the call length, and 1 for push.  */
+       got_offset = ((offset + 1) * sizeof(gpointer)) + 7 + call_len + 1;
+
+       /* We can't emit this data directly, hide in a "push imm32" */
+       emit_byte (acfg, '\x68'); /* push */
+       emit_symbol_diff (acfg, acfg->got_symbol, ".", got_offset);
+       emit_alignment (acfg, kNaClAlignment);
+#endif /*__native_client_codegen__*/
 #elif defined(TARGET_ARM)
        guint8 buf [128];
        guint8 *code;
@@ -985,6 +1060,7 @@ static void
 arch_emit_static_rgctx_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
 {
 #if defined(TARGET_AMD64)
+#if defined(__default_codegen__)
        /* This should be exactly 13 bytes long */
        *tramp_size = 13;
 
@@ -998,6 +1074,31 @@ arch_emit_static_rgctx_trampoline (MonoAotCompile *acfg, int offset, int *tramp_
        emit_byte (acfg, '\xff');
        emit_byte (acfg, '\x25');
        emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4);
+#elif defined(__native_client_codegen__)
+       guint8 buf [128];
+       guint8 *buf_aligned = ALIGN_TO(buf, kNaClAlignment);
+       guint8 *code = buf_aligned;
+
+       /* mov <OFFSET>(%rip), %r10d */
+       emit_byte (acfg, '\x45');
+       emit_byte (acfg, '\x8b');
+       emit_byte (acfg, '\x15');
+       emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
+
+       /* mov <OFFSET>(%rip), %r11d */
+       emit_byte (acfg, '\x45');
+       emit_byte (acfg, '\x8b');
+       emit_byte (acfg, '\x1d');
+       emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4);
+
+       /* nacljmp *%r11 */
+       amd64_jump_reg (code, AMD64_R11);
+       emit_bytes (acfg, buf_aligned, code - buf_aligned);
+
+       emit_alignment (acfg, kNaClAlignment);
+       *tramp_size = kNaClAlignment;
+#endif /*__native_client_codegen__*/
+
 #elif defined(TARGET_ARM)
        guint8 buf [128];
        guint8 *code;
@@ -1107,50 +1208,74 @@ arch_emit_imt_thunk (MonoAotCompile *acfg, int offset, int *tramp_size)
 {
 #if defined(TARGET_AMD64)
        guint8 *buf, *code;
+#if defined(__native_client_codegen__)
+       guint8 *buf_alloc;
+#endif
        guint8 *labels [3];
+       guint8 mov_buf[3];
+       guint8 *mov_buf_ptr = mov_buf;
 
+       const int kSizeOfMove = 7;
+#if defined(__default_codegen__)
        code = buf = g_malloc (256);
+#elif defined(__native_client_codegen__)
+       buf_alloc = g_malloc (256 + kNaClAlignment + kSizeOfMove);
+       buf = ((guint)buf_alloc + kNaClAlignment) & ~kNaClAlignmentMask;
+       /* The RIP relative move below is emitted first */
+       buf += kSizeOfMove;
+       code = buf;
+#endif
 
        /* FIXME: Optimize this, i.e. use binary search etc. */
        /* Maybe move the body into a separate function (slower, but much smaller) */
 
-       /* R11 is a free register */
+       /* MONO_ARCH_IMT_SCRATCH_REG is a free register */
 
        labels [0] = code;
-       amd64_alu_membase_imm (code, X86_CMP, AMD64_R11, 0, 0);
+       amd64_alu_membase_imm (code, X86_CMP, MONO_ARCH_IMT_SCRATCH_REG, 0, 0);
        labels [1] = code;
-       amd64_branch8 (code, X86_CC_Z, FALSE, 0);
+       amd64_branch8 (code, X86_CC_Z, 0, FALSE);
 
        /* Check key */
-       amd64_alu_membase_reg (code, X86_CMP, AMD64_R11, 0, MONO_ARCH_IMT_REG);
+       amd64_alu_membase_reg_size (code, X86_CMP, MONO_ARCH_IMT_SCRATCH_REG, 0, MONO_ARCH_IMT_REG, sizeof (gpointer));
        labels [2] = code;
-       amd64_branch8 (code, X86_CC_Z, FALSE, 0);
+       amd64_branch8 (code, X86_CC_Z, 0, FALSE);
 
        /* Loop footer */
-       amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, 2 * sizeof (gpointer));
+       amd64_alu_reg_imm (code, X86_ADD, MONO_ARCH_IMT_SCRATCH_REG, 2 * sizeof (gpointer));
        amd64_jump_code (code, labels [0]);
 
        /* Match */
        mono_amd64_patch (labels [2], code);
-       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, sizeof (gpointer), 8);
-       amd64_jump_membase (code, AMD64_R11, 0);
+       amd64_mov_reg_membase (code, MONO_ARCH_IMT_SCRATCH_REG, MONO_ARCH_IMT_SCRATCH_REG, sizeof (gpointer), sizeof (gpointer));
+       amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
 
        /* No match */
        /* FIXME: */
        mono_amd64_patch (labels [1], code);
        x86_breakpoint (code);
 
-       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 12345678, 8);
-
-       /* mov <OFFSET>(%rip), %r11 */
-       emit_byte (acfg, '\x4d');
-       emit_byte (acfg, '\x8b');
-       emit_byte (acfg, '\x1d');
+       /* mov <OFFSET>(%rip), MONO_ARCH_IMT_SCRATCH_REG */
+       amd64_emit_rex (mov_buf_ptr, sizeof(gpointer), MONO_ARCH_IMT_SCRATCH_REG, 0, AMD64_RIP);
+       *(mov_buf_ptr)++ = (unsigned char)0x8b; /* mov opcode */
+       x86_address_byte (mov_buf_ptr, 0, MONO_ARCH_IMT_SCRATCH_REG & 0x7, 5);
+       emit_bytes (acfg, mov_buf, mov_buf_ptr - mov_buf);
        emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
 
        emit_bytes (acfg, buf, code - buf);
        
-       *tramp_size = code - buf + 7;
+       *tramp_size = code - buf + kSizeOfMove;
+#if defined(__native_client_codegen__)
+       /* The tramp will be padded to the next kNaClAlignment bundle. */
+       *tramp_size = ALIGN_TO ((*tramp_size), kNaClAlignment);
+#endif
+
+#if defined(__default_codegen__)
+       g_free (buf);
+#elif defined(__native_client_codegen__)
+       g_free (buf_alloc); 
+#endif
+
 #elif defined(TARGET_X86)
        guint8 *buf, *code;
 #ifdef __native_client_codegen__
@@ -1158,11 +1283,11 @@ arch_emit_imt_thunk (MonoAotCompile *acfg, int offset, int *tramp_size)
 #endif
        guint8 *labels [3];
 
-#ifdef __native_client_codegen__
+#if defined(__default_codegen__)
+       code = buf = g_malloc (256);
+#elif defined(__native_client_codegen__)
        buf_alloc = g_malloc (256 + kNaClAlignment);
        code = buf = ((guint)buf_alloc + kNaClAlignment) & ~kNaClAlignmentMask;
-#else
-       code = buf = g_malloc (256);
 #endif
 
        /* Allocate a temporary stack slot */
@@ -1215,6 +1340,13 @@ arch_emit_imt_thunk (MonoAotCompile *acfg, int offset, int *tramp_size)
        emit_bytes (acfg, buf, code - buf);
        
        *tramp_size = code - buf;
+
+#if defined(__default_codegen__)
+       g_free (buf);
+#elif defined(__native_client_codegen__)
+       g_free (buf_alloc); 
+#endif
+
 #elif defined(TARGET_ARM)
        guint8 buf [128];
        guint8 *code, *code2, *labels [16];
@@ -4924,7 +5056,17 @@ emit_code (MonoAotCompile *acfg)
         * Emit some padding so the local symbol for the first method doesn't have the
         * same address as 'methods'.
         */
+#if defined(__default_codegen__)
        emit_zero_bytes (acfg, 16);
+#elif defined(__native_client_codegen__)
+       {
+               const int kPaddingSize = 16;
+               guint8 pad_buffer[kPaddingSize];
+               mono_arch_nacl_pad (pad_buffer, kPaddingSize);
+               emit_bytes (acfg, pad_buffer, kPaddingSize);
+       }
+#endif
+       
 
        for (l = acfg->method_order; l != NULL; l = l->next) {
                MonoCompile *cfg;
@@ -6117,7 +6259,11 @@ compile_asm (MonoAotCompile *acfg)
 #endif
 
 #ifdef __native_client_codegen__
+#if defined(TARGET_AMD64)
+#define AS_NAME "nacl64-as"
+#else
 #define AS_NAME "nacl-as"
+#endif
 #else
 #define AS_NAME "as"
 #endif
index 6912e3385f78a8baf07e8d51b213fc2e272d977e..bcd3b1c3e0a0ea129d02174855cb3c93d200594f 100644 (file)
@@ -944,6 +944,7 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
        int i;
        gpointer *got_addr;
        guint8 *blob;
+       gboolean do_load_image = TRUE;
 
        if (mono_compile_aot)
                return;
@@ -1215,8 +1216,20 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
         * non-lazily, since we can't handle out-of-date errors later.
         * The cached class info also depends on the exact assemblies.
         */
-       for (i = 0; i < amodule->image_table_len; ++i)
-               load_image (amodule, i, FALSE);
+#if defined(__native_client__)
+       /* TODO: Don't 'load_image' on mscorlib due to a */
+       /* recursive loading problem.  This should be    */
+       /* removed if mscorlib is loaded from disk.      */
+       if (strncmp(assembly->aname.name, "mscorlib", 8)) {
+               do_load_image = TRUE;
+       } else {
+               do_load_image = FALSE;
+       }
+#endif
+       if (do_load_image) {
+               for (i = 0; i < amodule->image_table_len; ++i)
+                       load_image (amodule, i, FALSE);
+       }
 
        if (amodule->out_of_date) {
                mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT Module %s is unusable because a dependency is out-of-date.\n", assembly->image->name);
index 13a399830a50aafee4f8945ebec351f392e0487d..141b90a2be2a7f30e677b621bb1f6eef46728708 100644 (file)
@@ -815,6 +815,15 @@ replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl
 static void
 replace_out_block_in_code (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl) {
        MonoInst *ins;
+
+#if defined(__native_client_codegen__)
+       /* Need to maintain this flag for the new block because */
+       /* we can't jump indirectly to a non-aligned block.     */
+       if (orig->flags & BB_INDIRECT_JUMP_TARGET)
+       {
+               repl->flags |= BB_INDIRECT_JUMP_TARGET;
+       }
+#endif
        
        for (ins = bb->code; ins != NULL; ins = ins->next) {
                switch (ins->opcode) {
index 6d2561b782ea2e2b5eb8928584398ce32532df93..7be114a24f96e4b81de9ff0b458578e4fbb8c7be 100644 (file)
 #
 # See the code in mini-x86.c for more details on how the specifiers are used.
 #
+#
+# Native Client Note: NaCl call sequences do not really reach > 32 bytes but 
+# the maximum length can be high, so if we get unlucky and wind up trying to 
+# emit a call sequence such that we are one or two bytes too long, we need to
+# pad out almost an entire 32 bytes.
+#
+
 break: len:2
 jmp: len:120
 tailcall: len:120 clob:c
@@ -60,8 +67,8 @@ br: len:6
 label: len:0
 seq_point: len:25
 
-long_add: dest:i src1:i src2:i len:3 clob:1
-long_sub: dest:i src1:i src2:i len:3 clob:1
+long_add: dest:i src1:i src2:i len:3 clob:1 nacl:6
+long_sub: dest:i src1:i src2:i len:3 clob:1 nacl:6
 long_mul: dest:i src1:i src2:i len:4 clob:1
 long_div: dest:a src1:a src2:i len:16 clob:d
 long_div_un: dest:a src1:a src2:i len:16 clob:d
@@ -96,11 +103,11 @@ long_min_un: dest:i src1:i src2:i len:16 clob:1
 long_max: dest:i src1:i src2:i len:16 clob:1
 long_max_un: dest:i src1:i src2:i len:16 clob:1
 
-throw: src1:i len:18
-rethrow: src1:i len:18
+throw: src1:i len:18 nacl:50
+rethrow: src1:i len:18 nacl:50
 start_handler: len:16
-endfinally: len:9
-endfilter: src1:a len:9
+endfinally: len:9 nacl:22
+endfilter: src1:a len:9 nacl:19
 ckfinite: dest:f src1:f len:43
 ceq: dest:c len:8
 cgt: dest:c len:8
@@ -115,11 +122,11 @@ compare_imm: src1:i len:13
 icompare_imm: src1:i len:8
 fcompare: src1:f src2:f clob:a len:13
 oparglist: src1:b len:11
-checkthis: src1:b len:5
-call: dest:a clob:c len:32
-voidcall: clob:c len:32
-voidcall_reg: src1:i clob:c len:32
-voidcall_membase: src1:b clob:c len:32
+checkthis: src1:b len:5 nacl:8
+call: dest:a clob:c len:32 nacl:64
+voidcall: clob:c len:32 nacl:64
+voidcall_reg: src1:i clob:c len:32 nacl:64
+voidcall_membase: src1:b clob:c len:32 nacl:64
 fcall: dest:f len:64 clob:c
 fcall_reg: dest:f src1:i len:64 clob:c
 fcall_membase: dest:f src1:b len:64 clob:c
@@ -129,39 +136,39 @@ lcall_membase: dest:a src1:b len:64 clob:c
 vcall: len:64 clob:c
 vcall_reg: src1:i len:64 clob:c
 vcall_membase: src1:b len:64 clob:c
-call_reg: dest:a src1:i len:32 clob:c
-call_membase: dest:a src1:b len:32 clob:c
+call_reg: dest:a src1:i len:32 clob:c nacl:64
+call_membase: dest:a src1:b len:32 clob:c nacl:64
 iconst: dest:i len:10
 i8const: dest:i len:10
 r4const: dest:f len:14
 r8const: dest:f len:9
 store_membase_imm: dest:b len:15
-store_membase_reg: dest:b src1:i len:9
-storei8_membase_reg: dest:b src1:i len:9
-storei1_membase_imm: dest:b len:11
-storei1_membase_reg: dest:b src1:c len:9
-storei2_membase_imm: dest:b len:13
-storei2_membase_reg: dest:b src1:i len:9
-storei4_membase_imm: dest:b len:13
-storei4_membase_reg: dest:b src1:i len:9
+store_membase_reg: dest:b src1:i len:9 nacl:11
+storei8_membase_reg: dest:b src1:i len:9 nacl:11
+storei1_membase_imm: dest:b len:11 nacl:15
+storei1_membase_reg: dest:b src1:c len:9 nacl:11
+storei2_membase_imm: dest:b len:13 nacl:15
+storei2_membase_reg: dest:b src1:i len:9 nacl:11
+storei4_membase_imm: dest:b len:13 nacl:15
+storei4_membase_reg: dest:b src1:i len:9 nacl:11
 storei8_membase_imm: dest:b len:18
 storer4_membase_reg: dest:b src1:f len:15
 storer8_membase_reg: dest:b src1:f len:10
-load_membase: dest:i src1:b len:8
-loadi1_membase: dest:c src1:b len:9
-loadu1_membase: dest:c src1:b len:9
-loadi2_membase: dest:i src1:b len:9
-loadu2_membase: dest:i src1:b len:9
-loadi4_membase: dest:i src1:b len:9
-loadu4_membase: dest:i src1:b len:9
-loadi8_membase: dest:i src1:b len:18
+load_membase: dest:i src1:b len:8 nacl:12
+loadi1_membase: dest:c src1:b len:9 nacl:12
+loadu1_membase: dest:c src1:b len:9 nacl:12
+loadi2_membase: dest:i src1:b len:9 nacl:12
+loadu2_membase: dest:i src1:b len:9 nacl:12
+loadi4_membase: dest:i src1:b len:9 nacl:12
+loadu4_membase: dest:i src1:b len:9 nacl:12
+loadi8_membase: dest:i src1:b len:18 nacl:14
 loadr4_membase: dest:f src1:b len:16
 loadr8_membase: dest:f src1:b len:16
 loadu4_mem: dest:i len:10
 amd64_loadi8_memindex: dest:i src1:i src2:i len:10
 move: dest:i src1:i len:3
-add_imm: dest:i src1:i len:8 clob:1
-sub_imm: dest:i src1:i len:8 clob:1
+add_imm: dest:i src1:i len:8 clob:1 nacl:11
+sub_imm: dest:i src1:i len:8 clob:1 nacl:11
 mul_imm: dest:i src1:i len:11
 and_imm: dest:i src1:i len:8 clob:1
 or_imm: dest:i src1:i len:8 clob:1
@@ -246,8 +253,9 @@ float_clt_membase: dest:i src1:f src2:b len:35
 float_clt_un_membase: dest:i src1:f src2:b len:42
 float_conv_to_u: dest:i src1:f len:46
 fmove: dest:f src1:f len:8
-call_handler: len:14 clob:c
+call_handler: len:14 clob:c nacl:52
 aot_const: dest:i len:10
+nacl_gc_safe_point: clob:c
 x86_test_null: src1:i len:5
 x86_compare_membase_reg: src1:b src2:i len:9
 x86_compare_membase_imm: src1:b len:13
@@ -263,7 +271,7 @@ x86_push_imm: len:6
 x86_push_membase: src1:b len:8
 x86_push_obj: src1:b len:40
 x86_lea: dest:i src1:i src2:i len:8
-x86_lea_membase: dest:i src1:i len:11
+x86_lea_membase: dest:i src1:i len:11 nacl:14
 x86_xchg: src1:i src2:i clob:x len:2
 x86_fpop: src1:f len:3
 x86_seteq_membase: src1:b len:9
@@ -298,7 +306,7 @@ subcc: dest:i src1:i src2:i len:3 clob:1
 adc_imm: dest:i src1:i len:8 clob:1
 sbb: dest:i src1:i src2:i len:3 clob:1
 sbb_imm: dest:i src1:i len:8 clob:1
-br_reg: src1:i len:3
+br_reg: src1:i len:3 nacl:8
 sin: dest:f src1:f len:32
 cos: dest:f src1:f len:32
 abs: dest:f src1:f clob:1 len:32
@@ -310,8 +318,8 @@ sext_i2: dest:i src1:i len:4
 sext_i4: dest:i src1:i len:8
 
 # 32 bit opcodes
-int_add: dest:i src1:i src2:i clob:1 len:4
-int_sub: dest:i src1:i src2:i clob:1 len:4
+int_add: dest:i src1:i src2:i clob:1 len:4 nacl:7
+int_sub: dest:i src1:i src2:i clob:1 len:4 nacl:7
 int_mul: dest:i src1:i src2:i clob:1 len:4
 int_mul_ovf: dest:i src1:i src2:i clob:1 len:32
 int_mul_ovf_un: dest:i src1:i src2:i clob:1 len:32
@@ -331,8 +339,8 @@ int_sbb: dest:i src1:i src2:i clob:1 len:4
 int_sbb_imm: dest:i src1:i clob:1 len:8
 int_addcc: dest:i src1:i src2:i clob:1 len:16
 int_subcc: dest:i src1:i src2:i clob:1 len:16
-int_add_imm: dest:i src1:i clob:1 len:8
-int_sub_imm: dest:i src1:i clob:1 len:8
+int_add_imm: dest:i src1:i clob:1 len:8 nacl:10
+int_sub_imm: dest:i src1:i clob:1 len:8 nacl:10
 int_mul_imm: dest:i src1:i clob:1 len:32
 int_div_imm: dest:a src1:i clob:d len:32
 int_div_un_imm: dest:a src1:i clob:d len:32
@@ -438,8 +446,8 @@ cmov_lgt_un: dest:i src1:i src2:i len:16 clob:1
 cmov_lle_un: dest:i src1:i src2:i len:16 clob:1
 cmov_llt_un: dest:i src1:i src2:i len:16 clob:1
 
-long_add_imm: dest:i src1:i clob:1 len:12
-long_sub_imm: dest:i src1:i clob:1 len:12
+long_add_imm: dest:i src1:i clob:1 len:12 nacl:15
+long_sub_imm: dest:i src1:i clob:1 len:12 nacl:15
 long_and_imm: dest:i src1:i clob:1 len:12
 long_or_imm: dest:i src1:i clob:1 len:12
 long_xor_imm: dest:i src1:i clob:1 len:12
@@ -486,7 +494,7 @@ vcall2: len:64 clob:c
 vcall2_reg: src1:i len:64 clob:c
 vcall2_membase: src1:b len:64 clob:c
 
-dyn_call: src1:i src2:i len:64 clob:c
+dyn_call: src1:i src2:i len:64 clob:c nacl:128
 
 localloc_imm: dest:i len:84
 
index baf1a890df48fdea02007986b84c3839595900ca..19932da73edca6719cec602bf7da2b563fd1cf63 100644 (file)
@@ -247,6 +247,7 @@ call_handler: len:11 clob:c
 aot_const: dest:i len:5
 load_gotaddr: dest:i len:64
 got_entry: dest:i src1:b len:7
+nacl_gc_safe_point: clob:c
 x86_test_null: src1:i len:2
 x86_compare_membase_reg: src1:b src2:i len:7
 x86_compare_membase_imm: src1:b len:11
index 5024e066f19386af89c0d5fe9faab85412209027..ad35cdb35aa31432ec8769cffd4c9132f403f6c9 100644 (file)
@@ -384,6 +384,7 @@ mono_compute_natural_loops (MonoCompile *cfg)
                        /* The loop body start is the first bblock in the order they will be emitted */
                        MonoBasicBlock *h = cfg->bblocks [i];
                        MonoBasicBlock *body_start = h;
+                       MonoInst *inst;
                        GList *l;
 
                        for (l = h->loop_blocks; l; l = l->next) {
@@ -394,6 +395,12 @@ mono_compute_natural_loops (MonoCompile *cfg)
                                }
                        }
 
+#if defined(__native_client_codegen__)
+                       /* Instrument the loop (GC back branch safe point) */
+                       MONO_INST_NEW (cfg, inst, OP_NACL_GC_SAFE_POINT);
+                       inst->dreg = mono_alloc_dreg (cfg, STACK_I4);
+                       mono_bblock_insert_before_ins (body_start, NULL, inst);
+#endif
                        body_start->loop_body_start = 1;
                }
        }
index 1de8758c07463ae5ae5061b3143cdbcba4b616a4..e8b6a23b3a620b1c9a407a12baba34ccfa5dae06 100644 (file)
@@ -115,7 +115,10 @@ opt_funcs [sizeof (int) * 8] = {
 };
 
 #ifdef __native_client_codegen__
-extern guint8 nacl_align_byte;
+extern gint8 nacl_align_byte;
+#endif
+#ifdef __native_client__
+extern char *nacl_mono_path;
 #endif
 
 #define DEFAULT_OPTIMIZATIONS (        \
@@ -1644,7 +1647,11 @@ mono_main (int argc, char* argv[])
                        mono_use_llvm = FALSE;
 #ifdef __native_client_codegen__
                } else if (strcmp (argv [i], "--nacl-align-mask-off") == 0){
-                       nacl_align_byte = 0xff; 
+                       nacl_align_byte = -1; /* 0xff */
+#endif
+#ifdef __native_client__
+               } else if (strcmp (argv [i], "--nacl-mono-path") == 0){
+                       nacl_mono_path = g_strdup(argv[++i]);
 #endif
                } else {
                        fprintf (stderr, "Unknown command line option: '%s'\n", argv [i]);
@@ -1655,7 +1662,7 @@ mono_main (int argc, char* argv[])
 #ifdef __native_client_codegen__
        if (getenv ("MONO_NACL_ALIGN_MASK_OFF"))
        {
-               nacl_align_byte = 0xff;
+               nacl_align_byte = -1; /* 0xff */
        }
 #endif
 
index 862b61ed53e9e0dc742c3fd9809956eb9935bed2..263edecff4ff1378d4ad94dc696f0139b865badc 100644 (file)
@@ -179,7 +179,9 @@ mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
        amd64_mov_reg_membase (code, AMD64_R12, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, r12), 8);
        amd64_mov_reg_membase (code, AMD64_R13, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, r13), 8);
        amd64_mov_reg_membase (code, AMD64_R14, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, r14), 8);
+#if !defined(__native_client_codegen__)
        amd64_mov_reg_membase (code, AMD64_R15, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, r15), 8);
+#endif
 
        if (mono_running_on_valgrind ()) {
                /* Prevent 'Address 0x... is just below the stack ptr.' errors */
@@ -195,6 +197,10 @@ mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
        /* jump to the saved IP */
        amd64_jump_reg (code, AMD64_R11);
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate(&start, 256, &code);
+#endif
+
        mono_arch_flush_icache (start, code - start);
 
        if (info)
@@ -219,8 +225,9 @@ mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
        guint32 pos;
        MonoJumpInfo *ji = NULL;
        GSList *unwind_ops = NULL;
+       const guint kMaxCodeSize = NACL_SIZE (128, 256);
 
-       start = code = mono_global_codeman_reserve (128);
+       start = code = mono_global_codeman_reserve (kMaxCodeSize);
 
        /* call_filter (MonoContext *ctx, unsigned long eip) */
        code = start;
@@ -252,7 +259,9 @@ mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
        amd64_mov_reg_membase (code, AMD64_R12, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r12), 8);
        amd64_mov_reg_membase (code, AMD64_R13, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r13), 8);
        amd64_mov_reg_membase (code, AMD64_R14, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r14), 8);
+#if !defined(__native_client_codegen__)
        amd64_mov_reg_membase (code, AMD64_R15, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r15), 8);
+#endif
 #ifdef TARGET_WIN32
        amd64_mov_reg_membase (code, AMD64_RDI, AMD64_ARG_REG1,  G_STRUCT_OFFSET (MonoContext, rdi), 8);
        amd64_mov_reg_membase (code, AMD64_RSI, AMD64_ARG_REG1,  G_STRUCT_OFFSET (MonoContext, rsi), 8);
@@ -275,7 +284,11 @@ mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
        amd64_leave (code);
        amd64_ret (code);
 
-       g_assert ((code - start) < 128);
+       g_assert ((code - start) < kMaxCodeSize);
+
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
+#endif
 
        mono_arch_flush_icache (start, code - start);
 
@@ -405,10 +418,10 @@ get_throw_trampoline (MonoTrampInfo **info, gboolean rethrow, gboolean corlib, g
        guint8 *code;
        MonoJumpInfo *ji = NULL;
        GSList *unwind_ops = NULL;
-       int i, buf_size, stack_size, arg_offsets [16], regs_offset;
+       int i, stack_size, arg_offsets [16], regs_offset;
+       const guint kMaxCodeSize = NACL_SIZE (256, 512);
 
-       buf_size = 256;
-       start = code = mono_global_codeman_reserve (buf_size);
+       start = code = mono_global_codeman_reserve (kMaxCodeSize);
 
        /* The stack is unaligned on entry */
        stack_size = 192 + 8;
@@ -482,7 +495,11 @@ get_throw_trampoline (MonoTrampInfo **info, gboolean rethrow, gboolean corlib, g
 
        mono_arch_flush_icache (start, code - start);
 
-       g_assert ((code - start) < buf_size);
+       g_assert ((code - start) < kMaxCodeSize);
+
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
+#endif
 
        if (info)
                *info = mono_tramp_info_create (g_strdup (tramp_name), start, code - start, ji, unwind_ops);
@@ -549,7 +566,7 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
        *new_ctx = *ctx;
 
        if (ji != NULL) {
-               gssize regs [MONO_MAX_IREGS + 1];
+               mgreg_t regs [MONO_MAX_IREGS + 1];
                guint8 *cfa;
                guint32 unwind_info_len;
                guint8 *unwind_info;
@@ -597,7 +614,7 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
                new_ctx->r15 = regs [AMD64_R15];
  
                /* The CFA becomes the new SP value */
-               new_ctx->rsp = (gssize)cfa;
+               new_ctx->rsp = (mgreg_t)cfa;
 
                /* Adjust IP */
                new_ctx->rip --;
@@ -650,7 +667,7 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
                         * The rsp field is set just before the call which transitioned to native 
                         * code. Obtain the rip from the stack.
                         */
-                       rip = *(guint64*)((*lmf)->rsp - sizeof (gpointer));
+                       rip = *(guint64*)((*lmf)->rsp - SIZEOF_REGISTER);
                }
 
                ji = mini_jit_info_table_find (domain, (gpointer)rip, NULL);
@@ -771,6 +788,10 @@ mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
 void
 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
 {
+#if defined(__native_client_codegen__) || defined(__native_client__)
+       printf("WARNING: mono_arch_sigctx_to_monoctx() called!\n");
+#endif
+
 #if defined(MONO_ARCH_USE_SIGACTION)
        ucontext_t *ctx = (ucontext_t*)sigctx;
 
@@ -809,6 +830,10 @@ mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
 void
 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
 {
+#if defined(__native_client__) || defined(__native_client_codegen__)
+  printf("WARNING: mono_arch_monoctx_to_sigctx() called!\n");
+#endif
+
 #if defined(MONO_ARCH_USE_SIGACTION)
        ucontext_t *ctx = (ucontext_t*)sigctx;
 
@@ -966,8 +991,9 @@ mono_arch_get_throw_pending_exception (MonoTrampInfo **info, gboolean aot)
        gpointer throw_trampoline;
        MonoJumpInfo *ji = NULL;
        GSList *unwind_ops = NULL;
+       const guint kMaxCodeSize = NACL_SIZE (128, 256);
 
-       start = code = mono_global_codeman_reserve (128);
+       start = code = mono_global_codeman_reserve (kMaxCodeSize);
 
        /* We are in the frame of a managed method after a call */
        /* 
@@ -1060,7 +1086,11 @@ mono_arch_get_throw_pending_exception (MonoTrampInfo **info, gboolean aot)
        /* Return to original code */
        amd64_jump_reg (code, AMD64_R11);
 
-       g_assert ((code - start) < 128);
+       g_assert ((code - start) < kMaxCodeSize);
+
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
+#endif
 
        if (info)
                *info = mono_tramp_info_create (g_strdup_printf ("throw_pending_exception"), start, code - start, ji, unwind_ops);
@@ -1402,10 +1432,12 @@ mono_tasklets_arch_restore (void)
        static guint8* saved = NULL;
        guint8 *code, *start;
        int cont_reg = AMD64_R9; /* register usable on both call conventions */
+       const guint kMaxCodeSize = NACL_SIZE (64, 128);
+       
 
        if (saved)
                return (MonoContinuationRestore)saved;
-       code = start = mono_global_codeman_reserve (64);
+       code = start = mono_global_codeman_reserve (kMaxCodeSize);
        /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
        /* cont is in AMD64_ARG_REG1 ($rcx or $rdi)
         * state is in AMD64_ARG_REG2 ($rdx or $rsi)
@@ -1431,7 +1463,9 @@ mono_tasklets_arch_restore (void)
        amd64_mov_reg_membase (code, AMD64_R12, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r12), 8);
        amd64_mov_reg_membase (code, AMD64_R13, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r13), 8);
        amd64_mov_reg_membase (code, AMD64_R14, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r14), 8);
+#if !defined(__native_client_codegen__)
        amd64_mov_reg_membase (code, AMD64_R15, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r15), 8);
+#endif
 #ifdef TARGET_WIN32
        amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rdi), 8);
        amd64_mov_reg_membase (code, AMD64_RSI, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rsi), 8);
@@ -1444,7 +1478,10 @@ mono_tasklets_arch_restore (void)
 
        /* state is already in rax */
        amd64_jump_membase (code, cont_reg, G_STRUCT_OFFSET (MonoContinuation, return_ip));
-       g_assert ((code - start) <= 64);
+       g_assert ((code - start) <= kMaxCodeSize);
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
+#endif
        saved = start;
        return (MonoContinuationRestore)saved;
 }
index 0507935c7fab3483d330bf409f6e7b6ed9c9927e..46370e18f01b3a5ba3ef8b17091ac9a8d2af0da8 100644 (file)
@@ -308,6 +308,10 @@ mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
        /* jump to the saved IP */
        x86_ret (code);
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate(&start, 128, &code);
+#endif
+
        if (info)
                *info = mono_tramp_info_create (g_strdup_printf ("restore_context"), start, code - start, ji, unwind_ops);
        else {
@@ -335,11 +339,7 @@ mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
        guint8 *code;
        MonoJumpInfo *ji = NULL;
        GSList *unwind_ops = NULL;
-#ifdef __native_client_codegen__
-       guint kMaxCodeSize = 128;
-#else
-       guint kMaxCodeSize = 64;
-#endif  /* __native_client_codegen__ */
+       guint kMaxCodeSize = NACL_SIZE (64, 128);
 
        /* call_filter (MonoContext *ctx, unsigned long eip) */
        start = code = mono_global_codeman_reserve (kMaxCodeSize);
@@ -387,6 +387,10 @@ mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
        x86_leave (code);
        x86_ret (code);
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
+#endif
+
        if (info)
                *info = mono_tramp_info_create (g_strdup_printf ("call_filter"), start, code - start, ji, unwind_ops);
        else {
@@ -515,11 +519,8 @@ get_throw_trampoline (const char *name, gboolean rethrow, gboolean llvm, gboolea
        int i, stack_size, stack_offset, arg_offsets [5], regs_offset;
        MonoJumpInfo *ji = NULL;
        GSList *unwind_ops = NULL;
-#ifdef __native_client_codegen__
-       guint kMaxCodeSize = 256;
-#else
-       guint kMaxCodeSize = 128;
-#endif
+       guint kMaxCodeSize = NACL_SIZE (128, 256);
+
        start = code = mono_global_codeman_reserve (kMaxCodeSize);
 
        stack_size = 128;
@@ -629,6 +630,10 @@ get_throw_trampoline (const char *name, gboolean rethrow, gboolean llvm, gboolea
        }
        x86_breakpoint (code);
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
+#endif
+
        g_assert ((code - start) < kMaxCodeSize);
 
        if (info)
index 7d66e31d76193fdc3cc213a23c7cbd5cd60987c2..8c13a6171dd947081cc78d7eb28b58ab8a8618b3 100644 (file)
@@ -79,7 +79,7 @@ sub load_opcodes
        if ($arch =~ "__i386__") {
                $arch_define = "TARGET_X86";
        }
-       if ($arch =~ " __x86_64__") {
+       if ($arch =~ "__x86_64__") {
                $arch_define = "TARGET_AMD64";
        }
        if ($arch =~ "__arm__") {
index 4a3cf02e6991a3d98023182e3ad6326545322598..e76e2fe221236d9e487990d4b56082d3b8234c1c 100644 (file)
@@ -72,7 +72,7 @@
 #define AS_INT32_DIRECTIVE ".long"
 #define AS_INT64_DIRECTIVE ".quad"
 
-#if (defined(TARGET_AMD64) || defined(TARGET_POWERPC64)) && !defined(__mono_ilp32__)
+#if (defined(TARGET_AMD64) || defined(TARGET_POWERPC64)) && !defined(__mono_ilp32__) &&!defined(__native_client_codegen__)
 #define AS_POINTER_DIRECTIVE ".quad"
 #else
 #define AS_POINTER_DIRECTIVE ".long"
index 1e4cbf540d996889b42776ad21513cd5c37a2352..e5f08461daf1c6d474eedced58f1b29996e038ad 100644 (file)
@@ -926,6 +926,16 @@ mono_lconv_to_r8_un (guint64 a)
 }
 #endif
 
+#if defined(__native_client_codegen__) || defined(__native_client__)
+/* When we cross-compile to Native Client we can't directly embed calls */
+/* to the math library on the host. This will use the fmod on the target*/
+double
+mono_fmod(double a, double b)
+{
+       return fmod(a, b);
+}
+#endif
+
 gpointer
 mono_helper_compile_generic_method (MonoObject *obj, MonoMethod *method, gpointer *this_arg)
 {
index d0c7214a5ff71da737b0ecd9bec73888c05b318c..16679d36ad094a665a8160f442d00db731fa7c35 100644 (file)
@@ -85,6 +85,10 @@ double mono_conv_to_r8_un (guint32 a) MONO_INTERNAL;
 
 double mono_lconv_to_r8_un (guint64 a) MONO_INTERNAL;
 
+#if defined(__native_client_codegen__) || defined(__native_client__)
+double mono_fmod(double a, double b) MONO_INTERNAL;
+#endif
+
 gpointer mono_helper_compile_generic_method (MonoObject *obj, MonoMethod *method, gpointer *this_arg) MONO_INTERNAL;
 
 MonoString *mono_helper_ldstr (MonoImage *image, guint32 idx) MONO_INTERNAL;
index 705617401ff8283c1c892fb1d3da6b4e6fe24bd3..8e5faeb3bdc0e6f18f3743accdd914b1f349c18e 100644 (file)
@@ -794,7 +794,7 @@ type_from_op (MonoInst *ins, MonoInst *src1, MonoInst *src2) {
        case OP_LCOMPARE:
        case OP_ICOMPARE:
                ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
-               if ((src1->type == STACK_I8) || ((SIZEOF_REGISTER == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
+               if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
                        ins->opcode = OP_LCOMPARE;
                else if (src1->type == STACK_R8)
                        ins->opcode = OP_FCOMPARE;
@@ -803,7 +803,7 @@ type_from_op (MonoInst *ins, MonoInst *src1, MonoInst *src2) {
                break;
        case OP_ICOMPARE_IMM:
                ins->type = bin_comp_table [src1->type] [src1->type] ? STACK_I4 : STACK_INV;
-               if ((src1->type == STACK_I8) || ((SIZEOF_REGISTER == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
+               if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
                        ins->opcode = OP_LCOMPARE_IMM;          
                break;
        case CEE_BEQ:
@@ -891,7 +891,7 @@ type_from_op (MonoInst *ins, MonoInst *src1, MonoInst *src2) {
                        break;
                case STACK_PTR:
                case STACK_MP:
-#if SIZEOF_REGISTER == 8
+#if SIZEOF_VOID_P == 8
                        ins->opcode = OP_LCONV_TO_U;
 #else
                        ins->opcode = OP_MOVE;
@@ -5690,6 +5690,11 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                cfg->bb_entry = start_bblock;
                start_bblock->cil_code = NULL;
                start_bblock->cil_length = 0;
+#if defined(__native_client_codegen__)
+               MONO_INST_NEW (cfg, ins, OP_NACL_GC_SAFE_POINT);
+               ins->dreg = alloc_dreg (cfg, STACK_I4);
+               MONO_ADD_INS (start_bblock, ins);
+#endif
 
                /* EXIT BLOCK */
                NEW_BBLOCK (cfg, end_bblock);
@@ -9860,7 +9865,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                cmp->sreg2 = sp [1]->dreg;
                                type_from_op (cmp, sp [0], sp [1]);
                                CHECK_TYPE (cmp);
-                               if ((sp [0]->type == STACK_I8) || ((SIZEOF_REGISTER == 8) && ((sp [0]->type == STACK_PTR) || (sp [0]->type == STACK_OBJ) || (sp [0]->type == STACK_MP))))
+                               if ((sp [0]->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((sp [0]->type == STACK_PTR) || (sp [0]->type == STACK_OBJ) || (sp [0]->type == STACK_MP))))
                                        cmp->opcode = OP_LCOMPARE;
                                else if (sp [0]->type == STACK_R8)
                                        cmp->opcode = OP_FCOMPARE;
@@ -10793,7 +10798,13 @@ op_to_op_src1_membase (int load_opcode, int opcode)
 
        switch (opcode) {
        case OP_X86_PUSH:
+#ifdef __native_client_codegen__
+               /* In AMD64 NaCl, pointers are 4 bytes, */
+               /*  so LOAD_* != LOADI8_* */
+               if (load_opcode == OP_LOADI8_MEMBASE)
+#else
                if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
+#endif
                        return OP_X86_PUSH_MEMBASE;
                break;
                /* FIXME: This only works for 32 bit immediates
@@ -10808,7 +10819,13 @@ op_to_op_src1_membase (int load_opcode, int opcode)
                break;
        case OP_COMPARE:
        case OP_LCOMPARE:
+#ifdef __native_client_codegen__
+               if (load_opcode == OP_LOAD_MEMBASE)
+                       return OP_AMD64_ICOMPARE_MEMBASE_REG;
+               if (load_opcode == OP_LOADI8_MEMBASE)
+#else
                if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
+#endif
                        return OP_AMD64_COMPARE_MEMBASE_REG;
                break;
        case OP_ICOMPARE:
@@ -10846,7 +10863,11 @@ op_to_op_src2_membase (int load_opcode, int opcode)
 #endif
 
 #ifdef TARGET_AMD64
+#if defined(__default_codegen__)
        if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)) {
+#elif defined(__native_client_codegen__)
+       if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE) ) {
+#endif
                switch (opcode) {
                case OP_ICOMPARE:
                        return OP_AMD64_ICOMPARE_REG_MEMBASE;
@@ -10861,7 +10882,11 @@ op_to_op_src2_membase (int load_opcode, int opcode)
                case OP_IXOR:
                        return OP_X86_XOR_REG_MEMBASE;
                }
+#if defined(__default_codegen__)
        } else if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE)) {
+#elif defined(__native_client_codegen__)
+       } else if (load_opcode == OP_LOADI8_MEMBASE) {
+#endif
                switch (opcode) {
                case OP_COMPARE:
                case OP_LCOMPARE:
index b009b50961308dec2066edbc64be60b7a942264d..5b9ec064200cb1fc001bb9b30d427c99abe2885c 100644 (file)
@@ -204,11 +204,278 @@ amd64_is_near_call (guint8 *code)
        return code [0] == 0xe8;
 }
 
+#ifdef __native_client_codegen__
+
+/* Keep track of instruction "depth", that is, the level of sub-instruction */
+/* for any given instruction.  For instance, amd64_call_reg resolves to     */
+/* amd64_call_reg_internal, which uses amd64_alu_* macros, etc.             */
+/* We only want to force bundle alignment for the top level instruction,    */
+/* so NaCl pseudo-instructions can be implemented with sub instructions.    */
+static guint32 nacl_instruction_depth;
+
+static guint32 nacl_rex_tag;
+static guint32 nacl_legacy_prefix_tag;
+
+void
+amd64_nacl_clear_legacy_prefix_tag ()
+{
+       TlsSetValue (nacl_legacy_prefix_tag, NULL);
+}
+
+void
+amd64_nacl_tag_legacy_prefix (guint8* code)
+{
+       if (TlsGetValue (nacl_legacy_prefix_tag) == NULL)
+               TlsSetValue (nacl_legacy_prefix_tag, code);
+}
+
+void
+amd64_nacl_tag_rex (guint8* code)
+{
+       TlsSetValue (nacl_rex_tag, code);
+}
+
+guint8*
+amd64_nacl_get_legacy_prefix_tag ()
+{
+       return (guint8*)TlsGetValue (nacl_legacy_prefix_tag);
+}
+
+guint8*
+amd64_nacl_get_rex_tag ()
+{
+       return (guint8*)TlsGetValue (nacl_rex_tag);
+}
+
+/* Increment the instruction "depth" described above */
+void
+amd64_nacl_instruction_pre ()
+{
+       intptr_t depth = (intptr_t) TlsGetValue (nacl_instruction_depth);
+       depth++;
+       TlsSetValue (nacl_instruction_depth, (gpointer)depth);
+}
+
+/* amd64_nacl_instruction_post: Decrement instruction "depth", force bundle */
+/* alignment if depth == 0 (top level instruction)                          */
+/* IN: start, end    pointers to instruction beginning and end              */
+/* OUT: start, end   pointers to beginning and end after possible alignment */
+/* GLOBALS: nacl_instruction_depth     defined above                        */
+void
+amd64_nacl_instruction_post (guint8 **start, guint8 **end)
+{
+       intptr_t depth = (intptr_t) TlsGetValue(nacl_instruction_depth);
+       depth--;
+       TlsSetValue (nacl_instruction_depth, (void*)depth);
+
+       g_assert ( depth >= 0 );
+       if (depth == 0) {
+               uintptr_t space_in_block;
+               uintptr_t instlen;
+               guint8 *prefix = amd64_nacl_get_legacy_prefix_tag ();
+               /* if legacy prefix is present, and if it was emitted before */
+               /* the start of the instruction sequence, adjust the start   */
+               if (prefix != NULL && prefix < *start) {
+                       g_assert (*start - prefix <= 3);/* only 3 are allowed */
+                       *start = prefix;
+               }
+               space_in_block = kNaClAlignment - ((uintptr_t)(*start) & kNaClAlignmentMask);
+               instlen = (uintptr_t)(*end - *start);
+               /* Only check for instructions which are less than        */
+               /* kNaClAlignment. The only instructions that should ever */
+               /* be that long are call sequences, which are already     */
+               /* padded out to align the return to the next bundle.     */
+               if (instlen > space_in_block && instlen < kNaClAlignment) {
+                       const size_t MAX_NACL_INST_LENGTH = kNaClAlignment;
+                       guint8 copy_of_instruction[MAX_NACL_INST_LENGTH];
+                       const size_t length = (size_t)((*end)-(*start));
+                       g_assert (length < MAX_NACL_INST_LENGTH);
+                       
+                       memcpy (copy_of_instruction, *start, length);
+                       *start = mono_arch_nacl_pad (*start, space_in_block);
+                       memcpy (*start, copy_of_instruction, length);
+                       *end = *start + length;
+               }
+               amd64_nacl_clear_legacy_prefix_tag ();
+               amd64_nacl_tag_rex (NULL);
+       }
+}
+
+/* amd64_nacl_membase_handler: ensure all access to memory of the form      */
+/*   OFFSET(%rXX) is sandboxed.  For allowable base registers %rip, %rbp,   */
+/*   %rsp, and %r15, emit the membase as usual.  For all other registers,   */
+/*   make sure the upper 32-bits are cleared, and use that register in the  */
+/*   index field of a new address of this form: OFFSET(%r15,%eXX,1)         */
+/* IN:      code                                                            */
+/*             pointer to current instruction stream (in the                */
+/*             middle of an instruction, after opcode is emitted)           */
+/*          basereg/offset/dreg                                             */
+/*             operands of normal membase address                           */
+/* OUT:     code                                                            */
+/*             pointer to the end of the membase/memindex emit              */
+/* GLOBALS: nacl_rex_tag                                                    */
+/*             position in instruction stream that rex prefix was emitted   */
+/*          nacl_legacy_prefix_tag                                          */
+/*             (possibly NULL) position in instruction of legacy x86 prefix */
+void
+amd64_nacl_membase_handler (guint8** code, gint8 basereg, gint32 offset, gint8 dreg)
+{
+       gint8 true_basereg = basereg;
+
+       /* Cache these values, they might change  */
+       /* as new instructions are emitted below. */
+       guint8* rex_tag = amd64_nacl_get_rex_tag ();
+       guint8* legacy_prefix_tag = amd64_nacl_get_legacy_prefix_tag ();
+
+       /* 'basereg' is given masked to 0x7 at this point, so check */
+       /* the rex prefix to see if this is an extended register.   */
+       if ((rex_tag != NULL) && IS_REX(*rex_tag) && (*rex_tag & AMD64_REX_B)) {
+               true_basereg |= 0x8;
+       }
+
+#define X86_LEA_OPCODE (0x8D)
+
+       if (!amd64_is_valid_nacl_base (true_basereg) && (*(*code-1) != X86_LEA_OPCODE)) {
+               guint8* old_instruction_start;
+               
+               /* This will hold the 'mov %eXX, %eXX' that clears the upper */
+               /* 32-bits of the old base register (new index register)     */
+               guint8 buf[32];
+               guint8* buf_ptr = buf;
+               size_t insert_len;
+
+               g_assert (rex_tag != NULL);
+
+               if (IS_REX(*rex_tag)) {
+                       /* The old rex.B should be the new rex.X */
+                       if (*rex_tag & AMD64_REX_B) {
+                               *rex_tag |= AMD64_REX_X;
+                       }
+                       /* Since our new base is %r15 set rex.B */
+                       *rex_tag |= AMD64_REX_B;
+               } else {
+                       /* Shift the instruction by one byte  */
+                       /* so we can insert a rex prefix      */
+                       memmove (rex_tag + 1, rex_tag, (size_t)(*code - rex_tag));
+                       *code += 1;
+                       /* New rex prefix only needs rex.B for %r15 base */
+                       *rex_tag = AMD64_REX(AMD64_REX_B);
+               }
+
+               if (legacy_prefix_tag) {
+                       old_instruction_start = legacy_prefix_tag;
+               } else {
+                       old_instruction_start = rex_tag;
+               }
+               
+               /* Clears the upper 32-bits of the previous base register */
+               amd64_mov_reg_reg_size (buf_ptr, true_basereg, true_basereg, 4);
+               insert_len = buf_ptr - buf;
+               
+               /* Move the old instruction forward to make */
+               /* room for 'mov' stored in 'buf_ptr'       */
+               memmove (old_instruction_start + insert_len, old_instruction_start, (size_t)(*code - old_instruction_start));
+               *code += insert_len;
+               memcpy (old_instruction_start, buf, insert_len);
+
+               /* Sandboxed replacement for the normal membase_emit */
+               x86_memindex_emit (*code, dreg, AMD64_R15, offset, basereg, 0);
+               
+       } else {
+               /* Normal default behavior, emit membase memory location */
+               x86_membase_emit_body (*code, dreg, basereg, offset);
+       }
+}
+
+
+static inline unsigned char*
+amd64_skip_nops (unsigned char* code)
+{
+       guint8 in_nop;
+       do {
+               in_nop = 0;
+               if (   code[0] == 0x90) {
+                       in_nop = 1;
+                       code += 1;
+               }
+               if (   code[0] == 0x66 && code[1] == 0x90) {
+                       in_nop = 1;
+                       code += 2;
+               }
+               if (code[0] == 0x0f && code[1] == 0x1f
+                && code[2] == 0x00) {
+                       in_nop = 1;
+                       code += 3;
+               }
+               if (code[0] == 0x0f && code[1] == 0x1f
+                && code[2] == 0x40 && code[3] == 0x00) {
+                       in_nop = 1;
+                       code += 4;
+               }
+               if (code[0] == 0x0f && code[1] == 0x1f
+                && code[2] == 0x44 && code[3] == 0x00
+                && code[4] == 0x00) {
+                       in_nop = 1;
+                       code += 5;
+               }
+               if (code[0] == 0x66 && code[1] == 0x0f
+                && code[2] == 0x1f && code[3] == 0x44
+                && code[4] == 0x00 && code[5] == 0x00) {
+                       in_nop = 1;
+                       code += 6;
+               }
+               if (code[0] == 0x0f && code[1] == 0x1f
+                && code[2] == 0x80 && code[3] == 0x00
+                && code[4] == 0x00 && code[5] == 0x00
+                && code[6] == 0x00) {
+                       in_nop = 1;
+                       code += 7;
+               }
+               if (code[0] == 0x0f && code[1] == 0x1f
+                && code[2] == 0x84 && code[3] == 0x00
+                && code[4] == 0x00 && code[5] == 0x00
+                && code[6] == 0x00 && code[7] == 0x00) {
+                       in_nop = 1;
+                       code += 8;
+               }
+       } while ( in_nop );
+       return code;
+}
+
+guint8*
+mono_arch_nacl_skip_nops (guint8* code)
+{
+  return amd64_skip_nops(code);
+}
+
+#endif /*__native_client_codegen__*/
+
 static inline void 
 amd64_patch (unsigned char* code, gpointer target)
 {
        guint8 rex = 0;
 
+#ifdef __native_client_codegen__
+       code = amd64_skip_nops (code);
+#endif
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       if (nacl_is_code_address (code)) {
+               /* For tail calls, code is patched after being installed */
+               /* but not through the normal "patch callsite" method.   */
+               unsigned char buf[kNaClAlignment];
+               unsigned char *aligned_code = (uintptr_t)code & ~kNaClAlignmentMask;
+               int ret;
+               memcpy (buf, aligned_code, kNaClAlignment);
+               /* Patch a temp buffer of bundle size, */
+               /* then install to actual location.    */
+               amd64_patch (buf + ((uintptr_t)code - (uintptr_t)aligned_code), target);
+               ret = nacl_dyncode_modify (aligned_code, buf, kNaClAlignment);
+               g_assert (ret == 0);
+               return;
+       }
+       target = nacl_modify_patch_target (target);
+#endif
+
        /* Skip REX */
        if ((code [0] >= 0x40) && (code [0] <= 0x4f)) {
                rex = code [0];
@@ -300,7 +567,9 @@ add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
 
     if (*gr >= PARAM_REGS) {
                ainfo->storage = ArgOnStack;
-               (*stack_size) += sizeof (gpointer);
+               /* Since the same stack slot size is used for all arg */
+               /*  types, it needs to be big enough to hold them all */
+               (*stack_size) += SIZEOF_REGISTER;
     }
     else {
                ainfo->storage = ArgInIReg;
@@ -322,7 +591,9 @@ add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
 
     if (*gr >= FLOAT_PARAM_REGS) {
                ainfo->storage = ArgOnStack;
-               (*stack_size) += sizeof (gpointer);
+               /* Since the same stack slot size is used for both float */
+               /*  types, it needs to be big enough to hold them both */
+               (*stack_size) += SIZEOF_REGISTER;
     }
     else {
                /* A double register */
@@ -417,6 +688,32 @@ merge_argument_class_from_type (MonoType *type, ArgumentClass class1)
 
        return class1;
 }
+#ifdef __native_client_codegen__
+const guint kNaClAlignment = kNaClAlignmentAMD64;
+const guint kNaClAlignmentMask = kNaClAlignmentMaskAMD64;
+
+/* Default alignment for Native Client is 32-byte. */
+gint8 nacl_align_byte = -32; /* signed version of 0xe0 */
+
+/* mono_arch_nacl_pad: Add pad bytes of alignment instructions at code,  */
+/* Check that alignment doesn't cross an alignment boundary.             */
+guint8*
+mono_arch_nacl_pad(guint8 *code, int pad)
+{
+       const int kMaxPadding = 8; /* see amd64-codegen.h:amd64_padding_size() */
+
+       if (pad == 0) return code;
+       /* assertion: alignment cannot cross a block boundary */
+       g_assert (((uintptr_t)code & (~kNaClAlignmentMask)) ==
+                (((uintptr_t)code + pad - 1) & (~kNaClAlignmentMask)));
+       while (pad >= kMaxPadding) {
+               amd64_padding (code, kMaxPadding);
+               pad -= kMaxPadding;
+       }
+       if (pad != 0) amd64_padding (code, pad);
+       return code;
+}
+#endif
 
 static void
 add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
@@ -424,6 +721,9 @@ add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgIn
                           guint32 *gr, guint32 *fr, guint32 *stack_size)
 {
        guint32 size, quad, nquads, i;
+       /* Keep track of the size used in each quad so we can */
+       /* use the right size when copying args/return vars.  */
+       guint32 quadsize [2] = {8, 8};
        ArgumentClass args [2];
        MonoMarshalType *info = NULL;
        MonoClass *klass;
@@ -452,6 +752,24 @@ add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgIn
        }
 #endif
 
+       /* If this struct can't be split up naturally into 8-byte */
+       /* chunks (registers), pass it on the stack.              */
+       if (sig->pinvoke && !pass_on_stack) {
+               info = mono_marshal_load_type_info (klass);
+               g_assert(info);
+               guint32 align;
+               guint32 field_size;
+               for (i = 0; i < info->num_fields; ++i) {
+                       field_size = mono_marshal_type_size (info->fields [i].field->type, 
+                                                          info->fields [i].mspec, 
+                                                          &align, TRUE, klass->unicode);
+                       if ((info->fields [i].offset < 8) && (info->fields [i].offset + field_size) > 8) {
+                               pass_on_stack = TRUE;
+                               break;
+                       }
+               }
+       }
+
        if (pass_on_stack) {
                /* Allways pass in memory */
                ainfo->offset = *stack_size;
@@ -551,6 +869,10 @@ add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgIn
                                if ((quad == 1) && (info->fields [i].offset < 8))
                                        continue;
 
+                               /* How far into this quad this data extends.*/
+                               /* (8 is size of quad) */
+                               quadsize [quad] = info->fields [i].offset + size - (quad * 8);
+
                                class1 = merge_argument_class_from_type (info->fields [i].field->type, class1);
                        }
                        g_assert (class1 != ARG_CLASS_NO_CLASS);
@@ -587,7 +909,9 @@ add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgIn
                                if (*fr >= FLOAT_PARAM_REGS)
                                        args [quad] = ARG_CLASS_MEMORY;
                                else {
-                                       ainfo->pair_storage [quad] = ArgInDoubleSSEReg;
+                                       if (quadsize[quad] <= 4)
+                                               ainfo->pair_storage [quad] = ArgInFloatSSEReg;
+                                       else ainfo->pair_storage [quad] = ArgInDoubleSSEReg;
                                        ainfo->pair_regs [quad] = *fr;
                                        (*fr) ++;
                                }
@@ -608,7 +932,7 @@ add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgIn
                        if (sig->pinvoke)
                                *stack_size += ALIGN_TO (info->native_size, 8);
                        else
-                               *stack_size += nquads * sizeof (gpointer);
+                               *stack_size += nquads * SIZEOF_REGISTER;
                        ainfo->storage = ArgOnStack;
                }
        }
@@ -905,6 +1229,9 @@ mono_amd64_tail_call_supported (MonoMethodSignature *caller_sig, MonoMethodSigna
 static int 
 cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
 {
+#if defined(MONO_CROSS_COMPILE)
+       return 0;
+#else
 #ifndef _MSC_VER
        __asm__ __volatile__ ("cpuid"
                : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
@@ -918,6 +1245,7 @@ cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
        *p_edx = info[3];
 #endif
        return 1;
+#endif
 }
 
 /*
@@ -951,6 +1279,12 @@ mono_arch_init (void)
        int flags;
 
        InitializeCriticalSection (&mini_arch_mutex);
+#if defined(__native_client_codegen__)
+       nacl_instruction_depth = TlsAlloc ();
+       TlsSetValue (nacl_instruction_depth, (gpointer)0);
+       nacl_rex_tag = TlsAlloc ();
+       nacl_legacy_prefix_tag = TlsAlloc ();
+#endif
 
 #ifdef MONO_ARCH_NOMAP32BIT
        flags = MONO_MMAP_READ;
@@ -983,6 +1317,11 @@ void
 mono_arch_cleanup (void)
 {
        DeleteCriticalSection (&mini_arch_mutex);
+#if defined(__native_client_codegen__)
+       TlsFree (nacl_instruction_depth);
+       TlsFree (nacl_rex_tag);
+       TlsFree (nacl_legacy_prefix_tag);
+#endif
 }
 
 /*
@@ -1114,6 +1453,13 @@ mono_arch_compute_omit_fp (MonoCompile *cfg)
        cfg->arch.omit_fp = TRUE;
        cfg->arch.omit_fp_computed = TRUE;
 
+#ifdef __native_client_codegen__
+       /* NaCl modules may not change the value of RBP, so it cannot be */
+       /* used as a normal register, but it can be used as a frame pointer*/
+       cfg->disable_omit_fp = TRUE;
+       cfg->arch.omit_fp = FALSE;
+#endif
+
        if (cfg->disable_omit_fp)
                cfg->arch.omit_fp = FALSE;
 
@@ -1170,7 +1516,9 @@ mono_arch_get_global_int_regs (MonoCompile *cfg)
                regs = g_list_prepend (regs, (gpointer)AMD64_R12);
                regs = g_list_prepend (regs, (gpointer)AMD64_R13);
                regs = g_list_prepend (regs, (gpointer)AMD64_R14);
+#ifndef __native_client_codegen__
                regs = g_list_prepend (regs, (gpointer)AMD64_R15);
+#endif
  
                regs = g_list_prepend (regs, (gpointer)AMD64_R10);
                regs = g_list_prepend (regs, (gpointer)AMD64_R9);
@@ -1189,7 +1537,9 @@ mono_arch_get_global_int_regs (MonoCompile *cfg)
                regs = g_list_prepend (regs, (gpointer)AMD64_R12);
                regs = g_list_prepend (regs, (gpointer)AMD64_R13);
                regs = g_list_prepend (regs, (gpointer)AMD64_R14);
+#ifndef __native_client_codegen__
                regs = g_list_prepend (regs, (gpointer)AMD64_R15);
+#endif
 #ifdef HOST_WIN32
                regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
                regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
@@ -1225,7 +1575,9 @@ mono_arch_get_iregs_clobbered_by_call (MonoCallInst *call)
                regs = g_list_prepend (regs, (gpointer)AMD64_R12);
                regs = g_list_prepend (regs, (gpointer)AMD64_R13);
                regs = g_list_prepend (regs, (gpointer)AMD64_R14);
+#ifndef __native_client_codegen__
                regs = g_list_prepend (regs, (gpointer)AMD64_R15);
+#endif
 
                regs = g_list_prepend (regs, (gpointer)AMD64_R10);
                regs = g_list_prepend (regs, (gpointer)AMD64_R9);
@@ -1426,7 +1778,7 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                /* Reserve space for caller saved registers */
                for (i = 0; i < AMD64_NREG; ++i)
                        if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
-                               offset += sizeof (gpointer);
+                               offset += SIZEOF_REGISTER;
                        }
        }
 
@@ -1555,12 +1907,12 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                                        ins->opcode = OP_REGOFFSET;
                                        ins->inst_basereg = cfg->frame_reg;
                                        /* These arguments are saved to the stack in the prolog */
-                                       offset = ALIGN_TO (offset, sizeof (gpointer));
+                                       offset = ALIGN_TO (offset, SIZEOF_REGISTER);
                                        if (cfg->arch.omit_fp) {
                                                ins->inst_offset = offset;
-                                               offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * sizeof (gpointer) : sizeof (gpointer);
+                                               offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * SIZEOF_REGISTER : SIZEOF_REGISTER;
                                        } else {
-                                               offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * sizeof (gpointer) : sizeof (gpointer);
+                                               offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * SIZEOF_REGISTER : SIZEOF_REGISTER;
                                                ins->inst_offset = - offset;
                                        }
                                        break;
@@ -1632,14 +1984,14 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                                ins->opcode = OP_REGOFFSET;
                                ins->inst_basereg = cfg->frame_reg;
                                /* These arguments are saved to the stack in the prolog */
-                               offset = ALIGN_TO (offset, sizeof (gpointer));
+                               offset = ALIGN_TO (offset, SIZEOF_REGISTER);
                                if (cfg->arch.omit_fp) {
                                        ins->inst_offset = offset;
-                                       offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * sizeof (gpointer) : sizeof (gpointer);
+                                       offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * SIZEOF_REGISTER : SIZEOF_REGISTER;
                                        // Arguments are yet supported by the stack map creation code
                                        //cfg->locals_max_stack_offset = MAX (cfg->locals_max_stack_offset, offset);
                                } else {
-                                       offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * sizeof (gpointer) : sizeof (gpointer);
+                                       offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * SIZEOF_REGISTER : SIZEOF_REGISTER;
                                        ins->inst_offset = - offset;
                                        //cfg->locals_min_stack_offset = MIN (cfg->locals_min_stack_offset, offset);
                                }
@@ -1735,7 +2087,11 @@ arg_storage_to_load_membase (ArgStorage storage)
 {
        switch (storage) {
        case ArgInIReg:
+#if defined(__default_codegen__)
                return OP_LOAD_MEMBASE;
+#elif defined(__native_client_codegen__)
+               return OP_LOADI8_MEMBASE;
+#endif
        case ArgInDoubleSSEReg:
                return OP_LOADR8_MEMBASE;
        case ArgInFloatSSEReg:
@@ -2139,7 +2495,7 @@ mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
 
                        MONO_INST_NEW (cfg, load, arg_storage_to_load_membase (ainfo->pair_storage [part]));
                        load->inst_basereg = src->dreg;
-                       load->inst_offset = part * sizeof (gpointer);
+                       load->inst_offset = part * SIZEOF_REGISTER;
 
                        switch (ainfo->pair_storage [part]) {
                        case ArgInIReg:
@@ -2351,6 +2707,15 @@ mono_arch_dyn_call_free (MonoDynCallInfo *info)
        g_free (ainfo);
 }
 
+#if !defined(__native_client__)
+#define PTR_TO_GREG(ptr) (mgreg_t)(ptr)
+#define GREG_TO_PTR(greg) (gpointer)(greg)
+#else
+/* Correctly handle casts to/from 32-bit pointers without compiler warnings */
+#define PTR_TO_GREG(ptr) (mgreg_t)(uintptr_t)(ptr)
+#define GREG_TO_PTR(greg) (gpointer)(guint32)(greg)
+#endif
+
 /*
  * mono_arch_get_start_dyn_call:
  *
@@ -2383,20 +2748,20 @@ mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, g
        pindex = 0;
 
        if (sig->hasthis || dinfo->cinfo->vret_arg_index == 1) {
-               p->regs [greg ++] = (mgreg_t)*(args [arg_index ++]);
+               p->regs [greg ++] = PTR_TO_GREG(*(args [arg_index ++]));
                if (!sig->hasthis)
                        pindex = 1;
        }
 
        if (dinfo->cinfo->vtype_retaddr)
-               p->regs [greg ++] = (mgreg_t)ret;
+               p->regs [greg ++] = PTR_TO_GREG(ret);
 
        for (i = pindex; i < sig->param_count; i++) {
                MonoType *t = mono_type_get_underlying_type (sig->params [i]);
                gpointer *arg = args [arg_index ++];
 
                if (t->byref) {
-                       p->regs [greg ++] = (mgreg_t)*(arg);
+                       p->regs [greg ++] = PTR_TO_GREG(*(arg));
                        continue;
                }
 
@@ -2409,11 +2774,20 @@ mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, g
                case MONO_TYPE_PTR:
                case MONO_TYPE_I:
                case MONO_TYPE_U:
+#if !defined(__native_client__)
                case MONO_TYPE_I8:
                case MONO_TYPE_U8:
+#endif
                        g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
-                       p->regs [greg ++] = (mgreg_t)*(arg);
+                       p->regs [greg ++] = PTR_TO_GREG(*(arg));
                        break;
+#if defined(__native_client__)
+               case MONO_TYPE_I8:
+               case MONO_TYPE_U8:
+                       g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
+                       p->regs [greg ++] = *(guint64*)(arg);
+                       break;
+#endif
                case MONO_TYPE_BOOLEAN:
                case MONO_TYPE_U1:
                        p->regs [greg ++] = *(guint8*)(arg);
@@ -2436,7 +2810,7 @@ mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, g
                        break;
                case MONO_TYPE_GENERICINST:
                    if (MONO_TYPE_IS_REFERENCE (t)) {
-                               p->regs [greg ++] = (mgreg_t)*(arg);
+                               p->regs [greg ++] = PTR_TO_GREG(*(arg));
                                break;
                        } else {
                                /* Fall through */
@@ -2492,7 +2866,7 @@ mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
        case MONO_TYPE_I:
        case MONO_TYPE_U:
        case MONO_TYPE_PTR:
-               *(gpointer*)ret = (gpointer)res;
+               *(gpointer*)ret = GREG_TO_PTR(res);
                break;
        case MONO_TYPE_I1:
                *(gint8*)ret = res;
@@ -2522,7 +2896,7 @@ mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
                break;
        case MONO_TYPE_GENERICINST:
                if (MONO_TYPE_IS_REFERENCE (sig->ret)) {
-                       *(gpointer*)ret = (gpointer)res;
+                       *(gpointer*)ret = GREG_TO_PTR(res);
                        break;
                } else {
                        /* Fall through */
@@ -2675,8 +3049,10 @@ emit_call_body (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointe
                         * not span cache lines. This is required for code patching to work on SMP
                         * systems.
                         */
-                       if (!no_patch && ((guint32)(code + 1 - cfg->native_code) % 4) != 0)
-                               amd64_padding (code, 4 - ((guint32)(code + 1 - cfg->native_code) % 4));
+                       if (!no_patch && ((guint32)(code + 1 - cfg->native_code) % 4) != 0) {
+                               guint32 pad_size = 4 - ((guint32)(code + 1 - cfg->native_code) % 4);
+                               amd64_padding (code, pad_size);
+                       }
                        mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
                        amd64_call_code (code, 0);
                }
@@ -2933,6 +3309,11 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                                ins->sreg2 = temp->dreg;
                        }
                        break;
+#ifndef __native_client_codegen__
+               /* In AMD64 NaCl, pointers are 4 bytes, */
+               /*  so LOAD_* != LOADI8_* */
+               /* Also, don't generate memindex opcodes (to simplify */
+               /*  read sandboxing) */
                case OP_LOAD_MEMBASE:
                case OP_LOADI8_MEMBASE:
                        if (!amd64_is_imm32 (ins->inst_offset)) {
@@ -2943,7 +3324,10 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                                ins->inst_indexreg = temp->dreg;
                        }
                        break;
+#endif
+#ifndef __native_client_codegen__
                case OP_STORE_MEMBASE_IMM:
+#endif
                case OP_STOREI8_MEMBASE_IMM:
                        if (!amd64_is_imm32 (ins->inst_imm)) {
                                NEW_INS (cfg, ins, temp, OP_I8CONST);
@@ -3095,8 +3479,20 @@ mono_emit_stack_alloc (MonoCompile *cfg, guchar *code, MonoInst* tree)
                if (cfg->param_area && cfg->arch.no_pushes)
                        amd64_alu_reg_imm (code, X86_ADD, AMD64_RDI, cfg->param_area);
                amd64_cld (code);
+#if defined(__default_codegen__)
+               amd64_prefix (code, X86_REP_PREFIX);
+               amd64_stosl (code);
+#elif defined(__native_client_codegen__)
+               /* NaCl stos pseudo-instruction */
+               amd64_codegen_pre(code);
+               /* First, clear the upper 32 bits of RDI (mov %edi, %edi)  */
+               amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
+               /* Add %r15 to %rdi using lea, condition flags unaffected. */
+               amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
                amd64_prefix (code, X86_REP_PREFIX);
                amd64_stosl (code);
+               amd64_codegen_post(code);
+#endif /* __native_client_codegen__ */
                
                if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI)
                        amd64_pop_reg (code, AMD64_RDI);
@@ -3148,12 +3544,12 @@ emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
 
                        /* Load the destination address */
                        g_assert (loc->opcode == OP_REGOFFSET);
-                       amd64_mov_reg_membase (code, AMD64_RCX, loc->inst_basereg, loc->inst_offset, 8);
+                       amd64_mov_reg_membase (code, AMD64_RCX, loc->inst_basereg, loc->inst_offset, SIZEOF_VOID_P);
 
                        for (quad = 0; quad < 2; quad ++) {
                                switch (cinfo->ret.pair_storage [quad]) {
                                case ArgInIReg:
-                                       amd64_mov_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad], 8);
+                                       amd64_mov_membase_reg (code, AMD64_RCX, (quad * SIZEOF_REGISTER), cinfo->ret.pair_regs [quad], SIZEOF_REGISTER);
                                        break;
                                case ArgInFloatSSEReg:
                                        amd64_movss_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
@@ -3229,6 +3625,15 @@ amd64_pop_reg (code, AMD64_RAX);
 
 #ifndef DISABLE_JIT
 
+#if defined(__native_client__) || defined(__native_client_codegen__)
+void mono_nacl_gc()
+{
+#ifdef __native_client_gc__
+       __nacl_suspend_thread_if_needed();
+#endif
+}
+#endif
+
 void
 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 {
@@ -3262,6 +3667,21 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                }
        }
 
+#if defined(__native_client_codegen__)
+       /* For Native Client, all indirect call/jump targets must be */
+       /* 32-byte aligned.  Exception handler blocks are jumped to  */
+       /* indirectly as well.                                       */
+       gboolean bb_needs_alignment = (bb->flags & BB_INDIRECT_JUMP_TARGET) ||
+                                     (bb->flags & BB_EXCEPTION_HANDLER);
+
+       if ( bb_needs_alignment && ((cfg->code_len & kNaClAlignmentMask) != 0)) {
+               int pad = kNaClAlignment - (cfg->code_len & kNaClAlignmentMask);
+               if (pad != kNaClAlignment) code = mono_arch_nacl_pad(code, pad);
+               cfg->code_len += pad;
+               bb->native_offset = cfg->code_len;
+       }
+#endif  /*__native_client_codegen__*/
+
        if (cfg->verbose_level > 2)
                g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
 
@@ -3287,9 +3707,11 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 
                max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
 
-               if (G_UNLIKELY (offset > (cfg->code_size - max_len - 16))) {
+#define EXTRA_CODE_SPACE (NACL_SIZE (16, 16 + kNaClAlignment))
+
+               if (G_UNLIKELY (offset > (cfg->code_size - max_len - EXTRA_CODE_SPACE))) {
                        cfg->code_size *= 2;
-                       cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
+                       cfg->native_code = mono_realloc_native_code(cfg);
                        code = cfg->native_code + offset;
                        mono_jit_stats.code_reallocs++;
                }
@@ -3322,7 +3744,11 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_STOREI2_MEMBASE_REG:
                        amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
                        break;
+               /* In AMD64 NaCl, pointers are 4 bytes, */
+               /*  so STORE_* != STOREI8_*. Likewise below. */
                case OP_STORE_MEMBASE_REG:
+                       amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, sizeof(gpointer));
+                       break;
                case OP_STOREI8_MEMBASE_REG:
                        amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 8);
                        break;
@@ -3330,15 +3756,32 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
                        break;
                case OP_STORE_MEMBASE_IMM:
+#ifndef __native_client_codegen__
+                       /* In NaCl, this could be a PCONST type, which could */
+                       /* mean a pointer type was copied directly into the  */
+                       /* lower 32-bits of inst_imm, so for InvalidPtr==-1  */
+                       /* the value would be 0x00000000FFFFFFFF which is    */
+                       /* not proper for an imm32 unless you cast it.       */
+                       g_assert (amd64_is_imm32 (ins->inst_imm));
+#endif
+                       amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, (gint32)ins->inst_imm, sizeof(gpointer));
+                       break;
                case OP_STOREI8_MEMBASE_IMM:
                        g_assert (amd64_is_imm32 (ins->inst_imm));
                        amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 8);
                        break;
                case OP_LOAD_MEM:
+#ifdef __native_client_codegen__
+                       /* For NaCl, pointers are 4 bytes, so separate these */
+                       /* cases, use literal 8 below where we really want 8 */
+                       amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
+                       amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, sizeof(gpointer));
+                       break;
+#endif
                case OP_LOADI8_MEM:
                        // FIXME: Decompose this earlier
                        if (amd64_is_imm32 (ins->inst_imm))
-                               amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, sizeof (gpointer));
+                               amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 8);
                        else {
                                amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
                                amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 8);
@@ -3362,13 +3805,20 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, FALSE);
                        break;
                case OP_LOADU2_MEM:
+                       /* For NaCl, pointers are 4 bytes, so separate these */
+                       /* cases, use literal 8 below where we really want 8 */
                        amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
                        amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, TRUE);
                        break;
                case OP_LOAD_MEMBASE:
+                       g_assert (amd64_is_imm32 (ins->inst_offset));
+                       amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, sizeof(gpointer));
+                       break;
                case OP_LOADI8_MEMBASE:
+                       /* Use literal 8 instead of sizeof pointer or */
+                       /* register, we really want 8 for this opcode */
                        g_assert (amd64_is_imm32 (ins->inst_offset));
-                       amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, sizeof (gpointer));
+                       amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 8);
                        break;
                case OP_LOADI4_MEMBASE:
                        amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
@@ -4056,14 +4506,14 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                case OP_AOTCONST:
                        mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
-                       amd64_mov_reg_membase (code, ins->dreg, AMD64_RIP, 0, 8);
+                       amd64_mov_reg_membase (code, ins->dreg, AMD64_RIP, 0, sizeof(gpointer));
                        break;
                case OP_JUMP_TABLE:
                        mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
                        amd64_mov_reg_imm_size (code, ins->dreg, 0, 8);
                        break;
                case OP_MOVE:
-                       amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, sizeof (gpointer));
+                       amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, SIZEOF_REGISTER);
                        break;
                case OP_AMD64_SET_XMMREG_R4: {
                        amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
@@ -4101,20 +4551,20 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        else {
                                for (i = 0; i < AMD64_NREG; ++i)
                                        if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i)))
-                                               pos -= sizeof (gpointer);
+                                               pos -= SIZEOF_REGISTER;
 
                                /* Restore callee-saved registers */
                                for (i = AMD64_NREG - 1; i > 0; --i) {
                                        if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
-                                               amd64_mov_reg_membase (code, i, AMD64_RBP, pos, 8);
-                                               pos += 8;
+                                               amd64_mov_reg_membase (code, i, AMD64_RBP, pos, SIZEOF_REGISTER);
+                                               pos += SIZEOF_REGISTER;
                                        }
                                }
 
                                /* Copy arguments on the stack to our argument area */
-                               for (i = 0; i < call->stack_usage; i += 8) {
-                                       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, i, 8);
-                                       amd64_mov_membase_reg (code, AMD64_RBP, 16 + i, AMD64_RAX, 8);
+                               for (i = 0; i < call->stack_usage; i += SIZEOF_REGISTER) {
+                                       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, i, SIZEOF_REGISTER);
+                                       amd64_mov_membase_reg (code, AMD64_RBP, 16 + i, AMD64_RAX, SIZEOF_REGISTER);
                                }
                        
                                if (pos)
@@ -4138,7 +4588,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                case OP_ARGLIST: {
                        amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, cfg->sig_cookie);
-                       amd64_mov_membase_reg (code, ins->sreg1, 0, AMD64_R11, 8);
+                       amd64_mov_membase_reg (code, ins->sreg1, 0, AMD64_R11, sizeof(gpointer));
                        break;
                }
                case OP_CALL:
@@ -4255,7 +4705,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 
                        /* Set argument registers */
                        for (i = 0; i < PARAM_REGS; ++i)
-                               amd64_mov_reg_membase (code, param_regs [i], AMD64_R11, i * sizeof (gpointer), 8);
+                               amd64_mov_reg_membase (code, param_regs [i], AMD64_R11, i * SIZEOF_REGISTER, SIZEOF_REGISTER);
                        
                        /* Make the call */
                        amd64_call_reg (code, AMD64_R10);
@@ -4373,8 +4823,11 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
                        break;
                case OP_START_HANDLER: {
+                       /* Even though we're saving RSP, use sizeof */
+                       /* gpointer because spvar is of type IntPtr */
+                       /* see: mono_create_spvar_for_region */
                        MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
-                       amd64_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, AMD64_RSP, 8);
+                       amd64_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, AMD64_RSP, sizeof(gpointer));
 
                        if ((MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY) ||
                                 MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY)) &&
@@ -4385,13 +4838,13 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                }
                case OP_ENDFINALLY: {
                        MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
-                       amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, 8);
+                       amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
                        amd64_ret (code);
                        break;
                }
                case OP_ENDFILTER: {
                        MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
-                       amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, 8);
+                       amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
                        /* The local allocator will put the result into RAX */
                        amd64_ret (code);
                        break;
@@ -5647,15 +6100,23 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
                        break;
                }
+               case OP_NACL_GC_SAFE_POINT: {
+#if defined(__native_client_codegen__)
+                       code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)mono_nacl_gc, TRUE);
+#endif
+                       break;
+               }
                default:
                        g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
                        g_assert_not_reached ();
                }
 
                if ((code - cfg->native_code - offset) > max_len) {
+#if !defined(__native_client_codegen__)
                        g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %ld)",
                                   mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
                        g_assert_not_reached ();
+#endif
                }
               
                last_ins = ins;
@@ -5785,10 +6246,27 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        gint32 lmf_offset = cfg->arch.lmf_offset;
        gboolean args_clobbered = FALSE;
        gboolean trace = FALSE;
+#ifdef __native_client_codegen__
+       guint alignment_check;
+#endif
 
        cfg->code_size =  MAX (cfg->header->code_size * 4, 10240);
 
+#if defined(__default_codegen__)
        code = cfg->native_code = g_malloc (cfg->code_size);
+#elif defined(__native_client_codegen__)
+       /* native_code_alloc is not 32-byte aligned, native_code is. */
+       cfg->native_code_alloc = g_malloc (cfg->code_size + kNaClAlignment);
+
+       /* Align native_code to next nearest kNaclAlignment byte. */
+       cfg->native_code = (uintptr_t)cfg->native_code_alloc + kNaClAlignment;
+       cfg->native_code = (uintptr_t)cfg->native_code & ~kNaClAlignmentMask;
+
+       code = cfg->native_code;
+
+       alignment_check = (guint)cfg->native_code & kNaClAlignmentMask;
+       g_assert (alignment_check == 0);
+#endif
 
        if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
                trace = TRUE;
@@ -5831,7 +6309,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                mono_arch_unwindinfo_add_push_nonvol (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
 #endif
                
-               amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof (gpointer));
+               amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, SIZEOF_REGISTER);
                mono_emit_unwind_op_def_cfa_reg (cfg, code, AMD64_RBP);
                async_exc_point (code);
 #ifdef HOST_WIN32
@@ -5846,7 +6324,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                for (i = 0; i < AMD64_NREG; ++i)
                        if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
                                amd64_push_reg (code, i);
-                               pos += sizeof (gpointer);
+                               pos += 8; /* AMD64 push inst is always 8 bytes, no way to change it */
                                offset += 8;
                                mono_emit_unwind_op_offset (cfg, code, i, - offset);
                                async_exc_point (code);
@@ -5859,7 +6337,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                if (cfg->arch.omit_fp)
                        // FIXME:
                        g_assert_not_reached ();
-               cfg->stack_offset += ALIGN_TO (cfg->param_area, sizeof (gpointer));
+               cfg->stack_offset += ALIGN_TO (cfg->param_area, SIZEOF_REGISTER);
        }
 
        if (cfg->arch.omit_fp) {
@@ -5890,7 +6368,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
                        while (required_code_size >= (cfg->code_size - offset))
                                cfg->code_size *= 2;
-                       cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
+                       cfg->native_code = mono_realloc_native_code (cfg);
                        code = cfg->native_code + offset;
                        mono_jit_stats.code_reallocs++;
                }
@@ -5956,8 +6434,20 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RSP, 8);
 
                amd64_cld (code);
+#if defined(__default_codegen__)
+               amd64_prefix (code, X86_REP_PREFIX);
+               amd64_stosl (code);
+#elif defined(__native_client_codegen__)
+               /* NaCl stos pseudo-instruction */
+               amd64_codegen_pre (code);
+               /* First, clear the upper 32 bits of RDI (mov %edi, %edi)  */
+               amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
+               /* Add %r15 to %rdi using lea, condition flags unaffected. */
+               amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
                amd64_prefix (code, X86_REP_PREFIX);
                amd64_stosl (code);
+               amd64_codegen_post (code);
+#endif /* __native_client_codegen__ */
 
                amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RSP, -8, 8);
                amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
@@ -5985,7 +6475,9 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                        case AMD64_R12: offset = G_STRUCT_OFFSET (MonoLMF, r12); break;
                        case AMD64_R13: offset = G_STRUCT_OFFSET (MonoLMF, r13); break;
                        case AMD64_R14: offset = G_STRUCT_OFFSET (MonoLMF, r14); break;
+#ifndef __native_client_codegen__
                        case AMD64_R15: offset = G_STRUCT_OFFSET (MonoLMF, r15); break;
+#endif
 #ifdef HOST_WIN32
                        case AMD64_RDI: offset = G_STRUCT_OFFSET (MonoLMF, rdi); break;
                        case AMD64_RSI: offset = G_STRUCT_OFFSET (MonoLMF, rsi); break;
@@ -6024,7 +6516,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET &&
                                (cfg->rgctx_var->inst_basereg == AMD64_RBP || cfg->rgctx_var->inst_basereg == AMD64_RSP));
 
-               amd64_mov_membase_reg (code, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, 8);
+               amd64_mov_membase_reg (code, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, sizeof(gpointer));
        }
 
        /* compute max_length in order to use short forward jumps */
@@ -6039,8 +6531,22 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                        /* max alignment for loops */
                        if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
                                max_length += LOOP_ALIGNMENT;
+#ifdef __native_client_codegen__
+                       /* max alignment for native client */
+                       max_length += kNaClAlignment;
+#endif
 
                        MONO_BB_FOR_EACH_INS (bb, ins) {
+#ifdef __native_client_codegen__
+                               {
+                                       int space_in_block = kNaClAlignment -
+                                               ((max_length + cfg->code_len) & kNaClAlignmentMask);
+                                       int max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
+                                       if (space_in_block < max_len && max_len < kNaClAlignment) {
+                                               max_length += space_in_block;
+                                       }
+                               }
+#endif  /*__native_client_codegen__*/
                                max_length += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
                        }
 
@@ -6092,13 +6598,13 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                                for (quad = 0; quad < 2; quad ++) {
                                        switch (ainfo->pair_storage [quad]) {
                                        case ArgInIReg:
-                                               amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad], sizeof (gpointer));
+                                               amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * SIZEOF_REGISTER), ainfo->pair_regs [quad], SIZEOF_REGISTER);
                                                break;
                                        case ArgInFloatSSEReg:
-                                               amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
+                                               amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * SIZEOF_REGISTER), ainfo->pair_regs [quad]);
                                                break;
                                        case ArgInDoubleSSEReg:
-                                               amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
+                                               amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * SIZEOF_REGISTER), ainfo->pair_regs [quad]);
                                                break;
                                        case ArgNone:
                                                break;
@@ -6144,13 +6650,13 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                                for (quad = 0; quad < 2; quad ++) {
                                        switch (ainfo->pair_storage [quad]) {
                                        case ArgInIReg:
-                                               amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad], sizeof (gpointer));
+                                               amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * SIZEOF_REGISTER), ainfo->pair_regs [quad], SIZEOF_REGISTER);
                                                break;
                                        case ArgInFloatSSEReg:
-                                               amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
+                                               amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * SIZEOF_REGISTER), ainfo->pair_regs [quad]);
                                                break;
                                        case ArgInDoubleSSEReg:
-                                               amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
+                                               amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * SIZEOF_REGISTER), ainfo->pair_regs [quad]);
                                                break;
                                        case ArgNone:
                                                break;
@@ -6278,13 +6784,13 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                        }
 
                        /* Save lmf_addr */
-                       amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, 8);
+                       amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, sizeof(gpointer));
                        /* Save previous_lmf */
-                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, 8);
-                       amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, 8);
+                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, sizeof(gpointer));
+                       amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, sizeof(gpointer));
                        /* Set new lmf */
                        amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, lmf_offset);
-                       amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, 8);
+                       amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, sizeof(gpointer));
                }
        }
 
@@ -6395,7 +6901,7 @@ mono_arch_emit_epilog (MonoCompile *cfg)
 
        while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
                cfg->code_size *= 2;
-               cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
+               cfg->native_code = mono_realloc_native_code (cfg);
                mono_jit_stats.code_reallocs++;
        }
 
@@ -6431,14 +6937,14 @@ mono_arch_emit_epilog (MonoCompile *cfg)
                         * through the mono_lmf_addr TLS variable.
                         */
                        /* reg = previous_lmf */
-                       amd64_mov_reg_membase (code, AMD64_R11, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
+                       amd64_mov_reg_membase (code, AMD64_R11, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof(gpointer));
                        x86_prefix (code, X86_FS_PREFIX);
                        amd64_mov_mem_reg (code, lmf_tls_offset, AMD64_R11, 8);
                } else {
                        /* Restore previous lmf */
-                       amd64_mov_reg_membase (code, AMD64_RCX, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
-                       amd64_mov_reg_membase (code, AMD64_R11, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 8);
-                       amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, 8);
+                       amd64_mov_reg_membase (code, AMD64_RCX, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof(gpointer));
+                       amd64_mov_reg_membase (code, AMD64_R11, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sizeof(gpointer));
+                       amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, sizeof(gpointer));
                }
 
                /* Restore caller saved regs */
@@ -6458,7 +6964,11 @@ mono_arch_emit_epilog (MonoCompile *cfg)
                        amd64_mov_reg_membase (code, AMD64_R14, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), 8);
                }
                if (cfg->used_int_regs & (1 << AMD64_R15)) {
+#if defined(__default_codegen__)
                        amd64_mov_reg_membase (code, AMD64_R15, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), 8);
+#elif defined(__native_client_codegen__)
+                       g_assert_not_reached();
+#endif
                }
 #ifdef HOST_WIN32
                if (cfg->used_int_regs & (1 << AMD64_RDI)) {
@@ -6482,10 +6992,10 @@ mono_arch_emit_epilog (MonoCompile *cfg)
                else {
                        for (i = 0; i < AMD64_NREG; ++i)
                                if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i)))
-                                       pos -= sizeof (gpointer);
+                                       pos -= SIZEOF_REGISTER;
 
                        if (pos) {
-                               if (pos == - sizeof (gpointer)) {
+                               if (pos == - SIZEOF_REGISTER) {
                                        /* Only one register, so avoid lea */
                                        for (i = AMD64_NREG - 1; i > 0; --i)
                                                if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
@@ -6514,13 +7024,13 @@ mono_arch_emit_epilog (MonoCompile *cfg)
                for (quad = 0; quad < 2; quad ++) {
                        switch (ainfo->pair_storage [quad]) {
                        case ArgInIReg:
-                               amd64_mov_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof (gpointer)), sizeof (gpointer));
+                               amd64_mov_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * SIZEOF_REGISTER), SIZEOF_REGISTER);
                                break;
                        case ArgInFloatSSEReg:
-                               amd64_movss_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof (gpointer)));
+                               amd64_movss_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * SIZEOF_REGISTER));
                                break;
                        case ArgInDoubleSSEReg:
-                               amd64_movsd_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof (gpointer)));
+                               amd64_movsd_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * SIZEOF_REGISTER));
                                break;
                        case ArgNone:
                                break;
@@ -6566,9 +7076,16 @@ mono_arch_emit_exceptions (MonoCompile *cfg)
                        code_size += 8 + 7; /*sizeof (void*) + alignment */
        }
 
+#ifdef __native_client_codegen__
+       /* Give us extra room on Native Client.  This could be   */
+       /* more carefully calculated, but bundle alignment makes */
+       /* it much trickier, so *2 like other places is good.    */
+       code_size *= 2;
+#endif
+
        while (cfg->code_len + code_size > (cfg->code_size - 16)) {
                cfg->code_size *= 2;
-               cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
+               cfg->native_code = mono_realloc_native_code (cfg);
                mono_jit_stats.code_reallocs++;
        }
 
@@ -6629,6 +7146,7 @@ mono_arch_emit_exceptions (MonoCompile *cfg)
                        /* do nothing */
                        break;
                }
+               g_assert(code < cfg->native_code + cfg->code_size);
        }
 
        /* Handle relocations with RIP relative addressing */
@@ -6639,27 +7157,69 @@ mono_arch_emit_exceptions (MonoCompile *cfg)
                switch (patch_info->type) {
                case MONO_PATCH_INFO_R8:
                case MONO_PATCH_INFO_R4: {
-                       guint8 *pos;
+                       guint8 *pos, *patch_pos, *target_pos;
 
                        /* The SSE opcodes require a 16 byte alignment */
+#if defined(__default_codegen__)
                        code = (guint8*)ALIGN_TO (code, 16);
-                       memset (orig_code, 0, code - orig_code);
+#elif defined(__native_client_codegen__)
+                       {
+                               /* Pad this out with HLT instructions  */
+                               /* or we can get garbage bytes emitted */
+                               /* which will fail validation          */
+                               guint8 *aligned_code;
+                               /* extra align to make room for  */
+                               /* mov/push below                      */
+                               int extra_align = patch_info->type == MONO_PATCH_INFO_R8 ? 2 : 1;
+                               aligned_code = (guint8*)ALIGN_TO (code + extra_align, 16);
+                               /* The technique of hiding data in an  */
+                               /* instruction has a problem here: we  */
+                               /* need the data aligned to a 16-byte  */
+                               /* boundary but the instruction cannot */
+                               /* cross the bundle boundary. so only  */
+                               /* odd multiples of 16 can be used     */
+                               if ((intptr_t)aligned_code % kNaClAlignment == 0) {
+                                       aligned_code += 16;
+                               }
+                               while (code < aligned_code) {
+                                       *(code++) = 0xf4; /* hlt */
+                               }
+                       }       
+#endif
 
                        pos = cfg->native_code + patch_info->ip.i;
-
-                       if (IS_REX (pos [1]))
-                               *(guint32*)(pos + 5) = (guint8*)code - pos - 9;
-                       else
-                               *(guint32*)(pos + 4) = (guint8*)code - pos - 8;
+                       if (IS_REX (pos [1])) {
+                               patch_pos = pos + 5;
+                               target_pos = code - pos - 9;
+                       }
+                       else {
+                               patch_pos = pos + 4;
+                               target_pos = code - pos - 8;
+                       }
 
                        if (patch_info->type == MONO_PATCH_INFO_R8) {
+#ifdef __native_client_codegen__
+                               /* Hide 64-bit data in a         */
+                               /* "mov imm64, r11" instruction. */
+                               /* write it before the start of  */
+                               /* the data*/
+                               *(code-2) = 0x49; /* prefix      */
+                               *(code-1) = 0xbb; /* mov X, %r11 */
+#endif
                                *(double*)code = *(double*)patch_info->data.target;
                                code += sizeof (double);
                        } else {
+#ifdef __native_client_codegen__
+                               /* Hide 32-bit data in a        */
+                               /* "push imm32" instruction.    */
+                               *(code-1) = 0x68; /* push */
+#endif
                                *(float*)code = *(float*)patch_info->data.target;
                                code += sizeof (float);
                        }
 
+                       *(guint32*)(patch_pos) = target_pos;
+
                        remove = TRUE;
                        break;
                }
@@ -6702,6 +7262,7 @@ mono_arch_emit_exceptions (MonoCompile *cfg)
                                tmp->next = patch_info->next;
                        }
                }
+               g_assert (code < cfg->native_code + cfg->code_size);
        }
 
        cfg->code_len = code - cfg->native_code;
@@ -7072,6 +7633,10 @@ get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *cod
                g_assert ((code - start) < 64);
        }
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate(&start, 64, &code);
+#endif
+
        mono_debug_add_delegate_trampoline (start, code - start);
 
        if (code_len)
@@ -7216,6 +7781,7 @@ mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
 
 #ifdef MONO_ARCH_HAVE_IMT
 
+#if defined(__default_codegen__)
 #define CMP_SIZE (6 + 1)
 #define CMP_REG_REG_SIZE (4 + 1)
 #define BR_SMALL_SIZE 2
@@ -7223,6 +7789,20 @@ mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
 #define MOV_REG_IMM_SIZE 10
 #define MOV_REG_IMM_32BIT_SIZE 6
 #define JUMP_REG_SIZE (2 + 1)
+#elif defined(__native_client_codegen__)
+/* NaCl N-byte instructions can be padded up to N-1 bytes */
+#define CMP_SIZE ((6 + 1) * 2 - 1)
+#define CMP_REG_REG_SIZE ((4 + 1) * 2 - 1)
+#define BR_SMALL_SIZE (2 * 2 - 1)
+#define BR_LARGE_SIZE (6 * 2 - 1)
+#define MOV_REG_IMM_SIZE (10 * 2 - 1)
+#define MOV_REG_IMM_32BIT_SIZE (6 * 2 - 1)
+/* Jump reg for NaCl adds a mask (+4) and add (+3) */
+#define JUMP_REG_SIZE ((2 + 1 + 4 + 3) * 2 - 1)
+/* Jump membase's size is large and unpredictable    */
+/* in native client, just pad it out a whole bundle. */
+#define JUMP_MEMBASE_SIZE (kNaClAlignment)
+#endif
 
 static int
 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
@@ -7262,6 +7842,9 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
                                                item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
                                        else
                                                item->chunk_size += MOV_REG_IMM_SIZE;
+#ifdef __native_client_codegen__
+                                       item->chunk_size += JUMP_MEMBASE_SIZE;
+#endif
                                }
                                item->chunk_size += BR_SMALL_SIZE + JUMP_REG_SIZE;
                        } else {
@@ -7277,6 +7860,9 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
                                        /* with assert below:
                                         * item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
                                         */
+#ifdef __native_client_codegen__
+                                       item->chunk_size += JUMP_MEMBASE_SIZE;
+#endif
                                }
                        }
                } else {
@@ -7289,10 +7875,16 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
                }
                size += item->chunk_size;
        }
+#if defined(__native_client__) && defined(__native_client_codegen__)
+       /* In Native Client, we don't re-use thunks, allocate from the */
+       /* normal code manager paths. */
+       code = mono_domain_code_reserve (domain, size);
+#else
        if (fail_tramp)
                code = mono_method_alloc_generic_virtual_thunk (domain, size);
        else
                code = mono_domain_code_reserve (domain, size);
+#endif
        start = code;
        for (i = 0; i < count; ++i) {
                MonoIMTCheckItem *item = imt_entries [i];
@@ -7305,24 +7897,24 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
                                        if (amd64_is_imm32 (item->key))
                                                amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key);
                                        else {
-                                               amd64_mov_reg_imm (code, AMD64_R11, item->key);
-                                               amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, AMD64_R11);
+                                               amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
+                                               amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
                                        }
                                }
                                item->jmp_code = code;
                                amd64_branch8 (code, X86_CC_NE, 0, FALSE);
                                if (item->has_target_code) {
-                                       amd64_mov_reg_imm (code, AMD64_R11, item->value.target_code);
-                                       amd64_jump_reg (code, AMD64_R11);
+                                       amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->value.target_code);
+                                       amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
                                } else {
-                                       amd64_mov_reg_imm (code, AMD64_R11, & (vtable->vtable [item->value.vtable_slot]));
-                                       amd64_jump_membase (code, AMD64_R11, 0);
+                                       amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
+                                       amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
                                }
 
                                if (fail_case) {
                                        amd64_patch (item->jmp_code, code);
-                                       amd64_mov_reg_imm (code, AMD64_R11, fail_tramp);
-                                       amd64_jump_reg (code, AMD64_R11);
+                                       amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, fail_tramp);
+                                       amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
                                        item->jmp_code = NULL;
                                }
                        } else {
@@ -7331,27 +7923,33 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
                                if (amd64_is_imm32 (item->key))
                                        amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key);
                                else {
-                                       amd64_mov_reg_imm (code, AMD64_R11, item->key);
-                                       amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, AMD64_R11);
+                                       amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
+                                       amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
                                }
                                item->jmp_code = code;
                                amd64_branch8 (code, X86_CC_NE, 0, FALSE);
-                               amd64_mov_reg_imm (code, AMD64_R11, & (vtable->vtable [item->value.vtable_slot]));
-                               amd64_jump_membase (code, AMD64_R11, 0);
+                               /* See the comment below about R10 */
+                               amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
+                               amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
                                amd64_patch (item->jmp_code, code);
                                amd64_breakpoint (code);
                                item->jmp_code = NULL;
 #else
-                               amd64_mov_reg_imm (code, AMD64_R11, & (vtable->vtable [item->value.vtable_slot]));
-                               amd64_jump_membase (code, AMD64_R11, 0);
+                               /* We're using R10 (MONO_ARCH_IMT_SCRATCH_REG) here because R11 (MONO_ARCH_IMT_REG)
+                                  needs to be preserved.  R10 needs
+                                  to be preserved for calls which
+                                  require a runtime generic context,
+                                  but interface calls don't. */
+                               amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
+                               amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
 #endif
                        }
                } else {
                        if (amd64_is_imm32 (item->key))
                                amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key);
                        else {
-                               amd64_mov_reg_imm (code, AMD64_R11, item->key);
-                               amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, AMD64_R11);
+                               amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
+                               amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
                        }
                        item->jmp_code = code;
                        if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
@@ -7375,6 +7973,10 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
                mono_stats.imt_thunks_size += code - start;
        g_assert (code - start <= size);
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_domain_code_validate(domain, &start, size, &code);
+#endif
+
        return start;
 }
 
index 33131f59250692a563cd12d4f50f8ebad2e17336..9c68222dc8e008fe0413f81f6d4eceb76a55e207 100644 (file)
@@ -5,6 +5,18 @@
 #include <mono/utils/mono-sigcontext.h>
 #include <glib.h>
 
+#ifdef __native_client_codegen__
+#define kNaClAlignmentAMD64 32
+#define kNaClAlignmentMaskAMD64 (kNaClAlignmentAMD64 - 1)
+
+/* TODO: use kamd64NaClLengthOfCallImm    */
+/* temporarily using kNaClAlignmentAMD64 so padding in */
+/* image-writer.c doesn't happen                       */
+#define kNaClLengthOfCallImm kNaClAlignmentAMD64
+
+int is_nacl_call_reg_sequence(guint8* code);
+#endif
+
 #ifdef HOST_WIN32
 #include <windows.h>
 /* use SIG* defines if possible */
@@ -146,7 +158,13 @@ struct MonoLMF {
        gpointer    lmf_addr;
        /* This is only set in trampoline LMF frames */
        MonoMethod *method;
+#if defined(__default_codegen__) || defined(HOST_WIN32)
        guint64     rip;
+#elif defined(__native_client_codegen__)
+       /* On 64-bit compilers, default alignment is 8 for this field, */
+       /* this allows the structure to match for 32-bit compilers.    */
+       guint64     rip __attribute__ ((aligned(8)));
+#endif
        guint64     rbx;
        guint64     rbp;
        guint64     rsp;
@@ -237,7 +255,7 @@ typedef struct {
  */
 #define MONO_ARCH_VARARG_ICALLS 1
 
-#ifndef HOST_WIN32
+#if !defined( HOST_WIN32 ) && !defined(__native_client__) && !defined(__native_client_codegen__)
 
 #define MONO_ARCH_USE_SIGACTION 1
 
@@ -247,7 +265,7 @@ typedef struct {
 
 #endif
 
-#endif /* HOST_WIN32 */
+#endif /* !HOST_WIN32 && !__native_client__ */
 
 #if defined (__APPLE__)
 
@@ -333,7 +351,15 @@ typedef struct {
 #define MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES_2 1
 #define MONO_ARCH_HAVE_IMT 1
 #define MONO_ARCH_HAVE_TLS_GET 1
+#if defined(__default_codegen__)
+#define MONO_ARCH_IMT_REG AMD64_R11
+#define MONO_ARCH_IMT_SCRATCH_REG AMD64_R10
+#elif defined(__native_client_codegen__)
+/* Use r10 as the IMT_REG because R11 is clobbered in the call.    */
+/* Original mono code used R10 as scratch, we can use R11 instead. */
 #define MONO_ARCH_IMT_REG AMD64_R10
+#define MONO_ARCH_IMT_SCRATCH_REG AMD64_R11
+#endif
 #define MONO_ARCH_VTABLE_REG MONO_AMD64_ARG_REG1
 /*
  * We use r10 for the imt/rgctx register rather than r11 because r11 is
@@ -356,7 +382,7 @@ typedef struct {
 #define MONO_ARCH_HAVE_GET_TRAMPOLINES 1
 
 #define MONO_ARCH_AOT_SUPPORTED 1
-#ifndef HOST_WIN32
+#if !defined( HOST_WIN32 ) && !defined( __native_client__ )
 #define MONO_ARCH_SOFT_DEBUG_SUPPORTED 1
 #else
 #define DISABLE_DEBUGGER_AGENT 1
index 0a541355c38fbc4cd7753ae948293decef2d8f1b..be987c0abab8be789bd0b21ce186af098148b858 100644 (file)
@@ -871,6 +871,13 @@ MINI_OP(OP_LIVERANGE_START, "liverange_start", NONE, NONE, NONE)
 MINI_OP(OP_LIVERANGE_END, "liverange_end", NONE, NONE, NONE)
 
 /* Arch specific opcodes */
+/* #if defined(__native_client_codegen__) || defined(__native_client__) */
+/* We have to define these in terms of the TARGET defines, not NaCl defines */
+/* because genmdesc.pl doesn't have multiple defines per platform.          */
+#if defined(TARGET_AMD64) || defined(TARGET_X86)
+MINI_OP(OP_NACL_GC_SAFE_POINT,     "nacl_gc_safe_point", IREG, NONE, NONE)
+#endif
+
 #if defined(TARGET_X86) || defined(TARGET_AMD64)
 MINI_OP(OP_X86_TEST_NULL,          "x86_test_null", NONE, IREG, NONE)
 MINI_OP(OP_X86_COMPARE_MEMBASE_REG,"x86_compare_membase_reg", NONE, IREG, IREG)
index bb54d80e92201d2a6092211cb4e99872ca9ce2a4..d1b74bea5c1b6984469ec81809936c74de4e7de8 100644 (file)
@@ -68,36 +68,13 @@ static CRITICAL_SECTION mini_arch_mutex;
 MonoBreakpointInfo
 mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
 
-static gpointer
-mono_realloc_native_code (MonoCompile *cfg)
-{
-#ifdef __native_client_codegen__
-       guint old_padding;
-       gpointer native_code;
-       guint alignment_check;
-
-       /* Save the old alignment offset so we can re-align after the realloc. */
-       old_padding = (guint)(cfg->native_code - cfg->native_code_alloc);
-
-       cfg->native_code_alloc = g_realloc (cfg->native_code_alloc, 
-                                                                               cfg->code_size + kNaClAlignment);
-
-       /* Align native_code to next nearest kNaClAlignment byte. */
-       native_code = (guint)cfg->native_code_alloc + kNaClAlignment;
-       native_code = (guint)native_code & ~kNaClAlignmentMask;
-
-       /* Shift the data to be 32-byte aligned again. */
-       memmove (native_code, cfg->native_code_alloc + old_padding, cfg->code_size);
-
-       alignment_check = (guint)native_code & kNaClAlignmentMask;
-       g_assert (alignment_check == 0);
-       return native_code;
-#else
-       return g_realloc (cfg->native_code, cfg->code_size);
-#endif
-}
 
 #ifdef __native_client_codegen__
+const guint kNaClAlignment = kNaClAlignmentX86;
+const guint kNaClAlignmentMask = kNaClAlignmentMaskX86;
+
+/* Default alignment for Native Client is 32-byte. */
+gint8 nacl_align_byte = -32; /* signed version of 0xe0 */
 
 /* mono_arch_nacl_pad: Add pad bytes of alignment instructions at code,       */
 /* Check that alignment doesn't cross an alignment boundary.        */
@@ -2331,6 +2308,16 @@ x86_pop_reg (code, X86_EAX);
 
 #ifndef DISABLE_JIT
 
+#if defined(__native_client__) || defined(__native_client_codegen__)
+void
+mono_nacl_gc()
+{
+#ifdef __native_client_gc__
+       __nacl_suspend_thread_if_needed();
+#endif
+}
+#endif
+
 void
 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 {
@@ -4682,6 +4669,12 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
                        break;
                }
+               case OP_NACL_GC_SAFE_POINT: {
+#if defined(__native_client_codegen__)
+                       code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)mono_nacl_gc);
+#endif
+                       break;
+               }
                default:
                        g_warning ("unknown opcode %s\n", mono_inst_name (ins->opcode));
                        g_assert_not_reached ();
@@ -4752,13 +4745,46 @@ mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, Mono
                case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
                case MONO_PATCH_INFO_MONITOR_ENTER:
                case MONO_PATCH_INFO_MONITOR_EXIT:
+#if defined(__native_client_codegen__) && defined(__native_client__)
+                       if (nacl_is_code_address (code)) {
+                               /* For tail calls, code is patched after being installed */
+                               /* but not through the normal "patch callsite" method.   */
+                               unsigned char buf[kNaClAlignment];
+                               unsigned char *aligned_code = (uintptr_t)code & ~kNaClAlignmentMask;
+                               unsigned char *_target = target;
+                               int ret;
+                               /* All patch targets modified in x86_patch */
+                               /* are IP relative.                        */
+                               _target = _target + (uintptr_t)buf - (uintptr_t)aligned_code;
+                               memcpy (buf, aligned_code, kNaClAlignment);
+                               /* Patch a temp buffer of bundle size, */
+                               /* then install to actual location.    */
+                               x86_patch (buf + ((uintptr_t)code - (uintptr_t)aligned_code), _target);
+                               ret = nacl_dyncode_modify (aligned_code, buf, kNaClAlignment);
+                               g_assert (ret == 0);
+                       }
+                       else {
+                               x86_patch (ip, target);
+                       }
+#else
                        x86_patch (ip, target);
+#endif
                        break;
                case MONO_PATCH_INFO_NONE:
                        break;
+               case MONO_PATCH_INFO_R4:
+               case MONO_PATCH_INFO_R8: {
+                       guint32 offset = mono_arch_get_patch_offset (ip);
+                       *((gconstpointer *)(ip + offset)) = target;
+                       break;
+               }
                default: {
                        guint32 offset = mono_arch_get_patch_offset (ip);
+#if !defined(__native_client__)
                        *((gconstpointer *)(ip + offset)) = target;
+#else
+                       *((gconstpointer *)(ip + offset)) = nacl_modify_patch_target (target);
+#endif
                        break;
                }
                }
@@ -4784,7 +4810,9 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
                cfg->code_size += 512;
 
-#ifdef __native_client_codegen__
+#if defined(__default_codegen__)
+       code = cfg->native_code = g_malloc (cfg->code_size);
+#elif defined(__native_client_codegen__)
        /* native_code_alloc is not 32-byte aligned, native_code is. */
        cfg->native_code_alloc = g_malloc (cfg->code_size + kNaClAlignment);
 
@@ -4796,8 +4824,6 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 
        alignment_check = (guint)cfg->native_code & kNaClAlignmentMask;
        g_assert(alignment_check == 0);
-#else
-       code = cfg->native_code = g_malloc (cfg->code_size);
 #endif
 
        /* Offset between RSP and the CFA */
@@ -5316,11 +5342,11 @@ mono_arch_emit_exceptions (MonoCompile *cfg)
                                guint32 size;
 
                                /* Compute size of code following the push <OFFSET> */
-#ifdef __native_client_codegen__
+#if defined(__default_codegen__)
+                               size = 5 + 5;
+#elif defined(__native_client_codegen__)
                                code = mono_nacl_align (code);
                                size = kNaClAlignment;
-#else
-                               size = 5 + 5;
 #endif
                                /*This is aligned to 16 bytes by the callee. This way we save a few bytes here.*/
 
@@ -5436,16 +5462,15 @@ mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
 //[1 + 5] x86_jump_mem(inst,mem)
 
 #define CMP_SIZE 6
-#ifdef __native_client_codegen__
-/* These constants should be coming from cpu-x86.md            */
+#if defined(__default_codegen__)
+#define BR_SMALL_SIZE 2
+#define BR_LARGE_SIZE 5
+#elif defined(__native_client_codegen__)
 /* I suspect the size calculation below is actually incorrect. */
-/* TODO: fix the calculation that uses these sizes.            */
+/* TODO: fix the calculation that uses these sizes.  */
 #define BR_SMALL_SIZE 16
 #define BR_LARGE_SIZE 12
-#else
-#define BR_SMALL_SIZE 2
-#define BR_LARGE_SIZE 5
-#endif  /* __native_client_codegen__ */
+#endif  /*__native_client_codegen__*/
 #define JUMP_IMM_SIZE 6
 #define ENABLE_WRONG_METHOD_CHECK 0
 #define DEBUG_IMT 0
@@ -5470,9 +5495,6 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
        int size = 0;
        guint8 *code, *start;
 
-#ifdef __native_client_codegen__
-       /* g_print("mono_arch_build_imt_thunk needs to be aligned.\n"); */
-#endif
        for (i = 0; i < count; ++i) {
                MonoIMTCheckItem *item = imt_entries [i];
                if (item->is_equals) {
@@ -5496,10 +5518,16 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
                }
                size += item->chunk_size;
        }
+#if defined(__native_client__) && defined(__native_client_codegen__)
+       /* In Native Client, we don't re-use thunks, allocate from the */
+       /* normal code manager paths. */
+       code = mono_domain_code_reserve (domain, size);
+#else
        if (fail_tramp)
                code = mono_method_alloc_generic_virtual_thunk (domain, size);
        else
                code = mono_domain_code_reserve (domain, size);
+#endif
        start = code;
        for (i = 0; i < count; ++i) {
                MonoIMTCheckItem *item = imt_entries [i];
@@ -5584,6 +5612,10 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
                g_free (buff);
        }
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_domain_code_validate (domain, &start, size, &code);
+#endif
+
        return start;
 }
 
@@ -5814,6 +5846,7 @@ static gpointer
 get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *code_len)
 {
        guint8 *code, *start;
+       int code_reserve = 64;
 
        /*
         * The stack contains:
@@ -5822,7 +5855,7 @@ get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *cod
         */
 
        if (has_target) {
-               start = code = mono_global_codeman_reserve (64);
+               start = code = mono_global_codeman_reserve (code_reserve);
 
                /* Replace the this argument with the target */
                x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
@@ -5830,15 +5863,15 @@ get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *cod
                x86_mov_membase_reg (code, X86_ESP, 4, X86_ECX, 4);
                x86_jump_membase (code, X86_EAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
 
-               g_assert ((code - start) < 64);
+               g_assert ((code - start) < code_reserve);
        } else {
                int i = 0;
                /* 8 for mov_reg and jump, plus 8 for each parameter */
 #ifdef __native_client_codegen__
                /* TODO: calculate this size correctly */
-               int code_reserve = 13 + (param_count * 8) + 2 * kNaClAlignment;
+               code_reserve = 13 + (param_count * 8) + 2 * kNaClAlignment;
 #else
-               int code_reserve = 8 + (param_count * 8);
+               code_reserve = 8 + (param_count * 8);
 #endif  /* __native_client_codegen__ */
                /*
                 * The stack contains:
@@ -5872,6 +5905,9 @@ get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *cod
                g_assert ((code - start) < code_reserve);
        }
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate(&start, code_reserve, &code);
+#endif
        mono_debug_add_delegate_trampoline (start, code - start);
 
        if (code_len)
index d6fb609d2a407a73a06ac0b462b238deba7d8e57..c99c5d26eb6b3658c8b35736d5012363c11d4427 100644 (file)
@@ -3,6 +3,14 @@
 
 #include <mono/arch/x86/x86-codegen.h>
 #include <mono/utils/mono-sigcontext.h>
+
+#ifdef __native_client_codegen__
+#define kNaClAlignmentX86 32
+#define kNaClAlignmentMaskX86 (kNaClAlignmentX86 - 1)
+
+#define kNaClLengthOfCallImm kx86NaClLengthOfCallImm
+#endif
+
 #ifdef HOST_WIN32
 #include <windows.h>
 /* use SIG* defines if possible */
@@ -59,12 +67,6 @@ struct sigcontext {
 #undef MONO_ARCH_USE_SIGACTION
 #endif
 
-#if defined(__native_client_codegen__) || defined(__native_client__)
-#define NACL_SIZE(a, b) (b)
-#else
-#define NACL_SIZE(a, b) (a)
-#endif
-
 #ifndef HOST_WIN32
 
 #ifdef HAVE_WORKING_SIGALTSTACK
index 19d182a09ec86931f7a90847b744dec20b3b633d..e65206ff6488bbc55e212369848533f58e0546a5 100644 (file)
@@ -160,6 +160,38 @@ gboolean disable_vtypes_in_regs = FALSE;
 
 gboolean mono_dont_free_global_codeman;
 
+gpointer
+mono_realloc_native_code (MonoCompile *cfg)
+{
+#if defined(__default_codegen__)
+       return g_realloc (cfg->native_code, cfg->code_size);
+#elif defined(__native_client_codegen__)
+       guint old_padding;
+       gpointer native_code;
+       guint alignment_check;
+
+       /* Save the old alignment offset so we can re-align after the realloc. */
+       old_padding = (guint)(cfg->native_code - cfg->native_code_alloc);
+
+       cfg->native_code_alloc = g_realloc ( cfg->native_code_alloc,
+                                                                                cfg->code_size + kNaClAlignment );
+
+       /* Align native_code to next nearest kNaClAlignment byte. */
+       native_code = (guint)cfg->native_code_alloc + kNaClAlignment;
+       native_code = (guint)native_code & ~kNaClAlignmentMask;
+
+       /* Shift the data to be 32-byte aligned again. */
+       memmove (native_code, cfg->native_code_alloc + old_padding, cfg->code_size);
+
+       alignment_check = (guint)native_code & kNaClAlignmentMask;
+       g_assert (alignment_check == 0);
+       return native_code;
+#else
+       g_assert_not_reached ();
+       return cfg->native_code;
+#endif
+}
+
 #ifdef __native_client_codegen__
 
 /* Prevent instructions from straddling a 32-byte alignment boundary.   */
@@ -426,6 +458,49 @@ void *mono_global_codeman_reserve (int size)
        }
 }
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+/* Given the temporary buffer (allocated by mono_global_codeman_reserve) into
+ * which we are generating code, return a pointer to the destination in the
+ * dynamic code segment into which the code will be copied when
+ * mono_global_codeman_commit is called.
+ * LOCKING: Acquires the jit lock.
+ */
+void*
+nacl_global_codeman_get_dest (void *data)
+{
+       void *dest;
+       mono_jit_lock ();
+       dest = nacl_code_manager_get_code_dest (global_codeman, data);
+       mono_jit_unlock ();
+       return dest;
+}
+
+void
+mono_global_codeman_commit (void *data, int size, int newsize)
+{
+       mono_jit_lock ();
+       mono_code_manager_commit (global_codeman, data, size, newsize);
+       mono_jit_unlock ();
+}
+
+/* 
+ * Convenience function which calls mono_global_codeman_commit to validate and
+ * copy the code. The caller sets *buf_base and *buf_size to the start and size
+ * of the buffer (allocated by mono_global_codeman_reserve), and *code_end to
+ * the byte after the last instruction byte. On return, *buf_base will point to
+ * the start of the copied in the code segment, and *code_end will point after
+ * the end of the copied code.
+ */
+void
+nacl_global_codeman_validate (guint8 **buf_base, int buf_size, guint8 **code_end)
+{
+       guint8 *tmp = nacl_global_codeman_get_dest (*buf_base);
+       mono_global_codeman_commit (*buf_base, buf_size, *code_end - *buf_base);
+       *code_end = tmp + (*code_end - *buf_base);
+       *buf_base = tmp;
+}
+#endif /* __native_client__ */
+
 /**
  * mono_create_unwind_op:
  *
@@ -1636,7 +1711,7 @@ mono_allocate_stack_slots_full2 (MonoCompile *cfg, gboolean backward, guint32 *s
                case MONO_TYPE_PTR:
                case MONO_TYPE_I:
                case MONO_TYPE_U:
-#if SIZEOF_REGISTER == 4
+#if SIZEOF_VOID_P == 4
                case MONO_TYPE_I4:
 #else
                case MONO_TYPE_I8:
@@ -1870,7 +1945,7 @@ mono_allocate_stack_slots_full (MonoCompile *cfg, gboolean backward, guint32 *st
 
        vars = mono_varlist_sort (cfg, vars, 0);
        offset = 0;
-       *stack_align = sizeof (gpointer);
+       *stack_align = SIZEOF_REGISTER;
        for (l = vars; l; l = l->next) {
                vmv = l->data;
                inst = cfg->varinfo [vmv->idx];
@@ -1925,7 +2000,7 @@ mono_allocate_stack_slots_full (MonoCompile *cfg, gboolean backward, guint32 *st
                        case MONO_TYPE_PTR:
                        case MONO_TYPE_I:
                        case MONO_TYPE_U:
-#if SIZEOF_REGISTER == 4
+#if SIZEOF_VOID_P == 4
                        case MONO_TYPE_I4:
 #else
                        case MONO_TYPE_I8:
@@ -2229,6 +2304,8 @@ mono_bblock_insert_before_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst *ins_
 {
        if (ins == NULL) {
                ins = bb->code;
+               if (ins)
+                       ins->prev = ins_to_insert;
                bb->code = ins_to_insert;
                ins_to_insert->next = ins;
                if (bb->last_ins == NULL)
@@ -2808,7 +2885,13 @@ mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code,
                target = patch_info->data.inst->inst_c0 + code;
                break;
        case MONO_PATCH_INFO_IP:
+#if defined(__native_client__) && defined(__native_client_codegen__)
+               /* Need to transform to the destination address, it's */
+               /* emitted as an immediate in the code. */
+               target = nacl_inverse_modify_patch_target(ip);
+#else
                target = ip;
+#endif
                break;
        case MONO_PATCH_INFO_METHOD_REL:
                target = code + patch_info->data.offset;
@@ -2824,6 +2907,13 @@ mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code,
        }
        case MONO_PATCH_INFO_METHOD_JUMP:
                target = mono_create_jump_trampoline (domain, patch_info->data.method, FALSE);
+#if defined(__native_client__) && defined(__native_client_codegen__)
+#if defined(TARGET_AMD64)
+               /* This target is an absolute address, not relative to the */
+               /* current code being emitted on AMD64. */
+               target = nacl_inverse_modify_patch_target(target);
+#endif
+#endif
                break;
        case MONO_PATCH_INFO_METHOD:
                if (patch_info->data.method == method) {
@@ -2837,6 +2927,11 @@ mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code,
                gpointer *jump_table;
                int i;
 
+#if defined(__native_client__) && defined(__native_client_codegen__)
+               /* This memory will leak, but we don't care if we're */
+               /* not deleting JIT'd methods anyway                 */
+               jump_table = g_malloc0 (sizeof(gpointer) * patch_info->data.table->table_size);
+#else
                if (method && method->dynamic) {
                        jump_table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain, method)->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
                } else {
@@ -2846,10 +2941,27 @@ mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code,
                                jump_table = mono_domain_code_reserve (domain, sizeof (gpointer) * patch_info->data.table->table_size);
                        }
                }
+#endif
 
-               for (i = 0; i < patch_info->data.table->table_size; i++)
+               for (i = 0; i < patch_info->data.table->table_size; i++) {
+#if defined(__native_client__) && defined(__native_client_codegen__)
+                       /* 'code' is relative to the current code blob, we */
+                       /* need to do this transform on it to make the     */
+                       /* pointers in this table absolute                 */
+                       jump_table [i] = nacl_inverse_modify_patch_target (code) + GPOINTER_TO_INT (patch_info->data.table->table [i]);
+#else
                        jump_table [i] = code + GPOINTER_TO_INT (patch_info->data.table->table [i]);
+#endif
+               }
+
+#if defined(__native_client__) && defined(__native_client_codegen__)
+               /* jump_table is in the data section, we need to transform */
+               /* it here so when it gets modified in amd64_patch it will */
+               /* then point back to the absolute data address            */
+               target = nacl_inverse_modify_patch_target (jump_table);
+#else
                target = jump_table;
+#endif
                break;
        }
        case MONO_PATCH_INFO_METHODCONST:
@@ -3195,11 +3307,18 @@ mono_postprocess_patches (MonoCompile *cfg)
                }
                case MONO_PATCH_INFO_SWITCH: {
                        gpointer *table;
+#if defined(__native_client__) && defined(__native_client_codegen__)
+                       /* This memory will leak.  */
+                       /* TODO: can we free this when  */
+                       /* making the final jump table? */
+                       table = g_malloc0 (sizeof(gpointer) * patch_info->data.table->table_size);
+#else
                        if (cfg->method->dynamic) {
                                table = mono_code_manager_reserve (cfg->dynamic_info->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
                        } else {
                                table = mono_domain_code_reserve (cfg->domain, sizeof (gpointer) * patch_info->data.table->table_size);
                        }
+#endif
 
                        for (i = 0; i < patch_info->data.table->table_size; i++) {
                                /* Might be NULL if the switch is eliminated */
@@ -3217,6 +3336,12 @@ mono_postprocess_patches (MonoCompile *cfg)
                        GSList *list;
                        MonoDomain *domain = cfg->domain;
                        unsigned char *ip = cfg->native_code + patch_info->ip.i;
+#if defined(__native_client__) && defined(__native_client_codegen__)
+                       /* When this jump target gets evaluated, the method */
+                       /* will be installed in the dynamic code section,   */
+                       /* not at the location of cfg->native_code.         */
+                       ip = nacl_inverse_modify_patch_target (cfg->native_code) + patch_info->ip.i;
+#endif
 
                        mono_domain_lock (domain);
                        if (!domain_jit_info (domain)->jump_target_hash)
@@ -3356,6 +3481,15 @@ mono_codegen (MonoCompile *cfg)
        int max_epilog_size;
        guint8 *code;
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       void *code_dest;
+
+       /* This keeps patch targets from being transformed during
+        * ordinary method compilation, for local branches and jumps.
+        */
+       nacl_allow_target_modification (FALSE);
+#endif
+
        for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
                cfg->spill_count = 0;
                /* we reuse dfn here */
@@ -3407,6 +3541,9 @@ mono_codegen (MonoCompile *cfg)
                }
        }
 
+#ifdef __native_client_codegen__
+       mono_nacl_fix_patches (cfg->native_code, cfg->patch_info);
+#endif
        mono_arch_emit_exceptions (cfg);
 
        max_epilog_size = 0;
@@ -3437,9 +3574,14 @@ mono_codegen (MonoCompile *cfg)
 #endif
                code = mono_domain_code_reserve (cfg->domain, cfg->code_size + unwindlen);
        }
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_allow_target_modification (TRUE);
+#endif
 
        memcpy (code, cfg->native_code, cfg->code_len);
-#ifdef __native_client_codegen__
+#if defined(__default_codegen__)
+       g_free (cfg->native_code);
+#elif defined(__native_client_codegen__)
        if (cfg->native_code_alloc) {
                g_free (cfg->native_code_alloc);
                cfg->native_code_alloc = 0;
@@ -3447,9 +3589,7 @@ mono_codegen (MonoCompile *cfg)
        else if (cfg->native_code) {
                g_free (cfg->native_code);
        }
-#else
-       g_free (cfg->native_code);
-#endif
+#endif /* __native_client_codegen__ */
        cfg->native_code = code;
        code = cfg->native_code + cfg->code_len;
   
@@ -3487,8 +3627,18 @@ if (valgrind_register){
 #ifdef MONO_ARCH_HAVE_SAVE_UNWIND_INFO
        mono_arch_save_unwind_info (cfg);
 #endif
-       
-#ifdef __native_client_codegen__
+
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       if (!cfg->compile_aot) {
+               if (cfg->method->dynamic) {
+                       code_dest = nacl_code_manager_get_code_dest(cfg->dynamic_info->code_mp, cfg->native_code);
+               } else {
+                       code_dest = nacl_domain_get_code_dest(cfg->domain, cfg->native_code);
+               }
+       }
+#endif
+
+#if defined(__native_client_codegen__)
        mono_nacl_fix_patches (cfg->native_code, cfg->patch_info);
 #endif
 
@@ -3499,6 +3649,9 @@ if (valgrind_register){
        } else {
                mono_domain_code_commit (cfg->domain, cfg->native_code, cfg->code_size, cfg->code_len);
        }
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       cfg->native_code = code_dest;
+#endif
        mono_profiler_code_buffer_new (cfg->native_code, cfg->code_len, MONO_PROFILER_CODE_BUFFER_METHOD, cfg->method);
        
        mono_arch_flush_icache (cfg->native_code, cfg->code_len);
@@ -6095,6 +6248,9 @@ mini_init (const char *filename, const char *runtime_version)
        register_icall (mono_load_remote_field_new, "mono_load_remote_field_new", "object object ptr ptr", FALSE);
        register_icall (mono_store_remote_field_new, "mono_store_remote_field_new", "void object ptr ptr object", FALSE);
 
+#if defined(__native_client__) || defined(__native_client_codegen__)
+       register_icall (mono_nacl_gc, "mono_nacl_gc", "void", TRUE);
+#endif
        /* 
         * NOTE, NOTE, NOTE, NOTE:
         * when adding emulation for some opcodes, remember to also add a dummy
@@ -6165,7 +6321,11 @@ mini_init (const char *filename, const char *runtime_version)
        mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", "double long", mono_lconv_to_r8_un, FALSE);
 #endif
 #ifdef MONO_ARCH_EMULATE_FREM
+#if defined(__default_codegen__)
        mono_register_opcode_emulation (OP_FREM, "__emul_frem", "double double double", fmod, FALSE);
+#elif defined(__native_client_codegen__)
+       mono_register_opcode_emulation (OP_FREM, "__emul_frem", "double double double", mono_fmod, FALSE);
+#endif
 #endif
 
 #ifdef MONO_ARCH_SOFT_FLOAT
index de5105f736eb1e10d41c13abeff5d77c498bd45b..b55595d3e4ff8fc0be6cda6bb874a62dfcfd231b 100644 (file)
@@ -1350,7 +1350,7 @@ enum {
 #endif
 
 /* Opcodes to load/store regsize quantities */
-#ifdef __mono_ilp32__
+#if defined (__mono_ilp32__) || (defined(__native_client_codegen__) && defined(TARGET_AMD64))
 #define OP_LOADR_MEMBASE OP_LOADI8_MEMBASE
 #define OP_STORER_MEMBASE_REG OP_STOREI8_MEMBASE_REG
 #else
@@ -1630,6 +1630,7 @@ void      mono_linterval_split              (MonoCompile *cfg, MonoLiveInterval
 void      mono_liveness_handle_exception_clauses (MonoCompile *cfg) MONO_INTERNAL;
 
 /* Native Client functions */
+gpointer mono_realloc_native_code(MonoCompile *cfg);
 #ifdef __native_client_codegen__
 void mono_nacl_align_inst(guint8 **pcode, int instlen);
 void mono_nacl_align_call(guint8 **start, guint8 **pcode);
@@ -1640,6 +1641,18 @@ void mono_nacl_fix_patches(const guint8 *code, MonoJumpInfo *ji);
 guint8 *mono_arch_nacl_pad(guint8 *code, int pad);
 guint8 *mono_arch_nacl_skip_nops(guint8 *code);
 
+extern const guint kNaClAlignment;
+extern const guint kNaClAlignmentMask;
+#endif
+
+#if defined(__native_client__) || defined(__native_client_codegen__)
+void mono_nacl_gc();
+#endif
+
+#if defined(__native_client_codegen__) || defined(__native_client__)
+#define NACL_SIZE(a, b) (b)
+#else
+#define NACL_SIZE(a, b) (a)
 #endif
 
 /* AOT */
@@ -1749,6 +1762,11 @@ char*             mono_get_rgctx_fetch_trampoline_name (int slot) MONO_INTERNAL;
 
 gboolean          mono_running_on_valgrind (void) MONO_INTERNAL;
 void*             mono_global_codeman_reserve (int size) MONO_INTERNAL;
+#if defined(__native_client_codegen__) && defined(__native_client__)
+void*             nacl_global_codeman_get_dest(void *data) MONO_INTERNAL;
+void              mono_global_codeman_commit(void *data, int size, int newsize) MONO_INTERNAL;
+void              nacl_global_codeman_validate(guint8 **buf_base, int buf_size, guint8 **code_end) MONO_INTERNAL;
+#endif
 const char       *mono_regname_full (int reg, int bank) MONO_INTERNAL;
 gint32*           mono_allocate_stack_slots_full (MonoCompile *cfg, gboolean backward, guint32 *stack_size, guint32 *stack_align) MONO_INTERNAL;
 gint32*           mono_allocate_stack_slots (MonoCompile *cfg, guint32 *stack_size, guint32 *stack_align) MONO_INTERNAL;
index cd42591d8769c0b6bfc61d79b41d996410ffc3ab..381e1b968601b0a7c4791cae67a62369962090a5 100644 (file)
 #include "mini.h"
 #include "mini-amd64.h"
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+#include <malloc.h>
+#include <sys/nacl_syscalls.h>
+#endif
+
 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
 
 static guint8* nullified_class_init_trampoline;
@@ -56,6 +61,10 @@ mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
        amd64_jump_reg (code, AMD64_RAX);
        g_assert ((code - start) < 20);
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_domain_code_validate (domain, &start, 20, &code);
+#endif
+
        mono_arch_flush_icache (start, code - start);
 
        return start;
@@ -90,6 +99,9 @@ mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericCo
        amd64_jump_code (code, addr);
        g_assert ((code - start) < buf_len);
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_domain_code_validate (domain, &start, buf_len, &code);
+#endif
        mono_arch_flush_icache (start, code - start);
 
        return start;
@@ -117,6 +129,10 @@ mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_off
 
        g_assert ((code - start) < buf_len);
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_domain_code_validate (domain, &start, buf_len, &code);
+#endif
+
        mono_arch_flush_icache (start, code - start);
 
        return start;
@@ -131,12 +147,14 @@ mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_off
 void
 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
 {
+#if defined(__default_codegen__)
        guint8 *code;
        guint8 buf [16];
        gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 14, buf, sizeof (buf));
 
        code = buf + 14;
 
+       /* mov 64-bit imm into r11 (followed by call reg?)  or direct call*/
        if (((code [-13] == 0x49) && (code [-12] == 0xbb)) || (code [-5] == 0xe8)) {
                if (code [-5] != 0xe8) {
                        if (can_write) {
@@ -184,6 +202,38 @@ mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
                        VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, sizeof (gpointer));
                }
        }
+#elif defined(__native_client__)
+       /* These are essentially the same 2 cases as above, modified for NaCl*/
+
+       /* Target must be bundle-aligned */
+       g_assert (((guint32)addr & kNaClAlignmentMask) == 0);
+       /* Return target must be bundle-aligned */
+       g_assert (((guint32)orig_code & kNaClAlignmentMask) == 0);
+
+       if (orig_code[-5] == 0xe8) {
+               /* Direct call */
+               int ret;
+               gint32 offset = (gint32)addr - (gint32)orig_code;
+               guint8 buf[sizeof(gint32)];
+               *((gint32*)(buf)) = offset;
+               ret = nacl_dyncode_modify (orig_code - sizeof(gint32), buf, sizeof(gint32));
+               g_assert (ret == 0);
+       }
+
+       else if (is_nacl_call_reg_sequence (orig_code - 10) && orig_code[-16] == 0x41 && orig_code[-15] == 0xbb) {
+               int ret;
+               guint8 buf[sizeof(gint32)];
+               *((gint32 *)(buf)) = addr;
+               /* orig_code[-14] is the start of the immediate. */
+               ret = nacl_dyncode_modify (orig_code - 14, buf, sizeof(gint32));
+               g_assert (ret == 0);
+       }
+       else {
+               g_assert_not_reached ();
+       }
+
+       return;
+#endif
 }
 
 void
@@ -192,6 +242,7 @@ mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *a
        gint32 disp;
        gpointer *plt_jump_table_entry;
 
+#if defined(__default_codegen__)
        /* A PLT entry: jmp *<DISP>(%rip) */
        g_assert (code [0] == 0xff);
        g_assert (code [1] == 0x25);
@@ -199,6 +250,24 @@ mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *a
        disp = *(gint32*)(code + 2);
 
        plt_jump_table_entry = (gpointer*)(code + 6 + disp);
+#elif defined(__native_client_codegen__)
+       /* A PLT entry:            */
+       /* mov <DISP>(%rip), %r11d */
+       /* nacljmp *%r11           */
+
+       /* Verify the 'mov' */
+       g_assert (code [0] == 0x45);
+       g_assert (code [1] == 0x8b);
+       g_assert (code [2] == 0x1d);
+
+       disp = *(gint32*)(code + 3);
+
+       /* 7 = 3 (mov opcode) + 4 (disp) */
+       /* This needs to resolve to the target of the RIP-relative offset */
+       plt_jump_table_entry = (gpointer*)(code + 7 + disp);
+
+#endif /* __native_client_codegen__ */
+
 
        InterlockedExchangePointer (plt_jump_table_entry, addr);
 }
@@ -321,22 +390,25 @@ guchar*
 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
 {
        guint8 *buf, *code, *tramp, *br [2], *r11_save_code, *after_r11_save_code;
-       int i, lmf_offset, offset, res_offset, arg_offset, rax_offset, tramp_offset;
-       int buf_len, saved_regs_offset;
+       int i, lmf_offset, offset, res_offset, arg_offset, rax_offset, tramp_offset, saved_regs_offset;
        int saved_fpregs_offset, rbp_offset, framesize, orig_rsp_to_rbp_offset, cfa_offset;
        gboolean has_caller;
        GSList *unwind_ops = NULL;
        MonoJumpInfo *ji = NULL;
+       const guint kMaxCodeSize = NACL_SIZE (548, 548*2);
+
+#if defined(__native_client_codegen__)
+       const guint kNaClTrampOffset = 17;
+#endif
 
        if (tramp_type == MONO_TRAMPOLINE_JUMP)
                has_caller = FALSE;
        else
                has_caller = TRUE;
 
-       buf_len = 548;
-       code = buf = mono_global_codeman_reserve (buf_len);
+       code = buf = mono_global_codeman_reserve (kMaxCodeSize);
 
-       framesize = 538 + sizeof (MonoLMF);
+       framesize = kMaxCodeSize + sizeof (MonoLMF);
        framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
 
        orig_rsp_to_rbp_offset = 0;
@@ -353,72 +425,76 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
 
        /* Pop the return address off the stack */
        amd64_pop_reg (code, AMD64_R11);
-       orig_rsp_to_rbp_offset += 8;
+       orig_rsp_to_rbp_offset += SIZEOF_REGISTER;
 
-       cfa_offset -= 8;
+       cfa_offset -= SIZEOF_REGISTER;
        mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
 
        /* 
         * Allocate a new stack frame
         */
        amd64_push_reg (code, AMD64_RBP);
-       cfa_offset += 8;
+       cfa_offset += SIZEOF_REGISTER;
        mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
        mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RBP, - cfa_offset);
 
-       orig_rsp_to_rbp_offset -= 8;
-       amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
+       orig_rsp_to_rbp_offset -= SIZEOF_REGISTER;
+       amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, SIZEOF_REGISTER);
        mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
        amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
 
        offset = 0;
        rbp_offset = - offset;
 
-       offset += 8;
+       offset += SIZEOF_REGISTER;
        rax_offset = - offset;
 
-       offset += 8;
+       offset += sizeof(gpointer);
        tramp_offset = - offset;
 
-       offset += 8;
+       offset += sizeof(gpointer);
        arg_offset = - offset;
 
        /* Compute the trampoline address from the return address */
        if (aot) {
+#if defined(__default_codegen__)
                /* 7 = length of call *<offset>(rip) */
                amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 7);
+#elif defined(__native_client_codegen__)
+               amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, kNaClTrampOffset);
+#endif
        } else {
                /* 5 = length of amd64_call_membase () */
                amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 5);
        }
-       amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, 8);
+       amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, sizeof(gpointer));
 
-       offset += 8;
+       offset += SIZEOF_REGISTER;
        res_offset = - offset;
 
        /* Save all registers */
 
-       offset += AMD64_NREG * 8;
+       offset += AMD64_NREG * SIZEOF_REGISTER;
        saved_regs_offset = - offset;
        for (i = 0; i < AMD64_NREG; ++i) {
                if (i == AMD64_RBP) {
                        /* RAX is already saved */
-                       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, rbp_offset, 8);
-                       amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), AMD64_RAX, 8);
+                       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, rbp_offset, SIZEOF_REGISTER);
+                       amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), AMD64_RAX, SIZEOF_REGISTER);
                } else if (i != AMD64_R11) {
-                       amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), i, 8);
+                       amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), i, SIZEOF_REGISTER);
                } else {
                        /* We have to save R11 right at the start of
                           the trampoline code because it's used as a
                           scratch register */
-                       amd64_mov_membase_reg (r11_save_code, AMD64_RSP, saved_regs_offset + orig_rsp_to_rbp_offset + (i * 8), i, 8);
+                       amd64_mov_membase_reg (r11_save_code, AMD64_RSP, saved_regs_offset + orig_rsp_to_rbp_offset + (i * SIZEOF_REGISTER), i, SIZEOF_REGISTER);
                        g_assert (r11_save_code == after_r11_save_code);
                }
        }
-       offset += 8 * 8;
+       offset += 8 * SIZEOF_REGISTER;
        saved_fpregs_offset = - offset;
        for (i = 0; i < 8; ++i)
-               amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * 8), i);
+               amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * SIZEOF_REGISTER), i);
 
        if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT &&
                        tramp_type != MONO_TRAMPOLINE_MONITOR_ENTER &&
@@ -426,14 +502,21 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
                /* Obtain the trampoline argument which is encoded in the instruction stream */
                if (aot) {
                        /* Load the GOT offset */
-                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, 8);
+                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
+#if defined(__default_codegen__)
                        amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 7, 4);
+#elif defined(__native_client_codegen__)
+                       /* The arg is hidden in a "push imm32" instruction, */
+                       /* add one to skip the opcode.                      */
+                       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, kNaClTrampOffset+1, 4);
+#endif
                        /* Compute the address of the GOT slot */
-                       amd64_alu_reg_reg_size (code, X86_ADD, AMD64_R11, AMD64_RAX, 8);
+                       amd64_alu_reg_reg_size (code, X86_ADD, AMD64_R11, AMD64_RAX, sizeof(gpointer));
                        /* Load the value */
-                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
+                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, sizeof(gpointer));
                } else {                        
-                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, 8);
+                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
+#if defined(__default_codegen__)
                        amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 5, 1);
                        amd64_widen_reg (code, AMD64_RAX, AMD64_RAX, TRUE, FALSE);
                        amd64_alu_reg_imm_size (code, X86_CMP, AMD64_RAX, 4, 1);
@@ -447,11 +530,15 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
                        mono_amd64_patch (br [0], code);
                        amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 8);
                        mono_amd64_patch (br [1], code);
+#elif defined(__native_client_codegen__)
+                       /* All args are 32-bit pointers in NaCl */
+                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
+#endif
                }
-               amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, 8);
+               amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, sizeof(gpointer));
        } else {
-               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, saved_regs_offset + (MONO_AMD64_ARG_REG1 * 8), 8);
-               amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, 8);
+               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, saved_regs_offset + (MONO_AMD64_ARG_REG1 * SIZEOF_REGISTER), SIZEOF_REGISTER);
+               amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, sizeof(gpointer));
        }
 
        /* Save LMF begin */
@@ -461,34 +548,34 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
 
        /* Save ip */
        if (has_caller)
-               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, 8);
+               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, sizeof(gpointer));
        else
                amd64_mov_reg_imm (code, AMD64_R11, 0);
-       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, 8);
+       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, SIZEOF_REGISTER);
        /* Save fp */
-       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, framesize, 8);
-       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), AMD64_R11, 8);
+       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, framesize, SIZEOF_REGISTER);
+       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), AMD64_R11, SIZEOF_REGISTER);
        /* Save sp */
-       amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, 8);
+       amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, SIZEOF_REGISTER);
        amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
-       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsp), AMD64_R11, 8);
+       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsp), AMD64_R11, SIZEOF_REGISTER);
        /* Save method */
        if (tramp_type == MONO_TRAMPOLINE_JIT || tramp_type == MONO_TRAMPOLINE_JUMP) {
-               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, arg_offset, 8);
-               amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), AMD64_R11, 8);
+               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, arg_offset, sizeof(gpointer));
+               amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), AMD64_R11, sizeof(gpointer));
        } else {
-               amd64_mov_membase_imm (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, 8);
+               amd64_mov_membase_imm (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, sizeof(gpointer));
        }
        /* Save callee saved regs */
 #ifdef TARGET_WIN32
-       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rdi), AMD64_RDI, 8);
-       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsi), AMD64_RSI, 8);
+       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rdi), AMD64_RDI, SIZEOF_REGISTER);
+       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsi), AMD64_RSI, SIZEOF_REGISTER);
 #endif
-       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, 8);
-       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, 8);
-       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, 8);
-       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, 8);
-       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, 8);
+       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, SIZEOF_REGISTER);
+       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, SIZEOF_REGISTER);
+       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, SIZEOF_REGISTER);
+       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, SIZEOF_REGISTER);
+       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, SIZEOF_REGISTER);
 
        if (aot) {
                code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
@@ -498,15 +585,15 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
        amd64_call_reg (code, AMD64_R11);
 
        /* Save lmf_addr */
-       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, 8);
+       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, sizeof(gpointer));
        /* Save previous_lmf */
        /* Set the lowest bit to 1 to signal that this LMF has the ip field set */
-       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, 8);
-       amd64_alu_reg_imm_size (code, X86_ADD, AMD64_R11, 1, 8);
-       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, 8);
+       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, sizeof(gpointer));
+       amd64_alu_reg_imm_size (code, X86_ADD, AMD64_R11, 1, sizeof(gpointer));
+       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, sizeof(gpointer));
        /* Set new lmf */
        amd64_lea_membase (code, AMD64_R11, AMD64_RBP, lmf_offset);
-       amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, 8);
+       amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, sizeof(gpointer));
 
        /* Save LMF end */
 
@@ -515,15 +602,15 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
 
        /* Arg2 is the address of the calling code */
        if (has_caller)
-               amd64_mov_reg_membase (code, AMD64_ARG_REG2, AMD64_RBP, 8, 8);
+               amd64_mov_reg_membase (code, AMD64_ARG_REG2, AMD64_RBP, 8, sizeof(gpointer));
        else
                amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
 
        /* Arg3 is the method/vtable ptr */
-       amd64_mov_reg_membase (code, AMD64_ARG_REG3, AMD64_RBP, arg_offset, 8);
+       amd64_mov_reg_membase (code, AMD64_ARG_REG3, AMD64_RBP, arg_offset, sizeof(gpointer));
 
        /* Arg4 is the trampoline address */
-       amd64_mov_reg_membase (code, AMD64_ARG_REG4, AMD64_RBP, tramp_offset, 8);
+       amd64_mov_reg_membase (code, AMD64_ARG_REG4, AMD64_RBP, tramp_offset, sizeof(gpointer));
 
        if (aot) {
                char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
@@ -539,7 +626,7 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
        /* 
         * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
         */
-       amd64_mov_membase_reg (code, AMD64_RBP, res_offset, AMD64_RAX, 8);
+       amd64_mov_membase_reg (code, AMD64_RBP, res_offset, AMD64_RAX, SIZEOF_REGISTER);
        if (aot) {
                code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
        } else {
@@ -547,43 +634,47 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
        }
        amd64_call_reg (code, AMD64_R11);
 
-       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, res_offset, 8);      
+       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, res_offset, SIZEOF_REGISTER);        
 
        /* Restore LMF */
 
-       amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
-       amd64_alu_reg_imm_size (code, X86_SUB, AMD64_RCX, 1, 8);
-       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 8);
-       amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, 8);
+       amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof(gpointer));
+       amd64_alu_reg_imm_size (code, X86_SUB, AMD64_RCX, 1, sizeof(gpointer));
+       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sizeof(gpointer));
+       amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, sizeof(gpointer));
 
        /* 
         * Save rax to the stack, after the leave instruction, this will become part of
         * the red zone.
         */
-       amd64_mov_membase_reg (code, AMD64_RBP, rax_offset, AMD64_RAX, 8);
+       amd64_mov_membase_reg (code, AMD64_RBP, rax_offset, AMD64_RAX, SIZEOF_REGISTER);
 
        /* Restore argument registers, r10 (imt method/rgxtx)
           and rax (needed for direct calls to C vararg functions). */
        for (i = 0; i < AMD64_NREG; ++i)
                if (AMD64_IS_ARGUMENT_REG (i) || i == AMD64_R10 || i == AMD64_RAX)
-                       amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * 8), 8);
+                       amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * SIZEOF_REGISTER), SIZEOF_REGISTER);
 
        for (i = 0; i < 8; ++i)
-               amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * 8));
+               amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * SIZEOF_REGISTER));
 
        /* Restore stack */
        amd64_leave (code);
 
        if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
                /* Load result */
-               amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, rax_offset - 0x8, 8);
+               amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, rax_offset - SIZEOF_REGISTER, SIZEOF_REGISTER);
                amd64_ret (code);
        } else {
                /* call the compiled method using the saved rax */
-               amd64_jump_membase (code, AMD64_RSP, rax_offset - 0x8);
+               amd64_jump_membase (code, AMD64_RSP, rax_offset - SIZEOF_REGISTER);
        }
 
-       g_assert ((code - buf) <= buf_len);
+       g_assert ((code - buf) <= kMaxCodeSize);
+
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate (&buf, kMaxCodeSize, &code);
+#endif
 
        mono_arch_flush_icache (buf, code - buf);
 
@@ -606,6 +697,10 @@ mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
        code = buf = mono_global_codeman_reserve (16);
        amd64_ret (code);
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate(&buf, 16, &code);
+#endif
+
        mono_arch_flush_icache (buf, code - buf);
 
        if (info)
@@ -625,15 +720,25 @@ mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_ty
 
        tramp = mono_get_trampoline_code (tramp_type);
 
+#if defined(__default_codegen__)
        if ((((guint64)arg1) >> 32) == 0)
                size = 5 + 1 + 4;
        else
                size = 5 + 1 + 8;
 
        code = buf = mono_domain_code_reserve_align (domain, size, 1);
+#elif defined(__native_client_codegen__)
+       size = 5 + 1 + 4;
+       /* Aligning the call site below could */
+       /* add up to kNaClAlignment-1 bytes   */
+       size += (kNaClAlignment-1);
+       buf = mono_domain_code_reserve_align (domain, size, kNaClAlignment);
+       code = buf;
+#endif
 
        amd64_call_code (code, tramp);
        /* The trampoline code will obtain the argument from the instruction stream */
+#if defined(__default_codegen__)
        if ((((guint64)arg1) >> 32) == 0) {
                *code = 0x4;
                *(guint32*)(code + 1) = (gint64)arg1;
@@ -643,12 +748,22 @@ mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_ty
                *(guint64*)(code + 1) = (gint64)arg1;
                code += 9;
        }
+#elif defined(__native_client_codegen__)
+       /* For NaCl, all tramp args are 32-bit because they're pointers */
+       *code = 0x68; /* push imm32 */
+       *(guint32*)(code + 1) = (gint32)arg1;
+       code += 5;
+#endif
 
        g_assert ((code - buf) <= size);
 
        if (code_len)
                *code_len = size;
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_domain_code_validate(domain, &buf, size, &code);
+#endif
+
        mono_arch_flush_icache (buf, size);
 
        return buf;
@@ -679,7 +794,7 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
                index -= size - 1;
        }
 
-       tramp_size = 64 + 8 * depth;
+       tramp_size = NACL_SIZE (64 + 8 * depth, 128 + 8 * depth);
 
        code = buf = mono_global_codeman_reserve (tramp_size);
 
@@ -692,7 +807,7 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
                amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
        } else {
                /* load rgctx ptr from vtable */
-               amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 8);
+               amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), sizeof(gpointer));
                /* is the rgctx ptr null? */
                amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
                /* if yes, jump to actual trampoline */
@@ -703,9 +818,9 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
        for (i = 0; i < depth; ++i) {
                /* load ptr to next array */
                if (mrgctx && i == 0)
-                       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 8);
+                       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, sizeof(gpointer));
                else
-                       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, 0, 8);
+                       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, 0, sizeof(gpointer));
                /* is the ptr null? */
                amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
                /* if yes, jump to actual trampoline */
@@ -714,7 +829,7 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
        }
 
        /* fetch slot */
-       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, sizeof (gpointer) * (index + 1), 8);
+       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, sizeof (gpointer) * (index + 1), sizeof(gpointer));
        /* is the slot null? */
        amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
        /* if yes, jump to actual trampoline */
@@ -724,12 +839,12 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
        amd64_ret (code);
 
        for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
-               x86_patch (rgctx_null_jumps [i], code);
+               mono_amd64_patch (rgctx_null_jumps [i], code);
 
        g_free (rgctx_null_jumps);
 
        /* move the rgctx pointer to the VTABLE register */
-       amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, 8);
+       amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, sizeof(gpointer));
 
        if (aot) {
                code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
@@ -741,6 +856,9 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
                amd64_jump_code (code, tramp);
        }
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate (&buf, tramp_size, &code);
+#endif
        mono_arch_flush_icache (buf, code - buf);
 
        g_assert (code - buf <= tramp_size);
@@ -788,6 +906,10 @@ mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean a
                amd64_jump_code (code, tramp);
        }
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate (&buf, tramp_size, &code);
+#endif
+
        mono_arch_flush_icache (buf, code - buf);
 
        g_assert (code - buf <= tramp_size);
@@ -911,6 +1033,10 @@ mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean aot)
                amd64_jump_code (code, tramp);
        }
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate (&buf, tramp_size, &code);
+#endif
+
        mono_arch_flush_icache (code, code - buf);
        g_assert (code - buf <= tramp_size);
 
@@ -1027,6 +1153,10 @@ mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
                amd64_jump_code (code, tramp);
        }
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate (&buf, tramp_size, &code);
+#endif
+
        mono_arch_flush_icache (code, code - buf);
        g_assert (code - buf <= tramp_size);
 
@@ -1118,5 +1248,11 @@ mono_arch_get_call_target (guint8 *code)
 guint32
 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
 {
+#if defined(__native_client__) || defined(__native_client_codegen__)
+       /* 18 = 3 (mov opcode) + 4 (disp) + 10 (nacljmp) + 1 (push opcode) */
+       /* See aot-compiler.c arch_emit_plt_entry for details.             */
+       return *(guint32*)(plt_entry + 18);
+#else
        return *(guint32*)(plt_entry + 6);
+#endif
 }
index 071db897631f311211678365d92c5438b0c19529..7ae63ab08f608e663c668e46a765fd2fdefea478 100644 (file)
@@ -49,6 +49,10 @@ mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
        x86_jump_code (code, addr);
        g_assert ((code - start) < 16);
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_domain_code_validate (domain, &start, 16, &code);
+#endif
+
        return start;
 }
 
@@ -68,6 +72,9 @@ mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericCo
        x86_jump_code (code, addr);
        g_assert ((code - start) <= buf_len);
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_domain_code_validate (domain, &start, buf_len, &code);
+#endif
        mono_arch_flush_icache (start, code - start);
 
        return start;
@@ -96,6 +103,10 @@ mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_off
 
        g_assert ((code - start) < buf_len);
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_domain_code_validate (domain, &start, buf_len, &code);
+#endif
+
        mono_arch_flush_icache (start, code - start);
 
        return start;
@@ -104,6 +115,7 @@ mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_off
 void
 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
 {
+#if defined(__default_codegen__)
        guint8 *code;
        guint8 buf [8];
        gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 8, buf, sizeof (buf));
@@ -135,6 +147,23 @@ mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
                                code [4], code [5], code [6]);
                g_assert_not_reached ();
        }
+#elif defined(__native_client__)
+       /* Target must be bundle-aligned */
+       g_assert (((guint32)addr & kNaClAlignmentMask) == 0);
+
+       /* 0xe8 = call <DISP>, 0xe9 = jump <DISP> */
+       if ((orig_code [-5] == 0xe8) || orig_code [-6] == 0xe9) {
+               int ret;
+               gint32 offset = (gint32)addr - (gint32)orig_code;
+               guint8 buf[sizeof(gint32)];
+               *((gint32*)(buf)) = offset;
+               ret = nacl_dyncode_modify (orig_code - sizeof(gint32), buf, sizeof(gint32));
+               g_assert (ret == 0);
+       } else {
+               printf ("Invalid trampoline sequence %p: %02x %02x %02x %02x %02x\n", orig_code, orig_code [-5], orig_code [-4], orig_code [-3], orig_code [-2], orig_code[-1]);
+               g_assert_not_reached ();
+       }
+#endif
 }
 
 void
@@ -154,7 +183,7 @@ mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *a
        g_assert (code [1] == 0x8b);
 
        offset = *(guint32*)(code + 2);
-#else
+#elif defined(__default_codegen__)
        /* A PLT entry: jmp *<DISP>(%ebx) */
        g_assert (code [0] == 0xff);
        g_assert (code [1] == 0xa3);
@@ -222,6 +251,7 @@ mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
 
        code -= 5;
        if (code [0] == 0xe8) {
+#if defined(__default_codegen__)
                if (!mono_running_on_valgrind ()) {
                        guint32 ops;
                        /*
@@ -248,6 +278,9 @@ mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
                        /* Tell valgrind to recompile the patched code */
                        //VALGRIND_DISCARD_TRANSLATIONS (code, 8);
                }
+#elif defined(__native_client_codegen__)
+               mono_arch_patch_callsite (code, code + 5, nullified_class_init_trampoline);
+#endif
        } else if (code [0] == 0x90 || code [0] == 0xeb) {
                /* Already changed by another thread */
                ;
@@ -499,6 +532,9 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
 
        x86_ret (code);
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate (&buf, 256, &code);
+#endif
        g_assert ((code - buf) <= 256);
 
        if (info)
@@ -516,10 +552,15 @@ gpointer
 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
 {
        guint8 *code, *buf;
+       int tramp_size = NACL_SIZE (16, kNaClAlignment);                
 
-       code = buf = mono_global_codeman_reserve (16);
+       code = buf = mono_global_codeman_reserve (tramp_size);
        x86_ret (code);
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate (&buf, tramp_size, &code);
+#endif
+
        mono_arch_flush_icache (buf, code - buf);
 
        if (info)
@@ -546,6 +587,10 @@ mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_ty
        x86_jump_code (buf, tramp);
        g_assert ((buf - code) <= TRAMPOLINE_SIZE);
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_domain_code_validate (domain, &code, kNaClAlignment, &buf);
+#endif
+
        mono_arch_flush_icache (code, buf - code);
 
        if (code_len)
@@ -581,13 +626,12 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
                index -= size - 1;
        }
 
-#ifdef __native_client_codegen__
-       /* TODO: align for Native Client */
-       tramp_size = (aot ? 64 : 36) + 2 * kNaClAlignment +
-               6 * (depth + kNaClAlignment);
-#else
+#if defined(__default_codegen__)
        tramp_size = (aot ? 64 : 36) + 6 * depth;
-#endif  /* __native_client_codegen__ */
+#elif defined(__native_client_codegen__)
+       tramp_size = (aot ? 64 : 36) + 2 * kNaClAlignment +
+         6 * (depth + kNaClAlignment);
+#endif
 
        code = buf = mono_global_codeman_reserve (tramp_size);
 
@@ -645,6 +689,9 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
                x86_jump_code (code, tramp);
        }
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate (&buf, tramp_size, &code);
+#endif
        mono_arch_flush_icache (buf, code - buf);
 
        g_assert (code - buf <= tramp_size);
@@ -703,6 +750,11 @@ mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean a
 #ifdef __native_client_codegen__
        g_assert (code - buf <= kNaClAlignment);
 #endif
+
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate (&buf, tramp_size, &code);
+#endif
+
        if (info)
                *info = mono_tramp_info_create (g_strdup_printf ("generic_class_init_trampoline"), buf, code - buf, ji, unwind_ops);
 
@@ -850,6 +902,10 @@ mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean aot)
        mono_arch_flush_icache (buf, code - buf);
        g_assert (code - buf <= tramp_size);
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate (&buf, tramp_size, &code);
+#endif
+
        if (info)
                *info = mono_tramp_info_create (g_strdup_printf ("monitor_enter_trampoline"), buf, code - buf, ji, unwind_ops);
 
@@ -960,6 +1016,10 @@ mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
                x86_jump_code (code, tramp);
        }
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate (&buf, tramp_size, &code);
+#endif
+
        mono_arch_flush_icache (buf, code - buf);
        g_assert (code - buf <= tramp_size);
 
@@ -1029,6 +1089,10 @@ mono_arch_create_handler_block_trampoline (void)
                x86_jump_code (code, handler_block_trampoline_helper);
        }
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       nacl_global_codeman_validate (&buf, tramp_size, &code);
+#endif
+
        mono_arch_flush_icache (buf, code - buf);
        g_assert (code - buf <= tramp_size);
 
index 32d7319dc14428d7289a7e4871cbff0b93c82d08..af73766a743cb21012b424dfc794c17638b4b82b 100644 (file)
 #ifdef HAVE_VALGRIND_MEMCHECK_H
 #include <valgrind/memcheck.h>
 #endif
+
+#if defined(__native_client_codegen__) && defined(__native_client__)
+#include <malloc.h>
+#include <sys/nacl_syscalls.h>
+#endif
+
 /*
  * AMD64 processors maintain icache coherency only for pages which are 
  * marked executable. Also, windows DEP requires us to obtain executable memory from
@@ -82,10 +87,119 @@ struct _MonoCodeManager {
        int read_only;
        CodeChunk *current;
        CodeChunk *full;
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       MonoGHashTable *hash;
+#endif
 };
 
 #define ALIGN_INT(val,alignment) (((val) + (alignment - 1)) & ~(alignment - 1))
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+/* End of text segment, set by linker. 
+ * Dynamic text starts on the next allocated page.
+ */
+extern char etext[];
+char *next_dynamic_code_addr = NULL;
+
+/*
+ * This routine gets the next available bundle aligned
+ * pointer in the dynamic code section.  It does not check
+ * for the section end, this error will be caught in the
+ * service runtime.
+ */
+void*
+allocate_code(intptr_t increment)
+{
+       char *addr;
+       if (increment < 0) return NULL;
+       increment = increment & kNaClBundleMask ? (increment & ~kNaClBundleMask) + kNaClBundleSize : increment;
+       addr = next_dynamic_code_addr;
+       next_dynamic_code_addr += increment;
+       return addr;
+}
+
+int
+nacl_is_code_address (void *target)
+{
+       return (char *)target < next_dynamic_code_addr;
+}
+
+const int kMaxPatchDepth = 32;
+__thread unsigned char **patch_source_base = NULL;
+__thread unsigned char **patch_dest_base = NULL;
+__thread int *patch_alloc_size = NULL;
+__thread int patch_current_depth = -1;
+__thread int allow_target_modification = 1;
+
+void
+nacl_allow_target_modification (int val)
+{
+       allow_target_modification = val;
+}
+
+static void
+nacl_jit_check_init ()
+{
+       if (patch_source_base == NULL) {
+               patch_source_base = g_malloc (kMaxPatchDepth * sizeof(unsigned char *));
+               patch_dest_base = g_malloc (kMaxPatchDepth * sizeof(unsigned char *));
+               patch_alloc_size = g_malloc (kMaxPatchDepth * sizeof(int));
+       }
+}
+
+
+/* Given a patch target, modify the target such that patching will work when
+ * the code is copied to the data section.
+ */
+void*
+nacl_modify_patch_target (unsigned char *target)
+{
+       /* This seems like a bit of an ugly way to do this but the advantage
+        * is we don't have to worry about all the conditions in
+        * mono_resolve_patch_target, and it can be used by all the bare uses
+        * of <arch>_patch.
+        */
+       unsigned char *sb;
+       unsigned char *db;
+
+       if (!allow_target_modification) return target;
+
+       nacl_jit_check_init ();
+       sb = patch_source_base[patch_current_depth];
+       db = patch_dest_base[patch_current_depth];
+
+       if (target >= sb && (target < sb + patch_alloc_size[patch_current_depth])) {
+               /* Do nothing.  target is in the section being generated.
+                * no need to modify, the disp will be the same either way.
+                */
+       } else {
+               int target_offset = target - db;
+               target = sb + target_offset;
+       }
+       return target;
+}
+
+void*
+nacl_inverse_modify_patch_target (unsigned char *target)
+{
+       unsigned char *sb;
+       unsigned char *db;
+       int target_offset;
+
+       if (!allow_target_modification) return target;
+
+       nacl_jit_check_init ();
+       sb = patch_source_base[patch_current_depth];
+       db = patch_dest_base[patch_current_depth];
+
+       target_offset = target - sb;
+       target = db + target_offset;
+       return target;
+}
+
+
+#endif /* __native_client_codegen && __native_client__ */
+
 /**
  * mono_code_manager_new:
  *
@@ -107,6 +221,24 @@ mono_code_manager_new (void)
        cman->full = NULL;
        cman->dynamic = 0;
        cman->read_only = 0;
+#if defined(__native_client_codegen__) && defined(__native_client__)
+       if (next_dynamic_code_addr == NULL) {
+               const guint kPageMask = 0xFFFF; /* 64K pages */
+               next_dynamic_code_addr = (uintptr_t)(etext + kPageMask) & ~kPageMask;
+               /* Workaround bug in service runtime, unable to allocate */
+               /* from the first page in the dynamic code section.    */
+               /* TODO: remove */
+               next_dynamic_code_addr += (uintptr_t)0x10000;
+       }
+       cman->hash =  mono_g_hash_table_new (NULL, NULL);
+       /* Keep the hash table from being collected */
+       mono_gc_register_root (&cman->hash, sizeof (void*), NULL);
+       if (patch_source_base == NULL) {
+               patch_source_base = g_malloc (kMaxPatchDepth * sizeof(unsigned char *));
+               patch_dest_base = g_malloc (kMaxPatchDepth * sizeof(unsigned char *));
+               patch_alloc_size = g_malloc (kMaxPatchDepth * sizeof(int));
+       }
+#endif
        return cman;
 }
 
@@ -288,7 +420,10 @@ new_codechunk (int dynamic, int size)
                if (!ptr)
                        return NULL;
        } else {
-               ptr = mono_valloc (NULL, chunk_size, MONO_PROT_RWX | ARCH_MAP_FLAGS);
+               /* Allocate MIN_ALIGN-1 more than we need so we can still */
+               /* guarantee MIN_ALIGN alignment for individual allocs    */
+               /* from mono_code_manager_reserve_align.                  */
+               ptr = mono_valloc (NULL, chunk_size + MIN_ALIGN - 1, MONO_PROT_RWX | ARCH_MAP_FLAGS);
                if (!ptr)
                        return NULL;
        }
@@ -333,8 +468,10 @@ new_codechunk (int dynamic, int size)
 void*
 mono_code_manager_reserve_align (MonoCodeManager *cman, int size, int alignment)
 {
+#if !defined(__native_client__) || !defined(__native_client_codegen__)
        CodeChunk *chunk, *prev;
        void *ptr;
+       guint32 align_mask = alignment - 1;
 
        g_assert (!cman->read_only);
 
@@ -357,8 +494,10 @@ mono_code_manager_reserve_align (MonoCodeManager *cman, int size, int alignment)
        for (chunk = cman->current; chunk; chunk = chunk->next) {
                if (ALIGN_INT (chunk->pos, alignment) + size <= chunk->size) {
                        chunk->pos = ALIGN_INT (chunk->pos, alignment);
-                       ptr = chunk->data + chunk->pos;
-                       chunk->pos += size;
+                       /* Align the chunk->data we add to chunk->pos */
+                       /* or we can't guarantee proper alignment     */
+                       ptr = (void*)((((uintptr_t)chunk->data + align_mask) & ~align_mask) + chunk->pos);
+                       chunk->pos = ((char*)ptr - chunk->data) + size;
                        return ptr;
                }
        }
@@ -385,9 +524,33 @@ mono_code_manager_reserve_align (MonoCodeManager *cman, int size, int alignment)
        chunk->next = cman->current;
        cman->current = chunk;
        chunk->pos = ALIGN_INT (chunk->pos, alignment);
-       ptr = chunk->data + chunk->pos;
-       chunk->pos += size;
+       /* Align the chunk->data we add to chunk->pos */
+       /* or we can't guarantee proper alignment     */
+       ptr = (void*)((((uintptr_t)chunk->data + align_mask) & ~align_mask) + chunk->pos);
+       chunk->pos = ((char*)ptr - chunk->data) + size;
        return ptr;
+#else
+       unsigned char *temp_ptr, *code_ptr;
+       /* Round up size to next bundle */
+       alignment = kNaClBundleSize;
+       size = (size + kNaClBundleSize) & (~kNaClBundleMask);
+       /* Allocate a temp buffer */
+       temp_ptr = memalign (alignment, size);
+       g_assert (((uintptr_t)temp_ptr & kNaClBundleMask) == 0);
+       /* Allocate code space from the service runtime */
+       code_ptr = allocate_code (size);
+       /* Insert pointer to code space in hash, keyed by buffer ptr */
+       mono_g_hash_table_insert (cman->hash, temp_ptr, code_ptr);
+
+       nacl_jit_check_init ();
+
+       patch_current_depth++;
+       patch_source_base[patch_current_depth] = temp_ptr;
+       patch_dest_base[patch_current_depth] = code_ptr;
+       patch_alloc_size[patch_current_depth] = size;
+       g_assert (patch_current_depth < kMaxPatchDepth);
+       return temp_ptr;
+#endif
 }
 
 /**
@@ -419,13 +582,45 @@ mono_code_manager_reserve (MonoCodeManager *cman, int size)
 void
 mono_code_manager_commit (MonoCodeManager *cman, void *data, int size, int newsize)
 {
+#if !defined(__native_client__) || !defined(__native_client_codegen__)
        g_assert (newsize <= size);
 
        if (cman->current && (size != newsize) && (data == cman->current->data + cman->current->pos - size)) {
                cman->current->pos -= size - newsize;
        }
+#else
+       unsigned char *code;
+       int status;
+       g_assert (newsize <= size);
+       code = mono_g_hash_table_lookup (cman->hash, data);
+       g_assert (code != NULL);
+       /* Pad space after code with HLTs */
+       /* TODO: this is x86/amd64 specific */
+       while (newsize & kNaClBundleMask) {
+               *((char *)data + newsize) = 0xf4;
+               newsize++;
+       }
+       status = nacl_dyncode_create (code, data, newsize);
+       if (status != 0) {
+               g_assert_not_reached ();
+       }
+       mono_g_hash_table_remove (cman->hash, data);
+       g_assert (data == patch_source_base[patch_current_depth]);
+       g_assert (code == patch_dest_base[patch_current_depth]);
+       patch_current_depth--;
+       g_assert (patch_current_depth >= -1);
+       free (data);
+#endif
 }
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+void *
+nacl_code_manager_get_code_dest (MonoCodeManager *cman, void *data)
+{
+       return mono_g_hash_table_lookup (cman->hash, data);
+}
+#endif
+
 /**
  * mono_code_manager_size:
  * @cman: a code manager
index 39cc1e280345dfc6163e18179f7a26cd4661b853..1507348b0b5c957ce149510a28465cf2b9b0d9a5 100644 (file)
@@ -19,5 +19,21 @@ int              mono_code_manager_size    (MonoCodeManager *cman, int *used_siz
 typedef int    (*MonoCodeManagerFunc)      (void *data, int csize, int size, void *user_data);
 void            mono_code_manager_foreach  (MonoCodeManager *cman, MonoCodeManagerFunc func, void *user_data);
 
+#if defined( __native_client_codegen__ ) && defined( __native_client__ )
+
+#define kNaClBundleSize 32
+#define kNaClBundleMask (kNaClBundleSize-1)
+
+extern __thread unsigned char **patch_source_base;
+extern __thread unsigned char **patch_dest_base;
+extern __thread int patch_current_depth;
+
+int              nacl_is_code_address             (void *target);
+void*            nacl_code_manager_get_code_dest  (MonoCodeManager *cman, void *data);
+void             nacl_allow_target_modification   (int val);
+void*            nacl_modify_patch_target         (unsigned char *target);
+void*            nacl_inverse_modify_patch_target (unsigned char *target);
+#endif /* __native_client__ */
+
 #endif /* __MONO_CODEMAN_H__ */
 
index 32ad8899f5218a4f3c9302f8b432df7d6e7df45b..ca71d98996f1efaed0742bfabaaa922f559aa6c7 100644 (file)
@@ -30,6 +30,9 @@
 
 /* Resolves '..' and '.' references in a path. If the path provided is relative,
  * it will be relative to the current directory */
+
+/* For Native Client, the above is not true.  Since there is no getcwd we fill */
+/* in the file being passed in relative to '.' and don't resolve it            */
 gchar *
 mono_path_canonicalize (const char *path)
 {
@@ -39,9 +42,14 @@ mono_path_canonicalize (const char *path)
        if (g_path_is_absolute (path)) {
                abspath = g_strdup (path);
        } else {
+#ifdef __native_client__
+               gchar *tmpdir = ".";
+               abspath = g_build_filename (tmpdir, path, NULL);
+#else
                gchar *tmpdir = g_get_current_dir ();
                abspath = g_build_filename (tmpdir, path, NULL);
                g_free (tmpdir);
+#endif
        }
 
 #ifdef HOST_WIN32
index eb6cd895921f9eaa9eec3b678180f6483d211563..3c94c5a8df1a74245910cfd45b5e565647e10bc9 100644 (file)
@@ -3,5 +3,6 @@ r='@mono_build_root@'
 MONO_CFG_DIR='@mono_cfg_dir@'
 PATH="$r/runtime/_tmpinst/bin:$PATH"
 MONO_SHARED_DIR=$r/runtime
+export MONO_NACL_ALIGN_MASK_OFF=@MONO_NACL_ALIGN_MASK_OFF@
 export MONO_CFG_DIR MONO_SHARED_DIR PATH
 exec "$r/libtool" --mode=execute "$r/@mono_runtime@" --config "@mono_cfg_dir@/mono/config" "$@"