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

1.1       anton       1: /* Gforth virtual machine (aka inner interpreter)
                      2: 
1.61      anton       3:   Copyright (C) 1995,1996,1997,1998,2000,2003 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: 
1.63      pazsan     62: #ifdef HAS_FFCALL
                     63: #include <avcall.h>
                     64: #include <callback.h>
                     65: #endif
                     66: 
1.1       anton      67: #ifndef SEEK_SET
                     68: /* should be defined in stdio.h, but some systems don't have it */
                     69: #define SEEK_SET 0
                     70: #endif
                     71: 
1.53      anton      72: #ifndef HAVE_FSEEKO
                     73: #define fseeko fseek
                     74: #endif
                     75: 
                     76: #ifndef HAVE_FTELLO
                     77: #define ftello ftell
                     78: #endif
                     79: 
1.1       anton      80: #define NULLC '\0'
                     81: 
1.14      pazsan     82: #ifdef MEMCMP_AS_SUBROUTINE
                     83: extern int gforth_memcmp(const char * s1, const char * s2, size_t n);
1.15      anton      84: #define memcmp(s1,s2,n) gforth_memcmp(s1,s2,n)
1.14      pazsan     85: #endif
                     86: 
1.1       anton      87: #define NEWLINE        '\n'
                     88: 
1.26      anton      89: /* conversion on fetch */
                     90: 
1.41      anton      91: #define vm_Cell2f(_cell,_x)            ((_x)=(Bool)(_cell))
                     92: #define vm_Cell2c(_cell,_x)            ((_x)=(Char)(_cell))
                     93: #define vm_Cell2n(_cell,_x)            ((_x)=(Cell)(_cell))
                     94: #define vm_Cell2w(_cell,_x)            ((_x)=(Cell)(_cell))
                     95: #define vm_Cell2u(_cell,_x)            ((_x)=(UCell)(_cell))
                     96: #define vm_Cell2a_(_cell,_x)           ((_x)=(Cell *)(_cell))
                     97: #define vm_Cell2c_(_cell,_x)           ((_x)=(Char *)(_cell))
                     98: #define vm_Cell2f_(_cell,_x)           ((_x)=(Float *)(_cell))
                     99: #define vm_Cell2df_(_cell,_x)          ((_x)=(DFloat *)(_cell))
                    100: #define vm_Cell2sf_(_cell,_x)          ((_x)=(SFloat *)(_cell))
                    101: #define vm_Cell2xt(_cell,_x)           ((_x)=(Xt)(_cell))
                    102: #define vm_Cell2f83name(_cell,_x)      ((_x)=(struct F83Name *)(_cell))
                    103: #define vm_Cell2longname(_cell,_x)     ((_x)=(struct Longname *)(_cell))
                    104: #define vm_Float2r(_float,_x)          (_x=_float)
1.26      anton     105: 
                    106: /* conversion on store */
                    107: 
1.41      anton     108: #define vm_f2Cell(_x,_cell)            ((_cell)=(Cell)(_x))
                    109: #define vm_c2Cell(_x,_cell)            ((_cell)=(Cell)(_x))
                    110: #define vm_n2Cell(_x,_cell)            ((_cell)=(Cell)(_x))
                    111: #define vm_w2Cell(_x,_cell)            ((_cell)=(Cell)(_x))
                    112: #define vm_u2Cell(_x,_cell)            ((_cell)=(Cell)(_x))
                    113: #define vm_a_2Cell(_x,_cell)           ((_cell)=(Cell)(_x))
                    114: #define vm_c_2Cell(_x,_cell)           ((_cell)=(Cell)(_x))
                    115: #define vm_f_2Cell(_x,_cell)           ((_cell)=(Cell)(_x))
                    116: #define vm_df_2Cell(_x,_cell)          ((_cell)=(Cell)(_x))
                    117: #define vm_sf_2Cell(_x,_cell)          ((_cell)=(Cell)(_x))
                    118: #define vm_xt2Cell(_x,_cell)           ((_cell)=(Cell)(_x))
                    119: #define vm_f83name2Cell(_x,_cell)      ((_cell)=(Cell)(_x))
                    120: #define vm_longname2Cell(_x,_cell)     ((_cell)=(Cell)(_x))
                    121: #define vm_r2Float(_x,_float)          (_float=_x)
1.26      anton     122: 
1.41      anton     123: #define vm_Cell2Cell(_x,_y)            (_y=_x)
1.29      anton     124: 
1.46      anton     125: #ifdef NO_IP
                    126: #define IMM_ARG(access,value)          (VARIANT(value))
                    127: #else
                    128: #define IMM_ARG(access,value)          (access)
                    129: #endif
                    130: 
