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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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, parent %u\n", ns->len, ns->parent);
  49. for (size_t i = 0; i < ns->size; ++i) {
  50. l2_word key = ns->data[i];
  51. l2_word val = ns->data[ns->size + i];
  52. if (key == 0 || key == ~(l2_word)0) continue;
  53. printf(" %u: %u\n", key, val);
  54. }
  55. }
  56. break;
  57. case L2_VAL_TYPE_FUNCTION:
  58. printf("FUNCTION, pos %u, ns %u\n", val->func.pos, val->func.namespace);
  59. break;
  60. }
  61. }
  62. void l2_vm_print_state(struct l2_vm *vm) {
  63. printf("Stack:\n");
  64. l2_vm_print_stack(vm);
  65. printf("Heap:\n");
  66. l2_vm_print_heap(vm);
  67. printf("Namespace Stack:\n");
  68. l2_vm_print_nstack(vm);
  69. }
  70. void l2_vm_print_heap(struct l2_vm *vm) {
  71. for (l2_word i = 0; i < vm->valuessize; ++i) {
  72. if (l2_bitset_get(&vm->valueset, i)) {
  73. printf(" %u: ", i);
  74. l2_vm_print_val(&vm->values[i]);
  75. }
  76. }
  77. }
  78. void l2_vm_print_stack(struct l2_vm *vm) {
  79. for (l2_word i = 0; i < vm->sptr; ++i) {
  80. printf(" %i: %i\n", i, vm->stack[i]);
  81. }
  82. }
  83. void l2_vm_print_nstack(struct l2_vm *vm) {
  84. for (l2_word i = 0; i < vm->nsptr; ++i) {
  85. printf(" %i: %i\n", i, vm->nstack[i]);
  86. }
  87. }
  88. static void print_op_num(l2_word *ops, size_t opcount, size_t ptr) {
  89. if (ptr >= opcount) {
  90. printf("<EOF>");
  91. } else {
  92. printf("%i", ops[ptr]);
  93. }
  94. }
  95. void l2_vm_print_op(l2_word *ops, size_t opcount, size_t *ptr) {
  96. enum l2_opcode opcode = (enum l2_opcode)ops[(*ptr)++];
  97. switch (opcode) {
  98. case L2_OP_NOP:
  99. printf("NOP\n");
  100. break;
  101. case L2_OP_PUSH:
  102. printf("PUSH ");
  103. print_op_num(ops, opcount, (*ptr)++);
  104. printf("\n");
  105. break;
  106. case L2_OP_PUSH_2:
  107. printf("PUSH2 ");
  108. print_op_num(ops, opcount, (*ptr)++);
  109. printf(" ");
  110. print_op_num(ops, opcount, (*ptr)++);
  111. printf("\n");
  112. break;
  113. case L2_OP_POP:
  114. printf("POP\n");
  115. break;
  116. case L2_OP_DUP:
  117. printf("DUP\n");
  118. break;
  119. case L2_OP_ADD:
  120. printf("ADD\n");
  121. break;
  122. case L2_OP_FUNC_CALL:
  123. printf("FUNC_CALL\n");
  124. break;
  125. case L2_OP_RJMP:
  126. printf("RJMP\n");
  127. break;
  128. case L2_OP_STACK_FRAME_LOOKUP:
  129. printf("STACK_FRAME_LOOKUP\n");
  130. break;
  131. case L2_OP_STACK_FRAME_SET:
  132. printf("STACK_FRAME_SET\n");
  133. break;
  134. case L2_OP_RET:
  135. printf("RET\n");
  136. break;
  137. case L2_OP_ALLOC_INTEGER_32:
  138. printf("ALLOC_INTEGER_32\n");
  139. break;
  140. case L2_OP_ALLOC_INTEGER_64:
  141. printf("ALLOC_INTEGER_64\n");
  142. break;
  143. case L2_OP_ALLOC_REAL_32:
  144. printf("ALLOC_REAL_32\n");
  145. break;
  146. case L2_OP_ALLOC_REAL_64:
  147. printf("ALLOC_REAL_64\n");
  148. break;
  149. case L2_OP_ALLOC_BUFFER_STATIC:
  150. printf("ALLOC_BUFFER_STATIC\n");
  151. break;
  152. case L2_OP_ALLOC_BUFFER_ZERO:
  153. printf("ALLOC_BUFFER_ZERO\n");
  154. break;
  155. case L2_OP_ALLOC_ARRAY:
  156. printf("ALLOC_ARRAY\n");
  157. break;
  158. case L2_OP_ALLOC_NAMESPACE:
  159. printf("ALLOC_NAMESPACE\n");
  160. break;
  161. case L2_OP_ALLOC_FUNCTION:
  162. printf("ALLOC_FUNCTION\n");
  163. break;
  164. case L2_OP_NAMESPACE_SET:
  165. printf("NAMESPACE_SET\n");
  166. break;
  167. case L2_OP_HALT:
  168. printf("HALT\n");
  169. break;
  170. default:
  171. {
  172. l2_word word = (l2_word)opcode;
  173. char bytes[sizeof(word)];
  174. memcpy(&bytes, &word, sizeof(word));
  175. printf("?");
  176. for (size_t i = 0; i < sizeof(bytes); ++i) {
  177. printf(" %02x", bytes[i]);
  178. }
  179. printf("\n");
  180. }
  181. }
  182. }
  183. void l2_vm_print_bytecode(l2_word *ops, size_t opcount) {
  184. size_t ptr = 0;
  185. while (ptr < opcount) {
  186. l2_vm_print_op(ops, opcount, &ptr);
  187. }
  188. }