debug.c 782 B

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // Created by 李晓明 on 2023/8/16.
  3. //
  4. #include "debug.h"
  5. #include <stdio.h>
  6. /**
  7. * @brief 打印指令
  8. * @param name 名称
  9. * @param offset 偏移
  10. * @return
  11. */
  12. static int simpleInstruction(const char *name, int offset) {
  13. printf("%s\n", name);
  14. return offset + 1;
  15. }
  16. void disassembleChunk(Chunk *chunk, const char *name) {
  17. printf("== %s ==\n", name);
  18. for (int offset = 0; offset < chunk->count;) {
  19. offset = disassembleInstruction(chunk, offset);
  20. }
  21. }
  22. int disassembleInstruction(Chunk *chunk, int offset) {
  23. printf("%04d ", offset);
  24. uint8_t instruction = chunk->code[offset];
  25. switch (instruction) {
  26. case OP_RETURN:return simpleInstruction("OP_RETURN", offset);
  27. default:printf("Unknown opcode %d\n", instruction);
  28. return offset + 1;
  29. }
  30. }