[Orc] Rename OrcArchitectureSupport to OrcABISupport and add Win32 ABI support.

This enables lazy JITing on Windows x86-64.

Patch by David. Thanks David!

llvm-svn: 268845
diff --git a/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt b/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
index d26f212..a312f8f 100644
--- a/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
+++ b/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
@@ -2,7 +2,7 @@
   ExecutionUtils.cpp
   IndirectionUtils.cpp
   NullResolver.cpp
-  OrcArchitectureSupport.cpp
+  OrcABISupport.cpp
   OrcCBindings.cpp
   OrcCBindingsStack.cpp
   OrcError.cpp
diff --git a/llvm/lib/ExecutionEngine/Orc/OrcArchitectureSupport.cpp b/llvm/lib/ExecutionEngine/Orc/OrcABISupport.cpp
similarity index 78%
rename from llvm/lib/ExecutionEngine/Orc/OrcArchitectureSupport.cpp
rename to llvm/lib/ExecutionEngine/Orc/OrcABISupport.cpp
index 2dd3762..9869b6c 100644
--- a/llvm/lib/ExecutionEngine/Orc/OrcArchitectureSupport.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/OrcABISupport.cpp
@@ -1,4 +1,4 @@
-//===------ OrcArchSupport.cpp - Architecture specific support code -------===//
+//===------------- OrcABISupport.cpp - ABI specific support code ----------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -7,7 +7,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/ExecutionEngine/Orc/OrcArchitectureSupport.h"
+#include "llvm/ExecutionEngine/Orc/OrcABISupport.h"
 #include "llvm/ADT/Triple.h"
 #include "llvm/Support/Process.h"
 
@@ -190,74 +190,9 @@
   return Error::success();
 }
 
-void OrcX86_64::writeResolverCode(uint8_t *ResolverMem, JITReentryFn ReentryFn,
-                                  void *CallbackMgr) {
-
-  const uint8_t ResolverCode[] = {
-      // resolver_entry:
-      0x55,                                     // 0x00: pushq     %rbp
-      0x48, 0x89, 0xe5,                         // 0x01: movq      %rsp, %rbp
-      0x50,                                     // 0x04: pushq     %rax
-      0x53,                                     // 0x05: pushq     %rbx
-      0x51,                                     // 0x06: pushq     %rcx
-      0x52,                                     // 0x07: pushq     %rdx
-      0x56,                                     // 0x08: pushq     %rsi
-      0x57,                                     // 0x09: pushq     %rdi
-      0x41, 0x50,                               // 0x0a: pushq     %r8
-      0x41, 0x51,                               // 0x0c: pushq     %r9
-      0x41, 0x52,                               // 0x0e: pushq     %r10
-      0x41, 0x53,                               // 0x10: pushq     %r11
-      0x41, 0x54,                               // 0x12: pushq     %r12
-      0x41, 0x55,                               // 0x14: pushq     %r13
-      0x41, 0x56,                               // 0x16: pushq     %r14
-      0x41, 0x57,                               // 0x18: pushq     %r15
-      0x48, 0x81, 0xec, 0x08, 0x02, 0x00, 0x00, // 0x1a: subq      0x208, %rsp
-      0x48, 0x0f, 0xae, 0x04, 0x24,             // 0x21: fxsave64  (%rsp)
-      0x48, 0xbf,                               // 0x26: movabsq   <CBMgr>, %rdi
-
-      // 0x28: Callback manager addr.
-      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-
-      0x48, 0x8b, 0x75, 0x08, // 0x30: movq      8(%rbp), %rsi
-      0x48, 0x83, 0xee, 0x06, // 0x34: subq      $6, %rsi
-      0x48, 0xb8,             // 0x38: movabsq   <REntry>, %rax
-
-      // 0x3a: JIT re-entry fn addr:
-      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-
-      0xff, 0xd0,                               // 0x42: callq     *%rax
-      0x48, 0x89, 0x45, 0x08,                   // 0x44: movq      %rax, 8(%rbp)
-      0x48, 0x0f, 0xae, 0x0c, 0x24,             // 0x48: fxrstor64 (%rsp)
-      0x48, 0x81, 0xc4, 0x08, 0x02, 0x00, 0x00, // 0x4d: addq      0x208, %rsp
-      0x41, 0x5f,                               // 0x54: popq      %r15
-      0x41, 0x5e,                               // 0x56: popq      %r14
-      0x41, 0x5d,                               // 0x58: popq      %r13
-      0x41, 0x5c,                               // 0x5a: popq      %r12
-      0x41, 0x5b,                               // 0x5c: popq      %r11
-      0x41, 0x5a,                               // 0x5e: popq      %r10
-      0x41, 0x59,                               // 0x60: popq      %r9
-      0x41, 0x58,                               // 0x62: popq      %r8
-      0x5f,                                     // 0x64: popq      %rdi
-      0x5e,                                     // 0x65: popq      %rsi
-      0x5a,                                     // 0x66: popq      %rdx
-      0x59,                                     // 0x67: popq      %rcx
-      0x5b,                                     // 0x68: popq      %rbx
-      0x58,                                     // 0x69: popq      %rax
-      0x5d,                                     // 0x6a: popq      %rbp
-      0xc3,                                     // 0x6b: retq
-  };
-
-  const unsigned ReentryFnAddrOffset = 0x3a;
-  const unsigned CallbackMgrAddrOffset = 0x28;
-
-  memcpy(ResolverMem, ResolverCode, sizeof(ResolverCode));
-  memcpy(ResolverMem + ReentryFnAddrOffset, &ReentryFn, sizeof(ReentryFn));
-  memcpy(ResolverMem + CallbackMgrAddrOffset, &CallbackMgr,
-         sizeof(CallbackMgr));
-}
-
-void OrcX86_64::writeTrampolines(uint8_t *TrampolineMem, void *ResolverAddr,
-                                 unsigned NumTrampolines) {
+void OrcX86_64_Base::writeTrampolines(uint8_t *TrampolineMem,
+                                      void *ResolverAddr,
+                                      unsigned NumTrampolines) {
 
   unsigned OffsetToPtr = NumTrampolines * TrampolineSize;
 
@@ -270,9 +205,9 @@
     Trampolines[I] = CallIndirPCRel | ((OffsetToPtr - 6) << 16);
 }
 
-Error OrcX86_64::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo,
-                                        unsigned MinStubs,
-                                        void *InitialPtrVal) {
+Error OrcX86_64_Base::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo,
+                                             unsigned MinStubs,
+                                             void *InitialPtrVal) {
   // Stub format is:
   //
   // .section __orc_stubs
@@ -336,6 +271,149 @@
   return Error::success();
 }
 
