Commit 6962647862

Luuk de Gram <Luukdegram@users.noreply.github.com>
2021-05-17 19:44:14
Do not create a local for the struct itself + test cases
1 parent ac5fd47
Changed files (2)
src
codegen
test
stage2
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) };
     }
 };
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");
+    }
 }