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/System/Signals.h"
32 // General options for llc. Other pass-specific options are specified
33 // within the corresponding llc passes, and target-specific options
34 // and back-end code generation options are specified with the target machine.
36 static cl::opt<std::string>
37 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
39 static cl::opt<std::string>
40 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
42 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
44 static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
45 MArch("march", cl::desc("Architecture to generate assembly for:"));
47 // GetFileNameRoot - Helper function to get the basename of a filename...
48 static inline std::string
49 GetFileNameRoot(const std::string &InputFilename) {
50 std::string IFN = InputFilename;
51 std::string outputFilename;
52 int Len = IFN.length();
54 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
55 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
59 return outputFilename;
63 // main - Entry point for the llc compiler.
65 int main(int argc, char **argv) {
66 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
67 sys::PrintStackTraceOnErrorSignal();
69 // Load the module to be compiled...
70 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
72 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
75 Module &mod = *M.get();
77 // Allocate target machine. First, check whether the user has
78 // explicitly specified an architecture to compile for.
79 TargetMachine* (*TargetMachineAllocator)(const Module&,
80 IntrinsicLowering *) = 0;
83 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err);
85 std::cerr << argv[0] << ": error auto-selecting target for module '"
86 << Err << "'. Please use the -march option to explicitly "
87 << "pick a target.\n";
92 std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, 0));
93 assert(target.get() && "Could not allocate target machine!");
94 TargetMachine &Target = *target.get();
95 const TargetData &TD = Target.getTargetData();
97 // Build up all of the passes that we want to do to the module...
99 Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(),
100 TD.getPointerAlignment(), TD.getDoubleAlignment()));
102 // Figure out where we are going to send the output...
103 std::ostream *Out = 0;
104 if (OutputFilename != "") {
105 if (OutputFilename != "-") {
106 // Specified an output filename?
107 if (!Force && std::ifstream(OutputFilename.c_str())) {
108 // If force is not specified, make sure not to overwrite a file!
109 std::cerr << argv[0] << ": error opening '" << OutputFilename
110 << "': file exists!\n"
111 << "Use -f command line argument to force output\n";
114 Out = new std::ofstream(OutputFilename.c_str());
116 // Make sure that the Out file gets unlinked from the disk if we get a
118 sys::RemoveFileOnSignal(OutputFilename);
123 if (InputFilename == "-") {
124 OutputFilename = "-";
127 OutputFilename = GetFileNameRoot(InputFilename);
129 if (MArch->Name[0] != 'c' || MArch->Name[1] != 0) // not CBE
130 OutputFilename += ".s";
132 OutputFilename += ".cbe.c";
134 if (!Force && std::ifstream(OutputFilename.c_str())) {
135 // If force is not specified, make sure not to overwrite a file!
136 std::cerr << argv[0] << ": error opening '" << OutputFilename
137 << "': file exists!\n"
138 << "Use -f command line argument to force output\n";
142 Out = new std::ofstream(OutputFilename.c_str());
144 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
149 // Make sure that the Out file gets unlinked from the disk if we get a
151 sys::RemoveFileOnSignal(OutputFilename);
155 // Ask the target to add backend passes as necessary
156 if (Target.addPassesToEmitAssembly(Passes, *Out)) {
157 std::cerr << argv[0] << ": target '" << Target.getName()
158 << "' does not support static compilation!\n";
159 if (Out != &std::cout) delete Out;
160 // And the Out file is empty and useless, so remove it now.
161 std::remove(OutputFilename.c_str());
164 // Run our queue of passes all at once now, efficiently.
165 Passes.run(*M.get());
168 // Delete the ostream if it's not a stdout stream
169 if (Out != &std::cout) delete Out;