Annotation of gforth/engine/main.c, revision 1.194

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

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