File:  [gforth] / gforth / engine / engine.c
Revision 1.104: download - view: text, annotated - select for diffs
Mon Dec 31 17:34:59 2007 UTC (16 years, 3 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright years

    1: /* Gforth virtual machine (aka inner interpreter)
    2: 
    3:   Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006,2007 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: 
   28: #include "config.h"
   29: #include "forth.h"
   30: #include <ctype.h>
   31: #include <stdio.h>
   32: #include <string.h>
   33: #include <math.h>
   34: #include <assert.h>
   35: #include <stdlib.h>
   36: #include <errno.h>
   37: #include "io.h"
   38: #include "threaded.h"
   39: #ifndef STANDALONE
   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>
   47: #include <dirent.h>
   48: #include <wchar.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: /* 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
  108: #define CHECK_DIVISION 1
  109: #else
  110: #define CHECK_DIVISION_SW 0
  111: #define CHECK_DIVISION 0
  112: #endif
  113: 
  114: /* conversion on fetch */
  115: 
  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)
  130: 
  131: /* conversion on store */
  132: 
  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)
  147: 
  148: #define vm_Cell2Cell(_x,_y)		(_y=_x)
  149: 
  150: #ifdef NO_IP
  151: #define IMM_ARG(access,value)		(VARIANT(value))
  152: #else
  153: #define IMM_ARG(access,value)		(access)
  154: #endif
  155: 
  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
  172: #ifndef CAREG
  173: #define CAREG
  174: #endif
  175: #ifndef CFAREG
  176: #define CFAREG
  177: #endif
  178: #ifndef UPREG
  179: #define UPREG
  180: #endif
  181: #ifndef TOSREG
  182: #define TOSREG
  183: #endif
  184: #ifndef spbREG
  185: #define spbREG
  186: #endif
  187: #ifndef spcREG
  188: #define spcREG
  189: #endif
  190: #ifndef spdREG
  191: #define spdREG
  192: #endif
  193: #ifndef speREG
  194: #define speREG
  195: #endif
  196: #ifndef spfREG
  197: #define spfREG
  198: #endif
  199: #ifndef spgREG
  200: #define spgREG
  201: #endif
  202: #ifndef sphREG
  203: #define sphREG
  204: #endif
  205: #ifndef FTOSREG
  206: #define FTOSREG
  207: #endif
  208: 
  209: #ifndef CPU_DEP1
  210: # define CPU_DEP1 0
  211: #endif
  212: 
  213: /* instructions containing SUPER_END must be the last instruction of a
  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
  225: #define SUPER_CONTINUE
  226: 
  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: 
  236: #ifdef GFORTH_DEBUGGING
  237: #if DEBUG
  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]);}
  239: #else /* !DEBUG */
  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. */
  246: #endif /* !DEBUG */
  247: #elif DEBUG
  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); }
  249: #else
  250: #	define	NAME(string) asmcomment(string);
  251: #endif
  252: 
  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
  266: 
  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: 
  276: #if defined(HAS_FFCALL) || defined(HAS_LIBFFI)
  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]);
  279: #endif
  280: 
  281: #if !defined(ENGINE)
  282: /* normal engine */
  283: #define VARIANT(v)	(v)
  284: #define JUMP(target)	goto I_noop
  285: #define LABEL(name) H_##name: asm(""); I_##name:
  286: #define LABEL3(name) J_##name: asm("");
  287: 
  288: #elif ENGINE==2
  289: /* variant with padding between VM instructions for finding out
  290:    cross-inst jumps (for dynamic code) */
  291: #define gforth_engine gforth_engine2
  292: #define VARIANT(v)	(v)
  293: #define JUMP(target)	goto I_noop
  294: #define LABEL(name) H_##name: SKIP16; I_##name:
  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;
  300: 
  301: #elif ENGINE==3
  302: /* variant with different immediate arguments for finding out
  303:    immediate arguments (for native code) */
  304: #define gforth_engine gforth_engine3
  305: #define VARIANT(v)	((v)^0xffffffff)
  306: #define JUMP(target)	goto K_lit
  307: #define LABEL(name) H_##name: asm(""); I_##name:
  308: #define LABEL3(name) J_##name: asm("");
  309: #else
  310: #error illegal ENGINE value
  311: #endif /* ENGINE */
  312: 
  313: /* the asm(""); is there to get a stop compiled on Itanium */
  314: #define LABEL2(name) K_##name: asm("");
  315: 
  316: Label *gforth_engine(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
  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: {
  321: #ifndef GFORTH_DEBUGGING
  322:   register Cell *rp RPREG;
  323: #endif
  324: #ifndef NO_IP
  325:   register Xt *ip IPREG = ip0;
  326: #endif
  327:   register Cell *sp SPREG = sp0;
  328:   register Float *fp FPREG = fp0;
  329:   register Address lp LPREG = lp0;
  330:   register Xt cfa CFAREG;
  331:   register Label real_ca CAREG;
  332: #ifdef MORE_VARS
  333:   MORE_VARS
  334: #endif
  335: #ifdef HAS_FFCALL
  336:   av_alist alist;
  337:   extern va_alist gforth_clist;
  338:   float frv;
  339:   int irv;
  340:   double drv;
  341:   long long llrv;
  342:   void * prv;
  343: #endif
  344: #ifdef HAS_LIBFFI
  345:   extern void * gforth_ritem;
  346:   extern void ** gforth_clist;
  347:   extern void ffi_callback(ffi_cif * cif, void * resp, void ** args, Xt * ip);
  348: #endif
  349:   register Address up UPREG = gforth_UP;
  350:   register Cell MAYBE_UNUSED spTOS TOSREG;
  351:   register Cell MAYBE_UNUSED spb spbREG;
  352:   register Cell MAYBE_UNUSED spc spcREG;
  353:   register Cell MAYBE_UNUSED spd spdREG;
  354:   register Cell MAYBE_UNUSED spe speREG;
  355:   register Cell MAYBE_UNUSED spf speREG;
  356:   register Cell MAYBE_UNUSED spg speREG;
  357:   register Cell MAYBE_UNUSED sph speREG;
  358:   IF_fpTOS(register Float fpTOS FTOSREG;)
  359: #if defined(DOUBLY_INDIRECT)
  360:   static Label *symbols;
  361:   static void *routines[]= {
  362: #define MAX_SYMBOLS (sizeof(routines)/sizeof(routines[0]))
  363: #else /* !defined(DOUBLY_INDIRECT) */
  364:   static Label symbols[]= {
  365: #define MAX_SYMBOLS (sizeof(symbols)/sizeof(symbols[0]))
  366: #endif /* !defined(DOUBLY_INDIRECT) */
  367: #define INST_ADDR(name) ((Label)&&I_##name)
  368: #include PRIM_LAB_I
  369: #undef INST_ADDR
  370:     (Label)0,
  371: #define INST_ADDR(name) ((Label)&&K_##name)
  372: #include PRIM_LAB_I
  373: #undef INST_ADDR
  374: #define INST_ADDR(name) ((Label)&&J_##name)
  375: #include PRIM_LAB_I
  376: #undef INST_ADDR
  377:     (Label)&&after_last,
  378:     (Label)&&before_goto,
  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
  384:   };
  385: #ifdef STANDALONE
  386: #define INST_ADDR(name) ((Label)&&I_##name)
  387: #include "image.i"
  388: #undef INST_ADDR
  389: #endif
  390: #ifdef CPU_DEP2
  391:   CPU_DEP2
  392: #endif
  393: 
  394:   rp = rp0;
  395: #ifdef DEBUG
  396:   fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
  397:           (unsigned)ip0,(unsigned)sp,(unsigned)rp,
  398: 	  (unsigned)fp,(unsigned)lp,(unsigned)up);
  399: #endif
  400: 
  401:   if (ip0 == NULL) {
  402: #if defined(DOUBLY_INDIRECT)
  403: #define CODE_OFFSET (26*sizeof(Cell))
  404: #define XT_OFFSET (22*sizeof(Cell))
  405:     int i;
  406:     Cell code_offset = offset_image? CODE_OFFSET : 0;
  407:     Cell xt_offset = offset_image? XT_OFFSET : 0;
  408: 
  409:     symbols = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+CODE_OFFSET)+code_offset);
  410:     xts = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+XT_OFFSET)+xt_offset);
  411:     for (i=0; i<DOESJUMP+1; i++)
  412:       xts[i] = symbols[i] = (Label)routines[i];
  413:     for (; routines[i]!=0; i++) {
  414:       if (i>=MAX_SYMBOLS) {
  415: 	fprintf(stderr,"gforth-ditc: more than %ld primitives\n",(long)MAX_SYMBOLS);
  416: 	exit(1);
  417:       }
  418:       xts[i] = symbols[i] = &routines[i];
  419:     }
  420: #endif /* defined(DOUBLY_INDIRECT) */
  421: #ifdef STANDALONE
  422:     return image;
  423: #else
  424:     return symbols;
  425: #endif
  426:   }
  427: 
  428: #if !(defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING))
  429:   sp += STACK_CACHE_DEFAULT-1;
  430:   /* some of those registers are dead, but its simpler to initialize them all */  spTOS = sp[0];
  431:   spb = sp[-1];
  432:   spc = sp[-2];
  433:   spd = sp[-3];
  434:   spe = sp[-4];
  435:   spf = sp[-5];
  436:   spg = sp[-6];
  437:   sph = sp[-7];
  438: #endif
  439: 
  440:   IF_fpTOS(fpTOS = fp[0]);
  441: /*  prep_terminal(); */
  442: #ifdef NO_IP
  443:   goto *(*(Label *)ip0);
  444:   before_goto:
  445:   goto *real_ca;
  446:   after_goto:;
  447: #else
  448:   SET_IP(ip);
  449:   SUPER_END; /* count the first block, too */
  450:   FIRST_NEXT;
  451: #endif
  452: 
  453: #ifdef CPU_DEP3
  454:   CPU_DEP3
  455: #endif
  456: 
  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;
  462: }

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