blob: 2a68686a01df361d14adb671a62e3229b8e59a0e [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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
Brian Gaekee40eae72004-03-16 21:47:20 +000012// or C code, given LLVM bytecode.
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
Vikram S. Advecb465fc2001-07-21 12:42:29 +000016#include "llvm/Bytecode/Reader.h"
Vikram S. Adve805eb962001-09-18 13:10:45 +000017#include "llvm/Target/TargetMachine.h"
Chris Lattnere45110e2004-07-11 04:03:24 +000018#include "llvm/Target/TargetMachineRegistry.h"
Chris Lattner65f1b892002-05-07 20:03:27 +000019#include "llvm/Transforms/Scalar.h"
Chris Lattner46ac43c2001-09-07 21:26:31 +000020#include "llvm/Module.h"
Chris Lattnercd50d3f2002-01-31 00:46:45 +000021#include "llvm/PassManager.h"
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000022#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/PluginLoader.h"
Reid Spencer4418c2b2005-07-28 02:25:30 +000025#include "llvm/Support/PassNameParser.h"
26#include "llvm/Analysis/Verifier.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000027#include "llvm/System/Signals.h"
Chris Lattner812125a2005-06-25 03:32:05 +000028#include "llvm/Config/config.h"
Chris Lattner78f7e1a2001-09-19 16:52:09 +000029#include <fstream>
Reid Spencer86f42bd2004-07-04 12:20:55 +000030#include <iostream>
31#include <memory>
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000032
Brian Gaeked0fde302003-11-11 22:41:34 +000033using namespace llvm;
34
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000035// General options for llc. Other pass-specific options are specified
36// within the corresponding llc passes, and target-specific options
37// and back-end code generation options are specified with the target machine.
Misha Brukman3da94ae2005-04-22 00:00:37 +000038//
Chris Lattnerb5881f12003-04-25 05:26:11 +000039static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000040InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
41
Chris Lattnerb5881f12003-04-25 05:26:11 +000042static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000043OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
44
45static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
46
Chris Lattnere45110e2004-07-11 04:03:24 +000047static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
Chris Lattnercbb34a72005-06-25 03:00:34 +000048MArch("march", cl::desc("Architecture to generate code for:"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000049
Chris Lattner812125a2005-06-25 03:32:05 +000050cl::opt<TargetMachine::CodeGenFileType>
51FileType("filetype", cl::init(TargetMachine::AssemblyFile),
52 cl::desc("Choose a file type (not all types are supported by all targets):"),
53 cl::values(
54 clEnumValN(TargetMachine::AssemblyFile, "asm",
55 " Emit an assembly ('.s') file"),
56 clEnumValN(TargetMachine::ObjectFile, "obj",
57 " Emit a native object ('.o') file"),
58 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
59 " Emit a native dynamic library ('.so') file"),
60 clEnumValEnd));
61
Reid Spencer4418c2b2005-07-28 02:25:30 +000062// The LLCPassList is populated with passes that were registered using
63// PassInfo::LLC by the FilteredPassNameParser:
64cl::list<const PassInfo*, bool, FilteredPassNameParser<PassInfo::LLC> >
65LLCPassList(cl::desc("Passes Available"));
66
67cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
68 cl::desc("Do not verify input module"));
69
Chris Lattner812125a2005-06-25 03:32:05 +000070
71// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerb5881f12003-04-25 05:26:11 +000072static inline std::string
Chris Lattnere45110e2004-07-11 04:03:24 +000073GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerb5881f12003-04-25 05:26:11 +000074 std::string IFN = InputFilename;
75 std::string outputFilename;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000076 int Len = IFN.length();
John Criswellb5d09bf2003-08-28 21:42:29 +000077 if ((Len > 2) &&
78 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerb5881f12003-04-25 05:26:11 +000079 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000080 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +000081 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000082 }
83 return outputFilename;
84}
85
Vikram S. Adve805eb962001-09-18 13:10:45 +000086
Chris Lattner5b836c42003-06-20 15:49:04 +000087// main - Entry point for the llc compiler.
88//
89int main(int argc, char **argv) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000090 try {
91 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
92 sys::PrintStackTraceOnErrorSignal();
Chris Lattner364d1202004-02-19 20:32:39 +000093
Reid Spencer1ef8bda2004-12-30 05:36:08 +000094 // Load the module to be compiled...
95 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
96 if (M.get() == 0) {
97 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattnerbb433502003-08-24 14:02:14 +000098 return 1;
Chris Lattnere45110e2004-07-11 04:03:24 +000099 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000100 Module &mod = *M.get();
Chris Lattnere45110e2004-07-11 04:03:24 +0000101
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000102 // Allocate target machine. First, check whether the user has
103 // explicitly specified an architecture to compile for.
104 TargetMachine* (*TargetMachineAllocator)(const Module&,
105 IntrinsicLowering *) = 0;
106 if (MArch == 0) {
107 std::string Err;
108 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err);
109 if (MArch == 0) {
110 std::cerr << argv[0] << ": error auto-selecting target for module '"
111 << Err << "'. Please use the -march option to explicitly "
112 << "pick a target.\n";
Misha Brukman878ba7c2004-07-12 22:58:07 +0000113 return 1;
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000114 }
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000115 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000116
117 std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, 0));
118 assert(target.get() && "Could not allocate target machine!");
119 TargetMachine &Target = *target.get();
120 const TargetData &TD = Target.getTargetData();
121
122 // Build up all of the passes that we want to do to the module...
123 PassManager Passes;
Chris Lattnercbb34a72005-06-25 03:00:34 +0000124 Passes.add(new TargetData(TD));
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000125
Reid Spencer4418c2b2005-07-28 02:25:30 +0000126 // Create a new pass for each one specified on the command line
127 for (unsigned i = 0; i < LLCPassList.size(); ++i) {
128 const PassInfo *aPass = LLCPassList[i];
129
130 if (aPass->getNormalCtor()) {
131 Pass *P = aPass->getNormalCtor()();
132 Passes.add(P);
133 } else {
134 std::cerr << argv[0] << ": cannot create pass: "
135 << aPass->getPassName() << "\n";
136 }
137 }
138
Reid Spencer540f7d62005-07-28 04:00:49 +0000139#ifndef NDEBUG
140 if(!NoVerify)
141 Passes.add(createVerifierPass());
142#endif
Reid Spencer4418c2b2005-07-28 02:25:30 +0000143
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000144 // Figure out where we are going to send the output...
145 std::ostream *Out = 0;
146 if (OutputFilename != "") {
147 if (OutputFilename != "-") {
148 // Specified an output filename?
149 if (!Force && std::ifstream(OutputFilename.c_str())) {
150 // If force is not specified, make sure not to overwrite a file!
151 std::cerr << argv[0] << ": error opening '" << OutputFilename
152 << "': file exists!\n"
153 << "Use -f command line argument to force output\n";
154 return 1;
155 }
156 Out = new std::ofstream(OutputFilename.c_str());
157
158 // Make sure that the Out file gets unlinked from the disk if we get a
159 // SIGINT
160 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
161 } else {
162 Out = &std::cout;
163 }
Chris Lattnercccc28c2003-06-18 18:46:08 +0000164 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000165 if (InputFilename == "-") {
166 OutputFilename = "-";
167 Out = &std::cout;
168 } else {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000169 OutputFilename = GetFileNameRoot(InputFilename);
Chris Lattner74661c82004-02-15 22:54:19 +0000170
Chris Lattner812125a2005-06-25 03:32:05 +0000171 switch (FileType) {
172 case TargetMachine::AssemblyFile:
173 if (MArch->Name[0] != 'c' || MArch->Name[1] != 0) // not CBE
174 OutputFilename += ".s";
175 else
176 OutputFilename += ".cbe.c";
177 break;
178 case TargetMachine::ObjectFile:
179 OutputFilename += ".o";
180 break;
181 case TargetMachine::DynamicLibrary:
182 OutputFilename += LTDL_SHLIB_EXT;
183 break;
184 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000185
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000186 if (!Force && std::ifstream(OutputFilename.c_str())) {
187 // If force is not specified, make sure not to overwrite a file!
188 std::cerr << argv[0] << ": error opening '" << OutputFilename
189 << "': file exists!\n"
190 << "Use -f command line argument to force output\n";
191 return 1;
192 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000193
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000194 Out = new std::ofstream(OutputFilename.c_str());
195 if (!Out->good()) {
196 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
197 delete Out;
198 return 1;
199 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000200
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000201 // Make sure that the Out file gets unlinked from the disk if we get a
202 // SIGINT
203 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Chris Lattner697954c2002-01-20 22:54:45 +0000204 }
Chris Lattner3524fc22001-10-15 17:30:47 +0000205 }
Vikram S. Adve7d0ba022002-09-16 16:35:34 +0000206
Chris Lattnercbb34a72005-06-25 03:00:34 +0000207 // Ask the target to add backend passes as necessary.
Chris Lattner812125a2005-06-25 03:32:05 +0000208 if (Target.addPassesToEmitFile(Passes, *Out, FileType)) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000209 std::cerr << argv[0] << ": target '" << Target.getName()
Chris Lattner812125a2005-06-25 03:32:05 +0000210 << "' does not support generation of this file type!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000211 if (Out != &std::cout) delete Out;
212 // And the Out file is empty and useless, so remove it now.
213 std::remove(OutputFilename.c_str());
214 return 1;
215 } else {
216 // Run our queue of passes all at once now, efficiently.
217 Passes.run(*M.get());
218 }
219
220 // Delete the ostream if it's not a stdout stream
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000221 if (Out != &std::cout) delete Out;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000222
223 return 0;
224 } catch (const std::string& msg) {
225 std::cerr << argv[0] << ": " << msg << "\n";
226 } catch (...) {
227 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner63342052002-10-29 21:12:46 +0000228 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000229 return 1;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000230}