picrin/lib/state.h

125 lines
3.9 KiB
C
Raw Normal View History

2016-02-14 10:50:02 -05:00
/**
* See Copyright Notice in picrin.h
*/
#ifndef PICRIN_STATE_H
#define PICRIN_STATE_H
#if defined(__cplusplus)
extern "C" {
#endif
2017-03-28 10:09:40 -04:00
#include "khash.h"
2017-04-14 10:40:07 -04:00
#include "object.h"
2016-02-14 10:50:02 -05:00
2017-04-14 10:40:07 -04:00
KHASH_DECLARE(oblist, struct string *, struct symbol *)
struct context {
PIC_JMPBUF jmp;
2017-04-20 16:22:28 -04:00
size_t ai;
2017-04-14 10:40:07 -04:00
/* vm */
const code_t *pc;
struct frame *sp;
struct frame *fp;
2016-02-21 06:32:00 -05:00
struct irep *irep;
2016-02-14 10:50:02 -05:00
2017-04-14 10:40:07 -04:00
code_t tmpcode[2];
2017-04-25 13:30:17 -04:00
pic_value conts;
2017-04-29 10:48:32 -04:00
bool reset;
2017-04-14 10:40:07 -04:00
struct context *prev;
};
2016-02-14 10:50:02 -05:00
struct pic_state {
pic_allocf allocf;
void *userdata;
2017-04-14 10:40:07 -04:00
struct context *cxt, default_cxt;
2016-02-14 10:50:02 -05:00
2017-04-15 16:20:55 -04:00
size_t ai;
2017-03-31 01:39:01 -04:00
pic_value dyn_env;
2017-04-09 02:05:59 -04:00
pic_value features; /* list of symbols */
2016-02-14 22:59:58 -05:00
khash_t(oblist) oblist; /* string to symbol */
2017-04-02 09:19:11 -04:00
pic_value globals; /* dict */
2016-02-14 10:50:02 -05:00
bool gc_enable;
2016-02-21 06:32:00 -05:00
struct heap *heap;
struct object **arena;
2017-04-14 10:40:07 -04:00
size_t arena_size;
2016-02-14 10:50:02 -05:00
2017-04-14 10:40:07 -04:00
pic_value halt; /* top continuation */
2016-02-14 10:50:02 -05:00
2016-02-24 02:32:24 -05:00
pic_panicf panicf;
2016-02-14 10:50:02 -05:00
};
2017-03-28 10:09:40 -04:00
struct heap *pic_heap_open(pic_state *);
void pic_heap_close(pic_state *, struct heap *);
2017-03-28 11:03:23 -04:00
pic_value pic_global_ref(pic_state *pic, pic_value uid);
void pic_global_set(pic_state *pic, pic_value uid, pic_value value);
2017-04-25 13:10:40 -04:00
#define MKCALL(cxt,argc) \
2017-04-29 11:42:07 -04:00
((argc) < 256 \
? ((cxt)->tmpcode[0] = OP_CALL, (cxt)->tmpcode[1] = (argc), (cxt)->tmpcode) \
: (pic_error(pic, "too many arguments", 1, pic_int_value(pic, (argc))), NULL))
2017-04-25 13:10:40 -04:00
#define CONTEXT_VINITK(pic,cxt,proc,k,n,ap) do { \
int i; \
(cxt)->pc = MKCALL((cxt), (n) + 1); \
(cxt)->sp = pic_make_frame_unsafe(pic, (n) + 3); \
(cxt)->sp->regs[0] = (proc); \
(cxt)->sp->regs[1] = k; \
for (i = 0; i < (n); ++i) { \
(cxt)->sp->regs[i + 2] = va_arg(ap, pic_value); \
} \
(cxt)->fp = NULL; \
(cxt)->irep = NULL; \
} while (0)
#define CONTEXT_INITK(pic,cxt,proc,k,n,argv) do { \
int i; \
(cxt)->pc = MKCALL((cxt), (n) + 1); \
(cxt)->sp = pic_make_frame_unsafe(pic, (n) + 3); \
(cxt)->sp->regs[0] = (proc); \
(cxt)->sp->regs[1] = k; \
for (i = 0; i < (n); ++i) { \
(cxt)->sp->regs[i + 2] = (argv)[i]; \
} \
(cxt)->fp = NULL; \
(cxt)->irep = NULL; \
} while (0)
#define CONTEXT_VINIT(pic,cxt,proc,n,ap) do { \
int i; \
(cxt)->pc = MKCALL((cxt), (n)); \
(cxt)->sp = pic_make_frame_unsafe(pic, (n) + 2); \
(cxt)->sp->regs[0] = (proc); \
for (i = 0; i < (n); ++i) { \
(cxt)->sp->regs[i + 1] = va_arg(ap, pic_value); \
} \
(cxt)->fp = NULL; \
(cxt)->irep = NULL; \
} while (0)
#define CONTEXT_INIT(pic,cxt,proc,n,argv) do { \
int i; \
(cxt)->pc = MKCALL((cxt), (n)); \
(cxt)->sp = pic_make_frame_unsafe(pic, (n) + 2); \
(cxt)->sp->regs[0] = (proc); \
for (i = 0; i < (n); ++i) { \
(cxt)->sp->regs[i + 1] = (argv)[i]; \
} \
(cxt)->fp = NULL; \
(cxt)->irep = NULL; \
} while (0)
void pic_vm(pic_state *pic, struct context *cxt);
2016-02-14 10:50:02 -05:00
#if defined(__cplusplus)
}
#endif
#endif