Made function jumps absolute

This commit is contained in:
Mattia Giambirtone 2022-05-16 19:31:17 +02:00
parent 620c56237a
commit ffee77b6fc
3 changed files with 7 additions and 8 deletions

View File

@ -271,7 +271,6 @@ proc patchJump(self: Compiler, offset: int) =
if jump > 16777215:
self.error("cannot jump more than 16777216 bytecode instructions")
if jump < uint16.high().int:
jump -= 3
case OpCode(self.chunk.code[offset]):
of LongJumpForwards:
self.chunk.code[offset] = JumpForwards.uint8()
@ -290,7 +289,6 @@ proc patchJump(self: Compiler, offset: int) =
self.chunk.code[offset + 1] = offsetArray[0]
self.chunk.code[offset + 2] = offsetArray[1]
else:
jump -= 4
case OpCode(self.chunk.code[offset]):
of JumpForwards:
self.chunk.code[offset] = LongJumpForwards.uint8()
@ -773,7 +771,7 @@ proc declareName(self: Compiler, node: Declaration) =
isLet: false))
self.names[^1].valueType = self.inferType(argument.valueType)
self.names[^1].valueType.node = argument.name
self.emitByte(StoreVar)
self.emitByte(LoadVar)
self.emitBytes(self.names.high().toTriple())
else:
discard # Unreachable
@ -1181,6 +1179,10 @@ proc varDecl(self: Compiler, node: VarDecl) =
proc funDecl(self: Compiler, node: FunDecl) =
## Compiles function declarations
# A function's code is just compiled linearly
# and then jumped over
let jmp = self.emitJump(Jump)
self.declareName(node)
if node.body != nil:
if BlockStmt(node.body).code.len() == 0:
@ -1199,9 +1201,6 @@ proc funDecl(self: Compiler, node: FunDecl) =
# We store the current function
var function = self.currentFunction
self.currentFunction = node
# A function's code is just compiled linearly
# and then jumped over
let jmp = self.emitJump(JumpForwards)
# Since the deferred array is a linear
# sequence of instructions and we want

View File

@ -159,7 +159,7 @@ const stackDoubleInstructions* = {}
const argumentDoubleInstructions* = {PopN, }
# Jump instructions jump at relative or absolute bytecode offsets
const jumpInstructions* = {JumpIfFalse, JumpIfFalsePop,
const jumpInstructions* = {Jump, LongJump, JumpIfFalse, JumpIfFalsePop,
JumpForwards, JumpBackwards,
LongJumpIfFalse, LongJumpIfFalsePop,
LongJumpForwards, LongJumpBackwards,

View File

@ -24,7 +24,7 @@ proc getLineEditor: LineEditor
# Handy dandy compile-time constants
const debugLexer = false
const debugParser = false
const debugParser = true
const debugCompiler = true
const debugSerializer = false
const debugRuntime = false