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