picrin/lib/proc.c

814 lines
21 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2017-05-05 23:53:20 -04:00
#include "value.h"
2017-03-28 10:09:40 -04:00
#include "object.h"
#include "state.h"
2017-04-14 10:40:07 -04:00
2017-04-22 20:16:40 -04:00
static struct irep *
assemble(pic_state *pic, pic_value as)
{
pic_value codes, reps, objs;
int argc, varg, frame_size, repc, objc, i;
struct irep **irep, *ir;
pic_value *obj, r, it;
code_t *code;
size_t ai = pic_enter(pic);
codes = pic_list_ref(pic, as, 0);
reps = pic_list_ref(pic, as, 1);
objs = pic_list_ref(pic, as, 2);
argc = pic_int(pic, pic_car(pic, pic_list_ref(pic, as, 3)));
varg = pic_bool(pic, pic_cdr(pic, pic_list_ref(pic, as, 3)));
frame_size = pic_int(pic, pic_list_ref(pic, as, 4));
repc = pic_length(pic, reps);
objc = pic_length(pic, objs);
assert(0 <= argc && argc < 256);
assert(0 <= frame_size && frame_size < 256);
assert(0 <= repc && repc < 256);
assert(0 <= objc && objc < 256);
irep = pic_malloc(pic, sizeof(*irep) * repc);
i = 0;
pic_for_each (r, reps, it) {
irep[i++] = assemble(pic, r);
}
obj = pic_malloc(pic, sizeof(*obj) * objc);
i = 0;
pic_for_each (r, objs, it) {
obj[i++] = r;
}
i = 0;
pic_for_each (r, codes, it) {
if (! pic_pair_p(pic, r))
continue;
if (pic_eq_p(pic, pic_car(pic, r), pic_intern_lit(pic, "COND"))) {
i += 4;
continue;
}
i += pic_length(pic, r);
}
code = pic_malloc(pic, i);
i = 0;
/* TODO: validate operands */
pic_for_each (r, codes, it) {
pic_value op;
if (! pic_pair_p(pic, r))
continue;
op = pic_car(pic, r);
if (pic_eq_p(pic, op, pic_intern_lit(pic, "HALT"))) {
code[i++] = OP_HALT;
}
else if (pic_eq_p(pic, op, pic_intern_lit(pic, "CALL"))) {
code[i++] = OP_CALL;
code[i++] = pic_int(pic, pic_list_ref(pic, r, 1));
}
else if (pic_eq_p(pic, op, pic_intern_lit(pic, "PROC"))) {
code[i++] = OP_PROC;
code[i++] = pic_int(pic, pic_list_ref(pic, r, 1));
code[i++] = pic_int(pic, pic_list_ref(pic, r, 2));
}
else if (pic_eq_p(pic, op, pic_intern_lit(pic, "LOAD"))) {
code[i++] = OP_LOAD;
code[i++] = pic_int(pic, pic_list_ref(pic, r, 1));
code[i++] = pic_int(pic, pic_list_ref(pic, r, 2));
}
else if (pic_eq_p(pic, op, pic_intern_lit(pic, "LREF"))) {
code[i++] = OP_LREF;
code[i++] = pic_int(pic, pic_list_ref(pic, r, 1));
code[i++] = pic_int(pic, pic_list_ref(pic, r, 2));
code[i++] = pic_int(pic, pic_list_ref(pic, r, 3));
}
else if (pic_eq_p(pic, op, pic_intern_lit(pic, "LSET"))) {
code[i++] = OP_LSET;
code[i++] = pic_int(pic, pic_list_ref(pic, r, 1));
code[i++] = pic_int(pic, pic_list_ref(pic, r, 2));
code[i++] = pic_int(pic, pic_list_ref(pic, r, 3));
}
else if (pic_eq_p(pic, op, pic_intern_lit(pic, "GREF"))) {
code[i++] = OP_GREF;
code[i++] = pic_int(pic, pic_list_ref(pic, r, 1));
code[i++] = pic_int(pic, pic_list_ref(pic, r, 2));
}
else if (pic_eq_p(pic, op, pic_intern_lit(pic, "GSET"))) {
code[i++] = OP_GSET;
code[i++] = pic_int(pic, pic_list_ref(pic, r, 1));
code[i++] = pic_int(pic, pic_list_ref(pic, r, 2));
}
else if (pic_eq_p(pic, op, pic_intern_lit(pic, "COND"))) {
pic_value label = pic_list_ref(pic, r, 2);
pic_value x, it2;
int offset = 0;
pic_for_each (x, it, it2) {
if (pic_eq_p(pic, x, label))
break;
if (! pic_pair_p(pic, x))
continue;
if (pic_eq_p(pic, pic_car(pic, x), pic_intern_lit(pic, "COND"))) {
offset += 4;
continue;
}
offset += pic_length(pic, x);
}
code[i++] = OP_COND;
code[i++] = pic_int(pic, pic_list_ref(pic, r, 1));
code[i++] = offset % 256;
code[i++] = offset / 256;
}
else if (pic_eq_p(pic, op, pic_intern_lit(pic, "LOADT"))) {
code[i++] = OP_LOADT;
code[i++] = pic_int(pic, pic_list_ref(pic, r, 1));
}
else if (pic_eq_p(pic, op, pic_intern_lit(pic, "LOADF"))) {
code[i++] = OP_LOADF;
code[i++] = pic_int(pic, pic_list_ref(pic, r, 1));
}
else if (pic_eq_p(pic, op, pic_intern_lit(pic, "LOADN"))) {
code[i++] = OP_LOADN;
code[i++] = pic_int(pic, pic_list_ref(pic, r, 1));
}
else if (pic_eq_p(pic, op, pic_intern_lit(pic, "LOADU"))) {
code[i++] = OP_LOADU;
code[i++] = pic_int(pic, pic_list_ref(pic, r, 1));
}
else if (pic_eq_p(pic, op, pic_intern_lit(pic, "LOADI"))) {
code[i++] = OP_LOADI;
code[i++] = pic_int(pic, pic_list_ref(pic, r, 1));
code[i++] = pic_int(pic, pic_list_ref(pic, r, 2));
}
}
ir = (struct irep *)pic_obj_alloc(pic, PIC_TYPE_IREP);
ir->argc = argc;
ir->flags = (varg ? IREP_VARG : 0);
ir->frame_size = frame_size;
ir->irepc = repc;
ir->objc = objc;
ir->irep = irep;
ir->obj = obj;
ir->code = code;
ir->codec = i;
pic_leave(pic, ai);
pic_protect(pic, obj_value(pic, ir));
return ir;
}
2017-04-14 10:40:07 -04:00
struct frame *
pic_make_frame_unsafe(pic_state *pic, int n)
{
struct frame *fp;
int i;
fp = (struct frame *)pic_obj_alloc_unsafe(pic, PIC_TYPE_FRAME);
fp->regs = n ? pic_malloc(pic, sizeof(pic_value) * n) : NULL;
fp->regc = n;
fp->up = NULL;
for (i = 0; i < n; ++i) {
fp->regs[i] = pic_invalid_value(pic);
}
return fp;
}
2016-02-06 10:09:40 -05:00
2017-03-28 11:03:23 -04:00
pic_value
pic_lambda(pic_state *pic, pic_func_t f, int n, ...)
{
pic_value proc;
va_list ap;
va_start(ap, n);
proc = pic_vlambda(pic, f, n, ap);
va_end(ap);
return proc;
}
pic_value
pic_vlambda(pic_state *pic, pic_func_t f, int n, va_list ap)
{
2017-04-14 10:40:07 -04:00
struct proc *proc;
2017-03-28 11:03:23 -04:00
int i;
2017-04-14 10:40:07 -04:00
assert(n >= 0);
proc = (struct proc *)pic_obj_alloc(pic, PIC_TYPE_PROC_FUNC);
proc->u.func = f;
proc->env = NULL;
if (n != 0) {
proc->env = pic_make_frame_unsafe(pic, n);
}
2017-03-28 11:03:23 -04:00
for (i = 0; i < n; ++i) {
2017-04-14 10:40:07 -04:00
proc->env->regs[i] = va_arg(ap, pic_value);
2017-03-28 11:03:23 -04:00
}
2017-04-14 10:40:07 -04:00
return obj_value(pic, proc);
2017-04-09 02:05:59 -04:00
}
pic_value
2017-04-14 10:40:07 -04:00
pic_make_proc_irep_unsafe(pic_state *pic, struct irep *irep, struct frame *fp)
2017-04-09 02:05:59 -04:00
{
struct proc *proc;
2017-04-14 10:40:07 -04:00
proc = (struct proc *)pic_obj_alloc_unsafe(pic, PIC_TYPE_PROC_IREP);
proc->u.irep = irep;
2017-04-14 10:40:07 -04:00
proc->env = fp;
2017-04-09 02:05:59 -04:00
return obj_value(pic, proc);
2017-03-28 11:03:23 -04:00
}
2016-02-06 10:09:40 -05:00
2016-02-22 14:03:42 -05:00
PIC_NORETURN static void
arg_error(pic_state *pic, int actual, bool varg, int expected)
{
const char *msg;
2017-05-09 11:49:15 -04:00
msg = pic_cstr(pic, pic_strf_value(pic, "wrong number of arguments (%d for %s%d)", actual, (varg ? "at least " : ""), expected), NULL);
2016-02-22 14:03:42 -05:00
pic_error(pic, msg, 0);
}
2017-04-14 10:40:07 -04:00
#define GET_ARGC(pic) (pic->cxt->pc[1])
#define GET_PROC(pic) (pic->cxt->fp->regs[0])
#define GET_CONT(pic) (pic->cxt->fp->regs[1])
#define GET_ARG(pic,n) (pic->cxt->fp->regs[(n)+2])
2016-02-06 10:09:40 -05:00
/**
* char type desc.
* ---- ---- ----
* o pic_value * object
* i int * int
* I int *, bool * int with exactness
* f double * float
* F double *, bool * float with exactness
* c char * char
* z char ** c string
2016-02-20 07:16:10 -05:00
* b unsigned char *, int * bytevector
* u void **, const pic_data_type * user data type
2016-02-20 01:31:14 -05:00
* m pic_value * symbol
2016-02-20 05:47:46 -05:00
* v pic_value * vector
* s pic_value * string
* l pic_value * lambda
* d pic_value * dictionary
* r pic_value * record
2016-02-06 10:09:40 -05:00
*
2016-02-20 07:16:10 -05:00
* + aliasing operator
2016-02-06 10:09:40 -05:00
* | optional operator
2016-02-20 03:27:13 -05:00
* * int *, pic_value ** variable length operator
* ---- ---- ----
2016-02-06 10:09:40 -05:00
*/
int
pic_get_args(pic_state *pic, const char *format, ...)
{
char c;
2016-02-20 07:16:10 -05:00
const char *p = format;
int paramc = 0, optc = 0;
2017-03-28 11:03:23 -04:00
bool proc = 0, rest = 0, opt = 0;
2017-04-14 10:40:07 -04:00
int i, argc = GET_ARGC(pic) - 1; /* one for continuation */
2016-02-06 10:09:40 -05:00
va_list ap;
/* parse format */
2016-02-20 07:16:10 -05:00
if ((c = *p) != '\0') {
if (c == '&') {
2016-02-20 07:16:10 -05:00
proc = 1;
p++;
2016-02-06 10:09:40 -05:00
}
2016-02-20 07:16:10 -05:00
while ((c = *p++) != '\0') {
if (c == '+')
continue;
if (c == '|') {
2016-02-20 07:16:10 -05:00
opt = 1; break;
} else if (c == '*') {
2016-02-20 07:16:10 -05:00
rest = 1; break;
}
2016-02-20 07:16:10 -05:00
paramc++;
2016-02-06 10:09:40 -05:00
}
2016-02-20 07:16:10 -05:00
if (opt) {
while ((c = *p++) != '\0') {
if (c == '+')
continue;
if (c == '*') {
rest = 1; break;
}
optc++;
}
2016-02-06 10:09:40 -05:00
}
2016-02-20 07:16:10 -05:00
if (rest) c = *p++;
assert(opt <= optc); /* at least 1 char after '|'? */
assert(c == '\0'); /* no extra chars? */
2016-02-06 10:09:40 -05:00
}
if (argc < paramc || (paramc + optc < argc && ! rest)) {
2016-02-22 14:03:42 -05:00
arg_error(pic, argc, rest, paramc);
2016-02-06 10:09:40 -05:00
}
va_start(ap, format);
/* dispatch */
if (proc) {
2016-02-19 10:03:16 -05:00
pic_value *proc;
2016-02-19 10:03:16 -05:00
proc = va_arg(ap, pic_value *);
2017-03-28 11:03:23 -04:00
*proc = GET_PROC(pic);
2016-02-20 07:16:10 -05:00
format++; /* skip '&' */
}
2017-03-28 18:11:27 -04:00
for (i = 0; i < argc && i < paramc + optc; ++i) {
2016-02-06 10:09:40 -05:00
c = *format++;
if (c == '|') {
c = *format++;
}
2016-02-06 10:09:40 -05:00
switch (c) {
case 'o': {
pic_value *p;
p = va_arg(ap, pic_value*);
2017-03-28 11:03:23 -04:00
*p = GET_ARG(pic, i);
2016-02-06 10:09:40 -05:00
break;
}
2016-02-20 07:16:10 -05:00
case 'u': {
void **data;
const pic_data_type *type;
pic_value v;
data = va_arg(ap, void **);
type = va_arg(ap, const pic_data_type *);
2017-03-28 11:03:23 -04:00
v = GET_ARG(pic, i);
2016-02-20 07:16:10 -05:00
if (pic_data_p(pic, v, type)) {
*data = pic_data(pic, v);
}
else {
2016-02-22 14:03:42 -05:00
const char *msg;
2017-05-09 11:49:15 -04:00
msg = pic_cstr(pic, pic_strf_value(pic, "pic_get_args: data type \"%s\" required", type->type_name), NULL);
2016-02-22 14:03:42 -05:00
pic_error(pic, msg, 1, v);
2016-02-20 07:16:10 -05:00
}
break;
}
case 'b': {
unsigned char **buf;
int *len;
pic_value v;
buf = va_arg(ap, unsigned char **);
len = va_arg(ap, int *);
2017-03-28 11:03:23 -04:00
v = GET_ARG(pic, i);
2016-02-20 07:16:10 -05:00
if (pic_blob_p(pic, v)) {
unsigned char *tmp = pic_blob(pic, v, len);
if (buf) *buf = tmp;
}
else {
2016-02-22 14:03:42 -05:00
pic_error(pic, "pic_get_args: bytevector required", 1, v);
2016-02-20 07:16:10 -05:00
}
break;
}
2016-02-06 10:09:40 -05:00
#define NUM_CASE(c1, c2, ctype) \
case c1: case c2: { \
ctype *n; \
bool *e, dummy; \
pic_value v; \
\
n = va_arg(ap, ctype *); \
e = (c == c2 ? va_arg(ap, bool *) : &dummy); \
\
2017-03-28 11:03:23 -04:00
v = GET_ARG(pic, i); \
switch (pic_type(pic, v)) { \
2016-02-22 14:03:42 -05:00
case PIC_TYPE_FLOAT: \
*n = pic_float(pic, v); \
2016-02-06 10:09:40 -05:00
*e = false; \
break; \
2016-02-22 14:03:42 -05:00
case PIC_TYPE_INT: \
*n = pic_int(pic, v); \
2016-02-06 10:09:40 -05:00
*e = true; \
break; \
default: \
2016-02-22 14:03:42 -05:00
pic_error(pic, "pic_get_args: float or int required", 1, v); \
2016-02-06 10:09:40 -05:00
} \
break; \
}
NUM_CASE('i', 'I', int)
NUM_CASE('f', 'F', double)
#define VAL_CASE(c, type, ctype, conv) \
case c: { \
ctype *ptr; \
pic_value v; \
\
ptr = va_arg(ap, ctype *); \
2017-03-28 11:03:23 -04:00
v = GET_ARG(pic, i); \
if (pic_## type ##_p(pic, v)) { \
2016-02-06 10:09:40 -05:00
*ptr = conv; \
} \
else { \
2016-02-22 14:03:42 -05:00
pic_error(pic, "pic_get_args: " #type " required", 1, v); \
2016-02-06 10:09:40 -05:00
} \
break; \
}
VAL_CASE('c', char, char, pic_char(pic, v))
2017-05-09 11:49:15 -04:00
VAL_CASE('z', str, const char *, pic_cstr(pic, v, NULL))
2016-02-06 10:09:40 -05:00
2016-02-19 05:08:45 -05:00
#define OBJ_CASE(c, type) VAL_CASE(c, type, pic_value, v)
2016-02-20 01:31:14 -05:00
OBJ_CASE('m', sym)
2016-02-19 13:26:52 -05:00
OBJ_CASE('s', str)
2016-02-19 10:03:16 -05:00
OBJ_CASE('l', proc)
2016-02-19 07:56:45 -05:00
OBJ_CASE('v', vec)
2016-02-19 05:08:45 -05:00
OBJ_CASE('d', dict)
2016-02-20 03:27:13 -05:00
OBJ_CASE('r', rec)
2016-02-19 05:08:45 -05:00
2016-02-06 10:09:40 -05:00
default:
2016-02-22 14:03:42 -05:00
pic_error(pic, "pic_get_args: invalid argument specifier given", 1, pic_char_value(pic, c));
2016-02-06 10:09:40 -05:00
}
2016-02-20 07:16:10 -05:00
2016-03-03 05:55:27 -05:00
if (*format == '+') {
2016-02-20 07:16:10 -05:00
pic_value *p;
2016-03-03 05:55:27 -05:00
format++;
2017-03-28 11:03:23 -04:00
p = va_arg(ap, pic_value *);
*p = GET_ARG(pic, i);
2016-02-20 07:16:10 -05:00
}
2016-02-06 10:09:40 -05:00
}
if (rest) {
int *n;
pic_value **argv;
2016-02-06 10:09:40 -05:00
n = va_arg(ap, int *);
argv = va_arg(ap, pic_value **);
2017-03-28 11:03:23 -04:00
*n = argc - i;
*argv = &GET_ARG(pic, i);
2016-02-06 10:09:40 -05:00
}
2016-02-06 10:09:40 -05:00
va_end(ap);
2016-02-06 10:09:40 -05:00
return argc;
}
2017-03-28 11:03:23 -04:00
pic_value
pic_closure_ref(pic_state *pic, int n)
2016-02-06 10:09:40 -05:00
{
2017-04-14 10:40:07 -04:00
struct frame *fp = pic->cxt->fp->up;
assert(n >= 0);
2017-04-14 10:40:07 -04:00
if (fp == NULL || fp->regc <= n) {
2017-03-28 11:03:23 -04:00
pic_error(pic, "pic_closure_ref: index out of range", 1, pic_int_value(pic, n));
2016-02-06 10:09:40 -05:00
}
2017-04-14 10:40:07 -04:00
return fp->regs[n];
2016-02-06 10:09:40 -05:00
}
2017-03-28 11:03:23 -04:00
void
pic_closure_set(pic_state *pic, int n, pic_value v)
2016-02-06 10:09:40 -05:00
{
2017-04-14 10:40:07 -04:00
struct frame *fp = pic->cxt->fp->up;
assert(n >= 0);
2017-04-14 10:40:07 -04:00
if (fp == NULL || fp->regc <= n) {
2017-03-28 11:03:23 -04:00
pic_error(pic, "pic_closure_ref: index out of range", 1, pic_int_value(pic, n));
2016-02-20 03:27:13 -05:00
}
2017-04-14 10:40:07 -04:00
fp->regs[n] = v;
2016-02-06 10:09:40 -05:00
}
2017-04-09 02:05:59 -04:00
pic_value
pic_call(pic_state *pic, pic_value proc, int n, ...)
{
pic_value r;
va_list ap;
va_start(ap, n);
r = pic_vcall(pic, proc, n, ap);
va_end(ap);
return r;
}
2017-04-25 13:10:40 -04:00
pic_value
pic_callk(pic_state *pic, pic_value proc, int n, ...)
{
va_list ap;
va_start(ap, n);
pic_vcallk(pic, proc, n, ap);
va_end(ap);
return pic_invalid_value(pic);
}
2017-04-09 02:05:59 -04:00
pic_value
pic_vcall(pic_state *pic, pic_value proc, int n, va_list ap)
{
2017-04-25 13:10:40 -04:00
struct context cxt;
CONTEXT_VINITK(pic, &cxt, proc, pic->halt, n, ap);
2017-04-29 10:48:32 -04:00
cxt.reset = 0;
2017-04-25 13:10:40 -04:00
pic_vm(pic, &cxt);
return pic_protect(pic, cxt.fp->regs[1]);
}
2017-04-09 02:05:59 -04:00
2017-04-25 13:10:40 -04:00
pic_value
pic_vcallk(pic_state *pic, pic_value proc, int n, va_list ap)
{
CONTEXT_VINITK(pic, pic->cxt, proc, GET_CONT(pic), n, ap);
return pic_invalid_value(pic);
2017-04-09 02:05:59 -04:00
}
2016-02-06 10:09:40 -05:00
pic_value
2016-02-19 10:03:16 -05:00
pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv)
2016-02-06 10:09:40 -05:00
{
2017-04-14 10:40:07 -04:00
struct context cxt;
2017-04-25 13:10:40 -04:00
CONTEXT_INITK(pic, &cxt, proc, pic->halt, argc, argv);
2017-04-29 10:48:32 -04:00
cxt.reset = 0;
2017-04-25 13:10:40 -04:00
pic_vm(pic, &cxt);
return pic_protect(pic, cxt.fp->regs[1]);
}
2017-04-09 02:05:59 -04:00
2017-04-25 13:10:40 -04:00
pic_value
pic_applyk(pic_state *pic, pic_value proc, int argc, pic_value *argv)
{
CONTEXT_INITK(pic, pic->cxt, proc, GET_CONT(pic), argc, argv);
return pic_invalid_value(pic);
}
2017-04-09 02:05:59 -04:00
2017-04-25 13:10:40 -04:00
pic_value
pic_values(pic_state *pic, int n, ...)
{
va_list ap;
pic_value ret;
va_start(ap, n);
ret = pic_vvalues(pic, n, ap);
va_end(ap);
return ret;
}
pic_value
pic_vvalues(pic_state *pic, int n, va_list ap)
{
if (n == 1) {
return va_arg(ap, pic_value);
2017-04-14 10:40:07 -04:00
}
2017-04-25 13:10:40 -04:00
CONTEXT_VINIT(pic, pic->cxt, GET_CONT(pic), n, ap);
return pic_invalid_value(pic);
}
void
pic_vm(pic_state *pic, struct context *cxt)
{
assert(cxt->fp == NULL);
assert(cxt->irep == NULL);
2017-04-14 10:40:07 -04:00
2017-04-25 13:30:17 -04:00
cxt->conts = pic_nil_value(pic);
2017-04-25 13:10:40 -04:00
cxt->prev = pic->cxt;
pic->cxt = cxt;
2017-04-15 16:20:55 -04:00
2017-04-25 13:10:40 -04:00
if (PIC_SETJMP(cxt->jmp) == 0) {
cxt->ai = pic->ai;
2017-04-09 02:05:59 -04:00
}
2017-04-25 13:10:40 -04:00
#define SAVE (pic->ai = cxt->ai)
#define A (cxt->pc[1])
#define B (cxt->pc[2])
#define C (cxt->pc[3])
2017-04-14 10:40:07 -04:00
#define Bx ((C << 8) + B)
2017-04-25 13:10:40 -04:00
#define REG(i) (cxt->sp->regs[i])
2017-04-09 02:05:59 -04:00
#if PIC_DIRECT_THREADED_VM
# define VM_LOOP JUMP;
# define CASE(x) L_##x:
2017-04-25 13:10:40 -04:00
# define NEXT(n) (cxt->pc += n); JUMP;
# define JUMP goto *oplabels[*cxt->pc];
2017-04-09 02:05:59 -04:00
# define VM_LOOP_END
#else
2017-04-25 13:10:40 -04:00
# define VM_LOOP for (;;) { switch (*cxt->pc) {
2017-04-09 02:05:59 -04:00
# define CASE(x) case x:
2017-04-25 13:10:40 -04:00
# define NEXT(n) (cxt->pc += n); break
2017-04-09 02:05:59 -04:00
# define JUMP break
# define VM_LOOP_END } }
#endif
2016-02-06 10:09:40 -05:00
#if PIC_DIRECT_THREADED_VM
static const void *oplabels[] = {
2017-04-14 10:40:07 -04:00
[OP_HALT] = &&L_OP_HALT, [OP_CALL] = &&L_OP_CALL, [OP_PROC] = &&L_OP_PROC,
[OP_LOAD] = &&L_OP_LOAD, [OP_LREF] = &&L_OP_LREF, [OP_LSET] = &&L_OP_LSET,
[OP_GREF] = &&L_OP_GREF, [OP_GSET] = &&L_OP_GSET, [OP_COND] = &&L_OP_COND,
[OP_LOADT] = &&L_OP_LOADT, [OP_LOADF] = &&L_OP_LOADF, [OP_LOADN] = &&L_OP_LOADN,
[OP_LOADU] = &&L_OP_LOADU, [OP_LOADI] = &&L_OP_LOADI
2016-02-06 10:09:40 -05:00
};
#endif
VM_LOOP {
2017-04-14 10:40:07 -04:00
CASE(OP_HALT) {
2017-04-25 13:30:17 -04:00
pic_value c, it;
pic_for_each (c, pic->cxt->conts, it) {
proc_ptr(pic, c)->env->regs[0] = pic_false_value(pic);
}
2017-04-14 10:40:07 -04:00
pic->cxt = pic->cxt->prev;
2017-04-25 13:10:40 -04:00
return;
2016-02-06 10:09:40 -05:00
}
CASE(OP_CALL) {
2016-02-21 06:32:00 -05:00
struct proc *proc;
2017-04-14 10:40:07 -04:00
if (! pic_proc_p(pic, REG(0))) {
pic_error(pic, "invalid application", 1, REG(0));
2016-02-06 10:09:40 -05:00
}
2017-04-14 10:40:07 -04:00
proc = proc_ptr(pic, REG(0));
2017-03-30 10:29:08 -04:00
if (proc->tt == PIC_TYPE_PROC_FUNC) {
2017-04-14 10:40:07 -04:00
pic_value v;
2017-04-25 13:10:40 -04:00
cxt->sp->up = proc->env; /* push static link */
cxt->fp = cxt->sp;
cxt->sp = NULL;
cxt->irep = NULL;
v = proc->u.func(pic);
2017-04-25 13:10:40 -04:00
if (cxt->sp != NULL) { /* tail call */
2017-04-14 10:40:07 -04:00
SAVE;
JUMP;
} else {
2017-04-25 13:10:40 -04:00
cxt->sp = pic_make_frame_unsafe(pic, 3);
cxt->sp->regs[0] = cxt->fp->regs[1]; /* cont. */
cxt->sp->regs[1] = v;
cxt->pc = MKCALL(cxt, 1);
2017-04-14 10:40:07 -04:00
SAVE;
JUMP;
}
} else {
struct irep *irep = proc->u.irep;
2016-02-06 10:09:40 -05:00
2017-04-14 10:40:07 -04:00
if (A != irep->argc) {
if (! ((irep->flags & IREP_VARG) != 0 && A >= irep->argc)) {
arg_error(pic, A, (irep->flags & IREP_VARG), irep->argc);
}
}
if (irep->flags & IREP_VARG) {
REG(irep->argc + 1) = pic_make_list(pic, A - irep->argc, &REG(irep->argc + 1));
SAVE; /* TODO: get rid of this */
}
2016-02-06 10:09:40 -05:00
2017-04-25 13:10:40 -04:00
cxt->sp->up = proc->env; /* push static link */
cxt->fp = cxt->sp;
cxt->sp = pic_make_frame_unsafe(pic, irep->frame_size);
cxt->pc = irep->code;
cxt->irep = irep;
2017-04-14 10:40:07 -04:00
JUMP;
2016-02-06 10:09:40 -05:00
}
}
2017-04-14 10:40:07 -04:00
CASE(OP_LREF) {
struct frame *f;
int depth = B;
2017-04-25 13:10:40 -04:00
for (f = cxt->fp; depth--; f = f->up);
2017-04-14 10:40:07 -04:00
REG(A) = f->regs[C];
NEXT(4);
2016-02-06 10:09:40 -05:00
}
2017-04-14 10:40:07 -04:00
CASE(OP_LSET) {
struct frame *f;
int depth = B;
2017-04-25 13:10:40 -04:00
for (f = cxt->fp; depth--; f = f->up);
2017-04-14 10:40:07 -04:00
f->regs[C] = REG(A);
NEXT(4);
2016-02-06 10:09:40 -05:00
}
2017-04-14 10:40:07 -04:00
CASE(OP_GREF) {
2017-04-25 13:10:40 -04:00
REG(A) = pic_global_ref(pic, cxt->irep->obj[B]);
2017-04-14 10:40:07 -04:00
NEXT(3);
2016-02-06 10:09:40 -05:00
}
2017-04-14 10:40:07 -04:00
CASE(OP_GSET) {
2017-04-25 13:10:40 -04:00
pic_global_set(pic, cxt->irep->obj[B], REG(A));
2017-04-14 10:40:07 -04:00
NEXT(3);
}
CASE(OP_COND) {
if (pic_false_p(pic, REG(A))) {
NEXT(Bx);
} else {
NEXT(4);
}
2016-02-06 10:09:40 -05:00
}
2017-04-14 10:40:07 -04:00
CASE(OP_PROC) {
2017-04-25 13:10:40 -04:00
REG(A) = pic_make_proc_irep_unsafe(pic, cxt->irep->irep[B], cxt->fp);
2017-04-14 10:40:07 -04:00
NEXT(3);
2016-02-06 10:09:40 -05:00
}
2017-04-14 10:40:07 -04:00
CASE(OP_LOAD) {
2017-04-25 13:10:40 -04:00
REG(A) = cxt->irep->obj[B];
2017-04-14 10:40:07 -04:00
NEXT(3);
2016-02-06 10:09:40 -05:00
}
2017-04-14 10:40:07 -04:00
CASE(OP_LOADU) {
REG(A) = pic_undef_value(pic);
NEXT(2);
2016-02-06 10:09:40 -05:00
}
2017-04-14 10:40:07 -04:00
CASE(OP_LOADT) {
REG(A) = pic_true_value(pic);
NEXT(2);
2016-02-06 10:09:40 -05:00
}
2017-04-14 10:40:07 -04:00
CASE(OP_LOADF) {
REG(A) = pic_false_value(pic);
NEXT(2);
2016-02-06 10:09:40 -05:00
}
2017-04-14 10:40:07 -04:00
CASE(OP_LOADN) {
REG(A) = pic_nil_value(pic);
NEXT(2);
2016-02-06 10:09:40 -05:00
}
2017-04-14 10:40:07 -04:00
CASE(OP_LOADI) {
REG(A) = pic_int_value(pic, (signed char) B);
NEXT(3);
2016-02-06 10:09:40 -05:00
}
2017-04-14 10:40:07 -04:00
} VM_LOOP_END
2016-02-06 10:09:40 -05:00
}
2017-04-22 22:56:41 -04:00
static pic_value
pic_proc_make_procedure(pic_state *pic)
{
pic_value as;
struct irep *irep;
struct proc *proc;
pic_get_args(pic, "o", &as);
irep = assemble(pic, as);
proc = (struct proc *)pic_obj_alloc(pic, PIC_TYPE_PROC_IREP);
proc->u.irep = irep;
proc->env = NULL;
return obj_value(pic, proc);
}
2014-08-25 00:38:09 -04:00
static pic_value
pic_proc_proc_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
return pic_bool_value(pic, pic_proc_p(pic, v));
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_proc_apply(pic_state *pic)
{
2016-02-19 10:03:16 -05:00
pic_value proc, *args, *arg_list;
2016-02-14 03:14:33 -05:00
int argc, n, i;
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "l*", &proc, &argc, &args);
if (argc == 0) {
2016-02-22 14:03:42 -05:00
pic_error(pic, "apply: wrong number of arguments", 0);
2014-08-25 00:38:09 -04:00
}
2016-02-14 03:14:33 -05:00
n = argc - 1 + pic_length(pic, args[argc - 1]);
2014-08-25 00:38:09 -04:00
2016-02-14 03:14:33 -05:00
arg_list = pic_alloca(pic, sizeof(pic_value) * n);
for (i = 0; i < argc - 1; ++i) {
arg_list[i] = args[i];
}
while (i < n) {
arg_list[i] = pic_list_ref(pic, args[argc - 1], i - argc + 1);
i++;
}
return pic_applyk(pic, proc, n, arg_list);
2014-08-25 00:38:09 -04:00
}
2017-04-25 01:08:37 -04:00
static pic_value
pic_proc_values(pic_state *pic)
{
int argc;
pic_value *argv;
pic_get_args(pic, "*", &argc, &argv);
if (argc == 1) {
return argv[0];
}
2017-04-25 13:10:40 -04:00
CONTEXT_INIT(pic, pic->cxt, GET_CONT(pic), argc, argv);
return pic_invalid_value(pic);
2017-04-25 01:08:37 -04:00
}
static pic_value
receive_call(pic_state *pic)
{
int argc = pic->cxt->pc[1];
2017-04-25 13:10:40 -04:00
pic_value *args = &pic->cxt->fp->regs[1];
2017-04-25 01:08:37 -04:00
/* receive_call is an inhabitant in the continuation side.
You can not use pic_get_args since it implicitly consumes the first argument. */
2017-04-25 13:10:40 -04:00
CONTEXT_INITK(pic, pic->cxt, pic_closure_ref(pic, 0), pic_closure_ref(pic, 1), argc, args);
2017-04-25 01:08:37 -04:00
2017-04-25 13:10:40 -04:00
return pic_invalid_value(pic);
2017-04-25 01:08:37 -04:00
}
static pic_value
pic_proc_call_with_values(pic_state *pic)
{
2017-04-25 13:10:40 -04:00
pic_value producer, consumer;
2017-04-25 01:08:37 -04:00
pic_get_args(pic, "ll", &producer, &consumer);
2017-04-25 13:10:40 -04:00
CONTEXT_INITK(pic, pic->cxt, producer, pic_lambda(pic, receive_call, 2, consumer, GET_CONT(pic)), 0, (pic_value *) NULL);
2017-04-25 01:08:37 -04:00
2017-04-25 13:10:40 -04:00
return pic_invalid_value(pic);
2017-04-25 01:08:37 -04:00
}
2014-08-25 00:38:09 -04:00
void
pic_init_proc(pic_state *pic)
{
2017-04-22 22:56:41 -04:00
pic_defun(pic, "make-procedure", pic_proc_make_procedure);
2014-08-25 00:38:09 -04:00
pic_defun(pic, "procedure?", pic_proc_proc_p);
pic_defun(pic, "apply", pic_proc_apply);
2017-04-25 01:08:37 -04:00
pic_defun(pic, "values", pic_proc_values);
pic_defun(pic, "call-with-values", pic_proc_call_with_values);
2014-08-25 00:38:09 -04:00
}