[weak vtables] Remove a bunch of weak vtables
[oota-llvm.git] / unittests / ExecutionEngine / MCJIT / MCJITTest.cpp
1 //===- MCJITTest.cpp - Unit tests for the MCJIT ---------------------------===//
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 // This test suite verifies basic MCJIT functionality such as making function
11 // calls, using global variables, and compiling multpile modules.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/ExecutionEngine/MCJIT.h"
16 #include "MCJITTestBase.h"
17 #include "gtest/gtest.h"
18
19 using namespace llvm;
20
21 class MCJITTest : public testing::Test, public MCJITTestBase {
22 protected:
23
24   virtual void SetUp();
25 };
26
27 void MCJITTest::SetUp() {
28   M.reset(createEmptyModule("<main>"));
29 }
30
31 namespace {
32
33 // FIXME: Ensure creating an execution engine does not crash when constructed
34 //        with a null module.
35 /*
36 TEST_F(MCJITTest, null_module) {
37   createJIT(0);
38 }
39 */
40
41 // FIXME: In order to JIT an empty module, there needs to be
42 // an interface to ExecutionEngine that forces compilation but
43 // does not require retrieval of a pointer to a function/global.
44 /*
45 TEST_F(MCJITTest, empty_module) {
46   createJIT(M.take());
47   //EXPECT_NE(0, TheJIT->getObjectImage())
48   //  << "Unable to generate executable loaded object image";
49 }
50 */
51
52 TEST_F(MCJITTest, global_variable) {
53   SKIP_UNSUPPORTED_PLATFORM;
54
55   int initialValue = 5;
56   GlobalValue *Global = insertGlobalInt32(M.get(), "test_global", initialValue);
57   createJIT(M.take());
58   void *globalPtr =  TheJIT->getPointerToGlobal(Global);
59   EXPECT_TRUE(0 != globalPtr)
60     << "Unable to get pointer to global value from JIT";
61
62   EXPECT_EQ(initialValue, *(int32_t*)globalPtr)
63     << "Unexpected initial value of global";
64 }
65
66 TEST_F(MCJITTest, add_function) {
67   SKIP_UNSUPPORTED_PLATFORM;
68
69   Function *F = insertAddFunction(M.get());
70   createJIT(M.take());
71   uint64_t addPtr = TheJIT->getFunctionAddress(F->getName().str());
72   EXPECT_TRUE(0 != addPtr)
73     << "Unable to get pointer to function from JIT";
74
75   ASSERT_TRUE(addPtr != 0) << "Unable to get pointer to function .";
76   int (*AddPtr)(int, int) = (int(*)(int, int))addPtr ;
77   EXPECT_EQ(0,   AddPtr(0, 0));
78   EXPECT_EQ(1,   AddPtr(1, 0));
79   EXPECT_EQ(3,   AddPtr(1, 2));
80   EXPECT_EQ(-5,  AddPtr(-2, -3));
81   EXPECT_EQ(30,  AddPtr(10, 20));
82   EXPECT_EQ(-30, AddPtr(-10, -20));
83   EXPECT_EQ(-40, AddPtr(-10, -30));
84 }
85
86 TEST_F(MCJITTest, run_main) {
87   SKIP_UNSUPPORTED_PLATFORM;
88
89   int rc = 6;
90   Function *Main = insertMainFunction(M.get(), 6);
91   createJIT(M.take());
92   uint64_t ptr = TheJIT->getFunctionAddress(Main->getName().str());
93   EXPECT_TRUE(0 != ptr)
94     << "Unable to get pointer to main() from JIT";
95
96   int (*FuncPtr)(void) = (int(*)(void))ptr;
97   int returnCode = FuncPtr();
98   EXPECT_EQ(returnCode, rc);
99 }
100
101 TEST_F(MCJITTest, return_global) {
102   SKIP_UNSUPPORTED_PLATFORM;
103
104   int32_t initialNum = 7;
105   GlobalVariable *GV = insertGlobalInt32(M.get(), "myglob", initialNum);
106
107   Function *ReturnGlobal = startFunction<int32_t(void)>(M.get(),
108                                                         "ReturnGlobal");
109   Value *ReadGlobal = Builder.CreateLoad(GV);
110   endFunctionWithRet(ReturnGlobal, ReadGlobal);
111
112   createJIT(M.take());
113   uint64_t rgvPtr = TheJIT->getFunctionAddress(ReturnGlobal->getName().str());
114   EXPECT_TRUE(0 != rgvPtr);
115
116   int32_t(*FuncPtr)(void) = (int32_t(*)(void))rgvPtr;
117   EXPECT_EQ(initialNum, FuncPtr())
118     << "Invalid value for global returned from JITted function";
119 }
120
121 // FIXME: This case fails due to a bug with getPointerToGlobal().
122 // The bug is due to MCJIT not having an implementation of getPointerToGlobal()
123 // which results in falling back on the ExecutionEngine implementation that
124 // allocates a new memory block for the global instead of using the same
125 // global variable that is emitted by MCJIT. Hence, the pointer (gvPtr below)
126 // has the correct initial value, but updates to the real global (accessed by
127 // JITted code) are not propagated. Instead, getPointerToGlobal() should return
128 // a pointer into the loaded ObjectImage to reference the emitted global.
129 /*
130 TEST_F(MCJITTest, increment_global) {
131   SKIP_UNSUPPORTED_PLATFORM;
132
133   int32_t initialNum = 5;
134   Function *IncrementGlobal = startFunction<int32_t(void)>(M.get(), "IncrementGlobal");
135   GlobalVariable *GV = insertGlobalInt32(M.get(), "my_global", initialNum);
136   Value *DerefGV = Builder.CreateLoad(GV);
137   Value *AddResult = Builder.CreateAdd(DerefGV,
138                                        ConstantInt::get(Context, APInt(32, 1)));
139   Builder.CreateStore(AddResult, GV);
140   endFunctionWithRet(IncrementGlobal, AddResult);
141
142   createJIT(M.take());
143   void *gvPtr = TheJIT->getPointerToGlobal(GV);
144   EXPECT_EQ(initialNum, *(int32_t*)gvPtr);
145
146   void *vPtr = TheJIT->getFunctionAddress(IncrementGlobal->getName().str());
147   EXPECT_TRUE(0 != vPtr)
148     << "Unable to get pointer to main() from JIT";
149
150   int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)vPtr;
151
152   for(int i = 1; i < 3; ++i) {
153     int32_t result = FuncPtr();
154     EXPECT_EQ(initialNum + i, result);            // OK
155     EXPECT_EQ(initialNum + i, *(int32_t*)gvPtr);  // FAILS
156   }
157 }
158 */
159
160 // PR16013: XFAIL this test on ARM, which currently can't handle multiple relocations.
161 #if !defined(__arm__)
162
163 TEST_F(MCJITTest, multiple_functions) {
164   SKIP_UNSUPPORTED_PLATFORM;
165
166   unsigned int numLevels = 23;
167   int32_t innerRetVal= 5;
168
169   Function *Inner = startFunction<int32_t(void)>(M.get(), "Inner");
170   endFunctionWithRet(Inner, ConstantInt::get(Context, APInt(32, innerRetVal)));
171
172   Function *Outer;
173   for (unsigned int i = 0; i < numLevels; ++i) {
174     std::stringstream funcName;
175     funcName << "level_" << i;
176     Outer = startFunction<int32_t(void)>(M.get(), funcName.str());
177     Value *innerResult = Builder.CreateCall(Inner);
178     endFunctionWithRet(Outer, innerResult);
179
180     Inner = Outer;
181   }
182
183   createJIT(M.take());
184   uint64_t ptr = TheJIT->getFunctionAddress(Outer->getName().str());
185   EXPECT_TRUE(0 != ptr)
186     << "Unable to get pointer to outer function from JIT";
187
188   int32_t(*FuncPtr)(void) = (int32_t(*)(void))ptr;
189   EXPECT_EQ(innerRetVal, FuncPtr())
190     << "Incorrect result returned from function";
191 }
192
193 #endif /*!defined(__arm__)*/
194
195 }