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

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

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