Annotation of gforth/engine/engine.c, revision 1.48

1.1       anton       1: /* Gforth virtual machine (aka inner interpreter)
                      2: 
1.22      anton       3:   Copyright (C) 1995,1996,1997,1998,2000 Free Software Foundation, Inc.
1.1       anton       4: 
                      5:   This file is part of Gforth.
                      6: 
                      7:   Gforth is free software; you can redistribute it and/or
                      8:   modify it under the terms of the GNU General Public License
                      9:   as published by the Free Software Foundation; either version 2
                     10:   of the License, or (at your option) any later version.
                     11: 
                     12:   This program is distributed in the hope that it will be useful,
                     13:   but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14:   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15:   GNU General Public License for more details.
                     16: 
                     17:   You should have received a copy of the GNU General Public License
                     18:   along with this program; if not, write to the Free Software
1.23      anton      19:   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.1       anton      20: */
                     21: 
1.36      pazsan     22: undefine(`symbols')
                     23: 
1.1       anton      24: #include "config.h"
1.31      pazsan     25: #include "forth.h"
1.1       anton      26: #include <ctype.h>
                     27: #include <stdio.h>
                     28: #include <string.h>
                     29: #include <math.h>
1.4       pazsan     30: #include <assert.h>
                     31: #include <stdlib.h>
                     32: #include <errno.h>
                     33: #include "io.h"
                     34: #include "threaded.h"
                     35: #ifndef STANDALONE
1.1       anton      36: #include <sys/types.h>
                     37: #include <sys/stat.h>
                     38: #include <fcntl.h>
                     39: #include <time.h>
                     40: #include <sys/time.h>
                     41: #include <unistd.h>
                     42: #include <pwd.h>
1.17      pazsan     43: #include <dirent.h>
1.21      anton      44: #include <sys/resource.h>
1.19      anton      45: #ifdef HAVE_FNMATCH_H
1.18      anton      46: #include <fnmatch.h>
1.19      anton      47: #else
                     48: #include "fnmatch.h"
                     49: #endif
1.4       pazsan     50: #else
                     51: #include "systypes.h"
                     52: #endif
1.1       anton      53: 
                     54: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN) /* what else? */
                     55: #include <dlfcn.h>
                     56: #endif
1.8       pazsan     57: #if defined(_WIN32)
                     58: #include <windows.h>
                     59: #endif
1.1       anton      60: #ifdef hpux
                     61: #include <dl.h>
                     62: #endif
                     63: 
                     64: #ifndef SEEK_SET
                     65: /* should be defined in stdio.h, but some systems don't have it */
                     66: #define SEEK_SET 0
                     67: #endif
                     68: 
                     69: #define IOR(flag)      ((flag)? -512-errno : 0)
                     70: 
1.4       pazsan     71: struct F83Name {
                     72:   struct F83Name *next;  /* the link field for old hands */
                     73:   char         countetc;
                     74:   char         name[0];
                     75: };
1.1       anton      76: 
                     77: #define F83NAME_COUNT(np)      ((np)->countetc & 0x1f)
1.25      anton      78: 
                     79: struct Longname {
                     80:   struct Longname *next;  /* the link field for old hands */
                     81:   Cell         countetc;
                     82:   char         name[0];
                     83: };
                     84: 
                     85: #define LONGNAME_COUNT(np)     ((np)->countetc & (((~((UCell)0))<<3)>>3))
1.1       anton      86: 
                     87: Cell *SP;
                     88: Float *FP;
                     89: Address UP=NULL;
                     90: 
                     91: #if 0
                     92: /* not used currently */
                     93: int emitcounter;
                     94: #endif
                     95: #define NULLC '\0'
                     96: 
1.14      pazsan     97: #ifdef MEMCMP_AS_SUBROUTINE
                     98: extern int gforth_memcmp(const char * s1, const char * s2, size_t n);
1.15      anton      99: #define memcmp(s1,s2,n) gforth_memcmp(s1,s2,n)
1.14      pazsan    100: #endif
                    101: 
1.1       anton     102: #define NEWLINE        '\n'
                    103: 
1.9       pazsan    104: #ifdef HAS_FILE
1.16      anton     105: static char* fileattr[6]={"rb","rb","r+b","r+b","wb","wb"};
1.32      pazsan    106: static char* pfileattr[6]={"r","r","r+","r+","w","w"};
1.1       anton     107: 
                    108: #ifndef O_BINARY
                    109: #define O_BINARY 0
                    110: #endif
                    111: #ifndef O_TEXT
                    112: #define O_TEXT 0
                    113: #endif
                    114: 
                    115: static int ufileattr[6]= {
1.16      anton     116:   O_RDONLY|O_BINARY, O_RDONLY|O_BINARY,
                    117:   O_RDWR  |O_BINARY, O_RDWR  |O_BINARY,
                    118:   O_WRONLY|O_BINARY, O_WRONLY|O_BINARY };
1.9       pazsan    119: #endif
1.1       anton     120: 
1.26      anton     121: /* conversion on fetch */
                    122: 
1.41      anton     123: #define vm_Cell2f(_cell,_x)            ((_x)=(Bool)(_cell))
                    124: #define vm_Cell2c(_cell,_x)            ((_x)=(Char)(_cell))
                    125: #define vm_Cell2n(_cell,_x)            ((_x)=(Cell)(_cell))
                    126: #define vm_Cell2w(_cell,_x)            ((_x)=(Cell)(_cell))
                    127: #define vm_Cell2u(_cell,_x)            ((_x)=(UCell)(_cell))
                    128: #define vm_Cell2a_(_cell,_x)           ((_x)=(Cell *)(_cell))
                    129: #define vm_Cell2c_(_cell,_x)           ((_x)=(Char *)(_cell))
                    130: #define vm_Cell2f_(_cell,_x)           ((_x)=(Float *)(_cell))
                    131: #define vm_Cell2df_(_cell,_x)          ((_x)=(DFloat *)(_cell))
                    132: #define vm_Cell2sf_(_cell,_x)          ((_x)=(SFloat *)(_cell))
                    133: #define vm_Cell2xt(_cell,_x)           ((_x)=(Xt)(_cell))
                    134: #define vm_Cell2f83name(_cell,_x)      ((_x)=(struct F83Name *)(_cell))
                    135: #define vm_Cell2longname(_cell,_x)     ((_x)=(struct Longname *)(_cell))
                    136: #define vm_Float2r(_float,_x)          (_x=_float)
1.26      anton     137: 
                    138: /* conversion on store */
                    139: 
1.41      anton     140: #define vm_f2Cell(_x,_cell)            ((_cell)=(Cell)(_x))
                    141: #define vm_c2Cell(_x,_cell)            ((_cell)=(Cell)(_x))
                    142: #define vm_n2Cell(_x,_cell)            ((_cell)=(Cell)(_x))
                    143: #define vm_w2Cell(_x,_cell)            ((_cell)=(Cell)(_x))
                    144: #define vm_u2Cell(_x,_cell)            ((_cell)=(Cell)(_x))
                    145: #define vm_a_2Cell(_x,_cell)           ((_cell)=(Cell)(_x))
                    146: #define vm_c_2Cell(_x,_cell)           ((_cell)=(Cell)(_x))
                    147: #define vm_f_2Cell(_x,_cell)           ((_cell)=(Cell)(_x))
                    148: #define vm_df_2Cell(_x,_cell)          ((_cell)=(Cell)(_x))
                    149: #define vm_sf_2Cell(_x,_cell)          ((_cell)=(Cell)(_x))
                    150: #define vm_xt2Cell(_x,_cell)           ((_cell)=(Cell)(_x))
                    151: #define vm_f83name2Cell(_x,_cell)      ((_cell)=(Cell)(_x))
                    152: #define vm_longname2Cell(_x,_cell)     ((_cell)=(Cell)(_x))
                    153: #define vm_r2Float(_x,_float)          (_float=_x)
1.26      anton     154: 
1.41      anton     155: #define vm_Cell2Cell(_x,_y)            (_y=_x)
1.29      anton     156: 
1.46      anton     157: #ifdef NO_IP
                    158: #define IMM_ARG(access,value)          (VARIANT(value))
                    159: #else
                    160: #define IMM_ARG(access,value)          (access)
                    161: #endif
                    162: 
1.1       anton     163: /* if machine.h has not defined explicit registers, define them as implicit */
                    164: #ifndef IPREG
                    165: #define IPREG
                    166: #endif
                    167: #ifndef SPREG
                    168: #define SPREG
                    169: #endif
                    170: #ifndef RPREG
                    171: #define RPREG
                    172: #endif
                    173: #ifndef FPREG
                    174: #define FPREG
                    175: #endif
                    176: #ifndef LPREG
                    177: #define LPREG
                    178: #endif
                    179: #ifndef CFAREG
                    180: #define CFAREG
                    181: #endif
                    182: #ifndef UPREG
                    183: #define UPREG
                    184: #endif
                    185: #ifndef TOSREG
                    186: #define TOSREG
                    187: #endif
                    188: #ifndef FTOSREG
                    189: #define FTOSREG
                    190: #endif
                    191: 
                    192: #ifndef CPU_DEP1
                    193: # define CPU_DEP1 0
                    194: #endif
                    195: 
1.28      anton     196: /* instructions containing these must be the last instruction of a
                    197:    super-instruction (e.g., branches, EXECUTE, and other instructions
                    198:    ending the basic block). Instructions containing SET_IP get this
                    199:    automatically, so you usually don't have to write it.  If you have
                    200:    to write it, write it after IP points to the next instruction.
                    201:    Used for profiling.  Don't write it in a word containing SET_IP, or
                    202:    the following block will be counted twice. */
                    203: #ifdef VM_PROFILING
                    204: #define SUPER_END  vm_count_block(IP)
                    205: #else
                    206: #define SUPER_END
                    207: #endif
1.35      anton     208: #define SUPER_CONTINUE
1.28      anton     209: 
1.10      anton     210: #ifdef GFORTH_DEBUGGING
                    211: /* define some VM registers as global variables, so they survive exceptions;
                    212:    global register variables are not up to the task (according to the 
                    213:    GNU C manual) */
1.44      anton     214: Xt *saved_ip;
1.10      anton     215: Cell *rp;
                    216: #endif
                    217: 
1.46      anton     218: #ifdef NO_IP
                    219: static Label next_code;
                    220: #endif
                    221: 
1.37      pazsan    222: #ifdef DEBUG
                    223: #define CFA_TO_NAME(__cfa) \
                    224:       Cell len, i; \
                    225:       char * name = __cfa; \
                    226:       for(i=0; i<32; i+=sizeof(Cell)) { \
                    227:         len = ((Cell*)name)[-1]; \
                    228:         if(len < 0) { \
                    229:          len &= 0x1F; \
                    230:           if((len+sizeof(Cell)) > i) break; \
                    231:        } len = 0; \
                    232:        name -= sizeof(Cell); \
                    233:       }
                    234: #endif
1.30      anton     235: 
1.34      anton     236: define(enginerest,
                    237: `(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
1.1       anton     238: /* executes code at ip, if ip!=NULL
                    239:    returns array of machine code labels (for use in a loader), if ip==NULL
                    240: */
                    241: {
1.10      anton     242: #ifndef GFORTH_DEBUGGING
                    243:   register Cell *rp RPREG;
                    244: #endif
1.46      anton     245: #ifndef NO_IP
                    246:   register Xt *ip IPREG = ip0;
                    247: #endif
1.1       anton     248:   register Cell *sp SPREG = sp0;
                    249:   register Float *fp FPREG = fp0;
                    250:   register Address lp LPREG = lp0;
                    251:   register Xt cfa CFAREG;
1.11      anton     252: #ifdef MORE_VARS
                    253:   MORE_VARS
                    254: #endif
1.1       anton     255:   register Address up UPREG = UP;
1.24      anton     256:   IF_spTOS(register Cell spTOS TOSREG;)
                    257:   IF_fpTOS(register Float fpTOS FTOSREG;)
1.1       anton     258: #if defined(DOUBLY_INDIRECT)
                    259:   static Label *symbols;
                    260:   static void *routines[]= {
1.27      anton     261: #define MAX_SYMBOLS (sizeof(routines)/sizeof(routines[0]))
1.1       anton     262: #else /* !defined(DOUBLY_INDIRECT) */
                    263:   static Label symbols[]= {
1.27      anton     264: #define MAX_SYMBOLS (sizeof(symbols)/sizeof(symbols[0]))
1.1       anton     265: #endif /* !defined(DOUBLY_INDIRECT) */
1.4       pazsan    266:     (Label)&&docol,
                    267:     (Label)&&docon,
                    268:     (Label)&&dovar,
                    269:     (Label)&&douser,
                    270:     (Label)&&dodefer,
                    271:     (Label)&&dofield,
                    272:     (Label)&&dodoes,
1.1       anton     273:     /* the following entry is normally unused;
1.34      anton     274:        it is there because its index indicates a does-handler */
1.7       pazsan    275:     CPU_DEP1,
1.34      anton     276: #define INST_ADDR(name) (Label)&&I_##name
                    277: #include "prim_lab.i"
                    278: #undef INST_ADDR
                    279:     (Label)&&after_last,
                    280:     (Label)0,
1.46      anton     281: #define INST_ADDR(name) (Label)&&K_##name
                    282: #include "prim_lab.i"
                    283: #undef INST_ADDR
1.34      anton     284: #ifdef IN_ENGINE2
                    285: #define INST_ADDR(name) (Label)&&J_##name
1.1       anton     286: #include "prim_lab.i"
1.34      anton     287: #undef INST_ADDR
                    288: #endif
1.1       anton     289:   };
                    290: #ifdef CPU_DEP2
                    291:   CPU_DEP2
                    292: #endif
                    293: 
1.10      anton     294:   rp = rp0;
1.1       anton     295: #ifdef DEBUG
                    296:   fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
1.46      anton     297:           (unsigned)ip0,(unsigned)sp,(unsigned)rp,
1.1       anton     298:          (unsigned)fp,(unsigned)lp,(unsigned)up);
                    299: #endif
                    300: 
1.46      anton     301:   if (ip0 == NULL) {
1.1       anton     302: #if defined(DOUBLY_INDIRECT)
1.38      anton     303: #define CODE_OFFSET (26*sizeof(Cell))
                    304: #define XT_OFFSET (22*sizeof(Cell))
1.1       anton     305:     int i;
1.3       anton     306:     Cell code_offset = offset_image? CODE_OFFSET : 0;
1.38      anton     307:     Cell xt_offset = offset_image? XT_OFFSET : 0;
1.7       pazsan    308: 
1.3       anton     309:     symbols = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+CODE_OFFSET)+code_offset);
1.38      anton     310:     xts = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+XT_OFFSET)+xt_offset);
1.1       anton     311:     for (i=0; i<DOESJUMP+1; i++)
1.38      anton     312:       xts[i] = symbols[i] = (Label)routines[i];
1.1       anton     313:     for (; routines[i]!=0; i++) {
                    314:       if (i>=MAX_SYMBOLS) {
                    315:        fprintf(stderr,"gforth-ditc: more than %d primitives\n",MAX_SYMBOLS);
                    316:        exit(1);
1.20      anton     317:       }
1.38      anton     318:       xts[i] = symbols[i] = &routines[i];
1.1       anton     319:     }
1.20      anton     320: #endif /* defined(DOUBLY_INDIRECT) */
                    321:     return symbols;
