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

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

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