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

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