blob: e71a7b985dba07b6f589e793d0fc97360ec92858 [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"
Jim Laskeyb1e11802005-09-01 21:38:21 +000017#include "llvm/Target/SubtargetFeature.h"
Vikram S. Adve805eb962001-09-18 13:10:45 +000018#include "llvm/Target/TargetMachine.h"
Chris Lattnere45110e2004-07-11 04:03:24 +000019#include "llvm/Target/TargetMachineRegistry.h"
Chris Lattner65f1b892002-05-07 20:03:27 +000020#include "llvm/Transforms/Scalar.h"
Chris Lattner46ac43c2001-09-07 21:26:31 +000021#include "llvm/Module.h"
Chris Lattnercd50d3f2002-01-31 00:46:45 +000022#include "llvm/PassManager.h"
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000023#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/PluginLoader.h"
Reid Spencer4418c2b2005-07-28 02:25:30 +000026#include "llvm/Support/PassNameParser.h"
27#include "llvm/Analysis/Verifier.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000028#include "llvm/System/Signals.h"
Chris Lattner812125a2005-06-25 03:32:05 +000029#include "llvm/Config/config.h"
Chris Lattner78f7e1a2001-09-19 16:52:09 +000030#include <fstream>
Reid Spencer86f42bd2004-07-04 12:20:55 +000031#include <iostream>
32#include <memory>
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000033
Brian Gaeked0fde302003-11-11 22:41:34 +000034using namespace llvm;
35
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000036// General options for llc. Other pass-specific options are specified
37// within the corresponding llc passes, and target-specific options
38// and back-end code generation options are specified with the target machine.
Misha Brukman3da94ae2005-04-22 00:00:37 +000039//
Chris Lattnerb5881f12003-04-25 05:26:11 +000040static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000041InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
42
Chris Lattnerb5881f12003-04-25 05:26:11 +000043static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000044OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
45
46static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
47
Chris Lattner178e0c42005-11-08 02:12:17 +000048static cl::opt<bool> Fast("fast",
49 cl::desc("Generate code quickly, potentially sacrificing code quality"));
50
51
Chris Lattnere45110e2004-07-11 04:03:24 +000052static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
Chris Lattnercbb34a72005-06-25 03:00:34 +000053MArch("march", cl::desc("Architecture to generate code for:"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000054
Jim Laskeyb1e11802005-09-01 21:38:21 +000055static cl::opt<std::string>
56MCPU("mcpu",
Chris Lattner7b7593c2005-10-23 22:35:42 +000057 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskeyb1e11802005-09-01 21:38:21 +000058 cl::value_desc("cpu-name"),
59 cl::init(""));
60
61static cl::list<std::string>
62MAttrs("mattr",
63 cl::CommaSeparated,
Chris Lattner7b7593c2005-10-23 22:35:42 +000064 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner20947252005-10-23 22:37:13 +000065 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskeyb1e11802005-09-01 21:38:21 +000066
Chris Lattner812125a2005-06-25 03:32:05 +000067cl::opt<TargetMachine::CodeGenFileType>
68FileType("filetype", cl::init(TargetMachine::AssemblyFile),
69 cl::desc("Choose a file type (not all types are supported by all targets):"),
70 cl::values(
71 clEnumValN(TargetMachine::AssemblyFile, "asm",
72 " Emit an assembly ('.s') file"),
73 clEnumValN(TargetMachine::ObjectFile, "obj",
Chris Lattner07be8d42005-10-22 22:00:45 +000074 " Emit a native object ('.o') file [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000075 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
76 " Emit a native dynamic library ('.so') file"),
77 clEnumValEnd));
78
Reid Spencer4418c2b2005-07-28 02:25:30 +000079// The LLCPassList is populated with passes that were registered using
80// PassInfo::LLC by the FilteredPassNameParser:
81cl::list<const PassInfo*, bool, FilteredPassNameParser<PassInfo::LLC> >
82LLCPassList(cl::desc("Passes Available"));
83
84cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohend29b6aa2005-07-30 18:33:25 +000085 cl::desc("Do not verify input module"));
Reid Spencer4418c2b2005-07-28 02:25:30 +000086
Chris Lattner812125a2005-06-25 03:32:05 +000087
88// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerb5881f12003-04-25 05:26:11 +000089static inline std::string
Chris Lattnere45110e2004-07-11 04:03:24 +000090GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerb5881f12003-04-25 05:26:11 +000091 std::string IFN = InputFilename;
92 std::string outputFilename;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000093 int Len = IFN.length();
John Criswellb5d09bf2003-08-28 21:42:29 +000094 if ((Len > 2) &&
95 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerb5881f12003-04-25 05:26:11 +000096 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000097 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +000098 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000099 }
100 return outputFilename;
101}
102
Vikram S. Adve805eb962001-09-18 13:10:45 +0000103
Chris Lattner5b836c42003-06-20 15:49:04 +0000104// main - Entry point for the llc compiler.
105//
106int main(int argc, char **argv) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000107 try {
108 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
109 sys::PrintStackTraceOnErrorSignal();
Chris Lattner364d1202004-02-19 20:32:39 +0000110
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000111 // Load the module to be compiled...
112 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
113 if (M.get() == 0) {
114 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattnerbb433502003-08-24 14:02:14 +0000115 return 1;
Chris Lattnere45110e2004-07-11 04:03:24 +0000116 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000117 Module &mod = *M.get();
Chris Lattnere45110e2004-07-11 04:03:24 +0000118
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000119 // Allocate target machine. First, check whether the user has
120 // explicitly specified an architecture to compile for.
121 TargetMachine* (*TargetMachineAllocator)(const Module&,
122 IntrinsicLowering *) = 0;
123 if (MArch == 0) {
124 std::string Err;
125 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err);
126 if (MArch == 0) {
127 std::cerr << argv[0] << ": error auto-selecting target for module '"
128 << Err << "'. Please use the -march option to explicitly "
129 << "pick a target.\n";
Misha Brukman878ba7c2004-07-12 22:58:07 +0000130 return 1;
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000131 }
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000132 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000133
Jim Laskeyb1e11802005-09-01 21:38:21 +0000134 // Package up features to be passed to target/subtarget
135 std::string FeaturesStr;
136 if (MCPU.size() || MAttrs.size()) {
137 SubtargetFeatures Features;
138 Features.setCPU(MCPU);
139 for (unsigned i = 0; i != MAttrs.size(); ++i)
140 Features.AddFeature(MAttrs[i]);
141 FeaturesStr = Features.getString();
142 }
143
144 std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, 0, FeaturesStr));
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000145 assert(target.get() && "Could not allocate target machine!");
146 TargetMachine &Target = *target.get();
147 const TargetData &TD = Target.getTargetData();
148
149 // Build up all of the passes that we want to do to the module...
150 PassManager Passes;
Chris Lattnercbb34a72005-06-25 03:00:34 +0000151 Passes.add(new TargetData(TD));
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000152
Reid Spencer4418c2b2005-07-28 02:25:30 +0000153 // Create a new pass for each one specified on the command line
154 for (unsigned i = 0; i < LLCPassList.size(); ++i) {
155 const PassInfo *aPass = LLCPassList[i];
156
157 if (aPass->getNormalCtor()) {
158 Pass *P = aPass->getNormalCtor()();
159 Passes.add(P);
160 } else {
161 std::cerr << argv[0] << ": cannot create pass: "
162 << aPass->getPassName() << "\n";
163 }
164 }
165
Reid Spencer540f7d62005-07-28 04:00:49 +0000166#ifndef NDEBUG
167 if(!NoVerify)
168 Passes.add(createVerifierPass());
169#endif
Reid Spencer4418c2b2005-07-28 02:25:30 +0000170
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000171 // Figure out where we are going to send the output...
172 std::ostream *Out = 0;
173 if (OutputFilename != "") {
174 if (OutputFilename != "-") {
175 // Specified an output filename?
176 if (!Force && std::ifstream(OutputFilename.c_str())) {
177 // If force is not specified, make sure not to overwrite a file!
178 std::cerr << argv[0] << ": error opening '" << OutputFilename
179 << "': file exists!\n"
180 << "Use -f command line argument to force output\n";
181 return 1;
182 }
183 Out = new std::ofstream(OutputFilename.c_str());
184
185 // Make sure that the Out file gets unlinked from the disk if we get a
186 // SIGINT
187 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
188 } else {
189 Out = &std::cout;
190 }
Chris Lattnercccc28c2003-06-18 18:46:08 +0000191 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000192 if (InputFilename == "-") {
193 OutputFilename = "-";
194 Out = &std::cout;
195 } else {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000196 OutputFilename = GetFileNameRoot(InputFilename);
Chris Lattner74661c82004-02-15 22:54:19 +0000197
Chris Lattner812125a2005-06-25 03:32:05 +0000198 switch (FileType) {
199 case TargetMachine::AssemblyFile:
200 if (MArch->Name[0] != 'c' || MArch->Name[1] != 0) // not CBE
201 OutputFilename += ".s";
202 else
203 OutputFilename += ".cbe.c";
204 break;
205 case TargetMachine::ObjectFile:
206 OutputFilename += ".o";
207 break;
208 case TargetMachine::DynamicLibrary:
209 OutputFilename += LTDL_SHLIB_EXT;
210 break;
211 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000212
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000213 if (!Force && std::ifstream(OutputFilename.c_str())) {
214 // If force is not specified, make sure not to overwrite a file!
215 std::cerr << argv[0] << ": error opening '" << OutputFilename
216 << "': file exists!\n"
217 << "Use -f command line argument to force output\n";
218 return 1;
219 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000220
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000221 Out = new std::ofstream(OutputFilename.c_str());
222 if (!Out->good()) {
223 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
224 delete Out;
225 return 1;
226 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000227
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000228 // Make sure that the Out file gets unlinked from the disk if we get a
229 // SIGINT
230 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Chris Lattner697954c2002-01-20 22:54:45 +0000231 }
Chris Lattner3524fc22001-10-15 17:30:47 +0000232 }
Vikram S. Adve7d0ba022002-09-16 16:35:34 +0000233
Chris Lattnercbb34a72005-06-25 03:00:34 +0000234 // Ask the target to add backend passes as necessary.
Chris Lattner178e0c42005-11-08 02:12:17 +0000235 if (Target.addPassesToEmitFile(Passes, *Out, FileType, Fast)) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000236 std::cerr << argv[0] << ": target '" << Target.getName()
Chris Lattner812125a2005-06-25 03:32:05 +0000237 << "' does not support generation of this file type!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000238 if (Out != &std::cout) delete Out;
239 // And the Out file is empty and useless, so remove it now.
240 std::remove(OutputFilename.c_str());
241 return 1;
242 } else {
243 // Run our queue of passes all at once now, efficiently.
244 Passes.run(*M.get());
245 }
246
247 // Delete the ostream if it's not a stdout stream
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000248 if (Out != &std::cout) delete Out;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000249
250 return 0;
251 } catch (const std::string& msg) {
252 std::cerr << argv[0] << ": " << msg << "\n";
253 } catch (...) {
254 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner63342052002-10-29 21:12:46 +0000255 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000256 return 1;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000257}