Skip to content

Commit

Permalink
Fix return expr resetting temp locals.
Browse files Browse the repository at this point in the history
  • Loading branch information
fubark committed Jan 31, 2023
1 parent bcc6bb8 commit 3343587
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/bytecode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ pub const ByteCodeBuffer = struct {
fmt.printStderr("{} {} startLocal={}, numArgs={}, numRet={}, symId={}\n", &.{v(pcOffset), v(code), v(startLocal), v(numArgs), v(numRet), v(symId)});
},
.jumpNotCond => {
const jump = @ptrCast(*const align(1) u16, &(pc + 1)).*;
const jump = @ptrCast(*const align(1) u16, pc + 1).*;
fmt.printStderr("{} {} offset={}, cond={}\n", &.{v(pcOffset), v(code), v(jump), v(pc[3].arg)});
},
else => {
Expand Down
3 changes: 1 addition & 2 deletions src/codegen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const sema = cy.sema;
const CompileChunk = cy.CompileChunk;
const fmt = @import("fmt.zig");
const v = fmt.v;
const log = stdx.log.scoped(.codegen);

const LocalId = u8;

Expand Down Expand Up @@ -1581,8 +1582,6 @@ fn genStatement(self: *CompileChunk, nodeId: cy.NodeId, comptime discardTopExprR
.return_expr_stmt => {
const lastArcStart = self.beginArcExpr();

self.setFirstFreeTempLocal(@intCast(u8, self.curBlock.numLocals));

if (self.blocks.items.len == 1) {
var val: GenValue = undefined;
if (self.curBlock.resolvedFuncSymId != cy.NullId) {
Expand Down
7 changes: 7 additions & 0 deletions src/vm_compiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,12 @@ pub const CompileChunk = struct {
self.prevSemaSubBlock();
}

pub fn reserveIfTempLocal(self: *CompileChunk, local: LocalId) !void {
if (self.isTempLocal(local)) {
try self.setReservedTempLocal(local);
}
}

pub fn setReservedTempLocal(self: *CompileChunk, local: LocalId) !void {
// log.debug("set reserved {}", .{self.reservedTempLocalStack.items.len});
try self.reservedTempLocalStack.append(self.alloc, .{
Expand Down Expand Up @@ -613,6 +619,7 @@ pub const CompileChunk = struct {
}

/// TODO: Rename to reserveNextTempLocal.
/// Assumes that if `firstFreeTempLocal` is in bounds, it is free.
pub fn nextFreeTempLocal(self: *CompileChunk) !LocalId {
if (self.curBlock.firstFreeTempLocal < 256) {
if (self.curBlock.firstFreeTempLocal == self.curBlock.numLocals + self.curBlock.numTempLocals) {
Expand Down
13 changes: 12 additions & 1 deletion test/for_iter_test.cy
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,15 @@ try t.eq(sum, 5)
list = [1, 2, 3]
sum = 0
for list each it: sum += it
try t.eq(sum, 6)
try t.eq(sum, 6)

-- Return expr inside loop body.
list = [1, 2, 3]
f = func (arr):
for arr each item:
if item == 4:
return 1
else item == 5:
return 1
return 0
try t.eq(f(list), 0)

0 comments on commit 3343587

Please sign in to comment.