1 //===-- JIT.cpp - MC-based Just-in-Time Compiler --------------------------===//
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 //===----------------------------------------------------------------------===//
11 #include "llvm/Function.h"
12 #include "llvm/ExecutionEngine/GenericValue.h"
13 #include "llvm/ExecutionEngine/MCJIT.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include "llvm/Support/DynamicLibrary.h"
16 #include "llvm/Target/TargetData.h"
22 static struct RegisterJIT {
23 RegisterJIT() { MCJIT::Register(); }
28 extern "C" void LLVMLinkInMCJIT() {
31 ExecutionEngine *MCJIT::createJIT(Module *M,
32 std::string *ErrorStr,
33 JITMemoryManager *JMM,
34 CodeGenOpt::Level OptLevel,
39 const SmallVectorImpl<std::string>& MAttrs) {
40 // Try to register the program as a source of symbols to resolve against.
42 // FIXME: Don't do this here.
43 sys::DynamicLibrary::LoadLibraryPermanently(0, NULL);
45 // Pick a target either via -march or by guessing the native arch.
47 // FIXME: This should be lifted out of here, it isn't something which should
48 // be part of the JIT policy, rather the burden for this selection should be
50 TargetMachine *TM = MCJIT::selectTarget(M, MArch, MCPU, MAttrs, ErrorStr);
51 if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0;
52 TM->setCodeModel(CMM);
54 // If the target supports JIT code generation, create the JIT.
55 if (TargetJITInfo *TJ = TM->getJITInfo())
56 return new MCJIT(M, TM, *TJ, JMM, OptLevel, GVsWithCode);
59 *ErrorStr = "target does not support JIT code generation";
63 MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji,
64 JITMemoryManager *JMM, CodeGenOpt::Level OptLevel,
65 bool AllocateGVsWithCode)
66 : ExecutionEngine(m), TM(tm), M(m), OS(Buffer) {
68 PM.add(new TargetData(*TM->getTargetData()));
70 // Turn the machine code intermediate representation into bytes in memory
71 // that may be executed.
72 if (TM->addPassesToEmitMC(PM, Ctx, OS, CodeGenOpt::Default, false)) {
73 report_fatal_error("Target does not support MC emission!");
77 ExecutionEngine::addModule(M);
78 // FIXME: When we support multiple modules, we'll want to move the code
79 // gen and finalization out of the constructor here and do it more
80 // on-demand as part of getPointerToFunction().
82 // Flush the output buffer so the SmallVector gets its data.
89 void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) {
90 report_fatal_error("not yet implemented");
94 void *MCJIT::getPointerToFunction(Function *F) {
98 void *MCJIT::recompileAndRelinkFunction(Function *F) {
99 report_fatal_error("not yet implemented");
102 void MCJIT::freeMachineCodeForFunction(Function *F) {
103 report_fatal_error("not yet implemented");
106 GenericValue MCJIT::runFunction(Function *F,
107 const std::vector<GenericValue> &ArgValues) {
108 assert(ArgValues.size() == 0 && "JIT arg passing not supported yet");
109 void *FPtr = getPointerToFunction(F);
111 report_fatal_error("Unable to locate function: '" + F->getName() + "'");
112 ((void(*)(void))FPtr)();
113 return GenericValue();