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() |
| 25 | : M(new Module("<main>", getGlobalContext())), |
| 26 | Engine(EngineBuilder(M).create()) { |
| 27 | } |
| 28 | |
| 29 | virtual void SetUp() { |
| 30 | ASSERT_TRUE(Engine.get() != NULL); |
| 31 | } |
| 32 | |
| 33 | GlobalVariable *NewExtGlobal(const Type *T, const Twine &Name) { |
| 34 | return new GlobalVariable(*M, T, false, // Not constant. |
| 35 | GlobalValue::ExternalLinkage, NULL, Name); |
| 36 | } |
| 37 | |
| 38 | Module *const M; |
| 39 | const OwningPtr<ExecutionEngine> Engine; |
| 40 | }; |
| 41 | |
| 42 | TEST_F(ExecutionEngineTest, ForwardGlobalMapping) { |
| 43 | GlobalVariable *G1 = NewExtGlobal(Type::Int32Ty, "Global1"); |
| 44 | int32_t Mem1 = 3; |
| 45 | Engine->addGlobalMapping(G1, &Mem1); |
| 46 | EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G1)); |
| 47 | int32_t Mem2 = 4; |
| 48 | Engine->updateGlobalMapping(G1, &Mem2); |
| 49 | EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1)); |
| 50 | Engine->updateGlobalMapping(G1, NULL); |
| 51 | EXPECT_EQ(NULL, Engine->getPointerToGlobalIfAvailable(G1)); |
| 52 | Engine->updateGlobalMapping(G1, &Mem2); |
| 53 | EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1)); |
| 54 | |
| 55 | GlobalVariable *G2 = NewExtGlobal(Type::Int32Ty, "Global1"); |
| 56 | EXPECT_EQ(NULL, Engine->getPointerToGlobalIfAvailable(G2)) |
| 57 | << "The NULL return shouldn't depend on having called" |
| 58 | << " updateGlobalMapping(..., NULL)"; |
| 59 | // Check that update...() can be called before add...(). |
| 60 | Engine->updateGlobalMapping(G2, &Mem1); |
| 61 | EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G2)); |
| 62 | EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1)) |
| 63 | << "A second mapping shouldn't affect the first."; |
| 64 | } |
| 65 | |
| 66 | TEST_F(ExecutionEngineTest, ReverseGlobalMapping) { |
| 67 | GlobalVariable *G1 = NewExtGlobal(Type::Int32Ty, "Global1"); |
| 68 | |
| 69 | int32_t Mem1 = 3; |
| 70 | Engine->addGlobalMapping(G1, &Mem1); |
| 71 | EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1)); |
| 72 | int32_t Mem2 = 4; |
| 73 | Engine->updateGlobalMapping(G1, &Mem2); |
| 74 | EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem1)); |
| 75 | EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2)); |
| 76 | |
| 77 | GlobalVariable *G2 = NewExtGlobal(Type::Int32Ty, "Global2"); |
| 78 | Engine->updateGlobalMapping(G2, &Mem1); |
| 79 | EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1)); |
| 80 | EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2)); |
| 81 | Engine->updateGlobalMapping(G1, NULL); |
| 82 | EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1)) |
| 83 | << "Removing one mapping doesn't affect a different one."; |
| 84 | EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem2)); |
| 85 | Engine->updateGlobalMapping(G2, &Mem2); |
| 86 | EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem1)); |
| 87 | EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem2)) |
| 88 | << "Once a mapping is removed, we can point another GV at the" |
| 89 | << " now-free address."; |
| 90 | } |
| 91 | |
| 92 | } |