blob: f58157082234daf594009db1643717a0bf83c773 [file] [log] [blame]
Chris Lattner67e08422003-06-20 15:49:04 +00001//===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-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 Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2cf137b2001-09-07 22:20:50 +00009//
Brian Gaeke33e83b62004-03-16 21:47:20 +000010// This is the llc code generator driver. It provides a convenient
Misha Brukman650ba8e2005-04-22 00:00:37 +000011// command-line interface for generating native assembly-language code
Gabor Greife16561c2007-07-05 17:07:56 +000012// or C code, given LLVM bitcode.
Chris Lattner2cf137b2001-09-07 22:20:50 +000013//
Chris Lattnerb27d4742001-10-04 01:40:53 +000014//===----------------------------------------------------------------------===//
Vikram S. Adve2d94a342001-07-21 12:42:29 +000015
Owen Anderson6773d382009-07-01 16:58:40 +000016#include "llvm/LLVMContext.h"
Chris Lattnered226062001-09-07 21:26:31 +000017#include "llvm/Module.h"
Chris Lattnerf5599ef2007-05-06 09:32:02 +000018#include "llvm/ModuleProvider.h"
Chris Lattner7139f282002-01-31 00:46:45 +000019#include "llvm/PassManager.h"
Vikram S. Adveeb818692002-09-16 16:35:34 +000020#include "llvm/Pass.h"
Daniel Dunbar0f16ea52009-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 Spencer7c16caa2004-09-01 22:55:40 +000030#include "llvm/Support/CommandLine.h"
Mikhail Glushenkov6e8d8142009-01-16 07:02:28 +000031#include "llvm/Support/FileUtilities.h"
David Greenea31f96c2009-07-14 20:18:05 +000032#include "llvm/Support/FormattedStream.h"
Chris Lattner76d46322006-12-06 01:18:01 +000033#include "llvm/Support/ManagedStatic.h"
Chris Lattner6dd290a2007-05-06 04:55:19 +000034#include "llvm/Support/MemoryBuffer.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000035#include "llvm/Support/PluginLoader.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000036#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000037#include "llvm/System/Host.h"
Chris Lattner278f5152004-05-27 05:41:36 +000038#include "llvm/System/Signals.h"
Daniel Dunbar0f16ea52009-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 Lattner5dcc4f62009-06-17 16:42:19 +000043#include "llvm/Target/TargetSelect.h"
Daniel Dunbar0f16ea52009-08-03 04:03:51 +000044#include "llvm/Transforms/Scalar.h"
Reid Spencerf0ebb252004-07-04 12:20:55 +000045#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000046using namespace llvm;
47
Vikram S. Adveeb818692002-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 Brukman650ba8e2005-04-22 00:00:37 +000051//
Chris Lattnerd64b2de2003-04-25 05:26:11 +000052static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000053InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000054
Chris Lattnerd64b2de2003-04-25 05:26:11 +000055static cl::opt<std::string>
Chris Lattnerf5cad152002-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 Cheng09bd0b12009-05-04 23:05:19 +000060// Determine optimization level.
Bill Wendling026e5d72009-04-29 23:29:43 +000061static cl::opt<char>
Bill Wendling084669a2009-04-29 00:15:41 +000062OptLevel("O",
Evan Cheng09bd0b12009-05-04 23:05:19 +000063 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
64 "(default = '-O2')"),
Bill Wendling084669a2009-04-29 00:15:41 +000065 cl::Prefix,
66 cl::ZeroOrMore,
Bill Wendling026e5d72009-04-29 23:29:43 +000067 cl::init(' '));
Chris Lattner731055e2005-11-08 02:12:17 +000068
Chris Lattnere568b882005-12-16 04:59:57 +000069static cl::opt<std::string>
Chris Lattner08a04cb2005-12-16 05:19:55 +000070TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner731055e2005-11-08 02:12:17 +000071
Daniel Dunbard3706452009-07-16 02:23:53 +000072static cl::opt<std::string>
73MArch("march", cl::desc("Architecture to generate code for (see --version)"));
Misha Brukman650ba8e2005-04-22 00:00:37 +000074
Jim Laskey19058c32005-09-01 21:38:21 +000075static cl::opt<std::string>
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000076MCPU("mcpu",
Chris Lattner0b2bf4c2005-10-23 22:35:42 +000077 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskey19058c32005-09-01 21:38:21 +000078 cl::value_desc("cpu-name"),
79 cl::init(""));
80
81static cl::list<std::string>
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000082MAttrs("mattr",
Jim Laskey19058c32005-09-01 21:38:21 +000083 cl::CommaSeparated,
Chris Lattner0b2bf4c2005-10-23 22:35:42 +000084 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner41548482005-10-23 22:37:13 +000085 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskey19058c32005-09-01 21:38:21 +000086
Chris Lattner06fcc4c2005-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 Brukmand07751e2008-12-31 17:39:58 +000091 clEnumValN(TargetMachine::AssemblyFile, "asm",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000092 "Emit an assembly ('.s') file"),
Misha Brukmand07751e2008-12-31 17:39:58 +000093 clEnumValN(TargetMachine::ObjectFile, "obj",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000094 "Emit a native object ('.o') file [experimental]"),
Chris Lattner06fcc4c2005-06-25 03:32:05 +000095 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000096 "Emit a native dynamic library ('.so') file"
Nate Begemana147cbf2006-08-23 21:29:52 +000097 " [experimental]"),
Chris Lattner06fcc4c2005-06-25 03:32:05 +000098 clEnumValEnd));
99
Reid Spencer8e3830d2005-07-28 02:25:30 +0000100cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohen546fd592005-07-30 18:33:25 +0000101 cl::desc("Do not verify input module"));
Reid Spencer8e3830d2005-07-28 02:25:30 +0000102
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000103
Devang Patel72a4d2f2009-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 Pateld1c7d342009-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 Lattner06fcc4c2005-06-25 03:32:05 +0000114// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000115static inline std::string
Chris Lattner6142ca82004-07-11 04:03:24 +0000116GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000117 std::string IFN = InputFilename;
118 std::string outputFilename;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000119 int Len = IFN.length();
John Criswella289abf2003-08-28 21:42:29 +0000120 if ((Len > 2) &&
121 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000122 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000123 } else {
Chris Lattner97fd6c42001-10-15 17:30:47 +0000124 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000125 }
126 return outputFilename;
127}
128
Daniel Dunbare8338102009-07-15 20:24:03 +0000129static formatted_raw_ostream *GetOutputStream(const char *TargetName,
130 const char *ProgName) {
Chris Lattner12e97302006-09-04 04:14:57 +0000131 if (OutputFilename != "") {
132 if (OutputFilename == "-")
David Greenea31f96c2009-07-14 20:18:05 +0000133 return &fouts();
Chris Lattner12e97302006-09-04 04:14:57 +0000134
Chris Lattner12e97302006-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 Anderson93719642008-08-21 00:14:44 +0000139 std::string error;
David Greenea31f96c2009-07-14 20:18:05 +0000140 raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(),
Dan Gohman607818a2009-07-15 17:29:42 +0000141 /*Binary=*/true, Force, error);
Dan Gohmanf3e13bb2008-08-21 15:33:45 +0000142 if (!error.empty()) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000143 errs() << error << '\n';
Dan Gohman607818a2009-07-15 17:29:42 +0000144 if (!Force)
145 errs() << "Use -f command line argument to force output\n";
146 delete FDOut;
Dan Gohmanf3e13bb2008-08-21 15:33:45 +0000147 return 0;
148 }
Dan Gohman607818a2009-07-15 17:29:42 +0000149 formatted_raw_ostream *Out =
150 new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
Dan Gohmanf3e13bb2008-08-21 15:33:45 +0000151
152 return Out;
Chris Lattner12e97302006-09-04 04:14:57 +0000153 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000154
Chris Lattner12e97302006-09-04 04:14:57 +0000155 if (InputFilename == "-") {
156 OutputFilename = "-";
David Greenea31f96c2009-07-14 20:18:05 +0000157 return &fouts();
Chris Lattner12e97302006-09-04 04:14:57 +0000158 }
159
160 OutputFilename = GetFileNameRoot(InputFilename);
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000161
Daniel Dunbared90e702008-11-13 05:01:07 +0000162 bool Binary = false;
Chris Lattner12e97302006-09-04 04:14:57 +0000163 switch (FileType) {
164 case TargetMachine::AssemblyFile:
Daniel Dunbare8338102009-07-15 20:24:03 +0000165 if (TargetName[0] == 'c') {
166 if (TargetName[1] == 0)
Anton Korobeynikov78695032008-04-23 22:29:24 +0000167 OutputFilename += ".cbe.c";
Daniel Dunbare8338102009-07-15 20:24:03 +0000168 else if (TargetName[1] == 'p' && TargetName[2] == 'p')
Anton Korobeynikov78695032008-04-23 22:29:24 +0000169 OutputFilename += ".cpp";
170 else
171 OutputFilename += ".s";
172 } else
Chris Lattner12e97302006-09-04 04:14:57 +0000173 OutputFilename += ".s";
Chris Lattner12e97302006-09-04 04:14:57 +0000174 break;
175 case TargetMachine::ObjectFile:
176 OutputFilename += ".o";
Daniel Dunbared90e702008-11-13 05:01:07 +0000177 Binary = true;
Chris Lattner12e97302006-09-04 04:14:57 +0000178 break;
179 case TargetMachine::DynamicLibrary:
180 OutputFilename += LTDL_SHLIB_EXT;
Daniel Dunbared90e702008-11-13 05:01:07 +0000181 Binary = true;
Chris Lattner12e97302006-09-04 04:14:57 +0000182 break;
183 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000184
Chris Lattner12e97302006-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 Glushenkovb2f9a732009-01-16 06:53:46 +0000188
Owen Anderson93719642008-08-21 00:14:44 +0000189 std::string error;
David Greenea31f96c2009-07-14 20:18:05 +0000190 raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(),
Dan Gohman607818a2009-07-15 17:29:42 +0000191 Binary, Force, error);
Owen Anderson93719642008-08-21 00:14:44 +0000192 if (!error.empty()) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000193 errs() << error << '\n';
Dan Gohman607818a2009-07-15 17:29:42 +0000194 if (!Force)
195 errs() << "Use -f command line argument to force output\n";
196 delete FDOut;
Chris Lattner12e97302006-09-04 04:14:57 +0000197 return 0;
198 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000199
Dan Gohman607818a2009-07-15 17:29:42 +0000200 formatted_raw_ostream *Out =
201 new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
202
Chris Lattner12e97302006-09-04 04:14:57 +0000203 return Out;
204}
Vikram S. Adve9d409352001-09-18 13:10:45 +0000205
Chris Lattner67e08422003-06-20 15:49:04 +0000206// main - Entry point for the llc compiler.
207//
208int main(int argc, char **argv) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000209 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000210 PrettyStackTraceProgram X(argc, argv);
Owen Anderson19251ec2009-07-15 22:16:10 +0000211 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000212 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner69e896b2004-02-19 20:32:39 +0000213
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000214 // Initialize targets first.
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000215 InitializeAllTargets();
216 InitializeAllAsmPrinters();
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000217
218 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000219
Chris Lattner6dd290a2007-05-06 04:55:19 +0000220 // Load the module to be compiled...
221 std::string ErrorMessage;
222 std::auto_ptr<Module> M;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000223
Chris Lattnerf5599ef2007-05-06 09:32:02 +0000224 std::auto_ptr<MemoryBuffer> Buffer(
Chris Lattner9e9a34c2007-05-06 23:45:49 +0000225 MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage));
Chris Lattnerf5599ef2007-05-06 09:32:02 +0000226 if (Buffer.get())
Owen Anderson1cf085d2009-07-01 21:22:36 +0000227 M.reset(ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000228 if (M.get() == 0) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000229 errs() << argv[0] << ": bitcode didn't read correctly.\n";
230 errs() << "Reason: " << ErrorMessage << "\n";
Chris Lattner6dd290a2007-05-06 04:55:19 +0000231 return 1;
232 }
233 Module &mod = *M.get();
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000234
Chris Lattner6dd290a2007-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 Glushenkovb2f9a732009-01-16 06:53:46 +0000238
Daniel Dunbar0f16ea52009-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 Dunbard3706452009-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 Dunbar0f16ea52009-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 Dunbare8338102009-07-15 20:24:03 +0000266 } else {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000267 std::string Err;
Daniel Dunbar719d2352009-08-03 04:20:57 +0000268 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Err);
Daniel Dunbare8338102009-07-15 20:24:03 +0000269 if (TheTarget == 0) {
Dan Gohmand8db3762009-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 Lattnerb1492402003-08-24 14:02:14 +0000273 return 1;
Chris Lattner6142ca82004-07-11 04:03:24 +0000274 }
Chris Lattner5667f0e2002-10-29 21:12:46 +0000275 }
Chris Lattner6dd290a2007-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 Glushenkovb2f9a732009-01-16 06:53:46 +0000286
Daniel Dunbare8338102009-07-15 20:24:03 +0000287 std::auto_ptr<TargetMachine>
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000288 target(TheTarget->createTargetMachine(mod, TheTriple.getTriple(),
289 FeaturesStr));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000290 assert(target.get() && "Could not allocate target machine!");
291 TargetMachine &Target = *target.get();
292
293 // Figure out where we are going to send the output...
Daniel Dunbare8338102009-07-15 20:24:03 +0000294 formatted_raw_ostream *Out = GetOutputStream(TheTarget->getName(), argv[0]);
Chris Lattner6dd290a2007-05-06 04:55:19 +0000295 if (Out == 0) return 1;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000296
Evan Cheng09bd0b12009-05-04 23:05:19 +0000297 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
Bill Wendling026e5d72009-04-29 23:29:43 +0000298 switch (OptLevel) {
299 default:
Dan Gohmand8db3762009-07-15 16:35:29 +0000300 errs() << argv[0] << ": invalid optimization level.\n";
Bill Wendlingdad99192009-04-29 23:46:43 +0000301 return 1;
Bill Wendling026e5d72009-04-29 23:29:43 +0000302 case ' ': break;
303 case '0': OLvl = CodeGenOpt::None; break;
Bill Wendlingaeaf5a52009-04-30 00:57:51 +0000304 case '1':
305 case '2': OLvl = CodeGenOpt::Default; break;
Bill Wendling026e5d72009-04-29 23:29:43 +0000306 case '3': OLvl = CodeGenOpt::Aggressive; break;
Bill Wendling026e5d72009-04-29 23:29:43 +0000307 }
308
Chris Lattner6dd290a2007-05-06 04:55:19 +0000309 // If this target requires addPassesToEmitWholeFile, do it now. This is
310 // used by strange things like the C backend.
311 if (Target.WantsWholeFile()) {
312 PassManager PM;
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000313
314 // Add the target data from the target machine, if it exists, or the module.
315 if (const TargetData *TD = Target.getTargetData())
316 PM.add(new TargetData(*TD));
317 else
318 PM.add(new TargetData(&mod));
319
Chris Lattner6dd290a2007-05-06 04:55:19 +0000320 if (!NoVerify)
321 PM.add(createVerifierPass());
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000322
Chris Lattner6dd290a2007-05-06 04:55:19 +0000323 // Ask the target to add backend passes as necessary.
Bill Wendling026e5d72009-04-29 23:29:43 +0000324 if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, OLvl)) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000325 errs() << argv[0] << ": target does not support generation of this"
326 << " file type!\n";
David Greenea31f96c2009-07-14 20:18:05 +0000327 if (Out != &fouts()) delete Out;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000328 // And the Out file is empty and useless, so remove it now.
329 sys::Path(OutputFilename).eraseFromDisk();
330 return 1;
331 }
332 PM.run(mod);
333 } else {
334 // Build up all of the passes that we want to do to the module.
Dan Gohmanbd2613d2008-04-16 15:56:26 +0000335 ExistingModuleProvider Provider(M.release());
336 FunctionPassManager Passes(&Provider);
Daniel Dunbarc0deed32009-08-03 17:34:19 +0000337
338 // Add the target data from the target machine, if it exists, or the module.
339 if (const TargetData *TD = Target.getTargetData())
340 Passes.add(new TargetData(*TD));
341 else
342 Passes.add(new TargetData(&mod));
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000343
Chris Lattner6dd290a2007-05-06 04:55:19 +0000344#ifndef NDEBUG
345 if (!NoVerify)
346 Passes.add(createVerifierPass());
347#endif
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000348
Chris Lattner6dd290a2007-05-06 04:55:19 +0000349 // Ask the target to add backend passes as necessary.
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000350 ObjectCodeEmitter *OCE = 0;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000351
Evan Cheng499d5f32009-03-25 01:48:21 +0000352 // Override default to generate verbose assembly.
353 Target.setAsmVerbosityDefault(true);
354
Bill Wendling026e5d72009-04-29 23:29:43 +0000355 switch (Target.addPassesToEmitFile(Passes, *Out, FileType, OLvl)) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000356 default:
357 assert(0 && "Invalid file model!");
358 return 1;
359 case FileModel::Error:
Dan Gohmand8db3762009-07-15 16:35:29 +0000360 errs() << argv[0] << ": target does not support generation of this"
361 << " file type!\n";
David Greenea31f96c2009-07-14 20:18:05 +0000362 if (Out != &fouts()) delete Out;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000363 // And the Out file is empty and useless, so remove it now.
364 sys::Path(OutputFilename).eraseFromDisk();
365 return 1;
366 case FileModel::AsmFile:
367 break;
368 case FileModel::MachOFile:
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000369 OCE = AddMachOWriter(Passes, *Out, Target);
Chris Lattner6dd290a2007-05-06 04:55:19 +0000370 break;
371 case FileModel::ElfFile:
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000372 OCE = AddELFWriter(Passes, *Out, Target);
Chris Lattner6dd290a2007-05-06 04:55:19 +0000373 break;
374 }
375
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000376 if (Target.addPassesToEmitFileFinish(Passes, OCE, OLvl)) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000377 errs() << argv[0] << ": target does not support generation of this"
378 << " file type!\n";
David Greenea31f96c2009-07-14 20:18:05 +0000379 if (Out != &fouts()) delete Out;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000380 // And the Out file is empty and useless, so remove it now.
381 sys::Path(OutputFilename).eraseFromDisk();
382 return 1;
383 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000384
Chris Lattner6dd290a2007-05-06 04:55:19 +0000385 Passes.doInitialization();
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000386
Chris Lattner6dd290a2007-05-06 04:55:19 +0000387 // Run our queue of passes all at once now, efficiently.
388 // TODO: this could lazily stream functions out of the module.
389 for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I)
Devang Patel72a4d2f2009-06-04 22:05:33 +0000390 if (!I->isDeclaration()) {
391 if (DisableRedZone)
392 I->addFnAttr(Attribute::NoRedZone);
Devang Pateld1c7d342009-06-05 21:57:13 +0000393 if (NoImplicitFloats)
394 I->addFnAttr(Attribute::NoImplicitFloat);
Chris Lattner6dd290a2007-05-06 04:55:19 +0000395 Passes.run(*I);
Devang Patel72a4d2f2009-06-04 22:05:33 +0000396 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000397
Chris Lattner6dd290a2007-05-06 04:55:19 +0000398 Passes.doFinalization();
399 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000400
David Greenea31f96c2009-07-14 20:18:05 +0000401 Out->flush();
402
Chris Lattner6dd290a2007-05-06 04:55:19 +0000403 // Delete the ostream if it's not a stdout stream
David Greenea31f96c2009-07-14 20:18:05 +0000404 if (Out != &fouts()) delete Out;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000405
406 return 0;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000407}