blob: 6d662680bfb69722b84e253c58665c06e8a77df1 [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
Chris Lattner6dd290a2007-05-06 04:55:19 +000016#include "llvm/Bitcode/ReaderWriter.h"
Bill Wendlingfe5ee142007-02-08 01:41:07 +000017#include "llvm/CodeGen/FileWriters.h"
Chris Lattner5af6a3f2006-08-01 22:34:35 +000018#include "llvm/CodeGen/LinkAllCodegenComponents.h"
Anton Korobeynikove0c83e42008-08-17 14:33:01 +000019#include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +000020#include "llvm/CodeGen/ObjectCodeEmitter.h"
Jim Laskey19058c32005-09-01 21:38:21 +000021#include "llvm/Target/SubtargetFeature.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000022#include "llvm/Target/TargetData.h"
Vikram S. Adve9d409352001-09-18 13:10:45 +000023#include "llvm/Target/TargetMachine.h"
Daniel Dunbard3706452009-07-16 02:23:53 +000024#include "llvm/Target/TargetRegistry.h"
Chris Lattner89a20ef2002-05-07 20:03:27 +000025#include "llvm/Transforms/Scalar.h"
Owen Anderson6773d382009-07-01 16:58:40 +000026#include "llvm/LLVMContext.h"
Chris Lattnered226062001-09-07 21:26:31 +000027#include "llvm/Module.h"
Chris Lattnerf5599ef2007-05-06 09:32:02 +000028#include "llvm/ModuleProvider.h"
Chris Lattner7139f282002-01-31 00:46:45 +000029#include "llvm/PassManager.h"
Vikram S. Adveeb818692002-09-16 16:35:34 +000030#include "llvm/Pass.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000031#include "llvm/Support/CommandLine.h"
Mikhail Glushenkov6e8d8142009-01-16 07:02:28 +000032#include "llvm/Support/FileUtilities.h"
David Greenea31f96c2009-07-14 20:18:05 +000033#include "llvm/Support/FormattedStream.h"
Chris Lattner76d46322006-12-06 01:18:01 +000034#include "llvm/Support/ManagedStatic.h"
Chris Lattner6dd290a2007-05-06 04:55:19 +000035#include "llvm/Support/MemoryBuffer.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000036#include "llvm/Support/PluginLoader.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000037#include "llvm/Support/PrettyStackTrace.h"
Reid Spencer8e3830d2005-07-28 02:25:30 +000038#include "llvm/Analysis/Verifier.h"
Chris Lattner278f5152004-05-27 05:41:36 +000039#include "llvm/System/Signals.h"
Chris Lattner06fcc4c2005-06-25 03:32:05 +000040#include "llvm/Config/config.h"
Reid Spencer5113dc52006-06-07 23:03:13 +000041#include "llvm/LinkAllVMCore.h"
Chris Lattner5dcc4f62009-06-17 16:42:19 +000042#include "llvm/Target/TargetSelect.h"
Reid Spencerf0ebb252004-07-04 12:20:55 +000043#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000044using namespace llvm;
45
Vikram S. Adveeb818692002-09-16 16:35:34 +000046// General options for llc. Other pass-specific options are specified
47// within the corresponding llc passes, and target-specific options
48// and back-end code generation options are specified with the target machine.
Misha Brukman650ba8e2005-04-22 00:00:37 +000049//
Chris Lattnerd64b2de2003-04-25 05:26:11 +000050static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000051InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000052
Chris Lattnerd64b2de2003-04-25 05:26:11 +000053static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000054OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
55
56static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
57
Evan Cheng09bd0b12009-05-04 23:05:19 +000058// Determine optimization level.
Bill Wendling026e5d72009-04-29 23:29:43 +000059static cl::opt<char>
Bill Wendling084669a2009-04-29 00:15:41 +000060OptLevel("O",
Evan Cheng09bd0b12009-05-04 23:05:19 +000061 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
62 "(default = '-O2')"),
Bill Wendling084669a2009-04-29 00:15:41 +000063 cl::Prefix,
64 cl::ZeroOrMore,
Bill Wendling026e5d72009-04-29 23:29:43 +000065 cl::init(' '));
Chris Lattner731055e2005-11-08 02:12:17 +000066
Chris Lattnere568b882005-12-16 04:59:57 +000067static cl::opt<std::string>
Chris Lattner08a04cb2005-12-16 05:19:55 +000068TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner731055e2005-11-08 02:12:17 +000069
Daniel Dunbard3706452009-07-16 02:23:53 +000070static cl::opt<std::string>
71MArch("march", cl::desc("Architecture to generate code for (see --version)"));
Misha Brukman650ba8e2005-04-22 00:00:37 +000072
Jim Laskey19058c32005-09-01 21:38:21 +000073static cl::opt<std::string>
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000074MCPU("mcpu",
Chris Lattner0b2bf4c2005-10-23 22:35:42 +000075 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskey19058c32005-09-01 21:38:21 +000076 cl::value_desc("cpu-name"),
77 cl::init(""));
78
79static cl::list<std::string>
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000080MAttrs("mattr",
Jim Laskey19058c32005-09-01 21:38:21 +000081 cl::CommaSeparated,
Chris Lattner0b2bf4c2005-10-23 22:35:42 +000082 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner41548482005-10-23 22:37:13 +000083 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskey19058c32005-09-01 21:38:21 +000084
Chris Lattner06fcc4c2005-06-25 03:32:05 +000085cl::opt<TargetMachine::CodeGenFileType>
86FileType("filetype", cl::init(TargetMachine::AssemblyFile),
87 cl::desc("Choose a file type (not all types are supported by all targets):"),
88 cl::values(
Misha Brukmand07751e2008-12-31 17:39:58 +000089 clEnumValN(TargetMachine::AssemblyFile, "asm",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000090 "Emit an assembly ('.s') file"),
Misha Brukmand07751e2008-12-31 17:39:58 +000091 clEnumValN(TargetMachine::ObjectFile, "obj",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000092 "Emit a native object ('.o') file [experimental]"),
Chris Lattner06fcc4c2005-06-25 03:32:05 +000093 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000094 "Emit a native dynamic library ('.so') file"
Nate Begemana147cbf2006-08-23 21:29:52 +000095 " [experimental]"),
Chris Lattner06fcc4c2005-06-25 03:32:05 +000096 clEnumValEnd));
97
Reid Spencer8e3830d2005-07-28 02:25:30 +000098cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohen546fd592005-07-30 18:33:25 +000099 cl::desc("Do not verify input module"));
Reid Spencer8e3830d2005-07-28 02:25:30 +0000100
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000101
Devang Patel72a4d2f2009-06-04 22:05:33 +0000102static cl::opt<bool>
103DisableRedZone("disable-red-zone",
104 cl::desc("Do not emit code that uses the red zone."),
105 cl::init(false));
106
Devang Pateld1c7d342009-06-05 21:57:13 +0000107static cl::opt<bool>
108NoImplicitFloats("no-implicit-float",
109 cl::desc("Don't generate implicit floating point instructions (x86-only)"),
110 cl::init(false));
111
Chris Lattner06fcc4c2005-06-25 03:32:05 +0000112// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000113static inline std::string
Chris Lattner6142ca82004-07-11 04:03:24 +0000114GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000115 std::string IFN = InputFilename;
116 std::string outputFilename;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000117 int Len = IFN.length();
John Criswella289abf2003-08-28 21:42:29 +0000118 if ((Len > 2) &&
119 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerd64b2de2003-04-25 05:26:11 +0000120 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000121 } else {
Chris Lattner97fd6c42001-10-15 17:30:47 +0000122 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000123 }
124 return outputFilename;
125}
126
Daniel Dunbare8338102009-07-15 20:24:03 +0000127static formatted_raw_ostream *GetOutputStream(const char *TargetName,
128 const char *ProgName) {
Chris Lattner12e97302006-09-04 04:14:57 +0000129 if (OutputFilename != "") {
130 if (OutputFilename == "-")
David Greenea31f96c2009-07-14 20:18:05 +0000131 return &fouts();
Chris Lattner12e97302006-09-04 04:14:57 +0000132
Chris Lattner12e97302006-09-04 04:14:57 +0000133 // Make sure that the Out file gets unlinked from the disk if we get a
134 // SIGINT
135 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
136
Owen Anderson93719642008-08-21 00:14:44 +0000137 std::string error;
David Greenea31f96c2009-07-14 20:18:05 +0000138 raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(),
Dan Gohman607818a2009-07-15 17:29:42 +0000139 /*Binary=*/true, Force, error);
Dan Gohmanf3e13bb2008-08-21 15:33:45 +0000140 if (!error.empty()) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000141 errs() << error << '\n';
Dan Gohman607818a2009-07-15 17:29:42 +0000142 if (!Force)
143 errs() << "Use -f command line argument to force output\n";
144 delete FDOut;
Dan Gohmanf3e13bb2008-08-21 15:33:45 +0000145 return 0;
146 }
Dan Gohman607818a2009-07-15 17:29:42 +0000147 formatted_raw_ostream *Out =
148 new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
Dan Gohmanf3e13bb2008-08-21 15:33:45 +0000149
150 return Out;
Chris Lattner12e97302006-09-04 04:14:57 +0000151 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000152
Chris Lattner12e97302006-09-04 04:14:57 +0000153 if (InputFilename == "-") {
154 OutputFilename = "-";
David Greenea31f96c2009-07-14 20:18:05 +0000155 return &fouts();
Chris Lattner12e97302006-09-04 04:14:57 +0000156 }
157
158 OutputFilename = GetFileNameRoot(InputFilename);
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000159
Daniel Dunbared90e702008-11-13 05:01:07 +0000160 bool Binary = false;
Chris Lattner12e97302006-09-04 04:14:57 +0000161 switch (FileType) {
162 case TargetMachine::AssemblyFile:
Daniel Dunbare8338102009-07-15 20:24:03 +0000163 if (TargetName[0] == 'c') {
164 if (TargetName[1] == 0)
Anton Korobeynikov78695032008-04-23 22:29:24 +0000165 OutputFilename += ".cbe.c";
Daniel Dunbare8338102009-07-15 20:24:03 +0000166 else if (TargetName[1] == 'p' && TargetName[2] == 'p')
Anton Korobeynikov78695032008-04-23 22:29:24 +0000167 OutputFilename += ".cpp";
168 else
169 OutputFilename += ".s";
170 } else
Chris Lattner12e97302006-09-04 04:14:57 +0000171 OutputFilename += ".s";
Chris Lattner12e97302006-09-04 04:14:57 +0000172 break;
173 case TargetMachine::ObjectFile:
174 OutputFilename += ".o";
Daniel Dunbared90e702008-11-13 05:01:07 +0000175 Binary = true;
Chris Lattner12e97302006-09-04 04:14:57 +0000176 break;
177 case TargetMachine::DynamicLibrary:
178 OutputFilename += LTDL_SHLIB_EXT;
Daniel Dunbared90e702008-11-13 05:01:07 +0000179 Binary = true;
Chris Lattner12e97302006-09-04 04:14:57 +0000180 break;
181 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000182
Chris Lattner12e97302006-09-04 04:14:57 +0000183 // Make sure that the Out file gets unlinked from the disk if we get a
184 // SIGINT
185 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000186
Owen Anderson93719642008-08-21 00:14:44 +0000187 std::string error;
David Greenea31f96c2009-07-14 20:18:05 +0000188 raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(),
Dan Gohman607818a2009-07-15 17:29:42 +0000189 Binary, Force, error);
Owen Anderson93719642008-08-21 00:14:44 +0000190 if (!error.empty()) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000191 errs() << error << '\n';
Dan Gohman607818a2009-07-15 17:29:42 +0000192 if (!Force)
193 errs() << "Use -f command line argument to force output\n";
194 delete FDOut;
Chris Lattner12e97302006-09-04 04:14:57 +0000195 return 0;
196 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000197
Dan Gohman607818a2009-07-15 17:29:42 +0000198 formatted_raw_ostream *Out =
199 new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
200
Chris Lattner12e97302006-09-04 04:14:57 +0000201 return Out;
202}
Vikram S. Adve9d409352001-09-18 13:10:45 +0000203
Chris Lattner67e08422003-06-20 15:49:04 +0000204// main - Entry point for the llc compiler.
205//
206int main(int argc, char **argv) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000207 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000208 PrettyStackTraceProgram X(argc, argv);
Owen Anderson19251ec2009-07-15 22:16:10 +0000209 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000210 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner69e896b2004-02-19 20:32:39 +0000211
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000212 // Initialize targets first.
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000213 InitializeAllTargets();
214 InitializeAllAsmPrinters();
Daniel Dunbar3d92d932009-07-16 02:04:54 +0000215
216 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000217
Chris Lattner6dd290a2007-05-06 04:55:19 +0000218 // Load the module to be compiled...
219 std::string ErrorMessage;
220 std::auto_ptr<Module> M;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000221
Chris Lattnerf5599ef2007-05-06 09:32:02 +0000222 std::auto_ptr<MemoryBuffer> Buffer(
Chris Lattner9e9a34c2007-05-06 23:45:49 +0000223 MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage));
Chris Lattnerf5599ef2007-05-06 09:32:02 +0000224 if (Buffer.get())
Owen Anderson1cf085d2009-07-01 21:22:36 +0000225 M.reset(ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000226 if (M.get() == 0) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000227 errs() << argv[0] << ": bitcode didn't read correctly.\n";
228 errs() << "Reason: " << ErrorMessage << "\n";
Chris Lattner6dd290a2007-05-06 04:55:19 +0000229 return 1;
230 }
231 Module &mod = *M.get();
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000232
Chris Lattner6dd290a2007-05-06 04:55:19 +0000233 // If we are supposed to override the target triple, do so now.
234 if (!TargetTriple.empty())
235 mod.setTargetTriple(TargetTriple);
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000236
Chris Lattner6dd290a2007-05-06 04:55:19 +0000237 // Allocate target machine. First, check whether the user has
238 // explicitly specified an architecture to compile for.
Daniel Dunbard3706452009-07-16 02:23:53 +0000239 const Target *TheTarget = 0;
240 if (!MArch.empty()) {
241 for (TargetRegistry::iterator it = TargetRegistry::begin(),
242 ie = TargetRegistry::end(); it != ie; ++it) {
243 if (MArch == it->getName()) {
244 TheTarget = &*it;
245 break;
246 }
247 }
248
249 if (!TheTarget) {
250 errs() << argv[0] << ": error: invalid target '" << MArch << "'.\n";
251 return 1;
252 }
Daniel Dunbare8338102009-07-15 20:24:03 +0000253 } else {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000254 std::string Err;
Daniel Dunbare8338102009-07-15 20:24:03 +0000255 TheTarget = TargetRegistry::getClosestStaticTargetForModule(mod, Err);
256 if (TheTarget == 0) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000257 errs() << argv[0] << ": error auto-selecting target for module '"
258 << Err << "'. Please use the -march option to explicitly "
259 << "pick a target.\n";
Chris Lattnerb1492402003-08-24 14:02:14 +0000260 return 1;
Chris Lattner6142ca82004-07-11 04:03:24 +0000261 }
Chris Lattner5667f0e2002-10-29 21:12:46 +0000262 }
Chris Lattner6dd290a2007-05-06 04:55:19 +0000263
264 // Package up features to be passed to target/subtarget
265 std::string FeaturesStr;
266 if (MCPU.size() || MAttrs.size()) {
267 SubtargetFeatures Features;
268 Features.setCPU(MCPU);
269 for (unsigned i = 0; i != MAttrs.size(); ++i)
270 Features.AddFeature(MAttrs[i]);
271 FeaturesStr = Features.getString();
272 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000273
Daniel Dunbare8338102009-07-15 20:24:03 +0000274 std::auto_ptr<TargetMachine>
275 target(TheTarget->createTargetMachine(mod, FeaturesStr));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000276 assert(target.get() && "Could not allocate target machine!");
277 TargetMachine &Target = *target.get();
278
279 // Figure out where we are going to send the output...
Daniel Dunbare8338102009-07-15 20:24:03 +0000280 formatted_raw_ostream *Out = GetOutputStream(TheTarget->getName(), argv[0]);
Chris Lattner6dd290a2007-05-06 04:55:19 +0000281 if (Out == 0) return 1;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000282
Evan Cheng09bd0b12009-05-04 23:05:19 +0000283 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
Bill Wendling026e5d72009-04-29 23:29:43 +0000284 switch (OptLevel) {
285 default:
Dan Gohmand8db3762009-07-15 16:35:29 +0000286 errs() << argv[0] << ": invalid optimization level.\n";
Bill Wendlingdad99192009-04-29 23:46:43 +0000287 return 1;
Bill Wendling026e5d72009-04-29 23:29:43 +0000288 case ' ': break;
289 case '0': OLvl = CodeGenOpt::None; break;
Bill Wendlingaeaf5a52009-04-30 00:57:51 +0000290 case '1':
291 case '2': OLvl = CodeGenOpt::Default; break;
Bill Wendling026e5d72009-04-29 23:29:43 +0000292 case '3': OLvl = CodeGenOpt::Aggressive; break;
Bill Wendling026e5d72009-04-29 23:29:43 +0000293 }
294
Chris Lattner6dd290a2007-05-06 04:55:19 +0000295 // If this target requires addPassesToEmitWholeFile, do it now. This is
296 // used by strange things like the C backend.
297 if (Target.WantsWholeFile()) {
298 PassManager PM;
299 PM.add(new TargetData(*Target.getTargetData()));
300 if (!NoVerify)
301 PM.add(createVerifierPass());
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000302
Chris Lattner6dd290a2007-05-06 04:55:19 +0000303 // Ask the target to add backend passes as necessary.
Bill Wendling026e5d72009-04-29 23:29:43 +0000304 if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, OLvl)) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000305 errs() << argv[0] << ": target does not support generation of this"
306 << " file type!\n";
David Greenea31f96c2009-07-14 20:18:05 +0000307 if (Out != &fouts()) delete Out;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000308 // And the Out file is empty and useless, so remove it now.
309 sys::Path(OutputFilename).eraseFromDisk();
310 return 1;
311 }
312 PM.run(mod);
313 } else {
314 // Build up all of the passes that we want to do to the module.
Dan Gohmanbd2613d2008-04-16 15:56:26 +0000315 ExistingModuleProvider Provider(M.release());
316 FunctionPassManager Passes(&Provider);
Chris Lattner6dd290a2007-05-06 04:55:19 +0000317 Passes.add(new TargetData(*Target.getTargetData()));
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000318
Chris Lattner6dd290a2007-05-06 04:55:19 +0000319#ifndef NDEBUG
320 if (!NoVerify)
321 Passes.add(createVerifierPass());
322#endif
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000323
Chris Lattner6dd290a2007-05-06 04:55:19 +0000324 // Ask the target to add backend passes as necessary.
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000325 ObjectCodeEmitter *OCE = 0;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000326
Evan Cheng499d5f32009-03-25 01:48:21 +0000327 // Override default to generate verbose assembly.
328 Target.setAsmVerbosityDefault(true);
329
Bill Wendling026e5d72009-04-29 23:29:43 +0000330 switch (Target.addPassesToEmitFile(Passes, *Out, FileType, OLvl)) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000331 default:
332 assert(0 && "Invalid file model!");
333 return 1;
334 case FileModel::Error:
Dan Gohmand8db3762009-07-15 16:35:29 +0000335 errs() << argv[0] << ": target does not support generation of this"
336 << " file type!\n";
David Greenea31f96c2009-07-14 20:18:05 +0000337 if (Out != &fouts()) delete Out;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000338 // And the Out file is empty and useless, so remove it now.
339 sys::Path(OutputFilename).eraseFromDisk();
340 return 1;
341 case FileModel::AsmFile:
342 break;
343 case FileModel::MachOFile:
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000344 OCE = AddMachOWriter(Passes, *Out, Target);
Chris Lattner6dd290a2007-05-06 04:55:19 +0000345 break;
346 case FileModel::ElfFile:
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000347 OCE = AddELFWriter(Passes, *Out, Target);
Chris Lattner6dd290a2007-05-06 04:55:19 +0000348 break;
349 }
350
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000351 if (Target.addPassesToEmitFileFinish(Passes, OCE, OLvl)) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000352 errs() << argv[0] << ": target does not support generation of this"
353 << " file type!\n";
David Greenea31f96c2009-07-14 20:18:05 +0000354 if (Out != &fouts()) delete Out;
Chris Lattner6dd290a2007-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 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000359
Chris Lattner6dd290a2007-05-06 04:55:19 +0000360 Passes.doInitialization();
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000361
Chris Lattner6dd290a2007-05-06 04:55:19 +0000362 // Run our queue of passes all at once now, efficiently.
363 // TODO: this could lazily stream functions out of the module.
364 for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I)
Devang Patel72a4d2f2009-06-04 22:05:33 +0000365 if (!I->isDeclaration()) {
366 if (DisableRedZone)
367 I->addFnAttr(Attribute::NoRedZone);
Devang Pateld1c7d342009-06-05 21:57:13 +0000368 if (NoImplicitFloats)
369 I->addFnAttr(Attribute::NoImplicitFloat);
Chris Lattner6dd290a2007-05-06 04:55:19 +0000370 Passes.run(*I);
Devang Patel72a4d2f2009-06-04 22:05:33 +0000371 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000372
Chris Lattner6dd290a2007-05-06 04:55:19 +0000373 Passes.doFinalization();
374 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000375
David Greenea31f96c2009-07-14 20:18:05 +0000376 Out->flush();
377
Chris Lattner6dd290a2007-05-06 04:55:19 +0000378 // Delete the ostream if it's not a stdout stream
David Greenea31f96c2009-07-14 20:18:05 +0000379 if (Out != &fouts()) delete Out;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000380
381 return 0;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000382}