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 | // |
Brian Gaeke | e40eae7 | 2004-03-16 21:47:20 +0000 | [diff] [blame] | 10 | // This is the llc code generator driver. It provides a convenient |
| 11 | // command-line interface for generating native assembly-language code |
| 12 | // or C code, given LLVM bytecode. |
Chris Lattner | e737c7a | 2001-09-07 22:20:50 +0000 | [diff] [blame] | 13 | // |
Chris Lattner | b79757c | 2001-10-04 01:40:53 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | cb465fc | 2001-07-21 12:42:29 +0000 | [diff] [blame] | 15 | |
Vikram S. Adve | cb465fc | 2001-07-21 12:42:29 +0000 | [diff] [blame] | 16 | #include "llvm/Bytecode/Reader.h" |
Vikram S. Adve | 805eb96 | 2001-09-18 13:10:45 +0000 | [diff] [blame] | 17 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | e45110e | 2004-07-11 04:03:24 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetMachineRegistry.h" |
Chris Lattner | 65f1b89 | 2002-05-07 20:03:27 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 46ac43c | 2001-09-07 21:26:31 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Chris Lattner | cd50d3f | 2002-01-31 00:46:45 +0000 | [diff] [blame] | 21 | #include "llvm/PassManager.h" |
Vikram S. Adve | 7d0ba02 | 2002-09-16 16:35:34 +0000 | [diff] [blame] | 22 | #include "llvm/Pass.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame^] | 23 | #include "llvm/Support/CommandLine.h" |
| 24 | #include "llvm/Support/PluginLoader.h" |
Chris Lattner | bed85ff | 2004-05-27 05:41:36 +0000 | [diff] [blame] | 25 | #include "llvm/System/Signals.h" |
Chris Lattner | 78f7e1a | 2001-09-19 16:52:09 +0000 | [diff] [blame] | 26 | #include <fstream> |
Reid Spencer | 86f42bd | 2004-07-04 12:20:55 +0000 | [diff] [blame] | 27 | #include <iostream> |
| 28 | #include <memory> |
Vikram S. Adve | 7d0ba02 | 2002-09-16 16:35:34 +0000 | [diff] [blame] | 29 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Vikram S. Adve | 7d0ba02 | 2002-09-16 16:35:34 +0000 | [diff] [blame] | 32 | // General options for llc. Other pass-specific options are specified |
| 33 | // within the corresponding llc passes, and target-specific options |
| 34 | // and back-end code generation options are specified with the target machine. |
| 35 | // |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 36 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 37 | InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-")); |
| 38 | |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 39 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 40 | OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename")); |
| 41 | |
| 42 | static cl::opt<bool> Force("f", cl::desc("Overwrite output files")); |
| 43 | |
Chris Lattner | e45110e | 2004-07-11 04:03:24 +0000 | [diff] [blame] | 44 | static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser> |
| 45 | MArch("march", cl::desc("Architecture to generate assembly for:")); |
| 46 | |
Chris Lattner | 3524fc2 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 47 | // GetFileNameRoot - Helper function to get the basename of a filename... |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 48 | static inline std::string |
Chris Lattner | e45110e | 2004-07-11 04:03:24 +0000 | [diff] [blame] | 49 | GetFileNameRoot(const std::string &InputFilename) { |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 50 | std::string IFN = InputFilename; |
| 51 | std::string outputFilename; |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 52 | int Len = IFN.length(); |
John Criswell | b5d09bf | 2003-08-28 21:42:29 +0000 | [diff] [blame] | 53 | if ((Len > 2) && |
| 54 | IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') { |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 55 | outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/ |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 56 | } else { |
Chris Lattner | 3524fc2 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 57 | outputFilename = IFN; |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 58 | } |
| 59 | return outputFilename; |
| 60 | } |
| 61 | |
Vikram S. Adve | 805eb96 | 2001-09-18 13:10:45 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 5b836c4 | 2003-06-20 15:49:04 +0000 | [diff] [blame] | 63 | // main - Entry point for the llc compiler. |
| 64 | // |
| 65 | int main(int argc, char **argv) { |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 66 | cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n"); |
Reid Spencer | 9de7b33 | 2004-08-29 19:28:55 +0000 | [diff] [blame] | 67 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | 364d120 | 2004-02-19 20:32:39 +0000 | [diff] [blame] | 68 | |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 69 | // Load the module to be compiled... |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 70 | std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); |
Chris Lattner | 5b836c4 | 2003-06-20 15:49:04 +0000 | [diff] [blame] | 71 | if (M.get() == 0) { |
| 72 | std::cerr << argv[0] << ": bytecode didn't read correctly.\n"; |
| 73 | return 1; |
| 74 | } |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 75 | Module &mod = *M.get(); |
| 76 | |
| 77 | // Allocate target machine. First, check whether the user has |
| 78 | // explicitly specified an architecture to compile for. |
Chris Lattner | 6fb6ce3 | 2003-12-28 09:51:04 +0000 | [diff] [blame] | 79 | TargetMachine* (*TargetMachineAllocator)(const Module&, |
| 80 | IntrinsicLowering *) = 0; |
Chris Lattner | e45110e | 2004-07-11 04:03:24 +0000 | [diff] [blame] | 81 | if (MArch == 0) { |
| 82 | std::string Err; |
| 83 | MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err); |
| 84 | if (MArch == 0) { |
| 85 | std::cerr << argv[0] << ": error auto-selecting target for module '" |
| 86 | << Err << "'. Please use the -march option to explicitly " |
| 87 | << "pick a target.\n"; |
Chris Lattner | bb43350 | 2003-08-24 14:02:14 +0000 | [diff] [blame] | 88 | return 1; |
Chris Lattner | e45110e | 2004-07-11 04:03:24 +0000 | [diff] [blame] | 89 | } |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 90 | } |
Chris Lattner | e45110e | 2004-07-11 04:03:24 +0000 | [diff] [blame] | 91 | |
| 92 | std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, 0)); |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 93 | assert(target.get() && "Could not allocate target machine!"); |
| 94 | TargetMachine &Target = *target.get(); |
| 95 | const TargetData &TD = Target.getTargetData(); |
Chris Lattner | 39fd659 | 2002-05-20 21:20:08 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 3524fc2 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 97 | // 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] | 98 | PassManager Passes; |
Chris Lattner | 10daaa1 | 2003-04-26 20:11:09 +0000 | [diff] [blame] | 99 | Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(), |
Chris Lattner | bc19466 | 2003-04-25 06:06:13 +0000 | [diff] [blame] | 100 | TD.getPointerAlignment(), TD.getDoubleAlignment())); |
Chris Lattner | 2b5f2c1 | 2003-04-25 05:22:29 +0000 | [diff] [blame] | 101 | |
Chris Lattner | e41576d | 2002-02-03 23:43:19 +0000 | [diff] [blame] | 102 | // Figure out where we are going to send the output... |
| 103 | std::ostream *Out = 0; |
Chris Lattner | cccc28c | 2003-06-18 18:46:08 +0000 | [diff] [blame] | 104 | if (OutputFilename != "") { |
Brian Gaeke | 5ce1a58 | 2003-06-18 21:43:33 +0000 | [diff] [blame] | 105 | if (OutputFilename != "-") { |
| 106 | // Specified an output filename? |
| 107 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
Misha Brukman | 878ba7c | 2004-07-12 22:58:07 +0000 | [diff] [blame] | 108 | // If force is not specified, make sure not to overwrite a file! |
| 109 | std::cerr << argv[0] << ": error opening '" << OutputFilename |
| 110 | << "': file exists!\n" |
| 111 | << "Use -f command line argument to force output\n"; |
| 112 | return 1; |
Brian Gaeke | 5ce1a58 | 2003-06-18 21:43:33 +0000 | [diff] [blame] | 113 | } |
| 114 | Out = new std::ofstream(OutputFilename.c_str()); |
Chris Lattner | cccc28c | 2003-06-18 18:46:08 +0000 | [diff] [blame] | 115 | |
Misha Brukman | 452fea9 | 2003-10-10 17:56:49 +0000 | [diff] [blame] | 116 | // 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] | 117 | // SIGINT |
Reid Spencer | 9de7b33 | 2004-08-29 19:28:55 +0000 | [diff] [blame] | 118 | sys::RemoveFileOnSignal(OutputFilename); |
Brian Gaeke | 5ce1a58 | 2003-06-18 21:43:33 +0000 | [diff] [blame] | 119 | } else { |
| 120 | Out = &std::cout; |
| 121 | } |
Chris Lattner | cccc28c | 2003-06-18 18:46:08 +0000 | [diff] [blame] | 122 | } else { |
| 123 | if (InputFilename == "-") { |
| 124 | OutputFilename = "-"; |
| 125 | Out = &std::cout; |
| 126 | } else { |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 127 | OutputFilename = GetFileNameRoot(InputFilename); |
Chris Lattner | 74661c8 | 2004-02-15 22:54:19 +0000 | [diff] [blame] | 128 | |
Chris Lattner | e45110e | 2004-07-11 04:03:24 +0000 | [diff] [blame] | 129 | if (MArch->Name[0] != 'c' || MArch->Name[1] != 0) // not CBE |
Chris Lattner | 74661c8 | 2004-02-15 22:54:19 +0000 | [diff] [blame] | 130 | OutputFilename += ".s"; |
| 131 | else |
| 132 | OutputFilename += ".cbe.c"; |
Chris Lattner | cccc28c | 2003-06-18 18:46:08 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 888912d | 2002-01-22 21:07:24 +0000 | [diff] [blame] | 134 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 135 | // If force is not specified, make sure not to overwrite a file! |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 136 | std::cerr << argv[0] << ": error opening '" << OutputFilename |
| 137 | << "': file exists!\n" |
| 138 | << "Use -f command line argument to force output\n"; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 139 | return 1; |
| 140 | } |
Chris Lattner | cccc28c | 2003-06-18 18:46:08 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 142 | Out = new std::ofstream(OutputFilename.c_str()); |
Chris Lattner | cccc28c | 2003-06-18 18:46:08 +0000 | [diff] [blame] | 143 | if (!Out->good()) { |
| 144 | std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; |
| 145 | delete Out; |
| 146 | return 1; |
| 147 | } |
| 148 | |
Misha Brukman | 452fea9 | 2003-10-10 17:56:49 +0000 | [diff] [blame] | 149 | // 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] | 150 | // SIGINT |
Reid Spencer | 9de7b33 | 2004-08-29 19:28:55 +0000 | [diff] [blame] | 151 | sys::RemoveFileOnSignal(OutputFilename); |
Chris Lattner | 3524fc2 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 152 | } |
Chris Lattner | cccc28c | 2003-06-18 18:46:08 +0000 | [diff] [blame] | 153 | } |
Vikram S. Adve | 7d0ba02 | 2002-09-16 16:35:34 +0000 | [diff] [blame] | 154 | |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 155 | // Ask the target to add backend passes as necessary |
Chris Lattner | 6334205 | 2002-10-29 21:12:46 +0000 | [diff] [blame] | 156 | if (Target.addPassesToEmitAssembly(Passes, *Out)) { |
Chris Lattner | b5881f1 | 2003-04-25 05:26:11 +0000 | [diff] [blame] | 157 | std::cerr << argv[0] << ": target '" << Target.getName() |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 158 | << "' 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 Lattner | 6334205 | 2002-10-29 21:12:46 +0000 | [diff] [blame] | 163 | } else { |
| 164 | // Run our queue of passes all at once now, efficiently. |
| 165 | Passes.run(*M.get()); |
| 166 | } |
Chris Lattner | 05e5e07 | 2001-10-18 01:31:22 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 0f82df4 | 2002-09-19 16:06:28 +0000 | [diff] [blame] | 168 | // Delete the ostream if it's not a stdout stream |
Chris Lattner | e41576d | 2002-02-03 23:43:19 +0000 | [diff] [blame] | 169 | if (Out != &std::cout) delete Out; |
| 170 | |
Chris Lattner | d7477ee | 2001-10-18 20:33:21 +0000 | [diff] [blame] | 171 | return 0; |
Vikram S. Adve | 2f64f9f | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 172 | } |