Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame^] | 1 | //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===// |
| 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 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This utility may be invoked in the following manner: |
| 11 | // llvm-as --help - Output information about command line switches |
| 12 | // llvm-as [options] - Read LLVM asm from stdin, write bytecode to stdout |
| 13 | // llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bytecode |
| 14 | // to the x.bc file. |
| 15 | // |
| 16 | //===------------------------------------------------------------------------=== |
| 17 | |
| 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/Assembly/Parser.h" |
| 20 | #include "llvm/Bytecode/Writer.h" |
| 21 | #include "llvm/Analysis/Verifier.h" |
| 22 | #include "llvm/Support/CommandLine.h" |
| 23 | #include "llvm/Support/SystemUtils.h" |
| 24 | #include "llvm/System/Signals.h" |
| 25 | #include "CppWriter.h" |
| 26 | #include <fstream> |
| 27 | #include <iostream> |
| 28 | #include <memory> |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | static cl::opt<std::string> |
| 33 | InputFilename(cl::Positional, cl::desc("<input LLVM assembly file>"), |
| 34 | cl::init("-")); |
| 35 | |
| 36 | static cl::opt<std::string> |
| 37 | OutputFilename("o", cl::desc("Override output filename"), |
| 38 | cl::value_desc("filename")); |
| 39 | |
| 40 | static cl::opt<bool> |
| 41 | Force("f", cl::desc("Overwrite output files")); |
| 42 | |
| 43 | static cl::opt<bool> |
| 44 | DisableVerify("disable-verify", cl::Hidden, |
| 45 | cl::desc("Do not run verifier on input LLVM (dangerous!)")); |
| 46 | |
| 47 | int main(int argc, char **argv) { |
| 48 | cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .cpp assembler\n"); |
| 49 | sys::PrintStackTraceOnErrorSignal(); |
| 50 | |
| 51 | int exitCode = 0; |
| 52 | std::ostream *Out = 0; |
| 53 | try { |
| 54 | // Parse the file now... |
| 55 | std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename)); |
| 56 | if (M.get() == 0) { |
| 57 | std::cerr << argv[0] << ": assembly didn't read correctly.\n"; |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | try { |
| 62 | if (!DisableVerify) |
| 63 | verifyModule(*M.get(), ThrowExceptionAction); |
| 64 | } catch (const std::string &Err) { |
| 65 | std::cerr << argv[0] |
| 66 | << ": assembly parsed, but does not verify as correct!\n"; |
| 67 | std::cerr << Err; |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | if (OutputFilename != "") { // Specified an output filename? |
| 72 | if (OutputFilename != "-") { // Not stdout? |
| 73 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
| 74 | // If force is not specified, make sure not to overwrite a file! |
| 75 | std::cerr << argv[0] << ": error opening '" << OutputFilename |
| 76 | << "': file exists!\n" |
| 77 | << "Use -f command line argument to force output\n"; |
| 78 | return 1; |
| 79 | } |
| 80 | Out = new std::ofstream(OutputFilename.c_str(), std::ios::out | |
| 81 | std::ios::trunc | std::ios::binary); |
| 82 | } else { // Specified stdout |
| 83 | // FIXME: cout is not binary! |
| 84 | Out = &std::cout; |
| 85 | } |
| 86 | } else { |
| 87 | if (InputFilename == "-") { |
| 88 | OutputFilename = "-"; |
| 89 | Out = &std::cout; |
| 90 | } else { |
| 91 | std::string IFN = InputFilename; |
| 92 | int Len = IFN.length(); |
| 93 | if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') { |
| 94 | // Source ends in .ll |
| 95 | OutputFilename = std::string(IFN.begin(), IFN.end()-3); |
| 96 | } else { |
| 97 | OutputFilename = IFN; // Append a .cpp to it |
| 98 | } |
| 99 | OutputFilename += ".cpp"; |
| 100 | |
| 101 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
| 102 | // If force is not specified, make sure not to overwrite a file! |
| 103 | std::cerr << argv[0] << ": error opening '" << OutputFilename |
| 104 | << "': file exists!\n" |
| 105 | << "Use -f command line argument to force output\n"; |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | Out = new std::ofstream(OutputFilename.c_str(), std::ios::out | |
| 110 | std::ios::trunc | std::ios::binary); |
| 111 | // Make sure that the Out file gets unlinked from the disk if we get a |
| 112 | // SIGINT |
| 113 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (!Out->good()) { |
| 118 | std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | WriteModuleToCppFile(M.get(), *Out); |
| 123 | |
| 124 | } catch (const ParseException &E) { |
| 125 | std::cerr << argv[0] << ": " << E.getMessage() << "\n"; |
| 126 | exitCode = 1; |
| 127 | } catch (const std::string& msg) { |
| 128 | std::cerr << argv[0] << ": " << msg << "\n"; |
| 129 | exitCode = 1; |
| 130 | } catch (...) { |
| 131 | std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; |
| 132 | exitCode = 1; |
| 133 | } |
| 134 | |
| 135 | if (Out != &std::cout) delete Out; |
| 136 | return exitCode; |
| 137 | } |
| 138 | |