blob: 70d8c70fc31524294fe753dc5afbd3210c8e4306 [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"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000025#include "llvm/System/Signals.h"
Chris Lattner812125a2005-06-25 03:32:05 +000026#include "llvm/Config/config.h"
Chris Lattner78f7e1a2001-09-19 16:52:09 +000027#include <fstream>
Reid Spencer86f42bd2004-07-04 12:20:55 +000028#include <iostream>
29#include <memory>
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000030
Brian Gaeked0fde302003-11-11 22:41:34 +000031using namespace llvm;
32
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000033// General options for llc. Other pass-specific options are specified
34// within the corresponding llc passes, and target-specific options
35// and back-end code generation options are specified with the target machine.
Misha Brukman3da94ae2005-04-22 00:00:37 +000036//
Chris Lattnerb5881f12003-04-25 05:26:11 +000037static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000038InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
39
Chris Lattnerb5881f12003-04-25 05:26:11 +000040static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000041OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
42
43static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
44
Chris Lattnere45110e2004-07-11 04:03:24 +000045static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
Chris Lattnercbb34a72005-06-25 03:00:34 +000046MArch("march", cl::desc("Architecture to generate code for:"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000047
Chris Lattner812125a2005-06-25 03:32:05 +000048cl::opt<TargetMachine::CodeGenFileType>
49FileType("filetype", cl::init(TargetMachine::AssemblyFile),
50 cl::desc("Choose a file type (not all types are supported by all targets):"),
51 cl::values(
52 clEnumValN(TargetMachine::AssemblyFile, "asm",
53 " Emit an assembly ('.s') file"),
54 clEnumValN(TargetMachine::ObjectFile, "obj",
55 " Emit a native object ('.o') file"),
56 clEnumValN(TargetMachine::DynamicLibrary, "dynlib",
57 " Emit a native dynamic library ('.so') file"),
58 clEnumValEnd));
59
60
61// GetFileNameRoot - Helper function to get the basename of a filename.
Chris Lattnerb5881f12003-04-25 05:26:11 +000062static inline std::string
Chris Lattnere45110e2004-07-11 04:03:24 +000063GetFileNameRoot(const std::string &InputFilename) {
Chris Lattnerb5881f12003-04-25 05:26:11 +000064 std::string IFN = InputFilename;
65 std::string outputFilename;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000066 int Len = IFN.length();
John Criswellb5d09bf2003-08-28 21:42:29 +000067 if ((Len > 2) &&
68 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerb5881f12003-04-25 05:26:11 +000069 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000070 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +000071 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000072 }
73 return outputFilename;
74}
75
Vikram S. Adve805eb962001-09-18 13:10:45 +000076
Chris Lattner5b836c42003-06-20 15:49:04 +000077// main - Entry point for the llc compiler.
78//
79int main(int argc, char **argv) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000080 try {
81 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
82 sys::PrintStackTraceOnErrorSignal();
Chris Lattner364d1202004-02-19 20:32:39 +000083
Reid Spencer1ef8bda2004-12-30 05:36:08 +000084 // Load the module to be compiled...
85 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
86 if (M.get() == 0) {
87 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattnerbb433502003-08-24 14:02:14 +000088 return 1;
Chris Lattnere45110e2004-07-11 04:03:24 +000089 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +000090 Module &mod = *M.get();
Chris Lattnere45110e2004-07-11 04:03:24 +000091
Reid Spencer1ef8bda2004-12-30 05:36:08 +000092 // Allocate target machine. First, check whether the user has
93 // explicitly specified an architecture to compile for.
94 TargetMachine* (*TargetMachineAllocator)(const Module&,
95 IntrinsicLowering *) = 0;
96 if (MArch == 0) {
97 std::string Err;
98 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err);
99 if (MArch == 0) {
100 std::cerr << argv[0] << ": error auto-selecting target for module '"
101 << Err << "'. Please use the -march option to explicitly "
102 << "pick a target.\n";
Misha Brukman878ba7c2004-07-12 22:58:07 +0000103 return 1;
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000104 }
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000105 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000106
107 std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, 0));
108 assert(target.get() && "Could not allocate target machine!");
109 TargetMachine &Target = *target.get();
110 const TargetData &TD = Target.getTargetData();
111
112 // Build up all of the passes that we want to do to the module...
113 PassManager Passes;
Chris Lattnercbb34a72005-06-25 03:00:34 +0000114 Passes.add(new TargetData(TD));
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000115
116 // Figure out where we are going to send the output...
117 std::ostream *Out = 0;
118 if (OutputFilename != "") {
119 if (OutputFilename != "-") {
120 // Specified an output filename?
121 if (!Force && std::ifstream(OutputFilename.c_str())) {
122 // If force is not specified, make sure not to overwrite a file!
123 std::cerr << argv[0] << ": error opening '" << OutputFilename
124 << "': file exists!\n"
125 << "Use -f command line argument to force output\n";
126 return 1;
127 }
128 Out = new std::ofstream(OutputFilename.c_str());
129
130 // Make sure that the Out file gets unlinked from the disk if we get a
131 // SIGINT
132 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
133 } else {
134 Out = &std::cout;
135 }
Chris Lattnercccc28c2003-06-18 18:46:08 +0000136 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000137 if (InputFilename == "-") {
138 OutputFilename = "-";
139 Out = &std::cout;
140 } else {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000141 OutputFilename = GetFileNameRoot(InputFilename);
Chris Lattner74661c82004-02-15 22:54:19 +0000142
Chris Lattner812125a2005-06-25 03:32:05 +0000143 switch (FileType) {
144 case TargetMachine::AssemblyFile:
145 if (MArch->Name[0] != 'c' || MArch->Name[1] != 0) // not CBE
146 OutputFilename += ".s";
147 else
148 OutputFilename += ".cbe.c";
149 break;
150 case TargetMachine::ObjectFile:
151 OutputFilename += ".o";
152 break;
153 case TargetMachine::DynamicLibrary:
154 OutputFilename += LTDL_SHLIB_EXT;
155 break;
156 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000157
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000158 if (!Force && std::ifstream(OutputFilename.c_str())) {
159 // If force is not specified, make sure not to overwrite a file!
160 std::cerr << argv[0] << ": error opening '" << OutputFilename
161 << "': file exists!\n"
162 << "Use -f command line argument to force output\n";
163 return 1;
164 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000165
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000166 Out = new std::ofstream(OutputFilename.c_str());
167 if (!Out->good()) {
168 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
169 delete Out;
170 return 1;
171 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000172
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000173 // Make sure that the Out file gets unlinked from the disk if we get a
174 // SIGINT
175 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Chris Lattner697954c2002-01-20 22:54:45 +0000176 }
Chris Lattner3524fc22001-10-15 17:30:47 +0000177 }
Vikram S. Adve7d0ba022002-09-16 16:35:34 +0000178
Chris Lattnercbb34a72005-06-25 03:00:34 +0000179 // Ask the target to add backend passes as necessary.
Chris Lattner812125a2005-06-25 03:32:05 +0000180 if (Target.addPassesToEmitFile(Passes, *Out, FileType)) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000181 std::cerr << argv[0] << ": target '" << Target.getName()
Chris Lattner812125a2005-06-25 03:32:05 +0000182 << "' does not support generation of this file type!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000183 if (Out != &std::cout) delete Out;
184 // And the Out file is empty and useless, so remove it now.
185 std::remove(OutputFilename.c_str());
186 return 1;
187 } else {
188 // Run our queue of passes all at once now, efficiently.
189 Passes.run(*M.get());
190 }
191
192 // Delete the ostream if it's not a stdout stream
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000193 if (Out != &std::cout) delete Out;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000194
195 return 0;
196 } catch (const std::string& msg) {
197 std::cerr << argv[0] << ": " << msg << "\n";
198 } catch (...) {
199 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner63342052002-10-29 21:12:46 +0000200 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000201 return 1;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000202}