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.

vm.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include "vm/vm.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. static l2_word alloc_val(struct l2_vm *vm) {
  5. size_t id = l2_bitset_set_next(&vm->valueset);
  6. if (id >= vm->valuessize) {
  7. if (vm->valuessize == 0) {
  8. vm->valuessize = 16;
  9. }
  10. while (id > vm->valuessize) {
  11. vm->valuessize *= 2;
  12. }
  13. vm->values = realloc(vm->values, sizeof(*vm->values) * vm->valuessize);
  14. }
  15. return (l2_word)id;
  16. }
  17. static float u32_to_float(uint32_t num) {
  18. float f;
  19. memcpy(&f, &num, sizeof(num));
  20. return f;
  21. }
  22. static double u32s_to_double(uint32_t high, uint32_t low) {
  23. double d;
  24. uint64_t num = (uint64_t)high << 32 | (uint64_t)low;
  25. memcpy(&d, &num, sizeof(num));
  26. return d;
  27. }
  28. static void gc_mark_array(struct l2_vm *vm, struct l2_vm_value *val);
  29. static void gc_mark_namespace(struct l2_vm *vm, struct l2_vm_value *val);
  30. static void gc_mark(struct l2_vm *vm, l2_word id) {
  31. printf("GC MARK %i\n", id);
  32. struct l2_vm_value *val = &vm->values[id];
  33. val->flags |= L2_VAL_MARKED;
  34. int typ = l2_vm_value_type(val);
  35. if (typ == L2_VAL_TYPE_ARRAY) {
  36. gc_mark_array(vm, val);
  37. } else if (typ == L2_VAL_TYPE_NAMESPACE) {
  38. gc_mark_namespace(vm, val);
  39. }
  40. }
  41. static void gc_mark_array(struct l2_vm *vm, struct l2_vm_value *val) {
  42. if (val->data == NULL) {
  43. return;
  44. }
  45. struct l2_vm_array *arr = (struct l2_vm_array *)val->data;
  46. for (size_t i = 0; i < arr->len; ++i) {
  47. gc_mark(vm, arr->data[i]);
  48. }
  49. }
  50. static void gc_mark_namespace(struct l2_vm *vm, struct l2_vm_value *val) {
  51. if (val->data == NULL) {
  52. return;
  53. }
  54. struct l2_vm_namespace *ns = (struct l2_vm_namespace *)val->data;
  55. for (size_t i = 0; i < ns->size; ++i) {
  56. l2_word key = ns->data[i];
  57. if (key == 0 || key == ~(l2_word)0) {
  58. continue;
  59. }
  60. gc_mark(vm, ns->data[ns->size + i]);
  61. }
  62. }
  63. static void gc_free(struct l2_vm *vm, l2_word id) {
  64. struct l2_vm_value *val = &vm->values[id];
  65. l2_bitset_unset(&vm->valueset, id);
  66. int typ = l2_vm_value_type(val);
  67. if (typ == L2_VAL_TYPE_ARRAY || typ == L2_VAL_TYPE_BUFFER || typ == L2_VAL_TYPE_NAMESPACE) {
  68. free(val->data);
  69. // Don't need to do anything more; the next round of GC will free
  70. // whichever values were only referenced by the array
  71. }
  72. }
  73. static size_t gc_sweep(struct l2_vm *vm) {
  74. size_t freed = 0;
  75. // Skip ID 0, because that should always exist
  76. for (size_t i = 1; i < vm->valuessize; ++i) {
  77. if (!l2_bitset_get(&vm->valueset, i)) {
  78. continue;
  79. }
  80. struct l2_vm_value *val = &vm->values[i];
  81. if (!(val->flags & L2_VAL_MARKED)) {
  82. gc_free(vm, i);
  83. freed += 1;
  84. } else {
  85. val->flags &= ~L2_VAL_MARKED;
  86. }
  87. }
  88. return freed;
  89. }
  90. void l2_vm_init(struct l2_vm *vm, l2_word *ops, size_t opcount) {
  91. vm->ops = ops;
  92. vm->opcount = opcount;
  93. vm->iptr = 0;
  94. vm->sptr = 0;
  95. vm->nsptr = 0;
  96. vm->values = NULL;
  97. vm->valuessize = 0;
  98. l2_bitset_init(&vm->valueset);
  99. // It's wasteful to allocate new 'none' variables all the time,
  100. // variable ID 0 should be the only 'none' variable in the system
  101. l2_word none_id = alloc_val(vm);
  102. vm->values[none_id].flags = L2_VAL_TYPE_NONE | L2_VAL_CONST;
  103. }
  104. void l2_vm_free(struct l2_vm *vm) {
  105. // Skip ID 0, because that's always NONE
  106. for (size_t i = 1; i < vm->valuessize; ++i) {
  107. if (!l2_bitset_get(&vm->valueset, i)) {
  108. continue;
  109. }
  110. gc_free(vm, i);
  111. }
  112. free(vm->values);
  113. l2_bitset_free(&vm->valueset);
  114. }
  115. size_t l2_vm_gc(struct l2_vm *vm) {
  116. for (l2_word nsptr = 0; nsptr < vm->nsptr; ++nsptr) {
  117. struct l2_vm_value *val = &vm->values[vm->nstack[nsptr]];
  118. val->flags |= L2_VAL_MARKED;
  119. gc_mark_namespace(vm, val);
  120. }
  121. return gc_sweep(vm);
  122. }
  123. void l2_vm_run(struct l2_vm *vm) {
  124. while ((enum l2_opcode)vm->ops[vm->iptr] != L2_OP_HALT) {
  125. l2_vm_step(vm);
  126. }
  127. }
  128. void l2_vm_step(struct l2_vm *vm) {
  129. enum l2_opcode opcode = (enum l2_opcode)vm->ops[vm->iptr++];
  130. l2_word word;
  131. switch (opcode) {
  132. case L2_OP_NOP:
  133. break;
  134. case L2_OP_PUSH:
  135. vm->stack[vm->sptr] = vm->ops[vm->iptr];
  136. vm->sptr += 1;
  137. vm->iptr += 1;
  138. break;
  139. case L2_OP_PUSH_2:
  140. vm->stack[vm->sptr] = vm->ops[vm->iptr];
  141. vm->stack[vm->sptr + 1] = vm->ops[vm->iptr + 1];
  142. vm->sptr += 2;
  143. vm->iptr += 2;
  144. break;
  145. case L2_OP_POP:
  146. vm->sptr -= 1;
  147. break;
  148. case L2_OP_DUP:
  149. vm->stack[vm->sptr] = vm->ops[vm->sptr - 1];
  150. vm->sptr += 1;
  151. break;
  152. case L2_OP_ADD:
  153. vm->stack[vm->sptr - 2] += vm->stack[vm->sptr - 1];
  154. vm->sptr -= 1;
  155. break;
  156. case L2_OP_CALL:
  157. word = vm->stack[vm->sptr - 1];
  158. vm->stack[vm->sptr - 1] = vm->iptr + 1;
  159. vm->iptr = word;
  160. break;
  161. case L2_OP_GEN_STACK_FRAME:
  162. word = alloc_val(vm);
  163. vm->values[word].flags = L2_VAL_TYPE_NAMESPACE;
  164. vm->values[word].data = NULL; // Will be allocated on first insert
  165. vm->nstack[vm->nsptr] = word;
  166. vm->nsptr += 1;
  167. break;
  168. case L2_OP_STACK_FRAME_LOOKUP:
  169. {
  170. l2_word key = vm->stack[vm->sptr - 1];
  171. struct l2_vm_value *ns = &vm->values[vm->nstack[vm->nsptr - 1]];
  172. vm->stack[vm->sptr - 1] = l2_vm_namespace_get(ns, key);
  173. }
  174. break;
  175. case L2_OP_STACK_FRAME_SET:
  176. {
  177. l2_word key = vm->stack[vm->sptr - 1];
  178. l2_word val = vm->stack[vm->sptr - 2];
  179. struct l2_vm_value *ns = &vm->values[vm->nstack[vm->nsptr - 1]];
  180. l2_vm_namespace_set(ns, key, val);
  181. vm->sptr -= 1;
  182. }
  183. break;
  184. case L2_OP_RET:
  185. vm->nsptr -= 1;
  186. vm->iptr = vm->stack[vm->sptr - 1];
  187. vm->sptr -= 1;
  188. vm->nsptr -= 1;
  189. break;
  190. case L2_OP_ALLOC_INTEGER_32:
  191. word = alloc_val(vm);
  192. vm->values[word].flags = L2_VAL_TYPE_INTEGER;
  193. vm->values[word].integer = vm->stack[--vm->sptr];
  194. vm->stack[vm->sptr] = word;
  195. vm->sptr += 1;
  196. break;
  197. case L2_OP_ALLOC_INTEGER_64:
  198. word = alloc_val(vm);
  199. vm->values[word].flags = L2_VAL_TYPE_INTEGER;
  200. vm->values[word].integer = (int64_t)(
  201. (uint64_t)vm->stack[vm->sptr - 1] << 32 |
  202. (uint64_t)vm->stack[vm->sptr - 2]);
  203. vm->sptr -= 2;
  204. vm->stack[vm->sptr] = word;
  205. vm->sptr += 1;
  206. break;
  207. case L2_OP_ALLOC_REAL_32:
  208. word = alloc_val(vm);
  209. vm->values[word].flags = L2_VAL_TYPE_REAL;
  210. vm->values[word].real = u32_to_float(vm->stack[--vm->sptr]);
  211. vm->stack[vm->sptr] = word;
  212. vm->sptr += 1;
  213. break;
  214. case L2_OP_ALLOC_REAL_64:
  215. word = alloc_val(vm);
  216. vm->values[word].flags = L2_VAL_TYPE_REAL;
  217. vm->values[word].real = u32s_to_double(vm->stack[vm->sptr - 1], vm->stack[vm->sptr - 2]);
  218. vm->sptr -= 2;
  219. vm->stack[vm->sptr] = word;
  220. vm->sptr += 1;
  221. break;
  222. case L2_OP_ALLOC_BUFFER_STATIC:
  223. {
  224. word = alloc_val(vm);
  225. l2_word length = vm->stack[--vm->sptr];
  226. l2_word offset = vm->stack[--vm->sptr];
  227. vm->values[word].flags = L2_VAL_TYPE_BUFFER;
  228. vm->values[word].data = malloc(sizeof(struct l2_vm_buffer) + length);
  229. ((struct l2_vm_buffer *)vm->values[word].data)->len = length;
  230. memcpy(
  231. (unsigned char *)vm->values[word].data + sizeof(struct l2_vm_buffer),
  232. vm->ops + offset, length);
  233. vm->stack[vm->sptr] = word;
  234. vm->sptr += 1;
  235. }
  236. break;
  237. case L2_OP_ALLOC_BUFFER_ZERO:
  238. {
  239. word = alloc_val(vm);
  240. l2_word length = vm->stack[--vm->sptr];
  241. vm->values[word].flags = L2_VAL_TYPE_BUFFER;
  242. vm->values[word].data = calloc(1, sizeof(struct l2_vm_buffer) + length);
  243. ((struct l2_vm_buffer *)vm->values[word].data)->len = length;
  244. vm->stack[vm->sptr] = word;
  245. vm->sptr += 1;
  246. }
  247. break;
  248. case L2_OP_ALLOC_ARRAY:
  249. word = alloc_val(vm);
  250. vm->values[word].flags = L2_VAL_TYPE_ARRAY;
  251. vm->values[word].data = NULL; // Will be allocated on first insert
  252. vm->stack[vm->sptr] = word;
  253. vm->sptr += 1;
  254. break;
  255. case L2_OP_ALLOC_NAMESPACE:
  256. word = alloc_val(vm);
  257. vm->values[word].flags = L2_VAL_TYPE_NAMESPACE;
  258. vm->values[word].data = NULL; // Will be allocated on first insert
  259. vm->stack[vm->sptr] = word;
  260. vm->sptr += 1;
  261. break;
  262. case L2_OP_NAMESPACE_SET:
  263. {
  264. l2_word key = vm->stack[vm->sptr - 1];
  265. l2_word val = vm->stack[vm->sptr - 2];
  266. l2_word ns = vm->stack[vm->sptr - 3];
  267. l2_vm_namespace_set(&vm->values[ns], key, val);
  268. vm->sptr -= 1;
  269. }
  270. break;
  271. case L2_OP_HALT:
  272. break;
  273. }
  274. }