Skip to content

Commit c228d86

Browse files
committed
adding types opcodes
1 parent 3283ed4 commit c228d86

4 files changed

Lines changed: 301 additions & 57 deletions

File tree

src/Module.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,23 @@ pub fn init(allocator: std.mem.Allocator, source: []const SpvWord) ModuleError!S
9595

9696
self.it = WordIterator.init(self.code);
9797

98-
const magic = self.it.next() orelse return ModuleError.InvalidSpirV;
98+
const magic = self.it.next() catch return ModuleError.InvalidSpirV;
9999
if (magic != spv.SpvMagicNumber) {
100100
return ModuleError.InvalidMagic;
101101
}
102102
if (!checkEndiannessFromSpvMagic(magic)) {
103103
return ModuleError.UnsupportedEndianness;
104104
}
105105

106-
const version = self.it.next() orelse return ModuleError.InvalidSpirV;
106+
const version = self.it.next() catch return ModuleError.InvalidSpirV;
107107
self.version_major = @intCast((version & 0x00FF0000) >> 16);
108108
self.version_minor = @intCast((version & 0x0000FF00) >> 8);
109109

110-
const generator = self.it.next() orelse return ModuleError.InvalidSpirV;
110+
const generator = self.it.next() catch return ModuleError.InvalidSpirV;
111111
self.generator_id = @intCast((generator & 0xFFFF0000) >> 16);
112112
self.generator_version = @intCast(generator & 0x0000FFFF);
113113

