blob: fd41c4c1273aa18a2043b6259ba47c714429bb95 [file] [log] [blame]
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001//===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
2//
3// This file implements the top-level support for creating a Just-In-Time
4// compiler for the current architecture.
5//
6//===----------------------------------------------------------------------===//
7
8#include "VM.h"
9#include "llvm/Target/TargetMachine.h"
10#include "llvm/Target/TargetMachineImpls.h"
11#include "llvm/Module.h"
Misha Brukmanabb027c2003-05-27 21:40:39 +000012#include "Support/CommandLine.h"
13
Misha Brukman4e8c9992003-06-06 06:59:55 +000014// FIXME: REMOVE THIS
15#include "llvm/PassManager.h"
16
Misha Brukmanabb027c2003-05-27 21:40:39 +000017namespace {
18 cl::opt<std::string>
19 Arch("march", cl::desc("Architecture: `x86' or `sparc'"), cl::Prefix,
20 cl::value_desc("machine architecture"));
21
22 static std::string DefaultArch =
23#if defined(i386) || defined(__i386__) || defined(__x86__)
24 "x86";
25#elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
26 "sparc";
27#else
28 "";
29#endif
30
31}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000032
Chris Lattnerbd199fb2002-12-24 00:01:05 +000033/// createJIT - Create an return a new JIT compiler if there is one available
34/// for the current target. Otherwise it returns null.
35///
36ExecutionEngine *ExecutionEngine::createJIT(Module *M, unsigned Config) {
Misha Brukmanabb027c2003-05-27 21:40:39 +000037
38 TargetMachine* (*TargetMachineAllocator)(unsigned) = 0;
39 if (Arch == "")
40 Arch = DefaultArch;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000041
Misha Brukmanabb027c2003-05-27 21:40:39 +000042 // Allow a command-line switch to override what *should* be the default target
43 // machine for this platform. This allows for debugging a Sparc JIT on X86 --
44 // our X86 machines are much faster at recompiling LLVM and linking lli.
45 if (Arch == "x86") {
46 TargetMachineAllocator = allocateX86TargetMachine;
47 } else if (Arch == "sparc") {
Misha Brukman906f5fa2003-06-02 03:23:16 +000048 TargetMachineAllocator = allocateSparcTargetMachine;
Misha Brukmanabb027c2003-05-27 21:40:39 +000049 }
50
51 if (TargetMachineAllocator) {
52 // Allocate a target...
53 TargetMachine *Target = (*TargetMachineAllocator)(Config);
54 assert(Target && "Could not allocate target machine!");
55
56 // Create the virtual machine object...
57 return new VM(M, Target);
58 } else {
59 return 0;
60 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000061}
62
63VM::VM(Module *M, TargetMachine *tm) : ExecutionEngine(M), TM(*tm) {
64 setTargetData(TM.getTargetData());
Misha Brukmanabb027c2003-05-27 21:40:39 +000065
66 // Initialize MCE
Misha Brukman906f5fa2003-06-02 03:23:16 +000067 MCE = createEmitter(*this);
Misha Brukmanabb027c2003-05-27 21:40:39 +000068
Chris Lattnerbd199fb2002-12-24 00:01:05 +000069 setupPassManager();
Misha Brukman4e8c9992003-06-06 06:59:55 +000070
71 // THIS GOES BEYOND UGLY HACKS
72 if (TM.getName() == "UltraSparc-Native") {
73 extern Pass *createPreSelectionPass(TargetMachine &TM);
74 PassManager PM;
75 // Specialize LLVM code for this target machine and then
76 // run basic dataflow optimizations on LLVM code.
77 PM.add(createPreSelectionPass(TM));
78 PM.run(*M);
79 }
80
Chris Lattner56adf152003-05-12 02:14:34 +000081 emitGlobals();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000082}
83
84int VM::run(const std::string &FnName, const std::vector<std::string> &Args) {
85 Function *F = getModule().getNamedFunction(FnName);
86 if (F == 0) {
87 std::cerr << "Could not find function '" << FnName <<"' in module!\n";
88 return 1;
89 }
90
91 int(*PF)(int, char**) = (int(*)(int, char**))getPointerToFunction(F);
92 assert(PF != 0 && "Null pointer to function?");
93
94 // Build an argv vector...
95 char **Argv = (char**)CreateArgv(Args);
96
97 // Call the main function...
Chris Lattner22080f92003-05-14 13:53:40 +000098 int Result = PF(Args.size(), Argv);
99
100 // Run any atexit handlers now!
101 runAtExitHandlers();
102 return Result;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000103}