First patch in the direction of splitting MachineCodeEmitter in two subclasses:
[oota-llvm.git] / lib / ExecutionEngine / JIT / TargetSelect.cpp
1 //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This just asks the TargetMachineRegistry for the appropriate JIT to use, and
11 // allows the user to specify a specific one on the commandline with -march=x.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "JIT.h"
16 #include "llvm/Module.h"
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/Support/RegistryParser.h"
19 #include "llvm/Support/Streams.h"
20 #include "llvm/Target/SubtargetFeature.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Target/TargetMachineRegistry.h"
23 using namespace llvm;
24
25 static cl::opt<const TargetMachineRegistry::entry*, false,
26                RegistryParser<TargetMachine> >
27 MArch("march", cl::desc("Architecture to generate assembly for:"));
28
29 static cl::opt<std::string>
30 MCPU("mcpu",
31   cl::desc("Target a specific cpu type (-mcpu=help for details)"),
32   cl::value_desc("cpu-name"),
33   cl::init(""));
34
35 static cl::list<std::string>
36 MAttrs("mattr",
37   cl::CommaSeparated,
38   cl::desc("Target specific attributes (-mattr=help for details)"),
39   cl::value_desc("a1,+a2,-a3,..."));
40
41 /// createInternal - Create an return a new JIT compiler if there is one
42 /// available for the current target.  Otherwise, return null.
43 ///
44 ExecutionEngine *JIT::createJIT(ModuleProvider *MP, std::string *ErrorStr,
45                                 JITMemoryManager *JMM,
46                                 CodeGenOpt::Level OptLevel) {
47   const TargetMachineRegistry::entry *TheArch = MArch;
48   if (TheArch == 0) {
49     std::string Error;
50     TheArch = TargetMachineRegistry::getClosestTargetForJIT(Error);
51     if (TheArch == 0) {
52       if (ErrorStr)
53         *ErrorStr = Error;
54       return 0;
55     }
56   } else if (TheArch->JITMatchQualityFn() == 0) {
57     cerr << "WARNING: This target JIT is not designed for the host you are"
58          << " running.  If bad things happen, please choose a different "
59          << "-march switch.\n";
60   }
61
62   // Package up features to be passed to target/subtarget
63   std::string FeaturesStr;
64   if (!MCPU.empty() || !MAttrs.empty()) {
65     SubtargetFeatures Features;
66     Features.setCPU(MCPU);
67     for (unsigned i = 0; i != MAttrs.size(); ++i)
68       Features.AddFeature(MAttrs[i]);
69     FeaturesStr = Features.getString();
70   }
71
72   // Allocate a target...
73   TargetMachine *Target = TheArch->CtorFn(*MP->getModule(), FeaturesStr);
74   assert(Target && "Could not allocate target machine!");
75
76   // If the target supports JIT code generation, return a new JIT now.
77   if (TargetJITInfo *TJ = Target->getJITInfo())
78     return new JIT(MP, *Target, *TJ, JMM, OptLevel);
79
80   if (ErrorStr)
81     *ErrorStr = "target does not support JIT code generation";
82   return 0;
83 }