debug.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_ADD: return simpleInstruction("OP_ADD", offset);
  34. case OP_SUBTRACT: return simpleInstruction("OP_SUBTRACT", offset);
  35. case OP_MULTIPLY: return simpleInstruction("OP_MULTIPLY", offset);
  36. case OP_DIVIDE: return simpleInstruction("OP_DIVIDE", offset);
  37. case OP_NOT: return simpleInstruction("OP_NOT", offset);
  38. case OP_NEGATE: return simpleInstruction("OP_NEGATE", offset);
  39. case OP_EQUAL: return simpleInstruction("OP_EQUAL", offset);
  40. case OP_GREATER: return simpleInstruction("OP_GREATER", offset);
  41. case OP_LESS: return simpleInstruction("OP_LESS", offset);
  42. case OP_PRINT: return simpleInstruction("OP_PRINT", offset);
  43. case OP_RETURN: return simpleInstruction("OP_RETURN", offset);
  44. default:
  45. printf("Unknown opcode %d\n", instruction);
  46. return offset + 1;
  47. }
  48. }
  49. /**
  50. * @brief 打印常量指令
  51. * @param name
  52. * @param chunk
  53. * @param offset
  54. * @return
  55. */
  56. static int constantInstruction(const char *name, Chunk *chunk, int offset) {
  57. uint8_t constant = chunk->code[offset + 1];
  58. printf("%-16s %4d '", name, constant);
  59. printValue(chunk->constants.values[constant]);
  60. printf("'\n");
  61. return offset + 2;
  62. }
  63. /**
  64. * @brief 打印指令
  65. * @param name 名称
  66. * @param offset 偏移
  67. * @return
  68. */
  69. static int simpleInstruction(const char *name, int offset) {
  70. printf("%s\n", name);
  71. return offset + 1;
  72. }