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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. #include "vm/vm.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include "vm/builtins.h"
  6. static int stdio_inited = 0;
  7. static struct l2_io_file_writer std_output;
  8. static struct l2_io_file_writer std_error;
  9. static l2_word alloc_val(struct l2_vm *vm) {
  10. size_t id = l2_bitset_set_next(&vm->valueset);
  11. if (id + 16 >= vm->valuessize) {
  12. if (id >= vm->valuessize) {
  13. if (vm->valuessize == 0) {
  14. vm->valuessize = 64;
  15. }
  16. while (id >= vm->valuessize) {
  17. vm->valuessize *= 2;
  18. }
  19. vm->values = realloc(vm->values, sizeof(*vm->values) * vm->valuessize);
  20. } else {
  21. vm->gc_scheduled = 1;
  22. }
  23. }
  24. return (l2_word)id;
  25. }
  26. static double u32s_to_double(uint32_t high, uint32_t low) {
  27. double d;
  28. uint64_t num = (uint64_t)high << 32 | (uint64_t)low;
  29. memcpy(&d, &num, sizeof(num));
  30. return d;
  31. }
  32. static void gc_mark_array(struct l2_vm *vm, struct l2_vm_value *val);
  33. static void gc_mark_namespace(struct l2_vm *vm, struct l2_vm_value *val);
  34. static void gc_mark(struct l2_vm *vm, l2_word id) {
  35. struct l2_vm_value *val = &vm->values[id];
  36. if (val->flags & L2_VAL_MARKED) {
  37. return;
  38. }
  39. val->flags |= L2_VAL_MARKED;
  40. int typ = l2_vm_value_type(val);
  41. if (typ == L2_VAL_TYPE_ARRAY) {
  42. gc_mark_array(vm, val);
  43. } else if (typ == L2_VAL_TYPE_NAMESPACE) {
  44. gc_mark_namespace(vm, val);
  45. } else if (typ == L2_VAL_TYPE_FUNCTION) {
  46. gc_mark(vm, val->func.ns);
  47. }
  48. }
  49. static void gc_mark_array(struct l2_vm *vm, struct l2_vm_value *val) {
  50. if (val->array == NULL) {
  51. return;
  52. }
  53. for (size_t i = 0; i < val->array->len; ++i) {
  54. gc_mark(vm, val->array->data[i]);
  55. }
  56. }
  57. static void gc_mark_namespace(struct l2_vm *vm, struct l2_vm_value *val) {
  58. if (val->extra.ns_parent != 0) {
  59. gc_mark(vm, val->extra.ns_parent);
  60. }
  61. if (val->ns == NULL) {
  62. return;
  63. }
  64. for (size_t i = 0; i < val->ns->size; ++i) {
  65. l2_word key = val->ns->data[i];
  66. if (key == 0 || key == ~(l2_word)0) {
  67. continue;
  68. }
  69. gc_mark(vm, val->ns->data[val->ns->size + i]);
  70. }
  71. }
  72. static void gc_free(struct l2_vm *vm, l2_word id) {
  73. struct l2_vm_value *val = &vm->values[id];
  74. l2_bitset_unset(&vm->valueset, id);
  75. // Don't need to do anything more; the next round of GC will free
  76. // whichever values were only referenced by the array
  77. int typ = l2_vm_value_type(val);
  78. if (typ == L2_VAL_TYPE_ARRAY) {
  79. free(val->array);
  80. } else if (typ == L2_VAL_TYPE_BUFFER) {
  81. free(val->buffer);
  82. } else if (typ == L2_VAL_TYPE_NAMESPACE) {
  83. free(val->ns);
  84. } else if (typ == L2_VAL_TYPE_ERROR) {
  85. free(val->error);
  86. }
  87. }
  88. static size_t gc_sweep(struct l2_vm *vm) {
  89. size_t freed = 0;
  90. for (size_t i = vm->gc_start; i < vm->valuessize; ++i) {
  91. if (!l2_bitset_get(&vm->valueset, i)) {
  92. continue;
  93. }
  94. struct l2_vm_value *val = &vm->values[i];
  95. if (!(val->flags & L2_VAL_MARKED)) {
  96. l2_bitset_unset(&vm->valueset, i);
  97. gc_free(vm, i);
  98. freed += 1;
  99. } else {
  100. val->flags &= ~L2_VAL_MARKED;
  101. }
  102. }
  103. // Normal variables are unmarked by the above loop,
  104. // but builtins don't go through that loop
  105. for (size_t i = 0; i < vm->gc_start; ++i) {
  106. vm->values[i].flags &= ~L2_VAL_MARKED;
  107. }
  108. return freed;
  109. }
  110. const char *l2_value_type_name(enum l2_value_type typ) {
  111. switch (typ) {
  112. case L2_VAL_TYPE_NONE: return "NONE";
  113. case L2_VAL_TYPE_ATOM: return "ATOM";
  114. case L2_VAL_TYPE_REAL: return "REAL";
  115. case L2_VAL_TYPE_BUFFER: return "BUFFER";
  116. case L2_VAL_TYPE_ARRAY: return "ARRAY";
  117. case L2_VAL_TYPE_NAMESPACE: return "NAMESPACE";
  118. case L2_VAL_TYPE_FUNCTION: return "FUNCTION";
  119. case L2_VAL_TYPE_CFUNCTION: return "CFUNCTION";
  120. case L2_VAL_TYPE_ERROR: return "ERROR";
  121. case L2_VAL_TYPE_CONTINUATION: return "CONTINUATION";
  122. }
  123. return "(unknown)";
  124. }
  125. void l2_vm_init(struct l2_vm *vm, l2_word *ops, size_t opcount) {
  126. if (!stdio_inited) {
  127. std_output.w.write = l2_io_file_write;
  128. std_output.f = stdout;
  129. std_error.w.write = l2_io_file_write;
  130. std_error.f = stderr;
  131. stdio_inited = 1;
  132. }
  133. vm->std_output = &std_output.w;
  134. vm->std_error = &std_error.w;
  135. vm->halted = 0;
  136. vm->gc_scheduled = 0;
  137. vm->ops = ops;
  138. vm->opcount = opcount;
  139. vm->iptr = 0;
  140. vm->sptr = 0;
  141. vm->fsptr = 0;
  142. vm->values = NULL;
  143. vm->valuessize = 0;
  144. l2_bitset_init(&vm->valueset);
  145. // It's wasteful to allocate new 'none' variables all the time,
  146. // variable ID 0 should be the only 'none' variable in the system
  147. l2_word none_id = alloc_val(vm);
  148. vm->values[none_id].flags = L2_VAL_TYPE_NONE | L2_VAL_CONST;
  149. // Need to allocate a builtins namespace
  150. l2_word builtins = alloc_val(vm);
  151. vm->values[builtins].extra.ns_parent = 0;
  152. vm->values[builtins].ns = NULL; // Will be allocated on first insert
  153. vm->values[builtins].flags = L2_VAL_TYPE_NAMESPACE;
  154. vm->fstack[vm->fsptr].ns = builtins;
  155. vm->fstack[vm->fsptr].retptr = 0;
  156. vm->fsptr += 1;
  157. // Need to allocate a root namespace
  158. l2_word root = alloc_val(vm);
  159. vm->values[root].extra.ns_parent = builtins;
  160. vm->values[root].ns = NULL;
  161. vm->values[root].flags = L2_VAL_TYPE_NAMESPACE;
  162. vm->fstack[vm->fsptr].ns = root;
  163. vm->fstack[vm->fsptr].retptr = 0;
  164. vm->fsptr += 1;
  165. // Define a C function variable for every builtin
  166. l2_word id;
  167. l2_word key = 1;
  168. #define Y(name, k) \
  169. if (strcmp(#k, "knone") == 0) { \
  170. id = 0; \
  171. l2_vm_namespace_set(&vm->values[builtins], key, id); \
  172. } else { \
  173. id = alloc_val(vm); \
  174. vm->values[id].flags = L2_VAL_TYPE_ATOM | L2_VAL_CONST; \
  175. vm->values[id].atom = key; \
  176. } \
  177. vm->k = id; \
  178. key += 1;
  179. #define X(name, f) \
  180. id = alloc_val(vm); \
  181. vm->values[id].flags = L2_VAL_TYPE_CFUNCTION | L2_VAL_CONST; \
  182. vm->values[id].cfunc = f; \
  183. l2_vm_namespace_set(&vm->values[builtins], key++, id);
  184. #include "builtins.x.h"
  185. #undef Y
  186. #undef X
  187. vm->gc_start = id + 1;
  188. }
  189. l2_word l2_vm_alloc(struct l2_vm *vm, enum l2_value_type typ, enum l2_value_flags flags) {
  190. l2_word id = alloc_val(vm);
  191. memset(&vm->values[id], 0, sizeof(vm->values[id]));
  192. vm->values[id].flags = typ | flags;
  193. return id;
  194. }
  195. l2_word l2_vm_error(struct l2_vm *vm, const char *fmt, ...) {
  196. l2_word id = alloc_val(vm);
  197. struct l2_vm_value *val = &vm->values[id];
  198. val->flags = L2_VAL_CONST | L2_VAL_TYPE_ERROR;
  199. char buf[256];
  200. va_list va;
  201. va_start(va, fmt);
  202. int n = vsnprintf(buf, sizeof(buf), fmt, va);
  203. if (n < 0) {
  204. const char *message = "Failed to generate error message!";
  205. val->error = malloc(strlen(message) + 1);
  206. strcpy(val->error, message);
  207. va_end(va);
  208. return id;
  209. } else if ((size_t)n + 1 < sizeof(buf)) {
  210. val->error = malloc(n + 1);
  211. strcpy(val->error, buf);
  212. va_end(va);
  213. return id;
  214. }
  215. val->error = malloc(n + 1);
  216. vsnprintf(val->error, n + 1, fmt, va);
  217. va_end(va);
  218. return id;
  219. }
  220. l2_word l2_vm_type_error(struct l2_vm *vm, struct l2_vm_value *val) {
  221. return l2_vm_error(vm, "Unexpected type %s", l2_value_type_name(l2_vm_value_type(val)));
  222. }
  223. void l2_vm_free(struct l2_vm *vm) {
  224. // Skip ID 0, because that's always NONE
  225. for (size_t i = 1; i < vm->valuessize; ++i) {
  226. if (!l2_bitset_get(&vm->valueset, i)) {
  227. continue;
  228. }
  229. gc_free(vm, i);
  230. }
  231. free(vm->values);
  232. l2_bitset_free(&vm->valueset);
  233. }
  234. size_t l2_vm_gc(struct l2_vm *vm) {
  235. for (l2_word sptr = 0; sptr < vm->sptr; ++sptr) {
  236. gc_mark(vm, vm->stack[sptr]);
  237. }
  238. // Don't need to mark the first stack frame, since that's where all the
  239. // builtins live, and they aren't sweeped anyways
  240. for (l2_word fsptr = 1; fsptr < vm->fsptr; ++fsptr) {
  241. gc_mark(vm, vm->fstack[fsptr].ns);
  242. }
  243. return gc_sweep(vm);
  244. }
  245. void l2_vm_run(struct l2_vm *vm) {
  246. while (!vm->halted) {
  247. l2_vm_step(vm);
  248. }
  249. }
  250. // The 'call_func' function assumes that all relevant values have been popped off
  251. // the stack, so that the return value can be pushed to the top of the stack
  252. // straight away
  253. static void call_func(
  254. struct l2_vm *vm, l2_word func_id,
  255. l2_word argc, l2_word *argv) {
  256. l2_word stack_base = vm->sptr;
  257. struct l2_vm_value *func = &vm->values[func_id];
  258. enum l2_value_type typ = l2_vm_value_type(func);
  259. // C functions are called differently from language functions
  260. if (typ == L2_VAL_TYPE_CFUNCTION) {
  261. // Make this a while loop, because using call_func would
  262. // make the call stack depth unbounded
  263. vm->stack[vm->sptr++] = func->cfunc(vm, argc, argv);
  264. while (1) {
  265. struct l2_vm_value *val = &vm->values[vm->stack[vm->sptr - 1]];
  266. if (l2_vm_value_type(val) != L2_VAL_TYPE_CONTINUATION) {
  267. break;
  268. }
  269. l2_word cont_id = val->cont.call;
  270. struct l2_vm_value *cont = &vm->values[cont_id];
  271. l2_word new_argc;
  272. l2_word new_argv[1];
  273. if (val->cont.arg == 0) {
  274. new_argc = 0;
  275. } else {
  276. new_argc = 1;
  277. new_argv[0] = val->cont.arg;
  278. }
  279. if (l2_vm_value_type(cont) == L2_VAL_TYPE_CFUNCTION) {
  280. vm->stack[vm->sptr - 1] = cont->cfunc(vm, new_argc, new_argv);
  281. } else {
  282. vm->sptr -= 1;
  283. call_func(vm, cont_id, new_argc, new_argv);
  284. break;
  285. }
  286. }
  287. return;
  288. }
  289. // Don't interpret a non-function as a function
  290. if (typ != L2_VAL_TYPE_FUNCTION) {
  291. vm->stack[vm->sptr++] = l2_vm_error(vm, "Attempt to call non-function");
  292. return;
  293. }
  294. l2_word arr_id = alloc_val(vm);
  295. vm->values[arr_id].flags = L2_VAL_TYPE_ARRAY;
  296. vm->values[arr_id].array = malloc(
  297. sizeof(struct l2_vm_array) + sizeof(l2_word) * argc);
  298. struct l2_vm_array *arr = vm->values[arr_id].array;
  299. arr->len = argc;
  300. arr->size = argc;
  301. for (l2_word i = 0; i < argc; ++i) {
  302. arr->data[i] = argv[i];
  303. }
  304. vm->stack[vm->sptr++] = arr_id;
  305. l2_word ns_id = alloc_val(vm);
  306. func = &vm->values[func_id]; // func might be stale after alloc
  307. vm->values[ns_id].extra.ns_parent = func->func.ns;
  308. vm->values[ns_id].ns = NULL;
  309. vm->values[ns_id].flags = L2_VAL_TYPE_NAMESPACE;
  310. vm->fstack[vm->fsptr].ns = ns_id;
  311. vm->fstack[vm->fsptr].retptr = vm->iptr;
  312. vm->fstack[vm->fsptr].sptr = stack_base;
  313. vm->fsptr += 1;
  314. vm->iptr = func->func.pos;
  315. }
  316. void l2_vm_step(struct l2_vm *vm) {
  317. enum l2_opcode opcode = (enum l2_opcode)vm->ops[vm->iptr++];
  318. l2_word word;
  319. switch (opcode) {
  320. case L2_OP_NOP:
  321. break;
  322. case L2_OP_DISCARD:
  323. vm->sptr -= 1;
  324. if (l2_vm_value_type(&vm->values[vm->stack[vm->sptr]]) == L2_VAL_TYPE_ERROR) {
  325. l2_io_printf(vm->std_error, "Error: %s\n", vm->values[vm->stack[vm->sptr]].error);
  326. vm->halted = 1;
  327. }
  328. break;
  329. case L2_OP_SWAP_DISCARD:
  330. vm->stack[vm->sptr - 2] = vm->stack[vm->sptr - 1];
  331. vm->sptr -= 1;
  332. if (l2_vm_value_type(&vm->values[vm->stack[vm->sptr]]) == L2_VAL_TYPE_ERROR) {
  333. l2_io_printf(vm->std_error, "Error: %s\n", vm->values[vm->stack[vm->sptr]].error);
  334. vm->halted = 1;
  335. }
  336. break;
  337. case L2_OP_DUP:
  338. vm->stack[vm->sptr] = vm->ops[vm->sptr - 1];
  339. vm->sptr += 1;
  340. break;
  341. case L2_OP_ADD:
  342. vm->stack[vm->sptr - 2] += vm->stack[vm->sptr - 1];
  343. vm->sptr -= 1;
  344. break;
  345. case L2_OP_FUNC_CALL:
  346. {
  347. l2_word argc = vm->ops[vm->iptr++];
  348. vm->sptr -= argc;
  349. l2_word *argv = vm->stack + vm->sptr;
  350. l2_word func_id = vm->stack[--vm->sptr];
  351. call_func(vm, func_id, argc, argv);
  352. }
  353. break;
  354. case L2_OP_RJMP:
  355. vm->iptr += vm->ops[vm->iptr] + 1;
  356. break;
  357. case L2_OP_STACK_FRAME_LOOKUP:
  358. {
  359. l2_word key = vm->ops[vm->iptr++];
  360. struct l2_vm_value *ns = &vm->values[vm->fstack[vm->fsptr - 1].ns];
  361. vm->stack[vm->sptr++] = l2_vm_namespace_get(vm, ns, key);
  362. }
  363. break;
  364. case L2_OP_STACK_FRAME_SET:
  365. {
  366. l2_word key = vm->ops[vm->iptr++];
  367. l2_word val = vm->stack[vm->sptr - 1];
  368. struct l2_vm_value *ns = &vm->values[vm->fstack[vm->fsptr - 1].ns];
  369. l2_vm_namespace_set(ns, key, val);
  370. }
  371. break;
  372. case L2_OP_STACK_FRAME_REPLACE:
  373. {
  374. l2_word key = vm->ops[vm->iptr++];
  375. l2_word val = vm->stack[vm->sptr - 1];
  376. struct l2_vm_value *ns = &vm->values[vm->fstack[vm->fsptr - 1].ns];
  377. l2_vm_namespace_replace(vm, ns, key, val); // TODO: error if returns -1
  378. }
  379. break;
  380. case L2_OP_RET:
  381. {
  382. l2_word retval = vm->stack[--vm->sptr];
  383. l2_word retptr = vm->fstack[vm->fsptr - 1].retptr;
  384. l2_word sptr = vm->fstack[vm->fsptr - 1].sptr;
  385. vm->fsptr -= 1;
  386. vm->sptr = sptr;
  387. vm->stack[vm->sptr++] = retval;
  388. vm->iptr = retptr;
  389. }
  390. break;
  391. case L2_OP_ALLOC_NONE:
  392. vm->stack[vm->sptr++] = 0;
  393. break;
  394. case L2_OP_ALLOC_ATOM:
  395. word = alloc_val(vm);
  396. vm->values[word].flags = L2_VAL_TYPE_ATOM;
  397. vm->values[word].atom = vm->ops[vm->iptr++];
  398. vm->stack[vm->sptr++] = word;
  399. break;
  400. case L2_OP_ALLOC_REAL:
  401. {
  402. word = alloc_val(vm);
  403. l2_word high = vm->ops[vm->iptr++];
  404. l2_word low = vm->ops[vm->iptr++];
  405. vm->values[word].flags = L2_VAL_TYPE_REAL;
  406. vm->values[word].real = u32s_to_double(high, low);
  407. vm->stack[vm->sptr++] = word;
  408. }
  409. break;
  410. case L2_OP_ALLOC_BUFFER_STATIC:
  411. {
  412. word = alloc_val(vm);
  413. l2_word length = vm->ops[vm->iptr++];
  414. l2_word offset = vm->ops[vm->iptr++];
  415. vm->values[word].flags = L2_VAL_TYPE_BUFFER;
  416. vm->values[word].buffer = malloc(sizeof(struct l2_vm_buffer) + length);
  417. vm->values[word].buffer->len = length;
  418. memcpy(
  419. (unsigned char *)vm->values[word].buffer + sizeof(struct l2_vm_buffer),
  420. vm->ops + offset, length);
  421. vm->stack[vm->sptr] = word;
  422. vm->sptr += 1;
  423. }
  424. break;
  425. case L2_OP_ALLOC_ARRAY:
  426. {
  427. l2_word count = vm->ops[vm->iptr++];
  428. l2_word arr_id = alloc_val(vm);
  429. struct l2_vm_value *arr = &vm->values[arr_id];
  430. arr->flags = L2_VAL_TYPE_ARRAY;
  431. if (count == 0) {
  432. arr->array = NULL;
  433. vm->stack[vm->sptr++] = arr_id;
  434. break;
  435. }
  436. arr->array = malloc(sizeof(struct l2_vm_array) + count * sizeof(l2_word));
  437. arr->array->len = count;
  438. arr->array->size = count;
  439. for (l2_word i = 0; i < count; ++i) {
  440. arr->array->data[count - 1 - i] = vm->stack[--vm->sptr];
  441. }
  442. vm->stack[vm->sptr++] = arr_id;
  443. }
  444. break;
  445. case L2_OP_ALLOC_NAMESPACE:
  446. word = alloc_val(vm);
  447. vm->values[word].flags = L2_VAL_TYPE_NAMESPACE;
  448. vm->values[word].extra.ns_parent = 0;
  449. vm->values[word].ns = NULL; // Will be allocated on first insert
  450. vm->stack[vm->sptr] = word;
  451. vm->sptr += 1;
  452. break;
  453. case L2_OP_ALLOC_FUNCTION:
  454. word = alloc_val(vm);
  455. vm->values[word].flags = L2_VAL_TYPE_FUNCTION;
  456. vm->values[word].func.pos = vm->ops[vm->iptr++];
  457. vm->values[word].func.ns = vm->fstack[vm->fsptr - 1].ns;
  458. vm->stack[vm->sptr] = word;
  459. vm->sptr += 1;
  460. break;
  461. case L2_OP_NAMESPACE_SET:
  462. {
  463. l2_word key = vm->ops[vm->iptr++];
  464. l2_word val = vm->stack[vm->sptr - 1];
  465. l2_word ns = vm->stack[vm->sptr - 2];
  466. l2_vm_namespace_set(&vm->values[ns], key, val);
  467. }
  468. break;
  469. case L2_OP_NAMESPACE_LOOKUP:
  470. {
  471. l2_word key = vm->ops[vm->iptr++];
  472. l2_word ns = vm->stack[--vm->sptr];
  473. vm->stack[vm->sptr++] = l2_vm_namespace_get(vm, &vm->values[ns], key);
  474. }
  475. break;
  476. case L2_OP_ARRAY_LOOKUP:
  477. {
  478. l2_word key = vm->ops[vm->iptr++];
  479. l2_word arr = vm->stack[--vm->sptr];
  480. // TODO: Error if out of bounds or incorrect type
  481. vm->stack[vm->sptr++] = vm->values[arr].array->data[key];
  482. }
  483. break;
  484. case L2_OP_ARRAY_SET:
  485. {
  486. l2_word key = vm->ops[vm->iptr++];
  487. l2_word val = vm->stack[vm->sptr - 1];
  488. l2_word arr = vm->stack[vm->sptr - 2];
  489. // TODO: Error if out of bounds or incorrect type
  490. vm->values[arr].array->data[key] = val;
  491. }
  492. break;
  493. case L2_OP_DYNAMIC_LOOKUP:
  494. {
  495. l2_word key_id = vm->stack[--vm->sptr];
  496. l2_word container_id = vm->stack[--vm->sptr];
  497. struct l2_vm_value *key = &vm->values[key_id];
  498. struct l2_vm_value *container = &vm->values[container_id];
  499. if (
  500. l2_vm_value_type(key) == L2_VAL_TYPE_REAL &&
  501. l2_vm_value_type(container) == L2_VAL_TYPE_ARRAY) {
  502. // TODO: Error if out of bounds
  503. vm->stack[vm->sptr++] = container->array->data[(size_t)key->real];
  504. } else if (
  505. l2_vm_value_type(key) == L2_VAL_TYPE_ATOM &&
  506. l2_vm_value_type(container) == L2_VAL_TYPE_NAMESPACE) {
  507. // TODO: Error if out of bounds
  508. vm->stack[vm->sptr++] = l2_vm_namespace_get(vm, container, key->atom);
  509. } else {
  510. // TODO: error
  511. }
  512. }
  513. break;
  514. case L2_OP_DYNAMIC_SET:
  515. {
  516. l2_word val = vm->stack[--vm->sptr];
  517. l2_word key_id = vm->stack[--vm->sptr];
  518. l2_word container_id = vm->stack[--vm->sptr];
  519. vm->stack[vm->sptr++] = val;
  520. struct l2_vm_value *key = &vm->values[key_id];
  521. struct l2_vm_value *container = &vm->values[container_id];
  522. if (
  523. l2_vm_value_type(key) == L2_VAL_TYPE_REAL &&
  524. l2_vm_value_type(container) == L2_VAL_TYPE_ARRAY) {
  525. // TODO: Error if out of bounds
  526. container->array->data[(size_t)key->real] = val;
  527. } else if (
  528. l2_vm_value_type(key) == L2_VAL_TYPE_ATOM &&
  529. l2_vm_value_type(container) == L2_VAL_TYPE_NAMESPACE) {
  530. // TODO: Error if out of bounds
  531. l2_vm_namespace_set(container, key->atom, val);
  532. } else {
  533. // TODO: error
  534. }
  535. }
  536. break;
  537. case L2_OP_FUNC_CALL_INFIX:
  538. {
  539. l2_word rhs = vm->stack[--vm->sptr];
  540. l2_word func_id = vm->stack[--vm->sptr];
  541. l2_word lhs = vm->stack[--vm->sptr];
  542. l2_word argv[] = {lhs, rhs};
  543. call_func(vm, func_id, 2, argv);
  544. }
  545. break;
  546. case L2_OP_HALT:
  547. vm->halted = 1;
  548. break;
  549. }
  550. if (vm->gc_scheduled) {
  551. l2_vm_gc(vm);
  552. vm->gc_scheduled = 0;
  553. }
  554. }