File:  [gforth] / gforth / engine / engine.c
Revision 1.64: download - view: text, annotated - select for diffs
Fri Aug 15 14:07:04 2003 UTC (16 years, 9 months ago) by anton
Branches: MAIN
CVS tags: HEAD
made superinstructions compatible with conditional compilation of primitives
  (in Gforth; don't use conditional compilation in vmgen).

    1: /* Gforth virtual machine (aka inner interpreter)
    2: 
    3:   Copyright (C) 1995,1996,1997,1998,2000,2003 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: #include "config.h"
   23: #include "forth.h"
   24: #include <ctype.h>
   25: #include <stdio.h>
   26: #include <string.h>
   27: #include <math.h>
   28: #include <assert.h>
   29: #include <stdlib.h>
   30: #include <errno.h>
   31: #include "io.h"
   32: #include "threaded.h"
   33: #ifndef STANDALONE
   34: #include <sys/types.h>
   35: #include <sys/stat.h>
   36: #include <fcntl.h>
   37: #include <time.h>
   38: #include <sys/time.h>
   39: #include <unistd.h>
   40: #include <pwd.h>
   41: #include <dirent.h>
   42: #include <sys/resource.h>
   43: #ifdef HAVE_FNMATCH_H
   44: #include <fnmatch.h>
   45: #else
   46: #include "fnmatch.h"
   47: #endif
   48: #else
   49: #include "systypes.h"
   50: #endif
   51: 
   52: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN) /* what else? */
   53: #include <dlfcn.h>
   54: #endif
   55: #if defined(_WIN32)
   56: #include <windows.h>
   57: #endif
   58: #ifdef hpux
   59: #include <dl.h>
   60: #endif
   61: 
   62: #ifdef HAS_FFCALL
   63: #include <avcall.h>
   64: #include <callback.h>
   65: #endif
   66: 
   67: #ifndef SEEK_SET
   68: /* should be defined in stdio.h, but some systems don't have it */
   69: #define SEEK_SET 0
   70: #endif
   71: 
   72: #ifndef HAVE_FSEEKO
   73: #define fseeko fseek
   74: #endif
   75: 
   76: #ifndef HAVE_FTELLO
   77: #define ftello ftell
   78: #endif
   79: 
   80: #define NULLC '\0'
   81: 
   82: #ifdef MEMCMP_AS_SUBROUTINE
   83: extern int gforth_memcmp(const char * s1, const char * s2, size_t n);
   84: #define memcmp(s1,s2,n) gforth_memcmp(s1,s2,n)
   85: #endif
   86: 
   87: #define NEWLINE	'\n'
   88: 
   89: /* conversion on fetch */
   90: 
   91: #define vm_Cell2f(_cell,_x)		((_x)=(Bool)(_cell))
   92: #define vm_Cell2c(_cell,_x)		((_x)=(Char)(_cell))
   93: #define vm_Cell2n(_cell,_x)		((_x)=(Cell)(_cell))
   94: #define vm_Cell2w(_cell,_x)		((_x)=(Cell)(_cell))
   95: #define vm_Cell2u(_cell,_x)		((_x)=(UCell)(_cell))
   96: #define vm_Cell2a_(_cell,_x)		((_x)=(Cell *)(_cell))
   97: #define vm_Cell2c_(_cell,_x)		((_x)=(Char *)(_cell))
   98: #define vm_Cell2f_(_cell,_x)		((_x)=(Float *)(_cell))
   99: #define vm_Cell2df_(_cell,_x)		((_x)=(DFloat *)(_cell))
  100: #define vm_Cell2sf_(_cell,_x)		((_x)=(SFloat *)(_cell))
  101: #define vm_Cell2xt(_cell,_x)		((_x)=(Xt)(_cell))
  102: #define vm_Cell2f83name(_cell,_x)	((_x)=(struct F83Name *)(_cell))
  103: #define vm_Cell2longname(_cell,_x)	((_x)=(struct Longname *)(_cell))
  104: #define vm_Float2r(_float,_x)		(_x=_float)
  105: 
  106: /* conversion on store */
  107: 
  108: #define vm_f2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  109: #define vm_c2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  110: #define vm_n2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  111: #define vm_w2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  112: #define vm_u2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  113: #define vm_a_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  114: #define vm_c_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  115: #define vm_f_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  116: #define vm_df_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  117: #define vm_sf_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  118: #define vm_xt2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  119: #define vm_f83name2Cell(_x,_cell)	((_cell)=(Cell)(_x))
  120: #define vm_longname2Cell(_x,_cell)	((_cell)=(Cell)(_x))
  121: #define vm_r2Float(_x,_float)		(_float=_x)
  122: 
  123: #define vm_Cell2Cell(_x,_y)		(_y=_x)
  124: 
  125: #ifdef NO_IP
  126: #define IMM_ARG(access,value)		(VARIANT(value))
  127: #else
  128: #define IMM_ARG(access,value)		(access)
  129: #endif
  130: 
  131: /* if machine.h has not defined explicit registers, define them as implicit */
  132: #ifndef IPREG
  133: #define IPREG
  134: #endif
  135: #ifndef SPREG
  136: #define SPREG
  137: #endif
  138: #ifndef RPREG
  139: #define RPREG
  140: #endif
  141: #ifndef FPREG
  142: #define FPREG
  143: #endif
  144: #ifndef LPREG
  145: #define LPREG
  146: #endif
  147: #ifndef CFAREG
  148: #define CFAREG
  149: #endif
  150: #ifndef UPREG
  151: #define UPREG
  152: #endif
  153: #ifndef TOSREG
  154: #define TOSREG
  155: #endif
  156: #ifndef FTOSREG
  157: #define FTOSREG
  158: #endif
  159: 
  160: #ifndef CPU_DEP1
  161: # define CPU_DEP1 0
  162: #endif
  163: 
  164: /* instructions containing SUPER_END must be the last instruction of a
  165:    super-instruction (e.g., branches, EXECUTE, and other instructions
  166:    ending the basic block). Instructions containing SET_IP get this
  167:    automatically, so you usually don't have to write it.  If you have
  168:    to write it, write it after IP points to the next instruction.
  169:    Used for profiling.  Don't write it in a word containing SET_IP, or
  170:    the following block will be counted twice. */
  171: #ifdef VM_PROFILING
  172: #define SUPER_END  vm_count_block(IP)
  173: #else
  174: #define SUPER_END
  175: #endif
  176: #define SUPER_CONTINUE
  177: 
  178: #ifdef DEBUG
  179: #define CFA_TO_NAME(__cfa) \
  180:       Cell len, i; \
  181:       char * name = __cfa; \
  182:       for(i=0; i<32; i+=sizeof(Cell)) { \
  183:         len = ((Cell*)name)[-1]; \
  184:         if(len < 0) { \
  185: 	  len &= 0x1F; \
  186:           if((len+sizeof(Cell)) > i) break; \
  187: 	} len = 0; \
  188: 	name -= sizeof(Cell); \
  189:       }
  190: #endif
  191: 
  192: #ifdef HAS_FFCALL
  193: #define SAVE_REGS IF_spTOS(sp[0]=spTOS); IF_fpTOS(fp[0]=fpTOS); SP=sp; FP=fp; RP=rp; LP=lp;
  194: #define REST_REGS sp=SP; fp=FP; rp=RP; lp=LP; IF_spTOS(spTOS=sp[0]); IF_fpTOS(fpTOS=fp[0]);
  195: #endif
  196: 
  197: #if !defined(ENGINE)
  198: /* normal engine */
  199: #define VARIANT(v)	(v)
  200: #define JUMP(target)	goto I_noop
  201: #define LABEL(name) J_##name: asm(""); I_##name:
  202: 
  203: #elif ENGINE==2
  204: /* variant with padding between VM instructions for finding out
  205:    cross-inst jumps (for dynamic code) */
  206: #define engine engine2
  207: #define VARIANT(v)	(v)
  208: #define JUMP(target)	goto I_noop
  209: #define LABEL(name) J_##name: SKIP16; I_##name:
  210: #define IN_ENGINE2
  211: 
  212: #elif ENGINE==3
  213: /* variant with different immediate arguments for finding out
  214:    immediate arguments (for native code) */
  215: #define engine engine3
  216: #define VARIANT(v)	((v)^0xffffffff)
  217: #define JUMP(target)	goto K_lit
  218: #define LABEL(name) J_##name: asm(""); I_##name:
  219: #else
  220: #error illegal ENGINE value
  221: #endif /* ENGINE */
  222: 
  223: #define LABEL2(name) K_##name:
  224: 
  225: Label *engine(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
  226: /* executes code at ip, if ip!=NULL
  227:    returns array of machine code labels (for use in a loader), if ip==NULL
  228: */
  229: {
  230: #ifndef GFORTH_DEBUGGING
  231:   register Cell *rp RPREG;
  232: #endif
  233: #ifndef NO_IP
  234:   register Xt *ip IPREG = ip0;
  235: #endif
  236:   register Cell *sp SPREG = sp0;
  237:   register Float *fp FPREG = fp0;
  238:   register Address lp LPREG = lp0;
  239:   register Xt cfa CFAREG;
  240: #ifdef MORE_VARS
  241:   MORE_VARS
  242: #endif
  243: #ifdef HAS_FFCALL
  244:   av_alist alist;
  245:   extern va_alist clist;
  246:   float frv;
  247:   int irv;
  248:   double drv;
  249:   long long llrv;
  250:   void * prv;
  251: #endif
  252:   register Address up UPREG = UP;
  253:   IF_spTOS(register Cell spTOS TOSREG;)
  254:   IF_fpTOS(register Float fpTOS FTOSREG;)
  255: #if defined(DOUBLY_INDIRECT)
  256:   static Label *symbols;
  257:   static void *routines[]= {
  258: #define MAX_SYMBOLS (sizeof(routines)/sizeof(routines[0]))
  259: #else /* !defined(DOUBLY_INDIRECT) */
  260:   static Label symbols[]= {
  261: #define MAX_SYMBOLS (sizeof(symbols)/sizeof(symbols[0]))
  262: #endif /* !defined(DOUBLY_INDIRECT) */
  263:     (Label)&&docol,
  264:     (Label)&&docon,
  265:     (Label)&&dovar,
  266:     (Label)&&douser,
  267:     (Label)&&dodefer,
  268:     (Label)&&dofield,
  269:     (Label)&&dodoes,
  270:     /* the following entry is normally unused;
  271:        it is there because its index indicates a does-handler */
  272:     CPU_DEP1,
  273: #define INST_ADDR(name) ((Label)&&I_##name)
  274: #include "prim_lab.i"
  275: #undef INST_ADDR
  276:     (Label)&&after_last,
  277:     (Label)0,
  278: #define INST_ADDR(name) ((Label)&&K_##name)
  279: #include "prim_lab.i"
  280: #undef INST_ADDR
  281: #define INST_ADDR(name) ((Label)&&J_##name)
  282: #include "prim_lab.i"
  283: #undef INST_ADDR
  284:     (Label)&&after_last
  285:   };
  286: #ifdef CPU_DEP2
  287:   CPU_DEP2
  288: #endif
  289: 
  290:   rp = rp0;
  291: #ifdef DEBUG
  292:   fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
  293:           (unsigned)ip0,(unsigned)sp,(unsigned)rp,
  294: 	  (unsigned)fp,(unsigned)lp,(unsigned)up);
  295: #endif
  296: 
  297:   if (ip0 == NULL) {
  298: #if defined(DOUBLY_INDIRECT)
  299: #define CODE_OFFSET (26*sizeof(Cell))
  300: #define XT_OFFSET (22*sizeof(Cell))
  301:     int i;
  302:     Cell code_offset = offset_image? CODE_OFFSET : 0;
  303:     Cell xt_offset = offset_image? XT_OFFSET : 0;
  304: 
  305:     symbols = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+CODE_OFFSET)+code_offset);
  306:     xts = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+XT_OFFSET)+xt_offset);
  307:     for (i=0; i<DOESJUMP+1; i++)
  308:       xts[i] = symbols[i] = (Label)routines[i];
  309:     for (; routines[i]!=0; i++) {
  310:       if (i>=MAX_SYMBOLS) {
  311: 	fprintf(stderr,"gforth-ditc: more than %ld primitives\n",(long)MAX_SYMBOLS);
  312: 	exit(1);
  313:       }
  314:       xts[i] = symbols[i] = &routines[i];
  315:     }
  316: #endif /* defined(DOUBLY_INDIRECT) */
  317:     return symbols;
  318:   }
  319: 
  320:   IF_spTOS(spTOS = sp[0]);
  321:   IF_fpTOS(fpTOS = fp[0]);
  322: /*  prep_terminal(); */
  323: #ifdef NO_IP
  324:   goto *(*(Label *)ip0);
  325: #else
  326:   SET_IP(ip);
  327:   SUPER_END; /* count the first block, too */
  328:   NEXT;
  329: #endif
  330: 
  331: #ifdef CPU_DEP3
  332:   CPU_DEP3
  333: #endif
  334:   
  335:  docol:
  336:   {
  337: #ifdef NO_IP
  338:     *--rp = next_code;
  339:     goto **(Label *)PFA1(cfa);
  340: #else
  341: #ifdef DEBUG
  342:     {
  343:       CFA_TO_NAME(cfa);
  344:       fprintf(stderr,"%08lx: col: %08lx %.*s\n",(Cell)ip,(Cell)PFA1(cfa),
  345: 	      len,name);
  346:     }
  347: #endif
  348: #ifdef CISC_NEXT
  349:     /* this is the simple version */
  350:     *--rp = (Cell)ip;
  351:     SET_IP((Xt *)PFA1(cfa));
  352:     SUPER_END;
  353:     NEXT;
  354: #else
  355:     /* this one is important, so we help the compiler optimizing */
  356:     {
  357:       DEF_CA
  358:       rp[-1] = (Cell)ip;
  359:       SET_IP((Xt *)PFA1(cfa));
  360:       SUPER_END;
  361:       NEXT_P1;
  362:       rp--;
  363:       NEXT_P2;
  364:     }
  365: #endif
  366: #endif
  367:   }
  368: 
  369:  docon:
  370:   {
  371: #ifdef DEBUG
  372:     fprintf(stderr,"%08lx: con: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
  373: #endif
  374: #ifdef USE_TOS
  375:     *sp-- = spTOS;
  376:     spTOS = *(Cell *)PFA1(cfa);
  377: #else
  378:     *--sp = *(Cell *)PFA1(cfa);
  379: #endif
  380:   }
  381: #ifdef NO_IP
  382:   goto *next_code;
  383: #else
  384:   NEXT_P0;
  385:   NEXT;
  386: #endif
  387:   
  388:  dovar:
  389:   {
  390: #ifdef DEBUG
  391:     fprintf(stderr,"%08lx: var: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
  392: #endif
  393: #ifdef USE_TOS
  394:     *sp-- = spTOS;
  395:     spTOS = (Cell)PFA1(cfa);
  396: #else
  397:     *--sp = (Cell)PFA1(cfa);
  398: #endif
  399:   }
  400: #ifdef NO_IP
  401:   goto *next_code;
  402: #else
  403:   NEXT_P0;
  404:   NEXT;
  405: #endif
  406:   
  407:  douser:
  408:   {
  409: #ifdef DEBUG
  410:     fprintf(stderr,"%08lx: user: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
  411: #endif
  412: #ifdef USE_TOS
  413:     *sp-- = spTOS;
  414:     spTOS = (Cell)(up+*(Cell*)PFA1(cfa));
  415: #else
  416:     *--sp = (Cell)(up+*(Cell*)PFA1(cfa));
  417: #endif
  418:   }
  419: #ifdef NO_IP
  420:   goto *next_code;
  421: #else
  422:   NEXT_P0;
  423:   NEXT;
  424: #endif
  425:   
  426:  dodefer:
  427:   {
  428: #ifdef DEBUG
  429:     fprintf(stderr,"%08lx: defer: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
  430: #endif
  431:     SUPER_END;
  432:     EXEC(*(Xt *)PFA1(cfa));
  433:   }
  434: 
  435:  dofield:
  436:   {
  437: #ifdef DEBUG
  438:     fprintf(stderr,"%08lx: field: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
  439: #endif
  440:     spTOS += *(Cell*)PFA1(cfa);
  441:   }
  442: #ifdef NO_IP
  443:   goto *next_code;
  444: #else
  445:   NEXT_P0;
  446:   NEXT;
  447: #endif
  448: 
  449:  dodoes:
  450:   /* this assumes the following structure:
  451:      defining-word:
  452:      
  453:      ...
  454:      DOES>
  455:      (possible padding)
  456:      possibly handler: jmp dodoes
  457:      (possible branch delay slot(s))
  458:      Forth code after DOES>
  459:      
  460:      defined word:
  461:      
  462:      cfa: address of or jump to handler OR
  463:           address of or jump to dodoes, address of DOES-code
  464:      pfa:
  465:      
  466:      */
  467: #ifdef NO_IP
  468:   *--rp = next_code;
  469:   IF_spTOS(spTOS = sp[0]);
  470:   sp--;
  471:   spTOS = (Cell)PFA(cfa);
  472:   goto **(Label *)DOES_CODE1(cfa);
  473: #else
  474:   {
  475:     /*    fprintf(stderr, "Got CFA %08lx at doescode %08lx/%08lx: does: %08lx\n",cfa,(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));*/
  476: #ifdef DEBUG
  477:     fprintf(stderr,"%08lx/%08lx: does: %08lx\n",(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));
  478:     fflush(stderr);
  479: #endif
  480:     *--rp = (Cell)ip;
  481:     /* PFA1 might collide with DOES_CODE1 here, so we use PFA */
  482: #ifdef USE_TOS
  483:     *sp-- = spTOS;
  484:     spTOS = (Cell)PFA(cfa);
  485: #else
  486:     *--sp = (Cell)PFA(cfa);
  487: #endif
  488:     SET_IP(DOES_CODE1(cfa));
  489:     SUPER_END;
  490:     /*    fprintf(stderr,"TOS = %08lx, IP=%08lx\n", spTOS, IP);*/
  491:   }
  492:   NEXT;
  493: #endif
  494: 
  495: #include "prim.i"
  496:   after_last: return (Label *)0;
  497:   /*needed only to get the length of the last primitive */
  498: }

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