1
0
mirror of https://github.com/stedolan/jq.git synced 2024-05-11 05:55:39 +00:00

Clean up lots of stack and frame logic.

Move frame defs to execute.c
This commit is contained in:
Stephen Dolan
2013-06-14 22:08:18 +01:00
parent 5f5c1dc5a6
commit 05d90517b0
3 changed files with 140 additions and 160 deletions

View File

@@ -2,20 +2,14 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
struct determine_alignment { struct determine_alignment {
char x; char x;
union { union { int i; double d; uint64_t u64; size_t sz; void* ptr; } u;
int i;
double d;
uint64_t u64;
size_t sz;
void* ptr;
} u;
}; };
enum {ALIGNMENT = offsetof(struct determine_alignment, u)}; enum {ALIGNMENT = offsetof(struct determine_alignment, u)};
size_t align_round_up(size_t sz) { static size_t align_round_up(size_t sz) {
return ((sz + (ALIGNMENT - 1)) / ALIGNMENT) * ALIGNMENT; return ((sz + (ALIGNMENT - 1)) / ALIGNMENT) * ALIGNMENT;
} }
@@ -27,35 +21,32 @@ struct stack {
stack_ptr limit; // 0 - stack is empty stack_ptr limit; // 0 - stack is empty
}; };
void stack_init(struct stack* s) { static void stack_init(struct stack* s) {
s->mem_end = 0; s->mem_end = 0;
s->bound = ALIGNMENT; s->bound = ALIGNMENT;
s->limit = 0; s->limit = 0;
} }
void stack_free(struct stack* s) { static void stack_free(struct stack* s) {
assert(s->limit == 0 && "stack freed while not empty");
char* mem_start = s->mem_end - ( -s->bound + ALIGNMENT); char* mem_start = s->mem_end - ( -s->bound + ALIGNMENT);
free(mem_start); free(mem_start);
stack_init(s); stack_init(s);
} }
stack_ptr stack_top(struct stack* s) { static int stack_pop_will_free(struct stack* s, stack_ptr p) {
return s->limit;
}
int stack_pop_will_free(struct stack* s, stack_ptr p) {
return p == s->limit; return p == s->limit;
} }
void* stack_block(struct stack* s, stack_ptr p) { static void* stack_block(struct stack* s, stack_ptr p) {
return (void*)(s->mem_end + p); return (void*)(s->mem_end + p);
} }
stack_ptr* stack_block_next(struct stack* s, stack_ptr p) { static stack_ptr* stack_block_next(struct stack* s, stack_ptr p) {
return &(((stack_ptr*)stack_block(s, p))[-1]); return &((stack_ptr*)stack_block(s, p))[-1];
} }
void stack_reallocate(struct stack* s, size_t sz) { static void stack_reallocate(struct stack* s, size_t sz) {
int old_mem_length = -(s->bound) + ALIGNMENT; int old_mem_length = -(s->bound) + ALIGNMENT;
char* old_mem_start = s->mem_end - old_mem_length; char* old_mem_start = s->mem_end - old_mem_length;
@@ -67,7 +58,7 @@ void stack_reallocate(struct stack* s, size_t sz) {
s->bound = -(new_mem_length - ALIGNMENT); s->bound = -(new_mem_length - ALIGNMENT);
} }
stack_ptr stack_push_block(struct stack* s, stack_ptr p, size_t sz) { static stack_ptr stack_push_block(struct stack* s, stack_ptr p, size_t sz) {
int alloc_sz = align_round_up(sz) + ALIGNMENT; int alloc_sz = align_round_up(sz) + ALIGNMENT;
stack_ptr r = s->limit - alloc_sz; stack_ptr r = s->limit - alloc_sz;
if (r < s->bound) { if (r < s->bound) {
@@ -78,7 +69,7 @@ stack_ptr stack_push_block(struct stack* s, stack_ptr p, size_t sz) {
return r; return r;
} }
stack_ptr stack_pop_block(struct stack* s, stack_ptr p, size_t sz) { static stack_ptr stack_pop_block(struct stack* s, stack_ptr p, size_t sz) {
stack_ptr r = *stack_block_next(s, p); stack_ptr r = *stack_block_next(s, p);
if (p == s->limit) { if (p == s->limit) {
int alloc_sz = align_round_up(sz) + ALIGNMENT; int alloc_sz = align_round_up(sz) + ALIGNMENT;

179
execute.c
View File

@@ -5,11 +5,10 @@
#include "execute.h" #include "execute.h"
#include "exec_stack.h"
#include "opcode.h" #include "opcode.h"
#include "bytecode.h" #include "bytecode.h"
#include "frame_layout.h"
#include "jv_alloc.h" #include "jv_alloc.h"
#include "jq_parser.h" #include "jq_parser.h"
#include "locfile.h" #include "locfile.h"
@@ -30,6 +29,110 @@ struct jq_state {
int initial_execution; int initial_execution;
}; };
struct closure {
struct bytecode* bc;
stack_ptr env;
};
union frame_entry {
struct closure closure;
jv localvar;
};
struct frame {
struct bytecode* bc;
stack_ptr env;
stack_ptr retdata;
uint16_t* retaddr;
/* bc->nclosures closures followed by bc->nlocals local variables */
union frame_entry entries[0];
};
static int frame_size(struct bytecode* bc) {
return sizeof(struct frame) + sizeof(union frame_entry) * (bc->nclosures + bc->nlocals);
}
static struct frame* frame_current(struct jq_state* jq) {
struct frame* fp = stack_block(&jq->stk, jq->curr_frame);
stack_ptr next = *stack_block_next(&jq->stk, jq->curr_frame);
if (next) {
struct frame* fpnext = stack_block(&jq->stk, next);
struct bytecode* bc = fpnext->bc;
assert(fp->retaddr >= bc->code && fp->retaddr < bc->code + bc->codelen);
} else {
assert(fp->retaddr == 0);
}
return fp;
}
static stack_ptr frame_get_level(struct jq_state* jq, int level) {
stack_ptr fr = jq->curr_frame;
for (int i=0; i<level; i++) {
struct frame* fp = stack_block(&jq->stk, fr);
fr = fp->env;
}
return fr;
}
static jv* frame_local_var(struct jq_state* jq, int var, int level) {
struct frame* fr = stack_block(&jq->stk, frame_get_level(jq, level));
assert(var >= 0);
assert(var < fr->bc->nlocals);
return &fr->entries[fr->bc->nclosures + var].localvar;
}
static struct closure make_closure(struct jq_state* jq, uint16_t* pc) {
uint16_t level = *pc++;
uint16_t idx = *pc++;
stack_ptr fridx = frame_get_level(jq, level);
struct frame* fr = stack_block(&jq->stk, fridx);
if (idx & ARG_NEWCLOSURE) {
int subfn_idx = idx & ~ARG_NEWCLOSURE;
assert(subfn_idx < fr->bc->nsubfunctions);
struct closure cl = {fr->bc->subfunctions[subfn_idx],
fridx};
return cl;
} else {
int closure = idx;
assert(closure >= 0);
assert(closure < fr->bc->nclosures);
return fr->entries[closure].closure;
}
}
static struct frame* frame_push(struct jq_state* jq, struct closure callee,
uint16_t* argdef, int nargs) {
stack_ptr new_frame_idx = stack_push_block(&jq->stk, jq->curr_frame, frame_size(callee.bc));
struct frame* new_frame = stack_block(&jq->stk, new_frame_idx);
new_frame->bc = callee.bc;
new_frame->env = callee.env;
assert(nargs == new_frame->bc->nclosures);
union frame_entry* entries = new_frame->entries;
for (int i=0; i<nargs; i++) {
entries->closure = make_closure(jq, argdef + i * 2);
entries++;
}
for (int i=0; i<callee.bc->nlocals; i++) {
entries->localvar = jv_invalid();
entries++;
}
jq->curr_frame = new_frame_idx;
return new_frame;
}
static void frame_pop(struct jq_state* jq) {
assert(jq->curr_frame);
struct frame* fp = frame_current(jq);
if (stack_pop_will_free(&jq->stk, jq->curr_frame)) {
int nlocals = fp->bc->nlocals;
for (int i=0; i<nlocals; i++) {
jv_free(*frame_local_var(jq, i, 0));
}
}
jq->curr_frame = stack_pop_block(&jq->stk, jq->curr_frame, frame_size(fp->bc));
}
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));
jq->stk_top = stack_push_block(&jq->stk, jq->stk_top, sizeof(jv)); jq->stk_top = stack_push_block(&jq->stk, jq->stk_top, sizeof(jv));
@@ -90,11 +193,11 @@ void path_append(jq_state* jq, jv component) {
} }
uint16_t* stack_restore(jq_state *jq){ uint16_t* stack_restore(jq_state *jq){
while (stack_top(&jq->stk) != jq->fork_top) { while (!stack_pop_will_free(&jq->stk, jq->fork_top)) {
if (stack_pop_will_free(&jq->stk, jq->stk_top)) { if (stack_pop_will_free(&jq->stk, jq->stk_top)) {
jv_free(stack_pop(jq)); jv_free(stack_pop(jq));
} else if (stack_pop_will_free(&jq->stk, jq->curr_frame)) { } else if (stack_pop_will_free(&jq->stk, jq->curr_frame)) {
jq->curr_frame = frame_pop(&jq->stk, jq->curr_frame); frame_pop(jq);
} else { } else {
assert(0); assert(0);
} }
@@ -121,22 +224,6 @@ uint16_t* stack_restore(jq_state *jq){
} }
static struct closure make_closure(struct jq_state* jq, stack_ptr fridx, uint16_t* pc) {
uint16_t level = *pc++;
uint16_t idx = *pc++;
fridx = frame_get_level(&jq->stk, fridx, level);
struct frame* fr = frame_current(&jq->stk, fridx);
if (idx & ARG_NEWCLOSURE) {
int subfn_idx = idx & ~ARG_NEWCLOSURE;
assert(subfn_idx < fr->bc->nsubfunctions);
struct closure cl = {fr->bc->subfunctions[subfn_idx],
fridx};
return cl;
} else {
return *frame_closure_arg(fr, idx);
}
}
void print_error(jv value) { void print_error(jv value) {
assert(!jv_is_valid(value)); assert(!jv_is_valid(value));
jv msg = jv_invalid_get_msg(value); jv msg = jv_invalid_get_msg(value);
@@ -159,7 +246,7 @@ 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(&jq->stk, jq->curr_frame)->bc, pc); dump_operation(frame_current(jq)->bc, pc);
printf("\t"); printf("\t");
const struct opcode_description* opdesc = opcode_describe(opcode); const struct opcode_description* opdesc = opcode_describe(opcode);
stack_ptr param = 0; stack_ptr param = 0;
@@ -196,7 +283,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(&jq->stk, jq->curr_frame)->bc->constants), *pc++); jv v = jv_array_get(jv_copy(frame_current(jq)->bc->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);
@@ -246,8 +333,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++;
struct frame* fp = frame_current(&jq->stk, frame_get_level(&jq->stk, jq->curr_frame, level)); jv* var = frame_local_var(jq, vidx, level);
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);
break; break;
@@ -278,8 +364,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++;
struct frame* fp = frame_current(&jq->stk, frame_get_level(&jq->stk, jq->curr_frame, level)); jv* var = frame_local_var(jq, v, level);
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 ||
jv_get_kind(max) != JV_KIND_NUMBER) { jv_get_kind(max) != JV_KIND_NUMBER) {
@@ -306,8 +391,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++;
struct frame* fp = frame_current(&jq->stk, frame_get_level(&jq->stk, jq->curr_frame, level)); jv* var = frame_local_var(jq, v, level);
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);
jv_dump(jv_copy(*var), 0); jv_dump(jv_copy(*var), 0);
@@ -322,8 +406,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++;
struct frame* fp = frame_current(&jq->stk, frame_get_level(&jq->stk, jq->curr_frame, level)); jv* var = frame_local_var(jq, v, level);
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);
jv_dump(jv_copy(*var), 0); jv_dump(jv_copy(*var), 0);
@@ -338,8 +421,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++;
struct frame* fp = frame_current(&jq->stk, frame_get_level(&jq->stk, jq->curr_frame, level)); jv* var = frame_local_var(jq, v, level);
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) {
printf("V%d = ", v); printf("V%d = ", v);
@@ -504,7 +586,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(&jq->stk, jq->curr_frame)->bc->globals->cfunctions[*pc++]; struct cfunction* func = &frame_current(jq)->bc->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);
@@ -519,32 +601,23 @@ 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;
stack_ptr old_frame = jq->curr_frame; struct frame* new_frame = frame_push(jq, make_closure(jq, pc),
jq->curr_frame = frame_push(&jq->stk, jq->curr_frame, pc + 2, nclosures);
make_closure(jq, jq->curr_frame, pc), new_frame->retdata = jq->stk_top;
retaddr, jq->stk_top); new_frame->retaddr = retaddr;
pc += 2; pc = new_frame->bc->code;
struct frame* new_frame = frame_current(&jq->stk, jq->curr_frame);
assert(nclosures == new_frame->bc->nclosures);
for (int i=0; i<nclosures; i++) {
*frame_closure_arg(new_frame, i) = make_closure(jq, old_frame, pc);
pc += 2;
}
pc = frame_current(&jq->stk, jq->curr_frame)->bc->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);
assert(jq->stk_top == frame_current(&jq->stk, jq->curr_frame)->retdata); assert(jq->stk_top == frame_current(jq)->retdata);
uint16_t* retaddr = frame_current(&jq->stk, jq->curr_frame)->retaddr; uint16_t* retaddr = frame_current(jq)->retaddr;
if (retaddr) { if (retaddr) {
// function return // function return
pc = retaddr; pc = retaddr;
jq->curr_frame = frame_pop(&jq->stk, jq->curr_frame); frame_pop(jq);
} else { } else {
// top-level return, yielding value // top-level return, yielding value
struct stack_pos spos = stack_get_pos(jq); struct stack_pos spos = stack_get_pos(jq);
@@ -575,7 +648,10 @@ void jq_init(struct bytecode* bc, jv input, jq_state **jq, int flags) {
new_jq->curr_frame = 0; new_jq->curr_frame = 0;
struct closure top = {bc, -1}; struct closure top = {bc, -1};
new_jq->curr_frame = frame_push(&new_jq->stk, new_jq->curr_frame, top, 0, new_jq->stk_top); struct frame* top_frame = frame_push(new_jq, top, 0, 0);
top_frame->retdata = 0;
top_frame->retaddr = 0;
stack_push(new_jq, input); stack_push(new_jq, input);
stack_save(new_jq, bc->code, stack_get_pos(new_jq)); stack_save(new_jq, bc->code, stack_get_pos(new_jq));
if (flags & JQ_DEBUG_TRACE) { if (flags & JQ_DEBUG_TRACE) {
@@ -595,7 +671,6 @@ void jq_teardown(jq_state **jq) {
while (stack_restore(old_jq)) {} while (stack_restore(old_jq)) {}
assert(stack_top(&old_jq->stk) == 0);
assert(old_jq->stk_top == 0); assert(old_jq->stk_top == 0);
assert(old_jq->fork_top == 0); assert(old_jq->fork_top == 0);
assert(old_jq->curr_frame == 0); assert(old_jq->curr_frame == 0);

View File

@@ -1,86 +0,0 @@
#ifndef FRAME_LAYOUT_H
#include "newstack.h"
#include "bytecode.h"
struct closure {
struct bytecode* bc;
stack_ptr env;
};
union frame_entry {
struct closure closure;
jv localvar;
};
struct frame {
struct bytecode* bc;
stack_ptr env;
stack_ptr retdata;
uint16_t* retaddr;
/* bc->nclosures closures followed by bc->nlocals local variables */
union frame_entry entries[0];
};
static int frame_size(struct bytecode* bc) {
return sizeof(struct frame) + sizeof(union frame_entry) * (bc->nclosures + bc->nlocals);
}
static struct closure* frame_closure_arg(struct frame* fr, int closure) {
assert(closure >= 0);
assert(closure < fr->bc->nclosures);
return &fr->entries[closure].closure;
}
static jv* frame_local_var(struct frame* fr, int var) {
assert(var >= 0);
assert(var < fr->bc->nlocals);
return &fr->entries[fr->bc->nclosures + var].localvar;
}
static struct frame* frame_current(struct stack* stk, stack_ptr idx) {
struct frame* fp = stack_block(stk, idx);
stack_ptr next = *stack_block_next(stk, idx);
if (next) {
struct frame* fpnext = stack_block(stk, next);
struct bytecode* bc = fpnext->bc;
assert(fp->retaddr >= bc->code && fp->retaddr < bc->code + bc->codelen);
} else {
assert(fp->retaddr == 0);
}
return fp;
}
static stack_ptr frame_get_level(struct stack* stk, stack_ptr fr, int level) {
for (int i=0; i<level; i++) {
struct frame* fp = stack_block(stk, fr);
fr = fp->env;
}
return fr;
}
static stack_ptr frame_push(struct stack* stk, stack_ptr curr, struct closure cl, uint16_t* retaddr, stack_ptr datastk) {
stack_ptr fpidx = stack_push_block(stk, curr, frame_size(cl.bc));
struct frame* fp = stack_block(stk, fpidx);
fp->bc = cl.bc;
fp->env = cl.env;
fp->retdata = datastk;
fp->retaddr = retaddr;
for (int i=0; i<cl.bc->nlocals; i++) {
*frame_local_var(fp, i) = jv_invalid();
}
return fpidx;
}
static stack_ptr frame_pop(struct stack* stk, stack_ptr curr) {
struct frame* fp = frame_current(stk, curr);
if (stack_pop_will_free(stk, curr)) {
int nlocals = fp->bc->nlocals;
for (int i=0; i<nlocals; i++) {
jv_free(*frame_local_var(fp, i));
}
}
return stack_pop_block(stk, curr, frame_size(fp->bc));
}
#endif