blob: 49371afd54b4a348ab8503284fa40ab05d4347b5 [file] [log] [blame]
Chris Lattner5b836c42003-06-20 15:49:04 +00001//===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
Chris Lattnere737c7a2001-09-07 22:20:50 +00002//
Chris Lattner5b836c42003-06-20 15:49:04 +00003// This is the llc code generator.
Chris Lattnere737c7a2001-09-07 22:20:50 +00004//
Chris Lattnerb79757c2001-10-04 01:40:53 +00005//===----------------------------------------------------------------------===//
Vikram S. Advecb465fc2001-07-21 12:42:29 +00006
Vikram S. Advecb465fc2001-07-21 12:42:29 +00007#include "llvm/Bytecode/Reader.h"
Chris Lattner4aba5e32002-10-29 20:45:04 +00008#include "llvm/Target/TargetMachineImpls.h"
Vikram S. Adve805eb962001-09-18 13:10:45 +00009#include "llvm/Target/TargetMachine.h"
Chris Lattner65f1b892002-05-07 20:03:27 +000010#include "llvm/Transforms/Scalar.h"
Chris Lattner46ac43c2001-09-07 21:26:31 +000011#include "llvm/Module.h"
Chris Lattnercd50d3f2002-01-31 00:46:45 +000012#include "llvm/PassManager.h"
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000013#include "llvm/Pass.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000014#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000015#include "Support/Signals.h"
Chris Lattnerda784ee2001-09-18 17:04:18 +000016#include <memory>
Chris Lattner78f7e1a2001-09-19 16:52:09 +000017#include <fstream>
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000018
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000019// General options for llc. Other pass-specific options are specified
20// within the corresponding llc passes, and target-specific options
21// and back-end code generation options are specified with the target machine.
22//
Chris Lattnerb5881f12003-04-25 05:26:11 +000023static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000024InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
25
Chris Lattnerb5881f12003-04-25 05:26:11 +000026static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000027OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
28
29static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
30
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000031enum ArchName { noarch, x86, Sparc };
Chris Lattner68492722003-04-28 03:28:56 +000032
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000033static cl::opt<ArchName>
34Arch("march", cl::desc("Architecture to generate assembly for:"), cl::Prefix,
35 cl::values(clEnumVal(x86, " IA-32 (Pentium and above)"),
36 clEnumValN(Sparc, "sparc", " SPARC V9"),
37 0),
38 cl::init(noarch));
Chris Lattner5ff62e92002-07-22 02:10:13 +000039
Chris Lattner3524fc22001-10-15 17:30:47 +000040// GetFileNameRoot - Helper function to get the basename of a filename...
Chris Lattnerb5881f12003-04-25 05:26:11 +000041static inline std::string
42GetFileNameRoot(const std::string &InputFilename)
Vikram S. Adve7d0ba022002-09-16 16:35:34 +000043{
Chris Lattnerb5881f12003-04-25 05:26:11 +000044 std::string IFN = InputFilename;
45 std::string outputFilename;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000046 int Len = IFN.length();
47 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
Chris Lattnerb5881f12003-04-25 05:26:11 +000048 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000049 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +000050 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000051 }
52 return outputFilename;
53}
54
Vikram S. Adve805eb962001-09-18 13:10:45 +000055
Chris Lattner5b836c42003-06-20 15:49:04 +000056// main - Entry point for the llc compiler.
57//
58int main(int argc, char **argv) {
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000059 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
60
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000061 // Load the module to be compiled...
Chris Lattner697954c2002-01-20 22:54:45 +000062 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
Chris Lattner5b836c42003-06-20 15:49:04 +000063 if (M.get() == 0) {
64 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
65 return 1;
66 }
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000067 Module &mod = *M.get();
68
69 // Allocate target machine. First, check whether the user has
70 // explicitly specified an architecture to compile for.
Chris Lattner5b836c42003-06-20 15:49:04 +000071 unsigned Config = (mod.isLittleEndian() ? TM::LittleEndian : TM::BigEndian)|
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000072 (mod.has32BitPointers() ? TM::PtrSize32 : TM::PtrSize64);
73 TargetMachine* (*TargetMachineAllocator)(unsigned) = 0;
74 switch (Arch) {
75 case x86:
76 TargetMachineAllocator = allocateX86TargetMachine;
77 break;
78 case Sparc:
79 TargetMachineAllocator = allocateSparcTargetMachine;
80 break;
81 default:
82 // Decide what the default target machine should be, by looking at
83 // the module. This heuristic (ILP32, LE -> IA32; LP64, BE ->
84 // SPARCV9) is kind of gross, but it will work until we have more
85 // sophisticated target information to work from.
86 if (mod.isLittleEndian() && mod.has32BitPointers()) {
87 TargetMachineAllocator = allocateX86TargetMachine;
88 } else if (mod.isBigEndian() && mod.has64BitPointers()) {
89 TargetMachineAllocator = allocateSparcTargetMachine;
90 } else {
91 assert(0 && "You must specify -march; I could not guess the default");
92 }
93 break;
94 }
95 std::auto_ptr<TargetMachine> target((*TargetMachineAllocator)(Config));
96 assert(target.get() && "Could not allocate target machine!");
97 TargetMachine &Target = *target.get();
98 const TargetData &TD = Target.getTargetData();
Chris Lattner39fd6592002-05-20 21:20:08 +000099
Chris Lattner3524fc22001-10-15 17:30:47 +0000100 // Build up all of the passes that we want to do to the module...
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000101 PassManager Passes;
Chris Lattner3524fc22001-10-15 17:30:47 +0000102
Chris Lattner10daaa12003-04-26 20:11:09 +0000103 Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(),
Chris Lattnerbc194662003-04-25 06:06:13 +0000104 TD.getPointerAlignment(), TD.getDoubleAlignment()));
Chris Lattner2b5f2c12003-04-25 05:22:29 +0000105
Chris Lattnere41576d2002-02-03 23:43:19 +0000106 // Figure out where we are going to send the output...
107 std::ostream *Out = 0;
Chris Lattnercccc28c2003-06-18 18:46:08 +0000108 if (OutputFilename != "") {
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000109 if (OutputFilename != "-") {
110 // Specified an output filename?
111 if (!Force && std::ifstream(OutputFilename.c_str())) {
112 // If force is not specified, make sure not to overwrite a file!
113 std::cerr << argv[0] << ": error opening '" << OutputFilename
114 << "': file exists!\n"
115 << "Use -f command line argument to force output\n";
116 return 1;
117 }
118 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattnercccc28c2003-06-18 18:46:08 +0000119
Brian Gaeke5ce1a582003-06-18 21:43:33 +0000120 // Make sure that the Out file gets unlink'd from the disk if we get a
121 // SIGINT
122 RemoveFileOnSignal(OutputFilename);
123 } else {
124 Out = &std::cout;
125 }
Chris Lattnercccc28c2003-06-18 18:46:08 +0000126 } else {
127 if (InputFilename == "-") {
128 OutputFilename = "-";
129 Out = &std::cout;
130 } else {
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000131 OutputFilename = GetFileNameRoot(InputFilename);
Chris Lattnercccc28c2003-06-18 18:46:08 +0000132 OutputFilename += ".s";
133
Chris Lattner888912d2002-01-22 21:07:24 +0000134 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000135 // If force is not specified, make sure not to overwrite a file!
Chris Lattnerb5881f12003-04-25 05:26:11 +0000136 std::cerr << argv[0] << ": error opening '" << OutputFilename
137 << "': file exists!\n"
138 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000139 return 1;
140 }
Chris Lattnercccc28c2003-06-18 18:46:08 +0000141
Chris Lattner697954c2002-01-20 22:54:45 +0000142 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattnercccc28c2003-06-18 18:46:08 +0000143 if (!Out->good()) {
144 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
145 delete Out;
146 return 1;
147 }
148
Chris Lattner0f82df42002-09-19 16:06:28 +0000149 // Make sure that the Out file gets unlink'd from the disk if we get a
150 // SIGINT
Chris Lattner76d12292002-04-18 19:55:25 +0000151 RemoveFileOnSignal(OutputFilename);
Chris Lattner3524fc22001-10-15 17:30:47 +0000152 }
Chris Lattnercccc28c2003-06-18 18:46:08 +0000153 }
Vikram S. Adve7d0ba022002-09-16 16:35:34 +0000154
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000155 // Ask the target to add backend passes as necessary
Chris Lattner63342052002-10-29 21:12:46 +0000156 if (Target.addPassesToEmitAssembly(Passes, *Out)) {
Chris Lattnerb5881f12003-04-25 05:26:11 +0000157 std::cerr << argv[0] << ": target '" << Target.getName()
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000158 << "' does not support static compilation!\n";
159 if (Out != &std::cout) delete Out;
160 // And the Out file is empty and useless, so remove it now.
161 std::remove(OutputFilename.c_str());
162 return 1;
Chris Lattner63342052002-10-29 21:12:46 +0000163 } else {
164 // Run our queue of passes all at once now, efficiently.
165 Passes.run(*M.get());
166 }
Chris Lattner05e5e072001-10-18 01:31:22 +0000167
Chris Lattner0f82df42002-09-19 16:06:28 +0000168 // Delete the ostream if it's not a stdout stream
Chris Lattnere41576d2002-02-03 23:43:19 +0000169 if (Out != &std::cout) delete Out;
170
Chris Lattnerd7477ee2001-10-18 20:33:21 +0000171 return 0;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000172}