blob: ea18547a71dccb1a2276cd0e39213541657fb47c [file] [log] [blame]
Chris Lattner5b836c42003-06-20 15:49:04 +00001//===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// 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.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
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
Misha Brukman3da94ae2005-04-22 00:00:37 +000011// command-line interface for generating native assembly-language code
Brian Gaekee40eae72004-03-16 21:47:20 +000012// 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 Lattnerd3a680a2006-08-01 22:34:35 +000017#include "llvm/CodeGen/LinkAllCodegenComponents.h"
Jim Laskeyb1e11802005-09-01 21:38:21 +000018#include "llvm/Target/SubtargetFeature.h"
Owen Anderson07000c62006-05-12 06:33:49 +000019#include "llvm/Target/TargetData.h"
Vikram S. Adve805eb962001-09-18 13:10:45 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattnere45110e2004-07-11 04:03:24 +000021#include "llvm/Target/TargetMachineRegistry.h"
Chris Lattner65f1b892002-05-07 20:03:27 +000022#include "llvm/Transforms/Scalar.h"
Chris Lattner46ac43c2001-09-07 21:26:31 +000023#include "llvm/Module.h"
Chris Lattnercd50d3f2002-01-31 00:46:45 +000024#include "llvm/PassManager.h"
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000025#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/PluginLoader.h"
Duraid Madinaea8aed72005-12-30 02:47:21 +000028#include "llvm/Support/FileUtilities.h"
Reid Spencer4418c2b2005-07-28 02:25:30 +000029#include "llvm/Analysis/Verifier.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000030#include "llvm/System/Signals.h"
Chris Lattner812125a2005-06-25 03:32:05 +000031#include "llvm/Config/config.h"
Reid Spenceraf303d52006-06-07 23:03:13 +000032#include "llvm/LinkAllVMCore.h"
Chris Lattner78f7e1a2001-09-19 16:52:09 +000033#include <fstream>
Reid Spencer86f42bd2004-07-04 12:20:55 +000034#include <iostream>
35#include <memory>
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000036
Brian Gaeked0fde302003-11-11 22:41:34 +000037using namespace llvm;
38
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000039// General options for llc. Other pass-specific options are specified
40// within the corresponding llc passes, and target-specific options
41// and back-end code generation options are specified with the target machine.
Misha Brukman3da94ae2005-04-22 00:00:37 +000042//
Chris Lattnerb5881f12003-04-25 05:26:11 +000043static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000044InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
45
Chris Lattnerb5881f12003-04-25 05:26:11 +000046static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000047OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
48
49static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
50
Chris Lattner178e0c42005-11-08 02:12:17 +000051static cl::opt<bool> Fast("fast",
52 cl::desc("Generate code quickly, potentially sacrificing code quality"));
53
Chris Lattnerf33b8662005-12-16 04:59:57 +000054static cl::opt<std::string>
Chris Lattnerbe193832005-12-16 05:19:55 +000055TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner178e0c42005-11-08 02:12:17 +000056
Chris Lattnere45110e2004-07-11 04:03:24 +000057static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
Chris Lattnercbb34a72005-06-25 03:00:34 +000058MArch("march", cl::desc("Architecture to generate code for:"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000059
Jim Laskeyb1e11802005-09-01 21:38:21 +000060static cl::opt<std::string>
61MCPU("mcpu",
Chris Lattner7b7593c2005-10-23 22:35:42 +000062 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskeyb1e11802005-09-01 21:38:21 +000063 cl::value_desc("cpu-name"),
64 cl::init(""));
65
66static cl::list<std::string>
67MAttrs("mattr",
68 cl::CommaSeparated,
Chris Lattner7b7593c2005-10-23 22:35:42 +000069 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner20947252005-10-23 22:37:13 +000070 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskeyb1e11802005-09-01 21:38:21 +000071
Chris Lattner812125a2005-06-25 03:32:05 +000072cl::opt<TargetMachine::CodeGenFileType>
73FileType("filetype", cl::init(TargetMachine::AssemblyFile),
74 cl::desc("Choose a file type (not all types are supported by all targets):"),
75 cl::values(
76 clEnumValN(TargetMachine::AssemblyFile, "asm",
77 " Emit an assembly ('.s') file"),
78 clEnumValN(TargetMachine::ObjectFile, "obj",
Chris Lattner07be8d42005-10-22 22:00:45 +000079 " Emit a native object ('.o') file [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000080 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
Nate Begeman712b8352006-08-23 21:29:52 +000081 " Emit a native dynamic library ('.so') file"
82 " [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000083 clEnumValEnd));
84
Reid Spencer4418c2b2005-07-28 02:25:30 +000085cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohend29b6aa2005-07-30 18:33:25 +000086 cl::desc("Do not verify input module"));
Reid Spencer4418c2b2005-07-28 02:25:30 +000087
Chris Lattner812125a2005-06-25 03:32:05 +000088
89// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerb5881f12003-04-25 05:26:11 +000090static inline std::string
Chris Lattnere45110e2004-07-11 04:03:24 +000091GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerb5881f12003-04-25 05:26:11 +000092 std::string IFN = InputFilename;
93 std::string outputFilename;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000094 int Len = IFN.length();
John Criswellb5d09bf2003-08-28 21:42:29 +000095 if ((Len > 2) &&
96 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerb5881f12003-04-25 05:26:11 +000097 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000098 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +000099 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000100 }
101 return outputFilename;
102}
103
Vikram S. Adve805eb962001-09-18 13:10:45 +0000104
Chris Lattner5b836c42003-06-20 15:49:04 +0000105// main - Entry point for the llc compiler.
106//
107int main(int argc, char **argv) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000108 try {
109 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
110 sys::PrintStackTraceOnErrorSignal();
Chris Lattner364d1202004-02-19 20:32:39 +0000111
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000112 // Load the module to be compiled...
113 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
114 if (M.get() == 0) {
115 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattnerbb433502003-08-24 14:02:14 +0000116 return 1;
Chris Lattnere45110e2004-07-11 04:03:24 +0000117 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000118 Module &mod = *M.get();
Chris Lattnere45110e2004-07-11 04:03:24 +0000119
Chris Lattnerf33b8662005-12-16 04:59:57 +0000120 // If we are supposed to override the target triple, do so now.
121 if (!TargetTriple.empty())
122 mod.setTargetTriple(TargetTriple);
123
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000124 // Allocate target machine. First, check whether the user has
125 // explicitly specified an architecture to compile for.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000126 if (MArch == 0) {
127 std::string Err;
128 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err);
129 if (MArch == 0) {
130 std::cerr << argv[0] << ": error auto-selecting target for module '"
131 << Err << "'. Please use the -march option to explicitly "
132 << "pick a target.\n";
Misha Brukman878ba7c2004-07-12 22:58:07 +0000133 return 1;
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000134 }
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000135 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000136
Jim Laskeyb1e11802005-09-01 21:38:21 +0000137 // Package up features to be passed to target/subtarget
138 std::string FeaturesStr;
139 if (MCPU.size() || MAttrs.size()) {
140 SubtargetFeatures Features;
141 Features.setCPU(MCPU);
142 for (unsigned i = 0; i != MAttrs.size(); ++i)
143 Features.AddFeature(MAttrs[i]);
144 FeaturesStr = Features.getString();
145 }
146
Chris Lattneref986912006-03-23 05:28:02 +0000147 std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, FeaturesStr));
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000148 assert(target.get() && "Could not allocate target machine!");
149 TargetMachine &Target = *target.get();
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000150
151 // Build up all of the passes that we want to do to the module...
152 PassManager Passes;
Chris Lattnerb93b0342006-05-04 21:18:40 +0000153 Passes.add(new TargetData(*Target.getTargetData()));
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000154
Reid Spencer540f7d62005-07-28 04:00:49 +0000155#ifndef NDEBUG
156 if(!NoVerify)
157 Passes.add(createVerifierPass());
158#endif
Reid Spencer4418c2b2005-07-28 02:25:30 +0000159
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000160 // Figure out where we are going to send the output...
161 std::ostream *Out = 0;
162 if (OutputFilename != "") {
163 if (OutputFilename != "-") {
164 // Specified an output filename?
165 if (!Force && std::ifstream(OutputFilename.c_str())) {
166 // If force is not specified, make sure not to overwrite a file!
167 std::cerr << argv[0] << ": error opening '" << OutputFilename
168 << "': file exists!\n"
169 << "Use -f command line argument to force output\n";
170 return 1;
171 }
172 Out = new std::ofstream(OutputFilename.c_str());
173
174 // Make sure that the Out file gets unlinked from the disk if we get a
175 // SIGINT
176 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
177 } else {
178 Out = &std::cout;
179 }
Chris Lattnercccc28c2003-06-18 18:46:08 +0000180 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000181 if (InputFilename == "-") {
182 OutputFilename = "-";
183 Out = &std::cout;
184 } else {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000185 OutputFilename = GetFileNameRoot(InputFilename);
Chris Lattner74661c82004-02-15 22:54:19 +0000186
Chris Lattner812125a2005-06-25 03:32:05 +0000187 switch (FileType) {
188 case TargetMachine::AssemblyFile:
189 if (MArch->Name[0] != 'c' || MArch->Name[1] != 0) // not CBE
190 OutputFilename += ".s";
191 else
192 OutputFilename += ".cbe.c";
193 break;
194 case TargetMachine::ObjectFile:
195 OutputFilename += ".o";
196 break;
197 case TargetMachine::DynamicLibrary:
198 OutputFilename += LTDL_SHLIB_EXT;
199 break;
200 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000201
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000202 if (!Force && std::ifstream(OutputFilename.c_str())) {
203 // If force is not specified, make sure not to overwrite a file!
204 std::cerr << argv[0] << ": error opening '" << OutputFilename
205 << "': file exists!\n"
206 << "Use -f command line argument to force output\n";
207 return 1;
208 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000209
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000210 Out = new std::ofstream(OutputFilename.c_str());
211 if (!Out->good()) {
212 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
213 delete Out;
214 return 1;
215 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000216
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000217 // Make sure that the Out file gets unlinked from the disk if we get a
218 // SIGINT
219 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Chris Lattner697954c2002-01-20 22:54:45 +0000220 }
Chris Lattner3524fc22001-10-15 17:30:47 +0000221 }
Chris Lattner8b57ff72006-04-18 05:31:20 +0000222
Chris Lattnercbb34a72005-06-25 03:00:34 +0000223 // Ask the target to add backend passes as necessary.
Chris Lattner178e0c42005-11-08 02:12:17 +0000224 if (Target.addPassesToEmitFile(Passes, *Out, FileType, Fast)) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000225 std::cerr << argv[0] << ": target '" << Target.getName()
Chris Lattner812125a2005-06-25 03:32:05 +0000226 << "' does not support generation of this file type!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000227 if (Out != &std::cout) delete Out;
228 // And the Out file is empty and useless, so remove it now.
Duraid Madinaea8aed72005-12-30 02:47:21 +0000229 sys::Path(OutputFilename).eraseFromDisk();
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000230 return 1;
231 } else {
232 // Run our queue of passes all at once now, efficiently.
233 Passes.run(*M.get());
234 }
235
236 // Delete the ostream if it's not a stdout stream
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000237 if (Out != &std::cout) delete Out;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000238
239 return 0;
240 } catch (const std::string& msg) {
241 std::cerr << argv[0] << ": " << msg << "\n";
242 } catch (...) {
243 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner63342052002-10-29 21:12:46 +0000244 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000245 return 1;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000246}