Goodbye macro hell, hello nice clean simple extensible code. This change
[oota-llvm.git] / lib / ExecutionEngine / JIT / TargetSelect.cpp
1 //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
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 contains the hideously gross code that is currently used to select
11 // a particular TargetMachine for the JIT to use.  This should obviously be
12 // improved in the future, probably by having the TargetMachines register
13 // themselves with the runtime, and then have them choose themselves if they
14 // match the current machine.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #include "JIT.h"
19 #include "llvm/Module.h"
20 #include "llvm/ModuleProvider.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Target/TargetMachineRegistry.h"
23 #include <iostream>
24 using namespace llvm;
25
26 static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
27 MArch("march", cl::desc("Architecture to generate assembly for:"));
28
29 /// create - Create an return a new JIT compiler if there is one available
30 /// for the current target.  Otherwise, return null.
31 ///
32 ExecutionEngine *JIT::create(ModuleProvider *MP, IntrinsicLowering *IL) {
33   if (MArch == 0) {
34     std::string Error;
35     MArch = TargetMachineRegistry::getClosestTargetForJIT(Error);
36     if (MArch == 0) return 0;
37   } else if (MArch->JITMatchQualityFn() == 0) {
38     std::cerr << "WARNING: This target JIT is not designed for the host you are"
39               << " running.  If bad things happen, please choose a different "
40               << "-march switch.\n";
41   }
42
43   // Allocate a target...
44   TargetMachine *Target = MArch->CtorFn(*MP->getModule(), IL);
45   assert(Target && "Could not allocate target machine!");
46
47   // If the target supports JIT code generation, return a new JIT now.
48   if (TargetJITInfo *TJ = Target->getJITInfo())
49     return new JIT(MP, *Target, *TJ);
50   return 0;
51 }