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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. #include "vm/vm.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include "vm/builtins.h"
  5. static l2_word alloc_val(struct l2_vm *vm) {
  6. size_t id = l2_bitset_set_next(&vm->valueset);
  7. if (id >= vm->valuessize) {
  8. if (vm->valuessize == 0) {
  9. vm->valuessize = 16;
  10. }
  11. while (id >= vm->valuessize) {
  12. vm->valuessize *= 2;
  13. }
  14. vm->values = realloc(vm->values, sizeof(*vm->values) * vm->valuessize);
  15. }
  16. return (l2_word)id;
  17. }
  18. static float u32_to_float(uint32_t num) {
  19. float f;
  20. memcpy(&f, &num, sizeof(num));
  21. return f;
  22. }
  23. static double u32s_to_double(uint32_t high, uint32_t low) {
  24. double d;
  25. uint64_t num = (uint64_t)high << 32 | (uint64_t)low;
  26. memcpy(&d, &num, sizeof(num));
  27. return d;
  28. }
  29. static void gc_mark_array(struct l2_vm *vm, struct l2_vm_value *val);
  30. static void gc_mark_namespace(struct l2_vm *vm, struct l2_vm_value *val);
  31. static void gc_mark(struct l2_vm *vm, l2_word id) {
  32. struct l2_vm_value *val = &vm->values[id];
  33. if (val->flags & L2_VAL_MARKED) {
  34. return;
  35. }
  36. printf("GC MARK %i\n", id);
  37. val->flags |= L2_VAL_MARKED;
  38. int typ = l2_vm_value_type(val);
  39. if (typ == L2_VAL_TYPE_ARRAY) {
  40. gc_mark_array(vm, val);
  41. } else if (typ == L2_VAL_TYPE_NAMESPACE) {
  42. gc_mark_namespace(vm, val);
  43. } else if (typ == L2_VAL_TYPE_FUNCTION) {
  44. gc_mark(vm, val->func.namespace);
  45. }
  46. }
  47. static void gc_mark_array(struct l2_vm *vm, struct l2_vm_value *val) {
  48. if (val->array == NULL) {
  49. return;
  50. }
  51. for (size_t i = 0; i < val->array->len; ++i) {
  52. gc_mark(vm, val->array->data[i]);
  53. }
  54. }
  55. static void gc_mark_namespace(struct l2_vm *vm, struct l2_vm_value *val) {
  56. if (val->ns == NULL) {
  57. return;
  58. }
  59. for (size_t i = 0; i < val->ns->size; ++i) {
  60. l2_word key = val->ns->data[i];
  61. if (key == 0 || key == ~(l2_word)0) {
  62. continue;
  63. }
  64. gc_mark(vm, val->ns->data[val->ns->size + i]);
  65. }
  66. if (val->extra.ns_parent != 0) {
  67. gc_mark(vm, val->extra.ns_parent);
  68. }
  69. }
  70. static void gc_free(struct l2_vm *vm, l2_word id) {
  71. struct l2_vm_value *val = &vm->values[id];
  72. l2_bitset_unset(&vm->valueset, id);
  73. // Don't need to do anything more; the next round of GC will free
  74. // whichever values were only referenced by the array
  75. int typ = l2_vm_value_type(val);
  76. if (typ == L2_VAL_TYPE_ARRAY || typ == L2_VAL_TYPE_BUFFER || typ == L2_VAL_TYPE_NAMESPACE) {
  77. free(val->array);
  78. } else if (typ == L2_VAL_TYPE_BUFFER) {
  79. free(val->buffer);
  80. } else if (typ == L2_VAL_TYPE_NAMESPACE) {
  81. free(val->ns);
  82. }
  83. }
  84. static size_t gc_sweep(struct l2_vm *vm) {
  85. size_t freed = 0;
  86. // Skip ID 0, because that should always exist
  87. for (size_t i = 1; i < vm->valuessize; ++i) {
  88. if (!l2_bitset_get(&vm->valueset, i)) {
  89. continue;
  90. }
  91. struct l2_vm_value *val = &vm->values[i];
  92. if (!(val->flags & L2_VAL_MARKED)) {
  93. printf("GC FREE %zi\n", i);
  94. gc_free(vm, i);
  95. freed += 1;
  96. } else {
  97. val->flags &= ~L2_VAL_MARKED;
  98. }
  99. }
  100. return freed;
  101. }
  102. void l2_vm_init(struct l2_vm *vm, l2_word *ops, size_t opcount) {
  103. vm->ops = ops;
  104. vm->opcount = opcount;
  105. vm->iptr = 0;
  106. vm->sptr = 0;
  107. vm->nsptr = 0;
  108. vm->values = NULL;
  109. vm->valuessize = 0;
  110. l2_bitset_init(&vm->valueset);
  111. // It's wasteful to allocate new 'none' variables all the time,
  112. // variable ID 0 should be the only 'none' variable in the system
  113. l2_word none_id = alloc_val(vm);
  114. vm->values[none_id].flags = L2_VAL_TYPE_NONE | L2_VAL_CONST;
  115. // Need to allocate a builtins namespace
  116. l2_word builtins = alloc_val(vm);
  117. vm->values[builtins].extra.ns_parent = 0;
  118. vm->values[builtins].ns = NULL; // Will be allocated on first insert
  119. vm->values[builtins].flags = L2_VAL_TYPE_NAMESPACE;
  120. vm->nstack[vm->nsptr++] = builtins;
  121. // Need to allocate a root namespace
  122. l2_word root = alloc_val(vm);
  123. vm->values[root].extra.ns_parent = builtins;
  124. vm->values[root].ns = NULL;
  125. vm->values[root].flags = L2_VAL_TYPE_NAMESPACE;
  126. vm->nstack[vm->nsptr++] = root;
  127. // Define a C function variable for every builtin
  128. l2_word id;
  129. l2_word key = 1;
  130. #define X(name, f) \
  131. id = alloc_val(vm); \
  132. vm->values[id].flags = L2_VAL_TYPE_CFUNCTION; \
  133. vm->values[id].cfunc = f; \
  134. l2_vm_namespace_set(&vm->values[builtins], key++, id);
  135. #include "builtins.x.h"
  136. #undef X
  137. }
  138. l2_word l2_vm_alloc(struct l2_vm *vm, enum l2_value_type typ, enum l2_value_flags flags) {
  139. l2_word id = alloc_val(vm);
  140. memset(&vm->values[id], 0, sizeof(vm->values[id]));
  141. vm->values[id].flags = typ | flags;
  142. return id;
  143. }
  144. void l2_vm_free(struct l2_vm *vm) {
  145. // Skip ID 0, because that's always NONE
  146. for (size_t i = 1; i < vm->valuessize; ++i) {
  147. if (!l2_bitset_get(&vm->valueset, i)) {
  148. continue;
  149. }
  150. gc_free(vm, i);
  151. }
  152. free(vm->values);
  153. l2_bitset_free(&vm->valueset);
  154. }
  155. size_t l2_vm_gc(struct l2_vm *vm) {
  156. for (l2_word nsptr = 0; nsptr < vm->nsptr; ++nsptr) {
  157. struct l2_vm_value *val = &vm->values[vm->nstack[nsptr]];
  158. val->flags |= L2_VAL_MARKED;
  159. gc_mark_namespace(vm, val);
  160. }
  161. return gc_sweep(vm);
  162. }
  163. void l2_vm_run(struct l2_vm *vm) {
  164. while ((enum l2_opcode)vm->ops[vm->iptr] != L2_OP_HALT) {
  165. l2_vm_step(vm);
  166. }
  167. }
  168. void l2_vm_step(struct l2_vm *vm) {
  169. enum l2_opcode opcode = (enum l2_opcode)vm->ops[vm->iptr++];
  170. l2_word word;
  171. switch (opcode) {
  172. case L2_OP_NOP:
  173. break;
  174. case L2_OP_PUSH:
  175. vm->stack[vm->sptr] = vm->ops[vm->iptr];
  176. vm->sptr += 1;
  177. vm->iptr += 1;
  178. break;
  179. case L2_OP_PUSH_2:
  180. vm->stack[vm->sptr] = vm->ops[vm->iptr];
  181. vm->stack[vm->sptr + 1] = vm->ops[vm->iptr + 1];
  182. vm->sptr += 2;
  183. vm->iptr += 2;
  184. break;
  185. case L2_OP_POP:
  186. vm->sptr -= 1;
  187. break;
  188. case L2_OP_DUP:
  189. vm->stack[vm->sptr] = vm->ops[vm->sptr - 1];
  190. vm->sptr += 1;
  191. break;
  192. case L2_OP_ADD:
  193. vm->stack[vm->sptr - 2] += vm->stack[vm->sptr - 1];
  194. vm->sptr -= 1;
  195. break;
  196. case L2_OP_FUNC_CALL:
  197. {
  198. l2_word argc = vm->stack[--vm->sptr];
  199. l2_word arr_id = alloc_val(vm);
  200. vm->values[arr_id].flags = L2_VAL_TYPE_ARRAY;
  201. vm->values[arr_id].array = malloc(
  202. sizeof(struct l2_vm_array) + sizeof(l2_word) * argc);
  203. struct l2_vm_array *arr = vm->values[arr_id].array;
  204. arr->len = argc;
  205. arr->size = argc;
  206. vm->sptr -= argc;
  207. for (l2_word i = 0; i < argc; ++i) {
  208. arr->data[i] = vm->stack[vm->sptr + i];
  209. }
  210. l2_word func_id = vm->stack[--vm->sptr];
  211. struct l2_vm_value *func = &vm->values[func_id];
  212. enum l2_value_type typ = l2_vm_value_type(func);
  213. // C functions are called differently from language functions
  214. if (typ == L2_VAL_TYPE_CFUNCTION) {
  215. vm->stack[vm->sptr++] = func->cfunc(vm, arr);
  216. break;
  217. }
  218. // Don't interpret a non-function as a function
  219. if (typ != L2_VAL_TYPE_FUNCTION) {
  220. // TODO: Error mechanism
  221. break;
  222. }
  223. vm->stack[vm->sptr++] = vm->iptr;
  224. vm->stack[vm->sptr++] = arr_id;
  225. l2_word ns_id = alloc_val(vm);
  226. vm->values[ns_id].extra.ns_parent = func->func.namespace;
  227. vm->values[ns_id].ns = NULL;
  228. vm->values[ns_id].flags = L2_VAL_TYPE_NAMESPACE;
  229. vm->nstack[vm->nsptr++] = ns_id;
  230. vm->iptr = func->func.pos;
  231. }
  232. break;
  233. case L2_OP_RJMP:
  234. word = vm->stack[vm->sptr - 1];
  235. vm->sptr -= 1;
  236. vm->iptr += word;
  237. break;
  238. case L2_OP_STACK_FRAME_LOOKUP:
  239. {
  240. l2_word key = vm->stack[vm->sptr - 1];
  241. struct l2_vm_value *ns = &vm->values[vm->nstack[vm->nsptr - 1]];
  242. vm->stack[vm->sptr - 1] = l2_vm_namespace_get(vm, ns, key);
  243. }
  244. break;
  245. case L2_OP_STACK_FRAME_SET:
  246. {
  247. l2_word key = vm->stack[--vm->sptr];
  248. l2_word val = vm->stack[vm->sptr - 1];
  249. struct l2_vm_value *ns = &vm->values[vm->nstack[vm->nsptr - 1]];
  250. l2_vm_namespace_set(ns, key, val);
  251. }
  252. break;
  253. case L2_OP_STACK_FRAME_REPLACE:
  254. {
  255. l2_word key = vm->stack[--vm->sptr];
  256. l2_word val = vm->stack[vm->sptr - 1];
  257. struct l2_vm_value *ns = &vm->values[vm->nstack[vm->nsptr - 1]];
  258. l2_vm_namespace_replace(vm, ns, key, val); // TODO: error if returns -1
  259. }
  260. break;
  261. case L2_OP_RET:
  262. {
  263. l2_word retval = vm->stack[--vm->sptr];
  264. vm->sptr -= 1; // Discard arguments array
  265. l2_word retaddr = vm->stack[--vm->sptr];
  266. vm->stack[vm->sptr++] = retval;
  267. vm->nsptr -= 1; // Pop function stack frame
  268. vm->iptr = retaddr;
  269. }
  270. break;
  271. case L2_OP_ALLOC_ATOM:
  272. word = alloc_val(vm);
  273. vm->values[word].flags = L2_VAL_TYPE_ATOM;
  274. vm->values[word].atom= vm->stack[--vm->sptr];
  275. vm->stack[vm->sptr++] = word;
  276. break;
  277. case L2_OP_ALLOC_REAL:
  278. word = alloc_val(vm);
  279. vm->values[word].flags = L2_VAL_TYPE_REAL;
  280. vm->values[word].real = u32s_to_double(vm->stack[vm->sptr - 1], vm->stack[vm->sptr - 2]);
  281. vm->sptr -= 2;
  282. vm->stack[vm->sptr] = word;
  283. vm->sptr += 1;
  284. break;
  285. case L2_OP_ALLOC_BUFFER_STATIC:
  286. {
  287. word = alloc_val(vm);
  288. l2_word length = vm->stack[--vm->sptr];
  289. l2_word offset = vm->stack[--vm->sptr];
  290. vm->values[word].flags = L2_VAL_TYPE_BUFFER;
  291. vm->values[word].buffer = malloc(sizeof(struct l2_vm_buffer) + length);
  292. vm->values[word].buffer->len = length;
  293. memcpy(
  294. (unsigned char *)vm->values[word].buffer + sizeof(struct l2_vm_buffer),
  295. vm->ops + offset, length);
  296. vm->stack[vm->sptr] = word;
  297. vm->sptr += 1;
  298. }
  299. break;
  300. case L2_OP_ALLOC_BUFFER_ZERO:
  301. {
  302. word = alloc_val(vm);
  303. l2_word length = vm->stack[--vm->sptr];
  304. vm->values[word].flags = L2_VAL_TYPE_BUFFER;
  305. vm->values[word].buffer = calloc(1, sizeof(struct l2_vm_buffer) + length);
  306. vm->values[word].buffer->len = length;
  307. vm->stack[vm->sptr] = word;
  308. vm->sptr += 1;
  309. }
  310. break;
  311. case L2_OP_ALLOC_ARRAY:
  312. {
  313. l2_word count = vm->stack[--vm->sptr];
  314. l2_word arr_id = alloc_val(vm);
  315. struct l2_vm_value *arr = &vm->values[arr_id];
  316. arr->flags = L2_VAL_TYPE_ARRAY;
  317. if (count == 0) {
  318. arr->array = NULL;
  319. vm->stack[vm->sptr++] = arr_id;
  320. break;
  321. }
  322. arr->array = malloc(sizeof(struct l2_vm_array) + count * sizeof(l2_word));
  323. arr->array->len = count;
  324. arr->array->size = count;
  325. for (l2_word i = 0; i < count; ++i) {
  326. arr->array->data[count - 1 - i] = vm->stack[--vm->sptr];
  327. }
  328. vm->stack[vm->sptr++] = arr_id;
  329. }
  330. break;
  331. case L2_OP_ALLOC_NAMESPACE:
  332. word = alloc_val(vm);
  333. vm->values[word].flags = L2_VAL_TYPE_NAMESPACE;
  334. vm->values[word].extra.ns_parent = 0;
  335. vm->values[word].ns = NULL; // Will be allocated on first insert
  336. vm->stack[vm->sptr] = word;
  337. vm->sptr += 1;
  338. break;
  339. case L2_OP_ALLOC_FUNCTION:
  340. word = alloc_val(vm);
  341. vm->values[word].flags = L2_VAL_TYPE_FUNCTION;
  342. vm->values[word].func.pos = vm->stack[--vm->sptr];
  343. vm->values[word].func.namespace = vm->nstack[vm->nsptr - 1];
  344. vm->stack[vm->sptr] = word;
  345. vm->sptr += 1;
  346. break;
  347. case L2_OP_NAMESPACE_SET:
  348. {
  349. l2_word key = vm->stack[--vm->sptr];
  350. l2_word val = vm->stack[vm->sptr - 1];
  351. l2_word ns = vm->stack[vm->sptr - 2];
  352. l2_vm_namespace_set(&vm->values[ns], key, val);
  353. }
  354. break;
  355. case L2_OP_NAMESPACE_LOOKUP:
  356. {
  357. l2_word key = vm->stack[--vm->sptr];
  358. l2_word ns = vm->stack[--vm->sptr];
  359. vm->stack[vm->sptr++] = l2_vm_namespace_get(vm, &vm->values[ns], key);
  360. }
  361. break;
  362. case L2_OP_DIRECT_ARRAY_LOOKUP:
  363. {
  364. l2_word key = vm->stack[--vm->sptr];
  365. l2_word arr = vm->stack[--vm->sptr];
  366. // TODO: Error if out of bounds or incorrect type
  367. vm->stack[vm->sptr++] = vm->values[arr].array->data[key];
  368. }
  369. break;
  370. case L2_OP_HALT:
  371. break;
  372. }
  373. }