File:  [gforth] / gforth / engine / engine.c
Revision 1.88: download - view: text, annotated - select for diffs
Thu Jan 5 01:37:32 2006 UTC (18 years, 3 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Modified some exported symbols to start with gforth_

    1: /* Gforth virtual machine (aka inner interpreter)
    2: 
    3:   Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005 Free Software Foundation, Inc.
    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
   19:   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: */
   21: 
   22: #if defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING)
   23: #define USE_NO_TOS
   24: #else
   25: #define USE_TOS
   26: #endif
   27: #define USE_NO_FTOS
   28: 
   29: #include "config.h"
   30: #include "forth.h"
   31: #include <ctype.h>
   32: #include <stdio.h>
   33: #include <string.h>
   34: #include <math.h>
   35: #include <assert.h>
   36: #include <stdlib.h>
   37: #include <errno.h>
   38: #include "io.h"
   39: #include "threaded.h"
   40: #ifndef STANDALONE
   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>
   48: #include <dirent.h>
   49: #include <sys/resource.h>
   50: #ifdef HAVE_FNMATCH_H
   51: #include <fnmatch.h>
   52: #else
   53: #include "fnmatch.h"
   54: #endif
   55: #else
   56: #include "systypes.h"
   57: #endif
   58: 
   59: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN) /* what else? */
   60: #include <dlfcn.h>
   61: #endif
   62: #if defined(_WIN32)
   63: #include <windows.h>
   64: #endif
   65: #ifdef hpux
   66: #include <dl.h>
   67: #endif
   68: 
   69: #ifdef HAS_FFCALL
   70: #include <avcall.h>
   71: #include <callback.h>
   72: #endif
   73: 
   74: #ifdef HAS_LIBFFI
   75: #include <ffi.h>
   76: #endif
   77: 
   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: 
   83: #ifndef HAVE_FSEEKO
   84: #define fseeko fseek
   85: #endif
   86: 
   87: #ifndef HAVE_FTELLO
   88: #define ftello ftell
   89: #endif
   90: 
   91: #define NULLC '\0'
   92: 
   93: #ifdef MEMCMP_AS_SUBROUTINE
   94: extern int gforth_memcmp(const char * s1, const char * s2, size_t n);
   95: #define memcmp(s1,s2,n) gforth_memcmp(s1,s2,n)
   96: #endif
   97: 
   98: #define NEWLINE	'\n'
   99: 
  100: /* conversion on fetch */
  101: 
  102: #define vm_Cell2f(_cell,_x)		((_x)=(Bool)(_cell))
  103: #define vm_Cell2c(_cell,_x)		((_x)=(Char)(_cell))
  104: #define vm_Cell2n(_cell,_x)		((_x)=(Cell)(_cell))
  105: #define vm_Cell2w(_cell,_x)		((_x)=(Cell)(_cell))
  106: #define vm_Cell2u(_cell,_x)		((_x)=(UCell)(_cell))
  107: #define vm_Cell2a_(_cell,_x)		((_x)=(Cell *)(_cell))
  108: #define vm_Cell2c_(_cell,_x)		((_x)=(Char *)(_cell))
  109: #define vm_Cell2f_(_cell,_x)		((_x)=(Float *)(_cell))
  110: #define vm_Cell2df_(_cell,_x)		((_x)=(DFloat *)(_cell))
  111: #define vm_Cell2sf_(_cell,_x)		((_x)=(SFloat *)(_cell))
  112: #define vm_Cell2xt(_cell,_x)		((_x)=(Xt)(_cell))
  113: #define vm_Cell2f83name(_cell,_x)	((_x)=(struct F83Name *)(_cell))
  114: #define vm_Cell2longname(_cell,_x)	((_x)=(struct Longname *)(_cell))
  115: #define vm_Float2r(_float,_x)		(_x=_float)
  116: 
  117: /* conversion on store */
  118: 
  119: #define vm_f2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  120: #define vm_c2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  121: #define vm_n2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  122: #define vm_w2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  123: #define vm_u2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  124: #define vm_a_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  125: #define vm_c_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  126: #define vm_f_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  127: #define vm_df_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  128: #define vm_sf_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  129: #define vm_xt2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  130: #define vm_f83name2Cell(_x,_cell)	((_cell)=(Cell)(_x))
  131: #define vm_longname2Cell(_x,_cell)	((_cell)=(Cell)(_x))
  132: #define vm_r2Float(_x,_float)		(_float=_x)
  133: 
  134: #define vm_Cell2Cell(_x,_y)		(_y=_x)
  135: 
  136: #ifdef NO_IP
  137: #define IMM_ARG(access,value)		(VARIANT(value))
  138: #else
  139: #define IMM_ARG(access,value)		(access)
  140: #endif
  141: 
  142: /* if machine.h has not defined explicit registers, define them as implicit */
  143: #ifndef IPREG
  144: #define IPREG
  145: #endif
  146: #ifndef SPREG
  147: #define SPREG
  148: #endif
  149: #ifndef RPREG
  150: #define RPREG
  151: #endif
  152: #ifndef FPREG
  153: #define FPREG
  154: #endif
  155: #ifndef LPREG
  156: #define LPREG
  157: #endif
  158: #ifndef CAREG
  159: #define CAREG
  160: #endif
  161: #ifndef CFAREG
  162: #define CFAREG
  163: #endif
  164: #ifndef UPREG
  165: #define UPREG
  166: #endif
  167: #ifndef TOSREG
  168: #define TOSREG
  169: #endif
  170: #ifndef spbREG
  171: #define spbREG
  172: #endif
  173: #ifndef spcREG
  174: #define spcREG
  175: #endif
  176: #ifndef spdREG
  177: #define spdREG
  178: #endif
  179: #ifndef speREG
  180: #define speREG
  181: #endif
  182: #ifndef spfREG
  183: #define spfREG
  184: #endif
  185: #ifndef spgREG
  186: #define spgREG
  187: #endif
  188: #ifndef sphREG
  189: #define sphREG
  190: #endif
  191: #ifndef FTOSREG
  192: #define FTOSREG
  193: #endif
  194: 
  195: #ifndef CPU_DEP1
  196: # define CPU_DEP1 0
  197: #endif
  198: 
  199: /* instructions containing SUPER_END must be the last instruction of a
  200:    super-instruction (e.g., branches, EXECUTE, and other instructions
  201:    ending the basic block). Instructions containing SET_IP get this
  202:    automatically, so you usually don't have to write it.  If you have
  203:    to write it, write it after IP points to the next instruction.
  204:    Used for profiling.  Don't write it in a word containing SET_IP, or
  205:    the following block will be counted twice. */
  206: #ifdef VM_PROFILING
  207: #define SUPER_END  vm_count_block(IP)
  208: #else
  209: #define SUPER_END
  210: #endif
  211: #define SUPER_CONTINUE
  212: 
  213: #ifdef GFORTH_DEBUGGING
  214: #if DEBUG
  215: #define NAME(string) { saved_ip=ip; asm("# "string); fprintf(stderr,"%08lx depth=%3ld: "string"\n",(Cell)ip,sp0+3-sp);}
  216: #else /* !DEBUG */
  217: #define NAME(string) { saved_ip=ip; asm(""); }
  218: /* the asm here is to avoid reordering of following stuff above the
  219:    assignment; this is an old-style asm (no operands), and therefore
  220:    is treated like "asm volatile ..."; i.e., it prevents most
  221:    reorderings across itself.  We want the assignment above first,
  222:    because the stack loads may already cause a stack underflow. */
  223: #endif /* !DEBUG */
  224: #elif DEBUG
  225: #       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); }
  226: #else
  227: #	define	NAME(string) asm("# "string);
  228: #endif
  229: 
  230: #ifdef DEBUG
  231: #define CFA_TO_NAME(__cfa) \
  232:       Cell len, i; \
  233:       char * name = __cfa; \
  234:       for(i=0; i<32; i+=sizeof(Cell)) { \
  235:         len = ((Cell*)name)[-1]; \
  236:         if(len < 0) { \
  237: 	  len &= 0x1F; \
  238:           if((len+sizeof(Cell)) > i) break; \
  239: 	} len = 0; \
  240: 	name -= sizeof(Cell); \
  241:       }
  242: #endif
  243: 
  244: #if defined(HAS_FFCALL) || defined(HAS_LIBFFI)
  245: #define SAVE_REGS IF_fpTOS(fp[0]=fpTOS); gforth_SP=sp; gforth_FP=fp; gforth_RP=rp; gforth_LP=lp;
  246: #define REST_REGS sp=gforth_SP; fp=gforth_FP; rp=gforth_RP; lp=gforth_LP; IF_fpTOS(fpTOS=fp[0]);
  247: #endif
  248: 
  249: #if !defined(ENGINE)
  250: /* normal engine */
  251: #define VARIANT(v)	(v)
  252: #define JUMP(target)	goto I_noop
  253: #define LABEL(name) H_##name: asm(""); I_##name:
  254: 
  255: #elif ENGINE==2
  256: /* variant with padding between VM instructions for finding out
  257:    cross-inst jumps (for dynamic code) */
  258: #define engine engine2
  259: #define VARIANT(v)	(v)
  260: #define JUMP(target)	goto I_noop
  261: #define LABEL(name) H_##name: SKIP16; I_##name:
  262: 
  263: #elif ENGINE==3
  264: /* variant with different immediate arguments for finding out
  265:    immediate arguments (for native code) */
  266: #define engine engine3
  267: #define VARIANT(v)	((v)^0xffffffff)
  268: #define JUMP(target)	goto K_lit
  269: #define LABEL(name) H_##name: asm(""); I_##name:
  270: #else
  271: #error illegal ENGINE value
  272: #endif /* ENGINE */
  273: 
  274: /* the asm(""); is there to get a stop compiled on Itanium */
  275: #define LABEL2(name) K_##name: asm("");
  276: #define LABEL3(name) J_##name: asm("");
  277: 
  278: Label *engine(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
  279: /* executes code at ip, if ip!=NULL
  280:    returns array of machine code labels (for use in a loader), if ip==NULL
  281: */
  282: {
  283: #ifndef GFORTH_DEBUGGING
  284:   register Cell *rp RPREG;
  285: #endif
  286: #ifndef NO_IP
  287:   register Xt *ip IPREG = ip0;
  288: #endif
  289:   register Cell *sp SPREG = sp0;
  290:   register Float *fp FPREG = fp0;
  291:   register Address lp LPREG = lp0;
  292:   register Xt cfa CFAREG;
  293:   register Label real_ca CAREG;
  294: #ifdef MORE_VARS
  295:   MORE_VARS
  296: #endif
  297: #ifdef HAS_FFCALL
  298:   av_alist alist;
  299:   extern va_alist clist;
  300:   float frv;
  301:   int irv;
  302:   double drv;
  303:   long long llrv;
  304:   void * prv;
  305: #endif
  306: #ifdef HAS_LIBFFI
  307:   extern void * ritem;
  308:   extern void ** clist;
  309:   extern void ffi_callback(ffi_cif * cif, void * resp, void ** args, Xt * ip);
  310: #endif
  311:   register Address up UPREG = gforth_UP;
  312:   register Cell MAYBE_UNUSED spTOS TOSREG;
  313:   register Cell MAYBE_UNUSED spb spbREG;
  314:   register Cell MAYBE_UNUSED spc spcREG;
  315:   register Cell MAYBE_UNUSED spd spdREG;
  316:   register Cell MAYBE_UNUSED spe speREG;
  317:   register Cell MAYBE_UNUSED spf speREG;
  318:   register Cell MAYBE_UNUSED spg speREG;
  319:   register Cell MAYBE_UNUSED sph speREG;
  320:   IF_fpTOS(register Float fpTOS FTOSREG;)
  321: #if defined(DOUBLY_INDIRECT)
  322:   static Label *symbols;
  323:   static void *routines[]= {
  324: #define MAX_SYMBOLS (sizeof(routines)/sizeof(routines[0]))
  325: #else /* !defined(DOUBLY_INDIRECT) */
  326:   static Label symbols[]= {
  327: #define MAX_SYMBOLS (sizeof(symbols)/sizeof(symbols[0]))
  328: #endif /* !defined(DOUBLY_INDIRECT) */
  329: #define INST_ADDR(name) ((Label)&&I_##name)
  330: #include PRIM_LAB_I
  331: #undef INST_ADDR
  332:     (Label)0,
  333: #define INST_ADDR(name) ((Label)&&K_##name)
  334: #include PRIM_LAB_I
  335: #undef INST_ADDR
  336: #define INST_ADDR(name) ((Label)&&J_##name)
  337: #include PRIM_LAB_I
  338: #undef INST_ADDR
  339:     (Label)&&after_last,
  340:     (Label)&&before_goto,
  341:     (Label)&&after_goto,
  342: /* just mention the H_ labels, so the SKIP16s are not optimized away */
  343: #define INST_ADDR(name) ((Label)&&H_##name)
  344: #include PRIM_LAB_I
  345: #undef INST_ADDR
  346:   };
  347: #ifdef CPU_DEP2
  348:   CPU_DEP2
  349: #endif
  350: 
  351:   rp = rp0;
  352: #ifdef DEBUG
  353:   fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
  354:           (unsigned)ip0,(unsigned)sp,(unsigned)rp,
  355: 	  (unsigned)fp,(unsigned)lp,(unsigned)up);
  356: #endif
  357: 
  358:   if (ip0 == NULL) {
  359: #if defined(DOUBLY_INDIRECT)
  360: #define CODE_OFFSET (26*sizeof(Cell))
  361: #define XT_OFFSET (22*sizeof(Cell))
  362:     int i;
  363:     Cell code_offset = offset_image? CODE_OFFSET : 0;
  364:     Cell xt_offset = offset_image? XT_OFFSET : 0;
  365: 
  366:     symbols = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+CODE_OFFSET)+code_offset);
  367:     xts = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+XT_OFFSET)+xt_offset);
  368:     for (i=0; i<DOESJUMP+1; i++)
  369:       xts[i] = symbols[i] = (Label)routines[i];
  370:     for (; routines[i]!=0; i++) {
  371:       if (i>=MAX_SYMBOLS) {
  372: 	fprintf(stderr,"gforth-ditc: more than %ld primitives\n",(long)MAX_SYMBOLS);
  373: 	exit(1);
  374:       }
  375:       xts[i] = symbols[i] = &routines[i];
  376:     }
  377: #endif /* defined(DOUBLY_INDIRECT) */
  378:     return symbols;
  379:   }
  380: 
  381: #if !(defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING))
  382:   sp += STACK_CACHE_DEFAULT-1;
  383:   /* some of those registers are dead, but its simpler to initialize them all */  spTOS = sp[0];
  384:   spb = sp[-1];
  385:   spc = sp[-2];
  386:   spd = sp[-3];
  387:   spe = sp[-4];
  388:   spf = sp[-5];
  389:   spg = sp[-6];
  390:   sph = sp[-7];
  391: #endif
  392: 
  393:   IF_fpTOS(fpTOS = fp[0]);
  394: /*  prep_terminal(); */
  395: #ifdef NO_IP
  396:   goto *(*(Label *)ip0);
  397:   before_goto:
  398:   goto *real_ca;
  399:   after_goto:;
  400: #else
  401:   SET_IP(ip);
  402:   SUPER_END; /* count the first block, too */
  403:   FIRST_NEXT;
  404: #endif
  405: 
  406: #ifdef CPU_DEP3
  407:   CPU_DEP3
  408: #endif
  409: 
  410: #include PRIM_I
  411:   after_last: return (Label *)0;
  412:   /*needed only to get the length of the last primitive */
  413: 
  414:   return (Label *)0;
  415: }

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