ref: e72acf740bac36235c309304c1f6f0da619ed768
parent: 300cfb6caacb43cbe98e61d2d0e4e071fb507f75
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Mon Feb 17 22:09:01 EST 2025
apply_cl: local sp
--- a/src/flisp.c
+++ b/src/flisp.c
@@ -839,21 +839,21 @@
static value_t
apply_cl(int nargs)
{
- value_t *top_frame = FL(curr_frame);
+ value_t *top_frame = FL(curr_frame), *sp = FL(sp);
bool tail;
int n;
apply_cl_top:;
- value_t *bp = FL(sp)-nargs;
+ value_t *bp = sp-nargs;
function_t *fn = (function_t*)ptr(bp[-1]);
const uint8_t *ip = cvalue_data(fn->bcode);
assert(!ismanaged((uintptr_t)ip));
- PUSH(fn->env);
- PUSH((value_t)FL(curr_frame));
- PUSH(nargs);
- value_t *ipd = FL(sp)++;
- FL(curr_frame) = FL(sp);
+ *sp++ = fn->env;
+ *sp++ = (value_t)FL(curr_frame);
+ *sp++ = nargs;
+ value_t *ipd = sp++;
+ FL(curr_frame) = sp;
#if defined(COMPUTED_GOTO)
#pragma GCC diagnostic push
--- a/src/vm.inc
+++ b/src/vm.inc
@@ -7,7 +7,7 @@
)
OP(OP_LOADA0)
- PUSH(*bp);
+ *sp++ = *bp;
NEXT_OP;
OP(OP_CALL) {
@@ -29,14 +29,14 @@
}
LABEL(do_call):
*ipd = (uintptr_t)ip;
- value_t v = FL(sp)[-n-1];
+ value_t v = sp[-n-1];
if(tag(v) == TAG_FUNCTION){
if(v > (N_BUILTINS<<3)){
if(tail){
FL(curr_frame) = (value_t*)FL(curr_frame)[-3];
for(fixnum_t s = -1; s < (fixnum_t)n; s++)
- bp[s] = FL(sp)[s-n];
- FL(sp) = bp+n;
+ bp[s] = sp[s-n];
+ sp = bp+n;
}
nargs = n;
goto apply_cl_top;
@@ -44,14 +44,17 @@
int i = uintval(v);
assert(isbuiltin(v));
fixnum_t s = builtins[i].nargs;
- if(s >= 0)
+ if(s >= 0){
+ FL(sp) = sp;
argcount(n, s);
- else if(s != ANYARGS && n < -s)
+ }else if(s != ANYARGS && n < -s){
+ FL(sp) = sp;
argcount(n, -s);
+ }
// remove function arg
- for(value_t *p = FL(sp)-n-1; p < FL(sp)-1; p++)
+ for(value_t *p = sp-n-1; p < sp-1; p++)
p[0] = p[1];
- FL(sp)--;
+ sp--;
switch(i){
case OP_VECTOR: goto LABEL(apply_vector);
case OP_ADD: goto LABEL(apply_add);
@@ -73,12 +76,14 @@
#endif
}
}else if(fl_likely(iscbuiltin(v))){
- value_t *p = FL(sp) - n;
+ value_t *p = sp - n;
+ FL(sp) = sp;
v = ((cvalue_t*)ptr(v))->cbuiltin(p, n);
- FL(sp) = p;
+ sp = p;
p[-1] = v;
NEXT_OP;
}
+ FL(sp) = sp;
type_error("function", v);
}
@@ -91,6 +96,7 @@
}
if(fl_unlikely(nargs != na)){
*ipd = (uintptr_t)ip;
+ FL(sp) = sp;
arity_error(nargs, na);
}
NEXT_OP;
@@ -97,34 +103,36 @@
}
OP(OP_LOADA1)
- PUSH(bp[1]);
+ *sp++ = bp[1];
NEXT_OP;
OP(OP_RET) {
- value_t v = POP();
- FL(sp) = FL(curr_frame);
- FL(curr_frame) = (value_t*)FL(sp)[-3];
- if(FL(curr_frame) == top_frame)
+ value_t v = *(--sp);
+ sp = FL(curr_frame);
+ FL(curr_frame) = (value_t*)sp[-3];
+ if(FL(curr_frame) == top_frame){
+ FL(sp) = sp;
return v;
- FL(sp) -= 4+nargs;
+ }
+ sp -= 4+nargs;
ipd = FL(curr_frame)-1;
ip = (uint8_t*)*ipd;
nargs = FL(curr_frame)[-2];
bp = FL(curr_frame) - 4 - nargs;
- FL(sp)[-1] = v;
+ sp[-1] = v;
NEXT_OP;
}
OP(OP_LOAD1)
- PUSH(fixnum(1));
+ *sp++ = fixnum(1);
NEXT_OP;
OP(OP_LOADA)
- PUSH(bp[*ip++]);
+ *sp++ = bp[*ip++];
NEXT_OP;
OP(OP_BRN)
- ip += POP() == FL_nil ? GET_INT16(ip) : 2;
+ ip += *(--sp) == FL_nil ? GET_INT16(ip) : 2;
NEXT_OP;
OP(OP_LOADG) {
@@ -142,9 +150,10 @@
symbol_t *sym = ptr(v);
if(fl_unlikely(sym->binding == UNBOUND)){
*ipd = (uintptr_t)ip;
+ FL(sp) = sp;
unbound_error(v);
}
- PUSH(sym->binding);
+ *sp++ = sym->binding;
NEXT_OP;
}
@@ -153,10 +162,10 @@
LABEL(apply_lt):
{
int i = n;
- value_t a = FL(sp)[-i], b, v;
+ value_t a = sp[-i], b, v;
for(v = FL_t; i > 1; a = b){
i--;
- b = FL(sp)[-i];
+ b = sp[-i];
if(bothfixnums(a, b)){
if((fixnum_t)a >= (fixnum_t)b){
v = FL_nil;
@@ -172,8 +181,8 @@
}
}
}
- POPN(n);
- PUSH(v);
+ sp -= n;
+ *sp++ = v;
}
NEXT_OP;
@@ -180,7 +189,7 @@
OP(OP_LOADV) {
value_t v = fn_vals(bp[-1]);
assert(*ip < vector_size(v));
- PUSH(vector_elt(v, *ip++));
+ *sp++ = vector_elt(v, *ip++);
NEXT_OP;
}
@@ -192,52 +201,53 @@
OP(OP_SUB2)
LABEL(do_sub2):
*ipd = (uintptr_t)ip;
- v = FL(sp)[-1];
+ v = sp[-1];
int64_t i64;
- FL(sp)[-1] = isfixnum(v) ? fixnum_neg(v) : fl_neg(v);
+ sp[-1] = isfixnum(v) ? fixnum_neg(v) : fl_neg(v);
}
value_t a, b, q;
- a = FL(sp)[-2];
- b = FL(sp)[-1];
+ a = sp[-2];
+ b = sp[-1];
if(bothfixnums(a, b) && !sadd_overflow(numval(a), numval(b), &q) && fits_fixnum(q))
v = fixnum(q);
else
- v = fl_add_any(FL(sp)-2, 2);
- POPN(1);
- FL(sp)[-1] = v;
+ v = fl_add_any(sp-2, 2);
+ sp--;
+ sp[-1] = v;
NEXT_OP;
}
OP(OP_LOADI8)
- PUSH(fixnum((int8_t)*ip++));
+ *sp++ = fixnum((int8_t)*ip++);
NEXT_OP;
OP(OP_POP)
- POPN(1);
+ sp--;
NEXT_OP;
OP(OP_BRNN)
- ip += POP() != FL_nil ? GET_INT16(ip) : 2;
+ ip += *(--sp) != FL_nil ? GET_INT16(ip) : 2;
NEXT_OP;
OP(OP_DUP)
- FL(sp)[0] = FL(sp)[-1];
- FL(sp)++;
+ sp[0] = sp[-1];
+ sp++;
NEXT_OP;
OP(OP_LOADC0)
- PUSH(vector_elt(bp[nargs], 0));
+ *sp++ = vector_elt(bp[nargs], 0);
NEXT_OP;
OP(OP_CAR) {
- value_t v = FL(sp)[-1];
+ value_t v = sp[-1];
if(fl_likely(iscons(v)))
v = car_(v);
else if(fl_unlikely(v != FL_nil)){
*ipd = (uintptr_t)ip;
+ FL(sp) = sp;
type_error("cons", v);
}
- FL(sp)[-1] = v;
+ sp[-1] = v;
NEXT_OP;
}
@@ -244,6 +254,7 @@
OP(OP_CLOSURE) {
int x = *ip++;
assert(x > 0);
+ FL(sp) = sp;
value_t *pv = alloc_words(
1+x+
#if !defined(BITS64)
@@ -253,64 +264,68 @@
value_t v = tagptr(pv, TAG_VECTOR);
*pv++ = fixnum(x);
for(int i = 0; i < x; i++)
- *pv++ = FL(sp)[-x+i];
- POPN(x);
- PUSH(v);
+ *pv++ = sp[-x+i];
+ sp -= x;
+ *sp++ = v;
#if !defined(BITS64)
if((x & 1) == 0)
pv++;
#endif
function_t *f = (function_t*)pv;
- value_t e = FL(sp)[-2]; // closure to copy
+ value_t e = sp[-2]; // closure to copy
assert(isfunction(e));
f->bcode = fn_bcode(e);
f->vals = fn_vals(e);
- f->env = FL(sp)[-1];
+ f->env = sp[-1];
f->name = fn_name(e);
- POPN(1);
- FL(sp)[-1] = tagptr(f, TAG_FUNCTION);
+ sp--;
+ sp[-1] = tagptr(f, TAG_FUNCTION);
NEXT_OP;
}
OP(OP_CONS) {
- if(FL(curheap) > FL(lim))
+ if(FL(curheap) > FL(lim)){
+ FL(sp) = sp;
fl_gc(0);
+ sp = FL(sp);
+ }
cons_t *c = (cons_t*)FL(curheap);
FL(curheap) += sizeof(cons_t);
- c->car = FL(sp)[-2];
- c->cdr = FL(sp)[-1];
- FL(sp)[-2] = tagptr(c, TAG_CONS);
- POPN(1);
+ c->car = sp[-2];
+ c->cdr = sp[-1];
+ sp[-2] = tagptr(c, TAG_CONS);
+ sp--;
NEXT_OP;
}
OP(OP_BRNE)
- ip += FL(sp)[-2] != FL(sp)[-1] ? GET_INT16(ip) : 2;
- POPN(2);
+ ip += sp[-2] != sp[-1] ? GET_INT16(ip) : 2;
+ sp -= 2;
NEXT_OP;
OP(OP_CDR) {
- value_t v = FL(sp)[-1];
+ value_t v = sp[-1];
if(fl_likely(iscons(v)))
v = cdr_(v);
else if(fl_unlikely(v != FL_nil)){
*ipd = (uintptr_t)ip;
+ FL(sp) = sp;
type_error("cons", v);
}
- FL(sp)[-1] = v;
+ sp[-1] = v;
NEXT_OP;
}
OP(OP_LOADVOID)
- PUSH(FL_void);
+ *sp++ = FL_void;
NEXT_OP;
OP(OP_NOT)
- FL(sp)[-1] = FL(sp)[-1] == FL_nil ? FL_t : FL_nil;
+ sp[-1] = sp[-1] == FL_nil ? FL_t : FL_nil;
NEXT_OP;
OP(OP_SETA)
- bp[*ip++] = FL(sp)[-1];
+ bp[*ip++] = sp[-1];
NEXT_OP;
OP(OP_VARGC) {
@@ -329,21 +344,22 @@
bp[i+2] = bp[nargs+1];
bp[i+3] = i+1;
bp[i+4] = 0;
- FL(sp) = bp+i+5;
- FL(curr_frame) = FL(sp);
+ sp = bp+i+5;
+ FL(curr_frame) = sp;
}
}else if(fl_unlikely(s < 0)){
*ipd = (uintptr_t)ip;
+ FL(sp) = sp;
lerrorf(FL_ArgError, "too few arguments");
}else{
- FL(sp)++;
- FL(sp)[-2] = i+1;
- FL(sp)[-3] = FL(sp)[-4];
- FL(sp)[-4] = FL(sp)[-5];
- FL(sp)[-5] = FL_nil;
- FL(curr_frame) = FL(sp);
+ sp++;
+ sp[-2] = i+1;
+ sp[-3] = sp[-4];
+ sp[-4] = sp[-5];
+ sp[-5] = FL_nil;
+ FL(curr_frame) = sp;
}
- ipd = FL(sp)-1;
+ ipd = sp-1;
nargs = i+1;
NEXT_OP;
}
@@ -350,28 +366,30 @@
OP(OP_SHIFT) {
int i = *ip++;
- FL(sp)[-1-i] = FL(sp)[-1];
- FL(sp) -= i;
+ sp[-1-i] = sp[-1];
+ sp -= i;
NEXT_OP;
}
OP(OP_SETCAR) {
- value_t v = FL(sp)[-2];
+ value_t v = sp[-2];
if(fl_unlikely(!iscons(v))){
*ipd = (uintptr_t)ip;
+ FL(sp) = sp;
type_error("cons", v);
}
- car_(v) = FL(sp)[-1];
- POPN(1);
+ car_(v) = sp[-1];
+ sp--;
NEXT_OP;
}
OP(OP_LOADNIL)
- PUSH(FL_nil);
+ *sp++ = FL_nil;
NEXT_OP;
OP(OP_BOX) {
int i = *ip++;
+ FL(sp) = sp;
value_t v = mk_cons();
car_(v) = bp[i];
cdr_(v) = FL_nil;
@@ -384,7 +402,7 @@
NEXT_OP;
OP(OP_ATOMP)
- FL(sp)[-1] = iscons(FL(sp)[-1]) ? FL_nil : FL_t;
+ sp[-1] = iscons(sp[-1]) ? FL_nil : FL_t;
NEXT_OP;
OP(OP_AREF2) {
@@ -395,23 +413,27 @@
n = 3 + *ip++;
}
LABEL(apply_aref):;
- value_t v = FL(sp)[-n];
+ value_t v = sp[-n];
for(int i = n-1; i > 0; i--){
if(isarray(v)){
- FL(sp)[-i-1] = v;
- v = cvalue_array_aref(FL(sp)-i-1);
+ sp[-i-1] = v;
+ v = cvalue_array_aref(sp-i-1);
continue;
}
- value_t e = FL(sp)[-i];
+ value_t e = sp[-i];
size_t isz = tosize(e);
if(isvector(v)){
- if(fl_unlikely(isz >= vector_size(v)))
+ if(fl_unlikely(isz >= vector_size(v))){
+ FL(sp) = sp;
bounds_error(v, e);
+ }
v = vector_elt(v, isz);
continue;
}
- if(!iscons(v) && v != FL_nil)
+ if(!iscons(v) && v != FL_nil){
+ FL(sp) = sp;
type_error("sequence", v);
+ }
for(value_t v0 = v;; isz--){
if(isz == 0){
v = car_(v);
@@ -418,17 +440,19 @@
break;
}
v = cdr_(v);
- if(fl_unlikely(!iscons(v)))
+ if(fl_unlikely(!iscons(v))){
+ FL(sp) = sp;
bounds_error(v0, e);
+ }
}
}
- POPN(n);
- PUSH(v);
+ sp -= n;
+ *sp++ = v;
NEXT_OP;
}
OP(OP_NANP) {
- value_t v = FL(sp)[-1];
+ value_t v = sp[-1];
if(!iscprim(v))
v = FL_nil;
else{
@@ -445,52 +469,57 @@
break;
}
}
- FL(sp)[-1] = v;
+ sp[-1] = v;
NEXT_OP;
}
OP(OP_LOAD0)
- PUSH(fixnum(0));
+ *sp++ = fixnum(0);
NEXT_OP;
OP(OP_SETCDR) {
- value_t v = FL(sp)[-2];
+ value_t v = sp[-2];
if(fl_unlikely(!iscons(v))){
*ipd = (uintptr_t)ip;
+ FL(sp) = sp;
type_error("cons", v);
}
- cdr_(v) = FL(sp)[-1];
- POPN(1);
+ cdr_(v) = sp[-1];
+ sp--;
NEXT_OP;
}
OP(OP_LOADC1)
- PUSH(vector_elt(bp[nargs], 1));
+ *sp++ = vector_elt(bp[nargs], 1);
NEXT_OP;
OP(OP_ASET) {
*ipd = (uintptr_t)ip;
- value_t v = FL(sp)[-3];
+ value_t v = sp[-3];
n = 3;
if(0){
LABEL(apply_aset):
- v = FL(sp)[-n];
+ v = sp[-n];
for(int i = n-1; i >= 3; i--){
if(isarray(v)){
- FL(sp)[-i-1] = v;
- v = cvalue_array_aref(FL(sp)-i-1);
+ sp[-i-1] = v;
+ v = cvalue_array_aref(sp-i-1);
continue;
}
- value_t e = FL(sp)[-i];
+ value_t e = sp[-i];
size_t isz = tosize(e);
if(isvector(v)){
- if(fl_unlikely(isz >= vector_size(v)))
+ if(fl_unlikely(isz >= vector_size(v))){
+ FL(sp) = sp;
bounds_error(v, e);
+ }
v = vector_elt(v, isz);
continue;
}
- if(!iscons(v) && v != FL_nil)
+ if(!iscons(v) && v != FL_nil){
+ FL(sp) = sp;
type_error("sequence", v);
+ }
for(value_t v0 = v;; isz--){
if(isz == 0){
v = car_(v);
@@ -497,51 +526,58 @@
break;
}
v = cdr_(v);
- if(fl_unlikely(!iscons(v)))
+ if(fl_unlikely(!iscons(v))){
+ FL(sp) = sp;
bounds_error(v0, e);
+ }
}
}
- FL(sp)[-3] = v;
+ sp[-3] = v;
}
- value_t e = FL(sp)[-2];
+ value_t e = sp[-2];
size_t isz = tosize(e);
if(isvector(v)){
- if(fl_unlikely(isz >= vector_size(v)))
+ if(fl_unlikely(isz >= vector_size(v))){
+ FL(sp) = sp;
bounds_error(v, e);
- vector_elt(v, isz) = (e = FL(sp)[-1]);
+ }
+ vector_elt(v, isz) = (e = sp[-1]);
}else if(iscons(v) || v == FL_nil){
for(value_t v0 = v;; isz--){
if(isz == 0){
- car_(v) = (e = FL(sp)[-1]);
+ car_(v) = (e = sp[-1]);
break;
}
v = cdr_(v);
- if(fl_unlikely(!iscons(v)))
+ if(fl_unlikely(!iscons(v))){
+ FL(sp) = sp;
bounds_error(v0, e);
+ }
}
}else if(isarray(v)){
- e = cvalue_array_aset(FL(sp)-3);
+ e = cvalue_array_aset(sp-3);
}else{
+ FL(sp) = sp;
type_error("sequence", v);
}
- POPN(n);
- PUSH(e);
+ sp -= n;
+ *sp++ = e;
NEXT_OP;
}
OP(OP_EQUAL) {
value_t v;
- if(FL(sp)[-2] == FL(sp)[-1])
+ if(sp[-2] == sp[-1])
v = FL_t;
else
- v = fl_compare(FL(sp)[-2], FL(sp)[-1], true) == 0 ? FL_t : FL_nil;
- FL(sp)[-2] = v;
- POPN(1);
+ v = fl_compare(sp[-2], sp[-1], true) == 0 ? FL_t : FL_nil;
+ sp[-2] = v;
+ sp--;
NEXT_OP;
}
OP(OP_CONSP)
- FL(sp)[-1] = iscons(FL(sp)[-1]) ? FL_t : FL_nil;
+ sp[-1] = iscons(sp[-1]) ? FL_t : FL_nil;
NEXT_OP;
OP(OP_LOADC) {
@@ -549,20 +585,20 @@
int i = *ip++;
assert(isvector(v));
assert(i < (int)vector_size(v));
- PUSH(vector_elt(v, i));
+ *sp++ = vector_elt(v, i);
NEXT_OP;
}
OP(OP_SYMBOLP)
- FL(sp)[-1] = issymbol(FL(sp)[-1]) ? FL_t : FL_nil;
+ sp[-1] = issymbol(sp[-1]) ? FL_t : FL_nil;
NEXT_OP;
OP(OP_NUMBERP)
- FL(sp)[-1] = fl_isnumber(FL(sp)[-1]) ? FL_t : FL_nil;
+ sp[-1] = fl_isnumber(sp[-1]) ? FL_t : FL_nil;
NEXT_OP;
OP(OP_BRBOUND)
- PUSH(bp[GET_INT32(ip)] != UNBOUND ? FL_t : FL_nil);
+ *sp++ = bp[GET_INT32(ip)] != UNBOUND ? FL_t : FL_nil;
ip += 4;
NEXT_OP;
@@ -573,11 +609,13 @@
ip += 4;
if(fl_unlikely(nargs < i)){
*ipd = (uintptr_t)ip;
+ FL(sp) = sp;
lerrorf(FL_ArgError, "too few arguments");
}
if(x > 0){
if(fl_unlikely(nargs > x)){
*ipd = (uintptr_t)ip;
+ FL(sp) = sp;
lerrorf(FL_ArgError, "too many arguments");
}
}else
@@ -584,13 +622,13 @@
x = -x;
if(fl_likely(x > nargs)){
x -= nargs;
- FL(sp) += x;
- FL(sp)[-1] = FL(sp)[-x-1];
- FL(sp)[-2] = nargs+x;
- FL(sp)[-3] = FL(sp)[-x-3];
- FL(sp)[-4] = FL(sp)[-x-4];
- FL(curr_frame) = FL(sp);
- ipd = FL(sp)-1;
+ sp += x;
+ sp[-1] = sp[-x-1];
+ sp[-2] = nargs+x;
+ sp[-3] = sp[-x-3];
+ sp[-4] = sp[-x-4];
+ FL(curr_frame) = sp;
+ ipd = sp-1;
for(i = 0; i < x; i++)
bp[nargs+i] = UNBOUND;
nargs += x;
@@ -599,55 +637,56 @@
}
OP(OP_EQ)
- FL(sp)[-2] = FL(sp)[-2] == FL(sp)[-1] ? FL_t : FL_nil;
- POPN(1);
+ sp[-2] = sp[-2] == sp[-1] ? FL_t : FL_nil;
+ sp--;
NEXT_OP;
-OP(OP_LIST)
+OP(OP_LIST) {
n = *ip++;
-LABEL(apply_list):
+LABEL(apply_list):;
+ value_t v;
if(n > 0){
- value_t v = list(FL(sp)-n, n, 0);
- POPN(n);
- PUSH(v);
+ v = list(sp-n, n, 0);
+ sp -= n;
}else{
- PUSH(FL_nil);
+ v = FL_nil;
}
+ *sp++ = v;
NEXT_OP;
+}
OP(OP_BOUNDP) {
*ipd = (uintptr_t)ip;
- symbol_t *sym = tosymbol(FL(sp)[-1]);
- FL(sp)[-1] = sym->binding == UNBOUND ? FL_nil : FL_t;
+ symbol_t *sym = tosymbol(sp[-1]);
+ sp[-1] = sym->binding == UNBOUND ? FL_nil : FL_t;
NEXT_OP;
}
-OP(OP_NUMEQ)
+OP(OP_NUMEQ) {
n = *ip++;
-LABEL(apply_numeq):
- {
- int i = n;
- value_t a = FL(sp)[-i], b, v;
- for(v = FL_t; i > 1; a = b){
- i--;
- b = FL(sp)[-i];
- if(bothfixnums(a, b)){
- if(a != b){
- v = FL_nil;
- break;
- }
- }else if(numeric_compare(a, b, true, false, true) != 0){
+LABEL(apply_numeq):;
+ int i = n;
+ value_t a = sp[-i], b, v;
+ for(v = FL_t; i > 1; a = b){
+ i--;
+ b = sp[-i];
+ if(bothfixnums(a, b)){
+ if(a != b){
v = FL_nil;
break;
}
+ }else if(numeric_compare(a, b, true, false, true) != 0){
+ v = FL_nil;
+ break;
}
- POPN(n);
- PUSH(v);
}
+ sp -= n;
+ *sp++ = v;
NEXT_OP;
+}
OP(OP_CADR) {
- value_t v = FL(sp)[-1];
+ value_t v = sp[-1];
if(fl_likely(iscons(v))){
v = cdr_(v);
if(fl_likely(iscons(v)))
@@ -658,10 +697,11 @@
LABEL(cadr_nil):
if(fl_unlikely(v != FL_nil)){
*ipd = (uintptr_t)ip;
+ FL(sp) = sp;
type_error("cons", v);
}
}
- FL(sp)[-1] = v;
+ sp[-1] = v;
NEXT_OP;
}
@@ -673,27 +713,28 @@
}
n = *ip++;
LABEL(apply_apply):;
- value_t v = POP(); // arglist
- value_t *p = FL(sp)-(n-2); // n-2 == # leading arguments not in the list
+ value_t v = *(--sp); // arglist
+ value_t *p = sp-(n-2); // n-2 == # leading arguments not in the list
while(iscons(v)){
- PUSH(car_(v));
+ *sp++ = car_(v);
v = cdr_(v);
}
if(v != FL_nil){
*ipd = (uintptr_t)ip;
+ FL(sp) = sp;
lerrorf(FL_ArgError, "apply: last argument: not a list");
}
- n = FL(sp)-p;
+ n = sp-p;
goto LABEL(do_call);
}
OP(OP_LOADT)
- PUSH(FL_t);
+ *sp++ = FL_t;
NEXT_OP;
OP(OP_BUILTINP) {
- value_t v = FL(sp)[-1];
- FL(sp)[-1] = (isbuiltin(v) || iscbuiltin(v)) ? FL_t : FL_nil;
+ value_t v = sp[-1];
+ sp[-1] = (isbuiltin(v) || iscbuiltin(v)) ? FL_t : FL_nil;
NEXT_OP;
}
@@ -700,14 +741,14 @@
OP(OP_NEG) {
LABEL(do_neg):
*ipd = (uintptr_t)ip;
- value_t v = FL(sp)[-1];
+ value_t v = sp[-1];
int64_t i64;
- FL(sp)[-1] = isfixnum(v) ? fixnum_neg(v) : fl_neg(v);
+ sp[-1] = isfixnum(v) ? fixnum_neg(v) : fl_neg(v);
NEXT_OP;
}
OP(OP_FIXNUMP)
- FL(sp)[-1] = isfixnum(FL(sp)[-1]) ? FL_t : FL_nil;
+ sp[-1] = isfixnum(sp[-1]) ? FL_t : FL_nil;
NEXT_OP;
OP(OP_MUL) {
@@ -714,19 +755,20 @@
n = *ip++;
LABEL(apply_mul):
*ipd = (uintptr_t)ip;
- value_t v = fl_mul_any(FL(sp)-n, n);
- POPN(n);
- PUSH(v);
+ value_t v = fl_mul_any(sp-n, n);
+ sp -= n;
+ *sp++ = v;
NEXT_OP;
}
OP(OP_IDIV) {
- value_t a = FL(sp)[-2];
- value_t b = FL(sp)[-1];
+ value_t a = sp[-2];
+ value_t b = sp[-1];
value_t v;
if(bothfixnums(a, b)){
if(b == 0){
*ipd = (uintptr_t)ip;
+ FL(sp) = sp;
divide_by_0_error();
}
v = fixnum(numval(a) / numval(b));
@@ -734,8 +776,8 @@
*ipd = (uintptr_t)ip;
v = fl_idiv2(a, b);
}
- POPN(1);
- FL(sp)[-1] = v;
+ sp--;
+ sp[-1] = v;
NEXT_OP;
}
@@ -743,19 +785,19 @@
n = *ip++;
LABEL(apply_div):
*ipd = (uintptr_t)ip;
- value_t *p = FL(sp)-n;
+ value_t *p = sp-n;
if(n == 1){
- FL(sp)[-1] = fl_div2(fixnum(1), *p);
+ sp[-1] = fl_div2(fixnum(1), *p);
}else{
if(fl_unlikely(n > 2)){
- PUSH(*p);
+ *sp++ = *p;
*p = fixnum(1);
p[1] = fl_mul_any(p, n);
- *p = POP();
+ *p = *(--sp);
}
value_t v = fl_div2(p[0], p[1]);
- POPN(n);
- PUSH(v);
+ sp -= n;
+ *sp++ = v;
}
NEXT_OP;
}
@@ -763,33 +805,35 @@
OP(OP_VECTOR) {
n = *ip++;
LABEL(apply_vector):;
+ FL(sp) = sp;
value_t v = alloc_vector(n, 0);
if(n){
- memcpy(&vector_elt(v, 0), FL(sp)-n, n*sizeof(value_t));
- POPN(n);
+ memcpy(&vector_elt(v, 0), sp-n, n*sizeof(value_t));
+ sp -= n;
}
- PUSH(v);
+ *sp++ = v;
NEXT_OP;
}
OP(OP_COMPARE)
- FL(sp)[-2] = fl_compare(FL(sp)[-2], FL(sp)[-1], false);
- POPN(1);
+ sp[-2] = fl_compare(sp[-2], sp[-1], false);
+ sp--;
NEXT_OP;
OP(OP_FOR) {
*ipd = (uintptr_t)ip;
- value_t *p = FL(sp);
+ value_t *p = sp;
value_t v;
fixnum_t s = tofixnum(p[-3]);
fixnum_t hi = tofixnum(p[-2]);
- FL(sp) += 2;
+ sp += 2;
+ FL(sp) = sp;
for(v = FL_void; s <= hi; s++){
p[0] = p[-1];
p[1] = fixnum(s);
v = _applyn(1);
}
- POPN(4);
+ sp -= 4;
p[1] = v;
NEXT_OP;
}
@@ -806,7 +850,7 @@
v = vector_elt(v, i);
assert(issymbol(v));
symbol_t *sym = ptr(v);
- v = FL(sp)[-1];
+ v = sp[-1];
if(!isconstant(sym))
sym->binding = v;
NEXT_OP;
@@ -813,14 +857,15 @@
}
OP(OP_VECTORP)
- FL(sp)[-1] = isvector(FL(sp)[-1]) ? FL_t : FL_nil;
+ sp[-1] = isvector(sp[-1]) ? FL_t : FL_nil;
NEXT_OP;
OP(OP_TRYCATCH) {
*ipd = (uintptr_t)ip;
+ FL(sp) = sp;
value_t v = do_trycatch();
- POPN(1);
- FL(sp)[-1] = v;
+ sp--;
+ sp[-1] = v;
NEXT_OP;
}
@@ -830,28 +875,28 @@
goto LABEL(do_add2);
LABEL(apply_add):
*ipd = (uintptr_t)ip;
- value_t v = fl_add_any(FL(sp)-n, n);
- POPN(n);
- PUSH(v);
+ value_t v = fl_add_any(sp-n, n);
+ sp -= n;
+ *sp++ = v;
NEXT_OP;
}
OP(OP_LOADAL)
assert(nargs > 0);
- PUSH(bp[GET_INT32(ip)]);
+ *sp++ = bp[GET_INT32(ip)];
ip += 4;
NEXT_OP;
OP(OP_EQV) {
value_t v;
- if(FL(sp)[-2] == FL(sp)[-1])
+ if(sp[-2] == sp[-1])
v = FL_t;
- else if(!leafp(FL(sp)[-2]) || !leafp(FL(sp)[-1]))
+ else if(!leafp(sp[-2]) || !leafp(sp[-1]))
v = FL_nil;
else
- v = fl_compare(FL(sp)[-2], FL(sp)[-1], true) == 0 ? FL_t : FL_nil;
- FL(sp)[-2] = v;
- POPN(1);
+ v = fl_compare(sp[-2], sp[-1], true) == 0 ? FL_t : FL_nil;
+ sp[-2] = v;
+ sp--;
NEXT_OP;
}
@@ -865,8 +910,10 @@
fixnum_t s = GET_INT32(ip);
ip += 4;
*ipd = (uintptr_t)ip;
+ FL(sp) = sp;
nargs = process_keys(v, i, x, labs(s)-(i+x), bp, nargs, s<0);
- ipd = FL(sp)-1;
+ sp = FL(sp);
+ ipd = sp-1;
NEXT_OP;
}
@@ -878,27 +925,27 @@
if(n == 1)
goto LABEL(do_neg);
*ipd = (uintptr_t)ip;
- value_t *p = FL(sp)-n;
+ value_t *p = sp-n;
// we need to pass the full arglist on to fl_add_any
// so it can handle rest args properly
- PUSH(*p);
+ *sp++ = *p;
*p = fixnum(0);
value_t v = fl_add_any(p, n);
int64_t i64;
p[1] = isfixnum(v) ? fixnum_neg(v) : fl_neg(v);
- p[0] = POP();
+ p[0] = *(--sp);
v = fl_add_any(p, 2);
- POPN(n);
- PUSH(v);
+ sp -= n;
+ *sp++ = v;
NEXT_OP;
}
OP(OP_BRNL)
- ip += POP() == FL_nil ? GET_INT32(ip) : 4;
+ ip += *(--sp) == FL_nil ? GET_INT32(ip) : 4;
NEXT_OP;
OP(OP_SETAL)
- bp[GET_INT32(ip)] = FL(sp)[-1];
+ bp[GET_INT32(ip)] = sp[-1];
ip += 4;
NEXT_OP;
@@ -905,6 +952,7 @@
OP(OP_BOXL) {
int i = GET_INT32(ip);
ip += 4;
+ FL(sp) = sp;
value_t v = mk_cons();
car_(v) = bp[i];
cdr_(v) = FL_nil;
@@ -913,8 +961,8 @@
}
OP(OP_FUNCTIONP) {
- value_t v = FL(sp)[-1];
- FL(sp)[-1] =
+ value_t v = sp[-1];
+ sp[-1] =
((tag(v) == TAG_FUNCTION &&
(isbuiltin(v) || v>(N_BUILTINS<<3))) ||
iscbuiltin(v)) ? FL_t : FL_nil;
@@ -926,17 +974,17 @@
NEXT_OP;
OP(OP_BRNEL)
- ip += FL(sp)[-2] != FL(sp)[-1] ? GET_INT32(ip) : 4;
- POPN(2);
+ ip += sp[-2] != sp[-1] ? GET_INT32(ip) : 4;
+ sp -= 2;
NEXT_OP;
OP(OP_BRNNL)
- ip += POP() != FL_nil ? GET_INT32(ip) : 4;
+ ip += *(--sp) != FL_nil ? GET_INT32(ip) : 4;
NEXT_OP;
OP(OP_LOADCL)
ip += 4;
- PUSH(vector_elt(bp[nargs], GET_INT32(ip)));
+ *sp++ = vector_elt(bp[nargs], GET_INT32(ip));
ip += 4;
NEXT_OP;
@@ -944,6 +992,6 @@
value_t v = fn_vals(bp[-1]);
v = vector_elt(v, GET_INT32(ip));
ip += 4;
- PUSH(v);
+ *sp++ = v;
NEXT_OP;
}