blob: 14e0a5f10e72dd4ee29752e4503b55c35b818a62 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- 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 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/Target/SubtargetFeature.h"
19#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/TargetMachineRegistry.h"
21using namespace llvm;
22
Gordon Henriksen99e34ab2007-10-17 21:28:48 +000023static cl::opt<const TargetMachineRegistry::entry*, false,
24 TargetMachineRegistry::Parser>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025MArch("march", cl::desc("Architecture to generate assembly for:"));
26
27static cl::opt<std::string>
28MCPU("mcpu",
29 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
30 cl::value_desc("cpu-name"),
31 cl::init(""));
32
33static cl::list<std::string>
34MAttrs("mattr",
35 cl::CommaSeparated,
36 cl::desc("Target specific attributes (-mattr=help for details)"),
37 cl::value_desc("a1,+a2,-a3,..."));
38
39/// create - Create an return a new JIT compiler if there is one available
40/// for the current target. Otherwise, return null.
41///
42ExecutionEngine *JIT::create(ModuleProvider *MP, std::string *ErrorStr) {
Gordon Henriksen99e34ab2007-10-17 21:28:48 +000043 const TargetMachineRegistry::entry *TheArch = MArch;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 if (TheArch == 0) {
45 std::string Error;
46 TheArch = TargetMachineRegistry::getClosestTargetForJIT(Error);
47 if (TheArch == 0) {
48 if (ErrorStr)
49 *ErrorStr = Error;
50 return 0;
51 }
52 } else if (TheArch->JITMatchQualityFn() == 0) {
53 cerr << "WARNING: This target JIT is not designed for the host you are"
54 << " running. If bad things happen, please choose a different "
55 << "-march switch.\n";
56 }
57
58 // Package up features to be passed to target/subtarget
59 std::string FeaturesStr;
60 if (MCPU.size() || MAttrs.size()) {
61 SubtargetFeatures Features;
62 Features.setCPU(MCPU);
63 for (unsigned i = 0; i != MAttrs.size(); ++i)
64 Features.AddFeature(MAttrs[i]);
65 FeaturesStr = Features.getString();
66 }
67
68 // Allocate a target...
69 TargetMachine *Target = TheArch->CtorFn(*MP->getModule(), FeaturesStr);
70 assert(Target && "Could not allocate target machine!");
71
72 // If the target supports JIT code generation, return a new JIT now.
73 if (TargetJITInfo *TJ = Target->getJITInfo())
74 return new JIT(MP, *Target, *TJ);
75
76 if (ErrorStr)
77 *ErrorStr = "target does not support JIT code generation";
78 return 0;
79}