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

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