2022-04-04 12:29:23 +02:00
|
|
|
# Copyright 2022 Mattia Giambirtone & All Contributors
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import ../frontend/meta/bytecode
|
|
|
|
import multibyte
|
|
|
|
|
|
|
|
|
|
|
|
import strformat
|
|
|
|
import strutils
|
|
|
|
import terminal
|
|
|
|
|
|
|
|
|
2022-05-24 22:26:45 +02:00
|
|
|
type
|
|
|
|
CFIElement = ref object
|
|
|
|
start, stop, bottom, argc: int
|
|
|
|
name: string
|
|
|
|
Debugger* = ref object
|
|
|
|
chunk: Chunk
|
|
|
|
cfiData: seq[CFIElement]
|
|
|
|
|
|
|
|
|
|
|
|
proc newDebugger*: Debugger =
|
|
|
|
## Initializes a new, empty
|
|
|
|
## debugger object
|
|
|
|
new(result)
|
|
|
|
result.cfiData = @[]
|
|
|
|
|
|
|
|
|
2022-04-04 12:29:23 +02:00
|
|
|
proc nl = stdout.write("\n")
|
|
|
|
|
|
|
|
|
|
|
|
proc printDebug(s: string, newline: bool = false) =
|
2022-05-20 18:34:14 +02:00
|
|
|
stdout.styledWrite(fgMagenta, "DEBUG - Disassembler -> ")
|
|
|
|
stdout.styledWrite(fgGreen, s)
|
2022-04-04 12:29:23 +02:00
|
|
|
if newline:
|
|
|
|
nl()
|
|
|
|
|
|
|
|
|
2022-05-23 10:49:38 +02:00
|
|
|
proc printName(opcode: OpCode, newline: bool = false) =
|
|
|
|
stdout.styledWrite(fgRed, $opcode, " (", fgYellow, $uint8(opcode), fgRed, ")")
|
2022-04-04 12:29:23 +02:00
|
|
|
if newline:
|
|
|
|
nl()
|
|
|
|
|
|
|
|
|
|
|
|
proc printInstruction(instruction: OpCode, newline: bool = false) =
|
|
|
|
printDebug("Instruction: ")
|
2022-05-23 10:49:38 +02:00
|
|
|
printName(instruction)
|
2022-04-04 12:29:23 +02:00
|
|
|
if newline:
|
|
|
|
nl()
|
|
|
|
|
|
|
|
|
2022-05-24 22:26:45 +02:00
|
|
|
proc checkFrame(self: Debugger, n: int) =
|
|
|
|
for i, e in self.cfiData:
|
|
|
|
if n == e.start:
|
|
|
|
styledEcho fgBlue, "==== Peon Bytecode Debugger - Begin Frame ", fgYellow, &"'{e.name}' ", fgBlue, "(", fgYellow, $i, fgBlue, ") ===="
|
|
|
|
styledEcho fgGreen, "\t- Start offset: ", fgYellow, $e.start
|
|
|
|
styledEcho fgGreen, "\t- End offset: ", fgYellow, $e.stop
|
|
|
|
styledEcho fgGreen, "\t- Stack bottom: ", fgYellow, $e.bottom
|
|
|
|
styledEcho fgGreen, "\t- Argument count: ", fgYellow, $e.argc
|
|
|
|
echo ""
|
|
|
|
elif n == e.stop:
|
|
|
|
styledEcho fgBlue, "==== Peon Bytecode Debugger - End Frame ", fgYellow, &"'{e.name}' ", fgBlue, "(", fgYellow, $i, fgBlue, ") ====\n"
|
|
|
|
|
|
|
|
|
|
|
|
proc simpleInstruction(self: Debugger, instruction: OpCode, offset: int): int =
|
2022-04-04 12:29:23 +02:00
|
|
|
printInstruction(instruction)
|
|
|
|
nl()
|
|
|
|
return offset + 1
|
|
|
|
|
|
|
|
|
2022-05-24 22:26:45 +02:00
|
|
|
proc stackTripleInstruction(self: Debugger, instruction: OpCode,
|
2022-05-18 13:32:32 +02:00
|
|
|
offset: int): int =
|
2022-04-04 12:29:23 +02:00
|
|
|
## Debugs instructions that operate on a single value on the stack using a 24-bit operand
|
2022-05-24 22:26:45 +02:00
|
|
|
var slot = [self.chunk.code[offset + 1], self.chunk.code[offset + 2], self.chunk.code[
|
2022-05-18 13:32:32 +02:00
|
|
|
offset + 3]].fromTriple()
|
2022-04-04 12:29:23 +02:00
|
|
|
printInstruction(instruction)
|
2022-05-20 18:34:14 +02:00
|
|
|
stdout.styledWrite(fgGreen, &", points to index ")
|
2022-05-23 10:49:38 +02:00
|
|
|
stdout.styledWriteLine(fgYellow, &"{slot}")
|
2022-04-04 12:29:23 +02:00
|
|
|
return offset + 4
|
|
|
|
|
|
|
|
|
2022-05-24 22:26:45 +02:00
|
|
|
proc stackDoubleInstruction(self: Debugger, instruction: OpCode,
|
2022-05-18 13:32:32 +02:00
|
|
|
offset: int): int =
|
2022-04-04 12:29:23 +02:00
|
|
|
## Debugs instructions that operate on a single value on the stack using a 16-bit operand
|
2022-05-24 22:26:45 +02:00
|
|
|
var slot = [self.chunk.code[offset + 1], self.chunk.code[offset + 2]].fromDouble()
|
2022-04-04 12:29:23 +02:00
|
|
|
printInstruction(instruction)
|
|
|
|
stdout.write(&", points to index ")
|
2022-05-20 18:34:14 +02:00
|
|
|
stdout.styledWrite(fgGreen, &", points to index ")
|
2022-05-23 10:49:38 +02:00
|
|
|
stdout.styledWriteLine(fgYellow, &"{slot}")
|
2022-04-04 12:29:23 +02:00
|
|
|
return offset + 3
|
|
|
|
|
|
|
|
|
2022-05-24 22:26:45 +02:00
|
|
|
proc argumentDoubleInstruction(self: Debugger, instruction: OpCode, offset: int): int =
|
2022-05-04 14:01:38 +02:00
|
|
|
## Debugs instructions that operate on a hardcoded value on the stack using a 16-bit operand
|
2022-05-24 22:26:45 +02:00
|
|
|
var slot = [self.chunk.code[offset + 1], self.chunk.code[offset + 2]].fromDouble()
|
2022-04-04 12:29:23 +02:00
|
|
|
printInstruction(instruction)
|
2022-05-22 13:09:46 +02:00
|
|
|
stdout.styledWrite(fgGreen, &", has argument ")
|
2022-05-23 10:49:38 +02:00
|
|
|
stdout.styledWriteLine(fgYellow, $slot)
|
2022-04-04 12:29:23 +02:00
|
|
|
return offset + 3
|
|
|
|
|
|
|
|
|
2022-05-24 22:26:45 +02:00
|
|
|
proc argumentTripleInstruction(self: Debugger, instruction: OpCode, offset: int): int =
|
2022-05-23 10:49:38 +02:00
|
|
|
## Debugs instructions that operate on a hardcoded value on the stack using a 24-bit operand
|
2022-05-24 22:26:45 +02:00
|
|
|
var slot = [self.chunk.code[offset + 1], self.chunk.code[offset + 2], self.chunk.code[offset + 3]].fromTriple()
|
2022-05-22 17:23:52 +02:00
|
|
|
printInstruction(instruction)
|
2022-05-23 10:49:38 +02:00
|
|
|
stdout.styledWrite(fgGreen, ", has argument ")
|
|
|
|
stdout.styledWriteLine(fgYellow, $slot)
|
2022-05-24 22:26:45 +02:00
|
|
|
return offset + 3
|
2022-05-22 17:23:52 +02:00
|
|
|
|
|
|
|
|
2022-05-24 22:26:45 +02:00
|
|
|
proc callInstruction(self: Debugger, instruction: OpCode, offset: int): int =
|
2022-05-23 10:49:38 +02:00
|
|
|
## Debugs function calls
|
2022-05-24 22:26:45 +02:00
|
|
|
var slot = [self.chunk.code[offset + 1], self.chunk.code[offset + 2], self.chunk.code[offset + 3]].fromTriple()
|
|
|
|
var args = [self.chunk.code[offset + 4], self.chunk.code[offset + 5], self.chunk.code[offset + 6]].fromTriple()
|
2022-05-23 10:49:38 +02:00
|
|
|
printInstruction(instruction)
|
|
|
|
stdout.styledWrite(fgGreen, &", jumps to address ", fgYellow, $slot, fgGreen, " with ", fgYellow, $args, fgGreen, " argument")
|
|
|
|
if args > 1:
|
|
|
|
stdout.styledWrite(fgYellow, "s")
|
|
|
|
nl()
|
|
|
|
return offset + 7
|
|
|
|
|
|
|
|
|
2022-05-24 22:26:45 +02:00
|
|
|
proc constantInstruction(self: Debugger, instruction: OpCode, offset: int): int =
|
2022-04-04 12:29:23 +02:00
|
|
|
## Debugs instructions that operate on the constant table
|
2022-05-24 22:26:45 +02:00
|
|
|
var constant = [self.chunk.code[offset + 1], self.chunk.code[offset + 2], self.chunk.code[
|
2022-05-18 13:32:32 +02:00
|
|
|
offset + 3]].fromTriple()
|
2022-04-04 12:29:23 +02:00
|
|
|
printInstruction(instruction)
|
2022-05-23 10:49:38 +02:00
|
|
|
stdout.styledWrite(fgGreen, &", points to constant at position ", fgYellow, $constant)
|
2022-04-04 12:29:23 +02:00
|
|
|
nl()
|
2022-05-18 13:32:32 +02:00
|
|
|
printDebug("Operand: ")
|
2022-05-24 22:26:45 +02:00
|
|
|
stdout.styledWriteLine(fgYellow, &"{self.chunk.consts[constant]}")
|
2022-04-04 12:29:23 +02:00
|
|
|
return offset + 4
|
|
|
|
|
|
|
|
|
2022-05-24 22:26:45 +02:00
|
|
|
proc jumpInstruction(self: Debugger, instruction: OpCode, offset: int): int =
|
2022-04-04 12:29:23 +02:00
|
|
|
## Debugs jumps
|
|
|
|
var jump: int
|
|
|
|
case instruction:
|
2022-05-16 19:40:13 +02:00
|
|
|
of Jump, JumpIfFalse, JumpIfTrue, JumpIfFalsePop, JumpForwards, JumpBackwards:
|
2022-05-24 22:26:45 +02:00
|
|
|
jump = [self.chunk.code[offset + 1], self.chunk.code[offset + 2]].fromDouble().int()
|
2022-05-18 13:32:32 +02:00
|
|
|
of LongJump, LongJumpIfFalse, LongJumpIfTrue, LongJumpIfFalsePop,
|
|
|
|
LongJumpForwards, LongJumpBackwards:
|
2022-05-24 22:26:45 +02:00
|
|
|
jump = [self.chunk.code[offset + 1], self.chunk.code[offset + 2], self.chunk.code[
|
2022-05-18 13:32:32 +02:00
|
|
|
offset + 3]].fromTriple().int()
|
2022-04-04 12:29:23 +02:00
|
|
|
else:
|
2022-05-18 13:32:32 +02:00
|
|
|
discard # Unreachable
|
2022-04-04 12:29:23 +02:00
|
|
|
printInstruction(instruction, true)
|
|
|
|
printDebug("Jump size: ")
|
2022-05-20 18:34:14 +02:00
|
|
|
stdout.styledWrite(fgYellow, $jump)
|
2022-04-04 12:29:23 +02:00
|
|
|
nl()
|
|
|
|
return offset + 3
|
|
|
|
|
|
|
|
|
2022-05-24 22:26:45 +02:00
|
|
|
proc disassembleInstruction*(self: Debugger, offset: int): int =
|
2022-04-04 12:29:23 +02:00
|
|
|
## Takes one bytecode instruction and prints it
|
2022-05-24 22:26:45 +02:00
|
|
|
self.checkFrame(offset)
|
2022-04-04 12:29:23 +02:00
|
|
|
printDebug("Offset: ")
|
2022-05-20 18:34:14 +02:00
|
|
|
stdout.styledWriteLine(fgYellow, $offset)
|
2022-04-04 12:29:23 +02:00
|
|
|
printDebug("Line: ")
|
2022-05-24 22:26:45 +02:00
|
|
|
stdout.styledWriteLine(fgYellow, &"{self.chunk.getLine(offset)}")
|
|
|
|
var opcode = OpCode(self.chunk.code[offset])
|
2022-04-04 12:29:23 +02:00
|
|
|
case opcode:
|
|
|
|
of simpleInstructions:
|
2022-05-24 22:26:45 +02:00
|
|
|
result = self.simpleInstruction(opcode, offset)
|
2022-04-04 12:29:23 +02:00
|
|
|
of constantInstructions:
|
2022-05-24 22:26:45 +02:00
|
|
|
result = self.constantInstruction(opcode, offset)
|
2022-04-04 12:29:23 +02:00
|
|
|
of stackDoubleInstructions:
|
2022-05-24 22:26:45 +02:00
|
|
|
result = self.stackDoubleInstruction(opcode, offset)
|
2022-04-04 12:29:23 +02:00
|
|
|
of stackTripleInstructions:
|
2022-05-24 22:26:45 +02:00
|
|
|
result = self.stackTripleInstruction(opcode, offset)
|
2022-04-04 12:29:23 +02:00
|
|
|
of argumentDoubleInstructions:
|
2022-05-24 22:26:45 +02:00
|
|
|
result = self.argumentDoubleInstruction(opcode, offset)
|
2022-05-22 17:23:52 +02:00
|
|
|
of argumentTripleInstructions:
|
2022-05-24 22:26:45 +02:00
|
|
|
result = self.argumentTripleInstruction(opcode, offset)
|
2022-05-23 10:49:38 +02:00
|
|
|
of callInstructions:
|
2022-05-24 22:26:45 +02:00
|
|
|
result = self.callInstruction(opcode, offset)
|
2022-04-04 12:29:23 +02:00
|
|
|
of jumpInstructions:
|
2022-05-24 22:26:45 +02:00
|
|
|
result = self.jumpInstruction(opcode, offset)
|
2022-04-04 12:29:23 +02:00
|
|
|
else:
|
|
|
|
echo &"DEBUG - Unknown opcode {opcode} at index {offset}"
|
|
|
|
result = offset + 1
|
|
|
|
|
|
|
|
|
2022-05-24 22:26:45 +02:00
|
|
|
proc parseCFIData(self: Debugger) =
|
|
|
|
var
|
|
|
|
start, stop, bottom, argc: int
|
|
|
|
fnName: string
|
|
|
|
idx = 0
|
|
|
|
size = 0
|
|
|
|
while idx < len(self.chunk.cfi):
|
|
|
|
start = int([self.chunk.cfi[idx], self.chunk.cfi[idx + 1], self.chunk.cfi[idx + 2]].fromTriple())
|
|
|
|
idx += 3
|
|
|
|
stop = int([self.chunk.cfi[idx], self.chunk.cfi[idx + 1], self.chunk.cfi[idx + 2]].fromTriple())
|
|
|
|
idx += 3
|
|
|
|
bottom = int([self.chunk.cfi[idx], self.chunk.cfi[idx + 1], self.chunk.cfi[idx + 2]].fromTriple())
|
|
|
|
idx += 3
|
|
|
|
argc = int(self.chunk.cfi[idx])
|
|
|
|
inc(idx)
|
|
|
|
size = int([self.chunk.cfi[idx], self.chunk.cfi[idx + 1]].fromDouble())
|
|
|
|
idx += 2
|
|
|
|
fnName = self.chunk.cfi[idx..<idx + size].fromBytes()
|
|
|
|
inc(idx, size)
|
|
|
|
self.cfiData.add(CFIElement(start: start, stop: stop, bottom: bottom, argc: argc, name: fnName))
|
|
|
|
|
|
|
|
|
|
|
|
proc disassembleChunk*(self: Debugger, chunk: Chunk, name: string) =
|
|
|
|
## Takes a chunk of bytecode and prints it
|
|
|
|
self.chunk = chunk
|
|
|
|
styledEcho fgBlue, &"==== Peon Bytecode Debugger - Chunk '{name}' ====\n"
|
2022-04-04 12:29:23 +02:00
|
|
|
var index = 0
|
2022-05-24 22:26:45 +02:00
|
|
|
self.parseCFIData()
|
|
|
|
while index < self.chunk.code.len:
|
|
|
|
index = self.disassembleInstruction(index)
|
2022-04-04 12:29:23 +02:00
|
|
|
echo ""
|
2022-05-24 22:26:45 +02:00
|
|
|
styledEcho fgBlue, &"==== Debug session ended - Chunk '{name}' ===="
|
2022-04-04 12:29:23 +02:00
|
|
|
|
|
|
|
|