testcase for PR159
[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 #if !defined(ENABLE_X86_JIT) && !defined(ENABLE_SPARC_JIT)
24 #define NO_JITS_ENABLED
25 #endif
26
27 namespace llvm {
28
29 namespace {
30   enum ArchName { x86, Sparc };
31
32 #ifndef NO_JITS_ENABLED
33   cl::opt<ArchName>
34   Arch("march", cl::desc("Architecture to JIT to:"), cl::Prefix,
35        cl::values(
36 #ifdef ENABLE_X86_JIT
37                   clEnumVal(x86, "  IA-32 (Pentium and above)"),
38 #endif
39 #ifdef ENABLE_SPARC_JIT
40                   clEnumValN(Sparc, "sparc", "  Sparc-V9"),
41 #endif
42                   0),
43 #if defined(ENABLE_X86_JIT)
44   cl::init(x86)
45 #elif defined(ENABLE_SPARC_JIT)
46   cl::init(Sparc)
47 #endif
48        );
49 #endif /* NO_JITS_ENABLED */
50 }
51
52 /// create - Create an return a new JIT compiler if there is one available
53 /// for the current target.  Otherwise, return null.
54 ///
55 ExecutionEngine *VM::create(ModuleProvider *MP) {
56   TargetMachine* (*TargetMachineAllocator)(const Module &) = 0;
57
58   // Allow a command-line switch to override what *should* be the default target
59   // machine for this platform. This allows for debugging a Sparc JIT on X86 --
60   // our X86 machines are much faster at recompiling LLVM and linking LLI.
61 #ifndef NO_JITS_ENABLED
62
63   switch (Arch) {
64 #ifdef ENABLE_X86_JIT
65   case x86:
66     TargetMachineAllocator = allocateX86TargetMachine;
67     break;
68 #endif
69 #ifdef ENABLE_SPARC_JIT
70   case Sparc:
71     TargetMachineAllocator = allocateSparcTargetMachine;
72     break;
73 #endif
74   default:
75     assert(0 && "-march flag not supported on this host!");
76   }
77 #else
78   return 0;
79 #endif
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   emitGlobals();
100 }
101
102 /// run - Start execution with the specified function and arguments.
103 ///
104 GenericValue VM::run(Function *F, const std::vector<GenericValue> &ArgValues)
105 {
106   assert (F && "Function *F was null at entry to run()");
107
108   int (*PF)(int, char **, const char **) =
109     (int(*)(int, char **, const char **))getPointerToFunction(F);
110   assert(PF != 0 && "Pointer to fn's code was null after getPointerToFunction");
111
112   // Call the function.
113   int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]),
114                     (const char **) GVTOP (ArgValues[2]));
115
116   // Run any atexit handlers now!
117   runAtExitHandlers();
118
119   GenericValue rv;
120   rv.IntVal = ExitCode;
121   return rv;
122 }
123
124 } // End llvm namespace