blob: e6f07dce1e5873130a3f5df4c67755d6803ce68c [file] [log] [blame]
Jeffrey Yasskin337b1242009-08-04 23:53:16 +00001//===- 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
Chandler Carruth130cec22012-12-04 10:23:08 +000010#include "llvm/ExecutionEngine/Interpreter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000011#include "llvm/IR/DerivedTypes.h"
12#include "llvm/IR/GlobalVariable.h"
13#include "llvm/IR/LLVMContext.h"
14#include "llvm/IR/Module.h"
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000015#include "gtest/gtest.h"
16
17using namespace llvm;
18
19namespace {
20
21class ExecutionEngineTest : public testing::Test {
22protected:
23 ExecutionEngineTest()
Dylan Noblesmithf6f9f1d2011-12-02 20:53:53 +000024 : M(new Module("<main>", getGlobalContext())), Error(""),
25 Engine(EngineBuilder(M).setErrorStr(&Error).create()) {
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000026 }
27
28 virtual void SetUp() {
Dylan Noblesmithf6f9f1d2011-12-02 20:53:53 +000029 ASSERT_TRUE(Engine.get() != NULL) << "EngineBuilder returned error: '"
30 << Error << "'";
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000031 }
32
Chris Lattner229907c2011-07-18 04:54:35 +000033 GlobalVariable *NewExtGlobal(Type *T, const Twine &Name) {
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000034 return new GlobalVariable(*M, T, false, // Not constant.
35 GlobalValue::ExternalLinkage, NULL, Name);
36 }
37
38 Module *const M;
Dylan Noblesmithf6f9f1d2011-12-02 20:53:53 +000039 std::string Error;
Ahmed Charles56440fd2014-03-06 05:51:42 +000040 const std::unique_ptr<ExecutionEngine> Engine;
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000041};
42
43TEST_F(ExecutionEngineTest, ForwardGlobalMapping) {
Owen Anderson55f1c092009-08-13 21:58:54 +000044 GlobalVariable *G1 =
45 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000046 int32_t Mem1 = 3;
47 Engine->addGlobalMapping(G1, &Mem1);
48 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G1));
49 int32_t Mem2 = 4;
50 Engine->updateGlobalMapping(G1, &Mem2);
51 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
52 Engine->updateGlobalMapping(G1, NULL);
53 EXPECT_EQ(NULL, Engine->getPointerToGlobalIfAvailable(G1));
54 Engine->updateGlobalMapping(G1, &Mem2);
55 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
56
Owen Anderson55f1c092009-08-13 21:58:54 +000057 GlobalVariable *G2 =
58 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000059 EXPECT_EQ(NULL, Engine->getPointerToGlobalIfAvailable(G2))
60 << "The NULL return shouldn't depend on having called"
61 << " updateGlobalMapping(..., NULL)";
62 // Check that update...() can be called before add...().
63 Engine->updateGlobalMapping(G2, &Mem1);
64 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G2));
65 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1))
66 << "A second mapping shouldn't affect the first.";
67}
68
69TEST_F(ExecutionEngineTest, ReverseGlobalMapping) {
Owen Anderson55f1c092009-08-13 21:58:54 +000070 GlobalVariable *G1 =
71 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000072
73 int32_t Mem1 = 3;
74 Engine->addGlobalMapping(G1, &Mem1);
75 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
76 int32_t Mem2 = 4;
77 Engine->updateGlobalMapping(G1, &Mem2);
78 EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem1));
79 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
80
Owen Anderson55f1c092009-08-13 21:58:54 +000081 GlobalVariable *G2 =
82 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000083 Engine->updateGlobalMapping(G2, &Mem1);
84 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
85 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
86 Engine->updateGlobalMapping(G1, NULL);
87 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1))
88 << "Removing one mapping doesn't affect a different one.";
89 EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem2));
90 Engine->updateGlobalMapping(G2, &Mem2);
91 EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem1));
92 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem2))
93 << "Once a mapping is removed, we can point another GV at the"
94 << " now-free address.";
95}
96
Jeffrey Yasskin307c0532009-10-09 22:10:27 +000097TEST_F(ExecutionEngineTest, ClearModuleMappings) {
98 GlobalVariable *G1 =
99 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
100
101 int32_t Mem1 = 3;
102 Engine->addGlobalMapping(G1, &Mem1);
103 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
104
105 Engine->clearGlobalMappingsFromModule(M);
106
107 EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem1));
108
109 GlobalVariable *G2 =
110 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
111 // After clearing the module mappings, we can assign a new GV to the
112 // same address.
113 Engine->addGlobalMapping(G2, &Mem1);
114 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
115}
116
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +0000117TEST_F(ExecutionEngineTest, DestructionRemovesGlobalMapping) {
118 GlobalVariable *G1 =
119 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
120 int32_t Mem1 = 3;
121 Engine->addGlobalMapping(G1, &Mem1);
122 // Make sure the reverse mapping is enabled.
123 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
124 // When the GV goes away, the ExecutionEngine should remove any
125 // mappings that refer to it.
126 G1->eraseFromParent();
127 EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem1));
128}
129
Jeffrey Yasskin337b1242009-08-04 23:53:16 +0000130}