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

1.1       anton       1: /* Gforth virtual machine (aka inner interpreter)
                      2: 
1.95      anton       3:   Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006 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.89      pazsan     49: #include <wchar.h>
1.21      anton      50: #include <sys/resource.h>
1.19      anton      51: #ifdef HAVE_FNMATCH_H
1.18      anton      52: #include <fnmatch.h>
1.19      anton      53: #else
                     54: #include "fnmatch.h"
                     55: #endif
1.4       pazsan     56: #else
1.96      pazsan     57: /* #include <systypes.h> */
1.4       pazsan     58: #endif
1.1       anton      59: 
                     60: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN) /* what else? */
                     61: #include <dlfcn.h>
                     62: #endif
1.8       pazsan     63: #if defined(_WIN32)
                     64: #include <windows.h>
                     65: #endif
1.1       anton      66: #ifdef hpux
                     67: #include <dl.h>
                     68: #endif
                     69: 
1.63      pazsan     70: #ifdef HAS_FFCALL
                     71: #include <avcall.h>
                     72: #include <callback.h>
                     73: #endif
                     74: 
1.84      pazsan     75: #ifdef HAS_LIBFFI
                     76: #include <ffi.h>
                     77: #endif
                     78: 
1.1       anton      79: #ifndef SEEK_SET
                     80: /* should be defined in stdio.h, but some systems don't have it */
                     81: #define SEEK_SET 0
                     82: #endif
                     83: 
1.53      anton      84: #ifndef HAVE_FSEEKO
                     85: #define fseeko fseek
                     86: #endif
                     87: 
                     88: #ifndef HAVE_FTELLO
                     89: #define ftello ftell
                     90: #endif
                     91: 
1.1       anton      92: #define NULLC '\0'
                     93: 
1.14      pazsan     94: #ifdef MEMCMP_AS_SUBROUTINE
                     95: extern int gforth_memcmp(const char * s1, const char * s2, size_t n);
1.15      anton      96: #define memcmp(s1,s2,n) gforth_memcmp(s1,s2,n)
1.14      pazsan     97: #endif
                     98: 
1.1       anton      99: #define NEWLINE        '\n'
1.94      anton     100: 
                    101: /* These two flags control whether divisions are checked by software.
                    102:    The CHECK_DIVISION_SW is for those cases where the event is a
                    103:    division by zero or overflow on the C level, and might be reported
                    104:    by hardware; we might check forr that in autoconf and set the
                    105:    switch appropriately, but currently don't.  The CHECK_DIVISION flag
                    106:    is for the other cases. */
                    107: #ifdef GFORTH_DEBUGGING
                    108: #define CHECK_DIVISION_SW 1
1.91      anton     109: #define CHECK_DIVISION 1
1.94      anton     110: #else
                    111: #define CHECK_DIVISION_SW 0
                    112: #define CHECK_DIVISION 0
                    113: #endif
1.1       anton     114: 
1.26      anton     115: /* conversion on fetch */
                    116: 
1.41      anton     117: #define vm_Cell2f(_cell,_x)            ((_x)=(Bool)(_cell))
                    118: #define vm_Cell2c(_cell,_x)            ((_x)=(Char)(_cell))
                    119: #define vm_Cell2n(_cell,_x)            ((_x)=(Cell)(_cell))
                    120: #define vm_Cell2w(_cell,_x)            ((_x)=(Cell)(_cell))
                    121: #define vm_Cell2u(_cell,_x)            ((_x)=(UCell)(_cell))
                    122: #define vm_Cell2a_(_cell,_x)           ((_x)=(Cell *)(_cell))
                    123: #define vm_Cell2c_(_cell,_x)           ((_x)=(Char *)(_cell))
                    124: #define vm_Cell2f_(_cell,_x)           ((_x)=(Float *)(_cell))
                    125: #define vm_Cell2df_(_cell,_x)          ((_x)=(DFloat *)(_cell))
                    126: #define vm_Cell2sf_(_cell,_x)          ((_x)=(SFloat *)(_cell))
                    127: #define vm_Cell2xt(_cell,_x)           ((_x)=(Xt)(_cell))
                    128: #define vm_Cell2f83name(_cell,_x)      ((_x)=(struct F83Name *)(_cell))
                    129: #define vm_Cell2longname(_cell,_x)     ((_x)=(struct Longname *)(_cell))
                    130: #define vm_Float2r(_float,_x)          (_x=_float)
