1 //===- MCJITTestBase.h - Common base class for MCJIT Unit tests ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
17 #ifndef MCJIT_TEST_BASE_H
18 #define MCJIT_TEST_BASE_H
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"
33 class MCJITTestBase : public MCJITTestAPICommon {
37 : OptLevel(CodeGenOpt::None)
38 , RelocModel(Reloc::Default)
39 , CodeModel(CodeModel::Default)
42 , MM(new SectionMemoryManager)
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
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);
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);
59 Module *createEmptyModule(StringRef Name) {
60 Module * M = new Module(Name, Context);
61 M->setTargetTriple(Triple::normalize(HostTriple));
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);
71 BasicBlock *BB = BasicBlock::Create(Context, Name, Result);
72 Builder.SetInsertPoint(BB);
77 void endFunctionWithRet(Function *Func, Value *RetValue) {
78 Builder.CreateRet(RetValue);
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");
87 SmallVector<Value*, 1> CallArgs;
89 Function::arg_iterator arg_iter = Result->arg_begin();
90 for(;arg_iter != Result->arg_end(); ++arg_iter)
91 CallArgs.push_back(arg_iter);
93 Value *ReturnCode = Builder.CreateCall(Callee, CallArgs);
94 Builder.CreateRet(ReturnCode);
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");
104 Value *ReturnVal = ConstantInt::get(Context, APInt(32, returnCode));
105 endFunctionWithRet(Result, ReturnVal);
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);
116 Function::arg_iterator args = Result->arg_begin();
118 Value *Arg2 = ++args;
119 Value *AddResult = Builder.CreateAdd(Arg1, Arg2);
121 endFunctionWithRet(Result, AddResult);
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,
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,
143 // Inserts a global variable of type int32
144 GlobalVariable *insertGlobalInt32(Module *M,
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,
152 GlobalValue::ExternalLinkage,
158 void createJIT(Module *M) {
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");
166 TheJIT.reset(EB.setEngineKind(EngineKind::JIT)
167 .setUseMCJIT(true) /* can this be folded into the EngineKind enum? */
168 .setJITMemoryManager(MM)
170 .setOptLevel(CodeGenOpt::None)
171 .setAllocateGVsWithCode(false) /*does this do anything?*/
172 .setCodeModel(CodeModel::JITDefault)
173 .setRelocationModel(Reloc::Default)
175 .setMCPU(sys::getHostCPUName())
178 // At this point, we cannot modify the module any more.
179 assert(TheJIT.get() != NULL && "error creating MCJIT with EngineBuilder");
183 CodeGenOpt::Level OptLevel;
184 Reloc::Model RelocModel;
185 CodeModel::Model CodeModel;
187 SmallVector<std::string, 1> MAttrs;
188 OwningPtr<TargetMachine> TM;
189 OwningPtr<ExecutionEngine> TheJIT;
191 JITMemoryManager *MM;
198 #endif // MCJIT_TEST_H