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

1.1       anton       1: /* Gforth virtual machine (aka inner interpreter)
                      2: 
1.75      anton       3:   Copyright (C) 1995,1996,1997,1998,2000,2003,2004 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.72      anton      22: #if defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING)
1.68      anton      23: #define USE_NO_TOS
1.72      anton      24: #else
                     25: #define USE_TOS
                     26: #endif
1.68      anton      27: #define USE_NO_FTOS
                     28: 
1.1       anton      29: #include "config.h"
1.31      pazsan     30: #include "forth.h"
1.1       anton      31: #include <ctype.h>
                     32: #include <stdio.h>
                     33: #include <string.h>
                     34: #include <math.h>
1.4       pazsan     35: #include <assert.h>
                     36: #include <stdlib.h>
                     37: #include <errno.h>
                     38: #include "io.h"
                     39: #include "threaded.h"
                     40: #ifndef STANDALONE
1.1       anton      41: #include <sys/types.h>
                     42: #include <sys/stat.h>
                     43: #include <fcntl.h>
                     44: #include <time.h>
                     45: #include <sys/time.h>
                     46: #include <unistd.h>
                     47: #include <pwd.h>
1.17      pazsan     48: #include <dirent.h>
1.21      anton      49: #include <sys/resource.h>
1.19      anton      50: #ifdef HAVE_FNMATCH_H
1.18      anton      51: #include <fnmatch.h>
1.19      anton      52: #else
                     53: #include "fnmatch.h"
                     54: #endif
1.4       pazsan     55: #else
                     56: #include "systypes.h"
                     57: #endif
1.1       anton      58: 
                     59: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN) /* what else? */
                     60: #include <dlfcn.h>
                     61: #endif
1.8       pazsan     62: #if defined(_WIN32)
                     63: #include <windows.h>
                     64: #endif
1.1       anton      65: #ifdef hpux
                     66: #include <dl.h>
                     67: #endif
                     68: 
1.63      pazsan     69: #ifdef HAS_FFCALL
                     70: #include <avcall.h>
                     71: #include <callback.h>
                     72: #endif
                     73: 
1.1       anton      74: #ifndef SEEK_SET
                     75: /* should be defined in stdio.h, but some systems don't have it */
                     76: #define SEEK_SET 0
                     77: #endif
                     78: 
1.53      anton      79: #ifndef HAVE_FSEEKO
                     80: #define fseeko fseek
                     81: #endif
                     82: 
                     83: #ifndef HAVE_FTELLO
                     84: #define ftello ftell
                     85: #endif
                     86: 
1.1       anton      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
1.79      pazsan    154: #ifndef CAREG
                    155: #define CAREG
                    156: #endif
1.1       anton     157: #ifndef CFAREG
                    158: #define CFAREG
                    159: #endif
                    160: #ifndef UPREG
                    161: #define UPREG
                    162: #endif
                    163: #ifndef TOSREG
                    164: #define TOSREG
                    165: #endif
1.69      anton     166: #ifndef spbREG
                    167: #define spbREG
                    168: #endif
1.74      anton     169: #ifndef spcREG
                    170: #define spcREG
                    171: #endif
1.80    ! anton     172: #ifndef spdREG
        !           173: #define spdREG
        !           174: #endif
        !           175: #ifndef speREG
        !           176: #define speREG
        !           177: #endif
1.1       anton     178: #ifndef FTOSREG
                    179: #define FTOSREG
                    180: #endif
                    181: 
                    182: #ifndef CPU_DEP1
                    183: # define CPU_DEP1 0
                    184: #endif
                    185: 
1.62      anton     186: /* instructions containing SUPER_END must be the last instruction of a
1.28      anton     187:    super-instruction (e.g., branches, EXECUTE, and other instructions
                    188:    ending the basic block). Instructions containing SET_IP get this
                    189:    automatically, so you usually don't have to write it.  If you have
                    190:    to write it, write it after IP points to the next instruction.
                    191:    Used for profiling.  Don't write it in a word containing SET_IP, or
                    192:    the following block will be counted twice. */
                    193: #ifdef VM_PROFILING
                    194: #define SUPER_END  vm_count_block(IP)
                    195: #else
                    196: #define SUPER_END
                    197: #endif
1.35      anton     198: #define SUPER_CONTINUE
1.46      anton     199: 
1.66      anton     200: #ifdef GFORTH_DEBUGGING
1.70      anton     201: #if DEBUG
1.73      pazsan    202: #define NAME(string) { saved_ip=ip; asm("# "string); fprintf(stderr,"%08lx depth=%3ld: "string"\n",(Cell)ip,sp0+3-sp);}
1.70      anton     203: #else /* !DEBUG */
1.66      anton     204: #define NAME(string) { saved_ip=ip; asm(""); }
                    205: /* the asm here is to avoid reordering of following stuff above the
                    206:    assignment; this is an old-style asm (no operands), and therefore
                    207:    is treated like "asm volatile ..."; i.e., it prevents most
                    208:    reorderings across itself.  We want the assignment above first,
                    209:    because the stack loads may already cause a stack underflow. */
