Skip to content

Commit

Permalink
Updated for 0.12 (#220)
Browse files Browse the repository at this point in the history
Minimal changes. As far as I can tell all the tests are passing.
  • Loading branch information
tgschultz authored Apr 29, 2024
1 parent 5f15cf8 commit 6f875c1
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 66 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.11.0
version: 0.12.0
- run: zig build unittest
testsuite:
strategy:
Expand All @@ -27,7 +27,7 @@ jobs:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.11.0
version: 0.12.0
- name: Run testsuite
run: zig build testsuite
parsecheck:
Expand All @@ -36,7 +36,7 @@ jobs:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.11.0
version: 0.12.0
- name: Build parsecheck
working-directory: ./test/parsecheck
run: zig build --prefix ./
Expand All @@ -51,7 +51,7 @@ jobs:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.11.0
version: 0.12.0
- name: Build interface
working-directory: ./test/interface
run: zig build
Expand All @@ -64,7 +64,7 @@ jobs:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.11.0
version: 0.12.0
- name: Build zware-gen
working-directory: ./tools/zware-gen
run: zig build
Expand All @@ -74,7 +74,7 @@ jobs:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.11.0
version: 0.12.0
- run: zig fmt --check src/*.zig
fib:
strategy:
Expand All @@ -85,7 +85,7 @@ jobs:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.11.0
version: 0.12.0
- name: Build fib
working-directory: ./examples/fib
run: zig build
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn main() !void {

### Compile-time

- Zig 0.11 (master)
- Zig 0.12 (master)

### Run-time

Expand Down
14 changes: 7 additions & 7 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const Builder = @import("std").build.Builder;
const Build = @import("std").Build;

pub fn build(b: *Builder) !void {
pub fn build(b: *Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

var zware_module = b.createModule(.{
.source_file = .{ .path = "src/main.zig" },
const zware_module = b.createModule(.{
.root_source_file = .{ .path = "src/main.zig" },
});

try b.modules.put(b.dupe("zware"), zware_module);
Expand Down Expand Up @@ -33,13 +33,13 @@ pub fn build(b: *Builder) !void {
.target = target,
.optimize = optimize,
});
testrunner.addModule("zware", zware_module);
testrunner.root_module.addImport("zware", zware_module);

const testsuite_step = b.step("testsuite", "Run all the testsuite tests");
for (test_names) |test_name| {
const run_test = b.addRunArtifact(testrunner);
run_test.addFileSourceArg(.{ .path = b.fmt("test/testsuite-generated/{s}.json", .{test_name}) });
run_test.cwd = b.pathFromRoot("test/testsuite-generated");
run_test.addFileArg(.{ .path = b.fmt("test/testsuite-generated/{s}.json", .{test_name}) });
run_test.cwd = .{ .path = b.pathFromRoot("test/testsuite-generated") };
const step = b.step(b.fmt("test-{s}", .{test_name}), b.fmt("Run the '{s}' test", .{test_name}));
step.dependOn(&run_test.step);
testsuite_step.dependOn(&run_test.step);
Expand Down
1 change: 1 addition & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.{
.paths =.{"."},
.name = "zware",
.version = "0.0.1",

Expand Down
8 changes: 4 additions & 4 deletions examples/fib/build.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Builder = @import("std").build.Builder;
const Build = @import("std").Build;

pub fn build(b: *Builder) void {
pub fn build(b: *Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

Expand All @@ -10,8 +10,8 @@ pub fn build(b: *Builder) void {
.target = target,
.optimize = optimize,
});
exe.addAnonymousModule("zware", .{
.source_file = .{ .path = "../../src/main.zig" },
exe.root_module.addAnonymousImport("zware", .{
.root_source_file = .{ .path = "../../src/main.zig" },
});
b.installArtifact(exe);

Expand Down
2 changes: 1 addition & 1 deletion src/instance.zig
Original file line number Diff line number Diff line change
Expand Up @@ -469,5 +469,5 @@ pub const Instance = struct {
pub const WasiPreopen = struct {
wasi_fd: wasi.fd_t,
name: []const u8,
host_fd: os.fd_t,
host_fd: std.posix.fd_t,
};
22 changes: 11 additions & 11 deletions src/instance/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ pub const VirtualMachine = struct {

// The mem copy is equivalent of popping n operands, doing everything
// up to and including popFrame and then repushing the n operands
var dst = self.op_stack[label.op_stack_len .. label.op_stack_len + n];
const dst = self.op_stack[label.op_stack_len .. label.op_stack_len + n];
const src = self.op_stack[self.op_ptr - n .. self.op_ptr];
mem.copy(u64, dst, src);
mem.copyForwards(u64, dst, src);

self.op_ptr = label.op_stack_len + n;
self.label_ptr = frame.label_stack_len;
Expand Down Expand Up @@ -304,7 +304,7 @@ pub const VirtualMachine = struct {

fn call_indirect(self: *VirtualMachine, ip: usize, code: []Rr) WasmError!void {
const call_indirect_instruction = code[ip].call_indirect;
var module = self.inst.module;
const module = self.inst.module;

const typeidx = call_indirect_instruction.typeidx;
const tableidx = call_indirect_instruction.tableidx;
Expand Down Expand Up @@ -1188,7 +1188,7 @@ pub const VirtualMachine = struct {
const c2 = self.popOperand(i32);
const c1 = self.popOperand(i32);

const abs = try math.absInt(c2);
const abs = if(c2 == math.minInt(i32)) return error.Overflow else @as(i32, @intCast(@abs(c2)));
const rem = try math.rem(i32, c1, abs);

self.pushOperandNoCheck(i32, rem);
Expand Down Expand Up @@ -1355,7 +1355,7 @@ pub const VirtualMachine = struct {
const c2 = self.popOperand(i64);
const c1 = self.popOperand(i64);

const abs = try math.absInt(c2);
const abs = if(c2 == math.minInt(i64)) return error.Overflow else @as(i64, @intCast(@abs(c2)));
const rem = try math.rem(i64, c1, abs);

self.pushOperandNoCheck(i64, rem);
Expand Down Expand Up @@ -1451,7 +1451,7 @@ pub const VirtualMachine = struct {
fn @"f32.abs"(self: *VirtualMachine, ip: usize, code: []Rr) WasmError!void {
const c1 = self.popOperand(f32);

self.pushOperandNoCheck(f32, math.fabs(c1));
self.pushOperandNoCheck(f32, @abs(c1));

return dispatch(self, ip + 1, code);
}
Expand Down Expand Up @@ -1607,9 +1607,9 @@ pub const VirtualMachine = struct {
const c1 = self.popOperand(f32);

if (math.signbit(c2)) {
self.pushOperandNoCheck(f32, -math.fabs(c1));
self.pushOperandNoCheck(f32, -@abs(c1));
} else {
self.pushOperandNoCheck(f32, math.fabs(c1));
self.pushOperandNoCheck(f32, @abs(c1));
}

return dispatch(self, ip + 1, code);
Expand All @@ -1618,7 +1618,7 @@ pub const VirtualMachine = struct {
fn @"f64.abs"(self: *VirtualMachine, ip: usize, code: []Rr) WasmError!void {
const c1 = self.popOperand(f64);

self.pushOperandNoCheck(f64, math.fabs(c1));
self.pushOperandNoCheck(f64, @abs(c1));

return dispatch(self, ip + 1, code);
}
Expand Down Expand Up @@ -1766,9 +1766,9 @@ pub const VirtualMachine = struct {
const c1 = self.popOperand(f64);

if (math.signbit(c2)) {
self.pushOperandNoCheck(f64, -math.fabs(c1));
self.pushOperandNoCheck(f64, -@abs(c1));
} else {
self.pushOperandNoCheck(f64, math.fabs(c1));
self.pushOperandNoCheck(f64, @abs(c1));
}

return dispatch(self, ip + 1, code);
Expand Down
8 changes: 4 additions & 4 deletions src/module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub const Module = struct {
const magic = try rd.readBytesNoEof(4);
if (!mem.eql(u8, magic[0..], "\x00asm")) return error.MagicNumberNotFound;

const version = try rd.readIntLittle(u32);
const version = try rd.readInt(u32, .little);
if (version != 1) return error.UnknownBinaryVersion;

// FIXME: in hindsight I don't like this:
Expand Down Expand Up @@ -196,8 +196,8 @@ pub const Module = struct {
}
const results_end = self.pos();

var params = self.module[params_start..params_end];
var results = self.module[results_start..results_end];
const params = self.module[params_start..params_end];
const results = self.module[results_start..results_end];

var params_valtype: []const ValType = undefined;
params_valtype.ptr = @ptrCast(params.ptr);
Expand Down Expand Up @@ -825,7 +825,7 @@ pub const Module = struct {
fn readEnum(self: *Module, comptime T: type) !T {
const rd = self.buf.reader();

return rd.readEnum(T, .Little);
return rd.readEnum(T, .little);
}

fn readULEB128(self: *Module, comptime T: type) !T {
Expand Down
4 changes: 2 additions & 2 deletions src/module/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ pub const Parser = struct {
pub fn readU32(self: *Parser) !u32 {
var buf = std.io.fixedBufferStream(self.code);
const rd = buf.reader();
const value = try rd.readIntLittle(u32);
const value = try rd.readInt(u32, .little);

self.code.ptr += buf.pos;
self.code.len -= buf.pos;
Expand All @@ -1223,7 +1223,7 @@ pub const Parser = struct {
pub fn readU64(self: *Parser) !u64 {
var buf = std.io.fixedBufferStream(self.code);
const rd = buf.reader();
const value = try rd.readIntLittle(u64);
const value = try rd.readInt(u64, .little);

self.code.ptr += buf.pos;
self.code.len -= buf.pos;
Expand Down
2 changes: 1 addition & 1 deletion src/store.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub const ArrayListStore = struct {
imports: ArrayList(ImportExport),

pub fn init(alloc: mem.Allocator) ArrayListStore {
var store = ArrayListStore{
const store = ArrayListStore{
.alloc = alloc,
.functions = ArrayList(Function).init(alloc),
.memories = ArrayList(Memory).init(alloc),
Expand Down
16 changes: 8 additions & 8 deletions src/store/memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ pub const Memory = struct {
pub fn copy(self: *Memory, address: u32, data: []const u8) !void {
if (address + data.len > self.data.items.len) return error.OutOfBoundsMemoryAccess;

mem.copy(u8, self.data.items[address .. address + data.len], data);
mem.copyForwards(u8, self.data.items[address .. address + data.len], data);
}

pub fn uncheckedFill(self: *Memory, dst_address: u32, n: u32, value: u8) void {
@memset(self.data.items[dst_address .. dst_address + n], value);
}

pub fn uncheckedCopy(self: *Memory, dst_address: u32, data: []const u8) void {
mem.copy(u8, self.data.items[dst_address .. dst_address + data.len], data);
mem.copyForwards(u8, self.data.items[dst_address .. dst_address + data.len], data);
}

pub fn uncheckedCopyBackwards(self: *Memory, dst_address: u32, data: []const u8) void {
Expand All @@ -86,13 +86,13 @@ pub const Memory = struct {
i16,
i32,
i64,
=> return mem.readInt(T, @as(*const [@sizeOf(T)]u8, @ptrCast(&self.data.items[effective_address])), .Little),
=> return mem.readInt(T, @as(*const [@sizeOf(T)]u8, @ptrCast(&self.data.items[effective_address])), .little),
f32 => {
const x = mem.readInt(u32, @as(*const [@sizeOf(T)]u8, @ptrCast(&self.data.items[effective_address])), .Little);
const x = mem.readInt(u32, @as(*const [@sizeOf(T)]u8, @ptrCast(&self.data.items[effective_address])), .little);
return @bitCast(x);
},
f64 => {
const x = mem.readInt(u64, @as(*const [@sizeOf(T)]u8, @ptrCast(&self.data.items[effective_address])), .Little);
const x = mem.readInt(u64, @as(*const [@sizeOf(T)]u8, @ptrCast(&self.data.items[effective_address])), .little);
return @bitCast(x);
},
else => @compileError("Memory.read unsupported type (not int/float): " ++ @typeName(T)),
Expand All @@ -112,14 +112,14 @@ pub const Memory = struct {
i16,
i32,
i64,
=> std.mem.writeInt(T, @as(*[@sizeOf(T)]u8, @ptrCast(&self.data.items[effective_address])), value, .Little),
=> std.mem.writeInt(T, @as(*[@sizeOf(T)]u8, @ptrCast(&self.data.items[effective_address])), value, .little),
f32 => {
const x: u32 = @bitCast(value);
std.mem.writeInt(u32, @as(*[@sizeOf(u32)]u8, @ptrCast(&self.data.items[effective_address])), x, .Little);
std.mem.writeInt(u32, @as(*[@sizeOf(u32)]u8, @ptrCast(&self.data.items[effective_address])), x, .little);
},
f64 => {
const x: u64 = @bitCast(value);
std.mem.writeInt(u64, @as(*[@sizeOf(u64)]u8, @ptrCast(&self.data.items[effective_address])), x, .Little);
std.mem.writeInt(u64, @as(*[@sizeOf(u64)]u8, @ptrCast(&self.data.items[effective_address])), x, .little);
},
else => @compileError("Memory.read unsupported type (not int/float): " ++ @typeName(T)),
}
Expand Down
8 changes: 4 additions & 4 deletions test/interface/build.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Builder = @import("std").build.Builder;
const Build = @import("std").Build;

pub fn build(b: *Builder) void {
pub fn build(b: *Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

Expand All @@ -10,8 +10,8 @@ pub fn build(b: *Builder) void {
.target = target,
.optimize = optimize,
});
exe.addAnonymousModule("zware", .{
.source_file = .{ .path = "../../src/main.zig" },
exe.root_module.addAnonymousImport("zware", .{
.root_source_file = .{ .path = "../../src/main.zig" },
});
b.installArtifact(exe);

Expand Down
8 changes: 4 additions & 4 deletions test/parsecheck/build.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Builder = @import("std").build.Builder;
const Build = @import("std").Build;

pub fn build(b: *Builder) void {
pub fn build(b: *Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

Expand All @@ -10,8 +10,8 @@ pub fn build(b: *Builder) void {
.target = target,
.optimize = optimize,
});
exe.addAnonymousModule("zware", .{
.source_file = .{ .path = "../../src/main.zig" },
exe.root_module.addAnonymousImport("zware", .{
.root_source_file = .{ .path = "../../src/main.zig" },
});
b.installArtifact(exe);

Expand Down
Loading

0 comments on commit 6f875c1

Please sign in to comment.