ByteCodePrinter.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. static void btPrintString(const char* msg) {
  86. btFillBase();
  87. int length = 0;
  88. btRead(&length, sizeof(int));
  89. char* s = (char*)(code->code + readIndex);
  90. readIndex += length;
  91. btAddOperation(msg);
  92. btAdd(" %10s |", s);
  93. btAddFiller();
  94. puts(buffer);
  95. }
  96. #define PRINT_OP_BASE(op, name) \
  97. case op: \
  98. btPrint##name(#op); \
  99. break;
  100. #define PRINT_OP(op) PRINT_OP_BASE(op, Op)
  101. #define PRINT_OP_INT(op) PRINT_OP_BASE(op, Int)
  102. #define PRINT_OP_INT2(op) PRINT_OP_BASE(op, Int2)
  103. static void btConsumeOperation() {
  104. int op = code->code[readIndex++];
  105. switch(op) {
  106. PRINT_OP(OP_NOTHING);
  107. PRINT_OP_INT(OP_PUSH_INT);
  108. PRINT_OP_BASE(OP_PUSH_FLOAT, Float);
  109. PRINT_OP(OP_PUSH_NULL);
  110. PRINT_OP(OP_PUSH_TRUE);
  111. PRINT_OP(OP_PUSH_FALSE);
  112. PRINT_OP_INT2(OP_PUSH_VARS);
  113. PRINT_OP_INT(OP_POP_VARS);
  114. PRINT_OP(OP_POP);
  115. PRINT_OP(OP_SET);
  116. PRINT_OP(OP_PRE_INCREMENT);
  117. PRINT_OP(OP_POST_INCREMENT);
  118. PRINT_OP(OP_PRE_DECREMENT);
  119. PRINT_OP(OP_POST_DECREMENT);
  120. PRINT_OP(OP_ADD);
  121. PRINT_OP(OP_SUB);
  122. PRINT_OP(OP_MUL);
  123. PRINT_OP(OP_DIV);
  124. PRINT_OP(OP_MOD);
  125. PRINT_OP(OP_INVERT_SIGN);
  126. PRINT_OP(OP_LESS);
  127. PRINT_OP(OP_GREATER);
  128. PRINT_OP(OP_EQUAL);
  129. PRINT_OP(OP_NOT);
  130. PRINT_OP(OP_AND);
  131. PRINT_OP(OP_OR);
  132. PRINT_OP(OP_BIT_NOT);
  133. PRINT_OP(OP_BIT_AND);
  134. PRINT_OP(OP_BIT_OR);
  135. PRINT_OP(OP_BIT_XOR);
  136. PRINT_OP(OP_LEFT_SHIFT);
  137. PRINT_OP(OP_RIGHT_SHIFT);
  138. PRINT_OP(OP_PRINT);
  139. PRINT_OP_INT(OP_GOTO);
  140. PRINT_OP_INT2(OP_GOSUB);
  141. PRINT_OP_INT(OP_IF_GOTO);
  142. PRINT_OP(OP_SET_RETURN);
  143. PRINT_OP(OP_RETURN);
  144. PRINT_OP(OP_DUPLICATE);
  145. PRINT_OP(OP_ALLOCATE_ARRAY);
  146. PRINT_OP(OP_ARRAY_LENGTH);
  147. PRINT_OP_INT(OP_REFERENCE_FROM_VAR);
  148. PRINT_OP_INT(OP_REFERENCE_FROM_ARRAY);
  149. PRINT_OP(OP_DEREFERENCE);
  150. PRINT_OP_BASE(OP_PUSH_CONST_STRING, String);
  151. case OP_LINE: sPrintLine(); break;
  152. default: printf("Unknown: %d\n", op);
  153. }
  154. }
  155. void btPrint(ByteCode* bt) {
  156. code = bt;
  157. readIndex = 0;
  158. line = 0;
  159. btPrintHeading();
  160. while(readIndex < code->length) {
  161. btConsumeOperation();
  162. }
  163. }