Added LLVM project notice to the top of every C++ source file.
[oota-llvm.git] / lib / ExecutionEngine / JIT / JIT.cpp
1 //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the top-level support for creating a Just-In-Time
11 // compiler for the current architecture.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "VM.h"
16 #include "llvm/Module.h"
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/ExecutionEngine/GenericValue.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "llvm/Target/TargetMachineImpls.h"
21 #include "Support/CommandLine.h"
22
23 // FIXME: REMOVE THIS
24 #include "llvm/PassManager.h"
25
26 #if !defined(ENABLE_X86_JIT) && !defined(ENABLE_SPARC_JIT)
27 #define NO_JITS_ENABLED
28 #endif
29
30 namespace {
31   enum ArchName { x86, Sparc };
32
33 #ifndef NO_JITS_ENABLED
34   cl::opt<ArchName>
35   Arch("march", cl::desc("Architecture to JIT to:"), cl::Prefix,
36        cl::values(
37 #ifdef ENABLE_X86_JIT
38                   clEnumVal(x86, "  IA-32 (Pentium and above)"),
39 #endif
40 #ifdef ENABLE_SPARC_JIT
41                   clEnumValN(Sparc, "sparc", "  Sparc-V9"),
42 #endif
43                   0),
44 #if defined(ENABLE_X86_JIT)
45   cl::init(x86)
46 #elif defined(ENABLE_SPARC_JIT)
47   cl::init(Sparc)
48 #endif
49        );
50 #endif /* NO_JITS_ENABLED */
51 }
52
53 /// create - Create an return a new JIT compiler if there is one available
54 /// for the current target.  Otherwise, return null.
55 ///
56 ExecutionEngine *VM::create(ModuleProvider *MP) {
57   TargetMachine* (*TargetMachineAllocator)(const Module &) = 0;
58
59   // Allow a command-line switch to override what *should* be the default target
60   // machine for this platform. This allows for debugging a Sparc JIT on X86 --
61   // our X86 machines are much faster at recompiling LLVM and linking LLI.
62 #ifdef NO_JITS_ENABLED
63   return 0;
64 #endif
65
66   switch (Arch) {
67 #ifdef ENABLE_X86_JIT
68   case x86:
69     TargetMachineAllocator = allocateX86TargetMachine;
70     break;
71 #endif
72 #ifdef ENABLE_SPARC_JIT
73   case Sparc:
74     TargetMachineAllocator = allocateSparcTargetMachine;
75     break;
76 #endif
77   default:
78     assert(0 && "-march flag not supported on this host!");
79   }
80
81   // Allocate a target...
82   TargetMachine *Target = TargetMachineAllocator(*MP->getModule());
83   assert(Target && "Could not allocate target machine!");
84   
85   // Create the virtual machine object...
86   return new VM(MP, Target);
87 }
88
89 VM::VM(ModuleProvider *MP, TargetMachine *tm) : ExecutionEngine(MP), TM(*tm),
90   PM(MP)
91 {
92   setTargetData(TM.getTargetData());
93
94   // Initialize MCE
95   MCE = createEmitter(*this);
96
97   setupPassManager();
98
99 #ifdef ENABLE_SPARC_JIT
100   // THIS GOES BEYOND UGLY HACKS
101   if (TM.getName() == "UltraSparc-Native") {
102     extern Pass *createPreSelectionPass(TargetMachine &TM);
103     PassManager PM;
104     // Specialize LLVM code for this target machine and then
105     // run basic dataflow optimizations on LLVM code.
106     PM.add(createPreSelectionPass(TM));
107     // We cannot utilize function-at-a-time loading here because PreSelection
108     // is a ModulePass.
109     MP->materializeModule();
110     PM.run(*MP->getModule());
111   }
112 #endif
113
114   emitGlobals();
115 }
116
117 /// run - Start execution with the specified function and arguments.
118 ///
119 GenericValue VM::run(Function *F, const std::vector<GenericValue> &ArgValues)
120 {
121   assert (F && "Function *F was null at entry to run()");
122
123   int (*PF)(int, char **, const char **) =
124     (int(*)(int, char **, const char **))getPointerToFunction(F);
125   assert(PF != 0 && "Pointer to fn's code was null after getPointerToFunction");
126
127   // Call the function.
128   int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]),
129                     (const char **) GVTOP (ArgValues[2]));
130
131   // Run any atexit handlers now!
132   runAtExitHandlers();
133
134   GenericValue rv;
135   rv.IntVal = ExitCode;
136   return rv;
137 }