Fixed issue with resuming from incremental compilation

This commit is contained in:
Mattia Giambirtone 2023-05-01 16:27:00 +02:00
parent dc195409c9
commit de9b51152e
Signed by: nocturn9x
GPG Key ID: 8270F9F467971E59
4 changed files with 15 additions and 11 deletions

View File

@ -220,7 +220,7 @@ proc markRoots(self: var PeonVM): HashSet[ptr HeapObject] =
result.incl(obj)
for obj in self.operands:
if obj in self.gc.pointers:
result.incl(obj)
result.incl(obj)
var obj: ptr HeapObject
for p in result:
obj = cast[ptr HeapObject](p)
@ -1063,14 +1063,13 @@ proc run*(self: var PeonVM, chunk: Chunk, breakpoints: seq[uint64] = @[], repl:
proc resume*(self: var PeonVM, chunk: Chunk) =
## Resumes execution of the given chunk (which
## may have changed since the last call to run()).
## The instruction pointer is set to address 12,
## as it is assumed that the compiler has produced
## new code in the chunk, replacing the previous one.
## No other state mutation occurs and all stacks as
## well as other metadata are left intact
## well as other metadata are left intact. This should
## not be used directly unless you know what you're
## doing, as incremental compilation support is very
## experimental and highly unstable
self.chunk = chunk
try:
self.ip = 12
self.chunk = chunk
self.dispatch()
except NilAccessDefect:
stderr.writeLine("Memory Access Violation: SIGSEGV")

View File

@ -1981,12 +1981,12 @@ proc funDecl(self: BytecodeCompiler, node: FunDecl, name: Name) =
self.patchJump(jump)
self.endScope()
# Terminates the function's context
let stop = self.chunk.code.len().toTriple()
self.emitByte(OpCode.Return, self.peek().token.line)
if hasVal:
self.emitByte(1, self.peek().token.line)
else:
self.emitByte(0, self.peek().token.line)
let stop = self.chunk.code.len().toTriple()
self.chunk.functions[idx] = stop[0]
self.chunk.functions[idx + 1] = stop[1]
self.chunk.functions[idx + 2] = stop[2]

View File

@ -58,7 +58,7 @@ proc repl(warnings: seq[WarningKind] = @[], mismatches: bool = false, mode: Comp
tokens: seq[Token] = @[]
tree: seq[Declaration] = @[]
compiler = newBytecodeCompiler(replMode=true)
compiled: Chunk
compiled: Chunk = newChunk()
serialized: Serialized
tokenizer = newLexer()
vm = newPeonVM()
@ -104,7 +104,7 @@ proc repl(warnings: seq[WarningKind] = @[], mismatches: bool = false, mode: Comp
for node in tree:
styledEcho fgGreen, "\t", $node
echo ""
compiled = compiler.compile(tree, "stdin", tokenizer.getLines(), input, showMismatches=mismatches, disabledWarnings=warnings, mode=mode)
compiled = compiler.compile(tree, "stdin", tokenizer.getLines(), input, chunk=compiled, showMismatches=mismatches, disabledWarnings=warnings, mode=mode, incremental=true)
when debugCompiler:
styledEcho fgCyan, "Compilation step:\n"
debugger.disassembleChunk(compiled, "stdin")

View File

@ -16,4 +16,9 @@ export comparisons;
var version* = 1;
var _private = 5; # Invisible outside the module (underscore is to silence warning)
var test* = 0x60;
var test* = 0x60;
fn testGlobals*: bool {
return version == 1 and _private == 5 and test == 0x60;
}