SectionMemoryManager shouldn't be a JITMemoryManager. Previously, the
[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 class MCJITTestBase : public MCJITTestAPICommon {
34 protected:
35
36   MCJITTestBase()
37     : OptLevel(CodeGenOpt::None)
38     , RelocModel(Reloc::Default)
39     , CodeModel(CodeModel::Default)
40     , MArch("")
41     , Builder(Context)
42     , MM(new SectionMemoryManager)
43   {
44     // The architectures below are known to be compatible with MCJIT as they
45     // are copied from test/ExecutionEngine/MCJIT/lit.local.cfg and should be
46     // kept in sync.
47     SupportedArchs.push_back(Triple::arm);
48     SupportedArchs.push_back(Triple::mips);
49     SupportedArchs.push_back(Triple::x86);
50     SupportedArchs.push_back(Triple::x86_64);
51
52     // The operating systems below are known to be incompatible with MCJIT as
53     // they are copied from the test/ExecutionEngine/MCJIT/lit.local.cfg and
54     // should be kept in sync.
55     UnsupportedOSs.push_back(Triple::Cygwin);
56     UnsupportedOSs.push_back(Triple::Darwin);
57   }
58
59   Module *createEmptyModule(StringRef Name) {
60     Module * M = new Module(Name, Context);
61     M->setTargetTriple(Triple::normalize(HostTriple));
62     return M;
63   }
64
65   template<typename FuncType>
66   Function *startFunction(Module *M, StringRef Name) {
67     Function *Result = Function::Create(
68       TypeBuilder<FuncType, false>::get(Context),
69       GlobalValue::ExternalLinkage, Name, M);
70
71     BasicBlock *BB = BasicBlock::Create(Context, Name, Result);
72     Builder.SetInsertPoint(BB);
73
74     return Result;
75   }
76
77   void endFunctionWithRet(Function *Func, Value *RetValue) {
78     Builder.CreateRet(RetValue);
79   }
80
81   // Inserts a simple function that invokes Callee and takes the same arguments:
82   //    int Caller(...) { return Callee(...); }
83   template<typename Signature>
84   Function *insertSimpleCallFunction(Module *M, Function *Callee) {
85     Function *Result = startFunction<Signature>(M, "caller");
86
87     SmallVector<Value*, 1> CallArgs;
88
89     Function::arg_iterator arg_iter = Result->arg_begin();
90     for(;arg_iter != Result->arg_end(); ++arg_iter)
91       CallArgs.push_back(arg_iter);
92
93     Value *ReturnCode = Builder.CreateCall(Callee, CallArgs);
94     Builder.CreateRet(ReturnCode);
95     return Result;
96   }
97
98   // Inserts a function named 'main' that returns a uint32_t:
99   //    int32_t main() { return X; }
100   // where X is given by returnCode
101   Function *insertMainFunction(Module *M, uint32_t returnCode) {
102     Function *Result = startFunction<int32_t(void)>(M, "main");
103
104     Value *ReturnVal = ConstantInt::get(Context, APInt(32, returnCode));
105     endFunctionWithRet(Result, ReturnVal);
106
107     return Result;
108   }
109
110   // Inserts a function
111   //    int32_t add(int32_t a, int32_t b) { return a + b; }
112   // in the current module and returns a pointer to it.
113   Function *insertAddFunction(Module *M, StringRef Name = "add") {
114     Function *Result = startFunction<int32_t(int32_t, int32_t)>(M, Name);
115
116     Function::arg_iterator args = Result->arg_begin();
117     Value *Arg1 = args;
118     Value *Arg2 = ++args;
119     Value *AddResult = Builder.CreateAdd(Arg1, Arg2);
120
121     endFunctionWithRet(Result, AddResult);
122
123     return Result;
124   }
125
126   // Inserts an declaration to a function defined elsewhere
127   Function *insertExternalReferenceToFunction(Module *M, StringRef Name,
128                                               FunctionType *FuncTy) {
129     Function *Result = Function::Create(FuncTy,
130                                         GlobalValue::ExternalLinkage,
131                                         Name, M);
132     return Result;
133   }
134
135   // Inserts an declaration to a function defined elsewhere
136   Function *insertExternalReferenceToFunction(Module *M, Function *Func) {
137     Function *Result = Function::Create(Func->getFunctionType(),
138                                         GlobalValue::AvailableExternallyLinkage,
139                                         Func->getName(), M);
140     return Result;
141   }
142
143   // Inserts a global variable of type int32
144   GlobalVariable *insertGlobalInt32(Module *M,
145                                     StringRef name,
146                                     int32_t InitialValue) {
147     Type *GlobalTy = TypeBuilder<types::i<32>, true>::get(Context);
148     Constant *IV = ConstantInt::get(Context, APInt(32, InitialValue));
149     GlobalVariable *Global = new GlobalVariable(*M,
150                                                 GlobalTy,
151                                                 false,
152                                                 GlobalValue::ExternalLinkage,
153                                                 IV,
154                                                 name);
155     return Global;
156   }
157
158   void createJIT(Module *M) {
159
160     // Due to the EngineBuilder constructor, it is required to have a Module
161     // in order to construct an ExecutionEngine (i.e. MCJIT)
162     assert(M != 0 && "a non-null Module must be provided to create MCJIT");
163
164     EngineBuilder EB(M);
165     std::string Error;
166     TheJIT.reset(EB.setEngineKind(EngineKind::JIT)
167                  .setUseMCJIT(true) /* can this be folded into the EngineKind enum? */
168                  .setMCJITMemoryManager(MM)
169                  .setErrorStr(&Error)
170                  .setOptLevel(CodeGenOpt::None)
171                  .setAllocateGVsWithCode(false) /*does this do anything?*/
172                  .setCodeModel(CodeModel::JITDefault)
173                  .setRelocationModel(Reloc::Default)
174                  .setMArch(MArch)
175                  .setMCPU(sys::getHostCPUName())
176                  //.setMAttrs(MAttrs)
177                  .create());
178     // At this point, we cannot modify the module any more.
179     assert(TheJIT.get() != NULL && "error creating MCJIT with EngineBuilder");
180   }
181
182   LLVMContext Context;
183   CodeGenOpt::Level OptLevel;
184   Reloc::Model RelocModel;
185   CodeModel::Model CodeModel;
186   StringRef MArch;
187   SmallVector<std::string, 1> MAttrs;
188   OwningPtr<TargetMachine> TM;
189   OwningPtr<ExecutionEngine> TheJIT;
190   IRBuilder<> Builder;
191   RTDyldMemoryManager *MM;
192
193   OwningPtr<Module> M;
194 };
195
196 } // namespace llvm
197
198 #endif // MCJIT_TEST_H