8
#include "exec_stack.h"
9
9
#include "bytecode.h"
11
#include "forkable_stack.h"
12
#include "frame_layout.h"
14
11
#include "jv_alloc.h"
15
12
#include "jq_parser.h"
16
13
#include "locfile.h"
19
16
#include "parser.h"
20
17
#include "builtin.h"
23
struct forkable_stack data_stk;
24
struct forkable_stack frame_stk;
25
struct forkable_stack fork_stk;
20
void (*nomem_handler)(void *);
21
void *nomem_handler_data;
28
34
int debug_trace_enabled;
29
35
int initial_execution;
33
FORKABLE_STACK_HEADER;
44
struct closure closure;
53
/* bc->nclosures closures followed by bc->nlocals local variables */
54
union frame_entry entries[0];
57
static int frame_size(struct bytecode* bc) {
58
return sizeof(struct frame) + sizeof(union frame_entry) * (bc->nclosures + bc->nlocals);
61
static struct frame* frame_current(struct jq_state* jq) {
62
struct frame* fp = stack_block(&jq->stk, jq->curr_frame);
64
stack_ptr next = *stack_block_next(&jq->stk, jq->curr_frame);
66
struct frame* fpnext = stack_block(&jq->stk, next);
67
struct bytecode* bc = fpnext->bc;
68
assert(fp->retaddr >= bc->code && fp->retaddr < bc->code + bc->codelen);
70
assert(fp->retaddr == 0);
75
static stack_ptr frame_get_level(struct jq_state* jq, int level) {
76
stack_ptr fr = jq->curr_frame;
77
for (int i=0; i<level; i++) {
78
struct frame* fp = stack_block(&jq->stk, fr);
84
static jv* frame_local_var(struct jq_state* jq, int var, int level) {
85
struct frame* fr = stack_block(&jq->stk, frame_get_level(jq, level));
87
assert(var < fr->bc->nlocals);
88
return &fr->entries[fr->bc->nclosures + var].localvar;
91
static struct closure make_closure(struct jq_state* jq, uint16_t* pc) {
92
uint16_t level = *pc++;
94
stack_ptr fridx = frame_get_level(jq, level);
95
struct frame* fr = stack_block(&jq->stk, fridx);
96
if (idx & ARG_NEWCLOSURE) {
97
int subfn_idx = idx & ~ARG_NEWCLOSURE;
98
assert(subfn_idx < fr->bc->nsubfunctions);
99
struct closure cl = {fr->bc->subfunctions[subfn_idx],
104
assert(closure >= 0);
105
assert(closure < fr->bc->nclosures);
106
return fr->entries[closure].closure;
110
static struct frame* frame_push(struct jq_state* jq, struct closure callee,
111
uint16_t* argdef, int nargs) {
112
stack_ptr new_frame_idx = stack_push_block(&jq->stk, jq->curr_frame, frame_size(callee.bc));
113
struct frame* new_frame = stack_block(&jq->stk, new_frame_idx);
114
new_frame->bc = callee.bc;
115
new_frame->env = callee.env;
116
assert(nargs == new_frame->bc->nclosures);
117
union frame_entry* entries = new_frame->entries;
118
for (int i=0; i<nargs; i++) {
119
entries->closure = make_closure(jq, argdef + i * 2);
122
for (int i=0; i<callee.bc->nlocals; i++) {
123
entries->localvar = jv_invalid();
126
jq->curr_frame = new_frame_idx;
130
static void frame_pop(struct jq_state* jq) {
131
assert(jq->curr_frame);
132
struct frame* fp = frame_current(jq);
133
if (stack_pop_will_free(&jq->stk, jq->curr_frame)) {
134
int nlocals = fp->bc->nlocals;
135
for (int i=0; i<nlocals; i++) {
136
jv_free(*frame_local_var(jq, i, 0));
139
jq->curr_frame = stack_pop_block(&jq->stk, jq->curr_frame, frame_size(fp->bc));
37
142
void stack_push(jq_state *jq, jv val) {
38
143
assert(jv_is_valid(val));
39
data_stk_elem* s = forkable_stack_push(&jq->data_stk, sizeof(data_stk_elem));
144
jq->stk_top = stack_push_block(&jq->stk, jq->stk_top, sizeof(jv));
145
jv* sval = stack_block(&jq->stk, jq->stk_top);
43
149
jv stack_pop(jq_state *jq) {
44
data_stk_elem* s = forkable_stack_peek(&jq->data_stk);
46
if (!forkable_stack_pop_will_free(&jq->data_stk)) {
150
jv* sval = stack_block(&jq->stk, jq->stk_top);
152
if (!stack_pop_will_free(&jq->stk, jq->stk_top)) {
47
153
val = jv_copy(val);
49
forkable_stack_pop(&jq->data_stk);
155
jq->stk_top = stack_pop_block(&jq->stk, jq->stk_top, sizeof(jv));
50
156
assert(jv_is_valid(val));
55
161
struct forkpoint {
56
FORKABLE_STACK_HEADER;
57
struct forkable_stack_state saved_data_stack;
58
struct forkable_stack_state saved_call_stack;
162
stack_ptr saved_data_stack;
163
stack_ptr saved_curr_frame;
59
164
int path_len, subexp_nest;
60
165
uint16_t* return_address;
64
void stack_save(jq_state *jq, uint16_t* retaddr){
65
struct forkpoint* fork = forkable_stack_push(&jq->fork_stk, sizeof(struct forkpoint));
66
forkable_stack_save(&jq->data_stk, &fork->saved_data_stack);
67
forkable_stack_save(&jq->frame_stk, &fork->saved_call_stack);
169
stack_ptr saved_data_stack, saved_curr_frame;
172
struct stack_pos stack_get_pos(jq_state* jq) {
173
struct stack_pos sp = {jq->stk_top, jq->curr_frame};
177
void stack_save(jq_state *jq, uint16_t* retaddr, struct stack_pos sp){
178
jq->fork_top = stack_push_block(&jq->stk, jq->fork_top, sizeof(struct forkpoint));
179
struct forkpoint* fork = stack_block(&jq->stk, jq->fork_top);
180
fork->saved_data_stack = jq->stk_top;
181
fork->saved_curr_frame = jq->curr_frame;
69
183
jv_get_kind(jq->path) == JV_KIND_ARRAY ? jv_array_length(jv_copy(jq->path)) : 0;
70
184
fork->subexp_nest = jq->subexp_nest;
71
185
fork->return_address = retaddr;
74
void stack_switch(jq_state *jq) {
75
struct forkpoint* fork = forkable_stack_peek(&jq->fork_stk);
76
forkable_stack_switch(&jq->data_stk, &fork->saved_data_stack);
77
forkable_stack_switch(&jq->frame_stk, &fork->saved_call_stack);
186
jq->stk_top = sp.saved_data_stack;
187
jq->curr_frame = sp.saved_curr_frame;
80
190
void path_append(jq_state* jq, jv component) {
91
201
uint16_t* stack_restore(jq_state *jq){
92
while (!forkable_stack_empty(&jq->data_stk) &&
93
forkable_stack_pop_will_free(&jq->data_stk)) {
94
jv_free(stack_pop(jq));
96
while (!forkable_stack_empty(&jq->frame_stk) &&
97
forkable_stack_pop_will_free(&jq->frame_stk)) {
98
frame_pop(&jq->frame_stk);
202
while (!stack_pop_will_free(&jq->stk, jq->fork_top)) {
203
if (stack_pop_will_free(&jq->stk, jq->stk_top)) {
204
jv_free(stack_pop(jq));
205
} else if (stack_pop_will_free(&jq->stk, jq->curr_frame)) {
101
if (forkable_stack_empty(&jq->fork_stk)) {
212
if (jq->fork_top == 0) {
105
struct forkpoint* fork = forkable_stack_peek(&jq->fork_stk);
216
struct forkpoint* fork = stack_block(&jq->stk, jq->fork_top);
106
217
uint16_t* retaddr = fork->return_address;
107
forkable_stack_restore(&jq->data_stk, &fork->saved_data_stack);
108
forkable_stack_restore(&jq->frame_stk, &fork->saved_call_stack);
218
jq->stk_top = fork->saved_data_stack;
219
jq->curr_frame = fork->saved_curr_frame;
109
220
int path_len = fork->path_len;
110
221
if (jv_get_kind(jq->path) == JV_KIND_ARRAY) {
111
222
assert(path_len >= 0);
114
225
assert(path_len == 0);
116
227
jq->subexp_nest = fork->subexp_nest;
117
forkable_stack_pop(&jq->fork_stk);
228
jq->fork_top = stack_pop_block(&jq->stk, jq->fork_top, sizeof(struct forkpoint));
122
static struct closure make_closure(struct forkable_stack* stk, frame_ptr fr, uint16_t* pc) {
123
uint16_t level = *pc++;
124
uint16_t idx = *pc++;
125
fr = frame_get_level(stk, fr, level);
126
if (idx & ARG_NEWCLOSURE) {
127
int subfn_idx = idx & ~ARG_NEWCLOSURE;
128
assert(subfn_idx < frame_self(fr)->bc->nsubfunctions);
129
struct closure cl = {frame_self(fr)->bc->subfunctions[subfn_idx],
130
forkable_stack_to_idx(stk, fr)};
133
return *frame_closure_arg(fr, idx);
232
static void jq_reset(jq_state *jq) {
233
while (stack_restore(jq)) {}
235
assert(jq->stk_top == 0);
236
assert(jq->fork_top == 0);
237
assert(jq->curr_frame == 0);
238
stack_reset(&jq->stk);
240
if (jv_get_kind(jq->path) != JV_KIND_INVALID)
242
jq->path = jv_null();
137
void print_error(jv value) {
246
static void print_error(jq_state *jq, jv value) {
138
247
assert(!jv_is_valid(value));
139
248
jv msg = jv_invalid_get_msg(value);
140
if (jv_get_kind(msg) == JV_KIND_STRING) {
141
fprintf(stderr, "jq: error: %s\n", jv_string_value(msg));
250
if (jv_get_kind(msg) == JV_KIND_STRING)
251
msg2 = jv_string_fmt("jq: error: %s", jv_string_value(msg));
253
msg2 = jv_string_fmt("jq: error: <unknown>");
257
jq->err_cb(jq->err_cb_data, msg2);
258
else if (jv_get_kind(msg2) == JV_KIND_STRING)
259
fprintf(stderr, "%s\n", jv_string_value(msg2));
261
fprintf(stderr, "jq: error: %s\n", strerror(ENOMEM));
145
264
#define ON_BACKTRACK(op) ((op)+NUM_OPCODES)
147
266
jv jq_next(jq_state *jq) {
148
267
jv cfunc_input[MAX_CFUNCTION_ARGS];
269
jv_nomem_handler(jq->nomem_handler, jq->nomem_handler_data);
150
271
uint16_t* pc = stack_restore(jq);
156
277
uint16_t opcode = *pc;
158
279
if (jq->debug_trace_enabled) {
159
dump_operation(frame_current_bytecode(&jq->frame_stk), pc);
280
dump_operation(frame_current(jq)->bc, pc);
161
282
const struct opcode_description* opdesc = opcode_describe(opcode);
162
data_stk_elem* param = 0;
163
int stack_in = opdesc->stack_in;
164
if (stack_in == -1) stack_in = pc[1];
165
for (int i=0; i<stack_in; i++) {
167
param = forkable_stack_peek(&jq->data_stk);
170
param = forkable_stack_peek_next(&jq->data_stk, param);
285
int stack_in = opdesc->stack_in;
286
if (stack_in == -1) stack_in = pc[1];
287
for (int i=0; i<stack_in; i++) {
292
param = *stack_block_next(&jq->stk, param);
295
jv_dump(jv_copy(*(jv*)stack_block(&jq->stk, param)), 0);
296
//printf("<%d>", jv_get_refcnt(param->val));
298
//jv_dump(jv_copy(jq->path), 0);
173
jv_dump(jv_copy(param->val), 0);
174
//printf("<%d>", jv_get_refcnt(param->val));
176
//jv_dump(jv_copy(jq->path), 0);
301
printf("\t<backtracking>");
179
if (backtracking) printf("\t<backtracking>");
183
307
if (backtracking) {
184
308
opcode = ON_BACKTRACK(opcode);
185
309
backtracking = 0;
495
622
case CALL_BUILTIN: {
496
623
int nargs = *pc++;
497
624
jv top = stack_pop(jq);
498
cfunc_input[0] = top;
625
jv* in = cfunc_input;
499
627
for (int i = 1; i < nargs; i++) {
500
cfunc_input[i] = stack_pop(jq);
502
struct cfunction* func = &frame_current_bytecode(&jq->frame_stk)->globals->cfunctions[*pc++];
503
top = cfunction_invoke(func, cfunc_input);
628
in[i] = stack_pop(jq);
630
struct cfunction* function = &frame_current(jq)->bc->globals->cfunctions[*pc++];
631
typedef jv (*func_1)(jv);
632
typedef jv (*func_2)(jv,jv);
633
typedef jv (*func_3)(jv,jv,jv);
634
typedef jv (*func_4)(jv,jv,jv,jv);
635
typedef jv (*func_5)(jv,jv,jv,jv,jv);
636
switch (function->nargs) {
637
case 1: top = ((func_1)function->fptr)(in[0]); break;
638
case 2: top = ((func_2)function->fptr)(in[0], in[1]); break;
639
case 3: top = ((func_3)function->fptr)(in[0], in[1], in[2]); break;
640
case 4: top = ((func_4)function->fptr)(in[0], in[1], in[2], in[3]); break;
641
case 5: top = ((func_5)function->fptr)(in[0], in[1], in[2], in[3], in[4]); break;
642
default: return jv_invalid_with_msg(jv_string("Function takes too many arguments"));
504
645
if (jv_is_valid(top)) {
505
646
stack_push(jq, top);
648
print_error(jq, top);
508
649
goto do_backtrack;
655
jv input = stack_pop(jq);
514
656
uint16_t nclosures = *pc++;
515
657
uint16_t* retaddr = pc + 2 + nclosures*2;
516
frame_ptr new_frame = frame_push(&jq->frame_stk,
517
make_closure(&jq->frame_stk, frame_current(&jq->frame_stk), pc),
520
frame_ptr old_frame = forkable_stack_peek_next(&jq->frame_stk, new_frame);
521
assert(nclosures == frame_self(new_frame)->bc->nclosures);
522
for (int i=0; i<nclosures; i++) {
523
*frame_closure_arg(new_frame, i) = make_closure(&jq->frame_stk, old_frame, pc);
527
pc = frame_current_bytecode(&jq->frame_stk)->code;
658
struct frame* new_frame = frame_push(jq, make_closure(jq, pc),
660
new_frame->retdata = jq->stk_top;
661
new_frame->retaddr = retaddr;
662
pc = new_frame->bc->code;
663
stack_push(jq, input);
532
uint16_t* retaddr = *frame_current_retaddr(&jq->frame_stk);
668
jv value = stack_pop(jq);
669
assert(jq->stk_top == frame_current(jq)->retdata);
670
uint16_t* retaddr = frame_current(jq)->retaddr;
534
672
// function return
536
frame_pop(&jq->frame_stk);
538
676
// top-level return, yielding value
539
jv value = stack_pop(jq);
540
stack_save(jq, pc - 1);
677
struct stack_pos spos = stack_get_pos(jq);
541
678
stack_push(jq, jv_null());
679
stack_save(jq, pc - 1, spos);
682
stack_push(jq, value);
547
685
case ON_BACKTRACK(RET): {
556
void jq_init(struct bytecode* bc, jv input, jq_state **jq, int flags) {
558
new_jq = jv_mem_alloc(sizeof(*new_jq));
559
memset(new_jq, 0, sizeof(*new_jq));
560
new_jq->path = jv_null();
561
forkable_stack_init(&new_jq->data_stk, sizeof(data_stk_elem) * 100);
562
forkable_stack_init(&new_jq->frame_stk, 1024);
563
forkable_stack_init(&new_jq->fork_stk, 1024);
693
jq_state *jq_init(void) {
695
jq = jv_mem_alloc_unguarded(sizeof(*jq));
701
stack_init(&jq->stk);
707
jq->err_cb_data = NULL;
709
jq->path = jv_null();
713
void jq_set_error_cb(jq_state *jq, jq_err_cb cb, void *data) {
715
jq->err_cb_data = data;
718
void jq_get_error_cb(jq_state *jq, jq_err_cb *cb, void **data) {
720
*data = jq->err_cb_data;
723
void jq_set_nomem_handler(jq_state *jq, void (*nomem_handler)(void *), void *data) {
724
jv_nomem_handler(nomem_handler, data);
725
jq->nomem_handler = nomem_handler;
726
jq->nomem_handler_data = data;
730
void jq_start(jq_state *jq, jv input, int flags) {
731
jv_nomem_handler(jq->nomem_handler, jq->nomem_handler_data);
565
stack_push(new_jq, input);
566
struct closure top = {bc, -1};
567
frame_push(&new_jq->frame_stk, top, 0);
568
stack_save(new_jq, bc->code);
569
stack_switch(new_jq);
734
struct closure top = {jq->bc, -1};
735
struct frame* top_frame = frame_push(jq, top, 0, 0);
736
top_frame->retdata = 0;
737
top_frame->retaddr = 0;
739
stack_push(jq, input);
740
stack_save(jq, jq->bc->code, stack_get_pos(jq));
570
741
if (flags & JQ_DEBUG_TRACE) {
571
new_jq->debug_trace_enabled = 1;
742
jq->debug_trace_enabled = 1;
573
new_jq->debug_trace_enabled = 0;
744
jq->debug_trace_enabled = 0;
575
new_jq->initial_execution = 1;
746
jq->initial_execution = 1;
579
749
void jq_teardown(jq_state **jq) {
585
while (stack_restore(old_jq)) {}
587
assert(forkable_stack_empty(&old_jq->fork_stk));
588
assert(forkable_stack_empty(&old_jq->data_stk));
589
assert(forkable_stack_empty(&old_jq->frame_stk));
590
forkable_stack_free(&old_jq->fork_stk);
591
forkable_stack_free(&old_jq->data_stk);
592
forkable_stack_free(&old_jq->frame_stk);
594
jv_free(old_jq->path);
756
bytecode_free(old_jq->bc);
595
759
jv_mem_free(old_jq);
598
struct bytecode* jq_compile_args(const char* str, jv args) {
762
int jq_compile_args(jq_state *jq, const char* str, jv args) {
763
jv_nomem_handler(jq->nomem_handler, jq->nomem_handler_data);
599
764
assert(jv_get_kind(args) == JV_KIND_ARRAY);
600
765
struct locfile locations;
601
locfile_init(&locations, str, strlen(str));
766
locfile_init(&locations, jq, str, strlen(str));
603
struct bytecode* bc = 0;
770
bytecode_free(jq->bc);
604
773
int nerrors = jq_parse(&locations, &program);
605
774
if (nerrors == 0) {
606
775
for (int i=0; i<jv_array_length(jv_copy(args)); i++) {
610
779
program = gen_var_binding(gen_const(value), jv_string_value(name), program);
614
program = builtins_bind(program);
615
nerrors = block_compile(program, &locations, &bc);
782
nerrors = builtins_bind(jq, &program);
784
nerrors = block_compile(program, &locations, &jq->bc);
618
fprintf(stderr, "%d compile %s\n", nerrors, nerrors > 1 ? "errors" : "error");
789
jv s = jv_string_fmt("%d compile %s", nerrors,
790
nerrors > 1 ? "errors" : "error");
791
if (jq->err_cb != NULL)
792
jq->err_cb(jq->err_cb_data, s);
793
else if (!jv_is_valid(s))
794
fprintf(stderr, "Error formatting jq compilation errors: %s\n", strerror(errno));
796
fprintf(stderr, "%s\n", jv_string_value(s));
620
799
locfile_free(&locations);
624
struct bytecode* jq_compile(const char* str) {
625
return jq_compile_args(str, jv_array());
800
return jq->bc != NULL;
803
int jq_compile(jq_state *jq, const char* str) {
804
return jq_compile_args(jq, str, jv_array());
807
void jq_dump_disassembly(jq_state *jq, int indent) {
808
dump_disassembly(indent, jq->bc);