blob: fd358cda0585d1c03ca55653667893d22ac8c6e0 [file] [log] [blame]
Andrew Kaylor2d6d5852012-10-04 20:29:44 +00001//===- 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
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000020#include "llvm/Config/config.h"
21#include "llvm/ExecutionEngine/ExecutionEngine.h"
Andrew Kaylor927ba6a2012-11-27 19:42:02 +000022#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000023#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"
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000028#include "llvm/Support/CodeGen.h"
Andrew Kaylord2755af2013-04-29 17:49:40 +000029#include "MCJITTestAPICommon.h"
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000030
31namespace llvm {
32
Andrew Kaylord2755af2013-04-29 17:49:40 +000033class MCJITTestBase : public MCJITTestAPICommon {
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000034protected:
35
36 MCJITTestBase()
37 : OptLevel(CodeGenOpt::None)
38 , RelocModel(Reloc::Default)
39 , CodeModel(CodeModel::Default)
40 , MArch("")
41 , Builder(Context)
42 , MM(new SectionMemoryManager)
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000043 {
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000044 // 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.
Tim Northover233a4d72013-05-19 19:44:56 +000047 SupportedArchs.push_back(Triple::aarch64);
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000048 SupportedArchs.push_back(Triple::arm);
49 SupportedArchs.push_back(Triple::mips);
50 SupportedArchs.push_back(Triple::x86);
51 SupportedArchs.push_back(Triple::x86_64);
52
Renato Golin4e4464b2013-05-19 20:10:10 +000053 // 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");
59
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000060 // 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);
65 }
66
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000067 Module *createEmptyModule(StringRef Name) {
68 Module * M = new Module(Name, Context);
69 M->setTargetTriple(Triple::normalize(HostTriple));
70 return M;
71 }
72
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);
78
79 BasicBlock *BB = BasicBlock::Create(Context, Name, Result);
80 Builder.SetInsertPoint(BB);
81
82 return Result;
83 }
84
85 void endFunctionWithRet(Function *Func, Value *RetValue) {
86 Builder.CreateRet(RetValue);
87 }
88
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");
94
95 SmallVector<Value*, 1> CallArgs;
96
97 Function::arg_iterator arg_iter = Result->arg_begin();
98 for(;arg_iter != Result->arg_end(); ++arg_iter)
99 CallArgs.push_back(arg_iter);
100
101 Value *ReturnCode = Builder.CreateCall(Callee, CallArgs);
102 Builder.CreateRet(ReturnCode);
103 return Result;
104 }
105
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");
111
112 Value *ReturnVal = ConstantInt::get(Context, APInt(32, returnCode));
113 endFunctionWithRet(Result, ReturnVal);
114
115 return Result;
116 }
117
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);
123
124 Function::arg_iterator args = Result->arg_begin();
125 Value *Arg1 = args;
126 Value *Arg2 = ++args;
127 Value *AddResult = Builder.CreateAdd(Arg1, Arg2);
128
129 endFunctionWithRet(Result, AddResult);
130
131 return Result;
132 }
133
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,
139 Name, M);
140 return Result;
141 }
142
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,
147 Func->getName(), M);
148 return Result;
149 }
150
151 // Inserts a global variable of type int32
152 GlobalVariable *insertGlobalInt32(Module *M,
153 StringRef name,
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,
158 GlobalTy,
159 false,
160 GlobalValue::ExternalLinkage,
161 IV,
162 name);
163 return Global;
164 }
165
166 void createJIT(Module *M) {
167
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");
171
172 EngineBuilder EB(M);
173 std::string Error;
174 TheJIT.reset(EB.setEngineKind(EngineKind::JIT)
175 .setUseMCJIT(true) /* can this be folded into the EngineKind enum? */
Filip Pizlo13a3cf12013-05-14 19:29:00 +0000176 .setMCJITMemoryManager(MM)
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000177 .setErrorStr(&Error)
178 .setOptLevel(CodeGenOpt::None)
179 .setAllocateGVsWithCode(false) /*does this do anything?*/
180 .setCodeModel(CodeModel::JITDefault)
181 .setRelocationModel(Reloc::Default)
182 .setMArch(MArch)
183 .setMCPU(sys::getHostCPUName())
184 //.setMAttrs(MAttrs)
185 .create());
186 // At this point, we cannot modify the module any more.
187 assert(TheJIT.get() != NULL && "error creating MCJIT with EngineBuilder");
188 }
189
190 LLVMContext Context;
191 CodeGenOpt::Level OptLevel;
192 Reloc::Model RelocModel;
193 CodeModel::Model CodeModel;
194 StringRef MArch;
195 SmallVector<std::string, 1> MAttrs;
196 OwningPtr<TargetMachine> TM;
197 OwningPtr<ExecutionEngine> TheJIT;
198 IRBuilder<> Builder;
Filip Pizlo13a3cf12013-05-14 19:29:00 +0000199 RTDyldMemoryManager *MM;
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000200
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000201 OwningPtr<Module> M;
202};
203
204} // namespace llvm
205
206#endif // MCJIT_TEST_H