blob: f6fccbe8aabc7e77c62e411261db3e4ab463b37c [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"
Dan Gohman778b06b2009-09-02 19:35:19 +000023#include "llvm/Support/IRReader.h"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000024#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 Dunbar2b991bb2009-09-03 05:47:22 +0000214 // Initialize targets first, so that --version shows registered targets.
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...
Dan Gohman778b06b2009-09-02 19:35:19 +0000221 SMDiagnostic Err;
Chris Lattner1a735402007-05-06 04:55:19 +0000222 std::auto_ptr<Module> M;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000223
Dan Gohman778b06b2009-09-02 19:35:19 +0000224 M.reset(ParseIRFile(InputFilename, Err, Context));
Chris Lattner1a735402007-05-06 04:55:19 +0000225 if (M.get() == 0) {
Dan Gohman778b06b2009-09-02 19:35:19 +0000226 Err.Print(argv[0], errs());
Chris Lattner1a735402007-05-06 04:55:19 +0000227 return 1;
228 }
229 Module &mod = *M.get();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000230
Chris Lattner1a735402007-05-06 04:55:19 +0000231 // If we are supposed to override the target triple, do so now.
232 if (!TargetTriple.empty())
233 mod.setTargetTriple(TargetTriple);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000234
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000235 Triple TheTriple(mod.getTargetTriple());
236 if (TheTriple.getTriple().empty())
237 TheTriple.setTriple(sys::getHostTriple());
238
239 // Allocate target machine. First, check whether the user has explicitly
240 // specified an architecture to compile for. If so we have to look it up by
241 // name, because it might be a backend that has no mapping to a target triple.
Daniel Dunbar1d929212009-07-16 02:23:53 +0000242 const Target *TheTarget = 0;
243 if (!MArch.empty()) {
244 for (TargetRegistry::iterator it = TargetRegistry::begin(),
245 ie = TargetRegistry::end(); it != ie; ++it) {
246 if (MArch == it->getName()) {
247 TheTarget = &*it;
248 break;
249 }
250 }
251
252 if (!TheTarget) {
253 errs() << argv[0] << ": error: invalid target '" << MArch << "'.\n";
254 return 1;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000255 }
256
257 // Adjust the triple to match (if known), otherwise stick with the
258 // module/host triple.
259 Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
260 if (Type != Triple::UnknownArch)
261 TheTriple.setArch(Type);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000262 } else {
Chris Lattner1a735402007-05-06 04:55:19 +0000263 std::string Err;
Daniel Dunbar4bd03ab2009-08-03 04:20:57 +0000264 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Err);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000265 if (TheTarget == 0) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000266 errs() << argv[0] << ": error auto-selecting target for module '"
267 << Err << "'. Please use the -march option to explicitly "
268 << "pick a target.\n";
Chris Lattnerbb433502003-08-24 14:02:14 +0000269 return 1;
Chris Lattnere45110e2004-07-11 04:03:24 +0000270 }
Chris Lattner63342052002-10-29 21:12:46 +0000271 }
Chris Lattner1a735402007-05-06 04:55:19 +0000272
273 // Package up features to be passed to target/subtarget
274 std::string FeaturesStr;
275 if (MCPU.size() || MAttrs.size()) {
276 SubtargetFeatures Features;
277 Features.setCPU(MCPU);
278 for (unsigned i = 0; i != MAttrs.size(); ++i)
279 Features.AddFeature(MAttrs[i]);
280 FeaturesStr = Features.getString();
281 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000282
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000283 std::auto_ptr<TargetMachine>
Daniel Dunbar4b3d5722009-08-04 04:08:40 +0000284 target(TheTarget->createTargetMachine(TheTriple.getTriple(), FeaturesStr));
Chris Lattner1a735402007-05-06 04:55:19 +0000285 assert(target.get() && "Could not allocate target machine!");
286 TargetMachine &Target = *target.get();
287
288 // Figure out where we are going to send the output...
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000289 formatted_raw_ostream *Out = GetOutputStream(TheTarget->getName(), argv[0]);
Chris Lattner1a735402007-05-06 04:55:19 +0000290 if (Out == 0) return 1;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000291
Evan Cheng712e80e2009-05-04 23:05:19 +0000292 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
Bill Wendling98a366d2009-04-29 23:29:43 +0000293 switch (OptLevel) {
294 default:
Dan Gohman65f57c22009-07-15 16:35:29 +0000295 errs() << argv[0] << ": invalid optimization level.\n";
Bill Wendling8dc85dd2009-04-29 23:46:43 +0000296 return 1;
Bill Wendling98a366d2009-04-29 23:29:43 +0000297 case ' ': break;
298 case '0': OLvl = CodeGenOpt::None; break;
Bill Wendling581b9342009-04-30 00:57:51 +0000299 case '1':
300 case '2': OLvl = CodeGenOpt::Default; break;
Bill Wendling98a366d2009-04-29 23:29:43 +0000301 case '3': OLvl = CodeGenOpt::Aggressive; break;
Bill Wendling98a366d2009-04-29 23:29:43 +0000302 }
303
Chris Lattner1a735402007-05-06 04:55:19 +0000304 // If this target requires addPassesToEmitWholeFile, do it now. This is
305 // used by strange things like the C backend.
306 if (Target.WantsWholeFile()) {
307 PassManager PM;
Daniel Dunbar4e02eb02009-08-03 17:34:19 +0000308
309 // Add the target data from the target machine, if it exists, or the module.
310 if (const TargetData *TD = Target.getTargetData())
311 PM.add(new TargetData(*TD));
312 else
313 PM.add(new TargetData(&mod));
314
Chris Lattner1a735402007-05-06 04:55:19 +0000315 if (!NoVerify)
316 PM.add(createVerifierPass());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000317
Chris Lattner1a735402007-05-06 04:55:19 +0000318 // Ask the target to add backend passes as necessary.
Bill Wendling98a366d2009-04-29 23:29:43 +0000319 if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, OLvl)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000320 errs() << argv[0] << ": target does not support generation of this"
321 << " file type!\n";
David Greene71847812009-07-14 20:18:05 +0000322 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000323 // And the Out file is empty and useless, so remove it now.
324 sys::Path(OutputFilename).eraseFromDisk();
325 return 1;
326 }
327 PM.run(mod);
328 } else {
329 // Build up all of the passes that we want to do to the module.
Dan Gohman33ef2bb2008-04-16 15:56:26 +0000330 ExistingModuleProvider Provider(M.release());
331 FunctionPassManager Passes(&Provider);
Daniel Dunbar4e02eb02009-08-03 17:34:19 +0000332
333 // Add the target data from the target machine, if it exists, or the module.
334 if (const TargetData *TD = Target.getTargetData())
335 Passes.add(new TargetData(*TD));
336 else
337 Passes.add(new TargetData(&mod));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000338
Chris Lattner1a735402007-05-06 04:55:19 +0000339#ifndef NDEBUG
340 if (!NoVerify)
341 Passes.add(createVerifierPass());
342#endif
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000343
Chris Lattner1a735402007-05-06 04:55:19 +0000344 // Ask the target to add backend passes as necessary.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000345 ObjectCodeEmitter *OCE = 0;
Chris Lattner1a735402007-05-06 04:55:19 +0000346
Evan Cheng23120ba2009-03-25 01:48:21 +0000347 // Override default to generate verbose assembly.
348 Target.setAsmVerbosityDefault(true);
349
Bill Wendling98a366d2009-04-29 23:29:43 +0000350 switch (Target.addPassesToEmitFile(Passes, *Out, FileType, OLvl)) {
Chris Lattner1a735402007-05-06 04:55:19 +0000351 default:
352 assert(0 && "Invalid file model!");
353 return 1;
354 case FileModel::Error:
Dan Gohman65f57c22009-07-15 16:35:29 +0000355 errs() << argv[0] << ": target does not support generation of this"
356 << " file type!\n";
David Greene71847812009-07-14 20:18:05 +0000357 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000358 // And the Out file is empty and useless, so remove it now.
359 sys::Path(OutputFilename).eraseFromDisk();
360 return 1;
361 case FileModel::AsmFile:
362 break;
363 case FileModel::MachOFile:
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000364 OCE = AddMachOWriter(Passes, *Out, Target);
Chris Lattner1a735402007-05-06 04:55:19 +0000365 break;
366 case FileModel::ElfFile:
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000367 OCE = AddELFWriter(Passes, *Out, Target);
Chris Lattner1a735402007-05-06 04:55:19 +0000368 break;
369 }
370
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000371 if (Target.addPassesToEmitFileFinish(Passes, OCE, OLvl)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000372 errs() << argv[0] << ": target does not support generation of this"
373 << " file type!\n";
David Greene71847812009-07-14 20:18:05 +0000374 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000375 // And the Out file is empty and useless, so remove it now.
376 sys::Path(OutputFilename).eraseFromDisk();
377 return 1;
378 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000379
Chris Lattner1a735402007-05-06 04:55:19 +0000380 Passes.doInitialization();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000381
Chris Lattner1a735402007-05-06 04:55:19 +0000382 // Run our queue of passes all at once now, efficiently.
383 // TODO: this could lazily stream functions out of the module.
384 for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I)
Devang Pateld18e31a2009-06-04 22:05:33 +0000385 if (!I->isDeclaration()) {
386 if (DisableRedZone)
387 I->addFnAttr(Attribute::NoRedZone);
Devang Patel578efa92009-06-05 21:57:13 +0000388 if (NoImplicitFloats)
389 I->addFnAttr(Attribute::NoImplicitFloat);
Chris Lattner1a735402007-05-06 04:55:19 +0000390 Passes.run(*I);
Devang Pateld18e31a2009-06-04 22:05:33 +0000391 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000392
Chris Lattner1a735402007-05-06 04:55:19 +0000393 Passes.doFinalization();
394 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000395
Chris Lattner1a735402007-05-06 04:55:19 +0000396 // Delete the ostream if it's not a stdout stream
David Greene71847812009-07-14 20:18:05 +0000397 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000398
399 return 0;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000400}