ByteCodePrinter.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "utils/ByteCodePrinter.h"
  5. #define LINE_LENGTH 80
  6. static ByteCode* code;
  7. static int readIndex;
  8. static int line;
  9. static char buffer[LINE_LENGTH];
  10. static int bIndex;
  11. static void btPrintHeading() {
  12. puts("| Index | Line | Operation | Argument 1 | Argument 2 |");
  13. puts("|--------|-------|----------------------|------------|------------|");
  14. }
  15. static void btAdd(const char* format, ...) {
  16. va_list args;
  17. va_start(args, format);
  18. bIndex += vsnprintf(buffer + bIndex, LINE_LENGTH - bIndex, format, args);
  19. va_end(args);
  20. }
  21. static void btFillBase() {
  22. buffer[0] = '\0';
  23. bIndex = 0;
  24. btAdd("| %6d | %5d |", readIndex - 1, line);
  25. }
  26. static void btRead(void* buffer, int length) {
  27. if(readIndex + length > code->length) {
  28. return;
  29. }
  30. memcpy(buffer, code->code + readIndex, length);
  31. readIndex += length;
  32. }
  33. static void btAddOperation(const char* s) {
  34. if(s[0] != '\0' && s[1] != '\0' && s[2] != '\0' && s[3] != '\0') {
  35. s += 3;
  36. }
  37. btAdd(" %20s |", s);
  38. }
  39. static void btAddInt() {
  40. int value = 0;
  41. btRead(&value, sizeof(int));
  42. btAdd(" %10d |", value);
  43. }
  44. static void btAddFloat() {
  45. float value = 0;
  46. btRead(&value, sizeof(float));
  47. btAdd(" %10.2f |", value);
  48. }
  49. static void btAddFiller() {
  50. btAdd(" |");
  51. }
  52. static void sPrintLine() {
  53. btRead(&line, 2);
  54. printf("| %6d |-------|----------------------|------------|------------|\n",
  55. readIndex - 3);
  56. }
  57. static void btPrintOp(const char* op) {
  58. btFillBase();
  59. btAddOperation(op);
  60. btAddFiller();
  61. btAddFiller();
  62. puts(buffer);
  63. }
  64. static void btPrintInt(const char* op) {
  65. btFillBase();
  66. btAddOperation(op);
  67. btAddInt();
  68. btAddFiller();
  69. puts(buffer);
  70. }
  71. static void btPrintInt2(const char* op) {
  72. btFillBase();
  73. btAddOperation(op);
  74. btAddInt();
  75. btAddInt();
  76. puts(buffer);
  77. }
  78. static void btPrintFloat(const char* op) {
  79. btFillBase();
  80. btAddOperation(op);
  81. btAddFloat();
  82. btAddFiller();
  83. puts(buffer);
  84. }
  85. #define PRINT_OP_BASE(op, name) \
  86. case op: \
  87. btPrint##name(#op); \
  88. break;
  89. #define PRINT_OP(op) PRINT_OP_BASE(op, Op)
  90. #define PRINT_OP_INT(op) PRINT_OP_BASE(op, Int)
  91. #define PRINT_OP_INT2(op) PRINT_OP_BASE(op, Int2)
  92. #define PRINT_NUMBER_OP(op) PRINT_OP(OP_##op##_INT) PRINT_OP(OP_##op##_FLOAT)
  93. #define PRINT_TYPES(TYPE) \
  94. PRINT_OP_INT(OP_LOAD_##TYPE); \
  95. PRINT_OP_INT(OP_STORE_##TYPE); \
  96. PRINT_OP_INT(OP_RETURN_##TYPE); \
  97. PRINT_OP(OP_EQUAL_##TYPE);
  98. static void btConsumeOperation() {
  99. Operation op = code->code[readIndex++];
  100. switch(op) {
  101. PRINT_TYPES(INT);
  102. PRINT_TYPES(BOOL);
  103. PRINT_TYPES(FLOAT);
  104. PRINT_OP(OP_NOTHING);
  105. PRINT_OP_INT(OP_PUSH_INT);
  106. PRINT_OP_BASE(OP_PUSH_FLOAT, Float);
  107. PRINT_OP(OP_PUSH_TRUE);
  108. PRINT_OP(OP_PUSH_FALSE);
  109. PRINT_NUMBER_OP(ADD);
  110. PRINT_NUMBER_OP(SUB);
  111. PRINT_NUMBER_OP(MUL);
  112. PRINT_NUMBER_OP(DIV);
  113. PRINT_OP(OP_MOD_INT);
  114. PRINT_NUMBER_OP(INVERT_SIGN);
  115. PRINT_NUMBER_OP(LESS);
  116. PRINT_NUMBER_OP(GREATER);
  117. PRINT_OP(OP_NOT);
  118. PRINT_OP(OP_AND);
  119. PRINT_OP(OP_OR);
  120. PRINT_OP(OP_BIT_NOT);
  121. PRINT_OP(OP_BIT_AND);
  122. PRINT_OP(OP_BIT_OR);
  123. PRINT_OP(OP_BIT_XOR);
  124. PRINT_OP(OP_LEFT_SHIFT);
  125. PRINT_OP(OP_RIGHT_SHIFT);
  126. PRINT_NUMBER_OP(PRINT);
  127. PRINT_OP(OP_PRINT_BOOL);
  128. PRINT_OP_INT(OP_GOTO);
  129. PRINT_OP_INT(OP_IF_GOTO);
  130. PRINT_OP_INT(OP_PEEK_FALSE_GOTO);
  131. PRINT_OP_INT(OP_PEEK_TRUE_GOTO);
  132. PRINT_OP_INT2(OP_GOSUB);
  133. PRINT_OP_INT(OP_RETURN);
  134. PRINT_OP_INT2(OP_RESERVE);
  135. case OP_LINE: sPrintLine(); break;
  136. }
  137. }
  138. void btPrint(ByteCode* bt) {
  139. code = bt;
  140. readIndex = 0;
  141. line = 0;
  142. btPrintHeading();
  143. while(readIndex < code->length) {
  144. btConsumeOperation();
  145. }
  146. }