Silence -Wunused-variable in release builds.
[llvm/stm8.git] / lib / ExecutionEngine / MCJIT / MCJIT.cpp
blob4475f4d5c0d8a3d4e95831d65f6dbd6eaefd4f3f
1 //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===//
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 //===----------------------------------------------------------------------===//
10 #include "MCJIT.h"
11 #include "MCJITMemoryManager.h"
12 #include "llvm/DerivedTypes.h"
13 #include "llvm/Function.h"
14 #include "llvm/ExecutionEngine/GenericValue.h"
15 #include "llvm/ExecutionEngine/MCJIT.h"
16 #include "llvm/ExecutionEngine/JITMemoryManager.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/DynamicLibrary.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Target/TargetData.h"
23 using namespace llvm;
25 namespace {
27 static struct RegisterJIT {
28 RegisterJIT() { MCJIT::Register(); }
29 } JITRegistrator;
33 extern "C" void LLVMLinkInMCJIT() {
36 ExecutionEngine *MCJIT::createJIT(Module *M,
37 std::string *ErrorStr,
38 JITMemoryManager *JMM,
39 CodeGenOpt::Level OptLevel,
40 bool GVsWithCode,
41 TargetMachine *TM) {
42 // Try to register the program as a source of symbols to resolve against.
44 // FIXME: Don't do this here.
45 sys::DynamicLibrary::LoadLibraryPermanently(0, NULL);
47 // If the target supports JIT code generation, create the JIT.
48 if (TargetJITInfo *TJ = TM->getJITInfo())
49 return new MCJIT(M, TM, *TJ, new MCJITMemoryManager(JMM, M), OptLevel,
50 GVsWithCode);
52 if (ErrorStr)
53 *ErrorStr = "target does not support JIT code generation";
54 return 0;
57 MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji,
58 RTDyldMemoryManager *MM, CodeGenOpt::Level OptLevel,
59 bool AllocateGVsWithCode)
60 : ExecutionEngine(m), TM(tm), MemMgr(MM), M(m), OS(Buffer), Dyld(MM) {
62 PM.add(new TargetData(*TM->getTargetData()));
64 // Turn the machine code intermediate representation into bytes in memory
65 // that may be executed.
66 if (TM->addPassesToEmitMC(PM, Ctx, OS, CodeGenOpt::Default, false)) {
67 report_fatal_error("Target does not support MC emission!");
70 // Initialize passes.
71 // FIXME: When we support multiple modules, we'll want to move the code
72 // gen and finalization out of the constructor here and do it more
73 // on-demand as part of getPointerToFunction().
74 PM.run(*M);
75 // Flush the output buffer so the SmallVector gets its data.
76 OS.flush();
78 // Load the object into the dynamic linker.
79 // FIXME: It would be nice to avoid making yet another copy.
80 MemoryBuffer *MB = MemoryBuffer::getMemBufferCopy(StringRef(Buffer.data(),
81 Buffer.size()));
82 if (Dyld.loadObject(MB))
83 report_fatal_error(Dyld.getErrorString());
84 // Resolve any relocations.
85 Dyld.resolveRelocations();
88 MCJIT::~MCJIT() {
89 delete MemMgr;
92 void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) {
93 report_fatal_error("not yet implemented");
94 return 0;
97 void *MCJIT::getPointerToFunction(Function *F) {
98 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
99 bool AbortOnFailure = !F->hasExternalWeakLinkage();
100 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
101 addGlobalMapping(F, Addr);
102 return Addr;
105 // FIXME: Should we be using the mangler for this? Probably.
106 StringRef BaseName = F->getName();
107 if (BaseName[0] == '\1')
108 return (void*)Dyld.getSymbolAddress(BaseName.substr(1));
109 return (void*)Dyld.getSymbolAddress((TM->getMCAsmInfo()->getGlobalPrefix()
110 + BaseName).str());
113 void *MCJIT::recompileAndRelinkFunction(Function *F) {
114 report_fatal_error("not yet implemented");
117 void MCJIT::freeMachineCodeForFunction(Function *F) {
118 report_fatal_error("not yet implemented");
121 GenericValue MCJIT::runFunction(Function *F,
122 const std::vector<GenericValue> &ArgValues) {
123 assert(F && "Function *F was null at entry to run()");
125 void *FPtr = getPointerToFunction(F);
126 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
127 const FunctionType *FTy = F->getFunctionType();
128 const Type *RetTy = FTy->getReturnType();
130 assert((FTy->getNumParams() == ArgValues.size() ||
131 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
132 "Wrong number of arguments passed into function!");
133 assert(FTy->getNumParams() == ArgValues.size() &&
134 "This doesn't support passing arguments through varargs (yet)!");
136 // Handle some common cases first. These cases correspond to common `main'
137 // prototypes.
138 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
139 switch (ArgValues.size()) {
140 case 3:
141 if (FTy->getParamType(0)->isIntegerTy(32) &&
142 FTy->getParamType(1)->isPointerTy() &&
143 FTy->getParamType(2)->isPointerTy()) {
144 int (*PF)(int, char **, const char **) =
145 (int(*)(int, char **, const char **))(intptr_t)FPtr;
147 // Call the function.
148 GenericValue rv;
149 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
150 (char **)GVTOP(ArgValues[1]),
151 (const char **)GVTOP(ArgValues[2])));
152 return rv;
154 break;
155 case 2:
156 if (FTy->getParamType(0)->isIntegerTy(32) &&
157 FTy->getParamType(1)->isPointerTy()) {
158 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
160 // Call the function.
161 GenericValue rv;
162 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
163 (char **)GVTOP(ArgValues[1])));
164 return rv;
166 break;
167 case 1:
168 if (FTy->getNumParams() == 1 &&
169 FTy->getParamType(0)->isIntegerTy(32)) {
170 GenericValue rv;
171 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
172 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
173 return rv;
175 break;
179 // Handle cases where no arguments are passed first.
180 if (ArgValues.empty()) {
181 GenericValue rv;
182 switch (RetTy->getTypeID()) {
183 default: llvm_unreachable("Unknown return type for function call!");
184 case Type::IntegerTyID: {
185 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
186 if (BitWidth == 1)
187 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
188 else if (BitWidth <= 8)
189 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
190 else if (BitWidth <= 16)
191 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
192 else if (BitWidth <= 32)
193 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
194 else if (BitWidth <= 64)
195 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
196 else
197 llvm_unreachable("Integer types > 64 bits not supported");
198 return rv;
200 case Type::VoidTyID:
201 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
202 return rv;
203 case Type::FloatTyID:
204 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
205 return rv;
206 case Type::DoubleTyID:
207 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
208 return rv;
209 case Type::X86_FP80TyID:
210 case Type::FP128TyID:
211 case Type::PPC_FP128TyID:
212 llvm_unreachable("long double not supported yet");
213 return rv;
214 case Type::PointerTyID:
215 return PTOGV(((void*(*)())(intptr_t)FPtr)());
219 assert("Full-featured argument passing not supported yet!");
220 return GenericValue();