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