You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

print.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "vm/print.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. void l2_vm_print_val(struct l2_vm_value *val) {
  5. switch (val->flags & 0x0f) {
  6. case L2_VAL_TYPE_NONE:
  7. printf("NONE\n");
  8. break;
  9. case L2_VAL_TYPE_INTEGER:
  10. printf("INTEGER %zi\n", val->integer);
  11. break;
  12. case L2_VAL_TYPE_REAL:
  13. printf("REAL %f\n", val->real);
  14. break;
  15. case L2_VAL_TYPE_ARRAY:
  16. {
  17. if (val->data == NULL) {
  18. printf("ARRAY, empty\n");
  19. return;
  20. }
  21. struct l2_vm_array *arr = (struct l2_vm_array *)val->data;
  22. printf("ARRAY, len %zu\n", arr->len);
  23. for (size_t i = 0; i < arr->len; ++i) {
  24. printf(" %zu: %u\n", i, arr->data[i]);
  25. }
  26. }
  27. break;
  28. case L2_VAL_TYPE_BUFFER:
  29. {
  30. if (val->data == NULL) {
  31. printf("BUFFER, empty\n");
  32. return;
  33. }
  34. struct l2_vm_buffer *buf = (struct l2_vm_buffer *)val->data;
  35. printf("BUFFER, len %zu\n", buf->len);
  36. for (size_t i = 0; i < buf->len; ++i) {
  37. printf(" %zu: %c\n", i, buf->data[i]);
  38. }
  39. }
  40. break;
  41. case L2_VAL_TYPE_NAMESPACE:
  42. {
  43. if (val->data == NULL) {
  44. printf("NAMESPACE, empty\n");
  45. return;
  46. }
  47. struct l2_vm_namespace *ns = (struct l2_vm_namespace *)val->data;
  48. printf("NAMESPACE, len %zu\n", ns->len);
  49. }
  50. break;
  51. case L2_VAL_TYPE_FUNCTION:
  52. printf("FUNCTION, pos %u, ns %u\n", val->func.pos, val->func.namespace);
  53. break;
  54. }
  55. }
  56. void l2_vm_print_state(struct l2_vm *vm) {
  57. printf("Stack:\n");
  58. l2_vm_print_stack(vm);
  59. printf("Heap:\n");
  60. l2_vm_print_heap(vm);
  61. printf("NStack:\n");
  62. l2_vm_print_nstack(vm);
  63. }
  64. void l2_vm_print_heap(struct l2_vm *vm) {
  65. for (l2_word i = 0; i < vm->valuessize; ++i) {
  66. if (l2_bitset_get(&vm->valueset, i)) {
  67. printf(" %u: ", i);
  68. l2_vm_print_val(&vm->values[i]);
  69. }
  70. }
  71. }
  72. void l2_vm_print_stack(struct l2_vm *vm) {
  73. for (l2_word i = 0; i < vm->sptr; ++i) {
  74. printf(" %i: %i\n", i, vm->stack[i]);
  75. }
  76. }
  77. void l2_vm_print_nstack(struct l2_vm *vm) {
  78. for (l2_word i = 0; i < vm->nsptr; ++i) {
  79. printf(" %i: %i\n", i, vm->nstack[i]);
  80. }
  81. }
  82. static void print_op_num(l2_word *ops, size_t opcount, size_t ptr) {
  83. if (ptr >= opcount) {
  84. printf("<EOF>");
  85. } else {
  86. printf("%i", ops[ptr]);
  87. }
  88. }
  89. void l2_vm_print_op(l2_word *ops, size_t opcount, size_t *ptr) {
  90. enum l2_opcode opcode = (enum l2_opcode)ops[(*ptr)++];
  91. switch (opcode) {
  92. case L2_OP_NOP:
  93. printf("NOP\n");
  94. break;
  95. case L2_OP_PUSH:
  96. printf("PUSH ");
  97. print_op_num(ops, opcount, (*ptr)++);
  98. printf("\n");
  99. break;
  100. case L2_OP_PUSH_2:
  101. printf("PUSH2 ");
  102. print_op_num(ops, opcount, (*ptr)++);
  103. printf(" ");
  104. print_op_num(ops, opcount, (*ptr)++);
  105. printf("\n");
  106. break;
  107. case L2_OP_POP:
  108. printf("POP\n");
  109. break;
  110. case L2_OP_DUP:
  111. printf("DUP\n");
  112. break;
  113. case L2_OP_ADD:
  114. printf("ADD\n");
  115. break;
  116. case L2_OP_CALL:
  117. printf("CALL\n");
  118. break;
  119. case L2_OP_RJMP:
  120. printf("RJMP\n");
  121. break;
  122. case L2_OP_GEN_STACK_FRAME:
  123. printf("GEN_STACK_FRAME\n");
  124. break;
  125. case L2_OP_STACK_FRAME_LOOKUP:
  126. printf("STACK_FRAME_LOOKUP\n");
  127. break;
  128. case L2_OP_STACK_FRAME_SET:
  129. printf("STACK_FRAME_SET\n");
  130. break;
  131. case L2_OP_RET:
  132. printf("RET\n");
  133. break;
  134. case L2_OP_ALLOC_INTEGER_32:
  135. printf("ALLOC_INTEGER_32\n");
  136. break;
  137. case L2_OP_ALLOC_INTEGER_64:
  138. printf("ALLOC_INTEGER_64\n");
  139. break;
  140. case L2_OP_ALLOC_REAL_32:
  141. printf("ALLOC_REAL_32\n");
  142. break;
  143. case L2_OP_ALLOC_REAL_64:
  144. printf("ALLOC_REAL_64\n");
  145. break;
  146. case L2_OP_ALLOC_BUFFER_STATIC:
  147. printf("ALLOC_BUFFER_STATIC\n");
  148. break;
  149. case L2_OP_ALLOC_BUFFER_ZERO:
  150. printf("ALLOC_BUFFER_ZERO\n");
  151. break;
  152. case L2_OP_ALLOC_ARRAY:
  153. printf("ALLOC_ARRAY\n");
  154. break;
  155. case L2_OP_ALLOC_NAMESPACE:
  156. printf("ALLOC_NAMESPACE\n");
  157. break;
  158. case L2_OP_ALLOC_FUNCTION:
  159. printf("ALLOC_FUNCTION\n");
  160. break;
  161. case L2_OP_NAMESPACE_SET:
  162. printf("NAMESPACE_SET\n");
  163. break;
  164. case L2_OP_HALT:
  165. printf("HALT\n");
  166. break;
  167. default:
  168. {
  169. l2_word word = (l2_word)opcode;
  170. char bytes[sizeof(word)];
  171. memcpy(&bytes, &word, sizeof(word));
  172. printf("?");
  173. for (size_t i = 0; i < sizeof(bytes); ++i) {
  174. printf(" %02x", bytes[i]);
  175. }
  176. printf("\n");
  177. }
  178. }
  179. }
  180. void l2_vm_print_bytecode(l2_word *ops, size_t opcount) {
  181. size_t ptr = 0;
  182. while (ptr < opcount) {
  183. l2_vm_print_op(ops, opcount, &ptr);
  184. }
  185. }