authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2021-05-17 19:44:14+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2021-05-19 10:37:44+02:00
log69626478626aee18953b2fbfa42d509c7dfff1e4
treef9c61fb252f6f1803eb9e8b79f84a9411ed6fd23
parentac5fd47e2ee7c43a3ad7e5d0275d4e745152a9a3
signaturelock-open Commit is signed but in an unrecognized format.

Do not create a local for the struct itself + test cases


2 files changed, 28 insertions(+), 6 deletions(-)

src/codegen/wasm.zig+6-6
......@@ -795,11 +795,7 @@ pub const Context = struct {
795795
796796 fn genAlloc(self: *Context, inst: *Inst.NoOp) InnerError!WValue {
797797 const elem_type = inst.base.ty.elemType();
798 const valtype = try self.genValtype(inst.base.src, elem_type);
799 try self.locals.append(self.gpa, valtype);
800
801798 const local_value = WValue{ .local = self.local_index };
802 self.local_index += 1;
803799
804800 switch (elem_type.zigTypeTag()) {
805801 .Struct => {
......@@ -816,7 +812,11 @@ pub const Context = struct {
816812 }
817813 },
818814 // TODO: Add more types that require extra locals such as optionals
819 else => {},
815 else => {
816 const valtype = try self.genValtype(inst.base.src, elem_type);
817 try self.locals.append(self.gpa, valtype);
818 self.local_index += 1;
819 },
820820 }
821821
822822 return local_value;
......@@ -1115,6 +1115,6 @@ pub const Context = struct {
11151115 fn genStructFieldPtr(self: *Context, inst: *Inst.StructFieldPtr) InnerError!WValue {
11161116 const struct_ptr = self.resolveInst(inst.struct_ptr);
11171117
1118 return WValue{ .local = struct_ptr.local + @intCast(u32, inst.field_index) + 1 };
1118 return WValue{ .local = struct_ptr.local + @intCast(u32, inst.field_index) };
11191119 }
11201120};
test/stage2/wasm.zig+22
......@@ -419,4 +419,26 @@ pub fn addCases(ctx: *TestContext) !void {
419419 \\}
420420 , "2\n");
421421 }
422
423 {
424 var case = ctx.exe("wasm structs", wasi);
425
426 case.addCompareOutput(
427 \\const Example = struct { x: u32 };
428 \\
429 \\export fn _start() u32 {
430 \\ var example: Example = .{ .x = 5 };
431 \\ return example.x;
432 \\}
433 , "5\n");
434
435 case.addCompareOutput(
436 \\const Example = struct { x: u32, y: u32 };
437 \\
438 \\export fn _start() u32 {
439 \\ var example: Example = .{ .x = 5, .y = 10 };
440 \\ return example.y + example.x;
441 \\}
442 , "15\n");
443 }
422444}