File:  [gforth] / gforth / engine / main.c
Revision 1.264: download - view: text, annotated - select for diffs
Fri Oct 5 23:48:27 2012 UTC (11 years, 5 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Generate image for Android Gforth

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

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