7f4877b9cb264ace3e1e5d823e57cd55caca2a68
[oota-llvm.git] / lib / ExecutionEngine / JIT / JIT.cpp
1 //===-- 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"
12 #include "Support/CommandLine.h"
13
14 // FIXME: REMOVE THIS
15 #include "llvm/PassManager.h"
16
17 #if !defined(ENABLE_X86_JIT) && !defined(ENABLE_SPARC_JIT)
18 #define NO_JITS_ENABLED
19 #endif
20
21 namespace {
22   enum ArchName { x86, Sparc };
23
24 #ifndef NO_JITS_ENABLED
25   cl::opt<ArchName>
26   Arch("march", cl::desc("Architecture to JIT to:"), cl::Prefix,
27        cl::values(
28 #ifdef ENABLE_X86_JIT
29                   clEnumVal(x86, "  IA-32 (Pentium and above)"),
30 #endif
31 #ifdef ENABLE_SPARC_JIT
32                   clEnumValN(Sparc, "sparc", "  Sparc-V9"),
33 #endif
34                   0),
35 #if defined(ENABLE_X86_JIT)
36   cl::init(x86)
37 #elif defined(ENABLE_SPARC_JIT)
38   cl::init(Sparc)
39 #endif
40        );
41 #endif /* NO_JITS_ENABLED */
42 }
43
44 /// createJIT - Create an return a new JIT compiler if there is one available
45 /// for the current target.  Otherwise it returns null.
46 ///
47 ExecutionEngine *ExecutionEngine::createJIT(Module *M, unsigned Config) {
48   
49   TargetMachine* (*TargetMachineAllocator)(unsigned) = 0;
50
51   // Allow a command-line switch to override what *should* be the default target
52   // machine for this platform. This allows for debugging a Sparc JIT on X86 --
53   // our X86 machines are much faster at recompiling LLVM and linking LLI.
54 #ifdef NO_JITS_ENABLED
55   return 0;
56 #endif
57
58   switch (Arch) {
59 #ifdef ENABLE_X86_JIT
60   case x86:
61     TargetMachineAllocator = allocateX86TargetMachine;
62     break;
63 #endif
64 #ifdef ENABLE_SPARC_JIT
65   case Sparc:
66     TargetMachineAllocator = allocateSparcTargetMachine;
67     break;
68 #endif
69   default:
70     assert(0 && "-march flag not supported on this host!");
71   }
72
73   // Allocate a target...
74   TargetMachine *Target = (*TargetMachineAllocator)(Config);
75   assert(Target && "Could not allocate target machine!");
76   
77   // Create the virtual machine object...
78   return new VM(M, Target);
79 }
80
81 VM::VM(Module *M, TargetMachine *tm) : ExecutionEngine(M), TM(*tm) {
82   setTargetData(TM.getTargetData());
83
84   // Initialize MCE
85   MCE = createEmitter(*this);
86
87   setupPassManager();
88
89 #ifdef ENABLE_SPARC_JIT
90   // THIS GOES BEYOND UGLY HACKS
91   if (TM.getName() == "UltraSparc-Native") {
92     extern Pass *createPreSelectionPass(TargetMachine &TM);
93     PassManager PM;
94     // Specialize LLVM code for this target machine and then
95     // run basic dataflow optimizations on LLVM code.
96     PM.add(createPreSelectionPass(TM));
97     PM.run(*M);
98   }
99 #endif
100
101   emitGlobals();
102 }
103
104 /// VM::run - This method begins the execution of a program beginning at the
105 /// specified function name.  The function is called with the specified
106 /// arguments and array of environment variables (a la main()).
107 ///
108 /// Inputs:
109 ///     FnName - The name of the function as a C++ string.
110 ///     Args   - A vector of C++ strings containing the arguments.
111 ///     envp   - An array of C strings containing the environment.
112 ///
113 /// Return value:
114 ///     1 - An error occurred.
115 ///     Otherwise, the return value from the specified function is returned.
116 ///
117 int VM::run(const std::string &FnName, const std::vector<std::string> &Args,
118             const char **envp) {
119   Function *F = getModule().getNamedFunction(FnName);
120   if (F == 0) {
121     std::cerr << "Could not find function '" << FnName << "' in module!\n";
122     return 1;
123   }
124
125   int (*PF)(int, char**, const char**) =
126     (int(*)(int, char**, const char**))getPointerToFunction(F);
127   assert(PF != 0 && "Null pointer to function?");
128
129   // Build an argv vector...
130   char **Argv = (char**)CreateArgv(Args);
131
132   // Call the main function...
133   int Result = PF(Args.size(), Argv, envp);
134
135   // Run any atexit handlers now!
136   runAtExitHandlers();
137   return Result;
138 }