summaryrefslogtreecommitdiff
path: root/src/mods/Parser.zig
diff options
context:
space:
mode:
authorErnesto Lanchares <elancha98@proton.me>2025-03-24 21:18:40 +0000
committerLorenzo Torres <torres@sideros.org>2025-03-24 22:38:12 +0100
commit09691ec4d93cda6ab31d28d6e478257209fe625e (patch)
treeb807d47ad7cf434a429b38804fadcbcc3b0a85c7 /src/mods/Parser.zig
parent7cf43ccb8b8b1726c2697188b9138847780cd08e (diff)
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 :)
Diffstat (limited to 'src/mods/Parser.zig')
-rw-r--r--src/mods/Parser.zig24
1 files changed, 23 insertions, 1 deletions
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),