blob: e34685751428e65897e7fe259e6214ae5e7f3bad [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
58static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
59
Evan Cheng712e80e2009-05-04 23:05:19 +000060// Determine optimization level.
Bill Wendling98a366d2009-04-29 23:29:43 +000061static cl::opt<char>
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000062OptLevel("O",
Evan Cheng712e80e2009-05-04 23:05:19 +000063 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
64 "(default = '-O2')"),
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000065 cl::Prefix,
66 cl::ZeroOrMore,
Bill Wendling98a366d2009-04-29 23:29:43 +000067 cl::init(' '));
Chris Lattner178e0c42005-11-08 02:12:17 +000068
Chris Lattnerf33b8662005-12-16 04:59:57 +000069static cl::opt<std::string>
Chris Lattnerbe193832005-12-16 05:19:55 +000070TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner178e0c42005-11-08 02:12:17 +000071
Daniel Dunbar1d929212009-07-16 02:23:53 +000072static cl::opt<std::string>
73MArch("march", cl::desc("Architecture to generate code for (see --version)"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000074
Jim Laskeyb1e11802005-09-01 21:38:21 +000075static cl::opt<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000076MCPU("mcpu",
Chris Lattner7b7593c2005-10-23 22:35:42 +000077 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskeyb1e11802005-09-01 21:38:21 +000078 cl::value_desc("cpu-name"),
79 cl::init(""));
80
81static cl::list<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000082MAttrs("mattr",
Jim Laskeyb1e11802005-09-01 21:38:21 +000083 cl::CommaSeparated,
Chris Lattner7b7593c2005-10-23 22:35:42 +000084 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner20947252005-10-23 22:37:13 +000085 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskeyb1e11802005-09-01 21:38:21 +000086
Chris Lattner812125a2005-06-25 03:32:05 +000087cl::opt<TargetMachine::CodeGenFileType>
88FileType("filetype", cl::init(TargetMachine::AssemblyFile),
89 cl::desc("Choose a file type (not all types are supported by all targets):"),
90 cl::values(
Misha Brukman262b05f2008-12-31 17:39:58 +000091 clEnumValN(TargetMachine::AssemblyFile, "asm",
Dan Gohmanb8cab922008-10-14 20:25:08 +000092 "Emit an assembly ('.s') file"),
Misha Brukman262b05f2008-12-31 17:39:58 +000093 clEnumValN(TargetMachine::ObjectFile, "obj",
Dan Gohmanb8cab922008-10-14 20:25:08 +000094 "Emit a native object ('.o') file [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000095 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
Dan Gohmanb8cab922008-10-14 20:25:08 +000096 "Emit a native dynamic library ('.so') file"
Nate Begeman712b8352006-08-23 21:29:52 +000097 " [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000098 clEnumValEnd));
99
Reid Spencer4418c2b2005-07-28 02:25:30 +0000100cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000101 cl::desc("Do not verify input module"));
Reid Spencer4418c2b2005-07-28 02:25:30 +0000102
Chris Lattner812125a2005-06-25 03:32:05 +0000103
Devang Pateld18e31a2009-06-04 22:05:33 +0000104static cl::opt<bool>
105DisableRedZone("disable-red-zone",
106 cl::desc("Do not emit code that uses the red zone."),
107 cl::init(false));
108
Devang Patel578efa92009-06-05 21:57:13 +0000109static cl::opt<bool>
110NoImplicitFloats("no-implicit-float",
111 cl::desc("Don't generate implicit floating point instructions (x86-only)"),
112 cl::init(false));
113
Chris Lattner812125a2005-06-25 03:32:05 +0000114// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerb5881f12003-04-25 05:26:11 +0000115static inline std::string
Chris Lattnere45110e2004-07-11 04:03:24 +0000116GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerb5881f12003-04-25 05:26:11 +0000117 std::string IFN = InputFilename;
118 std::string outputFilename;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000119 int Len = IFN.length();
John Criswellb5d09bf2003-08-28 21:42:29 +0000120 if ((Len > 2) &&
121 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerb5881f12003-04-25 05:26:11 +0000122 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000123 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +0000124 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000125 }
126 return outputFilename;
127}
128
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000129static formatted_raw_ostream *GetOutputStream(const char *TargetName,
130 const char *ProgName) {
Chris Lattner1911fd42006-09-04 04:14:57 +0000131 if (OutputFilename != "") {
132 if (OutputFilename == "-")
David Greene71847812009-07-14 20:18:05 +0000133 return &fouts();
Chris Lattner1911fd42006-09-04 04:14:57 +0000134
Chris Lattner1911fd42006-09-04 04:14:57 +0000135 // Make sure that the Out file gets unlinked from the disk if we get a
136 // SIGINT
137 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
138
Owen Andersoncb371882008-08-21 00:14:44 +0000139 std::string error;
David Greene71847812009-07-14 20:18:05 +0000140 raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(),
Dan Gohmana1bdced2009-07-15 17:29:42 +0000141 /*Binary=*/true, Force, error);
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000142 if (!error.empty()) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000143 errs() << error << '\n';
Dan Gohmana1bdced2009-07-15 17:29:42 +0000144 if (!Force)
145 errs() << "Use -f command line argument to force output\n";
146 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;
David Greene71847812009-07-14 20:18:05 +0000190 raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(),
Dan Gohmana1bdced2009-07-15 17:29:42 +0000191 Binary, Force, error);
Owen Andersoncb371882008-08-21 00:14:44 +0000192 if (!error.empty()) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000193 errs() << error << '\n';
Dan Gohmana1bdced2009-07-15 17:29:42 +0000194 if (!Force)
195 errs() << "Use -f command line argument to force output\n";
196 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 Dunbar3c2d4bf2009-08-03 04:03:51 +0000268 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(),
269 /*FallbackToHost=*/false,
Daniel Dunbara5881e32009-07-26 02:12:58 +0000270 /*RequireJIT=*/false,
271 Err);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000272 if (TheTarget == 0) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000273 errs() << argv[0] << ": error auto-selecting target for module '"
274 << Err << "'. Please use the -march option to explicitly "
275 << "pick a target.\n";
Chris Lattnerbb433502003-08-24 14:02:14 +0000276 return 1;
Chris Lattnere45110e2004-07-11 04:03:24 +0000277 }
Chris Lattner63342052002-10-29 21:12:46 +0000278 }
Chris Lattner1a735402007-05-06 04:55:19 +0000279
280 // Package up features to be passed to target/subtarget
281 std::string FeaturesStr;
282 if (MCPU.size() || MAttrs.size()) {
283 SubtargetFeatures Features;
284 Features.setCPU(MCPU);
285 for (unsigned i = 0; i != MAttrs.size(); ++i)
286 Features.AddFeature(MAttrs[i]);
287 FeaturesStr = Features.getString();
288 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000289
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000290 std::auto_ptr<TargetMachine>
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000291 target(TheTarget->createTargetMachine(mod, TheTriple.getTriple(),
292 FeaturesStr));
Chris Lattner1a735402007-05-06 04:55:19 +0000293 assert(target.get() && "Could not allocate target machine!");
294 TargetMachine &Target = *target.get();
295
296 // Figure out where we are going to send the output...
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000297 formatted_raw_ostream *Out = GetOutputStream(TheTarget->getName(), argv[0]);
Chris Lattner1a735402007-05-06 04:55:19 +0000298 if (Out == 0) return 1;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000299
Evan Cheng712e80e2009-05-04 23:05:19 +0000300 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
Bill Wendling98a366d2009-04-29 23:29:43 +0000301 switch (OptLevel) {
302 default:
Dan Gohman65f57c22009-07-15 16:35:29 +0000303 errs() << argv[0] << ": invalid optimization level.\n";
Bill Wendling8dc85dd2009-04-29 23:46:43 +0000304 return 1;
Bill Wendling98a366d2009-04-29 23:29:43 +0000305 case ' ': break;
306 case '0': OLvl = CodeGenOpt::None; break;
Bill Wendling581b9342009-04-30 00:57:51 +0000307 case '1':
308 case '2': OLvl = CodeGenOpt::Default; break;
Bill Wendling98a366d2009-04-29 23:29:43 +0000309 case '3': OLvl = CodeGenOpt::Aggressive; break;
Bill Wendling98a366d2009-04-29 23:29:43 +0000310 }
311
Chris Lattner1a735402007-05-06 04:55:19 +0000312 // If this target requires addPassesToEmitWholeFile, do it now. This is
313 // used by strange things like the C backend.
314 if (Target.WantsWholeFile()) {
315 PassManager PM;
316 PM.add(new TargetData(*Target.getTargetData()));
317 if (!NoVerify)
318 PM.add(createVerifierPass());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000319
Chris Lattner1a735402007-05-06 04:55:19 +0000320 // Ask the target to add backend passes as necessary.
Bill Wendling98a366d2009-04-29 23:29:43 +0000321 if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, OLvl)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000322 errs() << argv[0] << ": target does not support generation of this"
323 << " file type!\n";
David Greene71847812009-07-14 20:18:05 +0000324 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000325 // And the Out file is empty and useless, so remove it now.
326 sys::Path(OutputFilename).eraseFromDisk();
327 return 1;
328 }
329 PM.run(mod);
330 } else {
331 // Build up all of the passes that we want to do to the module.
Dan Gohman33ef2bb2008-04-16 15:56:26 +0000332 ExistingModuleProvider Provider(M.release());
333 FunctionPassManager Passes(&Provider);
Chris Lattner1a735402007-05-06 04:55:19 +0000334 Passes.add(new TargetData(*Target.getTargetData()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000335
Chris Lattner1a735402007-05-06 04:55:19 +0000336#ifndef NDEBUG
337 if (!NoVerify)
338 Passes.add(createVerifierPass());
339#endif
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000340
Chris Lattner1a735402007-05-06 04:55:19 +0000341 // Ask the target to add backend passes as necessary.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000342 ObjectCodeEmitter *OCE = 0;
Chris Lattner1a735402007-05-06 04:55:19 +0000343
Evan Cheng23120ba2009-03-25 01:48:21 +0000344 // Override default to generate verbose assembly.
345 Target.setAsmVerbosityDefault(true);
346
Bill Wendling98a366d2009-04-29 23:29:43 +0000347 switch (Target.addPassesToEmitFile(Passes, *Out, FileType, OLvl)) {
Chris Lattner1a735402007-05-06 04:55:19 +0000348 default:
349 assert(0 && "Invalid file model!");
350 return 1;
351 case FileModel::Error:
Dan Gohman65f57c22009-07-15 16:35:29 +0000352 errs() << argv[0] << ": target does not support generation of this"
353 << " file type!\n";
David Greene71847812009-07-14 20:18:05 +0000354 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000355 // And the Out file is empty and useless, so remove it now.
356 sys::Path(OutputFilename).eraseFromDisk();
357 return 1;
358 case FileModel::AsmFile:
359 break;
360 case FileModel::MachOFile:
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000361 OCE = AddMachOWriter(Passes, *Out, Target);
Chris Lattner1a735402007-05-06 04:55:19 +0000362 break;
363 case FileModel::ElfFile:
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000364 OCE = AddELFWriter(Passes, *Out, Target);
Chris Lattner1a735402007-05-06 04:55:19 +0000365 break;
366 }
367
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000368 if (Target.addPassesToEmitFileFinish(Passes, OCE, OLvl)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000369 errs() << argv[0] << ": target does not support generation of this"
370 << " file type!\n";
David Greene71847812009-07-14 20:18:05 +0000371 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000372 // And the Out file is empty and useless, so remove it now.
373 sys::Path(OutputFilename).eraseFromDisk();
374 return 1;
375 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000376
Chris Lattner1a735402007-05-06 04:55:19 +0000377 Passes.doInitialization();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000378
Chris Lattner1a735402007-05-06 04:55:19 +0000379 // Run our queue of passes all at once now, efficiently.
380 // TODO: this could lazily stream functions out of the module.
381 for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I)
Devang Pateld18e31a2009-06-04 22:05:33 +0000382 if (!I->isDeclaration()) {
383 if (DisableRedZone)
384 I->addFnAttr(Attribute::NoRedZone);
Devang Patel578efa92009-06-05 21:57:13 +0000385 if (NoImplicitFloats)
386 I->addFnAttr(Attribute::NoImplicitFloat);
Chris Lattner1a735402007-05-06 04:55:19 +0000387 Passes.run(*I);
Devang Pateld18e31a2009-06-04 22:05:33 +0000388 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000389
Chris Lattner1a735402007-05-06 04:55:19 +0000390 Passes.doFinalization();
391 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000392
David Greene71847812009-07-14 20:18:05 +0000393 Out->flush();
394
Chris Lattner1a735402007-05-06 04:55:19 +0000395 // Delete the ostream if it's not a stdout stream
David Greene71847812009-07-14 20:18:05 +0000396 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000397
398 return 0;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000399}