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

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