1.26      anton     131: 
                    132: /* conversion on store */
                    133: 
1.41      anton     134: #define vm_f2Cell(_x,_cell)            ((_cell)=(Cell)(_x))
                    135: #define vm_c2Cell(_x,_cell)            ((_cell)=(Cell)(_x))
                    136: #define vm_n2Cell(_x,_cell)            ((_cell)=(Cell)(_x))
                    137: #define vm_w2Cell(_x,_cell)            ((_cell)=(Cell)(_x))
                    138: #define vm_u2Cell(_x,_cell)            ((_cell)=(Cell)(_x))
                    139: #define vm_a_2Cell(_x,_cell)           ((_cell)=(Cell)(_x))
                    140: #define vm_c_2Cell(_x,_cell)           ((_cell)=(Cell)(_x))
                    141: #define vm_f_2Cell(_x,_cell)           ((_cell)=(Cell)(_x))
                    142: #define vm_df_2Cell(_x,_cell)          ((_cell)=(Cell)(_x))
                    143: #define vm_sf_2Cell(_x,_cell)          ((_cell)=(Cell)(_x))
                    144: #define vm_xt2Cell(_x,_cell)           ((_cell)=(Cell)(_x))
                    145: #define vm_f83name2Cell(_x,_cell)      ((_cell)=(Cell)(_x))
                    146: #define vm_longname2Cell(_x,_cell)     ((_cell)=(Cell)(_x))
                    147: #define vm_r2Float(_x,_float)          (_float=_x)
1.26      anton     148: 
1.41      anton     149: #define vm_Cell2Cell(_x,_y)            (_y=_x)
1.29      anton     150: 
1.46      anton     151: #ifdef NO_IP
                    152: #define IMM_ARG(access,value)          (VARIANT(value))
                    153: #else
                    154: #define IMM_ARG(access,value)          (access)
                    155: #endif
                    156: 
1.1       anton     157: /* if machine.h has not defined explicit registers, define them as implicit */
                    158: #ifndef IPREG
                    159: #define IPREG
                    160: #endif
                    161: #ifndef SPREG
                    162: #define SPREG
                    163: #endif
                    164: #ifndef RPREG
                    165: #define RPREG
                    166: #endif
                    167: #ifndef FPREG
                    168: #define FPREG
                    169: #endif
                    170: #ifndef LPREG
                    171: #define LPREG
                    172: #endif
1.79      pazsan    173: #ifndef CAREG
                    174: #define CAREG
                    175: #endif
1.1       anton     176: #ifndef CFAREG
                    177: #define CFAREG
                    178: #endif
                    179: #ifndef UPREG
                    180: #define UPREG
                    181: #endif
                    182: #ifndef TOSREG
                    183: #define TOSREG
                    184: #endif
1.69      anton     185: #ifndef spbREG
                    186: #define spbREG
                    187: #endif
1.74      anton     188: #ifndef spcREG
                    189: #define spcREG
                    190: #endif
1.80      anton     191: #ifndef spdREG
                    192: #define spdREG
                    193: #endif
                    194: #ifndef speREG
                    195: #define speREG
                    196: #endif
1.81      anton     197: #ifndef spfREG
                    198: #define spfREG
                    199: #endif
                    200: #ifndef spgREG
                    201: #define spgREG
                    202: #endif
                    203: #ifndef sphREG
                    204: #define sphREG
                    205: #endif
1.1       anton     206: #ifndef FTOSREG
                    207: #define FTOSREG
                    208: #endif
                    209: 
                    210: #ifndef CPU_DEP1
                    211: # define CPU_DEP1 0
                    212: #endif
                    213: 
1.62      anton     214: /* instructions containing SUPER_END must be the last instruction of a
1.28      anton     215:    super-instruction (e.g., branches, EXECUTE, and other instructions
                    216:    ending the basic block). Instructions containing SET_IP get this
                    217:    automatically, so you usually don't have to write it.  If you have
                    218:    to write it, write it after IP points to the next instruction.
                    219:    Used for profiling.  Don't write it in a word containing SET_IP, or
                    220:    the following block will be counted twice. */
                    221: #ifdef VM_PROFILING
                    222: #define SUPER_END  vm_count_block(IP)
                    223: #else
                    224: #define SUPER_END
                    225: #endif
