From 09691ec4d93cda6ab31d28d6e478257209fe625e Mon Sep 17 00:00:00 2001 From: Ernesto Lanchares Date: Mon, 24 Mar 2025 21:18:40 +0000 Subject: Some progress on IR parsing. Alhtough IR parsing is technically called while parsing, since we lack the hability to parse blocks or labels or if or any hard stuff really, it does not affect code parsing. However it is nice to have it there as zig compiles it :) --- src/mods/Parser.zig | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/mods/Parser.zig') diff --git a/src/mods/Parser.zig b/src/mods/Parser.zig index d9f7ccf..2e3c434 100644 --- a/src/mods/Parser.zig +++ b/src/mods/Parser.zig @@ -1,5 +1,6 @@ const std = @import("std"); const vm = @import("vm.zig"); +const IR = @import("ir.zig"); const Allocator = std.mem.Allocator; bytes: []const u8, @@ -38,6 +39,7 @@ pub const FunctionScope = enum { const Parser = @This(); pub const Error = error{ + invalid_instruction, invalid_magic, invalid_version, invalid_section, @@ -78,10 +80,28 @@ pub fn readByte(self: *Parser) !u8 { return (try self.read(1))[0]; } -fn readU32(self: *Parser) !u32 { +pub fn readU32(self: *Parser) !u32 { return std.leb.readUleb128(u32, self); } +pub fn readI32(self: *Parser) !i32 { + return std.leb.readIleb128(i32, self); +} + +pub fn readI64(self: *Parser) !i64 { + return std.leb.readIleb128(i64, self); +} + +pub fn readF32(self: *Parser) !f32 { + const bytes = try self.read(@sizeOf(f32)); + return std.mem.bytesAsValue(f32, bytes).*; +} + +pub fn readF64(self: *Parser) !f64 { + const bytes = try self.read(@sizeOf(f64)); + return std.mem.bytesAsValue(f64, bytes).*; +} + fn readName(self: *Parser) ![]const u8 { // NOTE: This should be the only vector not parsed through parseVector const size = try self.readU32(); @@ -442,6 +462,8 @@ fn parseCode(self: *Parser) !Func { local_count += l.n; } + _ = try IR.parse(self); + const func = Func{ .locals = try self.allocator.alloc(Valtype, local_count), .code = try self.read(end_idx - self.byte_idx), -- cgit v1.2.3