aboutsummaryrefslogtreecommitdiff
path: root/tools/buildHeaders/header.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tools/buildHeaders/header.cpp')
-rw-r--r--[-rwxr-xr-x]tools/buildHeaders/header.cpp170
1 files changed, 146 insertions, 24 deletions
diff --git a/tools/buildHeaders/header.cpp b/tools/buildHeaders/header.cpp
index b8b227f..e1e05d0 100755..100644
--- a/tools/buildHeaders/header.cpp
+++ b/tools/buildHeaders/header.cpp
@@ -1,19 +1,19 @@
-// Copyright (c) 2014-2018 The Khronos Group Inc.
-//
+// Copyright (c) 2014-2019 The Khronos Group Inc.
+//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and/or associated documentation files (the "Materials"),
// to deal in the Materials without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Materials, and to permit persons to whom the
// Materials are furnished to do so, subject to the following conditions:
-//
+//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Materials.
-//
+//
// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS KHRONOS
// STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS SPECIFICATIONS AND
-// HEADER INFORMATION ARE LOCATED AT https://www.khronos.org/registry/
-//
+// HEADER INFORMATION ARE LOCATED AT https://www.khronos.org/registry/
+//
// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
@@ -42,6 +42,7 @@
#include <cctype>
#include <vector>
#include <utility>
+#include <set>
#include "jsoncpp/dist/json/json.h"
@@ -68,7 +69,7 @@ namespace {
TPrinter();
static const int DocMagicNumber = 0x07230203;
- static const int DocVersion = 0x00010300;
+ static const int DocVersion = 0x00010400;
static const int DocRevision = 1;
#define DocRevisionString "1"
static const std::string DocCopyright;
@@ -97,7 +98,8 @@ namespace {
virtual void printEpilogue(std::ostream&) const { }
virtual void printMeta(std::ostream&) const;
virtual void printTypes(std::ostream&) const { }
-
+ virtual void printHasResultType(std::ostream&) const { };
+
virtual std::string escapeComment(const std::string& s) const;
// Default printComments() uses these comment strings
@@ -107,7 +109,7 @@ namespace {
virtual std::string commentEOL(bool isLast) const { return ""; }
typedef std::pair<unsigned, std::string> valpair_t;
-
+
// for printing enum values
virtual std::string enumBeg(const std::string&, enumStyle_t) const { return ""; }
virtual std::string enumEnd(const std::string&, enumStyle_t, bool isLast = false) const {
@@ -126,7 +128,7 @@ namespace {
const char* fmt, bool isLast = false) const {
return "";
}
-
+
std::vector<valpair_t> getSortedVals(const Json::Value&) const;
virtual std::string indent(int count = 1) const {
@@ -149,7 +151,7 @@ namespace {
}
void addComment(Json::Value& node, const std::string& str);
-
+
Json::Value spvRoot; // JSON SPIR-V data
};
@@ -167,7 +169,7 @@ namespace {
}
const std::string TPrinter::DocCopyright =
- "Copyright (c) 2014-2018 The Khronos Group Inc.\n"
+ "Copyright (c) 2014-2019 The Khronos Group Inc.\n"
"\n"
"Permission is hereby granted, free of charge, to any person obtaining a copy\n"
"of this software and/or associated documentation files (the \"Materials\"),\n"
@@ -197,13 +199,16 @@ namespace {
const std::string TPrinter::DocComment2 =
"Enumeration tokens for SPIR-V, in various styles:\n"
- " C, C++, C++11, JSON, Lua, Python\n"
+ " C, C++, C++11, JSON, Lua, Python, C#, D\n"
"\n"
"- C will have tokens with a \"Spv\" prefix, e.g.: SpvSourceLanguageGLSL\n"
"- C++ will have tokens in the \"spv\" name space, e.g.: spv::SourceLanguageGLSL\n"
"- C++11 will use enum classes in the spv namespace, e.g.: spv::SourceLanguage::GLSL\n"
"- Lua will use tables, e.g.: spv.SourceLanguage.GLSL\n"
"- Python will use dictionaries, e.g.: spv['SourceLanguage']['GLSL']\n"
+ "- C# will use enum classes in the Specification class located in the \"Spv\" namespace,\n"
+ " e.g.: Spv.Specification.SourceLanguage.GLSL\n"
+ "- D will have tokens under the \"spv\" module, e.g: spv.SourceLanguage.GLSL\n"
"\n"
"Some tokens act like mask values, which can be OR'd together,\n"
"while others are mutually exclusive. The mask-like ones have\n"
@@ -290,7 +295,7 @@ namespace {
{
const int commentCount = spvRoot["spv"]["meta"]["Comment"].size();
int commentNum = 0;
-
+
for (const auto& comment : spvRoot["spv"]["meta"]["Comment"]) {
out << commentBeg();
@@ -322,7 +327,7 @@ namespace {
void TPrinter::printDefs(std::ostream& out) const
{
const Json::Value& enums = spvRoot["spv"]["enum"];
-
+
for (auto opClass = enums.begin(); opClass != enums.end(); ++opClass) {
const bool isMask = (*opClass)["Type"].asString() == "Bit";
const auto opName = (*opClass)["Name"].asString();
@@ -337,13 +342,13 @@ namespace {
out << enumFmt(opPrefix, valpair_t(0, "MaskNone"), enumNoMask);
const auto sorted = getSortedVals((*opClass)["Values"]);
-
- std::string maxEnum = maxEnumFmt(opName, valpair_t(0x7FFFFFFF, "Max"), enumHex);
+
+ std::string maxEnum = maxEnumFmt(opName, valpair_t(0x7FFFFFFF, "Max"), enumHex);
bool printMax = (style != enumMask && maxEnum.size() > 0);
for (const auto& v : sorted)
- out << enumFmt(opPrefix, v, style, !printMax && v.first == sorted.back().first);
+ out << enumFmt(opPrefix, v, style, !printMax && v.second == sorted.back().second);
if (printMax)
out << maxEnum;
@@ -361,6 +366,7 @@ namespace {
printTypes(out);
printMeta(out);
printDefs(out);
+ printHasResultType(out);
printEpilogue(out);
}
@@ -390,7 +396,7 @@ namespace {
}
return newStr;
}
-
+
std::string fmtConstInt(unsigned val, const std::string& name,
const char* fmt, bool isLast) const override {
return indent(3) + '"' + name + "\": " + fmtNum("%d", val) + (isLast ? "\n" : ",\n");
@@ -475,7 +481,7 @@ namespace {
}
virtual void printEpilogue(std::ostream& out) const override {
- out << "#endif // #ifndef spirv_" << headerGuardSuffix() << std::endl;
+ out << "#endif" << std::endl;
}
virtual void printTypes(std::ostream& out) const override {
@@ -488,9 +494,48 @@ namespace {
return std::string("static const unsigned int ") + pre() + name +
" = " + fmtNum(fmt, val) + (isLast ? ";\n\n" : ";\n");
}
-
+
virtual std::string pre() const { return ""; } // C name prefix
virtual std::string headerGuardSuffix() const = 0;
+
+ virtual std::string fmtEnumUse(const std::string& opPrefix, const std::string& name) const { return pre() + name; }
+
+ virtual void printHasResultType(std::ostream& out) const
+ {
+ const Json::Value& enums = spvRoot["spv"]["enum"];
+
+ std::set<unsigned> seenValues;
+
+ for (auto opClass = enums.begin(); opClass != enums.end(); ++opClass) {
+ const auto opName = (*opClass)["Name"].asString();
+ if (opName != "Op") {
+ continue;
+ }
+
+ out << "#ifdef SPV_ENABLE_UTILITY_CODE" << std::endl;
+ out << "inline void " << pre() << "HasResultAndType(" << pre() << opName << " opcode, bool *hasResult, bool *hasResultType) {" << std::endl;
+ out << " *hasResult = *hasResultType = false;" << std::endl;
+ out << " switch (opcode) {" << std::endl;
+ out << " default: /* unknown opcode */ break;" << std::endl;
+
+ for (auto& inst : spv::InstructionDesc) {
+
+ // Filter out duplicate enum values, which would break the switch statement.
+ // These are probably just extension enums promoted to core.
+ if (seenValues.find(inst.value) != seenValues.end()) {
+ continue;
+ }
+ seenValues.insert(inst.value);
+
+ std::string name = inst.name;
+ out << " case " << fmtEnumUse("Op", name) << ": *hasResult = " << (inst.hasResult() ? "true" : "false") << "; *hasResultType = " << (inst.hasType() ? "true" : "false") << "; break;" << std::endl;
+ }
+
+ out << " }" << std::endl;
+ out << "}" << std::endl;
+ out << "#endif /* SPV_ENABLE_UTILITY_CODE */" << std::endl << std::endl;
+ }
+ }
};
// C printer
@@ -542,19 +587,19 @@ namespace {
if (isMask) {
const auto typeName = opName + styleStr(enumMask);
-
+
out << "inline " + typeName + " operator|(" + typeName + " a, " + typeName + " b) { return " +
typeName + "(unsigned(a) | unsigned(b)); }\n";
}
}
out << "\n} // end namespace spv\n\n";
- TPrinterCBase::printEpilogue(out);
+ out << "#endif // #ifndef spirv_" << headerGuardSuffix() << std::endl;
}
std::string commentBOL() const override { return "// "; }
-
+
virtual std::string enumBeg(const std::string& s, enumStyle_t style) const override {
return std::string("enum ") + s + styleStr(style) + " {\n";
}
@@ -597,6 +642,9 @@ namespace {
return enumFmt(s, v, style, true);
}
+ // Add type prefix for scoped enum
+ virtual std::string fmtEnumUse(const std::string& opPrefix, const std::string& name) const { return opPrefix + "::" + name; }
+
std::string headerGuardSuffix() const override { return "HPP"; }
};
@@ -658,6 +706,76 @@ namespace {
}
};
+ // C# printer
+ class TPrinterCSharp final : public TPrinter {
+ private:
+ std::string commentBOL() const override { return "// "; }
+
+ void printPrologue(std::ostream& out) const override {
+ out << "namespace Spv\n{\n\n";
+ out << indent() << "public static class Specification\n";
+ out << indent() << "{\n";
+ }
+
+ void printEpilogue(std::ostream& out) const override {
+ out << indent() << "}\n";
+ out << "}\n";
+ }
+
+ std::string enumBeg(const std::string& s, enumStyle_t style) const override {
+ return indent(2) + "public enum " + s + styleStr(style) + "\n" + indent(2) + "{\n";
+ }
+
+ std::string enumEnd(const std::string& s, enumStyle_t style, bool isLast) const override {
+ return indent(2) + "}" + + (isLast ? "\n" : "\n\n");
+ }
+
+ std::string enumFmt(const std::string& s, const valpair_t& v,
+ enumStyle_t style, bool isLast) const override {
+ return indent(3) + prependIfDigit(s, v.second) + " = " + fmtStyleVal(v.first, style) + ",\n";
+ }
+
+ std::string fmtConstInt(unsigned val, const std::string& name,
+ const char* fmt, bool isLast) const override {
+ return indent(2) + std::string("public const uint ") + name +
+ " = " + fmtNum(fmt, val) + (isLast ? ";\n\n" : ";\n");
+ }
+ };
+
+ // D printer
+ class TPrinterD final : public TPrinter {
+ private:
+ std::string commentBeg() const override { return "/+\n"; }
+ std::string commentBOL() const override { return " + "; }
+ std::string commentEnd(bool isLast) const override { return " +/\n"; }
+
+ void printPrologue(std::ostream& out) const override {
+ out << "module spv;\n\n";
+ }
+
+ void printEpilogue(std::ostream& out) const override {
+ }
+
+ std::string enumBeg(const std::string& s, enumStyle_t style) const override {
+ return "enum " + s + styleStr(style) + " : uint\n{\n";
+ }
+
+ std::string enumEnd(const std::string& s, enumStyle_t style, bool isLast) const override {
+ return std::string("}\n\n");
+ }
+
+ std::string enumFmt(const std::string& s, const valpair_t& v,
+ enumStyle_t style, bool isLast) const override {
+ return indent() + prependIfDigit("_", v.second) + " = " + fmtStyleVal(v.first, style) + ",\n";
+ }
+
+ std::string fmtConstInt(unsigned val, const std::string& name,
+ const char* fmt, bool isLast) const override {
+ return std::string("enum uint ") + name +
+ " = " + fmtNum(fmt, val) + (isLast ? ";\n\n" : ";\n");
+ }
+ };
+
} // namespace
namespace spv {
@@ -672,6 +790,8 @@ namespace spv {
langInfo.push_back(std::make_pair(ELangJSON, "spirv.json"));
langInfo.push_back(std::make_pair(ELangLua, "spirv.lua"));
langInfo.push_back(std::make_pair(ELangPython, "spirv.py"));
+ langInfo.push_back(std::make_pair(ELangCSharp, "spirv.cs"));
+ langInfo.push_back(std::make_pair(ELangD, "spv.d"));
for (const auto& lang : langInfo) {
std::ofstream out(lang.second, std::ios::out);
@@ -697,6 +817,8 @@ namespace spv {
case ELangJSON: p = TPrinterPtr(new TPrinterJSON); break;
case ELangLua: p = TPrinterPtr(new TPrinterLua); break;
case ELangPython: p = TPrinterPtr(new TPrinterPython); break;
+ case ELangCSharp: p = TPrinterPtr(new TPrinterCSharp); break;
+ case ELangD: p = TPrinterPtr(new TPrinterD); break;
case ELangAll: PrintAllHeaders(); break;
default:
std::cerr << "Unknown language." << std::endl;