1 //===-- llc.cpp - Implement the LLVM Compiler -----------------------------===//
3 // This is the llc compiler driver.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Bytecode/Reader.h"
8 #include "llvm/Target/TargetMachineImpls.h"
9 #include "llvm/Target/TargetMachine.h"
10 #include "llvm/Transforms/Scalar.h"
11 #include "llvm/Assembly/PrintModulePass.h"
12 #include "llvm/Module.h"
13 #include "llvm/PassManager.h"
14 #include "llvm/Pass.h"
15 #include "llvm/Support/PassNameParser.h"
16 #include "Support/CommandLine.h"
17 #include "Support/Signals.h"
21 //------------------------------------------------------------------------------
22 // Option declarations for LLC.
23 //------------------------------------------------------------------------------
25 // Make all registered optimization passes available to llc. These passes
26 // will all be run before the simplification and lowering steps used by the
27 // back-end code generator, and will be run in the order specified on the
28 // command line. The OptimizationList is automatically populated with
29 // registered Passes by the PassNameParser.
31 static cl::list<const PassInfo*, bool,
32 FilteredPassNameParser<PassInfo::Optimization> >
33 OptimizationList(cl::desc("Optimizations available:"));
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"));
49 DisableStrip("disable-strip",
50 cl::desc("Do not strip the LLVM bytecode included in the executable"));
53 DumpAsm("d", cl::desc("Print bytecode before native code generation"),
56 // GetFileNameRoot - Helper function to get the basename of a filename...
57 static inline std::string
58 GetFileNameRoot(const std::string &InputFilename)
60 std::string IFN = InputFilename;
61 std::string outputFilename;
62 int Len = IFN.length();
63 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
64 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
68 return outputFilename;
72 //===---------------------------------------------------------------------===//
75 // Entry point for the llc compiler.
76 //===---------------------------------------------------------------------===//
79 main(int argc, char **argv)
81 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
83 // Allocate a target... in the future this will be controllable on the
85 std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
86 assert(target.get() && "Could not allocate target machine!");
88 TargetMachine &Target = *target.get();
89 const TargetData &TD = Target.getTargetData();
91 // Load the module to be compiled...
92 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
95 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
99 // Build up all of the passes that we want to do to the module...
102 Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(),
103 TD.getPointerAlignment(), TD.getDoubleAlignment()));
105 // Create a new optimization pass for each one specified on the command line
106 // Deal specially with tracing passes, which must be run differently than opt.
108 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
109 const PassInfo *Opt = OptimizationList[i];
111 // handle other passes as normal optimization passes
112 if (Opt->getNormalCtor())
113 Passes.add(Opt->getNormalCtor()());
114 else if (Opt->getTargetCtor())
115 Passes.add(Opt->getTargetCtor()(Target));
117 std::cerr << argv[0] << ": cannot create pass: "
118 << Opt->getPassName() << "\n";
121 // Replace malloc and free instructions with library calls.
122 // Do this after tracing until lli implements these lib calls.
123 // For now, it will emulate malloc and free internally.
124 // FIXME: This is sparc specific!
125 Passes.add(createLowerAllocationsPass());
127 // If LLVM dumping after transformations is requested, add it to the pipeline
129 Passes.add(new PrintFunctionPass("Code after xformations: \n", &std::cerr));
131 // Strip all of the symbols from the bytecode so that it will be smaller...
133 Passes.add(createSymbolStrippingPass());
135 // Figure out where we are going to send the output...
136 std::ostream *Out = 0;
137 if (OutputFilename != "") {
138 // Specified an output filename?
139 if (!Force && std::ifstream(OutputFilename.c_str())) {
140 // If force is not specified, make sure not to overwrite a file!
141 std::cerr << argv[0] << ": error opening '" << OutputFilename
142 << "': file exists!\n"
143 << "Use -f command line argument to force output\n";
146 Out = new std::ofstream(OutputFilename.c_str());
148 // Make sure that the Out file gets unlink'd from the disk if we get a
150 RemoveFileOnSignal(OutputFilename);
152 if (InputFilename == "-") {
153 OutputFilename = "-";
156 std::string OutputFilename = GetFileNameRoot(InputFilename);
157 OutputFilename += ".s";
159 if (!Force && std::ifstream(OutputFilename.c_str())) {
160 // If force is not specified, make sure not to overwrite a file!
161 std::cerr << argv[0] << ": error opening '" << OutputFilename
162 << "': file exists!\n"
163 << "Use -f command line argument to force output\n";
167 Out = new std::ofstream(OutputFilename.c_str());
169 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
174 // Make sure that the Out file gets unlink'd from the disk if we get a
176 RemoveFileOnSignal(OutputFilename);
180 // Ask the target to add backend passes as neccesary
181 if (Target.addPassesToEmitAssembly(Passes, *Out)) {
182 std::cerr << argv[0] << ": target '" << Target.getName()
183 << " does not support static compilation!\n";
185 // Run our queue of passes all at once now, efficiently.
186 Passes.run(*M.get());
189 // Delete the ostream if it's not a stdout stream
190 if (Out != &std::cout) delete Out;