Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ jobs:
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v2
- uses: mlugg/setup-zig@v1.2.1
with:
version: 0.14.0
- uses: mlugg/setup-zig@v2.0.5
- run: zig build unittest
testsuite:
strategy:
Expand All @@ -25,18 +23,14 @@ jobs:
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v2
- uses: mlugg/setup-zig@v1.2.1
with:
version: 0.14.0
- uses: mlugg/setup-zig@v2.0.5
- name: Run testsuite
run: zig build testsuite
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: mlugg/setup-zig@v1.2.1
with:
version: 0.14.0
- uses: mlugg/setup-zig@v2.0.5
- run: zig fmt --check src/*.zig
fib:
strategy:
Expand All @@ -45,9 +39,7 @@ jobs:
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v2
- uses: mlugg/setup-zig@v1.2.1
with:
version: 0.14.0
- uses: mlugg/setup-zig@v2.0.5
- name: Build fib
working-directory: ./examples/fib
run: zig build
Expand All @@ -58,8 +50,6 @@ jobs:
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v2
- uses: mlugg/setup-zig@v1.2.1
with:
version: 0.14.0
- uses: mlugg/setup-zig@v2.0.5
- name: Build zware-gen, zware-run and libzware.a
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.12 (master)
- Zig 0.15.1 (master)

### Run-time

Expand Down
49 changes: 32 additions & 17 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ pub fn build(b: *Build) !void {

try b.modules.put(b.dupe("zware"), zware_module);

const lib = b.addStaticLibrary(.{
.name = "zware",
const main_mod = b.addModule("zware", .{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const lib = b.addLibrary(.{
.name = "zware",
.root_module = main_mod,
});
b.installArtifact(lib);

const main_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.optimize = optimize,
.root_module = main_mod,
.use_llvm = true,
});

const run_main_tests = b.addRunArtifact(main_tests);
Expand All @@ -31,9 +34,12 @@ pub fn build(b: *Build) !void {

const testrunner = b.addExecutable(.{
.name = "testrunner",
.root_source_file = b.path("test/testrunner/src/testrunner.zig"),
.target = target,
.optimize = optimize,
.root_module = b.addModule("testrunner", .{
.root_source_file = b.path("test/testrunner/src/testrunner.zig"),
.target = target,
.optimize = optimize,
}),
.use_llvm = true,
});
testrunner.root_module.addImport("zware", zware_module);

Expand Down Expand Up @@ -61,9 +67,12 @@ pub fn build(b: *Build) !void {
{
const exe = b.addExecutable(.{
.name = "zware-run",
.root_source_file = b.path("tools/zware-run.zig"),
.target = target,
.optimize = optimize,
.root_module = b.addModule("zware-run", .{
.root_source_file = b.path("tools/zware-run.zig"),
.target = target,
.optimize = optimize,
}),
.use_llvm = true,
});
exe.root_module.addImport("zware", zware_module);
const install = b.addInstallArtifact(exe, .{});
Expand All @@ -79,9 +88,11 @@ pub fn build(b: *Build) !void {
{
const exe = b.addExecutable(.{
.name = "zware-gen",
.root_source_file = b.path("tools/zware-gen.zig"),
.target = target,
.optimize = optimize,
.root_module = b.addModule("zware-gen", .{
.root_source_file = b.path("tools/zware-gen.zig"),
.target = target,
.optimize = optimize,
}),
});
exe.root_module.addImport("zware", zware_module);
const install = b.addInstallArtifact(exe, .{});
Expand Down Expand Up @@ -116,10 +127,12 @@ fn addWast2Json(b: *Build) *Build.Step.Compile {
.SIZEOF_SIZE_T = @sizeOf(usize),
});

const wabt_lib = b.addStaticLibrary(.{
const wabt_lib = b.addLibrary(.{
.name = "wabt",
.target = b.graph.host,
.optimize = .Debug,
.root_module = b.createModule(.{
.target = b.graph.host,
.optimize = .Debug,
}),
});
wabt_lib.addConfigHeader(wabt_config_h);
wabt_lib.addIncludePath(wabt_dep.path("include"));
Expand All @@ -131,7 +144,9 @@ fn addWast2Json(b: *Build) *Build.Step.Compile {

const wast2json = b.addExecutable(.{
.name = "wast2json",
.target = b.graph.host,
.root_module = b.createModule(.{
.target = b.graph.host,
}),
});
wast2json.addConfigHeader(wabt_config_h);
wast2json.addIncludePath(wabt_dep.path("include"));
Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.name = .zware,
.version = "0.0.1",
.fingerprint = 0xea6cb8e2e9e30e64,
.minimum_zig_version = "0.14.0",
.minimum_zig_version = "0.15.1",
.dependencies = .{
.wabt = .{
.url = "https://github.com/WebAssembly/wabt/archive/39f85a791cbbad91a253a851841a29777efdc2cd.tar.gz",
Expand Down
9 changes: 6 additions & 3 deletions examples/fib/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ pub fn build(b: *Build) void {

const exe = b.addExecutable(.{
.name = "fib",
.root_source_file = b.path("src/fib.zig"),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.root_source_file = b.path("src/fib.zig"),
.target = target,
.optimize = optimize,
}),
.use_llvm = true,
});
exe.root_module.addAnonymousImport("zware", .{
.root_source_file = b.path("../../src/main.zig"),
Expand Down
6 changes: 1 addition & 5 deletions src/error.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ pub const Error = union(enum) {
}
pub fn format(
self: Error,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
writer: *std.Io.Writer,
) !void {
_ = fmt;
_ = options;
switch (self) {
.missing_import => |import| try writer.print(
"missing {s} import '{s}' from module '{s}'",
Expand Down
66 changes: 34 additions & 32 deletions src/instance.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const VirtualMachineOptions = struct {
// - `tableaddrs`: as per `memaddrs` but for tables
// - `globaladdrs`: as per `memaddrs` but for globals
pub const Instance = struct {
alloc: mem.Allocator,
module: Module,
store: *Store,
funcaddrs: ArrayList(usize),
Expand All @@ -55,38 +56,39 @@ pub const Instance = struct {
// a VirtualMachine swaps out its `inst` (instance) pointer as
// it executes; an arbitrary `inst` will not contain the correct
// data.
wasi_preopens: std.AutoHashMap(wasi.fd_t, WasiPreopen),
wasi_preopens: std.AutoHashMapUnmanaged(wasi.fd_t, WasiPreopen),
wasi_args: std.ArrayList([:0]u8),
wasi_env: std.StringHashMap([]const u8),
wasi_env: std.StringHashMapUnmanaged([]const u8),

pub fn init(alloc: mem.Allocator, store: *Store, module: Module) Instance {
return Instance{
.alloc = alloc,
.module = module,
.store = store,
.funcaddrs = ArrayList(usize).init(alloc),
.memaddrs = ArrayList(usize).init(alloc),
.tableaddrs = ArrayList(usize).init(alloc),
.globaladdrs = ArrayList(usize).init(alloc),
.elemaddrs = ArrayList(usize).init(alloc),
.dataaddrs = ArrayList(usize).init(alloc),

.wasi_preopens = std.AutoHashMap(wasi.fd_t, WasiPreopen).init(alloc),
.wasi_args = ArrayList([:0]u8).init(alloc),
.wasi_env = std.StringHashMap([]const u8).init(alloc),
.funcaddrs = .empty,
.memaddrs = .empty,
.tableaddrs = .empty,
.globaladdrs = .empty,
.elemaddrs = .empty,
.dataaddrs = .empty,

.wasi_preopens = .empty,
.wasi_args = .empty,
.wasi_env = .empty,
};
}

pub fn deinit(self: *Instance) void {
defer self.funcaddrs.deinit();
defer self.memaddrs.deinit();
defer self.tableaddrs.deinit();
defer self.globaladdrs.deinit();
defer self.elemaddrs.deinit();
defer self.dataaddrs.deinit();

defer self.wasi_preopens.deinit();
defer self.wasi_args.deinit();
defer self.wasi_env.deinit();
defer self.funcaddrs.deinit(self.alloc);
defer self.memaddrs.deinit(self.alloc);
defer self.tableaddrs.deinit(self.alloc);
defer self.globaladdrs.deinit(self.alloc);
defer self.elemaddrs.deinit(self.alloc);
defer self.dataaddrs.deinit(self.alloc);

defer self.wasi_preopens.deinit(self.alloc);
defer self.wasi_args.deinit(self.alloc);
defer self.wasi_env.deinit(self.alloc);
}

pub fn getFunc(self: *Instance, funcidx: usize) !Function {
Expand Down Expand Up @@ -161,10 +163,10 @@ pub const Instance = struct {
error.ImportNotFound => return err.set(.{ .missing_import = import }),
};
switch (import.desc_tag) {
.Func => try self.funcaddrs.append(import_handle),
.Mem => try self.memaddrs.append(import_handle),
.Table => try self.tableaddrs.append(import_handle),
.Global => try self.globaladdrs.append(import_handle),
.Func => try self.funcaddrs.append(self.alloc, import_handle),
.Mem => try self.memaddrs.append(self.alloc, import_handle),
.Table => try self.tableaddrs.append(self.alloc, import_handle),
.Global => try self.globaladdrs.append(self.alloc, import_handle),
}
}
}
Expand Down Expand Up @@ -208,7 +210,7 @@ pub const Instance = struct {
});

// Need to do this regardless of if import or internal
try self.funcaddrs.append(handle);
try self.funcaddrs.append(self.alloc, handle);
}
}
}
Expand All @@ -226,7 +228,7 @@ pub const Instance = struct {
.mutability = global_def.mutability,
.valtype = global_def.valtype,
});
try self.globaladdrs.append(handle);
try self.globaladdrs.append(self.alloc, handle);
}
}
}
Expand All @@ -239,7 +241,7 @@ pub const Instance = struct {
try memtype.limits.checkMatch(imported_mem.size(), imported_mem.max);
} else {
const handle = try self.store.addMemory(memtype.limits.min, memtype.limits.max);
try self.memaddrs.append(handle);
try self.memaddrs.append(self.alloc, handle);
}
}
}
Expand All @@ -252,15 +254,15 @@ pub const Instance = struct {
try tabletype.limits.checkMatch(imported_table.min, imported_table.max);
} else {
const handle = try self.store.addTable(tabletype.reftype, tabletype.limits.min, tabletype.limits.max);
try self.tableaddrs.append(handle);
try self.tableaddrs.append(self.alloc, handle);
}
}
}

fn instantiateData(self: *Instance) !void {
for (self.module.datas.list.items) |datatype| {
const dataddr = try self.store.addData(datatype.count);
try self.dataaddrs.append(dataddr);
try self.dataaddrs.append(self.alloc, dataddr);
var data = try self.store.data(dataddr);

// TODO: Do we actually need to copy the data or just close over module bytes?
Expand All @@ -285,7 +287,7 @@ pub const Instance = struct {
fn instantiateElements(self: *Instance) !void {
for (self.module.elements.list.items) |elemtype| {
const elemaddr = try self.store.addElem(elemtype.reftype, elemtype.count);
try self.elemaddrs.append(elemaddr);
try self.elemaddrs.append(self.alloc, elemaddr);
var elem = try self.store.elem(elemaddr);

for (self.module.element_init_offsets.items[elemtype.init .. elemtype.init + elemtype.count], 0..) |expr, j| {
Expand Down
17 changes: 13 additions & 4 deletions src/instance/vm.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const builtin = @import("builtin");
const os = std.os;
const posix = std.posix;
const mem = std.mem;
Expand Down Expand Up @@ -52,9 +53,9 @@ pub const VirtualMachine = struct {
// These fields match the types in Instance but are
// instead pointers. These will point to the Instance
// that initialises the VirtualMachine
wasi_preopens: *std.AutoHashMap(wasi.fd_t, WasiPreopen),
wasi_preopens: *std.AutoHashMapUnmanaged(wasi.fd_t, WasiPreopen),
wasi_args: *std.ArrayList([:0]u8),
wasi_env: *std.StringHashMap([]const u8),
wasi_env: *std.StringHashMapUnmanaged([]const u8),

pub const Frame = struct {
locals: []u64 = undefined, // TODO: we're in trouble if we move our stacks in memory
Expand Down Expand Up @@ -125,7 +126,11 @@ pub const VirtualMachine = struct {
inline fn dispatch(self: *VirtualMachine, next_ip: usize, code: []Rr) WasmError!void {
const next_instr = code[next_ip];

return try @call(.always_tail, lookup[@intFromEnum(next_instr)], .{ self, next_ip, code });
if (builtin.zig_backend == .stage2_x86_64) {
@compileError("zware currently requires the LLVM backend for `.always_tail`. See https://github.com/ziglang/zig/issues/24044");
}

return @call(.always_tail, lookup[@intFromEnum(next_instr)], .{ self, next_ip, code });
}

pub const REF_NULL: u64 = 0xFFFF_FFFF_FFFF_FFFF;
Expand Down Expand Up @@ -2111,7 +2116,11 @@ pub const VirtualMachine = struct {
inline fn miscDispatch(self: *VirtualMachine, next_ip: usize, code: []Rr) WasmError!void {
const next_instr = code[next_ip].misc;

return try @call(.always_tail, misc_lookup[@intFromEnum(next_instr)], .{ self, next_ip, code });
if (builtin.zig_backend == .stage2_x86_64) {
@compileError("zware currently requires the LLVM backend for `.always_tail`. See https://github.com/ziglang/zig/issues/24044");
}

return @call(.always_tail, misc_lookup[@intFromEnum(next_instr)], .{ self, next_ip, code });
}

fn @"i32.trunc_sat_f32_s"(self: *VirtualMachine, ip: usize, code: []Rr) WasmError!void {
Expand Down
Loading