114-
self.bound = self.it.next() orelse return ModuleError.InvalidSpirV;
114+
self.bound = self.it.next() catch return ModuleError.InvalidSpirV;
115115
self.results.resize(allocator, self.bound) catch return ModuleError.OutOfMemory;
116116
for (self.results.items) |*result| {
117117
result.* = Result.init();
@@ -181,7 +181,7 @@ fn checkEndiannessFromSpvMagic(magic: SpvWord) bool {
181181
fn pass(self: *Self, allocator: std.mem.Allocator, opcodes: std.EnumSet(spv.SpvOp)) ModuleError!void {
182182
var rt = Runtime.init(self);
183183
defer rt.deinit();
184-
while (rt.it.next()) |opcode_data| {
184+
while (rt.it.nextOrNull()) |opcode_data| {
185185
const word_count = ((opcode_data & (~spv.SpvOpCodeMask)) >> spv.SpvWordCountShift) - 1;
186186
const opcode = (opcode_data & spv.SpvOpCodeMask);
187187

src/Result.zig

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,36 @@ const SpvByte = spv.SpvByte;
66
const SpvWord = spv.SpvWord;
77
const SpvBool = spv.SpvBool;
88

9-
const RType = enum {
9+
const Type = enum {
1010
None,
1111
String,
1212
Extension,
13-
Function_type,
13+
FunctionType,
1414
Type,
1515
Variable,
1616
Constant,
1717
Function,
18-
Access_chain,
19-
Function_parameter,
18+
AccessChain,
19+
FunctionParameter,
2020
Label,
2121
};
2222

23+
const ValueType = enum {
24+
Void,
25+
Bool,
26+
Int,
27+
Float,
28+
Vector,
29+
Matrix,
30+
Array,
31+
RuntimeArray,
32+
Structure,
33+
Image,
34+
Sampler,
35+
SampledImage,
36+
Pointer,
37+
};
38+
2339
const ImageInfo = struct {
2440
dim: spv.SpvDim,
2541
depth: SpvByte,
@@ -33,37 +49,81 @@ const ImageInfo = struct {
3349
const Decoration = struct {
3450
rtype: spv.SpvDecoration,
3551
literal_1: SpvWord,
36-
literal_2: SpvWord,
52+
literal_2: ?SpvWord,
3753
index: SpvWord,
3854
};
3955

4056
const Self = @This();
4157

4258
name: ?[]const u8,
43-
ptr: SpvWord,
4459

45-
storage_class: spv.SpvStorageClass,
4660
parent: ?*const Self,
4761

4862
member_names: std.ArrayList([]const u8),
4963
members: std.ArrayList(spv.SpvMember),
5064

5165
decorations: std.ArrayList(Decoration),
5266

53-
/// Only for functions
54-
return_type: SpvWord,
55-
56-
rtype: RType,
67+
res_type: Type,
68+
type_data: union(Type) {
69+
None: struct {},
70+
String: []const u8,
71+
Extension: struct {},
72+
FunctionType: struct {
73+
return_type: SpvWord,
74+
},
75+
Type: struct {
76+
value_type: ValueType,
77+
data: union(ValueType) {
78+
Void: struct {},
79+
Bool: struct {},
80+
Int: struct {
81+
bit_length: SpvWord,
82+
is_signed: bool,
83+
},
84+
Float: struct {
85+
bit_length: SpvWord,
86+
},
87+
Vector: struct {
88+
components_type: ValueType,
89+
},
90+
Matrix: struct {
91+
column_type: ValueType,
92+
},
93+
Array: struct {},
94+
RuntimeArray: struct {},
95+
Structure: struct {},
96+
Image: struct {},
97+
Sampler: struct {},
98+
SampledImage: struct {},
99+
Pointer: struct {
100+
storage_class: spv.SpvStorageClass,
101+
},
102+
},
103+
member_count: SpvWord = 0,
104+
id: SpvWord = 0,
105+
},
106+
Variable: struct {},
107+
Constant: struct {},
108+
Function: struct {
109+
/// Allocated array
110+
params: []SpvWord,
111+
},
112+
AccessChain: struct {},
113+
FunctionParameter: struct {},
114+
Label: struct {},
115+
},
57116

58117
pub fn init() Self {
59-
return std.mem.zeroInit(Self, .{
118+
return .{
60119
.name = null,
61120
.parent = null,
62-
.member_names = std.ArrayList([]const u8).empty,
63-
.members = std.ArrayList(spv.SpvMember).empty,
64-
.decorations = std.ArrayList(Decoration).empty,
65-
.rtype = RType.None,
66-
});
121+
.member_names = .empty,
122+
.members = .empty,
123+
.decorations = .empty,
124+
.res_type = .None,
125+
.type_data = undefined,
126+
};
67127
}
68128

69129
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
@@ -73,6 +133,11 @@ pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
73133
for (self.member_names.items) |name| {
74134
allocator.free(name);
75135
}
136+
// FIXME
137+
//switch (self.type_data) {
138+
// .Function => |data| allocator.free(data.params),
139+
// else => {},
140+
//}
76141
self.member_names.deinit(allocator);
77142
self.members.deinit(allocator);
78143
self.decorations.deinit(allocator);

src/WordIterator.zig

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const std = @import("std");
22
const spv = @import("spv.zig");
33

4+
const RuntimeError = @import("Runtime.zig").RuntimeError;
5+
46
const SpvWord = spv.SpvWord;
57

68
const Self = @This();
@@ -15,16 +17,22 @@ pub fn init(buffer: []const SpvWord) Self {
1517
};
1618
}
1719

18-
pub fn next(self: *Self) ?SpvWord {
20+
pub fn nextOrNull(self: *Self) ?SpvWord {
1921
const word = self.peek() orelse return null;
2022
self.index += 1;
2123
return word;
2224
}
2325

24-
pub fn nextAs(self: *Self, comptime E: type) ?E {
25-
const word = self.peek() orelse return null;
26-
self.index += 1;
27-
return std.enums.fromInt(E, word);
26+
pub inline fn nextAsOrNull(self: *Self, comptime E: type) ?E {
27+
return if (self.nextOrNull()) |word| std.enums.fromInt(E, word) else null;
28+
}
29+
30+
pub inline fn next(self: *Self) RuntimeError!SpvWord {
31+
return self.nextOrNull() orelse return RuntimeError.InvalidSpirV;
32+
}
33+
34+
pub fn nextAs(self: *Self, comptime E: type) RuntimeError!E {
35+
return self.nextAsOrNull(E) orelse return RuntimeError.InvalidSpirV;
2836
}
2937

3038
pub fn peek(self: *const Self) ?SpvWord {

0 commit comments

Comments
 (0)