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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. // Define a C function variable for every builtin
  189. l2_word id;
  190. l2_word key = 1;
  191. #define Y(name, k) \
  192. if (strcmp(#k, "knone") == 0) { \
  193. id = 0; \
  194. l2_vm_namespace_set(&vm->values[builtins], key, id); \
  195. } else { \
  196. id = alloc_val(vm); \
  197. vm->values[id].flags = L2_VAL_TYPE_ATOM | L2_VAL_CONST; \
  198. vm->values[id].atom = key; \
  199. } \
  200. vm->k = id; \
  201. key += 1;
  202. #define X(name, f) \
  203. id = alloc_val(vm); \
  204. vm->values[id].flags = L2_VAL_TYPE_CFUNCTION | L2_VAL_CONST; \
  205. vm->values[id].cfunc = f; \
  206. l2_vm_namespace_set(&vm->values[builtins], key++, id);
  207. #include "builtins.x.h"
  208. #undef Y
  209. #undef X
  210. vm->gc_start = id + 1;
  211. }
  212. l2_word l2_vm_alloc(struct l2_vm *vm, enum l2_value_type typ, enum l2_value_flags flags) {
  213. l2_word id = alloc_val(vm);
  214. memset(&vm->values[id], 0, sizeof(vm->values[id]));
  215. vm->values[id].flags = typ | flags;
  216. return id;
  217. }
  218. l2_word l2_vm_error(struct l2_vm *vm, const char *fmt, ...) {
  219. l2_word id = alloc_val(vm);
  220. struct l2_vm_value *val = &vm->values[id];
  221. val->flags = L2_VAL_CONST | L2_VAL_TYPE_ERROR;
  222. char buf[256];
  223. va_list va;
  224. va_start(va, fmt);
  225. int n = vsnprintf(buf, sizeof(buf), fmt, va);
  226. if (n < 0) {
  227. const char *message = "Failed to generate error message!";
  228. val->error = malloc(strlen(message) + 1);
  229. strcpy(val->error, message);
  230. va_end(va);
  231. return id;
  232. } else if ((size_t)n + 1 < sizeof(buf)) {
  233. val->error = malloc(n + 1);
  234. strcpy(val->error, buf);
  235. va_end(va);
  236. return id;
  237. }
  238. val->error = malloc(n + 1);
  239. vsnprintf(val->error, n + 1, fmt, va);
  240. va_end(va);
  241. return id;
  242. }
  243. l2_word l2_vm_type_error(struct l2_vm *vm, struct l2_vm_value *val) {
  244. enum l2_value_type typ = l2_value_get_type(val);
  245. if (typ == L2_VAL_TYPE_ERROR) {
  246. return val - vm->values;
  247. }
  248. return l2_vm_error(vm, "Unexpected type %s", l2_value_type_name(l2_value_get_type(val)));
  249. }
  250. void l2_vm_free(struct l2_vm *vm) {
  251. // Skip ID 0, because that's always NONE
  252. for (size_t i = 1; i < vm->valuessize; ++i) {
  253. if (!l2_bitset_get(&vm->valueset, i)) {
  254. continue;
  255. }
  256. gc_free(vm, i);
  257. }
  258. free(vm->values);
  259. l2_bitset_free(&vm->valueset);
  260. }
  261. size_t l2_vm_gc(struct l2_vm *vm) {
  262. for (l2_word sptr = 0; sptr < vm->sptr; ++sptr) {
  263. gc_mark(vm, vm->stack[sptr]);
  264. }
  265. // Don't need to mark the first stack frame, since that's where all the
  266. // builtins live, and they aren't sweeped anyways
  267. for (l2_word fsptr = 1; fsptr < vm->fsptr; ++fsptr) {
  268. gc_mark(vm, vm->fstack[fsptr].ns);
  269. }
  270. return gc_sweep(vm);
  271. }
  272. void l2_vm_run(struct l2_vm *vm) {
  273. while (!vm->halted) {
  274. l2_vm_step(vm);
  275. }
  276. }
  277. // The 'call_func' function assumes that all relevant values have been popped off
  278. // the stack, so that the return value can be pushed to the top of the stack
  279. // straight away
  280. static void call_func(
  281. struct l2_vm *vm, l2_word func_id,
  282. l2_word argc, l2_word *argv) {
  283. l2_word stack_base = vm->sptr;
  284. struct l2_vm_value *func = &vm->values[func_id];
  285. enum l2_value_type typ = l2_value_get_type(func);
  286. // C functions are called differently from language functions
  287. if (typ == L2_VAL_TYPE_CFUNCTION) {
  288. // Make this a while loop, because using call_func would
  289. // make the call stack depth unbounded
  290. vm->stack[vm->sptr++] = func->cfunc(vm, argc, argv);
  291. while (1) {
  292. l2_word cont_id = vm->stack[vm->sptr - 1];
  293. struct l2_vm_value *cont = &vm->values[cont_id];
  294. if (l2_value_get_type(cont) != L2_VAL_TYPE_CONTINUATION) {
  295. break;
  296. }
  297. // If there's no callback it's easy, just call the function
  298. // it wants us to call
  299. l2_word call_id = cont->extra.cont_call;
  300. if (cont->cont == NULL) {
  301. vm->sptr -= 1;
  302. call_func(vm, call_id, 0, NULL);
  303. break;
  304. }
  305. struct l2_vm_value *call = &vm->values[call_id];
  306. if (l2_value_get_type(call) == L2_VAL_TYPE_CFUNCTION) {
  307. l2_word retval = call->cfunc(vm, 0, NULL);
  308. vm->stack[vm->sptr - 1] = cont->cont->callback(vm, retval, cont_id);
  309. } else if (l2_value_get_type(call) == L2_VAL_TYPE_FUNCTION) {
  310. // Leave the continuation on the stack,
  311. // let the L2_OP_RET code deal with it
  312. cont->flags |= L2_VAL_CONT_CALLBACK;
  313. call_func(vm, call_id, 0, NULL);
  314. break;
  315. } else {
  316. l2_word err = l2_vm_type_error(vm, call);
  317. vm->stack[vm->sptr - 1] = cont->cont->callback(vm, err, cont_id);
  318. }
  319. }
  320. return;
  321. }
  322. // Don't interpret a non-function as a function
  323. if (typ != L2_VAL_TYPE_FUNCTION) {
  324. vm->stack[vm->sptr++] = l2_vm_error(vm, "Attempt to call non-function");
  325. return;
  326. }
  327. l2_word arr_id = alloc_val(vm);
  328. struct l2_vm_value *arr = &vm->values[arr_id];
  329. arr->extra.arr_length = argc;
  330. if (argc <= 2) {
  331. arr->flags = L2_VAL_TYPE_ARRAY | L2_VAL_SBO;
  332. memcpy(arr->shortarray, argv, argc * sizeof(l2_word));
  333. } else {
  334. arr->flags = L2_VAL_TYPE_ARRAY;
  335. arr->array = malloc(
  336. sizeof(struct l2_vm_array) + sizeof(l2_word) * argc);
  337. arr->array->size = argc;
  338. memcpy(arr->array->data, argv, argc * sizeof(l2_word));
  339. }
  340. vm->stack[vm->sptr++] = arr_id;
  341. l2_word ns_id = alloc_val(vm);
  342. func = &vm->values[func_id]; // func might be stale after alloc
  343. vm->values[ns_id].extra.ns_parent = func->func.ns;
  344. vm->values[ns_id].ns = NULL;
  345. vm->values[ns_id].flags = L2_VAL_TYPE_NAMESPACE;
  346. vm->fstack[vm->fsptr].ns = ns_id;
  347. vm->fstack[vm->fsptr].retptr = vm->iptr;
  348. vm->fstack[vm->fsptr].sptr = stack_base;
  349. vm->fsptr += 1;
  350. vm->iptr = func->func.pos;
  351. }
  352. static l2_word read_u4le(struct l2_vm *vm) {
  353. unsigned char *data = &vm->ops[vm->iptr];
  354. l2_word ret =
  355. (l2_word)data[0] |
  356. (l2_word)data[1] << 8 |
  357. (l2_word)data[2] << 16 |
  358. (l2_word)data[3] << 24;
  359. vm->iptr += 4;
  360. return ret;
  361. }
  362. static l2_word read_u1le(struct l2_vm *vm) {
  363. return vm->ops[vm->iptr++];
  364. }
  365. static double read_d8le(struct l2_vm *vm) {
  366. unsigned char *data = &vm->ops[vm->iptr];
  367. uint64_t integer = 0 |
  368. (uint64_t)data[0] |
  369. (uint64_t)data[1] << 8 |
  370. (uint64_t)data[2] << 16 |
  371. (uint64_t)data[3] << 24 |
  372. (uint64_t)data[4] << 32 |
  373. (uint64_t)data[5] << 40 |
  374. (uint64_t)data[6] << 48 |
  375. (uint64_t)data[7] << 56;
  376. double num;
  377. memcpy(&num, &integer, 8);
  378. vm->iptr += 8;
  379. return num;
  380. }
  381. void l2_vm_step(struct l2_vm *vm) {
  382. enum l2_opcode opcode = (enum l2_opcode)vm->ops[vm->iptr++];
  383. l2_word word;
  384. switch (opcode) {
  385. case L2_OP_NOP:
  386. break;
  387. case L2_OP_DISCARD:
  388. vm->sptr -= 1;
  389. if (l2_value_get_type(&vm->values[vm->stack[vm->sptr]]) == L2_VAL_TYPE_ERROR) {
  390. l2_io_printf(vm->std_error, "Error: %s\n", vm->values[vm->stack[vm->sptr]].error);
  391. vm->halted = 1;
  392. }
  393. break;
  394. case L2_OP_SWAP_DISCARD:
  395. vm->stack[vm->sptr - 2] = vm->stack[vm->sptr - 1];
  396. vm->sptr -= 1;
  397. if (l2_value_get_type(&vm->values[vm->stack[vm->sptr]]) == L2_VAL_TYPE_ERROR) {
  398. l2_io_printf(vm->std_error, "Error: %s\n", vm->values[vm->stack[vm->sptr]].error);
  399. vm->halted = 1;
  400. }
  401. break;
  402. case L2_OP_DUP:
  403. vm->stack[vm->sptr] = vm->ops[vm->sptr - 1];
  404. vm->sptr += 1;
  405. break;
  406. case L2_OP_ADD:
  407. vm->stack[vm->sptr - 2] += vm->stack[vm->sptr - 1];
  408. vm->sptr -= 1;
  409. break;
  410. #define X(read) \
  411. l2_word argc = read(vm); \
  412. vm->sptr -= argc; \
  413. l2_word *argv = vm->stack + vm->sptr; \
  414. l2_word func_id = vm->stack[--vm->sptr]; \
  415. call_func(vm, func_id, argc, argv)
  416. case L2_OP_FUNC_CALL_U4: { X(read_u4le); } break;
  417. case L2_OP_FUNC_CALL_U1: { X(read_u1le); } break;
  418. #undef X
  419. #define X(read) word = read(vm); vm->iptr += word;
  420. case L2_OP_RJMP_U4: { X(read_u4le); } break;
  421. case L2_OP_RJMP_U1: { X(read_u1le); } break;
  422. #undef X
  423. #define X(read) \
  424. l2_word key = read(vm); \
  425. struct l2_vm_value *ns = &vm->values[vm->fstack[vm->fsptr - 1].ns]; \
  426. vm->stack[vm->sptr++] = l2_vm_namespace_get(vm, ns, key);
  427. case L2_OP_STACK_FRAME_LOOKUP_U4: { X(read_u4le); } break;
  428. case L2_OP_STACK_FRAME_LOOKUP_U1: { X(read_u1le); } break;
  429. #undef X
  430. #define X(read) \
  431. l2_word key = read(vm); \
  432. l2_word val = vm->stack[vm->sptr - 1]; \
  433. struct l2_vm_value *ns = &vm->values[vm->fstack[vm->fsptr - 1].ns]; \
  434. l2_vm_namespace_set(ns, key, val);
  435. case L2_OP_STACK_FRAME_SET_U4: { X(read_u4le); } break;
  436. case L2_OP_STACK_FRAME_SET_U1: { X(read_u1le); } break;
  437. #undef X
  438. #define X(read) \
  439. l2_word key = read(vm); \
  440. l2_word val = vm->stack[vm->sptr - 1]; \
  441. struct l2_vm_value *ns = &vm->values[vm->fstack[vm->fsptr - 1].ns]; \
  442. if (l2_vm_namespace_replace(vm, ns, key, val) < 0) { \
  443. vm->stack[vm->sptr - 1] = l2_vm_error(vm, "Variable not found"); \
  444. }
  445. case L2_OP_STACK_FRAME_REPLACE_U4: { X(read_u4le); } break;
  446. case L2_OP_STACK_FRAME_REPLACE_U1: { X(read_u1le); } break;
  447. #undef X
  448. case L2_OP_RET:
  449. {
  450. l2_word retval = vm->stack[--vm->sptr];
  451. l2_word retptr = vm->fstack[vm->fsptr - 1].retptr;
  452. l2_word sptr = vm->fstack[vm->fsptr - 1].sptr;
  453. vm->fsptr -= 1;
  454. vm->sptr = sptr;
  455. vm->iptr = retptr;
  456. l2_word cont_id;
  457. struct l2_vm_value *cont = NULL;
  458. if (vm->sptr > 0) {
  459. cont_id = vm->stack[vm->sptr - 1];
  460. cont = &vm->values[cont_id];
  461. }
  462. int iscont =
  463. cont != NULL && l2_value_get_type(cont) == L2_VAL_TYPE_CONTINUATION;
  464. int nocallback =
  465. !iscont || (cont->flags & L2_VAL_CONT_CALLBACK && cont->cont == NULL);
  466. if (nocallback) {
  467. if (iscont) {
  468. vm->stack[vm->sptr - 1] = retval;
  469. } else {
  470. vm->stack[vm->sptr++] = retval;
  471. }
  472. break;
  473. }
  474. if (cont->flags & L2_VAL_CONT_CALLBACK) {
  475. retval = cont->cont->callback(vm, retval, cont_id);
  476. cont_id = retval;
  477. cont = &vm->values[cont_id];
  478. if (l2_value_get_type(cont) != L2_VAL_TYPE_CONTINUATION) {
  479. vm->stack[vm->sptr - 1] = retval;
  480. break;
  481. }
  482. }
  483. cont->flags |= L2_VAL_CONT_CALLBACK;
  484. call_func(vm, cont->extra.cont_call, 0, NULL);
  485. }
  486. break;
  487. case L2_OP_ALLOC_NONE:
  488. vm->stack[vm->sptr++] = 0;
  489. break;
  490. #define X(read) \
  491. word = alloc_val(vm); \
  492. vm->values[word].flags = L2_VAL_TYPE_ATOM; \
  493. vm->values[word].atom = read(vm); \
  494. vm->stack[vm->sptr++] = word;
  495. case L2_OP_ALLOC_ATOM_U4: { X(read_u4le); } break;
  496. case L2_OP_ALLOC_ATOM_U1: { X(read_u1le); } break;
  497. #undef X
  498. case L2_OP_ALLOC_REAL_D8:
  499. {
  500. word = alloc_val(vm);
  501. vm->values[word].flags = L2_VAL_TYPE_REAL;
  502. vm->values[word].real = read_d8le(vm);
  503. vm->stack[vm->sptr++] = word;
  504. }
  505. break;
  506. #define X(read) \
  507. word = alloc_val(vm); \
  508. l2_word length = read(vm); \
  509. l2_word offset = read(vm); \
  510. vm->values[word].flags = L2_VAL_TYPE_BUFFER; \
  511. vm->values[word].buffer = length > 0 ? malloc(length) : NULL; \
  512. vm->values[word].extra.buf_length = length; \
  513. memcpy(vm->values[word].buffer, vm->ops + offset, length); \
  514. vm->stack[vm->sptr] = word; \
  515. vm->sptr += 1;
  516. case L2_OP_ALLOC_BUFFER_STATIC_U4: { X(read_u4le); } break;
  517. case L2_OP_ALLOC_BUFFER_STATIC_U1: { X(read_u1le); } break;
  518. #undef X
  519. #define X(read) \
  520. l2_word count = read(vm); \
  521. l2_word arr_id = alloc_val(vm); \
  522. struct l2_vm_value *arr = &vm->values[arr_id]; \
  523. arr->extra.arr_length = count; \
  524. l2_word *data; \
  525. if (count <= 2) { \
  526. arr->flags = L2_VAL_TYPE_ARRAY | L2_VAL_SBO; \
  527. data = arr->shortarray; \
  528. } else { \
  529. arr->flags = L2_VAL_TYPE_ARRAY; \
  530. arr->array = malloc(sizeof(struct l2_vm_array) + count * sizeof(l2_word)); \
  531. arr->array->size = count; \
  532. data = arr->array->data; \
  533. } \
  534. for (l2_word i = 0; i < count; ++i) { \
  535. data[count - 1 - i] = vm->stack[--vm->sptr]; \
  536. } \
  537. vm->stack[vm->sptr++] = arr_id;
  538. case L2_OP_ALLOC_ARRAY_U4: { X(read_u4le); } break;
  539. case L2_OP_ALLOC_ARRAY_U1: { X(read_u1le); } break;
  540. #undef X
  541. case L2_OP_ALLOC_NAMESPACE:
  542. word = alloc_val(vm);
  543. vm->values[word].flags = L2_VAL_TYPE_NAMESPACE;
  544. vm->values[word].extra.ns_parent = 0;
  545. vm->values[word].ns = NULL; // Will be allocated on first insert
  546. vm->stack[vm->sptr] = word;
  547. vm->sptr += 1;
  548. break;
  549. #define X(read) \
  550. word = alloc_val(vm); \
  551. vm->values[word].flags = L2_VAL_TYPE_FUNCTION; \
  552. vm->values[word].func.pos = read(vm); \
  553. vm->values[word].func.ns = vm->fstack[vm->fsptr - 1].ns; \
  554. vm->stack[vm->sptr] = word; \
  555. vm->sptr += 1;
  556. case L2_OP_ALLOC_FUNCTION_U4: { X(read_u4le); } break;
  557. case L2_OP_ALLOC_FUNCTION_U1: { X(read_u1le); } break;
  558. #undef X
  559. #define X(read) \
  560. l2_word key = read(vm); \
  561. l2_word val = vm->stack[vm->sptr - 1]; \
  562. l2_word ns = vm->stack[vm->sptr - 2]; \
  563. l2_vm_namespace_set(&vm->values[ns], key, val);
  564. case L2_OP_NAMESPACE_SET_U4: { X(read_u4le); } break;
  565. case L2_OP_NAMESPACE_SET_U1: { X(read_u1le); } break;
  566. #undef X
  567. #define X(read) \
  568. l2_word key = read(vm); \
  569. l2_word ns = vm->stack[--vm->sptr]; \
  570. vm->stack[vm->sptr++] = l2_vm_namespace_get(vm, &vm->values[ns], key);
  571. case L2_OP_NAMESPACE_LOOKUP_U4: { X(read_u4le); } break;
  572. case L2_OP_NAMESPACE_LOOKUP_U1: { X(read_u1le); } break;
  573. #undef X
  574. #define X(read) \
  575. l2_word key = read(vm); \
  576. l2_word arr_id = vm->stack[--vm->sptr]; \
  577. struct l2_vm_value *arr = &vm->values[arr_id]; \
  578. if (l2_value_get_type(arr) != L2_VAL_TYPE_ARRAY) { \
  579. vm->stack[vm->sptr++] = l2_vm_type_error(vm, arr); \
  580. } else { \
  581. vm->stack[vm->sptr++] = l2_value_arr_get(vm, arr, key); \
  582. }
  583. case L2_OP_ARRAY_LOOKUP_U4: { X(read_u4le); } break;
  584. case L2_OP_ARRAY_LOOKUP_U1: { X(read_u1le); } break;
  585. #undef X
  586. #define X(read) \
  587. l2_word key = read(vm); \
  588. l2_word val = vm->stack[vm->sptr - 1]; \
  589. l2_word arr_id = vm->stack[vm->sptr - 2]; \
  590. struct l2_vm_value *arr = &vm->values[arr_id]; \
  591. if (l2_value_get_type(arr) != L2_VAL_TYPE_ARRAY) { \
  592. vm->stack[vm->sptr - 1] = l2_vm_type_error(vm, arr); \
  593. } else { \
  594. vm->stack[vm->sptr - 1] = l2_value_arr_set(vm, arr, key, val); \
  595. }
  596. case L2_OP_ARRAY_SET_U4: { X(read_u4le); } break;
  597. case L2_OP_ARRAY_SET_U1: { X(read_u1le); } break;
  598. case L2_OP_DYNAMIC_LOOKUP:
  599. {
  600. l2_word key_id = vm->stack[--vm->sptr];
  601. l2_word container_id = vm->stack[--vm->sptr];
  602. struct l2_vm_value *key = &vm->values[key_id];
  603. struct l2_vm_value *container = &vm->values[container_id];
  604. if (l2_value_get_type(container) == L2_VAL_TYPE_ARRAY) {
  605. if (l2_value_get_type(key) != L2_VAL_TYPE_REAL) {
  606. vm->stack[vm->sptr++] = l2_vm_type_error(vm, key);
  607. } else if (key->real >= container->extra.arr_length) {
  608. vm->stack[vm->sptr++] = l2_vm_error(vm, "Index out of range");
  609. } else {
  610. vm->stack[vm->sptr++] = container->array->data[(l2_word)key->real];
  611. }
  612. } else if (l2_value_get_type(container) == L2_VAL_TYPE_NAMESPACE) {
  613. if (l2_value_get_type(key) != L2_VAL_TYPE_ATOM) {
  614. vm->stack[vm->sptr++] = l2_vm_type_error(vm, key);
  615. } else {
  616. vm->stack[vm->sptr++] = l2_vm_namespace_get(vm, container, key->atom);
  617. }
  618. } else {
  619. vm->stack[vm->sptr++] = l2_vm_type_error(vm, container);
  620. }
  621. }
  622. break;
  623. case L2_OP_DYNAMIC_SET:
  624. {
  625. l2_word val = vm->stack[--vm->sptr];
  626. l2_word key_id = vm->stack[--vm->sptr];
  627. l2_word container_id = vm->stack[--vm->sptr];
  628. vm->stack[vm->sptr++] = val;
  629. struct l2_vm_value *key = &vm->values[key_id];
  630. struct l2_vm_value *container = &vm->values[container_id];
  631. if (l2_value_get_type(container) == L2_VAL_TYPE_ARRAY) {
  632. if (l2_value_get_type(key) != L2_VAL_TYPE_REAL) {
  633. vm->stack[vm->sptr - 1] = l2_vm_type_error(vm, key);
  634. } else if (key->real >= container->extra.arr_length) {
  635. vm->stack[vm->sptr - 1] = l2_vm_error(vm, "Index out of range");
  636. } else {
  637. container->array->data[(size_t)key->real] = val;
  638. }
  639. } else if (l2_value_get_type(container) == L2_VAL_TYPE_NAMESPACE) {
  640. if (l2_value_get_type(key) != L2_VAL_TYPE_ATOM) {
  641. vm->stack[vm->sptr - 1] = l2_vm_type_error(vm, key);
  642. } else {
  643. l2_vm_namespace_set(container, key->atom, val);
  644. }
  645. } else {
  646. vm->stack[vm->sptr - 1] = l2_vm_type_error(vm, container);
  647. }
  648. }
  649. break;
  650. case L2_OP_FUNC_CALL_INFIX:
  651. {
  652. l2_word rhs = vm->stack[--vm->sptr];
  653. l2_word func_id = vm->stack[--vm->sptr];
  654. l2_word lhs = vm->stack[--vm->sptr];
  655. l2_word argv[] = {lhs, rhs};
  656. call_func(vm, func_id, 2, argv);
  657. }
  658. break;
  659. case L2_OP_HALT:
  660. vm->halted = 1;
  661. break;
  662. }
  663. if (vm->gc_scheduled) {
  664. l2_vm_gc(vm);
  665. vm->gc_scheduled = 0;
  666. }
  667. }
  668. int l2_vm_val_is_true(struct l2_vm *vm, l2_word id) {
  669. struct l2_vm_value *val = &vm->values[id];
  670. l2_word true_atom = vm->values[vm->ktrue].atom;
  671. return l2_value_get_type(val) == L2_VAL_TYPE_ATOM && val->atom == true_atom;
  672. }