1.35      anton     226: #define SUPER_CONTINUE
1.46      anton     227: 
1.98      anton     228: #ifdef ASMCOMMENT
                    229: /* an individualized asm statement so that (hopefully) gcc's optimizer
                    230:    does not do cross-jumping */
                    231: #define asmcomment(string) asm(ASMCOMMENT string)
                    232: #else
                    233: /* we don't know how to do an asm comment, so we just do an empty asm */
                    234: #define asmcomment(string) asm("")
                    235: #endif
                    236: 
1.66      anton     237: #ifdef GFORTH_DEBUGGING
1.70      anton     238: #if DEBUG
1.102   ! pazsan    239: #define NAME(string) { saved_ip=ip; asmcomment(string); fprintf(stderr,"%08lx depth=%3ld tos=%016lx: "string"\n",(Cell)ip,sp0+3-sp,sp[0]);}
1.70      anton     240: #else /* !DEBUG */
1.66      anton     241: #define NAME(string) { saved_ip=ip; asm(""); }
                    242: /* the asm here is to avoid reordering of following stuff above the
                    243:    assignment; this is an old-style asm (no operands), and therefore
                    244:    is treated like "asm volatile ..."; i.e., it prevents most
                    245:    reorderings across itself.  We want the assignment above first,
                    246:    because the stack loads may already cause a stack underflow. */
1.70      anton     247: #endif /* !DEBUG */
1.66      anton     248: #elif DEBUG
1.72      anton     249: #       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     250: #else
1.98      anton     251: #      define  NAME(string) asmcomment(string);
1.66      anton     252: #endif
                    253: 
1.37      pazsan    254: #ifdef DEBUG
                    255: #define CFA_TO_NAME(__cfa) \
                    256:       Cell len, i; \
                    257:       char * name = __cfa; \
                    258:       for(i=0; i<32; i+=sizeof(Cell)) { \
                    259:         len = ((Cell*)name)[-1]; \
                    260:         if(len < 0) { \
                    261:          len &= 0x1F; \
                    262:           if((len+sizeof(Cell)) > i) break; \
                    263:        } len = 0; \
                    264:        name -= sizeof(Cell); \
                    265:       }
                    266: #endif
1.30      anton     267: 
1.101     pazsan    268: #ifdef STANDALONE
                    269: jmp_buf throw_jmp_buf;
                    270: 
                    271: void throw(int code)
                    272: {
                    273:   longjmp(throw_jmp_buf,code); /* !! or use siglongjmp ? */
                    274: }
                    275: #endif
                    276: 
1.85      pazsan    277: #if defined(HAS_FFCALL) || defined(HAS_LIBFFI)
1.88      pazsan    278: #define SAVE_REGS IF_fpTOS(fp[0]=fpTOS); gforth_SP=sp; gforth_FP=fp; gforth_RP=rp; gforth_LP=lp;
                    279: #define REST_REGS sp=gforth_SP; fp=gforth_FP; rp=gforth_RP; lp=gforth_LP; IF_fpTOS(fpTOS=fp[0]);
1.63      pazsan    280: #endif
                    281: 
1.50      anton     282: #if !defined(ENGINE)
                    283: /* normal engine */
                    284: #define VARIANT(v)     (v)
                    285: #define JUMP(target)   goto I_noop
1.82      anton     286: #define LABEL(name) H_##name: asm(""); I_##name:
1.100     anton     287: #define LABEL3(name) J_##name: asm("");
1.50      anton     288: 
                    289: #elif ENGINE==2
                    290: /* variant with padding between VM instructions for finding out
                    291:    cross-inst jumps (for dynamic code) */
1.90      pazsan    292: #define gforth_engine gforth_engine2
1.50      anton     293: #define VARIANT(v)     (v)
                    294: #define JUMP(target)   goto I_noop
1.78      anton     295: #define LABEL(name) H_##name: SKIP16; I_##name:
1.100     anton     296: /* the SKIP16 after LABEL3 is there, because the ARM gcc may place
                    297:    some constants after the final branch, and may refer to them from
                    298:    the code before label3.  Since we don't copy the constants, we have
                    299:    to make sure that such code is recognized as non-relocatable. */
                    300: #define LABEL3(name) J_##name: SKIP16;
