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

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

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