File:  [gforth] / gforth / engine / main.c
Revision 1.209: download - view: text, annotated - select for diffs
Sat Aug 9 13:24:25 2008 UTC (15 years, 7 months ago) by anton
Branches: MAIN
CVS tags: HEAD
The Makefile now uses an EMACS variable
Eliminated most compilation warnings

    1: /* command line interpretation, image loading etc. for Gforth
    2: 
    3: 
    4:   Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006,2007,2008 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 3
   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, see http://www.gnu.org/licenses/.
   20: */
   21: 
   22: #include "config.h"
   23: #include "forth.h"
   24: #include <errno.h>
   25: #include <ctype.h>
   26: #include <stdio.h>
   27: #include <unistd.h>
   28: #include <string.h>
   29: #include <math.h>
   30: #include <sys/types.h>
   31: #ifndef STANDALONE
   32: #include <sys/stat.h>
   33: #endif
   34: #include <fcntl.h>
   35: #include <assert.h>
   36: #include <stdlib.h>
   37: #include <stdbool.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: /* output rules etc. for burg with --debug and --print-sequences */
   51: /* #define BURG_FORMAT*/
   52: 
   53: typedef enum prim_num {
   54: /* definitions of N_execute etc. */
   55: #include PRIM_NUM_I
   56:   N_START_SUPER
   57: } PrimNum;
   58: 
   59: /* global variables for engine.c 
   60:    We put them here because engine.c is compiled several times in
   61:    different ways for the same engine. */
   62: Cell *gforth_SP;
   63: Float *gforth_FP;
   64: Address gforth_UP=NULL;
   65: Cell *gforth_RP;
   66: Address gforth_LP;
   67: 
   68: #ifdef HAS_FFCALL
   69: 
   70: #include <callback.h>
   71: 
   72: va_alist gforth_clist;
   73: 
   74: void gforth_callback(Xt* fcall, void * alist)
   75: {
   76:   /* save global valiables */
   77:   Cell *rp = gforth_RP;
   78:   Cell *sp = gforth_SP;
   79:   Float *fp = gforth_FP;
   80:   Address lp = gforth_LP;
   81:   va_alist clist = gforth_clist;
   82: 
   83:   gforth_clist = (va_alist)alist;
   84: 
   85:   gforth_engine(fcall, sp, rp, fp, lp sr_call);
   86: 
   87:   /* restore global variables */
   88:   gforth_RP = rp;
   89:   gforth_SP = sp;
   90:   gforth_FP = fp;
   91:   gforth_LP = lp;
   92:   gforth_clist = clist;
   93: }
   94: #endif
   95: 
   96: #ifdef GFORTH_DEBUGGING
   97: /* define some VM registers as global variables, so they survive exceptions;
   98:    global register variables are not up to the task (according to the 
   99:    GNU C manual) */
  100: #if defined(GLOBALS_NONRELOC)
  101: saved_regs saved_regs_v;
  102: saved_regs *saved_regs_p = &saved_regs_v;
  103: #else /* !defined(GLOBALS_NONRELOC) */
  104: Xt *saved_ip;
  105: Cell *rp;
  106: #endif /* !defined(GLOBALS_NONRELOC) */
  107: #endif /* !defined(GFORTH_DEBUGGING) */
  108: 
  109: #ifdef NO_IP
  110: Label next_code;
  111: #endif
  112: 
  113: #ifdef HAS_FILE
  114: char* fileattr[6]={"rb","rb","r+b","r+b","wb","wb"};
  115: char* pfileattr[6]={"r","r","r+","r+","w","w"};
  116: 
  117: #ifndef O_BINARY
  118: #define O_BINARY 0
  119: #endif
  120: #ifndef O_TEXT
  121: #define O_TEXT 0
  122: #endif
  123: 
  124: int ufileattr[6]= {
  125:   O_RDONLY|O_BINARY, O_RDONLY|O_BINARY,
  126:   O_RDWR  |O_BINARY, O_RDWR  |O_BINARY,
  127:   O_WRONLY|O_BINARY, O_WRONLY|O_BINARY };
  128: #endif
  129: /* end global vars for engine.c */
  130: 
  131: #define PRIM_VERSION 1
  132: /* increment this whenever the primitives change in an incompatible way */
  133: 
  134: #ifndef DEFAULTPATH
  135: #  define DEFAULTPATH "."
  136: #endif
  137: 
  138: #ifdef MSDOS
  139: jmp_buf throw_jmp_buf;
  140: #endif
  141: 
  142: #if defined(DOUBLY_INDIRECT)
  143: #  define CFA(n)	({Cell _n = (n); ((Cell)(((_n & 0x4000) ? symbols : xts)+(_n&~0x4000UL)));})
  144: #else
  145: #  define CFA(n)	((Cell)(symbols+((n)&~0x4000UL)))
  146: #endif
  147: 
  148: #define maxaligned(n)	(typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
  149: 
  150: static UCell dictsize=0;
  151: static UCell dsize=0;
  152: static UCell rsize=0;
  153: static UCell fsize=0;
  154: static UCell lsize=0;
  155: int offset_image=0;
  156: int die_on_signal=0;
  157: int ignore_async_signals=0;
  158: #ifndef INCLUDE_IMAGE
  159: static int clear_dictionary=0;
  160: UCell pagesize=1;
  161: char *progname;
  162: #else
  163: char *progname = "gforth";
  164: int optind = 1;
  165: #endif
  166: #ifndef MAP_NORESERVE
  167: #define MAP_NORESERVE 0
  168: #endif
  169: /* IF you have an old Cygwin, this may help:
  170: #ifdef __CYGWIN__
  171: #define MAP_NORESERVE 0
  172: #endif
  173: */
  174: static int map_noreserve=MAP_NORESERVE;
  175: 
  176: #define CODE_BLOCK_SIZE (512*1024) /* !! overflow handling for -native */
  177: Address code_area=0;
  178: Cell code_area_size = CODE_BLOCK_SIZE;
  179: Address code_here=NULL+CODE_BLOCK_SIZE; /* does for code-area what HERE
  180: 					   does for the dictionary */
  181: Address start_flush=NULL; /* start of unflushed code */
  182: Cell last_jump=0; /* if the last prim was compiled without jump, this
  183:                      is it's number, otherwise this contains 0 */
  184: 
  185: static int no_super=0;   /* true if compile_prim should not fuse prims */
  186: static int no_dynamic=NO_DYNAMIC_DEFAULT; /* if true, no code is generated
  187: 					     dynamically */
  188: static int print_metrics=0; /* if true, print metrics on exit */
  189: static int static_super_number = 10000; /* number of ss used if available */
  190: #define MAX_STATE 9 /* maximum number of states */
  191: static int maxstates = MAX_STATE; /* number of states for stack caching */
  192: static int ss_greedy = 0; /* if true: use greedy, not optimal ss selection */
  193: static int diag = 0; /* if true: print diagnostic informations */
  194: static int tpa_noequiv = 0;     /* if true: no state equivalence checking */
  195: static int tpa_noautomaton = 0; /* if true: no tree parsing automaton */
  196: static int tpa_trace = 0; /* if true: data for line graph of new states etc. */
  197: static int print_sequences = 0; /* print primitive sequences for optimization */
  198: static int relocs = 0;
  199: static int nonrelocs = 0;
  200: 
  201: #ifdef HAS_DEBUG
  202: int debug=0;
  203: # define debugp(x...) do { if (debug) fprintf(x); } while (0)
  204: #else
  205: # define perror(x...)
  206: # define fprintf(x...)
  207: # define debugp(x...)
  208: #endif
  209: 
  210: ImageHeader *gforth_header;
  211: Label *vm_prims;
  212: #ifdef DOUBLY_INDIRECT
  213: Label *xts; /* same content as vm_prims, but should only be used for xts */
  214: #endif
  215: 
  216: #ifndef NO_DYNAMIC
  217: #ifndef CODE_ALIGNMENT
  218: #define CODE_ALIGNMENT 0
  219: #endif
  220: 
  221: #define MAX_IMMARGS 2
  222: 
  223: typedef struct {
  224:   Label start; /* NULL if not relocatable */
  225:   Cell length; /* only includes the jump iff superend is true*/
  226:   Cell restlength; /* length of the rest (i.e., the jump or (on superend) 0) */
  227:   char superend; /* true if primitive ends superinstruction, i.e.,
  228:                      unconditional branch, execute, etc. */
  229:   Cell nimmargs;
  230:   struct immarg {
  231:     Cell offset; /* offset of immarg within prim */
  232:     char rel;    /* true if immarg is relative */
  233:   } immargs[MAX_IMMARGS];
  234: } PrimInfo;
  235: 
  236: PrimInfo *priminfos;
  237: PrimInfo **decomp_prims;
  238: 
  239: const char const* const prim_names[]={
  240: #include PRIM_NAMES_I
  241: };
  242: 
  243: void init_ss_cost(void);
  244: 
  245: static int is_relocatable(int p)
  246: {
  247:   return !no_dynamic && priminfos[p].start != NULL;
  248: }
  249: #else /* defined(NO_DYNAMIC) */
  250: static int is_relocatable(int p)
  251: {
  252:   return 0;
  253: }
  254: #endif /* defined(NO_DYNAMIC) */
  255: 
  256: #ifdef MEMCMP_AS_SUBROUTINE
  257: int gforth_memcmp(const char * s1, const char * s2, size_t n)
  258: {
  259:   return memcmp(s1, s2, n);
  260: }
  261: #endif
  262: 
  263: static Cell max(Cell a, Cell b)
  264: {
  265:   return a>b?a:b;
  266: }
  267: 
  268: static Cell min(Cell a, Cell b)
  269: {
  270:   return a<b?a:b;
  271: }
  272: 
  273: #ifndef STANDALONE
  274: /* image file format:
  275:  *  "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.4.0 -i\n")
  276:  *   padding to a multiple of 8
  277:  *   magic: "Gforth3x" means format 0.6,
  278:  *              where x is a byte with
  279:  *              bit 7:   reserved = 0
  280:  *              bit 6:5: address unit size 2^n octets
  281:  *              bit 4:3: character size 2^n octets
  282:  *              bit 2:1: cell size 2^n octets
  283:  *              bit 0:   endian, big=0, little=1.
  284:  *  The magic are always 8 octets, no matter what the native AU/character size is
  285:  *  padding to max alignment (no padding necessary on current machines)
  286:  *  ImageHeader structure (see forth.h)
  287:  *  data (size in ImageHeader.image_size)
  288:  *  tags ((if relocatable, 1 bit/data cell)
  289:  *
  290:  * tag==1 means that the corresponding word is an address;
  291:  * If the word is >=0, the address is within the image;
  292:  * addresses within the image are given relative to the start of the image.
  293:  * If the word =-1 (CF_NIL), the address is NIL,
  294:  * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
  295:  * If the word =CF(DODOES), it's a DOES> CFA
  296:  * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
  297:  *					possibly containing a jump to dodoes)
  298:  * If the word is <CF(DOESJUMP) and bit 14 is set, it's the xt of a primitive
  299:  * If the word is <CF(DOESJUMP) and bit 14 is clear, 
  300:  *                                        it's the threaded code of a primitive
  301:  * bits 13..9 of a primitive token state which group the primitive belongs to,
  302:  * bits 8..0 of a primitive token index into the group
  303:  */
  304: 
  305: Cell groups[32] = {
  306:   0,
  307:   0
  308: #undef GROUP
  309: #undef GROUPADD
  310: #define GROUPADD(n) +n
  311: #define GROUP(x, n) , 0
  312: #include PRIM_GRP_I
  313: #undef GROUP
  314: #undef GROUPADD
  315: #define GROUP(x, n)
  316: #define GROUPADD(n)
  317: };
  318: 
  319: static unsigned char *branch_targets(Cell *image, const unsigned char *bitstring,
  320: 			      int size, Cell base)
  321:      /* produce a bitmask marking all the branch targets */
  322: {
  323:   int i=0, j, k, steps=(((size-1)/sizeof(Cell))/RELINFOBITS)+1;
  324:   Cell token;
  325:   unsigned char bits;
  326:   unsigned char *result=malloc(steps);
  327: 
  328:   memset(result, 0, steps);
  329:   for(k=0; k<steps; k++) {
  330:     for(j=0, bits=bitstring[k]; j<RELINFOBITS; j++, i++, bits<<=1) {
  331:       if(bits & (1U << (RELINFOBITS-1))) {
  332: 	assert(i*sizeof(Cell) < size);
  333:         token=image[i];
  334: 	if (token>=base) { /* relocatable address */
  335: 	  UCell bitnum=(token-base)/sizeof(Cell);
  336: 	  if (bitnum/RELINFOBITS < (UCell)steps)
  337: 	    result[bitnum/RELINFOBITS] |= 1U << ((~bitnum)&(RELINFOBITS-1));
  338: 	}
  339:       }
  340:     }
  341:   }
  342:   return result;
  343: }
  344: 
  345: void gforth_relocate(Cell *image, const Char *bitstring, 
  346: 		     UCell size, Cell base, Label symbols[])
  347: {
  348:   int i=0, j, k, steps=(((size-1)/sizeof(Cell))/RELINFOBITS)+1;
  349:   Cell token;
  350:   char bits;
  351:   Cell max_symbols;
  352:   /* 
  353:    * A virtual start address that's the real start address minus 
  354:    * the one in the image 
  355:    */
  356:   Cell *start = (Cell * ) (((void *) image) - ((void *) base));
  357:   unsigned char *targets = branch_targets(image, bitstring, size, base);
  358: 
  359:   /* group index into table */
  360:   if(groups[31]==0) {
  361:     int groupsum=0;
  362:     for(i=0; i<32; i++) {
  363:       groupsum += groups[i];
  364:       groups[i] = groupsum;
  365:       /* printf("group[%d]=%d\n",i,groupsum); */
  366:     }
  367:     i=0;
  368:   }
  369:   
  370: /* printf("relocating to %x[%x] start=%x base=%x\n", image, size, start, base); */
  371:   
  372:   for (max_symbols=0; symbols[max_symbols]!=0; max_symbols++)
  373:     ;
  374:   max_symbols--;
  375: 
  376:   for(k=0; k<steps; k++) {
  377:     for(j=0, bits=bitstring[k]; j<RELINFOBITS; j++, i++, bits<<=1) {
  378:       /*      fprintf(stderr,"relocate: image[%d]\n", i);*/
  379:       if(bits & (1U << (RELINFOBITS-1))) {
  380: 	assert(i*sizeof(Cell) < size);
  381: 	/* fprintf(stderr,"relocate: image[%d]=%d of %d\n", i, image[i], size/sizeof(Cell)); */
  382:         token=image[i];
  383: 	if(token<0) {
  384: 	  int group = (-token & 0x3E00) >> 9;
  385: 	  if(group == 0) {
  386: 	    switch(token|0x4000) {
  387: 	    case CF_NIL      : image[i]=0; break;
  388: #if !defined(DOUBLY_INDIRECT)
  389: 	    case CF(DOCOL)   :
  390: 	    case CF(DOVAR)   :
  391: 	    case CF(DOCON)   :
  392: 	    case CF(DOVAL)   :
  393: 	    case CF(DOUSER)  : 
  394: 	    case CF(DODEFER) : 
  395: 	    case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(token)]); break;
  396: 	    case CF(DOESJUMP): image[i]=0; break;
  397: #endif /* !defined(DOUBLY_INDIRECT) */
  398: 	    case CF(DODOES)  :
  399: 	      MAKE_DOES_CF(image+i,(Xt *)(image[i+1]+((Cell)start)));
  400: 	      break;
  401: 	    default          : /* backward compatibility */
  402: /*	      printf("Code field generation image[%x]:=CFA(%x)\n",
  403: 		     i, CF(image[i])); */
  404: 	      if (CF((token | 0x4000))<max_symbols) {
  405: 		image[i]=(Cell)CFA(CF(token));
  406: #ifdef DIRECT_THREADED
  407: 		if ((token & 0x4000) == 0) { /* threade code, no CFA */
  408: 		  if (targets[k] & (1U<<(RELINFOBITS-1-j)))
  409: 		    compile_prim1(0);
  410: 		  compile_prim1(&image[i]);
  411: 		}
  412: #endif
  413: 	      } else
  414: 		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);
  415: 	    }
  416: 	  } else {
  417: 	    int tok = -token & 0x1FF;
  418: 	    if (tok < (groups[group+1]-groups[group])) {
  419: #if defined(DOUBLY_INDIRECT)
  420: 	      image[i]=(Cell)CFA(((groups[group]+tok) | (CF(token) & 0x4000)));
  421: #else
  422: 	      image[i]=(Cell)CFA((groups[group]+tok));
  423: #endif
  424: #ifdef DIRECT_THREADED
  425: 	      if ((token & 0x4000) == 0) { /* threade code, no CFA */
  426: 		if (targets[k] & (1U<<(RELINFOBITS-1-j)))
  427: 		  compile_prim1(0);
  428: 		compile_prim1(&image[i]);
  429: 	      }
  430: #endif
  431: 	    } else
  432: 	      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);
  433: 	  }
  434: 	} else {
  435:           /* if base is > 0: 0 is a null reference so don't adjust*/
  436:           if (token>=base) {
  437:             image[i]+=(Cell)start;
  438:           }
  439:         }
  440:       }
  441:     }
  442:   }
  443:   free(targets);
  444:   finish_code();
  445:   ((ImageHeader*)(image))->base = (Address) image;
  446: }
  447: 
  448: #ifndef DOUBLY_INDIRECT
  449: static UCell checksum(Label symbols[])
  450: {
  451:   UCell r=PRIM_VERSION;
  452:   Cell i;
  453: 
  454:   for (i=DOCOL; i<=DOESJUMP; i++) {
  455:     r ^= (UCell)(symbols[i]);
  456:     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
  457:   }
  458: #ifdef DIRECT_THREADED
  459:   /* we have to consider all the primitives */
  460:   for (; symbols[i]!=(Label)0; i++) {
  461:     r ^= (UCell)(symbols[i]);
  462:     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
  463:   }
  464: #else
  465:   /* in indirect threaded code all primitives are accessed through the
  466:      symbols table, so we just have to put the base address of symbols
  467:      in the checksum */
  468:   r ^= (UCell)symbols;
  469: #endif
  470:   return r;
  471: }
  472: #endif
  473: 
  474: static Address verbose_malloc(Cell size)
  475: {
  476:   Address r;
  477:   /* leave a little room (64B) for stack underflows */
  478:   if ((r = malloc(size+64))==NULL) {
  479:     perror(progname);
  480:     exit(1);
  481:   }
  482:   r = (Address)((((Cell)r)+(sizeof(Float)-1))&(-sizeof(Float)));
  483:   debugp(stderr, "malloc succeeds, address=$%lx\n", (long)r);
  484:   return r;
  485: }
  486: 
  487: static Address next_address=0;
  488: static void after_alloc(Address r, Cell size)
  489: {
  490:   if (r != (Address)-1) {
  491:     debugp(stderr, "success, address=$%lx\n", (long) r);
  492: #if 0
  493:     /* not needed now that we protect the stacks with mprotect */
  494:     if (pagesize != 1)
  495:       next_address = (Address)(((((Cell)r)+size-1)&-pagesize)+2*pagesize); /* leave one page unmapped */
  496: #endif
  497:   } else {
  498:     debugp(stderr, "failed: %s\n", strerror(errno));
  499:   }
  500: }
  501: 
  502: #ifndef MAP_FAILED
  503: #define MAP_FAILED ((Address) -1)
  504: #endif
  505: #ifndef MAP_FILE
  506: # define MAP_FILE 0
  507: #endif
  508: #ifndef MAP_PRIVATE
  509: # define MAP_PRIVATE 0
  510: #endif
  511: #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
  512: # define MAP_ANON MAP_ANONYMOUS
  513: #endif
  514: 
  515: #if defined(HAVE_MMAP)
  516: static Address alloc_mmap(Cell size)
  517: {
  518:   Address r;
  519: 
  520: #if defined(MAP_ANON)
  521:   debugp(stderr,"try mmap($%lx, $%lx, ..., MAP_ANON, ...); ", (long)next_address, (long)size);
  522:   r = mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE|map_noreserve, -1, 0);
  523: #else /* !defined(MAP_ANON) */
  524:   /* Ultrix (at least) does not define MAP_FILE and MAP_PRIVATE (both are
  525:      apparently defaults) */
  526:   static int dev_zero=-1;
  527: 
  528:   if (dev_zero == -1)
  529:     dev_zero = open("/dev/zero", O_RDONLY);
  530:   if (dev_zero == -1) {
  531:     r = MAP_FAILED;
  532:     debugp(stderr, "open(\"/dev/zero\"...) failed (%s), no mmap; ", 
  533: 	      strerror(errno));
  534:   } else {
  535:     debugp(stderr,"try mmap($%lx, $%lx, ..., MAP_FILE, dev_zero, ...); ", (long)next_address, (long)size);
  536:     r=mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FILE|MAP_PRIVATE|map_noreserve, dev_zero, 0);
  537:   }
  538: #endif /* !defined(MAP_ANON) */
  539:   after_alloc(r, size);
  540:   return r;  
  541: }
  542: 
  543: static void page_noaccess(Address a)
  544: {
  545:   /* try mprotect first; with munmap the page might be allocated later */
  546:   debugp(stderr, "try mprotect(%p,%ld,PROT_NONE); ", a, (long)pagesize);
  547:   if (mprotect(a, pagesize, PROT_NONE)==0) {
  548:     debugp(stderr, "ok\n");
  549:     return;
  550:   }
  551:   debugp(stderr, "failed: %s\n", strerror(errno));
  552:   debugp(stderr, "try munmap(%p,%ld); ", a, (long)pagesize);
  553:   if (munmap(a,pagesize)==0) {
  554:     debugp(stderr, "ok\n");
  555:     return;
  556:   }
  557:   debugp(stderr, "failed: %s\n", strerror(errno));
  558: }  
  559: 
  560: static size_t wholepage(size_t n)
  561: {
  562:   return (n+pagesize-1)&~(pagesize-1);
  563: }
  564: #endif
  565: 
  566: Address gforth_alloc(Cell size)
  567: {
  568: #if HAVE_MMAP
  569:   Address r;
  570: 
  571:   r=alloc_mmap(size);
  572:   if (r!=(Address)MAP_FAILED)
  573:     return r;
  574: #endif /* HAVE_MMAP */
  575:   /* use malloc as fallback */
  576:   return verbose_malloc(size);
  577: }
  578: 
  579: static Address dict_alloc_read(FILE *file, Cell imagesize, Cell dictsize, Cell offset)
  580: {
  581:   Address image = MAP_FAILED;
  582: 
  583: #if defined(HAVE_MMAP)
  584:   if (offset==0) {
  585:     image=alloc_mmap(dictsize);
  586:     if (image != (Address)MAP_FAILED) {
  587:       Address image1;
  588:       debugp(stderr,"try mmap($%lx, $%lx, ..., MAP_FIXED|MAP_FILE, imagefile, 0); ", (long)image, (long)imagesize);
  589:       image1 = mmap(image, imagesize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FIXED|MAP_FILE|MAP_PRIVATE|map_noreserve, fileno(file), 0);
  590:       after_alloc(image1,dictsize);
  591:       if (image1 == (Address)MAP_FAILED)
  592: 	goto read_image;
  593:     }
  594:   }
  595: #endif /* defined(HAVE_MMAP) */
  596:   if (image == (Address)MAP_FAILED) {
  597:     image = gforth_alloc(dictsize+offset)+offset;
  598:   read_image:
  599:     rewind(file);  /* fseek(imagefile,0L,SEEK_SET); */
  600:     fread(image, 1, imagesize, file);
  601:   }
  602:   return image;
  603: }
  604: #endif
  605: 
  606: void set_stack_sizes(ImageHeader * header)
  607: {
  608:   if (dictsize==0)
  609:     dictsize = header->dict_size;
  610:   if (dsize==0)
  611:     dsize = header->data_stack_size;
  612:   if (rsize==0)
  613:     rsize = header->return_stack_size;
  614:   if (fsize==0)
  615:     fsize = header->fp_stack_size;
  616:   if (lsize==0)
  617:     lsize = header->locals_stack_size;
  618:   dictsize=maxaligned(dictsize);
  619:   dsize=maxaligned(dsize);
  620:   rsize=maxaligned(rsize);
  621:   lsize=maxaligned(lsize);
  622:   fsize=maxaligned(fsize);
  623: }
  624: 
  625: #ifdef STANDALONE
  626: void alloc_stacks(ImageHeader * h)
  627: {
  628: #define SSTACKSIZE 0x200
  629:   static Cell dstack[SSTACKSIZE+1];
  630:   static Cell rstack[SSTACKSIZE+1];
  631: 
  632:   h->dict_size=dictsize;
  633:   h->data_stack_size=dsize;
  634:   h->fp_stack_size=fsize;
  635:   h->return_stack_size=rsize;
  636:   h->locals_stack_size=lsize;
  637: 
  638:   h->data_stack_base=dstack+SSTACKSIZE;
  639:   //  h->fp_stack_base=gforth_alloc(fsize);
  640:   h->return_stack_base=rstack+SSTACKSIZE;
  641:   //  h->locals_stack_base=gforth_alloc(lsize);
  642: }
  643: #else
  644: void alloc_stacks(ImageHeader * h)
  645: {
  646:   h->dict_size=dictsize;
  647:   h->data_stack_size=dsize;
  648:   h->fp_stack_size=fsize;
  649:   h->return_stack_size=rsize;
  650:   h->locals_stack_size=lsize;
  651: 
  652: #if defined(HAVE_MMAP) && !defined(STANDALONE)
  653:   if (pagesize > 1) {
  654:     size_t p = pagesize;
  655:     size_t totalsize =
  656:       wholepage(dsize)+wholepage(fsize)+wholepage(rsize)+wholepage(lsize)+5*p;
  657:     Address a = alloc_mmap(totalsize);
  658:     if (a != (Address)MAP_FAILED) {
  659:       page_noaccess(a); a+=p; h->  data_stack_base=a; a+=wholepage(dsize);
  660:       page_noaccess(a); a+=p; h->    fp_stack_base=a; a+=wholepage(fsize);
  661:       page_noaccess(a); a+=p; h->return_stack_base=a; a+=wholepage(rsize);
  662:       page_noaccess(a); a+=p; h->locals_stack_base=a; a+=wholepage(lsize);
  663:       page_noaccess(a);
  664:       debugp(stderr,"stack addresses: d=%p f=%p r=%p l=%p\n",
  665: 	     h->data_stack_base,
  666: 	     h->fp_stack_base,
  667: 	     h->return_stack_base,
  668: 	     h->locals_stack_base);
  669:       return;
  670:     }
  671:   }
  672: #endif
  673:   h->data_stack_base=gforth_alloc(dsize);
  674:   h->fp_stack_base=gforth_alloc(fsize);
  675:   h->return_stack_base=gforth_alloc(rsize);
  676:   h->locals_stack_base=gforth_alloc(lsize);
  677: }
  678: #endif
  679: 
  680: #warning You can ignore the warnings about clobbered variables in gforth_go
  681: int gforth_go(Address image, int stack, Cell *entries)
  682: {
  683:   volatile ImageHeader *image_header = (ImageHeader *)image;
  684:   Cell *sp0=(Cell*)(image_header->data_stack_base + dsize);
  685:   Cell *rp0=(Cell *)(image_header->return_stack_base + rsize);
  686:   Float *fp0=(Float *)(image_header->fp_stack_base + fsize);
  687: #ifdef GFORTH_DEBUGGING
  688:   volatile Cell *orig_rp0=rp0;
  689: #endif
  690:   Address lp0=image_header->locals_stack_base + lsize;
  691:   Xt *ip0=(Xt *)(image_header->boot_entry);
  692: #ifdef SYSSIGNALS
  693:   int throw_code;
  694: #endif
  695: 
  696:   /* ensure that the cached elements (if any) are accessible */
  697: #if !(defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING))
  698:   sp0 -= 8; /* make stuff below bottom accessible for stack caching */
  699:   fp0--;
  700: #endif
  701:   
  702:   for(;stack>0;stack--)
  703:     *--sp0=entries[stack-1];
  704: 
  705: #if defined(SYSSIGNALS) && !defined(STANDALONE)
  706:   get_winsize();
  707:    
  708:   install_signal_handlers(); /* right place? */
  709:   
  710:   if ((throw_code=setjmp(throw_jmp_buf))) {
  711:     static Cell signal_data_stack[24];
  712:     static Cell signal_return_stack[16];
  713:     static Float signal_fp_stack[1];
  714: 
  715:     signal_data_stack[15]=throw_code;
  716: 
  717: #ifdef GFORTH_DEBUGGING
  718:     debugp(stderr,"\ncaught signal, throwing exception %d, ip=%p rp=%p\n",
  719: 	      throw_code, saved_ip, rp);
  720:     if (rp <= orig_rp0 && rp > (Cell *)(image_header->return_stack_base+5)) {
  721:       /* no rstack overflow or underflow */
  722:       rp0 = rp;
  723:       *--rp0 = (Cell)saved_ip;
  724:     }
  725:     else /* I love non-syntactic ifdefs :-) */
  726:       rp0 = signal_return_stack+16;
  727: #else  /* !defined(GFORTH_DEBUGGING) */
  728:     debugp(stderr,"\ncaught signal, throwing exception %d\n", throw_code);
  729:       rp0 = signal_return_stack+16;
  730: #endif /* !defined(GFORTH_DEBUGGING) */
  731:     /* fprintf(stderr, "rp=$%x\n",rp0);*/
  732:     
  733:     return((int)(Cell)gforth_engine(image_header->throw_entry, signal_data_stack+15,
  734: 		       rp0, signal_fp_stack, 0 sr_call));
  735:   }
  736: #endif
  737: 
  738:   return((int)(Cell)gforth_engine(ip0,sp0,rp0,fp0,lp0 sr_call));
  739: }
  740: 
  741: #if !defined(INCLUDE_IMAGE) && !defined(STANDALONE)
  742: static void print_sizes(Cell sizebyte)
  743:      /* print size information */
  744: {
  745:   static char* endianstring[]= { "   big","little" };
  746:   
  747:   fprintf(stderr,"%s endian, cell=%d bytes, char=%d bytes, au=%d bytes\n",
  748: 	  endianstring[sizebyte & 1],
  749: 	  1 << ((sizebyte >> 1) & 3),
  750: 	  1 << ((sizebyte >> 3) & 3),
  751: 	  1 << ((sizebyte >> 5) & 3));
  752: }
  753: 
  754: /* static superinstruction stuff */
  755: 
  756: struct cost { /* super_info might be a more accurate name */
  757:   char loads;       /* number of stack loads */
  758:   char stores;      /* number of stack stores */
  759:   char updates;     /* number of stack pointer updates */
  760:   char branch;	    /* is it a branch (SET_IP) */
  761:   unsigned char state_in;    /* state on entry */
  762:   unsigned char state_out;   /* state on exit */
  763:   unsigned char imm_ops;     /* number of immediate operands */
  764:   short offset;     /* offset into super2 table */
  765:   unsigned char length;      /* number of components */
  766: };
  767: 
  768: PrimNum super2[] = {
  769: #include SUPER2_I
  770: };
  771: 
  772: struct cost super_costs[] = {
  773: #include COSTS_I
  774: };
  775: 
  776: struct super_state {
  777:   struct super_state *next;
  778:   PrimNum super;
  779: };
  780: 
  781: #define HASH_SIZE 256
  782: 
  783: struct super_table_entry {
  784:   struct super_table_entry *next;
  785:   PrimNum *start;
  786:   short length;
  787:   struct super_state *ss_list; /* list of supers */
  788: } *super_table[HASH_SIZE];
  789: int max_super=2;
  790: 
  791: struct super_state *state_transitions=NULL;
  792: 
  793: static int hash_super(PrimNum *start, int length)
  794: {
  795:   int i, r;
  796:   
  797:   for (i=0, r=0; i<length; i++) {
  798:     r <<= 1;
  799:     r += start[i];
  800:   }
  801:   return r & (HASH_SIZE-1);
  802: }
  803: 
  804: static struct super_state **lookup_super(PrimNum *start, int length)
  805: {
  806:   int hash=hash_super(start,length);
  807:   struct super_table_entry *p = super_table[hash];
  808: 
  809:   /* assert(length >= 2); */
  810:   for (; p!=NULL; p = p->next) {
  811:     if (length == p->length &&
  812: 	memcmp((char *)p->start, (char *)start, length*sizeof(PrimNum))==0)
  813:       return &(p->ss_list);
  814:   }
  815:   return NULL;
  816: }
  817: 
  818: static void prepare_super_table()
  819: {
  820:   int i;
  821:   int nsupers = 0;
  822: 
  823:   for (i=0; i<sizeof(super_costs)/sizeof(super_costs[0]); i++) {
  824:     struct cost *c = &super_costs[i];
  825:     if ((c->length < 2 || nsupers < static_super_number) &&
  826: 	c->state_in < maxstates && c->state_out < maxstates) {
  827:       struct super_state **ss_listp= lookup_super(super2+c->offset, c->length);
  828:       struct super_state *ss = malloc(sizeof(struct super_state));
  829:       ss->super= i;
  830:       if (c->offset==N_noop && i != N_noop) {
  831: 	if (is_relocatable(i)) {
  832: 	  ss->next = state_transitions;
  833: 	  state_transitions = ss;
  834: 	}
  835:       } else if (ss_listp != NULL) {
  836: 	ss->next = *ss_listp;
  837: 	*ss_listp = ss;
  838:       } else {
  839: 	int hash = hash_super(super2+c->offset, c->length);
  840: 	struct super_table_entry **p = &super_table[hash];
  841: 	struct super_table_entry *e = malloc(sizeof(struct super_table_entry));
  842: 	ss->next = NULL;
  843: 	e->next = *p;
  844: 	e->start = super2 + c->offset;
  845: 	e->length = c->length;
  846: 	e->ss_list = ss;
  847: 	*p = e;
  848:       }
  849:       if (c->length > max_super)
  850: 	max_super = c->length;
  851:       if (c->length >= 2)
  852: 	nsupers++;
  853:     }
  854:   }
  855:   debugp(stderr, "Using %d static superinsts\n", nsupers);
  856:   if (nsupers>0 && !tpa_noautomaton && !tpa_noequiv) {
  857:     /* Currently these two things don't work together; see Section 3.2
  858:        of <http://www.complang.tuwien.ac.at/papers/ertl+06pldi.ps.gz>,
  859:        in particular Footnote 6 for the reason; hmm, we should be able
  860:        to use an automaton without state equivalence, but that costs
  861:        significant space so we only do it if the user explicitly
  862:        disables state equivalence. */
  863:     debugp(stderr, "Disabling tpa-automaton, because nsupers>0 and state equivalence is enabled.\n");
  864:     tpa_noautomaton = true;
  865:   }
  866: }
  867: 
  868: /* dynamic replication/superinstruction stuff */
  869: 
  870: #ifndef NO_DYNAMIC
  871: static int compare_priminfo_length(const void *_a, const void *_b)
  872: {
  873:   PrimInfo **a = (PrimInfo **)_a;
  874:   PrimInfo **b = (PrimInfo **)_b;
  875:   Cell diff = (*a)->length - (*b)->length;
  876:   if (diff)
  877:     return diff;
  878:   else /* break ties by start address; thus the decompiler produces
  879:           the earliest primitive with the same code (e.g. noop instead
  880:           of (char) and @ instead of >code-address */
  881:     return (*b)->start - (*a)->start;
  882: }
  883: #endif /* !defined(NO_DYNAMIC) */
  884: 
  885: static char MAYBE_UNUSED superend[]={
  886: #include PRIM_SUPEREND_I
  887: };
  888: 
  889: Cell npriminfos=0;
  890: 
  891: Label goto_start;
  892: Cell goto_len;
  893: 
  894: #ifndef NO_DYNAMIC
  895: static int compare_labels(const void *pa, const void *pb)
  896: {
  897:   Label a = *(Label *)pa;
  898:   Label b = *(Label *)pb;
  899:   return a-b;
  900: }
  901: #endif
  902: 
  903: static Label bsearch_next(Label key, Label *a, UCell n)
  904:      /* a is sorted; return the label >=key that is the closest in a;
  905:         return NULL if there is no label in a >=key */
  906: {
  907:   int mid = (n-1)/2;
  908:   if (n<1)
  909:     return NULL;
  910:   if (n == 1) {
  911:     if (a[0] < key)
  912:       return NULL;
  913:     else
  914:       return a[0];
  915:   }
  916:   if (a[mid] < key)
  917:     return bsearch_next(key, a+mid+1, n-mid-1);
  918:   else
  919:     return bsearch_next(key, a, mid+1);
  920: }
  921: 
  922: static void check_prims(Label symbols1[])
  923: {
  924:   int i;
  925: #ifndef NO_DYNAMIC
  926:   Label *symbols2, *symbols3, *ends1, *ends1j, *ends1jsorted, *goto_p;
  927:   int nends1j;
  928: #endif
  929: 
  930:   if (debug)
  931: #ifdef __VERSION__
  932:     fprintf(stderr, "Compiled with gcc-" __VERSION__ "\n");
  933: #else
  934: #define xstr(s) str(s)
  935: #define str(s) #s
  936:   fprintf(stderr, "Compiled with gcc-" xstr(__GNUC__) "." xstr(__GNUC_MINOR__) "\n"); 
  937: #endif
  938:   for (i=0; symbols1[i]!=0; i++)
  939:     ;
  940:   npriminfos = i;
  941:   
  942: #ifndef NO_DYNAMIC
  943:   if (no_dynamic)
  944:     return;
  945:   symbols2=gforth_engine2(0,0,0,0,0 sr_call);
  946: #if NO_IP
  947:   symbols3=gforth_engine3(0,0,0,0,0 sr_call);
  948: #else
  949:   symbols3=symbols1;
  950: #endif
  951:   ends1 = symbols1+i+1;
  952:   ends1j =   ends1+i;
  953:   goto_p = ends1j+i+1; /* goto_p[0]==before; ...[1]==after;*/
  954:   nends1j = i+1;
  955:   ends1jsorted = (Label *)alloca(nends1j*sizeof(Label));
  956:   memcpy(ends1jsorted,ends1j,nends1j*sizeof(Label));
  957:   qsort(ends1jsorted, nends1j, sizeof(Label), compare_labels);
  958: 
  959:   /* check whether the "goto *" is relocatable */
  960:   goto_len = goto_p[1]-goto_p[0];
  961:   debugp(stderr, "goto * %p %p len=%ld\n",
  962: 	 goto_p[0],symbols2[goto_p-symbols1],(long)goto_len);
  963:   if (memcmp(goto_p[0],symbols2[goto_p-symbols1],goto_len)!=0) { /* unequal */
  964:     no_dynamic=1;
  965:     debugp(stderr,"  not relocatable, disabling dynamic code generation\n");
  966:     init_ss_cost();
  967:     return;
  968:   }
  969:   goto_start = goto_p[0];
  970:   
  971:   priminfos = calloc(i,sizeof(PrimInfo));
  972:   for (i=0; symbols1[i]!=0; i++) {
  973:     int prim_len = ends1[i]-symbols1[i];
  974:     PrimInfo *pi=&priminfos[i];
  975:     struct cost *sc=&super_costs[i];
  976:     int j=0;
  977:     char *s1 = (char *)symbols1[i];
  978:     char *s2 = (char *)symbols2[i];
  979:     char *s3 = (char *)symbols3[i];
  980:     Label endlabel = bsearch_next(symbols1[i]+1,ends1jsorted,nends1j);
  981: 
  982:     pi->start = s1;
  983:     pi->superend = superend[i]|no_super;
  984:     pi->length = prim_len;
  985:     pi->restlength = endlabel - symbols1[i] - pi->length;
  986:     pi->nimmargs = 0;
  987:     relocs++;
  988: #if defined(BURG_FORMAT)
  989:     { /* output as burg-style rules */
  990:       int p=super_costs[i].offset;
  991:       if (p==N_noop)
  992: 	debugp(stderr, "S%d: S%d = %d (%d);", sc->state_in, sc->state_out, i+1, pi->length);
  993:       else
  994: 	debugp(stderr, "S%d: op%d(S%d) = %d (%d);", sc->state_in, p, sc->state_out, i+1, pi->length);
  995:     }
  996: #else
  997:     debugp(stderr, "%-15s %d-%d %4d %p %p %p len=%3ld rest=%2ld send=%1d",
  998: 	   prim_names[i], sc->state_in, sc->state_out,
  999: 	   i, s1, s2, s3, (long)(pi->length), (long)(pi->restlength),
 1000: 	   pi->superend);
 1001: #endif
 1002:     if (endlabel == NULL) {
 1003:       pi->start = NULL; /* not relocatable */
 1004:       if (pi->length<0) pi->length=100;
 1005: #ifndef BURG_FORMAT
 1006:       debugp(stderr,"\n   non_reloc: no J label > start found\n");
 1007: #endif
 1008:       relocs--;
 1009:       nonrelocs++;
 1010:       continue;
 1011:     }
 1012:     if (ends1[i] > endlabel && !pi->superend) {
 1013:       pi->start = NULL; /* not relocatable */
 1014:       pi->length = endlabel-symbols1[i];
 1015: #ifndef BURG_FORMAT
 1016:       debugp(stderr,"\n   non_reloc: there is a J label before the K label (restlength<0)\n");
 1017: #endif
 1018:       relocs--;
 1019:       nonrelocs++;
 1020:       continue;
 1021:     }
 1022:     if (ends1[i] < pi->start && !pi->superend) {
 1023:       pi->start = NULL; /* not relocatable */
 1024:       pi->length = endlabel-symbols1[i];
 1025: #ifndef BURG_FORMAT
 1026:       debugp(stderr,"\n   non_reloc: K label before I label (length<0)\n");
 1027: #endif
 1028:       relocs--;
 1029:       nonrelocs++;
 1030:       continue;
 1031:     }
 1032:     assert(pi->length>=0);
 1033:     assert(pi->restlength >=0);
 1034:     while (j<(pi->length+pi->restlength)) {
 1035:       if (s1[j]==s3[j]) {
 1036: 	if (s1[j] != s2[j]) {
 1037: 	  pi->start = NULL; /* not relocatable */
 1038: #ifndef BURG_FORMAT
 1039: 	  debugp(stderr,"\n   non_reloc: engine1!=engine2 offset %3d",j);
 1040: #endif
 1041: 	  /* assert(j<prim_len); */
 1042: 	  relocs--;
 1043: 	  nonrelocs++;
 1044: 	  break;
 1045: 	}
 1046: 	j++;
 1047:       } else {
 1048: 	struct immarg *ia=&pi->immargs[pi->nimmargs];
 1049: 
 1050: 	pi->nimmargs++;
 1051: 	ia->offset=j;
 1052: 	if ((~*(Cell *)&(s1[j]))==*(Cell *)&(s3[j])) {
 1053: 	  ia->rel=0;
 1054: 	  debugp(stderr,"\n   absolute immarg: offset %3d",j);
 1055: 	} else if ((&(s1[j]))+(*(Cell *)&(s1[j]))+4 ==
 1056: 		   symbols1[DOESJUMP+1]) {
 1057: 	  ia->rel=1;
 1058: 	  debugp(stderr,"\n   relative immarg: offset %3d",j);
 1059: 	} else {
 1060: 	  pi->start = NULL; /* not relocatable */
 1061: #ifndef BURG_FORMAT
 1062: 	  debugp(stderr,"\n   non_reloc: engine1!=engine3 offset %3d",j);
 1063: #endif
 1064: 	  /* assert(j<prim_len);*/
 1065: 	  relocs--;
 1066: 	  nonrelocs++;
 1067: 	  break;
 1068: 	}
 1069: 	j+=4;
 1070:       }
 1071:     }
 1072:     debugp(stderr,"\n");
 1073:   }
 1074:   decomp_prims = calloc(i,sizeof(PrimInfo *));
 1075:   for (i=DOESJUMP+1; i<npriminfos; i++)
 1076:     decomp_prims[i] = &(priminfos[i]);
 1077:   qsort(decomp_prims+DOESJUMP+1, npriminfos-DOESJUMP-1, sizeof(PrimInfo *),
 1078: 	compare_priminfo_length);
 1079: #endif
 1080: }
 1081: 
 1082: static void flush_to_here(void)
 1083: {
 1084: #ifndef NO_DYNAMIC
 1085:   if (start_flush)
 1086:     FLUSH_ICACHE(start_flush, code_here-start_flush);
 1087:   start_flush=code_here;
 1088: #endif
 1089: }
 1090: 
 1091: static void MAYBE_UNUSED align_code(void)
 1092:      /* align code_here on some platforms */
 1093: {
 1094: #ifndef NO_DYNAMIC
 1095: #if defined(CODE_PADDING)
 1096:   Cell alignment = CODE_ALIGNMENT;
 1097:   static char nops[] = CODE_PADDING;
 1098:   UCell maxpadding=MAX_PADDING;
 1099:   UCell offset = ((UCell)code_here)&(alignment-1);
 1100:   UCell length = alignment-offset;
 1101:   if (length <= maxpadding) {
 1102:     memcpy(code_here,nops+offset,length);
 1103:     code_here += length;
 1104:   }
 1105: #endif /* defined(CODE_PADDING) */
 1106: #endif /* defined(NO_DYNAMIC */
 1107: }  
 1108: 
 1109: #ifndef NO_DYNAMIC
 1110: static void append_jump(void)
 1111: {
 1112:   if (last_jump) {
 1113:     PrimInfo *pi = &priminfos[last_jump];
 1114:     
 1115:     memcpy(code_here, pi->start+pi->length, pi->restlength);
 1116:     code_here += pi->restlength;
 1117:     memcpy(code_here, goto_start, goto_len);
 1118:     code_here += goto_len;
 1119:     align_code();
 1120:     last_jump=0;
 1121:   }
 1122: }
 1123: 
 1124: /* Gforth remembers all code blocks in this list.  On forgetting (by
 1125: executing a marker) the code blocks are not freed (because Gforth does
 1126: not remember how they were allocated; hmm, remembering that might be
 1127: easier and cleaner).  Instead, code_here etc. are reset to the old
 1128: value, and the "forgotten" code blocks are reused when they are
 1129: needed. */
 1130: 
 1131: struct code_block_list {
 1132:   struct code_block_list *next;
 1133:   Address block;
 1134:   Cell size;
 1135: } *code_block_list=NULL, **next_code_blockp=&code_block_list;
 1136: 
 1137: static Address append_prim(Cell p)
 1138: {
 1139:   PrimInfo *pi = &priminfos[p];
 1140:   Address old_code_here = code_here;
 1141: 
 1142:   if (code_area+code_area_size < code_here+pi->length+pi->restlength+goto_len+CODE_ALIGNMENT) {
 1143:     struct code_block_list *p;
 1144:     append_jump();
 1145:     flush_to_here();
 1146:     if (*next_code_blockp == NULL) {
 1147:       code_here = start_flush = code_area = gforth_alloc(code_area_size);
 1148:       p = (struct code_block_list *)malloc(sizeof(struct code_block_list));
 1149:       *next_code_blockp = p;
 1150:       p->next = NULL;
 1151:       p->block = code_here;
 1152:       p->size = code_area_size;
 1153:     } else {
 1154:       p = *next_code_blockp;
 1155:       code_here = start_flush = code_area = p->block;
 1156:     }
 1157:     old_code_here = code_here;
 1158:     next_code_blockp = &(p->next);
 1159:   }
 1160:   memcpy(code_here, pi->start, pi->length);
 1161:   code_here += pi->length;
 1162:   return old_code_here;
 1163: }
 1164: #endif
 1165: 
 1166: int forget_dyncode(Address code)
 1167: {
 1168: #ifdef NO_DYNAMIC
 1169:   return -1;
 1170: #else
 1171:   struct code_block_list *p, **pp;
 1172: 
 1173:   for (pp=&code_block_list, p=*pp; p!=NULL; pp=&(p->next), p=*pp) {
 1174:     if (code >= p->block && code < p->block+p->size) {
 1175:       next_code_blockp = &(p->next);
 1176:       code_here = start_flush = code;
 1177:       code_area = p->block;
 1178:       last_jump = 0;
 1179:       return -1;
 1180:     }
 1181:   }
 1182:   return -no_dynamic;
 1183: #endif /* !defined(NO_DYNAMIC) */
 1184: }
 1185: 
 1186: static long dyncodesize(void)
 1187: {
 1188: #ifndef NO_DYNAMIC
 1189:   struct code_block_list *p;
 1190:   long size=0;
 1191:   for (p=code_block_list; p!=NULL; p=p->next) {
 1192:     if (code_here >= p->block && code_here < p->block+p->size)
 1193:       return size + (code_here - p->block);
 1194:     else
 1195:       size += p->size;
 1196:   }
 1197: #endif /* !defined(NO_DYNAMIC) */
 1198:   return 0;
 1199: }
 1200: 
 1201: Label decompile_code(Label _code)
 1202: {
 1203: #ifdef NO_DYNAMIC
 1204:   return _code;
 1205: #else /* !defined(NO_DYNAMIC) */
 1206:   Cell i;
 1207:   struct code_block_list *p;
 1208:   Address code=_code;
 1209: 
 1210:   /* first, check if we are in code at all */
 1211:   for (p = code_block_list;; p = p->next) {
 1212:     if (p == NULL)
 1213:       return code;
 1214:     if (code >= p->block && code < p->block+p->size)
 1215:       break;
 1216:   }
 1217:   /* reverse order because NOOP might match other prims */
 1218:   for (i=npriminfos-1; i>DOESJUMP; i--) {
 1219:     PrimInfo *pi=decomp_prims[i];
 1220:     if (pi->start==code || (pi->start && memcmp(code,pi->start,pi->length)==0))
 1221:       return vm_prims[super2[super_costs[pi-priminfos].offset]];
 1222:     /* return pi->start;*/
 1223:   }
 1224:   return code;
 1225: #endif /* !defined(NO_DYNAMIC) */
 1226: }
 1227: 
 1228: #ifdef NO_IP
 1229: int nbranchinfos=0;
 1230: 
 1231: struct branchinfo {
 1232:   Label **targetpp; /* **(bi->targetpp) is the target */
 1233:   Cell *addressptr; /* store the target here */
 1234: } branchinfos[100000];
 1235: 
 1236: int ndoesexecinfos=0;
 1237: struct doesexecinfo {
 1238:   int branchinfo; /* fix the targetptr of branchinfos[...->branchinfo] */
 1239:   Label *targetp; /*target for branch (because this is not in threaded code)*/
 1240:   Cell *xt; /* cfa of word whose does-code needs calling */
 1241: } doesexecinfos[10000];
 1242: 
 1243: static void set_rel_target(Cell *source, Label target)
 1244: {
 1245:   *source = ((Cell)target)-(((Cell)source)+4);
 1246: }
 1247: 
 1248: static void register_branchinfo(Label source, Cell *targetpp)
 1249: {
 1250:   struct branchinfo *bi = &(branchinfos[nbranchinfos]);
 1251:   bi->targetpp = (Label **)targetpp;
 1252:   bi->addressptr = (Cell *)source;
 1253:   nbranchinfos++;
 1254: }
 1255: 
 1256: static Address compile_prim1arg(PrimNum p, Cell **argp)
 1257: {
 1258:   Address old_code_here=append_prim(p);
 1259: 
 1260:   assert(vm_prims[p]==priminfos[p].start);
 1261:   *argp = (Cell*)(old_code_here+priminfos[p].immargs[0].offset);
 1262:   return old_code_here;
 1263: }
 1264: 
 1265: static Address compile_call2(Cell *targetpp, Cell **next_code_targetp)
 1266: {
 1267:   PrimInfo *pi = &priminfos[N_call2];
 1268:   Address old_code_here = append_prim(N_call2);
 1269: 
 1270:   *next_code_targetp = (Cell *)(old_code_here + pi->immargs[0].offset);
 1271:   register_branchinfo(old_code_here + pi->immargs[1].offset, targetpp);
 1272:   return old_code_here;
 1273: }
 1274: #endif
 1275: 
 1276: void finish_code(void)
 1277: {
 1278: #ifdef NO_IP
 1279:   Cell i;
 1280: 
 1281:   compile_prim1(NULL);
 1282:   for (i=0; i<ndoesexecinfos; i++) {
 1283:     struct doesexecinfo *dei = &doesexecinfos[i];
 1284:     dei->targetp = (Label *)DOES_CODE1((dei->xt));
 1285:     branchinfos[dei->branchinfo].targetpp = &(dei->targetp);
 1286:   }
 1287:   ndoesexecinfos = 0;
 1288:   for (i=0; i<nbranchinfos; i++) {
 1289:     struct branchinfo *bi=&branchinfos[i];
 1290:     set_rel_target(bi->addressptr, **(bi->targetpp));
 1291:   }
 1292:   nbranchinfos = 0;
 1293: #else
 1294:   compile_prim1(NULL);
 1295: #endif
 1296:   flush_to_here();
 1297: }
 1298: 
 1299: #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
 1300: #ifdef NO_IP
 1301: static Cell compile_prim_dyn(PrimNum p, Cell *tcp)
 1302:      /* compile prim #p dynamically (mod flags etc.) and return start
 1303: 	address of generated code for putting it into the threaded
 1304: 	code. This function is only called if all the associated
 1305: 	inline arguments of p are already in place (at tcp[1] etc.) */
 1306: {
 1307:   PrimInfo *pi=&priminfos[p];
 1308:   Cell *next_code_target=NULL;
 1309:   Address codeaddr;
 1310:   Address primstart;
 1311:   
 1312:   assert(p<npriminfos);
 1313:   if (p==N_execute || p==N_perform || p==N_lit_perform) {
 1314:     codeaddr = compile_prim1arg(N_set_next_code, &next_code_target);
 1315:     primstart = append_prim(p);
 1316:     goto other_prim;
 1317:   } else if (p==N_call) {
 1318:     codeaddr = compile_call2(tcp+1, &next_code_target);
 1319:   } else if (p==N_does_exec) {
 1320:     struct doesexecinfo *dei = &doesexecinfos[ndoesexecinfos++];
 1321:     Cell *arg;
 1322:     codeaddr = compile_prim1arg(N_lit,&arg);
 1323:     *arg = (Cell)PFA(tcp[1]);
 1324:     /* we cannot determine the callee now (last_start[1] may be a
 1325:        forward reference), so just register an arbitrary target, and
 1326:        register in dei that we need to fix this before resolving
 1327:        branches */
 1328:     dei->branchinfo = nbranchinfos;
 1329:     dei->xt = (Cell *)(tcp[1]);
 1330:     compile_call2(0, &next_code_target);
 1331:   } else if (!is_relocatable(p)) {
 1332:     Cell *branch_target;
 1333:     codeaddr = compile_prim1arg(N_set_next_code, &next_code_target);
 1334:     compile_prim1arg(N_branch,&branch_target);
 1335:     set_rel_target(branch_target,vm_prims[p]);
 1336:   } else {
 1337:     unsigned j;
 1338: 
 1339:     codeaddr = primstart = append_prim(p);
 1340:   other_prim:
 1341:     for (j=0; j<pi->nimmargs; j++) {
 1342:       struct immarg *ia = &(pi->immargs[j]);
 1343:       Cell *argp = tcp + pi->nimmargs - j;
 1344:       Cell argval = *argp; /* !! specific to prims */
 1345:       if (ia->rel) { /* !! assumption: relative refs are branches */
 1346: 	register_branchinfo(primstart + ia->offset, argp);
 1347:       } else /* plain argument */
 1348: 	*(Cell *)(primstart + ia->offset) = argval;
 1349:     }
 1350:   }
 1351:   if (next_code_target!=NULL)
 1352:     *next_code_target = (Cell)code_here;
 1353:   return (Cell)codeaddr;
 1354: }
 1355: #else /* !defined(NO_IP) */
 1356: static Cell compile_prim_dyn(PrimNum p, Cell *tcp)
 1357:      /* compile prim #p dynamically (mod flags etc.) and return start
 1358:         address of generated code for putting it into the threaded code */
 1359: {
 1360:   Cell static_prim = (Cell)vm_prims[p];
 1361: #if defined(NO_DYNAMIC)
 1362:   return static_prim;
 1363: #else /* !defined(NO_DYNAMIC) */
 1364:   Address old_code_here;
 1365: 
 1366:   if (no_dynamic)
 1367:     return static_prim;
 1368:   if (p>=npriminfos || !is_relocatable(p)) {
 1369:     append_jump();
 1370:     return static_prim;
 1371:   }
 1372:   old_code_here = append_prim(p);
 1373:   last_jump = p;
 1374:   if (priminfos[p].superend)
 1375:     append_jump();
 1376:   return (Cell)old_code_here;
 1377: #endif  /* !defined(NO_DYNAMIC) */
 1378: }
 1379: #endif /* !defined(NO_IP) */
 1380: #endif
 1381: 
 1382: #ifndef NO_DYNAMIC
 1383: static int cost_codesize(int prim)
 1384: {
 1385:   return priminfos[prim].length;
 1386: }
 1387: #endif
 1388: 
 1389: static int cost_ls(int prim)
 1390: {
 1391:   struct cost *c = super_costs+prim;
 1392: 
 1393:   return c->loads + c->stores;
 1394: }
 1395: 
 1396: static int cost_lsu(int prim)
 1397: {
 1398:   struct cost *c = super_costs+prim;
 1399: 
 1400:   return c->loads + c->stores + c->updates;
 1401: }
 1402: 
 1403: static int cost_nexts(int prim)
 1404: {
 1405:   return 1;
 1406: }
 1407: 
 1408: typedef int Costfunc(int);
 1409: Costfunc *ss_cost =  /* cost function for optimize_bb */
 1410: #ifdef NO_DYNAMIC
 1411: cost_lsu;
 1412: #else
 1413: cost_codesize;
 1414: #endif
 1415: 
 1416: struct {
 1417:   Costfunc *costfunc;
 1418:   char *metricname;
 1419:   long sum;
 1420: } cost_sums[] = {
 1421: #ifndef NO_DYNAMIC
 1422:   { cost_codesize, "codesize", 0 },
 1423: #endif
 1424:   { cost_ls,       "ls",       0 },
 1425:   { cost_lsu,      "lsu",      0 },
 1426:   { cost_nexts,    "nexts",    0 }
 1427: };
 1428: 
 1429: #ifndef NO_DYNAMIC
 1430: void init_ss_cost(void) {
 1431:   if (no_dynamic && ss_cost == cost_codesize) {
 1432:     ss_cost = cost_nexts;
 1433:     cost_sums[0] = cost_sums[1]; /* don't use cost_codesize for print-metrics */
 1434:     debugp(stderr, "--no-dynamic conflicts with --ss-min-codesize, reverting to --ss-min-nexts\n");
 1435:   }
 1436: }
 1437: #endif
 1438: 
 1439: #define MAX_BB 128 /* maximum number of instructions in BB */
 1440: #define INF_COST 1000000 /* infinite cost */
 1441: #define CANONICAL_STATE 0
 1442: 
 1443: struct waypoint {
 1444:   int cost;     /* the cost from here to the end */
 1445:   PrimNum inst; /* the inst used from here to the next waypoint */
 1446:   char relocatable; /* the last non-transition was relocatable */
 1447:   char no_transition; /* don't use the next transition (relocatability)
 1448: 		       * or this transition (does not change state) */
 1449: };
 1450: 
 1451: struct tpa_state { /* tree parsing automaton (like) state */
 1452:   /* labeling is back-to-front */
 1453:   struct waypoint *inst;  /* in front of instruction */
 1454:   struct waypoint *trans; /* in front of instruction and transition */
 1455: }; 
 1456: 
 1457: struct tpa_state *termstate = NULL; /* initialized in loader() */
 1458: 
 1459: /* statistics about tree parsing (lazyburg) stuff */
 1460: long lb_basic_blocks = 0;
 1461: long lb_labeler_steps = 0;
 1462: long lb_labeler_automaton = 0;
 1463: long lb_labeler_dynprog = 0;
 1464: long lb_newstate_equiv = 0;
 1465: long lb_newstate_new = 0;
 1466: long lb_applicable_base_rules = 0;
 1467: long lb_applicable_chain_rules = 0;
 1468: 
 1469: #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
 1470: static void init_waypoints(struct waypoint ws[])
 1471: {
 1472:   int k;
 1473: 
 1474:   for (k=0; k<maxstates; k++)
 1475:     ws[k].cost=INF_COST;
 1476: }
 1477: 
 1478: static struct tpa_state *empty_tpa_state()
 1479: {
 1480:   struct tpa_state *s = malloc(sizeof(struct tpa_state));
 1481: 
 1482:   s->inst  = calloc(maxstates,sizeof(struct waypoint));
 1483:   init_waypoints(s->inst);
 1484:   s->trans = calloc(maxstates,sizeof(struct waypoint));
 1485:   /* init_waypoints(s->trans);*/
 1486:   return s;
 1487: }
 1488: 
 1489: static void transitions(struct tpa_state *t)
 1490: {
 1491:   int k;
 1492:   struct super_state *l;
 1493:   
 1494:   for (k=0; k<maxstates; k++) {
 1495:     t->trans[k] = t->inst[k];
 1496:     t->trans[k].no_transition = 1;
 1497:   }
 1498:   for (l = state_transitions; l != NULL; l = l->next) {
 1499:     PrimNum s = l->super;
 1500:     int jcost;
 1501:     struct cost *c=super_costs+s;
 1502:     struct waypoint *wi=&(t->trans[c->state_in]);
 1503:     struct waypoint *wo=&(t->inst[c->state_out]);
 1504:     lb_applicable_chain_rules++;
 1505:     if (wo->cost == INF_COST)
 1506:       continue;
 1507:     jcost = wo->cost + ss_cost(s);
 1508:     if (jcost <= wi->cost) {
 1509:       wi->cost = jcost;
 1510:       wi->inst = s;
 1511:       wi->relocatable = wo->relocatable;
 1512:       wi->no_transition = 0;
 1513:       /* if (ss_greedy) wi->cost = wo->cost ? */
 1514:     }
 1515:   }
 1516: }
 1517: 
 1518: static struct tpa_state *make_termstate()
 1519: {
 1520:   struct tpa_state *s = empty_tpa_state();
 1521: 
 1522:   s->inst[CANONICAL_STATE].cost = 0;
 1523:   transitions(s);
 1524:   return s;
 1525: }
 1526: #endif
 1527: 
 1528: #define TPA_SIZE 16384
 1529: 
 1530: struct tpa_entry {
 1531:   struct tpa_entry *next;
 1532:   PrimNum inst;
 1533:   struct tpa_state *state_behind;  /* note: brack-to-front labeling */
 1534:   struct tpa_state *state_infront; /* note: brack-to-front labeling */
 1535: } *tpa_table[TPA_SIZE];
 1536: 
 1537: #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
 1538: static Cell hash_tpa(PrimNum p, struct tpa_state *t)
 1539: {
 1540:   UCell it = (UCell )t;
 1541:   return (p+it+(it>>14))&(TPA_SIZE-1);
 1542: }
 1543: 
 1544: static struct tpa_state **lookup_tpa(PrimNum p, struct tpa_state *t2)
 1545: {
 1546:   int hash=hash_tpa(p, t2);
 1547:   struct tpa_entry *te = tpa_table[hash];
 1548: 
 1549:   if (tpa_noautomaton) {
 1550:     static struct tpa_state *t;
 1551:     t = NULL;
 1552:     return &t;
 1553:   }
 1554:   for (; te!=NULL; te = te->next) {
 1555:     if (p == te->inst && t2 == te->state_behind)
 1556:       return &(te->state_infront);
 1557:   }
 1558:   te = (struct tpa_entry *)malloc(sizeof(struct tpa_entry));
 1559:   te->next = tpa_table[hash];
 1560:   te->inst = p;
 1561:   te->state_behind = t2;
 1562:   te->state_infront = NULL;
 1563:   tpa_table[hash] = te;
 1564:   return &(te->state_infront);
 1565: }
 1566: 
 1567: static void tpa_state_normalize(struct tpa_state *t)
 1568: {
 1569:   /* normalize so cost of canonical state=0; this may result in
 1570:      negative states for some states */
 1571:   int d = t->inst[CANONICAL_STATE].cost;
 1572:   int i;
 1573: 
 1574:   for (i=0; i<maxstates; i++) {
 1575:     if (t->inst[i].cost != INF_COST)
 1576:       t->inst[i].cost -= d;
 1577:     if (t->trans[i].cost != INF_COST)
 1578:       t->trans[i].cost -= d;
 1579:   }
 1580: }
 1581: 
 1582: static int tpa_state_equivalent(struct tpa_state *t1, struct tpa_state *t2)
 1583: {
 1584:   return (memcmp(t1->inst, t2->inst, maxstates*sizeof(struct waypoint)) == 0 &&
 1585: 	  memcmp(t1->trans,t2->trans,maxstates*sizeof(struct waypoint)) == 0);
 1586: }
 1587: #endif
 1588: 
 1589: struct tpa_state_entry {
 1590:   struct tpa_state_entry *next;
 1591:   struct tpa_state *state;
 1592: } *tpa_state_table[TPA_SIZE];
 1593: 
 1594: #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
 1595: static Cell hash_tpa_state(struct tpa_state *t)
 1596: {
 1597:   int *ti = (int *)(t->inst);
 1598:   int *tt = (int *)(t->trans);
 1599:   int r=0;
 1600:   int i;
 1601: 
 1602:   for (i=0; ti+i < (int *)(t->inst+maxstates); i++)
 1603:     r += ti[i]+tt[i];
 1604:   return (r+(r>>14)+(r>>22)) & (TPA_SIZE-1);
 1605: }
 1606: 
 1607: static struct tpa_state *lookup_tpa_state(struct tpa_state *t)
 1608: {
 1609:   Cell hash = hash_tpa_state(t);
 1610:   struct tpa_state_entry *te = tpa_state_table[hash];
 1611:   struct tpa_state_entry *tn;
 1612: 
 1613:   if (!tpa_noequiv) {
 1614:     for (; te!=NULL; te = te->next) {
 1615:       if (tpa_state_equivalent(t, te->state)) {
 1616: 	lb_newstate_equiv++;
 1617: 	free(t->inst);
 1618: 	free(t->trans);
 1619: 	free(t);
 1620: 	return te->state;
 1621:       }
 1622:     }
 1623:     tn = (struct tpa_state_entry *)malloc(sizeof(struct tpa_state_entry));
 1624:     tn->next = te;
 1625:     tn->state = t;
 1626:     tpa_state_table[hash] = tn;
 1627:   }
 1628:   lb_newstate_new++;
 1629:   if (tpa_trace)
 1630:     fprintf(stderr, "%ld %ld lb_states\n", lb_labeler_steps, lb_newstate_new);
 1631:   return t;
 1632: }
 1633: 
 1634: /* use dynamic programming to find the shortest paths within the basic
 1635:    block origs[0..ninsts-1] and rewrite the instructions pointed to by
 1636:    instps to use it */
 1637: static void optimize_rewrite(Cell *instps[], PrimNum origs[], int ninsts)
 1638: {
 1639:   int i,j;
 1640:   struct tpa_state *ts[ninsts+1];
 1641:   int nextdyn, nextstate, no_transition;
 1642:   
 1643:   lb_basic_blocks++;
 1644:   ts[ninsts] = termstate;
 1645: #ifndef NO_DYNAMIC
 1646:   if (print_sequences) {
 1647:     for (i=0; i<ninsts; i++)
 1648: #if defined(BURG_FORMAT)
 1649:       fprintf(stderr, "op%d ", super_costs[origs[i]].offset);
 1650: #else
 1651:       fprintf(stderr, "%s ", prim_names[origs[i]]);
 1652: #endif
 1653:     fprintf(stderr, "\n");
 1654:   }
 1655: #endif
 1656:   for (i=ninsts-1; i>=0; i--) {
 1657:     struct tpa_state **tp = lookup_tpa(origs[i],ts[i+1]);
 1658:     struct tpa_state *t = *tp;
 1659:     lb_labeler_steps++;
 1660:     if (t) {
 1661:       ts[i] = t;
 1662:       lb_labeler_automaton++;
 1663:     }
 1664:     else {
 1665:       lb_labeler_dynprog++;
 1666:       ts[i] = empty_tpa_state();
 1667:       for (j=1; j<=max_super && i+j<=ninsts; j++) {
 1668: 	struct super_state **superp = lookup_super(origs+i, j);
 1669: 	if (superp!=NULL) {
 1670: 	  struct super_state *supers = *superp;
 1671: 	  for (; supers!=NULL; supers = supers->next) {
 1672: 	    PrimNum s = supers->super;
 1673: 	    int jcost;
 1674: 	    struct cost *c=super_costs+s;
 1675: 	    struct waypoint *wi=&(ts[i]->inst[c->state_in]);
 1676: 	    struct waypoint *wo=&(ts[i+j]->trans[c->state_out]);
 1677: 	    int no_transition = wo->no_transition;
 1678: 	    lb_applicable_base_rules++;
 1679: 	    if (!(is_relocatable(s)) && !wo->relocatable) {
 1680: 	      wo=&(ts[i+j]->inst[c->state_out]);
 1681: 	      no_transition=1;
 1682: 	    }
 1683: 	    if (wo->cost == INF_COST) 
 1684: 	      continue;
 1685: 	    jcost = wo->cost + ss_cost(s);
 1686: 	    if (jcost <= wi->cost) {
 1687: 	      wi->cost = jcost;
 1688: 	      wi->inst = s;
 1689: 	      wi->relocatable = is_relocatable(s);
 1690: 	      wi->no_transition = no_transition;
 1691: 	      /* if (ss_greedy) wi->cost = wo->cost ? */
 1692: 	    }
 1693: 	  }
 1694: 	}
 1695:       }
 1696:       transitions(ts[i]);
 1697:       tpa_state_normalize(ts[i]);
 1698:       *tp = ts[i] = lookup_tpa_state(ts[i]);
 1699:       if (tpa_trace)
 1700: 	fprintf(stderr, "%ld %ld lb_table_entries\n", lb_labeler_steps, lb_labeler_dynprog);
 1701:     }
 1702:   }
 1703:   /* now rewrite the instructions */
 1704:   nextdyn=0;
 1705:   nextstate=CANONICAL_STATE;
 1706:   no_transition = ((!ts[0]->trans[nextstate].relocatable) 
 1707: 		   ||ts[0]->trans[nextstate].no_transition);
 1708:   for (i=0; i<ninsts; i++) {
 1709:     Cell tc=0, tc2;
 1710:     if (i==nextdyn) {
 1711:       if (!no_transition) {
 1712: 	/* process trans */
 1713: 	PrimNum p = ts[i]->trans[nextstate].inst;
 1714: 	struct cost *c = super_costs+p;
 1715: 	assert(ts[i]->trans[nextstate].cost != INF_COST);
 1716: 	assert(c->state_in==nextstate);
 1717: 	tc = compile_prim_dyn(p,NULL);
 1718: 	nextstate = c->state_out;
 1719:       }
 1720:       {
 1721: 	/* process inst */
 1722: 	PrimNum p = ts[i]->inst[nextstate].inst;
 1723: 	struct cost *c=super_costs+p;
 1724: 	assert(c->state_in==nextstate);
 1725: 	assert(ts[i]->inst[nextstate].cost != INF_COST);
 1726: #if defined(GFORTH_DEBUGGING)
 1727: 	assert(p == origs[i]);
 1728: #endif
 1729: 	tc2 = compile_prim_dyn(p,instps[i]);
 1730: 	if (no_transition || !is_relocatable(p))
 1731: 	  /* !! actually what we care about is if and where
 1732: 	   * compile_prim_dyn() puts NEXTs */
 1733: 	  tc=tc2;
 1734: 	no_transition = ts[i]->inst[nextstate].no_transition;
 1735: 	nextstate = c->state_out;
 1736: 	nextdyn += c->length;
 1737:       }
 1738:     } else {
 1739: #if defined(GFORTH_DEBUGGING)
 1740:       assert(0);
 1741: #endif
 1742:       tc=0;
 1743:       /* tc= (Cell)vm_prims[ts[i]->inst[CANONICAL_STATE].inst]; */
 1744:     }
 1745:     *(instps[i]) = tc;
 1746:   }      
 1747:   if (!no_transition) {
 1748:     PrimNum p = ts[i]->trans[nextstate].inst;
 1749:     struct cost *c = super_costs+p;
 1750:     assert(c->state_in==nextstate);
 1751:     assert(ts[i]->trans[nextstate].cost != INF_COST);
 1752:     assert(i==nextdyn);
 1753:     (void)compile_prim_dyn(p,NULL);
 1754:     nextstate = c->state_out;
 1755:   }
 1756:   assert(nextstate==CANONICAL_STATE);
 1757: }
 1758: #endif
 1759: 
 1760: /* compile *start, possibly rewriting it into a static and/or dynamic
 1761:    superinstruction */
 1762: void compile_prim1(Cell *start)
 1763: {
 1764: #if defined(DOUBLY_INDIRECT)
 1765:   Label prim;
 1766: 
 1767:   if (start==NULL)
 1768:     return;
 1769:   prim = (Label)*start;
 1770:   if (prim<((Label)(xts+DOESJUMP)) || prim>((Label)(xts+npriminfos))) {
 1771:     fprintf(stderr,"compile_prim encountered xt %p\n", prim);
 1772:     *start=(Cell)prim;
 1773:     return;
 1774:   } else {
 1775:     *start = (Cell)(prim-((Label)xts)+((Label)vm_prims));
 1776:     return;
 1777:   }
 1778: #elif defined(INDIRECT_THREADED)
 1779:   return;
 1780: #else /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
 1781:   /* !! does not work, for unknown reasons; but something like this is
 1782:      probably needed to ensure that we don't call compile_prim_dyn
 1783:      before the inline arguments are there */
 1784:   static Cell *instps[MAX_BB];
 1785:   static PrimNum origs[MAX_BB];
 1786:   static int ninsts=0;
 1787:   PrimNum prim_num;
 1788: 
 1789:   if (start==NULL || ninsts >= MAX_BB ||
 1790:       (ninsts>0 && superend[origs[ninsts-1]])) {
 1791:     /* after bb, or at the start of the next bb */
 1792:     optimize_rewrite(instps,origs,ninsts);
 1793:     /* fprintf(stderr,"optimize_rewrite(...,%d)\n",ninsts); */
 1794:     ninsts=0;
 1795:     if (start==NULL) {
 1796:       align_code();
 1797:       return;
 1798:     }
 1799:   }
 1800:   prim_num = ((Xt)*start)-vm_prims;
 1801:   if(prim_num >= npriminfos) {
 1802:     optimize_rewrite(instps,origs,ninsts);
 1803:     /* fprintf(stderr,"optimize_rewrite(...,%d)\n",ninsts);*/
 1804:     ninsts=0;
 1805:     return;
 1806:   }    
 1807:   assert(ninsts<MAX_BB);
 1808:   instps[ninsts] = start;
 1809:   origs[ninsts] = prim_num;
 1810:   ninsts++;
 1811: #endif /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
 1812: }
 1813: 
 1814: #ifndef STANDALONE
 1815: Address gforth_loader(FILE *imagefile, char* filename)
 1816: /* returns the address of the image proper (after the preamble) */
 1817: {
 1818:   ImageHeader header;
 1819:   Address image;
 1820:   Address imp; /* image+preamble */
 1821:   Char magic[8];
 1822:   char magic7; /* size byte of magic number */
 1823:   Cell preamblesize=0;
 1824:   Cell data_offset = offset_image ? 56*sizeof(Cell) : 0;
 1825:   UCell check_sum;
 1826:   Cell ausize = ((RELINFOBITS ==  8) ? 0 :
 1827: 		 (RELINFOBITS == 16) ? 1 :
 1828: 		 (RELINFOBITS == 32) ? 2 : 3);
 1829:   Cell charsize = ((sizeof(Char) == 1) ? 0 :
 1830: 		   (sizeof(Char) == 2) ? 1 :
 1831: 		   (sizeof(Char) == 4) ? 2 : 3) + ausize;
 1832:   Cell cellsize = ((sizeof(Cell) == 1) ? 0 :
 1833: 		   (sizeof(Cell) == 2) ? 1 :
 1834: 		   (sizeof(Cell) == 4) ? 2 : 3) + ausize;
 1835:   Cell sizebyte = (ausize << 5) + (charsize << 3) + (cellsize << 1) +
 1836: #ifdef WORDS_BIGENDIAN
 1837:        0
 1838: #else
 1839:        1
 1840: #endif
 1841:     ;
 1842: 
 1843:   vm_prims = gforth_engine(0,0,0,0,0 sr_call);
 1844:   check_prims(vm_prims);
 1845:   prepare_super_table();
 1846: #ifndef DOUBLY_INDIRECT
 1847: #ifdef PRINT_SUPER_LENGTHS
 1848:   print_super_lengths();
 1849: #endif
 1850:   check_sum = checksum(vm_prims);
 1851: #else /* defined(DOUBLY_INDIRECT) */
 1852:   check_sum = (UCell)vm_prims;
 1853: #endif /* defined(DOUBLY_INDIRECT) */
 1854: #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
 1855:   termstate = make_termstate();
 1856: #endif /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
 1857:   
 1858:   do {
 1859:     if(fread(magic,sizeof(Char),8,imagefile) < 8) {
 1860:       fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.6) image.\n",
 1861: 	      progname, filename);
 1862:       exit(1);
 1863:     }
 1864:     preamblesize+=8;
 1865:   } while(memcmp(magic,"Gforth3",7));
 1866:   magic7 = magic[7];
 1867:   if (debug) {
 1868:     magic[7]='\0';
 1869:     fprintf(stderr,"Magic found: %s ", magic);
 1870:     print_sizes(magic7);
 1871:   }
 1872: 
 1873:   if (magic7 != sizebyte)
 1874:     {
 1875:       fprintf(stderr,"This image is:         ");
 1876:       print_sizes(magic7);
 1877:       fprintf(stderr,"whereas the machine is ");
 1878:       print_sizes(sizebyte);
 1879:       exit(-2);
 1880:     };
 1881: 
 1882:   fread((void *)&header,sizeof(ImageHeader),1,imagefile);
 1883: 
 1884:   set_stack_sizes(&header);
 1885:   
 1886: #if HAVE_GETPAGESIZE
 1887:   pagesize=getpagesize(); /* Linux/GNU libc offers this */
 1888: #elif HAVE_SYSCONF && defined(_SC_PAGESIZE)
 1889:   pagesize=sysconf(_SC_PAGESIZE); /* POSIX.4 */
 1890: #elif PAGESIZE
 1891:   pagesize=PAGESIZE; /* in limits.h according to Gallmeister's POSIX.4 book */
 1892: #endif
 1893:   debugp(stderr,"pagesize=%ld\n",(unsigned long) pagesize);
 1894: 
 1895:   image = dict_alloc_read(imagefile, preamblesize+header.image_size,
 1896: 			  preamblesize+dictsize, data_offset);
 1897:   imp=image+preamblesize;
 1898: 
 1899:   alloc_stacks((ImageHeader *)imp);
 1900:   if (clear_dictionary)
 1901:     memset(imp+header.image_size, 0, dictsize-header.image_size);
 1902:   if(header.base==0 || header.base  == (Address)0x100) {
 1903:     Cell reloc_size=((header.image_size-1)/sizeof(Cell))/8+1;
 1904:     Char reloc_bits[reloc_size];
 1905:     fseek(imagefile, preamblesize+header.image_size, SEEK_SET);
 1906:     fread(reloc_bits, 1, reloc_size, imagefile);
 1907:     gforth_relocate((Cell *)imp, reloc_bits, header.image_size, (Cell)header.base, vm_prims);
 1908: #if 0
 1909:     { /* let's see what the relocator did */
 1910:       FILE *snapshot=fopen("snapshot.fi","wb");
 1911:       fwrite(image,1,imagesize,snapshot);
 1912:       fclose(snapshot);
 1913:     }
 1914: #endif
 1915:   }
 1916:   else if(header.base!=imp) {
 1917:     fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\n",
 1918: 	    progname, (unsigned long)header.base, (unsigned long)imp);
 1919:     exit(1);
 1920:   }
 1921:   if (header.checksum==0)
 1922:     ((ImageHeader *)imp)->checksum=check_sum;
 1923:   else if (header.checksum != check_sum) {
 1924:     fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\n",
 1925: 	    progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
 1926:     exit(1);
 1927:   }
 1928: #ifdef DOUBLY_INDIRECT
 1929:   ((ImageHeader *)imp)->xt_base = xts;
 1930: #endif
 1931:   fclose(imagefile);
 1932: 
 1933:   /* unnecessary, except maybe for CODE words */
 1934:   /* FLUSH_ICACHE(imp, header.image_size);*/
 1935: 
 1936:   return imp;
 1937: }
 1938: #endif
 1939: 
 1940: /* pointer to last '/' or '\' in file, 0 if there is none. */
 1941: static char *onlypath(char *filename)
 1942: {
 1943:   return strrchr(filename, DIRSEP);
 1944: }
 1945: 
 1946: static FILE *openimage(char *fullfilename)
 1947: {
 1948:   FILE *image_file;
 1949:   char * expfilename = tilde_cstr((Char *)fullfilename, strlen(fullfilename), 1);
 1950: 
 1951:   image_file=fopen(expfilename,"rb");
 1952:   if (image_file!=NULL && debug)
 1953:     fprintf(stderr, "Opened image file: %s\n", expfilename);
 1954:   return image_file;
 1955: }
 1956: 
 1957: /* try to open image file concat(path[0:len],imagename) */
 1958: static FILE *checkimage(char *path, int len, char *imagename)
 1959: {
 1960:   int dirlen=len;
 1961:   char fullfilename[dirlen+strlen((char *)imagename)+2];
 1962: 
 1963:   memcpy(fullfilename, path, dirlen);
 1964:   if (fullfilename[dirlen-1]!=DIRSEP)
 1965:     fullfilename[dirlen++]=DIRSEP;
 1966:   strcpy(fullfilename+dirlen,imagename);
 1967:   return openimage(fullfilename);
 1968: }
 1969: 
 1970: static FILE * open_image_file(char * imagename, char * path)
 1971: {
 1972:   FILE * image_file=NULL;
 1973:   char *origpath=path;
 1974:   
 1975:   if(strchr(imagename, DIRSEP)==NULL) {
 1976:     /* first check the directory where the exe file is in !! 01may97jaw */
 1977:     if (onlypath(progname))
 1978:       image_file=checkimage(progname, onlypath(progname)-progname, imagename);
 1979:     if (!image_file)
 1980:       do {
 1981: 	char *pend=strchr(path, PATHSEP);
 1982: 	if (pend==NULL)
 1983: 	  pend=path+strlen(path);
 1984: 	if (strlen(path)==0) break;
 1985: 	image_file=checkimage(path, pend-path, imagename);
 1986: 	path=pend+(*pend==PATHSEP);
 1987:       } while (image_file==NULL);
 1988:   } else {
 1989:     image_file=openimage(imagename);
 1990:   }
 1991: 
 1992:   if (!image_file) {
 1993:     fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
 1994: 	    progname, imagename, origpath);
 1995:     exit(1);
 1996:   }
 1997: 
 1998:   return image_file;
 1999: }
 2000: #endif
 2001: 
 2002: #ifdef STANDALONE_ALLOC
 2003: Address gforth_alloc(Cell size)
 2004: {
 2005:   Address r;
 2006:   /* leave a little room (64B) for stack underflows */
 2007:   if ((r = malloc(size+64))==NULL) {
 2008:     perror(progname);
 2009:     exit(1);
 2010:   }
 2011:   r = (Address)((((Cell)r)+(sizeof(Float)-1))&(-sizeof(Float)));
 2012:   debugp(stderr, "malloc succeeds, address=$%lx\n", (long)r);
 2013:   return r;
 2014: }
 2015: #endif
 2016: 
 2017: #ifdef HAS_OS
 2018: static UCell convsize(char *s, UCell elemsize)
 2019: /* converts s of the format [0-9]+[bekMGT]? (e.g. 25k) into the number
 2020:    of bytes.  the letter at the end indicates the unit, where e stands
 2021:    for the element size. default is e */
 2022: {
 2023:   char *endp;
 2024:   UCell n,m;
 2025: 
 2026:   m = elemsize;
 2027:   n = strtoul(s,&endp,0);
 2028:   if (endp!=NULL) {
 2029:     if (strcmp(endp,"b")==0)
 2030:       m=1;
 2031:     else if (strcmp(endp,"k")==0)
 2032:       m=1024;
 2033:     else if (strcmp(endp,"M")==0)
 2034:       m=1024*1024;
 2035:     else if (strcmp(endp,"G")==0)
 2036:       m=1024*1024*1024;
 2037:     else if (strcmp(endp,"T")==0) {
 2038: #if (SIZEOF_CHAR_P > 4)
 2039:       m=1024L*1024*1024*1024;
 2040: #else
 2041:       fprintf(stderr,"%s: size specification \"%s\" too large for this machine\n", progname, endp);
 2042:       exit(1);
 2043: #endif
 2044:     } else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
 2045:       fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
 2046:       exit(1);
 2047:     }
 2048:   }
 2049:   return n*m;
 2050: }
 2051: 
 2052: enum {
 2053:   ss_number = 256,
 2054:   ss_states,
 2055:   ss_min_codesize,
 2056:   ss_min_ls,
 2057:   ss_min_lsu,
 2058:   ss_min_nexts,
 2059: };
 2060: 
 2061: #ifndef STANDALONE
 2062: void gforth_args(int argc, char ** argv, char ** path, char ** imagename)
 2063: {
 2064:   int c;
 2065: 
 2066:   opterr=0;
 2067:   while (1) {
 2068:     int option_index=0;
 2069:     static struct option opts[] = {
 2070:       {"appl-image", required_argument, NULL, 'a'},
 2071:       {"image-file", required_argument, NULL, 'i'},
 2072:       {"dictionary-size", required_argument, NULL, 'm'},
 2073:       {"data-stack-size", required_argument, NULL, 'd'},
 2074:       {"return-stack-size", required_argument, NULL, 'r'},
 2075:       {"fp-stack-size", required_argument, NULL, 'f'},
 2076:       {"locals-stack-size", required_argument, NULL, 'l'},
 2077:       {"vm-commit", no_argument, &map_noreserve, 0},
 2078:       {"path", required_argument, NULL, 'p'},
 2079:       {"version", no_argument, NULL, 'v'},
 2080:       {"help", no_argument, NULL, 'h'},
 2081:       /* put something != 0 into offset_image */
 2082:       {"offset-image", no_argument, &offset_image, 1},
 2083:       {"no-offset-im", no_argument, &offset_image, 0},
 2084:       {"clear-dictionary", no_argument, &clear_dictionary, 1},
 2085:       {"debug", no_argument, &debug, 1},
 2086:       {"diag", no_argument, &diag, 1},
 2087:       {"die-on-signal", no_argument, &die_on_signal, 1},
 2088:       {"ignore-async-signals", no_argument, &ignore_async_signals, 1},
 2089:       {"no-super", no_argument, &no_super, 1},
 2090:       {"no-dynamic", no_argument, &no_dynamic, 1},
 2091:       {"dynamic", no_argument, &no_dynamic, 0},
 2092:       {"print-metrics", no_argument, &print_metrics, 1},
 2093:       {"print-sequences", no_argument, &print_sequences, 1},
 2094:       {"ss-number", required_argument, NULL, ss_number},
 2095:       {"ss-states", required_argument, NULL, ss_states},
 2096: #ifndef NO_DYNAMIC
 2097:       {"ss-min-codesize", no_argument, NULL, ss_min_codesize},
 2098: #endif
 2099:       {"ss-min-ls",       no_argument, NULL, ss_min_ls},
 2100:       {"ss-min-lsu",      no_argument, NULL, ss_min_lsu},
 2101:       {"ss-min-nexts",    no_argument, NULL, ss_min_nexts},
 2102:       {"ss-greedy",       no_argument, &ss_greedy, 1},
 2103:       {"tpa-noequiv",     no_argument, &tpa_noequiv, 1},
 2104:       {"tpa-noautomaton", no_argument, &tpa_noautomaton, 1},
 2105:       {"tpa-trace",	  no_argument, &tpa_trace, 1},
 2106:       {0,0,0,0}
 2107:       /* no-init-file, no-rc? */
 2108:     };
 2109:     
 2110:     c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vhoncsx", opts, &option_index);
 2111:     
 2112:     switch (c) {
 2113:     case EOF: return;
 2114:     case '?': optind--; return;
 2115:     case 'a': *imagename = optarg; return;
 2116:     case 'i': *imagename = optarg; break;
 2117:     case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
 2118:     case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
 2119:     case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
 2120:     case 'f': fsize = convsize(optarg,sizeof(Float)); break;
 2121:     case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
 2122:     case 'p': *path = optarg; break;
 2123:     case 'o': offset_image = 1; break;
 2124:     case 'n': offset_image = 0; break;
 2125:     case 'c': clear_dictionary = 1; break;
 2126:     case 's': die_on_signal = 1; break;
 2127:     case 'x': debug = 1; break;
 2128:     case 'v': fputs(PACKAGE_STRING"\n", stderr); exit(0);
 2129:     case ss_number: static_super_number = atoi(optarg); break;
 2130:     case ss_states: maxstates = max(min(atoi(optarg),MAX_STATE),1); break;
 2131: #ifndef NO_DYNAMIC
 2132:     case ss_min_codesize: ss_cost = cost_codesize; break;
 2133: #endif
 2134:     case ss_min_ls:       ss_cost = cost_ls;       break;
 2135:     case ss_min_lsu:      ss_cost = cost_lsu;      break;
 2136:     case ss_min_nexts:    ss_cost = cost_nexts;    break;
 2137:     case 'h': 
 2138:       fprintf(stderr, "Usage: %s [engine options] ['--'] [image arguments]\n\
 2139: Engine Options:\n\
 2140:   --appl-image FILE		    Equivalent to '--image-file=FILE --'\n\
 2141:   --clear-dictionary		    Initialize the dictionary with 0 bytes\n\
 2142:   -d SIZE, --data-stack-size=SIZE   Specify data stack size\n\
 2143:   --debug			    Print debugging information during startup\n\
 2144:   --diag			    Print diagnostic information during startup\n\
 2145:   --die-on-signal		    Exit instead of THROWing some signals\n\
 2146:   --dynamic			    Use dynamic native code\n\
 2147:   -f SIZE, --fp-stack-size=SIZE	    Specify floating point stack size\n\
 2148:   -h, --help			    Print this message and exit\n\
 2149:   --ignore-async-signals	    Ignore instead of THROWing async. signals\n\
 2150:   -i FILE, --image-file=FILE	    Use image FILE instead of `gforth.fi'\n\
 2151:   -l SIZE, --locals-stack-size=SIZE Specify locals stack size\n\
 2152:   -m SIZE, --dictionary-size=SIZE   Specify Forth dictionary size\n\
 2153:   --no-dynamic			    Use only statically compiled primitives\n\
 2154:   --no-offset-im		    Load image at normal position\n\
 2155:   --no-super			    No dynamically formed superinstructions\n\
 2156:   --offset-image		    Load image at a different position\n\
 2157:   -p PATH, --path=PATH		    Search path for finding image and sources\n\
 2158:   --print-metrics		    Print some code generation metrics on exit\n\
 2159:   --print-sequences		    Print primitive sequences for optimization\n\
 2160:   -r SIZE, --return-stack-size=SIZE Specify return stack size\n\
 2161:   --ss-greedy			    Greedy, not optimal superinst selection\n\
 2162:   --ss-min-codesize		    Select superinsts for smallest native code\n\
 2163:   --ss-min-ls			    Minimize loads and stores\n\
 2164:   --ss-min-lsu			    Minimize loads, stores, and pointer updates\n\
 2165:   --ss-min-nexts		    Minimize the number of static superinsts\n\
 2166:   --ss-number=N			    Use N static superinsts (default max)\n\
 2167:   --ss-states=N			    N states for stack caching (default max)\n\
 2168:   --tpa-noequiv			    Automaton without state equivalence\n\
 2169:   --tpa-noautomaton		    Dynamic programming only\n\
 2170:   --tpa-trace			    Report new states etc.\n\
 2171:   -v, --version			    Print engine version and exit\n\
 2172:   --vm-commit			    Use OS default for memory overcommit\n\
 2173: SIZE arguments consist of an integer followed by a unit. The unit can be\n\
 2174:   `b' (byte), `e' (element; default), `k' (KB), `M' (MB), `G' (GB) or `T' (TB).\n",
 2175: 	      argv[0]);
 2176:       optind--;
 2177:       return;
 2178:     }
 2179:   }
 2180: }
 2181: #endif
 2182: #endif
 2183: 
 2184: static void print_diag()
 2185: {
 2186: 
 2187: #if !defined(HAVE_GETRUSAGE)
 2188:   fprintf(stderr, "*** missing functionality ***\n"
 2189: #ifndef HAVE_GETRUSAGE
 2190: 	  "    no getrusage -> CPUTIME broken\n"
 2191: #endif
 2192: 	  );
 2193: #endif
 2194:   if((relocs < nonrelocs) ||
 2195: #if defined(BUGGY_LL_CMP) || defined(BUGGY_LL_MUL) || defined(BUGGY_LL_DIV) || defined(BUGGY_LL_ADD) || defined(BUGGY_LL_SHIFT) || defined(BUGGY_LL_D2F) || defined(BUGGY_LL_F2D)
 2196:      1
 2197: #else
 2198:      0
 2199: #endif
 2200:      )
 2201:     debugp(stderr, "relocs: %d:%d\n", relocs, nonrelocs);
 2202:     fprintf(stderr, "*** %sperformance problems ***\n%s%s",
 2203: #if defined(BUGGY_LL_CMP) || defined(BUGGY_LL_MUL) || defined(BUGGY_LL_DIV) || defined(BUGGY_LL_ADD) || defined(BUGGY_LL_SHIFT) || defined(BUGGY_LL_D2F) || defined(BUGGY_LL_F2D) || !(defined(FORCE_REG) || defined(FORCE_REG_UNNECESSARY)) || defined(BUGGY_LONG_LONG)
 2204: 	    "",
 2205: #else
 2206: 	    "no ",
 2207: #endif
 2208: #if defined(BUGGY_LL_CMP) || defined(BUGGY_LL_MUL) || defined(BUGGY_LL_DIV) || defined(BUGGY_LL_ADD) || defined(BUGGY_LL_SHIFT) || defined(BUGGY_LL_D2F) || defined(BUGGY_LL_F2D)
 2209: 	    "    double-cell integer type buggy ->\n        "
 2210: #ifdef BUGGY_LL_CMP
 2211: 	    "CMP, "
 2212: #endif
 2213: #ifdef BUGGY_LL_MUL
 2214: 	    "MUL, "
 2215: #endif
 2216: #ifdef BUGGY_LL_DIV
 2217: 	    "DIV, "
 2218: #endif
 2219: #ifdef BUGGY_LL_ADD
 2220: 	    "ADD, "
 2221: #endif
 2222: #ifdef BUGGY_LL_SHIFT
 2223: 	    "SHIFT, "
 2224: #endif
 2225: #ifdef BUGGY_LL_D2F
 2226: 	    "D2F, "
 2227: #endif
 2228: #ifdef BUGGY_LL_F2D
 2229: 	    "F2D, "
 2230: #endif
 2231: 	    "\b\b slow\n"
 2232: #endif
 2233: #if !(defined(FORCE_REG) || defined(FORCE_REG_UNNECESSARY))
 2234: 	    "    automatic register allocation: performance degradation possible\n"
 2235: #endif
 2236: 	    "",
 2237: 	    (relocs < nonrelocs) ? "no dynamic code generation (--debug for details) -> factor 2 slowdown\n" : "");
 2238: }
 2239: 
 2240: #ifdef STANDALONE
 2241: Cell data_abort_pc;
 2242: 
 2243: void data_abort_C(void)
 2244: {
 2245:   while(1) {
 2246:   }
 2247: }
 2248: #endif
 2249: 
 2250: int main(int argc, char **argv, char **env)
 2251: {
 2252: #ifdef HAS_OS
 2253:   char *path = getenv("GFORTHPATH") ? : DEFAULTPATH;
 2254: #else
 2255:   char *path = DEFAULTPATH;
 2256: #endif
 2257: #ifndef INCLUDE_IMAGE
 2258:   char *imagename="gforth.fi";
 2259:   FILE *image_file;
 2260:   Address image;
 2261: #endif
 2262:   int retvalue;
 2263: 	  
 2264: #ifndef STANDALONE
 2265:   /* buffering of the user output device */
 2266: #ifdef _IONBF
 2267:   if (isatty(fileno(stdout))) {
 2268:     fflush(stdout);
 2269:     setvbuf(stdout,NULL,_IONBF,0);
 2270:   }
 2271: #endif
 2272: #else
 2273:   prep_terminal();
 2274: #endif
 2275: 
 2276:   progname = argv[0];
 2277: 
 2278: #ifndef STANDALONE
 2279:   if (lt_dlinit()!=0) {
 2280:     fprintf(stderr,"%s: lt_dlinit failed", progname);
 2281:     exit(1);
 2282:   }
 2283:     
 2284: #ifdef HAS_OS
 2285:   gforth_args(argc, argv, &path, &imagename);
 2286: #ifndef NO_DYNAMIC
 2287:   init_ss_cost();
 2288: #endif /* !defined(NO_DYNAMIC) */
 2289: #endif /* defined(HAS_OS) */
 2290: #endif
 2291: 
 2292: #ifdef STANDALONE
 2293:   image = gforth_engine(0, 0, 0, 0, 0 sr_call);
 2294:   alloc_stacks((ImageHeader *)image);
 2295: #else
 2296:   image_file = open_image_file(imagename, path);
 2297:   image = gforth_loader(image_file, imagename);
 2298: #endif
 2299:   gforth_header=(ImageHeader *)image; /* used in SIGSEGV handler */
 2300: 
 2301:   if (diag)
 2302:     print_diag();
 2303:   {
 2304:     char path2[strlen(path)+1];
 2305:     char *p1, *p2;
 2306:     Cell environ[]= {
 2307:       (Cell)argc-(optind-1),
 2308:       (Cell)(argv+(optind-1)),
 2309:       (Cell)strlen(path),
 2310:       (Cell)path2};
 2311:     argv[optind-1] = progname;
 2312:     /*
 2313:        for (i=0; i<environ[0]; i++)
 2314:        printf("%s\n", ((char **)(environ[1]))[i]);
 2315:        */
 2316:     /* make path OS-independent by replacing path separators with NUL */
 2317:     for (p1=path, p2=path2; *p1!='\0'; p1++, p2++)
 2318:       if (*p1==PATHSEP)
 2319: 	*p2 = '\0';
 2320:       else
 2321: 	*p2 = *p1;
 2322:     *p2='\0';
 2323:     retvalue = gforth_go(image, 4, environ);
 2324: #if defined(SIGPIPE) && !defined(STANDALONE)
 2325:     bsd_signal(SIGPIPE, SIG_IGN);
 2326: #endif
 2327: #ifdef VM_PROFILING
 2328:     vm_print_profile(stderr);
 2329: #endif
 2330:     deprep_terminal();
 2331: #ifndef STANDALONE
 2332:     if (lt_dlexit()!=0)
 2333:       fprintf(stderr,"%s: lt_dlexit failed", progname);
 2334: #endif
 2335:   }
 2336:   if (print_metrics) {
 2337:     int i;
 2338:     fprintf(stderr, "code size = %8ld\n", dyncodesize());
 2339: #ifndef STANDALONE
 2340:     for (i=0; i<sizeof(cost_sums)/sizeof(cost_sums[0]); i++)
 2341:       fprintf(stderr, "metric %8s: %8ld\n",
 2342: 	      cost_sums[i].metricname, cost_sums[i].sum);
 2343: #endif
 2344:     fprintf(stderr,"lb_basic_blocks = %ld\n", lb_basic_blocks);
 2345:     fprintf(stderr,"lb_labeler_steps = %ld\n", lb_labeler_steps);
 2346:     fprintf(stderr,"lb_labeler_automaton = %ld\n", lb_labeler_automaton);
 2347:     fprintf(stderr,"lb_labeler_dynprog = %ld\n", lb_labeler_dynprog);
 2348:     fprintf(stderr,"lb_newstate_equiv = %ld\n", lb_newstate_equiv);
 2349:     fprintf(stderr,"lb_newstate_new = %ld\n", lb_newstate_new);
 2350:     fprintf(stderr,"lb_applicable_base_rules = %ld\n", lb_applicable_base_rules);
 2351:     fprintf(stderr,"lb_applicable_chain_rules = %ld\n", lb_applicable_chain_rules);
 2352:   }
 2353:   if (tpa_trace) {
 2354:     fprintf(stderr, "%ld %ld lb_states\n", lb_labeler_steps, lb_newstate_new);
 2355:     fprintf(stderr, "%ld %ld lb_table_entries\n", lb_labeler_steps, lb_labeler_dynprog);
 2356:   }
 2357:   return retvalue;
 2358: }

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