| ... | ... | @@ -1,97 +1,7 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const g = @import("spirv/grammar.zig"); |
| 2 | 3 | const Writer = std.ArrayList(u8).Writer; |
| 3 | 4 | |
| 4 | | //! See https://www.khronos.org/registry/spir-v/specs/unified1/MachineReadableGrammar.html |
| 5 | | //! and the files in https://github.com/KhronosGroup/SPIRV-Headers/blob/master/include/spirv/unified1/ |
| 6 | | //! Note: Non-canonical casing in these structs used to match SPIR-V spec json. |
| 7 | | const Registry = union(enum) { |
| 8 | | core: CoreRegistry, |
| 9 | | extension: ExtensionRegistry, |
| 10 | | }; |
| 11 | | |
| 12 | | const CoreRegistry = struct { |
| 13 | | copyright: [][]const u8, |
| 14 | | /// Hexadecimal representation of the magic number |
| 15 | | magic_number: []const u8, |
| 16 | | major_version: u32, |
| 17 | | minor_version: u32, |
| 18 | | revision: u32, |
| 19 | | instruction_printing_class: []InstructionPrintingClass, |
| 20 | | instructions: []Instruction, |
| 21 | | operand_kinds: []OperandKind, |
| 22 | | }; |
| 23 | | |
| 24 | | const ExtensionRegistry = struct { |
| 25 | | copyright: [][]const u8, |
| 26 | | version: u32, |
| 27 | | revision: u32, |
| 28 | | instructions: []Instruction, |
| 29 | | operand_kinds: []OperandKind = &[_]OperandKind{}, |
| 30 | | }; |
| 31 | | |
| 32 | | const InstructionPrintingClass = struct { |
| 33 | | tag: []const u8, |
| 34 | | heading: ?[]const u8 = null, |
| 35 | | }; |
| 36 | | |
| 37 | | const Instruction = struct { |
| 38 | | opname: []const u8, |
| 39 | | class: ?[]const u8 = null, // Note: Only available in the core registry. |
| 40 | | opcode: u32, |
| 41 | | operands: []Operand = &[_]Operand{}, |
| 42 | | capabilities: [][]const u8 = &[_][]const u8{}, |
| 43 | | extensions: [][]const u8 = &[_][]const u8{}, |
| 44 | | version: ?[]const u8 = null, |
| 45 | | |
| 46 | | lastVersion: ?[]const u8 = null, |
| 47 | | }; |
| 48 | | |
| 49 | | const Operand = struct { |
| 50 | | kind: []const u8, |
| 51 | | /// If this field is 'null', the operand is only expected once. |
| 52 | | quantifier: ?Quantifier = null, |
| 53 | | name: []const u8 = "", |
| 54 | | }; |
| 55 | | |
| 56 | | const Quantifier = enum { |
| 57 | | /// zero or once |
| 58 | | @"?", |
| 59 | | /// zero or more |
| 60 | | @"*", |
| 61 | | }; |
| 62 | | |
| 63 | | const OperandCategory = enum { |
| 64 | | BitEnum, |
| 65 | | ValueEnum, |
| 66 | | Id, |
| 67 | | Literal, |
| 68 | | Composite, |
| 69 | | }; |
| 70 | | |
| 71 | | const OperandKind = struct { |
| 72 | | category: OperandCategory, |
| 73 | | /// The name |
| 74 | | kind: []const u8, |
| 75 | | doc: ?[]const u8 = null, |
| 76 | | enumerants: ?[]Enumerant = null, |
| 77 | | bases: ?[]const []const u8 = null, |
| 78 | | }; |
| 79 | | |
| 80 | | const Enumerant = struct { |
| 81 | | enumerant: []const u8, |
| 82 | | value: union(enum) { |
| 83 | | bitflag: []const u8, // Hexadecimal representation of the value |
| 84 | | int: u31, |
| 85 | | }, |
| 86 | | capabilities: [][]const u8 = &[_][]const u8{}, |
| 87 | | /// Valid for .ValueEnum and .BitEnum |
| 88 | | extensions: [][]const u8 = &[_][]const u8{}, |
| 89 | | /// `quantifier` will always be `null`. |
| 90 | | parameters: []Operand = &[_]Operand{}, |
| 91 | | version: ?[]const u8 = null, |
| 92 | | lastVersion: ?[]const u8 = null, |
| 93 | | }; |
| 94 | | |
| 95 | 5 | pub fn main() !void { |
| 96 | 6 | var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); |
| 97 | 7 | defer arena.deinit(); |
| ... | ... | @@ -106,7 +16,7 @@ pub fn main() !void { |
| 106 | 16 | const spec = try std.fs.cwd().readFileAlloc(allocator, spec_path, std.math.maxInt(usize)); |
| 107 | 17 | |
| 108 | 18 | var tokens = std.json.TokenStream.init(spec); |
| 109 | | var registry = try std.json.parse(Registry, &tokens, .{.allocator = allocator}); |
| 19 | var registry = try std.json.parse(g.Registry, &tokens, .{.allocator = allocator}); |
| 110 | 20 | |
| 111 | 21 | var buf = std.ArrayList(u8).init(allocator); |
| 112 | 22 | defer buf.deinit(); |
| ... | ... | @@ -118,7 +28,7 @@ pub fn main() !void { |
| 118 | 28 | try std.io.getStdOut().writeAll(formatted); |
| 119 | 29 | } |
| 120 | 30 | |
| 121 | | fn render(writer: Writer, registry: Registry) !void { |
| 31 | fn render(writer: Writer, registry: g.Registry) !void { |
| 122 | 32 | try writer.writeAll( |
| 123 | 33 | \\//! This file is auto-generated by tools/gen_spirv_spec.zig. |
| 124 | 34 | \\ |
| ... | ... | @@ -149,7 +59,7 @@ fn render(writer: Writer, registry: Registry) !void { |
| 149 | 59 | } |
| 150 | 60 | } |
| 151 | 61 | |
| 152 | | fn renderOpcodes(writer: Writer, instructions: []const Instruction) !void { |
| 62 | fn renderOpcodes(writer: Writer, instructions: []const g.Instruction) !void { |
| 153 | 63 | try writer.writeAll("pub const Opcode = extern enum(u16) {\n"); |
| 154 | 64 | for (instructions) |instr| { |
| 155 | 65 | try writer.print("{} = {},\n", .{ std.zig.fmtId(instr.opname), instr.opcode }); |
| ... | ... | @@ -157,7 +67,7 @@ fn renderOpcodes(writer: Writer, instructions: []const Instruction) !void { |
| 157 | 67 | try writer.writeAll("_,\n};\n"); |
| 158 | 68 | } |
| 159 | 69 | |
| 160 | | fn renderOperandKinds(writer: Writer, kinds: []const OperandKind) !void { |
| 70 | fn renderOperandKinds(writer: Writer, kinds: []const g.OperandKind) !void { |
| 161 | 71 | for (kinds) |kind| { |
| 162 | 72 | switch (kind.category) { |
| 163 | 73 | .ValueEnum => try renderValueEnum(writer, kind), |
| ... | ... | @@ -167,7 +77,7 @@ fn renderOperandKinds(writer: Writer, kinds: []const OperandKind) !void { |
| 167 | 77 | } |
| 168 | 78 | } |
| 169 | 79 | |
| 170 | | fn renderValueEnum(writer: Writer, enumeration: OperandKind) !void { |
| 80 | fn renderValueEnum(writer: Writer, enumeration: g.OperandKind) !void { |
| 171 | 81 | try writer.print("pub const {s} = extern enum(u32) {{\n", .{ enumeration.kind }); |
| 172 | 82 | |
| 173 | 83 | const enumerants = enumeration.enumerants orelse return error.InvalidRegistry; |
| ... | ... | @@ -180,7 +90,7 @@ fn renderValueEnum(writer: Writer, enumeration: OperandKind) !void { |
| 180 | 90 | try writer.writeAll("_,\n};\n"); |
| 181 | 91 | } |
| 182 | 92 | |
| 183 | | fn renderBitEnum(writer: Writer, enumeration: OperandKind) !void { |
| 93 | fn renderBitEnum(writer: Writer, enumeration: g.OperandKind) !void { |
| 184 | 94 | try writer.print("pub const {s} = packed struct {{\n", .{ enumeration.kind }); |
| 185 | 95 | |
| 186 | 96 | var flags_by_bitpos = [_]?[]const u8{null} ** 32; |