debug.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // Created by 李晓明 on 2023/8/16.
  3. //
  4. #include "debug.h"
  5. #include "value.h"
  6. #include <stdio.h>
  7. static int constantInstruction(const char *, Chunk *, int);
  8. static int simpleInstruction(const char *name, int offset);
  9. void disassembleChunk(Chunk *chunk, const char *name) {
  10. printf("== %s STARTING ==\n", name);
  11. for (int offset = 0; offset < chunk->count;) {
  12. offset = disassembleInstruction(chunk, offset);
  13. }
  14. printf("== %s END ==\n", name);
  15. }
  16. int disassembleInstruction(Chunk *chunk, int offset) {
  17. printf("%04d ", offset);
  18. if (offset > 0 && chunk->lines[offset] == chunk->lines[offset - 1]) {
  19. printf(" | ");
  20. } else {
  21. printf("%4d ", chunk->lines[offset]);
  22. }
  23. uint8_t instruction = chunk->code[offset];
  24. switch (instruction) {
  25. case OP_CONSTANT: return constantInstruction("OP_CONSTANT", chunk, offset);
  26. case OP_NIL: return simpleInstruction("OP_NIL", offset);
  27. case OP_TRUE: return simpleInstruction("OP_TRUE", offset);
  28. case OP_FALSE: return simpleInstruction("OP_FALSE", offset);
  29. case OP_POP: return simpleInstruction("OP_POP", offset);
  30. case OP_DEFINE_GLOBAL: return constantInstruction("OP_DEFINE_GLOBAL", chunk, offset);
  31. case OP_GET_GLOBAL: return constantInstruction("OP_GET_GLOBAL", chunk, offset);
  32. case OP_SET_GLOBAL: return constantInstruction("OP_SET_GLOBAL", chunk, offset);
  33. case OP_GET_LOCAL: return constantInstruction("OP_GET_LOCAL", chunk, offset);
  34. case OP_SET_LOCAL: return constantInstruction("OP_SET_LOCAL", chunk, offset);
  35. case OP_ADD: return simpleInstruction("OP_ADD", offset);
  36. case OP_SUBTRACT: return simpleInstruction("OP_SUBTRACT", offset);
  37. case OP_MULTIPLY: return simpleInstruction("OP_MULTIPLY", offset);
  38. case OP_DIVIDE: return simpleInstruction("OP_DIVIDE", offset);
  39. case OP_NOT: return simpleInstruction("OP_NOT", offset);
  40. case OP_NEGATE: return simpleInstruction("OP_NEGATE", offset);
  41. case OP_EQUAL: return simpleInstruction("OP_EQUAL", offset);
  42. case OP_GREATER: return simpleInstruction("OP_GREATER", offset);
  43. case OP_LESS: return simpleInstruction("OP_LESS", offset);
  44. case OP_PRINT: return simpleInstruction("OP_PRINT", offset);
  45. case OP_RETURN: return simpleInstruction("OP_RETURN", offset);
  46. default:
  47. printf("Unknown opcode %d\n", instruction);
  48. return offset + 1;
  49. }
  50. }
  51. /**
  52. * @brief 打印常量指令
  53. * @param name
  54. * @param chunk
  55. * @param offset
  56. * @return
  57. */
  58. static int constantInstruction(const char *name, Chunk *chunk, int offset) {
  59. uint8_t constant = chunk->code[offset + 1];
  60. printf("%-16s %4d '", name, constant);
  61. printValue(chunk->constants.values[constant]);
  62. printf("'\n");
  63. return offset + 2;
  64. }
  65. /**
  66. * @brief 打印指令
  67. * @param name 名称
  68. * @param offset 偏移
  69. * @return
  70. */
  71. static int simpleInstruction(const char *name, int offset) {
  72. printf("%s\n", name);
  73. return offset + 1;
  74. }