ByteCodePrinter.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 btAddByte() {
  45. char value = 0;
  46. btRead(&value, sizeof(char));
  47. btAdd(" %10d |", (int)value);
  48. }
  49. static void btAddFloat() {
  50. float value = 0;
  51. btRead(&value, sizeof(float));
  52. btAdd(" %10.2f |", value);
  53. }
  54. static void btAddFiller() {
  55. btAdd(" |");
  56. }
  57. static void sPrintLine() {
  58. btRead(&line, 2);
  59. printf("| %6d |-------|----------------------|------------|------------|\n",
  60. readIndex - 3);
  61. }
  62. static void btPrintOp(const char* op) {
  63. btFillBase();
  64. btAddOperation(op);
  65. btAddFiller();
  66. btAddFiller();
  67. puts(buffer);
  68. }
  69. static void btPrintInt(const char* op) {
  70. btFillBase();
  71. btAddOperation(op);
  72. btAddInt();
  73. btAddFiller();
  74. puts(buffer);
  75. }
  76. static void btPrintByte(const char* op) {
  77. btFillBase();
  78. btAddOperation(op);
  79. btAddByte();
  80. btAddFiller();
  81. puts(buffer);
  82. }
  83. static void btPrintInt2(const char* op) {
  84. btFillBase();
  85. btAddOperation(op);
  86. btAddInt();
  87. btAddInt();
  88. puts(buffer);
  89. }
  90. static void btPrintFloat(const char* op) {
  91. btFillBase();
  92. btAddOperation(op);
  93. btAddFloat();
  94. btAddFiller();
  95. puts(buffer);
  96. }
  97. #define PRINT_OP_BASE(op, name) \
  98. case op: \
  99. btPrint##name(#op); \
  100. break;
  101. #define PRINT_OP(op) PRINT_OP_BASE(op, Op)
  102. #define PRINT_OP_BYTE(op) PRINT_OP_BASE(op, Byte)
  103. #define PRINT_OP_INT(op) PRINT_OP_BASE(op, Int)
  104. #define PRINT_OP_INT2(op) PRINT_OP_BASE(op, Int2)
  105. #define PRINT_NUMBER_OP(op) PRINT_OP(OP_##op##_INT) PRINT_OP(OP_##op##_FLOAT)
  106. #define PRINT_TYPES(TYPE) \
  107. PRINT_OP(OP_STORE_##TYPE); \
  108. PRINT_OP(OP_LOAD_##TYPE); \
  109. PRINT_OP_INT(OP_RETURN_##TYPE); \
  110. PRINT_OP(OP_EQUAL_##TYPE);
  111. static void btConsumeOperation() {
  112. Operation op = code->code[readIndex++];
  113. switch(op) {
  114. PRINT_TYPES(INT);
  115. PRINT_TYPES(BOOL);
  116. PRINT_TYPES(FLOAT);
  117. PRINT_OP(OP_NOTHING);
  118. PRINT_OP_INT(OP_PUSH_INT);
  119. PRINT_OP_BASE(OP_PUSH_FLOAT, Float);
  120. PRINT_OP(OP_PUSH_TRUE);
  121. PRINT_OP(OP_PUSH_FALSE);
  122. PRINT_NUMBER_OP(ADD);
  123. PRINT_NUMBER_OP(SUB);
  124. PRINT_NUMBER_OP(MUL);
  125. PRINT_NUMBER_OP(DIV);
  126. PRINT_OP(OP_MOD_INT);
  127. PRINT_NUMBER_OP(INVERT_SIGN);
  128. PRINT_NUMBER_OP(LESS);
  129. PRINT_NUMBER_OP(GREATER);
  130. PRINT_OP(OP_NOT);
  131. PRINT_OP(OP_AND);
  132. PRINT_OP(OP_OR);
  133. PRINT_OP(OP_BIT_NOT);
  134. PRINT_OP(OP_BIT_AND);
  135. PRINT_OP(OP_BIT_OR);
  136. PRINT_OP(OP_BIT_XOR);
  137. PRINT_OP(OP_LEFT_SHIFT);
  138. PRINT_OP(OP_RIGHT_SHIFT);
  139. PRINT_NUMBER_OP(PRINT);
  140. PRINT_OP(OP_PRINT_BOOL);
  141. PRINT_OP(OP_PRINT_POINTER);
  142. PRINT_OP_INT(OP_GOTO);
  143. PRINT_OP_INT(OP_IF_GOTO);
  144. PRINT_OP_INT(OP_PEEK_FALSE_GOTO);
  145. PRINT_OP_INT(OP_PEEK_TRUE_GOTO);
  146. PRINT_OP_INT2(OP_GOSUB);
  147. PRINT_OP_INT(OP_RETURN);
  148. PRINT_OP_INT(OP_RETURN_POINTER);
  149. PRINT_OP_INT2(OP_RESERVE);
  150. PRINT_OP_INT(OP_DEREFERENCE_VAR);
  151. PRINT_OP(OP_REFERENCE);
  152. PRINT_OP(OP_DUPLICATE_REFERENCE);
  153. PRINT_OP(OP_ADD_REFERENCE);
  154. PRINT_OP_INT(OP_NEW);
  155. PRINT_OP(OP_DELETE);
  156. PRINT_OP(OP_LENGTH);
  157. PRINT_OP_INT(OP_LOAD);
  158. PRINT_OP(OP_STORE_POINTER);
  159. PRINT_OP(OP_EQUAL_POINTER);
  160. PRINT_OP_BYTE(OP_PUSH_PRE_INT_CHANGE);
  161. PRINT_OP_BYTE(OP_PUSH_POST_INT_CHANGE);
  162. PRINT_OP_BYTE(OP_INT_CHANGE);
  163. case OP_LINE: sPrintLine(); break;
  164. }
  165. }
  166. void btPrint(ByteCode* bt) {
  167. code = bt;
  168. readIndex = 0;
  169. line = 0;
  170. btPrintHeading();
  171. while(readIndex < code->length) {
  172. btConsumeOperation();
  173. }
  174. }