blob: 41811afa7e37656667fda64dadff248cba9f342c [file] [log] [blame]
Chris Lattnerb27d4742001-10-04 01:40:53 +00001//===-- llc.cpp - Implement the LLVM Compiler -----------------------------===//
Chris Lattner2cf137b2001-09-07 22:20:50 +00002//
3// This is the llc compiler driver.
4//
Chris Lattnerb27d4742001-10-04 01:40:53 +00005//===----------------------------------------------------------------------===//
Vikram S. Adve2d94a342001-07-21 12:42:29 +00006
Vikram S. Adve2d94a342001-07-21 12:42:29 +00007#include "llvm/Bytecode/Reader.h"
Chris Lattner22a6a902001-09-14 05:34:53 +00008#include "llvm/Target/Sparc.h"
Vikram S. Adve9d409352001-09-18 13:10:45 +00009#include "llvm/Target/TargetMachine.h"
Vikram S. Adve2f084b22001-10-14 23:29:28 +000010#include "llvm/Transforms/Instrumentation/TraceValues.h"
Chris Lattner97fd6c42001-10-15 17:30:47 +000011#include "llvm/Transforms/LowerAllocations.h"
12#include "llvm/Transforms/HoistPHIConstants.h"
13#include "llvm/Transforms/PrintModulePass.h"
Chris Lattnere2472bb2001-07-23 17:46:59 +000014#include "llvm/Support/CommandLine.h"
Chris Lattnered226062001-09-07 21:26:31 +000015#include "llvm/Module.h"
16#include "llvm/Method.h"
Chris Lattner6c2c8702001-09-18 17:04:18 +000017#include <memory>
Vikram S. Adve2f084b22001-10-14 23:29:28 +000018#include <string>
Chris Lattner46f1b612001-09-19 16:52:09 +000019#include <fstream>
Chris Lattner0af24642001-07-23 02:35:57 +000020
Chris Lattnerab0cc402001-07-23 19:27:24 +000021cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
Chris Lattner0af24642001-07-23 02:35:57 +000022cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
Chris Lattner46f1b612001-09-19 16:52:09 +000023cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Vikram S. Adve2f084b22001-10-14 23:29:28 +000024cl::Flag DumpAsm ("d", "Print bytecode before native code generation", cl::Hidden,false);
25cl::Flag DoNotEmitAssembly("noasm", "Do not emit assembly code", cl::Hidden, false);
26cl::Flag TraceBBValues ("trace",
27 "Trace values at basic block and method exits",
28 cl::NoFlags, false);
29cl::Flag TraceMethodValues("tracem", "Trace values only at method exits",
30 cl::NoFlags, false);
Chris Lattner97fd6c42001-10-15 17:30:47 +000031cl::Flag DebugTrace ("dumptrace",
32 "output trace code to a <fn>.trace.ll file",
33 cl::Hidden, false);
Chris Lattner0af24642001-07-23 02:35:57 +000034
Vikram S. Adve9d409352001-09-18 13:10:45 +000035
Chris Lattner97fd6c42001-10-15 17:30:47 +000036// GetFileNameRoot - Helper function to get the basename of a filename...
37static inline string GetFileNameRoot(const string &InputFilename) {
Vikram S. Adve2f084b22001-10-14 23:29:28 +000038 string IFN = InputFilename;
39 string outputFilename;
40 int Len = IFN.length();
41 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
42 outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
43 } else {
Chris Lattner97fd6c42001-10-15 17:30:47 +000044 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000045 }
46 return outputFilename;
47}
48
Vikram S. Adve9d409352001-09-18 13:10:45 +000049
50//===---------------------------------------------------------------------===//
Chris Lattner97fd6c42001-10-15 17:30:47 +000051// GenerateCodeForTarget Pass
Vikram S. Adve2f084b22001-10-14 23:29:28 +000052//
53// Native code generation for a specified target.
54//===---------------------------------------------------------------------===//
55
Chris Lattner97fd6c42001-10-15 17:30:47 +000056class GenerateCodeForTarget : public ConcretePass<GenerateCodeForTarget> {
57 TargetMachine &Target;
58public:
59 inline GenerateCodeForTarget(TargetMachine &T) : Target(T) {}
60
61 // doPerMethodWork - This method does the actual work of generating code for
62 // the specified method.
63 //
64 bool doPerMethodWorkVirt(Method *M) {
65 if (!M->isExternal() && Target.compileMethod(M)) {
66 cerr << "Error compiling " << InputFilename << "!\n";
67 return true;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000068 }
Chris Lattner97fd6c42001-10-15 17:30:47 +000069
70 return false;
71 }
72};
Vikram S. Adve2f084b22001-10-14 23:29:28 +000073
74
75//===---------------------------------------------------------------------===//
Chris Lattner97fd6c42001-10-15 17:30:47 +000076// EmitAssembly Pass
Vikram S. Adve2f084b22001-10-14 23:29:28 +000077//
Chris Lattner97fd6c42001-10-15 17:30:47 +000078// Write assembly code to specified output stream
Vikram S. Adve2f084b22001-10-14 23:29:28 +000079//===---------------------------------------------------------------------===//
80
Chris Lattner97fd6c42001-10-15 17:30:47 +000081class EmitAssembly : public ConcretePass<EmitAssembly> {
82 const TargetMachine &Target; // Target to compile for
83 ostream *Out; // Stream to print on
84 bool DeleteStream; // Delete stream in dtor?
85
86 Module *TheMod;
87public:
88 inline EmitAssembly(const TargetMachine &T, ostream *O, bool D)
89 : Target(T), Out(O), DeleteStream(D) {}
90
91 virtual bool doPassInitializationVirt(Module *M) {
92 TheMod = M;
93 return false;
Chris Lattner46f1b612001-09-19 16:52:09 +000094 }
95
Chris Lattner97fd6c42001-10-15 17:30:47 +000096 ~EmitAssembly() {
97 Target.emitAssembly(TheMod, *Out);
Chris Lattner46f1b612001-09-19 16:52:09 +000098
Chris Lattner97fd6c42001-10-15 17:30:47 +000099 if (DeleteStream) delete Out;
100 }
101};
Chris Lattner0a823a02001-09-14 03:37:52 +0000102
Vikram S. Adve9d409352001-09-18 13:10:45 +0000103
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000104//===---------------------------------------------------------------------===//
105// Function main()
106//
107// Entry point for the llc compiler.
108//===---------------------------------------------------------------------===//
109
110int
111main(int argc, char **argv)
112{
113 // Parse command line options...
114 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
115
116 // Allocate a target... in the future this will be controllable on the
117 // command line.
118 auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
Chris Lattner97fd6c42001-10-15 17:30:47 +0000119 assert(target.get() && "Could not allocate target machine!");
120
121 TargetMachine &Target = *target.get();
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000122
123 // Load the module to be compiled...
124 auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
125 if (M.get() == 0) {
126 cerr << "bytecode didn't read correctly.\n";
127 return 1;
128 }
Chris Lattner97fd6c42001-10-15 17:30:47 +0000129
130 // Build up all of the passes that we want to do to the module...
131 vector<Pass*> Passes;
132
133 // Replace malloc and free instructions with library calls
134 Passes.push_back(new LowerAllocations(Target.DataLayout));
135
136 // Hoist constants out of PHI nodes into predecessor BB's
137 Passes.push_back(new HoistPHIConstants());
138
139 if (TraceBBValues || TraceMethodValues) // If tracing enabled...
140 // Insert trace code in all methods in the module
141 Passes.push_back(new InsertTraceCode(TraceBBValues,
142 TraceBBValues || TraceMethodValues));
143
144
145 if (DebugTrace) { // If Trace Debugging is enabled...
146 // Then write the module with tracing code out in assembly form
147 assert(InputFilename != "-" && "files on stdin not supported with tracing");
148 string traceFileName = GetFileNameRoot(InputFilename) + ".trace.ll";
149
150 ostream *os = new ofstream(traceFileName.c_str(),
151 (Force ? 0 : ios::noreplace)|ios::out);
152 if (!os->good()) {
153 cerr << "Error opening " << traceFileName << "!\n";
154 delete os;
155 return 1;
156 }
157
158 Passes.push_back(new PrintModulePass("", os, true));
159 }
160
161 // If LLVM dumping after transformations is requested, add it to the pipeline
162 if (DumpAsm)
163 Passes.push_back(new PrintModulePass("Method after xformations: \n",&cerr));
164
165 // Generate Target code...
166 Passes.push_back(new GenerateCodeForTarget(Target));
167
168 if (!DoNotEmitAssembly) { // If asm output is enabled...
169 // Figure out where we are going to send the output...
170 ostream *Out = 0;
171 if (OutputFilename != "") { // Specified an output filename?
172 Out = new ofstream(OutputFilename.c_str(),
173 (Force ? 0 : ios::noreplace)|ios::out);
174 } else {
175 if (InputFilename == "-") {
176 OutputFilename = "-";
177 Out = &cout;
178 } else {
179 string OutputFilename = GetFileNameRoot(InputFilename);
180 OutputFilename += ".s";
181 Out = new ofstream(OutputFilename.c_str(),
182 (Force ? 0 : ios::noreplace)|ios::out);
183 if (!Out->good()) {
184 cerr << "Error opening " << OutputFilename << "!\n";
185 delete Out;
186 return 1;
187 }
188 }
189 }
190
191 // Output assembly language to the .s file
192 Passes.push_back(new EmitAssembly(Target, Out, Out != &cout));
193 }
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000194
Chris Lattner97fd6c42001-10-15 17:30:47 +0000195 // Run our queue of passes all at once now, efficiently.
196 return Pass::runAllPassesAndFree(M.get(), Passes);
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000197}
198
Chris Lattner97fd6c42001-10-15 17:30:47 +0000199