1.1       anton     131: /* if machine.h has not defined explicit registers, define them as implicit */
                    132: #ifndef IPREG
                    133: #define IPREG
                    134: #endif
                    135: #ifndef SPREG
                    136: #define SPREG
                    137: #endif
                    138: #ifndef RPREG
                    139: #define RPREG
                    140: #endif
                    141: #ifndef FPREG
                    142: #define FPREG
                    143: #endif
                    144: #ifndef LPREG
                    145: #define LPREG
                    146: #endif
                    147: #ifndef CFAREG
                    148: #define CFAREG
                    149: #endif
                    150: #ifndef UPREG
                    151: #define UPREG
                    152: #endif
                    153: #ifndef TOSREG
                    154: #define TOSREG
                    155: #endif
                    156: #ifndef FTOSREG
                    157: #define FTOSREG
                    158: #endif
                    159: 
                    160: #ifndef CPU_DEP1
                    161: # define CPU_DEP1 0
                    162: #endif
                    163: 
1.62      anton     164: /* instructions containing SUPER_END must be the last instruction of a
1.28      anton     165:    super-instruction (e.g., branches, EXECUTE, and other instructions
                    166:    ending the basic block). Instructions containing SET_IP get this
                    167:    automatically, so you usually don't have to write it.  If you have
                    168:    to write it, write it after IP points to the next instruction.
                    169:    Used for profiling.  Don't write it in a word containing SET_IP, or
                    170:    the following block will be counted twice. */
                    171: #ifdef VM_PROFILING
                    172: #define SUPER_END  vm_count_block(IP)
                    173: #else
                    174: #define SUPER_END
                    175: #endif
1.35      anton     176: #define SUPER_CONTINUE
1.46      anton     177: 
1.66      anton     178: #ifdef GFORTH_DEBUGGING
                    179: #define NAME(string) { saved_ip=ip; asm(""); }
                    180: /* the asm here is to avoid reordering of following stuff above the
                    181:    assignment; this is an old-style asm (no operands), and therefore
                    182:    is treated like "asm volatile ..."; i.e., it prevents most
                    183:    reorderings across itself.  We want the assignment above first,
                    184:    because the stack loads may already cause a stack underflow. */
                    185: #elif DEBUG
                    186: #       define  NAME(string)    fprintf(stderr,"%08lx depth=%3ld: "string"\n",(Cell)ip,sp0+3-sp);
                    187: #else
                    188: #      define  NAME(string)
                    189: #endif
                    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.63      pazsan    205: #ifdef HAS_FFCALL
                    206: #define SAVE_REGS IF_spTOS(sp[0]=spTOS); IF_fpTOS(fp[0]=fpTOS); SP=sp; FP=fp; RP=rp; LP=lp;
                    207: #define REST_REGS sp=SP; fp=FP; rp=RP; lp=LP; IF_spTOS(spTOS=sp[0]); IF_fpTOS(fpTOS=fp[0]);
                    208: #endif
                    209: 
1.50      anton     210: #if !defined(ENGINE)
                    211: /* normal engine */
                    212: #define VARIANT(v)     (v)
                    213: #define JUMP(target)   goto I_noop
1.51      anton     214: #define LABEL(name) J_##name: asm(""); I_##name:
1.50      anton     215: 
                    216: #elif ENGINE==2
                    217: /* variant with padding between VM instructions for finding out
                    218:    cross-inst jumps (for dynamic code) */
                    219: #define engine engine2
                    220: #define VARIANT(v)     (v)
                    221: #define JUMP(target)   goto I_noop
1.52      anton     222: #define LABEL(name) J_##name: SKIP16; I_##name:
1.50      anton     223: #define IN_ENGINE2
                    224: 
                    225: #elif ENGINE==3
                    226: /* variant with different immediate arguments for finding out
                    227:    immediate arguments (for native code) */
                    228: #define engine engine3
                    229: #define VARIANT(v)     ((v)^0xffffffff)
                    230: #define JUMP(target)   goto K_lit
1.51      anton     231: #define LABEL(name) J_##name: asm(""); I_##name:
1.50      anton     232: #else
                    233: #error illegal ENGINE value
                    234: #endif /* ENGINE */
                    235: 
1.67    ! anton     236: /* the asm(""); is there to get a stop compiled on Itanium */
        !           237: #define LABEL2(name) K_##name: asm("");
