blob: 3176b4ce7432c51d076dc0d0f586706260d3733a [file] [log] [blame]
Chris Lattner5b836c42003-06-20 15:49:04 +00001//===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
John Criswell7c0e0222003-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 Lattnere737c7a2001-09-07 22:20:50 +00009//
Chris Lattner5b836c42003-06-20 15:49:04 +000010// This is the llc code generator.
Chris Lattnere737c7a2001-09-07 22:20:50 +000011//
Chris Lattnerb79757c2001-10-04 01:40:53 +000012//===----------------------------------------------------------------------===//
Vikram S. Advecb465fc2001-07-21 12:42:29 +000013
Vikram S. Advecb465fc2001-07-21 12:42:29 +000014#include "llvm/Bytecode/Reader.h"
Chris Lattner4aba5e32002-10-29 20:45:04 +000015#include "llvm/Target/TargetMachineImpls.h"
Vikram S. Adve805eb962001-09-18 13:10:45 +000016#include "llvm/Target/TargetMachine.h"
Chris Lattner65f1b892002-05-07 20:03:27 +000017#include "llvm/Transforms/Scalar.h"
Chris Lattner46ac43c2001-09-07 21:26:31 +000018#include "llvm/Module.h"
Chris Lattnercd50d3f2002-01-31 00:46:45 +000019#include "llvm/PassManager.h"
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000020#include "llvm/Pass.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000021#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000022#include "Support/Signals.h"
Chris Lattnerda784ee2001-09-18 17:04:18 +000023#include <memory>
Chris Lattner78f7e1a2001-09-19 16:52:09 +000024#include <fstream>
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000025
Brian Gaeked0fde302003-11-11 22:41:34 +000026using namespace llvm;
27
Vikram S. Adve7d0ba022002-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 Lattnerb5881f12003-04-25 05:26:11 +000032static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000033InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
34
Chris Lattnerb5881f12003-04-25 05:26:11 +000035static cl::opt<std::string>
Chris Lattner5ff62e92002-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 Lattner666d20a2004-02-13 23:19:09 +000040enum ArchName { noarch, X86, Sparc, PowerPC, CBackend };
Chris Lattner68492722003-04-28 03:28:56 +000041
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000042static cl::opt<ArchName>
43Arch("march", cl::desc("Architecture to generate assembly for:"), cl::Prefix,
Chris Lattner666d20a2004-02-13 23:19:09 +000044 cl::values(clEnumValN(X86, "x86", " IA-32 (Pentium and above)"),
45 clEnumValN(Sparc, "sparc", " SPARC V9"),
46 clEnumValN(PowerPC, "powerpc", " PowerPC"),
47 clEnumValN(CBackend, "c", " C backend"),
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000048 0),
Misha Brukman5d5bc7b2003-07-30 15:29:55 +000049 cl::init(noarch));
Chris Lattner5ff62e92002-07-22 02:10:13 +000050
Chris Lattner3524fc22001-10-15 17:30:47 +000051// GetFileNameRoot - Helper function to get the basename of a filename...
Chris Lattnerb5881f12003-04-25 05:26:11 +000052static inline std::string
53GetFileNameRoot(const std::string &InputFilename)
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000054{
Chris Lattnerb5881f12003-04-25 05:26:11 +000055 std::string IFN = InputFilename;
56 std::string outputFilename;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000057 int Len = IFN.length();
John Criswellb5d09bf2003-08-28 21:42:29 +000058 if ((Len > 2) &&
59 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerb5881f12003-04-25 05:26:11 +000060 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000061 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +000062 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000063 }
64 return outputFilename;
65}
66
Vikram S. Adve805eb962001-09-18 13:10:45 +000067
Chris Lattner5b836c42003-06-20 15:49:04 +000068// main - Entry point for the llc compiler.
69//
70int main(int argc, char **argv) {
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000071 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
Chris Lattner364d1202004-02-19 20:32:39 +000072 PrintStackTraceOnErrorSignal();
73
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000074 // Load the module to be compiled...
Chris Lattner697954c2002-01-20 22:54:45 +000075 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
Chris Lattner5b836c42003-06-20 15:49:04 +000076 if (M.get() == 0) {
77 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
78 return 1;
79 }
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000080 Module &mod = *M.get();
81
82 // Allocate target machine. First, check whether the user has
83 // explicitly specified an architecture to compile for.
Chris Lattner6fb6ce32003-12-28 09:51:04 +000084 TargetMachine* (*TargetMachineAllocator)(const Module&,
85 IntrinsicLowering *) = 0;
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000086 switch (Arch) {
Chris Lattner666d20a2004-02-13 23:19:09 +000087 case CBackend:
88 TargetMachineAllocator = allocateCTargetMachine;
89 break;
90 case X86:
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000091 TargetMachineAllocator = allocateX86TargetMachine;
92 break;
93 case Sparc:
94 TargetMachineAllocator = allocateSparcTargetMachine;
95 break;
Brian Gaeke2217bdb2004-02-02 19:06:12 +000096 case PowerPC:
97 TargetMachineAllocator = allocatePowerPCTargetMachine;
98 break;
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000099 default:
100 // Decide what the default target machine should be, by looking at
101 // the module. This heuristic (ILP32, LE -> IA32; LP64, BE ->
102 // SPARCV9) is kind of gross, but it will work until we have more
103 // sophisticated target information to work from.
Chris Lattnerbb433502003-08-24 14:02:14 +0000104 if (mod.getEndianness() == Module::LittleEndian &&
105 mod.getPointerSize() == Module::Pointer32) {
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000106 TargetMachineAllocator = allocateX86TargetMachine;
Brian Gaeke2217bdb2004-02-02 19:06:12 +0000107 } else if (mod.getEndianness() == Module::BigEndian &&
108 mod.getPointerSize() == Module::Pointer32) {
109 TargetMachineAllocator = allocatePowerPCTargetMachine;
Chris Lattnerbb433502003-08-24 14:02:14 +0000110 } else if (mod.getEndianness() == Module::BigEndian &&
111 mod.getPointerSize() == Module::Pointer64) {
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000112 TargetMachineAllocator = allocateSparcTargetMachine;
113 } else {
Chris Lattnerbb433502003-08-24 14:02:14 +0000114 // If the module is target independent, favor a target which matches the
115 // current build system.
116#if defined(i386) || defined(__i386__) || defined(__x86__)
117 TargetMachineAllocator = allocateX86TargetMachine;
118#elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
119 TargetMachineAllocator = allocateSparcTargetMachine;
Brian Gaeke2217bdb2004-02-02 19:06:12 +0000120#elif defined(__POWERPC__) || defined(__ppc__) || defined(__APPLE__)
121 TargetMachineAllocator = allocatePowerPCTargetMachine;
Chris Lattnerbb433502003-08-24 14:02:14 +0000122#else
123 std::cerr << argv[0] << ": module does not specify a target to use. "
Chris Lattner666d20a2004-02-13 23:19:09 +0000124 << "You must use the -march option. If no native target is "
125 << "available, use -march=c to emit C code.\n";
Chris Lattnerbb433502003-08-24 14:02:14 +0000126 return 1;
127#endif
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000128 }
129 break;
130 }
Chris Lattner6fb6ce32003-12-28 09:51:04 +0000131 std::auto_ptr<TargetMachine> target(TargetMachineAllocator(mod, 0));
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000132 assert(target.get() && "Could not allocate target machine!");
133 TargetMachine &Target = *target.get();
134 const TargetData &TD = Target.getTargetData();
Chris Lattner39fd6592002-05-20 21:20:08 +0000135
Chris Lattner3524fc22001-10-15 17:30:47 +0000136 // Build up all of the passes that we want to do to the module...
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000137 PassManager Passes;
Chris Lattner3524fc22001-10-15 17:30:47 +0000138
Chris Lattner10daaa12003-04-26 20:11:09 +0000139 Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(),
Chris Lattnerbc194662003-04-25 06:06:13 +0000140 TD.getPointerAlignment(), TD.getDoubleAlignment()));
Chris Lattner2b5f2c12003-04-25 05:22:29 +0000141
Chris Lattnere41576d2002-02-03 23:43:19 +0000142 // Figure out where we are going to send the output...
143 std::ostream *Out = 0;
Chris Lattnercccc28c2003-06-18 18:46:08 +0000144 if (OutputFilename != "") {
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000145 if (OutputFilename != "-") {
146 // Specified an output filename?
147 if (!Force && std::ifstream(OutputFilename.c_str())) {
148 // If force is not specified, make sure not to overwrite a file!
149 std::cerr << argv[0] << ": error opening '" << OutputFilename
150 << "': file exists!\n"
151 << "Use -f command line argument to force output\n";
152 return 1;
153 }
154 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattnercccc28c2003-06-18 18:46:08 +0000155
Misha Brukman452fea92003-10-10 17:56:49 +0000156 // Make sure that the Out file gets unlinked from the disk if we get a
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000157 // SIGINT
158 RemoveFileOnSignal(OutputFilename);
159 } else {
160 Out = &std::cout;
161 }
Chris Lattnercccc28c2003-06-18 18:46:08 +0000162 } else {
163 if (InputFilename == "-") {
164 OutputFilename = "-";
165 Out = &std::cout;
166 } else {
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000167 OutputFilename = GetFileNameRoot(InputFilename);
Chris Lattner74661c82004-02-15 22:54:19 +0000168
169 if (Arch != CBackend)
170 OutputFilename += ".s";
171 else
172 OutputFilename += ".cbe.c";
Chris Lattnercccc28c2003-06-18 18:46:08 +0000173
Chris Lattner888912d2002-01-22 21:07:24 +0000174 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000175 // If force is not specified, make sure not to overwrite a file!
Chris Lattnerb5881f12003-04-25 05:26:11 +0000176 std::cerr << argv[0] << ": error opening '" << OutputFilename
177 << "': file exists!\n"
178 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000179 return 1;
180 }
Chris Lattnercccc28c2003-06-18 18:46:08 +0000181
Chris Lattner697954c2002-01-20 22:54:45 +0000182 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattnercccc28c2003-06-18 18:46:08 +0000183 if (!Out->good()) {
184 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
185 delete Out;
186 return 1;
187 }
188
Misha Brukman452fea92003-10-10 17:56:49 +0000189 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner0f82df42002-09-19 16:06:28 +0000190 // SIGINT
Chris Lattner76d12292002-04-18 19:55:25 +0000191 RemoveFileOnSignal(OutputFilename);
Chris Lattner3524fc22001-10-15 17:30:47 +0000192 }
Chris Lattnercccc28c2003-06-18 18:46:08 +0000193 }
Vikram S. Adve7d0ba022002-09-16 16:35:34 +0000194
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000195 // Ask the target to add backend passes as necessary
Chris Lattner63342052002-10-29 21:12:46 +0000196 if (Target.addPassesToEmitAssembly(Passes, *Out)) {
Chris Lattnerb5881f12003-04-25 05:26:11 +0000197 std::cerr << argv[0] << ": target '" << Target.getName()
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000198 << "' does not support static compilation!\n";
199 if (Out != &std::cout) delete Out;
200 // And the Out file is empty and useless, so remove it now.
201 std::remove(OutputFilename.c_str());
202 return 1;
Chris Lattner63342052002-10-29 21:12:46 +0000203 } else {
204 // Run our queue of passes all at once now, efficiently.
205 Passes.run(*M.get());
206 }
Chris Lattner05e5e072001-10-18 01:31:22 +0000207
Chris Lattner0f82df42002-09-19 16:06:28 +0000208 // Delete the ostream if it's not a stdout stream
Chris Lattnere41576d2002-02-03 23:43:19 +0000209 if (Out != &std::cout) delete Out;
210
Chris Lattnerd7477ee2001-10-18 20:33:21 +0000211 return 0;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000212}