1 //===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
3 // This is the llc code generator.
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/Module.h"
12 #include "llvm/PassManager.h"
13 #include "llvm/Pass.h"
14 #include "Support/CommandLine.h"
15 #include "Support/Signals.h"
19 // General options for llc. Other pass-specific options are specified
20 // within the corresponding llc passes, and target-specific options
21 // and back-end code generation options are specified with the target machine.
23 static cl::opt<std::string>
24 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
26 static cl::opt<std::string>
27 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
29 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
31 enum ArchName { noarch, x86, Sparc };
33 static cl::opt<ArchName>
34 Arch("march", cl::desc("Architecture to generate assembly for:"), cl::Prefix,
35 cl::values(clEnumVal(x86, " IA-32 (Pentium and above)"),
36 clEnumValN(Sparc, "sparc", " SPARC V9"),
40 // GetFileNameRoot - Helper function to get the basename of a filename...
41 static inline std::string
42 GetFileNameRoot(const std::string &InputFilename)
44 std::string IFN = InputFilename;
45 std::string outputFilename;
46 int Len = IFN.length();
47 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
48 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
52 return outputFilename;
56 // main - Entry point for the llc compiler.
58 int main(int argc, char **argv) {
59 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
61 // Load the module to be compiled...
62 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
64 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
67 Module &mod = *M.get();
69 // Allocate target machine. First, check whether the user has
70 // explicitly specified an architecture to compile for.
71 unsigned Config = (mod.isLittleEndian() ? TM::LittleEndian : TM::BigEndian)|
72 (mod.has32BitPointers() ? TM::PtrSize32 : TM::PtrSize64);
73 TargetMachine* (*TargetMachineAllocator)(unsigned) = 0;
76 TargetMachineAllocator = allocateX86TargetMachine;
79 TargetMachineAllocator = allocateSparcTargetMachine;
82 // Decide what the default target machine should be, by looking at
83 // the module. This heuristic (ILP32, LE -> IA32; LP64, BE ->
84 // SPARCV9) is kind of gross, but it will work until we have more
85 // sophisticated target information to work from.
86 if (mod.isLittleEndian() && mod.has32BitPointers()) {
87 TargetMachineAllocator = allocateX86TargetMachine;
88 } else if (mod.isBigEndian() && mod.has64BitPointers()) {
89 TargetMachineAllocator = allocateSparcTargetMachine;
91 assert(0 && "You must specify -march; I could not guess the default");
95 std::auto_ptr<TargetMachine> target((*TargetMachineAllocator)(Config));
96 assert(target.get() && "Could not allocate target machine!");
97 TargetMachine &Target = *target.get();
98 const TargetData &TD = Target.getTargetData();
100 // Build up all of the passes that we want to do to the module...
103 Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(),
104 TD.getPointerAlignment(), TD.getDoubleAlignment()));
106 // Figure out where we are going to send the output...
107 std::ostream *Out = 0;
108 if (OutputFilename != "") {
109 if (OutputFilename != "-") {
110 // Specified an output filename?
111 if (!Force && std::ifstream(OutputFilename.c_str())) {
112 // If force is not specified, make sure not to overwrite a file!
113 std::cerr << argv[0] << ": error opening '" << OutputFilename
114 << "': file exists!\n"
115 << "Use -f command line argument to force output\n";
118 Out = new std::ofstream(OutputFilename.c_str());
120 // Make sure that the Out file gets unlink'd from the disk if we get a
122 RemoveFileOnSignal(OutputFilename);
127 if (InputFilename == "-") {
128 OutputFilename = "-";
131 OutputFilename = GetFileNameRoot(InputFilename);
132 OutputFilename += ".s";
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 unlink'd from the disk if we get a
151 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;