1.50      anton     238: 
                    239: Label *engine(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
1.1       anton     240: /* executes code at ip, if ip!=NULL
                    241:    returns array of machine code labels (for use in a loader), if ip==NULL
                    242: */
                    243: {
1.10      anton     244: #ifndef GFORTH_DEBUGGING
                    245:   register Cell *rp RPREG;
                    246: #endif
1.46      anton     247: #ifndef NO_IP
                    248:   register Xt *ip IPREG = ip0;
                    249: #endif
1.1       anton     250:   register Cell *sp SPREG = sp0;
                    251:   register Float *fp FPREG = fp0;
                    252:   register Address lp LPREG = lp0;
                    253:   register Xt cfa CFAREG;
1.11      anton     254: #ifdef MORE_VARS
                    255:   MORE_VARS
1.63      pazsan    256: #endif
                    257: #ifdef HAS_FFCALL
                    258:   av_alist alist;
                    259:   extern va_alist clist;
                    260:   float frv;
                    261:   int irv;
                    262:   double drv;
                    263:   long long llrv;
                    264:   void * prv;
1.11      anton     265: #endif
1.1       anton     266:   register Address up UPREG = UP;
1.24      anton     267:   IF_spTOS(register Cell spTOS TOSREG;)
                    268:   IF_fpTOS(register Float fpTOS FTOSREG;)
1.1       anton     269: #if defined(DOUBLY_INDIRECT)
                    270:   static Label *symbols;
                    271:   static void *routines[]= {
1.27      anton     272: #define MAX_SYMBOLS (sizeof(routines)/sizeof(routines[0]))
1.1       anton     273: #else /* !defined(DOUBLY_INDIRECT) */
                    274:   static Label symbols[]= {
1.27      anton     275: #define MAX_SYMBOLS (sizeof(symbols)/sizeof(symbols[0]))
1.1       anton     276: #endif /* !defined(DOUBLY_INDIRECT) */
1.55      anton     277: #define INST_ADDR(name) ((Label)&&I_##name)
1.34      anton     278: #include "prim_lab.i"
                    279: #undef INST_ADDR
                    280:     (Label)0,
1.55      anton     281: #define INST_ADDR(name) ((Label)&&K_##name)
1.46      anton     282: #include "prim_lab.i"
                    283: #undef INST_ADDR
1.55      anton     284: #define INST_ADDR(name) ((Label)&&J_##name)
1.1       anton     285: #include "prim_lab.i"
1.34      anton     286: #undef INST_ADDR
1.64      anton     287:     (Label)&&after_last
1.1       anton     288:   };
                    289: #ifdef CPU_DEP2
                    290:   CPU_DEP2
                    291: #endif
                    292: 
1.10      anton     293:   rp = rp0;
1.1       anton     294: #ifdef DEBUG
                    295:   fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
1.46      anton     296:           (unsigned)ip0,(unsigned)sp,(unsigned)rp,
1.1       anton     297:          (unsigned)fp,(unsigned)lp,(unsigned)up);
                    298: #endif
                    299: 
1.46      anton     300:   if (ip0 == NULL) {
1.1       anton     301: #if defined(DOUBLY_INDIRECT)
1.38      anton     302: #define CODE_OFFSET (26*sizeof(Cell))
                    303: #define XT_OFFSET (22*sizeof(Cell))
1.1       anton     304:     int i;
1.3       anton     305:     Cell code_offset = offset_image? CODE_OFFSET : 0;
1.38      anton     306:     Cell xt_offset = offset_image? XT_OFFSET : 0;
1.7       pazsan    307: 
1.3       anton     308:     symbols = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+CODE_OFFSET)+code_offset);
1.38      anton     309:     xts = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+XT_OFFSET)+xt_offset);
1.1       anton     310:     for (i=0; i<DOESJUMP+1; i++)
1.38      anton     311:       xts[i] = symbols[i] = (Label)routines[i];
1.1       anton     312:     for (; routines[i]!=0; i++) {
                    313:       if (i>=MAX_SYMBOLS) {
1.60      anton     314:        fprintf(stderr,"gforth-ditc: more than %ld primitives\n",(long)MAX_SYMBOLS);
1.1       anton     315:        exit(1);
1.20      anton     316:       }
1.38      anton     317:       xts[i] = symbols[i] = &routines[i];
1.1       anton     318:     }
1.20      anton     319: #endif /* defined(DOUBLY_INDIRECT) */
                    320:     return symbols;
1.7       pazsan    321:   }
1.1       anton     322: 
1.24      anton     323:   IF_spTOS(spTOS = sp[0]);
                    324:   IF_fpTOS(fpTOS = fp[0]);
1.7       pazsan    325: /*  prep_terminal(); */
1.46      anton     326: #ifdef NO_IP
                    327:   goto *(*(Label *)ip0);
                    328: #else
1.11      anton     329:   SET_IP(ip);
1.28      anton     330:   SUPER_END; /* count the first block, too */
1.1       anton     331:   NEXT;
1.46      anton     332: #endif
1.11      anton     333: 
1.1       anton     334: #ifdef CPU_DEP3
                    335:   CPU_DEP3
1.46      anton     336: #endif
1.1       anton     337: 
                    338: #include "prim.i"
1.34      anton     339:   after_last: return (Label *)0;
                    340:   /*needed only to get the length of the last primitive */
1.50      anton     341: }

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