Add full support for globals
This commit is contained in:
parent
53eb24dc35
commit
6f942fe9c2
3 changed files with 21 additions and 17 deletions
|
|
@ -255,12 +255,14 @@ fn parseTabletype(self: *Parser) !Tabletype {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const GlobalMutability = enum {
|
||||||
|
@"const",
|
||||||
|
@"var",
|
||||||
|
};
|
||||||
|
|
||||||
pub const Globaltype = struct {
|
pub const Globaltype = struct {
|
||||||
t: vm.Valtype,
|
t: vm.Valtype,
|
||||||
m: enum {
|
m: GlobalMutability,
|
||||||
@"const",
|
|
||||||
@"var",
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
fn parseGlobaltype(self: *Parser) !Globaltype {
|
fn parseGlobaltype(self: *Parser) !Globaltype {
|
||||||
return .{
|
return .{
|
||||||
|
|
|
||||||
|
|
@ -170,7 +170,7 @@ pub const Runtime = struct {
|
||||||
.localset => frame.locals[index.u32] = self.stack.pop().?,
|
.localset => frame.locals[index.u32] = self.stack.pop().?,
|
||||||
.localtee => frame.locals[index.u32] = self.stack.items[self.stack.items.len - 1],
|
.localtee => frame.locals[index.u32] = self.stack.items[self.stack.items.len - 1],
|
||||||
.globalget => try self.stack.append(self.global_runtime.getGlobal(index.u32)),
|
.globalget => try self.stack.append(self.global_runtime.getGlobal(index.u32)),
|
||||||
.globalset => @panic("UNIMPLEMENTED"),
|
.globalset => try self.global_runtime.updateGlobal(index.u32, self.stack.pop().?),
|
||||||
|
|
||||||
.tableget => @panic("UNIMPLEMENTED"),
|
.tableget => @panic("UNIMPLEMENTED"),
|
||||||
.tableset => @panic("UNIMPLEMENTED"),
|
.tableset => @panic("UNIMPLEMENTED"),
|
||||||
|
|
@ -212,13 +212,14 @@ pub const Runtime = struct {
|
||||||
.i64_load16_u => @panic("UNIMPLEMENTED"),
|
.i64_load16_u => @panic("UNIMPLEMENTED"),
|
||||||
.i64_load32_s => @panic("UNIMPLEMENTED"),
|
.i64_load32_s => @panic("UNIMPLEMENTED"),
|
||||||
.i64_load32_u => @panic("UNIMPLEMENTED"),
|
.i64_load32_u => @panic("UNIMPLEMENTED"),
|
||||||
.i32_store => {
|
// .i32_store => {
|
||||||
// TODO(ernesto): I'm pretty sure this is wrong
|
// // TODO(ernesto): I'm pretty sure this is wrong
|
||||||
const start = index.memarg.offset + index.memarg.alignment;
|
// const start = index.memarg.offset + index.memarg.alignment;
|
||||||
const end = start + @sizeOf(u32);
|
// const end = start + @sizeOf(u32);
|
||||||
const val = std.mem.nativeToLittle(i32, self.stack.pop().?.i32);
|
// const val = std.mem.nativeToLittle(i32, self.stack.pop().?.i32);
|
||||||
@memcpy(self.memory[start..end], std.mem.asBytes(&val));
|
// @memcpy(self.memory[start..end], std.mem.asBytes(&val));
|
||||||
},
|
// },
|
||||||
|
.i32_store => @panic("UNIMPLEMENTED"),
|
||||||
.i64_store => @panic("UNIMPLEMENTED"),
|
.i64_store => @panic("UNIMPLEMENTED"),
|
||||||
.f32_store => @panic("UNIMPLEMENTED"),
|
.f32_store => @panic("UNIMPLEMENTED"),
|
||||||
.f64_store => @panic("UNIMPLEMENTED"),
|
.f64_store => @panic("UNIMPLEMENTED"),
|
||||||
|
|
@ -306,7 +307,11 @@ pub const Runtime = struct {
|
||||||
const b = self.stack.pop().?.i32;
|
const b = self.stack.pop().?.i32;
|
||||||
try self.stack.append(Value{ .i32 = a + b });
|
try self.stack.append(Value{ .i32 = a + b });
|
||||||
},
|
},
|
||||||
.i32_sub => @panic("UNIMPLEMENTED"),
|
.i32_sub => {
|
||||||
|
const b = self.stack.pop().?.i32;
|
||||||
|
const a = self.stack.pop().?.i32;
|
||||||
|
try self.stack.append(Value{ .i32 = a - b });
|
||||||
|
},
|
||||||
.i32_and => {
|
.i32_and => {
|
||||||
const a = self.stack.pop().?.i32;
|
const a = self.stack.pop().?.i32;
|
||||||
const b = self.stack.pop().?.i32;
|
const b = self.stack.pop().?.i32;
|
||||||
|
|
|
||||||
|
|
@ -41,12 +41,9 @@ pub const GlobalRuntime = struct {
|
||||||
|
|
||||||
pub fn updateGlobal(self: *GlobalRuntime, index: u32, value: vm.Value) !void {
|
pub fn updateGlobal(self: *GlobalRuntime, index: u32, value: vm.Value) !void {
|
||||||
const globType = self.globals.get(index) orelse std.debug.panic("Tried updating global {any} but couldn't find it.\n", .{index});
|
const globType = self.globals.get(index) orelse std.debug.panic("Tried updating global {any} but couldn't find it.\n", .{index});
|
||||||
if(globType.m == globType.m.@"const"){
|
if(globType.m == Parser.GlobalMutability.@"const"){
|
||||||
std.debug.panic("Attempted write to immutable global\n", .{});
|
std.debug.panic("Attempted write to immutable global\n", .{});
|
||||||
}
|
}
|
||||||
if (globType.@"type" != value) {
|
|
||||||
std.debug.panic("Type mismatches for global {any}\n", .{index});
|
|
||||||
}
|
|
||||||
try self.globalExprs.put(index, value);
|
try self.globalExprs.put(index, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue