blob: 47acb324fb9425af5a5a7a3dc71810eb4c120cc1 [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"
Chris Lattner6142ca82004-07-11 04:03:24 +000024#include "llvm/Target/TargetMachineRegistry.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"
Mikhail Glushenkov6e8d8142009-01-16 07:02:28 +000038#include "llvm/Support/RegistryParser.h"
Reid Spencer8e3830d2005-07-28 02:25:30 +000039#include "llvm/Analysis/Verifier.h"
Chris Lattner278f5152004-05-27 05:41:36 +000040#include "llvm/System/Signals.h"
Chris Lattner06fcc4c2005-06-25 03:32:05 +000041#include "llvm/Config/config.h"
Reid Spencer5113dc52006-06-07 23:03:13 +000042#include "llvm/LinkAllVMCore.h"
Chris Lattner5dcc4f62009-06-17 16:42:19 +000043#include "llvm/Target/TargetSelect.h"
Reid Spencerf0ebb252004-07-04 12:20:55 +000044#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000045using namespace llvm;
46
Vikram S. Adveeb818692002-09-16 16:35:34 +000047// General options for llc. Other pass-specific options are specified
48// within the corresponding llc passes, and target-specific options
49// and back-end code generation options are specified with the target machine.
Misha Brukman650ba8e2005-04-22 00:00:37 +000050//
Chris Lattnerd64b2de2003-04-25 05:26:11 +000051static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000052InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000053
Chris Lattnerd64b2de2003-04-25 05:26:11 +000054static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000055OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
56
57static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
58
Evan Cheng09bd0b12009-05-04 23:05:19 +000059// Determine optimization level.
Bill Wendling026e5d72009-04-29 23:29:43 +000060static cl::opt<char>
Bill Wendling084669a2009-04-29 00:15:41 +000061OptLevel("O",
Evan Cheng09bd0b12009-05-04 23:05:19 +000062 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
63 "(default = '-O2')"),
Bill Wendling084669a2009-04-29 00:15:41 +000064 cl::Prefix,
65 cl::ZeroOrMore,
Bill Wendling026e5d72009-04-29 23:29:43 +000066 cl::init(' '));
Chris Lattner731055e2005-11-08 02:12:17 +000067
Chris Lattnere568b882005-12-16 04:59:57 +000068static cl::opt<std::string>
Chris Lattner08a04cb2005-12-16 05:19:55 +000069TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner731055e2005-11-08 02:12:17 +000070
Gordon Henriksenef5d08f2007-10-17 21:28:48 +000071static cl::opt<const TargetMachineRegistry::entry*, false,
Mikhail Glushenkov6e8d8142009-01-16 07:02:28 +000072 RegistryParser<TargetMachine> >
Chris Lattner09b0eb32005-06-25 03:00:34 +000073MArch("march", cl::desc("Architecture to generate code for:"));
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 Anderson6773d382009-07-01 16:58:40 +0000211 LLVMContext Context;
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000212 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
213 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
Chris Lattner69e896b2004-02-19 20:32:39 +0000214
Chris Lattner5dcc4f62009-06-17 16:42:19 +0000215 InitializeAllTargets();
216 InitializeAllAsmPrinters();
217
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 Dunbare8338102009-07-15 20:24:03 +0000239 const Target *TheTarget;
240 if (MArch) {
241 TheTarget = &MArch->TheTarget;
242 } else {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000243 std::string Err;
Daniel Dunbare8338102009-07-15 20:24:03 +0000244 TheTarget = TargetRegistry::getClosestStaticTargetForModule(mod, Err);
245 if (TheTarget == 0) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000246 errs() << argv[0] << ": error auto-selecting target for module '"
247 << Err << "'. Please use the -march option to explicitly "
248 << "pick a target.\n";
Chris Lattnerb1492402003-08-24 14:02:14 +0000249 return 1;
Chris Lattner6142ca82004-07-11 04:03:24 +0000250 }
Chris Lattner5667f0e2002-10-29 21:12:46 +0000251 }
Chris Lattner6dd290a2007-05-06 04:55:19 +0000252
253 // Package up features to be passed to target/subtarget
254 std::string FeaturesStr;
255 if (MCPU.size() || MAttrs.size()) {
256 SubtargetFeatures Features;
257 Features.setCPU(MCPU);
258 for (unsigned i = 0; i != MAttrs.size(); ++i)
259 Features.AddFeature(MAttrs[i]);
260 FeaturesStr = Features.getString();
261 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000262
Daniel Dunbare8338102009-07-15 20:24:03 +0000263 std::auto_ptr<TargetMachine>
264 target(TheTarget->createTargetMachine(mod, FeaturesStr));
Chris Lattner6dd290a2007-05-06 04:55:19 +0000265 assert(target.get() && "Could not allocate target machine!");
266 TargetMachine &Target = *target.get();
267
268 // Figure out where we are going to send the output...
Daniel Dunbare8338102009-07-15 20:24:03 +0000269 formatted_raw_ostream *Out = GetOutputStream(TheTarget->getName(), argv[0]);
Chris Lattner6dd290a2007-05-06 04:55:19 +0000270 if (Out == 0) return 1;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000271
Evan Cheng09bd0b12009-05-04 23:05:19 +0000272 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
Bill Wendling026e5d72009-04-29 23:29:43 +0000273 switch (OptLevel) {
274 default:
Dan Gohmand8db3762009-07-15 16:35:29 +0000275 errs() << argv[0] << ": invalid optimization level.\n";
Bill Wendlingdad99192009-04-29 23:46:43 +0000276 return 1;
Bill Wendling026e5d72009-04-29 23:29:43 +0000277 case ' ': break;
278 case '0': OLvl = CodeGenOpt::None; break;
Bill Wendlingaeaf5a52009-04-30 00:57:51 +0000279 case '1':
280 case '2': OLvl = CodeGenOpt::Default; break;
Bill Wendling026e5d72009-04-29 23:29:43 +0000281 case '3': OLvl = CodeGenOpt::Aggressive; break;
Bill Wendling026e5d72009-04-29 23:29:43 +0000282 }
283
Chris Lattner6dd290a2007-05-06 04:55:19 +0000284 // If this target requires addPassesToEmitWholeFile, do it now. This is
285 // used by strange things like the C backend.
286 if (Target.WantsWholeFile()) {
287 PassManager PM;
288 PM.add(new TargetData(*Target.getTargetData()));
289 if (!NoVerify)
290 PM.add(createVerifierPass());
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000291
Chris Lattner6dd290a2007-05-06 04:55:19 +0000292 // Ask the target to add backend passes as necessary.
Bill Wendling026e5d72009-04-29 23:29:43 +0000293 if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, OLvl)) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000294 errs() << argv[0] << ": target does not support generation of this"
295 << " file type!\n";
David Greenea31f96c2009-07-14 20:18:05 +0000296 if (Out != &fouts()) delete Out;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000297 // And the Out file is empty and useless, so remove it now.
298 sys::Path(OutputFilename).eraseFromDisk();
299 return 1;
300 }
301 PM.run(mod);
302 } else {
303 // Build up all of the passes that we want to do to the module.
Dan Gohmanbd2613d2008-04-16 15:56:26 +0000304 ExistingModuleProvider Provider(M.release());
305 FunctionPassManager Passes(&Provider);
Chris Lattner6dd290a2007-05-06 04:55:19 +0000306 Passes.add(new TargetData(*Target.getTargetData()));
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000307
Chris Lattner6dd290a2007-05-06 04:55:19 +0000308#ifndef NDEBUG
309 if (!NoVerify)
310 Passes.add(createVerifierPass());
311#endif
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000312
Chris Lattner6dd290a2007-05-06 04:55:19 +0000313 // Ask the target to add backend passes as necessary.
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000314 ObjectCodeEmitter *OCE = 0;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000315
Evan Cheng499d5f32009-03-25 01:48:21 +0000316 // Override default to generate verbose assembly.
317 Target.setAsmVerbosityDefault(true);
318
Bill Wendling026e5d72009-04-29 23:29:43 +0000319 switch (Target.addPassesToEmitFile(Passes, *Out, FileType, OLvl)) {
Chris Lattner6dd290a2007-05-06 04:55:19 +0000320 default:
321 assert(0 && "Invalid file model!");
322 return 1;
323 case FileModel::Error:
Dan Gohmand8db3762009-07-15 16:35:29 +0000324 errs() << argv[0] << ": target does not support generation of this"
325 << " file type!\n";
David Greenea31f96c2009-07-14 20:18:05 +0000326 if (Out != &fouts()) delete Out;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000327 // And the Out file is empty and useless, so remove it now.
328 sys::Path(OutputFilename).eraseFromDisk();
329 return 1;
330 case FileModel::AsmFile:
331 break;
332 case FileModel::MachOFile:
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000333 OCE = AddMachOWriter(Passes, *Out, Target);
Chris Lattner6dd290a2007-05-06 04:55:19 +0000334 break;
335 case FileModel::ElfFile:
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000336 OCE = AddELFWriter(Passes, *Out, Target);
Chris Lattner6dd290a2007-05-06 04:55:19 +0000337 break;
338 }
339
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000340 if (Target.addPassesToEmitFileFinish(Passes, OCE, OLvl)) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000341 errs() << argv[0] << ": target does not support generation of this"
342 << " file type!\n";
David Greenea31f96c2009-07-14 20:18:05 +0000343 if (Out != &fouts()) delete Out;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000344 // And the Out file is empty and useless, so remove it now.
345 sys::Path(OutputFilename).eraseFromDisk();
346 return 1;
347 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000348
Chris Lattner6dd290a2007-05-06 04:55:19 +0000349 Passes.doInitialization();
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000350
Chris Lattner6dd290a2007-05-06 04:55:19 +0000351 // Run our queue of passes all at once now, efficiently.
352 // TODO: this could lazily stream functions out of the module.
353 for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I)
Devang Patel72a4d2f2009-06-04 22:05:33 +0000354 if (!I->isDeclaration()) {
355 if (DisableRedZone)
356 I->addFnAttr(Attribute::NoRedZone);
Devang Pateld1c7d342009-06-05 21:57:13 +0000357 if (NoImplicitFloats)
358 I->addFnAttr(Attribute::NoImplicitFloat);
Chris Lattner6dd290a2007-05-06 04:55:19 +0000359 Passes.run(*I);
Devang Patel72a4d2f2009-06-04 22:05:33 +0000360 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000361
Chris Lattner6dd290a2007-05-06 04:55:19 +0000362 Passes.doFinalization();
363 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000364
David Greenea31f96c2009-07-14 20:18:05 +0000365 Out->flush();
366
Chris Lattner6dd290a2007-05-06 04:55:19 +0000367 // Delete the ostream if it's not a stdout stream
David Greenea31f96c2009-07-14 20:18:05 +0000368 if (Out != &fouts()) delete Out;
Chris Lattner6dd290a2007-05-06 04:55:19 +0000369
370 return 0;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000371}