From 52c1f96040c6ce0f147f12bbbfd27f0532d3f9d3 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Wed, 7 Jan 2026 19:13:10 +0100 Subject: [PATCH] working on restuls --- src/Module.zig | 16 ++++++++-------- src/Result.zig | 34 ++++++++++++++++++++++++++++------ src/opcodes.zig | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 80 insertions(+), 18 deletions(-) diff --git a/src/Module.zig b/src/Module.zig index df7824c..6267faf 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -17,7 +17,7 @@ const Result = @import("Result.zig"); const Runtime = @import("Runtime.zig"); const WordIterator = @import("WordIterator.zig"); -const SpvValue = Result.SpvValue; +const Value = Result.Value; const Self = @This(); @@ -74,10 +74,10 @@ geometry_output_count: SpvWord, geometry_input: SpvWord, geometry_output: SpvWord, -input_locations: std.AutoHashMap(SpvWord, *SpvValue), -output_locations: std.AutoHashMap(SpvWord, *SpvValue), -bindings: std.AutoHashMap(SpvBinding, *SpvValue), -push_constants: []SpvValue, +input_locations: std.AutoHashMap(SpvWord, *Value), +output_locations: std.AutoHashMap(SpvWord, *Value), +bindings: std.AutoHashMap(SpvBinding, *Value), +push_constants: []Value, pub fn init(allocator: std.mem.Allocator, source: []const SpvWord) ModuleError!Self { var self: Self = std.mem.zeroInit(Self, .{ @@ -90,9 +90,9 @@ pub fn init(allocator: std.mem.Allocator, source: []const SpvWord) ModuleError!S .local_size_x = 1, .local_size_y = 1, .local_size_z = 1, - .input_locations = std.AutoHashMap(SpvWord, *SpvValue).init(allocator), - .output_locations = std.AutoHashMap(SpvWord, *SpvValue).init(allocator), - .bindings = std.AutoHashMap(SpvBinding, *SpvValue).init(allocator), + .input_locations = std.AutoHashMap(SpvWord, *Value).init(allocator), + .output_locations = std.AutoHashMap(SpvWord, *Value).init(allocator), + .bindings = std.AutoHashMap(SpvBinding, *Value).init(allocator), }); errdefer self.deinit(allocator); diff --git a/src/Result.zig b/src/Result.zig index 259c177..627f0f8 100644 --- a/src/Result.zig +++ b/src/Result.zig @@ -1,6 +1,8 @@ const std = @import("std"); const spv = @import("spv.zig"); +const RuntimeError = @import("Runtime.zig").RuntimeError; + const SpvVoid = spv.SpvVoid; const SpvByte = spv.SpvByte; const SpvWord = spv.SpvWord; @@ -52,7 +54,7 @@ const Decoration = struct { index: SpvWord, }; -pub const SpvValue = union(Type) { +pub const Value = union(Type) { Void: noreturn, Bool: bool, Int: union { @@ -70,8 +72,8 @@ pub const SpvValue = union(Type) { float32: f32, float64: f64, }, - Vector: noreturn, - Matrix: noreturn, + Vector: []Value, + Matrix: []Value, Array: struct {}, RuntimeArray: struct {}, Structure: struct {}, @@ -134,10 +136,15 @@ variant: ?union(Variant) { }, Variable: struct { storage_class: spv.SpvStorageClass, - value: SpvValue, + value: Value, + }, + Constant: Value, + Function: struct { + return_type: SpvWord, + function_type: SpvWord, + /// Allocated array + params: []const SpvWord, }, - Constant: SpvValue, - Function: struct {}, AccessChain: struct {}, FunctionParameter: struct {}, Label: struct {}, @@ -188,3 +195,18 @@ pub fn resolveType(self: *const Self, results: []const Self) *const Self { else self; } + +pub fn initConstantValue(self: *Self, results: []const Self, target: SpvWord) RuntimeError!void { + _ = self; + const resolved = results[target].resolveType(results); + if (resolved.variant) |variant| { + switch (variant) { + .Type => |t| switch (t) { + .Structure => |s| + else => return RuntimeError.InvalidSpirV, + }, + else => return RuntimeError.InvalidSpirV, + } + } + return RuntimeError.InvalidSpirV; +} diff --git a/src/opcodes.zig b/src/opcodes.zig index 2fd0412..c3f989d 100644 --- a/src/opcodes.zig +++ b/src/opcodes.zig @@ -15,9 +15,6 @@ const SpvBool = spv.SpvBool; // DUMB INDEV OPCODES TODO : // OpVariable X -// OpFunction X -// OpLabel X -// OpCompositeConstruct X // OpAccessChain X // OpStore X // OpLoad X @@ -51,6 +48,9 @@ pub const SetupDispatcher = block: { .TypeVector = opTypeVector, .TypeVoid = opTypeVoid, .Variable = opVariable, + .Function = opFunction, + .Label = opLabel, + .CompositeConstruct = opCompositeConstruct, }); }; @@ -334,7 +334,7 @@ fn opConstant(_: std.mem.Allocator, word_count: SpvWord, rt: *Runtime) RuntimeEr .Int => { break :value if (word_count - 2 != 1) .{ .Int = .{ - .uint64 = try rt.it.next() | (@as(u64, try rt.it.next()) >> 32), + .uint64 = @as(u64, try rt.it.next()) | (@as(u64, try rt.it.next()) >> 32), }, } else .{ .Int = .{ @@ -382,6 +382,46 @@ fn opVariable(_: std.mem.Allocator, word_count: SpvWord, rt: *Runtime) RuntimeEr }; } +fn opFunction(allocator: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void { + const return_type = try rt.it.next(); + const id = try rt.it.next(); + _ = rt.it.skip(); // Skip function control + const function_type_id = try rt.it.next(); + + rt.mod.results.items[id].variant = .{ + .Function = .{ + .return_type = return_type, + .function_type = function_type_id, + .params = params: { + if (rt.mod.results.items[function_type_id].variant) |variant| { + const params_count = switch (variant) { + .Type => |t| switch (t) { + .Function => |f| f.params.len, + else => return RuntimeError.InvalidSpirV, + }, + else => return RuntimeError.InvalidSpirV, + }; + break :params allocator.alloc(SpvWord, params_count) catch return RuntimeError.OutOfMemory; + } + return RuntimeError.InvalidSpirV; + }, + }, + }; + + rt.current_function = &rt.mod.results.items[id]; +} + +fn opLabel(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void { + const id = try rt.it.next(); + rt.mod.results.items[id].variant = .{ + .Label = .{}, + }; +} + +fn opCompositeConstruct(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void { + _ = rt; +} + fn readString(allocator: std.mem.Allocator, it: *WordIterator) RuntimeError![]const u8 { var str: std.ArrayList(u8) = .empty; while (it.nextOrNull()) |word| {