#broken haku2: progress on making it work
This commit is contained in:
parent
7658e0d4e8
commit
c5e2892def
7 changed files with 187 additions and 130 deletions
|
@ -1,7 +1,7 @@
|
|||
const std = @import("std");
|
||||
const mem = std.mem;
|
||||
const debug = std.debug;
|
||||
const log = std.log;
|
||||
const log = std.log.scoped(.vm);
|
||||
const testAllocator = std.testing.allocator;
|
||||
|
||||
const bytecode = @import("bytecode.zig");
|
||||
|
@ -107,6 +107,7 @@ pub fn push(vm: *Vm, val: Value) Error!void {
|
|||
if (vm.stack_top >= vm.stack.len) {
|
||||
return vm.throw("too many live temporary values (local variables and expression operands)", .{});
|
||||
}
|
||||
log.debug("PUSH {any} <- {}", .{ vm.stack[0..vm.stack_top], val });
|
||||
vm.stack[vm.stack_top] = val;
|
||||
vm.stack_top += 1;
|
||||
}
|
||||
|
@ -114,6 +115,8 @@ pub fn push(vm: *Vm, val: Value) Error!void {
|
|||
pub fn pop(vm: *Vm) Error!Value {
|
||||
try vm.validateBytecode(vm.stack_top > 0, "value stack underflow", .{});
|
||||
vm.stack_top -= 1;
|
||||
const result = vm.stack[vm.stack_top];
|
||||
log.debug("POP {any} -> {}", .{ vm.stack[0..vm.stack_top], result });
|
||||
return vm.stack[vm.stack_top];
|
||||
}
|
||||
|
||||
|
@ -129,12 +132,14 @@ pub fn pushCall(vm: *Vm, frame: CallFrame) Error!void {
|
|||
if (vm.call_stack_top >= vm.call_stack.len) {
|
||||
return vm.throw("too much recursion", .{});
|
||||
}
|
||||
log.debug("PUSH CALL {}", .{frame});
|
||||
vm.call_stack[vm.call_stack_top] = frame;
|
||||
vm.call_stack_top += 1;
|
||||
}
|
||||
|
||||
pub fn popCall(vm: *Vm) Error!CallFrame {
|
||||
try vm.validateBytecode(vm.call_stack_top > 0, "call stack underflow", .{});
|
||||
log.debug("POP CALL", .{});
|
||||
vm.call_stack_top -= 1;
|
||||
return vm.call_stack[vm.call_stack_top];
|
||||
}
|
||||
|
@ -172,7 +177,9 @@ inline fn read(comptime T: type, ip: *[*]const u8) T {
|
|||
}
|
||||
|
||||
inline fn readOpcode(ip: *[*]const u8) bytecode.Opcode {
|
||||
return @enumFromInt(read(u8, ip));
|
||||
const opcode: bytecode.Opcode = @enumFromInt(read(u8, ip));
|
||||
log.debug("OP {*} {}", .{ ip.*, opcode });
|
||||
return opcode;
|
||||
}
|
||||
|
||||
/// Before calling this, vm.stack_top should be saved and the appropriate amount of parameters must
|
||||
|
@ -186,6 +193,8 @@ pub fn run(
|
|||
init_closure: *const value.Closure,
|
||||
init_bottom: u32,
|
||||
) Error!void {
|
||||
log.debug("BEGIN RUN {}", .{init_closure});
|
||||
|
||||
var closure = init_closure;
|
||||
var ip: [*]const u8 = closure.chunk.bytecode[closure.start..].ptr;
|
||||
var bottom = init_bottom;
|
||||
|
@ -432,11 +441,13 @@ pub fn run(
|
|||
const arg_count = read(u8, &ip);
|
||||
const system_fn = system.fns[index];
|
||||
|
||||
log.debug("system index={} arg_count={} system_fn={p}", .{ index, arg_count, system_fn });
|
||||
|
||||
vm.storeContext(.{ .fuel = fuel });
|
||||
const result = try system_fn(.{
|
||||
.vm = vm,
|
||||
.allocator = allocator,
|
||||
.args = vm.stack[vm.stack_top - arg_count ..],
|
||||
.args = vm.stack[vm.stack_top - arg_count .. vm.stack_top],
|
||||
});
|
||||
const context = vm.restoreContext();
|
||||
fuel = context.fuel;
|
||||
|
@ -487,6 +498,8 @@ pub fn run(
|
|||
return vm.throw("corrupted bytecode: invalid opcode {}", .{invalid_opcode});
|
||||
},
|
||||
}
|
||||
|
||||
log.debug("END RUN", .{});
|
||||
}
|
||||
|
||||
pub const Dotter = struct {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue