blob: c63a1ac762cbe15be0d528da2a0be95aa4a23f34 [file] [log] [blame]
Tobias Grosserc908b902013-06-21 11:28:49 -07001//===--- 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 Murrayee4016d2014-04-10 15:49:08 -070019#include "llvm/IR/Verifier.h"
20#include "llvm/AsmParser/Parser.h"
Tobias Grosserc908b902013-06-21 11:28:49 -070021#include "llvm/Bitcode/ReaderWriter.h"
22#include "llvm/IR/Module.h"
23#include "llvm/Support/CommandLine.h"
Stephen Hines75d47182014-05-28 09:43:18 -070024#include "llvm/Support/FileSystem.h"
Tobias Grosserc908b902013-06-21 11:28:49 -070025#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 Waladabd2462015-07-15 18:39:41 -070032#include "slang_bitcode_gen.h"
33#include "slang_version.h"
Tobias Grosserc908b902013-06-21 11:28:49 -070034
35#include <memory>
36using namespace llvm;
37
38static cl::opt<std::string>
39InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
40
41static cl::opt<std::string>
42OutputFilename("o", cl::desc("Override output filename"),
43 cl::value_desc("filename"));
44
45static cl::opt<bool>
46Force("f", cl::desc("Enable binary output on terminals"));
47
48static cl::opt<bool>
49DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
50
Matt Waladabd2462015-07-15 18:39:41 -070051static cl::opt<uint32_t>
52TargetAPI("target-api", cl::desc("Specify RenderScript target API version "
53 "(0 = development API) (default is 0)"),
54 cl::init(0));
55
Tobias Grosserc908b902013-06-21 11:28:49 -070056static cl::opt<bool>
57DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
58
59static cl::opt<bool>
60DisableVerify("disable-verify", cl::Hidden,
61 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
62
Tobias Grosserc908b902013-06-21 11:28:49 -070063
Matt Waladabd2462015-07-15 18:39:41 -070064static void WriteOutputFile(const Module *M, uint32_t ModuleTargetAPI) {
Tobias Grosserc908b902013-06-21 11:28:49 -070065 // 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 Hines3eb819a2014-11-24 18:16:52 -080082 std::error_code EC;
Stephen Hines2eb9a3f2014-07-15 16:50:03 -070083 std::unique_ptr<tool_output_file> Out
Stephen Hines3eb819a2014-11-24 18:16:52 -080084 (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 Grosserc908b902013-06-21 11:28:49 -070088 exit(1);
89 }
90
91 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true)) {
Matt Waladabd2462015-07-15 18:39:41 -070092 slang::writeBitcode(Out->os(), *M,
93 /* TargetAPI = */ ModuleTargetAPI,
94 /* OptimizationLevel = */ 3);
95
96 if (!Out->os().has_error()) {
97 // Declare success.
98 Out->keep();
Tobias Grosserc908b902013-06-21 11:28:49 -070099 }
100 }
Tobias Grosserc908b902013-06-21 11:28:49 -0700101}
102
103int main(int argc, char **argv) {
104 // Print a stack trace if we signal out.
105 sys::PrintStackTraceOnErrorSignal();
106 PrettyStackTraceProgram X(argc, argv);
107 LLVMContext &Context = getGlobalContext();
108 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
109 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
110
Matt Waladabd2462015-07-15 18:39:41 -0700111 // Check target API.
112 uint32_t ActualTargetAPI = (TargetAPI == 0) ? RS_DEVELOPMENT_API : TargetAPI;
113
114 if (ActualTargetAPI != RS_DEVELOPMENT_API &&
115 (ActualTargetAPI < SLANG_MINIMUM_TARGET_API ||
116 ActualTargetAPI > SLANG_MAXIMUM_TARGET_API)) {
117 errs() << "target API level '" << ActualTargetAPI << "' is out of range "
118 << "('" << SLANG_MINIMUM_TARGET_API << "' - '"
119 << SLANG_MAXIMUM_TARGET_API << "')\n";
120 return 1;
121 }
122
Tobias Grosserc908b902013-06-21 11:28:49 -0700123 // Parse the file now...
124 SMDiagnostic Err;
Stephen Hines3eb819a2014-11-24 18:16:52 -0800125 std::unique_ptr<Module> M(parseAssemblyFile(InputFilename, Err, Context));
Tobias Grosserc908b902013-06-21 11:28:49 -0700126 if (M.get() == 0) {
127 Err.print(argv[0], errs());
128 return 1;
129 }
130
131 if (!DisableVerify) {
132 std::string Err;
Tim Murrayee4016d2014-04-10 15:49:08 -0700133 raw_string_ostream stream(Err);
134 if (verifyModule(*M.get(), &stream)) {
Tobias Grosserc908b902013-06-21 11:28:49 -0700135 errs() << argv[0]
136 << ": assembly parsed, but does not verify as correct!\n";
137 errs() << Err;
138 return 1;
139 }
140 }
141
142 if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get();
143
144 if (!DisableOutput)
Matt Waladabd2462015-07-15 18:39:41 -0700145 WriteOutputFile(M.get(), ActualTargetAPI);
Tobias Grosserc908b902013-06-21 11:28:49 -0700146
147 return 0;
148}