1 //===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This is the llc code generator driver. It provides a convenient
11 // command-line interface for generating native assembly-language code
12 // or C code, given LLVM bytecode.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Bytecode/Reader.h"
17 #include "llvm/Target/SubtargetFeature.h"
18 #include "llvm/Target/TargetMachine.h"
19 #include "llvm/Target/TargetMachineRegistry.h"
20 #include "llvm/Transforms/Scalar.h"
21 #include "llvm/Module.h"
22 #include "llvm/PassManager.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/PluginLoader.h"
26 #include "llvm/Support/FileUtilities.h"
27 #include "llvm/Analysis/Verifier.h"
28 #include "llvm/System/Signals.h"
29 #include "llvm/Config/config.h"
36 // General options for llc. Other pass-specific options are specified
37 // within the corresponding llc passes, and target-specific options
38 // and back-end code generation options are specified with the target machine.
40 static cl::opt<std::string>
41 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
43 static cl::opt<std::string>
44 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
46 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
48 static cl::opt<bool> Fast("fast",
49 cl::desc("Generate code quickly, potentially sacrificing code quality"));
51 static cl::opt<std::string>
52 TargetTriple("mtriple", cl::desc("Override target triple for module"));
54 static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
55 MArch("march", cl::desc("Architecture to generate code for:"));
57 static cl::opt<std::string>
59 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
60 cl::value_desc("cpu-name"),
63 static cl::list<std::string>
66 cl::desc("Target specific attributes (-mattr=help for details)"),
67 cl::value_desc("a1,+a2,-a3,..."));
69 cl::opt<TargetMachine::CodeGenFileType>
70 FileType("filetype", cl::init(TargetMachine::AssemblyFile),
71 cl::desc("Choose a file type (not all types are supported by all targets):"),
73 clEnumValN(TargetMachine::AssemblyFile, "asm",
74 " Emit an assembly ('.s') file"),
75 clEnumValN(TargetMachine::ObjectFile, "obj",
76 " Emit a native object ('.o') file [experimental]"),
77 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
78 " Emit a native dynamic library ('.so') file"),
81 cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
82 cl::desc("Do not verify input module"));
85 // GetFileNameRoot - Helper function to get the basename of a filename.
86 static inline std::string
87 GetFileNameRoot(const std::string &InputFilename) {
88 std::string IFN = InputFilename;
89 std::string outputFilename;
90 int Len = IFN.length();
92 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
93 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
97 return outputFilename;
101 // main - Entry point for the llc compiler.
103 int main(int argc, char **argv) {
105 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
106 sys::PrintStackTraceOnErrorSignal();
108 // Load the module to be compiled...
109 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
111 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
114 Module &mod = *M.get();
116 // If we are supposed to override the target triple, do so now.
117 if (!TargetTriple.empty())
118 mod.setTargetTriple(TargetTriple);
120 // Allocate target machine. First, check whether the user has
121 // explicitly specified an architecture to compile for.
124 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err);
126 std::cerr << argv[0] << ": error auto-selecting target for module '"
127 << Err << "'. Please use the -march option to explicitly "
128 << "pick a target.\n";
133 // Package up features to be passed to target/subtarget
134 std::string FeaturesStr;
135 if (MCPU.size() || MAttrs.size()) {
136 SubtargetFeatures Features;
137 Features.setCPU(MCPU);
138 for (unsigned i = 0; i != MAttrs.size(); ++i)
139 Features.AddFeature(MAttrs[i]);
140 FeaturesStr = Features.getString();
143 std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, FeaturesStr));
144 assert(target.get() && "Could not allocate target machine!");
145 TargetMachine &Target = *target.get();
146 const TargetData &TD = Target.getTargetData();
148 // Build up all of the passes that we want to do to the module...
150 Passes.add(new TargetData(TD));
154 Passes.add(createVerifierPass());
157 // Figure out where we are going to send the output...
158 std::ostream *Out = 0;
159 if (OutputFilename != "") {
160 if (OutputFilename != "-") {
161 // Specified an output filename?
162 if (!Force && std::ifstream(OutputFilename.c_str())) {
163 // If force is not specified, make sure not to overwrite a file!
164 std::cerr << argv[0] << ": error opening '" << OutputFilename
165 << "': file exists!\n"
166 << "Use -f command line argument to force output\n";
169 Out = new std::ofstream(OutputFilename.c_str());
171 // Make sure that the Out file gets unlinked from the disk if we get a
173 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
178 if (InputFilename == "-") {
179 OutputFilename = "-";
182 OutputFilename = GetFileNameRoot(InputFilename);
185 case TargetMachine::AssemblyFile:
186 if (MArch->Name[0] != 'c' || MArch->Name[1] != 0) // not CBE
187 OutputFilename += ".s";
189 OutputFilename += ".cbe.c";
191 case TargetMachine::ObjectFile:
192 OutputFilename += ".o";
194 case TargetMachine::DynamicLibrary:
195 OutputFilename += LTDL_SHLIB_EXT;
199 if (!Force && std::ifstream(OutputFilename.c_str())) {
200 // If force is not specified, make sure not to overwrite a file!
201 std::cerr << argv[0] << ": error opening '" << OutputFilename
202 << "': file exists!\n"
203 << "Use -f command line argument to force output\n";
207 Out = new std::ofstream(OutputFilename.c_str());
209 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
214 // Make sure that the Out file gets unlinked from the disk if we get a
216 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
220 // Ask the target to add backend passes as necessary.
221 if (Target.addPassesToEmitFile(Passes, *Out, FileType, Fast)) {
222 std::cerr << argv[0] << ": target '" << Target.getName()
223 << "' does not support generation of this file type!\n";
224 if (Out != &std::cout) delete Out;
225 // And the Out file is empty and useless, so remove it now.
226 sys::Path(OutputFilename).eraseFromDisk();
229 // Run our queue of passes all at once now, efficiently.
230 Passes.run(*M.get());
233 // Delete the ostream if it's not a stdout stream
234 if (Out != &std::cout) delete Out;
237 } catch (const std::string& msg) {
238 std::cerr << argv[0] << ": " << msg << "\n";
240 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";