-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from FluffyLabs/debug
Debugger WIP
- Loading branch information
Showing
37 changed files
with
193 additions
and
2,555 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { InitialState } from "@/types/pvm"; | ||
import { Pvm } from "../../../node_modules/typeberry/packages/pvm/pvm"; | ||
import { Status } from "../../../node_modules/typeberry/packages/pvm/status"; | ||
|
||
// The only files we need from PVM repo: | ||
import { ProgramDecoder } from "../../pvm-packages/pvm/program-decoder/program-decoder"; | ||
import { ArgsDecoder } from "../../pvm-packages/pvm/args-decoder/args-decoder"; | ||
import { byteToOpCodeMap } from "../../pvm-packages/pvm/assemblify"; | ||
|
||
export const initPvm = (program: number[], initialState: InitialState) => { | ||
const pvm = new Pvm(new Uint8Array(program), initialState); | ||
|
||
return pvm; | ||
}; | ||
|
||
export const runAllInstructions = (pvm: Pvm, program: number[]) => { | ||
const programPreviewResult = []; | ||
|
||
do { | ||
const result = nextInstruction(pvm, program); | ||
programPreviewResult.push(result); | ||
} while (pvm.nextStep() === Status.OK); | ||
|
||
return { | ||
programRunResult: { | ||
pc: pvm.getPC(), | ||
regs: Array.from(pvm.getRegisters()), | ||
gas: pvm.getGas(), | ||
pageMap: pvm.getMemory(), | ||
memory: pvm.getMemory(), | ||
status: pvm.getStatus(), | ||
}, | ||
programPreviewResult, | ||
}; | ||
}; | ||
|
||
export type CurrentInstruction = | ||
| { | ||
args: any; | ||
name: string; | ||
gas: number; | ||
instructionCode: number; | ||
} | ||
| { | ||
error: string; | ||
name: string; | ||
gas: number; | ||
instructionCode: number; | ||
}; | ||
export const nextInstruction = (pvm: Pvm, program: number[]) => { | ||
const programDecoder = new ProgramDecoder(new Uint8Array(program)); | ||
const code = programDecoder.getCode(); | ||
const mask = programDecoder.getMask(); | ||
const argsDecoder = new ArgsDecoder(code, mask); | ||
const currentInstruction = code[pvm.getPC()]; | ||
|
||
let args; | ||
|
||
try { | ||
args = argsDecoder.getArgs(pvm.getPC()) as any; | ||
|
||
const currentInstructionDebug = { | ||
instructionCode: currentInstruction, | ||
...byteToOpCodeMap[currentInstruction], | ||
args: { | ||
...args, | ||
immediate: args.immediateDecoder?.getUnsigned(), | ||
}, | ||
}; | ||
return currentInstructionDebug; | ||
} catch (e) { | ||
// The last iteration goes here since there's no instruction to proces and we didn't check if there's a next operation | ||
return { instructionCode: currentInstruction, ...byteToOpCodeMap[currentInstruction], error: "Cannot get arguments from args decoder" }; | ||
// newProgramPreviewResult.push({ instructionCode: currentInstruction, ...byteToOpCodeMap[currentInstruction], error: "Cannot get arguments from args decoder" }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { ArgsDecoder } from "./args-decoder/args-decoder"; | ||
import { byteToOpCodeMap } from "./assemblify"; | ||
import { ProgramDecoder } from "./program-decoder/program-decoder"; | ||
|
||
export function disassemblify(rawProgram: Uint8Array) { | ||
const programDecoder = new ProgramDecoder(rawProgram); | ||
const code = programDecoder.getCode(); | ||
const mask = programDecoder.getMask(); | ||
const argsDecoder = new ArgsDecoder(code, mask); | ||
let i = 0; | ||
const printableProgram = []; | ||
|
||
while (i < code.length) { | ||
const currentInstruction = code[i]; | ||
let args; | ||
|
||
try { | ||
args = argsDecoder.getArgs(i) as any; | ||
i += args.noOfInstructionsToSkip; | ||
} catch (e) { | ||
printableProgram.push({ instructionCode: currentInstruction, ...byteToOpCodeMap[currentInstruction], error: "Cannot get arguments from args decoder" }); | ||
return printableProgram; | ||
} | ||
|
||
const currentInstructionDebug = { | ||
instructionCode: currentInstruction, | ||
...byteToOpCodeMap[currentInstruction], | ||
args: { | ||
...args, | ||
immediate: args.immediateDecoder?.getUnsigned() as number, | ||
}, | ||
}; | ||
|
||
printableProgram.push(currentInstructionDebug); | ||
} | ||
|
||
return printableProgram; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.