File:  [gforth] / gforth / engine / main.c
Revision 1.230: download - view: text, annotated - select for diffs
Tue Apr 20 19:27:12 2010 UTC (14 years ago) by anton
Branches: MAIN
CVS tags: HEAD
prepare for removing DOESJUMP: assert(0) when it occurs

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

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