File:  [gforth] / gforth / engine / engine.c
Revision 1.90: download - view: text, annotated - select for diffs
Sat Mar 11 23:05:09 2006 UTC (14 years, 2 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added gforth_ prefix to some functions

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

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