ByteCodePrinter.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "Types.h"
  5. #include "utils/ByteCodePrinter.h"
  6. #define LINE_LENGTH 80
  7. static ByteCode* code;
  8. static int readIndex;
  9. static int line;
  10. static char buffer[LINE_LENGTH];
  11. static int bIndex;
  12. static void btPrintEnd() {
  13. puts("+--------+-------+----------------------+------------+------------+");
  14. }
  15. static void btPrintHeading() {
  16. btPrintEnd();
  17. puts("| Index | Line | Operation | Argument 1 | Argument 2 |");
  18. btPrintEnd();
  19. }
  20. static void btAdd(const char* format, ...) {
  21. va_list args;
  22. va_start(args, format);
  23. bIndex += vsnprintf(buffer + bIndex, LINE_LENGTH - bIndex, format, args);
  24. va_end(args);
  25. }
  26. static void btFillBase() {
  27. buffer[0] = '\0';
  28. bIndex = 0;
  29. btAdd("| %6d | %5d |", readIndex - 1, line);
  30. }
  31. static void btRead(void* buffer, int length) {
  32. if(readIndex + length > code->length) {
  33. return;
  34. }
  35. memcpy(buffer, code->code + readIndex, length);
  36. readIndex += length;
  37. }
  38. static void btAddOperation(const char* s) {
  39. if(s[0] != '\0' && s[1] != '\0' && s[2] != '\0' && s[3] != '\0') {
  40. s += 3;
  41. }
  42. btAdd(" %20s |", s);
  43. }
  44. static void btAddInt32() {
  45. int32 value = 0;
  46. btRead(&value, sizeof(int32));
  47. btAdd(" %10d |", value);
  48. }
  49. static void btAddInt8() {
  50. int8 value = 0;
  51. btRead(&value, sizeof(int8));
  52. btAdd(" %10d |", (int32)value);
  53. }
  54. static void btAddFloat() {
  55. float value = 0;
  56. btRead(&value, sizeof(float));
  57. btAdd(" %10.2f |", value);
  58. }
  59. static void btAddText() {
  60. int32 length = 0;
  61. btRead(&length, sizeof(int32));
  62. for(int i = 0; i < length; i++) {
  63. int32 c;
  64. btRead(&c, sizeof(int32));
  65. }
  66. btAdd(" %10s |", "Text");
  67. }
  68. static void btAddFiller() {
  69. btAdd(" |");
  70. }
  71. static void sPrintLine() {
  72. btRead(&line, 2);
  73. printf("| %6d |-------|----------------------|------------|------------|\n",
  74. readIndex - 3);
  75. }
  76. static void btPrintOp(const char* op) {
  77. btFillBase();
  78. btAddOperation(op);
  79. btAddFiller();
  80. btAddFiller();
  81. puts(buffer);
  82. }
  83. static void btPrintInt32(const char* op) {
  84. btFillBase();
  85. btAddOperation(op);
  86. btAddInt32();
  87. btAddFiller();
  88. puts(buffer);
  89. }
  90. static void btPrintInt8(const char* op) {
  91. btFillBase();
  92. btAddOperation(op);
  93. btAddInt8();
  94. btAddFiller();
  95. puts(buffer);
  96. }
  97. static void btPrint2Int32(const char* op) {
  98. btFillBase();
  99. btAddOperation(op);
  100. btAddInt32();
  101. btAddInt32();
  102. puts(buffer);
  103. }
  104. static void btPrintFloat(const char* op) {
  105. btFillBase();
  106. btAddOperation(op);
  107. btAddFloat();
  108. btAddFiller();
  109. puts(buffer);
  110. }
  111. static void btPrintText(const char* op) {
  112. btFillBase();
  113. btAddOperation(op);
  114. btAddText();
  115. btAddFiller();
  116. puts(buffer);
  117. }
  118. #define PRINT_OP_BASE(op, name) \
  119. case op: \
  120. btPrint##name(#op); \
  121. break;
  122. #define PRINT_OP(op) PRINT_OP_BASE(op, Op)
  123. #define PRINT_OP_INT8(op) PRINT_OP_BASE(op, Int8)
  124. #define PRINT_OP_INT32(op) PRINT_OP_BASE(op, Int32)
  125. #define PRINT_OP_2INT32(op) PRINT_OP_BASE(op, 2Int32)
  126. #define PRINT_OP_INT64(op) PRINT_OP_BASE(op, Int64)
  127. #define PRINT_INTEGRAL_OP(op) PRINT_OP(OP_##op##_INT)
  128. #define PRINT_NUMBER_OP(op) PRINT_INTEGRAL_OP(op) PRINT_OP(OP_##op##_FLOAT)
  129. #define PRINT_TYPES(TYPE) \
  130. PRINT_OP(OP_STORE_##TYPE); \
  131. PRINT_OP(OP_LOAD_##TYPE); \
  132. PRINT_OP_INT32(OP_RETURN_##TYPE); \
  133. PRINT_OP(OP_EQUAL_##TYPE); \
  134. PRINT_OP(OP_NOT_EQUAL_##TYPE);
  135. static void btConsumeOperation() {
  136. Operation op = code->code[readIndex++];
  137. switch(op) {
  138. PRINT_TYPES(INT);
  139. PRINT_TYPES(FLOAT);
  140. PRINT_OP(OP_NOTHING);
  141. PRINT_OP_INT32(OP_PUSH_INT);
  142. PRINT_OP_BASE(OP_PUSH_FLOAT, Float);
  143. PRINT_OP_BASE(OP_PUSH_TEXT, Text);
  144. PRINT_NUMBER_OP(ADD);
  145. PRINT_NUMBER_OP(SUB);
  146. PRINT_NUMBER_OP(MUL);
  147. PRINT_NUMBER_OP(DIV);
  148. PRINT_INTEGRAL_OP(MOD);
  149. PRINT_NUMBER_OP(INVERT_SIGN);
  150. PRINT_NUMBER_OP(LESS);
  151. PRINT_NUMBER_OP(LESS_EQUAL);
  152. PRINT_NUMBER_OP(GREATER);
  153. PRINT_NUMBER_OP(GREATER_EQUAL);
  154. PRINT_OP(OP_NOT);
  155. PRINT_OP(OP_AND);
  156. PRINT_OP(OP_OR);
  157. PRINT_INTEGRAL_OP(BIT_NOT);
  158. PRINT_INTEGRAL_OP(BIT_AND);
  159. PRINT_INTEGRAL_OP(BIT_OR);
  160. PRINT_INTEGRAL_OP(BIT_XOR);
  161. PRINT_INTEGRAL_OP(LEFT_SHIFT);
  162. PRINT_INTEGRAL_OP(RIGHT_SHIFT);
  163. PRINT_OP_INT32(OP_GOTO);
  164. PRINT_OP_INT32(OP_IF_GOTO);
  165. PRINT_OP_INT32(OP_PEEK_FALSE_GOTO);
  166. PRINT_OP_INT32(OP_PEEK_TRUE_GOTO);
  167. PRINT_OP_2INT32(OP_GOSUB);
  168. PRINT_OP_INT32(OP_RETURN);
  169. PRINT_OP_INT32(OP_RETURN_POINTER);
  170. PRINT_OP_2INT32(OP_RESERVE);
  171. PRINT_OP_INT32(OP_GRESERVE);
  172. PRINT_OP_INT32(OP_DEREFERENCE_VAR);
  173. PRINT_OP_INT32(OP_DEREFERENCE_GVAR);
  174. PRINT_OP(OP_LOAD_ARRAY);
  175. PRINT_OP(OP_DUPLICATE_REFERENCE);
  176. PRINT_OP_INT32(OP_ADD_REFERENCE);
  177. PRINT_OP_INT32(OP_NEW);
  178. PRINT_OP(OP_LENGTH);
  179. PRINT_OP_INT32(OP_PUSH_STRUCT_REFERENCE);
  180. PRINT_OP(OP_STORE_ARRAY);
  181. PRINT_OP(OP_EQUAL_POINTER);
  182. PRINT_OP(OP_NOT_EQUAL_POINTER);
  183. PRINT_OP_INT8(OP_PUSH_PRE_CHANGE_INT);
  184. PRINT_OP_INT8(OP_PUSH_POST_CHANGE_INT);
  185. PRINT_OP_INT8(OP_CHANGE_INT);
  186. PRINT_OP(OP_INT_TO_FLOAT);
  187. PRINT_OP(OP_FLOAT_TO_INT);
  188. PRINT_OP_INT32(OP_CALL);
  189. case OP_LINE: sPrintLine(); break;
  190. }
  191. }
  192. void btPrint(ByteCode* bt) {
  193. code = bt;
  194. readIndex = 0;
  195. line = 0;
  196. btPrintHeading();
  197. while(readIndex < code->length) {
  198. btConsumeOperation();
  199. }
  200. btPrintEnd();
  201. }