1 //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
3 // This file implements the top-level support for creating a Just-In-Time
4 // compiler for the current architecture.
6 //===----------------------------------------------------------------------===//
9 #include "llvm/Target/TargetMachine.h"
10 #include "llvm/Target/TargetMachineImpls.h"
11 #include "llvm/Module.h"
12 #include "Support/CommandLine.h"
15 #include "llvm/PassManager.h"
19 Arch("march", cl::desc("Architecture: `x86' or `sparc'"), cl::Prefix,
20 cl::value_desc("machine architecture"));
22 static std::string DefaultArch =
23 #if defined(i386) || defined(__i386__) || defined(__x86__)
25 #elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
32 /// createJIT - Create an return a new JIT compiler if there is one available
33 /// for the current target. Otherwise it returns null.
35 ExecutionEngine *ExecutionEngine::createJIT(Module *M, unsigned Config) {
37 TargetMachine* (*TargetMachineAllocator)(unsigned) = 0;
41 // Allow a command-line switch to override what *should* be the default target
42 // machine for this platform. This allows for debugging a Sparc JIT on X86 --
43 // our X86 machines are much faster at recompiling LLVM and linking lli.
45 TargetMachineAllocator = allocateX86TargetMachine;
46 #if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
47 } else if (Arch == "sparc") {
48 TargetMachineAllocator = allocateSparcTargetMachine;
52 if (TargetMachineAllocator) {
53 // Allocate a target...
54 TargetMachine *Target = (*TargetMachineAllocator)(Config);
55 assert(Target && "Could not allocate target machine!");
57 // Create the virtual machine object...
58 return new VM(M, Target);
64 VM::VM(Module *M, TargetMachine *tm) : ExecutionEngine(M), TM(*tm) {
65 setTargetData(TM.getTargetData());
68 MCE = createEmitter(*this);
72 #if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
73 // THIS GOES BEYOND UGLY HACKS
74 if (TM.getName() == "UltraSparc-Native") {
75 extern Pass *createPreSelectionPass(TargetMachine &TM);
77 // Specialize LLVM code for this target machine and then
78 // run basic dataflow optimizations on LLVM code.
79 PM.add(createPreSelectionPass(TM));
87 int VM::run(const std::string &FnName, const std::vector<std::string> &Args) {
88 Function *F = getModule().getNamedFunction(FnName);
90 std::cerr << "Could not find function '" << FnName <<"' in module!\n";
94 int(*PF)(int, char**) = (int(*)(int, char**))getPointerToFunction(F);
95 assert(PF != 0 && "Null pointer to function?");
97 // Build an argv vector...
98 char **Argv = (char**)CreateArgv(Args);
100 // Call the main function...
101 int Result = PF(Args.size(), Argv);
103 // Run any atexit handlers now!