From d45e7dfc241f917946e057ad67d291bf1f0028e0 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Wed, 5 May 2021 01:59:23 +0200 Subject: SPIR-V: Begin generating types --- tools/gen_spirv_spec.zig | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'tools') diff --git a/tools/gen_spirv_spec.zig b/tools/gen_spirv_spec.zig index 22fe3443a5..7550f26346 100644 --- a/tools/gen_spirv_spec.zig +++ b/tools/gen_spirv_spec.zig @@ -118,11 +118,16 @@ pub fn main() !void { } fn render(writer: Writer, registry: 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 magic_number: u32 = {s}; \\ @@ -132,9 +137,7 @@ 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 = {}}}; \\ , .{ ext_reg.version, ext_reg.revision }, @@ -145,12 +148,6 @@ 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 { try writer.writeAll("pub const Opcode = extern enum(u16) {\n"); for (instructions) |instr| { -- cgit v1.2.3 From 6d1a4c8b1ec66969f262a5469dd51223bcda720f Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Wed, 12 May 2021 00:00:44 +0200 Subject: SPIR-V: Adapt spec generator to new render api --- tools/gen_spirv_spec.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/gen_spirv_spec.zig b/tools/gen_spirv_spec.zig index 7550f26346..4ca69330c7 100644 --- a/tools/gen_spirv_spec.zig +++ b/tools/gen_spirv_spec.zig @@ -114,7 +114,8 @@ pub fn main() !void { try render(buf.writer(), registry); const tree = try std.zig.parse(allocator, buf.items); - _ = try std.zig.render(allocator, std.io.getStdOut().writer(), tree); + const formatted = try tree.render(allocator); + try std.io.getStdOut().writeAll(formatted); } fn render(writer: Writer, registry: Registry) !void { -- cgit v1.2.3 From 25329ca8521fc34a5cedc837a1142e1111d4e844 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Wed, 12 May 2021 00:08:14 +0200 Subject: SPIR-V: Split out registry from gen_spirv_spec.zig --- tools/gen_spirv_spec.zig | 104 ++++------------------------------------------- tools/spirv/grammar.zig | 90 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 97 deletions(-) create mode 100644 tools/spirv/grammar.zig (limited to 'tools') diff --git a/tools/gen_spirv_spec.zig b/tools/gen_spirv_spec.zig index 4ca69330c7..2176d1fa5e 100644 --- a/tools/gen_spirv_spec.zig +++ b/tools/gen_spirv_spec.zig @@ -1,97 +1,7 @@ const std = @import("std"); +const g = @import("spirv/grammar.zig"); 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, -}; - pub fn main() !void { var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); @@ -106,7 +16,7 @@ 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(); @@ -118,7 +28,7 @@ pub fn main() !void { try std.io.getStdOut().writeAll(formatted); } -fn render(writer: Writer, registry: Registry) !void { +fn render(writer: Writer, registry: g.Registry) !void { try writer.writeAll( \\//! This file is auto-generated by tools/gen_spirv_spec.zig. \\ @@ -149,7 +59,7 @@ fn render(writer: Writer, registry: Registry) !void { } } -fn renderOpcodes(writer: Writer, instructions: []const Instruction) !void { +fn renderOpcodes(writer: Writer, 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 }); @@ -157,7 +67,7 @@ fn renderOpcodes(writer: Writer, instructions: []const Instruction) !void { try writer.writeAll("_,\n};\n"); } -fn renderOperandKinds(writer: Writer, kinds: []const OperandKind) !void { +fn renderOperandKinds(writer: Writer, kinds: []const g.OperandKind) !void { for (kinds) |kind| { switch (kind.category) { .ValueEnum => try renderValueEnum(writer, kind), @@ -167,7 +77,7 @@ fn renderOperandKinds(writer: Writer, kinds: []const OperandKind) !void { } } -fn renderValueEnum(writer: Writer, enumeration: OperandKind) !void { +fn renderValueEnum(writer: Writer, enumeration: g.OperandKind) !void { try writer.print("pub const {s} = extern enum(u32) {{\n", .{ enumeration.kind }); const enumerants = enumeration.enumerants orelse return error.InvalidRegistry; @@ -180,7 +90,7 @@ fn renderValueEnum(writer: Writer, enumeration: OperandKind) !void { try writer.writeAll("_,\n};\n"); } -fn renderBitEnum(writer: Writer, enumeration: OperandKind) !void { +fn renderBitEnum(writer: Writer, enumeration: g.OperandKind) !void { try writer.print("pub const {s} = packed struct {{\n", .{ enumeration.kind }); var flags_by_bitpos = [_]?[]const u8{null} ** 32; diff --git a/tools/spirv/grammar.zig b/tools/spirv/grammar.zig new file mode 100644 index 0000000000..86a01641e4 --- /dev/null +++ b/tools/spirv/grammar.zig @@ -0,0 +1,90 @@ +//! 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. +pub const Registry = union(enum) { + core: CoreRegistry, + extension: ExtensionRegistry, +}; + +pub 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, +}; + +pub const ExtensionRegistry = struct { + copyright: [][]const u8, + version: u32, + revision: u32, + instructions: []Instruction, + operand_kinds: []OperandKind = &[_]OperandKind{}, +}; + +pub const InstructionPrintingClass = struct { + tag: []const u8, + heading: ?[]const u8 = null, +}; + +pub 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, +}; + +pub const Operand = struct { + kind: []const u8, + /// If this field is 'null', the operand is only expected once. + quantifier: ?Quantifier = null, + name: []const u8 = "", +}; + +pub const Quantifier = enum { + /// zero or once + @"?", + /// zero or more + @"*", +}; + +pub const OperandCategory = enum { + BitEnum, + ValueEnum, + Id, + Literal, + Composite, +}; + +pub const OperandKind = struct { + category: OperandCategory, + /// The name + kind: []const u8, + doc: ?[]const u8 = null, + enumerants: ?[]Enumerant = null, + bases: ?[]const []const u8 = null, +}; + +pub 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, +}; -- cgit v1.2.3 From 4fe0a0b82b531d4ac44ed9a249137e9f6c186d0b Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Thu, 13 May 2021 01:19:32 +0200 Subject: SPIR-V: SPIR-V feature generation tool There is a lot left to be desired for this tool, as currently dependencies of extensions and dependencies of capabilities on extensions are not included: - There is no machine-readable definition of dependencies of extensions. - A capability may depend on either of a multitude of extensions, which as of yet cannot be properly modelled in the target system. --- tools/update_spirv_features.zig | 321 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 321 insertions(+) create mode 100644 tools/update_spirv_features.zig (limited to 'tools') diff --git a/tools/update_spirv_features.zig b/tools/update_spirv_features.zig new file mode 100644 index 0000000000..94fdb38229 --- /dev/null +++ b/tools/update_spirv_features.zig @@ -0,0 +1,321 @@ +const std = @import("std"); +const fs = std.fs; +const Allocator = std.mem.Allocator; +const g = @import("spirv/grammar.zig"); + +//! This tool generates SPIR-V features from the grammar files in the SPIRV-Headers +//! (https://github.com/KhronosGroup/SPIRV-Headers/) and SPIRV-Registry (https://github.com/KhronosGroup/SPIRV-Registry/) +//! repositories. Currently it only generates a basic feature set definition consisting of versions, extensions and capabilities. +//! There is a lot left to be desired, as currently dependencies of extensions and dependencies on extensions aren't generated. +//! This is because there are some peculiarities in the SPIR-V registries: +//! - Capabilities may depend on multiple extensions, which cannot be modelled yet by std.Target. +//! - Extension dependencies are not documented in a machine-readable manner. +//! - Note that the grammar spec also contains definitions from extensions which aren't actually official. Most of these seem to be +//! from an intel project (https://github.com/intel/llvm/, https://github.com/intel/llvm/tree/sycl/sycl/doc/extensions/SPIRV), +//! and so ONLY extensions in the SPIRV-Registry should be included. + +const Version = struct { + major: u32, + minor: u32, + + fn parse(str: []const u8) !Version { + var it = std.mem.split(str, "."); + + const major = it.next() orelse return error.InvalidVersion; + const minor = it.next() orelse return error.InvalidVersion; + + if (it.next() != null) return error.InvalidVersion; + + return Version{ + .major = std.fmt.parseInt(u32, major, 10) catch return error.InvalidVersion, + .minor = std.fmt.parseInt(u32, minor, 10) catch return error.InvalidVersion, + }; + } + + fn eql(a: Version, b: Version) bool { + return a.major == b.major and a.minor == b.minor; + } + + fn lessThan(ctx: void, a: Version, b: Version) bool { + return if (a.major == b.major) + a.minor < b.minor + else + a.major < b.major; + } +}; + +pub fn main() !void { + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + const allocator = &arena.allocator; + + const args = try std.process.argsAlloc(allocator); + + if (args.len <= 1) { + usageAndExit(std.io.getStdErr(), args[0], 1); + } + if (std.mem.eql(u8, args[1], "--help")) { + usageAndExit(std.io.getStdErr(), args[0], 0); + } + if (args.len != 3) { + usageAndExit(std.io.getStdErr(), args[0], 1); + } + + const spirv_headers_root = args[1]; + const spirv_registry_root = args[2]; + + if (std.mem.startsWith(u8, spirv_headers_root, "-") or std.mem.startsWith(u8, spirv_registry_root, "-")) { + usageAndExit(std.io.getStdErr(), args[0], 1); + } + + const registry_path = try fs.path.join(allocator, &.{ spirv_headers_root, "include", "spirv", "unified1", "spirv.core.grammar.json" }); + const registry_json = try std.fs.cwd().readFileAlloc(allocator, registry_path, std.math.maxInt(usize)); + var tokens = std.json.TokenStream.init(registry_json); + const registry = try std.json.parse(g.CoreRegistry, &tokens, .{ .allocator = allocator }); + + const capabilities = for (registry.operand_kinds) |opkind| { + if (std.mem.eql(u8, opkind.kind, "Capability")) + break opkind.enumerants orelse return error.InvalidRegistry; + } else return error.InvalidRegistry; + + const extensions = try gather_extensions(allocator, spirv_registry_root); + const versions = try gatherVersions(allocator, registry); + + var bw = std.io.bufferedWriter(std.io.getStdOut().writer()); + const w = bw.writer(); + + try w.writeAll( + \\//! This file is auto-generated by tools/update_spirv_features.zig. + \\//! TODO: Dependencies of capabilities on extensions. + \\//! TODO: Dependencies of extensions on extensions. + \\//! TODO: Dependencies of extensions on versions. + \\ + \\const std = @import("../std.zig"); + \\const CpuFeature = std.Target.Cpu.Feature; + \\const CpuModel = std.Target.Cpu.Model; + \\ + \\pub const Feature = enum { + \\ + ); + + for (versions) |ver| { + try w.print(" v{}_{},\n", .{ ver.major, ver.minor }); + } + + for (extensions) |ext| { + try w.print(" {},\n", .{ std.zig.fmtId(ext) }); + } + + for (capabilities) |cap| { + try w.print(" {},\n", .{ std.zig.fmtId(cap.enumerant) }); + } + + try w.writeAll( + \\}; + \\ + \\pub usingnamespace CpuFeature.feature_set_fns(Feature); + \\ + \\pub const all_features = blk: { + \\ @setEvalBranchQuota(2000); + \\ const len = @typeInfo(Feature).Enum.fields.len; + \\ std.debug.assert(len <= CpuFeature.Set.needed_bit_count); + \\ var result: [len]CpuFeature = undefined; + \\ + ); + + for (versions) |ver, i| { + try w.print( + \\ result[@enumToInt(Feature.v{0}_{1})] = .{{ + \\ .llvm_name = null, + \\ .description = "SPIR-V version {0}.{1}", + \\ + , .{ ver.major, ver.minor } + ); + + if (i == 0) { + try w.writeAll( + \\ .dependencies = featureSet(&[_]Feature{}), + \\ }; + \\ + ); + } else { + try w.print( + \\ .dependencies = featureSet(&[_]Feature{{ + \\ .v{}_{}, + \\ }}), + \\ }}; + \\ + , .{ versions[i - 1].major, versions[i - 1].minor } + ); + } + } + + // TODO: Extension dependencies. + for (extensions) |ext| { + try w.print( + \\ result[@enumToInt(Feature.{s})] = .{{ + \\ .llvm_name = null, + \\ .description = "SPIR-V extension {s}", + \\ .dependencies = featureSet(&[_]Feature{{}}), + \\ }}; + \\ + , .{ + std.zig.fmtId(ext), + ext, + } + ); + } + + // TODO: Capability extension dependencies. + for (capabilities) |cap| { + try w.print( + \\ result[@enumToInt(Feature.{s})] = .{{ + \\ .llvm_name = null, + \\ .description = "Enable SPIR-V capability {s}", + \\ .dependencies = featureSet(&[_]Feature{{ + \\ + , .{ + std.zig.fmtId(cap.enumerant), + cap.enumerant, + } + ); + + if (cap.version) |ver_str| { + if (!std.mem.eql(u8, ver_str, "None")) { + const ver = try Version.parse(ver_str); + try w.print(" .v{}_{},\n", .{ ver.major, ver.minor }); + } + } + + for (cap.capabilities) |cap_dep| { + try w.print(" .{},\n", .{ std.zig.fmtId(cap_dep) }); + } + + try w.writeAll( + \\ }), + \\ }; + \\ + ); + } + + try w.writeAll( + \\ const ti = @typeInfo(Feature); + \\ for (result) |*elem, i| { + \\ elem.index = i; + \\ elem.name = ti.Enum.fields[i].name; + \\ } + \\ break :blk result; + \\}; + \\ + ); + + try bw.flush(); +} + +/// SPIRV-Registry should hold all extensions currently registered for SPIR-V. +/// The *.grammar.json in SPIRV-Headers should have most of these as well, but with this we're sure to get only the actually +/// registered ones. +/// TODO: Unfortunately, neither repository contains a machine-readable list of extension dependencies. +fn gather_extensions(allocator: *Allocator, spirv_registry_root: []const u8) ![]const []const u8 { + const extensions_path = try fs.path.join(allocator, &.{spirv_registry_root, "extensions"}); + var extensions_dir = try fs.cwd().openDir(extensions_path, .{ .iterate = true }); + defer extensions_dir.close(); + + var extensions = std.ArrayList([]const u8).init(allocator); + + var vendor_it = extensions_dir.iterate(); + while (try vendor_it.next()) |vendor_entry| { + std.debug.assert(vendor_entry.kind == .Directory); // If this fails, the structure of SPIRV-Registry has changed. + + const vendor_dir = try extensions_dir.openDir(vendor_entry.name, .{ .iterate = true }); + var ext_it = vendor_dir.iterate(); + while (try ext_it.next()) |ext_entry| { + // There is both a HTML and asciidoc version of every spec (as well as some other directories), + // we need just the name, but to avoid duplicates here we will just skip anything thats not asciidoc. + if (!std.mem.endsWith(u8, ext_entry.name, ".asciidoc")) + continue; + + // Unfortunately, some extension filenames are incorrect, so we need to look for the string in tne 'Name Strings' section. + // This has the following format: + // ``` + // Name Strings + // ------------ + // + // SPV_EXT_name + // ``` + // OR + // ``` + // == Name Strings + // + // SPV_EXT_name + // ``` + + const ext_spec = try vendor_dir.readFileAlloc(allocator, ext_entry.name, std.math.maxInt(usize)); + const name_strings = "Name Strings"; + + const name_strings_offset = std.mem.indexOf(u8, ext_spec, name_strings) orelse return error.InvalidRegistry; + + // As the specs are inconsistent on this next part, just skip any newlines/minuses + var ext_start = name_strings_offset + name_strings.len + 1; + while (ext_spec[ext_start] == '\n' or ext_spec[ext_start] == '-') { + ext_start += 1; + } + + const ext_end = std.mem.indexOfScalarPos(u8, ext_spec, ext_start, '\n') orelse return error.InvalidRegistry; + const ext = ext_spec[ext_start .. ext_end]; + + std.debug.assert(std.mem.startsWith(u8, ext, "SPV_")); // Sanity check, all extensions should have a name like SPV_VENDOR_extension. + + try extensions.append(try allocator.dupe(u8, ext)); + } + } + + return extensions.items; +} + +fn insertVersion(versions: *std.ArrayList(Version), version: ?[]const u8) !void { + const ver_str = version orelse return; + if (std.mem.eql(u8, ver_str, "None")) + return; + + const ver = try Version.parse(ver_str); + for (versions.items) |existing_ver| { + if (ver.eql(existing_ver)) return; + } + + try versions.append(ver); +} + +fn gatherVersions(allocator: *Allocator, registry: g.CoreRegistry) ![]const Version { + // Expected number of versions is small + var versions = std.ArrayList(Version).init(allocator); + + for (registry.instructions) |inst| { + try insertVersion(&versions, inst.version); + } + + for (registry.operand_kinds) |opkind| { + const enumerants = opkind.enumerants orelse continue; + for (enumerants) |enumerant| { + try insertVersion(&versions, enumerant.version); + } + } + + std.sort.sort(Version, versions.items, {}, Version.lessThan); + + return versions.items; +} + +fn usageAndExit(file: fs.File, arg0: []const u8, code: u8) noreturn { + file.writer().print( + \\Usage: {s} /path/git/SPIRV-Headers /path/git/SPIRV-Registry + \\ + \\Prints to stdout Zig code which can be used to replace the file lib/std/target/spirv.zig. + \\ + \\SPIRV-Headers can be cloned from https://github.com/KhronosGroup/SPIRV-Headers, + \\SPIRV-Registry can be cloned from https://github.com/KhronosGroup/SPIRV-Registry. + \\ + , .{arg0} + ) catch std.process.exit(1); + std.process.exit(code); +} -- cgit v1.2.3 From 00428ac11b4d5b71ded1f9e572652ff52814f7b7 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Fri, 14 May 2021 19:46:38 +0200 Subject: SPIR-V: Don't parse/render in gen_spirv_spec.zig, just emit in the right format --- tools/gen_spirv_spec.zig | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) (limited to 'tools') diff --git a/tools/gen_spirv_spec.zig b/tools/gen_spirv_spec.zig index 2176d1fa5e..4e6d559c41 100644 --- a/tools/gen_spirv_spec.zig +++ b/tools/gen_spirv_spec.zig @@ -1,6 +1,5 @@ const std = @import("std"); const g = @import("spirv/grammar.zig"); -const Writer = std.ArrayList(u8).Writer; pub fn main() !void { var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); @@ -18,17 +17,12 @@ pub fn main() !void { var tokens = std.json.TokenStream.init(spec); 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); - const formatted = try tree.render(allocator); - try std.io.getStdOut().writeAll(formatted); + var bw = std.io.bufferedWriter(std.io.getStdOut().writer()); + try render(bw.writer(), registry); + try bw.flush(); } -fn render(writer: Writer, registry: g.Registry) !void { +fn render(writer: anytype, registry: g.Registry) !void { try writer.writeAll( \\//! This file is auto-generated by tools/gen_spirv_spec.zig. \\ @@ -39,7 +33,7 @@ fn render(writer: Writer, registry: g.Registry) !void { switch (registry) { .core => |core_reg| { try writer.print( - \\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 }, @@ -49,7 +43,7 @@ fn render(writer: Writer, registry: g.Registry) !void { }, .extension => |ext_reg| { try writer.print( - \\pub const version = Version{{.major = {}, .minor = 0, .patch = {}}}; + \\pub const version = Version{{ .major = {}, .minor = 0, .patch = {} }}; \\ , .{ ext_reg.version, ext_reg.revision }, ); @@ -59,15 +53,15 @@ fn render(writer: Writer, registry: g.Registry) !void { } } -fn renderOpcodes(writer: Writer, instructions: []const g.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 g.OperandKind) !void { +fn renderOperandKinds(writer: anytype, kinds: []const g.OperandKind) !void { for (kinds) |kind| { switch (kind.category) { .ValueEnum => try renderValueEnum(writer, kind), @@ -77,20 +71,20 @@ fn renderOperandKinds(writer: Writer, kinds: []const g.OperandKind) !void { } } -fn renderValueEnum(writer: Writer, enumeration: g.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: g.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; @@ -113,6 +107,7 @@ fn renderBitEnum(writer: Writer, enumeration: g.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 { @@ -123,7 +118,7 @@ fn renderBitEnum(writer: Writer, enumeration: g.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"); -- cgit v1.2.3