lib/Target/Sparc/Sparc.cpp:
[oota-llvm.git] / tools / llc / llc.cpp
1 //===-- llc.cpp - Implement the LLVM Compiler -----------------------------===//
2 //
3 // This is the llc compiler driver.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/Bytecode/Reader.h"
8 #include "llvm/Target/TargetMachineImpls.h"
9 #include "llvm/Target/TargetMachine.h"
10 #include "llvm/Transforms/Scalar.h"
11 #include "llvm/Module.h"
12 #include "llvm/PassManager.h"
13 #include "llvm/Pass.h"
14 #include "Support/CommandLine.h"
15 #include "Support/Signals.h"
16 #include <memory>
17 #include <fstream>
18 #include <cstdio>
19
20 //------------------------------------------------------------------------------
21 // Option declarations for LLC.
22 //------------------------------------------------------------------------------
23
24 // General options for llc.  Other pass-specific options are specified
25 // within the corresponding llc passes, and target-specific options
26 // and back-end code generation options are specified with the target machine.
27 // 
28 static cl::opt<std::string>
29 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
30
31 static cl::opt<std::string>
32 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
33
34 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
35
36 enum ArchName { noarch, x86, Sparc };
37
38 static cl::opt<ArchName>
39 Arch("march", cl::desc("Architecture to generate assembly for:"), cl::Prefix,
40      cl::values(clEnumVal(x86, "  IA-32 (Pentium and above)"),
41                 clEnumValN(Sparc, "sparc", "  SPARC V9"),
42                 0),
43      cl::init(noarch));
44
45 // GetFileNameRoot - Helper function to get the basename of a filename...
46 static inline std::string
47 GetFileNameRoot(const std::string &InputFilename)
48 {
49   std::string IFN = InputFilename;
50   std::string outputFilename;
51   int Len = IFN.length();
52   if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
53     outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
54   } else {
55     outputFilename = IFN;
56   }
57   return outputFilename;
58 }
59
60
61 //===---------------------------------------------------------------------===//
62 // Function main()
63 // 
64 // Entry point for the llc compiler.
65 //===---------------------------------------------------------------------===//
66
67 int
68 main(int argc, char **argv)
69 {
70   cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
71   
72   // Load the module to be compiled...
73   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
74   if (M.get() == 0)
75     {
76       std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
77       return 1;
78     }
79   Module &mod = *M.get();
80
81   // Allocate target machine.  First, check whether the user has
82   // explicitly specified an architecture to compile for.
83   unsigned Config = (mod.isLittleEndian()   ? TM::LittleEndian : TM::BigEndian) |
84                     (mod.has32BitPointers() ? TM::PtrSize32    : TM::PtrSize64);
85   TargetMachine* (*TargetMachineAllocator)(unsigned) = 0;
86   switch (Arch) {
87   case x86:
88     TargetMachineAllocator = allocateX86TargetMachine;
89     break;
90   case Sparc:
91     TargetMachineAllocator = allocateSparcTargetMachine;
92     break;
93   default:
94     // Decide what the default target machine should be, by looking at
95     // the module. This heuristic (ILP32, LE -> IA32; LP64, BE ->
96     // SPARCV9) is kind of gross, but it will work until we have more
97     // sophisticated target information to work from.
98     if (mod.isLittleEndian() && mod.has32BitPointers()) { 
99       TargetMachineAllocator = allocateX86TargetMachine;
100     } else if (mod.isBigEndian() && mod.has64BitPointers()) {
101       TargetMachineAllocator = allocateSparcTargetMachine;
102     } else {
103       assert(0 && "You must specify -march; I could not guess the default");
104     } 
105     break;
106   }
107   std::auto_ptr<TargetMachine> target((*TargetMachineAllocator)(Config));
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;
114
115   Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(),
116                             TD.getPointerAlignment(), TD.getDoubleAlignment()));
117
118   // Figure out where we are going to send the output...
119   std::ostream *Out = 0;
120   if (OutputFilename != "") {
121     // Specified an output filename?
122     if (!Force && std::ifstream(OutputFilename.c_str())) {
123       // If force is not specified, make sure not to overwrite a file!
124       std::cerr << argv[0] << ": error opening '" << OutputFilename
125                 << "': file exists!\n"
126                 << "Use -f command line argument to force output\n";
127       return 1;
128     }
129     Out = new std::ofstream(OutputFilename.c_str());
130
131     // Make sure that the Out file gets unlink'd from the disk if we get a
132     // SIGINT
133     RemoveFileOnSignal(OutputFilename);
134   } else {
135     if (InputFilename == "-") {
136       OutputFilename = "-";
137       Out = &std::cout;
138     } else {
139       OutputFilename = GetFileNameRoot(InputFilename); 
140       OutputFilename += ".s";
141       
142       if (!Force && std::ifstream(OutputFilename.c_str())) {
143         // If force is not specified, make sure not to overwrite a file!
144         std::cerr << argv[0] << ": error opening '" << OutputFilename
145                   << "': file exists!\n"
146                   << "Use -f command line argument to force output\n";
147         return 1;
148       }
149       
150       Out = new std::ofstream(OutputFilename.c_str());
151       if (!Out->good()) {
152         std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
153         delete Out;
154         return 1;
155       }
156       
157       // Make sure that the Out file gets unlink'd from the disk if we get a
158       // SIGINT
159       RemoveFileOnSignal(OutputFilename);
160     }
161   }
162
163   // Ask the target to add backend passes as necessary
164   if (Target.addPassesToEmitAssembly(Passes, *Out)) {
165     std::cerr << argv[0] << ": target '" << Target.getName()
166               << "' does not support static compilation!\n";
167     if (Out != &std::cout) delete Out;
168     // And the Out file is empty and useless, so remove it now.
169     std::remove(OutputFilename.c_str());
170     return 1;
171   } else {
172     // Run our queue of passes all at once now, efficiently.
173     Passes.run(*M.get());
174   }
175
176   // Delete the ostream if it's not a stdout stream
177   if (Out != &std::cout) delete Out;
178
179   return 0;
180 }