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::aarch64);
48 SupportedArchs.push_back(Triple::arm);
49 SupportedArchs.push_back(Triple::mips);
50 SupportedArchs.push_back(Triple::x86);
51 SupportedArchs.push_back(Triple::x86_64);
53 // Some architectures have sub-architectures in which tests will fail, like
54 // ARM. These two vectors will define if they do have sub-archs (to avoid
55 // extra work for those who don't), and if so, if they are listed to work
56 HasSubArchs.push_back(Triple::arm);
57 SupportedSubArchs.push_back("armv6");
58 SupportedSubArchs.push_back("armv7");
60 // The operating systems below are known to be incompatible with MCJIT as
61 // they are copied from the test/ExecutionEngine/MCJIT/lit.local.cfg and
62 // should be kept in sync.
63 UnsupportedOSs.push_back(Triple::Cygwin);
64 UnsupportedOSs.push_back(Triple::Darwin);
67 Module *createEmptyModule(StringRef Name) {
68 Module * M = new Module(Name, Context);
69 M->setTargetTriple(Triple::normalize(HostTriple));
73 template<typename FuncType>
74 Function *startFunction(Module *M, StringRef Name) {
75 Function *Result = Function::Create(
76 TypeBuilder<FuncType, false>::get(Context),
77 GlobalValue::ExternalLinkage, Name, M);
79 BasicBlock *BB = BasicBlock::Create(Context, Name, Result);
80 Builder.SetInsertPoint(BB);
85 void endFunctionWithRet(Function *Func, Value *RetValue) {
86 Builder.CreateRet(RetValue);
89 // Inserts a simple function that invokes Callee and takes the same arguments:
90 // int Caller(...) { return Callee(...); }
91 template<typename Signature>
92 Function *insertSimpleCallFunction(Module *M, Function *Callee) {
93 Function *Result = startFunction<Signature>(M, "caller");
95 SmallVector<Value*, 1> CallArgs;
97 Function::arg_iterator arg_iter = Result->arg_begin();
98 for(;arg_iter != Result->arg_end(); ++arg_iter)
99 CallArgs.push_back(arg_iter);
101 Value *ReturnCode = Builder.CreateCall(Callee, CallArgs);
102 Builder.CreateRet(ReturnCode);
106 // Inserts a function named 'main' that returns a uint32_t:
107 // int32_t main() { return X; }
108 // where X is given by returnCode
109 Function *insertMainFunction(Module *M, uint32_t returnCode) {
110 Function *Result = startFunction<int32_t(void)>(M, "main");
112 Value *ReturnVal = ConstantInt::get(Context, APInt(32, returnCode));
113 endFunctionWithRet(Result, ReturnVal);
118 // Inserts a function
119 // int32_t add(int32_t a, int32_t b) { return a + b; }
120 // in the current module and returns a pointer to it.
121 Function *insertAddFunction(Module *M, StringRef Name = "add") {
122 Function *Result = startFunction<int32_t(int32_t, int32_t)>(M, Name);
124 Function::arg_iterator args = Result->arg_begin();
126 Value *Arg2 = ++args;
127 Value *AddResult = Builder.CreateAdd(Arg1, Arg2);
129 endFunctionWithRet(Result, AddResult);
134 // Inserts an declaration to a function defined elsewhere
135 Function *insertExternalReferenceToFunction(Module *M, StringRef Name,
136 FunctionType *FuncTy) {
137 Function *Result = Function::Create(FuncTy,
138 GlobalValue::ExternalLinkage,
143 // Inserts an declaration to a function defined elsewhere
144 Function *insertExternalReferenceToFunction(Module *M, Function *Func) {
145 Function *Result = Function::Create(Func->getFunctionType(),
146 GlobalValue::AvailableExternallyLinkage,
151 // Inserts a global variable of type int32
152 GlobalVariable *insertGlobalInt32(Module *M,
154 int32_t InitialValue) {
155 Type *GlobalTy = TypeBuilder<types::i<32>, true>::get(Context);
156 Constant *IV = ConstantInt::get(Context, APInt(32, InitialValue));
157 GlobalVariable *Global = new GlobalVariable(*M,
160 GlobalValue::ExternalLinkage,
166 void createJIT(Module *M) {
168 // Due to the EngineBuilder constructor, it is required to have a Module
169 // in order to construct an ExecutionEngine (i.e. MCJIT)
170 assert(M != 0 && "a non-null Module must be provided to create MCJIT");
174 TheJIT.reset(EB.setEngineKind(EngineKind::JIT)
175 .setUseMCJIT(true) /* can this be folded into the EngineKind enum? */
176 .setMCJITMemoryManager(MM)
178 .setOptLevel(CodeGenOpt::None)
179 .setAllocateGVsWithCode(false) /*does this do anything?*/
180 .setCodeModel(CodeModel::JITDefault)
181 .setRelocationModel(Reloc::Default)
183 .setMCPU(sys::getHostCPUName())
186 // At this point, we cannot modify the module any more.
187 assert(TheJIT.get() != NULL && "error creating MCJIT with EngineBuilder");
191 CodeGenOpt::Level OptLevel;
192 Reloc::Model RelocModel;
193 CodeModel::Model CodeModel;
195 SmallVector<std::string, 1> MAttrs;
196 OwningPtr<TargetMachine> TM;
197 OwningPtr<ExecutionEngine> TheJIT;
199 RTDyldMemoryManager *MM;
206 #endif // MCJIT_TEST_H