debug.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. static int jumpInstruction(const char *name, int sign, Chunk *chunk, int offset) {
  17. uint16_t jump = (uint16_t) (chunk->code[offset + 1] << 8);
  18. jump |= chunk->code[offset + 2];
  19. printf("%-16s %4d -> %d\n", name, offset, offset + 3 + sign * jump);
  20. return offset + 3;
  21. }
  22. int disassembleInstruction(Chunk *chunk, int offset) {
  23. printf("%04d ", offset);
  24. if (offset > 0 && chunk->lines[offset] == chunk->lines[offset - 1]) {
  25. printf(" | ");
  26. } else {
  27. printf("%4d ", chunk->lines[offset]);
  28. }
  29. uint8_t instruction = chunk->code[offset];
  30. switch (instruction) {
  31. case OP_CONSTANT: return constantInstruction("OP_CONSTANT", chunk, offset);
  32. case OP_NIL: return simpleInstruction("OP_NIL", offset);
  33. case OP_TRUE: return simpleInstruction("OP_TRUE", offset);
  34. case OP_FALSE: return simpleInstruction("OP_FALSE", offset);
  35. case OP_POP: return simpleInstruction("OP_POP", offset);
  36. case OP_DEFINE_GLOBAL: return constantInstruction("OP_DEFINE_GLOBAL", chunk, offset);
  37. case OP_GET_GLOBAL: return constantInstruction("OP_GET_GLOBAL", chunk, offset);
  38. case OP_SET_GLOBAL: return constantInstruction("OP_SET_GLOBAL", chunk, offset);
  39. case OP_GET_LOCAL: return constantInstruction("OP_GET_LOCAL", chunk, offset);
  40. case OP_SET_LOCAL: return constantInstruction("OP_SET_LOCAL", chunk, offset);
  41. case OP_ADD: return simpleInstruction("OP_ADD", offset);
  42. case OP_SUBTRACT: return simpleInstruction("OP_SUBTRACT", offset);
  43. case OP_MULTIPLY: return simpleInstruction("OP_MULTIPLY", offset);
  44. case OP_DIVIDE: return simpleInstruction("OP_DIVIDE", offset);
  45. case OP_NOT: return simpleInstruction("OP_NOT", offset);
  46. case OP_NEGATE: return simpleInstruction("OP_NEGATE", offset);
  47. case OP_EQUAL: return simpleInstruction("OP_EQUAL", offset);
  48. case OP_GREATER: return simpleInstruction("OP_GREATER", offset);
  49. case OP_LESS: return simpleInstruction("OP_LESS", offset);
  50. case OP_PRINT: return simpleInstruction("OP_PRINT", offset);
  51. case OP_JUMP: return jumpInstruction("OP_JUMP", 1, chunk, offset);
  52. case OP_JUMP_IF_FALSE: return jumpInstruction("OP_JUMP_IF_FALSE", 1, chunk, offset);
  53. case OP_RETURN: return simpleInstruction("OP_RETURN", offset);
  54. default:
  55. printf("Unknown opcode %d\n", instruction);
  56. return offset + 1;
  57. }
  58. }
  59. /**
  60. * @brief 打印常量指令
  61. * @param name
  62. * @param chunk
  63. * @param offset
  64. * @return
  65. */
  66. static int constantInstruction(const char *name, Chunk *chunk, int offset) {
  67. uint8_t constant = chunk->code[offset + 1];
  68. printf("%-16s %4d '", name, constant);
  69. printValue(chunk->constants.values[constant]);
  70. printf("'\n");
  71. return offset + 2;
  72. }
  73. /**
  74. * @brief 打印指令
  75. * @param name 名称
  76. * @param offset 偏移
  77. * @return
  78. */
  79. static int simpleInstruction(const char *name, int offset) {
  80. printf("%s\n", name);
  81. return offset + 1;
  82. }