Tobias Grosser | c908b90 | 2013-06-21 11:28:49 -0700 | [diff] [blame] | 1 | //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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 bitcode to stdout |
| 13 | // llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bitcode |
| 14 | // to the x.bc file. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "llvm/IR/LLVMContext.h" |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 19 | #include "llvm/IR/Verifier.h" |
| 20 | #include "llvm/AsmParser/Parser.h" |
Tobias Grosser | c908b90 | 2013-06-21 11:28:49 -0700 | [diff] [blame] | 21 | #include "llvm/Bitcode/ReaderWriter.h" |
| 22 | #include "llvm/IR/Module.h" |
| 23 | #include "llvm/Support/CommandLine.h" |
Stephen Hines | 75d4718 | 2014-05-28 09:43:18 -0700 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
Tobias Grosser | c908b90 | 2013-06-21 11:28:49 -0700 | [diff] [blame] | 25 | #include "llvm/Support/ManagedStatic.h" |
| 26 | #include "llvm/Support/PrettyStackTrace.h" |
| 27 | #include "llvm/Support/Signals.h" |
| 28 | #include "llvm/Support/SourceMgr.h" |
| 29 | #include "llvm/Support/SystemUtils.h" |
| 30 | #include "llvm/Support/ToolOutputFile.h" |
| 31 | |
Matt Wala | dabd246 | 2015-07-15 18:39:41 -0700 | [diff] [blame] | 32 | #include "slang_bitcode_gen.h" |
| 33 | #include "slang_version.h" |
Tobias Grosser | c908b90 | 2013-06-21 11:28:49 -0700 | [diff] [blame] | 34 | |
| 35 | #include <memory> |
| 36 | using namespace llvm; |
| 37 | |
| 38 | static cl::opt<std::string> |
| 39 | InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-")); |
| 40 | |
| 41 | static cl::opt<std::string> |
| 42 | OutputFilename("o", cl::desc("Override output filename"), |
| 43 | cl::value_desc("filename")); |
| 44 | |
| 45 | static cl::opt<bool> |
| 46 | Force("f", cl::desc("Enable binary output on terminals")); |
| 47 | |
| 48 | static cl::opt<bool> |
| 49 | DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false)); |
| 50 | |
Matt Wala | dabd246 | 2015-07-15 18:39:41 -0700 | [diff] [blame] | 51 | static cl::opt<uint32_t> |
| 52 | TargetAPI("target-api", cl::desc("Specify RenderScript target API version " |
| 53 | "(0 = development API) (default is 0)"), |
| 54 | cl::init(0)); |
| 55 | |
Tobias Grosser | c908b90 | 2013-06-21 11:28:49 -0700 | [diff] [blame] | 56 | static cl::opt<bool> |
| 57 | DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden); |
| 58 | |
| 59 | static cl::opt<bool> |
| 60 | DisableVerify("disable-verify", cl::Hidden, |
| 61 | cl::desc("Do not run verifier on input LLVM (dangerous!)")); |
| 62 | |
Tobias Grosser | c908b90 | 2013-06-21 11:28:49 -0700 | [diff] [blame] | 63 | |
Matt Wala | dabd246 | 2015-07-15 18:39:41 -0700 | [diff] [blame] | 64 | static void WriteOutputFile(const Module *M, uint32_t ModuleTargetAPI) { |
Tobias Grosser | c908b90 | 2013-06-21 11:28:49 -0700 | [diff] [blame] | 65 | // Infer the output filename if needed. |
| 66 | if (OutputFilename.empty()) { |
| 67 | if (InputFilename == "-") { |
| 68 | OutputFilename = "-"; |
| 69 | } else { |
| 70 | std::string IFN = InputFilename; |
| 71 | int Len = IFN.length(); |
| 72 | if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') { |
| 73 | // Source ends in .ll |
| 74 | OutputFilename = std::string(IFN.begin(), IFN.end()-3); |
| 75 | } else { |
| 76 | OutputFilename = IFN; // Append a .bc to it |
| 77 | } |
| 78 | OutputFilename += ".bc"; |
| 79 | } |
| 80 | } |
| 81 | |
Stephen Hines | 3eb819a | 2014-11-24 18:16:52 -0800 | [diff] [blame] | 82 | std::error_code EC; |
Stephen Hines | 2eb9a3f | 2014-07-15 16:50:03 -0700 | [diff] [blame] | 83 | std::unique_ptr<tool_output_file> Out |
Stephen Hines | 3eb819a | 2014-11-24 18:16:52 -0800 | [diff] [blame] | 84 | (new tool_output_file(OutputFilename.c_str(), EC, llvm::sys::fs::F_None)); |
| 85 | if (EC) { |
| 86 | // TODO(srhines): This isn't actually very specific and needs cleanup. |
| 87 | errs() << EC.message() << '\n'; |
Tobias Grosser | c908b90 | 2013-06-21 11:28:49 -0700 | [diff] [blame] | 88 | exit(1); |
| 89 | } |
| 90 | |
| 91 | if (Force || !CheckBitcodeOutputToConsole(Out->os(), true)) { |
Matt Wala | dabd246 | 2015-07-15 18:39:41 -0700 | [diff] [blame] | 92 | slang::writeBitcode(Out->os(), *M, |
| 93 | /* TargetAPI = */ ModuleTargetAPI, |
Stephen McGroarty | b130e15 | 2015-07-23 16:55:02 +0100 | [diff] [blame] | 94 | /* OptimizationLevel = */ 3, |
| 95 | /* GenerateDebugInfo = */ false); |
Matt Wala | dabd246 | 2015-07-15 18:39:41 -0700 | [diff] [blame] | 96 | |
| 97 | if (!Out->os().has_error()) { |
| 98 | // Declare success. |
| 99 | Out->keep(); |
Tobias Grosser | c908b90 | 2013-06-21 11:28:49 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
Tobias Grosser | c908b90 | 2013-06-21 11:28:49 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | int main(int argc, char **argv) { |
| 105 | // Print a stack trace if we signal out. |
| 106 | sys::PrintStackTraceOnErrorSignal(); |
| 107 | PrettyStackTraceProgram X(argc, argv); |
| 108 | LLVMContext &Context = getGlobalContext(); |
| 109 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 110 | cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n"); |
| 111 | |
Matt Wala | dabd246 | 2015-07-15 18:39:41 -0700 | [diff] [blame] | 112 | // Check target API. |
| 113 | uint32_t ActualTargetAPI = (TargetAPI == 0) ? RS_DEVELOPMENT_API : TargetAPI; |
| 114 | |
| 115 | if (ActualTargetAPI != RS_DEVELOPMENT_API && |
| 116 | (ActualTargetAPI < SLANG_MINIMUM_TARGET_API || |
| 117 | ActualTargetAPI > SLANG_MAXIMUM_TARGET_API)) { |
| 118 | errs() << "target API level '" << ActualTargetAPI << "' is out of range " |
| 119 | << "('" << SLANG_MINIMUM_TARGET_API << "' - '" |
| 120 | << SLANG_MAXIMUM_TARGET_API << "')\n"; |
| 121 | return 1; |
| 122 | } |
| 123 | |
Tobias Grosser | c908b90 | 2013-06-21 11:28:49 -0700 | [diff] [blame] | 124 | // Parse the file now... |
| 125 | SMDiagnostic Err; |
Stephen Hines | 3eb819a | 2014-11-24 18:16:52 -0800 | [diff] [blame] | 126 | std::unique_ptr<Module> M(parseAssemblyFile(InputFilename, Err, Context)); |
Tobias Grosser | c908b90 | 2013-06-21 11:28:49 -0700 | [diff] [blame] | 127 | if (M.get() == 0) { |
| 128 | Err.print(argv[0], errs()); |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | if (!DisableVerify) { |
| 133 | std::string Err; |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 134 | raw_string_ostream stream(Err); |
| 135 | if (verifyModule(*M.get(), &stream)) { |
Tobias Grosser | c908b90 | 2013-06-21 11:28:49 -0700 | [diff] [blame] | 136 | errs() << argv[0] |
| 137 | << ": assembly parsed, but does not verify as correct!\n"; |
| 138 | errs() << Err; |
| 139 | return 1; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get(); |
| 144 | |
| 145 | if (!DisableOutput) |
Matt Wala | dabd246 | 2015-07-15 18:39:41 -0700 | [diff] [blame] | 146 | WriteOutputFile(M.get(), ActualTargetAPI); |
Tobias Grosser | c908b90 | 2013-06-21 11:28:49 -0700 | [diff] [blame] | 147 | |
| 148 | return 0; |
| 149 | } |