[C++11] Use 'nullptr'.
[oota-llvm.git] / unittests / ExecutionEngine / JIT / MultiJITTest.cpp
index c0c1f262421ae579299cb992d7ff8d38c86e2874..f530e0de5d51f1186cb8b4f2633679596dc50cdb 100644 (file)
@@ -7,23 +7,27 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "gtest/gtest.h"
-#include "llvm/LLVMContext.h"
-#include "llvm/Module.h"
-#include "llvm/Assembly/Parser.h"
-#include "llvm/ExecutionEngine/GenericValue.h"
 #include "llvm/ExecutionEngine/JIT.h"
+#include "llvm/AsmParser/Parser.h"
+#include "llvm/ExecutionEngine/GenericValue.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
 #include "llvm/Support/SourceMgr.h"
+#include "gtest/gtest.h"
 #include <vector>
 
 using namespace llvm;
 
 namespace {
 
+// ARM, PowerPC and SystemZ tests disabled pending fix for PR10783.
+#if !defined(__arm__) && !defined(__powerpc__) && !defined(__s390__) \
+                      && !defined(__aarch64__)
+
 bool LoadAssemblyInto(Module *M, const char *assembly) {
   SMDiagnostic Error;
   bool success =
-    NULL != ParseAssemblyString(assembly, M, Error, M->getContext());
+    nullptr != ParseAssemblyString(assembly, M, Error, M->getContext());
   std::string errMsg;
   raw_string_ostream os(errMsg);
   Error.print("", os);
@@ -65,24 +69,21 @@ void createModule2(LLVMContext &Context2, Module *&M2, Function *&FooF2) {
   FooF2 = M2->getFunction("foo2");
 }
 
-// ARM tests disabled pending fix for PR10783.
-#if !defined(__arm__)
-
 TEST(MultiJitTest, EagerMode) {
   LLVMContext Context1;
-  Module *M1 = 0;
-  Function *FooF1 = 0;
+  Module *M1 = nullptr;
+  Function *FooF1 = nullptr;
   createModule1(Context1, M1, FooF1);
 
   LLVMContext Context2;
-  Module *M2 = 0;
-  Function *FooF2 = 0;
+  Module *M2 = nullptr;
+  Function *FooF2 = nullptr;
   createModule2(Context2, M2, FooF2);
 
   // Now we create the JIT in eager mode
-  OwningPtr<ExecutionEngine> EE1(EngineBuilder(M1).create());
+  std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(M1).create());
   EE1->DisableLazyCompilation(true);
-  OwningPtr<ExecutionEngine> EE2(EngineBuilder(M2).create());
+  std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(M2).create());
   EE2->DisableLazyCompilation(true);
 
   // Call the `foo' function with no arguments:
@@ -100,19 +101,19 @@ TEST(MultiJitTest, EagerMode) {
 
 TEST(MultiJitTest, LazyMode) {
   LLVMContext Context1;
-  Module *M1 = 0;
-  Function *FooF1 = 0;
+  Module *M1 = nullptr;
+  Function *FooF1 = nullptr;
   createModule1(Context1, M1, FooF1);
 
   LLVMContext Context2;
-  Module *M2 = 0;
-  Function *FooF2 = 0;
+  Module *M2 = nullptr;
+  Function *FooF2 = nullptr;
   createModule2(Context2, M2, FooF2);
 
   // Now we create the JIT in lazy mode
-  OwningPtr<ExecutionEngine> EE1(EngineBuilder(M1).create());
+  std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(M1).create());
   EE1->DisableLazyCompilation(false);
-  OwningPtr<ExecutionEngine> EE2(EngineBuilder(M2).create());
+  std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(M2).create());
   EE2->DisableLazyCompilation(false);
 
   // Call the `foo' function with no arguments:
@@ -134,18 +135,18 @@ extern "C" {
 
 TEST(MultiJitTest, JitPool) {
   LLVMContext Context1;
-  Module *M1 = 0;
-  Function *FooF1 = 0;
+  Module *M1 = nullptr;
+  Function *FooF1 = nullptr;
   createModule1(Context1, M1, FooF1);
 
   LLVMContext Context2;
-  Module *M2 = 0;
-  Function *FooF2 = 0;
+  Module *M2 = nullptr;
+  Function *FooF2 = nullptr;
   createModule2(Context2, M2, FooF2);
 
   // Now we create two JITs
-  OwningPtr<ExecutionEngine> EE1(EngineBuilder(M1).create());
-  OwningPtr<ExecutionEngine> EE2(EngineBuilder(M2).create());
+  std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(M1).create());
+  std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(M2).create());
 
   Function *F1 = EE1->FindFunctionNamed("foo1");
   void *foo1 = EE1->getPointerToFunction(F1);
@@ -160,9 +161,30 @@ TEST(MultiJitTest, JitPool) {
   EXPECT_EQ(getPointerToNamedFunction("foo2"), foo2);
 
   // Symbol search
-  EXPECT_EQ((intptr_t)getPointerToNamedFunction("getPointerToNamedFunction"),
-            (intptr_t)&getPointerToNamedFunction);
+  intptr_t
+    sa = (intptr_t)getPointerToNamedFunction("getPointerToNamedFunction");
+  EXPECT_TRUE(sa != 0);
+  intptr_t fa = (intptr_t)&getPointerToNamedFunction;
+  EXPECT_TRUE(fa != 0);
+#ifdef __i386__
+  // getPointerToNamedFunction might be indirect jump on Win32 --enable-shared.
+  // FF 25 <disp32>: jmp *(pointer to IAT)
+  if (sa != fa && memcmp((char *)fa, "\xFF\x25", 2) == 0) {
+    fa = *(intptr_t *)(fa + 2); // Address to IAT
+    EXPECT_TRUE(fa != 0);
+    fa = *(intptr_t *)fa;       // Bound value of IAT
+  }
+#elif defined(__x86_64__)
+  // getPointerToNamedFunction might be indirect jump
+  // on Win32 x64 --enable-shared.
+  // FF 25 <pcrel32>: jmp *(RIP + pointer to IAT)
+  if (sa != fa && memcmp((char *)fa, "\xFF\x25", 2) == 0) {
+    fa += *(int32_t *)(fa + 2) + 6;     // Address to IAT(RIP)
+    fa = *(intptr_t *)fa;               // Bound value of IAT
+  }
+#endif
+  EXPECT_TRUE(sa == fa);
 }
-#endif  // !defined(__arm__)
+#endif  // !defined(__arm__) && !defined(__powerpc__) && !defined(__s390__)
 
 }  // anonymous namespace