ByteCodePrinter.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. Operation 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_INT);
  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_INT);
  121. PRINT_OP(OP_ADD_FLOAT);
  122. PRINT_OP(OP_SUB_INT);
  123. PRINT_OP(OP_SUB_FLOAT);
  124. PRINT_OP(OP_MUL_INT);
  125. PRINT_OP(OP_MUL_FLOAT);
  126. PRINT_OP(OP_DIV_INT);
  127. PRINT_OP(OP_DIV_FLOAT);
  128. PRINT_OP(OP_MOD_INT);
  129. PRINT_OP(OP_INVERT_SIGN_INT);
  130. PRINT_OP(OP_INVERT_SIGN_FLOAT);
  131. PRINT_OP(OP_LESS_INT);
  132. PRINT_OP(OP_LESS_FLOAT);
  133. PRINT_OP(OP_GREATER_INT);
  134. PRINT_OP(OP_GREATER_FLOAT);
  135. PRINT_OP(OP_EQUAL_INT);
  136. PRINT_OP(OP_EQUAL_FLOAT);
  137. PRINT_OP(OP_EQUAL_BOOL);
  138. PRINT_OP(OP_NOT);
  139. // PRINT_OP(OP_AND);
  140. // PRINT_OP(OP_OR);
  141. // PRINT_OP(OP_BIT_NOT);
  142. // PRINT_OP(OP_BIT_AND);
  143. // PRINT_OP(OP_BIT_OR);
  144. // PRINT_OP(OP_BIT_XOR);
  145. // PRINT_OP(OP_LEFT_SHIFT);
  146. // PRINT_OP(OP_RIGHT_SHIFT);
  147. PRINT_OP(OP_PRINT_INT);
  148. PRINT_OP(OP_PRINT_FLOAT);
  149. PRINT_OP(OP_PRINT_BOOL);
  150. // PRINT_OP_INT(OP_GOTO);
  151. // PRINT_OP_INT2(OP_GOSUB);
  152. // PRINT_OP_INT(OP_IF_GOTO);
  153. // PRINT_OP(OP_SET_RETURN);
  154. // PRINT_OP(OP_RETURN);
  155. // PRINT_OP(OP_DUPLICATE);
  156. // PRINT_OP(OP_ALLOCATE_ARRAY);
  157. // PRINT_OP(OP_ARRAY_LENGTH);
  158. PRINT_OP_INT(OP_REFERENCE_FROM_VAR);
  159. // PRINT_OP_INT(OP_REFERENCE_FROM_ARRAY);
  160. PRINT_OP(OP_DEREFERENCE_INT);
  161. // PRINT_OP_BASE(OP_PUSH_CONST_STRING, String);
  162. case OP_LINE:
  163. sPrintLine();
  164. break;
  165. // default: printf("Unknown: %d\n", op);
  166. }
  167. }
  168. void btPrint(ByteCode* bt) {
  169. code = bt;
  170. readIndex = 0;
  171. line = 0;
  172. btPrintHeading();
  173. while(readIndex < code->length) {
  174. btConsumeOperation();
  175. }
  176. }