debug.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // Created by 李晓明 on 2023/8/16.
  3. //
  4. #include "debug.h"
  5. #include "object.h"
  6. #include "value.h"
  7. #include <stdio.h>
  8. static int constantInstruction(const char *, Chunk *, int);
  9. static int simpleInstruction(const char *name, int offset);
  10. static int byteInstruction(const char *name, Chunk *chunk, int offset);
  11. void disassembleChunk(Chunk *chunk, const char *name) {
  12. printf("== %s STARTING ==\n", name);
  13. for (int offset = 0; offset < chunk->count;) {
  14. offset = disassembleInstruction(chunk, offset);
  15. }
  16. printf("== %s END ==\n", name);
  17. }
  18. static int jumpInstruction(const char *name, int sign, Chunk *chunk, int offset) {
  19. uint16_t jump = (uint16_t) (chunk->code[offset + 1] << 8);
  20. jump |= chunk->code[offset + 2];
  21. printf("%-16s %4d -> %d\n", name, offset, offset + 3 + sign * jump);
  22. return offset + 3;
  23. }
  24. int disassembleInstruction(Chunk *chunk, int offset) {
  25. printf("%04d ", offset);
  26. if (offset > 0 && chunk->lines[offset] == chunk->lines[offset - 1]) {
  27. printf(" | ");
  28. } else {
  29. printf("%4d ", chunk->lines[offset]);
  30. }
  31. uint8_t instruction = chunk->code[offset];
  32. switch (instruction) {
  33. case OP_CONSTANT: return constantInstruction("OP_CONSTANT", chunk, offset);
  34. case OP_NIL: return simpleInstruction("OP_NIL", offset);
  35. case OP_TRUE: return simpleInstruction("OP_TRUE", offset);
  36. case OP_FALSE: return simpleInstruction("OP_FALSE", offset);
  37. case OP_POP: return simpleInstruction("OP_POP", offset);
  38. case OP_DEFINE_GLOBAL: return constantInstruction("OP_DEFINE_GLOBAL", chunk, offset);
  39. case OP_GET_GLOBAL: return constantInstruction("OP_GET_GLOBAL", chunk, offset);
  40. case OP_SET_GLOBAL: return constantInstruction("OP_SET_GLOBAL", chunk, offset);
  41. case OP_GET_LOCAL: return byteInstruction("OP_GET_LOCAL", chunk, offset);
  42. case OP_SET_LOCAL: return byteInstruction("OP_SET_LOCAL", chunk, offset);
  43. case OP_ADD: return simpleInstruction("OP_ADD", offset);
  44. case OP_SUBTRACT: return simpleInstruction("OP_SUBTRACT", offset);
  45. case OP_MULTIPLY: return simpleInstruction("OP_MULTIPLY", offset);
  46. case OP_DIVIDE: return simpleInstruction("OP_DIVIDE", offset);
  47. case OP_NOT: return simpleInstruction("OP_NOT", offset);
  48. case OP_NEGATE: return simpleInstruction("OP_NEGATE", offset);
  49. case OP_EQUAL: return simpleInstruction("OP_EQUAL", offset);
  50. case OP_GREATER: return simpleInstruction("OP_GREATER", offset);
  51. case OP_LESS: return simpleInstruction("OP_LESS", offset);
  52. case OP_PRINT: return simpleInstruction("OP_PRINT", offset);
  53. case OP_JUMP: return jumpInstruction("OP_JUMP", 1, chunk, offset);
  54. case OP_LOOP: return jumpInstruction("OP_LOOP", -1, chunk, offset);
  55. case OP_CALL: return byteInstruction("OP_CALL", chunk, offset);
  56. case OP_JUMP_IF_FALSE: return jumpInstruction("OP_JUMP_IF_FALSE", 1, chunk, offset);
  57. case OP_CLOSURE: {
  58. offset++;
  59. uint8_t constant = chunk->code[offset++];
  60. printf("%-16s %4d ", "OP_CLOSURE", constant);
  61. printValue(chunk->constants.values[constant]);
  62. printf("\n");
  63. ObjFunction *function = AS_FUNCTION(chunk->constants.values[constant]);
  64. for (int j = 0; j < function->upvalueCount; j++) {
  65. int isLocal = chunk->code[offset++];
  66. int index = chunk->code[offset++];
  67. printf("%04d | %s %d\n", offset - 2, isLocal ? "local" : "upvalue", index);
  68. }
  69. return offset;
  70. }
  71. case OP_GET_UPVALUE: return byteInstruction("OP_GET_UPVALUE", chunk, offset);
  72. case OP_SET_UPVALUE: return byteInstruction("OP_SET_UPVALUE", chunk, offset);
  73. case OP_CLOSE_UPVALUE: return simpleInstruction("OP_CLOSE_UPVALUE", offset);
  74. case OP_CLASS: return constantInstruction("OP_CLASS", chunk, offset);
  75. case OP_METHOD: return constantInstruction("OP_METHOD", chunk, offset);
  76. case OP_GET_PROPERTY: return constantInstruction("OP_GET_PROPERTY", chunk, offset);
  77. case OP_SET_PROPERTY: return constantInstruction("OP_SET_PROPERTY", chunk, offset);
  78. case OP_RETURN: return simpleInstruction("OP_RETURN", offset);
  79. default:
  80. printf("Unknown opcode %d\n", instruction);
  81. return offset + 1;
  82. }
  83. }
  84. /**
  85. * @brief 打印常量指令
  86. * @param name
  87. * @param chunk
  88. * @param offset
  89. * @return
  90. */
  91. static int constantInstruction(const char *name, Chunk *chunk, int offset) {
  92. uint8_t constant = chunk->code[offset + 1];
  93. printf("%-16s %4d '", name, constant);
  94. printValue(chunk->constants.values[constant]);
  95. printf("'\n");
  96. return offset + 2;
  97. }
  98. /**
  99. * @brief 打印指令
  100. * @param name 名称
  101. * @param offset 偏移
  102. * @return
  103. */
  104. static int simpleInstruction(const char *name, int offset) {
  105. printf("%s\n", name);
  106. return offset + 1;
  107. }
  108. static int byteInstruction(const char *name, Chunk *chunk, int offset) {
  109. uint8_t slot = chunk->code[offset + 1];
  110. printf("%-16s %4d\n", name, slot);
  111. return offset + 2;
  112. }