shithub: femtolisp

Download patch

ref: 63d9386558212f72a4e6bc6ac70544030545d7c1
parent: 1cfb58e7b71eb6a4e455ffcaeb33d78156a978e2
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Mon Mar 13 18:44:26 EDT 2023

no computed gotos

--- a/Makefile
+++ b/Makefile
@@ -32,7 +32,7 @@
 
 .SUFFIXES: .c .o
 .c.o:
-	${CC} -o $@ -c $< ${CFLAGS} -Iposix -Illt -DUSE_COMPUTED_GOTO
+	${CC} -o $@ -c $< ${CFLAGS} -Iposix -Illt
 
 flisp.o:  flisp.c cvalues.c operators.c types.c flisp.h print.c read.c equal.c
 flmain.o: flmain.c flisp.h
--- a/flisp.c
+++ b/flisp.c
@@ -905,13 +905,8 @@
 #define SWAP_INT32(a) (*(int32_t*)(a) = bswap_32(*(int32_t*)(a)))
 #define SWAP_INT16(a) (*(int16_t*)(a) = bswap_16(*(int16_t*)(a)))
 
-#ifdef USE_COMPUTED_GOTO
-#define OP(x) L_##x:
-#define NEXT_OP goto *vm_labels[*ip++]
-#else
 #define OP(x) case x:
 #define NEXT_OP goto next_op
-#endif
 
 /*
   stack on entry: <func>  <nargs args...>
@@ -928,8 +923,6 @@
 */
 static value_t apply_cl(uint32_t nargs)
 {
-    VM_LABELS;
-    VM_APPLY_LABELS;
     uint32_t top_frame = curr_frame;
     // frame variables
     uint32_t n, captured;
@@ -938,10 +931,7 @@
     fixnum_t s, hi;
 
     // temporary variables (not necessary to preserve across calls)
-#ifndef USE_COMPUTED_GOTO
-    uint32_t op;
-#endif
-    uint32_t i;
+    uint32_t op, i;
     symbol_t *sym;
     static cons_t *c;
     static value_t *pv;
@@ -969,15 +959,10 @@
     curr_frame = SP;
 
     {
-#ifdef USE_COMPUTED_GOTO
-    {
-        NEXT_OP;
-#else
     next_op:
         op = *ip++;
     dispatch:
         switch (op) {
-#endif
         OP(OP_ARGC)
             n = *ip++;
         do_argc:
@@ -1060,11 +1045,6 @@
                         for(s=SP-n-1; s < (int)SP-1; s++)
                             Stack[s] = Stack[s+1];
                         SP--;
-#ifdef USE_COMPUTED_GOTO
-                        if (i == OP_APPLY)
-                            goto apply_tapply;
-                        goto *vm_apply_labels[i];
-#else
                         switch (i) {
                         case OP_LIST:   goto apply_list;
                         case OP_VECTOR: goto apply_vector;
@@ -1077,7 +1057,6 @@
                             op = (uint8_t)i;
                             goto dispatch;
                         }
-#endif
                     }
                 }
             }
@@ -1112,9 +1091,6 @@
                         for(s=SP-n-1; s < (int)SP-1; s++)
                             Stack[s] = Stack[s+1];
                         SP--;
-#ifdef USE_COMPUTED_GOTO
-                        goto *vm_apply_labels[i];
-#else
                         switch (i) {
                         case OP_LIST:   goto apply_list;
                         case OP_VECTOR: goto apply_vector;
@@ -1127,7 +1103,6 @@
                             op = (uint8_t)i;
                             goto dispatch;
                         }
-#endif
                     }
                 }
             }
@@ -1783,15 +1758,10 @@
             nargs = process_keys(v, i, n, labs(s)-(i+n), bp, nargs, s<0);
             NEXT_OP;
 
-#ifndef USE_COMPUTED_GOTO
         default:
             goto dispatch;
-#endif
         }
     }
-#ifdef USE_COMPUTED_GOTO
-    return UNBOUND;  // not reached
-#endif
 }
 
 static uint32_t compute_maxstack(uint8_t *code, size_t len, int bswap)
--- a/flisp.h
+++ b/flisp.h
@@ -110,8 +110,6 @@
 void fl_gc_handle(value_t *pv);
 void fl_free_gc_handles(uint32_t n);
 
-#include "opcodes.h"
-
 // utility for iterating over all arguments in a builtin
 // i=index, i0=start index, arg = var for each arg, args = arg array
 // assumes "nargs" is the argument count
--- a/opcodes.h
+++ b/opcodes.h
@@ -1,6 +1,3 @@
-#ifndef OPCODES_H
-#define OPCODES_H
-
 enum {
     OP_NOP=0, OP_DUP, OP_POP, OP_CALL, OP_TCALL, OP_JMP, OP_BRF, OP_BRT,
     OP_JMPL, OP_BRFL, OP_BRTL, OP_RET,
@@ -33,69 +30,3 @@
 
     N_OPCODES
 };