+void OrcX86_64_SysV::writeResolverCode(uint8_t *ResolverMem,
+                                       JITReentryFn ReentryFn,
+                                       void *CallbackMgr) {
+
+  const uint8_t ResolverCode[] = {
+      // resolver_entry:
+      0x55,                                     // 0x00: pushq     %rbp
+      0x48, 0x89, 0xe5,                         // 0x01: movq      %rsp, %rbp
+      0x50,                                     // 0x04: pushq     %rax
+      0x53,                                     // 0x05: pushq     %rbx
+      0x51,                                     // 0x06: pushq     %rcx
+      0x52,                                     // 0x07: pushq     %rdx
+      0x56,                                     // 0x08: pushq     %rsi
+      0x57,                                     // 0x09: pushq     %rdi
+      0x41, 0x50,                               // 0x0a: pushq     %r8
+      0x41, 0x51,                               // 0x0c: pushq     %r9
+      0x41, 0x52,                               // 0x0e: pushq     %r10
+      0x41, 0x53,                               // 0x10: pushq     %r11
+      0x41, 0x54,                               // 0x12: pushq     %r12
+      0x41, 0x55,                               // 0x14: pushq     %r13
+      0x41, 0x56,                               // 0x16: pushq     %r14
+      0x41, 0x57,                               // 0x18: pushq     %r15
+      0x48, 0x81, 0xec, 0x08, 0x02, 0x00, 0x00, // 0x1a: subq      0x208, %rsp
+      0x48, 0x0f, 0xae, 0x04, 0x24,             // 0x21: fxsave64  (%rsp)
+      0x48, 0xbf,                               // 0x26: movabsq   <CBMgr>, %rdi
+
+      // 0x28: Callback manager addr.
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+
+      0x48, 0x8b, 0x75, 0x08,                   // 0x30: movq      8(%rbp), %rsi
+      0x48, 0x83, 0xee, 0x06,                   // 0x34: subq      $6, %rsi
+      0x48, 0xb8,                               // 0x38: movabsq   <REntry>, %rax
+
+      // 0x3a: JIT re-entry fn addr:
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+
+      0xff, 0xd0,                               // 0x42: callq     *%rax
+      0x48, 0x89, 0x45, 0x08,                   // 0x44: movq      %rax, 8(%rbp)
+      0x48, 0x0f, 0xae, 0x0c, 0x24,             // 0x48: fxrstor64 (%rsp)
+      0x48, 0x81, 0xc4, 0x08, 0x02, 0x00, 0x00, // 0x4d: addq      0x208, %rsp
+      0x41, 0x5f,                               // 0x54: popq      %r15
+      0x41, 0x5e,                               // 0x56: popq      %r14
+      0x41, 0x5d,                               // 0x58: popq      %r13
+      0x41, 0x5c,                               // 0x5a: popq      %r12
+      0x41, 0x5b,                               // 0x5c: popq      %r11
+      0x41, 0x5a,                               // 0x5e: popq      %r10
+      0x41, 0x59,                               // 0x60: popq      %r9
+      0x41, 0x58,                               // 0x62: popq      %r8
+      0x5f,                                     // 0x64: popq      %rdi
+      0x5e,                                     // 0x65: popq      %rsi
+      0x5a,                                     // 0x66: popq      %rdx
+      0x59,                                     // 0x67: popq      %rcx
+      0x5b,                                     // 0x68: popq      %rbx
+      0x58,                                     // 0x69: popq      %rax
+      0x5d,                                     // 0x6a: popq      %rbp
+      0xc3,                                     // 0x6b: retq
+  };
+
+  const unsigned ReentryFnAddrOffset = 0x3a;
+  const unsigned CallbackMgrAddrOffset = 0x28;
+
+  memcpy(ResolverMem, ResolverCode, sizeof(ResolverCode));
+  memcpy(ResolverMem + ReentryFnAddrOffset, &ReentryFn, sizeof(ReentryFn));
+  memcpy(ResolverMem + CallbackMgrAddrOffset, &CallbackMgr,
+         sizeof(CallbackMgr));
+}
+
+void OrcX86_64_Win32::writeResolverCode(uint8_t *ResolverMem,
+                                        JITReentryFn ReentryFn,
+                                        void *CallbackMgr) {
+
+  // resolverCode is similar to OrcX86_64 with differences specific to windows x64 calling convention:
+  // arguments go into rcx, rdx and come in reverse order, shadow space allocation on stack
+  const uint8_t ResolverCode[] = {
+      // resolver_entry:
+      0x55,                                      // 0x00: pushq     %rbp
+      0x48, 0x89, 0xe5,                          // 0x01: movq      %rsp, %rbp
+      0x50,                                      // 0x04: pushq     %rax
+      0x53,                                      // 0x05: pushq     %rbx
+      0x51,                                      // 0x06: pushq     %rcx
+      0x52,                                      // 0x07: pushq     %rdx
+      0x56,                                      // 0x08: pushq     %rsi
+      0x57,                                      // 0x09: pushq     %rdi
+      0x41, 0x50,                                // 0x0a: pushq     %r8
+      0x41, 0x51,                                // 0x0c: pushq     %r9
+      0x41, 0x52,                                // 0x0e: pushq     %r10
+      0x41, 0x53,                                // 0x10: pushq     %r11
+      0x41, 0x54,                                // 0x12: pushq     %r12
+      0x41, 0x55,                                // 0x14: pushq     %r13
+      0x41, 0x56,                                // 0x16: pushq     %r14
+      0x41, 0x57,                                // 0x18: pushq     %r15
+      0x48, 0x81, 0xec, 0x08, 0x02, 0x00, 0x00,  // 0x1a: subq      0x208, %rsp
+      0x48, 0x0f, 0xae, 0x04, 0x24,              // 0x21: fxsave64  (%rsp)
+
+      0x48, 0xb9,                                // 0x26: movabsq   <CBMgr>, %rcx
+      // 0x28: Callback manager addr.
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+
+      0x48, 0x8B, 0x55, 0x08,                    // 0x30: mov       rdx, [rbp+0x8]
+      0x48, 0x83, 0xea, 0x06,                    // 0x34: sub       rdx, 0x6
+
+      0x48, 0xb8,                                // 0x38: movabsq   <REntry>, %rax
+      // 0x3a: JIT re-entry fn addr:
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+
+      // 0x42: sub       rsp, 0x20 (Allocate shadow space)
+      0x48, 0x83, 0xEC, 0x20,
+      0xff, 0xd0,                                // 0x46: callq     *%rax
+
+      // 0x48: add       rsp, 0x20 (Free shadow space)
+      0x48, 0x83, 0xC4, 0x20,
+
+      0x48, 0x89, 0x45, 0x08,                    // 0x4C: movq      %rax, 8(%rbp)
+      0x48, 0x0f, 0xae, 0x0c, 0x24,              // 0x50: fxrstor64 (%rsp)
+      0x48, 0x81, 0xc4, 0x08, 0x02, 0x00, 0x00,  // 0x55: addq      0x208, %rsp
+      0x41, 0x5f,                                // 0x5C: popq      %r15
+      0x41, 0x5e,                                // 0x5E: popq      %r14
+      0x41, 0x5d,                                // 0x60: popq      %r13
+      0x41, 0x5c,                                // 0x62: popq      %r12
+      0x41, 0x5b,                                // 0x64: popq      %r11
+      0x41, 0x5a,                                // 0x66: popq      %r10
+      0x41, 0x59,                                // 0x68: popq      %r9
+      0x41, 0x58,                                // 0x6a: popq      %r8
+      0x5f,                                      // 0x6c: popq      %rdi
+      0x5e,                                      // 0x6d: popq      %rsi
+      0x5a,                                      // 0x6e: popq      %rdx
+      0x59,                                      // 0x6f: popq      %rcx
+      0x5b,                                      // 0x70: popq      %rbx
+      0x58,                                      // 0x71: popq      %rax
+      0x5d,                                      // 0x72: popq      %rbp
+      0xc3,                                      // 0x73: retq
+  };
+
+
+  const unsigned ReentryFnAddrOffset = 0x3a;
+  const unsigned CallbackMgrAddrOffset = 0x28;
+
+  memcpy(ResolverMem, ResolverCode, sizeof(ResolverCode));
+  memcpy(ResolverMem + ReentryFnAddrOffset, &ReentryFn, sizeof(ReentryFn));
+  memcpy(ResolverMem + CallbackMgrAddrOffset, &CallbackMgr,
+         sizeof(CallbackMgr));
+}
+
 void OrcI386::writeResolverCode(uint8_t *ResolverMem, JITReentryFn ReentryFn,
                                 void *CallbackMgr) {
 
diff --git a/llvm/lib/ExecutionEngine/Orc/OrcCBindingsStack.cpp b/llvm/lib/ExecutionEngine/Orc/OrcCBindingsStack.cpp
index 10f6692..ddc7d71 100644
--- a/llvm/lib/ExecutionEngine/Orc/OrcCBindingsStack.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/OrcCBindingsStack.cpp
@@ -9,7 +9,7 @@
 
 #include "OrcCBindingsStack.h"
 
-#include "llvm/ExecutionEngine/Orc/OrcArchitectureSupport.h"
+#include "llvm/ExecutionEngine/Orc/OrcABISupport.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/DynamicLibrary.h"
 #include <cstdio>
@@ -29,8 +29,13 @@
   };
 
   case Triple::x86_64: {
-    typedef orc::LocalJITCompileCallbackManager<orc::OrcX86_64> CCMgrT;
-    return llvm::make_unique<CCMgrT>(0);
+    if ( T.getOS() == Triple::OSType::Win32 ) {
+      typedef orc::LocalJITCompileCallbackManager<orc::OrcX86_64_Win32> CCMgrT;
+      return llvm::make_unique<CCMgrT>(0);
+    } else {
+      typedef orc::LocalJITCompileCallbackManager<orc::OrcX86_64_SysV> CCMgrT;
+      return llvm::make_unique<CCMgrT>(0);
+    }
   }
   }
 }
@@ -47,9 +52,16 @@
     };
 
   case Triple::x86_64:
-    return []() {
-      return llvm::make_unique<
-          orc::LocalIndirectStubsManager<orc::OrcX86_64>>();
-    };
+    if (T.getOS() == Triple::OSType::Win32) {
+      return [](){
+        return llvm::make_unique<
+          orc::LocalIndirectStubsManager<orc::OrcX86_64_Win32>>();
+      };
+    } else {
+      return [](){
+        return llvm::make_unique<
+          orc::LocalIndirectStubsManager<orc::OrcX86_64_SysV>>();
+      };
+    }
   }
 }