From d8642bfe0ac9ab578d4f8a0a7e3a16ca4110614e Mon Sep 17 00:00:00 2001 From: luccie Date: Sun, 10 Aug 2025 22:52:04 +0200 Subject: [PATCH] [MODS/IR]: Fix a bug in fix_branches_for_block --- src/mods/ir.zig | 8 +++----- src/mods/vm.zig | 9 +++++---- src/sideros.zig | 4 ++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/mods/ir.zig b/src/mods/ir.zig index 48a6700..a6624db 100644 --- a/src/mods/ir.zig +++ b/src/mods/ir.zig @@ -630,7 +630,6 @@ const IRParserState = struct { 0x02...0x03 => self.parseBlock(b), 0x04 => self.parseIf(), 0x0C...0x0D => self.parseBranch(b), - // 0x0E => self.parseTableBranch(b), 0x0E => self.parseBrTable(b), 0x0F => self.push(@enumFromInt(b), .{ .u64 = 0 }), 0x10 => self.push(@enumFromInt(b), .{ .u32 = try self.parser.readU32() }), @@ -793,14 +792,12 @@ const IRParserState = struct { var todel: std.ArrayListUnmanaged(u32) = .{}; defer todel.deinit(self.allocator); - var idx: u32 = 0; - for (self.branches.items) |branch| { + for (self.branches.items, 0..) |branch, idx| { if (start <= branch.pc and branch.pc < end) { const ptr = if (branch.table) &self.br_table_vectors.items[branch.index] else &self.indices.items[branch.index].u32; if (ptr.* == 0) { ptr.* = jump_addr; try todel.append(self.allocator, @intCast(idx)); - idx += 1; } else { ptr.* -= 1; } @@ -863,7 +860,8 @@ pub fn parse(parser: *Parser) !IR { .allocator = parser.allocator, }; try state.parseFunction(); - // if (state.branches.count() != 0) return Parser.Error.unresolved_branch; + std.debug.print("Br length: {d}\n", .{state.branches.items.len}); + if (state.branches.items.len != 0) return Parser.Error.unresolved_branch; return .{ .opcodes = try state.opcodes.toOwnedSlice(state.allocator), .indices = try state.indices.toOwnedSlice(state.allocator), diff --git a/src/mods/vm.zig b/src/mods/vm.zig index 24e08ae..9e3e2a0 100644 --- a/src/mods/vm.zig +++ b/src/mods/vm.zig @@ -123,7 +123,7 @@ pub const Runtime = struct { loop: while (frame.program_counter < frame.code.opcodes.len) { const opcode: IR.Opcode = frame.code.opcodes[frame.program_counter]; const index = frame.code.indices[frame.program_counter]; - // std.debug.print("Executing at {X} {any} \n", .{frame.program_counter, opcode}); + std.debug.print("Executing at {X} {any} {X}\n", .{frame.program_counter, opcode, if (opcode == IR.Opcode.br_if) @as(i64, @intCast(index.u32)) else -1}); switch (opcode) { .@"unreachable" => { std.debug.panic("Reached unreachable statement at IR counter {any}\n", .{frame.program_counter}); @@ -134,7 +134,7 @@ pub const Runtime = struct { continue; }, .br_if => { - if (self.stack.items[self.stack.items.len - 1].i32 != 0) { + if (self.stack.pop().?.i32 != 0) { frame.program_counter = index.u32; continue; } @@ -408,7 +408,7 @@ pub const Runtime = struct { .i64_lt_u => { const a = @as(u64, @intCast(self.stack.pop().?.i64)); const b = @as(u64, @intCast(self.stack.pop().?.i64)); - try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(b < a))) }); + try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(b < a))) }); }, .i64_gt_s => @panic("UNIMPLEMENTED"), .i64_gt_u => @panic("UNIMPLEMENTED"), @@ -531,7 +531,7 @@ pub const Runtime = struct { .i64_shl => { const a = self.stack.pop().?.i64; const b = self.stack.pop().?.i64; - try self.stack.append(.{ .i64 = (b << @as(u6, @intCast(a))) }); + try self.stack.append(.{ .i64 = @intCast(b << @as(u6, @intCast(a))) }); }, .i64_shr_s => @panic("UNIMPLEMENTED"), .i64_shr_u => { @@ -656,6 +656,7 @@ pub const Runtime = struct { } switch (f.typ) { .internal => { + std.debug.print("Calling {d}\n", .{function}); const ir: IR = f.typ.internal.ir; const function_type = f.func_type; var frame = CallFrame{ diff --git a/src/sideros.zig b/src/sideros.zig index 5c22c70..54603c1 100644 --- a/src/sideros.zig +++ b/src/sideros.zig @@ -26,8 +26,8 @@ fn init_mods() void { var global_runtime = mods.GlobalRuntime.init(allocator); defer global_runtime.deinit(); - const file = std.fs.cwd().openFile("assets/mods/core.wasm", .{}) catch @panic("Couldn't open assets/mods/core.wasm"); - // const file = std.fs.cwd().openFile("./test.wasm", .{}) catch @panic("Couldn't open test.wasm"); + // const file = std.fs.cwd().openFile("assets/mods/core.wasm", .{}) catch @panic("Couldn't open assets/mods/core.wasm"); + const file = std.fs.cwd().openFile("./test.wasm", .{}) catch @panic("Couldn't open test.wasm"); const all = file.readToEndAlloc(allocator, 1_000_000) catch @panic("Unable to read the file"); // 1 MB defer allocator.free(all); var parser = mods.Parser.init(allocator, all) catch @panic("Failed to init parser");