From 7fd32de0180c29dc4e3da72e9347f6f5ce167a38 Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Thu, 3 Mar 2022 22:12:53 +0100 Subject: cbe: Implement wasm builtins This implements the wasm builtins by lowering to builtins that are supported by c-compilers. In this case: Clang. This also simplifies the `AIR` instruction as it now uses the payload field of `ty_pl` and `pl_op` directly to store the index argument rather than storing it inside Extra. This saves us 4 bytes per builtin call. --- src/codegen/c.zig | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 988576a6f1..a36b58041c 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -1759,8 +1759,8 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO .wrap_errunion_err => try airWrapErrUnionErr(f, inst), .errunion_payload_ptr_set => try airErrUnionPayloadPtrSet(f, inst), - .wasm_memory_size => unreachable, - .wasm_memory_grow => unreachable, + .wasm_memory_size => try airWasmMemorySize(f, inst), + .wasm_memory_grow => try airWasmMemoryGrow(f, inst), // zig fmt: on }; switch (result_value) { @@ -3591,6 +3591,34 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { return CValue.none; } +fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue { + const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; + + const writer = f.object.writer(); + const inst_ty = f.air.typeOfIndex(inst); + const local = try f.allocLocal(inst_ty, .Const); + + try writer.writeAll(" = "); + try writer.print("zig_wasm_memory_size({d});\n", .{ty_pl.payload}); + + return local; +} + +fn airWasmMemoryGrow(f: *Function, inst: Air.Inst.Index) !CValue { + const pl_op = f.air.instructions.items(.data)[inst].pl_op; + + const writer = f.object.writer(); + const inst_ty = f.air.typeOfIndex(inst); + const operand = try f.resolveInst(pl_op.operand); + const local = try f.allocLocal(inst_ty, .Const); + + try writer.writeAll(" = "); + try writer.print("zig_wasm_memory_grow({d}, ", .{pl_op.payload}); + try f.writeCValue(writer, operand); + try writer.writeAll(");\n"); + return local; +} + fn toMemoryOrder(order: std.builtin.AtomicOrder) [:0]const u8 { return switch (order) { .Unordered => "memory_order_relaxed", -- cgit v1.2.3