2
#include "forkable_stack.h"
16
typedef union frame_elem {
17
FORKABLE_STACK_HEADER;
18
struct continuation cont;
19
struct closure closure;
25
* fr[0] - FORKABLE_STACK_HEADER (next pointer)
26
* fr[1] - self (used to store return addresses, etc)
27
* fr[2...nclosures+2] - closure params
28
* fr[nclosures+2..nclosures+nlocals+2] - local variables
31
static int frame_size(struct bytecode* bc) {
32
return sizeof(union frame_elem) * (bc->nclosures + bc->nlocals + 2);
35
static struct continuation* frame_self(frame_ptr fr) {
39
static struct closure* frame_closure_arg(frame_ptr fr, int closure) {
41
assert(closure < frame_self(fr)->bc->nclosures);
42
return &fr[2+closure].closure;
45
static jv* frame_local_var(frame_ptr fr, int var) {
47
assert(var < frame_self(fr)->bc->nlocals);
48
return &fr[2 + frame_self(fr)->bc->nclosures + var].jsonval;
52
static frame_ptr frame_current(struct forkable_stack* stk) {
53
frame_ptr fp = forkable_stack_peek(stk);
54
if (forkable_stack_peek_next(stk, fp)) {
55
struct bytecode* bc = frame_self(forkable_stack_peek_next(stk, fp))->bc;
56
assert(frame_self(fp)->retaddr >= bc->code && frame_self(fp)->retaddr < bc->code + bc->codelen);
58
assert(frame_self(fp)->retaddr == 0);
63
static struct bytecode* frame_current_bytecode(struct forkable_stack* stk) {
64
return frame_self(frame_current(stk))->bc;
66
static uint16_t** frame_current_retaddr(struct forkable_stack* stk) {
67
return &frame_self(frame_current(stk))->retaddr;
70
static frame_ptr frame_get_parent(struct forkable_stack* stk, frame_ptr fr) {
71
return forkable_stack_from_idx(stk, frame_self(fr)->env);
74
static frame_ptr frame_get_level(struct forkable_stack* stk, frame_ptr fr, int level) {
75
for (int i=0; i<level; i++) {
76
fr = frame_get_parent(stk, fr);
81
static frame_ptr frame_push(struct forkable_stack* stk, struct closure cl, uint16_t* retaddr) {
82
frame_ptr fp = forkable_stack_push(stk, frame_size(cl.bc));
83
struct continuation* cc = frame_self(fp);
86
cc->retaddr = retaddr;
87
for (int i=0; i<cl.bc->nlocals; i++) {
88
*frame_local_var(fp, i) = jv_invalid();
93
static void frame_pop(struct forkable_stack* stk) {
94
frame_ptr fp = frame_current(stk);
95
if (forkable_stack_pop_will_free(stk)) {
96
int nlocals = frame_self(fp)->bc->nlocals;
97
for (int i=0; i<nlocals; i++) {
98
jv_free(*frame_local_var(fp, i));
101
forkable_stack_pop(stk);