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" 19 #include "llvm/IR/Verifier.h" 20 #include "llvm/AsmParser/Parser.h" 21 #include "llvm/Bitcode/ReaderWriter.h" 22 #include "llvm/IR/Module.h" 23 #include "llvm/Support/CommandLine.h" 24 #include "llvm/Support/FileSystem.h" 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 32 #include "slang_bitcode_gen.h" 33 #include "slang_version.h" 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 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 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 63 64 static void WriteOutputFile(const Module *M, uint32_t ModuleTargetAPI) { 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 82 std::error_code EC; 83 std::unique_ptr<tool_output_file> Out 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'; 88 exit(1); 89 } 90 91 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true)) { 92 slang::writeBitcode(Out->os(), *M, 93 /* TargetAPI = */ ModuleTargetAPI, 94 /* OptimizationLevel = */ 3, 95 /* GenerateDebugInfo = */ false); 96 97 if (!Out->os().has_error()) { 98 // Declare success. 99 Out->keep(); 100 } 101 } 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 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 124 // Parse the file now... 125 SMDiagnostic Err; 126 std::unique_ptr<Module> M(parseAssemblyFile(InputFilename, Err, Context)); 127 if (M.get() == 0) { 128 Err.print(argv[0], errs()); 129 return 1; 130 } 131 132 if (!DisableVerify) { 133 std::string Err; 134 raw_string_ostream stream(Err); 135 if (verifyModule(*M.get(), &stream)) { 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) 146 WriteOutputFile(M.get(), ActualTargetAPI); 147 148 return 0; 149 } 150