Eric Christopher | bb498ca | 2011-04-22 03:07:06 +0000 | [diff] [blame] | 1 | //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===// |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 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 | #include "MCJIT.h" |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 11 | #include "MCJITMemoryManager.h" |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 12 | #include "llvm/DerivedTypes.h" |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 13 | #include "llvm/Function.h" |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 14 | #include "llvm/ExecutionEngine/GenericValue.h" |
| 15 | #include "llvm/ExecutionEngine/MCJIT.h" |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
| 17 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 19 | #include "llvm/Support/DynamicLibrary.h" |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MemoryBuffer.h" |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | static struct RegisterJIT { |
| 28 | RegisterJIT() { MCJIT::Register(); } |
| 29 | } JITRegistrator; |
| 30 | |
| 31 | } |
| 32 | |
| 33 | extern "C" void LLVMLinkInMCJIT() { |
| 34 | } |
| 35 | |
| 36 | ExecutionEngine *MCJIT::createJIT(Module *M, |
| 37 | std::string *ErrorStr, |
| 38 | JITMemoryManager *JMM, |
| 39 | CodeGenOpt::Level OptLevel, |
| 40 | bool GVsWithCode, |
Dylan Noblesmith | c06b511 | 2011-05-06 22:06:22 +0000 | [diff] [blame] | 41 | TargetMachine *TM) { |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 42 | // Try to register the program as a source of symbols to resolve against. |
| 43 | // |
| 44 | // FIXME: Don't do this here. |
| 45 | sys::DynamicLibrary::LoadLibraryPermanently(0, NULL); |
| 46 | |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 47 | // If the target supports JIT code generation, create the JIT. |
| 48 | if (TargetJITInfo *TJ = TM->getJITInfo()) |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 49 | return new MCJIT(M, TM, *TJ, new MCJITMemoryManager(JMM), OptLevel, |
| 50 | GVsWithCode); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 51 | |
| 52 | if (ErrorStr) |
| 53 | *ErrorStr = "target does not support JIT code generation"; |
| 54 | return 0; |
| 55 | } |
| 56 | |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 57 | MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji, |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 58 | RTDyldMemoryManager *MM, CodeGenOpt::Level OptLevel, |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 59 | bool AllocateGVsWithCode) |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 60 | : ExecutionEngine(m), TM(tm), MemMgr(MM), M(m), OS(Buffer), Dyld(MM) { |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 61 | |
| 62 | PM.add(new TargetData(*TM->getTargetData())); |
| 63 | |
| 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!"); |
| 68 | } |
| 69 | |
| 70 | // Initialize passes. |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 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(); |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 77 | |
| 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())); |
Jim Grosbach | 8086f3b | 2011-03-23 19:51:34 +0000 | [diff] [blame] | 82 | if (Dyld.loadObject(MB)) |
| 83 | report_fatal_error(Dyld.getErrorString()); |
Jim Grosbach | 69e8132 | 2011-04-13 15:28:10 +0000 | [diff] [blame] | 84 | // Resolve any relocations. |
| 85 | Dyld.resolveRelocations(); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | MCJIT::~MCJIT() { |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 89 | delete MemMgr; |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) { |
| 93 | report_fatal_error("not yet implemented"); |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | void *MCJIT::getPointerToFunction(Function *F) { |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 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; |
| 103 | } |
| 104 | |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 105 | Twine Name = TM->getMCAsmInfo()->getGlobalPrefix() + F->getName(); |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 106 | return (void*)Dyld.getSymbolAddress(Name.str()); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void *MCJIT::recompileAndRelinkFunction(Function *F) { |
| 110 | report_fatal_error("not yet implemented"); |
| 111 | } |
| 112 | |
| 113 | void MCJIT::freeMachineCodeForFunction(Function *F) { |
| 114 | report_fatal_error("not yet implemented"); |
| 115 | } |
| 116 | |
| 117 | GenericValue MCJIT::runFunction(Function *F, |
| 118 | const std::vector<GenericValue> &ArgValues) { |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 119 | assert(F && "Function *F was null at entry to run()"); |
| 120 | |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 121 | void *FPtr = getPointerToFunction(F); |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 122 | assert(FPtr && "Pointer to fn's code was null after getPointerToFunction"); |
| 123 | const FunctionType *FTy = F->getFunctionType(); |
| 124 | const Type *RetTy = FTy->getReturnType(); |
| 125 | |
| 126 | assert((FTy->getNumParams() == ArgValues.size() || |
| 127 | (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) && |
| 128 | "Wrong number of arguments passed into function!"); |
| 129 | assert(FTy->getNumParams() == ArgValues.size() && |
| 130 | "This doesn't support passing arguments through varargs (yet)!"); |
| 131 | |
| 132 | // Handle some common cases first. These cases correspond to common `main' |
| 133 | // prototypes. |
| 134 | if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) { |
| 135 | switch (ArgValues.size()) { |
| 136 | case 3: |
| 137 | if (FTy->getParamType(0)->isIntegerTy(32) && |
| 138 | FTy->getParamType(1)->isPointerTy() && |
| 139 | FTy->getParamType(2)->isPointerTy()) { |
| 140 | int (*PF)(int, char **, const char **) = |
| 141 | (int(*)(int, char **, const char **))(intptr_t)FPtr; |
| 142 | |
| 143 | // Call the function. |
| 144 | GenericValue rv; |
| 145 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), |
| 146 | (char **)GVTOP(ArgValues[1]), |
| 147 | (const char **)GVTOP(ArgValues[2]))); |
| 148 | return rv; |
| 149 | } |
| 150 | break; |
| 151 | case 2: |
| 152 | if (FTy->getParamType(0)->isIntegerTy(32) && |
| 153 | FTy->getParamType(1)->isPointerTy()) { |
| 154 | int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr; |
| 155 | |
| 156 | // Call the function. |
| 157 | GenericValue rv; |
| 158 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), |
| 159 | (char **)GVTOP(ArgValues[1]))); |
| 160 | return rv; |
| 161 | } |
| 162 | break; |
| 163 | case 1: |
| 164 | if (FTy->getNumParams() == 1 && |
| 165 | FTy->getParamType(0)->isIntegerTy(32)) { |
| 166 | GenericValue rv; |
| 167 | int (*PF)(int) = (int(*)(int))(intptr_t)FPtr; |
| 168 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue())); |
| 169 | return rv; |
| 170 | } |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Handle cases where no arguments are passed first. |
| 176 | if (ArgValues.empty()) { |
| 177 | GenericValue rv; |
| 178 | switch (RetTy->getTypeID()) { |
| 179 | default: llvm_unreachable("Unknown return type for function call!"); |
| 180 | case Type::IntegerTyID: { |
| 181 | unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth(); |
| 182 | if (BitWidth == 1) |
| 183 | rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)()); |
| 184 | else if (BitWidth <= 8) |
| 185 | rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)()); |
| 186 | else if (BitWidth <= 16) |
| 187 | rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)()); |
| 188 | else if (BitWidth <= 32) |
| 189 | rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)()); |
| 190 | else if (BitWidth <= 64) |
| 191 | rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)()); |
| 192 | else |
| 193 | llvm_unreachable("Integer types > 64 bits not supported"); |
| 194 | return rv; |
| 195 | } |
| 196 | case Type::VoidTyID: |
| 197 | rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)()); |
| 198 | return rv; |
| 199 | case Type::FloatTyID: |
| 200 | rv.FloatVal = ((float(*)())(intptr_t)FPtr)(); |
| 201 | return rv; |
| 202 | case Type::DoubleTyID: |
| 203 | rv.DoubleVal = ((double(*)())(intptr_t)FPtr)(); |
| 204 | return rv; |
| 205 | case Type::X86_FP80TyID: |
| 206 | case Type::FP128TyID: |
| 207 | case Type::PPC_FP128TyID: |
| 208 | llvm_unreachable("long double not supported yet"); |
| 209 | return rv; |
| 210 | case Type::PointerTyID: |
| 211 | return PTOGV(((void*(*)())(intptr_t)FPtr)()); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | assert("Full-featured argument passing not supported yet!"); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 216 | return GenericValue(); |
| 217 | } |