Jeffrey Yasskin | 0a96231 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 1 | //===- ExecutionEngineTest.cpp - Unit tests for ExecutionEngine -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/DerivedTypes.h" |
| 11 | #include "llvm/GlobalVariable.h" |
| 12 | #include "llvm/LLVMContext.h" |
| 13 | #include "llvm/Module.h" |
| 14 | #include "llvm/ADT/OwningPtr.h" |
| 15 | #include "llvm/ExecutionEngine/Interpreter.h" |
| 16 | #include "gtest/gtest.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | class ExecutionEngineTest : public testing::Test { |
| 23 | protected: |
| 24 | ExecutionEngineTest() |
Dylan Noblesmith | 35af1d7 | 2011-12-02 20:53:53 +0000 | [diff] [blame] | 25 | : M(new Module("<main>", getGlobalContext())), Error(""), |
| 26 | Engine(EngineBuilder(M).setErrorStr(&Error).create()) { |
Jeffrey Yasskin | 0a96231 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | virtual void SetUp() { |
Dylan Noblesmith | 35af1d7 | 2011-12-02 20:53:53 +0000 | [diff] [blame] | 30 | ASSERT_TRUE(Engine.get() != NULL) << "EngineBuilder returned error: '" |
| 31 | << Error << "'"; |
Jeffrey Yasskin | 0a96231 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 34 | GlobalVariable *NewExtGlobal(Type *T, const Twine &Name) { |
Jeffrey Yasskin | 0a96231 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 35 | return new GlobalVariable(*M, T, false, // Not constant. |
| 36 | GlobalValue::ExternalLinkage, NULL, Name); |
| 37 | } |
| 38 | |
| 39 | Module *const M; |
Dylan Noblesmith | 35af1d7 | 2011-12-02 20:53:53 +0000 | [diff] [blame] | 40 | std::string Error; |
Jeffrey Yasskin | 0a96231 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 41 | const OwningPtr<ExecutionEngine> Engine; |
| 42 | }; |
| 43 | |
| 44 | TEST_F(ExecutionEngineTest, ForwardGlobalMapping) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 45 | GlobalVariable *G1 = |
| 46 | NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1"); |
Jeffrey Yasskin | 0a96231 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 47 | int32_t Mem1 = 3; |
| 48 | Engine->addGlobalMapping(G1, &Mem1); |
| 49 | EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G1)); |
| 50 | int32_t Mem2 = 4; |
| 51 | Engine->updateGlobalMapping(G1, &Mem2); |
| 52 | EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1)); |
| 53 | Engine->updateGlobalMapping(G1, NULL); |
| 54 | EXPECT_EQ(NULL, Engine->getPointerToGlobalIfAvailable(G1)); |
| 55 | Engine->updateGlobalMapping(G1, &Mem2); |
| 56 | EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1)); |
| 57 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 58 | GlobalVariable *G2 = |
| 59 | NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1"); |
Jeffrey Yasskin | 0a96231 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 60 | EXPECT_EQ(NULL, Engine->getPointerToGlobalIfAvailable(G2)) |
| 61 | << "The NULL return shouldn't depend on having called" |
| 62 | << " updateGlobalMapping(..., NULL)"; |
| 63 | // Check that update...() can be called before add...(). |
| 64 | Engine->updateGlobalMapping(G2, &Mem1); |
| 65 | EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G2)); |
| 66 | EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1)) |
| 67 | << "A second mapping shouldn't affect the first."; |
| 68 | } |
| 69 | |
| 70 | TEST_F(ExecutionEngineTest, ReverseGlobalMapping) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 71 | GlobalVariable *G1 = |
| 72 | NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1"); |
Jeffrey Yasskin | 0a96231 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 73 | |
| 74 | int32_t Mem1 = 3; |
| 75 | Engine->addGlobalMapping(G1, &Mem1); |
| 76 | EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1)); |
| 77 | int32_t Mem2 = 4; |
| 78 | Engine->updateGlobalMapping(G1, &Mem2); |
| 79 | EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem1)); |
| 80 | EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2)); |
| 81 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 82 | GlobalVariable *G2 = |
| 83 | NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2"); |
Jeffrey Yasskin | 0a96231 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 84 | Engine->updateGlobalMapping(G2, &Mem1); |
| 85 | EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1)); |
| 86 | EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2)); |
| 87 | Engine->updateGlobalMapping(G1, NULL); |
| 88 | EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1)) |
| 89 | << "Removing one mapping doesn't affect a different one."; |
| 90 | EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem2)); |
| 91 | Engine->updateGlobalMapping(G2, &Mem2); |
| 92 | EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem1)); |
| 93 | EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem2)) |
| 94 | << "Once a mapping is removed, we can point another GV at the" |
| 95 | << " now-free address."; |
| 96 | } |
| 97 | |
Jeffrey Yasskin | c89d27a | 2009-10-09 22:10:27 +0000 | [diff] [blame] | 98 | TEST_F(ExecutionEngineTest, ClearModuleMappings) { |
| 99 | GlobalVariable *G1 = |
| 100 | NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1"); |
| 101 | |
| 102 | int32_t Mem1 = 3; |
| 103 | Engine->addGlobalMapping(G1, &Mem1); |
| 104 | EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1)); |
| 105 | |
| 106 | Engine->clearGlobalMappingsFromModule(M); |
| 107 | |
| 108 | EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem1)); |
| 109 | |
| 110 | GlobalVariable *G2 = |
| 111 | NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2"); |
| 112 | // After clearing the module mappings, we can assign a new GV to the |
| 113 | // same address. |
| 114 | Engine->addGlobalMapping(G2, &Mem1); |
| 115 | EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1)); |
| 116 | } |
| 117 | |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 118 | TEST_F(ExecutionEngineTest, DestructionRemovesGlobalMapping) { |
| 119 | GlobalVariable *G1 = |
| 120 | NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1"); |
| 121 | int32_t Mem1 = 3; |
| 122 | Engine->addGlobalMapping(G1, &Mem1); |
| 123 | // Make sure the reverse mapping is enabled. |
| 124 | EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1)); |
| 125 | // When the GV goes away, the ExecutionEngine should remove any |
| 126 | // mappings that refer to it. |
| 127 | G1->eraseFromParent(); |
| 128 | EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem1)); |
| 129 | } |
| 130 | |
Jeffrey Yasskin | 0a96231 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 131 | } |