1.50      anton     301: 
                    302: #elif ENGINE==3
                    303: /* variant with different immediate arguments for finding out
                    304:    immediate arguments (for native code) */
1.90      pazsan    305: #define gforth_engine gforth_engine3
1.50      anton     306: #define VARIANT(v)     ((v)^0xffffffff)
                    307: #define JUMP(target)   goto K_lit
1.83      anton     308: #define LABEL(name) H_##name: asm(""); I_##name:
1.100     anton     309: #define LABEL3(name) J_##name: asm("");
1.50      anton     310: #else
                    311: #error illegal ENGINE value
                    312: #endif /* ENGINE */
                    313: 
1.67      anton     314: /* the asm(""); is there to get a stop compiled on Itanium */
                    315: #define LABEL2(name) K_##name: asm("");
1.50      anton     316: 
1.90      pazsan    317: Label *gforth_engine(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
1.1       anton     318: /* executes code at ip, if ip!=NULL
                    319:    returns array of machine code labels (for use in a loader), if ip==NULL
                    320: */
                    321: {
1.10      anton     322: #ifndef GFORTH_DEBUGGING
                    323:   register Cell *rp RPREG;
                    324: #endif
1.46      anton     325: #ifndef NO_IP
                    326:   register Xt *ip IPREG = ip0;
                    327: #endif
1.1       anton     328:   register Cell *sp SPREG = sp0;
                    329:   register Float *fp FPREG = fp0;
                    330:   register Address lp LPREG = lp0;
                    331:   register Xt cfa CFAREG;
1.79      pazsan    332:   register Label real_ca CAREG;
1.11      anton     333: #ifdef MORE_VARS
                    334:   MORE_VARS
1.63      pazsan    335: #endif
                    336: #ifdef HAS_FFCALL
                    337:   av_alist alist;
1.90      pazsan    338:   extern va_alist gforth_clist;
1.63      pazsan    339:   float frv;
                    340:   int irv;
                    341:   double drv;
                    342:   long long llrv;
                    343:   void * prv;
1.84      pazsan    344: #endif
                    345: #ifdef HAS_LIBFFI
1.90      pazsan    346:   extern void * gforth_ritem;
                    347:   extern void ** gforth_clist;
1.84      pazsan    348:   extern void ffi_callback(ffi_cif * cif, void * resp, void ** args, Xt * ip);
1.11      anton     349: #endif
1.88      pazsan    350:   register Address up UPREG = gforth_UP;
1.80      anton     351:   register Cell MAYBE_UNUSED spTOS TOSREG;
1.74      anton     352:   register Cell MAYBE_UNUSED spb spbREG;
                    353:   register Cell MAYBE_UNUSED spc spcREG;
1.80      anton     354:   register Cell MAYBE_UNUSED spd spdREG;
                    355:   register Cell MAYBE_UNUSED spe speREG;
1.81      anton     356:   register Cell MAYBE_UNUSED spf speREG;
                    357:   register Cell MAYBE_UNUSED spg speREG;
                    358:   register Cell MAYBE_UNUSED sph speREG;
1.24      anton     359:   IF_fpTOS(register Float fpTOS FTOSREG;)
1.1       anton     360: #if defined(DOUBLY_INDIRECT)
                    361:   static Label *symbols;
                    362:   static void *routines[]= {
1.27      anton     363: #define MAX_SYMBOLS (sizeof(routines)/sizeof(routines[0]))
1.1       anton     364: #else /* !defined(DOUBLY_INDIRECT) */
                    365:   static Label symbols[]= {
1.27      anton     366: #define MAX_SYMBOLS (sizeof(symbols)/sizeof(symbols[0]))
1.1       anton     367: #endif /* !defined(DOUBLY_INDIRECT) */
1.55      anton     368: #define INST_ADDR(name) ((Label)&&I_##name)
1.71      anton     369: #include PRIM_LAB_I
1.34      anton     370: #undef INST_ADDR
                    371:     (Label)0,
1.55      anton     372: #define INST_ADDR(name) ((Label)&&K_##name)
1.71      anton     373: #include PRIM_LAB_I
1.46      anton     374: #undef INST_ADDR
1.55      anton     375: #define INST_ADDR(name) ((Label)&&J_##name)
1.71      anton     376: #include PRIM_LAB_I
1.34      anton     377: #undef INST_ADDR
1.76      anton     378:     (Label)&&after_last,
                    379:     (Label)&&before_goto,
1.78      anton     380:     (Label)&&after_goto,
                    381: /* just mention the H_ labels, so the SKIP16s are not optimized away */
                    382: #define INST_ADDR(name) ((Label)&&H_##name)
                    383: #include PRIM_LAB_I
                    384: #undef INST_ADDR
1.1       anton     385:   };
1.99      pazsan    386: #ifdef STANDALONE
1.97      pazsan    387: #define INST_ADDR(name) ((Label)&&I_##name)
                    388: #include "image.i"
                    389: #undef INST_ADDR
                    390: #endif
1.1       anton     391: #ifdef CPU_DEP2
                    392:   CPU_DEP2
                    393: #endif
                    394: 
1.10      anton     395:   rp = rp0;
1.1       anton     396: #ifdef DEBUG
                    397:   fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
1.46      anton     398:           (unsigned)ip0,(unsigned)sp,(unsigned)rp,
1.1       anton     399:          (unsigned)fp,(unsigned)lp,(unsigned)up);
                    400: #endif
                    401: 
1.46      anton     402:   if (ip0 == NULL) {
1.1       anton     403: #if defined(DOUBLY_INDIRECT)
1.38      anton     404: #define CODE_OFFSET (26*sizeof(Cell))
                    405: #define XT_OFFSET (22*sizeof(Cell))
1.1       anton     406:     int i;
1.3       anton     407:     Cell code_offset = offset_image? CODE_OFFSET : 0;
1.38      anton     408:     Cell xt_offset = offset_image? XT_OFFSET : 0;
1.7       pazsan    409: 
1.3       anton     410:     symbols = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+CODE_OFFSET)+code_offset);
1.38      anton     411:     xts = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+XT_OFFSET)+xt_offset);
1.1       anton     412:     for (i=0; i<DOESJUMP+1; i++)
1.38      anton     413:       xts[i] = symbols[i] = (Label)routines[i];
1.1       anton     414:     for (; routines[i]!=0; i++) {
                    415:       if (i>=MAX_SYMBOLS) {
1.60      anton     416:        fprintf(stderr,"gforth-ditc: more than %ld primitives\n",(long)MAX_SYMBOLS);
1.1       anton     417:        exit(1);
1.20      anton     418:       }
1.38      anton     419:       xts[i] = symbols[i] = &routines[i];
1.1       anton     420:     }
1.20      anton     421: #endif /* defined(DOUBLY_INDIRECT) */
1.99      pazsan    422: #ifdef STANDALONE
                    423:     return image;
                    424: #else
