Skip to content

Commit 52c1f96

Browse files
committed
working on restuls
1 parent 02b8c69 commit 52c1f96

3 files changed

Lines changed: 80 additions & 18 deletions

File tree

src/Module.zig

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const Result = @import("Result.zig");
1717
const Runtime = @import("Runtime.zig");
1818
const WordIterator = @import("WordIterator.zig");
1919

20-
const SpvValue = Result.SpvValue;
20+
const Value = Result.Value;
2121

2222
const Self = @This();
2323

@@ -74,10 +74,10 @@ geometry_output_count: SpvWord,
7474
geometry_input: SpvWord,
7575
geometry_output: SpvWord,
7676

77-
input_locations: std.AutoHashMap(SpvWord, *SpvValue),
78-
output_locations: std.AutoHashMap(SpvWord, *SpvValue),
79-
bindings: std.AutoHashMap(SpvBinding, *SpvValue),
80-
push_constants: []SpvValue,
77+
input_locations: std.AutoHashMap(SpvWord, *Value),
78+
output_locations: std.AutoHashMap(SpvWord, *Value),
79+
bindings: std.AutoHashMap(SpvBinding, *Value),
80+
push_constants: []Value,
8181

8282
pub fn init(allocator: std.mem.Allocator, source: []const SpvWord) ModuleError!Self {
8383
var self: Self = std.mem.zeroInit(Self, .{
@@ -90,9 +90,9 @@ pub fn init(allocator: std.mem.Allocator, source: []const SpvWord) ModuleError!S
9090
.local_size_x = 1,
9191
.local_size_y = 1,
9292
.local_size_z = 1,
93-
.input_locations = std.AutoHashMap(SpvWord, *SpvValue).init(allocator),
94-
.output_locations = std.AutoHashMap(SpvWord, *SpvValue).init(allocator),
95-
.bindings = std.AutoHashMap(SpvBinding, *SpvValue).init(allocator),
93+
.input_locations = std.AutoHashMap(SpvWord, *Value).init(allocator),
94+
.output_locations = std.AutoHashMap(SpvWord, *Value).init(allocator),
95+
.bindings = std.AutoHashMap(SpvBinding, *Value).init(allocator),
9696
});
9797
errdefer self.deinit(allocator);
9898

src/Result.zig

Lines changed: 28 additions & 6 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 SpvVoid = spv.SpvVoid;
57
const SpvByte = spv.SpvByte;
68
const SpvWord = spv.SpvWord;
@@ -52,7 +54,7 @@ const Decoration = struct {
5254
index: SpvWord,
5355
};
5456

55-
pub const SpvValue = union(Type) {
57+
pub const Value = union(Type) {
5658
Void: noreturn,
5759
Bool: bool,
5860
Int: union {
@@ -70,8 +72,8 @@ pub const SpvValue = union(Type) {
7072
float32: f32,
7173
float64: f64,
7274
},
73-
Vector: noreturn,
74-
Matrix: noreturn,
75+
Vector: []Value,
76+
Matrix: []Value,
7577
Array: struct {},
7678
RuntimeArray: struct {},
7779
Structure: struct {},
@@ -134,10 +136,15 @@ variant: ?union(Variant) {
134136
},
135137
Variable: struct {
136138
storage_class: spv.SpvStorageClass,
137-
value: SpvValue,
139+
value: Value,
140+
},
141+
Constant: Value,
142+
Function: struct {
143+
return_type: SpvWord,
144+
function_type: SpvWord,
145+
/// Allocated array
146+
params: []const SpvWord,
138147
},
139-
Constant: SpvValue,
140-
Function: struct {},
141148
AccessChain: struct {},
142149
FunctionParameter: struct {},
143150
Label: struct {},
@@ -188,3 +195,18 @@ pub fn resolveType(self: *const Self, results: []const Self) *const Self {
188195
else
189196
self;
190197
}
198+
199+
pub fn initConstantValue(self: *Self, results: []const Self, target: SpvWord) RuntimeError!void {
200+
_ = self;
201+
const resolved = results[target].resolveType(results);
202+
if (resolved.variant) |variant| {
203+
switch (variant) {
204+
.Type => |t| switch (t) {
205+
.Structure => |s|
206+
else => return RuntimeError.InvalidSpirV,
207+
},
208+
else => return RuntimeError.InvalidSpirV,
209+
}
210+
}
211+
return RuntimeError.InvalidSpirV;
212+
}

src/opcodes.zig

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ const SpvBool = spv.SpvBool;
1515

1616
// DUMB INDEV OPCODES TODO :
1717
// OpVariable X
18-
// OpFunction X
19-
// OpLabel X
20-
// OpCompositeConstruct X
2118
// OpAccessChain X
2219
// OpStore X
2320
// OpLoad X
@@ -51,6 +48,9 @@ pub const SetupDispatcher = block: {
5148
.TypeVector = opTypeVector,
5249
.TypeVoid = opTypeVoid,
5350
.Variable = opVariable,
51+
.Function = opFunction,
52+
.Label = opLabel,
53+
.CompositeConstruct = opCompositeConstruct,
5454
});
5555
};
5656

@@ -334,7 +334,7 @@ fn opConstant(_: std.mem.Allocator, word_count: SpvWord, rt: *Runtime) RuntimeEr
334334
.Int => {
335335
break :value if (word_count - 2 != 1) .{
336336
.Int = .{
337-
.uint64 = try rt.it.next() | (@as(u64, try rt.it.next()) >> 32),
337+
.uint64 = @as(u64, try rt.it.next()) | (@as(u64, try rt.it.next()) >> 32),
338338
},
339339
} else .{
340340
.Int = .{
@@ -382,6 +382,46 @@ fn opVariable(_: std.mem.Allocator, word_count: SpvWord, rt: *Runtime) RuntimeEr
382382
};
383383
}
384384

385+
fn opFunction(allocator: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
386+
const return_type = try rt.it.next();
387+
const id = try rt.it.next();
388+
_ = rt.it.skip(); // Skip function control
389+
const function_type_id = try rt.it.next();
390+
391+
rt.mod.results.items[id].variant = .{
392+
.Function = .{
393+
.return_type = return_type,
394+
.function_type = function_type_id,
395+
.params = params: {
396+
if (rt.mod.results.items[function_type_id].variant) |variant| {
397+
const params_count = switch (variant) {
398+
.Type => |t| switch (t) {
399+
.Function => |f| f.params.len,
400+
else => return RuntimeError.InvalidSpirV,
401+
},
402+
else => return RuntimeError.InvalidSpirV,
403+
};
404+
break :params allocator.alloc(SpvWord, params_count) catch return RuntimeError.OutOfMemory;
405+
}
406+
return RuntimeError.InvalidSpirV;
407+
},
408+
},
409+
};
410+
411+
rt.current_function = &rt.mod.results.items[id];
412+
}
413+
414+
fn opLabel(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
415+
const id = try rt.it.next();
416+
rt.mod.results.items[id].variant = .{
417+
.Label = .{},
418+
};
419+
}
420+
421+
fn opCompositeConstruct(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
422+
_ = rt;
423+
}
424+
385425
fn readString(allocator: std.mem.Allocator, it: *WordIterator) RuntimeError![]const u8 {
386426
var str: std.ArrayList(u8) = .empty;
387427
while (it.nextOrNull()) |word| {

0 commit comments

Comments
 (0)