blob: 7e800f11319abb50ab67d983d07c46e467041c75 [file] [log] [blame]
Chris Lattner67e08422003-06-20 15:49:04 +00001//===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
John Criswell09344dc2003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2cf137b2001-09-07 22:20:50 +00009//
Chris Lattner67e08422003-06-20 15:49:04 +000010// This is the llc code generator.
Chris Lattner2cf137b2001-09-07 22:20:50 +000011//
Chris Lattnerb27d4742001-10-04 01:40:53 +000012//===----------------------------------------------------------------------===//
Vikram S. Adve2d94a342001-07-21 12:42:29 +000013
Vikram S. Adve2d94a342001-07-21 12:42:29 +000014#include "llvm/Bytecode/Reader.h"
Chris Lattner23930c52002-10-29 20:45:04 +000015#include "llvm/Target/TargetMachineImpls.h"
Vikram S. Adve9d409352001-09-18 13:10:45 +000016#include "llvm/Target/TargetMachine.h"
Chris Lattner89a20ef2002-05-07 20:03:27 +000017#include "llvm/Transforms/Scalar.h"
Chris Lattnered226062001-09-07 21:26:31 +000018#include "llvm/Module.h"
Chris Lattner7139f282002-01-31 00:46:45 +000019#include "llvm/PassManager.h"
Vikram S. Adveeb818692002-09-16 16:35:34 +000020#include "llvm/Pass.h"
Chris Lattner5de22042001-11-27 00:03:19 +000021#include "Support/CommandLine.h"
Chris Lattnerc065ad82002-04-18 19:55:25 +000022#include "Support/Signals.h"
Chris Lattner6c2c8702001-09-18 17:04:18 +000023#include <memory>
Chris Lattner46f1b612001-09-19 16:52:09 +000024#include <fstream>
Vikram S. Adveeb818692002-09-16 16:35:34 +000025
Brian Gaeke960707c2003-11-11 22:41:34 +000026using namespace llvm;
27
Vikram S. Adveeb818692002-09-16 16:35:34 +000028// General options for llc. Other pass-specific options are specified
29// within the corresponding llc passes, and target-specific options
30// and back-end code generation options are specified with the target machine.
31//
Chris Lattnerd64b2de2003-04-25 05:26:11 +000032static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000033InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
34
Chris Lattnerd64b2de2003-04-25 05:26:11 +000035static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000036OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
37
38static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
39
Chris Lattner72170c42004-02-28 19:55:16 +000040enum ArchName { noarch, X86, SparcV8, SparcV9, PowerPC, CBackend };
Chris Lattner23f358b2003-04-28 03:28:56 +000041
Brian Gaekecf8c4f52003-06-18 21:14:23 +000042static cl::opt<ArchName>
43Arch("march", cl::desc("Architecture to generate assembly for:"), cl::Prefix,
Chris Lattner3fba9dc2004-02-13 23:19:09 +000044 cl::values(clEnumValN(X86, "x86", " IA-32 (Pentium and above)"),
Chris Lattner72170c42004-02-28 19:55:16 +000045 clEnumValN(SparcV8, "sparcv8", " SPARC V8 (experimental)"),
Brian Gaeke068b4592004-02-25 19:08:12 +000046 clEnumValN(SparcV9, "sparcv9", " SPARC V9"),
Chris Lattner72170c42004-02-28 19:55:16 +000047 clEnumValN(PowerPC, "powerpc", " PowerPC (experimental)"),
Chris Lattner3fba9dc2004-02-13 23:19:09 +000048 clEnumValN(CBackend, "c", " C backend"),
Brian Gaekecf8c4f52003-06-18 21:14:23 +000049 0),
Misha Brukman4b628c52003-07-30 15:29:55 +000050 cl::init(noarch));
Chris Lattnerf5cad152002-07-22 02:10:13 +000051
Chris Lattner97fd6c42001-10-15 17:30:47 +000052// GetFileNameRoot - Helper function to get the basename of a filename...
Chris Lattnerd64b2de2003-04-25 05:26:11 +000053static inline std::string
54GetFileNameRoot(const std::string &InputFilename)
Vikram S. Adveeb818692002-09-16 16:35:34 +000055{
Chris Lattnerd64b2de2003-04-25 05:26:11 +000056 std::string IFN = InputFilename;
57 std::string outputFilename;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000058 int Len = IFN.length();
John Criswella289abf2003-08-28 21:42:29 +000059 if ((Len > 2) &&
60 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerd64b2de2003-04-25 05:26:11 +000061 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f084b22001-10-14 23:29:28 +000062 } else {
Chris Lattner97fd6c42001-10-15 17:30:47 +000063 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000064 }
65 return outputFilename;
66}
67
Vikram S. Adve9d409352001-09-18 13:10:45 +000068
Chris Lattner67e08422003-06-20 15:49:04 +000069// main - Entry point for the llc compiler.
70//
71int main(int argc, char **argv) {
Vikram S. Adve2f084b22001-10-14 23:29:28 +000072 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
Chris Lattner69e896b2004-02-19 20:32:39 +000073 PrintStackTraceOnErrorSignal();
74
Vikram S. Adve2f084b22001-10-14 23:29:28 +000075 // Load the module to be compiled...
Chris Lattner7f74a562002-01-20 22:54:45 +000076 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
Chris Lattner67e08422003-06-20 15:49:04 +000077 if (M.get() == 0) {
78 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
79 return 1;
80 }
Brian Gaekecf8c4f52003-06-18 21:14:23 +000081 Module &mod = *M.get();
82
83 // Allocate target machine. First, check whether the user has
84 // explicitly specified an architecture to compile for.
Chris Lattnerdabec3b2003-12-28 09:51:04 +000085 TargetMachine* (*TargetMachineAllocator)(const Module&,
86 IntrinsicLowering *) = 0;
Brian Gaekecf8c4f52003-06-18 21:14:23 +000087 switch (Arch) {
Chris Lattner3fba9dc2004-02-13 23:19:09 +000088 case CBackend:
89 TargetMachineAllocator = allocateCTargetMachine;
90 break;
91 case X86:
Brian Gaekecf8c4f52003-06-18 21:14:23 +000092 TargetMachineAllocator = allocateX86TargetMachine;
93 break;
Brian Gaeke068b4592004-02-25 19:08:12 +000094 case SparcV9:
95 TargetMachineAllocator = allocateSparcV9TargetMachine;
Brian Gaekecf8c4f52003-06-18 21:14:23 +000096 break;
Chris Lattner72170c42004-02-28 19:55:16 +000097 case SparcV8:
98 TargetMachineAllocator = allocateSparcV8TargetMachine;
99 break;
Brian Gaeke57b8ef22004-02-02 19:06:12 +0000100 case PowerPC:
101 TargetMachineAllocator = allocatePowerPCTargetMachine;
102 break;
Brian Gaekecf8c4f52003-06-18 21:14:23 +0000103 default:
104 // Decide what the default target machine should be, by looking at
105 // the module. This heuristic (ILP32, LE -> IA32; LP64, BE ->
106 // SPARCV9) is kind of gross, but it will work until we have more
107 // sophisticated target information to work from.
Chris Lattnerb1492402003-08-24 14:02:14 +0000108 if (mod.getEndianness() == Module::LittleEndian &&
109 mod.getPointerSize() == Module::Pointer32) {
Brian Gaekecf8c4f52003-06-18 21:14:23 +0000110 TargetMachineAllocator = allocateX86TargetMachine;
Brian Gaeke57b8ef22004-02-02 19:06:12 +0000111 } else if (mod.getEndianness() == Module::BigEndian &&
112 mod.getPointerSize() == Module::Pointer32) {
113 TargetMachineAllocator = allocatePowerPCTargetMachine;
Chris Lattnerb1492402003-08-24 14:02:14 +0000114 } else if (mod.getEndianness() == Module::BigEndian &&
115 mod.getPointerSize() == Module::Pointer64) {
Brian Gaeke068b4592004-02-25 19:08:12 +0000116 TargetMachineAllocator = allocateSparcV9TargetMachine;
Brian Gaekecf8c4f52003-06-18 21:14:23 +0000117 } else {
Chris Lattnerb1492402003-08-24 14:02:14 +0000118 // If the module is target independent, favor a target which matches the
119 // current build system.
120#if defined(i386) || defined(__i386__) || defined(__x86__)
121 TargetMachineAllocator = allocateX86TargetMachine;
122#elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Brian Gaeke068b4592004-02-25 19:08:12 +0000123 TargetMachineAllocator = allocateSparcV9TargetMachine;
Brian Gaeke57b8ef22004-02-02 19:06:12 +0000124#elif defined(__POWERPC__) || defined(__ppc__) || defined(__APPLE__)
125 TargetMachineAllocator = allocatePowerPCTargetMachine;
Chris Lattnerb1492402003-08-24 14:02:14 +0000126#else
127 std::cerr << argv[0] << ": module does not specify a target to use. "
Chris Lattner3fba9dc2004-02-13 23:19:09 +0000128 << "You must use the -march option. If no native target is "
129 << "available, use -march=c to emit C code.\n";
Chris Lattnerb1492402003-08-24 14:02:14 +0000130 return 1;
131#endif
Brian Gaekecf8c4f52003-06-18 21:14:23 +0000132 }
133 break;
134 }
Chris Lattnerdabec3b2003-12-28 09:51:04 +0000135 std::auto_ptr<TargetMachine> target(TargetMachineAllocator(mod, 0));
Brian Gaekecf8c4f52003-06-18 21:14:23 +0000136 assert(target.get() && "Could not allocate target machine!");
137 TargetMachine &Target = *target.get();
138 const TargetData &TD = Target.getTargetData();
Chris Lattner4955d3e2002-05-20 21:20:08 +0000139
Chris Lattner97fd6c42001-10-15 17:30:47 +0000140 // Build up all of the passes that we want to do to the module...
Chris Lattner0686e432002-01-21 07:31:50 +0000141 PassManager Passes;
Chris Lattner97fd6c42001-10-15 17:30:47 +0000142
Chris Lattner4f6cdbd2003-04-26 20:11:09 +0000143 Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(),
Chris Lattner8d6d4252003-04-25 06:06:13 +0000144 TD.getPointerAlignment(), TD.getDoubleAlignment()));
Chris Lattner21a72182003-04-25 05:22:29 +0000145
Chris Lattnerf7286372002-02-03 23:43:19 +0000146 // Figure out where we are going to send the output...
147 std::ostream *Out = 0;
Chris Lattner422de252003-06-18 18:46:08 +0000148 if (OutputFilename != "") {
Brian Gaekec39d16a2003-06-18 21:43:33 +0000149 if (OutputFilename != "-") {
150 // Specified an output filename?
151 if (!Force && std::ifstream(OutputFilename.c_str())) {
152 // If force is not specified, make sure not to overwrite a file!
153 std::cerr << argv[0] << ": error opening '" << OutputFilename
154 << "': file exists!\n"
155 << "Use -f command line argument to force output\n";
156 return 1;
157 }
158 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner422de252003-06-18 18:46:08 +0000159
Misha Brukmand6769742003-10-10 17:56:49 +0000160 // Make sure that the Out file gets unlinked from the disk if we get a
Brian Gaekec39d16a2003-06-18 21:43:33 +0000161 // SIGINT
162 RemoveFileOnSignal(OutputFilename);
163 } else {
164 Out = &std::cout;
165 }
Chris Lattner422de252003-06-18 18:46:08 +0000166 } else {
167 if (InputFilename == "-") {
168 OutputFilename = "-";
169 Out = &std::cout;
170 } else {
Brian Gaekecf8c4f52003-06-18 21:14:23 +0000171 OutputFilename = GetFileNameRoot(InputFilename);
Chris Lattner5df6d8de2004-02-15 22:54:19 +0000172
173 if (Arch != CBackend)
174 OutputFilename += ".s";
175 else
176 OutputFilename += ".cbe.c";
Chris Lattner422de252003-06-18 18:46:08 +0000177
Chris Lattner0e11e542002-01-22 21:07:24 +0000178 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000179 // If force is not specified, make sure not to overwrite a file!
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000180 std::cerr << argv[0] << ": error opening '" << OutputFilename
181 << "': file exists!\n"
182 << "Use -f command line argument to force output\n";
Chris Lattner7f74a562002-01-20 22:54:45 +0000183 return 1;
184 }
Chris Lattner422de252003-06-18 18:46:08 +0000185
Chris Lattner7f74a562002-01-20 22:54:45 +0000186 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner422de252003-06-18 18:46:08 +0000187 if (!Out->good()) {
188 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
189 delete Out;
190 return 1;
191 }
192
Misha Brukmand6769742003-10-10 17:56:49 +0000193 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner51eb6112002-09-19 16:06:28 +0000194 // SIGINT
Chris Lattnerc065ad82002-04-18 19:55:25 +0000195 RemoveFileOnSignal(OutputFilename);
Chris Lattner97fd6c42001-10-15 17:30:47 +0000196 }
Chris Lattner422de252003-06-18 18:46:08 +0000197 }
Vikram S. Adveeb818692002-09-16 16:35:34 +0000198
Brian Gaekecf8c4f52003-06-18 21:14:23 +0000199 // Ask the target to add backend passes as necessary
Chris Lattner5667f0e2002-10-29 21:12:46 +0000200 if (Target.addPassesToEmitAssembly(Passes, *Out)) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000201 std::cerr << argv[0] << ": target '" << Target.getName()
Brian Gaekecf8c4f52003-06-18 21:14:23 +0000202 << "' does not support static compilation!\n";
203 if (Out != &std::cout) delete Out;
204 // And the Out file is empty and useless, so remove it now.
205 std::remove(OutputFilename.c_str());
206 return 1;
Chris Lattner5667f0e2002-10-29 21:12:46 +0000207 } else {
208 // Run our queue of passes all at once now, efficiently.
209 Passes.run(*M.get());
210 }
Chris Lattner2fa0dab2001-10-18 01:31:22 +0000211
Chris Lattner51eb6112002-09-19 16:06:28 +0000212 // Delete the ostream if it's not a stdout stream
Chris Lattnerf7286372002-02-03 23:43:19 +0000213 if (Out != &std::cout) delete Out;
214
Chris Lattner1a9680f2001-10-18 20:33:21 +0000215 return 0;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000216}