zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 69626478626aee18953b2fbfa42d509c7dfff1e4 (tree)
parent ac5fd47e2ee7c43a3ad7e5d0275d4e745152a9a3
Author: Luuk de Gram <Luukdegram@users.noreply.github.com>
Date:   Mon, 17 May 2021 19:44:14 +0200

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

Diffstat:
Msrc/codegen/wasm.zig | 12++++++------
Mtest/stage2/wasm.zig | 22++++++++++++++++++++++
2 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/src/codegen/wasm.zig b/src/codegen/wasm.zig @@ -795,11 +795,7 @@ pub const Context = struct { fn genAlloc(self: *Context, inst: *Inst.NoOp) InnerError!WValue { const elem_type = inst.base.ty.elemType(); - const valtype = try self.genValtype(inst.base.src, elem_type); - try self.locals.append(self.gpa, valtype); - const local_value = WValue{ .local = self.local_index }; - self.local_index += 1; switch (elem_type.zigTypeTag()) { .Struct => { @@ -816,7 +812,11 @@ pub const Context = struct { } }, // TODO: Add more types that require extra locals such as optionals - else => {}, + else => { + const valtype = try self.genValtype(inst.base.src, elem_type); + try self.locals.append(self.gpa, valtype); + self.local_index += 1; + }, } return local_value; @@ -1115,6 +1115,6 @@ pub const Context = struct { fn genStructFieldPtr(self: *Context, inst: *Inst.StructFieldPtr) InnerError!WValue { const struct_ptr = self.resolveInst(inst.struct_ptr); - return WValue{ .local = struct_ptr.local + @intCast(u32, inst.field_index) + 1 }; + return WValue{ .local = struct_ptr.local + @intCast(u32, inst.field_index) }; } }; diff --git a/test/stage2/wasm.zig b/test/stage2/wasm.zig @@ -419,4 +419,26 @@ pub fn addCases(ctx: *TestContext) !void { \\} , "2\n"); } + + { + var case = ctx.exe("wasm structs", wasi); + + case.addCompareOutput( + \\const Example = struct { x: u32 }; + \\ + \\export fn _start() u32 { + \\ var example: Example = .{ .x = 5 }; + \\ return example.x; + \\} + , "5\n"); + + case.addCompareOutput( + \\const Example = struct { x: u32, y: u32 }; + \\ + \\export fn _start() u32 { + \\ var example: Example = .{ .x = 5, .y = 10 }; + \\ return example.y + example.x; + \\} + , "15\n"); + } }