2017-04-18 02:40:39 +00:00
|
|
|
/*
|
2022-03-21 22:47:37 +00:00
|
|
|
* Copyright (c) 2022 Calvin Rose
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2019-12-31 00:06:15 +00:00
|
|
|
#include "features.h"
|
2019-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2018-03-31 20:40:36 +00:00
|
|
|
#include "state.h"
|
2018-03-11 19:35:23 +00:00
|
|
|
#include "fiber.h"
|
2017-12-21 04:03:34 +00:00
|
|
|
#include "gc.h"
|
2018-03-11 19:35:23 +00:00
|
|
|
#include "symcache.h"
|
2018-07-04 03:07:35 +00:00
|
|
|
#include "util.h"
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2020-03-07 20:05:28 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* Virtual registers
|
|
|
|
*
|
2019-01-05 01:08:43 +00:00
|
|
|
* One instruction word
|
|
|
|
* CC | BB | AA | OP
|
|
|
|
* DD | DD | DD | OP
|
|
|
|
* EE | EE | AA | OP
|
|
|
|
*/
|
|
|
|
#define A ((*pc >> 8) & 0xFF)
|
|
|
|
#define B ((*pc >> 16) & 0xFF)
|
|
|
|
#define C (*pc >> 24)
|
|
|
|
#define D (*pc >> 8)
|
|
|
|
#define E (*pc >> 16)
|
|
|
|
|
|
|
|
/* Signed interpretations of registers */
|
|
|
|
#define CS (*((int32_t *)pc) >> 24)
|
|
|
|
#define DS (*((int32_t *)pc) >> 8)
|
|
|
|
#define ES (*((int32_t *)pc) >> 16)
|
|
|
|
|
|
|
|
/* How we dispatch instructions. By default, we use
|
|
|
|
* a switch inside an infinite loop. For GCC/clang, we use
|
|
|
|
* computed gotos. */
|
2019-07-14 21:11:00 +00:00
|
|
|
#if defined(__GNUC__) && !defined(__EMSCRIPTEN__)
|
2019-09-22 22:50:31 +00:00
|
|
|
#define JANET_USE_COMPUTED_GOTOS
|
2019-07-14 14:58:11 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef JANET_USE_COMPUTED_GOTOS
|
2018-12-13 23:46:53 +00:00
|
|
|
#define VM_START() { goto *op_lookup[first_opcode];
|
2018-02-01 02:40:19 +00:00
|
|
|
#define VM_END() }
|
|
|
|
#define VM_OP(op) label_##op :
|
|
|
|
#define VM_DEFAULT() label_unknown_op:
|
2019-01-08 18:42:29 +00:00
|
|
|
#define vm_next() goto *op_lookup[*pc & 0xFF]
|
2019-05-24 17:54:23 +00:00
|
|
|
#define opcode (*pc & 0xFF)
|
2018-02-01 02:40:19 +00:00
|
|
|
#else
|
2018-12-13 23:46:53 +00:00
|
|
|
#define VM_START() uint8_t opcode = first_opcode; for (;;) {switch(opcode) {
|
2018-02-01 02:40:19 +00:00
|
|
|
#define VM_END() }}
|
|
|
|
#define VM_OP(op) case op :
|
|
|
|
#define VM_DEFAULT() default:
|
2018-12-13 23:46:53 +00:00
|
|
|
#define vm_next() opcode = *pc & 0xFF; continue
|
2018-02-01 02:40:19 +00:00
|
|
|
#endif
|
|
|
|
|
2019-01-05 02:15:37 +00:00
|
|
|
/* Commit and restore VM state before possible longjmp */
|
|
|
|
#define vm_commit() do { janet_stack_frame(stack)->pc = pc; } while (0)
|
|
|
|
#define vm_restore() do { \
|
|
|
|
stack = fiber->data + fiber->frame; \
|
|
|
|
pc = janet_stack_frame(stack)->pc; \
|
|
|
|
func = janet_stack_frame(stack)->func; \
|
|
|
|
} while (0)
|
2019-01-05 04:20:34 +00:00
|
|
|
#define vm_return(sig, val) do { \
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.return_reg[0] = (val); \
|
2020-04-10 23:29:10 +00:00
|
|
|
vm_commit(); \
|
2019-01-05 04:20:34 +00:00
|
|
|
return (sig); \
|
|
|
|
} while (0)
|
2020-08-17 12:01:58 +00:00
|
|
|
#define vm_return_no_restore(sig, val) do { \
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.return_reg[0] = (val); \
|
2020-08-17 12:01:58 +00:00
|
|
|
return (sig); \
|
|
|
|
} while (0)
|
2019-01-05 02:15:37 +00:00
|
|
|
|
2019-01-05 01:08:43 +00:00
|
|
|
/* Next instruction variations */
|
2019-01-05 02:15:37 +00:00
|
|
|
#define maybe_collect() do {\
|
2021-07-17 01:59:03 +00:00
|
|
|
if (janet_vm.next_collection >= janet_vm.gc_interval) janet_collect(); } while (0)
|
2019-01-05 02:15:37 +00:00
|
|
|
#define vm_checkgc_next() maybe_collect(); vm_next()
|
2019-01-05 01:08:43 +00:00
|
|
|
#define vm_pcnext() pc++; vm_next()
|
2019-01-05 02:15:37 +00:00
|
|
|
#define vm_checkgc_pcnext() maybe_collect(); vm_pcnext()
|
2017-12-08 20:57:02 +00:00
|
|
|
|
2019-01-05 01:08:43 +00:00
|
|
|
/* Handle certain errors in main vm loop */
|
2019-01-05 04:20:34 +00:00
|
|
|
#define vm_throw(e) do { vm_commit(); janet_panic(e); } while (0)
|
2017-11-06 03:05:47 +00:00
|
|
|
#define vm_assert(cond, e) do {if (!(cond)) vm_throw((e)); } while (0)
|
2018-07-04 03:07:35 +00:00
|
|
|
#define vm_assert_type(X, T) do { \
|
2018-09-06 02:18:42 +00:00
|
|
|
if (!(janet_checktype((X), (T)))) { \
|
2019-01-05 05:33:20 +00:00
|
|
|
vm_commit(); \
|
2020-04-14 01:24:11 +00:00
|
|
|
janet_panicf("expected %T, got %v", (1 << (T)), (X)); \
|
2018-07-04 03:07:35 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#define vm_assert_types(X, TS) do { \
|
2019-01-05 01:08:43 +00:00
|
|
|
if (!(janet_checktypes((X), (TS)))) { \
|
2019-01-05 05:33:20 +00:00
|
|
|
vm_commit(); \
|
2020-04-14 01:24:11 +00:00
|
|
|
janet_panicf("expected %T, got %v", (TS), (X)); \
|
2018-07-04 03:07:35 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
2021-07-24 20:14:37 +00:00
|
|
|
#ifdef JANET_NO_INTERPRETER_INTERRUPT
|
|
|
|
#define vm_maybe_auto_suspend(COND)
|
|
|
|
#else
|
|
|
|
#define vm_maybe_auto_suspend(COND) do { \
|
|
|
|
if ((COND) && janet_vm.auto_suspend) { \
|
|
|
|
janet_vm.auto_suspend = 0; \
|
|
|
|
fiber->flags |= (JANET_FIBER_RESUME_NO_USEVAL | JANET_FIBER_RESUME_NO_SKIP); \
|
2021-07-25 01:30:36 +00:00
|
|
|
vm_return(JANET_SIGNAL_INTERRUPT, janet_wrap_nil()); \
|
2021-07-24 20:14:37 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2019-01-05 01:08:43 +00:00
|
|
|
/* Templates for certain patterns in opcodes */
|
2018-12-27 18:05:29 +00:00
|
|
|
#define vm_binop_immediate(op)\
|
|
|
|
{\
|
2019-01-05 01:08:43 +00:00
|
|
|
Janet op1 = stack[B];\
|
2019-09-08 20:36:21 +00:00
|
|
|
if (!janet_checktype(op1, JANET_NUMBER)) {\
|
|
|
|
vm_commit();\
|
|
|
|
Janet _argv[2] = { op1, janet_wrap_number(CS) };\
|
|
|
|
stack[A] = janet_mcall(#op, 2, _argv);\
|
2020-01-23 00:59:41 +00:00
|
|
|
vm_checkgc_pcnext();\
|
2019-09-08 20:36:21 +00:00
|
|
|
} else {\
|
|
|
|
double x1 = janet_unwrap_number(op1);\
|
|
|
|
stack[A] = janet_wrap_number(x1 op CS);\
|
|
|
|
vm_pcnext();\
|
|
|
|
}\
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
|
|
|
#define _vm_bitop_immediate(op, type1)\
|
|
|
|
{\
|
2019-01-05 01:08:43 +00:00
|
|
|
Janet op1 = stack[B];\
|
2020-01-23 00:59:41 +00:00
|
|
|
if (!janet_checktype(op1, JANET_NUMBER)) {\
|
|
|
|
vm_commit();\
|
|
|
|
Janet _argv[2] = { op1, janet_wrap_number(CS) };\
|
|
|
|
stack[A] = janet_mcall(#op, 2, _argv);\
|
|
|
|
vm_checkgc_pcnext();\
|
|
|
|
} else {\
|
|
|
|
type1 x1 = (type1) janet_unwrap_integer(op1);\
|
|
|
|
stack[A] = janet_wrap_integer(x1 op CS);\
|
|
|
|
vm_pcnext();\
|
|
|
|
}\
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
|
|
|
#define vm_bitop_immediate(op) _vm_bitop_immediate(op, int32_t);
|
|
|
|
#define vm_bitopu_immediate(op) _vm_bitop_immediate(op, uint32_t);
|
|
|
|
#define _vm_binop(op, wrap)\
|
2017-11-06 03:05:47 +00:00
|
|
|
{\
|
2019-01-05 01:08:43 +00:00
|
|
|
Janet op1 = stack[B];\
|
|
|
|
Janet op2 = stack[C];\
|
2020-01-23 00:59:41 +00:00
|
|
|
if (janet_checktype(op1, JANET_NUMBER) && janet_checktype(op2, JANET_NUMBER)) {\
|
2019-09-08 20:36:21 +00:00
|
|
|
double x1 = janet_unwrap_number(op1);\
|
|
|
|
double x2 = janet_unwrap_number(op2);\
|
|
|
|
stack[A] = wrap(x1 op x2);\
|
|
|
|
vm_pcnext();\
|
2020-01-23 00:59:41 +00:00
|
|
|
} else {\
|
|
|
|
vm_commit();\
|
|
|
|
stack[A] = janet_binop_call(#op, "r" #op, op1, op2);\
|
|
|
|
vm_checkgc_pcnext();\
|
2019-09-08 20:36:21 +00:00
|
|
|
}\
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
#define vm_binop(op) _vm_binop(op, janet_wrap_number)
|
|
|
|
#define _vm_bitop(op, type1)\
|
2018-07-10 01:24:22 +00:00
|
|
|
{\
|
2019-01-05 01:08:43 +00:00
|
|
|
Janet op1 = stack[B];\
|
|
|
|
Janet op2 = stack[C];\
|
2020-01-23 00:59:41 +00:00
|
|
|
if (janet_checktype(op1, JANET_NUMBER) && janet_checktype(op2, JANET_NUMBER)) {\
|
|
|
|
type1 x1 = (type1) janet_unwrap_integer(op1);\
|
|
|
|
int32_t x2 = janet_unwrap_integer(op2);\
|
|
|
|
stack[A] = janet_wrap_integer(x1 op x2);\
|
|
|
|
vm_pcnext();\
|
|
|
|
} else {\
|
|
|
|
vm_commit();\
|
|
|
|
stack[A] = janet_binop_call(#op, "r" #op, op1, op2);\
|
|
|
|
vm_checkgc_pcnext();\
|
|
|
|
}\
|
2018-07-10 01:24:22 +00:00
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
#define vm_bitop(op) _vm_bitop(op, int32_t)
|
|
|
|
#define vm_bitopu(op) _vm_bitop(op, uint32_t)
|
2019-12-28 20:57:36 +00:00
|
|
|
#define vm_compop(op) \
|
|
|
|
{\
|
|
|
|
Janet op1 = stack[B];\
|
|
|
|
Janet op2 = stack[C];\
|
2020-01-23 00:59:41 +00:00
|
|
|
if (janet_checktype(op1, JANET_NUMBER) && janet_checktype(op2, JANET_NUMBER)) {\
|
2019-12-28 20:57:36 +00:00
|
|
|
double x1 = janet_unwrap_number(op1);\
|
|
|
|
double x2 = janet_unwrap_number(op2);\
|
|
|
|
stack[A] = janet_wrap_boolean(x1 op x2);\
|
|
|
|
vm_pcnext();\
|
2020-01-23 00:59:41 +00:00
|
|
|
} else {\
|
|
|
|
vm_commit();\
|
|
|
|
stack[A] = janet_wrap_boolean(janet_compare(op1, op2) op 0);\
|
|
|
|
vm_checkgc_pcnext();\
|
2019-12-28 20:57:36 +00:00
|
|
|
}\
|
|
|
|
}
|
2021-02-08 17:53:25 +00:00
|
|
|
#define vm_compop_imm(op) \
|
|
|
|
{\
|
|
|
|
Janet op1 = stack[B];\
|
|
|
|
if (janet_checktype(op1, JANET_NUMBER)) {\
|
|
|
|
double x1 = janet_unwrap_number(op1);\
|
|
|
|
double x2 = (double) CS; \
|
|
|
|
stack[A] = janet_wrap_boolean(x1 op x2);\
|
|
|
|
vm_pcnext();\
|
|
|
|
} else {\
|
|
|
|
vm_commit();\
|
|
|
|
stack[A] = janet_wrap_boolean(janet_compare(op1, janet_wrap_integer(CS)) op 0);\
|
|
|
|
vm_checkgc_pcnext();\
|
|
|
|
}\
|
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
|
2019-05-04 19:03:41 +00:00
|
|
|
/* Trace a function call */
|
2020-01-23 15:01:33 +00:00
|
|
|
static void vm_do_trace(JanetFunction *func, int32_t argc, const Janet *argv) {
|
2020-01-24 05:39:49 +00:00
|
|
|
if (func->def->name) {
|
2019-05-04 19:03:41 +00:00
|
|
|
janet_printf("trace (%S", func->def->name);
|
|
|
|
} else {
|
|
|
|
janet_printf("trace (%p", janet_wrap_function(func));
|
|
|
|
}
|
|
|
|
for (int32_t i = 0; i < argc; i++) {
|
2020-01-23 15:01:33 +00:00
|
|
|
janet_printf(" %p", argv[i]);
|
2019-05-04 19:03:41 +00:00
|
|
|
}
|
2020-01-23 15:01:33 +00:00
|
|
|
janet_printf(")\n");
|
2019-05-04 19:03:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 15:01:33 +00:00
|
|
|
/* Invoke a method once we have looked it up */
|
|
|
|
static Janet janet_method_invoke(Janet method, int32_t argc, Janet *argv) {
|
|
|
|
switch (janet_type(method)) {
|
|
|
|
case JANET_CFUNCTION:
|
|
|
|
return (janet_unwrap_cfunction(method))(argc, argv);
|
|
|
|
case JANET_FUNCTION: {
|
|
|
|
JanetFunction *fun = janet_unwrap_function(method);
|
|
|
|
return janet_call(fun, argc, argv);
|
|
|
|
}
|
|
|
|
case JANET_ABSTRACT: {
|
|
|
|
JanetAbstract abst = janet_unwrap_abstract(method);
|
|
|
|
const JanetAbstractType *at = janet_abstract_type(abst);
|
|
|
|
if (NULL != at->call) {
|
|
|
|
return at->call(abst, argc, argv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* fallthrough */
|
|
|
|
case JANET_STRING:
|
|
|
|
case JANET_BUFFER:
|
|
|
|
case JANET_TABLE:
|
|
|
|
case JANET_STRUCT:
|
|
|
|
case JANET_ARRAY:
|
|
|
|
case JANET_TUPLE: {
|
|
|
|
if (argc != 1) {
|
|
|
|
janet_panicf("%v called with %d arguments, possibly expected 1", method, argc);
|
|
|
|
}
|
|
|
|
return janet_in(method, argv[0]);
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
if (argc != 1) {
|
|
|
|
janet_panicf("%v called with %d arguments, possibly expected 1", method, argc);
|
|
|
|
}
|
|
|
|
return janet_in(argv[0], method);
|
2020-01-22 00:22:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-23 15:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Call a non function type from a JOP_CALL or JOP_TAILCALL instruction.
|
|
|
|
* Assumes that the arguments are on the fiber stack. */
|
|
|
|
static Janet call_nonfn(JanetFiber *fiber, Janet callee) {
|
|
|
|
int32_t argc = fiber->stacktop - fiber->stackstart;
|
2019-01-05 04:20:34 +00:00
|
|
|
fiber->stacktop = fiber->stackstart;
|
2020-01-23 15:01:33 +00:00
|
|
|
return janet_method_invoke(callee, argc, fiber->data + fiber->stacktop);
|
2019-01-05 04:20:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 20:38:46 +00:00
|
|
|
/* Method lookup could potentially handle tables specially... */
|
2021-03-19 20:18:19 +00:00
|
|
|
static Janet method_to_fun(Janet method, Janet obj) {
|
2021-03-19 20:38:46 +00:00
|
|
|
return janet_get(obj, method);
|
2021-03-19 20:18:19 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 15:01:33 +00:00
|
|
|
/* Get a callable from a keyword method name and ensure that it is valid. */
|
2019-08-06 00:06:58 +00:00
|
|
|
static Janet resolve_method(Janet name, JanetFiber *fiber) {
|
|
|
|
int32_t argc = fiber->stacktop - fiber->stackstart;
|
2019-08-13 00:20:01 +00:00
|
|
|
if (argc < 1) janet_panicf("method call (%v) takes at least 1 argument, got 0", name);
|
2021-03-19 20:18:19 +00:00
|
|
|
Janet callee = method_to_fun(name, fiber->data[fiber->stackstart]);
|
2019-08-06 00:06:58 +00:00
|
|
|
if (janet_checktype(callee, JANET_NIL))
|
|
|
|
janet_panicf("unknown method %v invoked on %v", name, fiber->data[fiber->stackstart]);
|
|
|
|
return callee;
|
|
|
|
}
|
|
|
|
|
2020-01-23 00:59:41 +00:00
|
|
|
/* Lookup method on value x */
|
|
|
|
static Janet janet_method_lookup(Janet x, const char *name) {
|
2021-03-19 20:18:19 +00:00
|
|
|
return method_to_fun(janet_ckeywordv(name), x);
|
2020-01-23 00:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Call a method first on the righthand side, and then on the left hand side with a prefix */
|
|
|
|
static Janet janet_binop_call(const char *lmethod, const char *rmethod, Janet lhs, Janet rhs) {
|
|
|
|
Janet lm = janet_method_lookup(lhs, lmethod);
|
|
|
|
if (janet_checktype(lm, JANET_NIL)) {
|
|
|
|
/* Invert order for rmethod */
|
|
|
|
Janet lr = janet_method_lookup(rhs, rmethod);
|
|
|
|
Janet argv[2] = { rhs, lhs };
|
|
|
|
if (janet_checktype(lr, JANET_NIL)) {
|
|
|
|
janet_panicf("could not find method :%s for %v, or :%s for %v",
|
|
|
|
lmethod, lhs,
|
|
|
|
rmethod, rhs);
|
|
|
|
}
|
|
|
|
return janet_method_invoke(lr, 2, argv);
|
|
|
|
} else {
|
|
|
|
Janet argv[2] = { lhs, rhs };
|
|
|
|
return janet_method_invoke(lm, 2, argv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 23:29:10 +00:00
|
|
|
/* Forward declaration */
|
2022-02-10 03:48:49 +00:00
|
|
|
static JanetSignal janet_check_can_resume(JanetFiber *fiber, Janet *out, int is_cancel);
|
2020-04-10 23:29:10 +00:00
|
|
|
static JanetSignal janet_continue_no_check(JanetFiber *fiber, Janet in, Janet *out);
|
|
|
|
|
2019-01-05 01:08:43 +00:00
|
|
|
/* Interpreter main loop */
|
2020-01-10 23:25:10 +00:00
|
|
|
static JanetSignal run_vm(JanetFiber *fiber, Janet in) {
|
2019-01-05 01:08:43 +00:00
|
|
|
|
2019-05-24 17:54:23 +00:00
|
|
|
/* opcode -> label lookup if using clang/GCC */
|
2019-07-14 14:58:11 +00:00
|
|
|
#ifdef JANET_USE_COMPUTED_GOTOS
|
2019-05-24 17:54:23 +00:00
|
|
|
static void *op_lookup[255] = {
|
|
|
|
&&label_JOP_NOOP,
|
|
|
|
&&label_JOP_ERROR,
|
|
|
|
&&label_JOP_TYPECHECK,
|
|
|
|
&&label_JOP_RETURN,
|
|
|
|
&&label_JOP_RETURN_NIL,
|
|
|
|
&&label_JOP_ADD_IMMEDIATE,
|
|
|
|
&&label_JOP_ADD,
|
|
|
|
&&label_JOP_SUBTRACT,
|
|
|
|
&&label_JOP_MULTIPLY_IMMEDIATE,
|
|
|
|
&&label_JOP_MULTIPLY,
|
|
|
|
&&label_JOP_DIVIDE_IMMEDIATE,
|
|
|
|
&&label_JOP_DIVIDE,
|
2020-01-24 00:54:30 +00:00
|
|
|
&&label_JOP_MODULO,
|
|
|
|
&&label_JOP_REMAINDER,
|
2019-05-24 17:54:23 +00:00
|
|
|
&&label_JOP_BAND,
|
|
|
|
&&label_JOP_BOR,
|
|
|
|
&&label_JOP_BXOR,
|
|
|
|
&&label_JOP_BNOT,
|
|
|
|
&&label_JOP_SHIFT_LEFT,
|
|
|
|
&&label_JOP_SHIFT_LEFT_IMMEDIATE,
|
|
|
|
&&label_JOP_SHIFT_RIGHT,
|
|
|
|
&&label_JOP_SHIFT_RIGHT_IMMEDIATE,
|
|
|
|
&&label_JOP_SHIFT_RIGHT_UNSIGNED,
|
|
|
|
&&label_JOP_SHIFT_RIGHT_UNSIGNED_IMMEDIATE,
|
|
|
|
&&label_JOP_MOVE_FAR,
|
|
|
|
&&label_JOP_MOVE_NEAR,
|
|
|
|
&&label_JOP_JUMP,
|
|
|
|
&&label_JOP_JUMP_IF,
|
|
|
|
&&label_JOP_JUMP_IF_NOT,
|
2020-01-18 23:55:07 +00:00
|
|
|
&&label_JOP_JUMP_IF_NIL,
|
|
|
|
&&label_JOP_JUMP_IF_NOT_NIL,
|
2019-05-24 17:54:23 +00:00
|
|
|
&&label_JOP_GREATER_THAN,
|
|
|
|
&&label_JOP_GREATER_THAN_IMMEDIATE,
|
|
|
|
&&label_JOP_LESS_THAN,
|
|
|
|
&&label_JOP_LESS_THAN_IMMEDIATE,
|
|
|
|
&&label_JOP_EQUALS,
|
|
|
|
&&label_JOP_EQUALS_IMMEDIATE,
|
|
|
|
&&label_JOP_COMPARE,
|
|
|
|
&&label_JOP_LOAD_NIL,
|
|
|
|
&&label_JOP_LOAD_TRUE,
|
|
|
|
&&label_JOP_LOAD_FALSE,
|
|
|
|
&&label_JOP_LOAD_INTEGER,
|
|
|
|
&&label_JOP_LOAD_CONSTANT,
|
|
|
|
&&label_JOP_LOAD_UPVALUE,
|
|
|
|
&&label_JOP_LOAD_SELF,
|
|
|
|
&&label_JOP_SET_UPVALUE,
|
|
|
|
&&label_JOP_CLOSURE,
|
|
|
|
&&label_JOP_PUSH,
|
|
|
|
&&label_JOP_PUSH_2,
|
|
|
|
&&label_JOP_PUSH_3,
|
|
|
|
&&label_JOP_PUSH_ARRAY,
|
|
|
|
&&label_JOP_CALL,
|
|
|
|
&&label_JOP_TAILCALL,
|
|
|
|
&&label_JOP_RESUME,
|
|
|
|
&&label_JOP_SIGNAL,
|
2019-06-24 16:44:13 +00:00
|
|
|
&&label_JOP_PROPAGATE,
|
2019-12-03 03:15:08 +00:00
|
|
|
&&label_JOP_IN,
|
2019-05-24 17:54:23 +00:00
|
|
|
&&label_JOP_GET,
|
|
|
|
&&label_JOP_PUT,
|
|
|
|
&&label_JOP_GET_INDEX,
|
|
|
|
&&label_JOP_PUT_INDEX,
|
|
|
|
&&label_JOP_LENGTH,
|
|
|
|
&&label_JOP_MAKE_ARRAY,
|
|
|
|
&&label_JOP_MAKE_BUFFER,
|
|
|
|
&&label_JOP_MAKE_STRING,
|
|
|
|
&&label_JOP_MAKE_STRUCT,
|
|
|
|
&&label_JOP_MAKE_TABLE,
|
|
|
|
&&label_JOP_MAKE_TUPLE,
|
|
|
|
&&label_JOP_MAKE_BRACKET_TUPLE,
|
2019-12-28 20:57:36 +00:00
|
|
|
&&label_JOP_GREATER_THAN_EQUAL,
|
|
|
|
&&label_JOP_LESS_THAN_EQUAL,
|
2020-01-18 23:55:07 +00:00
|
|
|
&&label_JOP_NEXT,
|
2020-06-28 23:16:57 +00:00
|
|
|
&&label_JOP_NOT_EQUALS,
|
|
|
|
&&label_JOP_NOT_EQUALS_IMMEDIATE,
|
2020-08-22 20:35:37 +00:00
|
|
|
&&label_JOP_CANCEL,
|
2019-09-22 22:50:31 +00:00
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
|
|
|
&&label_unknown_op,
|
2019-05-24 17:54:23 +00:00
|
|
|
&&label_unknown_op
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2019-01-05 02:15:37 +00:00
|
|
|
/* Interpreter state */
|
2019-01-05 01:08:43 +00:00
|
|
|
register Janet *stack;
|
|
|
|
register uint32_t *pc;
|
|
|
|
register JanetFunction *func;
|
2020-08-11 12:19:34 +00:00
|
|
|
|
|
|
|
if (fiber->flags & JANET_FIBER_RESUME_SIGNAL) {
|
|
|
|
JanetSignal sig = (fiber->gc.flags & JANET_FIBER_STATUS_MASK) >> JANET_FIBER_STATUS_OFFSET;
|
|
|
|
fiber->gc.flags &= ~JANET_FIBER_STATUS_MASK;
|
|
|
|
fiber->flags &= ~(JANET_FIBER_RESUME_SIGNAL | JANET_FIBER_FLAG_MASK);
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.return_reg[0] = in;
|
2020-08-11 12:19:34 +00:00
|
|
|
return sig;
|
|
|
|
}
|
|
|
|
|
2019-01-05 02:15:37 +00:00
|
|
|
vm_restore();
|
2019-01-06 08:23:03 +00:00
|
|
|
|
2020-01-10 23:25:10 +00:00
|
|
|
if (fiber->flags & JANET_FIBER_DID_LONGJUMP) {
|
|
|
|
if (janet_fiber_frame(fiber)->func == NULL) {
|
|
|
|
/* Inside a c function */
|
|
|
|
janet_fiber_popframe(fiber);
|
|
|
|
vm_restore();
|
|
|
|
}
|
|
|
|
/* Check if we were at a tail call instruction. If so, do implicit return */
|
|
|
|
if ((*pc & 0xFF) == JOP_TAILCALL) {
|
|
|
|
/* Tail call resume */
|
|
|
|
int entrance_frame = janet_stack_frame(stack)->flags & JANET_STACKFRAME_ENTRANCE;
|
|
|
|
janet_fiber_popframe(fiber);
|
|
|
|
if (entrance_frame) {
|
|
|
|
fiber->flags &= ~JANET_FIBER_FLAG_MASK;
|
|
|
|
vm_return(JANET_SIGNAL_OK, in);
|
|
|
|
}
|
|
|
|
vm_restore();
|
|
|
|
}
|
2019-01-05 01:08:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 23:25:10 +00:00
|
|
|
if (!(fiber->flags & JANET_FIBER_RESUME_NO_USEVAL)) stack[A] = in;
|
|
|
|
if (!(fiber->flags & JANET_FIBER_RESUME_NO_SKIP)) pc++;
|
|
|
|
|
|
|
|
uint8_t first_opcode = *pc & ((fiber->flags & JANET_FIBER_BREAKPOINT) ? 0x7F : 0xFF);
|
|
|
|
|
|
|
|
fiber->flags &= ~JANET_FIBER_FLAG_MASK;
|
|
|
|
|
2018-05-11 00:25:49 +00:00
|
|
|
/* Main interpreter loop. Semantically is a switch on
|
2019-01-06 08:23:03 +00:00
|
|
|
* (*pc & 0xFF) inside of an infinite loop. */
|
2018-02-01 02:40:19 +00:00
|
|
|
VM_START();
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2018-02-01 02:40:19 +00:00
|
|
|
VM_DEFAULT();
|
2020-01-10 23:25:10 +00:00
|
|
|
fiber->flags |= JANET_FIBER_BREAKPOINT | JANET_FIBER_RESUME_NO_USEVAL | JANET_FIBER_RESUME_NO_SKIP;
|
2019-01-05 04:20:34 +00:00
|
|
|
vm_return(JANET_SIGNAL_DEBUG, janet_wrap_nil());
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_NOOP)
|
2019-01-05 01:08:43 +00:00
|
|
|
vm_pcnext();
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_ERROR)
|
2019-01-05 05:33:20 +00:00
|
|
|
vm_return(JANET_SIGNAL_ERROR, stack[A]);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_TYPECHECK)
|
2019-01-05 05:33:20 +00:00
|
|
|
vm_assert_types(stack[A], E);
|
2019-01-05 01:08:43 +00:00
|
|
|
vm_pcnext();
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
VM_OP(JOP_RETURN) {
|
2019-01-05 05:33:20 +00:00
|
|
|
Janet retval = stack[D];
|
2019-02-01 16:56:25 +00:00
|
|
|
int entrance_frame = janet_stack_frame(stack)->flags & JANET_STACKFRAME_ENTRANCE;
|
2019-01-05 05:33:20 +00:00
|
|
|
janet_fiber_popframe(fiber);
|
2020-08-17 12:01:58 +00:00
|
|
|
if (entrance_frame) vm_return_no_restore(JANET_SIGNAL_OK, retval);
|
2019-01-05 05:33:20 +00:00
|
|
|
vm_restore();
|
|
|
|
stack[A] = retval;
|
|
|
|
vm_checkgc_pcnext();
|
|
|
|
}
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
VM_OP(JOP_RETURN_NIL) {
|
2019-01-05 05:33:20 +00:00
|
|
|
Janet retval = janet_wrap_nil();
|
2019-02-01 16:56:25 +00:00
|
|
|
int entrance_frame = janet_stack_frame(stack)->flags & JANET_STACKFRAME_ENTRANCE;
|
2019-01-05 05:33:20 +00:00
|
|
|
janet_fiber_popframe(fiber);
|
2020-08-17 12:01:58 +00:00
|
|
|
if (entrance_frame) vm_return_no_restore(JANET_SIGNAL_OK, retval);
|
2019-01-05 05:33:20 +00:00
|
|
|
vm_restore();
|
|
|
|
stack[A] = retval;
|
|
|
|
vm_checkgc_pcnext();
|
|
|
|
}
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_ADD_IMMEDIATE)
|
2018-02-01 02:40:19 +00:00
|
|
|
vm_binop_immediate(+);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_ADD)
|
2018-02-01 02:40:19 +00:00
|
|
|
vm_binop(+);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_SUBTRACT)
|
2018-02-01 02:40:19 +00:00
|
|
|
vm_binop(-);
|
2018-05-20 01:16:00 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_MULTIPLY_IMMEDIATE)
|
2018-02-01 02:40:19 +00:00
|
|
|
vm_binop_immediate(*);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_MULTIPLY)
|
2018-02-01 02:40:19 +00:00
|
|
|
vm_binop(*);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_DIVIDE_IMMEDIATE)
|
2019-02-20 01:51:34 +00:00
|
|
|
vm_binop_immediate( /);
|
2018-02-01 02:40:19 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_DIVIDE)
|
2019-02-20 01:51:34 +00:00
|
|
|
vm_binop( /);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2020-01-24 05:39:49 +00:00
|
|
|
VM_OP(JOP_MODULO) {
|
2020-01-24 00:54:30 +00:00
|
|
|
Janet op1 = stack[B];
|
|
|
|
Janet op2 = stack[C];
|
|
|
|
if (janet_checktype(op1, JANET_NUMBER) && janet_checktype(op2, JANET_NUMBER)) {
|
|
|
|
double x1 = janet_unwrap_number(op1);
|
|
|
|
double x2 = janet_unwrap_number(op2);
|
|
|
|
double intres = x2 * floor(x1 / x2);
|
|
|
|
stack[A] = janet_wrap_number(x1 - intres);
|
|
|
|
vm_pcnext();
|
|
|
|
} else {
|
|
|
|
vm_commit();
|
|
|
|
stack[A] = janet_binop_call("mod", "rmod", op1, op2);
|
|
|
|
vm_checkgc_pcnext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 05:39:49 +00:00
|
|
|
VM_OP(JOP_REMAINDER) {
|
2020-01-24 00:54:30 +00:00
|
|
|
Janet op1 = stack[B];
|
|
|
|
Janet op2 = stack[C];
|
|
|
|
if (janet_checktype(op1, JANET_NUMBER) && janet_checktype(op2, JANET_NUMBER)) {
|
|
|
|
double x1 = janet_unwrap_number(op1);
|
|
|
|
double x2 = janet_unwrap_number(op2);
|
|
|
|
stack[A] = janet_wrap_number(fmod(x1, x2));
|
|
|
|
vm_pcnext();
|
|
|
|
} else {
|
|
|
|
vm_commit();
|
|
|
|
stack[A] = janet_binop_call("%", "r%", op1, op2);
|
|
|
|
vm_checkgc_pcnext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_BAND)
|
2018-12-27 18:05:29 +00:00
|
|
|
vm_bitop(&);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_BOR)
|
2019-02-20 01:51:34 +00:00
|
|
|
vm_bitop( |);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_BXOR)
|
2018-12-27 18:05:29 +00:00
|
|
|
vm_bitop(^);
|
2017-02-13 05:11:30 +00:00
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
VM_OP(JOP_BNOT) {
|
2019-01-05 01:08:43 +00:00
|
|
|
Janet op = stack[E];
|
2018-12-27 18:05:29 +00:00
|
|
|
vm_assert_type(op, JANET_NUMBER);
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[A] = janet_wrap_integer(~janet_unwrap_integer(op));
|
|
|
|
vm_pcnext();
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2018-05-20 01:16:00 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_SHIFT_RIGHT_UNSIGNED)
|
2019-02-20 01:51:34 +00:00
|
|
|
vm_bitopu( >>);
|
2017-11-25 04:17:04 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_SHIFT_RIGHT_UNSIGNED_IMMEDIATE)
|
2019-02-20 01:51:34 +00:00
|
|
|
vm_bitopu_immediate( >>);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_SHIFT_RIGHT)
|
2019-02-20 01:51:34 +00:00
|
|
|
vm_bitop( >>);
|
2017-12-16 06:17:53 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_SHIFT_RIGHT_IMMEDIATE)
|
2019-02-20 01:51:34 +00:00
|
|
|
vm_bitop_immediate( >>);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_SHIFT_LEFT)
|
2019-02-20 01:51:34 +00:00
|
|
|
vm_bitop( <<);
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_SHIFT_LEFT_IMMEDIATE)
|
2019-02-20 01:51:34 +00:00
|
|
|
vm_bitop_immediate( <<);
|
2017-11-25 04:17:04 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_MOVE_NEAR)
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[A] = stack[E];
|
|
|
|
vm_pcnext();
|
2018-02-01 02:40:19 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_MOVE_FAR)
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[E] = stack[A];
|
|
|
|
vm_pcnext();
|
2018-02-01 02:40:19 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_JUMP)
|
2019-01-05 01:08:43 +00:00
|
|
|
pc += DS;
|
2021-07-24 20:14:37 +00:00
|
|
|
vm_maybe_auto_suspend(DS < 0);
|
2018-02-01 02:40:19 +00:00
|
|
|
vm_next();
|
2017-11-25 04:17:04 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_JUMP_IF)
|
2019-01-05 01:08:43 +00:00
|
|
|
if (janet_truthy(stack[A])) {
|
|
|
|
pc += ES;
|
2021-07-24 20:14:37 +00:00
|
|
|
vm_maybe_auto_suspend(ES < 0);
|
2018-02-01 02:40:19 +00:00
|
|
|
} else {
|
2017-11-27 00:31:40 +00:00
|
|
|
pc++;
|
2018-02-01 02:40:19 +00:00
|
|
|
}
|
|
|
|
vm_next();
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_JUMP_IF_NOT)
|
2019-01-05 01:08:43 +00:00
|
|
|
if (janet_truthy(stack[A])) {
|
2017-11-27 00:31:40 +00:00
|
|
|
pc++;
|
2018-02-01 02:40:19 +00:00
|
|
|
} else {
|
2019-01-05 01:08:43 +00:00
|
|
|
pc += ES;
|
2021-07-24 20:14:37 +00:00
|
|
|
vm_maybe_auto_suspend(ES < 0);
|
2018-02-01 02:40:19 +00:00
|
|
|
}
|
|
|
|
vm_next();
|
|
|
|
|
2020-01-18 23:55:07 +00:00
|
|
|
VM_OP(JOP_JUMP_IF_NIL)
|
|
|
|
if (janet_checktype(stack[A], JANET_NIL)) {
|
|
|
|
pc += ES;
|
2021-07-24 20:14:37 +00:00
|
|
|
vm_maybe_auto_suspend(ES < 0);
|
2020-01-18 23:55:07 +00:00
|
|
|
} else {
|
|
|
|
pc++;
|
|
|
|
}
|
|
|
|
vm_next();
|
|
|
|
|
|
|
|
VM_OP(JOP_JUMP_IF_NOT_NIL)
|
|
|
|
if (janet_checktype(stack[A], JANET_NIL)) {
|
|
|
|
pc++;
|
|
|
|
} else {
|
|
|
|
pc += ES;
|
2021-07-24 20:14:37 +00:00
|
|
|
vm_maybe_auto_suspend(ES < 0);
|
2020-01-18 23:55:07 +00:00
|
|
|
}
|
|
|
|
vm_next();
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_LESS_THAN)
|
2019-12-28 20:57:36 +00:00
|
|
|
vm_compop( <);
|
|
|
|
|
|
|
|
VM_OP(JOP_LESS_THAN_EQUAL)
|
|
|
|
vm_compop( <=);
|
2018-02-01 02:40:19 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_LESS_THAN_IMMEDIATE)
|
2021-02-08 17:53:25 +00:00
|
|
|
vm_compop_imm( <);
|
2018-05-07 16:34:04 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_GREATER_THAN)
|
2019-12-28 20:57:36 +00:00
|
|
|
vm_compop( >);
|
|
|
|
|
|
|
|
VM_OP(JOP_GREATER_THAN_EQUAL)
|
|
|
|
vm_compop( >=);
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_GREATER_THAN_IMMEDIATE)
|
2021-02-08 17:53:25 +00:00
|
|
|
vm_compop_imm( >);
|
2018-05-07 16:34:04 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_EQUALS)
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[A] = janet_wrap_boolean(janet_equals(stack[B], stack[C]));
|
|
|
|
vm_pcnext();
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_EQUALS_IMMEDIATE)
|
2021-02-08 17:41:48 +00:00
|
|
|
stack[A] = janet_wrap_boolean(janet_unwrap_number(stack[B]) == (double) CS);
|
2019-01-05 01:08:43 +00:00
|
|
|
vm_pcnext();
|
2018-05-07 16:34:04 +00:00
|
|
|
|
2020-06-28 23:16:57 +00:00
|
|
|
VM_OP(JOP_NOT_EQUALS)
|
|
|
|
stack[A] = janet_wrap_boolean(!janet_equals(stack[B], stack[C]));
|
|
|
|
vm_pcnext();
|
|
|
|
|
|
|
|
VM_OP(JOP_NOT_EQUALS_IMMEDIATE)
|
2021-02-08 17:41:48 +00:00
|
|
|
stack[A] = janet_wrap_boolean(janet_unwrap_number(stack[B]) != (double) CS);
|
2020-06-28 23:16:57 +00:00
|
|
|
vm_pcnext();
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_COMPARE)
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[A] = janet_wrap_integer(janet_compare(stack[B], stack[C]));
|
|
|
|
vm_pcnext();
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2020-01-18 23:55:07 +00:00
|
|
|
VM_OP(JOP_NEXT)
|
2020-08-14 03:28:50 +00:00
|
|
|
vm_commit();
|
2021-01-03 22:17:36 +00:00
|
|
|
{
|
|
|
|
Janet temp = janet_next_impl(stack[B], stack[C], 1);
|
|
|
|
vm_restore();
|
|
|
|
stack[A] = temp;
|
|
|
|
}
|
2020-01-18 23:55:07 +00:00
|
|
|
vm_pcnext();
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_LOAD_NIL)
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[D] = janet_wrap_nil();
|
|
|
|
vm_pcnext();
|
2017-12-16 06:17:53 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_LOAD_TRUE)
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[D] = janet_wrap_true();
|
|
|
|
vm_pcnext();
|
2017-04-24 20:02:54 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_LOAD_FALSE)
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[D] = janet_wrap_false();
|
|
|
|
vm_pcnext();
|
2017-02-13 05:11:30 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_LOAD_INTEGER)
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[A] = janet_wrap_integer(ES);
|
|
|
|
vm_pcnext();
|
2017-02-13 05:11:30 +00:00
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
VM_OP(JOP_LOAD_CONSTANT) {
|
2019-01-05 01:08:43 +00:00
|
|
|
int32_t cindex = (int32_t)E;
|
|
|
|
vm_assert(cindex < func->def->constants_length, "invalid constant");
|
|
|
|
stack[A] = func->def->constants[cindex];
|
|
|
|
vm_pcnext();
|
2018-02-01 02:40:19 +00:00
|
|
|
}
|
2017-12-16 06:17:53 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_LOAD_SELF)
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[D] = janet_wrap_function(func);
|
|
|
|
vm_pcnext();
|
2017-04-19 13:02:12 +00:00
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
VM_OP(JOP_LOAD_UPVALUE) {
|
2019-01-05 01:08:43 +00:00
|
|
|
int32_t eindex = B;
|
|
|
|
int32_t vindex = C;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFuncEnv *env;
|
2018-05-17 02:09:36 +00:00
|
|
|
vm_assert(func->def->environments_length > eindex, "invalid upvalue environment");
|
2018-02-01 02:40:19 +00:00
|
|
|
env = func->envs[eindex];
|
2018-05-17 02:09:36 +00:00
|
|
|
vm_assert(env->length > vindex, "invalid upvalue index");
|
2020-04-06 15:58:47 +00:00
|
|
|
vm_assert(janet_env_valid(env), "invalid upvalue environment");
|
|
|
|
if (env->offset > 0) {
|
2018-02-01 02:40:19 +00:00
|
|
|
/* On stack */
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[A] = env->as.fiber->data[env->offset + vindex];
|
2018-02-01 02:40:19 +00:00
|
|
|
} else {
|
|
|
|
/* Off stack */
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[A] = env->as.values[vindex];
|
2017-11-27 00:31:40 +00:00
|
|
|
}
|
2019-01-05 01:08:43 +00:00
|
|
|
vm_pcnext();
|
2018-02-01 02:40:19 +00:00
|
|
|
}
|
2017-04-19 13:02:12 +00:00
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
VM_OP(JOP_SET_UPVALUE) {
|
2019-01-05 01:08:43 +00:00
|
|
|
int32_t eindex = B;
|
|
|
|
int32_t vindex = C;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFuncEnv *env;
|
2018-05-17 02:09:36 +00:00
|
|
|
vm_assert(func->def->environments_length > eindex, "invalid upvalue environment");
|
2018-02-01 02:40:19 +00:00
|
|
|
env = func->envs[eindex];
|
2018-05-17 02:09:36 +00:00
|
|
|
vm_assert(env->length > vindex, "invalid upvalue index");
|
2020-04-06 15:58:47 +00:00
|
|
|
vm_assert(janet_env_valid(env), "invalid upvalue environment");
|
|
|
|
if (env->offset > 0) {
|
2019-01-05 01:08:43 +00:00
|
|
|
env->as.fiber->data[env->offset + vindex] = stack[A];
|
2018-02-01 02:40:19 +00:00
|
|
|
} else {
|
2019-01-05 01:08:43 +00:00
|
|
|
env->as.values[vindex] = stack[A];
|
2018-02-01 02:40:19 +00:00
|
|
|
}
|
2019-01-05 01:08:43 +00:00
|
|
|
vm_pcnext();
|
2018-02-01 02:40:19 +00:00
|
|
|
}
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
VM_OP(JOP_CLOSURE) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFuncDef *fd;
|
|
|
|
JanetFunction *fn;
|
2018-02-13 21:14:55 +00:00
|
|
|
int32_t elen;
|
2019-01-05 01:08:43 +00:00
|
|
|
int32_t defindex = (int32_t)E;
|
|
|
|
vm_assert(defindex < func->def->defs_length, "invalid funcdef");
|
|
|
|
fd = func->def->defs[defindex];
|
2018-02-13 21:14:55 +00:00
|
|
|
elen = fd->environments_length;
|
2020-01-03 04:02:57 +00:00
|
|
|
fn = janet_gcalloc(JANET_MEMORY_FUNCTION, sizeof(JanetFunction) + ((size_t) elen * sizeof(JanetFuncEnv *)));
|
2018-02-13 21:14:55 +00:00
|
|
|
fn->def = fd;
|
2018-02-12 21:43:59 +00:00
|
|
|
{
|
2018-02-13 21:14:55 +00:00
|
|
|
int32_t i;
|
|
|
|
for (i = 0; i < elen; ++i) {
|
|
|
|
int32_t inherit = fd->environments[i];
|
|
|
|
if (inherit == -1) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetStackFrame *frame = janet_stack_frame(stack);
|
2018-02-13 21:14:55 +00:00
|
|
|
if (!frame->env) {
|
|
|
|
/* Lazy capture of current stack frame */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFuncEnv *env = janet_gcalloc(JANET_MEMORY_FUNCENV, sizeof(JanetFuncEnv));
|
2018-03-11 19:35:23 +00:00
|
|
|
env->offset = fiber->frame;
|
|
|
|
env->as.fiber = fiber;
|
2018-02-13 21:14:55 +00:00
|
|
|
env->length = func->def->slotcount;
|
|
|
|
frame->env = env;
|
2018-02-12 21:43:59 +00:00
|
|
|
}
|
2018-02-13 21:14:55 +00:00
|
|
|
fn->envs[i] = frame->env;
|
|
|
|
} else {
|
|
|
|
fn->envs[i] = func->envs[inherit];
|
2018-02-12 21:43:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[A] = janet_wrap_function(fn);
|
|
|
|
vm_checkgc_pcnext();
|
2018-05-20 01:16:00 +00:00
|
|
|
}
|
2017-04-19 13:02:12 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_PUSH)
|
2019-01-05 01:08:43 +00:00
|
|
|
janet_fiber_push(fiber, stack[D]);
|
|
|
|
stack = fiber->data + fiber->frame;
|
|
|
|
vm_checkgc_pcnext();
|
2018-02-01 02:40:19 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_PUSH_2)
|
2019-01-05 01:08:43 +00:00
|
|
|
janet_fiber_push2(fiber, stack[A], stack[E]);
|
2018-03-11 19:35:23 +00:00
|
|
|
stack = fiber->data + fiber->frame;
|
2019-01-05 01:08:43 +00:00
|
|
|
vm_checkgc_pcnext();
|
2018-02-01 02:40:19 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_PUSH_3)
|
2019-01-05 01:08:43 +00:00
|
|
|
janet_fiber_push3(fiber, stack[A], stack[B], stack[C]);
|
2018-03-11 19:35:23 +00:00
|
|
|
stack = fiber->data + fiber->frame;
|
2019-01-05 01:08:43 +00:00
|
|
|
vm_checkgc_pcnext();
|
2018-02-01 02:40:19 +00:00
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
VM_OP(JOP_PUSH_ARRAY) {
|
2018-09-06 02:18:42 +00:00
|
|
|
const Janet *vals;
|
2018-02-01 02:40:19 +00:00
|
|
|
int32_t len;
|
2019-01-05 01:08:43 +00:00
|
|
|
if (janet_indexed_view(stack[D], &vals, &len)) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_fiber_pushn(fiber, vals, len);
|
2018-02-01 02:40:19 +00:00
|
|
|
} else {
|
2020-04-14 01:24:11 +00:00
|
|
|
janet_panicf("expected %T, got %v", JANET_TFLAG_INDEXED, stack[D]);
|
2018-01-12 18:54:37 +00:00
|
|
|
}
|
2018-02-01 02:40:19 +00:00
|
|
|
}
|
2018-03-11 19:35:23 +00:00
|
|
|
stack = fiber->data + fiber->frame;
|
2019-01-05 01:08:43 +00:00
|
|
|
vm_checkgc_pcnext();
|
2018-02-01 02:40:19 +00:00
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
VM_OP(JOP_CALL) {
|
2021-07-24 20:14:37 +00:00
|
|
|
vm_maybe_auto_suspend(1);
|
2019-01-05 01:08:43 +00:00
|
|
|
Janet callee = stack[E];
|
2019-01-05 04:20:34 +00:00
|
|
|
if (fiber->stacktop > fiber->maxstack) {
|
2018-02-12 16:49:10 +00:00
|
|
|
vm_throw("stack overflow");
|
|
|
|
}
|
2019-01-07 19:47:47 +00:00
|
|
|
if (janet_checktype(callee, JANET_KEYWORD)) {
|
|
|
|
vm_commit();
|
2019-08-06 00:06:58 +00:00
|
|
|
callee = resolve_method(callee, fiber);
|
2019-01-07 19:47:47 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_checktype(callee, JANET_FUNCTION)) {
|
|
|
|
func = janet_unwrap_function(callee);
|
2020-01-23 15:01:33 +00:00
|
|
|
if (func->gc.flags & JANET_FUNCFLAG_TRACE) {
|
2020-06-14 16:58:20 +00:00
|
|
|
vm_do_trace(func, fiber->stacktop - fiber->stackstart, fiber->data + fiber->stackstart);
|
2020-01-23 15:01:33 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_stack_frame(stack)->pc = pc;
|
2019-01-05 04:20:34 +00:00
|
|
|
if (janet_fiber_funcframe(fiber, func)) {
|
|
|
|
int32_t n = fiber->stacktop - fiber->stackstart;
|
|
|
|
janet_panicf("%v called with %d argument%s, expected %d",
|
2019-02-20 01:51:34 +00:00
|
|
|
callee, n, n == 1 ? "" : "s", func->def->arity);
|
2019-01-05 04:20:34 +00:00
|
|
|
}
|
2018-03-11 19:35:23 +00:00
|
|
|
stack = fiber->data + fiber->frame;
|
2018-02-01 02:40:19 +00:00
|
|
|
pc = func->def->bytecode;
|
|
|
|
vm_checkgc_next();
|
2018-09-06 02:18:42 +00:00
|
|
|
} else if (janet_checktype(callee, JANET_CFUNCTION)) {
|
2019-01-05 05:33:20 +00:00
|
|
|
vm_commit();
|
2019-01-06 01:09:03 +00:00
|
|
|
int32_t argc = fiber->stacktop - fiber->stackstart;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_fiber_cframe(fiber, janet_unwrap_cfunction(callee));
|
2019-01-06 01:09:03 +00:00
|
|
|
Janet ret = janet_unwrap_cfunction(callee)(argc, fiber->data + fiber->frame);
|
2019-01-05 02:15:37 +00:00
|
|
|
janet_fiber_popframe(fiber);
|
|
|
|
stack = fiber->data + fiber->frame;
|
2019-01-06 01:09:03 +00:00
|
|
|
stack[A] = ret;
|
2019-01-05 02:15:37 +00:00
|
|
|
vm_checkgc_pcnext();
|
2019-01-04 03:46:25 +00:00
|
|
|
} else {
|
2019-01-05 04:20:34 +00:00
|
|
|
vm_commit();
|
|
|
|
stack[A] = call_nonfn(fiber, callee);
|
2019-01-05 01:08:43 +00:00
|
|
|
vm_pcnext();
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2018-02-01 02:40:19 +00:00
|
|
|
}
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
VM_OP(JOP_TAILCALL) {
|
2021-07-24 20:14:37 +00:00
|
|
|
vm_maybe_auto_suspend(1);
|
2019-01-05 01:08:43 +00:00
|
|
|
Janet callee = stack[D];
|
2019-08-19 05:19:51 +00:00
|
|
|
if (fiber->stacktop > fiber->maxstack) {
|
|
|
|
vm_throw("stack overflow");
|
|
|
|
}
|
2019-01-07 19:47:47 +00:00
|
|
|
if (janet_checktype(callee, JANET_KEYWORD)) {
|
|
|
|
vm_commit();
|
2019-08-06 00:06:58 +00:00
|
|
|
callee = resolve_method(callee, fiber);
|
2019-01-07 19:47:47 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_checktype(callee, JANET_FUNCTION)) {
|
|
|
|
func = janet_unwrap_function(callee);
|
2020-01-23 15:01:33 +00:00
|
|
|
if (func->gc.flags & JANET_FUNCFLAG_TRACE) {
|
2020-06-14 16:58:20 +00:00
|
|
|
vm_do_trace(func, fiber->stacktop - fiber->stackstart, fiber->data + fiber->stackstart);
|
2020-01-23 15:01:33 +00:00
|
|
|
}
|
2019-01-05 04:20:34 +00:00
|
|
|
if (janet_fiber_funcframe_tail(fiber, func)) {
|
2019-01-06 16:56:40 +00:00
|
|
|
janet_stack_frame(fiber->data + fiber->frame)->pc = pc;
|
2019-01-05 04:20:34 +00:00
|
|
|
int32_t n = fiber->stacktop - fiber->stackstart;
|
|
|
|
janet_panicf("%v called with %d argument%s, expected %d",
|
2019-02-20 01:51:34 +00:00
|
|
|
callee, n, n == 1 ? "" : "s", func->def->arity);
|
2019-01-05 04:20:34 +00:00
|
|
|
}
|
2018-03-11 19:35:23 +00:00
|
|
|
stack = fiber->data + fiber->frame;
|
2018-02-01 02:40:19 +00:00
|
|
|
pc = func->def->bytecode;
|
|
|
|
vm_checkgc_next();
|
2019-01-04 03:46:25 +00:00
|
|
|
} else {
|
2019-01-06 01:09:03 +00:00
|
|
|
Janet retreg;
|
2019-02-01 16:56:25 +00:00
|
|
|
int entrance_frame = janet_stack_frame(stack)->flags & JANET_STACKFRAME_ENTRANCE;
|
2019-01-06 01:09:03 +00:00
|
|
|
vm_commit();
|
2019-01-05 05:33:20 +00:00
|
|
|
if (janet_checktype(callee, JANET_CFUNCTION)) {
|
2019-01-06 01:09:03 +00:00
|
|
|
int32_t argc = fiber->stacktop - fiber->stackstart;
|
2019-01-05 05:33:20 +00:00
|
|
|
janet_fiber_cframe(fiber, janet_unwrap_cfunction(callee));
|
2019-01-06 01:09:03 +00:00
|
|
|
retreg = janet_unwrap_cfunction(callee)(argc, fiber->data + fiber->frame);
|
2019-01-05 05:33:20 +00:00
|
|
|
janet_fiber_popframe(fiber);
|
|
|
|
} else {
|
|
|
|
retreg = call_nonfn(fiber, callee);
|
|
|
|
}
|
|
|
|
janet_fiber_popframe(fiber);
|
2020-08-17 12:01:58 +00:00
|
|
|
if (entrance_frame) {
|
|
|
|
vm_return_no_restore(JANET_SIGNAL_OK, retreg);
|
|
|
|
}
|
2019-01-05 05:33:20 +00:00
|
|
|
vm_restore();
|
|
|
|
stack[A] = retreg;
|
|
|
|
vm_checkgc_pcnext();
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2018-02-01 02:40:19 +00:00
|
|
|
}
|
2017-04-12 14:31:50 +00:00
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
VM_OP(JOP_RESUME) {
|
2019-01-05 05:33:20 +00:00
|
|
|
Janet retreg;
|
2021-07-24 20:14:37 +00:00
|
|
|
vm_maybe_auto_suspend(1);
|
2019-01-05 02:15:37 +00:00
|
|
|
vm_assert_type(stack[B], JANET_FIBER);
|
|
|
|
JanetFiber *child = janet_unwrap_fiber(stack[B]);
|
2022-02-10 03:48:49 +00:00
|
|
|
if (janet_check_can_resume(child, &retreg, 0)) {
|
2020-04-10 23:29:10 +00:00
|
|
|
vm_commit();
|
|
|
|
janet_panicv(retreg);
|
|
|
|
}
|
2019-01-05 02:15:37 +00:00
|
|
|
fiber->child = child;
|
2020-04-10 23:29:10 +00:00
|
|
|
JanetSignal sig = janet_continue_no_check(child, stack[C], &retreg);
|
2020-01-10 23:25:10 +00:00
|
|
|
if (sig != JANET_SIGNAL_OK && !(child->flags & (1 << sig))) {
|
2019-01-05 04:20:34 +00:00
|
|
|
vm_return(sig, retreg);
|
2020-01-10 23:25:10 +00:00
|
|
|
}
|
2019-01-05 02:15:37 +00:00
|
|
|
fiber->child = NULL;
|
2019-04-11 03:29:40 +00:00
|
|
|
stack = fiber->data + fiber->frame;
|
2019-01-05 02:15:37 +00:00
|
|
|
stack[A] = retreg;
|
|
|
|
vm_checkgc_pcnext();
|
2018-02-01 02:40:19 +00:00
|
|
|
}
|
2017-11-25 04:17:04 +00:00
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
VM_OP(JOP_SIGNAL) {
|
2019-01-05 01:08:43 +00:00
|
|
|
int32_t s = C;
|
2018-09-06 02:18:42 +00:00
|
|
|
if (s > JANET_SIGNAL_USER9) s = JANET_SIGNAL_USER9;
|
2018-05-17 02:09:36 +00:00
|
|
|
if (s < 0) s = 0;
|
2019-01-05 04:20:34 +00:00
|
|
|
vm_return(s, stack[B]);
|
2018-03-11 19:35:23 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 16:44:13 +00:00
|
|
|
VM_OP(JOP_PROPAGATE) {
|
|
|
|
Janet fv = stack[C];
|
|
|
|
vm_assert_type(fv, JANET_FIBER);
|
|
|
|
JanetFiber *f = janet_unwrap_fiber(fv);
|
2019-10-11 03:59:43 +00:00
|
|
|
JanetFiberStatus sub_status = janet_fiber_status(f);
|
|
|
|
if (sub_status > JANET_STATUS_USER9) {
|
2019-11-24 23:45:53 +00:00
|
|
|
vm_commit();
|
|
|
|
janet_panicf("cannot propagate from fiber with status :%s",
|
|
|
|
janet_status_names[sub_status]);
|
2019-06-24 16:44:13 +00:00
|
|
|
}
|
2020-01-10 23:25:10 +00:00
|
|
|
fiber->child = f;
|
2021-01-17 21:33:42 +00:00
|
|
|
vm_return((int) sub_status, stack[B]);
|
2019-06-24 16:44:13 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 20:35:37 +00:00
|
|
|
VM_OP(JOP_CANCEL) {
|
|
|
|
Janet retreg;
|
|
|
|
vm_assert_type(stack[B], JANET_FIBER);
|
|
|
|
JanetFiber *child = janet_unwrap_fiber(stack[B]);
|
2022-02-10 03:48:49 +00:00
|
|
|
if (janet_check_can_resume(child, &retreg, 1)) {
|
2020-08-22 20:35:37 +00:00
|
|
|
vm_commit();
|
|
|
|
janet_panicv(retreg);
|
|
|
|
}
|
|
|
|
fiber->child = child;
|
|
|
|
JanetSignal sig = janet_continue_signal(child, stack[C], &retreg, JANET_SIGNAL_ERROR);
|
|
|
|
if (sig != JANET_SIGNAL_OK && !(child->flags & (1 << sig))) {
|
|
|
|
vm_return(sig, retreg);
|
|
|
|
}
|
|
|
|
fiber->child = NULL;
|
|
|
|
stack = fiber->data + fiber->frame;
|
|
|
|
stack[A] = retreg;
|
|
|
|
vm_checkgc_pcnext();
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_PUT)
|
2019-01-06 01:09:03 +00:00
|
|
|
vm_commit();
|
2020-01-10 23:25:10 +00:00
|
|
|
fiber->flags |= JANET_FIBER_RESUME_NO_USEVAL;
|
2019-01-05 05:33:20 +00:00
|
|
|
janet_put(stack[A], stack[B], stack[C]);
|
2020-01-10 23:25:10 +00:00
|
|
|
fiber->flags &= ~JANET_FIBER_RESUME_NO_USEVAL;
|
2019-01-05 05:33:20 +00:00
|
|
|
vm_checkgc_pcnext();
|
2017-11-25 04:17:04 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_PUT_INDEX)
|
2019-01-06 01:09:03 +00:00
|
|
|
vm_commit();
|
2020-01-10 23:25:10 +00:00
|
|
|
fiber->flags |= JANET_FIBER_RESUME_NO_USEVAL;
|
2019-01-05 05:33:20 +00:00
|
|
|
janet_putindex(stack[A], C, stack[B]);
|
2020-01-10 23:25:10 +00:00
|
|
|
fiber->flags &= ~JANET_FIBER_RESUME_NO_USEVAL;
|
2019-01-05 05:33:20 +00:00
|
|
|
vm_checkgc_pcnext();
|
2017-11-25 04:17:04 +00:00
|
|
|
|
2019-12-03 03:15:08 +00:00
|
|
|
VM_OP(JOP_IN)
|
|
|
|
vm_commit();
|
|
|
|
stack[A] = janet_in(stack[B], stack[C]);
|
|
|
|
vm_pcnext();
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_GET)
|
2019-01-06 01:09:03 +00:00
|
|
|
vm_commit();
|
2019-01-05 05:33:20 +00:00
|
|
|
stack[A] = janet_get(stack[B], stack[C]);
|
|
|
|
vm_pcnext();
|
2017-11-25 04:17:04 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_GET_INDEX)
|
2019-01-06 01:09:03 +00:00
|
|
|
vm_commit();
|
2019-01-05 05:33:20 +00:00
|
|
|
stack[A] = janet_getindex(stack[B], C);
|
|
|
|
vm_pcnext();
|
2018-02-01 02:40:19 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
VM_OP(JOP_LENGTH)
|
2019-01-06 01:09:03 +00:00
|
|
|
vm_commit();
|
2019-09-08 20:36:21 +00:00
|
|
|
stack[A] = janet_lengthv(stack[E]);
|
2019-01-05 05:33:20 +00:00
|
|
|
vm_pcnext();
|
2018-02-01 02:40:19 +00:00
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
VM_OP(JOP_MAKE_ARRAY) {
|
2018-07-04 03:07:35 +00:00
|
|
|
int32_t count = fiber->stacktop - fiber->stackstart;
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet *mem = fiber->data + fiber->stackstart;
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[D] = janet_wrap_array(janet_array_n(mem, count));
|
2018-07-04 03:07:35 +00:00
|
|
|
fiber->stacktop = fiber->stackstart;
|
2019-01-05 01:08:43 +00:00
|
|
|
vm_checkgc_pcnext();
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 18:14:25 +00:00
|
|
|
VM_OP(JOP_MAKE_TUPLE)
|
|
|
|
/* fallthrough */
|
|
|
|
VM_OP(JOP_MAKE_BRACKET_TUPLE) {
|
2018-07-04 03:07:35 +00:00
|
|
|
int32_t count = fiber->stacktop - fiber->stackstart;
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet *mem = fiber->data + fiber->stackstart;
|
2019-03-31 18:14:25 +00:00
|
|
|
const Janet *tup = janet_tuple_n(mem, count);
|
|
|
|
if (opcode == JOP_MAKE_BRACKET_TUPLE)
|
|
|
|
janet_tuple_flag(tup) |= JANET_TUPLE_FLAG_BRACKETCTOR;
|
|
|
|
stack[D] = janet_wrap_tuple(tup);
|
2018-07-04 03:07:35 +00:00
|
|
|
fiber->stacktop = fiber->stackstart;
|
2019-01-05 01:08:43 +00:00
|
|
|
vm_checkgc_pcnext();
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
VM_OP(JOP_MAKE_TABLE) {
|
2018-07-04 03:07:35 +00:00
|
|
|
int32_t count = fiber->stacktop - fiber->stackstart;
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet *mem = fiber->data + fiber->stackstart;
|
2019-11-24 23:45:53 +00:00
|
|
|
if (count & 1) {
|
|
|
|
vm_commit();
|
|
|
|
janet_panicf("expected even number of arguments to table constructor, got %d", count);
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetTable *table = janet_table(count / 2);
|
2018-07-04 03:07:35 +00:00
|
|
|
for (int32_t i = 0; i < count; i += 2)
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_table_put(table, mem[i], mem[i + 1]);
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[D] = janet_wrap_table(table);
|
2018-07-04 03:07:35 +00:00
|
|
|
fiber->stacktop = fiber->stackstart;
|
2019-01-05 01:08:43 +00:00
|
|
|
vm_checkgc_pcnext();
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
VM_OP(JOP_MAKE_STRUCT) {
|
2018-07-04 03:07:35 +00:00
|
|
|
int32_t count = fiber->stacktop - fiber->stackstart;
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet *mem = fiber->data + fiber->stackstart;
|
2019-11-24 23:45:53 +00:00
|
|
|
if (count & 1) {
|
|
|
|
vm_commit();
|
|
|
|
janet_panicf("expected even number of arguments to struct constructor, got %d", count);
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetKV *st = janet_struct_begin(count / 2);
|
2018-07-04 03:07:35 +00:00
|
|
|
for (int32_t i = 0; i < count; i += 2)
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_struct_put(st, mem[i], mem[i + 1]);
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[D] = janet_wrap_struct(janet_struct_end(st));
|
2018-07-04 03:07:35 +00:00
|
|
|
fiber->stacktop = fiber->stackstart;
|
2019-01-05 01:08:43 +00:00
|
|
|
vm_checkgc_pcnext();
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
VM_OP(JOP_MAKE_STRING) {
|
2018-07-04 03:07:35 +00:00
|
|
|
int32_t count = fiber->stacktop - fiber->stackstart;
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet *mem = fiber->data + fiber->stackstart;
|
|
|
|
JanetBuffer buffer;
|
|
|
|
janet_buffer_init(&buffer, 10 * count);
|
2018-07-04 03:07:35 +00:00
|
|
|
for (int32_t i = 0; i < count; i++)
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_to_string_b(&buffer, mem[i]);
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[D] = janet_stringv(buffer.data, buffer.count);
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_deinit(&buffer);
|
2018-07-04 03:07:35 +00:00
|
|
|
fiber->stacktop = fiber->stackstart;
|
2019-01-05 01:08:43 +00:00
|
|
|
vm_checkgc_pcnext();
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
VM_OP(JOP_MAKE_BUFFER) {
|
2018-07-04 03:07:35 +00:00
|
|
|
int32_t count = fiber->stacktop - fiber->stackstart;
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet *mem = fiber->data + fiber->stackstart;
|
|
|
|
JanetBuffer *buffer = janet_buffer(10 * count);
|
2018-07-04 03:07:35 +00:00
|
|
|
for (int32_t i = 0; i < count; i++)
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_to_string_b(buffer, mem[i]);
|
2019-01-05 01:08:43 +00:00
|
|
|
stack[D] = janet_wrap_buffer(buffer);
|
2018-07-04 03:07:35 +00:00
|
|
|
fiber->stacktop = fiber->stackstart;
|
2019-01-05 01:08:43 +00:00
|
|
|
vm_checkgc_pcnext();
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 02:40:19 +00:00
|
|
|
VM_END()
|
2019-01-05 01:08:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 00:14:34 +00:00
|
|
|
/*
|
|
|
|
* Execute a single instruction in the fiber. Does this by inspecting
|
|
|
|
* the fiber, setting a breakpoint at the next instruction, executing, and
|
|
|
|
* reseting breakpoints to how they were prior. Yes, it's a bit hacky.
|
|
|
|
*/
|
|
|
|
JanetSignal janet_step(JanetFiber *fiber, Janet in, Janet *out) {
|
2019-11-26 02:00:13 +00:00
|
|
|
/* No finished or currently alive fibers. */
|
|
|
|
JanetFiberStatus status = janet_fiber_status(fiber);
|
|
|
|
if (status == JANET_STATUS_ALIVE ||
|
|
|
|
status == JANET_STATUS_DEAD ||
|
|
|
|
status == JANET_STATUS_ERROR) {
|
|
|
|
janet_panicf("cannot step fiber with status :%s", janet_status_names[status]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get PC for setting breakpoints */
|
|
|
|
uint32_t *pc = janet_stack_frame(fiber->data + fiber->frame)->pc;
|
2019-11-26 00:14:34 +00:00
|
|
|
|
|
|
|
/* Check current opcode (sans debug flag). This tells us where the next or next two candidate
|
|
|
|
* instructions will be. Usually it's the next instruction in memory,
|
|
|
|
* but for branching instructions it is also the target of the branch. */
|
2019-12-08 18:39:37 +00:00
|
|
|
uint32_t *nexta = NULL, *nextb = NULL, olda = 0, oldb = 0;
|
2019-11-26 00:14:34 +00:00
|
|
|
|
|
|
|
/* Set temporary breakpoints */
|
|
|
|
switch (*pc & 0x7F) {
|
|
|
|
default:
|
|
|
|
nexta = pc + 1;
|
|
|
|
break;
|
|
|
|
/* These we just ignore for now. Supporting them means
|
|
|
|
* we could step into and out of functions (including JOP_CALL). */
|
|
|
|
case JOP_RETURN_NIL:
|
|
|
|
case JOP_RETURN:
|
|
|
|
case JOP_ERROR:
|
|
|
|
case JOP_TAILCALL:
|
|
|
|
break;
|
|
|
|
case JOP_JUMP:
|
|
|
|
nexta = pc + DS;
|
|
|
|
break;
|
|
|
|
case JOP_JUMP_IF:
|
|
|
|
case JOP_JUMP_IF_NOT:
|
|
|
|
nexta = pc + 1;
|
|
|
|
nextb = pc + ES;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (nexta) {
|
|
|
|
olda = *nexta;
|
|
|
|
*nexta |= 0x80;
|
|
|
|
}
|
|
|
|
if (nextb) {
|
|
|
|
oldb = *nextb;
|
|
|
|
*nextb |= 0x80;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Go */
|
2019-11-26 02:00:13 +00:00
|
|
|
JanetSignal signal = janet_continue(fiber, in, out);
|
2019-11-26 00:14:34 +00:00
|
|
|
|
|
|
|
/* Restore */
|
|
|
|
if (nexta) *nexta = olda;
|
|
|
|
if (nextb) *nextb = oldb;
|
|
|
|
|
|
|
|
return signal;
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:31:15 +00:00
|
|
|
Janet janet_call(JanetFunction *fun, int32_t argc, const Janet *argv) {
|
2019-02-01 16:56:25 +00:00
|
|
|
/* Check entry conditions */
|
2021-07-17 01:59:03 +00:00
|
|
|
if (!janet_vm.fiber)
|
2019-02-01 16:56:25 +00:00
|
|
|
janet_panic("janet_call failed because there is no current fiber");
|
2021-07-17 01:59:03 +00:00
|
|
|
if (janet_vm.stackn >= JANET_RECURSION_GUARD)
|
2019-01-12 22:31:15 +00:00
|
|
|
janet_panic("C stack recursed too deeply");
|
2019-02-01 16:56:25 +00:00
|
|
|
|
2020-01-23 15:01:33 +00:00
|
|
|
/* Tracing */
|
|
|
|
if (fun->gc.flags & JANET_FUNCFLAG_TRACE) {
|
|
|
|
vm_do_trace(fun, argc, argv);
|
|
|
|
}
|
|
|
|
|
2019-02-01 16:56:25 +00:00
|
|
|
/* Push frame */
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_fiber_pushn(janet_vm.fiber, argv, argc);
|
|
|
|
if (janet_fiber_funcframe(janet_vm.fiber, fun)) {
|
2021-02-06 00:01:52 +00:00
|
|
|
int32_t min = fun->def->min_arity;
|
|
|
|
int32_t max = fun->def->max_arity;
|
|
|
|
Janet funv = janet_wrap_function(fun);
|
|
|
|
if (min == max && min != argc)
|
|
|
|
janet_panicf("arity mismatch in %v, expected %d, got %d", funv, min, argc);
|
|
|
|
if (min >= 0 && argc < min)
|
|
|
|
janet_panicf("arity mismatch in %v, expected at least %d, got %d", funv, min, argc);
|
|
|
|
janet_panicf("arity mismatch in %v, expected at most %d, got %d", funv, max, argc);
|
2019-02-01 16:56:25 +00:00
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_fiber_frame(janet_vm.fiber)->flags |= JANET_STACKFRAME_ENTRANCE;
|
2019-02-01 16:56:25 +00:00
|
|
|
|
|
|
|
/* Set up */
|
2021-07-17 01:59:03 +00:00
|
|
|
int32_t oldn = janet_vm.stackn++;
|
2019-01-17 23:10:04 +00:00
|
|
|
int handle = janet_gclock();
|
2019-01-12 22:31:15 +00:00
|
|
|
|
2019-02-01 16:56:25 +00:00
|
|
|
/* Run vm */
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.fiber->flags |= JANET_FIBER_RESUME_NO_USEVAL | JANET_FIBER_RESUME_NO_SKIP;
|
|
|
|
JanetSignal signal = run_vm(janet_vm.fiber, janet_wrap_nil());
|
2019-01-17 23:10:04 +00:00
|
|
|
|
2019-02-01 16:56:25 +00:00
|
|
|
/* Teardown */
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.stackn = oldn;
|
2019-01-12 22:31:15 +00:00
|
|
|
janet_gcunlock(handle);
|
2019-02-01 16:56:25 +00:00
|
|
|
|
2020-02-10 01:04:34 +00:00
|
|
|
if (signal != JANET_SIGNAL_OK) {
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_panicv(*janet_vm.return_reg);
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
2019-02-01 16:56:25 +00:00
|
|
|
|
2021-07-17 01:59:03 +00:00
|
|
|
return *janet_vm.return_reg;
|
2019-01-12 22:31:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 03:48:49 +00:00
|
|
|
static JanetSignal janet_check_can_resume(JanetFiber *fiber, Janet *out, int is_cancel) {
|
2019-01-05 01:08:43 +00:00
|
|
|
/* Check conditions */
|
2019-01-08 18:42:29 +00:00
|
|
|
JanetFiberStatus old_status = janet_fiber_status(fiber);
|
2021-07-17 01:59:03 +00:00
|
|
|
if (janet_vm.stackn >= JANET_RECURSION_GUARD) {
|
2019-01-05 01:08:43 +00:00
|
|
|
janet_fiber_set_status(fiber, JANET_STATUS_ERROR);
|
|
|
|
*out = janet_cstringv("C stack recursed too deeply");
|
|
|
|
return JANET_SIGNAL_ERROR;
|
|
|
|
}
|
2022-02-10 23:40:08 +00:00
|
|
|
/* If a "task" fiber is trying to be used as a normal fiber, detect that. See bug #920.
|
|
|
|
* Fibers must be marked as root fibers manually, or by the ev scheduler. */
|
|
|
|
if (janet_vm.fiber != NULL && (fiber->gc.flags & JANET_FIBER_FLAG_ROOT)) {
|
2022-02-10 04:36:07 +00:00
|
|
|
#ifdef JANET_EV
|
|
|
|
*out = janet_cstringv(is_cancel
|
|
|
|
? "cannot cancel root fiber, use ev/cancel"
|
|
|
|
: "cannot resume root fiber, use ev/go");
|
|
|
|
#else
|
2022-02-10 03:48:49 +00:00
|
|
|
*out = janet_cstringv(is_cancel
|
|
|
|
? "cannot cancel root fiber"
|
2022-02-10 04:36:07 +00:00
|
|
|
: "cannot resume root fiber");
|
2022-02-10 03:48:49 +00:00
|
|
|
#endif
|
|
|
|
return JANET_SIGNAL_ERROR;
|
|
|
|
}
|
2019-01-08 18:42:29 +00:00
|
|
|
if (old_status == JANET_STATUS_ALIVE ||
|
|
|
|
old_status == JANET_STATUS_DEAD ||
|
2020-02-23 19:31:27 +00:00
|
|
|
(old_status >= JANET_STATUS_USER0 && old_status <= JANET_STATUS_USER4) ||
|
2019-01-08 18:42:29 +00:00
|
|
|
old_status == JANET_STATUS_ERROR) {
|
2019-11-24 23:45:53 +00:00
|
|
|
const uint8_t *str = janet_formatc("cannot resume fiber with status :%s",
|
|
|
|
janet_status_names[old_status]);
|
|
|
|
*out = janet_wrap_string(str);
|
2019-01-05 01:08:43 +00:00
|
|
|
return JANET_SIGNAL_ERROR;
|
|
|
|
}
|
2020-04-10 23:29:10 +00:00
|
|
|
return JANET_SIGNAL_OK;
|
|
|
|
}
|
|
|
|
|
2020-09-19 23:47:47 +00:00
|
|
|
void janet_try_init(JanetTryState *state) {
|
2021-07-17 01:59:03 +00:00
|
|
|
state->stackn = janet_vm.stackn++;
|
|
|
|
state->gc_handle = janet_vm.gc_suspend;
|
|
|
|
state->vm_fiber = janet_vm.fiber;
|
|
|
|
state->vm_jmp_buf = janet_vm.signal_buf;
|
|
|
|
state->vm_return_reg = janet_vm.return_reg;
|
|
|
|
janet_vm.return_reg = &(state->payload);
|
|
|
|
janet_vm.signal_buf = &(state->buf);
|
2020-09-19 23:47:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void janet_restore(JanetTryState *state) {
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.stackn = state->stackn;
|
|
|
|
janet_vm.gc_suspend = state->gc_handle;
|
|
|
|
janet_vm.fiber = state->vm_fiber;
|
|
|
|
janet_vm.signal_buf = state->vm_jmp_buf;
|
|
|
|
janet_vm.return_reg = state->vm_return_reg;
|
2020-09-19 23:47:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 23:29:10 +00:00
|
|
|
static JanetSignal janet_continue_no_check(JanetFiber *fiber, Janet in, Janet *out) {
|
|
|
|
|
|
|
|
JanetFiberStatus old_status = janet_fiber_status(fiber);
|
2019-01-05 01:08:43 +00:00
|
|
|
|
2020-07-20 00:41:12 +00:00
|
|
|
#ifdef JANET_EV
|
2020-08-16 22:29:57 +00:00
|
|
|
janet_fiber_did_resume(fiber);
|
2020-07-20 00:41:12 +00:00
|
|
|
#endif
|
|
|
|
|
2021-01-03 22:17:36 +00:00
|
|
|
/* Clear last value */
|
|
|
|
fiber->last_value = janet_wrap_nil();
|
|
|
|
|
2019-01-05 02:15:37 +00:00
|
|
|
/* Continue child fiber if it exists */
|
|
|
|
if (fiber->child) {
|
2021-07-17 01:59:03 +00:00
|
|
|
if (janet_vm.root_fiber == NULL) janet_vm.root_fiber = fiber;
|
2019-01-05 02:15:37 +00:00
|
|
|
JanetFiber *child = fiber->child;
|
2021-01-03 22:17:36 +00:00
|
|
|
uint32_t instr = (janet_stack_frame(fiber->data + fiber->frame)->pc)[0];
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.stackn++;
|
2019-01-05 02:15:37 +00:00
|
|
|
JanetSignal sig = janet_continue(child, in, &in);
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.stackn--;
|
|
|
|
if (janet_vm.root_fiber == fiber) janet_vm.root_fiber = NULL;
|
2019-01-05 02:15:37 +00:00
|
|
|
if (sig != JANET_SIGNAL_OK && !(child->flags & (1 << sig))) {
|
|
|
|
*out = in;
|
2021-02-20 16:48:10 +00:00
|
|
|
janet_fiber_set_status(fiber, sig);
|
2019-01-05 02:15:37 +00:00
|
|
|
return sig;
|
|
|
|
}
|
2021-01-03 22:17:36 +00:00
|
|
|
/* Check if we need any special handling for certain opcodes */
|
|
|
|
switch (instr & 0x7F) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case JOP_NEXT: {
|
|
|
|
if (sig == JANET_SIGNAL_OK ||
|
|
|
|
sig == JANET_SIGNAL_ERROR ||
|
|
|
|
sig == JANET_SIGNAL_USER0 ||
|
|
|
|
sig == JANET_SIGNAL_USER1 ||
|
|
|
|
sig == JANET_SIGNAL_USER2 ||
|
|
|
|
sig == JANET_SIGNAL_USER3 ||
|
|
|
|
sig == JANET_SIGNAL_USER4) {
|
|
|
|
in = janet_wrap_nil();
|
|
|
|
} else {
|
|
|
|
in = janet_wrap_integer(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-01-05 02:15:37 +00:00
|
|
|
fiber->child = NULL;
|
|
|
|
}
|
|
|
|
|
2020-02-23 20:47:29 +00:00
|
|
|
/* Handle new fibers being resumed with a non-nil value */
|
|
|
|
if (old_status == JANET_STATUS_NEW && !janet_checktype(in, JANET_NIL)) {
|
|
|
|
Janet *stack = fiber->data + fiber->frame;
|
|
|
|
JanetFunction *func = janet_stack_frame(stack)->func;
|
|
|
|
if (func) {
|
|
|
|
if (func->def->arity > 0) {
|
|
|
|
stack[0] = in;
|
|
|
|
} else if (func->def->flags & JANET_FUNCDEF_FLAG_VARARG) {
|
|
|
|
stack[0] = janet_wrap_tuple(janet_tuple_n(&in, 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:31:15 +00:00
|
|
|
/* Save global state */
|
2020-09-19 23:47:47 +00:00
|
|
|
JanetTryState tstate;
|
2021-02-20 16:48:10 +00:00
|
|
|
JanetSignal sig = janet_try(&tstate);
|
|
|
|
if (!sig) {
|
2020-09-19 23:47:47 +00:00
|
|
|
/* Normal setup */
|
2021-07-17 01:59:03 +00:00
|
|
|
if (janet_vm.root_fiber == NULL) janet_vm.root_fiber = fiber;
|
|
|
|
janet_vm.fiber = fiber;
|
2020-09-19 23:47:47 +00:00
|
|
|
janet_fiber_set_status(fiber, JANET_STATUS_ALIVE);
|
2021-02-20 16:48:10 +00:00
|
|
|
sig = run_vm(fiber, in);
|
2019-01-05 04:20:34 +00:00
|
|
|
}
|
2017-03-10 05:09:42 +00:00
|
|
|
|
2020-09-19 23:47:47 +00:00
|
|
|
/* Restore */
|
2021-07-17 01:59:03 +00:00
|
|
|
if (janet_vm.root_fiber == fiber) janet_vm.root_fiber = NULL;
|
2021-02-20 16:48:10 +00:00
|
|
|
janet_fiber_set_status(fiber, sig);
|
2020-09-19 23:47:47 +00:00
|
|
|
janet_restore(&tstate);
|
2021-01-03 22:17:36 +00:00
|
|
|
fiber->last_value = tstate.payload;
|
2020-09-19 23:47:47 +00:00
|
|
|
*out = tstate.payload;
|
2019-01-12 22:31:15 +00:00
|
|
|
|
2021-02-20 16:48:10 +00:00
|
|
|
return sig;
|
2017-03-10 05:09:42 +00:00
|
|
|
}
|
2018-05-17 02:09:36 +00:00
|
|
|
|
2020-04-10 23:29:10 +00:00
|
|
|
/* Enter the main vm loop */
|
|
|
|
JanetSignal janet_continue(JanetFiber *fiber, Janet in, Janet *out) {
|
|
|
|
/* Check conditions */
|
2022-02-10 03:48:49 +00:00
|
|
|
JanetSignal tmp_signal = janet_check_can_resume(fiber, out, 0);
|
2020-04-10 23:29:10 +00:00
|
|
|
if (tmp_signal) return tmp_signal;
|
|
|
|
return janet_continue_no_check(fiber, in, out);
|
|
|
|
}
|
|
|
|
|
2020-08-11 12:19:34 +00:00
|
|
|
/* Enter the main vm loop but immediately raise a signal */
|
|
|
|
JanetSignal janet_continue_signal(JanetFiber *fiber, Janet in, Janet *out, JanetSignal sig) {
|
2022-02-10 03:48:49 +00:00
|
|
|
JanetSignal tmp_signal = janet_check_can_resume(fiber, out, sig != JANET_SIGNAL_OK);
|
2020-08-11 12:19:34 +00:00
|
|
|
if (tmp_signal) return tmp_signal;
|
|
|
|
if (sig != JANET_SIGNAL_OK) {
|
|
|
|
JanetFiber *child = fiber;
|
|
|
|
while (child->child) child = child->child;
|
|
|
|
child->gc.flags &= ~JANET_FIBER_STATUS_MASK;
|
|
|
|
child->gc.flags |= sig << JANET_FIBER_STATUS_OFFSET;
|
|
|
|
child->flags |= JANET_FIBER_RESUME_SIGNAL;
|
|
|
|
}
|
|
|
|
return janet_continue_no_check(fiber, in, out);
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:31:15 +00:00
|
|
|
JanetSignal janet_pcall(
|
2019-02-20 01:51:34 +00:00
|
|
|
JanetFunction *fun,
|
|
|
|
int32_t argc,
|
|
|
|
const Janet *argv,
|
|
|
|
Janet *out,
|
|
|
|
JanetFiber **f) {
|
2019-02-22 22:10:24 +00:00
|
|
|
JanetFiber *fiber;
|
|
|
|
if (f && *f) {
|
|
|
|
fiber = janet_fiber_reset(*f, fun, argc, argv);
|
|
|
|
} else {
|
|
|
|
fiber = janet_fiber(fun, 64, argc, argv);
|
|
|
|
}
|
2018-11-26 14:02:07 +00:00
|
|
|
if (f) *f = fiber;
|
2018-12-30 23:41:44 +00:00
|
|
|
if (!fiber) {
|
|
|
|
*out = janet_cstringv("arity mismatch");
|
|
|
|
return JANET_SIGNAL_ERROR;
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_continue(fiber, janet_wrap_nil(), out);
|
2018-03-04 23:56:13 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 20:36:21 +00:00
|
|
|
Janet janet_mcall(const char *name, int32_t argc, Janet *argv) {
|
|
|
|
/* At least 1 argument */
|
2021-10-07 07:38:40 +00:00
|
|
|
if (argc < 1) {
|
|
|
|
janet_panicf("method :%s expected at least 1 argument", name);
|
|
|
|
}
|
2019-09-08 20:36:21 +00:00
|
|
|
/* Find method */
|
2020-01-23 00:59:41 +00:00
|
|
|
Janet method = janet_method_lookup(argv[0], name);
|
|
|
|
if (janet_checktype(method, JANET_NIL)) {
|
2019-09-08 20:36:21 +00:00
|
|
|
janet_panicf("could not find method :%s for %v", name, argv[0]);
|
|
|
|
}
|
|
|
|
/* Invoke method */
|
2020-01-23 00:59:41 +00:00
|
|
|
return janet_method_invoke(method, argc, argv);
|
2019-09-08 20:36:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 00:31:28 +00:00
|
|
|
/* Setup VM */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_init(void) {
|
2021-07-17 01:59:03 +00:00
|
|
|
|
2017-02-12 20:16:55 +00:00
|
|
|
/* Garbage collection */
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.blocks = NULL;
|
|
|
|
janet_vm.next_collection = 0;
|
|
|
|
janet_vm.gc_interval = 0x400000;
|
|
|
|
janet_vm.block_count = 0;
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_symcache_init();
|
2021-07-17 01:59:03 +00:00
|
|
|
|
2018-09-23 21:53:55 +00:00
|
|
|
/* Initialize gc roots */
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.roots = NULL;
|
|
|
|
janet_vm.root_count = 0;
|
|
|
|
janet_vm.root_capacity = 0;
|
|
|
|
|
2019-06-02 03:31:39 +00:00
|
|
|
/* Scratch memory */
|
2021-08-27 16:46:42 +00:00
|
|
|
janet_vm.user = NULL;
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.scratch_mem = NULL;
|
|
|
|
janet_vm.scratch_len = 0;
|
|
|
|
janet_vm.scratch_cap = 0;
|
|
|
|
|
2018-06-12 18:24:45 +00:00
|
|
|
/* Initialize registry */
|
2021-07-30 02:29:08 +00:00
|
|
|
janet_vm.registry = NULL;
|
|
|
|
janet_vm.registry_cap = 0;
|
|
|
|
janet_vm.registry_count = 0;
|
|
|
|
janet_vm.registry_dirty = 0;
|
|
|
|
|
|
|
|
/* Intialize abstract registry */
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.abstract_registry = janet_table(0);
|
|
|
|
janet_gcroot(janet_wrap_table(janet_vm.abstract_registry));
|
|
|
|
|
2020-04-24 21:18:31 +00:00
|
|
|
/* Traversal */
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.traversal = NULL;
|
|
|
|
janet_vm.traversal_base = NULL;
|
|
|
|
janet_vm.traversal_top = NULL;
|
|
|
|
|
2019-12-02 10:15:22 +00:00
|
|
|
/* Core env */
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.core_env = NULL;
|
|
|
|
|
2021-07-24 20:14:37 +00:00
|
|
|
/* Auto suspension */
|
|
|
|
janet_vm.auto_suspend = 0;
|
|
|
|
|
2020-06-22 13:56:04 +00:00
|
|
|
/* Dynamic bindings */
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.top_dyns = NULL;
|
|
|
|
|
2019-11-08 23:35:27 +00:00
|
|
|
/* Seed RNG */
|
|
|
|
janet_rng_seed(janet_default_rng(), 0);
|
2021-07-17 01:59:03 +00:00
|
|
|
|
2020-02-10 01:04:34 +00:00
|
|
|
/* Fibers */
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.fiber = NULL;
|
|
|
|
janet_vm.root_fiber = NULL;
|
|
|
|
janet_vm.stackn = 0;
|
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
#ifdef JANET_EV
|
|
|
|
janet_ev_init();
|
|
|
|
#endif
|
|
|
|
#ifdef JANET_NET
|
|
|
|
janet_net_init();
|
2019-12-05 03:44:53 +00:00
|
|
|
#endif
|
2017-11-06 03:05:47 +00:00
|
|
|
return 0;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear all memory associated with the VM */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_deinit(void) {
|
|
|
|
janet_clear_memory();
|
|
|
|
janet_symcache_deinit();
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_free(janet_vm.roots);
|
|
|
|
janet_vm.roots = NULL;
|
|
|
|
janet_vm.root_count = 0;
|
|
|
|
janet_vm.root_capacity = 0;
|
|
|
|
janet_vm.abstract_registry = NULL;
|
|
|
|
janet_vm.core_env = NULL;
|
|
|
|
janet_vm.top_dyns = NULL;
|
2021-08-27 16:46:42 +00:00
|
|
|
janet_vm.user = NULL;
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_free(janet_vm.traversal_base);
|
|
|
|
janet_vm.fiber = NULL;
|
|
|
|
janet_vm.root_fiber = NULL;
|
2021-07-30 02:29:08 +00:00
|
|
|
janet_free(janet_vm.registry);
|
2021-08-15 18:14:33 +00:00
|
|
|
janet_vm.registry = NULL;
|
2020-05-28 15:39:40 +00:00
|
|
|
#ifdef JANET_EV
|
|
|
|
janet_ev_deinit();
|
|
|
|
#endif
|
2020-04-18 23:14:38 +00:00
|
|
|
#ifdef JANET_NET
|
|
|
|
janet_net_deinit();
|
|
|
|
#endif
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|