ByteCodePrinter.c 6.0 KB

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