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/TargetMachine.h"
18 #include "llvm/Target/TargetMachineRegistry.h"
19 #include "llvm/Transforms/Scalar.h"
20 #include "llvm/Module.h"
21 #include "llvm/PassManager.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/PluginLoader.h"
25 #include "llvm/Support/PassNameParser.h"
26 #include "llvm/Analysis/Verifier.h"
27 #include "llvm/System/Signals.h"
28 #include "llvm/Config/config.h"
35 // General options for llc. Other pass-specific options are specified
36 // within the corresponding llc passes, and target-specific options
37 // and back-end code generation options are specified with the target machine.
39 static cl::opt<std::string>
40 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
42 static cl::opt<std::string>
43 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
45 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
47 static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
48 MArch("march", cl::desc("Architecture to generate code for:"));
50 cl::opt<TargetMachine::CodeGenFileType>
51 FileType("filetype", cl::init(TargetMachine::AssemblyFile),
52 cl::desc("Choose a file type (not all types are supported by all targets):"),
54 clEnumValN(TargetMachine::AssemblyFile, "asm",
55 " Emit an assembly ('.s') file"),
56 clEnumValN(TargetMachine::ObjectFile, "obj",
57 " Emit a native object ('.o') file"),
58 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
59 " Emit a native dynamic library ('.so') file"),
62 // The LLCPassList is populated with passes that were registered using
63 // PassInfo::LLC by the FilteredPassNameParser:
64 cl::list<const PassInfo*, bool, FilteredPassNameParser<PassInfo::LLC> >
65 LLCPassList(cl::desc("Passes Available"));
67 cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
68 cl::desc("Do not verify input module"));
71 // GetFileNameRoot - Helper function to get the basename of a filename.
72 static inline std::string
73 GetFileNameRoot(const std::string &InputFilename) {
74 std::string IFN = InputFilename;
75 std::string outputFilename;
76 int Len = IFN.length();
78 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
79 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
83 return outputFilename;
87 // main - Entry point for the llc compiler.
89 int main(int argc, char **argv) {
91 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
92 sys::PrintStackTraceOnErrorSignal();
94 // Load the module to be compiled...
95 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
97 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
100 Module &mod = *M.get();
102 // Allocate target machine. First, check whether the user has
103 // explicitly specified an architecture to compile for.
104 TargetMachine* (*TargetMachineAllocator)(const Module&,
105 IntrinsicLowering *) = 0;
108 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err);
110 std::cerr << argv[0] << ": error auto-selecting target for module '"
111 << Err << "'. Please use the -march option to explicitly "
112 << "pick a target.\n";
117 std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, 0));
118 assert(target.get() && "Could not allocate target machine!");
119 TargetMachine &Target = *target.get();
120 const TargetData &TD = Target.getTargetData();
122 // Build up all of the passes that we want to do to the module...
124 Passes.add(new TargetData(TD));
126 // Create a new pass for each one specified on the command line
127 for (unsigned i = 0; i < LLCPassList.size(); ++i) {
128 const PassInfo *aPass = LLCPassList[i];
130 if (aPass->getNormalCtor()) {
131 Pass *P = aPass->getNormalCtor()();
134 std::cerr << argv[0] << ": cannot create pass: "
135 << aPass->getPassName() << "\n";
141 Passes.add(createVerifierPass());
144 // Figure out where we are going to send the output...
145 std::ostream *Out = 0;
146 if (OutputFilename != "") {
147 if (OutputFilename != "-") {
148 // Specified an output filename?
149 if (!Force && std::ifstream(OutputFilename.c_str())) {
150 // If force is not specified, make sure not to overwrite a file!
151 std::cerr << argv[0] << ": error opening '" << OutputFilename
152 << "': file exists!\n"
153 << "Use -f command line argument to force output\n";
156 Out = new std::ofstream(OutputFilename.c_str());
158 // Make sure that the Out file gets unlinked from the disk if we get a
160 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
165 if (InputFilename == "-") {
166 OutputFilename = "-";
169 OutputFilename = GetFileNameRoot(InputFilename);
172 case TargetMachine::AssemblyFile:
173 if (MArch->Name[0] != 'c' || MArch->Name[1] != 0) // not CBE
174 OutputFilename += ".s";
176 OutputFilename += ".cbe.c";
178 case TargetMachine::ObjectFile:
179 OutputFilename += ".o";
181 case TargetMachine::DynamicLibrary:
182 OutputFilename += LTDL_SHLIB_EXT;
186 if (!Force && std::ifstream(OutputFilename.c_str())) {
187 // If force is not specified, make sure not to overwrite a file!
188 std::cerr << argv[0] << ": error opening '" << OutputFilename
189 << "': file exists!\n"
190 << "Use -f command line argument to force output\n";
194 Out = new std::ofstream(OutputFilename.c_str());
196 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
201 // Make sure that the Out file gets unlinked from the disk if we get a
203 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
207 // Ask the target to add backend passes as necessary.
208 if (Target.addPassesToEmitFile(Passes, *Out, FileType)) {
209 std::cerr << argv[0] << ": target '" << Target.getName()
210 << "' does not support generation of this file type!\n";
211 if (Out != &std::cout) delete Out;
212 // And the Out file is empty and useless, so remove it now.
213 std::remove(OutputFilename.c_str());
216 // Run our queue of passes all at once now, efficiently.
217 Passes.run(*M.get());
220 // Delete the ostream if it's not a stdout stream
221 if (Out != &std::cout) delete Out;
224 } catch (const std::string& msg) {
225 std::cerr << argv[0] << ": " << msg << "\n";
227 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";