Fixed many bugs in the serializer and added (de)serialization output to main.nim

This commit is contained in:
Nocturn9x 2021-11-12 17:47:11 +01:00
parent afa4369321
commit 5cc2a60c8c
3 changed files with 30 additions and 19 deletions

View File

@ -43,6 +43,10 @@ type
chunk*: Chunk
proc `$`*(self: Serialized): string =
result = &"Serialized(fileHash={self.fileHash}, version={self.japlVer.major}.{self.japlVer.minor}.{self.japlVer.patch}, branch={self.japlBranch}), commitHash={self.commitHash}, date={self.compileDate}, chunk={self.chunk[]}"
proc error(self: Serializer, message: string) =
## Raises a formatted SerializationError exception
raise newException(SerializationError, &"A fatal error occurred while serializing '{self.filename}' -> {message}")
@ -74,7 +78,7 @@ proc bytesToString(self: Serializer, input: seq[byte]): string =
result.add(char(b))
proc bytesToInt(self: Serializer, input: seq[byte]): int =
proc bytesToInt(self: Serializer, input: array[8, byte]): int =
copyMem(result.addr, input.unsafeAddr, sizeof(int))
@ -147,18 +151,18 @@ proc loadBytes*(self: Serializer, stream: seq[byte]): Serialized =
try:
if stream[0..<len(BYTECODE_MARKER)] != self.toBytes(BYTECODE_MARKER):
self.error("malformed bytecode marker")
stream = stream[len(BYTECODE_MARKER) - 1..^1]
stream = stream[len(BYTECODE_MARKER)..^1]
result.japlVer = (major: int(stream[0]), minor: int(stream[1]), patch: int(stream[2]))
stream = stream[2..^1]
stream = stream[3..^1]
let branchLength = stream[0]
stream = stream[1..^1]
result.japlBranch = self.bytesToString(stream[0..<branchLength])
stream = stream[branchLength..^1]
result.commitHash = self.bytesToString(stream[0..<40]).toHex()
stream = stream[39..^1]
result.compileDate = self.bytesToInt(stream[0..7])
stream = stream[7..^1]
result.fileHash = self.bytesToString(stream[0..<32]).toHex()
result.commitHash = self.bytesToString(stream[0..<40]).toLowerAscii()
stream = stream[40..^1]
result.compileDate = self.bytesToInt([stream[0], stream[1], stream[2], stream[3], stream[4], stream[5], stream[6], stream[7]])
stream = stream[8..^1]
result.fileHash = self.bytesToString(stream[0..<32]).toHex().toLowerAscii()
except IndexDefect:
self.error("truncated bytecode file")

View File

@ -21,6 +21,9 @@ import backend/serializer
import strformat
import strutils
import sequtils
import times
import nimSHA2
proc hook() {.noconv.} =
@ -34,18 +37,15 @@ proc main() =
var tree: seq[ASTNode]
var optimized: tuple[tree: seq[ASTNode], warnings: seq[Warning]]
var compiled: Chunk
var serializedBytes: seq[byte]
var serialized: Serialized
var serializedRaw: seq[byte]
var lexer = initLexer()
var parser = initParser()
var optimizer = initOptimizer(foldConstants=false)
var compiler = initCompiler()
var serializer = initSerializer()
var japlBranch = ""
var japlVersion = ""
var japlCommitHash = ""
var fileHash = ""
var compileDate = 0
var hashMatches: bool
echo "NimVM REPL\n"
while true:
try:
@ -97,10 +97,17 @@ proc main() =
disassembleChunk(compiled, filename)
echo ""
serializedBytes = serializer.dumpBytes(compiled, source, filename)
echo "(De)Serialization step:"
echo "\t"
echo &"\tRaw byte stream: [{serializedBytes.join(\", \")}]"
serializedRaw = serializer.dumpBytes(compiled, source, filename)
echo "Serialization step: "
echo &"\tRaw hex output: {serializedRaw.mapIt(toHex(it)).join(\"\").toLowerAscii()}"
echo ""
serialized = serializer.loadBytes(serializedRaw)
hashMatches = if computeSHA256(source).toHex().toLowerAscii() == serialized.fileHash: true else: false
echo "Deserialization step:"
echo &"\t\t- File hash: {serialized.fileHash} (matches: {hashMatches})"
echo &"\t\t- JAPL version: {serialized.japlVer.major}.{serialized.japlVer.minor}.{serialized.japlVer.patch} (commit {serialized.commitHash[0..7]} on branch {serialized.japlBranch})"
echo &"\t\t- Compilation date & time: {fromUnix(serialized.compileDate).format(\"d/M/yyyy H:m:s\")}"
except:
echo &"A Nim runtime exception occurred: {getCurrentExceptionMsg()}"
continue

View File

@ -32,7 +32,7 @@ proc nl =
proc simpleInstruction(name: string, offset: int): int =
write stdout, &"DEBUG - Disassembler->\tInstruction :"
write stdout, &"DEBUG - Disassembler->\tInstruction: "
printName(name)
nl()
return offset + 1