1.20      anton     425:     return symbols;
1.99      pazsan    426: #endif
1.7       pazsan    427:   }
1.1       anton     428: 
1.81      anton     429: #if !(defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING))
1.80      anton     430:   sp += STACK_CACHE_DEFAULT-1;
1.81      anton     431:   /* some of those registers are dead, but its simpler to initialize them all */  spTOS = sp[0];
1.80      anton     432:   spb = sp[-1];
                    433:   spc = sp[-2];
                    434:   spd = sp[-3];
                    435:   spe = sp[-4];
1.81      anton     436:   spf = sp[-5];
                    437:   spg = sp[-6];
                    438:   sph = sp[-7];
1.80      anton     439: #endif
                    440: 
1.24      anton     441:   IF_fpTOS(fpTOS = fp[0]);
1.7       pazsan    442: /*  prep_terminal(); */
1.46      anton     443: #ifdef NO_IP
                    444:   goto *(*(Label *)ip0);
1.79      pazsan    445:   before_goto:
                    446:   goto *real_ca;
                    447:   after_goto:;
1.46      anton     448: #else
1.11      anton     449:   SET_IP(ip);
1.28      anton     450:   SUPER_END; /* count the first block, too */
1.79      pazsan    451:   FIRST_NEXT;
1.46      anton     452: #endif
1.11      anton     453: 
1.1       anton     454: #ifdef CPU_DEP3
                    455:   CPU_DEP3
1.46      anton     456: #endif
1.76      anton     457: 
1.78      anton     458: #include PRIM_I
                    459:   after_last: return (Label *)0;
                    460:   /*needed only to get the length of the last primitive */
                    461: 
                    462:   return (Label *)0;
1.50      anton     463: }

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