-
-#ifdef USE_COMPUTED_GOTO
-#define VM_LABELS                                                       \
-    static void *vm_labels[] = {                                        \
-NULL, &&L_OP_DUP, &&L_OP_POP, &&L_OP_CALL, &&L_OP_TCALL, &&L_OP_JMP, \
-    &&L_OP_BRF, &&L_OP_BRT,                                             \
-    &&L_OP_JMPL, &&L_OP_BRFL, &&L_OP_BRTL, &&L_OP_RET,                  \
-                                                                        \
-    &&L_OP_EQ, &&L_OP_EQV, &&L_OP_EQUAL, &&L_OP_ATOMP, &&L_OP_NOT,      \
-    &&L_OP_NULLP, &&L_OP_BOOLEANP,                                      \
-    &&L_OP_SYMBOLP, &&L_OP_NUMBERP, &&L_OP_BOUNDP, &&L_OP_PAIRP,        \
-    &&L_OP_BUILTINP, &&L_OP_VECTORP,                                    \
-    &&L_OP_FIXNUMP, &&L_OP_FUNCTIONP,                                   \
-                                                                        \
-    &&L_OP_CONS, &&L_OP_LIST, &&L_OP_CAR, &&L_OP_CDR, &&L_OP_SETCAR,    \
-    &&L_OP_SETCDR, &&L_OP_APPLY,                                        \
-                                                                        \
-    &&L_OP_ADD, &&L_OP_SUB, &&L_OP_MUL, &&L_OP_DIV, &&L_OP_IDIV, &&L_OP_NUMEQ, \
-    &&L_OP_LT, &&L_OP_COMPARE,                                          \
-                                                                        \
-    &&L_OP_VECTOR, &&L_OP_AREF, &&L_OP_ASET,                            \
-                                                                        \
-    &&L_OP_LOADT, &&L_OP_LOADF, &&L_OP_LOADNIL, &&L_OP_LOAD0, &&L_OP_LOAD1, \
-    &&L_OP_LOADI8,                                                      \
-    &&L_OP_LOADV, &&L_OP_LOADVL,                                        \
-    &&L_OP_LOADG, &&L_OP_LOADGL,                                        \
-    &&L_OP_LOADA, &&L_OP_LOADAL, &&L_OP_LOADC, &&L_OP_LOADCL,           \
-    &&L_OP_SETG, &&L_OP_SETGL,                                          \
-    &&L_OP_SETA, &&L_OP_SETAL, &&L_OP_SETC, &&L_OP_SETCL,               \
-                                                                        \
-    &&L_OP_CLOSURE, &&L_OP_ARGC, &&L_OP_VARGC, &&L_OP_TRYCATCH,         \
-    &&L_OP_FOR,                                                         \
-    &&L_OP_TAPPLY, &&L_OP_ADD2, &&L_OP_SUB2, &&L_OP_NEG, &&L_OP_LARGC,  \
-    &&L_OP_LVARGC,                                                      \
-    &&L_OP_LOADA0, &&L_OP_LOADA1, &&L_OP_LOADC00, &&L_OP_LOADC01,       \
-    &&L_OP_CALLL, &&L_OP_TCALLL, &&L_OP_BRNE, &&L_OP_BRNEL, &&L_OP_CADR,\
-    &&L_OP_BRNN, &&L_OP_BRNNL, &&L_OP_BRN, &&L_OP_BRNL,                 \
-    &&L_OP_OPTARGS, &&L_OP_BRBOUND, &&L_OP_KEYARGS                      \
-    }
-
-#define VM_APPLY_LABELS                                                 \
-    static void *vm_apply_labels[] = {                                  \
-NULL, &&L_OP_DUP, &&L_OP_POP, &&L_OP_CALL, &&L_OP_TCALL, &&L_OP_JMP, \
-    &&L_OP_BRF, &&L_OP_BRT,                                             \
-    &&L_OP_JMPL, &&L_OP_BRFL, &&L_OP_BRTL, &&L_OP_RET,                  \
-                                                                        \
-    &&L_OP_EQ, &&L_OP_EQV, &&L_OP_EQUAL, &&L_OP_ATOMP, &&L_OP_NOT,      \
-    &&L_OP_NULLP, &&L_OP_BOOLEANP,                                      \
-    &&L_OP_SYMBOLP, &&L_OP_NUMBERP, &&L_OP_BOUNDP, &&L_OP_PAIRP,        \
-    &&L_OP_BUILTINP, &&L_OP_VECTORP,                                    \
-    &&L_OP_FIXNUMP, &&L_OP_FUNCTIONP,                                   \
-                                                                        \
-    &&L_OP_CONS, &&apply_list, &&L_OP_CAR, &&L_OP_CDR, &&L_OP_SETCAR,    \
-    &&L_OP_SETCDR, &&apply_apply,                                        \
-                                                                        \
-    &&apply_add, &&apply_sub, &&apply_mul, &&apply_div, &&L_OP_IDIV, &&L_OP_NUMEQ, \
-    &&L_OP_LT, &&L_OP_COMPARE,                                          \
-                                                                        \
-    &&apply_vector, &&L_OP_AREF, &&L_OP_ASET                            \
-    }
-#else
-#define VM_LABELS
-#define VM_APPLY_LABELS
-#endif
-
-#endif