1 //===- ExecutionEngineTest.cpp - Unit tests for ExecutionEngine -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
22 class ExecutionEngineTest : public testing::Test {
25 : M(new Module("<main>", getGlobalContext())),
26 Engine(EngineBuilder(M).create()) {
29 virtual void SetUp() {
30 ASSERT_TRUE(Engine.get() != NULL);
33 GlobalVariable *NewExtGlobal(const Type *T, const Twine &Name) {
34 return new GlobalVariable(*M, T, false, // Not constant.
35 GlobalValue::ExternalLinkage, NULL, Name);
39 const OwningPtr<ExecutionEngine> Engine;
42 TEST_F(ExecutionEngineTest, ForwardGlobalMapping) {
43 GlobalVariable *G1 = NewExtGlobal(Type::Int32Ty, "Global1");
45 Engine->addGlobalMapping(G1, &Mem1);
46 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G1));
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));
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.";
66 TEST_F(ExecutionEngineTest, ReverseGlobalMapping) {
67 GlobalVariable *G1 = NewExtGlobal(Type::Int32Ty, "Global1");
70 Engine->addGlobalMapping(G1, &Mem1);
71 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
73 Engine->updateGlobalMapping(G1, &Mem2);
74 EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem1));
75 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
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.";