aboutsummaryrefslogtreecommitdiff
path: root/tools/spirv/grammar.zig
blob: 93bbf561fd4ce04cb19187e46902a02e3a7ae443 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//! 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 std = @import("std");

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 = null,
    version: ?u32 = null,
    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.
    aliases: [][]const u8 = &[_][]const u8{},
    opcode: u32,
    operands: []Operand = &[_]Operand{},
    capabilities: [][]const u8 = &[_][]const u8{},
    provisional: bool = false,
    // DebugModuleINTEL has this...
    capability: ?[]const u8 = null,
    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,
    aliases: [][]const u8 = &[_][]const u8{},
    value: union(enum) {
        bitflag: []const u8, // Hexadecimal representation of the value
        int: u31,

        pub fn jsonParse(
            allocator: std.mem.Allocator,
            source: anytype,
            options: std.json.ParseOptions,
        ) std.json.ParseError(@TypeOf(source.*))!@This() {
            _ = options;
            switch (try source.nextAlloc(allocator, .alloc_if_needed)) {
                inline .string, .allocated_string => |s| return @This(){ .bitflag = s },
                inline .number, .allocated_number => |s| return @This(){ .int = try std.fmt.parseInt(u31, s, 10) },
                else => return error.UnexpectedToken,
            }
        }
        pub const jsonStringify = @compileError("not supported");
    },
    capabilities: [][]const u8 = &[_][]const u8{},
    provisional: bool = false,
    /// 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,
};