shithub: femtolisp

Download patch

ref: fce9b7e74afe19b3517efff0ac77bf4b169951c7
parent: fafab820d599c6a6f81bfc7c7930af3f14c28c6e
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Tue Mar 21 20:35:49 EDT 2023

reduce compute_maxstack size

--- a/Makefile
+++ b/Makefile
@@ -33,7 +33,7 @@
 .c.o:
 	${CC} -o $@ -c $< ${CFLAGS} -Iposix -Illt
 
-flisp.o:  flisp.c cvalues.c operators.c types.c flisp.h print.c read.c equal.c
+flisp.o:  flisp.c cvalues.c operators.c types.c flisp.h print.c read.c equal.c maxstack.inc
 flmain.o: flmain.c boot.h flisp.h
 
 boot.h: flisp.boot
--- a/flisp.c
+++ b/flisp.c
@@ -734,9 +734,7 @@
 
 // eval -----------------------------------------------------------------------
 
-#define list(a,n) _list((a),(n),0)
-
-static value_t _list(value_t *args, uint32_t nargs, int star)
+static value_t list(value_t *args, uint32_t nargs, int star)
 {
     cons_t *c;
     uint32_t i;
@@ -977,7 +975,7 @@
             }
             s = (fixnum_t)nargs - (fixnum_t)i;
             if (s > 0) {
-                v = list(&Stack[bp+i], s);
+                v = list(&Stack[bp+i], s, 0);
                 Stack[bp+i] = v;
                 if (s > 1) {
                     Stack[bp+i+1] = Stack[bp+nargs+0];
@@ -1277,7 +1275,7 @@
             n = *ip++;
         apply_list:
             if (n > 0) {
-                v = list(&Stack[SP-n], n);
+                v = list(&Stack[SP-n], n, 0);
                 POPN(n);
                 PUSH(v);
             }
@@ -1946,7 +1944,7 @@
 {
     if (nargs == 1) return args[0];
     else if (nargs == 0) argcount("list*", nargs, 1);
-    return _list(args, nargs, 1);
+    return list(args, nargs, 1);
 }
 
 value_t fl_stacktrace(value_t *args, uint32_t nargs)
--- a/maxstack.inc
+++ b/maxstack.inc
@@ -4,52 +4,43 @@
     uint8_t op;
     uint32_t i, n, sp = 0, maxsp = 0;
 
-    while (1) {
+    while (ip < end) {
         if ((int32_t)sp > (int32_t)maxsp) maxsp = sp;
-        if (ip >= end) break;
         op = *ip++;
         switch (op) {
-        case OP_ARGC:
-            n = *ip++;
-            USED(n);
+        case OP_SETG: case OP_SETA: case OP_ARGC:
+            ip++;
             break;
         case OP_VARGC:
             n = *ip++;
-            sp += (n+2);
+            sp += n+2;
             break;
-        case OP_LARGC:
-            SWAP_INT32(ip);
-            n = GET_INT32(ip); ip+=4;
-            USED(n);
-            break;
         case OP_LVARGC:
             SWAP_INT32(ip);
-            n = GET_INT32(ip); ip+=4;
-            sp += (n+2);
+            n = GET_INT32(ip); ip += 4;
+            sp += n+2;
             break;
         case OP_OPTARGS:
             SWAP_INT32(ip);
-            i = GET_INT32(ip); ip+=4;
+            i = GET_INT32(ip); ip += 4;
             SWAP_INT32(ip);
-            n = abs(GET_INT32(ip)); ip+=4;
-            sp += (n-i);
+            n = abs(GET_INT32(ip)); ip += 4;
+            sp += n-i;
             break;
         case OP_KEYARGS:
             SWAP_INT32(ip);
-            i = GET_INT32(ip); ip+=4;
+            i = GET_INT32(ip); ip += 4;
             SWAP_INT32(ip);
-            n = GET_INT32(ip); ip+=4;
-            USED(n);
+            ip += 4;
             SWAP_INT32(ip);
-            n = abs(GET_INT32(ip)); ip+=4;
-            sp += (n-i);
+            n = abs(GET_INT32(ip)); ip += 4;
+            sp += n-i;
             break;
         case OP_BRBOUND:
             SWAP_INT32(ip);
-            ip+=4;
+            ip += 4;
             sp++;
             break;
-
         case OP_TCALL: case OP_CALL:
             n = *ip++;  // nargs
             sp -= n;
@@ -67,7 +58,7 @@
             ip += 4; break;
         case OP_BRF: case OP_BRT:
             SWAP_INT16(ip);
-            ip+=2;
+            ip += 2;
             sp--;
             break;
         case OP_BRFL: case OP_BRTL:
@@ -93,10 +84,7 @@
         case OP_BRNNL: case OP_BRNL:
             SWAP_INT32(ip);
             ip += 4;
-            sp--;
-            break;
-        case OP_RET: sp--; break;
-
+        case OP_RET:
         case OP_CONS: case OP_SETCAR: case OP_SETCDR: case OP_POP:
         case OP_EQ: case OP_EQV: case OP_EQUAL: case OP_ADD2: case OP_SUB2:
         case OP_IDIV: case OP_NUMEQ: case OP_LT: case OP_COMPARE:
@@ -104,31 +92,22 @@
             sp--;
             break;
 
-        case OP_PAIRP: case OP_ATOMP: case OP_NOT: case OP_NULLP:
-        case OP_BOOLEANP: case OP_SYMBOLP: case OP_NUMBERP: case OP_FIXNUMP:
-        case OP_BOUNDP: case OP_BUILTINP: case OP_FUNCTIONP: case OP_VECTORP:
-        case OP_NOP: case OP_CAR: case OP_CDR: case OP_NEG: case OP_CLOSURE:
-            break;
-
         case OP_TAPPLY: case OP_APPLY:
-            n = *ip++;
-            sp -= (n-1);
-            break;
-
         case OP_LIST: case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV:
         case OP_VECTOR:
             n = *ip++;
-            sp -= (n-1);
+            sp -= n-1;
             break;
 
+        case OP_FOR:
+            if (maxsp < sp+2)
+                maxsp = sp+2;
         case OP_ASET:
             sp -= 2;
             break;
-        case OP_FOR:
-            if (sp+2 > maxsp) maxsp = sp+2;
-            sp -=2;
-            break;
 
+        case OP_LOADI8: case OP_LOADV: case OP_LOADG: case OP_LOADA:
+            ip++;
         case OP_LOADT: case OP_LOADF: case OP_LOADNIL: case OP_LOAD0:
         case OP_LOAD1: case OP_LOADA0: case OP_LOADA1: case OP_LOADC00:
         case OP_LOADC01: case OP_DUP:
@@ -135,39 +114,28 @@
             sp++;
             break;
 
-        case OP_LOADI8: case OP_LOADV: case OP_LOADG: case OP_LOADA:
-            ip++;
-            sp++;
-            break;
-        case OP_LOADVL: case OP_LOADGL: case OP_LOADAL:
+        case OP_SETCL:
             SWAP_INT32(ip);
-            ip+=4;
+            ip += 4;
+        case OP_LOADVL: case OP_LOADGL: case OP_LOADAL:
             sp++;
-            break;
-
-        case OP_SETG: case OP_SETA:
-            ip++;
-            break;
-        case OP_SETGL: case OP_SETAL:
+        case OP_SETGL: case OP_SETAL: case OP_LARGC:
             SWAP_INT32(ip);
-            ip+=4;
+            ip += 4;
             break;
 
-        case OP_LOADC: ip+=2; sp++; break;
+        case OP_LOADC:
+            sp++;
         case OP_SETC:
-            ip+=2;
+            ip += 2;
             break;
+
         case OP_LOADCL:
+            sp++;
             SWAP_INT32(ip);
-            ip+=4;
+            ip += 4;
             SWAP_INT32(ip);
-            ip+=4;
-            sp++; break;
-        case OP_SETCL:
-            SWAP_INT32(ip);
-            ip+=4;
-            SWAP_INT32(ip);
-            ip+=4;
+            ip += 4;
             break;
         }
     }
--- a/mkfile
+++ b/mkfile
@@ -37,7 +37,7 @@
 boot.h: flisp.boot
 	sed 's,\\,\\\\,g;s,",\\",g;s,^,",g;s,$,\\n",g' $prereq >$target
 
-flmain.$O: boot.h
+flmain.$O: boot.h maxstack.inc
 
 bootstrap:V: $O.out
 	cp flisp.boot flisp.boot.bak && \