authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-03-19 16:32:07+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-03-19 20:56:03+01:00
log2041176c5e7d2b469c2fe0168e142fe8f322b9bc
tree1868288991f3288032fd38bb9674ec838b4b716e
parent69e6d455ce8e21835ec3ce268a0533e0e7666e8b
signaturelock-open Commit is signed but in an unrecognized format.

wasm: Implement `union_init` instruction

Implements the `@unionInit` builtin instruction.

1 files changed, 28 insertions(+), 1 deletions(-)

src/arch/wasm/CodeGen.zig+28-1
...@@ -3221,7 +3221,34 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) InnerError!WValue {...@@ -3221,7 +3221,34 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
32213221
3222fn airUnionInit(self: *Self, inst: Air.Inst.Index) InnerError!WValue {3222fn airUnionInit(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3223 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };3223 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3224 return self.fail("TODO: Wasm backend: implement airUnionInit", .{});3224
3225 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
3226 const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data;
3227 const union_ty = self.air.typeOfIndex(inst);
3228 const layout = union_ty.unionGetLayout(self.target);
3229 if (layout.payload_size == 0) {
3230 if (layout.tag_size == 0) {
3231 return WValue{ .none = {} };
3232 }
3233 assert(!isByRef(union_ty, self.target));
3234 return WValue{ .imm32 = extra.field_index };
3235 }
3236 assert(isByRef(union_ty, self.target));
3237
3238 const result_ptr = try self.allocStack(union_ty);
3239 const payload = try self.resolveInst(extra.init);
3240 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
3241 assert(union_obj.haveFieldTypes());
3242 const field = union_obj.fields.values()[extra.field_index];
3243
3244 if (layout.tag_align >= layout.payload_align) {
3245 const payload_ptr = try self.buildPointerOffset(result_ptr, layout.tag_size, .new);
3246 try self.store(payload_ptr, payload, field.ty, 0);
3247 } else {
3248 try self.store(result_ptr, payload, field.ty, 0);
3249 }
3250
3251 return result_ptr;
3225}3252}
32263253
3227fn airPrefetch(self: *Self, inst: Air.Inst.Index) InnerError!WValue {3254fn airPrefetch(self: *Self, inst: Air.Inst.Index) InnerError!WValue {