blob: bf968af4796b100e2cfdb57b55e76c8a9de747b7 [file] [log] [blame]
Chris Lattner4d326fa2003-12-20 01:46:27 +00001//===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Chris Lattner4d326fa2003-12-20 01:46:27 +00003// 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.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Chris Lattner4d326fa2003-12-20 01:46:27 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerbeff74f2004-07-11 08:24:02 +000010// 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.
Chris Lattner4d326fa2003-12-20 01:46:27 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "JIT.h"
16#include "llvm/Module.h"
17#include "llvm/ModuleProvider.h"
Jim Laskeyb1e11802005-09-01 21:38:21 +000018#include "llvm/Target/SubtargetFeature.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000019#include "llvm/Target/TargetMachine.h"
Chris Lattnerd5e1d9d2004-07-11 04:02:06 +000020#include "llvm/Target/TargetMachineRegistry.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000021using namespace llvm;
22
Chris Lattnerd5e1d9d2004-07-11 04:02:06 +000023static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
24MArch("march", cl::desc("Architecture to generate assembly for:"));
Chris Lattner4d326fa2003-12-20 01:46:27 +000025
Jim Laskeyb1e11802005-09-01 21:38:21 +000026static cl::opt<std::string>
27MCPU("mcpu",
Chris Lattner667eeca2005-10-23 22:39:01 +000028 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskeyb1e11802005-09-01 21:38:21 +000029 cl::value_desc("cpu-name"),
30 cl::init(""));
31
32static cl::list<std::string>
33MAttrs("mattr",
34 cl::CommaSeparated,
Chris Lattner667eeca2005-10-23 22:39:01 +000035 cl::desc("Target specific attributes (-mattr=help for details)"),
36 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskeyb1e11802005-09-01 21:38:21 +000037
Chris Lattner4d326fa2003-12-20 01:46:27 +000038/// create - Create an return a new JIT compiler if there is one available
39/// for the current target. Otherwise, return null.
40///
Reid Spencerd4c0e622007-03-03 18:19:18 +000041ExecutionEngine *JIT::create(ModuleProvider *MP, std::string *ErrorStr) {
Chris Lattner108ec4b2007-04-20 22:57:20 +000042 const TargetMachineRegistry::Entry *TheArch = MArch;
43 if (TheArch == 0) {
Chris Lattnerd5e1d9d2004-07-11 04:02:06 +000044 std::string Error;
Chris Lattner108ec4b2007-04-20 22:57:20 +000045 TheArch = TargetMachineRegistry::getClosestTargetForJIT(Error);
46 if (TheArch == 0) {
Reid Spencerd4c0e622007-03-03 18:19:18 +000047 if (ErrorStr)
48 *ErrorStr = Error;
49 return 0;
50 }
Chris Lattner108ec4b2007-04-20 22:57:20 +000051 } else if (TheArch->JITMatchQualityFn() == 0) {
Bill Wendling832171c2006-12-07 20:04:42 +000052 cerr << "WARNING: This target JIT is not designed for the host you are"
53 << " running. If bad things happen, please choose a different "
54 << "-march switch.\n";
Chris Lattner4d326fa2003-12-20 01:46:27 +000055 }
Chris Lattner4d326fa2003-12-20 01:46:27 +000056
Jim Laskeyb1e11802005-09-01 21:38:21 +000057 // Package up features to be passed to target/subtarget
58 std::string FeaturesStr;
59 if (MCPU.size() || MAttrs.size()) {
60 SubtargetFeatures Features;
61 Features.setCPU(MCPU);
62 for (unsigned i = 0; i != MAttrs.size(); ++i)
63 Features.AddFeature(MAttrs[i]);
64 FeaturesStr = Features.getString();
65 }
66
Chris Lattner4d326fa2003-12-20 01:46:27 +000067 // Allocate a target...
Chris Lattner108ec4b2007-04-20 22:57:20 +000068 TargetMachine *Target = TheArch->CtorFn(*MP->getModule(), FeaturesStr);
Chris Lattner4d326fa2003-12-20 01:46:27 +000069 assert(Target && "Could not allocate target machine!");
70
71 // If the target supports JIT code generation, return a new JIT now.
72 if (TargetJITInfo *TJ = Target->getJITInfo())
73 return new JIT(MP, *Target, *TJ);
Reid Spencerd4c0e622007-03-03 18:19:18 +000074
75 if (ErrorStr)
76 *ErrorStr = "target does not support JIT code generation";
Chris Lattner4d326fa2003-12-20 01:46:27 +000077 return 0;
78}