1 //===-- llc.cpp - Implement the LLVM Compiler -----------------------------===//
3 // This is the llc compiler driver.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Bytecode/Reader.h"
8 #include "llvm/Target/Sparc.h"
9 #include "llvm/Target/TargetMachine.h"
10 #include "llvm/Transforms/Instrumentation/TraceValues.h"
11 #include "llvm/Transforms/ChangeAllocations.h"
12 #include "llvm/Transforms/Scalar/DecomposeMultiDimRefs.h"
13 #include "llvm/Assembly/PrintModulePass.h"
14 #include "llvm/Bytecode/WriteBytecodePass.h"
15 #include "llvm/Transforms/ConstantMerge.h"
16 #include "llvm/Module.h"
17 #include "llvm/Function.h"
18 #include "llvm/PassManager.h"
19 #include "Support/CommandLine.h"
20 #include "Support/Signals.h"
25 static cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
26 static cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
27 static cl::Flag Force ("f", "Overwrite output files");
28 static cl::Flag DumpAsm ("d", "Print bytecode before native code generation", cl::Hidden);
31 TraceOff, TraceFunctions, TraceBasicBlocks
34 static cl::Enum<enum TraceLevel> TraceValues("trace", cl::NoFlags,
35 "Trace values through functions or basic blocks",
36 clEnumValN(TraceOff , "off", "Disable trace code"),
37 clEnumValN(TraceFunctions , "function", "Trace each function"),
38 clEnumValN(TraceBasicBlocks, "basicblock", "Trace each basic block"), 0);
41 // GetFileNameRoot - Helper function to get the basename of a filename...
42 static inline string GetFileNameRoot(const string &InputFilename) {
43 string IFN = InputFilename;
44 string outputFilename;
45 int Len = IFN.length();
46 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
47 outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
51 return outputFilename;
55 //===---------------------------------------------------------------------===//
58 // Entry point for the llc compiler.
59 //===---------------------------------------------------------------------===//
61 int main(int argc, char **argv) {
62 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
64 // Allocate a target... in the future this will be controllable on the
66 std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
67 assert(target.get() && "Could not allocate target machine!");
69 TargetMachine &Target = *target.get();
71 // Load the module to be compiled...
72 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
74 cerr << "bytecode didn't read correctly.\n";
78 // Build up all of the passes that we want to do to the module...
81 if (TraceValues != TraceOff) { // If tracing enabled...
82 // Insert trace code in all functions in the module
83 if (TraceValues == TraceBasicBlocks)
84 Passes.add(createTraceValuesPassForBasicBlocks());
85 else if (TraceValues == TraceFunctions)
86 Passes.add(createTraceValuesPassForFunction());
88 assert(0 && "Bad value for TraceValues!");
90 // Eliminate duplication in constant pool
91 Passes.add(createDynamicConstantMergePass());
94 // Decompose multi-dimensional refs into a sequence of 1D refs
95 Passes.add(createDecomposeMultiDimRefsPass());
97 // Write out the module with tracing code just before code generation
98 if (TraceValues != TraceOff) { // If tracing enabled...
99 assert(InputFilename != "-" &&
100 "files on stdin not supported with tracing");
101 string traceFileName = GetFileNameRoot(InputFilename) + ".trace.bc";
103 if (!Force && std::ifstream(OutputFilename.c_str())) {
104 // If force is not specified, make sure not to overwrite a file!
105 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
106 << "Use -f command line argument to force output\n";
110 std::ostream *os = new std::ofstream(traceFileName.c_str());
112 cerr << "Error opening " << traceFileName
113 << "! SKIPPING OUTPUT OF TRACE CODE\n";
118 Passes.add(new WriteBytecodePass(os, true));
121 // Replace malloc and free instructions with library calls.
122 // Do this after tracing until lli implements these lib calls.
123 // For now, it will emulate malloc and free internally.
124 Passes.add(createLowerAllocationsPass(Target.DataLayout));
126 // If LLVM dumping after transformations is requested, add it to the pipeline
128 Passes.add(new PrintFunctionPass("Code after xformations: \n", &cerr));
130 // Figure out where we are going to send the output...
131 std::ostream *Out = 0;
132 if (OutputFilename != "") { // Specified an output filename?
133 if (!Force && std::ifstream(OutputFilename.c_str())) {
134 // If force is not specified, make sure not to overwrite a file!
135 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
136 << "Use -f command line argument to force output\n";
139 Out = new std::ofstream(OutputFilename.c_str());
141 // Make sure that the Out file gets unlink'd from the disk if we get a
143 RemoveFileOnSignal(OutputFilename);
145 if (InputFilename == "-") {
146 OutputFilename = "-";
149 string OutputFilename = GetFileNameRoot(InputFilename);
150 OutputFilename += ".s";
152 if (!Force && std::ifstream(OutputFilename.c_str())) {
153 // If force is not specified, make sure not to overwrite a file!
154 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
155 << "Use -f command line argument to force output\n";
159 Out = new std::ofstream(OutputFilename.c_str());
161 cerr << "Error opening " << OutputFilename << "!\n";
165 // Make sure that the Out file gets unlink'd from the disk if we get a
167 RemoveFileOnSignal(OutputFilename);
171 Target.addPassesToEmitAssembly(Passes, *Out);
173 // Run our queue of passes all at once now, efficiently.
176 if (Out != &std::cout) delete Out;