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

This commit is contained in:
Luuk de Gram 2021-05-17 19:44:14 +02:00
parent ac5fd47e2e
commit 6962647862
No known key found for this signature in database
GPG Key ID: A002B174963DBB7D
2 changed files with 28 additions and 6 deletions

View File

@ -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) };
}
};

View File

@ -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");
}
}