Skip to content

Commit

Permalink
fix(wasm): Fixed wasm build
Browse files Browse the repository at this point in the history
closes #142
  • Loading branch information
giann committed May 16, 2024
1 parent 90ce7d1 commit 8e66570
Show file tree
Hide file tree
Showing 21 changed files with 326 additions and 202 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ jobs:
run: zig build -Doptimize=ReleaseFast -Djit_always_on test
- name: Cleanup
run: rm -rf zig-out zig-cache
wasm-build:
runs-on: ubuntu-latest
steps:
- name: Checkout project
uses: actions/checkout@v3.0.0
- name: Checkout submodules
run: git submodule update --init --recursive
- name: Setup nightly Zig
uses: goto-bus-stop/setup-zig@v1
with:
version: master
- name: Build for wasm
run: zig build -Dtarget=wasm32-freestanding -freference-trace -Doptimize=ReleaseSmall
- name: Cleanup
run: rm -rf zig-out zig-cache
lint:
runs-on: macos-latest
steps:
Expand Down
4 changes: 0 additions & 4 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,6 @@ pub fn build(b: *Build) !void {
run_tests.setEnvironmentVariable("BUZZ_PATH", try getBuzzPrefix(b));
run_tests.step.dependOn(install_step); // wait for libraries to be installed
test_step.dependOn(&run_tests.step);

if (is_wasm) {
buildWasmReplDemo(b, exe);
}
}

pub fn buildPcre2(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) !*Build.Step.Compile {
Expand Down
25 changes: 13 additions & 12 deletions src/Codegen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const Reporter = @import("Reporter.zig");
const BuildOptions = @import("build_options");
const JIT = if (!is_wasm) @import("Jit.zig") else void;
const disassembler = @import("disassembler.zig");
const io = @import("io.zig");

const Self = @This();

Expand Down Expand Up @@ -162,7 +163,7 @@ pub fn generate(self: *Self, ast: Ast) anyerror!?*obj.ObjFunction {

// try root.node.toJson(&root.node, &out.writer());

// try std.io.getStdOut().writer().print("\n{s}", .{out.items});
// try io.stdOutWriter.print("\n{s}", .{out.items});
// }

const function = self.generateNode(self.ast.root.?, null);
Expand Down Expand Up @@ -1770,8 +1771,8 @@ fn generateFor(self: *Self, node: Ast.Node.Index, breaks: ?*Breaks) Error!?*obj.
}

const loop_start = self.currentCode();
const jit_jump = try self.emitJump(locations[node], .OP_HOTSPOT);
try self.emit(locations[node], node);
const jit_jump = if (!is_wasm) try self.emitJump(locations[node], .OP_HOTSPOT) else {};
if (!is_wasm) try self.emit(locations[node], node);

const condition_type_def = type_defs[components.condition].?;
if (condition_type_def.def_type == .Placeholder) {
Expand Down Expand Up @@ -1831,7 +1832,7 @@ fn generateFor(self: *Self, node: Ast.Node.Index, breaks: ?*Breaks) Error!?*obj.
try self.patchOptJumps(node);
try self.endScope(node);

self.patchTryOrJit(jit_jump);
if (!is_wasm) self.patchTryOrJit(jit_jump);

return null;
}
Expand Down Expand Up @@ -2038,8 +2039,8 @@ fn generateForEach(self: *Self, node: Ast.Node.Index, breaks: ?*Breaks) Error!?*
_ = try self.generateNode(components.iterable, breaks);

const loop_start: usize = self.currentCode();
const jit_jump = try self.emitJump(locations[node], .OP_HOTSPOT);
try self.emit(locations[node], node);
const jit_jump = if (!is_wasm) try self.emitJump(locations[node], .OP_HOTSPOT) else {};
if (!is_wasm) try self.emit(locations[node], node);

// Calls `next` and update key and value locals
try self.emitOpCode(
Expand Down Expand Up @@ -2105,7 +2106,7 @@ fn generateForEach(self: *Self, node: Ast.Node.Index, breaks: ?*Breaks) Error!?*
);
try self.endScope(node);

self.patchTryOrJit(jit_jump);
if (!is_wasm) self.patchTryOrJit(jit_jump);

return null;
}
Expand Down Expand Up @@ -2285,7 +2286,7 @@ fn generateFunction(self: *Self, node: Ast.Node.Index, breaks: ?*Breaks) Error!?

if (BuildOptions.debug) {
disassembler.disassembleChunk(&current_function.chunk, current_function.name.string);
std.debug.print("\n\n", .{});
io.print("\n\n", .{});
}

