blob: 631ac99bd6cdd6e6fcfac9ce7237baf3fac903c4 [file] [log] [blame]
Chris Lattner67e08422003-06-20 15:49:04 +00001//===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
Chris Lattner2cf137b2001-09-07 22:20:50 +00002//
Chris Lattner67e08422003-06-20 15:49:04 +00003// This is the llc code generator.
Chris Lattner2cf137b2001-09-07 22:20:50 +00004//
Chris Lattnerb27d4742001-10-04 01:40:53 +00005//===----------------------------------------------------------------------===//
Vikram S. Adve2d94a342001-07-21 12:42:29 +00006
Vikram S. Adve2d94a342001-07-21 12:42:29 +00007#include "llvm/Bytecode/Reader.h"
Chris Lattner23930c52002-10-29 20:45:04 +00008#include "llvm/Target/TargetMachineImpls.h"
Vikram S. Adve9d409352001-09-18 13:10:45 +00009#include "llvm/Target/TargetMachine.h"
Chris Lattner89a20ef2002-05-07 20:03:27 +000010#include "llvm/Transforms/Scalar.h"
Chris Lattnered226062001-09-07 21:26:31 +000011#include "llvm/Module.h"
Chris Lattner7139f282002-01-31 00:46:45 +000012#include "llvm/PassManager.h"
Vikram S. Adveeb818692002-09-16 16:35:34 +000013#include "llvm/Pass.h"
Chris Lattner5de22042001-11-27 00:03:19 +000014#include "Support/CommandLine.h"
Chris Lattnerc065ad82002-04-18 19:55:25 +000015#include "Support/Signals.h"
Chris Lattner6c2c8702001-09-18 17:04:18 +000016#include <memory>
Chris Lattner46f1b612001-09-19 16:52:09 +000017#include <fstream>
Vikram S. Adveeb818692002-09-16 16:35:34 +000018
Vikram S. Adveeb818692002-09-16 16:35:34 +000019// 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.
22//
Chris Lattnerd64b2de2003-04-25 05:26:11 +000023static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000024InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
25
Chris Lattnerd64b2de2003-04-25 05:26:11 +000026static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000027OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
28
29static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
30
Brian Gaekecf8c4f52003-06-18 21:14:23 +000031enum ArchName { noarch, x86, Sparc };
Chris Lattner23f358b2003-04-28 03:28:56 +000032
Brian Gaekecf8c4f52003-06-18 21:14:23 +000033static cl::opt<ArchName>
34Arch("march", cl::desc("Architecture to generate assembly for:"), cl::Prefix,
35 cl::values(clEnumVal(x86, " IA-32 (Pentium and above)"),
Misha Brukmanf75934f2003-07-07 22:28:42 +000036 clEnumValN(Sparc, "sparc", " SPARC V9"),
Brian Gaekecf8c4f52003-06-18 21:14:23 +000037 0),
Misha Brukman4b628c52003-07-30 15:29:55 +000038 cl::init(noarch));
Chris Lattnerf5cad152002-07-22 02:10:13 +000039
Chris Lattner97fd6c42001-10-15 17:30:47 +000040// GetFileNameRoot - Helper function to get the basename of a filename...
Chris Lattnerd64b2de2003-04-25 05:26:11 +000041static inline std::string
42GetFileNameRoot(const std::string &InputFilename)
Vikram S. Adveeb818692002-09-16 16:35:34 +000043{
Chris Lattnerd64b2de2003-04-25 05:26:11 +000044 std::string IFN = InputFilename;
45 std::string outputFilename;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000046 int Len = IFN.length();
47 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerd64b2de2003-04-25 05:26:11 +000048 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f084b22001-10-14 23:29:28 +000049 } else {
Chris Lattner97fd6c42001-10-15 17:30:47 +000050 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000051 }
52 return outputFilename;
53}
54
Vikram S. Adve9d409352001-09-18 13:10:45 +000055
Chris Lattner67e08422003-06-20 15:49:04 +000056// main - Entry point for the llc compiler.
57//
58int main(int argc, char **argv) {
Vikram S. Adve2f084b22001-10-14 23:29:28 +000059 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
60
Vikram S. Adve2f084b22001-10-14 23:29:28 +000061 // Load the module to be compiled...
Chris Lattner7f74a562002-01-20 22:54:45 +000062 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
Chris Lattner67e08422003-06-20 15:49:04 +000063 if (M.get() == 0) {
64 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
65 return 1;
66 }
Brian Gaekecf8c4f52003-06-18 21:14:23 +000067 Module &mod = *M.get();
68
69 // Allocate target machine. First, check whether the user has
70 // explicitly specified an architecture to compile for.
Brian Gaekecf8c4f52003-06-18 21:14:23 +000071 TargetMachine* (*TargetMachineAllocator)(unsigned) = 0;
72 switch (Arch) {
73 case x86:
74 TargetMachineAllocator = allocateX86TargetMachine;
75 break;
76 case Sparc:
77 TargetMachineAllocator = allocateSparcTargetMachine;
78 break;
79 default:
80 // Decide what the default target machine should be, by looking at
81 // the module. This heuristic (ILP32, LE -> IA32; LP64, BE ->
82 // SPARCV9) is kind of gross, but it will work until we have more
83 // sophisticated target information to work from.
Chris Lattnerb1492402003-08-24 14:02:14 +000084 if (mod.getEndianness() == Module::LittleEndian &&
85 mod.getPointerSize() == Module::Pointer32) {
Brian Gaekecf8c4f52003-06-18 21:14:23 +000086 TargetMachineAllocator = allocateX86TargetMachine;
Chris Lattnerb1492402003-08-24 14:02:14 +000087 } else if (mod.getEndianness() == Module::BigEndian &&
88 mod.getPointerSize() == Module::Pointer64) {
Brian Gaekecf8c4f52003-06-18 21:14:23 +000089 TargetMachineAllocator = allocateSparcTargetMachine;
90 } else {
Chris Lattnerb1492402003-08-24 14:02:14 +000091 // If the module is target independent, favor a target which matches the
92 // current build system.
93#if defined(i386) || defined(__i386__) || defined(__x86__)
94 TargetMachineAllocator = allocateX86TargetMachine;
95#elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
96 TargetMachineAllocator = allocateSparcTargetMachine;
97#else
98 std::cerr << argv[0] << ": module does not specify a target to use. "
99 << "You must use the -march option.\n";
100 return 1;
101#endif
Brian Gaekecf8c4f52003-06-18 21:14:23 +0000102 }
103 break;
104 }
Chris Lattnerb1492402003-08-24 14:02:14 +0000105 std::auto_ptr<TargetMachine> target((*TargetMachineAllocator)(0));
Brian Gaekecf8c4f52003-06-18 21:14:23 +0000106 assert(target.get() && "Could not allocate target machine!");
107 TargetMachine &Target = *target.get();
108 const TargetData &TD = Target.getTargetData();
Chris Lattner4955d3e2002-05-20 21:20:08 +0000109
Chris Lattner97fd6c42001-10-15 17:30:47 +0000110 // Build up all of the passes that we want to do to the module...
Chris Lattner0686e432002-01-21 07:31:50 +0000111 PassManager Passes;
Chris Lattner97fd6c42001-10-15 17:30:47 +0000112
Chris Lattner4f6cdbd2003-04-26 20:11:09 +0000113 Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(),
Chris Lattner8d6d4252003-04-25 06:06:13 +0000114 TD.getPointerAlignment(), TD.getDoubleAlignment()));
Chris Lattner21a72182003-04-25 05:22:29 +0000115
Chris Lattnerf7286372002-02-03 23:43:19 +0000116 // Figure out where we are going to send the output...
117 std::ostream *Out = 0;
Chris Lattner422de252003-06-18 18:46:08 +0000118 if (OutputFilename != "") {
Brian Gaekec39d16a2003-06-18 21:43:33 +0000119 if (OutputFilename != "-") {
120 // Specified an output filename?
121 if (!Force && std::ifstream(OutputFilename.c_str())) {
122 // If force is not specified, make sure not to overwrite a file!
123 std::cerr << argv[0] << ": error opening '" << OutputFilename
124 << "': file exists!\n"
125 << "Use -f command line argument to force output\n";
126 return 1;
127 }
128 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner422de252003-06-18 18:46:08 +0000129
Brian Gaekec39d16a2003-06-18 21:43:33 +0000130 // Make sure that the Out file gets unlink'd from the disk if we get a
131 // SIGINT
132 RemoveFileOnSignal(OutputFilename);
133 } else {
134 Out = &std::cout;
135 }
Chris Lattner422de252003-06-18 18:46:08 +0000136 } else {
137 if (InputFilename == "-") {
138 OutputFilename = "-";
139 Out = &std::cout;
140 } else {
Brian Gaekecf8c4f52003-06-18 21:14:23 +0000141 OutputFilename = GetFileNameRoot(InputFilename);
Chris Lattner422de252003-06-18 18:46:08 +0000142 OutputFilename += ".s";
143
Chris Lattner0e11e542002-01-22 21:07:24 +0000144 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000145 // If force is not specified, make sure not to overwrite a file!
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000146 std::cerr << argv[0] << ": error opening '" << OutputFilename
147 << "': file exists!\n"
148 << "Use -f command line argument to force output\n";
Chris Lattner7f74a562002-01-20 22:54:45 +0000149 return 1;
150 }
Chris Lattner422de252003-06-18 18:46:08 +0000151
Chris Lattner7f74a562002-01-20 22:54:45 +0000152 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner422de252003-06-18 18:46:08 +0000153 if (!Out->good()) {
154 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
155 delete Out;
156 return 1;
157 }
158
Chris Lattner51eb6112002-09-19 16:06:28 +0000159 // Make sure that the Out file gets unlink'd from the disk if we get a
160 // SIGINT
Chris Lattnerc065ad82002-04-18 19:55:25 +0000161 RemoveFileOnSignal(OutputFilename);
Chris Lattner97fd6c42001-10-15 17:30:47 +0000162 }
Chris Lattner422de252003-06-18 18:46:08 +0000163 }
Vikram S. Adveeb818692002-09-16 16:35:34 +0000164
Brian Gaekecf8c4f52003-06-18 21:14:23 +0000165 // Ask the target to add backend passes as necessary
Chris Lattner5667f0e2002-10-29 21:12:46 +0000166 if (Target.addPassesToEmitAssembly(Passes, *Out)) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000167 std::cerr << argv[0] << ": target '" << Target.getName()
Brian Gaekecf8c4f52003-06-18 21:14:23 +0000168 << "' does not support static compilation!\n";
169 if (Out != &std::cout) delete Out;
170 // And the Out file is empty and useless, so remove it now.
171 std::remove(OutputFilename.c_str());
172 return 1;
Chris Lattner5667f0e2002-10-29 21:12:46 +0000173 } else {
174 // Run our queue of passes all at once now, efficiently.
175 Passes.run(*M.get());
176 }
Chris Lattner2fa0dab2001-10-18 01:31:22 +0000177
Chris Lattner51eb6112002-09-19 16:06:28 +0000178 // Delete the ostream if it's not a stdout stream
Chris Lattnerf7286372002-02-03 23:43:19 +0000179 if (Out != &std::cout) delete Out;
180
Chris Lattner1a9680f2001-10-18 20:33:21 +0000181 return 0;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000182}