LLVM API Documentation
00001 //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This just asks the TargetMachineRegistry for the appropriate JIT to use, and 00011 // allows the user to specify a specific one on the commandline with -march=x. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "JIT.h" 00016 #include "llvm/Module.h" 00017 #include "llvm/ModuleProvider.h" 00018 #include "llvm/Support/RegistryParser.h" 00019 #include "llvm/Support/Streams.h" 00020 #include "llvm/Target/SubtargetFeature.h" 00021 #include "llvm/Target/TargetMachine.h" 00022 #include "llvm/Target/TargetMachineRegistry.h" 00023 using namespace llvm; 00024 00025 static cl::opt<const TargetMachineRegistry::entry*, false, 00026 RegistryParser<TargetMachine> > 00027 MArch("march", cl::desc("Architecture to generate assembly for:")); 00028 00029 static cl::opt<std::string> 00030 MCPU("mcpu", 00031 cl::desc("Target a specific cpu type (-mcpu=help for details)"), 00032 cl::value_desc("cpu-name"), 00033 cl::init("")); 00034 00035 static cl::list<std::string> 00036 MAttrs("mattr", 00037 cl::CommaSeparated, 00038 cl::desc("Target specific attributes (-mattr=help for details)"), 00039 cl::value_desc("a1,+a2,-a3,...")); 00040 00041 /// createInternal - Create an return a new JIT compiler if there is one 00042 /// available for the current target. Otherwise, return null. 00043 /// 00044 ExecutionEngine *JIT::createJIT(ModuleProvider *MP, std::string *ErrorStr, 00045 JITMemoryManager *JMM, bool Fast) { 00046 const TargetMachineRegistry::entry *TheArch = MArch; 00047 if (TheArch == 0) { 00048 std::string Error; 00049 TheArch = TargetMachineRegistry::getClosestTargetForJIT(Error); 00050 if (TheArch == 0) { 00051 if (ErrorStr) 00052 *ErrorStr = Error; 00053 return 0; 00054 } 00055 } else if (TheArch->JITMatchQualityFn() == 0) { 00056 cerr << "WARNING: This target JIT is not designed for the host you are" 00057 << " running. If bad things happen, please choose a different " 00058 << "-march switch.\n"; 00059 } 00060 00061 // Package up features to be passed to target/subtarget 00062 std::string FeaturesStr; 00063 if (!MCPU.empty() || !MAttrs.empty()) { 00064 SubtargetFeatures Features; 00065 Features.setCPU(MCPU); 00066 for (unsigned i = 0; i != MAttrs.size(); ++i) 00067 Features.AddFeature(MAttrs[i]); 00068 FeaturesStr = Features.getString(); 00069 } 00070 00071 // Allocate a target... 00072 TargetMachine *Target = TheArch->CtorFn(*MP->getModule(), FeaturesStr); 00073 assert(Target && "Could not allocate target machine!"); 00074 00075 // If the target supports JIT code generation, return a new JIT now. 00076 if (TargetJITInfo *TJ = Target->getJITInfo()) 00077 return new JIT(MP, *Target, *TJ, JMM, Fast); 00078 00079 if (ErrorStr) 00080 *ErrorStr = "target does not support JIT code generation"; 00081 return 0; 00082 }