File:  [gforth] / gforth / engine / main.c
Revision 1.140: download - view: text, annotated - select for diffs
Sat Apr 10 00:16:55 2004 UTC (20 years ago) by pazsan
Branches: MAIN
CVS tags: HEAD
fixed "too many crashes" in httpd.fs
fixed reentrance problem with callbacks

    1: /* command line interpretation, image loading etc. for Gforth
    2: 
    3: 
    4:   Copyright (C) 1995,1996,1997,1998,2000,2003 Free Software Foundation, Inc.
    5: 
    6:   This file is part of Gforth.
    7: 
    8:   Gforth is free software; you can redistribute it and/or
    9:   modify it under the terms of the GNU General Public License
   10:   as published by the Free Software Foundation; either version 2
   11:   of the License, or (at your option) any later version.
   12: 
   13:   This program is distributed in the hope that it will be useful,
   14:   but WITHOUT ANY WARRANTY; without even the implied warranty of
   15:   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16:   GNU General Public License for more details.
   17: 
   18:   You should have received a copy of the GNU General Public License
   19:   along with this program; if not, write to the Free Software
   20:   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   21: */
   22: 
   23: #include "config.h"
   24: #include "forth.h"
   25: #include <errno.h>
   26: #include <ctype.h>
   27: #include <stdio.h>
   28: #include <unistd.h>
   29: #include <string.h>
   30: #include <math.h>
   31: #include <sys/types.h>
   32: #ifndef STANDALONE
   33: #include <sys/stat.h>
   34: #endif
   35: #include <fcntl.h>
   36: #include <assert.h>
   37: #include <stdlib.h>
   38: #include <signal.h>
   39: #ifndef STANDALONE
   40: #if HAVE_SYS_MMAN_H
   41: #include <sys/mman.h>
   42: #endif
   43: #endif
   44: #include "io.h"
   45: #include "getopt.h"
   46: #ifdef STANDALONE
   47: #include <systypes.h>
   48: #endif
   49: 
   50: typedef enum prim_num {
   51: /* definitions of N_execute etc. */
   52: #include PRIM_NUM_I
   53:   N_START_SUPER
   54: } PrimNum;
   55: 
   56: /* global variables for engine.c 
   57:    We put them here because engine.c is compiled several times in
   58:    different ways for the same engine. */
   59: Cell *SP;
   60: Float *FP;
   61: Address UP=NULL;
   62: 
   63: #ifdef HAS_FFCALL
   64: Cell *RP;
   65: Address LP;
   66: 
   67: #include <callback.h>
   68: 
   69: va_alist clist;
   70: 
   71: void engine_callback(Xt* fcall, void * alist)
   72: {
   73:   /* save global valiables */
   74:   Cell *rp = RP;
   75:   Cell *sp = SP;
   76:   Float *fp = FP;
   77:   Address lp = LP;
   78: 
   79:   clist = (va_alist)alist;
   80: 
   81:   engine(fcall, sp, rp, fp, lp);
   82: 
   83:   /* restore global variables */
   84:   RP = rp;
   85:   SP = sp;
   86:   FP = fp;
   87:   LP = lp;
   88: }
   89: #endif
   90: 
   91: #ifdef GFORTH_DEBUGGING
   92: /* define some VM registers as global variables, so they survive exceptions;
   93:    global register variables are not up to the task (according to the 
   94:    GNU C manual) */
   95: Xt *saved_ip;
   96: Cell *rp;
   97: #endif
   98: 
   99: #ifdef NO_IP
  100: Label next_code;
  101: #endif
  102: 
  103: #ifdef HAS_FILE
  104: char* fileattr[6]={"rb","rb","r+b","r+b","wb","wb"};
  105: char* pfileattr[6]={"r","r","r+","r+","w","w"};
  106: 
  107: #ifndef O_BINARY
  108: #define O_BINARY 0
  109: #endif
  110: #ifndef O_TEXT
  111: #define O_TEXT 0
  112: #endif
  113: 
  114: int ufileattr[6]= {
  115:   O_RDONLY|O_BINARY, O_RDONLY|O_BINARY,
  116:   O_RDWR  |O_BINARY, O_RDWR  |O_BINARY,
  117:   O_WRONLY|O_BINARY, O_WRONLY|O_BINARY };
  118: #endif
  119: /* end global vars for engine.c */
  120: 
  121: #define PRIM_VERSION 1
  122: /* increment this whenever the primitives change in an incompatible way */
  123: 
  124: #ifndef DEFAULTPATH
  125: #  define DEFAULTPATH "."
  126: #endif
  127: 
  128: #ifdef MSDOS
  129: jmp_buf throw_jmp_buf;
  130: #endif
  131: 
  132: #if defined(DOUBLY_INDIRECT)
  133: #  define CFA(n)	({Cell _n = (n); ((Cell)(((_n & 0x4000) ? symbols : xts)+(_n&~0x4000UL)));})
  134: #else
  135: #  define CFA(n)	((Cell)(symbols+((n)&~0x4000UL)))
  136: #endif
  137: 
  138: #define maxaligned(n)	(typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
  139: 
  140: static UCell dictsize=0;
  141: static UCell dsize=0;
  142: static UCell rsize=0;
  143: static UCell fsize=0;
  144: static UCell lsize=0;
  145: int offset_image=0;
  146: int die_on_signal=0;
  147: #ifndef INCLUDE_IMAGE
  148: static int clear_dictionary=0;
  149: UCell pagesize=1;
  150: char *progname;
  151: #else
  152: char *progname = "gforth";
  153: int optind = 1;
  154: #endif
  155: 
  156: #define CODE_BLOCK_SIZE (4096*1024) /* !! overflow handling for -native */
  157: Address code_area=0;
  158: Cell code_area_size = CODE_BLOCK_SIZE;
  159: Address code_here=NULL+CODE_BLOCK_SIZE; /* does for code-area what HERE
  160: 					   does for the dictionary */
  161: Address start_flush=NULL; /* start of unflushed code */
  162: Cell last_jump=0; /* if the last prim was compiled without jump, this
  163:                      is it's number, otherwise this contains 0 */
  164: 
  165: static int no_super=0;   /* true if compile_prim should not fuse prims */
  166: static int no_dynamic=NO_DYNAMIC_DEFAULT; /* if true, no code is generated
  167: 					     dynamically */
  168: static int print_metrics=0; /* if true, print metrics on exit */
  169: static int static_super_number = 10000000; /* number of ss used if available */
  170: #define MAX_STATE 4 /* maximum number of states */
  171: static int maxstates = MAX_STATE; /* number of states for stack caching */
  172: static int ss_greedy = 0; /* if true: use greedy, not optimal ss selection */
  173: 
  174: #ifdef HAS_DEBUG
  175: int debug=0;
  176: #else
  177: # define perror(x...)
  178: # define fprintf(x...)
  179: #endif
  180: 
  181: ImageHeader *gforth_header;
  182: Label *vm_prims;
  183: #ifdef DOUBLY_INDIRECT
  184: Label *xts; /* same content as vm_prims, but should only be used for xts */
  185: #endif
  186: 
  187: #ifndef NO_DYNAMIC
  188: #define MAX_IMMARGS 2
  189: 
  190: typedef struct {
  191:   Label start; /* NULL if not relocatable */
  192:   Cell length; /* only includes the jump iff superend is true*/
  193:   Cell restlength; /* length of the rest (i.e., the jump or (on superend) 0) */
  194:   char superend; /* true if primitive ends superinstruction, i.e.,
  195:                      unconditional branch, execute, etc. */
  196:   Cell nimmargs;
  197:   struct immarg {
  198:     Cell offset; /* offset of immarg within prim */
  199:     char rel;    /* true if immarg is relative */
  200:   } immargs[MAX_IMMARGS];
  201: } PrimInfo;
  202: 
  203: PrimInfo *priminfos;
  204: PrimInfo **decomp_prims;
  205: 
  206: const char const* const prim_names[]={
  207: #include PRIM_NAMES_I
  208: };
  209: 
  210: static int is_relocatable(int p)
  211: {
  212:   return !no_dynamic && priminfos[p].start != NULL;
  213: }
  214: #else /* defined(NO_DYNAMIC) */
  215: static int is_relocatable(int p)
  216: {
  217:   return 0;
  218: }
  219: #endif /* defined(NO_DYNAMIC) */
  220: 
  221: #ifdef MEMCMP_AS_SUBROUTINE
  222: int gforth_memcmp(const char * s1, const char * s2, size_t n)
  223: {
  224:   return memcmp(s1, s2, n);
  225: }
  226: #endif
  227: 
  228: static Cell max(Cell a, Cell b)
  229: {
  230:   return a>b?a:b;
  231: }
  232: 
  233: static Cell min(Cell a, Cell b)
  234: {
  235:   return a<b?a:b;
  236: }
  237: 
  238: /* image file format:
  239:  *  "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.4.0 -i\n")
  240:  *   padding to a multiple of 8
  241:  *   magic: "Gforth3x" means format 0.6,
  242:  *              where x is a byte with
  243:  *              bit 7:   reserved = 0
  244:  *              bit 6:5: address unit size 2^n octets
  245:  *              bit 4:3: character size 2^n octets
  246:  *              bit 2:1: cell size 2^n octets
  247:  *              bit 0:   endian, big=0, little=1.
  248:  *  The magic are always 8 octets, no matter what the native AU/character size is
  249:  *  padding to max alignment (no padding necessary on current machines)
  250:  *  ImageHeader structure (see forth.h)
  251:  *  data (size in ImageHeader.image_size)
  252:  *  tags ((if relocatable, 1 bit/data cell)
  253:  *
  254:  * tag==1 means that the corresponding word is an address;
  255:  * If the word is >=0, the address is within the image;
  256:  * addresses within the image are given relative to the start of the image.
  257:  * If the word =-1 (CF_NIL), the address is NIL,
  258:  * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
  259:  * If the word =CF(DODOES), it's a DOES> CFA
  260:  * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
  261:  *					possibly containing a jump to dodoes)
  262:  * If the word is <CF(DOESJUMP) and bit 14 is set, it's the xt of a primitive
  263:  * If the word is <CF(DOESJUMP) and bit 14 is clear, 
  264:  *                                        it's the threaded code of a primitive
  265:  * bits 13..9 of a primitive token state which group the primitive belongs to,
  266:  * bits 8..0 of a primitive token index into the group
  267:  */
  268: 
  269: Cell groups[32] = {
  270:   0,
  271:   0
  272: #undef GROUP
  273: #undef GROUPADD
  274: #define GROUPADD(n) +n
  275: #define GROUP(x, n) , 0
  276: #include PRIM_GRP_I
  277: #undef GROUP
  278: #undef GROUPADD
  279: #define GROUP(x, n)
  280: #define GROUPADD(n)
  281: };
  282: 
  283: unsigned char *branch_targets(Cell *image, const unsigned char *bitstring,
  284: 			      int size, Cell base)
  285:      /* produce a bitmask marking all the branch targets */
  286: {
  287:   int i=0, j, k, steps=(((size-1)/sizeof(Cell))/RELINFOBITS)+1;
  288:   Cell token;
  289:   unsigned char bits;
  290:   unsigned char *result=malloc(steps);
  291: 
  292:   memset(result, 0, steps);
  293:   for(k=0; k<steps; k++) {
  294:     for(j=0, bits=bitstring[k]; j<RELINFOBITS; j++, i++, bits<<=1) {
  295:       if(bits & (1U << (RELINFOBITS-1))) {
  296: 	assert(i*sizeof(Cell) < size);
  297:         token=image[i];
  298: 	if (token>=base) { /* relocatable address */
  299: 	  UCell bitnum=(token-base)/sizeof(Cell);
  300: 	  result[bitnum/RELINFOBITS] |= 1U << ((~bitnum)&(RELINFOBITS-1));
  301: 	}
  302:       }
  303:     }
  304:   }
  305:   return result;
  306: }
  307: 
  308: void relocate(Cell *image, const unsigned char *bitstring, 
  309:               int size, Cell base, Label symbols[])
  310: {
  311:   int i=0, j, k, steps=(((size-1)/sizeof(Cell))/RELINFOBITS)+1;
  312:   Cell token;
  313:   char bits;
  314:   Cell max_symbols;
  315:   /* 
  316:    * A virtual start address that's the real start address minus 
  317:    * the one in the image 
  318:    */
  319:   Cell *start = (Cell * ) (((void *) image) - ((void *) base));
  320:   unsigned char *targets = branch_targets(image, bitstring, size, base);
  321: 
  322:   /* group index into table */
  323:   if(groups[31]==0) {
  324:     int groupsum=0;
  325:     for(i=0; i<32; i++) {
  326:       groupsum += groups[i];
  327:       groups[i] = groupsum;
  328:       /* printf("group[%d]=%d\n",i,groupsum); */
  329:     }
  330:     i=0;
  331:   }
  332:   
  333: /* printf("relocating to %x[%x] start=%x base=%x\n", image, size, start, base); */
  334:   
  335:   for (max_symbols=0; symbols[max_symbols]!=0; max_symbols++)
  336:     ;
  337:   max_symbols--;
  338: 
  339:   for(k=0; k<steps; k++) {
  340:     for(j=0, bits=bitstring[k]; j<RELINFOBITS; j++, i++, bits<<=1) {
  341:       /*      fprintf(stderr,"relocate: image[%d]\n", i);*/
  342:       if(bits & (1U << (RELINFOBITS-1))) {
  343: 	assert(i*sizeof(Cell) < size);
  344: 	/* fprintf(stderr,"relocate: image[%d]=%d of %d\n", i, image[i], size/sizeof(Cell)); */
  345:         token=image[i];
  346: 	if(token<0) {
  347: 	  int group = (-token & 0x3E00) >> 9;
  348: 	  if(group == 0) {
  349: 	    switch(token|0x4000) {
  350: 	    case CF_NIL      : image[i]=0; break;
  351: #if !defined(DOUBLY_INDIRECT)
  352: 	    case CF(DOCOL)   :
  353: 	    case CF(DOVAR)   :
  354: 	    case CF(DOCON)   :
  355: 	    case CF(DOUSER)  : 
  356: 	    case CF(DODEFER) : 
  357: 	    case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(token)]); break;
  358: 	    case CF(DOESJUMP): image[i]=0; break;
  359: #endif /* !defined(DOUBLY_INDIRECT) */
  360: 	    case CF(DODOES)  :
  361: 	      MAKE_DOES_CF(image+i,(Xt *)(image[i+1]+((Cell)start)));
  362: 	      break;
  363: 	    default          : /* backward compatibility */
  364: /*	      printf("Code field generation image[%x]:=CFA(%x)\n",
  365: 		     i, CF(image[i])); */
  366: 	      if (CF((token | 0x4000))<max_symbols) {
  367: 		image[i]=(Cell)CFA(CF(token));
  368: #ifdef DIRECT_THREADED
  369: 		if ((token & 0x4000) == 0) { /* threade code, no CFA */
  370: 		  if (targets[k] & (1U<<(RELINFOBITS-1-j)))
  371: 		    compile_prim1(0);
  372: 		  compile_prim1(&image[i]);
  373: 		}
  374: #endif
  375: 	      } else
  376: 		fprintf(stderr,"Primitive %ld used in this image at $%lx (offset $%x) is not implemented by this\n engine (%s); executing this code will crash.\n",(long)CF(token),(long)&image[i], i, PACKAGE_VERSION);
  377: 	    }
  378: 	  } else {
  379: 	    int tok = -token & 0x1FF;
  380: 	    if (tok < (groups[group+1]-groups[group])) {
  381: #if defined(DOUBLY_INDIRECT)
  382: 	      image[i]=(Cell)CFA(((groups[group]+tok) | (CF(token) & 0x4000)));
  383: #else
  384: 	      image[i]=(Cell)CFA((groups[group]+tok));
  385: #endif
  386: #ifdef DIRECT_THREADED
  387: 	      if ((token & 0x4000) == 0) { /* threade code, no CFA */
  388: 		if (targets[k] & (1U<<(RELINFOBITS-1-j)))
  389: 		  compile_prim1(0);
  390: 		compile_prim1(&image[i]);
  391: 	      }
  392: #endif
  393: 	    } else
  394: 	      fprintf(stderr,"Primitive %lx, %d of group %d used in this image at $%lx (offset $%x) is not implemented by this\n engine (%s); executing this code will crash.\n", (long)-token, tok, group, (long)&image[i],i,PACKAGE_VERSION);
  395: 	  }
  396: 	} else {
  397:           /* if base is > 0: 0 is a null reference so don't adjust*/
  398:           if (token>=base) {
  399:             image[i]+=(Cell)start;
  400:           }
  401:         }
  402:       }
  403:     }
  404:   }
  405:   free(targets);
  406:   finish_code();
  407:   ((ImageHeader*)(image))->base = (Address) image;
  408: }
  409: 
  410: UCell checksum(Label symbols[])
  411: {
  412:   UCell r=PRIM_VERSION;
  413:   Cell i;
  414: 
  415:   for (i=DOCOL; i<=DOESJUMP; i++) {
  416:     r ^= (UCell)(symbols[i]);
  417:     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
  418:   }
  419: #ifdef DIRECT_THREADED
  420:   /* we have to consider all the primitives */
  421:   for (; symbols[i]!=(Label)0; i++) {
  422:     r ^= (UCell)(symbols[i]);
  423:     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
  424:   }
  425: #else
  426:   /* in indirect threaded code all primitives are accessed through the
  427:      symbols table, so we just have to put the base address of symbols
  428:      in the checksum */
  429:   r ^= (UCell)symbols;
  430: #endif
  431:   return r;
  432: }
  433: 
  434: Address verbose_malloc(Cell size)
  435: {
  436:   Address r;
  437:   /* leave a little room (64B) for stack underflows */
  438:   if ((r = malloc(size+64))==NULL) {
  439:     perror(progname);
  440:     exit(1);
  441:   }
  442:   r = (Address)((((Cell)r)+(sizeof(Float)-1))&(-sizeof(Float)));
  443:   if (debug)
  444:     fprintf(stderr, "malloc succeeds, address=$%lx\n", (long)r);
  445:   return r;
  446: }
  447: 
  448: static Address next_address=0;
  449: void after_alloc(Address r, Cell size)
  450: {
  451:   if (r != (Address)-1) {
  452:     if (debug)
  453:       fprintf(stderr, "success, address=$%lx\n", (long) r);
  454:     if (pagesize != 1)
  455:       next_address = (Address)(((((Cell)r)+size-1)&-pagesize)+2*pagesize); /* leave one page unmapped */
  456:   } else {
  457:     if (debug)
  458:       fprintf(stderr, "failed: %s\n", strerror(errno));
  459:   }
  460: }
  461: 
  462: #ifndef MAP_FAILED
  463: #define MAP_FAILED ((Address) -1)
  464: #endif
  465: #ifndef MAP_FILE
  466: # define MAP_FILE 0
  467: #endif
  468: #ifndef MAP_PRIVATE
  469: # define MAP_PRIVATE 0
  470: #endif
  471: #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
  472: # define MAP_ANON MAP_ANONYMOUS
  473: #endif
  474: 
  475: #if defined(HAVE_MMAP)
  476: static Address alloc_mmap(Cell size)
  477: {
  478:   Address r;
  479: 
  480: #if defined(MAP_ANON)
  481:   if (debug)
  482:     fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_ANON, ...); ", (long)next_address, (long)size);
  483:   r = mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
  484: #else /* !defined(MAP_ANON) */
  485:   /* Ultrix (at least) does not define MAP_FILE and MAP_PRIVATE (both are
  486:      apparently defaults) */
  487:   static int dev_zero=-1;
  488: 
  489:   if (dev_zero == -1)
  490:     dev_zero = open("/dev/zero", O_RDONLY);
  491:   if (dev_zero == -1) {
  492:     r = MAP_FAILED;
  493:     if (debug)
  494:       fprintf(stderr, "open(\"/dev/zero\"...) failed (%s), no mmap; ", 
  495: 	      strerror(errno));
  496:   } else {
  497:     if (debug)
  498:       fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FILE, dev_zero, ...); ", (long)next_address, (long)size);
  499:     r=mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FILE|MAP_PRIVATE, dev_zero, 0);
  500:   }
  501: #endif /* !defined(MAP_ANON) */
  502:   after_alloc(r, size);
  503:   return r;  
  504: }
  505: #endif
  506: 
  507: Address my_alloc(Cell size)
  508: {
  509: #if HAVE_MMAP
  510:   Address r;
  511: 
  512:   r=alloc_mmap(size);
  513:   if (r!=(Address)MAP_FAILED)
  514:     return r;
  515: #endif /* HAVE_MMAP */
  516:   /* use malloc as fallback */
  517:   return verbose_malloc(size);
  518: }
  519: 
  520: Address dict_alloc_read(FILE *file, Cell imagesize, Cell dictsize, Cell offset)
  521: {
  522:   Address image = MAP_FAILED;
  523: 
  524: #if defined(HAVE_MMAP)
  525:   if (offset==0) {
  526:     image=alloc_mmap(dictsize);
  527:     if (debug)
  528:       fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FIXED|MAP_FILE, imagefile, 0); ", (long)image, (long)imagesize);
  529:     image = mmap(image, imagesize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FIXED|MAP_FILE|MAP_PRIVATE, fileno(file), 0);
  530:     after_alloc(image,dictsize);
  531:   }
  532: #endif /* defined(HAVE_MMAP) */
  533:   if (image == (Address)MAP_FAILED) {
  534:     image = my_alloc(dictsize+offset)+offset;
  535:     rewind(file);  /* fseek(imagefile,0L,SEEK_SET); */
  536:     fread(image, 1, imagesize, file);
  537:   }
  538:   return image;
  539: }
  540: 
  541: void set_stack_sizes(ImageHeader * header)
  542: {
  543:   if (dictsize==0)
  544:     dictsize = header->dict_size;
  545:   if (dsize==0)
  546:     dsize = header->data_stack_size;
  547:   if (rsize==0)
  548:     rsize = header->return_stack_size;
  549:   if (fsize==0)
  550:     fsize = header->fp_stack_size;
  551:   if (lsize==0)
  552:     lsize = header->locals_stack_size;
  553:   dictsize=maxaligned(dictsize);
  554:   dsize=maxaligned(dsize);
  555:   rsize=maxaligned(rsize);
  556:   lsize=maxaligned(lsize);
  557:   fsize=maxaligned(fsize);
  558: }
  559: 
  560: void alloc_stacks(ImageHeader * header)
  561: {
  562:   header->dict_size=dictsize;
  563:   header->data_stack_size=dsize;
  564:   header->fp_stack_size=fsize;
  565:   header->return_stack_size=rsize;
  566:   header->locals_stack_size=lsize;
  567: 
  568:   header->data_stack_base=my_alloc(dsize);
  569:   header->fp_stack_base=my_alloc(fsize);
  570:   header->return_stack_base=my_alloc(rsize);
  571:   header->locals_stack_base=my_alloc(lsize);
  572: }
  573: 
  574: #warning You can ignore the warnings about clobbered variables in go_forth
  575: int go_forth(Address image, int stack, Cell *entries)
  576: {
  577:   volatile ImageHeader *image_header = (ImageHeader *)image;
  578:   Cell *sp0=(Cell*)(image_header->data_stack_base + dsize);
  579:   Cell *rp0=(Cell *)(image_header->return_stack_base + rsize);
  580:   Float *fp0=(Float *)(image_header->fp_stack_base + fsize);
  581: #ifdef GFORTH_DEBUGGING
  582:   volatile Cell *orig_rp0=rp0;
  583: #endif
  584:   Address lp0=image_header->locals_stack_base + lsize;
  585:   Xt *ip0=(Xt *)(image_header->boot_entry);
  586: #ifdef SYSSIGNALS
  587:   int throw_code;
  588: #endif
  589: 
  590:   /* ensure that the cached elements (if any) are accessible */
  591:   IF_spTOS(sp0--);
  592:   IF_fpTOS(fp0--);
  593:   
  594:   for(;stack>0;stack--)
  595:     *--sp0=entries[stack-1];
  596: 
  597: #ifdef SYSSIGNALS
  598:   get_winsize();
  599:    
  600:   install_signal_handlers(); /* right place? */
  601:   
  602:   if ((throw_code=setjmp(throw_jmp_buf))) {
  603:     static Cell signal_data_stack[8];
  604:     static Cell signal_return_stack[8];
  605:     static Float signal_fp_stack[1];
  606: 
  607:     signal_data_stack[7]=throw_code;
  608: 
  609: #ifdef GFORTH_DEBUGGING
  610:     if (debug)
  611:       fprintf(stderr,"\ncaught signal, throwing exception %d, ip=%p rp=%p\n",
  612: 	      throw_code, saved_ip, rp);
  613:     if (rp <= orig_rp0 && rp > (Cell *)(image_header->return_stack_base+5)) {
  614:       /* no rstack overflow or underflow */
  615:       rp0 = rp;
  616:       *--rp0 = (Cell)saved_ip;
  617:     }
  618:     else /* I love non-syntactic ifdefs :-) */
  619:       rp0 = signal_return_stack+8;
  620: #else  /* !defined(GFORTH_DEBUGGING) */
  621:     if (debug)
  622:       fprintf(stderr,"\ncaught signal, throwing exception %d\n", throw_code);
  623:       rp0 = signal_return_stack+8;
  624: #endif /* !defined(GFORTH_DEBUGGING) */
  625:     /* fprintf(stderr, "rp=$%x\n",rp0);*/
  626:     
  627:     return((int)(Cell)engine(image_header->throw_entry, signal_data_stack+7,
  628: 		       rp0, signal_fp_stack, 0));
  629:   }
  630: #endif
  631: 
  632:   return((int)(Cell)engine(ip0,sp0,rp0,fp0,lp0));
  633: }
  634: 
  635: #ifndef INCLUDE_IMAGE
  636: void print_sizes(Cell sizebyte)
  637:      /* print size information */
  638: {
  639:   static char* endianstring[]= { "   big","little" };
  640:   
  641:   fprintf(stderr,"%s endian, cell=%d bytes, char=%d bytes, au=%d bytes\n",
  642: 	  endianstring[sizebyte & 1],
  643: 	  1 << ((sizebyte >> 1) & 3),
  644: 	  1 << ((sizebyte >> 3) & 3),
  645: 	  1 << ((sizebyte >> 5) & 3));
  646: }
  647: 
  648: /* static superinstruction stuff */
  649: 
  650: struct cost {
  651:   char loads;       /* number of stack loads */
  652:   char stores;      /* number of stack stores */
  653:   char updates;     /* number of stack pointer updates */
  654:   char branch;	    /* is it a branch (SET_IP) */
  655:   unsigned char state_in;    /* state on entry */
  656:   unsigned char state_out;   /* state on exit */
  657:   short offset;     /* offset into super2 table */
  658:   unsigned char length;      /* number of components */
  659: };
  660: 
  661: PrimNum super2[] = {
  662: #include SUPER2_I
  663: };
  664: 
  665: struct cost super_costs[] = {
  666: #include COSTS_I
  667: };
  668: 
  669: struct super_state {
  670:   struct super_state *next;
  671:   PrimNum super;
  672: };
  673: 
  674: #define HASH_SIZE 256
  675: 
  676: struct super_table_entry {
  677:   struct super_table_entry *next;
  678:   PrimNum *start;
  679:   short length;
  680:   struct super_state *ss_list; /* list of supers */
  681: } *super_table[HASH_SIZE];
  682: int max_super=2;
  683: 
  684: struct super_state *state_transitions=NULL;
  685: 
  686: int hash_super(PrimNum *start, int length)
  687: {
  688:   int i, r;
  689:   
  690:   for (i=0, r=0; i<length; i++) {
  691:     r <<= 1;
  692:     r += start[i];
  693:   }
  694:   return r & (HASH_SIZE-1);
  695: }
  696: 
  697: struct super_state **lookup_super(PrimNum *start, int length)
  698: {
  699:   int hash=hash_super(start,length);
  700:   struct super_table_entry *p = super_table[hash];
  701: 
  702:   /* assert(length >= 2); */
  703:   for (; p!=NULL; p = p->next) {
  704:     if (length == p->length &&
  705: 	memcmp((char *)p->start, (char *)start, length*sizeof(PrimNum))==0)
  706:       return &(p->ss_list);
  707:   }
  708:   return NULL;
  709: }
  710: 
  711: void prepare_super_table()
  712: {
  713:   int i;
  714:   int nsupers = 0;
  715: 
  716:   for (i=0; i<sizeof(super_costs)/sizeof(super_costs[0]); i++) {
  717:     struct cost *c = &super_costs[i];
  718:     if ((c->length < 2 || nsupers < static_super_number) &&
  719: 	c->state_in < maxstates && c->state_out < maxstates) {
  720:       struct super_state **ss_listp= lookup_super(super2+c->offset, c->length);
  721:       struct super_state *ss = malloc(sizeof(struct super_state));
  722:       ss->super= i;
  723:       if (c->offset==N_noop && i != N_noop) {
  724: 	if (is_relocatable(i)) {
  725: 	  ss->next = state_transitions;
  726: 	  state_transitions = ss;
  727: 	}
  728:       } else if (ss_listp != NULL) {
  729: 	ss->next = *ss_listp;
  730: 	*ss_listp = ss;
  731:       } else {
  732: 	int hash = hash_super(super2+c->offset, c->length);
  733: 	struct super_table_entry **p = &super_table[hash];
  734: 	struct super_table_entry *e = malloc(sizeof(struct super_table_entry));
  735: 	ss->next = NULL;
  736: 	e->next = *p;
  737: 	e->start = super2 + c->offset;
  738: 	e->length = c->length;
  739: 	e->ss_list = ss;
  740: 	*p = e;
  741:       }
  742:       if (c->length > max_super)
  743: 	max_super = c->length;
  744:       if (c->length >= 2)
  745: 	nsupers++;
  746:     }
  747:   }
  748:   if (debug)
  749:     fprintf(stderr, "Using %d static superinsts\n", nsupers);
  750: }
  751: 
  752: /* dynamic replication/superinstruction stuff */
  753: 
  754: #ifndef NO_DYNAMIC
  755: int compare_priminfo_length(const void *_a, const void *_b)
  756: {
  757:   PrimInfo **a = (PrimInfo **)_a;
  758:   PrimInfo **b = (PrimInfo **)_b;
  759:   Cell diff = (*a)->length - (*b)->length;
  760:   if (diff)
  761:     return diff;
  762:   else /* break ties by start address; thus the decompiler produces
  763:           the earliest primitive with the same code (e.g. noop instead
  764:           of (char) and @ instead of >code-address */
  765:     return (*b)->start - (*a)->start;
  766: }
  767: #endif /* !defined(NO_DYNAMIC) */
  768: 
  769: static char MAYBE_UNUSED superend[]={
  770: #include PRIM_SUPEREND_I
  771: };
  772: 
  773: Cell npriminfos=0;
  774: 
  775: int compare_labels(const void *pa, const void *pb)
  776: {
  777:   Label a = *(Label *)pa;
  778:   Label b = *(Label *)pb;
  779:   return a-b;
  780: }
  781: 
  782: Label bsearch_next(Label key, Label *a, UCell n)
  783:      /* a is sorted; return the label >=key that is the closest in a;
  784:         return NULL if there is no label in a >=key */
  785: {
  786:   int mid = (n-1)/2;
  787:   if (n<1)
  788:     return NULL;
  789:   if (n == 1) {
  790:     if (a[0] < key)
  791:       return NULL;
  792:     else
  793:       return a[0];
  794:   }
  795:   if (a[mid] < key)
  796:     return bsearch_next(key, a+mid+1, n-mid-1);
  797:   else
  798:     return bsearch_next(key, a, mid+1);
  799: }
  800: 
  801: void check_prims(Label symbols1[])
  802: {
  803:   int i;
  804: #ifndef NO_DYNAMIC
  805:   Label *symbols2, *symbols3, *ends1, *ends1j, *ends1jsorted;
  806:   int nends1j;
  807: #endif
  808: 
  809:   if (debug)
  810: #ifdef __VERSION__
  811:     fprintf(stderr, "Compiled with gcc-" __VERSION__ "\n");
  812: #else
  813: #define xstr(s) str(s)
  814: #define str(s) #s
  815:   fprintf(stderr, "Compiled with gcc-" xstr(__GNUC__) "." xstr(__GNUC_MINOR__) "\n"); 
  816: #endif
  817:   for (i=0; symbols1[i]!=0; i++)
  818:     ;
  819:   npriminfos = i;
  820:   
  821: #ifndef NO_DYNAMIC
  822:   if (no_dynamic)
  823:     return;
  824:   symbols2=engine2(0,0,0,0,0);
  825: #if NO_IP
  826:   symbols3=engine3(0,0,0,0,0);
  827: #else
  828:   symbols3=symbols1;
  829: #endif
  830:   ends1 = symbols1+i+1;
  831:   ends1j =   ends1+i;
  832:   nends1j = i+1;
  833:   ends1jsorted = (Label *)alloca(nends1j*sizeof(Label));
  834:   memcpy(ends1jsorted,ends1j,nends1j*sizeof(Label));
  835:   qsort(ends1jsorted, nends1j, sizeof(Label), compare_labels);
  836:   
  837:   priminfos = calloc(i,sizeof(PrimInfo));
  838:   for (i=0; symbols1[i]!=0; i++) {
  839:     int prim_len = ends1[i]-symbols1[i];
  840:     PrimInfo *pi=&priminfos[i];
  841:     int j=0;
  842:     char *s1 = (char *)symbols1[i];
  843:     char *s2 = (char *)symbols2[i];
  844:     char *s3 = (char *)symbols3[i];
  845:     Label endlabel = bsearch_next(symbols1[i]+1,ends1jsorted,nends1j);
  846: 
  847:     pi->start = s1;
  848:     pi->superend = superend[i]|no_super;
  849:     if (pi->superend)
  850:       pi->length = endlabel-symbols1[i];
  851:     else
  852:       pi->length = prim_len;
  853:     pi->restlength = endlabel - symbols1[i] - pi->length;
  854:     pi->nimmargs = 0;
  855:     if (debug)
  856:       fprintf(stderr, "%-15s %3d %p %p %p len=%3ld restlen=%2ld s-end=%1d",
  857: 	      prim_names[i], i, s1, s2, s3, (long)(pi->length), (long)(pi->restlength), pi->superend);
  858:     if (endlabel == NULL) {
  859:       pi->start = NULL; /* not relocatable */
  860:       if (pi->length<0) pi->length=100;
  861:       if (debug)
  862: 	fprintf(stderr,"\n   non_reloc: no J label > start found\n");
  863:       continue;
  864:     }
  865:     if (ends1[i] > endlabel && !pi->superend) {
  866:       pi->start = NULL; /* not relocatable */
  867:       pi->length = endlabel-symbols1[i];
  868:       if (debug)
  869: 	fprintf(stderr,"\n   non_reloc: there is a J label before the K label (restlength<0)\n");
  870:       continue;
  871:     }
  872:     if (ends1[i] < pi->start && !pi->superend) {
  873:       pi->start = NULL; /* not relocatable */
  874:       pi->length = endlabel-symbols1[i];
  875:       if (debug)
  876: 	fprintf(stderr,"\n   non_reloc: K label before I label (length<0)\n");
  877:       continue;
  878:     }
  879:     assert(pi->length>=0);
  880:     assert(pi->restlength >=0);
  881:     while (j<(pi->length+pi->restlength)) {
  882:       if (s1[j]==s3[j]) {
  883: 	if (s1[j] != s2[j]) {
  884: 	  pi->start = NULL; /* not relocatable */
  885: 	  if (debug)
  886: 	    fprintf(stderr,"\n   non_reloc: engine1!=engine2 offset %3d",j);
  887: 	  /* assert(j<prim_len); */
  888: 	  break;
  889: 	}
  890: 	j++;
  891:       } else {
  892: 	struct immarg *ia=&pi->immargs[pi->nimmargs];
  893: 
  894: 	pi->nimmargs++;
  895: 	ia->offset=j;
  896: 	if ((~*(Cell *)&(s1[j]))==*(Cell *)&(s3[j])) {
  897: 	  ia->rel=0;
  898: 	  if (debug)
  899: 	    fprintf(stderr,"\n   absolute immarg: offset %3d",j);
  900: 	} else if ((&(s1[j]))+(*(Cell *)&(s1[j]))+4 ==
  901: 		   symbols1[DOESJUMP+1]) {
  902: 	  ia->rel=1;
  903: 	  if (debug)
  904: 	    fprintf(stderr,"\n   relative immarg: offset %3d",j);
  905: 	} else {
  906: 	  pi->start = NULL; /* not relocatable */
  907: 	  if (debug)
  908: 	    fprintf(stderr,"\n   non_reloc: engine1!=engine3 offset %3d",j);
  909: 	  /* assert(j<prim_len);*/
  910: 	  break;
  911: 	}
  912: 	j+=4;
  913:       }
  914:     }
  915:     if (debug)
  916:       fprintf(stderr,"\n");
  917:   }
  918:   decomp_prims = calloc(i,sizeof(PrimInfo *));
  919:   for (i=DOESJUMP+1; i<npriminfos; i++)
  920:     decomp_prims[i] = &(priminfos[i]);
  921:   qsort(decomp_prims+DOESJUMP+1, npriminfos-DOESJUMP-1, sizeof(PrimInfo *),
  922: 	compare_priminfo_length);
  923: #endif
  924: }
  925: 
  926: void flush_to_here(void)
  927: {
  928: #ifndef NO_DYNAMIC
  929:   if (start_flush)
  930:     FLUSH_ICACHE(start_flush, code_here-start_flush);
  931:   start_flush=code_here;
  932: #endif
  933: }
  934: 
  935: #ifndef NO_DYNAMIC
  936: void append_jump(void)
  937: {
  938:   if (last_jump) {
  939:     PrimInfo *pi = &priminfos[last_jump];
  940:     
  941:     memcpy(code_here, pi->start+pi->length, pi->restlength);
  942:     code_here += pi->restlength;
  943:     last_jump=0;
  944:   }
  945: }
  946: 
  947: /* Gforth remembers all code blocks in this list.  On forgetting (by
  948: executing a marker) the code blocks are not freed (because Gforth does
  949: not remember how they were allocated; hmm, remembering that might be
  950: easier and cleaner).  Instead, code_here etc. are reset to the old
  951: value, and the "forgotten" code blocks are reused when they are
  952: needed. */
  953: 
  954: struct code_block_list {
  955:   struct code_block_list *next;
  956:   Address block;
  957:   Cell size;
  958: } *code_block_list=NULL, **next_code_blockp=&code_block_list;
  959: 
  960: Address append_prim(Cell p)
  961: {
  962:   PrimInfo *pi = &priminfos[p];
  963:   Address old_code_here = code_here;
  964: 
  965:   if (code_area+code_area_size < code_here+pi->length+pi->restlength) {
  966:     struct code_block_list *p;
  967:     append_jump();
  968:     flush_to_here();
  969:     if (*next_code_blockp == NULL) {
  970:       code_here = start_flush = code_area = my_alloc(code_area_size);
  971:       p = (struct code_block_list *)malloc(sizeof(struct code_block_list));
  972:       *next_code_blockp = p;
  973:       p->next = NULL;
  974:       p->block = code_here;
  975:       p->size = code_area_size;
  976:     } else {
  977:       p = *next_code_blockp;
  978:       code_here = start_flush = code_area = p->block;
  979:     }
  980:     old_code_here = code_here;
  981:     next_code_blockp = &(p->next);
  982:   }
  983:   memcpy(code_here, pi->start, pi->length);
  984:   code_here += pi->length;
  985:   return old_code_here;
  986: }
  987: #endif
  988: 
  989: int forget_dyncode(Address code)
  990: {
  991: #ifdef NO_DYNAMIC
  992:   return -1;
  993: #else
  994:   struct code_block_list *p, **pp;
  995: 
  996:   for (pp=&code_block_list, p=*pp; p!=NULL; pp=&(p->next), p=*pp) {
  997:     if (code >= p->block && code < p->block+p->size) {
  998:       next_code_blockp = &(p->next);
  999:       code_here = start_flush = code;
 1000:       code_area = p->block;
 1001:       last_jump = 0;
 1002:       return -1;
 1003:     }
 1004:   }
 1005:   return -no_dynamic;
 1006: #endif /* !defined(NO_DYNAMIC) */
 1007: }
 1008: 
 1009: long dyncodesize(void)
 1010: {
 1011: #ifndef NO_DYNAMIC
 1012:   struct code_block_list *p;
 1013:   long size=0;
 1014:   for (p=code_block_list; p!=NULL; p=p->next) {
 1015:     if (code_here >= p->block && code_here < p->block+p->size)
 1016:       return size + (code_here - p->block);
 1017:     else
 1018:       size += p->size;
 1019:   }
 1020: #endif /* !defined(NO_DYNAMIC) */
 1021:   return 0;
 1022: }
 1023: 
 1024: Label decompile_code(Label _code)
 1025: {
 1026: #ifdef NO_DYNAMIC
 1027:   return _code;
 1028: #else /* !defined(NO_DYNAMIC) */
 1029:   Cell i;
 1030:   struct code_block_list *p;
 1031:   Address code=_code;
 1032: 
 1033:   /* first, check if we are in code at all */
 1034:   for (p = code_block_list;; p = p->next) {
 1035:     if (p == NULL)
 1036:       return code;
 1037:     if (code >= p->block && code < p->block+p->size)
 1038:       break;
 1039:   }
 1040:   /* reverse order because NOOP might match other prims */
 1041:   for (i=npriminfos-1; i>DOESJUMP; i--) {
 1042:     PrimInfo *pi=decomp_prims[i];
 1043:     if (pi->start==code || (pi->start && memcmp(code,pi->start,pi->length)==0))
 1044:       return vm_prims[super2[super_costs[pi-priminfos].offset]];
 1045:     /* return pi->start;*/
 1046:   }
 1047:   return code;
 1048: #endif /* !defined(NO_DYNAMIC) */
 1049: }
 1050: 
 1051: #ifdef NO_IP
 1052: int nbranchinfos=0;
 1053: 
 1054: struct branchinfo {
 1055:   Label **targetpp; /* **(bi->targetpp) is the target */
 1056:   Cell *addressptr; /* store the target here */
 1057: } branchinfos[100000];
 1058: 
 1059: int ndoesexecinfos=0;
 1060: struct doesexecinfo {
 1061:   int branchinfo; /* fix the targetptr of branchinfos[...->branchinfo] */
 1062:   Label *targetp; /*target for branch (because this is not in threaded code)*/
 1063:   Cell *xt; /* cfa of word whose does-code needs calling */
 1064: } doesexecinfos[10000];
 1065: 
 1066: void set_rel_target(Cell *source, Label target)
 1067: {
 1068:   *source = ((Cell)target)-(((Cell)source)+4);
 1069: }
 1070: 
 1071: void register_branchinfo(Label source, Cell *targetpp)
 1072: {
 1073:   struct branchinfo *bi = &(branchinfos[nbranchinfos]);
 1074:   bi->targetpp = (Label **)targetpp;
 1075:   bi->addressptr = (Cell *)source;
 1076:   nbranchinfos++;
 1077: }
 1078: 
 1079: Address compile_prim1arg(PrimNum p, Cell **argp)
 1080: {
 1081:   Address old_code_here=append_prim(p);
 1082: 
 1083:   assert(vm_prims[p]==priminfos[p].start);
 1084:   *argp = (Cell*)(old_code_here+priminfos[p].immargs[0].offset);
 1085:   return old_code_here;
 1086: }
 1087: 
 1088: Address compile_call2(Cell *targetpp, Cell **next_code_targetp)
 1089: {
 1090:   PrimInfo *pi = &priminfos[N_call2];
 1091:   Address old_code_here = append_prim(N_call2);
 1092: 
 1093:   *next_code_targetp = (Cell *)(old_code_here + pi->immargs[0].offset);
 1094:   register_branchinfo(old_code_here + pi->immargs[1].offset, targetpp);
 1095:   return old_code_here;
 1096: }
 1097: #endif
 1098: 
 1099: void finish_code(void)
 1100: {
 1101: #ifdef NO_IP
 1102:   Cell i;
 1103: 
 1104:   compile_prim1(NULL);
 1105:   for (i=0; i<ndoesexecinfos; i++) {
 1106:     struct doesexecinfo *dei = &doesexecinfos[i];
 1107:     dei->targetp = (Label *)DOES_CODE1((dei->xt));
 1108:     branchinfos[dei->branchinfo].targetpp = &(dei->targetp);
 1109:   }
 1110:   ndoesexecinfos = 0;
 1111:   for (i=0; i<nbranchinfos; i++) {
 1112:     struct branchinfo *bi=&branchinfos[i];
 1113:     set_rel_target(bi->addressptr, **(bi->targetpp));
 1114:   }
 1115:   nbranchinfos = 0;
 1116: #else
 1117:   compile_prim1(NULL);
 1118: #endif
 1119:   flush_to_here();
 1120: }
 1121: 
 1122: #ifdef NO_IP
 1123: Cell compile_prim_dyn(PrimNum p, Cell *tcp)
 1124:      /* compile prim #p dynamically (mod flags etc.) and return start
 1125: 	address of generated code for putting it into the threaded
 1126: 	code. This function is only called if all the associated
 1127: 	inline arguments of p are already in place (at tcp[1] etc.) */
 1128: {
 1129:   PrimInfo *pi=&priminfos[p];
 1130:   Cell *next_code_target=NULL;
 1131:   Address codeaddr;
 1132:   Address primstart;
 1133:   
 1134:   assert(p<npriminfos);
 1135:   if (p==N_execute || p==N_perform || p==N_lit_perform) {
 1136:     codeaddr = compile_prim1arg(N_set_next_code, &next_code_target);
 1137:     primstart = append_prim(p);
 1138:     goto other_prim;
 1139:   } else if (p==N_call) {
 1140:     codeaddr = compile_call2(tcp+1, &next_code_target);
 1141:   } else if (p==N_does_exec) {
 1142:     struct doesexecinfo *dei = &doesexecinfos[ndoesexecinfos++];
 1143:     Cell *arg;
 1144:     codeaddr = compile_prim1arg(N_lit,&arg);
 1145:     *arg = (Cell)PFA(tcp[1]);
 1146:     /* we cannot determine the callee now (last_start[1] may be a
 1147:        forward reference), so just register an arbitrary target, and
 1148:        register in dei that we need to fix this before resolving
 1149:        branches */
 1150:     dei->branchinfo = nbranchinfos;
 1151:     dei->xt = (Cell *)(tcp[1]);
 1152:     compile_call2(0, &next_code_target);
 1153:   } else if (!is_relocatable(p)) {
 1154:     Cell *branch_target;
 1155:     codeaddr = compile_prim1arg(N_set_next_code, &next_code_target);
 1156:     compile_prim1arg(N_branch,&branch_target);
 1157:     set_rel_target(branch_target,vm_prims[p]);
 1158:   } else {
 1159:     unsigned j;
 1160: 
 1161:     codeaddr = primstart = append_prim(p);
 1162:   other_prim:
 1163:     for (j=0; j<pi->nimmargs; j++) {
 1164:       struct immarg *ia = &(pi->immargs[j]);
 1165:       Cell *argp = tcp + pi->nimmargs - j;
 1166:       Cell argval = *argp; /* !! specific to prims */
 1167:       if (ia->rel) { /* !! assumption: relative refs are branches */
 1168: 	register_branchinfo(primstart + ia->offset, argp);
 1169:       } else /* plain argument */
 1170: 	*(Cell *)(primstart + ia->offset) = argval;
 1171:     }
 1172:   }
 1173:   if (next_code_target!=NULL)
 1174:     *next_code_target = (Cell)code_here;
 1175:   return (Cell)codeaddr;
 1176: }
 1177: #else /* !defined(NO_IP) */
 1178: Cell compile_prim_dyn(PrimNum p, Cell *tcp)
 1179:      /* compile prim #p dynamically (mod flags etc.) and return start
 1180:         address of generated code for putting it into the threaded code */
 1181: {
 1182:   Cell static_prim = (Cell)vm_prims[p];
 1183: #if defined(NO_DYNAMIC)
 1184:   return static_prim;
 1185: #else /* !defined(NO_DYNAMIC) */
 1186:   Address old_code_here;
 1187: 
 1188:   if (no_dynamic)
 1189:     return static_prim;
 1190:   if (p>=npriminfos || !is_relocatable(p)) {
 1191:     append_jump();
 1192:     return static_prim;
 1193:   }
 1194:   old_code_here = append_prim(p);
 1195:   last_jump = (priminfos[p].superend) ? 0 : p;
 1196:   return (Cell)old_code_here;
 1197: #endif  /* !defined(NO_DYNAMIC) */
 1198: }
 1199: #endif /* !defined(NO_IP) */
 1200: 
 1201: #ifndef NO_DYNAMIC
 1202: int cost_codesize(int prim)
 1203: {
 1204:   return priminfos[prim].length;
 1205: }
 1206: #endif
 1207: 
 1208: int cost_ls(int prim)
 1209: {
 1210:   struct cost *c = super_costs+prim;
 1211: 
 1212:   return c->loads + c->stores;
 1213: }
 1214: 
 1215: int cost_lsu(int prim)
 1216: {
 1217:   struct cost *c = super_costs+prim;
 1218: 
 1219:   return c->loads + c->stores + c->updates;
 1220: }
 1221: 
 1222: int cost_nexts(int prim)
 1223: {
 1224:   return 1;
 1225: }
 1226: 
 1227: typedef int Costfunc(int);
 1228: Costfunc *ss_cost =  /* cost function for optimize_bb */
 1229: #ifdef NO_DYNAMIC
 1230: cost_lsu;
 1231: #else
 1232: cost_codesize;
 1233: #endif
 1234: 
 1235: struct {
 1236:   Costfunc *costfunc;
 1237:   char *metricname;
 1238:   long sum;
 1239: } cost_sums[] = {
 1240: #ifndef NO_DYNAMIC
 1241:   { cost_codesize, "codesize", 0 },
 1242: #endif
 1243:   { cost_ls,       "ls",       0 },
 1244:   { cost_lsu,      "lsu",      0 },
 1245:   { cost_nexts,    "nexts",    0 }
 1246: };
 1247: 
 1248: #define MAX_BB 128 /* maximum number of instructions in BB */
 1249: #define INF_COST 1000000 /* infinite cost */
 1250: #define CANONICAL_STATE 0
 1251: 
 1252: struct waypoint {
 1253:   int cost;     /* the cost from here to the end */
 1254:   PrimNum inst; /* the inst used from here to the next waypoint */
 1255:   char relocatable; /* the last non-transition was relocatable */
 1256:   char no_transition; /* don't use the next transition (relocatability)
 1257: 		       * or this transition (does not change state) */
 1258: };
 1259: 
 1260: void init_waypoints(struct waypoint ws[])
 1261: {
 1262:   int k;
 1263: 
 1264:   for (k=0; k<maxstates; k++)
 1265:     ws[k].cost=INF_COST;
 1266: }
 1267: 
 1268: void transitions(struct waypoint inst[], struct waypoint trans[])
 1269: {
 1270:   int k;
 1271:   struct super_state *l;
 1272:   
 1273:   for (k=0; k<maxstates; k++) {
 1274:     trans[k] = inst[k];
 1275:     trans[k].no_transition = 1;
 1276:   }
 1277:   for (l = state_transitions; l != NULL; l = l->next) {
 1278:     PrimNum s = l->super;
 1279:     int jcost;
 1280:     struct cost *c=super_costs+s;
 1281:     struct waypoint *wi=&(trans[c->state_in]);
 1282:     struct waypoint *wo=&(inst[c->state_out]);
 1283:     if (wo->cost == INF_COST)
 1284:       continue;
 1285:     jcost = wo->cost + ss_cost(s);
 1286:     if (jcost <= wi->cost) {
 1287:       wi->cost = jcost;
 1288:       wi->inst = s;
 1289:       wi->relocatable = wo->relocatable;
 1290:       wi->no_transition = 0;
 1291:       /* if (ss_greedy) wi->cost = wo->cost ? */
 1292:     }
 1293:   }
 1294: }
 1295: 
 1296: /* use dynamic programming to find the shortest paths within the basic
 1297:    block origs[0..ninsts-1] and rewrite the instructions pointed to by
 1298:    instps to use it */
 1299: void optimize_rewrite(Cell *instps[], PrimNum origs[], int ninsts)
 1300: {
 1301:   int i,j;
 1302:   static struct waypoint inst[MAX_BB+1][MAX_STATE];  /* before instruction*/
 1303:   static struct waypoint trans[MAX_BB+1][MAX_STATE]; /* before transition */
 1304:   int nextdyn, nextstate, no_transition;
 1305:   
 1306:   init_waypoints(inst[ninsts]);
 1307:   inst[ninsts][CANONICAL_STATE].cost=0;
 1308:   transitions(inst[ninsts],trans[ninsts]);
 1309:   for (i=ninsts-1; i>=0; i--) {
 1310:     init_waypoints(inst[i]);
 1311:     for (j=1; j<=max_super && i+j<=ninsts; j++) {
 1312:       struct super_state **superp = lookup_super(origs+i, j);
 1313:       if (superp!=NULL) {
 1314: 	struct super_state *supers = *superp;
 1315: 	for (; supers!=NULL; supers = supers->next) {
 1316: 	  PrimNum s = supers->super;
 1317: 	  int jcost;
 1318: 	  struct cost *c=super_costs+s;
 1319: 	  struct waypoint *wi=&(inst[i][c->state_in]);
 1320: 	  struct waypoint *wo=&(trans[i+j][c->state_out]);
 1321: 	  int no_transition = wo->no_transition;
 1322: 	  if (!(is_relocatable(s)) && !wo->relocatable) {
 1323: 	    wo=&(inst[i+j][c->state_out]);
 1324: 	    no_transition=1;
 1325: 	  }
 1326: 	  if (wo->cost == INF_COST) 
 1327: 	    continue;
 1328: 	  jcost = wo->cost + ss_cost(s);
 1329: 	  if (jcost <= wi->cost) {
 1330: 	    wi->cost = jcost;
 1331: 	    wi->inst = s;
 1332: 	    wi->relocatable = is_relocatable(s);
 1333: 	    wi->no_transition = no_transition;
 1334: 	    /* if (ss_greedy) wi->cost = wo->cost ? */
 1335: 	  }
 1336: 	}
 1337:       }
 1338:     }
 1339:     transitions(inst[i],trans[i]);
 1340:   }
 1341:   /* now rewrite the instructions */
 1342:   nextdyn=0;
 1343:   nextstate=CANONICAL_STATE;
 1344:   no_transition = ((!trans[0][nextstate].relocatable) 
 1345: 		   ||trans[0][nextstate].no_transition);
 1346:   for (i=0; i<ninsts; i++) {
 1347:     Cell tc=0, tc2;
 1348:     if (i==nextdyn) {
 1349:       if (!no_transition) {
 1350: 	/* process trans */
 1351: 	PrimNum p = trans[i][nextstate].inst;
 1352: 	struct cost *c = super_costs+p;
 1353: 	assert(trans[i][nextstate].cost != INF_COST);
 1354: 	assert(c->state_in==nextstate);
 1355: 	tc = compile_prim_dyn(p,NULL);
 1356: 	nextstate = c->state_out;
 1357:       }
 1358:       {
 1359: 	/* process inst */
 1360: 	PrimNum p = inst[i][nextstate].inst;
 1361: 	struct cost *c=super_costs+p;
 1362: 	assert(c->state_in==nextstate);
 1363: 	assert(inst[i][nextstate].cost != INF_COST);
 1364: #if defined(GFORTH_DEBUGGING)
 1365: 	assert(p == origs[i]);
 1366: #endif
 1367: 	tc2 = compile_prim_dyn(p,instps[i]);
 1368: 	if (no_transition || !is_relocatable(p))
 1369: 	  /* !! actually what we care about is if and where
 1370: 	   * compile_prim_dyn() puts NEXTs */
 1371: 	  tc=tc2;
 1372: 	no_transition = inst[i][nextstate].no_transition;
 1373: 	nextstate = c->state_out;
 1374: 	nextdyn += c->length;
 1375:       }
 1376:     } else {
 1377: #if defined(GFORTH_DEBUGGING)
 1378:       assert(0);
 1379: #endif
 1380:       tc=0;
 1381:       /* tc= (Cell)vm_prims[inst[i][CANONICAL_STATE].inst]; */
 1382:     }
 1383:     *(instps[i]) = tc;
 1384:   }      
 1385:   if (!no_transition) {
 1386:     PrimNum p = trans[i][nextstate].inst;
 1387:     struct cost *c = super_costs+p;
 1388:     assert(c->state_in==nextstate);
 1389:     assert(trans[i][nextstate].cost != INF_COST);
 1390:     assert(i==nextdyn);
 1391:     (void)compile_prim_dyn(p,NULL);
 1392:     nextstate = c->state_out;
 1393:   }
 1394:   assert(nextstate==CANONICAL_STATE);
 1395: }
 1396: 
 1397: /* compile *start, possibly rewriting it into a static and/or dynamic
 1398:    superinstruction */
 1399: void compile_prim1(Cell *start)
 1400: {
 1401: #if defined(DOUBLY_INDIRECT)
 1402:   Label prim;
 1403: 
 1404:   if (start==NULL)
 1405:     return;
 1406:   prim = (Label)*start;
 1407:   if (prim<((Label)(xts+DOESJUMP)) || prim>((Label)(xts+npriminfos))) {
 1408:     fprintf(stderr,"compile_prim encountered xt %p\n", prim);
 1409:     *start=(Cell)prim;
 1410:     return;
 1411:   } else {
 1412:     *start = (Cell)(prim-((Label)xts)+((Label)vm_prims));
 1413:     return;
 1414:   }
 1415: #elif defined(INDIRECT_THREADED)
 1416:   return;
 1417: #else /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
 1418:   /* !! does not work, for unknown reasons; but something like this is
 1419:      probably needed to ensure that we don't call compile_prim_dyn
 1420:      before the inline arguments are there */
 1421:   static Cell *instps[MAX_BB];
 1422:   static PrimNum origs[MAX_BB];
 1423:   static int ninsts=0;
 1424:   PrimNum prim_num;
 1425: 
 1426:   if (start==NULL || ninsts >= MAX_BB ||
 1427:       (ninsts>0 && superend[origs[ninsts-1]])) {
 1428:     /* after bb, or at the start of the next bb */
 1429:     optimize_rewrite(instps,origs,ninsts);
 1430:     /* fprintf(stderr,"optimize_rewrite(...,%d)\n",ninsts); */
 1431:     ninsts=0;
 1432:     if (start==NULL)
 1433:       return;
 1434:   }
 1435:   prim_num = ((Xt)*start)-vm_prims;
 1436:   if(prim_num >= npriminfos) {
 1437:     optimize_rewrite(instps,origs,ninsts);
 1438:     /* fprintf(stderr,"optimize_rewrite(...,%d)\n",ninsts);*/
 1439:     ninsts=0;
 1440:     return;
 1441:   }    
 1442:   assert(ninsts<MAX_BB);
 1443:   instps[ninsts] = start;
 1444:   origs[ninsts] = prim_num;
 1445:   ninsts++;
 1446: #endif /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
 1447: }
 1448: 
 1449: Address loader(FILE *imagefile, char* filename)
 1450: /* returns the address of the image proper (after the preamble) */
 1451: {
 1452:   ImageHeader header;
 1453:   Address image;
 1454:   Address imp; /* image+preamble */
 1455:   Char magic[8];
 1456:   char magic7; /* size byte of magic number */
 1457:   Cell preamblesize=0;
 1458:   Cell data_offset = offset_image ? 56*sizeof(Cell) : 0;
 1459:   UCell check_sum;
 1460:   Cell ausize = ((RELINFOBITS ==  8) ? 0 :
 1461: 		 (RELINFOBITS == 16) ? 1 :
 1462: 		 (RELINFOBITS == 32) ? 2 : 3);
 1463:   Cell charsize = ((sizeof(Char) == 1) ? 0 :
 1464: 		   (sizeof(Char) == 2) ? 1 :
 1465: 		   (sizeof(Char) == 4) ? 2 : 3) + ausize;
 1466:   Cell cellsize = ((sizeof(Cell) == 1) ? 0 :
 1467: 		   (sizeof(Cell) == 2) ? 1 :
 1468: 		   (sizeof(Cell) == 4) ? 2 : 3) + ausize;
 1469:   Cell sizebyte = (ausize << 5) + (charsize << 3) + (cellsize << 1) +
 1470: #ifdef WORDS_BIGENDIAN
 1471:        0
 1472: #else
 1473:        1
 1474: #endif
 1475:     ;
 1476: 
 1477:   vm_prims = engine(0,0,0,0,0);
 1478:   check_prims(vm_prims);
 1479:   prepare_super_table();
 1480: #ifndef DOUBLY_INDIRECT
 1481: #ifdef PRINT_SUPER_LENGTHS
 1482:   print_super_lengths();
 1483: #endif
 1484:   check_sum = checksum(vm_prims);
 1485: #else /* defined(DOUBLY_INDIRECT) */
 1486:   check_sum = (UCell)vm_prims;
 1487: #endif /* defined(DOUBLY_INDIRECT) */
 1488:   
 1489:   do {
 1490:     if(fread(magic,sizeof(Char),8,imagefile) < 8) {
 1491:       fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.6) image.\n",
 1492: 	      progname, filename);
 1493:       exit(1);
 1494:     }
 1495:     preamblesize+=8;
 1496:   } while(memcmp(magic,"Gforth3",7));
 1497:   magic7 = magic[7];
 1498:   if (debug) {
 1499:     magic[7]='\0';
 1500:     fprintf(stderr,"Magic found: %s ", magic);
 1501:     print_sizes(magic7);
 1502:   }
 1503: 
 1504:   if (magic7 != sizebyte)
 1505:     {
 1506:       fprintf(stderr,"This image is:         ");
 1507:       print_sizes(magic7);
 1508:       fprintf(stderr,"whereas the machine is ");
 1509:       print_sizes(sizebyte);
 1510:       exit(-2);
 1511:     };
 1512: 
 1513:   fread((void *)&header,sizeof(ImageHeader),1,imagefile);
 1514: 
 1515:   set_stack_sizes(&header);
 1516:   
 1517: #if HAVE_GETPAGESIZE
 1518:   pagesize=getpagesize(); /* Linux/GNU libc offers this */
 1519: #elif HAVE_SYSCONF && defined(_SC_PAGESIZE)
 1520:   pagesize=sysconf(_SC_PAGESIZE); /* POSIX.4 */
 1521: #elif PAGESIZE
 1522:   pagesize=PAGESIZE; /* in limits.h according to Gallmeister's POSIX.4 book */
 1523: #endif
 1524:   if (debug)
 1525:     fprintf(stderr,"pagesize=%ld\n",(unsigned long) pagesize);
 1526: 
 1527:   image = dict_alloc_read(imagefile, preamblesize+header.image_size,
 1528: 			  preamblesize+dictsize, data_offset);
 1529:   imp=image+preamblesize;
 1530:   alloc_stacks((ImageHeader *)imp);
 1531:   if (clear_dictionary)
 1532:     memset(imp+header.image_size, 0, dictsize-header.image_size);
 1533:   if(header.base==0 || header.base  == (Address)0x100) {
 1534:     Cell reloc_size=((header.image_size-1)/sizeof(Cell))/8+1;
 1535:     char reloc_bits[reloc_size];
 1536:     fseek(imagefile, preamblesize+header.image_size, SEEK_SET);
 1537:     fread(reloc_bits, 1, reloc_size, imagefile);
 1538:     relocate((Cell *)imp, reloc_bits, header.image_size, (Cell)header.base, vm_prims);
 1539: #if 0
 1540:     { /* let's see what the relocator did */
 1541:       FILE *snapshot=fopen("snapshot.fi","wb");
 1542:       fwrite(image,1,imagesize,snapshot);
 1543:       fclose(snapshot);
 1544:     }
 1545: #endif
 1546:   }
 1547:   else if(header.base!=imp) {
 1548:     fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\n",
 1549: 	    progname, (unsigned long)header.base, (unsigned long)imp);
 1550:     exit(1);
 1551:   }
 1552:   if (header.checksum==0)
 1553:     ((ImageHeader *)imp)->checksum=check_sum;
 1554:   else if (header.checksum != check_sum) {
 1555:     fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\n",
 1556: 	    progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
 1557:     exit(1);
 1558:   }
 1559: #ifdef DOUBLY_INDIRECT
 1560:   ((ImageHeader *)imp)->xt_base = xts;
 1561: #endif
 1562:   fclose(imagefile);
 1563: 
 1564:   /* unnecessary, except maybe for CODE words */
 1565:   /* FLUSH_ICACHE(imp, header.image_size);*/
 1566: 
 1567:   return imp;
 1568: }
 1569: 
 1570: /* pointer to last '/' or '\' in file, 0 if there is none. */
 1571: char *onlypath(char *filename)
 1572: {
 1573:   return strrchr(filename, DIRSEP);
 1574: }
 1575: 
 1576: FILE *openimage(char *fullfilename)
 1577: {
 1578:   FILE *image_file;
 1579:   char * expfilename = tilde_cstr(fullfilename, strlen(fullfilename), 1);
 1580: 
 1581:   image_file=fopen(expfilename,"rb");
 1582:   if (image_file!=NULL && debug)
 1583:     fprintf(stderr, "Opened image file: %s\n", expfilename);
 1584:   return image_file;
 1585: }
 1586: 
 1587: /* try to open image file concat(path[0:len],imagename) */
 1588: FILE *checkimage(char *path, int len, char *imagename)
 1589: {
 1590:   int dirlen=len;
 1591:   char fullfilename[dirlen+strlen(imagename)+2];
 1592: 
 1593:   memcpy(fullfilename, path, dirlen);
 1594:   if (fullfilename[dirlen-1]!=DIRSEP)
 1595:     fullfilename[dirlen++]=DIRSEP;
 1596:   strcpy(fullfilename+dirlen,imagename);
 1597:   return openimage(fullfilename);
 1598: }
 1599: 
 1600: FILE * open_image_file(char * imagename, char * path)
 1601: {
 1602:   FILE * image_file=NULL;
 1603:   char *origpath=path;
 1604:   
 1605:   if(strchr(imagename, DIRSEP)==NULL) {
 1606:     /* first check the directory where the exe file is in !! 01may97jaw */
 1607:     if (onlypath(progname))
 1608:       image_file=checkimage(progname, onlypath(progname)-progname, imagename);
 1609:     if (!image_file)
 1610:       do {
 1611: 	char *pend=strchr(path, PATHSEP);
 1612: 	if (pend==NULL)
 1613: 	  pend=path+strlen(path);
 1614: 	if (strlen(path)==0) break;
 1615: 	image_file=checkimage(path, pend-path, imagename);
 1616: 	path=pend+(*pend==PATHSEP);
 1617:       } while (image_file==NULL);
 1618:   } else {
 1619:     image_file=openimage(imagename);
 1620:   }
 1621: 
 1622:   if (!image_file) {
 1623:     fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
 1624: 	    progname, imagename, origpath);
 1625:     exit(1);
 1626:   }
 1627: 
 1628:   return image_file;
 1629: }
 1630: #endif
 1631: 
 1632: #ifdef HAS_OS
 1633: UCell convsize(char *s, UCell elemsize)
 1634: /* converts s of the format [0-9]+[bekMGT]? (e.g. 25k) into the number
 1635:    of bytes.  the letter at the end indicates the unit, where e stands
 1636:    for the element size. default is e */
 1637: {
 1638:   char *endp;
 1639:   UCell n,m;
 1640: 
 1641:   m = elemsize;
 1642:   n = strtoul(s,&endp,0);
 1643:   if (endp!=NULL) {
 1644:     if (strcmp(endp,"b")==0)
 1645:       m=1;
 1646:     else if (strcmp(endp,"k")==0)
 1647:       m=1024;
 1648:     else if (strcmp(endp,"M")==0)
 1649:       m=1024*1024;
 1650:     else if (strcmp(endp,"G")==0)
 1651:       m=1024*1024*1024;
 1652:     else if (strcmp(endp,"T")==0) {
 1653: #if (SIZEOF_CHAR_P > 4)
 1654:       m=1024L*1024*1024*1024;
 1655: #else
 1656:       fprintf(stderr,"%s: size specification \"%s\" too large for this machine\n", progname, endp);
 1657:       exit(1);
 1658: #endif
 1659:     } else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
 1660:       fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
 1661:       exit(1);
 1662:     }
 1663:   }
 1664:   return n*m;
 1665: }
 1666: 
 1667: enum {
 1668:   ss_number = 256,
 1669:   ss_states,
 1670:   ss_min_codesize,
 1671:   ss_min_ls,
 1672:   ss_min_lsu,
 1673:   ss_min_nexts,
 1674: };
 1675: 
 1676: void gforth_args(int argc, char ** argv, char ** path, char ** imagename)
 1677: {
 1678:   int c;
 1679: 
 1680:   opterr=0;
 1681:   while (1) {
 1682:     int option_index=0;
 1683:     static struct option opts[] = {
 1684:       {"appl-image", required_argument, NULL, 'a'},
 1685:       {"image-file", required_argument, NULL, 'i'},
 1686:       {"dictionary-size", required_argument, NULL, 'm'},
 1687:       {"data-stack-size", required_argument, NULL, 'd'},
 1688:       {"return-stack-size", required_argument, NULL, 'r'},
 1689:       {"fp-stack-size", required_argument, NULL, 'f'},
 1690:       {"locals-stack-size", required_argument, NULL, 'l'},
 1691:       {"path", required_argument, NULL, 'p'},
 1692:       {"version", no_argument, NULL, 'v'},
 1693:       {"help", no_argument, NULL, 'h'},
 1694:       /* put something != 0 into offset_image */
 1695:       {"offset-image", no_argument, &offset_image, 1},
 1696:       {"no-offset-im", no_argument, &offset_image, 0},
 1697:       {"clear-dictionary", no_argument, &clear_dictionary, 1},
 1698:       {"die-on-signal", no_argument, &die_on_signal, 1},
 1699:       {"debug", no_argument, &debug, 1},
 1700:       {"no-super", no_argument, &no_super, 1},
 1701:       {"no-dynamic", no_argument, &no_dynamic, 1},
 1702:       {"dynamic", no_argument, &no_dynamic, 0},
 1703:       {"print-metrics", no_argument, &print_metrics, 1},
 1704:       {"ss-number", required_argument, NULL, ss_number},
 1705:       {"ss-states", required_argument, NULL, ss_states},
 1706: #ifndef NO_DYNAMIC
 1707:       {"ss-min-codesize", no_argument, NULL, ss_min_codesize},
 1708: #endif
 1709:       {"ss-min-ls",       no_argument, NULL, ss_min_ls},
 1710:       {"ss-min-lsu",      no_argument, NULL, ss_min_lsu},
 1711:       {"ss-min-nexts",    no_argument, NULL, ss_min_nexts},
 1712:       {"ss-greedy",       no_argument, &ss_greedy, 1},
 1713:       {0,0,0,0}
 1714:       /* no-init-file, no-rc? */
 1715:     };
 1716:     
 1717:     c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vhoncsx", opts, &option_index);
 1718:     
 1719:     switch (c) {
 1720:     case EOF: return;
 1721:     case '?': optind--; return;
 1722:     case 'a': *imagename = optarg; return;
 1723:     case 'i': *imagename = optarg; break;
 1724:     case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
 1725:     case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
 1726:     case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
 1727:     case 'f': fsize = convsize(optarg,sizeof(Float)); break;
 1728:     case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
 1729:     case 'p': *path = optarg; break;
 1730:     case 'o': offset_image = 1; break;
 1731:     case 'n': offset_image = 0; break;
 1732:     case 'c': clear_dictionary = 1; break;
 1733:     case 's': die_on_signal = 1; break;
 1734:     case 'x': debug = 1; break;
 1735:     case 'v': fputs(PACKAGE_STRING"\n", stderr); exit(0);
 1736:     case ss_number: static_super_number = atoi(optarg); break;
 1737:     case ss_states: maxstates = max(min(atoi(optarg),MAX_STATE),1); break;
 1738: #ifndef NO_DYNAMIC
 1739:     case ss_min_codesize: ss_cost = cost_codesize; break;
 1740: #endif
 1741:     case ss_min_ls:       ss_cost = cost_ls;       break;
 1742:     case ss_min_lsu:      ss_cost = cost_lsu;      break;
 1743:     case ss_min_nexts:    ss_cost = cost_nexts;    break;
 1744:     case 'h': 
 1745:       fprintf(stderr, "Usage: %s [engine options] ['--'] [image arguments]\n\
 1746: Engine Options:\n\
 1747:   --appl-image FILE		    equivalent to '--image-file=FILE --'\n\
 1748:   --clear-dictionary		    Initialize the dictionary with 0 bytes\n\
 1749:   -d SIZE, --data-stack-size=SIZE   Specify data stack size\n\
 1750:   --debug			    Print debugging information during startup\n\
 1751:   --die-on-signal		    exit instead of CATCHing some signals\n\
 1752:   --dynamic			    use dynamic native code\n\
 1753:   -f SIZE, --fp-stack-size=SIZE	    Specify floating point stack size\n\
 1754:   -h, --help			    Print this message and exit\n\
 1755:   -i FILE, --image-file=FILE	    Use image FILE instead of `gforth.fi'\n\
 1756:   -l SIZE, --locals-stack-size=SIZE Specify locals stack size\n\
 1757:   -m SIZE, --dictionary-size=SIZE   Specify Forth dictionary size\n\
 1758:   --no-dynamic			    Use only statically compiled primitives\n\
 1759:   --no-offset-im		    Load image at normal position\n\
 1760:   --no-super                        No dynamically formed superinstructions\n\
 1761:   --offset-image		    Load image at a different position\n\
 1762:   -p PATH, --path=PATH		    Search path for finding image and sources\n\
 1763:   --print-metrics		    Print some code generation metrics on exit\n\
 1764:   -r SIZE, --return-stack-size=SIZE Specify return stack size\n\
 1765:   --ss-greedy                       greedy, not optimal superinst selection\n\
 1766:   --ss-min-codesize                 select superinsts for smallest native code\n\
 1767:   --ss-min-ls                       minimize loads and stores\n\
 1768:   --ss-min-lsu                      minimize loads, stores, and pointer updates\n\
 1769:   --ss-min-nexts                    minimize the number of static superinsts\n\
 1770:   --ss-number=N                     use N static superinsts (default max)\n\
 1771:   --ss-states=N                     N states for stack caching (default max)\n\
 1772:   -v, --version			    Print engine version and exit\n\
 1773: SIZE arguments consist of an integer followed by a unit. The unit can be\n\
 1774:   `b' (byte), `e' (element; default), `k' (KB), `M' (MB), `G' (GB) or `T' (TB).\n",
 1775: 	      argv[0]);
 1776:       optind--;
 1777:       return;
 1778:     }
 1779:   }
 1780: }
 1781: #endif
 1782: 
 1783: #ifdef INCLUDE_IMAGE
 1784: extern Cell image[];
 1785: extern const char reloc_bits[];
 1786: #endif
 1787: 
 1788: int main(int argc, char **argv, char **env)
 1789: {
 1790: #ifdef HAS_OS
 1791:   char *path = getenv("GFORTHPATH") ? : DEFAULTPATH;
 1792: #else
 1793:   char *path = DEFAULTPATH;
 1794: #endif
 1795: #ifndef INCLUDE_IMAGE
 1796:   char *imagename="gforth.fi";
 1797:   FILE *image_file;
 1798:   Address image;
 1799: #endif
 1800:   int retvalue;
 1801: 	  
 1802: #if defined(i386) && defined(ALIGNMENT_CHECK)
 1803:   /* turn on alignment checks on the 486.
 1804:    * on the 386 this should have no effect. */
 1805:   __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
 1806:   /* this is unusable with Linux' libc.4.6.27, because this library is
 1807:      not alignment-clean; we would have to replace some library
 1808:      functions (e.g., memcpy) to make it work. Also GCC doesn't try to keep
 1809:      the stack FP-aligned. */
 1810: #endif
 1811: 
 1812:   /* buffering of the user output device */
 1813: #ifdef _IONBF
 1814:   if (isatty(fileno(stdout))) {
 1815:     fflush(stdout);
 1816:     setvbuf(stdout,NULL,_IONBF,0);
 1817:   }
 1818: #endif
 1819: 
 1820:   progname = argv[0];
 1821: 
 1822: #ifdef HAS_OS
 1823:   gforth_args(argc, argv, &path, &imagename);
 1824: #ifndef NO_DYNAMIC
 1825:   if (no_dynamic && ss_cost == cost_codesize) {
 1826:     ss_cost = cost_nexts;
 1827:     cost_sums[0] = cost_sums[1]; /* don't use cost_codesize for print-metrics */
 1828:     if (debug)
 1829:       fprintf(stderr, "--no-dynamic conflicts with --ss-min-codesize, reverting to --ss-min-nexts\n");
 1830:   }
 1831: #endif /* !defined(NO_DYNAMIC) */
 1832: #endif /* defined(HAS_OS) */
 1833: 
 1834: #ifdef INCLUDE_IMAGE
 1835:   set_stack_sizes((ImageHeader *)image);
 1836:   if(((ImageHeader *)image)->base != image)
 1837:     relocate(image, reloc_bits, ((ImageHeader *)image)->image_size,
 1838: 	     (Label*)engine(0, 0, 0, 0, 0));
 1839:   alloc_stacks((ImageHeader *)image);
 1840: #else
 1841:   image_file = open_image_file(imagename, path);
 1842:   image = loader(image_file, imagename);
 1843: #endif
 1844:   gforth_header=(ImageHeader *)image; /* used in SIGSEGV handler */
 1845: 
 1846:   {
 1847:     char path2[strlen(path)+1];
 1848:     char *p1, *p2;
 1849:     Cell environ[]= {
 1850:       (Cell)argc-(optind-1),
 1851:       (Cell)(argv+(optind-1)),
 1852:       (Cell)strlen(path),
 1853:       (Cell)path2};
 1854:     argv[optind-1] = progname;
 1855:     /*
 1856:        for (i=0; i<environ[0]; i++)
 1857:        printf("%s\n", ((char **)(environ[1]))[i]);
 1858:        */
 1859:     /* make path OS-independent by replacing path separators with NUL */
 1860:     for (p1=path, p2=path2; *p1!='\0'; p1++, p2++)
 1861:       if (*p1==PATHSEP)
 1862: 	*p2 = '\0';
 1863:       else
 1864: 	*p2 = *p1;
 1865:     *p2='\0';
 1866:     retvalue = go_forth(image, 4, environ);
 1867: #ifdef SIGPIPE
 1868:     bsd_signal(SIGPIPE, SIG_IGN);
 1869: #endif
 1870: #ifdef VM_PROFILING
 1871:     vm_print_profile(stderr);
 1872: #endif
 1873:     deprep_terminal();
 1874:   }
 1875:   if (print_metrics) {
 1876:     int i;
 1877:     fprintf(stderr, "code size = %8ld\n", dyncodesize());
 1878:     for (i=0; i<sizeof(cost_sums)/sizeof(cost_sums[0]); i++)
 1879:       fprintf(stderr, "metric %8s: %8ld\n",
 1880: 	      cost_sums[i].metricname, cost_sums[i].sum);
 1881:   }
 1882:   return retvalue;
 1883: }

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