aboutsummaryrefslogtreecommitdiff
path: root/tools/gen_spirv_spec.zig
diff options
context:
space:
mode:
Diffstat (limited to 'tools/gen_spirv_spec.zig')
-rw-r--r--tools/gen_spirv_spec.zig147
1 files changed, 25 insertions, 122 deletions
diff --git a/tools/gen_spirv_spec.zig b/tools/gen_spirv_spec.zig
index 22fe3443a5..4e6d559c41 100644
--- a/tools/gen_spirv_spec.zig
+++ b/tools/gen_spirv_spec.zig
@@ -1,96 +1,5 @@
const std = @import("std");
-const Writer = std.ArrayList(u8).Writer;
-
-//! See https://www.khronos.org/registry/spir-v/specs/unified1/MachineReadableGrammar.html
-//! and the files in https://github.com/KhronosGroup/SPIRV-Headers/blob/master/include/spirv/unified1/
-//! Note: Non-canonical casing in these structs used to match SPIR-V spec json.
-const Registry = union(enum) {
- core: CoreRegistry,
- extension: ExtensionRegistry,
-};
-
-const CoreRegistry = struct {
- copyright: [][]const u8,
- /// Hexadecimal representation of the magic number
- magic_number: []const u8,
- major_version: u32,
- minor_version: u32,
- revision: u32,
- instruction_printing_class: []InstructionPrintingClass,
- instructions: []Instruction,
- operand_kinds: []OperandKind,
-};
-
-const ExtensionRegistry = struct {
- copyright: [][]const u8,
- version: u32,
- revision: u32,
- instructions: []Instruction,
- operand_kinds: []OperandKind = &[_]OperandKind{},
-};
-
-const InstructionPrintingClass = struct {
- tag: []const u8,
- heading: ?[]const u8 = null,
-};
-
-const Instruction = struct {
- opname: []const u8,
- class: ?[]const u8 = null, // Note: Only available in the core registry.
- opcode: u32,
- operands: []Operand = &[_]Operand{},
- capabilities: [][]const u8 = &[_][]const u8{},
- extensions: [][]const u8 = &[_][]const u8{},
- version: ?[]const u8 = null,
-
- lastVersion: ?[]const u8 = null,
-};
-
-const Operand = struct {
- kind: []const u8,
- /// If this field is 'null', the operand is only expected once.
- quantifier: ?Quantifier = null,
- name: []const u8 = "",
-};
-
-const Quantifier = enum {
- /// zero or once
- @"?",
- /// zero or more
- @"*",
-};
-
-const OperandCategory = enum {
- BitEnum,
- ValueEnum,
- Id,
- Literal,
- Composite,
-};
-
-const OperandKind = struct {
- category: OperandCategory,
- /// The name
- kind: []const u8,
- doc: ?[]const u8 = null,
- enumerants: ?[]Enumerant = null,
- bases: ?[]const []const u8 = null,
-};
-
-const Enumerant = struct {
- enumerant: []const u8,
- value: union(enum) {
- bitflag: []const u8, // Hexadecimal representation of the value
- int: u31,
- },
- capabilities: [][]const u8 = &[_][]const u8{},
- /// Valid for .ValueEnum and .BitEnum
- extensions: [][]const u8 = &[_][]const u8{},
- /// `quantifier` will always be `null`.
- parameters: []Operand = &[_]Operand{},
- version: ?[]const u8 = null,
- lastVersion: ?[]const u8 = null,
-};
+const g = @import("spirv/grammar.zig");
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
@@ -106,24 +15,25 @@ pub fn main() !void {
const spec = try std.fs.cwd().readFileAlloc(allocator, spec_path, std.math.maxInt(usize));
var tokens = std.json.TokenStream.init(spec);
- var registry = try std.json.parse(Registry, &tokens, .{.allocator = allocator});
+ var registry = try std.json.parse(g.Registry, &tokens, .{.allocator = allocator});
- var buf = std.ArrayList(u8).init(allocator);
- defer buf.deinit();
-
- try render(buf.writer(), registry);
-
- const tree = try std.zig.parse(allocator, buf.items);
- _ = try std.zig.render(allocator, std.io.getStdOut().writer(), tree);
+ var bw = std.io.bufferedWriter(std.io.getStdOut().writer());
+ try render(bw.writer(), registry);
+ try bw.flush();
}
-fn render(writer: Writer, registry: Registry) !void {
+fn render(writer: anytype, registry: g.Registry) !void {
+ try writer.writeAll(
+ \\//! This file is auto-generated by tools/gen_spirv_spec.zig.
+ \\
+ \\const Version = @import("builtin").Version;
+ \\
+ );
+
switch (registry) {
.core => |core_reg| {
- try renderCopyRight(writer, core_reg.copyright);
try writer.print(
- \\const Version = @import("builtin").Version;
- \\pub const version = Version{{.major = {}, .minor = {}, .patch = {}}};
+ \\pub const version = Version{{ .major = {}, .minor = {}, .patch = {} }};
\\pub const magic_number: u32 = {s};
\\
, .{ core_reg.major_version, core_reg.minor_version, core_reg.revision, core_reg.magic_number },
@@ -132,10 +42,8 @@ fn render(writer: Writer, registry: Registry) !void {
try renderOperandKinds(writer, core_reg.operand_kinds);
},
.extension => |ext_reg| {
- try renderCopyRight(writer, ext_reg.copyright);
try writer.print(
- \\const Version = @import("builtin").Version;
- \\pub const version = Version{{.major = {}, .minor = 0, .patch = {}}};
+ \\pub const version = Version{{ .major = {}, .minor = 0, .patch = {} }};
\\
, .{ ext_reg.version, ext_reg.revision },
);
@@ -145,21 +53,15 @@ fn render(writer: Writer, registry: Registry) !void {
}
}
-fn renderCopyRight(writer: Writer, copyright: []const []const u8) !void {
- for (copyright) |line| {
- try writer.print("// {s}\n", .{ line });
- }
-}
-
-fn renderOpcodes(writer: Writer, instructions: []const Instruction) !void {
+fn renderOpcodes(writer: anytype, instructions: []const g.Instruction) !void {
try writer.writeAll("pub const Opcode = extern enum(u16) {\n");
for (instructions) |instr| {
- try writer.print("{} = {},\n", .{ std.zig.fmtId(instr.opname), instr.opcode });
+ try writer.print(" {} = {},\n", .{ std.zig.fmtId(instr.opname), instr.opcode });
}
- try writer.writeAll("_,\n};\n");
+ try writer.writeAll(" _,\n};\n");
}
-fn renderOperandKinds(writer: Writer, kinds: []const OperandKind) !void {
+fn renderOperandKinds(writer: anytype, kinds: []const g.OperandKind) !void {
for (kinds) |kind| {
switch (kind.category) {
.ValueEnum => try renderValueEnum(writer, kind),
@@ -169,20 +71,20 @@ fn renderOperandKinds(writer: Writer, kinds: []const OperandKind) !void {
}
}
-fn renderValueEnum(writer: Writer, enumeration: OperandKind) !void {
+fn renderValueEnum(writer: anytype, enumeration: g.OperandKind) !void {
try writer.print("pub const {s} = extern enum(u32) {{\n", .{ enumeration.kind });
const enumerants = enumeration.enumerants orelse return error.InvalidRegistry;
for (enumerants) |enumerant| {
if (enumerant.value != .int) return error.InvalidRegistry;
- try writer.print("{} = {},\n", .{ std.zig.fmtId(enumerant.enumerant), enumerant.value.int });
+ try writer.print(" {} = {},\n", .{ std.zig.fmtId(enumerant.enumerant), enumerant.value.int });
}
- try writer.writeAll("_,\n};\n");
+ try writer.writeAll(" _,\n};\n");
}
-fn renderBitEnum(writer: Writer, enumeration: OperandKind) !void {
+fn renderBitEnum(writer: anytype, enumeration: g.OperandKind) !void {
try writer.print("pub const {s} = packed struct {{\n", .{ enumeration.kind });
var flags_by_bitpos = [_]?[]const u8{null} ** 32;
@@ -205,6 +107,7 @@ fn renderBitEnum(writer: Writer, enumeration: OperandKind) !void {
}
for (flags_by_bitpos) |maybe_flag_name, bitpos| {
+ try writer.writeAll(" ");
if (maybe_flag_name) |flag_name| {
try writer.writeAll(flag_name);
} else {
@@ -215,7 +118,7 @@ fn renderBitEnum(writer: Writer, enumeration: OperandKind) !void {
if (bitpos == 0) { // Force alignment to integer boundaries
try writer.writeAll("align(@alignOf(u32)) ");
}
- try writer.writeAll("= false, ");
+ try writer.writeAll("= false,\n");
}
try writer.writeAll("};\n");