From 11c8c0a5ab1cae830638ed970d428aa131b808c1 Mon Sep 17 00:00:00 2001 From: Mattia Giambirtone Date: Sat, 8 Oct 2022 09:18:35 +0200 Subject: [PATCH] Definitive fix for closures --- src/backend/vm.nim | 2 +- src/frontend/compiler.nim | 36 ++++++++++++++++++------------------ tests/closures.pn | 1 + 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/backend/vm.nim b/src/backend/vm.nim index b105d4d..94829e6 100644 --- a/src/backend/vm.nim +++ b/src/backend/vm.nim @@ -31,7 +31,7 @@ when debugVM or debugMem or debugGC: import std/terminal -{.push checks:off.} # The VM is a critical point where checks are deleterious +{.push checks:on.} # The VM is a critical point where checks are deleterious type PeonVM* = ref object diff --git a/src/frontend/compiler.nim b/src/frontend/compiler.nim index ee66d99..ef517ac 100644 --- a/src/frontend/compiler.nim +++ b/src/frontend/compiler.nim @@ -188,7 +188,7 @@ type # Stores the position of all jumps jumps: seq[tuple[patched: bool, offset: int]] # List of CFI start offsets into our CFI data - cfiOffsets: seq[tuple[value, offset: int, fn: Name]] + cfiOffsets: seq[tuple[start, stop: int, fn: Name]] CompileError* = ref object of PeonException compiler*: Compiler node*: ASTNode @@ -416,18 +416,20 @@ proc fixCFIOffsets(self: Compiler, oldLen: int, modifiedAt: int) = let offset = self.chunk.code.len() - oldLen var newCFI: array[3, uint8] var tmp: int - for cfi in self.cfiOffsets: - if cfi.offset >= modifiedAt: - newCFI = (cfi.value + offset).toTriple() - self.chunk.cfi[cfi.offset] = newCFI[0] - self.chunk.cfi[cfi.offset + 1] = newCFI[1] - self.chunk.cfi[cfi.offset + 2] = newCFI[2] - tmp = [self.chunk.cfi[cfi.offset + 3], self.chunk.cfi[cfi.offset + 4], self.chunk.cfi[cfi.offset + 5]].fromTriple().int + for cfi in self.cfiOffsets.mitems(): + if cfi.start >= modifiedAt: + newCFI = (cfi.start + offset).toTriple() + self.chunk.cfi[cfi.start] = newCFI[0] + self.chunk.cfi[cfi.start + 1] = newCFI[1] + self.chunk.cfi[cfi.start + 2] = newCFI[2] + tmp = [self.chunk.cfi[cfi.start + 3], self.chunk.cfi[cfi.start + 4], self.chunk.cfi[cfi.start + 5]].fromTriple().int newCFI = (tmp + offset).toTriple() - self.chunk.cfi[cfi.offset + 3] = newCFI[0] - self.chunk.cfi[cfi.offset + 4] = newCFI[1] - self.chunk.cfi[cfi.offset + 5] = newCFI[2] + self.chunk.cfi[cfi.start + 3] = newCFI[0] + self.chunk.cfi[cfi.start + 4] = newCFI[1] + self.chunk.cfi[cfi.start + 5] = newCFI[2] cfi.fn.codePos += offset + cfi.start += offset + cfi.stop += offset proc fixJumps(self: Compiler, oldLen: int, modifiedAt: int) = @@ -444,8 +446,7 @@ proc fixJumps(self: Compiler, oldLen: int, modifiedAt: int) = # list in cases where we shifted the jump # instruction itself into the code! jump.offset += offset - if jump.patched: - self.setJump(jump.offset, self.chunk.code[jump.offset..