blob: b995e7d564e03af64352e7d382cc48c6925a6520 [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//
Brian Gaekee40eae72004-03-16 21:47:20 +000010// This is the llc code generator driver. It provides a convenient
11// command-line interface for generating native assembly-language code
12// or C code, given LLVM bytecode.
Chris Lattnere737c7a2001-09-07 22:20:50 +000013//
Chris Lattnerb79757c2001-10-04 01:40:53 +000014//===----------------------------------------------------------------------===//
Vikram S. Advecb465fc2001-07-21 12:42:29 +000015
Vikram S. Advecb465fc2001-07-21 12:42:29 +000016#include "llvm/Bytecode/Reader.h"
Chris Lattner4aba5e32002-10-29 20:45:04 +000017#include "llvm/Target/TargetMachineImpls.h"
Vikram S. Adve805eb962001-09-18 13:10:45 +000018#include "llvm/Target/TargetMachine.h"
Chris Lattner65f1b892002-05-07 20:03:27 +000019#include "llvm/Transforms/Scalar.h"
Chris Lattner46ac43c2001-09-07 21:26:31 +000020#include "llvm/Module.h"
Chris Lattnercd50d3f2002-01-31 00:46:45 +000021#include "llvm/PassManager.h"
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000022#include "llvm/Pass.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000024#include "Support/Signals.h"
Chris Lattnerda784ee2001-09-18 17:04:18 +000025#include <memory>
Chris Lattner78f7e1a2001-09-19 16:52:09 +000026#include <fstream>
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000027
Brian Gaeked0fde302003-11-11 22:41:34 +000028using namespace llvm;
29
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000030// General options for llc. Other pass-specific options are specified
31// within the corresponding llc passes, and target-specific options
32// and back-end code generation options are specified with the target machine.
33//
Chris Lattnerb5881f12003-04-25 05:26:11 +000034static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000035InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
36
Chris Lattnerb5881f12003-04-25 05:26:11 +000037static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000038OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
39
40static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
41
Misha Brukman094158a2004-03-11 18:16:33 +000042enum ArchName { noarch, X86, SparcV9, PowerPC, CBackend };
Chris Lattner68492722003-04-28 03:28:56 +000043
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000044static cl::opt<ArchName>
45Arch("march", cl::desc("Architecture to generate assembly for:"), cl::Prefix,
Chris Lattner666d20a2004-02-13 23:19:09 +000046 cl::values(clEnumValN(X86, "x86", " IA-32 (Pentium and above)"),
Brian Gaeke150666f2004-02-25 19:08:12 +000047 clEnumValN(SparcV9, "sparcv9", " SPARC V9"),
Chris Lattnerd62276a2004-02-28 19:55:16 +000048 clEnumValN(PowerPC, "powerpc", " PowerPC (experimental)"),
Chris Lattner666d20a2004-02-13 23:19:09 +000049 clEnumValN(CBackend, "c", " C backend"),
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000050 0),
Misha Brukman5d5bc7b2003-07-30 15:29:55 +000051 cl::init(noarch));
Chris Lattner5ff62e92002-07-22 02:10:13 +000052
Chris Lattner3524fc22001-10-15 17:30:47 +000053// GetFileNameRoot - Helper function to get the basename of a filename...
Chris Lattnerb5881f12003-04-25 05:26:11 +000054static inline std::string
55GetFileNameRoot(const std::string &InputFilename)
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000056{
Chris Lattnerb5881f12003-04-25 05:26:11 +000057 std::string IFN = InputFilename;
58 std::string outputFilename;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000059 int Len = IFN.length();
John Criswellb5d09bf2003-08-28 21:42:29 +000060 if ((Len > 2) &&
61 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerb5881f12003-04-25 05:26:11 +000062 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000063 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +000064 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000065 }
66 return outputFilename;
67}
68
Vikram S. Adve805eb962001-09-18 13:10:45 +000069
Chris Lattner5b836c42003-06-20 15:49:04 +000070// main - Entry point for the llc compiler.
71//
72int main(int argc, char **argv) {
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000073 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
Chris Lattner364d1202004-02-19 20:32:39 +000074 PrintStackTraceOnErrorSignal();
75
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000076 // Load the module to be compiled...
Chris Lattner697954c2002-01-20 22:54:45 +000077 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
Chris Lattner5b836c42003-06-20 15:49:04 +000078 if (M.get() == 0) {
79 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
80 return 1;
81 }
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000082 Module &mod = *M.get();
83
84 // Allocate target machine. First, check whether the user has
85 // explicitly specified an architecture to compile for.
Chris Lattner6fb6ce32003-12-28 09:51:04 +000086 TargetMachine* (*TargetMachineAllocator)(const Module&,
87 IntrinsicLowering *) = 0;
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000088 switch (Arch) {
Chris Lattner666d20a2004-02-13 23:19:09 +000089 case CBackend:
90 TargetMachineAllocator = allocateCTargetMachine;
91 break;
92 case X86:
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000093 TargetMachineAllocator = allocateX86TargetMachine;
94 break;
Brian Gaeke150666f2004-02-25 19:08:12 +000095 case SparcV9:
96 TargetMachineAllocator = allocateSparcV9TargetMachine;
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000097 break;
Brian Gaeke2217bdb2004-02-02 19:06:12 +000098 case PowerPC:
99 TargetMachineAllocator = allocatePowerPCTargetMachine;
100 break;
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000101 default:
102 // Decide what the default target machine should be, by looking at
103 // the module. This heuristic (ILP32, LE -> IA32; LP64, BE ->
104 // SPARCV9) is kind of gross, but it will work until we have more
105 // sophisticated target information to work from.
Chris Lattnerbb433502003-08-24 14:02:14 +0000106 if (mod.getEndianness() == Module::LittleEndian &&
107 mod.getPointerSize() == Module::Pointer32) {
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000108 TargetMachineAllocator = allocateX86TargetMachine;
Brian Gaeke2217bdb2004-02-02 19:06:12 +0000109 } else if (mod.getEndianness() == Module::BigEndian &&
110 mod.getPointerSize() == Module::Pointer32) {
111 TargetMachineAllocator = allocatePowerPCTargetMachine;
Chris Lattnerbb433502003-08-24 14:02:14 +0000112 } else if (mod.getEndianness() == Module::BigEndian &&
113 mod.getPointerSize() == Module::Pointer64) {
Brian Gaeke150666f2004-02-25 19:08:12 +0000114 TargetMachineAllocator = allocateSparcV9TargetMachine;
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000115 } else {
Chris Lattnerbb433502003-08-24 14:02:14 +0000116 // If the module is target independent, favor a target which matches the
117 // current build system.
118#if defined(i386) || defined(__i386__) || defined(__x86__)
119 TargetMachineAllocator = allocateX86TargetMachine;
120#elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Brian Gaeke150666f2004-02-25 19:08:12 +0000121 TargetMachineAllocator = allocateSparcV9TargetMachine;
Brian Gaeke2217bdb2004-02-02 19:06:12 +0000122#elif defined(__POWERPC__) || defined(__ppc__) || defined(__APPLE__)
123 TargetMachineAllocator = allocatePowerPCTargetMachine;
Chris Lattnerbb433502003-08-24 14:02:14 +0000124#else
125 std::cerr << argv[0] << ": module does not specify a target to use. "
Chris Lattner666d20a2004-02-13 23:19:09 +0000126 << "You must use the -march option. If no native target is "
127 << "available, use -march=c to emit C code.\n";
Chris Lattnerbb433502003-08-24 14:02:14 +0000128 return 1;
129#endif
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000130 }
131 break;
132 }
Chris Lattner6fb6ce32003-12-28 09:51:04 +0000133 std::auto_ptr<TargetMachine> target(TargetMachineAllocator(mod, 0));
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000134 assert(target.get() && "Could not allocate target machine!");
135 TargetMachine &Target = *target.get();
136 const TargetData &TD = Target.getTargetData();
Chris Lattner39fd6592002-05-20 21:20:08 +0000137
Chris Lattner3524fc22001-10-15 17:30:47 +0000138 // Build up all of the passes that we want to do to the module...
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000139 PassManager Passes;
Chris Lattner3524fc22001-10-15 17:30:47 +0000140
Chris Lattner10daaa12003-04-26 20:11:09 +0000141 Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(),
Chris Lattnerbc194662003-04-25 06:06:13 +0000142 TD.getPointerAlignment(), TD.getDoubleAlignment()));
Chris Lattner2b5f2c12003-04-25 05:22:29 +0000143
Chris Lattnere41576d2002-02-03 23:43:19 +0000144 // Figure out where we are going to send the output...
145 std::ostream *Out = 0;
Chris Lattnercccc28c2003-06-18 18:46:08 +0000146 if (OutputFilename != "") {
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000147 if (OutputFilename != "-") {
148 // Specified an output filename?
149 if (!Force && std::ifstream(OutputFilename.c_str())) {
150 // If force is not specified, make sure not to overwrite a file!
151 std::cerr << argv[0] << ": error opening '" << OutputFilename
152 << "': file exists!\n"
153 << "Use -f command line argument to force output\n";
154 return 1;
155 }
156 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattnercccc28c2003-06-18 18:46:08 +0000157
Misha Brukman452fea92003-10-10 17:56:49 +0000158 // Make sure that the Out file gets unlinked from the disk if we get a
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000159 // SIGINT
160 RemoveFileOnSignal(OutputFilename);
161 } else {
162 Out = &std::cout;
163 }
Chris Lattnercccc28c2003-06-18 18:46:08 +0000164 } else {
165 if (InputFilename == "-") {
166 OutputFilename = "-";
167 Out = &std::cout;
168 } else {
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000169 OutputFilename = GetFileNameRoot(InputFilename);
Chris Lattner74661c82004-02-15 22:54:19 +0000170
171 if (Arch != CBackend)
172 OutputFilename += ".s";
173 else
174 OutputFilename += ".cbe.c";
Chris Lattnercccc28c2003-06-18 18:46:08 +0000175
Chris Lattner888912d2002-01-22 21:07:24 +0000176 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000177 // If force is not specified, make sure not to overwrite a file!
Chris Lattnerb5881f12003-04-25 05:26:11 +0000178 std::cerr << argv[0] << ": error opening '" << OutputFilename
179 << "': file exists!\n"
180 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000181 return 1;
182 }
Chris Lattnercccc28c2003-06-18 18:46:08 +0000183
Chris Lattner697954c2002-01-20 22:54:45 +0000184 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattnercccc28c2003-06-18 18:46:08 +0000185 if (!Out->good()) {
186 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
187 delete Out;
188 return 1;
189 }
190
Misha Brukman452fea92003-10-10 17:56:49 +0000191 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner0f82df42002-09-19 16:06:28 +0000192 // SIGINT
Chris Lattner76d12292002-04-18 19:55:25 +0000193 RemoveFileOnSignal(OutputFilename);
Chris Lattner3524fc22001-10-15 17:30:47 +0000194 }
Chris Lattnercccc28c2003-06-18 18:46:08 +0000195 }
Vikram S. Adve7d0ba022002-09-16 16:35:34 +0000196
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000197 // Ask the target to add backend passes as necessary
Chris Lattner63342052002-10-29 21:12:46 +0000198 if (Target.addPassesToEmitAssembly(Passes, *Out)) {
Chris Lattnerb5881f12003-04-25 05:26:11 +0000199 std::cerr << argv[0] << ": target '" << Target.getName()
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000200 << "' does not support static compilation!\n";
201 if (Out != &std::cout) delete Out;
202 // And the Out file is empty and useless, so remove it now.
203 std::remove(OutputFilename.c_str());
204 return 1;
Chris Lattner63342052002-10-29 21:12:46 +0000205 } else {
206 // Run our queue of passes all at once now, efficiently.
207 Passes.run(*M.get());
208 }
Chris Lattner05e5e072001-10-18 01:31:22 +0000209
Chris Lattner0f82df42002-09-19 16:06:28 +0000210 // Delete the ostream if it's not a stdout stream
Chris Lattnere41576d2002-02-03 23:43:19 +0000211 if (Out != &std::cout) delete Out;
212
Chris Lattnerd7477ee2001-10-18 20:33:21 +0000213 return 0;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000214}