blob: 229754775c11fd2b0d9c3ecb9cf974143fed0820 [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 {
Chris Lattner97ac14f2003-06-17 15:43:13 +000018 enum ArchName { nojit, x86, sparc };
19
20 cl::opt<ArchName>
21 Arch("march", cl::desc("Architecture to JIT to:"), cl::Prefix,
22 cl::values(clEnumVal(x86, " IA-32 (pentium and above)"),
23#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
24 clEnumVal(sparc, " Sparc-V9"),
Misha Brukmanabb027c2003-05-27 21:40:39 +000025#endif
Chris Lattner97ac14f2003-06-17 15:43:13 +000026 0),
27#if defined(i386) || defined(__i386__) || defined(__x86__)
28 cl::init(x86)
29#elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
30 cl::init(sparc)
31#else
32 cl::init(nojit)
33#endif
34 );
Misha Brukmanabb027c2003-05-27 21:40:39 +000035}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000036
Chris Lattnerbd199fb2002-12-24 00:01:05 +000037/// createJIT - Create an return a new JIT compiler if there is one available
38/// for the current target. Otherwise it returns null.
39///
40ExecutionEngine *ExecutionEngine::createJIT(Module *M, unsigned Config) {
Misha Brukmanabb027c2003-05-27 21:40:39 +000041
42 TargetMachine* (*TargetMachineAllocator)(unsigned) = 0;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000043
Misha Brukmanabb027c2003-05-27 21:40:39 +000044 // Allow a command-line switch to override what *should* be the default target
45 // machine for this platform. This allows for debugging a Sparc JIT on X86 --
46 // our X86 machines are much faster at recompiling LLVM and linking lli.
Chris Lattner97ac14f2003-06-17 15:43:13 +000047 switch (Arch) {
48 case x86:
Misha Brukmanabb027c2003-05-27 21:40:39 +000049 TargetMachineAllocator = allocateX86TargetMachine;
Chris Lattner97ac14f2003-06-17 15:43:13 +000050 break;
Chris Lattner7aefa962003-06-17 15:32:38 +000051#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Chris Lattner97ac14f2003-06-17 15:43:13 +000052 case sparc:
Misha Brukman906f5fa2003-06-02 03:23:16 +000053 TargetMachineAllocator = allocateSparcTargetMachine;
Chris Lattner97ac14f2003-06-17 15:43:13 +000054 break;
Chris Lattner7aefa962003-06-17 15:32:38 +000055#endif
Chris Lattner97ac14f2003-06-17 15:43:13 +000056 default:
57 assert(0 && "-march flag not supported on this host!");
58 case nojit:
Misha Brukmanabb027c2003-05-27 21:40:39 +000059 return 0;
60 }
Chris Lattner97ac14f2003-06-17 15:43:13 +000061
62 // Allocate a target...
63 TargetMachine *Target = (*TargetMachineAllocator)(Config);
64 assert(Target && "Could not allocate target machine!");
65
66 // Create the virtual machine object...
67 return new VM(M, Target);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000068}
69
70VM::VM(Module *M, TargetMachine *tm) : ExecutionEngine(M), TM(*tm) {
71 setTargetData(TM.getTargetData());
Misha Brukmanabb027c2003-05-27 21:40:39 +000072
73 // Initialize MCE
Misha Brukman906f5fa2003-06-02 03:23:16 +000074 MCE = createEmitter(*this);
Misha Brukmanabb027c2003-05-27 21:40:39 +000075
Chris Lattnerbd199fb2002-12-24 00:01:05 +000076 setupPassManager();
Misha Brukman4e8c9992003-06-06 06:59:55 +000077
Chris Lattner7aefa962003-06-17 15:32:38 +000078#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukman4e8c9992003-06-06 06:59:55 +000079 // THIS GOES BEYOND UGLY HACKS
80 if (TM.getName() == "UltraSparc-Native") {
81 extern Pass *createPreSelectionPass(TargetMachine &TM);
82 PassManager PM;
83 // Specialize LLVM code for this target machine and then
84 // run basic dataflow optimizations on LLVM code.
85 PM.add(createPreSelectionPass(TM));
86 PM.run(*M);
87 }
Chris Lattner7aefa962003-06-17 15:32:38 +000088#endif
Misha Brukman4e8c9992003-06-06 06:59:55 +000089
Chris Lattner56adf152003-05-12 02:14:34 +000090 emitGlobals();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000091}
92
93int VM::run(const std::string &FnName, const std::vector<std::string> &Args) {
94 Function *F = getModule().getNamedFunction(FnName);
95 if (F == 0) {
96 std::cerr << "Could not find function '" << FnName <<"' in module!\n";
97 return 1;
98 }
99
100 int(*PF)(int, char**) = (int(*)(int, char**))getPointerToFunction(F);
101 assert(PF != 0 && "Null pointer to function?");
102
103 // Build an argv vector...
104 char **Argv = (char**)CreateArgv(Args);
105
106 // Call the main function...
Chris Lattner22080f92003-05-14 13:53:40 +0000107 int Result = PF(Args.size(), Argv);
108
109 // Run any atexit handlers now!
110 runAtExitHandlers();
111 return Result;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000112}