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