From 81e10ae1ea374ce17d475fe68e254764375dabe8 Mon Sep 17 00:00:00 2001 From: Mattia Giambirtone Date: Thu, 26 Jan 2023 12:11:29 +0100 Subject: [PATCH] Reformatted help menu, added -w option. All code can now be disassembled --- src/config.nim | 13 ++++---- .../compiler/targets/bytecode/target.nim | 32 +++++++++---------- src/main.nim | 15 ++++++--- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/config.nim b/src/config.nim index b8c0958..7dc8ac3 100644 --- a/src/config.nim +++ b/src/config.nim @@ -53,22 +53,21 @@ $ peon file.pbc Run the given Peon bytecode file Options ------- --h, --help Show this help text and exits --v, --version Print the current peon version and exits +-h, --help Show this help text and exit +-v, --version Print the current peon version and exit -s, --string Execute the passed string as if it was a file -n, --noDump Don't dump the result of compilation to a file. Note that no dump is created when using -s/--string -b, --breakpoints Run the debugger at specific bytecode offsets (comma-separated). Only available with --backend:bytecode and when compiled with VM debugging on --d, --disassemble Disassemble the given bytecode file instead of executing it - (only makes sense with --backend:bytecode) +-d, --disassemble Disassemble the output of compilation (only makes sense with --backend:bytecode) -m, --mode Set the compilation mode. Acceptable values are 'debug' and 'release'. Defaults to 'debug' --c, --compile Compile the code, but do not execute it ---warnings Turn warnings on or off (default: on). Acceptable values are +-c, --compile Compile the code, but do not execute it. Useful along with -d +-w, --warnings Turn warnings on or off (default: on). Acceptable values are yes/on and no/off ---noWarn Disable a specific warning (for example, --noWarn unusedVariable) +--noWarn Disable a specific warning (for example, --noWarn:unusedVariable) --showMismatches Show all mismatches when function dispatching fails (output is really verbose) --backend Select the compilation backend (valid values are: 'c', 'cpp' and 'bytecode'). Note that the REPL always uses the bytecode target. Defaults to 'bytecode' diff --git a/src/frontend/compiler/targets/bytecode/target.nim b/src/frontend/compiler/targets/bytecode/target.nim index c232058..fffb03d 100644 --- a/src/frontend/compiler/targets/bytecode/target.nim +++ b/src/frontend/compiler/targets/bytecode/target.nim @@ -274,23 +274,6 @@ proc setJump(self: BytecodeCompiler, offset: int, jmp: seq[uint8]) = self.chunk.code[offset + 1] = jmp[0] self.chunk.code[offset + 2] = jmp[1] self.chunk.code[offset + 3] = jmp[2] - - -proc patchJump(self: BytecodeCompiler, offset: int) = - ## Patches a previously emitted relative - ## jump using emitJump - var jump: int = self.chunk.code.len() - self.jumps[offset].offset - if jump < 0: - self.error("jump size cannot be negative (This is an internal error and most likely a bug)") - if jump > 16777215: - # TODO: Emit consecutive jumps using insertAt - self.error("cannot jump more than 16777215 instructions") - if jump > 0: - self.setJump(self.jumps[offset].offset, (jump - 4).toTriple()) - self.jumps[offset].patched = true - else: - # TODO: Discard jump of size 0 - discard proc emitJump(self: BytecodeCompiler, opcode: OpCode, line: int): int = @@ -401,6 +384,21 @@ proc insertAt(self: BytecodeCompiler, where: int, opcode: OpCode, data: openarra self.fixFunctionOffsets(oldLen, where) + +proc patchJump(self: BytecodeCompiler, offset: int) = + ## Patches a previously emitted relative + ## jump using emitJump + var jump: int = self.chunk.code.len() - self.jumps[offset].offset + if jump < 0: + self.error("jump size cannot be negative (This is an internal error and most likely a bug)") + if jump > 16777215: + # TODO: Emit consecutive jumps using insertAt + self.error("cannot jump more than 16777215 instructions") + if jump > 0: + self.setJump(self.jumps[offset].offset, (jump - 4).toTriple()) + self.jumps[offset].patched = true + + proc handleBuiltinFunction(self: BytecodeCompiler, fn: Type, args: seq[Expression], line: int) = ## Emits instructions for builtin functions ## such as addition or subtraction diff --git a/src/main.nim b/src/main.nim index f0cd4bc..b781613 100644 --- a/src/main.nim +++ b/src/main.nim @@ -179,9 +179,6 @@ proc runFile(f: string, fromString: bool = false, dump: bool = true, breakpoints if not fromString: if not f.endsWith(".pn") and not f.endsWith(".pbc"): f &= ".pn" - if backend == PeonBackend.Bytecode and dis: - debugger.disassembleChunk(serializer.loadFile(f).chunk, f) - return input = readFile(f) else: input = f @@ -212,7 +209,8 @@ proc runFile(f: string, fromString: bool = false, dump: bool = true, breakpoints when debugCompiler: styledEcho fgCyan, "Compilation step:\n" debugger.disassembleChunk(compiled, f) - echo "" + if dis: + debugger.disassembleChunk(compiled, f) var path = splitFile(if output.len() > 0: output else: f).dir if path.len() > 0: path &= "/" @@ -384,6 +382,15 @@ when isMainModule: fromString = true of "n": dump = false + of "w": + if value.toLowerAscii() in ["yes", "on"]: + warnings = @[] + elif value.toLowerAscii() in ["no", "off"]: + for warning in WarningKind: + warnings.add(warning) + else: + stderr.styledWriteLine(fgRed, styleBright, "Error: ", fgDefault, "invalid value for option 'w' (valid options are: yes, on, no, off)") + quit() of "b": when not debugVM: stderr.styledWriteLine(fgRed, styleBright, "Error: ", fgDefault, "cannot set breakpoints in release mode")