aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/c.zig
diff options
context:
space:
mode:
authorLuuk de Gram <luuk@degram.dev>2022-03-03 22:12:53 +0100
committerAndrew Kelley <andrew@ziglang.org>2022-03-03 16:33:46 -0700
commit7fd32de0180c29dc4e3da72e9347f6f5ce167a38 (patch)
tree6c72da6895e5da86988f6ed606eb98eee85c39ab /src/codegen/c.zig
parent21f0503c0137b7bb59edd87e17e1649152d342ba (diff)
downloadzig-7fd32de0180c29dc4e3da72e9347f6f5ce167a38.tar.gz
zig-7fd32de0180c29dc4e3da72e9347f6f5ce167a38.zip
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.
Diffstat (limited to 'src/codegen/c.zig')
-rw-r--r--src/codegen/c.zig32
1 files changed, 30 insertions, 2 deletions
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",