File:  [gforth] / gforth / engine / main.c
Revision 1.205: download - view: text, annotated - select for diffs
Mon Apr 28 08:42:59 2008 UTC (15 years, 11 months ago) by anton
Branches: MAIN
CVS tags: HEAD
added CLEAR-LIB ADD-LIB (libcc.fs), documented them,
  and used them in libffi.fs and fflib.fs
OPEN-LIB no longer guesses library extensions (lt_dladvise_ext())

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

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