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 "StripUnkAttr/strip_unknown_attributes.h" 36 37 #include <memory> 38 using namespace llvm; 39 40 static cl::opt<std::string> 41 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-")); 42 43 static cl::opt<std::string> 44 OutputFilename("o", cl::desc("Override output filename"), 45 cl::value_desc("filename")); 46 47 static cl::opt<bool> 48 Force("f", cl::desc("Enable binary output on terminals")); 49 50 static cl::opt<bool> 51 DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false)); 52 53 static cl::opt<uint32_t> 54 TargetAPI("target-api", cl::desc("Specify RenderScript target API version " 55 "(0 = development API) (default is 0)"), 56 cl::init(0)); 57 58 static cl::opt<bool> 59 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden); 60 61 static cl::opt<bool> 62 DisableVerify("disable-verify", cl::Hidden, 63 cl::desc("Do not run verifier on input LLVM (dangerous!)")); 64 65 static void stripUnknownAttributes(llvm::Module *M) { 66 for (llvm::Function &F : *M) 67 slang::stripUnknownAttributes(F); 68 } 69 70 static void WriteOutputFile(const Module *M, uint32_t ModuleTargetAPI) { 71 // Infer the output filename if needed. 72 if (OutputFilename.empty()) { 73 if (InputFilename == "-") { 74 OutputFilename = "-"; 75 } else { 76 std::string IFN = InputFilename; 77 int Len = IFN.length(); 78 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') { 79 // Source ends in .ll 80 OutputFilename = std::string(IFN.begin(), IFN.end()-3); 81 } else { 82 OutputFilename = IFN; // Append a .bc to it 83 } 84 OutputFilename += ".bc"; 85 } 86 } 87 88 std::error_code EC; 89 std::unique_ptr<tool_output_file> Out 90 (new tool_output_file(OutputFilename.c_str(), EC, llvm::sys::fs::F_None)); 91 if (EC) { 92 // TODO(srhines): This isn't actually very specific and needs cleanup. 93 errs() << EC.message() << '\n'; 94 exit(1); 95 } 96 97 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true)) { 98 slang::writeBitcode(Out->os(), *M, 99 /* TargetAPI = */ ModuleTargetAPI, 100 /* OptimizationLevel = */ 3, 101 /* GenerateDebugInfo = */ false); 102 103 if (!Out->os().has_error()) { 104 // Declare success. 105 Out->keep(); 106 } 107 } 108 } 109 110 int main(int argc, char **argv) { 111 // Print a stack trace if we signal out. 112 sys::PrintStackTraceOnErrorSignal(argv[0]); 113 PrettyStackTraceProgram X(argc, argv); 114 LLVMContext Context; 115 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 116 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n"); 117 118 // Check target API. 119 uint32_t ActualTargetAPI = (TargetAPI == 0) ? RS_DEVELOPMENT_API : TargetAPI; 120 121 if (ActualTargetAPI != RS_DEVELOPMENT_API && 122 (ActualTargetAPI < SLANG_MINIMUM_TARGET_API || 123 ActualTargetAPI > SLANG_MAXIMUM_TARGET_API)) { 124 errs() << "target API level '" << ActualTargetAPI << "' is out of range " 125 << "('" << SLANG_MINIMUM_TARGET_API << "' - '" 126 << SLANG_MAXIMUM_TARGET_API << "')\n"; 127 return 1; 128 } 129 130 // Parse the file now... 131 SMDiagnostic Err; 132 std::unique_ptr<Module> M(parseAssemblyFile(InputFilename, Err, Context)); 133 if (M.get() == 0) { 134 Err.print(argv[0], errs()); 135 return 1; 136 } 137 138 if (!DisableVerify) { 139 std::string Err; 140 raw_string_ostream stream(Err); 141 if (verifyModule(*M.get(), &stream)) { 142 errs() << argv[0] 143 << ": assembly parsed, but does not verify as correct!\n"; 144 errs() << Err; 145 return 1; 146 } 147 } 148 149 stripUnknownAttributes(M.get()); 150 151 if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get(); 152 153 if (!DisableOutput) 154 WriteOutputFile(M.get(), ActualTargetAPI); 155 156 return 0; 157 } 158