1.7       pazsan    322:   }
1.1       anton     323: 
1.24      anton     324:   IF_spTOS(spTOS = sp[0]);
                    325:   IF_fpTOS(fpTOS = fp[0]);
1.7       pazsan    326: /*  prep_terminal(); */
1.46      anton     327: #ifdef NO_IP
                    328:   goto *(*(Label *)ip0);
                    329: #else
1.11      anton     330:   SET_IP(ip);
1.28      anton     331:   SUPER_END; /* count the first block, too */
1.1       anton     332:   NEXT;
1.46      anton     333: #endif
1.11      anton     334: 
1.1       anton     335: #ifdef CPU_DEP3
                    336:   CPU_DEP3
                    337: #endif
                    338:   
                    339:  docol:
                    340:   {
1.46      anton     341: #ifdef NO_IP
                    342:     *--rp = next_code;
                    343:     goto **(Label *)PFA1(cfa);
                    344: #else
1.1       anton     345: #ifdef DEBUG
1.37      pazsan    346:     {
                    347:       CFA_TO_NAME(cfa);
                    348:       fprintf(stderr,"%08lx: col: %08lx %.*s\n",(Cell)ip,(Cell)PFA1(cfa),
                    349:              len,name);
                    350:     }
1.1       anton     351: #endif
                    352: #ifdef CISC_NEXT
                    353:     /* this is the simple version */
                    354:     *--rp = (Cell)ip;
1.11      anton     355:     SET_IP((Xt *)PFA1(cfa));
1.28      anton     356:     SUPER_END;
1.1       anton     357:     NEXT;
                    358: #else
1.11      anton     359:     /* this one is important, so we help the compiler optimizing */
1.1       anton     360:     {
                    361:       DEF_CA
1.11      anton     362:       rp[-1] = (Cell)ip;
                    363:       SET_IP((Xt *)PFA1(cfa));
1.28      anton     364:       SUPER_END;
1.11      anton     365:       NEXT_P1;
                    366:       rp--;
                    367:       NEXT_P2;
1.1       anton     368:     }
                    369: #endif
1.46      anton     370: #endif
1.1       anton     371:   }
                    372: 
                    373:  docon:
                    374:   {
                    375: #ifdef DEBUG
                    376:     fprintf(stderr,"%08lx: con: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
                    377: #endif
                    378: #ifdef USE_TOS
1.24      anton     379:     *sp-- = spTOS;
                    380:     spTOS = *(Cell *)PFA1(cfa);
1.1       anton     381: #else
                    382:     *--sp = *(Cell *)PFA1(cfa);
                    383: #endif
                    384:   }
1.46      anton     385: #ifdef NO_IP
                    386:   goto *next_code;
                    387: #else
1.1       anton     388:   NEXT_P0;
                    389:   NEXT;
1.46      anton     390: #endif
1.1       anton     391:   
                    392:  dovar:
                    393:   {
                    394: #ifdef DEBUG
                    395:     fprintf(stderr,"%08lx: var: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
                    396: #endif
                    397: #ifdef USE_TOS
1.24      anton     398:     *sp-- = spTOS;
                    399:     spTOS = (Cell)PFA1(cfa);
1.1       anton     400: #else
                    401:     *--sp = (Cell)PFA1(cfa);
                    402: #endif
                    403:   }
1.46      anton     404: #ifdef NO_IP
                    405:   goto *next_code;
                    406: #else
1.1       anton     407:   NEXT_P0;
                    408:   NEXT;
1.46      anton     409: #endif
1.1       anton     410:   
                    411:  douser:
                    412:   {
                    413: #ifdef DEBUG
                    414:     fprintf(stderr,"%08lx: user: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
                    415: #endif
                    416: #ifdef USE_TOS
1.24      anton     417:     *sp-- = spTOS;
                    418:     spTOS = (Cell)(up+*(Cell*)PFA1(cfa));
1.1       anton     419: #else
                    420:     *--sp = (Cell)(up+*(Cell*)PFA1(cfa));
                    421: #endif
                    422:   }
1.46      anton     423: #ifdef NO_IP
                    424:   goto *next_code;
                    425: #else
1.1       anton     426:   NEXT_P0;
                    427:   NEXT;
1.46      anton     428: #endif
1.1       anton     429:   
                    430:  dodefer:
                    431:   {
                    432: #ifdef DEBUG
                    433:     fprintf(stderr,"%08lx: defer: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
                    434: #endif
1.28      anton     435:     SUPER_END;
1.1       anton     436:     EXEC(*(Xt *)PFA1(cfa));
                    437:   }
                    438: 
                    439:  dofield:
                    440:   {
                    441: #ifdef DEBUG
                    442:     fprintf(stderr,"%08lx: field: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
                    443: #endif
1.24      anton     444:     spTOS += *(Cell*)PFA1(cfa);
1.1       anton     445:   }
1.46      anton     446: #ifdef NO_IP
                    447:   goto *next_code;
                    448: #else
1.1       anton     449:   NEXT_P0;
                    450:   NEXT;
1.46      anton     451: #endif
1.1       anton     452: 
                    453:  dodoes:
                    454:   /* this assumes the following structure:
                    455:      defining-word:
                    456:      
                    457:      ...
                    458:      DOES>
                    459:      (possible padding)
                    460:      possibly handler: jmp dodoes
                    461:      (possible branch delay slot(s))
                    462:      Forth code after DOES>
                    463:      
                    464:      defined word:
                    465:      
                    466:      cfa: address of or jump to handler OR
                    467:           address of or jump to dodoes, address of DOES-code
                    468:      pfa:
                    469:      
                    470:      */
1.46      anton     471: #ifdef NO_IP
                    472:   *--rp = next_code;
                    473:   IF_spTOS(spTOS = sp[0]);
                    474:   sp--;
                    475:   spTOS = (Cell)PFA(cfa);
                    476:   goto **(Label *)DOES_CODE1(cfa);
                    477: #else
1.1       anton     478:   {
                    479:     /*    fprintf(stderr, "Got CFA %08lx at doescode %08lx/%08lx: does: %08lx\n",cfa,(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));*/
                    480: #ifdef DEBUG
                    481:     fprintf(stderr,"%08lx/%08lx: does: %08lx\n",(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));
                    482:     fflush(stderr);
                    483: #endif
                    484:     *--rp = (Cell)ip;
                    485:     /* PFA1 might collide with DOES_CODE1 here, so we use PFA */
                    486: #ifdef USE_TOS
1.24      anton     487:     *sp-- = spTOS;
                    488:     spTOS = (Cell)PFA(cfa);
1.1       anton     489: #else
                    490:     *--sp = (Cell)PFA(cfa);
                    491: #endif
1.11      anton     492:     SET_IP(DOES_CODE1(cfa));
1.28      anton     493:     SUPER_END;
1.24      anton     494:     /*    fprintf(stderr,"TOS = %08lx, IP=%08lx\n", spTOS, IP);*/
1.1       anton     495:   }
                    496:   NEXT;
1.46      anton     497: #endif
1.1       anton     498: 
1.34      anton     499: #ifndef IN_ENGINE2
1.43      anton     500: #define LABEL(name) I_##name:
1.34      anton     501: #else
1.43      anton     502: #define LABEL(name) J_##name: asm(".skip 16"); I_##name:
1.34      anton     503: #endif
1.46      anton     504: #define LABEL2(name) K_##name:
1.1       anton     505: #include "prim.i"
1.34      anton     506: #undef LABEL
                    507:   after_last: return (Label *)0;
                    508:   /*needed only to get the length of the last primitive */
                    509: }'
                    510: )
                    511: 
1.46      anton     512: #define VARIANT(v)     (v)
                    513: #define JUMP(target)   goto I_noop
                    514: 
1.34      anton     515: Label *engine enginerest
                    516: 
1.45      anton     517: #ifndef NO_DYNAMIC
1.46      anton     518: 
                    519: #ifdef NO_IP
                    520: #undef VARIANT
                    521: #define VARIANT(v)     ((v)^0xffffffff)
                    522: #undef JUMP
                    523: #define JUMP(target)   goto K_lit
                    524: Label *engine3 enginerest
                    525: #endif
                    526: 
                    527: #undef VARIANT
                    528: #define VARIANT(v)     (v)
                    529: #undef JUMP
                    530: #define JUMP(target)   goto I_noop
1.34      anton     531: #define IN_ENGINE2
                    532: Label *engine2 enginerest
1.45      anton     533: #endif

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