ByteCodePrinter.c 6.2 KB

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