Chris Lattner | 5b836c4 | 2003-06-20 15:49:04 +0000 | [diff] [blame] | 1 | //===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===// |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | e737c7a | 2001-09-07 22:20:50 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 5b836c4 | 2003-06-20 15:49:04 +0000 | [diff] [blame] | 10 | // This is the llc code generator. |
Chris Lattner | e737c7a | 2001-09-07 22:20:50 +0000 | [diff] [blame] | 11 | // |
Chris Lattner | b79757c | 2001-10-04 01:40:53 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | cb465fc | 2001-07-21 12:42:29 +0000 | [diff] [blame] | 13 | |
Vikram S. Adve | cb465fc | 2001-07-21 12:42:29 +0000 | [diff] [blame] | 14 | #include "llvm/Bytecode/Reader.h" |
Chris Lattner | 4aba5e3 | 2002-10-29 20:45:04 +0000 | [diff] [blame] | 15 | #include "llvm/Target/TargetMachineImpls.h" |
Vikram S. Adve | 805eb96 | 2001-09-18 13:10:45 +0000 | [diff] [blame] | 16 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 65f1b89 | 2002-05-07 20:03:27 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 46ac43c | 2001-09-07 21:26:31 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Chris Lattner | cd50d3f | 2002-01-31 00:46:45 +0000 | [diff] [blame] | 19 | #include "llvm/PassManager.h" |
Vikram S. Adve | 7d0ba02 | 2002-09-16 16:35:34 +0000 | [diff] [blame] | 20 | #include "llvm/Pass.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 21 | #include "Support/CommandLine.h" |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 22 | #include "Support/Signals.h" |
Chris Lattner | da784ee | 2001-09-18 17:04:18 +0000 | [diff] [blame] | 23 | #include <memory> |
Chris Lattner | 78f7e1a | 2001-09-19 16:52:09 +0000 | [diff] [blame] | 24 | #include <fstream> |
Vikram S. Adve | 7d0ba02 | 2002-09-16 16:35:34 +0000 | [diff] [blame] | 25 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame^] | 26 | using namespace llvm; |
| 27 | |
Vikram S. Adve | 7d0ba02 | 2002-09-16 16:35:34 +0000 | [diff] [blame] | 28 | // General options for llc. Other pass-specific options are specified |
| 29 | // within the corresponding llc passes, and target-specific options |
| 30 | // and back-end code generation options are specified with the target machine. |
| 31 | // |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 32 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 33 | InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-")); |
| 34 | |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 35 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 36 | OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename")); |
| 37 | |
| 38 | static cl::opt<bool> Force("f", cl::desc("Overwrite output files")); |
| 39 | |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 40 | enum ArchName { noarch, x86, Sparc }; |
Chris Lattner | 6849272 | 2003-04-28 03:28:56 +0000 | [diff] [blame] | 41 | |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 42 | static cl::opt<ArchName> |
| 43 | Arch("march", cl::desc("Architecture to generate assembly for:"), cl::Prefix, |
| 44 | cl::values(clEnumVal(x86, " IA-32 (Pentium and above)"), |
Misha Brukman | 29abc97 | 2003-07-07 22:28:42 +0000 | [diff] [blame] | 45 | clEnumValN(Sparc, "sparc", " SPARC V9"), |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 46 | 0), |
Misha Brukman | 5d5bc7b | 2003-07-30 15:29:55 +0000 | [diff] [blame] | 47 | cl::init(noarch)); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 3524fc2 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 49 | // GetFileNameRoot - Helper function to get the basename of a filename... |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 50 | static inline std::string |
| 51 | GetFileNameRoot(const std::string &InputFilename) |
Vikram S. Adve | 7d0ba02 | 2002-09-16 16:35:34 +0000 | [diff] [blame] | 52 | { |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 53 | std::string IFN = InputFilename; |
| 54 | std::string outputFilename; |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 55 | int Len = IFN.length(); |
John Criswell | b5d09bf | 2003-08-28 21:42:29 +0000 | [diff] [blame] | 56 | if ((Len > 2) && |
| 57 | IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') { |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 58 | outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/ |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 59 | } else { |
Chris Lattner | 3524fc2 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 60 | outputFilename = IFN; |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 61 | } |
| 62 | return outputFilename; |
| 63 | } |
| 64 | |
Vikram S. Adve | 805eb96 | 2001-09-18 13:10:45 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 5b836c4 | 2003-06-20 15:49:04 +0000 | [diff] [blame] | 66 | // main - Entry point for the llc compiler. |
| 67 | // |
| 68 | int main(int argc, char **argv) { |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 69 | cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n"); |
| 70 | |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 71 | // Load the module to be compiled... |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 72 | std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); |
Chris Lattner | 5b836c4 | 2003-06-20 15:49:04 +0000 | [diff] [blame] | 73 | if (M.get() == 0) { |
| 74 | std::cerr << argv[0] << ": bytecode didn't read correctly.\n"; |
| 75 | return 1; |
| 76 | } |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 77 | Module &mod = *M.get(); |
| 78 | |
| 79 | // Allocate target machine. First, check whether the user has |
| 80 | // explicitly specified an architecture to compile for. |
Chris Lattner | 62c720a | 2003-08-24 19:50:12 +0000 | [diff] [blame] | 81 | TargetMachine* (*TargetMachineAllocator)(const Module&) = 0; |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 82 | switch (Arch) { |
| 83 | case x86: |
| 84 | TargetMachineAllocator = allocateX86TargetMachine; |
| 85 | break; |
| 86 | case Sparc: |
| 87 | TargetMachineAllocator = allocateSparcTargetMachine; |
| 88 | break; |
| 89 | default: |
| 90 | // Decide what the default target machine should be, by looking at |
| 91 | // the module. This heuristic (ILP32, LE -> IA32; LP64, BE -> |
| 92 | // SPARCV9) is kind of gross, but it will work until we have more |
| 93 | // sophisticated target information to work from. |
Chris Lattner | bb43350 | 2003-08-24 14:02:14 +0000 | [diff] [blame] | 94 | if (mod.getEndianness() == Module::LittleEndian && |
| 95 | mod.getPointerSize() == Module::Pointer32) { |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 96 | TargetMachineAllocator = allocateX86TargetMachine; |
Chris Lattner | bb43350 | 2003-08-24 14:02:14 +0000 | [diff] [blame] | 97 | } else if (mod.getEndianness() == Module::BigEndian && |
| 98 | mod.getPointerSize() == Module::Pointer64) { |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 99 | TargetMachineAllocator = allocateSparcTargetMachine; |
| 100 | } else { |
Chris Lattner | bb43350 | 2003-08-24 14:02:14 +0000 | [diff] [blame] | 101 | // If the module is target independent, favor a target which matches the |
| 102 | // current build system. |
| 103 | #if defined(i386) || defined(__i386__) || defined(__x86__) |
| 104 | TargetMachineAllocator = allocateX86TargetMachine; |
| 105 | #elif defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
| 106 | TargetMachineAllocator = allocateSparcTargetMachine; |
| 107 | #else |
| 108 | std::cerr << argv[0] << ": module does not specify a target to use. " |
| 109 | << "You must use the -march option.\n"; |
| 110 | return 1; |
| 111 | #endif |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 112 | } |
| 113 | break; |
| 114 | } |
Chris Lattner | 62c720a | 2003-08-24 19:50:12 +0000 | [diff] [blame] | 115 | std::auto_ptr<TargetMachine> target(TargetMachineAllocator(mod)); |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 116 | assert(target.get() && "Could not allocate target machine!"); |
| 117 | TargetMachine &Target = *target.get(); |
| 118 | const TargetData &TD = Target.getTargetData(); |
Chris Lattner | 39fd659 | 2002-05-20 21:20:08 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 3524fc2 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 120 | // Build up all of the passes that we want to do to the module... |
Chris Lattner | f4de63f | 2002-01-21 07:31:50 +0000 | [diff] [blame] | 121 | PassManager Passes; |
Chris Lattner | 3524fc2 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 10daaa1 | 2003-04-26 20:11:09 +0000 | [diff] [blame] | 123 | Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(), |
Chris Lattner | bc19466 | 2003-04-25 06:06:13 +0000 | [diff] [blame] | 124 | TD.getPointerAlignment(), TD.getDoubleAlignment())); |
Chris Lattner | 2b5f2c1 | 2003-04-25 05:22:29 +0000 | [diff] [blame] | 125 | |
Chris Lattner | e41576d | 2002-02-03 23:43:19 +0000 | [diff] [blame] | 126 | // Figure out where we are going to send the output... |
| 127 | std::ostream *Out = 0; |
Chris Lattner | cccc28c | 2003-06-18 18:46:08 +0000 | [diff] [blame] | 128 | if (OutputFilename != "") { |
Brian Gaeke | 5ce1a58 | 2003-06-18 21:43:33 +0000 | [diff] [blame] | 129 | if (OutputFilename != "-") { |
| 130 | // Specified an output filename? |
| 131 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
| 132 | // If force is not specified, make sure not to overwrite a file! |
| 133 | std::cerr << argv[0] << ": error opening '" << OutputFilename |
| 134 | << "': file exists!\n" |
| 135 | << "Use -f command line argument to force output\n"; |
| 136 | return 1; |
| 137 | } |
| 138 | Out = new std::ofstream(OutputFilename.c_str()); |
Chris Lattner | cccc28c | 2003-06-18 18:46:08 +0000 | [diff] [blame] | 139 | |
Misha Brukman | 452fea9 | 2003-10-10 17:56:49 +0000 | [diff] [blame] | 140 | // Make sure that the Out file gets unlinked from the disk if we get a |
Brian Gaeke | 5ce1a58 | 2003-06-18 21:43:33 +0000 | [diff] [blame] | 141 | // SIGINT |
| 142 | RemoveFileOnSignal(OutputFilename); |
| 143 | } else { |
| 144 | Out = &std::cout; |
| 145 | } |
Chris Lattner | cccc28c | 2003-06-18 18:46:08 +0000 | [diff] [blame] | 146 | } else { |
| 147 | if (InputFilename == "-") { |
| 148 | OutputFilename = "-"; |
| 149 | Out = &std::cout; |
| 150 | } else { |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 151 | OutputFilename = GetFileNameRoot(InputFilename); |
Chris Lattner | cccc28c | 2003-06-18 18:46:08 +0000 | [diff] [blame] | 152 | OutputFilename += ".s"; |
| 153 | |
Chris Lattner | 888912d | 2002-01-22 21:07:24 +0000 | [diff] [blame] | 154 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 155 | // If force is not specified, make sure not to overwrite a file! |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 156 | std::cerr << argv[0] << ": error opening '" << OutputFilename |
| 157 | << "': file exists!\n" |
| 158 | << "Use -f command line argument to force output\n"; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 159 | return 1; |
| 160 | } |
Chris Lattner | cccc28c | 2003-06-18 18:46:08 +0000 | [diff] [blame] | 161 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 162 | Out = new std::ofstream(OutputFilename.c_str()); |
Chris Lattner | cccc28c | 2003-06-18 18:46:08 +0000 | [diff] [blame] | 163 | if (!Out->good()) { |
| 164 | std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; |
| 165 | delete Out; |
| 166 | return 1; |
| 167 | } |
| 168 | |
Misha Brukman | 452fea9 | 2003-10-10 17:56:49 +0000 | [diff] [blame] | 169 | // Make sure that the Out file gets unlinked from the disk if we get a |
Chris Lattner | 0f82df4 | 2002-09-19 16:06:28 +0000 | [diff] [blame] | 170 | // SIGINT |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 171 | RemoveFileOnSignal(OutputFilename); |
Chris Lattner | 3524fc2 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 172 | } |
Chris Lattner | cccc28c | 2003-06-18 18:46:08 +0000 | [diff] [blame] | 173 | } |
Vikram S. Adve | 7d0ba02 | 2002-09-16 16:35:34 +0000 | [diff] [blame] | 174 | |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 175 | // Ask the target to add backend passes as necessary |
Chris Lattner | 6334205 | 2002-10-29 21:12:46 +0000 | [diff] [blame] | 176 | if (Target.addPassesToEmitAssembly(Passes, *Out)) { |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 177 | std::cerr << argv[0] << ": target '" << Target.getName() |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 178 | << "' does not support static compilation!\n"; |
| 179 | if (Out != &std::cout) delete Out; |
| 180 | // And the Out file is empty and useless, so remove it now. |
| 181 | std::remove(OutputFilename.c_str()); |
| 182 | return 1; |
Chris Lattner | 6334205 | 2002-10-29 21:12:46 +0000 | [diff] [blame] | 183 | } else { |
| 184 | // Run our queue of passes all at once now, efficiently. |
| 185 | Passes.run(*M.get()); |
| 186 | } |
Chris Lattner | 05e5e07 | 2001-10-18 01:31:22 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 0f82df4 | 2002-09-19 16:06:28 +0000 | [diff] [blame] | 188 | // Delete the ostream if it's not a stdout stream |
Chris Lattner | e41576d | 2002-02-03 23:43:19 +0000 | [diff] [blame] | 189 | if (Out != &std::cout) delete Out; |
| 190 | |
Chris Lattner | d7477ee | 2001-10-18 20:33:21 +0000 | [diff] [blame] | 191 | return 0; |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 192 | } |