[mips] Use pristine object file while processing relocations.
[oota-llvm.git] / unittests / ExecutionEngine / MCJIT / MCJITTestBase.h
1 //===- MCJITTestBase.h - Common base class for MCJIT Unit tests  ----------===//
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 class implements common functionality required by the MCJIT unit tests,
11 // as well as logic to skip tests on unsupported architectures and operating
12 // systems.
13 //
14 //===----------------------------------------------------------------------===//
15
16
17 #ifndef MCJIT_TEST_BASE_H
18 #define MCJIT_TEST_BASE_H
19
20 #include "llvm/Config/config.h"
21 #include "llvm/ExecutionEngine/ExecutionEngine.h"
22 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/TypeBuilder.h"
28 #include "llvm/Support/CodeGen.h"
29 #include "MCJITTestAPICommon.h"
30
31 namespace llvm {
32
33 /// Helper class that can build very simple Modules
34 class TrivialModuleBuilder {
35 protected:
36   LLVMContext Context;
37   IRBuilder<> Builder;
38   std::string BuilderTriple;
39
40   TrivialModuleBuilder(const std::string &Triple)
41     : Builder(Context), BuilderTriple(Triple) {}
42
43   Module *createEmptyModule(StringRef Name = StringRef()) {
44     Module * M = new Module(Name, Context);
45     M->setTargetTriple(Triple::normalize(BuilderTriple));
46     return M;
47   }
48
49   template<typename FuncType>
50   Function *startFunction(Module *M, StringRef Name) {
51     Function *Result = Function::Create(
52       TypeBuilder<FuncType, false>::get(Context),
53       GlobalValue::ExternalLinkage, Name, M);
54
55     BasicBlock *BB = BasicBlock::Create(Context, Name, Result);
56     Builder.SetInsertPoint(BB);
57
58     return Result;
59   }
60
61   void endFunctionWithRet(Function *Func, Value *RetValue) {
62     Builder.CreateRet(RetValue);
63   }
64
65   // Inserts a simple function that invokes Callee and takes the same arguments:
66   //    int Caller(...) { return Callee(...); }
67   template<typename Signature>
68   Function *insertSimpleCallFunction(Module *M, Function *Callee) {
69     Function *Result = startFunction<Signature>(M, "caller");
70
71     SmallVector<Value*, 1> CallArgs;
72
73     Function::arg_iterator arg_iter = Result->arg_begin();
74     for(;arg_iter != Result->arg_end(); ++arg_iter)
75       CallArgs.push_back(arg_iter);
76
77     Value *ReturnCode = Builder.CreateCall(Callee, CallArgs);
78     Builder.CreateRet(ReturnCode);
79     return Result;
80   }
81
82   // Inserts a function named 'main' that returns a uint32_t:
83   //    int32_t main() { return X; }
84   // where X is given by returnCode
85   Function *insertMainFunction(Module *M, uint32_t returnCode) {
86     Function *Result = startFunction<int32_t(void)>(M, "main");
87
88     Value *ReturnVal = ConstantInt::get(Context, APInt(32, returnCode));
89     endFunctionWithRet(Result, ReturnVal);
90
91     return Result;
92   }
93
94   // Inserts a function
95   //    int32_t add(int32_t a, int32_t b) { return a + b; }
96   // in the current module and returns a pointer to it.
97   Function *insertAddFunction(Module *M, StringRef Name = "add") {
98     Function *Result = startFunction<int32_t(int32_t, int32_t)>(M, Name);
99
100     Function::arg_iterator args = Result->arg_begin();
101     Value *Arg1 = args;
102     Value *Arg2 = ++args;
103     Value *AddResult = Builder.CreateAdd(Arg1, Arg2);
104
105     endFunctionWithRet(Result, AddResult);
106
107     return Result;
108   }
109
110   // Inserts an declaration to a function defined elsewhere
111   Function *insertExternalReferenceToFunction(Module *M, StringRef Name,
112                                               FunctionType *FuncTy) {
113     Function *Result = Function::Create(FuncTy,
114                                         GlobalValue::ExternalLinkage,
115                                         Name, M);
116     return Result;
117   }
118
119   // Inserts an declaration to a function defined elsewhere
120   Function *insertExternalReferenceToFunction(Module *M, Function *Func) {
121     Function *Result = Function::Create(Func->getFunctionType(),
122                                         GlobalValue::AvailableExternallyLinkage,
123                                         Func->getName(), M);
124     return Result;
125   }
126
127   // Inserts a global variable of type int32
128   GlobalVariable *insertGlobalInt32(Module *M,
129                                     StringRef name,
130                                     int32_t InitialValue) {
131     Type *GlobalTy = TypeBuilder<types::i<32>, true>::get(Context);
132     Constant *IV = ConstantInt::get(Context, APInt(32, InitialValue));
133     GlobalVariable *Global = new GlobalVariable(*M,
134                                                 GlobalTy,
135                                                 false,
136                                                 GlobalValue::ExternalLinkage,
137                                                 IV,
138                                                 name);
139     return Global;
140   }
141 };
142
143 class MCJITTestBase : public MCJITTestAPICommon, public TrivialModuleBuilder {
144 protected:
145   
146   MCJITTestBase()
147     : TrivialModuleBuilder(HostTriple)
148     , OptLevel(CodeGenOpt::None)
149     , RelocModel(Reloc::Default)
150     , CodeModel(CodeModel::Default)
151     , MArch("")
152     , MM(new SectionMemoryManager)
153   {
154     // The architectures below are known to be compatible with MCJIT as they
155     // are copied from test/ExecutionEngine/MCJIT/lit.local.cfg and should be
156     // kept in sync.
157     SupportedArchs.push_back(Triple::aarch64);
158     SupportedArchs.push_back(Triple::arm);
159     SupportedArchs.push_back(Triple::mips);
160     SupportedArchs.push_back(Triple::mipsel);
161     SupportedArchs.push_back(Triple::x86);
162     SupportedArchs.push_back(Triple::x86_64);
163
164     // Some architectures have sub-architectures in which tests will fail, like
165     // ARM. These two vectors will define if they do have sub-archs (to avoid
166     // extra work for those who don't), and if so, if they are listed to work
167     HasSubArchs.push_back(Triple::arm);
168     SupportedSubArchs.push_back("armv6");
169     SupportedSubArchs.push_back("armv7");
170
171     // The operating systems below are known to be incompatible with MCJIT as
172     // they are copied from the test/ExecutionEngine/MCJIT/lit.local.cfg and
173     // should be kept in sync.
174     UnsupportedOSs.push_back(Triple::Cygwin);
175     UnsupportedOSs.push_back(Triple::Darwin);
176   }
177
178   void createJIT(Module *M) {
179
180     // Due to the EngineBuilder constructor, it is required to have a Module
181     // in order to construct an ExecutionEngine (i.e. MCJIT)
182     assert(M != 0 && "a non-null Module must be provided to create MCJIT");
183
184     EngineBuilder EB(M);
185     std::string Error;
186     TheJIT.reset(EB.setEngineKind(EngineKind::JIT)
187                  .setUseMCJIT(true) /* can this be folded into the EngineKind enum? */
188                  .setMCJITMemoryManager(MM)
189                  .setErrorStr(&Error)
190                  .setOptLevel(CodeGenOpt::None)
191                  .setAllocateGVsWithCode(false) /*does this do anything?*/
192                  .setCodeModel(CodeModel::JITDefault)
193                  .setRelocationModel(Reloc::Default)
194                  .setMArch(MArch)
195                  .setMCPU(sys::getHostCPUName())
196                  //.setMAttrs(MAttrs)
197                  .create());
198     // At this point, we cannot modify the module any more.
199     assert(TheJIT.get() != NULL && "error creating MCJIT with EngineBuilder");
200   }
201
202   CodeGenOpt::Level OptLevel;
203   Reloc::Model RelocModel;
204   CodeModel::Model CodeModel;
205   StringRef MArch;
206   SmallVector<std::string, 1> MAttrs;
207   OwningPtr<ExecutionEngine> TheJIT;
208   RTDyldMemoryManager *MM;
209
210   OwningPtr<Module> M;
211 };
212
213 } // namespace llvm
214
215 #endif // MCJIT_TEST_H