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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 (l2_vm_value_type(val)) {
  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->array == NULL) {
  18. printf("ARRAY, empty\n");
  19. return;
  20. }
  21. printf("ARRAY, len %zu\n", val->array->len);
  22. for (size_t i = 0; i < val->array->len; ++i) {
  23. printf(" %zu: %u\n", i, val->array->data[i]);
  24. }
  25. }
  26. break;
  27. case L2_VAL_TYPE_BUFFER:
  28. {
  29. if (val->buffer == NULL) {
  30. printf("BUFFER, empty\n");
  31. return;
  32. }
  33. printf("BUFFER, len %zu\n", val->buffer->len);
  34. for (size_t i = 0; i < val->buffer->len; ++i) {
  35. printf(" %zu: %c\n", i, val->buffer->data[i]);
  36. }
  37. }
  38. break;
  39. case L2_VAL_TYPE_NAMESPACE:
  40. {
  41. if (val->ns == NULL) {
  42. printf("NAMESPACE, empty, parent %u\n", val->extra.ns_parent);
  43. return;
  44. }
  45. printf("NAMESPACE, len %zu, parent %u\n", val->ns->len, val->extra.ns_parent);
  46. for (size_t i = 0; i < val->ns->size; ++i) {
  47. l2_word key = val->ns->data[i];
  48. l2_word v = val->ns->data[val->ns->size + i];
  49. if (key == 0 || key == ~(l2_word)0) continue;
  50. printf(" %u: %u\n", key, v);
  51. }
  52. }
  53. break;
  54. case L2_VAL_TYPE_FUNCTION:
  55. printf("FUNCTION, pos %u, ns %u\n", val->func.pos, val->func.namespace);
  56. break;
  57. case L2_VAL_TYPE_CFUNCTION:
  58. printf("C FUNCTION, %p\n", val->cfunc);
  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. }