1.70      anton     210: #endif /* !DEBUG */
1.66      anton     211: #elif DEBUG
1.72      anton     212: #       define  NAME(string)    {Cell __depth=sp0+3-sp; int i; fprintf(stderr,"%08lx depth=%3ld: "string,(Cell)ip,sp0+3-sp); for (i=__depth-1; i>0; i--) fprintf(stderr, " $%lx",sp[i]); fprintf(stderr, " $%lx\n",spTOS); }
1.66      anton     213: #else
1.73      pazsan    214: #      define  NAME(string) asm("# "string);
1.66      anton     215: #endif
                    216: 
1.37      pazsan    217: #ifdef DEBUG
                    218: #define CFA_TO_NAME(__cfa) \
                    219:       Cell len, i; \
                    220:       char * name = __cfa; \
                    221:       for(i=0; i<32; i+=sizeof(Cell)) { \
                    222:         len = ((Cell*)name)[-1]; \
                    223:         if(len < 0) { \
                    224:          len &= 0x1F; \
                    225:           if((len+sizeof(Cell)) > i) break; \
                    226:        } len = 0; \
                    227:        name -= sizeof(Cell); \
                    228:       }
                    229: #endif
1.30      anton     230: 
1.63      pazsan    231: #ifdef HAS_FFCALL
1.80    ! anton     232: #define SAVE_REGS IF_fpTOS(fp[0]=fpTOS); SP=sp; FP=fp; RP=rp; LP=lp;
        !           233: #define REST_REGS sp=SP; fp=FP; rp=RP; lp=LP; IF_fpTOS(fpTOS=fp[0]);
1.63      pazsan    234: #endif
                    235: 
1.50      anton     236: #if !defined(ENGINE)
                    237: /* normal engine */
                    238: #define VARIANT(v)     (v)
                    239: #define JUMP(target)   goto I_noop
1.78      anton     240: #define LABEL(name) H_##name: I_##name:
1.50      anton     241: 
                    242: #elif ENGINE==2
                    243: /* variant with padding between VM instructions for finding out
                    244:    cross-inst jumps (for dynamic code) */
                    245: #define engine engine2
                    246: #define VARIANT(v)     (v)
                    247: #define JUMP(target)   goto I_noop
1.78      anton     248: #define LABEL(name) H_##name: SKIP16; I_##name:
1.50      anton     249: #define IN_ENGINE2
                    250: 
                    251: #elif ENGINE==3
                    252: /* variant with different immediate arguments for finding out
                    253:    immediate arguments (for native code) */
                    254: #define engine engine3
                    255: #define VARIANT(v)     ((v)^0xffffffff)
                    256: #define JUMP(target)   goto K_lit
1.78      anton     257: #define LABEL(name) H_##name: I_##name:
1.50      anton     258: #else
                    259: #error illegal ENGINE value
                    260: #endif /* ENGINE */
                    261: 
1.67      anton     262: /* the asm(""); is there to get a stop compiled on Itanium */
                    263: #define LABEL2(name) K_##name: asm("");
