blob: 9f7f0a43f1500d5d913068609e9f0fd579c6ded1 [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
Chris Lattner1a735402007-05-06 04:55:19 +000016#include "llvm/Bitcode/ReaderWriter.h"
Bill Wendling546d0fb2007-02-08 01:41:07 +000017#include "llvm/CodeGen/FileWriters.h"
Chris Lattnerd3a680a2006-08-01 22:34:35 +000018#include "llvm/CodeGen/LinkAllCodegenComponents.h"
Anton Korobeynikov2b110042008-08-17 14:33:01 +000019#include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000020#include "llvm/CodeGen/ObjectCodeEmitter.h"
Jim Laskeyb1e11802005-09-01 21:38:21 +000021#include "llvm/Target/SubtargetFeature.h"
Owen Anderson07000c62006-05-12 06:33:49 +000022#include "llvm/Target/TargetData.h"
Vikram S. Adve805eb962001-09-18 13:10:45 +000023#include "llvm/Target/TargetMachine.h"
Daniel Dunbar1d929212009-07-16 02:23:53 +000024#include "llvm/Target/TargetRegistry.h"
Chris Lattner65f1b892002-05-07 20:03:27 +000025#include "llvm/Transforms/Scalar.h"
Owen Anderson8b477ed2009-07-01 16:58:40 +000026#include "llvm/LLVMContext.h"
Chris Lattner46ac43c2001-09-07 21:26:31 +000027#include "llvm/Module.h"
Chris Lattner744879e2007-05-06 09:32:02 +000028#include "llvm/ModuleProvider.h"
Chris Lattnercd50d3f2002-01-31 00:46:45 +000029#include "llvm/PassManager.h"
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000030#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/Support/CommandLine.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000032#include "llvm/Support/FileUtilities.h"
David Greene71847812009-07-14 20:18:05 +000033#include "llvm/Support/FormattedStream.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000034#include "llvm/Support/ManagedStatic.h"
Chris Lattner1a735402007-05-06 04:55:19 +000035#include "llvm/Support/MemoryBuffer.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000036#include "llvm/Support/PluginLoader.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000037#include "llvm/Support/PrettyStackTrace.h"
Reid Spencer4418c2b2005-07-28 02:25:30 +000038#include "llvm/Analysis/Verifier.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000039#include "llvm/System/Signals.h"
Chris Lattner812125a2005-06-25 03:32:05 +000040#include "llvm/Config/config.h"
Reid Spenceraf303d52006-06-07 23:03:13 +000041#include "llvm/LinkAllVMCore.h"
Chris Lattner2deb58f2009-06-17 16:42:19 +000042#include "llvm/Target/TargetSelect.h"
Reid Spencer86f42bd2004-07-04 12:20:55 +000043#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000044using namespace llvm;
45
Vikram S. Adve7d0ba022002-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 Brukman3da94ae2005-04-22 00:00:37 +000049//
Chris Lattnerb5881f12003-04-25 05:26:11 +000050static cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000051InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000052
Chris Lattnerb5881f12003-04-25 05:26:11 +000053static cl::opt<std::string>
Chris Lattner5ff62e92002-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 Cheng712e80e2009-05-04 23:05:19 +000058// Determine optimization level.
Bill Wendling98a366d2009-04-29 23:29:43 +000059static cl::opt<char>
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000060OptLevel("O",
Evan Cheng712e80e2009-05-04 23:05:19 +000061 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
62 "(default = '-O2')"),
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000063 cl::Prefix,
64 cl::ZeroOrMore,
Bill Wendling98a366d2009-04-29 23:29:43 +000065 cl::init(' '));
Chris Lattner178e0c42005-11-08 02:12:17 +000066
Chris Lattnerf33b8662005-12-16 04:59:57 +000067static cl::opt<std::string>
Chris Lattnerbe193832005-12-16 05:19:55 +000068TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner178e0c42005-11-08 02:12:17 +000069
Daniel Dunbar1d929212009-07-16 02:23:53 +000070static cl::opt<std::string>
71MArch("march", cl::desc("Architecture to generate code for (see --version)"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000072
Jim Laskeyb1e11802005-09-01 21:38:21 +000073static cl::opt<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000074MCPU("mcpu",
Chris Lattner7b7593c2005-10-23 22:35:42 +000075 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskeyb1e11802005-09-01 21:38:21 +000076 cl::value_desc("cpu-name"),
77 cl::init(""));
78
79static cl::list<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000080MAttrs("mattr",
Jim Laskeyb1e11802005-09-01 21:38:21 +000081 cl::CommaSeparated,
Chris Lattner7b7593c2005-10-23 22:35:42 +000082 cl::desc("Target specific attributes (-mattr=help for details)"),
Chris Lattner20947252005-10-23 22:37:13 +000083 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskeyb1e11802005-09-01 21:38:21 +000084
Chris Lattner812125a2005-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 Brukman262b05f2008-12-31 17:39:58 +000089 clEnumValN(TargetMachine::AssemblyFile, "asm",
Dan Gohmanb8cab922008-10-14 20:25:08 +000090 "Emit an assembly ('.s') file"),
Misha Brukman262b05f2008-12-31 17:39:58 +000091 clEnumValN(TargetMachine::ObjectFile, "obj",
Dan Gohmanb8cab922008-10-14 20:25:08 +000092 "Emit a native object ('.o') file [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000093 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
Dan Gohmanb8cab922008-10-14 20:25:08 +000094 "Emit a native dynamic library ('.so') file"
Nate Begeman712b8352006-08-23 21:29:52 +000095 " [experimental]"),
Chris Lattner812125a2005-06-25 03:32:05 +000096 clEnumValEnd));
97
Reid Spencer4418c2b2005-07-28 02:25:30 +000098cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
Jeff Cohend29b6aa2005-07-30 18:33:25 +000099 cl::desc("Do not verify input module"));
Reid Spencer4418c2b2005-07-28 02:25:30 +0000100
Chris Lattner812125a2005-06-25 03:32:05 +0000101
Devang Pateld18e31a2009-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 Patel578efa92009-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 Lattner812125a2005-06-25 03:32:05 +0000112// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerb5881f12003-04-25 05:26:11 +0000113static inline std::string
Chris Lattnere45110e2004-07-11 04:03:24 +0000114GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerb5881f12003-04-25 05:26:11 +0000115 std::string IFN = InputFilename;
116 std::string outputFilename;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000117 int Len = IFN.length();
John Criswellb5d09bf2003-08-28 21:42:29 +0000118 if ((Len > 2) &&
119 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerb5881f12003-04-25 05:26:11 +0000120 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000121 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +0000122 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000123 }
124 return outputFilename;
125}
126
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000127static formatted_raw_ostream *GetOutputStream(const char *TargetName,
128 const char *ProgName) {
Chris Lattner1911fd42006-09-04 04:14:57 +0000129 if (OutputFilename != "") {
130 if (OutputFilename == "-")
David Greene71847812009-07-14 20:18:05 +0000131 return &fouts();
Chris Lattner1911fd42006-09-04 04:14:57 +0000132
Chris Lattner1911fd42006-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 Andersoncb371882008-08-21 00:14:44 +0000137 std::string error;
David Greene71847812009-07-14 20:18:05 +0000138 raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(),
Dan Gohmana1bdced2009-07-15 17:29:42 +0000139 /*Binary=*/true, Force, error);
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000140 if (!error.empty()) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000141 errs() << error << '\n';
Dan Gohmana1bdced2009-07-15 17:29:42 +0000142 if (!Force)
143 errs() << "Use -f command line argument to force output\n";
144 delete FDOut;
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000145 return 0;
146 }
Dan Gohmana1bdced2009-07-15 17:29:42 +0000147 formatted_raw_ostream *Out =
148 new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000149
150 return Out;
Chris Lattner1911fd42006-09-04 04:14:57 +0000151 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000152
Chris Lattner1911fd42006-09-04 04:14:57 +0000153 if (InputFilename == "-") {
154 OutputFilename = "-";
David Greene71847812009-07-14 20:18:05 +0000155 return &fouts();
Chris Lattner1911fd42006-09-04 04:14:57 +0000156 }
157
158 OutputFilename = GetFileNameRoot(InputFilename);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000159
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000160 bool Binary = false;
Chris Lattner1911fd42006-09-04 04:14:57 +0000161 switch (FileType) {
162 case TargetMachine::AssemblyFile:
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000163 if (TargetName[0] == 'c') {
164 if (TargetName[1] == 0)
Anton Korobeynikov50276522008-04-23 22:29:24 +0000165 OutputFilename += ".cbe.c";
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000166 else if (TargetName[1] == 'p' && TargetName[2] == 'p')
Anton Korobeynikov50276522008-04-23 22:29:24 +0000167 OutputFilename += ".cpp";
168 else
169 OutputFilename += ".s";
170 } else
Chris Lattner1911fd42006-09-04 04:14:57 +0000171 OutputFilename += ".s";
Chris Lattner1911fd42006-09-04 04:14:57 +0000172 break;
173 case TargetMachine::ObjectFile:
174 OutputFilename += ".o";
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000175 Binary = true;
Chris Lattner1911fd42006-09-04 04:14:57 +0000176 break;
177 case TargetMachine::DynamicLibrary:
178 OutputFilename += LTDL_SHLIB_EXT;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000179 Binary = true;
Chris Lattner1911fd42006-09-04 04:14:57 +0000180 break;
181 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000182
Chris Lattner1911fd42006-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 Glushenkov5c1799b2009-01-16 06:53:46 +0000186
Owen Andersoncb371882008-08-21 00:14:44 +0000187 std::string error;
David Greene71847812009-07-14 20:18:05 +0000188 raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(),
Dan Gohmana1bdced2009-07-15 17:29:42 +0000189 Binary, Force, error);
Owen Andersoncb371882008-08-21 00:14:44 +0000190 if (!error.empty()) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000191 errs() << error << '\n';
Dan Gohmana1bdced2009-07-15 17:29:42 +0000192 if (!Force)
193 errs() << "Use -f command line argument to force output\n";
194 delete FDOut;
Chris Lattner1911fd42006-09-04 04:14:57 +0000195 return 0;
196 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000197
Dan Gohmana1bdced2009-07-15 17:29:42 +0000198 formatted_raw_ostream *Out =
199 new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
200
Chris Lattner1911fd42006-09-04 04:14:57 +0000201 return Out;
202}
Vikram S. Adve805eb962001-09-18 13:10:45 +0000203
Chris Lattner5b836c42003-06-20 15:49:04 +0000204// main - Entry point for the llc compiler.
205//
206int main(int argc, char **argv) {
Chris Lattner1a735402007-05-06 04:55:19 +0000207 sys::PrintStackTraceOnErrorSignal();
Chris Lattnercc14d252009-03-06 05:34:10 +0000208 PrettyStackTraceProgram X(argc, argv);
Owen Anderson0d7c6952009-07-15 22:16:10 +0000209 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000210 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner364d1202004-02-19 20:32:39 +0000211
Daniel Dunbar494d6632009-07-16 02:04:54 +0000212 // Initialize targets first.
Chris Lattner2deb58f2009-06-17 16:42:19 +0000213 InitializeAllTargets();
214 InitializeAllAsmPrinters();
Daniel Dunbar494d6632009-07-16 02:04:54 +0000215
216 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Chris Lattner2deb58f2009-06-17 16:42:19 +0000217
Chris Lattner1a735402007-05-06 04:55:19 +0000218 // Load the module to be compiled...
219 std::string ErrorMessage;
220 std::auto_ptr<Module> M;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000221
Chris Lattner744879e2007-05-06 09:32:02 +0000222 std::auto_ptr<MemoryBuffer> Buffer(
Chris Lattner065344d2007-05-06 23:45:49 +0000223 MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage));
Chris Lattner744879e2007-05-06 09:32:02 +0000224 if (Buffer.get())
Owen Anderson31895e72009-07-01 21:22:36 +0000225 M.reset(ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage));
Chris Lattner1a735402007-05-06 04:55:19 +0000226 if (M.get() == 0) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000227 errs() << argv[0] << ": bitcode didn't read correctly.\n";
228 errs() << "Reason: " << ErrorMessage << "\n";
Chris Lattner1a735402007-05-06 04:55:19 +0000229 return 1;
230 }
231 Module &mod = *M.get();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000232
Chris Lattner1a735402007-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 Glushenkov5c1799b2009-01-16 06:53:46 +0000236
Chris Lattner1a735402007-05-06 04:55:19 +0000237 // Allocate target machine. First, check whether the user has
238 // explicitly specified an architecture to compile for.
Daniel Dunbar1d929212009-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 Dunbar51b198a2009-07-15 20:24:03 +0000253 } else {
Chris Lattner1a735402007-05-06 04:55:19 +0000254 std::string Err;
Daniel Dunbara5881e32009-07-26 02:12:58 +0000255 TheTarget = TargetRegistry::lookupTarget(mod.getTargetTriple(),
256 /*FallbackToHost=*/true,
257 /*RequireJIT=*/false,
258 Err);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000259 if (TheTarget == 0) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000260 errs() << argv[0] << ": error auto-selecting target for module '"
261 << Err << "'. Please use the -march option to explicitly "
262 << "pick a target.\n";
Chris Lattnerbb433502003-08-24 14:02:14 +0000263 return 1;
Chris Lattnere45110e2004-07-11 04:03:24 +0000264 }
Chris Lattner63342052002-10-29 21:12:46 +0000265 }
Chris Lattner1a735402007-05-06 04:55:19 +0000266
267 // Package up features to be passed to target/subtarget
268 std::string FeaturesStr;
269 if (MCPU.size() || MAttrs.size()) {
270 SubtargetFeatures Features;
271 Features.setCPU(MCPU);
272 for (unsigned i = 0; i != MAttrs.size(); ++i)
273 Features.AddFeature(MAttrs[i]);
274 FeaturesStr = Features.getString();
275 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000276
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000277 std::auto_ptr<TargetMachine>
278 target(TheTarget->createTargetMachine(mod, FeaturesStr));
Chris Lattner1a735402007-05-06 04:55:19 +0000279 assert(target.get() && "Could not allocate target machine!");
280 TargetMachine &Target = *target.get();
281
282 // Figure out where we are going to send the output...
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000283 formatted_raw_ostream *Out = GetOutputStream(TheTarget->getName(), argv[0]);
Chris Lattner1a735402007-05-06 04:55:19 +0000284 if (Out == 0) return 1;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000285
Evan Cheng712e80e2009-05-04 23:05:19 +0000286 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
Bill Wendling98a366d2009-04-29 23:29:43 +0000287 switch (OptLevel) {
288 default:
Dan Gohman65f57c22009-07-15 16:35:29 +0000289 errs() << argv[0] << ": invalid optimization level.\n";
Bill Wendling8dc85dd2009-04-29 23:46:43 +0000290 return 1;
Bill Wendling98a366d2009-04-29 23:29:43 +0000291 case ' ': break;
292 case '0': OLvl = CodeGenOpt::None; break;
Bill Wendling581b9342009-04-30 00:57:51 +0000293 case '1':
294 case '2': OLvl = CodeGenOpt::Default; break;
Bill Wendling98a366d2009-04-29 23:29:43 +0000295 case '3': OLvl = CodeGenOpt::Aggressive; break;
Bill Wendling98a366d2009-04-29 23:29:43 +0000296 }
297
Chris Lattner1a735402007-05-06 04:55:19 +0000298 // If this target requires addPassesToEmitWholeFile, do it now. This is
299 // used by strange things like the C backend.
300 if (Target.WantsWholeFile()) {
301 PassManager PM;
302 PM.add(new TargetData(*Target.getTargetData()));
303 if (!NoVerify)
304 PM.add(createVerifierPass());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000305
Chris Lattner1a735402007-05-06 04:55:19 +0000306 // Ask the target to add backend passes as necessary.
Bill Wendling98a366d2009-04-29 23:29:43 +0000307 if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, OLvl)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000308 errs() << argv[0] << ": target does not support generation of this"
309 << " file type!\n";
David Greene71847812009-07-14 20:18:05 +0000310 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000311 // And the Out file is empty and useless, so remove it now.
312 sys::Path(OutputFilename).eraseFromDisk();
313 return 1;
314 }
315 PM.run(mod);
316 } else {
317 // Build up all of the passes that we want to do to the module.
Dan Gohman33ef2bb2008-04-16 15:56:26 +0000318 ExistingModuleProvider Provider(M.release());
319 FunctionPassManager Passes(&Provider);
Chris Lattner1a735402007-05-06 04:55:19 +0000320 Passes.add(new TargetData(*Target.getTargetData()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000321
Chris Lattner1a735402007-05-06 04:55:19 +0000322#ifndef NDEBUG
323 if (!NoVerify)
324 Passes.add(createVerifierPass());
325#endif
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000326
Chris Lattner1a735402007-05-06 04:55:19 +0000327 // Ask the target to add backend passes as necessary.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000328 ObjectCodeEmitter *OCE = 0;
Chris Lattner1a735402007-05-06 04:55:19 +0000329
Evan Cheng23120ba2009-03-25 01:48:21 +0000330 // Override default to generate verbose assembly.
331 Target.setAsmVerbosityDefault(true);
332
Bill Wendling98a366d2009-04-29 23:29:43 +0000333 switch (Target.addPassesToEmitFile(Passes, *Out, FileType, OLvl)) {
Chris Lattner1a735402007-05-06 04:55:19 +0000334 default:
335 assert(0 && "Invalid file model!");
336 return 1;
337 case FileModel::Error:
Dan Gohman65f57c22009-07-15 16:35:29 +0000338 errs() << argv[0] << ": target does not support generation of this"
339 << " file type!\n";
David Greene71847812009-07-14 20:18:05 +0000340 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000341 // And the Out file is empty and useless, so remove it now.
342 sys::Path(OutputFilename).eraseFromDisk();
343 return 1;
344 case FileModel::AsmFile:
345 break;
346 case FileModel::MachOFile:
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000347 OCE = AddMachOWriter(Passes, *Out, Target);
Chris Lattner1a735402007-05-06 04:55:19 +0000348 break;
349 case FileModel::ElfFile:
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000350 OCE = AddELFWriter(Passes, *Out, Target);
Chris Lattner1a735402007-05-06 04:55:19 +0000351 break;
352 }
353
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000354 if (Target.addPassesToEmitFileFinish(Passes, OCE, OLvl)) {
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 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000362
Chris Lattner1a735402007-05-06 04:55:19 +0000363 Passes.doInitialization();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000364
Chris Lattner1a735402007-05-06 04:55:19 +0000365 // Run our queue of passes all at once now, efficiently.
366 // TODO: this could lazily stream functions out of the module.
367 for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I)
Devang Pateld18e31a2009-06-04 22:05:33 +0000368 if (!I->isDeclaration()) {
369 if (DisableRedZone)
370 I->addFnAttr(Attribute::NoRedZone);
Devang Patel578efa92009-06-05 21:57:13 +0000371 if (NoImplicitFloats)
372 I->addFnAttr(Attribute::NoImplicitFloat);
Chris Lattner1a735402007-05-06 04:55:19 +0000373 Passes.run(*I);
Devang Pateld18e31a2009-06-04 22:05:33 +0000374 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000375
Chris Lattner1a735402007-05-06 04:55:19 +0000376 Passes.doFinalization();
377 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000378
David Greene71847812009-07-14 20:18:05 +0000379 Out->flush();
380
Chris Lattner1a735402007-05-06 04:55:19 +0000381 // Delete the ostream if it's not a stdout stream
David Greene71847812009-07-14 20:18:05 +0000382 if (Out != &fouts()) delete Out;
Chris Lattner1a735402007-05-06 04:55:19 +0000383
384 return 0;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000385}