Changed structs in the VM

This commit is contained in:
Ernesto Lanchares 2025-03-29 19:16:17 +01:00
parent fd7973173f
commit 7023261320
4 changed files with 158 additions and 138 deletions

View file

@ -22,18 +22,18 @@ pub fn main() !void {
const file = try std.fs.cwd().openFile("assets/core.wasm", .{}); const file = try std.fs.cwd().openFile("assets/core.wasm", .{});
const all = try file.readToEndAlloc(allocator, 1_000_000); // 1 MB const all = try file.readToEndAlloc(allocator, 1_000_000); // 1 MB
var parser = mods.Parser{ var parser = mods.Parser{
.bytes = all, .bytes = all,
.byte_idx = 0, .byte_idx = 0,
.allocator = allocator, .allocator = allocator,
}; };
const module = parser.parseModule() catch |err| { const module = parser.parseModule() catch |err| {
std.debug.print("[ERROR]: error at byte {x}(0x{x})\n", .{ parser.byte_idx, parser.bytes[parser.byte_idx] }); std.debug.print("[ERROR]: error at byte {x}(0x{x})\n", .{ parser.byte_idx, parser.bytes[parser.byte_idx] });
return err; return err;
}; };
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{17}; var parameters = [_]mods.VM.Value{.{ .i32 = 17 }};
try runtime.callExternal(allocator, "preinit", &parameters); try runtime.callExternal(allocator, "preinit", &parameters);
const result = runtime.stack.pop().?; const result = runtime.stack.pop().?;
std.debug.print("Result of preinit: {any}\n", .{result}); std.debug.print("Result of preinit: {any}\n", .{result});

View file

@ -7,34 +7,10 @@ bytes: []const u8,
byte_idx: usize, byte_idx: usize,
allocator: Allocator, allocator: Allocator,
// TODO: We don't really need ArrayLists types: ?[]vm.Functype = null,
types: std.ArrayListUnmanaged(Functype) = .{}, functions: ?[]vm.Function = null,
imports: std.ArrayListUnmanaged(Import) = .{},
exports: std.StringHashMapUnmanaged(u32) = .{},
functions: std.ArrayListUnmanaged(u32) = .{},
memory: ?Memtype = null, memory: ?Memtype = null,
code: std.ArrayListUnmanaged(Func) = .{}, exports: std.StringHashMapUnmanaged(u32) = .{},
funcs: std.ArrayListUnmanaged(vm.Func) = .{},
pub const FunctionType = struct {
parameters: []u8,
results: []u8,
pub fn deinit(self: FunctionType, allocator: Allocator) void {
allocator.free(self.parameters);
allocator.free(self.results);
}
};
pub const FunctionBody = struct {
locals: []Local,
code: []u8,
};
pub const FunctionScope = enum {
external,
internal,
};
const Parser = @This(); const Parser = @This();
@ -56,6 +32,7 @@ pub const Error = error{
invalid_importdesc, invalid_importdesc,
invalid_exportdesc, invalid_exportdesc,
double_else, double_else,
duplicated_funcsec,
unresolved_branch, unresolved_branch,
unterminated_wasm, unterminated_wasm,
}; };
@ -172,11 +149,7 @@ pub fn parseReftype(self: *Parser) !std.wasm.RefType {
// NOTE: Parsing of Valtype can be improved but it makes it less close to spec so... // NOTE: Parsing of Valtype can be improved but it makes it less close to spec so...
// TODO: Do we really need Valtype? // TODO: Do we really need Valtype?
pub const Valtype = union(enum) { fn parseValtype(self: *Parser) !vm.Valtype {
val: std.wasm.Valtype,
ref: std.wasm.RefType,
};
fn parseValtype(self: *Parser) !Valtype {
const pb = self.peek() orelse return Error.unterminated_wasm; const pb = self.peek() orelse return Error.unterminated_wasm;
return switch (pb) { return switch (pb) {
0x7F, 0x7E, 0x7D, 0x7C => .{ .val = try self.parseNumtype() }, 0x7F, 0x7E, 0x7D, 0x7C => .{ .val = try self.parseNumtype() },
@ -186,24 +159,15 @@ fn parseValtype(self: *Parser) !Valtype {
}; };
} }
fn parseResultType(self: *Parser) ![]Valtype { fn parseResultType(self: *Parser) ![]vm.Valtype {
return try self.parseVector(Parser.parseValtype); return try self.parseVector(Parser.parseValtype);
} }
pub const Functype = struct { fn parseFunctype(self: *Parser) !vm.Functype {
parameters: []Valtype,
rt2: []Valtype,
pub fn deinit(self: Functype, allocator: Allocator) void {
allocator.free(self.parameters);
allocator.free(self.rt2);
}
};
fn parseFunctype(self: *Parser) !Functype {
if (try self.readByte() != 0x60) return Error.invalid_functype; if (try self.readByte() != 0x60) return Error.invalid_functype;
return .{ return .{
.parameters = try self.parseResultType(), .parameters = try self.parseResultType(),
.rt2 = try self.parseResultType(), .returns = try self.parseResultType(),
}; };
} }
@ -245,7 +209,7 @@ fn parseTabletype(self: *Parser) !Tabletype {
} }
const Globaltype = struct { const Globaltype = struct {
t: Valtype, t: vm.Valtype,
m: enum { m: enum {
@"const", @"const",
@"var", @"var",
@ -296,11 +260,7 @@ pub fn parseModule(self: *Parser) !vm.Module {
.max = self.memory.?.lim.max, .max = self.memory.?.lim.max,
}, },
.exports = self.exports, .exports = self.exports,
.funcs = try self.funcs.toOwnedSlice(self.allocator), .functions = self.functions.?,
.types = try self.types.toOwnedSlice(self.allocator),
.functions = try self.functions.toOwnedSlice(self.allocator),
.imports = try self.imports.toOwnedSlice(self.allocator),
.code = try self.code.toOwnedSlice(self.allocator),
}; };
} }
@ -315,10 +275,9 @@ fn parseTypesec(self: *Parser) !void {
const end_idx = self.byte_idx + size; const end_idx = self.byte_idx + size;
const ft = try self.parseVector(Parser.parseFunctype); const ft = try self.parseVector(Parser.parseFunctype);
// TODO: Maybe the interface should be better? self.types = ft;
try self.types.appendSlice(self.allocator, ft);
// TODO: run this check not only on debug // TODO(ernesto): run this check not only on debug
std.debug.assert(self.byte_idx == end_idx); std.debug.assert(self.byte_idx == end_idx);
} }
@ -349,8 +308,9 @@ fn parseImportsec(self: *Parser) !void {
const size = try self.readU32(); const size = try self.readU32();
const end_idx = self.byte_idx + size; const end_idx = self.byte_idx + size;
// TODO(ernesto): this should be used to do name resolution.
const imports = try self.parseVector(Parser.parseImport); const imports = try self.parseVector(Parser.parseImport);
try self.imports.appendSlice(self.allocator, imports); _ = imports;
// TODO: run this check not only on debug // TODO: run this check not only on debug
std.debug.assert(self.byte_idx == end_idx); std.debug.assert(self.byte_idx == end_idx);
@ -361,7 +321,16 @@ fn parseFuncsec(self: *Parser) !void {
const end_idx = self.byte_idx + size; const end_idx = self.byte_idx + size;
const types = try self.parseVector(Parser.readU32); const types = try self.parseVector(Parser.readU32);
try self.functions.appendSlice(self.allocator, types);
if (self.functions != null) return Error.duplicated_funcsec;
self.functions = try self.allocator.alloc(vm.Function, types.len);
for (types, 0..) |t, i| {
self.functions.?[i].func_type = self.types.?[t];
}
// TODO(ernesto): run this check not only in debug
std.debug.assert(types.len == self.functions.?.len);
// TODO: run this check not only on debug // TODO: run this check not only on debug
std.debug.assert(self.byte_idx == end_idx); std.debug.assert(self.byte_idx == end_idx);
@ -446,13 +415,12 @@ fn parseElemsec(self: *Parser) !void {
} }
pub const Func = struct { pub const Func = struct {
locals: []Valtype, locals: []vm.Valtype,
code: []const u8,
ir: IR, ir: IR,
}; };
const Local = struct { const Local = struct {
n: u32, n: u32,
t: Valtype, t: vm.Valtype,
}; };
fn parseLocal(self: *Parser) !Local { fn parseLocal(self: *Parser) !Local {
return .{ return .{
@ -476,8 +444,7 @@ fn parseCode(self: *Parser) !Func {
try ir.print(stdout); try ir.print(stdout);
const func = Func{ const func = Func{
.locals = try self.allocator.alloc(Valtype, local_count), .locals = try self.allocator.alloc(vm.Valtype, local_count),
.code = &.{},
.ir = ir, .ir = ir,
}; };
@ -498,10 +465,15 @@ fn parseCodesec(self: *Parser) !void {
const end_idx = self.byte_idx + size; const end_idx = self.byte_idx + size;
const codes = try self.parseVector(Parser.parseCode); const codes = try self.parseVector(Parser.parseCode);
for (codes, 0..) |_, i| { // TODO: run this check not only on debug
try self.funcs.append(self.allocator, .{ .internal = @intCast(i) }); std.debug.assert(codes.len == self.functions.?.len);
for (codes, self.functions.?) |code, *f| {
f.typ = .{ .internal = .{
.locals = code.locals,
.ir = code.ir,
} };
} }
try self.code.appendSlice(self.allocator, codes);
// TODO: run this check not only on debug // TODO: run this check not only on debug
std.debug.assert(self.byte_idx == end_idx); std.debug.assert(self.byte_idx == end_idx);

View file

@ -1,5 +1,6 @@
const std = @import("std"); const std = @import("std");
const Parser = @import("Parser.zig"); const Parser = @import("Parser.zig");
const vm = @import("vm.zig");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
@ -44,7 +45,7 @@ opcodes: []Opcode,
indices: []Index, indices: []Index,
// TODO: this could be a byte array and v128.const and i8x16.shuffle could live here too // TODO: this could be a byte array and v128.const and i8x16.shuffle could live here too
select_valtypes: []Parser.Valtype, select_valtypes: []vm.Valtype,
pub fn print(self: IR, writer: anytype) !void { pub fn print(self: IR, writer: anytype) !void {
for (self.opcodes, 0..) |op, i| { for (self.opcodes, 0..) |op, i| {
@ -621,8 +622,8 @@ const IRParserState = struct {
fn parseExpression(self: *IRParserState) Parser.Error!void { fn parseExpression(self: *IRParserState) Parser.Error!void {
const b = try self.parser.readByte(); const b = try self.parser.readByte();
try switch (b) { try switch (b) {
0x00 => {}, // TODO 0x00 => self.push(@enumFromInt(b), .{ .u64 = 0 }),
0x01 => {}, 0x01 => self.push(@enumFromInt(b), .{ .u64 = 0 }),
0x02...0x03 => self.parseBlock(b), 0x02...0x03 => self.parseBlock(b),
0x04 => self.parseIf(), 0x04 => self.parseIf(),
0x0C...0x0D => self.parseBranch(b), 0x0C...0x0D => self.parseBranch(b),

View file

@ -9,29 +9,50 @@ pub const Memory = struct {
min: u32, min: u32,
max: ?u32, max: ?u32,
}; };
// TODO: Resolve function calls at parse time
// TODO: Resolve function types at compile time pub const Valtype = union(enum) {
pub const Func = union(enum) { val: std.wasm.Valtype,
internal: u32, ref: std.wasm.RefType,
external: u32,
}; };
pub const Functype = struct {
parameters: []Valtype,
returns: []Valtype,
pub fn deinit(self: Functype, allocator: Allocator) void {
allocator.free(self.parameters);
allocator.free(self.returns);
}
};
pub const Function = struct { func_type: Functype, typ: union(enum) {
internal: struct {
locals: []Valtype,
ir: IR,
},
external: void,
} };
pub const Module = struct { pub const Module = struct {
memory: Memory, memory: Memory,
funcs: []Func, functions: []Function,
exports: std.StringHashMapUnmanaged(u32), exports: std.StringHashMapUnmanaged(u32),
imports: []Parser.Import,
types: []Parser.Functype,
functions: []u32,
code: []Parser.Func,
fn deinit(self: *Module, allocator: Allocator) void { fn deinit(self: *Module, allocator: Allocator) void {
self.exports.deinit(allocator); self.exports.deinit(allocator);
allocator.free(self.funcs); for (self.functions) |f| {
allocator.free(self.imports); allocator.free(f.func_type.parameters);
allocator.free(self.types); allocator.free(f.func_type.returns);
switch (f.typ) {
.internal => {
allocator.free(f.typ.internal.ir.opcodes);
allocator.free(f.typ.internal.ir.indices);
allocator.free(f.typ.internal.ir.select_valtypes);
allocator.free(f.typ.internal.locals);
},
.external => {},
}
}
allocator.free(self.functions); allocator.free(self.functions);
allocator.free(self.code);
} }
}; };
@ -61,7 +82,7 @@ pub const Runtime = struct {
// if memory max is not set the memory is allowed to grow but it is not supported at the moment // if memory max is not set the memory is allowed to grow but it is not supported at the moment
const max = module.memory.max orelse 1_000; const max = module.memory.max orelse 1_000;
if (module.memory.max == null) { if (module.memory.max == null) {
std.debug.print("[WARN]: growing memory is not yet supported, usign a default value of 1Kb\n", .{}); std.log.warn("Growing memory is not yet supported, usign a default value of 1Kb\n", .{});
} }
const memory = try allocator.alloc(u8, max); const memory = try allocator.alloc(u8, max);
return Runtime{ return Runtime{
@ -78,32 +99,78 @@ pub const Runtime = struct {
allocator.free(self.memory); allocator.free(self.memory);
} }
pub fn executeFrame(self: *Runtime, _: Allocator, frame: *CallFrame) !void { pub fn executeFrame(self: *Runtime, allocator: Allocator, frame: *CallFrame) !void {
loop: while (true) { loop: while (frame.program_counter < frame.code.opcodes.len) {
const opcode: IR.Opcode = frame.code.opcodes[frame.program_counter]; const opcode: IR.Opcode = frame.code.opcodes[frame.program_counter];
const index = frame.code.indices[frame.program_counter];
switch (opcode) { switch (opcode) {
// TODO(ernesto): How should we handle unreachable?
.@"unreachable" => {},
.nop => {},
.br => { .br => {
// TODO(luccie-cmd): Branching like this is dangerous, we should do safety things or smth. frame.program_counter = index.u32;
frame.program_counter = frame.code.indices[frame.program_counter].u32 - 1; continue;
}, },
.br_if => { .br_if => {
if (self.stack.pop().?.i32 != 0) { if (self.stack.pop().?.i32 != 0) {
// TODO(luccie-cmd): Branching like this is dangerous, we should do safety things or smth. frame.program_counter = index.u32;
frame.program_counter = frame.code.indices[frame.program_counter].u32 - 1; continue;
} }
}, },
.localget => { .br_table => @panic("UNIMPLEMENTED"),
try self.stack.append(frame.locals[frame.code.indices[frame.program_counter].u32]); .@"return" => break :loop,
.call => {
// TODO: figure out how many parameters to push
try self.call(allocator, index.u32, &[_]Value{});
}, },
.localset => { .call_indirect => @panic("UNIMPLEMENTED"),
const a = self.stack.pop().?;
frame.locals[frame.code.indices[frame.program_counter].u32] = a; .refnull => @panic("UNIMPLEMENTED"),
}, .refisnull => @panic("UNIMPLEMENTED"),
.localtee => { .reffunc => @panic("UNIMPLEMENTED"),
const a = self.stack.pop().?;
try self.stack.append(a); .drop => @panic("UNIMPLEMENTED"),
frame.locals[frame.code.indices[frame.program_counter].u32] = a; .select => @panic("UNIMPLEMENTED"),
.select_with_values => @panic("UNIMPLEMENTED"),
.localget => try self.stack.append(frame.locals[index.u32]),
.localset => frame.locals[index.u32] = self.stack.pop().?,
.localtee => frame.locals[index.u32] = self.stack.items[self.stack.items.len - 1],
.globalget => @panic("UNIMPLEMENTED"),
.globalset => @panic("UNIMPLEMENTED"),
.tableget => @panic("UNIMPLEMENTED"),
.tableset => @panic("UNIMPLEMENTED"),
.tableinit => @panic("UNIMPLEMENTED"),
.elemdrop => @panic("UNIMPLEMENTED"),
.tablecopy => @panic("UNIMPLEMENTED"),
.tablegrow => @panic("UNIMPLEMENTED"),
.tablesize => @panic("UNIMPLEMENTED"),
.tablefill => @panic("UNIMPLEMENTED"),
.i32_load => {
const start = index.memarg.alignment + index.memarg.offset;
const end = start + @sizeOf(i32);
try self.stack.append(.{ .i32 = std.mem.littleToNative(i32, std.mem.bytesAsValue(i32, self.memory[start..end]).*) });
}, },
// 0x28 => {
// const address = leb128Decode(u32, frame.code[frame.program_counter..]);
// frame.program_counter += address.len;
// const offset = leb128Decode(u32, frame.code[frame.program_counter..]);
// frame.program_counter += offset.len;
// const start = (address.val + offset.val);
// const end = start + @sizeOf(u32);
// try self.stack.append(Value{ .i32 = decodeLittleEndian(i32, self.memory[start..end]) });
// },
// 0x29 => {
// const address = leb128Decode(u32, frame.code[frame.program_counter..]);
// frame.program_counter += address.len;
// const offset = leb128Decode(u32, frame.code[frame.program_counter..]);
// frame.program_counter += offset.len;
// const start = (address.val + offset.val);
// const end = start + @sizeOf(u64);
// try self.stack.append(Value{ .i64 = decodeLittleEndian(i64, self.memory[start..end]) });
.i32_const => { .i32_const => {
try self.stack.append(Value{ .i32 = frame.code.indices[frame.program_counter].i32 }); try self.stack.append(Value{ .i32 = frame.code.indices[frame.program_counter].i32 });
}, },
@ -141,9 +208,6 @@ pub const Runtime = struct {
.i64_extend_i32_u => { .i64_extend_i32_u => {
try self.stack.append(.{ .i64 = self.stack.pop().?.i32 }); try self.stack.append(.{ .i64 = self.stack.pop().?.i32 });
}, },
.@"return" => {
break :loop;
},
else => { else => {
std.log.err("instruction {any} not implemented\n", .{opcode}); std.log.err("instruction {any} not implemented\n", .{opcode});
std.process.exit(1); std.process.exit(1);
@ -592,14 +656,11 @@ pub const Runtime = struct {
// else => std.log.err("instruction {} not implemented\n", .{byte}), // else => std.log.err("instruction {} not implemented\n", .{byte}),
// } // }
frame.program_counter += 1; frame.program_counter += 1;
if (frame.program_counter >= frame.code.opcodes.len) {
break :loop;
}
} }
} }
// TODO: Do name resolution // TODO: Do name resolution at parseTime
pub fn callExternal(self: *Runtime, allocator: Allocator, name: []const u8, parameters: []usize) !void { pub fn callExternal(self: *Runtime, allocator: Allocator, name: []const u8, parameters: []Value) !void {
if (self.module.exports.get(name)) |function| { if (self.module.exports.get(name)) |function| {
try self.call(allocator, function, parameters); try self.call(allocator, function, parameters);
} else { } else {
@ -607,36 +668,21 @@ pub const Runtime = struct {
} }
} }
pub fn call(self: *Runtime, allocator: Allocator, function: usize, parameters: []usize) AllocationError!void { pub fn call(self: *Runtime, allocator: Allocator, function: usize, parameters: []Value) AllocationError!void {
const f = self.module.funcs[function]; const f = self.module.functions[function];
switch (f) { switch (f.typ) {
.internal => { .internal => {
const ir: IR = self.module.code[f.internal].ir; const ir: IR = f.typ.internal.ir;
const function_type = self.module.types[self.module.functions[f.internal]]; const function_type = f.func_type;
var frame = CallFrame{ var frame = CallFrame{
.code = ir, .code = ir,
.program_counter = 0x0, .program_counter = 0x0,
.locals = try allocator.alloc(Value, self.module.code[f.internal].locals.len + function_type.parameters.len), .locals = try allocator.alloc(Value, f.typ.internal.locals.len + function_type.parameters.len),
}; };
for (parameters, 0..) |p, i| { @memcpy(frame.locals[0..parameters.len], parameters);
switch (function_type.parameters[i]) {
.val => |v| switch (v) {
.i32 => {
std.debug.print("Local with type i32\n", .{});
frame.locals[i] = .{ .i32 = @intCast(p) };
},
.i64 => {
std.debug.print("Local with type i64\n", .{});
frame.locals[i] = .{ .i64 = @intCast(p) };
},
else => unreachable,
},
.ref => unreachable,
}
}
for (self.module.code[f.internal].locals, function_type.parameters.len..) |local, i| { for (f.typ.internal.locals, function_type.parameters.len..) |local, i| {
switch (local) { switch (local) {
.val => |v| switch (v) { .val => |v| switch (v) {
.i32 => { .i32 => {
@ -658,10 +704,11 @@ pub const Runtime = struct {
allocator.free(frame.locals); allocator.free(frame.locals);
}, },
.external => { .external => {
const name = self.module.imports[f.external].name; // TODO(ernesto): handle external functions
if (self.global_runtime.functions.get(name)) |external| { // const name = self.module.imports[f.external].name;
external(&self.stack); // if (self.global_runtime.functions.get(name)) |external| {
} // external(&self.stack);
// }
}, },
} }
} }