1.77      anton     264: #define LABEL3(name) J_##name: asm("");
1.50      anton     265: 
                    266: Label *engine(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
1.1       anton     267: /* executes code at ip, if ip!=NULL
                    268:    returns array of machine code labels (for use in a loader), if ip==NULL
                    269: */
                    270: {
1.10      anton     271: #ifndef GFORTH_DEBUGGING
                    272:   register Cell *rp RPREG;
                    273: #endif
1.46      anton     274: #ifndef NO_IP
                    275:   register Xt *ip IPREG = ip0;
                    276: #endif
1.1       anton     277:   register Cell *sp SPREG = sp0;
                    278:   register Float *fp FPREG = fp0;
                    279:   register Address lp LPREG = lp0;
                    280:   register Xt cfa CFAREG;
1.79      pazsan    281:   register Label real_ca CAREG;
1.11      anton     282: #ifdef MORE_VARS
                    283:   MORE_VARS
1.63      pazsan    284: #endif
                    285: #ifdef HAS_FFCALL
                    286:   av_alist alist;
                    287:   extern va_alist clist;
                    288:   float frv;
                    289:   int irv;
                    290:   double drv;
                    291:   long long llrv;
                    292:   void * prv;
1.11      anton     293: #endif
1.1       anton     294:   register Address up UPREG = UP;
1.80    ! anton     295:   register Cell MAYBE_UNUSED spTOS TOSREG;
1.74      anton     296:   register Cell MAYBE_UNUSED spb spbREG;
                    297:   register Cell MAYBE_UNUSED spc spcREG;
1.80    ! anton     298:   register Cell MAYBE_UNUSED spd spdREG;
        !           299:   register Cell MAYBE_UNUSED spe speREG;
1.24      anton     300:   IF_fpTOS(register Float fpTOS FTOSREG;)
1.1       anton     301: #if defined(DOUBLY_INDIRECT)
                    302:   static Label *symbols;
                    303:   static void *routines[]= {
1.27      anton     304: #define MAX_SYMBOLS (sizeof(routines)/sizeof(routines[0]))
1.1       anton     305: #else /* !defined(DOUBLY_INDIRECT) */
                    306:   static Label symbols[]= {
1.27      anton     307: #define MAX_SYMBOLS (sizeof(symbols)/sizeof(symbols[0]))
1.1       anton     308: #endif /* !defined(DOUBLY_INDIRECT) */
1.55      anton     309: #define INST_ADDR(name) ((Label)&&I_##name)
1.71      anton     310: #include PRIM_LAB_I
1.34      anton     311: #undef INST_ADDR
                    312:     (Label)0,
1.55      anton     313: #define INST_ADDR(name) ((Label)&&K_##name)
1.71      anton     314: #include PRIM_LAB_I
1.46      anton     315: #undef INST_ADDR
1.55      anton     316: #define INST_ADDR(name) ((Label)&&J_##name)
1.71      anton     317: #include PRIM_LAB_I
1.34      anton     318: #undef INST_ADDR
1.76      anton     319:     (Label)&&after_last,
                    320:     (Label)&&before_goto,
1.78      anton     321:     (Label)&&after_goto,
                    322: /* just mention the H_ labels, so the SKIP16s are not optimized away */
                    323: #define INST_ADDR(name) ((Label)&&H_##name)
                    324: #include PRIM_LAB_I
                    325: #undef INST_ADDR
1.1       anton     326:   };
                    327: #ifdef CPU_DEP2
                    328:   CPU_DEP2
                    329: #endif
                    330: 
1.10      anton     331:   rp = rp0;
1.1       anton     332: #ifdef DEBUG
                    333:   fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
1.46      anton     334:           (unsigned)ip0,(unsigned)sp,(unsigned)rp,
1.1       anton     335:          (unsigned)fp,(unsigned)lp,(unsigned)up);
                    336: #endif
                    337: 
1.46      anton     338:   if (ip0 == NULL) {
1.1       anton     339: #if defined(DOUBLY_INDIRECT)
1.38      anton     340: #define CODE_OFFSET (26*sizeof(Cell))
                    341: #define XT_OFFSET (22*sizeof(Cell))
1.1       anton     342:     int i;
1.3       anton     343:     Cell code_offset = offset_image? CODE_OFFSET : 0;
1.38      anton     344:     Cell xt_offset = offset_image? XT_OFFSET : 0;
1.7       pazsan    345: 
1.3       anton     346:     symbols = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+CODE_OFFSET)+code_offset);
1.38      anton     347:     xts = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+XT_OFFSET)+xt_offset);
1.1       anton     348:     for (i=0; i<DOESJUMP+1; i++)
1.38      anton     349:       xts[i] = symbols[i] = (Label)routines[i];
1.1       anton     350:     for (; routines[i]!=0; i++) {
                    351:       if (i>=MAX_SYMBOLS) {
1.60      anton     352:        fprintf(stderr,"gforth-ditc: more than %ld primitives\n",(long)MAX_SYMBOLS);
1.1       anton     353:        exit(1);
1.20      anton     354:       }
1.38      anton     355:       xts[i] = symbols[i] = &routines[i];
1.1       anton     356:     }
1.20      anton     357: #endif /* defined(DOUBLY_INDIRECT) */
                    358:     return symbols;
1.7       pazsan    359:   }
1.1       anton     360: 
1.80    ! anton     361: #if STACK_CACHE_DEFAULT>0
        !           362:   sp += STACK_CACHE_DEFAULT-1;
        !           363: #endif
        !           364: 
        !           365: #if STACK_CACHE_DEFAULT>0
        !           366:   spTOS = sp[0];
        !           367: #endif
        !           368: #if STACK_CACHE_DEFAULT>1
        !           369:   spb = sp[-1];
        !           370: #endif
        !           371: #if STACK_CACHE_DEFAULT>2
        !           372:   spc = sp[-2];
        !           373: #endif
        !           374: #if STACK_CACHE_DEFAULT>3
        !           375:   spd = sp[-3];
        !           376: #endif
        !           377: #if STACK_CACHE_DEFAULT>4
        !           378:   spe = sp[-4];
        !           379: #endif
        !           380: 
1.24      anton     381:   IF_fpTOS(fpTOS = fp[0]);
1.7       pazsan    382: /*  prep_terminal(); */
1.46      anton     383: #ifdef NO_IP
                    384:   goto *(*(Label *)ip0);
1.79      pazsan    385:   before_goto:
                    386:   goto *real_ca;
                    387:   after_goto:;
1.46      anton     388: #else
1.11      anton     389:   SET_IP(ip);
1.28      anton     390:   SUPER_END; /* count the first block, too */
1.79      pazsan    391:   FIRST_NEXT;
1.46      anton     392: #endif
1.11      anton     393: 
1.1       anton     394: #ifdef CPU_DEP3
                    395:   CPU_DEP3
1.46      anton     396: #endif
1.76      anton     397: 
1.78      anton     398: #include PRIM_I
                    399:   after_last: return (Label *)0;
                    400:   /*needed only to get the length of the last primitive */
                    401: 
                    402:   return (Label *)0;
1.50      anton     403: }

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