Annotation of gforth/engine.c, revision 1.5

1.1       anton       1: /*
1.5     ! anton       2:   $Id: engine.c,v 1.4 1994/05/05 15:46:42 pazsan Exp $
1.1       anton       3:   Copyright 1992 by the ANSI figForth Development Group
                      4: */
                      5: 
                      6: #include <ctype.h>
                      7: #include <stdio.h>
                      8: #include <string.h>
                      9: #include <math.h>
                     10: #include <sys/types.h>
                     11: #include <sys/stat.h>
                     12: #include <fcntl.h>
                     13: #include <assert.h>
                     14: #include <stdlib.h>
1.2       pazsan     15: #include <time.h>
1.1       anton      16: #include "forth.h"
                     17: #include "io.h"
                     18: 
                     19: extern unlink(char *);
                     20: extern ftruncate(int, int);
                     21: 
                     22: typedef union {
                     23:   struct {
                     24: #ifdef BIG_ENDIAN
                     25:     Cell high;
                     26:     Cell low;
                     27: #else
                     28:     Cell low;
                     29:     Cell high;
                     30: #endif;
                     31:   } cells;
                     32:   DCell dcell;
                     33: } Double_Store;
                     34: 
                     35: typedef struct F83Name {
                     36:   struct F83Name       *next;  /* the link field for old hands */
                     37:   char                 countetc;
                     38:   Char                 name[0];
                     39: } F83Name;
                     40: 
                     41: /* are macros for setting necessary? */
                     42: #define F83NAME_COUNT(np)      ((np)->countetc & 0x1f)
                     43: #define F83NAME_SMUDGE(np)     (((np)->countetc & 0x40) != 0)
                     44: #define F83NAME_IMMEDIATE(np)  (((np)->countetc & 0x20) != 0)
                     45: 
                     46: /* NEXT and NEXT1 are split into several parts to help scheduling */
                     47: #ifdef DIRECT_THREADED
1.4       pazsan     48: #      define NEXT1_P1 
                     49: #      define NEXT1_P2 ({goto *cfa;})
                     50: #      define DEF_CA
1.1       anton      51: #else
1.4       pazsan     52: #      define NEXT1_P1 ({ca = *cfa;})
                     53: #      define NEXT1_P2 ({goto *ca;})
                     54: #      define DEF_CA   Label ca;
1.1       anton      55: #endif
                     56: #define NEXT_P1 ({cfa = *ip++; NEXT1_P1;})
                     57: 
1.4       pazsan     58: #define NEXT1 ({DEF_CA NEXT1_P1; NEXT1_P2;})
                     59: #define NEXT ({DEF_CA NEXT_P1; NEXT1_P2;})
1.1       anton      60: 
                     61: #ifdef USE_TOS
                     62: #define IF_TOS(x) x
                     63: #else
                     64: #define IF_TOS(x)
                     65: #define TOS (sp[0])
                     66: #endif
                     67: 
                     68: #ifdef USE_FTOS
                     69: #define IF_FTOS(x) x
                     70: #else
                     71: #define IF_FTOS(x)
                     72: #define FTOS (fp[0])
                     73: #endif
                     74: 
1.4       pazsan     75: /*
                     76: #define CA_DODOES      (symbols[DODOES])
                     77: */
1.1       anton      78: 
                     79: int emitcounter;
                     80: #define NULLC '\0'
                     81: 
1.3       pazsan     82: #define cstr(to,from,size)\
                     83:        {       memcpy(to,from,size);\
1.1       anton      84:                to[size]=NULLC;}
                     85: #define NEWLINE        '\n'
                     86: 
                     87: static char* fileattr[6]={"r","rb","r+","r+b","w+","w+b"};
                     88: 