self.current = frame.enclosing;
Expand Down Expand Up @@ -3059,7 +3060,7 @@ fn generateObjectInit(self: *Self, node: Ast.Node.Index, breaks: ?*Breaks) Error
self.reporter.reportPlaceholder(self.ast, value_type_def.resolved_type.?.Placeholder);
} else if (!prop.eql(value_type_def)) {
if (BuildOptions.debug_placeholders) {
std.debug.print(
io.print(
"prop {}({}), value {}({})\n",
.{
@intFromPtr(prop.resolved_type.?.ObjectInstance),
Expand Down Expand Up @@ -3852,8 +3853,8 @@ fn generateWhile(self: *Self, node: Ast.Node.Index, breaks: ?*Breaks) Error!?*ob

const loop_start: usize = self.currentCode();

const jit_jump = try self.emitJump(locations[node], .OP_HOTSPOT);
try self.emit(locations[node], node);
const jit_jump = if (!is_wasm) try self.emitJump(locations[node], .OP_HOTSPOT) else {};
if (!is_wasm) try self.emit(locations[node], node);

if (condition_type_def.def_type == .Placeholder) {
self.reporter.reportPlaceholder(self.ast, condition_type_def.resolved_type.?.Placeholder);
Expand Down Expand Up @@ -3893,7 +3894,7 @@ fn generateWhile(self: *Self, node: Ast.Node.Index, breaks: ?*Breaks) Error!?*ob
try self.patchOptJumps(node);
try self.endScope(node);

self.patchTryOrJit(jit_jump);
if (!is_wasm) self.patchTryOrJit(jit_jump);

return null;
}
Expand Down
23 changes: 12 additions & 11 deletions src/Jit.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const VM = r.VM;
const ZigType = @import("zigtypes.zig").Type;
const ExternApi = @import("jit_extern_api.zig").ExternApi;
const api = @import("lib/buzz_api.zig");
const io = @import("io.zig");

pub const Error = error{
CantCompile,
Expand Down Expand Up @@ -157,7 +158,7 @@ pub fn compileFunction(self: *Self, ast: Ast, closure: *o.ObjClosure) Error!void
&seen,
)) {
if (BuildOptions.jit_debug) {
std.debug.print(
io.print(
"Not compiling node {s}#{}, likely because it uses a fiber\n",
.{
@tagName(ast.nodes.items(.tag)[ast_node]),
Expand Down Expand Up @@ -237,7 +238,7 @@ pub fn compileHotSpot(self: *Self, ast: Ast, hotspot_node: Ast.Node.Index) Error
&seen,
)) {
if (BuildOptions.jit_debug) {
std.debug.print(
io.print(
"Not compiling node {s}#{}, likely because it uses a fiber\n",
.{
@tagName(ast.nodes.items(.tag)[hotspot_node]),
Expand Down Expand Up @@ -370,7 +371,7 @@ fn buildFunction(self: *Self, ast: Ast, closure: ?*o.ObjClosure, ast_node: Ast.N
try self.compiled_closures.put(uclosure, {});

if (BuildOptions.jit_debug) {
std.debug.print(
io.print(
"Compiling function `{s}` because it was called {}/{} times\n",
.{
qualified_name.items,
Expand All @@ -383,7 +384,7 @@ fn buildFunction(self: *Self, ast: Ast, closure: ?*o.ObjClosure, ast_node: Ast.N
try self.compiled_hotspots.put(ast_node, {});
} else {
if (BuildOptions.jit_debug) {
std.debug.print(
io.print(
"Compiling closure `{s}`\n",
.{
qualified_name.items,
Expand All @@ -398,7 +399,7 @@ fn buildFunction(self: *Self, ast: Ast, closure: ?*o.ObjClosure, ast_node: Ast.N
self.generateNode(ast_node)) catch |err| {
if (err == Error.CantCompile) {
if (BuildOptions.jit_debug) {
std.debug.print("Not compiling `{s}`, likely because it uses a fiber\n", .{qualified_name.items});
io.print("Not compiling `{s}`, likely because it uses a fiber\n", .{qualified_name.items});
}

m.MIR_finish_func(self.ctx);
Expand Down Expand Up @@ -509,7 +510,7 @@ fn generateNode(self: *Self, node: Ast.Node.Index) Error!?m.MIR_op_t {
=> return Error.CantCompile,

else => {
std.debug.print("{} NYI\n", .{tag});
io.print("{} NYI\n", .{tag});
unreachable;
},
};
Expand Down Expand Up @@ -4434,7 +4435,7 @@ fn generateHotspotFunction(self: *Self, node: Ast.Node.Index) Error!?m.MIR_op_t
_ = self.generateNode(node) catch |err| {
if (err == error.CantCompile) {
if (BuildOptions.jit_debug) {
std.debug.print(
io.print(
"Not compiling node {s}#{}, likely because it uses a fiber\n",
.{
@tagName(self.state.?.ast.nodes.items(.tag)[self.state.?.ast_node]),
Expand Down Expand Up @@ -4692,7 +4693,7 @@ fn getQualifiedName(self: *Self, node: Ast.Node.Index, raw: bool) !std.ArrayList

else => {
if (BuildOptions.debug) {
std.debug.print(
io.print(
"Ast {s} node are not valid hotspots",
.{
@tagName(tag),
Expand All @@ -4715,7 +4716,7 @@ pub fn compileZdefContainer(self: *Self, ast: Ast, zdef_element: Ast.Zdef.ZdefEl
defer m.MIR_finish_module(self.ctx);

if (BuildOptions.jit_debug) {
std.debug.print(
io.print(
"Compiling zdef struct getters/setters for `{s}` of type `{s}`\n",
.{
zdef_element.zdef.name,
Expand Down Expand Up @@ -5004,7 +5005,7 @@ pub fn compileZdef(self: *Self, buzz_ast: Ast, zdef: Ast.Zdef.ZdefElement) Error
defer m.MIR_finish_module(self.ctx);

if (BuildOptions.jit_debug) {
std.debug.print(
io.print(
"Compiling zdef wrapper for `{s}` of type `{s}`\n",
.{
zdef.zdef.name,
Expand Down Expand Up @@ -5113,7 +5114,7 @@ fn zigToMIRRegType(zig_type: ZigType) m.MIR_type_t {
// Optional are only allowed on pointers
.Optional => m.MIR_T_I64,
else => {
std.debug.print("{}\n", .{zig_type});
io.print("{}\n", .{zig_type});
unreachable;
},
};
Expand Down
Loading

0 comments on commit 8e66570

Please sign in to comment.