Fix branching and VM

Signed-off-by: luccie-cmd <luccie@sideros.org>
This commit is contained in:
luccie-cmd 2025-03-26 17:18:33 +01:00 committed by Ernesto Lanchares
parent bfe9fb5e63
commit 1db7f9e506
2 changed files with 296 additions and 260 deletions

View file

@ -41,8 +41,11 @@ pub fn main() !void {
var runtime = try mods.Runtime.init(allocator, module, &global_runtime); var runtime = try mods.Runtime.init(allocator, module, &global_runtime);
defer runtime.deinit(allocator); defer runtime.deinit(allocator);
var parameters = [_]usize{}; var parameters = [_]usize{17};
try runtime.callExternal(allocator, "preinit", &parameters); try runtime.callExternal(allocator, "preinit", &parameters);
const result = runtime.stack.pop().?;
std.debug.print("Result of preinit: {any}\n", .{result});
const w = try window.Window.create(800, 600, "sideros"); const w = try window.Window.create(800, 600, "sideros");
defer w.destroy(); defer w.destroy();
@ -62,7 +65,6 @@ pub fn main() !void {
// try pool.addComponent(entity, ecs.components.Speed{ .speed = 5.0 }); // try pool.addComponent(entity, ecs.components.Speed{ .speed = 5.0 });
// } // }
// TODO(luccie-cmd): Renderer.create shouldn't return an error
// var r = try Renderer.create(allocator, w); // var r = try Renderer.create(allocator, w);
// defer r.destroy(); // defer r.destroy();

View file

@ -162,7 +162,7 @@ pub const Runtime = struct {
loop: while (true) { loop: while (true) {
const byte: u8 = frame.code[frame.program_counter]; const byte: u8 = frame.code[frame.program_counter];
frame.program_counter += 1; frame.program_counter += 1;
std.debug.print("b: {x}\n", .{byte}); // std.debug.print("b: 0x{x} pc: 0x{x}\n", .{ byte, frame.program_counter-1 });
switch (byte) { switch (byte) {
0x02 => { 0x02 => {
var depth: usize = 1; var depth: usize = 1;
@ -170,8 +170,9 @@ pub const Runtime = struct {
while (depth > 0) { while (depth > 0) {
const opcode = frame.code[pc]; const opcode = frame.code[pc];
const operand = frame.code[pc + 1]; const operand = frame.code[pc + 1];
if (opcode == 0x02 and operand == 0x40) { if ((opcode == 0x02 and operand == 0x40) or (opcode == 0x03 and operand == 0x40) or (opcode == 0x04 and operand == 0x40)) {
depth += 1; depth += 1;
pc += 1;
} else if (opcode == 0x0B) { } else if (opcode == 0x0B) {
depth -= 1; depth -= 1;
} }
@ -181,21 +182,28 @@ pub const Runtime = struct {
frame.program_counter += 1; frame.program_counter += 1;
}, },
0x03 => { 0x03 => {
try self.labels.append(frame.program_counter); try self.labels.append(frame.program_counter-1);
frame.program_counter += 1; frame.program_counter += 1;
//const a = frame.code[frame.program_counter];
for_loop = true; for_loop = true;
}, },
0x0d => { 0x0c => {
const label = leb128Decode(u32, frame.code[frame.program_counter..]); const label = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += label.len;
var address = @as(usize, 0); var address = @as(usize, 0);
for (0..(label.val)) |_| { for (0..(label.val+(if(label.val == 0) @as(u32, 1) else @as(u32, 0)))) |_| {
address = self.labels.pop().?; address = self.labels.pop().?;
} }
if (self.stack.pop().?.i32 != 0) {
frame.program_counter = address; frame.program_counter = address;
},
0x0d => {
if (self.stack.pop().?.i32 != 0){
const label = leb128Decode(u32, frame.code[frame.program_counter..]);
var address = @as(usize, 0);
for (0..(label.val+(if(label.val == 0) @as(u32, 1) else @as(u32, 0)))) |_| {
address = self.labels.pop().?;
}
frame.program_counter = address;
} else {
frame.program_counter += 1;
} }
}, },
@ -215,8 +223,9 @@ pub const Runtime = struct {
const integer = leb128Decode(u32, frame.code[frame.program_counter..]); const integer = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += integer.len; frame.program_counter += integer.len;
frame.locals[integer.val] = self.stack.pop().?; const a = self.stack.pop().?;
try self.stack.append(Value{ .i32 = @intCast(integer.val) }); frame.locals[integer.val] = a;
try self.stack.append(a);
}, },
0x28 => { 0x28 => {
const address = leb128Decode(u32, frame.code[frame.program_counter..]); const address = leb128Decode(u32, frame.code[frame.program_counter..]);
@ -284,33 +293,45 @@ pub const Runtime = struct {
0x47 => { 0x47 => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i32 != self.stack.pop().?.i32))) }); try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i32 != self.stack.pop().?.i32))) });
}, },
0x48 => { // 0x48 => {
const a = self.stack.pop().?.i32; // const a = self.stack.pop().?.i32;
const b = self.stack.pop().?.i32; // const b = self.stack.pop().?.i32;
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(b < a))) }); // try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(b < a))) });
}, // },
0x49 => { 0x49 => {
const a = self.stack.pop().?.i32; const a = self.stack.pop().?.i32;
const b = self.stack.pop().?.i32; const b = self.stack.pop().?.i32;
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(b < a))) }); try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(b < a))) });
}, },
0x4a => { // 0x4b => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i32 > self.stack.pop().?.i32))) }); // const b = self.stack.pop().?.i32;
}, // const a = self.stack.pop().?.i32;
0x4b => { // try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(a > b))) });
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(@as(u32, @bitCast(self.stack.pop().?.i32)) > @as(u32, @bitCast(self.stack.pop().?.i32))))) }); // },
}, // 0x4d => {
0x4c => { // const b = self.stack.pop().?.i32;
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i32 <= self.stack.pop().?.i32))) }); // const a = self.stack.pop().?.i32;
}, // try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(b <= a))) });
0x4d => { // },
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(@as(u32, @bitCast(self.stack.pop().?.i32)) <= @as(u32, @bitCast(self.stack.pop().?.i32))))) }); // 0x4a => {
}, // try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i32 > self.stack.pop().?.i32))) });
0x4e => { // },
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i32 >= self.stack.pop().?.i32))) }); // 0x4b => {
}, // try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(@as(u32, @bitCast(self.stack.pop().?.i32)) > @as(u32, @bitCast(self.stack.pop().?.i32))))) });
// },
// 0x4c => {
// try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i32 <= self.stack.pop().?.i32))) });
// },
// 0x4d => {
// try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(@as(u32, @bitCast(self.stack.pop().?.i32)) <= @as(u32, @bitCast(self.stack.pop().?.i32))))) });
// },
// 0x4e => {
// try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i32 >= self.stack.pop().?.i32))) });
// },
0x4f => { 0x4f => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(@as(u32, @bitCast(self.stack.pop().?.i32)) >= @as(u32, @bitCast(self.stack.pop().?.i32))))) }); const a = self.stack.pop().?.i32;
const b = self.stack.pop().?.i32;
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(a >= b))) });
}, },
0x50 => { 0x50 => {
@ -322,247 +343,254 @@ pub const Runtime = struct {
0x52 => { 0x52 => {
try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i64 != self.stack.pop().?.i64))) }); try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i64 != self.stack.pop().?.i64))) });
}, },
0x53 => { // 0x53 => {
try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i64 < self.stack.pop().?.i64))) }); // try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i64 < self.stack.pop().?.i64))) });
}, // },
0x54 => { // 0x54 => {
try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(@as(u64, @bitCast(self.stack.pop().?.i64)) < @as(u64, @bitCast(self.stack.pop().?.i64))))) }); // try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(@as(u64, @bitCast(self.stack.pop().?.i64)) < @as(u64, @bitCast(self.stack.pop().?.i64))))) });
}, // },
0x55 => { // 0x55 => {
try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i64 > self.stack.pop().?.i64))) }); // try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i64 > self.stack.pop().?.i64))) });
}, // },
0x56 => { // 0x56 => {
try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(@as(u64, @bitCast(self.stack.pop().?.i64)) > @as(u64, @bitCast(self.stack.pop().?.i64))))) }); // try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(@as(u64, @bitCast(self.stack.pop().?.i64)) > @as(u64, @bitCast(self.stack.pop().?.i64))))) });
}, // },
0x57 => { // 0x57 => {
try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i64 <= self.stack.pop().?.i64))) }); // try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i64 <= self.stack.pop().?.i64))) });
}, // },
0x58 => { // 0x58 => {
try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(@as(u64, @bitCast(self.stack.pop().?.i64)) <= @as(u64, @bitCast(self.stack.pop().?.i64))))) }); // try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(@as(u64, @bitCast(self.stack.pop().?.i64)) <= @as(u64, @bitCast(self.stack.pop().?.i64))))) });
}, // },
0x59 => { // 0x59 => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i64 >= self.stack.pop().?.i64))) }); // try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i64 >= self.stack.pop().?.i64))) });
}, // },
0x5a => { // 0x5a => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(@as(u64, @bitCast(self.stack.pop().?.i64)) >= @as(u64, @bitCast(self.stack.pop().?.i64))))) }); // try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(@as(u64, @bitCast(self.stack.pop().?.i64)) >= @as(u64, @bitCast(self.stack.pop().?.i64))))) });
}, // },
0x67 => { // 0x67 => {
var i = @as(i32, 0); // var i = @as(i32, 0);
const number = self.stack.pop().?.i32; // const number = self.stack.pop().?.i32;
for (0..@sizeOf(i32)) |b| { // for (0..@sizeOf(i32)) |b| {
if (number & (@as(i32, 0x1) << @intCast((@sizeOf(i32) - b - 1))) == 1) { // if (number & (@as(i32, 0x1) << @intCast((@sizeOf(i32) - b - 1))) == 1) {
break; // break;
} // }
i += 1; // i += 1;
} // }
try self.stack.append(Value{ .i32 = i }); // try self.stack.append(Value{ .i32 = i });
}, // },
0x68 => { // 0x68 => {
var i = @as(i32, 0); // var i = @as(i32, 0);
const number = self.stack.pop().?.i32; // const number = self.stack.pop().?.i32;
for (0..@sizeOf(i32)) |b| { // for (0..@sizeOf(i32)) |b| {
if (number & (@as(i32, 0x1) << @intCast(b)) == 1) { // if (number & (@as(i32, 0x1) << @intCast(b)) == 1) {
break; // break;
} // }
i += 1; // i += 1;
} // }
try self.stack.append(Value{ .i32 = i }); // try self.stack.append(Value{ .i32 = i });
}, // },
0x69 => { // 0x69 => {
var i = @as(i32, 0); // var i = @as(i32, 0);
const number = self.stack.pop().?.i32; // const number = self.stack.pop().?.i32;
for (0..@sizeOf(i32)) |b| { // for (0..@sizeOf(i32)) |b| {
if (number & (@as(i32, 0x1) << @intCast(b)) == 1) { // if (number & (@as(i32, 0x1) << @intCast(b)) == 1) {
i += 1; // i += 1;
} // }
} // }
try self.stack.append(Value{ .i32 = i }); // try self.stack.append(Value{ .i32 = i });
}, // },
0x6a => { 0x6a => {
const a = self.stack.pop().?; const a = self.stack.pop().?;
const b = self.stack.pop().?; const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = a.i32 + b.i32 }); try self.stack.append(.{ .i32 = a.i32 + b.i32 });
}, },
0x6b => { // 0x6b => {
const a = self.stack.pop().?; // const b = self.stack.pop().?;
const b = self.stack.pop().?; // const a = self.stack.pop().?;
try self.stack.append(.{ .i32 = a.i32 - b.i32 }); // try self.stack.append(.{ .i32 = a.i32 - b.i32 });
}, // },
0x6c => { // 0x6c => {
const a = self.stack.pop().?; // const a = self.stack.pop().?;
const b = self.stack.pop().?; // const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = a.i32 * b.i32 }); // try self.stack.append(.{ .i32 = a.i32 * b.i32 });
}, // },
0x6d => { // 0x6d => {
const a = self.stack.pop().?; // const b = self.stack.pop().?;
const b = self.stack.pop().?; // const a = self.stack.pop().?;
try self.stack.append(.{ .i32 = @divTrunc(a.i32, b.i32) }); // try self.stack.append(.{ .i32 = @divTrunc(a.i32, b.i32) });
}, // },
0x6e => { // 0x6e => {
const a = self.stack.pop().?; // const b = self.stack.pop().?;
const b = self.stack.pop().?; // const a = self.stack.pop().?;
try self.stack.append(.{ .i32 = @as(i32, @bitCast(@as(u32, @bitCast(a.i32)) / @as(u32, @bitCast(b.i32)))) }); // try self.stack.append(.{ .i32 = @as(i32, @bitCast(@as(u32, @bitCast(a.i32)) / @as(u32, @bitCast(b.i32)))) });
}, // },
0x6f => { // 0x6f => {
const a = self.stack.pop().?; // const b = self.stack.pop().?;
const b = self.stack.pop().?; // const a = self.stack.pop().?;
try self.stack.append(.{ .i32 = @rem(a.i32, b.i32) }); // try self.stack.append(.{ .i32 = @rem(a.i32, b.i32) });
}, // },
0x70 => { // 0x70 => {
const a = self.stack.pop().?; // const b = self.stack.pop().?;
const b = self.stack.pop().?; // const a = self.stack.pop().?;
try self.stack.append(.{ .i32 = @as(i32, @bitCast(@as(u32, @bitCast(a.i32)) % @as(u32, @bitCast(b.i32)))) }); // try self.stack.append(.{ .i32 = @as(i32, @bitCast(@as(u32, @bitCast(a.i32)) % @as(u32, @bitCast(b.i32)))) });
}, // },
0x71 => { 0x71 => {
const a = self.stack.pop().?; const a = self.stack.pop().?;
const b = self.stack.pop().?; const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = a.i32 & b.i32 }); try self.stack.append(.{ .i32 = a.i32 & b.i32 });
}, },
0x72 => { // 0x72 => {
const a = self.stack.pop().?; // const a = self.stack.pop().?;
const b = self.stack.pop().?; // const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = a.i32 | b.i32 }); // try self.stack.append(.{ .i32 = a.i32 | b.i32 });
}, // },
0x73 => { // 0x73 => {
const a = self.stack.pop().?; // const a = self.stack.pop().?;
const b = self.stack.pop().?; // const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = a.i32 ^ b.i32 }); // try self.stack.append(.{ .i32 = a.i32 ^ b.i32 });
}, // },
0x74 => { // 0x74 => {
const a = self.stack.pop().?; // const a = self.stack.pop().?;
const b = self.stack.pop().?; // const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = a.i32 << @intCast(b.i32) }); // try self.stack.append(.{ .i32 = a.i32 << @intCast(b.i32) });
}, // },
0x75 => { // 0x75 => {
const a = self.stack.pop().?; // const a = self.stack.pop().?;
const b = self.stack.pop().?; // const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = a.i32 >> @intCast(b.i32) }); // try self.stack.append(.{ .i32 = a.i32 >> @intCast(b.i32) });
}, // },
0x76 => { // 0x76 => {
const a = self.stack.pop().?; // const a = self.stack.pop().?;
const b = self.stack.pop().?; // const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = @as(i32, @bitCast(@as(u32, @bitCast(a.i32)) >> @intCast(@as(u32, @bitCast(b.i32))))) }); // try self.stack.append(.{ .i32 = @as(i32, @bitCast(@as(u32, @bitCast(a.i32)) >> @intCast(@as(u32, @bitCast(b.i32))))) });
}, // },
0x77 => { // 0x77 => {
const a = self.stack.pop().?; // const a = self.stack.pop().?;
const b = self.stack.pop().?; // const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = (a.i32 << @intCast(@as(u32, @bitCast(b.i32)))) | (a.i32 >> @intCast((@sizeOf(u32) * 8 - b.i32))) }); // try self.stack.append(.{ .i32 = (a.i32 << @intCast(@as(u32, @bitCast(b.i32)))) | (a.i32 >> @intCast((@sizeOf(u32) * 8 - b.i32))) });
}, // },
0x78 => { // 0x78 => {
const a = self.stack.pop().?; // const a = self.stack.pop().?;
const b = self.stack.pop().?; // const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = (a.i32 >> @intCast(@as(u32, @bitCast(b.i32)))) | (a.i32 << @intCast((@sizeOf(u32) * 8 - b.i32))) }); // try self.stack.append(.{ .i32 = (a.i32 >> @intCast(@as(u32, @bitCast(b.i32)))) | (a.i32 << @intCast((@sizeOf(u32) * 8 - b.i32))) });
}, // },
0x79 => { // 0x79 => {
var i = @as(i64, 0); // var i = @as(i64, 0);
const number = self.stack.pop().?.i64; // const number = self.stack.pop().?.i64;
for (0..@sizeOf(i64)) |b| { // for (0..@sizeOf(i64)) |b| {
if (number & (@as(i64, 0x1) << @intCast((@sizeOf(i64) - b - 1))) == 1) { // if (number & (@as(i64, 0x1) << @intCast((@sizeOf(i64) - b - 1))) == 1) {
break; // break;
} // }
i += 1; // i += 1;
} // }
try self.stack.append(Value{ .i64 = i }); // try self.stack.append(Value{ .i64 = i });
}, // },
0x7a => { // 0x7a => {
var i = @as(i64, 0); // var i = @as(i64, 0);
const number = self.stack.pop().?.i64; // const number = self.stack.pop().?.i64;
for (0..@sizeOf(i64)) |b| { // for (0..@sizeOf(i64)) |b| {
if (number & (@as(i64, 0x1) << @intCast(b)) == 1) { // if (number & (@as(i64, 0x1) << @intCast(b)) == 1) {
break; // break;
} // }
i += 1; // i += 1;
} // }
try self.stack.append(Value{ .i64 = i }); // try self.stack.append(Value{ .i64 = i });
}, // },
0x7b => { // 0x7b => {
var i = @as(i64, 0); // var i = @as(i64, 0);
const number = self.stack.pop().?.i64; // const number = self.stack.pop().?.i64;
for (0..@sizeOf(i64)) |b| { // for (0..@sizeOf(i64)) |b| {
if (number & (@as(i64, 0x1) << @intCast(b)) == 1) { // if (number & (@as(i64, 0x1) << @intCast(b)) == 1) {
i += 1; // i += 1;
} // }
} // }
try self.stack.append(Value{ .i64 = i }); // try self.stack.append(Value{ .i64 = i });
}, // },
0x7c => { 0x7c => {
const a = self.stack.pop().?; const a = self.stack.pop().?;
const b = self.stack.pop().?; const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = a.i64 + b.i64 }); try self.stack.append(.{ .i64 = a.i64 + b.i64 });
}, },
0x7d => { // 0x7d => {
const a = self.stack.pop().?; // const b = self.stack.pop().?;
const b = self.stack.pop().?; // const a = self.stack.pop().?;
try self.stack.append(.{ .i64 = a.i64 - b.i64 }); // try self.stack.append(.{ .i64 = a.i64 - b.i64 });
}, // },
0x7e => { // 0x7e => {
const a = self.stack.pop().?; // const a = self.stack.pop().?;
const b = self.stack.pop().?; // const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = a.i64 * b.i64 }); // try self.stack.append(.{ .i64 = a.i64 * b.i64 });
}, // },
0x7f => { // 0x7f => {
const a = self.stack.pop().?; // const b = self.stack.pop().?;
const b = self.stack.pop().?; // const a = self.stack.pop().?;
try self.stack.append(.{ .i64 = @divTrunc(a.i64, b.i64) }); // try self.stack.append(.{ .i64 = @divTrunc(a.i64, b.i64) });
}, // },
0x80 => { // 0x80 => {
const a = self.stack.pop().?; // const b = self.stack.pop().?;
const b = self.stack.pop().?; // const a = self.stack.pop().?;
try self.stack.append(.{ .i64 = @as(i64, @bitCast(@as(u64, @bitCast(a.i64)) / @as(u64, @bitCast(b.i64)))) }); // try self.stack.append(.{ .i64 = @as(i64, @bitCast(@as(u64, @bitCast(a.i64)) / @as(u64, @bitCast(b.i64)))) });
}, // },
0x81 => { // 0x81 => {
const a = self.stack.pop().?; // const b = self.stack.pop().?;
const b = self.stack.pop().?; // const a = self.stack.pop().?;
try self.stack.append(.{ .i64 = @rem(a.i64, b.i64) }); // try self.stack.append(.{ .i64 = @rem(a.i64, b.i64) });
}, // },
0x82 => { // 0x82 => {
const a = self.stack.pop().?; // const b = self.stack.pop().?;
const b = self.stack.pop().?; // const a = self.stack.pop().?;
try self.stack.append(.{ .i64 = @as(i64, @bitCast(@as(u64, @bitCast(a.i64)) % @as(u64, @bitCast(b.i64)))) }); // try self.stack.append(.{ .i64 = @as(i64, @bitCast(@as(u64, @bitCast(a.i64)) % @as(u64, @bitCast(b.i64)))) });
}, // },
0x83 => { // 0x83 => {
const a = self.stack.pop().?; // const a = self.stack.pop().?;
const b = self.stack.pop().?; // const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = a.i64 & b.i64 }); // try self.stack.append(.{ .i64 = a.i64 & b.i64 });
}, // },
0x84 => { // 0x84 => {
const a = self.stack.pop().?; // const a = self.stack.pop().?;
const b = self.stack.pop().?; // const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = a.i64 | b.i64 }); // try self.stack.append(.{ .i64 = a.i64 | b.i64 });
}, // },
0x85 => { // 0x85 => {
const a = self.stack.pop().?; // const a = self.stack.pop().?;
const b = self.stack.pop().?; // const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = a.i64 ^ b.i64 }); // try self.stack.append(.{ .i64 = a.i64 ^ b.i64 });
}, // },
0x86 => { // 0x86 => {
const a = self.stack.pop().?; // const a = self.stack.pop().?;
const b = self.stack.pop().?; // const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = a.i64 << @intCast(b.i64) }); // try self.stack.append(.{ .i64 = a.i64 << @intCast(b.i64) });
}, // },
0x87 => { // 0x87 => {
const a = self.stack.pop().?; // const a = self.stack.pop().?;
const b = self.stack.pop().?; // const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = a.i64 >> @intCast(b.i64) }); // try self.stack.append(.{ .i64 = a.i64 >> @intCast(b.i64) });
}, // },
0x88 => { // 0x88 => {
const a = self.stack.pop().?; // const a = self.stack.pop().?;
const b = self.stack.pop().?; // const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = @as(i64, @bitCast(@as(u64, @bitCast(a.i64)) >> @intCast(@as(u64, @bitCast(b.i64))))) }); // try self.stack.append(.{ .i64 = @as(i64, @bitCast(@as(u64, @bitCast(a.i64)) >> @intCast(@as(u64, @bitCast(b.i64))))) });
}, // },
0x89 => { // 0x89 => {
const a = self.stack.pop().?; // const a = self.stack.pop().?;
const b = self.stack.pop().?; // const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = (a.i64 << @intCast(@as(u64, @bitCast(b.i64)))) | (a.i64 >> @intCast((@sizeOf(u64) * 8 - b.i64))) }); // try self.stack.append(.{ .i64 = (a.i64 << @intCast(@as(u64, @bitCast(b.i64)))) | (a.i64 >> @intCast((@sizeOf(u64) * 8 - b.i64))) });
}, // },
0x8a => { // 0x8a => {
const a = self.stack.pop().?; // const a = self.stack.pop().?;
const b = self.stack.pop().?; // const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = (a.i64 >> @intCast(@as(u64, @bitCast(b.i64)))) | (a.i64 << @intCast((@sizeOf(u64) * 8 - b.i64))) }); // try self.stack.append(.{ .i64 = (a.i64 >> @intCast(@as(u64, @bitCast(b.i64)))) | (a.i64 << @intCast((@sizeOf(u64) * 8 - b.i64))) });
// },
0xad => {
try self.stack.append(.{ .i64 = self.stack.pop().?.i32 });
}, },
0x0f => {
break :loop;
},
0x10 => { 0x10 => {
const integer = leb128Decode(u32, frame.code[frame.program_counter..]); const integer = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += integer.len; frame.program_counter += integer.len;
@ -570,13 +598,15 @@ pub const Runtime = struct {
self.call(allocator, integer.val, &[_]usize{}) catch {}; self.call(allocator, integer.val, &[_]usize{}) catch {};
}, },
0xb => { 0xb => {
_ = self.labels.pop();
if (for_loop) { if (for_loop) {
frame.program_counter += 1; for_loop = false;
} else {
break :loop;
} }
}, },
else => std.debug.print("instruction {} not implemented\n", .{byte}), else => std.log.err("instruction {} not implemented\n", .{byte}),
}
if (frame.program_counter >= frame.code.len) {
break :loop;
} }
} }
} }
@ -605,9 +635,11 @@ pub const Runtime = struct {
switch (function_type.parameters[i]) { switch (function_type.parameters[i]) {
.val => |v| switch (v) { .val => |v| switch (v) {
.i32 => { .i32 => {
std.debug.print("Local with type i32\n", .{});
frame.locals[i] = .{ .i32 = @intCast(p) }; frame.locals[i] = .{ .i32 = @intCast(p) };
}, },
.i64 => { .i64 => {
std.debug.print("Local with type i64\n", .{});
frame.locals[i] = .{ .i64 = @intCast(p) }; frame.locals[i] = .{ .i64 = @intCast(p) };
}, },
else => unreachable, else => unreachable,
@ -620,9 +652,11 @@ pub const Runtime = struct {
switch (local) { switch (local) {
.val => |v| switch (v) { .val => |v| switch (v) {
.i32 => { .i32 => {
std.debug.print("Local with type i32\n", .{});
frame.locals[i] = .{ .i32 = 0 }; frame.locals[i] = .{ .i32 = 0 };
}, },
.i64 => { .i64 => {
std.debug.print("Local with type i64\n", .{});
frame.locals[i] = .{ .i64 = 0 }; frame.locals[i] = .{ .i64 = 0 };
}, },
else => unreachable, else => unreachable,