[Orc] Re-add C bindings for the Orc APIs, with a fix to remove the union that
[oota-llvm.git] / unittests / ExecutionEngine / Orc / OrcTestCommon.h
index 4be2e1967072382806606dd69b762faf12780dba..18e3874acaf8e6a11dd67c3f389cbe86aeb2416c 100644 (file)
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/TypeBuilder.h"
+#include "llvm/ExecutionEngine/ExecutionEngine.h"
 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
+#include "llvm/Support/TargetSelect.h"
 #include <memory>
 
 namespace llvm {
 
+// Base class for Orc tests that will execute code.
+class OrcExecutionTest {
+public:
+
+  OrcExecutionTest() {
+    if (!NativeTargetInitialized) {
+      InitializeNativeTarget();
+      InitializeNativeTargetAsmParser();
+      InitializeNativeTargetAsmPrinter();
+      NativeTargetInitialized = true;
+    }
+  };
+
+  // Get a target machine for the host if it supports JIT execution.
+  std::unique_ptr<TargetMachine> getHostTargetMachineIfSupported() {
+    std::unique_ptr<TargetMachine> TM(EngineBuilder().selectTarget());
+
+    const Triple& TT = TM->getTargetTriple();
+
+    if (TT.getArch() == Triple::x86_64)
+      return std::move(TM);
+
+    return nullptr;
+  }
+
+private:
+  static bool NativeTargetInitialized;
+};
+
 class ModuleBuilder {
 public:
   ModuleBuilder(LLVMContext &Context, StringRef Triple,
                 StringRef Name);
 
   template <typename FuncType>
-  Function* createFunctionDecl(Module *M, StringRef Name) {
+  Function* createFunctionDecl(StringRef Name) {
     return Function::Create(
              TypeBuilder<FuncType, false>::get(M->getContext()),
-             GlobalValue::ExternalLinkage, Name, M);
+             GlobalValue::ExternalLinkage, Name, M.get());
   }
 
   Module* getModule() { return M.get(); }