blob: c194709256a6041bf31c04560141dd9af3ceb989 [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//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Gabor Greifa99be512007-07-05 17:07:56 +000012// or C code, given LLVM bitcode.
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
Owen Anderson8b477ed2009-07-01 16:58:40 +000016#include "llvm/LLVMContext.h"
Chris Lattner46ac43c2001-09-07 21:26:31 +000017#include "llvm/Module.h"
Chris Lattner744879e2007-05-06 09:32:02 +000018#include "llvm/ModuleProvider.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"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000021#include "llvm/ADT/Triple.h"
22#include "llvm/Analysis/Verifier.h"
23#include "llvm/Bitcode/ReaderWriter.h"
24#include "llvm/CodeGen/FileWriters.h"
25#include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
26#include "llvm/CodeGen/LinkAllCodegenComponents.h"
27#include "llvm/CodeGen/ObjectCodeEmitter.h"
28#include "llvm/Config/config.h"
29#include "llvm/LinkAllVMCore.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000030#include "llvm/Support/CommandLine.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000031#include "llvm/Support/FileUtilities.h"
David Greene71847812009-07-14 20:18:05 +000032#include "llvm/Support/FormattedStream.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000033#include "llvm/Support/ManagedStatic.h"
Chris Lattner1a735402007-05-06 04:55:19 +000034#include "llvm/Support/MemoryBuffer.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000035#include "llvm/Support/PluginLoader.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000036#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000037#include "llvm/System/Host.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000038#include "llvm/System/Signals.h"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000039#include "llvm/Target/SubtargetFeature.h"
40#include "llvm/Target/TargetData.h"
41#include "llvm/Target/TargetMachine.h"
42#include "llvm/Target/TargetRegistry.h"
Chris Lattner2deb58f2009-06-17 16:42:19 +000043#include "llvm/Target/TargetSelect.h"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000044#include "llvm/Transforms/Scalar.h"
Reid Spencer86f42bd2004-07-04 12:20:55 +000045#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000046using namespace llvm;
47
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000048// General options for llc. Other pass-specific options are specified
49// within the corresponding llc passes, and target-specific options
50// and back-end code generation options are specified with the target machine.
Misha Brukman3da94ae2005-04-22 00:00:37 +000051//
Chris Lattnerb5881f12003-04-25 05:26:11 +000052static cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000053InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000054
Chris Lattnerb5881f12003-04-25 05:26:11 +000055static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000056OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
57
Dan Gohmanbaa26392009-08-25 15:34:52 +000058static cl::opt<bool>
59Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000060
Evan Cheng712e80e2009-05-04 23:05:19 +000061// Determine optimization level.
Bill Wendling98a366d2009-04-29 23:29:43 +000062static cl::opt<char>
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000063OptLevel("O",
Evan Cheng712e80e2009-05-04 23:05:19 +000064 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
65 "(default = '-O2')"),
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000066 cl::Prefix,
67 cl::ZeroOrMore,
Bill Wendling98a366d2009-04-29 23:29:43 +000068 cl::init(' '));
Chris Lattner178e0c42005-11-08 02:12:17 +000069
Chris Lattnerf33b8662005-12-16 04:59:57 +000070static cl::opt<std::string>
Chris Lattnerbe193832005-12-16 05:19:55 +000071TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner178e0c42005-11-08 02:12:17 +000072
Daniel Dunbar1d929212009-07-16 02:23:53 +000073static cl::opt<std::string>
74MArch("march", cl::desc("Architecture to generate code for (see --version)"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000075
Jim Laskeyb1e11802005-09-01 21:38:21 +000076static cl::opt<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000077MCPU("mcpu",
Chris Lattner7b7593c2005-10-23 22:35:42 +000078 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskeyb1e11802005-09-01 21:38:21 +000079 cl::value_desc("cpu-name"),
80 cl::init(""));
81
82static cl::list<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000083MAttrs("mattr",
Jim Laskeyb1e11802005-09-01 21:38:21 +000084 cl::CommaSeparated,
Chris Lattner7b7593c2005-10-23 22:35:42 +000085 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner20947252005-10-23 22:37:13 +000086 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskeyb1e11802005-09-01 21:38:21 +000087
Chris Lattner812125a2005-06-25 03:32:05 +000088cl::opt<TargetMachine::CodeGenFileType>
89FileType("filetype", cl::init(TargetMachine::AssemblyFile),
90 cl::desc("Choose a file type (not all types are supported by all targets):"),
91 cl::values(
Misha Brukman262b05f2008-12-31 17:39:58 +000092 clEnumValN(TargetMachine::AssemblyFile, "asm",
Dan Gohmanb8cab922008-10-14 20:25:08 +000093 "Emit an assembly ('.s') file"),
Misha Brukman262b05f2008-12-31 17:39:58 +000094 clEnumValN(TargetMachine::ObjectFile, "obj",
Dan Gohmanb8cab922008-10-14 20:25:08 +000095 "Emit a native object ('.o') file [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000096 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
Dan Gohmanb8cab922008-10-14 20:25:08 +000097 "Emit a native dynamic library ('.so') file"
Nate Begeman712b8352006-08-23 21:29:52 +000098 " [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000099 clEnumValEnd));
100
Reid Spencer4418c2b2005-07-28 02:25:30 +0000101cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000102 cl::desc("Do not verify input module"));
Reid Spencer4418c2b2005-07-28 02:25:30 +0000103
Chris Lattner812125a2005-06-25 03:32:05 +0000104
Devang Pateld18e31a2009-06-04 22:05:33 +0000105static cl::opt<bool>
106DisableRedZone("disable-red-zone",
107 cl::desc("Do not emit code that uses the red zone."),
108 cl::init(false));
109
Devang Patel578efa92009-06-05 21:57:13 +0000110static cl::opt<bool>
111NoImplicitFloats("no-implicit-float",
112 cl::desc("Don't generate implicit floating point instructions (x86-only)"),
113 cl::init(false));
114
Chris Lattner812125a2005-06-25 03:32:05 +0000115// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerb5881f12003-04-25 05:26:11 +0000116static inline std::string
Chris Lattnere45110e2004-07-11 04:03:24 +0000117GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerb5881f12003-04-25 05:26:11 +0000118 std::string IFN = InputFilename;
119 std::string outputFilename;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000120 int Len = IFN.length();
John Criswellb5d09bf2003-08-28 21:42:29 +0000121 if ((Len > 2) &&
122 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerb5881f12003-04-25 05:26:11 +0000123 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000124 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +0000125 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000126 }
127 return outputFilename;
128}
129
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000130static formatted_raw_ostream *GetOutputStream(const char *TargetName,
131 const char *ProgName) {
Chris Lattner1911fd42006-09-04 04:14:57 +0000132 if (OutputFilename != "") {
133 if (OutputFilename == "-")
David Greene71847812009-07-14 20:18:05 +0000134 return &fouts();
Chris Lattner1911fd42006-09-04 04:14:57 +0000135
Chris Lattner1911fd42006-09-04 04:14:57 +0000136 // Make sure that the Out file gets unlinked from the disk if we get a
137 // SIGINT
138 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
139
Owen Andersoncb371882008-08-21 00:14:44 +0000140 std::string error;
Chris Lattner17e9edc2009-08-23 02:51:22 +0000141 raw_fd_ostream *FDOut =
142 new raw_fd_ostream(OutputFilename.c_str(), error,
Chris Lattner17e9edc2009-08-23 02:51:22 +0000143 raw_fd_ostream::F_Binary);
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000144 if (!error.empty()) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000145 errs() << error << '\n';
Dan Gohmana1bdced2009-07-15 17:29:42 +0000146 delete FDOut;
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000147 return 0;
148 }
Dan Gohmana1bdced2009-07-15 17:29:42 +0000149 formatted_raw_ostream *Out =
150 new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000151
152 return Out;
Chris Lattner1911fd42006-09-04 04:14:57 +0000153 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000154
Chris Lattner1911fd42006-09-04 04:14:57 +0000155 if (InputFilename == "-") {
156 OutputFilename = "-";
David Greene71847812009-07-14 20:18:05 +0000157 return &fouts();
Chris Lattner1911fd42006-09-04 04:14:57 +0000158 }
159
160 OutputFilename = GetFileNameRoot(InputFilename);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000161
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000162 bool Binary = false;
Chris Lattner1911fd42006-09-04 04:14:57 +0000163 switch (FileType) {
164 case TargetMachine::AssemblyFile:
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000165 if (TargetName[0] == 'c') {
166 if (TargetName[1] == 0)
Anton Korobeynikov50276522008-04-23 22:29:24 +0000167 OutputFilename += ".cbe.c";
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000168 else if (TargetName[1] == 'p' && TargetName[2] == 'p')
Anton Korobeynikov50276522008-04-23 22:29:24 +0000169 OutputFilename += ".cpp";
170 else
171 OutputFilename += ".s";
172 } else
Chris Lattner1911fd42006-09-04 04:14:57 +0000173 OutputFilename += ".s";
Chris Lattner1911fd42006-09-04 04:14:57 +0000174 break;
175 case TargetMachine::ObjectFile:
176 OutputFilename += ".o";
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000177 Binary = true;
Chris Lattner1911fd42006-09-04 04:14:57 +0000178 break;
179 case TargetMachine::DynamicLibrary:
180 OutputFilename += LTDL_SHLIB_EXT;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000181 Binary = true;
Chris Lattner1911fd42006-09-04 04:14:57 +0000182 break;
183 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000184
Chris Lattner1911fd42006-09-04 04:14:57 +0000185 // Make sure that the Out file gets unlinked from the disk if we get a
186 // SIGINT
187 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000188
Owen Andersoncb371882008-08-21 00:14:44 +0000189 std::string error;
Chris Lattner17e9edc2009-08-23 02:51:22 +0000190 unsigned OpenFlags = 0;
Chris Lattner17e9edc2009-08-23 02:51:22 +0000191 if (Binary) OpenFlags |= raw_fd_ostream::F_Binary;
192 raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(), error,
193 OpenFlags);
Owen Andersoncb371882008-08-21 00:14:44 +0000194 if (!error.empty()) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000195 errs() << error << '\n';
Dan Gohmana1bdced2009-07-15 17:29:42 +0000196 delete FDOut;
Chris Lattner1911fd42006-09-04 04:14:57 +0000197 return 0;
198 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000199
Dan Gohmana1bdced2009-07-15 17:29:42 +0000200 formatted_raw_ostream *Out =
201 new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
202
Chris Lattner1911fd42006-09-04 04:14:57 +0000203 return Out;
204}
Vikram S. Adve805eb962001-09-18 13:10:45 +0000205
Chris Lattner5b836c42003-06-20 15:49:04 +0000206// main - Entry point for the llc compiler.
207//
208int main(int argc, char **argv) {
Chris Lattner1a735402007-05-06 04:55:19 +0000209 sys::PrintStackTraceOnErrorSignal();
Chris Lattnercc14d252009-03-06 05:34:10 +0000210 PrettyStackTraceProgram X(argc, argv);
Owen Anderson0d7c6952009-07-15 22:16:10 +0000211 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000212 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner364d1202004-02-19 20:32:39 +0000213
Daniel Dunbar494d6632009-07-16 02:04:54 +0000214 // Initialize targets first.
Chris Lattner2deb58f2009-06-17 16:42:19 +0000215 InitializeAllTargets();
216 InitializeAllAsmPrinters();
Daniel Dunbar494d6632009-07-16 02:04:54 +0000217
218 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Chris Lattner2deb58f2009-06-17 16:42:19 +0000219
Chris Lattner1a735402007-05-06 04:55:19 +0000220 // Load the module to be compiled...
221 std::string ErrorMessage;
222 std::auto_ptr<Module> M;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000223
Chris Lattner744879e2007-05-06 09:32:02 +0000224 std::auto_ptr<MemoryBuffer> Buffer(
Chris Lattner065344d2007-05-06 23:45:49 +0000225 MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage));
Chris Lattner744879e2007-05-06 09:32:02 +0000226 if (Buffer.get())
Owen Anderson31895e72009-07-01 21:22:36 +0000227 M.reset(ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage));
Chris Lattner1a735402007-05-06 04:55:19 +0000228 if (M.get() == 0) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000229 errs() << argv[0] << ": bitcode didn't read correctly.\n";
230 errs() << "Reason: " << ErrorMessage << "\n";
Chris Lattner1a735402007-05-06 04:55:19 +0000231 return 1;
232 }
233 Module &mod = *M.get();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000234
Chris Lattner1a735402007-05-06 04:55:19 +0000235 // If we are supposed to override the target triple, do so now.
236 if (!TargetTriple.empty())
237 mod.setTargetTriple(TargetTriple);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000238
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000239 Triple TheTriple(mod.getTargetTriple());
240 if (TheTriple.getTriple().empty())
241 TheTriple.setTriple(sys::getHostTriple());
242
243 // Allocate target machine. First, check whether the user has explicitly
244 // specified an architecture to compile for. If so we have to look it up by
245 // name, because it might be a backend that has no mapping to a target triple.
Daniel Dunbar1d929212009-07-16 02:23:53 +0000246 const Target *TheTarget = 0;
247 if (!MArch.empty()) {
248 for (TargetRegistry::iterator it = TargetRegistry::begin(),
249 ie = TargetRegistry::end(); it != ie; ++it) {
250 if (MArch == it->getName()) {
251 TheTarget = &*it;
252 break;
253 }
254 }
255
256 if (!TheTarget) {
257 errs() << argv[0] << ": error: invalid target '" << MArch << "'.\n";
258 return 1;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000259 }
260
261 // Adjust the triple to match (if known), otherwise stick with the
262 // module/host triple.
263 Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
264 if (Type != Triple::UnknownArch)
265 TheTriple.setArch(Type);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000266 } else {
Chris Lattner1a735402007-05-06 04:55:19 +0000267 std::string Err;
Daniel Dunbar4bd03ab2009-08-03 04:20:57 +0000268 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Err);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000269 if (TheTarget == 0) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000270 errs() << argv[0] << ": error auto-selecting target for module '"
271 << Err << "'. Please use the -march option to explicitly "
272 << "pick a target.\n";
Chris Lattnerbb433502003-08-24 14:02:14 +0000273 return 1;
Chris Lattnere45110e2004-07-11 04:03:24 +0000274 }
Chris Lattner63342052002-10-29 21:12:46 +0000275 }
Chris Lattner1a735402007-05-06 04:55:19 +0000276
277 // Package up features to be passed to target/subtarget
278 std::string FeaturesStr;
279 if (MCPU.size() || MAttrs.size()) {
280 SubtargetFeatures Features;
281 Features.setCPU(MCPU);
282 for (unsigned i = 0; i != MAttrs.size(); ++i)
283 Features.AddFeature(MAttrs[i]);
284 FeaturesStr = Features.getString();
285 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000286
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000287 std::auto_ptr<TargetMachine>
Daniel Dunbar4b3d5722009-08-04 04:08:40 +0000288 target(TheTarget->createTargetMachine(TheTriple.getTriple(), FeaturesStr));
Chris Lattner1a735402007-05-06 04:55:19 +0000289 assert(target.get() && "Could not allocate target machine!");
290 TargetMachine &Target = *target.get();
291
292 // Figure out where we are going to send the output...
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000293 formatted_raw_ostream *Out = GetOutputStream(TheTarget->getName(), argv[0]);
Chris Lattner1a735402007-05-06 04:55:19 +0000294 if (Out == 0) return 1;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000295
Evan Cheng712e80e2009-05-04 23:05:19 +0000296 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
Bill Wendling98a366d2009-04-29 23:29:43 +0000297 switch (OptLevel) {
298 default:
Dan Gohman65f57c22009-07-15 16:35:29 +0000299 errs() << argv[0] << ": invalid optimization level.\n";
Bill Wendling8dc85dd2009-04-29 23:46:43 +0000300 return 1;
Bill Wendling98a366d2009-04-29 23:29:43 +0000301 case ' ': break;
302 case '0': OLvl = CodeGenOpt::None; break;
Bill Wendling581b9342009-04-30 00:57:51 +0000303 case '1':
304 case '2': OLvl = CodeGenOpt::Default; break;
Bill Wendling98a366d2009-04-29 23:29:43 +0000305 case '3': OLvl = CodeGenOpt::Aggressive; break;
Bill Wendling98a366d2009-04-29 23:29:43 +0000306 }
307
Chris Lattner1a735402007-05-06 04:55:19 +0000308 // If this target requires addPassesToEmitWholeFile, do it now. This is
309 // used by strange things like the C backend.
310 if (Target.WantsWholeFile()) {
311 PassManager PM;
Daniel Dunbar4e02eb02009-08-03 17:34:19 +0000312
313 // Add the target data from the target machine, if it exists, or the module.
314 if (const TargetData *TD = Target.getTargetData())
315 PM.add(new TargetData(*TD));
316 else
317 PM.add(new TargetData(&mod));
318
Chris Lattner1a735402007-05-06 04:55:19 +0000319 if (!NoVerify)
320 PM.add(createVerifierPass());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000321
Chris Lattner1a735402007-05-06 04:55:19 +0000322 // Ask the target to add backend passes as necessary.
Bill Wendling98a366d2009-04-29 23:29:43 +0000323 if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, OLvl)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000324 errs() << argv[0] << ": target does not support generation of this"
325 << " file type!\n";
David Greene71847812009-07-14 20:18:05 +0000326 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000327 // And the Out file is empty and useless, so remove it now.
328 sys::Path(OutputFilename).eraseFromDisk();
329 return 1;
330 }
331 PM.run(mod);
332 } else {
333 // Build up all of the passes that we want to do to the module.
Dan Gohman33ef2bb2008-04-16 15:56:26 +0000334 ExistingModuleProvider Provider(M.release());
335 FunctionPassManager Passes(&Provider);
Daniel Dunbar4e02eb02009-08-03 17:34:19 +0000336
337 // Add the target data from the target machine, if it exists, or the module.
338 if (const TargetData *TD = Target.getTargetData())
339 Passes.add(new TargetData(*TD));
340 else
341 Passes.add(new TargetData(&mod));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000342
Chris Lattner1a735402007-05-06 04:55:19 +0000343#ifndef NDEBUG
344 if (!NoVerify)
345 Passes.add(createVerifierPass());
346#endif
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000347
Chris Lattner1a735402007-05-06 04:55:19 +0000348 // Ask the target to add backend passes as necessary.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000349 ObjectCodeEmitter *OCE = 0;
Chris Lattner1a735402007-05-06 04:55:19 +0000350
Evan Cheng23120ba2009-03-25 01:48:21 +0000351 // Override default to generate verbose assembly.
352 Target.setAsmVerbosityDefault(true);
353
Bill Wendling98a366d2009-04-29 23:29:43 +0000354 switch (Target.addPassesToEmitFile(Passes, *Out, FileType, OLvl)) {
Chris Lattner1a735402007-05-06 04:55:19 +0000355 default:
356 assert(0 && "Invalid file model!");
357 return 1;
358 case FileModel::Error:
Dan Gohman65f57c22009-07-15 16:35:29 +0000359 errs() << argv[0] << ": target does not support generation of this"
360 << " file type!\n";
David Greene71847812009-07-14 20:18:05 +0000361 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000362 // And the Out file is empty and useless, so remove it now.
363 sys::Path(OutputFilename).eraseFromDisk();
364 return 1;
365 case FileModel::AsmFile:
366 break;
367 case FileModel::MachOFile:
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000368 OCE = AddMachOWriter(Passes, *Out, Target);
Chris Lattner1a735402007-05-06 04:55:19 +0000369 break;
370 case FileModel::ElfFile:
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000371 OCE = AddELFWriter(Passes, *Out, Target);
Chris Lattner1a735402007-05-06 04:55:19 +0000372 break;
373 }
374
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000375 if (Target.addPassesToEmitFileFinish(Passes, OCE, OLvl)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000376 errs() << argv[0] << ": target does not support generation of this"
377 << " file type!\n";
David Greene71847812009-07-14 20:18:05 +0000378 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000379 // And the Out file is empty and useless, so remove it now.
380 sys::Path(OutputFilename).eraseFromDisk();
381 return 1;
382 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000383
Chris Lattner1a735402007-05-06 04:55:19 +0000384 Passes.doInitialization();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000385
Chris Lattner1a735402007-05-06 04:55:19 +0000386 // Run our queue of passes all at once now, efficiently.
387 // TODO: this could lazily stream functions out of the module.
388 for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I)
Devang Pateld18e31a2009-06-04 22:05:33 +0000389 if (!I->isDeclaration()) {
390 if (DisableRedZone)
391 I->addFnAttr(Attribute::NoRedZone);
Devang Patel578efa92009-06-05 21:57:13 +0000392 if (NoImplicitFloats)
393 I->addFnAttr(Attribute::NoImplicitFloat);
Chris Lattner1a735402007-05-06 04:55:19 +0000394 Passes.run(*I);
Devang Pateld18e31a2009-06-04 22:05:33 +0000395 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000396
Chris Lattner1a735402007-05-06 04:55:19 +0000397 Passes.doFinalization();
398 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000399
David Greene71847812009-07-14 20:18:05 +0000400 Out->flush();
401
Chris Lattner1a735402007-05-06 04:55:19 +0000402 // Delete the ostream if it's not a stdout stream
David Greene71847812009-07-14 20:18:05 +0000403 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000404
405 return 0;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000406}