1.5     ! anton      89: Label *engine(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp)
1.1       anton      90: /* executes code at ip, if ip!=NULL
                     91:    returns array of machine code labels (for use in a loader), if ip==NULL
                     92:    This is very preliminary, as the bootstrap architecture is not yet decided
                     93: */
                     94: {
                     95:   Xt cfa;
1.4       pazsan     96:   Address up=NULL;
1.1       anton      97:   static Label symbols[]= {
                     98:     &&docol,
                     99:     &&docon,
                    100:     &&dovar,
1.4       pazsan    101:     &&douser,
1.1       anton     102:     &&dodoes,
1.4       pazsan    103:     &&docol,  /* dummy for does handler address */
1.1       anton     104: #include "prim_labels.i"
                    105:   };
                    106:   IF_TOS(register Cell TOS;)
                    107:   IF_FTOS(Float FTOS;)
                    108: #ifdef CPU_DEP
                    109:   CPU_DEP;
                    110: #endif
                    111: 
                    112:   if (ip == NULL)
                    113:     return symbols;
                    114:   
                    115:   IF_TOS(TOS = sp[0]);
                    116:   IF_FTOS(FTOS = fp[0]);
                    117:   prep_terminal();
                    118:   NEXT;
                    119:   
                    120:  docol:
                    121: #ifdef DEBUG
                    122:   printf("col: %x\n",(Cell)PFA1(cfa));
                    123: #endif
                    124: #ifdef undefined
                    125:   /* this is the simple version */
                    126:   *--rp = (Cell)ip;
                    127:   ip = (Xt *)PFA1(cfa);
                    128:   NEXT;
                    129: #endif
                    130:   /* this one is important, so we help the compiler optimizing
                    131:      The following version may be better (for scheduling), but probably has
                    132:      problems with code fields employing calls and delay slots
                    133:   */
                    134:   {
1.4       pazsan    135:     DEF_CA
1.1       anton     136:     Xt *current_ip = (Xt *)PFA1(cfa);
                    137:     cfa = *current_ip;
                    138:     NEXT1_P1;
                    139:     *--rp = (Cell)ip;
                    140:     ip = current_ip+1;
1.3       pazsan    141:     NEXT1_P2;
1.1       anton     142:   }
                    143:   
                    144:  docon:
                    145: #ifdef DEBUG
                    146:   printf("con: %x\n",*(Cell*)PFA1(cfa));
                    147: #endif
                    148: #ifdef USE_TOS
                    149:   *sp-- = TOS;
                    150:   TOS = *(Cell *)PFA1(cfa);
                    151: #else
                    152:   *--sp = *(Cell *)PFA1(cfa);
                    153: #endif
                    154:   NEXT;
                    155:   
                    156:  dovar:
                    157: #ifdef DEBUG
                    158:   printf("var: %x\n",(Cell)PFA1(cfa));
                    159: #endif
                    160: #ifdef USE_TOS
                    161:   *sp-- = TOS;
                    162:   TOS = (Cell)PFA1(cfa);
                    163: #else
                    164:   *--sp = (Cell)PFA1(cfa);
                    165: #endif
                    166:   NEXT;
                    167:   
                    168:   /* !! user? */
                    169:   
1.4       pazsan    170:  douser:
                    171: #ifdef DEBUG
                    172:   printf("user: %x\n",(Cell)PFA1(cfa));
                    173: #endif
                    174: #ifdef USE_TOS
                    175:   *sp-- = TOS;
1.5     ! anton     176:   TOS = (Cell)(up+*(Cell*)PFA1(cfa));
1.4       pazsan    177: #else
1.5     ! anton     178:   *--sp = (Cell)(up+*(Cell*)PFA1(cfa));
1.4       pazsan    179: #endif
                    180:   NEXT;
                    181:   
1.1       anton     182:  dodoes:
                    183:   /* this assumes the following structure:
                    184:      defining-word:
                    185:      
                    186:      ...
                    187:      DOES>
                    188:      (possible padding)
                    189:      possibly handler: jmp dodoes
                    190:      (possible branch delay slot(s))
                    191:      Forth code after DOES>
                    192:      
                    193:      defined word:
                    194:      
                    195:      cfa: address of or jump to handler OR
                    196:           address of or jump to dodoes, address of DOES-code
                    197:      pfa:
                    198:      
                    199:      */
                    200: #ifdef DEBUG
1.4       pazsan    201:   printf("does: %x\n",(Cell)PFA(cfa)); fflush(stdout);
1.1       anton     202: #endif
                    203:   *--rp = (Cell)ip;
                    204:   /* PFA1 might collide with DOES_CODE1 here, so we use PFA */
                    205: #ifdef USE_TOS
                    206:   *sp-- = TOS;
                    207:   TOS = (Cell)PFA(cfa);
                    208: #else
                    209:   *--sp = (Cell)PFA(cfa);
                    210: #endif
                    211:   ip = DOES_CODE1(cfa);
                    212:   NEXT;
                    213:   
                    214: #include "primitives.i"
                    215: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>