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

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