Unify all stacks. Passes tests, but needs cleanup.

This commit is contained in:
Stephen Dolan committed 2013-06-13 21:50:32 +01:00
1 parent 25bf1a0169
commit e0524644f8
4 files changed
+212 -277

No files matched your search

+87 -81
View File
@@ -8,7 +8,6 @@
#include "opcode.h" #include "opcode.h"
#include "bytecode.h" #include "bytecode.h"
#include "forkable_stack.h"
#include "frame_layout.h" #include "frame_layout.h"
#include "jv_alloc.h" #include "jv_alloc.h"
@@ -20,60 +19,63 @@
#include "builtin.h" #include "builtin.h"
struct jq_state { struct jq_state {
struct forkable_stack data_stk; struct stack stk;
stack_idx curr_frame; stack_ptr curr_frame;
struct forkable_stack fork_stk; stack_ptr stk_top;
stack_ptr fork_top;
jv path; jv path;
int subexp_nest; int subexp_nest;
int debug_trace_enabled; int debug_trace_enabled;
int initial_execution; int initial_execution;
}; };
typedef struct {
FORKABLE_STACK_HEADER;
jv val;
} data_stk_elem;
void stack_push(jq_state *jq, jv val) { void stack_push(jq_state *jq, jv val) {
assert(jv_is_valid(val)); assert(jv_is_valid(val));
data_stk_elem* s = forkable_stack_push(&jq->data_stk, sizeof(data_stk_elem)); jq->stk_top = stack_push_block(&jq->stk, jq->stk_top, sizeof(jv));
s->val = val; jv* sval = stack_block(&jq->stk, jq->stk_top);
*sval = val;
} }
jv stack_pop(jq_state *jq) { jv stack_pop(jq_state *jq) {
data_stk_elem* s = forkable_stack_peek(&jq->data_stk); jv* sval = stack_block(&jq->stk, jq->stk_top);
jv val = s->val; jv val = *sval;
if (!forkable_stack_pop_will_free(&jq->data_stk)) { if (!stack_pop_will_free(&jq->stk, jq->stk_top)) {
val = jv_copy(val); val = jv_copy(val);
} }
forkable_stack_pop(&jq->data_stk); jq->stk_top = stack_pop_block(&jq->stk, jq->stk_top, sizeof(jv));
assert(jv_is_valid(val)); assert(jv_is_valid(val));
return val; return val;
} }
struct forkpoint { struct forkpoint {
FORKABLE_STACK_HEADER; stack_ptr saved_data_stack;
struct forkable_stack_state saved_data_stack; stack_ptr saved_curr_frame;
int path_len, subexp_nest; int path_len, subexp_nest;
stack_idx saved_curr_frame;
uint16_t* return_address; uint16_t* return_address;
}; };
struct stack_pos {
stack_ptr saved_data_stack, saved_curr_frame;
};
void stack_save(jq_state *jq, uint16_t* retaddr){ struct stack_pos stack_get_pos(jq_state* jq) {
struct forkpoint* fork = forkable_stack_push(&jq->fork_stk, sizeof(struct forkpoint)); struct stack_pos sp = {jq->stk_top, jq->curr_frame};
forkable_stack_save(&jq->data_stk, &fork->saved_data_stack); return sp;
}
void stack_save(jq_state *jq, uint16_t* retaddr, struct stack_pos sp){
jq->fork_top = stack_push_block(&jq->stk, jq->fork_top, sizeof(struct forkpoint));
struct forkpoint* fork = stack_block(&jq->stk, jq->fork_top);
fork->saved_data_stack = jq->stk_top;
fork->saved_curr_frame = jq->curr_frame;
fork->path_len = fork->path_len =
jv_get_kind(jq->path) == JV_KIND_ARRAY ? jv_array_length(jv_copy(jq->path)) : 0; jv_get_kind(jq->path) == JV_KIND_ARRAY ? jv_array_length(jv_copy(jq->path)) : 0;
fork->subexp_nest = jq->subexp_nest; fork->subexp_nest = jq->subexp_nest;
fork->saved_curr_frame = jq->curr_frame;
fork->return_address = retaddr; fork->return_address = retaddr;
} jq->stk_top = sp.saved_data_stack;
jq->curr_frame = sp.saved_curr_frame;
void stack_switch(jq_state *jq) {
struct forkpoint* fork = forkable_stack_peek(&jq->fork_stk);
forkable_stack_switch(&jq->data_stk, &fork->saved_data_stack);
} }
void path_append(jq_state* jq, jv component) { void path_append(jq_state* jq, jv component) {
@@ -88,22 +90,23 @@ void path_append(jq_state* jq, jv component) {
} }
uint16_t* stack_restore(jq_state *jq){ uint16_t* stack_restore(jq_state *jq){
while (!forkable_stack_empty(&jq->data_stk) && while (stack_top(&jq->stk) != jq->fork_top) {
forkable_stack_pop_will_free(&jq->data_stk)) { if (stack_pop_will_free(&jq->stk, jq->stk_top)) {
if (forkable_stack_peek(&jq->data_stk) != forkable_stack_from_idx(&jq->data_stk, jq->curr_frame)) {
jv_free(stack_pop(jq)); jv_free(stack_pop(jq));
} else if (stack_pop_will_free(&jq->stk, jq->curr_frame)) {
jq->curr_frame = frame_pop(&jq->stk, jq->curr_frame);
} else { } else {
jq->curr_frame = frame_pop(&jq->data_stk, jq->curr_frame); assert(0);
} }
} }
if (forkable_stack_empty(&jq->fork_stk)) { if (jq->fork_top == 0) {
return 0; return 0;
} }
struct forkpoint* fork = forkable_stack_peek(&jq->fork_stk); struct forkpoint* fork = stack_block(&jq->stk, jq->fork_top);
uint16_t* retaddr = fork->return_address; uint16_t* retaddr = fork->return_address;
forkable_stack_restore(&jq->data_stk, &fork->saved_data_stack); jq->stk_top = fork->saved_data_stack;
jq->curr_frame = fork->saved_curr_frame; jq->curr_frame = fork->saved_curr_frame;
int path_len = fork->path_len; int path_len = fork->path_len;
if (jv_get_kind(jq->path) == JV_KIND_ARRAY) { if (jv_get_kind(jq->path) == JV_KIND_ARRAY) {
@@ -113,20 +116,21 @@ uint16_t* stack_restore(jq_state *jq){
assert(path_len == 0); assert(path_len == 0);
} }
jq->subexp_nest = fork->subexp_nest; jq->subexp_nest = fork->subexp_nest;
forkable_stack_pop(&jq->fork_stk); jq->fork_top = stack_pop_block(&jq->stk, jq->fork_top, sizeof(struct forkpoint));
return retaddr; return retaddr;
} }
static struct closure make_closure(struct forkable_stack* stk, frame_ptr fr, uint16_t* pc) { static struct closure make_closure(struct jq_state* jq, stack_ptr fridx, uint16_t* pc) {
uint16_t level = *pc++; uint16_t level = *pc++;
uint16_t idx = *pc++; uint16_t idx = *pc++;
fr = frame_get_level(stk, fr, level); fridx = frame_get_level(&jq->stk, fridx, level);
frame_ptr fr = frame_current(&jq->stk, fridx);
if (idx & ARG_NEWCLOSURE) { if (idx & ARG_NEWCLOSURE) {
int subfn_idx = idx & ~ARG_NEWCLOSURE; int subfn_idx = idx & ~ARG_NEWCLOSURE;
assert(subfn_idx < frame_self(fr)->bc->nsubfunctions); assert(subfn_idx < frame_self(fr)->bc->nsubfunctions);
struct closure cl = {frame_self(fr)->bc->subfunctions[subfn_idx], struct closure cl = {frame_self(fr)->bc->subfunctions[subfn_idx],
forkable_stack_to_idx(stk, fr)}; fridx};
return cl; return cl;
} else { } else {
return *frame_closure_arg(fr, idx); return *frame_closure_arg(fr, idx);
@@ -155,22 +159,22 @@ jv jq_next(jq_state *jq) {
uint16_t opcode = *pc; uint16_t opcode = *pc;
if (jq->debug_trace_enabled) { if (jq->debug_trace_enabled) {
dump_operation(frame_current_bytecode(&jq->data_stk, jq->curr_frame), pc); dump_operation(frame_current_bytecode(&jq->stk, jq->curr_frame), pc);
printf("\t"); printf("\t");
const struct opcode_description* opdesc = opcode_describe(opcode); const struct opcode_description* opdesc = opcode_describe(opcode);
data_stk_elem* param = 0; stack_ptr param = 0;
if (!backtracking) { if (!backtracking) {
int stack_in = opdesc->stack_in; int stack_in = opdesc->stack_in;
if (stack_in == -1) stack_in = pc[1]; if (stack_in == -1) stack_in = pc[1];
for (int i=0; i<stack_in; i++) { for (int i=0; i<stack_in; i++) {
if (i == 0) { if (i == 0) {
param = forkable_stack_peek(&jq->data_stk); param = jq->stk_top;
} else { } else {
printf(" | "); printf(" | ");
param = forkable_stack_peek_next(&jq->data_stk, param); param = *stack_block_next(&jq->stk, param);
} }
if (!param) break; if (!param) break;
jv_dump(jv_copy(param->val), 0); jv_dump(jv_copy(*(jv*)stack_block(&jq->stk, param)), 0);
//printf("<%d>", jv_get_refcnt(param->val)); //printf("<%d>", jv_get_refcnt(param->val));
//printf(" -- "); //printf(" -- ");
//jv_dump(jv_copy(jq->path), 0); //jv_dump(jv_copy(jq->path), 0);
@@ -181,6 +185,7 @@ jv jq_next(jq_state *jq) {
printf("\n"); printf("\n");
} }
if (backtracking) { if (backtracking) {
opcode = ON_BACKTRACK(opcode); opcode = ON_BACKTRACK(opcode);
backtracking = 0; backtracking = 0;
@@ -191,7 +196,7 @@ jv jq_next(jq_state *jq) {
default: assert(0 && "invalid instruction"); default: assert(0 && "invalid instruction");
case LOADK: { case LOADK: {
jv v = jv_array_get(jv_copy(frame_current_bytecode(&jq->data_stk, jq->curr_frame)->constants), *pc++); jv v = jv_array_get(jv_copy(frame_current_bytecode(&jq->stk, jq->curr_frame)->constants), *pc++);
assert(jv_is_valid(v)); assert(jv_is_valid(v));
jv_free(stack_pop(jq)); jv_free(stack_pop(jq));
stack_push(jq, v); stack_push(jq, v);
@@ -241,7 +246,7 @@ jv jq_next(jq_state *jq) {
jv v = stack_pop(jq); jv v = stack_pop(jq);
uint16_t level = *pc++; uint16_t level = *pc++;
uint16_t vidx = *pc++; uint16_t vidx = *pc++;
frame_ptr fp = frame_get_level(&jq->data_stk, frame_current(&jq->data_stk, jq->curr_frame), level); frame_ptr fp = frame_current(&jq->stk, frame_get_level(&jq->stk, jq->curr_frame, level));
jv* var = frame_local_var(fp, vidx); jv* var = frame_local_var(fp, vidx);
assert(jv_get_kind(*var) == JV_KIND_ARRAY); assert(jv_get_kind(*var) == JV_KIND_ARRAY);
*var = jv_array_append(*var, v); *var = jv_array_append(*var, v);
@@ -273,7 +278,7 @@ jv jq_next(jq_state *jq) {
case RANGE: { case RANGE: {
uint16_t level = *pc++; uint16_t level = *pc++;
uint16_t v = *pc++; uint16_t v = *pc++;
frame_ptr fp = frame_get_level(&jq->data_stk, frame_current(&jq->data_stk, jq->curr_frame), level); frame_ptr fp = frame_current(&jq->stk, frame_get_level(&jq->stk, jq->curr_frame, level));
jv* var = frame_local_var(fp, v); jv* var = frame_local_var(fp, v);
jv max = stack_pop(jq); jv max = stack_pop(jq);
if (jv_get_kind(*var) != JV_KIND_NUMBER || if (jv_get_kind(*var) != JV_KIND_NUMBER ||
@@ -288,9 +293,10 @@ jv jq_next(jq_state *jq) {
jv curr = jv_copy(*var); jv curr = jv_copy(*var);
*var = jv_number(jv_number_value(*var) + 1); *var = jv_number(jv_number_value(*var) + 1);
stack_save(jq, pc - 3); struct stack_pos spos = stack_get_pos(jq);
stack_push(jq, jv_copy(max)); stack_push(jq, jv_copy(max));
stack_switch(jq); stack_save(jq, pc - 3, spos);
stack_push(jq, curr); stack_push(jq, curr);
} }
break; break;
@@ -300,7 +306,7 @@ jv jq_next(jq_state *jq) {
case LOADV: { case LOADV: {
uint16_t level = *pc++; uint16_t level = *pc++;
uint16_t v = *pc++; uint16_t v = *pc++;
frame_ptr fp = frame_get_level(&jq->data_stk, frame_current(&jq->data_stk, jq->curr_frame), level); frame_ptr fp = frame_current(&jq->stk, frame_get_level(&jq->stk, jq->curr_frame, level));
jv* var = frame_local_var(fp, v); jv* var = frame_local_var(fp, v);
if (jq->debug_trace_enabled) { if (jq->debug_trace_enabled) {
printf("V%d = ", v); printf("V%d = ", v);
@@ -316,7 +322,7 @@ jv jq_next(jq_state *jq) {
case LOADVN: { case LOADVN: {
uint16_t level = *pc++; uint16_t level = *pc++;
uint16_t v = *pc++; uint16_t v = *pc++;
frame_ptr fp = frame_get_level(&jq->data_stk, frame_current(&jq->data_stk, jq->curr_frame), level); frame_ptr fp = frame_current(&jq->stk, frame_get_level(&jq->stk, jq->curr_frame, level));
jv* var = frame_local_var(fp, v); jv* var = frame_local_var(fp, v);
if (jq->debug_trace_enabled) { if (jq->debug_trace_enabled) {
printf("V%d = ", v); printf("V%d = ", v);
@@ -332,7 +338,7 @@ jv jq_next(jq_state *jq) {
case STOREV: { case STOREV: {
uint16_t level = *pc++; uint16_t level = *pc++;
uint16_t v = *pc++; uint16_t v = *pc++;
frame_ptr fp = frame_get_level(&jq->data_stk, frame_current(&jq->data_stk, jq->curr_frame), level); frame_ptr fp = frame_current(&jq->stk, frame_get_level(&jq->stk, jq->curr_frame, level));
jv* var = frame_local_var(fp, v); jv* var = frame_local_var(fp, v);
jv val = stack_pop(jq); jv val = stack_pop(jq);
if (jq->debug_trace_enabled) { if (jq->debug_trace_enabled) {
@@ -349,8 +355,7 @@ jv jq_next(jq_state *jq) {
jv v = stack_pop(jq); jv v = stack_pop(jq);
stack_push(jq, jq->path); stack_push(jq, jq->path);
stack_save(jq, pc - 1); stack_save(jq, pc - 1, stack_get_pos(jq));
stack_switch(jq);
stack_push(jq, jv_number(jq->subexp_nest)); stack_push(jq, jv_number(jq->subexp_nest));
stack_push(jq, v); stack_push(jq, v);
@@ -369,9 +374,9 @@ jv jq_next(jq_state *jq) {
jv path = jq->path; jv path = jq->path;
jq->path = stack_pop(jq); jq->path = stack_pop(jq);
stack_save(jq, pc - 1); struct stack_pos spos = stack_get_pos(jq);
stack_push(jq, jv_copy(path)); stack_push(jq, jv_copy(path));
stack_switch(jq); stack_save(jq, pc - 1, spos);
stack_push(jq, path); stack_push(jq, path);
jq->subexp_nest = old_subexp_nest; jq->subexp_nest = old_subexp_nest;
@@ -460,10 +465,10 @@ jv jq_next(jq_state *jq) {
path_append(jq, key); path_append(jq, key);
stack_push(jq, value); stack_push(jq, value);
} else { } else {
stack_save(jq, pc - 1); struct stack_pos spos = stack_get_pos(jq);
stack_push(jq, container); stack_push(jq, container);
stack_push(jq, jv_number(idx)); stack_push(jq, jv_number(idx));
stack_switch(jq); stack_save(jq, pc - 1, spos);
path_append(jq, key); path_append(jq, key);
stack_push(jq, value); stack_push(jq, value);
} }
@@ -481,8 +486,7 @@ jv jq_next(jq_state *jq) {
} }
case FORK: { case FORK: {
stack_save(jq, pc - 1); stack_save(jq, pc - 1, stack_get_pos(jq));
stack_switch(jq);
pc++; // skip offset this time pc++; // skip offset this time
break; break;
} }
@@ -500,7 +504,7 @@ jv jq_next(jq_state *jq) {
for (int i = 1; i < nargs; i++) { for (int i = 1; i < nargs; i++) {
cfunc_input[i] = stack_pop(jq); cfunc_input[i] = stack_pop(jq);
} }
struct cfunction* func = &frame_current_bytecode(&jq->data_stk, jq->curr_frame)->globals->cfunctions[*pc++]; struct cfunction* func = &frame_current_bytecode(&jq->stk, jq->curr_frame)->globals->cfunctions[*pc++];
top = cfunction_invoke(func, cfunc_input); top = cfunction_invoke(func, cfunc_input);
if (jv_is_valid(top)) { if (jv_is_valid(top)) {
stack_push(jq, top); stack_push(jq, top);
@@ -515,36 +519,37 @@ jv jq_next(jq_state *jq) {
jv input = stack_pop(jq); jv input = stack_pop(jq);
uint16_t nclosures = *pc++; uint16_t nclosures = *pc++;
uint16_t* retaddr = pc + 2 + nclosures*2; uint16_t* retaddr = pc + 2 + nclosures*2;
frame_ptr new_frame = frame_push(&jq->data_stk, jq->curr_frame, stack_ptr old_frame = jq->curr_frame;
make_closure(&jq->data_stk, frame_current(&jq->data_stk, jq->curr_frame), pc), jq->curr_frame = frame_push(&jq->stk, jq->curr_frame,
retaddr); make_closure(jq, jq->curr_frame, pc),
retaddr, jq->stk_top);
pc += 2; pc += 2;
frame_ptr old_frame = frame_current(&jq->data_stk, jq->curr_frame);
jq->curr_frame = forkable_stack_to_idx(&jq->data_stk, forkable_stack_peek(&jq->data_stk));
frame_ptr new_frame = frame_current(&jq->stk, jq->curr_frame);
assert(nclosures == frame_self(new_frame)->bc->nclosures); assert(nclosures == frame_self(new_frame)->bc->nclosures);
for (int i=0; i<nclosures; i++) { for (int i=0; i<nclosures; i++) {
*frame_closure_arg(new_frame, i) = make_closure(&jq->data_stk, old_frame, pc); *frame_closure_arg(new_frame, i) = make_closure(jq, old_frame, pc);
pc += 2; pc += 2;
} }
pc = frame_current_bytecode(&jq->data_stk, jq->curr_frame)->code; pc = frame_current_bytecode(&jq->stk, jq->curr_frame)->code;
stack_push(jq, input); stack_push(jq, input);
break; break;
} }
case RET: { case RET: {
jv value = stack_pop(jq); jv value = stack_pop(jq);
uint16_t* retaddr = *frame_current_retaddr(&jq->data_stk, jq->curr_frame); assert(jq->stk_top == frame_self(frame_current(&jq->stk, jq->curr_frame))->retdata);
uint16_t* retaddr = *frame_current_retaddr(&jq->stk, jq->curr_frame);
if (retaddr) { if (retaddr) {
// function return // function return
pc = retaddr; pc = retaddr;
jq->curr_frame = frame_pop(&jq->data_stk, jq->curr_frame); jq->curr_frame = frame_pop(&jq->stk, jq->curr_frame);
} else { } else {
// top-level return, yielding value // top-level return, yielding value
stack_save(jq, pc - 1); struct stack_pos spos = stack_get_pos(jq);
stack_push(jq, jv_null()); stack_push(jq, jv_null());
stack_switch(jq); stack_save(jq, pc - 1, spos);
return value; return value;
} }
stack_push(jq, value); stack_push(jq, value);
@@ -564,15 +569,15 @@ void jq_init(struct bytecode* bc, jv input, jq_state **jq, int flags) {
new_jq = jv_mem_alloc(sizeof(*new_jq)); new_jq = jv_mem_alloc(sizeof(*new_jq));
memset(new_jq, 0, sizeof(*new_jq)); memset(new_jq, 0, sizeof(*new_jq));
new_jq->path = jv_null(); new_jq->path = jv_null();
forkable_stack_init(&new_jq->data_stk, sizeof(data_stk_elem) * 100); stack_init(&new_jq->stk);
forkable_stack_init(&new_jq->fork_stk, 1024); new_jq->stk_top = 0;
new_jq->fork_top = 0;
new_jq->curr_frame = 0;
struct closure top = {bc, -1}; struct closure top = {bc, -1};
frame_push(&new_jq->data_stk, 0, top, 0); new_jq->curr_frame = frame_push(&new_jq->stk, new_jq->curr_frame, top, 0, new_jq->stk_top);
new_jq->curr_frame = forkable_stack_to_idx(&new_jq->data_stk, forkable_stack_peek(&new_jq->data_stk));
stack_push(new_jq, input); stack_push(new_jq, input);
stack_save(new_jq, bc->code); stack_save(new_jq, bc->code, stack_get_pos(new_jq));
stack_switch(new_jq);
if (flags & JQ_DEBUG_TRACE) { if (flags & JQ_DEBUG_TRACE) {
new_jq->debug_trace_enabled = 1; new_jq->debug_trace_enabled = 1;
} else { } else {
@@ -590,10 +595,11 @@ void jq_teardown(jq_state **jq) {
while (stack_restore(old_jq)) {} while (stack_restore(old_jq)) {}
assert(forkable_stack_empty(&old_jq->fork_stk)); assert(stack_top(&old_jq->stk) == 0);
assert(forkable_stack_empty(&old_jq->data_stk)); assert(old_jq->stk_top == 0);
forkable_stack_free(&old_jq->fork_stk); assert(old_jq->fork_top == 0);
forkable_stack_free(&old_jq->data_stk); assert(old_jq->curr_frame == 0);
stack_free(&old_jq->stk);
jv_free(old_jq->path); jv_free(old_jq->path);
jv_mem_free(old_jq); jv_mem_free(old_jq);
-172
View File
@@ -1,172 +0,0 @@
#ifndef FORKABLE_STACK_H
#define FORKABLE_STACK_H
#include <stddef.h>
#include <assert.h>
#include <string.h>
#include "jv_alloc.h"
struct forkable_stack_header {
/* distance in bytes between this header and the header
of the next object on the stack */
int next_delta;
};
#define FORKABLE_STACK_HEADER struct forkable_stack_header fk_header_
struct forkable_stack {
// Stack grows down from stk+length
char* stk;
// stk+length is just past end of allocated area
// stk = jv_mem_alloc(length)
int length;
// stk+pos is header of top object, or stk+length if empty
int pos;
// everything after-or-including stk+savedlimit must be preserved
int savedlimit;
};
static void forkable_stack_check(struct forkable_stack* s) {
assert(s->stk);
assert(s->length > 0);
assert(s->pos >= 0 && s->pos <= s->length);
assert(s->savedlimit >= 0 && s->savedlimit <= s->length);
}
static int forkable_stack_empty(struct forkable_stack* s) {
return s->pos == s->length;
}
static void forkable_stack_init(struct forkable_stack* s, size_t sz) {
s->stk = jv_mem_alloc(sz);
s->length = sz;
s->pos = s->length;
s->savedlimit = s->length;
forkable_stack_check(s);
}
static void forkable_stack_free(struct forkable_stack* s) {
assert(forkable_stack_empty(s));
assert(s->savedlimit == s->length);
jv_mem_free(s->stk);
s->stk = 0;
}
static void* forkable_stack_push(struct forkable_stack* s, size_t sz_size) {
assert(sz_size > 0 && sz_size % sizeof(struct forkable_stack_header) == 0);
int size = (int)sz_size;
forkable_stack_check(s);
int curr = s->pos < s->savedlimit ? s->pos : s->savedlimit;
if (curr - size < 0) {
int oldlen = s->length;
s->length = (size + oldlen + 1024) * 2;
s->stk = jv_mem_realloc(s->stk, s->length);
int shift = s->length - oldlen;
memmove(s->stk + shift, s->stk, oldlen);
s->pos += shift;
s->savedlimit += shift;
curr += shift;
}
int newpos = curr - size;
assert(newpos < s->pos);
void* ret = (void*)(s->stk + newpos);
((struct forkable_stack_header*)ret)->next_delta = s->pos - newpos;
s->pos = newpos;
forkable_stack_check(s);
return ret;
}
static void* forkable_stack_peek(struct forkable_stack* s) {
assert(!forkable_stack_empty(s));
forkable_stack_check(s);
return (void*)(s->stk + s->pos);
}
static void* forkable_stack_peek_next(struct forkable_stack* s, void* top) {
forkable_stack_check(s);
struct forkable_stack_header* elem = top;
int elempos = (char*)top - s->stk;
assert(elempos >= 0 && elempos < s->length);
assert(elempos + elem->next_delta <= s->length);
if (elempos + elem->next_delta < s->length) {
return (void*)(s->stk + elempos + elem->next_delta);
} else {
return 0;
}
}
// Returns 1 if the next forkable_stack_pop will permanently remove an
// object from the stack (i.e. the top object was not saved with a fork)
static int forkable_stack_pop_will_free(struct forkable_stack* s) {
return s->pos < s->savedlimit;
}
static void forkable_stack_pop(struct forkable_stack* s) {
forkable_stack_check(s);
struct forkable_stack_header* elem = forkable_stack_peek(s);
int reclaim_upto = s->pos + elem->next_delta < s->savedlimit ?
s->pos + elem->next_delta : s->savedlimit;
assert(reclaim_upto <= s->length);
int reclaimed = reclaim_upto > s->pos ? reclaim_upto - s->pos : 0;
assert(0 <= reclaimed && s->pos + reclaimed <= s->length);
// s->pos <= i < reclaim_upto means that position i is to be freed
assert((reclaimed > 0) == forkable_stack_pop_will_free(s));
int new_pos = s->pos + elem->next_delta;
jv_mem_invalidate(elem, reclaimed);
s->pos = new_pos;
}
struct forkable_stack_state {
// We save the previous pos, savedlimit as
// length-pos, length-savedlimit since these
// values are stable across stack reallocations.
int prev_pos_delta, prev_limit_delta;
};
static void forkable_stack_save(struct forkable_stack* s, struct forkable_stack_state* state) {
forkable_stack_check(s);
state->prev_pos_delta = s->length - s->pos;
state->prev_limit_delta = s->length - s->savedlimit;
if (s->pos < s->savedlimit) s->savedlimit = s->pos;
}
static void forkable_stack_switch(struct forkable_stack* s, struct forkable_stack_state* state) {
forkable_stack_check(s);
int curr_pos = s->pos;
s->pos = s->length - state->prev_pos_delta;
state->prev_pos_delta = s->length - curr_pos;
int curr_limit = s->savedlimit;
if (curr_pos < curr_limit) s->savedlimit = curr_pos;
//state->prevlimit = curr_limit; FIXME clean up
forkable_stack_check(s);
}
static void forkable_stack_restore(struct forkable_stack* s, struct forkable_stack_state* state) {
forkable_stack_check(s);
assert(s->savedlimit <= s->length - state->prev_pos_delta);
assert(s->savedlimit <= s->length - state->prev_limit_delta);
s->pos = s->length - state->prev_pos_delta;
s->savedlimit = s->length - state->prev_limit_delta;
forkable_stack_check(s);
}
typedef int stack_idx; // never zero when valid
static stack_idx forkable_stack_to_idx(struct forkable_stack* s, void* ptr) {
char* item = ptr;
int pos = item - s->stk;
assert(pos >= 0 && pos < s->length);
return s->length - pos;
}
static void* forkable_stack_from_idx(struct forkable_stack* s, stack_idx idx) {
assert(idx >= 1 && idx <= s->length);
return &s->stk[s->length - idx];
}
#endif
+22 -24
View File
@@ -1,21 +1,20 @@
#ifndef FRAME_LAYOUT_H #ifndef FRAME_LAYOUT_H
#include "forkable_stack.h" #include "newstack.h"
#include "bytecode.h" #include "bytecode.h"
struct closure { struct closure {
struct bytecode* bc; struct bytecode* bc;
stack_idx env; stack_ptr env;
}; };
struct continuation { struct continuation {
struct bytecode* bc; struct bytecode* bc;
stack_idx env; stack_ptr env;
stack_idx next; stack_ptr retdata;
uint16_t* retaddr; uint16_t* retaddr;
}; };
typedef union frame_elem { typedef union frame_elem {
FORKABLE_STACK_HEADER;
struct continuation cont; struct continuation cont;
struct closure closure; struct closure closure;
jv jsonval; jv jsonval;
@@ -50,11 +49,12 @@ static jv* frame_local_var(frame_ptr fr, int var) {
} }
static frame_ptr frame_current(struct forkable_stack* stk, stack_idx idx) { static frame_ptr frame_current(struct stack* stk, stack_ptr idx) {
frame_ptr fp = forkable_stack_from_idx(stk, idx); frame_ptr fp = stack_block(stk, idx);
if (frame_self(fp)->next) { stack_ptr next = *stack_block_next(stk, idx);
frame_ptr fpnext = forkable_stack_from_idx(stk, frame_self(fp)->next); if (next) {
frame_ptr fpnext = stack_block(stk, next);
struct bytecode* bc = frame_self(fpnext)->bc; struct bytecode* bc = frame_self(fpnext)->bc;
assert(frame_self(fp)->retaddr >= bc->code && frame_self(fp)->retaddr < bc->code + bc->codelen); assert(frame_self(fp)->retaddr >= bc->code && frame_self(fp)->retaddr < bc->code + bc->codelen);
} else { } else {
@@ -63,49 +63,47 @@ static frame_ptr frame_current(struct forkable_stack* stk, stack_idx idx) {
return fp; return fp;
} }
static struct bytecode* frame_current_bytecode(struct forkable_stack* stk, stack_idx curr) { static struct bytecode* frame_current_bytecode(struct stack* stk, stack_ptr curr) {
return frame_self(frame_current(stk, curr))->bc; return frame_self(frame_current(stk, curr))->bc;
} }
static uint16_t** frame_current_retaddr(struct forkable_stack* stk, stack_idx curr) { static uint16_t** frame_current_retaddr(struct stack* stk, stack_ptr curr) {
return &frame_self(frame_current(stk, curr))->retaddr; return &frame_self(frame_current(stk, curr))->retaddr;
} }
static frame_ptr frame_get_parent(struct forkable_stack* stk, frame_ptr fr) { static stack_ptr frame_get_parent(struct stack* stk, stack_ptr fr) {
return forkable_stack_from_idx(stk, frame_self(fr)->env); return frame_self(stack_block(stk, fr))->env;
} }
static frame_ptr frame_get_level(struct forkable_stack* stk, frame_ptr fr, int level) { static stack_ptr frame_get_level(struct stack* stk, stack_ptr fr, int level) {
for (int i=0; i<level; i++) { for (int i=0; i<level; i++) {
fr = frame_get_parent(stk, fr); fr = frame_get_parent(stk, fr);
} }
return fr; return fr;
} }
static frame_ptr frame_push(struct forkable_stack* stk, stack_idx curr, struct closure cl, uint16_t* retaddr) { static stack_ptr frame_push(struct stack* stk, stack_ptr curr, struct closure cl, uint16_t* retaddr, stack_ptr datastk) {
frame_ptr fp = forkable_stack_push(stk, frame_size(cl.bc)); stack_ptr fpidx = stack_push_block(stk, curr, frame_size(cl.bc));
frame_ptr fp = stack_block(stk, fpidx);
struct continuation* cc = frame_self(fp); struct continuation* cc = frame_self(fp);
cc->bc = cl.bc; cc->bc = cl.bc;
cc->env = cl.env; cc->env = cl.env;
cc->next = curr; cc->retdata = datastk;
cc->retaddr = retaddr; cc->retaddr = retaddr;
for (int i=0; i<cl.bc->nlocals; i++) { for (int i=0; i<cl.bc->nlocals; i++) {
*frame_local_var(fp, i) = jv_invalid(); *frame_local_var(fp, i) = jv_invalid();
} }
return fp; return fpidx;
} }
static stack_idx frame_pop(struct forkable_stack* stk, stack_idx curr) { static stack_ptr frame_pop(struct stack* stk, stack_ptr curr) {
frame_ptr fp = frame_current(stk, curr); frame_ptr fp = frame_current(stk, curr);
stack_idx next = frame_self(fp)->next; if (stack_pop_will_free(stk, curr)) {
assert(fp == forkable_stack_peek(stk));
if (forkable_stack_pop_will_free(stk)) {
int nlocals = frame_self(fp)->bc->nlocals; int nlocals = frame_self(fp)->bc->nlocals;
for (int i=0; i<nlocals; i++) { for (int i=0; i<nlocals; i++) {
jv_free(*frame_local_var(fp, i)); jv_free(*frame_local_var(fp, i));
} }
} }
forkable_stack_pop(stk); return stack_pop_block(stk, curr, frame_size(frame_self(fp)->bc));
return next;
} }
#endif #endif
+103
View File
@@ -0,0 +1,103 @@
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
struct determine_alignment {
char x;
union {
int i;
double d;
uint64_t u64;
size_t sz;
void* ptr;
} u;
};
enum {ALIGNMENT = offsetof(struct determine_alignment, u)};
size_t align_round_up(size_t sz) {
return ((sz + (ALIGNMENT - 1)) / ALIGNMENT) * ALIGNMENT;
}
typedef int stack_ptr;
struct stack {
char* mem_end; // one-past-the-end of allocated region
stack_ptr bound;
stack_ptr limit; // 0 - stack is empty
};
void stack_init(struct stack* s) {
s->mem_end = 0;
s->bound = ALIGNMENT;
s->limit = 0;
}
void stack_free(struct stack* s) {
char* mem_start = s->mem_end - ( -s->bound + ALIGNMENT);
free(mem_start);
stack_init(s);
}
stack_ptr stack_top(struct stack* s) {
return s->limit;
}
int stack_pop_will_free(struct stack* s, stack_ptr p) {
return p == s->limit;
}
void* stack_block(struct stack* s, stack_ptr p) {
return (void*)(s->mem_end + p);
}
stack_ptr* stack_block_next(struct stack* s, stack_ptr p) {
return &(((stack_ptr*)stack_block(s, p))[-1]);
}
void stack_reallocate(struct stack* s, size_t sz) {
int old_mem_length = -(s->bound) + ALIGNMENT;
char* old_mem_start = s->mem_end - old_mem_length;
int new_mem_length = align_round_up((old_mem_length + sz + 256) * 2);
char* new_mem_start = realloc(old_mem_start, new_mem_length);
memmove(new_mem_start + (new_mem_length - old_mem_length),
new_mem_start, old_mem_length);
s->mem_end = new_mem_start + new_mem_length;
s->bound = -(new_mem_length - ALIGNMENT);
}
stack_ptr stack_push_block(struct stack* s, stack_ptr p, size_t sz) {
int alloc_sz = align_round_up(sz) + ALIGNMENT;
stack_ptr r = s->limit - alloc_sz;
if (r < s->bound) {
stack_reallocate(s, alloc_sz);
}
s->limit = r;
*stack_block_next(s, r) = p;
return r;
}
stack_ptr stack_pop_block(struct stack* s, stack_ptr p, size_t sz) {
stack_ptr r = *stack_block_next(s, p);
if (p == s->limit) {
int alloc_sz = align_round_up(sz) + ALIGNMENT;
s->limit += alloc_sz;
}
return r;
}
#if 0
int main() {
stack s;
stack_init(&s);
stack_ptr top = 0;
top = stack_push_block(&s, top, sizeof(double));
double* d1 = stack_block(&s, top);
*d1 = 42;
top = stack_push_block(&s, top, sizeof(double));
double* d2 = stack_block(&s, top);
*d2 = 33;
}
#endif