2020-03-02 20:47:43 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-03-25 19:16:36 +00:00
|
|
|
#include "src/context.h"
|
2020-03-02 20:47:43 +00:00
|
|
|
#include "src/reader/reader.h"
|
|
|
|
#include "src/type_determiner.h"
|
2020-03-25 19:16:36 +00:00
|
|
|
#include "src/type_manager.h"
|
2020-03-02 20:47:43 +00:00
|
|
|
#include "src/validator.h"
|
|
|
|
#include "src/writer/writer.h"
|
|
|
|
|
2020-03-18 14:08:48 +00:00
|
|
|
#if TINT_BUILD_SPV_READER
|
2020-03-17 20:07:41 +00:00
|
|
|
#include "src/reader/spirv/parser.h"
|
2020-03-18 14:08:48 +00:00
|
|
|
#endif // TINT_BUILD_SPV_READER
|
|
|
|
|
|
|
|
#if TINT_BUILD_WGSL_READER
|
|
|
|
#include "src/reader/wgsl/parser.h"
|
|
|
|
#endif // TINT_BUILD_WGSL_READER
|
|
|
|
|
|
|
|
#if TINT_BUILD_SPV_WRITER
|
|
|
|
#include "spirv-tools/libspirv.hpp"
|
|
|
|
#include "src/writer/spirv/generator.h"
|
|
|
|
#endif // TINT_BUILD_SPV_WRITER
|
|
|
|
|
|
|
|
#if TINT_BUILD_WGSL_WRITER
|
|
|
|
#include "src/writer/wgsl/generator.h"
|
|
|
|
#endif // TINT_BUILD_WGSL_WRITER
|
2020-03-10 22:54:12 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum class Format {
|
|
|
|
kNone = -1,
|
|
|
|
kSpirv,
|
|
|
|
kSpvAsm,
|
|
|
|
kWgsl,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Options {
|
|
|
|
bool show_help = false;
|
|
|
|
|
|
|
|
std::string input_filename;
|
|
|
|
std::string output_name = "";
|
|
|
|
std::string output_ext = "spv";
|
|
|
|
|
|
|
|
bool parse_only = false;
|
|
|
|
bool dump_ast = false;
|
|
|
|
|
|
|
|
Format format = Format::kSpirv;
|
|
|
|
};
|
|
|
|
|
|
|
|
const char kUsage[] = R"(Usage: tint [options] SCRIPT [SCRIPTS...]
|
|
|
|
|
|
|
|
options:
|
|
|
|
--format <spirv|spvasm|wgsl> -- Output format
|
|
|
|
--output-name <name> -- Name for the output file, without extension
|
|
|
|
--parse-only -- Stop after parsing the input
|
|
|
|
--dump-ast -- Dump the generated AST to stdout
|
|
|
|
-h -- This help text)";
|
|
|
|
|
2020-03-18 14:08:48 +00:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wunused-parameter"
|
2020-03-02 20:47:43 +00:00
|
|
|
Format parse_format(const std::string& fmt) {
|
2020-03-18 14:08:48 +00:00
|
|
|
#pragma clang diagnostic pop
|
|
|
|
|
|
|
|
#if TINT_BUILD_SPV_WRITER
|
2020-03-02 20:47:43 +00:00
|
|
|
if (fmt == "spirv")
|
|
|
|
return Format::kSpirv;
|
|
|
|
if (fmt == "spvasm")
|
|
|
|
return Format::kSpvAsm;
|
2020-03-18 14:08:48 +00:00
|
|
|
#endif // TINT_BUILD_SPV_WRITER
|
|
|
|
|
|
|
|
#if TINT_BUILD_WGSL_WRITER
|
2020-03-02 20:47:43 +00:00
|
|
|
if (fmt == "wgsl")
|
|
|
|
return Format::kWgsl;
|
2020-03-18 14:08:48 +00:00
|
|
|
#endif // TINT_BUILD_WGSL_WRITER
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
return Format::kNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParseArgs(const std::vector<std::string>& args, Options* opts) {
|
|
|
|
for (size_t i = 1; i < args.size(); ++i) {
|
|
|
|
const std::string& arg = args[i];
|
|
|
|
if (arg == "--format") {
|
|
|
|
++i;
|
|
|
|
if (i >= args.size()) {
|
|
|
|
std::cerr << "Missing value for --format argument." << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
opts->format = parse_format(args[i]);
|
|
|
|
|
|
|
|
if (opts->format == Format::kNone) {
|
|
|
|
std::cerr << "Unknown output format: " << args[i] << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts->format == Format::kSpvAsm)
|
|
|
|
opts->output_ext = "spvasm";
|
|
|
|
else if (opts->format == Format::kWgsl)
|
|
|
|
opts->output_ext = "wgsl";
|
|
|
|
}
|
|
|
|
if (arg == "--output-name") {
|
|
|
|
++i;
|
|
|
|
if (i >= args.size()) {
|
|
|
|
std::cerr << "Missing value for --output_name argument." << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
opts->output_name = args[i];
|
|
|
|
|
|
|
|
} else if (arg == "-h" || arg == "--help") {
|
|
|
|
opts->show_help = true;
|
|
|
|
} else if (arg == "--parse-only") {
|
|
|
|
opts->parse_only = true;
|
|
|
|
} else if (arg == "--dump-ast") {
|
|
|
|
opts->dump_ast = true;
|
|
|
|
} else if (!arg.empty()) {
|
|
|
|
opts->input_filename = arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-10 22:54:12 +00:00
|
|
|
/// Copies the content from the file named |filename| to |buffer|,
|
|
|
|
/// assuming each element in the file is of type |T|. If any error occurs,
|
|
|
|
/// writes error messages to the standard error stream and returns false.
|
|
|
|
/// Assumes the size of a |T| object is divisible by its required alignment.
|
|
|
|
/// @returns true if we successfully read the file.
|
2020-03-18 14:08:48 +00:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wunused-template"
|
2020-03-10 22:54:12 +00:00
|
|
|
template <typename T>
|
|
|
|
bool ReadFile(const std::string& input_file, std::vector<T>* buffer) {
|
2020-04-08 18:52:53 +00:00
|
|
|
#pragma clang diagnostic pop
|
2020-03-18 14:08:48 +00:00
|
|
|
|
2020-03-10 22:54:12 +00:00
|
|
|
if (!buffer) {
|
|
|
|
std::cerr << "The buffer pointer was null" << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
FILE* file = nullptr;
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
fopen_s(&file, input_file.c_str(), "rb");
|
|
|
|
#else
|
|
|
|
file = fopen(input_file.c_str(), "rb");
|
|
|
|
#endif
|
|
|
|
if (!file) {
|
|
|
|
std::cerr << "Failed to open " << input_file << std::endl;
|
2020-03-10 22:54:12 +00:00
|
|
|
return false;
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
uint64_t tell_file_size = static_cast<uint64_t>(ftell(file));
|
|
|
|
if (tell_file_size <= 0) {
|
|
|
|
std::cerr << "Input file of incorrect size: " << input_file << std::endl;
|
|
|
|
fclose(file);
|
|
|
|
return {};
|
|
|
|
}
|
2020-03-10 22:54:12 +00:00
|
|
|
const auto file_size = static_cast<size_t>(tell_file_size);
|
|
|
|
if (0 != (file_size % sizeof(T))) {
|
|
|
|
std::cerr << "File " << input_file
|
|
|
|
<< " does not contain an integral number of objects: "
|
|
|
|
<< file_size << " bytes in the file, require " << sizeof(T)
|
|
|
|
<< " bytes per object" << std::endl;
|
|
|
|
fclose(file);
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-02 20:47:43 +00:00
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
|
2020-03-10 22:54:12 +00:00
|
|
|
buffer->clear();
|
|
|
|
buffer->resize(file_size / sizeof(T));
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2020-03-10 22:54:12 +00:00
|
|
|
size_t bytes_read = fread(buffer->data(), 1, file_size, file);
|
2020-03-02 20:47:43 +00:00
|
|
|
fclose(file);
|
|
|
|
if (bytes_read != file_size) {
|
|
|
|
std::cerr << "Failed to read " << input_file << std::endl;
|
2020-03-10 22:54:12 +00:00
|
|
|
return false;
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 22:54:12 +00:00
|
|
|
return true;
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 14:08:48 +00:00
|
|
|
#if TINT_BUILD_SPV_WRITER
|
2020-03-02 20:47:43 +00:00
|
|
|
std::string Disassemble(const std::vector<uint32_t>& data) {
|
|
|
|
std::string spv_errors;
|
|
|
|
spv_target_env target_env = SPV_ENV_UNIVERSAL_1_0;
|
|
|
|
|
|
|
|
auto msg_consumer = [&spv_errors](spv_message_level_t level, const char*,
|
|
|
|
const spv_position_t& position,
|
|
|
|
const char* message) {
|
|
|
|
switch (level) {
|
|
|
|
case SPV_MSG_FATAL:
|
|
|
|
case SPV_MSG_INTERNAL_ERROR:
|
|
|
|
case SPV_MSG_ERROR:
|
|
|
|
spv_errors += "error: line " + std::to_string(position.index) + ": " +
|
|
|
|
message + "\n";
|
|
|
|
break;
|
|
|
|
case SPV_MSG_WARNING:
|
|
|
|
spv_errors += "warning: line " + std::to_string(position.index) + ": " +
|
|
|
|
message + "\n";
|
|
|
|
break;
|
|
|
|
case SPV_MSG_INFO:
|
|
|
|
spv_errors += "info: line " + std::to_string(position.index) + ": " +
|
|
|
|
message + "\n";
|
|
|
|
break;
|
|
|
|
case SPV_MSG_DEBUG:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
spvtools::SpirvTools tools(target_env);
|
|
|
|
tools.SetMessageConsumer(msg_consumer);
|
|
|
|
|
|
|
|
std::string result;
|
2020-04-01 23:40:53 +00:00
|
|
|
if (!tools.Disassemble(data, &result,
|
|
|
|
SPV_BINARY_TO_TEXT_OPTION_INDENT |
|
|
|
|
SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES)) {
|
|
|
|
std::cerr << spv_errors << std::endl;
|
|
|
|
}
|
2020-03-02 20:47:43 +00:00
|
|
|
return result;
|
|
|
|
}
|
2020-03-18 14:08:48 +00:00
|
|
|
#endif // TINT_BUILD_SPV_WRITER
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int main(int argc, const char** argv) {
|
|
|
|
std::vector<std::string> args(argv, argv + argc);
|
|
|
|
Options options;
|
|
|
|
|
|
|
|
if (!ParseArgs(args, &options)) {
|
|
|
|
std::cerr << "Failed to parse arguments." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.show_help) {
|
|
|
|
std::cout << kUsage << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
2020-03-26 15:31:36 +00:00
|
|
|
if (options.input_filename.empty()) {
|
2020-03-02 20:47:43 +00:00
|
|
|
std::cerr << "Input file missing" << std::endl;
|
|
|
|
std::cout << kUsage << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-03-25 19:16:36 +00:00
|
|
|
tint::Context ctx;
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
std::unique_ptr<tint::reader::Reader> reader;
|
2020-03-18 14:08:48 +00:00
|
|
|
#if TINT_BUILD_WGSL_READER
|
2020-03-10 22:54:12 +00:00
|
|
|
if (options.input_filename.size() > 5 &&
|
|
|
|
options.input_filename.substr(options.input_filename.size() - 5) ==
|
|
|
|
".wgsl") {
|
|
|
|
std::vector<uint8_t> data;
|
|
|
|
if (!ReadFile<uint8_t>(options.input_filename, &data)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2020-03-02 20:47:43 +00:00
|
|
|
reader = std::make_unique<tint::reader::wgsl::Parser>(
|
2020-03-27 00:47:16 +00:00
|
|
|
&ctx, std::string(data.begin(), data.end()));
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
2020-03-18 14:08:48 +00:00
|
|
|
#endif // TINT_BUILD_WGSL_READER
|
|
|
|
|
|
|
|
#if TINT_BUILD_SPV_READER
|
2020-03-10 22:54:12 +00:00
|
|
|
if (options.input_filename.size() > 4 &&
|
|
|
|
options.input_filename.substr(options.input_filename.size() - 4) ==
|
|
|
|
".spv") {
|
|
|
|
std::vector<uint32_t> data;
|
|
|
|
if (!ReadFile<uint32_t>(options.input_filename, &data)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2020-03-27 00:47:16 +00:00
|
|
|
reader = std::make_unique<tint::reader::spirv::Parser>(&ctx, data);
|
2020-03-10 22:54:12 +00:00
|
|
|
}
|
2020-03-18 14:08:48 +00:00
|
|
|
#endif // TINT_BUILD_SPV_READER
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
if (!reader) {
|
|
|
|
std::cerr << "Failed to create reader for input file: "
|
|
|
|
<< options.input_filename << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!reader->Parse()) {
|
|
|
|
std::cerr << reader->error() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-04-06 18:43:56 +00:00
|
|
|
auto mod = reader->module();
|
2020-03-02 20:47:43 +00:00
|
|
|
if (options.dump_ast) {
|
2020-04-06 18:43:56 +00:00
|
|
|
std::cout << std::endl << mod.to_str() << std::endl;
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
if (options.parse_only) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-04-06 18:43:56 +00:00
|
|
|
if (!mod.IsValid()) {
|
|
|
|
std::cerr << "Invalid module generated..." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-04-20 14:20:01 +00:00
|
|
|
tint::TypeDeterminer td(&ctx, &mod);
|
|
|
|
if (!td.Determine()) {
|
2020-03-02 20:47:43 +00:00
|
|
|
std::cerr << td.error() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tint::Validator v;
|
2020-04-06 18:43:56 +00:00
|
|
|
if (!v.Validate(mod)) {
|
2020-03-02 20:47:43 +00:00
|
|
|
std::cerr << v.error() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<tint::writer::Writer> writer;
|
2020-03-18 14:08:48 +00:00
|
|
|
|
|
|
|
#if TINT_BUILD_SPV_WRITER
|
2020-03-02 20:47:43 +00:00
|
|
|
if (options.format == Format::kSpirv || options.format == Format::kSpvAsm) {
|
2020-04-06 18:43:56 +00:00
|
|
|
writer = std::make_unique<tint::writer::spirv::Generator>(std::move(mod));
|
2020-03-18 14:08:48 +00:00
|
|
|
}
|
|
|
|
#endif // TINT_BUILD_SPV_WRITER
|
|
|
|
|
|
|
|
#if TINT_BUILD_WGSL_WRITER
|
|
|
|
if (options.format == Format::kWgsl) {
|
2020-04-06 18:43:56 +00:00
|
|
|
writer = std::make_unique<tint::writer::wgsl::Generator>(std::move(mod));
|
2020-03-18 14:08:48 +00:00
|
|
|
}
|
|
|
|
#endif // TINT_BUILD_WGSL_WRITER
|
|
|
|
|
|
|
|
if (!writer) {
|
2020-03-02 20:47:43 +00:00
|
|
|
std::cerr << "Unknown output format specified" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!writer->Generate()) {
|
|
|
|
std::cerr << "Failed to generate SPIR-V: " << writer->error() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-03-18 14:08:48 +00:00
|
|
|
#if TINT_BUILD_SPV_WRITER
|
2020-03-02 20:47:43 +00:00
|
|
|
if (options.format == Format::kSpvAsm) {
|
2020-04-15 20:47:55 +00:00
|
|
|
auto* w = static_cast<tint::writer::spirv::Generator*>(writer.get());
|
2020-03-02 20:47:43 +00:00
|
|
|
auto str = Disassemble(w->result());
|
|
|
|
// TODO(dsinclair): Write to file if output_file given
|
|
|
|
std::cout << str << std::endl;
|
2020-03-18 14:08:48 +00:00
|
|
|
}
|
|
|
|
if (options.format == Format::kSpirv) {
|
2020-03-17 21:00:22 +00:00
|
|
|
// auto w = static_cast<tint::writer::spirv::Generator*>(writer.get());
|
2020-03-02 20:47:43 +00:00
|
|
|
// TODO(dsincliair): Write to to file
|
2020-03-18 14:08:48 +00:00
|
|
|
}
|
|
|
|
#endif // TINT_BUILD_SPV_WRITER
|
|
|
|
|
|
|
|
#if TINT_BUILD_WGSL_WRITER
|
|
|
|
if (options.format == Format::kWgsl) {
|
2020-04-15 20:54:10 +00:00
|
|
|
auto* w = static_cast<tint::writer::wgsl::Generator*>(writer.get());
|
2020-03-02 20:47:43 +00:00
|
|
|
std::cout << w->result() << std::endl;
|
|
|
|
}
|
2020-03-18 14:08:48 +00:00
|
|
|
#endif // TINT_BUILD_WGSL_WRITER
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|