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

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.102     anton      37: #include <signal.h>
1.11      pazsan     38: #ifndef STANDALONE
1.1       anton      39: #if HAVE_SYS_MMAN_H
                     40: #include <sys/mman.h>
                     41: #endif
1.11      pazsan     42: #endif
1.1       anton      43: #include "io.h"
                     44: #include "getopt.h"
1.11      pazsan     45: #ifdef STANDALONE
1.174     pazsan     46: /* #include <systypes.h> */
1.11      pazsan     47: #endif
1.1       anton      48: 
1.190     anton      49: /* output rules etc. for burg with --debug and --print-sequences */
                     50: /* #define BURG_FORMAT*/
                     51: 
1.121     anton      52: typedef enum prim_num {
1.119     anton      53: /* definitions of N_execute etc. */
1.126     anton      54: #include PRIM_NUM_I
1.119     anton      55:   N_START_SUPER
1.121     anton      56: } PrimNum;
1.119     anton      57: 
1.79      anton      58: /* global variables for engine.c 
                     59:    We put them here because engine.c is compiled several times in
                     60:    different ways for the same engine. */
1.161     pazsan     61: Cell *gforth_SP;
                     62: Float *gforth_FP;
                     63: Address gforth_UP=NULL;
1.79      anton      64: 
1.115     pazsan     65: #ifdef HAS_FFCALL
1.161     pazsan     66: Cell *gforth_RP;
                     67: Address gforth_LP;
1.115     pazsan     68: 
                     69: #include <callback.h>
                     70: 
1.161     pazsan     71: va_alist gforth_clist;
1.115     pazsan     72: 
1.161     pazsan     73: void gforth_callback(Xt* fcall, void * alist)
1.115     pazsan     74: {
1.140     pazsan     75:   /* save global valiables */
1.161     pazsan     76:   Cell *rp = gforth_RP;
                     77:   Cell *sp = gforth_SP;
                     78:   Float *fp = gforth_FP;
                     79:   Address lp = gforth_LP;
1.168     pazsan     80:   va_alist clist = gforth_clist;
1.140     pazsan     81: 
1.161     pazsan     82:   gforth_clist = (va_alist)alist;
1.140     pazsan     83: 
1.161     pazsan     84:   gforth_engine(fcall, sp, rp, fp, lp);
1.140     pazsan     85: 
                     86:   /* restore global variables */
1.161     pazsan     87:   gforth_RP = rp;
                     88:   gforth_SP = sp;
                     89:   gforth_FP = fp;
                     90:   gforth_LP = lp;
1.168     pazsan     91:   gforth_clist = clist;
1.115     pazsan     92: }
                     93: #endif
                     94: 
1.153     pazsan     95: #ifdef HAS_LIBFFI
1.161     pazsan     96: Cell *gforth_RP;
                     97: Address gforth_LP;
1.153     pazsan     98: 
                     99: #include <ffi.h>
                    100: 
1.164     pazsan    101: void ** gforth_clist;
                    102: void * gforth_ritem;
1.153     pazsan    103: 
1.162     pazsan    104: void gforth_callback(ffi_cif * cif, void * resp, void ** args, void * ip)
1.153     pazsan    105: {
1.161     pazsan    106:   Cell *rp = gforth_RP;
                    107:   Cell *sp = gforth_SP;
                    108:   Float *fp = gforth_FP;
                    109:   Address lp = gforth_LP;
1.168     pazsan    110:   void ** clist = gforth_clist;
                    111:   void * ritem = gforth_ritem;
1.153     pazsan    112: 
1.164     pazsan    113:   gforth_clist = args;
                    114:   gforth_ritem = resp;
1.153     pazsan    115: 
1.164     pazsan    116:   gforth_engine((Xt *)ip, sp, rp, fp, lp);
1.153     pazsan    117: 
                    118:   /* restore global variables */
1.161     pazsan    119:   gforth_RP = rp;
                    120:   gforth_SP = sp;
                    121:   gforth_FP = fp;
                    122:   gforth_LP = lp;
1.168     pazsan    123:   gforth_clist = clist;
                    124:   gforth_ritem = ritem;
1.153     pazsan    125: }
                    126: #endif
                    127: 
1.79      anton     128: #ifdef GFORTH_DEBUGGING
                    129: /* define some VM registers as global variables, so they survive exceptions;
                    130:    global register variables are not up to the task (according to the 
                    131:    GNU C manual) */
                    132: Xt *saved_ip;
                    133: Cell *rp;
                    134: #endif
                    135: 
                    136: #ifdef NO_IP
                    137: Label next_code;
                    138: #endif
                    139: 
                    140: #ifdef HAS_FILE
                    141: char* fileattr[6]={"rb","rb","r+b","r+b","wb","wb"};
                    142: char* pfileattr[6]={"r","r","r+","r+","w","w"};
                    143: 
                    144: #ifndef O_BINARY
                    145: #define O_BINARY 0
                    146: #endif
                    147: #ifndef O_TEXT
                    148: #define O_TEXT 0
                    149: #endif
                    150: 
                    151: int ufileattr[6]= {
                    152:   O_RDONLY|O_BINARY, O_RDONLY|O_BINARY,
                    153:   O_RDWR  |O_BINARY, O_RDWR  |O_BINARY,
                    154:   O_WRONLY|O_BINARY, O_WRONLY|O_BINARY };
                    155: #endif
                    156: /* end global vars for engine.c */
                    157: 
1.1       anton     158: #define PRIM_VERSION 1
                    159: /* increment this whenever the primitives change in an incompatible way */
                    160: 
1.14      pazsan    161: #ifndef DEFAULTPATH
1.39      anton     162: #  define DEFAULTPATH "."
1.14      pazsan    163: #endif
                    164: 
1.1       anton     165: #ifdef MSDOS
                    166: jmp_buf throw_jmp_buf;
                    167: #endif
                    168: 
1.56      anton     169: #if defined(DOUBLY_INDIRECT)
                    170: #  define CFA(n)       ({Cell _n = (n); ((Cell)(((_n & 0x4000) ? symbols : xts)+(_n&~0x4000UL)));})
1.1       anton     171: #else
1.56      anton     172: #  define CFA(n)       ((Cell)(symbols+((n)&~0x4000UL)))
1.1       anton     173: #endif
                    174: 
                    175: #define maxaligned(n)  (typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
                    176: 
                    177: static UCell dictsize=0;
                    178: static UCell dsize=0;
                    179: static UCell rsize=0;
                    180: static UCell fsize=0;
                    181: static UCell lsize=0;
                    182: int offset_image=0;
1.4       anton     183: int die_on_signal=0;
1.169     anton     184: int ignore_async_signals=0;
1.13      pazsan    185: #ifndef INCLUDE_IMAGE
1.1       anton     186: static int clear_dictionary=0;
1.24      anton     187: UCell pagesize=1;
1.22      pazsan    188: char *progname;
                    189: #else
                    190: char *progname = "gforth";
                    191: int optind = 1;
1.13      pazsan    192: #endif
1.181     anton     193: #ifndef MAP_NORESERVE
                    194: #define MAP_NORESERVE 0
                    195: #endif
1.183     pazsan    196: /* IF you have an old Cygwin, this may help:
1.182     pazsan    197: #ifdef __CYGWIN__
                    198: #define MAP_NORESERVE 0
                    199: #endif
1.183     pazsan    200: */
1.181     anton     201: static int map_noreserve=MAP_NORESERVE;
1.31      pazsan    202: 
1.167     anton     203: #define CODE_BLOCK_SIZE (512*1024) /* !! overflow handling for -native */
1.48      anton     204: Address code_area=0;
1.73      anton     205: Cell code_area_size = CODE_BLOCK_SIZE;
1.75      anton     206: Address code_here=NULL+CODE_BLOCK_SIZE; /* does for code-area what HERE
                    207:                                           does for the dictionary */
1.100     anton     208: Address start_flush=NULL; /* start of unflushed code */
1.74      anton     209: Cell last_jump=0; /* if the last prim was compiled without jump, this
                    210:                      is it's number, otherwise this contains 0 */
1.48      anton     211: 
1.60      anton     212: static int no_super=0;   /* true if compile_prim should not fuse prims */
1.81      anton     213: static int no_dynamic=NO_DYNAMIC_DEFAULT; /* if true, no code is generated
                    214:                                             dynamically */
1.110     anton     215: static int print_metrics=0; /* if true, print metrics on exit */
1.171     anton     216: static int static_super_number = 0; /* number of ss used if available */
                    217:                                     /* disabled because of tpa */
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.106     anton     884: }
                    885: 
                    886: /* dynamic replication/superinstruction stuff */
                    887: 
1.69      anton     888: #ifndef NO_DYNAMIC
1.161     pazsan    889: static int compare_priminfo_length(const void *_a, const void *_b)
1.76      anton     890: {
1.90      anton     891:   PrimInfo **a = (PrimInfo **)_a;
                    892:   PrimInfo **b = (PrimInfo **)_b;
1.77      anton     893:   Cell diff = (*a)->length - (*b)->length;
                    894:   if (diff)
                    895:     return diff;
                    896:   else /* break ties by start address; thus the decompiler produces
                    897:           the earliest primitive with the same code (e.g. noop instead
                    898:           of (char) and @ instead of >code-address */
                    899:     return (*b)->start - (*a)->start;
1.76      anton     900: }
1.112     anton     901: #endif /* !defined(NO_DYNAMIC) */
1.76      anton     902: 
1.125     anton     903: static char MAYBE_UNUSED superend[]={
1.126     anton     904: #include PRIM_SUPEREND_I
1.106     anton     905: };
1.107     anton     906: 
                    907: Cell npriminfos=0;
1.76      anton     908: 
1.146     anton     909: Label goto_start;
                    910: Cell goto_len;
                    911: 
1.162     pazsan    912: #ifndef NO_DYNAMIC
1.161     pazsan    913: static int compare_labels(const void *pa, const void *pb)
1.113     anton     914: {
1.114     anton     915:   Label a = *(Label *)pa;
                    916:   Label b = *(Label *)pb;
                    917:   return a-b;
                    918: }
1.162     pazsan    919: #endif
1.113     anton     920: 
1.161     pazsan    921: static Label bsearch_next(Label key, Label *a, UCell n)
1.114     anton     922:      /* a is sorted; return the label >=key that is the closest in a;
                    923:         return NULL if there is no label in a >=key */
                    924: {
                    925:   int mid = (n-1)/2;
                    926:   if (n<1)
                    927:     return NULL;
                    928:   if (n == 1) {
                    929:     if (a[0] < key)
                    930:       return NULL;
                    931:     else
                    932:       return a[0];
                    933:   }
                    934:   if (a[mid] < key)
                    935:     return bsearch_next(key, a+mid+1, n-mid-1);
                    936:   else
                    937:     return bsearch_next(key, a, mid+1);
1.113     anton     938: }
                    939: 
1.161     pazsan    940: static void check_prims(Label symbols1[])
1.47      anton     941: {
                    942:   int i;
1.90      anton     943: #ifndef NO_DYNAMIC
1.146     anton     944:   Label *symbols2, *symbols3, *ends1, *ends1j, *ends1jsorted, *goto_p;
1.119     anton     945:   int nends1j;
1.90      anton     946: #endif
1.47      anton     947: 
1.66      anton     948:   if (debug)
                    949: #ifdef __VERSION__
                    950:     fprintf(stderr, "Compiled with gcc-" __VERSION__ "\n");
                    951: #else
                    952: #define xstr(s) str(s)
                    953: #define str(s) #s
                    954:   fprintf(stderr, "Compiled with gcc-" xstr(__GNUC__) "." xstr(__GNUC_MINOR__) "\n"); 
                    955: #endif
1.121     anton     956:   for (i=0; symbols1[i]!=0; i++)
1.47      anton     957:     ;
1.55      anton     958:   npriminfos = i;
1.70      anton     959:   
                    960: #ifndef NO_DYNAMIC
1.66      anton     961:   if (no_dynamic)
                    962:     return;
1.164     pazsan    963:   symbols2=gforth_engine2(0,0,0,0,0);
1.70      anton     964: #if NO_IP
1.164     pazsan    965:   symbols3=gforth_engine3(0,0,0,0,0);
1.70      anton     966: #else
                    967:   symbols3=symbols1;
                    968: #endif
1.121     anton     969:   ends1 = symbols1+i+1;
1.119     anton     970:   ends1j =   ends1+i;
1.146     anton     971:   goto_p = ends1j+i+1; /* goto_p[0]==before; ...[1]==after;*/
1.121     anton     972:   nends1j = i+1;
1.119     anton     973:   ends1jsorted = (Label *)alloca(nends1j*sizeof(Label));
                    974:   memcpy(ends1jsorted,ends1j,nends1j*sizeof(Label));
                    975:   qsort(ends1jsorted, nends1j, sizeof(Label), compare_labels);
1.146     anton     976: 
                    977:   /* check whether the "goto *" is relocatable */
                    978:   goto_len = goto_p[1]-goto_p[0];
                    979:   debugp(stderr, "goto * %p %p len=%ld\n",
1.190     anton     980:         goto_p[0],symbols2[goto_p-symbols1],(long)goto_len);
1.146     anton     981:   if (memcmp(goto_p[0],symbols2[goto_p-symbols1],goto_len)!=0) { /* unequal */
                    982:     no_dynamic=1;
                    983:     debugp(stderr,"  not relocatable, disabling dynamic code generation\n");
1.148     anton     984:     init_ss_cost();
1.146     anton     985:     return;
                    986:   }
                    987:   goto_start = goto_p[0];
1.113     anton     988:   
1.47      anton     989:   priminfos = calloc(i,sizeof(PrimInfo));
1.121     anton     990:   for (i=0; symbols1[i]!=0; i++) {
1.70      anton     991:     int prim_len = ends1[i]-symbols1[i];
1.47      anton     992:     PrimInfo *pi=&priminfos[i];
1.154     anton     993:     struct cost *sc=&super_costs[i];
1.70      anton     994:     int j=0;
                    995:     char *s1 = (char *)symbols1[i];
                    996:     char *s2 = (char *)symbols2[i];
                    997:     char *s3 = (char *)symbols3[i];
1.119     anton     998:     Label endlabel = bsearch_next(symbols1[i]+1,ends1jsorted,nends1j);
1.70      anton     999: 
                   1000:     pi->start = s1;
1.121     anton    1001:     pi->superend = superend[i]|no_super;
1.147     anton    1002:     pi->length = prim_len;
1.113     anton    1003:     pi->restlength = endlabel - symbols1[i] - pi->length;
1.70      anton    1004:     pi->nimmargs = 0;
1.144     pazsan   1005:     relocs++;
1.190     anton    1006: #if defined(BURG_FORMAT)
                   1007:     { /* output as burg-style rules */
                   1008:       int p=super_costs[i].offset;
                   1009:       if (p==N_noop)
                   1010:        debugp(stderr, "S%d: S%d = %d (%d);", sc->state_in, sc->state_out, i+1, pi->length);
                   1011:       else
                   1012:        debugp(stderr, "S%d: op%d(S%d) = %d (%d);", sc->state_in, p, sc->state_out, i+1, pi->length);
                   1013:     }
                   1014: #else
1.154     anton    1015:     debugp(stderr, "%-15s %d-%d %4d %p %p %p len=%3ld rest=%2ld send=%1d",
                   1016:           prim_names[i], sc->state_in, sc->state_out,
                   1017:           i, s1, s2, s3, (long)(pi->length), (long)(pi->restlength),
                   1018:           pi->superend);
1.190     anton    1019: #endif
1.114     anton    1020:     if (endlabel == NULL) {
                   1021:       pi->start = NULL; /* not relocatable */
1.122     anton    1022:       if (pi->length<0) pi->length=100;
1.190     anton    1023: #ifndef BURG_FORMAT
1.144     pazsan   1024:       debugp(stderr,"\n   non_reloc: no J label > start found\n");
1.190     anton    1025: #endif
1.144     pazsan   1026:       relocs--;
                   1027:       nonrelocs++;
1.114     anton    1028:       continue;
                   1029:     }
                   1030:     if (ends1[i] > endlabel && !pi->superend) {
1.113     anton    1031:       pi->start = NULL; /* not relocatable */
1.122     anton    1032:       pi->length = endlabel-symbols1[i];
1.190     anton    1033: #ifndef BURG_FORMAT
1.144     pazsan   1034:       debugp(stderr,"\n   non_reloc: there is a J label before the K label (restlength<0)\n");
1.190     anton    1035: #endif
1.144     pazsan   1036:       relocs--;
                   1037:       nonrelocs++;
1.113     anton    1038:       continue;
                   1039:     }
1.114     anton    1040:     if (ends1[i] < pi->start && !pi->superend) {
1.113     anton    1041:       pi->start = NULL; /* not relocatable */
1.122     anton    1042:       pi->length = endlabel-symbols1[i];
1.190     anton    1043: #ifndef BURG_FORMAT
1.144     pazsan   1044:       debugp(stderr,"\n   non_reloc: K label before I label (length<0)\n");
1.190     anton    1045: #endif
1.144     pazsan   1046:       relocs--;
                   1047:       nonrelocs++;
1.113     anton    1048:       continue;
                   1049:     }
1.138     anton    1050:     assert(pi->length>=0);
1.113     anton    1051:     assert(pi->restlength >=0);
1.74      anton    1052:     while (j<(pi->length+pi->restlength)) {
1.70      anton    1053:       if (s1[j]==s3[j]) {
                   1054:        if (s1[j] != s2[j]) {
                   1055:          pi->start = NULL; /* not relocatable */
1.190     anton    1056: #ifndef BURG_FORMAT
1.144     pazsan   1057:          debugp(stderr,"\n   non_reloc: engine1!=engine2 offset %3d",j);
1.190     anton    1058: #endif
1.74      anton    1059:          /* assert(j<prim_len); */
1.144     pazsan   1060:          relocs--;
                   1061:          nonrelocs++;
1.70      anton    1062:          break;
                   1063:        }
                   1064:        j++;
                   1065:       } else {
                   1066:        struct immarg *ia=&pi->immargs[pi->nimmargs];
                   1067: 
                   1068:        pi->nimmargs++;
                   1069:        ia->offset=j;
                   1070:        if ((~*(Cell *)&(s1[j]))==*(Cell *)&(s3[j])) {
                   1071:          ia->rel=0;
1.144     pazsan   1072:          debugp(stderr,"\n   absolute immarg: offset %3d",j);
1.70      anton    1073:        } else if ((&(s1[j]))+(*(Cell *)&(s1[j]))+4 ==
                   1074:                   symbols1[DOESJUMP+1]) {
                   1075:          ia->rel=1;
1.144     pazsan   1076:          debugp(stderr,"\n   relative immarg: offset %3d",j);
1.70      anton    1077:        } else {
                   1078:          pi->start = NULL; /* not relocatable */
1.190     anton    1079: #ifndef BURG_FORMAT
1.144     pazsan   1080:          debugp(stderr,"\n   non_reloc: engine1!=engine3 offset %3d",j);
1.190     anton    1081: #endif
1.74      anton    1082:          /* assert(j<prim_len);*/
1.144     pazsan   1083:          relocs--;
                   1084:          nonrelocs++;
1.70      anton    1085:          break;
                   1086:        }
                   1087:        j+=4;
1.47      anton    1088:       }
                   1089:     }
1.144     pazsan   1090:     debugp(stderr,"\n");
1.70      anton    1091:   }
1.76      anton    1092:   decomp_prims = calloc(i,sizeof(PrimInfo *));
                   1093:   for (i=DOESJUMP+1; i<npriminfos; i++)
                   1094:     decomp_prims[i] = &(priminfos[i]);
                   1095:   qsort(decomp_prims+DOESJUMP+1, npriminfos-DOESJUMP-1, sizeof(PrimInfo *),
                   1096:        compare_priminfo_length);
1.70      anton    1097: #endif
                   1098: }
                   1099: 
1.161     pazsan   1100: static void flush_to_here(void)
1.74      anton    1101: {
1.93      anton    1102: #ifndef NO_DYNAMIC
1.100     anton    1103:   if (start_flush)
                   1104:     FLUSH_ICACHE(start_flush, code_here-start_flush);
1.74      anton    1105:   start_flush=code_here;
1.93      anton    1106: #endif
1.74      anton    1107: }
                   1108: 
1.185     anton    1109: static void align_code(void)
                   1110:      /* align code_here on some platforms */
                   1111: {
                   1112: #ifndef NO_DYNAMIC
1.186     anton    1113: #if defined(CODE_PADDING)
1.185     anton    1114:   Cell alignment = CODE_ALIGNMENT;
1.186     anton    1115:   static char nops[] = CODE_PADDING;
                   1116:   UCell maxpadding=MAX_PADDING;
1.185     anton    1117:   UCell offset = ((UCell)code_here)&(alignment-1);
                   1118:   UCell length = alignment-offset;
1.186     anton    1119:   if (length <= maxpadding) {
                   1120:     memcpy(code_here,nops+offset,length);
1.185     anton    1121:     code_here += length;
                   1122:   }
1.186     anton    1123: #endif /* defined(CODE_PADDING) */
1.185     anton    1124: #endif /* defined(NO_DYNAMIC */
                   1125: }  
                   1126: 
1.93      anton    1127: #ifndef NO_DYNAMIC
1.161     pazsan   1128: static void append_jump(void)
1.74      anton    1129: {
                   1130:   if (last_jump) {
                   1131:     PrimInfo *pi = &priminfos[last_jump];
                   1132:     
                   1133:     memcpy(code_here, pi->start+pi->length, pi->restlength);
                   1134:     code_here += pi->restlength;
1.147     anton    1135:     memcpy(code_here, goto_start, goto_len);
                   1136:     code_here += goto_len;
1.185     anton    1137:     align_code();
1.74      anton    1138:     last_jump=0;
                   1139:   }
                   1140: }
                   1141: 
1.75      anton    1142: /* Gforth remembers all code blocks in this list.  On forgetting (by
                   1143: executing a marker) the code blocks are not freed (because Gforth does
                   1144: not remember how they were allocated; hmm, remembering that might be
                   1145: easier and cleaner).  Instead, code_here etc. are reset to the old
                   1146: value, and the "forgotten" code blocks are reused when they are
                   1147: needed. */
                   1148: 
                   1149: struct code_block_list {
                   1150:   struct code_block_list *next;
                   1151:   Address block;
                   1152:   Cell size;
                   1153: } *code_block_list=NULL, **next_code_blockp=&code_block_list;
                   1154: 
1.161     pazsan   1155: static Address append_prim(Cell p)
1.74      anton    1156: {
                   1157:   PrimInfo *pi = &priminfos[p];
                   1158:   Address old_code_here = code_here;
                   1159: 
1.185     anton    1160:   if (code_area+code_area_size < code_here+pi->length+pi->restlength+goto_len+CODE_ALIGNMENT) {
1.75      anton    1161:     struct code_block_list *p;
1.74      anton    1162:     append_jump();
1.93      anton    1163:     flush_to_here();
1.75      anton    1164:     if (*next_code_blockp == NULL) {
1.161     pazsan   1165:       code_here = start_flush = code_area = gforth_alloc(code_area_size);
1.75      anton    1166:       p = (struct code_block_list *)malloc(sizeof(struct code_block_list));
                   1167:       *next_code_blockp = p;
                   1168:       p->next = NULL;
                   1169:       p->block = code_here;
                   1170:       p->size = code_area_size;
                   1171:     } else {
                   1172:       p = *next_code_blockp;
                   1173:       code_here = start_flush = code_area = p->block;
                   1174:     }
1.74      anton    1175:     old_code_here = code_here;
1.75      anton    1176:     next_code_blockp = &(p->next);
1.74      anton    1177:   }
                   1178:   memcpy(code_here, pi->start, pi->length);
                   1179:   code_here += pi->length;
                   1180:   return old_code_here;
                   1181: }
                   1182: #endif
1.75      anton    1183: 
                   1184: int forget_dyncode(Address code)
                   1185: {
                   1186: #ifdef NO_DYNAMIC
                   1187:   return -1;
                   1188: #else
                   1189:   struct code_block_list *p, **pp;
                   1190: 
                   1191:   for (pp=&code_block_list, p=*pp; p!=NULL; pp=&(p->next), p=*pp) {
                   1192:     if (code >= p->block && code < p->block+p->size) {
                   1193:       next_code_blockp = &(p->next);
                   1194:       code_here = start_flush = code;
                   1195:       code_area = p->block;
                   1196:       last_jump = 0;
                   1197:       return -1;
                   1198:     }
                   1199:   }
1.78      anton    1200:   return -no_dynamic;
1.75      anton    1201: #endif /* !defined(NO_DYNAMIC) */
                   1202: }
                   1203: 
1.161     pazsan   1204: static long dyncodesize(void)
1.104     anton    1205: {
                   1206: #ifndef NO_DYNAMIC
1.106     anton    1207:   struct code_block_list *p;
1.104     anton    1208:   long size=0;
                   1209:   for (p=code_block_list; p!=NULL; p=p->next) {
                   1210:     if (code_here >= p->block && code_here < p->block+p->size)
                   1211:       return size + (code_here - p->block);
                   1212:     else
                   1213:       size += p->size;
                   1214:   }
                   1215: #endif /* !defined(NO_DYNAMIC) */
                   1216:   return 0;
                   1217: }
                   1218: 
1.90      anton    1219: Label decompile_code(Label _code)
1.75      anton    1220: {
1.76      anton    1221: #ifdef NO_DYNAMIC
1.90      anton    1222:   return _code;
1.76      anton    1223: #else /* !defined(NO_DYNAMIC) */
                   1224:   Cell i;
1.77      anton    1225:   struct code_block_list *p;
1.90      anton    1226:   Address code=_code;
1.76      anton    1227: 
1.77      anton    1228:   /* first, check if we are in code at all */
                   1229:   for (p = code_block_list;; p = p->next) {
                   1230:     if (p == NULL)
                   1231:       return code;
                   1232:     if (code >= p->block && code < p->block+p->size)
                   1233:       break;
                   1234:   }
1.76      anton    1235:   /* reverse order because NOOP might match other prims */
                   1236:   for (i=npriminfos-1; i>DOESJUMP; i--) {
                   1237:     PrimInfo *pi=decomp_prims[i];
                   1238:     if (pi->start==code || (pi->start && memcmp(code,pi->start,pi->length)==0))
1.121     anton    1239:       return vm_prims[super2[super_costs[pi-priminfos].offset]];
1.118     anton    1240:     /* return pi->start;*/
1.76      anton    1241:   }
                   1242:   return code;
                   1243: #endif /* !defined(NO_DYNAMIC) */
1.75      anton    1244: }
1.74      anton    1245: 
1.70      anton    1246: #ifdef NO_IP
                   1247: int nbranchinfos=0;
                   1248: 
                   1249: struct branchinfo {
1.136     anton    1250:   Label **targetpp; /* **(bi->targetpp) is the target */
1.70      anton    1251:   Cell *addressptr; /* store the target here */
                   1252: } branchinfos[100000];
                   1253: 
                   1254: int ndoesexecinfos=0;
                   1255: struct doesexecinfo {
                   1256:   int branchinfo; /* fix the targetptr of branchinfos[...->branchinfo] */
1.136     anton    1257:   Label *targetp; /*target for branch (because this is not in threaded code)*/
1.70      anton    1258:   Cell *xt; /* cfa of word whose does-code needs calling */
                   1259: } doesexecinfos[10000];
                   1260: 
1.161     pazsan   1261: static void set_rel_target(Cell *source, Label target)
1.70      anton    1262: {
                   1263:   *source = ((Cell)target)-(((Cell)source)+4);
                   1264: }
                   1265: 
1.161     pazsan   1266: static void register_branchinfo(Label source, Cell *targetpp)
1.70      anton    1267: {
                   1268:   struct branchinfo *bi = &(branchinfos[nbranchinfos]);
1.136     anton    1269:   bi->targetpp = (Label **)targetpp;
1.70      anton    1270:   bi->addressptr = (Cell *)source;
                   1271:   nbranchinfos++;
                   1272: }
                   1273: 
1.161     pazsan   1274: static Address compile_prim1arg(PrimNum p, Cell **argp)
1.70      anton    1275: {
1.133     anton    1276:   Address old_code_here=append_prim(p);
1.70      anton    1277: 
1.74      anton    1278:   assert(vm_prims[p]==priminfos[p].start);
1.133     anton    1279:   *argp = (Cell*)(old_code_here+priminfos[p].immargs[0].offset);
                   1280:   return old_code_here;
1.70      anton    1281: }
                   1282: 
1.161     pazsan   1283: static Address compile_call2(Cell *targetpp, Cell **next_code_targetp)
1.70      anton    1284: {
1.73      anton    1285:   PrimInfo *pi = &priminfos[N_call2];
1.74      anton    1286:   Address old_code_here = append_prim(N_call2);
1.70      anton    1287: 
1.134     anton    1288:   *next_code_targetp = (Cell *)(old_code_here + pi->immargs[0].offset);
1.136     anton    1289:   register_branchinfo(old_code_here + pi->immargs[1].offset, targetpp);
1.134     anton    1290:   return old_code_here;
1.70      anton    1291: }
                   1292: #endif
                   1293: 
                   1294: void finish_code(void)
                   1295: {
                   1296: #ifdef NO_IP
                   1297:   Cell i;
                   1298: 
                   1299:   compile_prim1(NULL);
                   1300:   for (i=0; i<ndoesexecinfos; i++) {
                   1301:     struct doesexecinfo *dei = &doesexecinfos[i];
1.136     anton    1302:     dei->targetp = (Label *)DOES_CODE1((dei->xt));
                   1303:     branchinfos[dei->branchinfo].targetpp = &(dei->targetp);
1.70      anton    1304:   }
                   1305:   ndoesexecinfos = 0;
                   1306:   for (i=0; i<nbranchinfos; i++) {
                   1307:     struct branchinfo *bi=&branchinfos[i];
1.136     anton    1308:     set_rel_target(bi->addressptr, **(bi->targetpp));
1.70      anton    1309:   }
                   1310:   nbranchinfos = 0;
1.128     anton    1311: #else
                   1312:   compile_prim1(NULL);
1.48      anton    1313: #endif
1.93      anton    1314:   flush_to_here();
1.48      anton    1315: }
                   1316: 
1.162     pazsan   1317: #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
1.128     anton    1318: #ifdef NO_IP
1.161     pazsan   1319: static Cell compile_prim_dyn(PrimNum p, Cell *tcp)
1.128     anton    1320:      /* compile prim #p dynamically (mod flags etc.) and return start
                   1321:        address of generated code for putting it into the threaded
                   1322:        code. This function is only called if all the associated
                   1323:        inline arguments of p are already in place (at tcp[1] etc.) */
                   1324: {
                   1325:   PrimInfo *pi=&priminfos[p];
                   1326:   Cell *next_code_target=NULL;
1.135     anton    1327:   Address codeaddr;
                   1328:   Address primstart;
1.128     anton    1329:   
                   1330:   assert(p<npriminfos);
                   1331:   if (p==N_execute || p==N_perform || p==N_lit_perform) {
1.134     anton    1332:     codeaddr = compile_prim1arg(N_set_next_code, &next_code_target);
1.135     anton    1333:     primstart = append_prim(p);
                   1334:     goto other_prim;
                   1335:   } else if (p==N_call) {
1.136     anton    1336:     codeaddr = compile_call2(tcp+1, &next_code_target);
1.128     anton    1337:   } else if (p==N_does_exec) {
                   1338:     struct doesexecinfo *dei = &doesexecinfos[ndoesexecinfos++];
1.133     anton    1339:     Cell *arg;
                   1340:     codeaddr = compile_prim1arg(N_lit,&arg);
                   1341:     *arg = (Cell)PFA(tcp[1]);
1.128     anton    1342:     /* we cannot determine the callee now (last_start[1] may be a
                   1343:        forward reference), so just register an arbitrary target, and
                   1344:        register in dei that we need to fix this before resolving
                   1345:        branches */
                   1346:     dei->branchinfo = nbranchinfos;
                   1347:     dei->xt = (Cell *)(tcp[1]);
1.134     anton    1348:     compile_call2(0, &next_code_target);
1.128     anton    1349:   } else if (!is_relocatable(p)) {
1.133     anton    1350:     Cell *branch_target;
                   1351:     codeaddr = compile_prim1arg(N_set_next_code, &next_code_target);
                   1352:     compile_prim1arg(N_branch,&branch_target);
                   1353:     set_rel_target(branch_target,vm_prims[p]);
1.128     anton    1354:   } else {
                   1355:     unsigned j;
1.135     anton    1356: 
                   1357:     codeaddr = primstart = append_prim(p);
                   1358:   other_prim:
1.128     anton    1359:     for (j=0; j<pi->nimmargs; j++) {
                   1360:       struct immarg *ia = &(pi->immargs[j]);
1.136     anton    1361:       Cell *argp = tcp + pi->nimmargs - j;
                   1362:       Cell argval = *argp; /* !! specific to prims */
1.128     anton    1363:       if (ia->rel) { /* !! assumption: relative refs are branches */
1.136     anton    1364:        register_branchinfo(primstart + ia->offset, argp);
1.128     anton    1365:       } else /* plain argument */
1.135     anton    1366:        *(Cell *)(primstart + ia->offset) = argval;
1.128     anton    1367:     }
                   1368:   }
                   1369:   if (next_code_target!=NULL)
                   1370:     *next_code_target = (Cell)code_here;
1.135     anton    1371:   return (Cell)codeaddr;
1.128     anton    1372: }
                   1373: #else /* !defined(NO_IP) */
1.161     pazsan   1374: static Cell compile_prim_dyn(PrimNum p, Cell *tcp)
1.128     anton    1375:      /* compile prim #p dynamically (mod flags etc.) and return start
                   1376:         address of generated code for putting it into the threaded code */
1.108     anton    1377: {
1.121     anton    1378:   Cell static_prim = (Cell)vm_prims[p];
1.108     anton    1379: #if defined(NO_DYNAMIC)
                   1380:   return static_prim;
                   1381: #else /* !defined(NO_DYNAMIC) */
                   1382:   Address old_code_here;
                   1383: 
                   1384:   if (no_dynamic)
                   1385:     return static_prim;
1.125     anton    1386:   if (p>=npriminfos || !is_relocatable(p)) {
1.108     anton    1387:     append_jump();
                   1388:     return static_prim;
                   1389:   }
                   1390:   old_code_here = append_prim(p);
1.147     anton    1391:   last_jump = p;
                   1392:   if (priminfos[p].superend)
                   1393:     append_jump();
1.108     anton    1394:   return (Cell)old_code_here;
                   1395: #endif  /* !defined(NO_DYNAMIC) */
                   1396: }
1.128     anton    1397: #endif /* !defined(NO_IP) */
1.162     pazsan   1398: #endif
1.70      anton    1399: 
1.109     anton    1400: #ifndef NO_DYNAMIC
1.161     pazsan   1401: static int cost_codesize(int prim)
1.109     anton    1402: {
1.121     anton    1403:   return priminfos[prim].length;
1.109     anton    1404: }
                   1405: #endif
                   1406: 
1.161     pazsan   1407: static int cost_ls(int prim)
1.109     anton    1408: {
                   1409:   struct cost *c = super_costs+prim;
                   1410: 
                   1411:   return c->loads + c->stores;
                   1412: }
                   1413: 
1.161     pazsan   1414: static int cost_lsu(int prim)
1.109     anton    1415: {
                   1416:   struct cost *c = super_costs+prim;
                   1417: 
                   1418:   return c->loads + c->stores + c->updates;
                   1419: }
                   1420: 
1.161     pazsan   1421: static int cost_nexts(int prim)
1.109     anton    1422: {
                   1423:   return 1;
                   1424: }
                   1425: 
                   1426: typedef int Costfunc(int);
                   1427: Costfunc *ss_cost =  /* cost function for optimize_bb */
                   1428: #ifdef NO_DYNAMIC
                   1429: cost_lsu;
                   1430: #else
                   1431: cost_codesize;
                   1432: #endif
                   1433: 
1.110     anton    1434: struct {
                   1435:   Costfunc *costfunc;
                   1436:   char *metricname;
                   1437:   long sum;
                   1438: } cost_sums[] = {
                   1439: #ifndef NO_DYNAMIC
                   1440:   { cost_codesize, "codesize", 0 },
                   1441: #endif
                   1442:   { cost_ls,       "ls",       0 },
                   1443:   { cost_lsu,      "lsu",      0 },
                   1444:   { cost_nexts,    "nexts",    0 }
                   1445: };
                   1446: 
1.148     anton    1447: #ifndef NO_DYNAMIC
                   1448: void init_ss_cost(void) {
                   1449:   if (no_dynamic && ss_cost == cost_codesize) {
                   1450:     ss_cost = cost_nexts;
                   1451:     cost_sums[0] = cost_sums[1]; /* don't use cost_codesize for print-metrics */
                   1452:     debugp(stderr, "--no-dynamic conflicts with --ss-min-codesize, reverting to --ss-min-nexts\n");
                   1453:   }
                   1454: }
                   1455: #endif
                   1456: 
1.106     anton    1457: #define MAX_BB 128 /* maximum number of instructions in BB */
1.125     anton    1458: #define INF_COST 1000000 /* infinite cost */
                   1459: #define CANONICAL_STATE 0
                   1460: 
                   1461: struct waypoint {
                   1462:   int cost;     /* the cost from here to the end */
                   1463:   PrimNum inst; /* the inst used from here to the next waypoint */
                   1464:   char relocatable; /* the last non-transition was relocatable */
                   1465:   char no_transition; /* don't use the next transition (relocatability)
                   1466:                       * or this transition (does not change state) */
                   1467: };
                   1468: 
1.156     anton    1469: struct tpa_state { /* tree parsing automaton (like) state */
1.155     anton    1470:   /* labeling is back-to-front */
                   1471:   struct waypoint *inst;  /* in front of instruction */
                   1472:   struct waypoint *trans; /* in front of instruction and transition */
                   1473: }; 
                   1474: 
1.156     anton    1475: struct tpa_state *termstate = NULL; /* initialized in loader() */
1.155     anton    1476: 
1.158     anton    1477: /* statistics about tree parsing (lazyburg) stuff */
                   1478: long lb_basic_blocks = 0;
                   1479: long lb_labeler_steps = 0;
                   1480: long lb_labeler_automaton = 0;
                   1481: long lb_labeler_dynprog = 0;
                   1482: long lb_newstate_equiv = 0;
                   1483: long lb_newstate_new = 0;
                   1484: long lb_applicable_base_rules = 0;
                   1485: long lb_applicable_chain_rules = 0;
                   1486: 
1.162     pazsan   1487: #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
1.161     pazsan   1488: static void init_waypoints(struct waypoint ws[])
1.125     anton    1489: {
                   1490:   int k;
                   1491: 
                   1492:   for (k=0; k<maxstates; k++)
                   1493:     ws[k].cost=INF_COST;
                   1494: }
1.106     anton    1495: 
1.161     pazsan   1496: static struct tpa_state *empty_tpa_state()
1.155     anton    1497: {
1.156     anton    1498:   struct tpa_state *s = malloc(sizeof(struct tpa_state));
1.155     anton    1499: 
1.157     anton    1500:   s->inst  = calloc(maxstates,sizeof(struct waypoint));
1.155     anton    1501:   init_waypoints(s->inst);
1.157     anton    1502:   s->trans = calloc(maxstates,sizeof(struct waypoint));
1.155     anton    1503:   /* init_waypoints(s->trans);*/
                   1504:   return s;
                   1505: }
                   1506: 
1.161     pazsan   1507: static void transitions(struct tpa_state *t)
1.107     anton    1508: {
1.125     anton    1509:   int k;
                   1510:   struct super_state *l;
                   1511:   
                   1512:   for (k=0; k<maxstates; k++) {
1.155     anton    1513:     t->trans[k] = t->inst[k];
                   1514:     t->trans[k].no_transition = 1;
1.125     anton    1515:   }
                   1516:   for (l = state_transitions; l != NULL; l = l->next) {
                   1517:     PrimNum s = l->super;
                   1518:     int jcost;
                   1519:     struct cost *c=super_costs+s;
1.155     anton    1520:     struct waypoint *wi=&(t->trans[c->state_in]);
                   1521:     struct waypoint *wo=&(t->inst[c->state_out]);
1.158     anton    1522:     lb_applicable_chain_rules++;
1.125     anton    1523:     if (wo->cost == INF_COST)
                   1524:       continue;
                   1525:     jcost = wo->cost + ss_cost(s);
                   1526:     if (jcost <= wi->cost) {
                   1527:       wi->cost = jcost;
                   1528:       wi->inst = s;
                   1529:       wi->relocatable = wo->relocatable;
                   1530:       wi->no_transition = 0;
                   1531:       /* if (ss_greedy) wi->cost = wo->cost ? */
                   1532:     }
                   1533:   }
                   1534: }
1.107     anton    1535: 
1.161     pazsan   1536: static struct tpa_state *make_termstate()
1.155     anton    1537: {
1.157     anton    1538:   struct tpa_state *s = empty_tpa_state();
1.155     anton    1539: 
                   1540:   s->inst[CANONICAL_STATE].cost = 0;
                   1541:   transitions(s);
                   1542:   return s;
                   1543: }
1.162     pazsan   1544: #endif
1.155     anton    1545: 
1.156     anton    1546: #define TPA_SIZE 16384
                   1547: 
                   1548: struct tpa_entry {
                   1549:   struct tpa_entry *next;
                   1550:   PrimNum inst;
                   1551:   struct tpa_state *state_behind;  /* note: brack-to-front labeling */
                   1552:   struct tpa_state *state_infront; /* note: brack-to-front labeling */
                   1553: } *tpa_table[TPA_SIZE];
                   1554: 
1.162     pazsan   1555: #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
1.161     pazsan   1556: static Cell hash_tpa(PrimNum p, struct tpa_state *t)
1.156     anton    1557: {
                   1558:   UCell it = (UCell )t;
                   1559:   return (p+it+(it>>14))&(TPA_SIZE-1);
                   1560: }
                   1561: 
1.161     pazsan   1562: static struct tpa_state **lookup_tpa(PrimNum p, struct tpa_state *t2)
1.156     anton    1563: {
                   1564:   int hash=hash_tpa(p, t2);
                   1565:   struct tpa_entry *te = tpa_table[hash];
                   1566: 
1.158     anton    1567:   if (tpa_noautomaton) {
                   1568:     static struct tpa_state *t;
                   1569:     t = NULL;
                   1570:     return &t;
                   1571:   }
1.156     anton    1572:   for (; te!=NULL; te = te->next) {
                   1573:     if (p == te->inst && t2 == te->state_behind)
                   1574:       return &(te->state_infront);
                   1575:   }
                   1576:   te = (struct tpa_entry *)malloc(sizeof(struct tpa_entry));
                   1577:   te->next = tpa_table[hash];
                   1578:   te->inst = p;
                   1579:   te->state_behind = t2;
                   1580:   te->state_infront = NULL;
                   1581:   tpa_table[hash] = te;
                   1582:   return &(te->state_infront);
                   1583: }
                   1584: 
1.161     pazsan   1585: static void tpa_state_normalize(struct tpa_state *t)
1.157     anton    1586: {
                   1587:   /* normalize so cost of canonical state=0; this may result in
                   1588:      negative states for some states */
                   1589:   int d = t->inst[CANONICAL_STATE].cost;
                   1590:   int i;
                   1591: 
                   1592:   for (i=0; i<maxstates; i++) {
                   1593:     if (t->inst[i].cost != INF_COST)
                   1594:       t->inst[i].cost -= d;
                   1595:     if (t->trans[i].cost != INF_COST)
                   1596:       t->trans[i].cost -= d;
                   1597:   }
                   1598: }
                   1599: 
1.161     pazsan   1600: static int tpa_state_equivalent(struct tpa_state *t1, struct tpa_state *t2)
1.157     anton    1601: {
                   1602:   return (memcmp(t1->inst, t2->inst, maxstates*sizeof(struct waypoint)) == 0 &&
                   1603:          memcmp(t1->trans,t2->trans,maxstates*sizeof(struct waypoint)) == 0);
                   1604: }
1.162     pazsan   1605: #endif
1.157     anton    1606: 
                   1607: struct tpa_state_entry {
                   1608:   struct tpa_state_entry *next;
                   1609:   struct tpa_state *state;
                   1610: } *tpa_state_table[TPA_SIZE];
                   1611: 
1.163     pazsan   1612: #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
1.161     pazsan   1613: static Cell hash_tpa_state(struct tpa_state *t)
1.157     anton    1614: {
                   1615:   int *ti = (int *)(t->inst);
                   1616:   int *tt = (int *)(t->trans);
                   1617:   int r=0;
                   1618:   int i;
                   1619: 
                   1620:   for (i=0; ti+i < (int *)(t->inst+maxstates); i++)
                   1621:     r += ti[i]+tt[i];
                   1622:   return (r+(r>>14)+(r>>22)) & (TPA_SIZE-1);
                   1623: }
                   1624: 
1.161     pazsan   1625: static struct tpa_state *lookup_tpa_state(struct tpa_state *t)
1.157     anton    1626: {
                   1627:   Cell hash = hash_tpa_state(t);
                   1628:   struct tpa_state_entry *te = tpa_state_table[hash];
                   1629:   struct tpa_state_entry *tn;
                   1630: 
1.158     anton    1631:   if (!tpa_noequiv) {
                   1632:     for (; te!=NULL; te = te->next) {
                   1633:       if (tpa_state_equivalent(t, te->state)) {
                   1634:        lb_newstate_equiv++;
                   1635:        free(t->inst);
                   1636:        free(t->trans);
                   1637:        free(t);
                   1638:        return te->state;
                   1639:       }
1.157     anton    1640:     }
1.158     anton    1641:     tn = (struct tpa_state_entry *)malloc(sizeof(struct tpa_state_entry));
                   1642:     tn->next = te;
                   1643:     tn->state = t;
                   1644:     tpa_state_table[hash] = tn;
                   1645:   }
                   1646:   lb_newstate_new++;
                   1647:   if (tpa_trace)
                   1648:     fprintf(stderr, "%ld %ld lb_states\n", lb_labeler_steps, lb_newstate_new);
1.157     anton    1649:   return t;
                   1650: }
                   1651: 
1.125     anton    1652: /* use dynamic programming to find the shortest paths within the basic
                   1653:    block origs[0..ninsts-1] and rewrite the instructions pointed to by
                   1654:    instps to use it */
1.161     pazsan   1655: static void optimize_rewrite(Cell *instps[], PrimNum origs[], int ninsts)
1.125     anton    1656: {
                   1657:   int i,j;
1.156     anton    1658:   struct tpa_state *ts[ninsts+1];
1.125     anton    1659:   int nextdyn, nextstate, no_transition;
                   1660:   
1.158     anton    1661:   lb_basic_blocks++;
1.155     anton    1662:   ts[ninsts] = termstate;
1.189     anton    1663: #ifndef NO_DYNAMIC
                   1664:   if (print_sequences) {
                   1665:     for (i=0; i<ninsts; i++)
1.190     anton    1666: #if defined(BURG_FORMAT)
                   1667:       fprintf(stderr, "op%d ", super_costs[origs[i]].offset);
                   1668: #else
1.189     anton    1669:       fprintf(stderr, "%s ", prim_names[origs[i]]);
1.190     anton    1670: #endif
1.189     anton    1671:     fprintf(stderr, "\n");
                   1672:   }
                   1673: #endif
1.107     anton    1674:   for (i=ninsts-1; i>=0; i--) {
1.156     anton    1675:     struct tpa_state **tp = lookup_tpa(origs[i],ts[i+1]);
                   1676:     struct tpa_state *t = *tp;
1.158     anton    1677:     lb_labeler_steps++;
                   1678:     if (t) {
1.156     anton    1679:       ts[i] = t;
1.158     anton    1680:       lb_labeler_automaton++;
                   1681:     }
1.156     anton    1682:     else {
1.158     anton    1683:       lb_labeler_dynprog++;
1.156     anton    1684:       ts[i] = empty_tpa_state();
                   1685:       for (j=1; j<=max_super && i+j<=ninsts; j++) {
                   1686:        struct super_state **superp = lookup_super(origs+i, j);
                   1687:        if (superp!=NULL) {
                   1688:          struct super_state *supers = *superp;
                   1689:          for (; supers!=NULL; supers = supers->next) {
                   1690:            PrimNum s = supers->super;
                   1691:            int jcost;
                   1692:            struct cost *c=super_costs+s;
                   1693:            struct waypoint *wi=&(ts[i]->inst[c->state_in]);
                   1694:            struct waypoint *wo=&(ts[i+j]->trans[c->state_out]);
                   1695:            int no_transition = wo->no_transition;
1.158     anton    1696:            lb_applicable_base_rules++;
1.156     anton    1697:            if (!(is_relocatable(s)) && !wo->relocatable) {
                   1698:              wo=&(ts[i+j]->inst[c->state_out]);
                   1699:              no_transition=1;
                   1700:            }
                   1701:            if (wo->cost == INF_COST) 
                   1702:              continue;
                   1703:            jcost = wo->cost + ss_cost(s);
                   1704:            if (jcost <= wi->cost) {
                   1705:              wi->cost = jcost;
                   1706:              wi->inst = s;
                   1707:              wi->relocatable = is_relocatable(s);
                   1708:              wi->no_transition = no_transition;
                   1709:              /* if (ss_greedy) wi->cost = wo->cost ? */
                   1710:            }
1.125     anton    1711:          }
1.107     anton    1712:        }
                   1713:       }
1.156     anton    1714:       transitions(ts[i]);
1.157     anton    1715:       tpa_state_normalize(ts[i]);
                   1716:       *tp = ts[i] = lookup_tpa_state(ts[i]);
1.158     anton    1717:       if (tpa_trace)
                   1718:        fprintf(stderr, "%ld %ld lb_table_entries\n", lb_labeler_steps, lb_labeler_dynprog);
1.107     anton    1719:     }
1.125     anton    1720:   }
                   1721:   /* now rewrite the instructions */
                   1722:   nextdyn=0;
                   1723:   nextstate=CANONICAL_STATE;
1.155     anton    1724:   no_transition = ((!ts[0]->trans[nextstate].relocatable) 
                   1725:                   ||ts[0]->trans[nextstate].no_transition);
1.125     anton    1726:   for (i=0; i<ninsts; i++) {
                   1727:     Cell tc=0, tc2;
                   1728:     if (i==nextdyn) {
                   1729:       if (!no_transition) {
                   1730:        /* process trans */
1.155     anton    1731:        PrimNum p = ts[i]->trans[nextstate].inst;
1.125     anton    1732:        struct cost *c = super_costs+p;
1.155     anton    1733:        assert(ts[i]->trans[nextstate].cost != INF_COST);
1.125     anton    1734:        assert(c->state_in==nextstate);
1.128     anton    1735:        tc = compile_prim_dyn(p,NULL);
1.125     anton    1736:        nextstate = c->state_out;
                   1737:       }
                   1738:       {
                   1739:        /* process inst */
1.155     anton    1740:        PrimNum p = ts[i]->inst[nextstate].inst;
1.125     anton    1741:        struct cost *c=super_costs+p;
                   1742:        assert(c->state_in==nextstate);
1.155     anton    1743:        assert(ts[i]->inst[nextstate].cost != INF_COST);
1.125     anton    1744: #if defined(GFORTH_DEBUGGING)
                   1745:        assert(p == origs[i]);
                   1746: #endif
1.128     anton    1747:        tc2 = compile_prim_dyn(p,instps[i]);
1.125     anton    1748:        if (no_transition || !is_relocatable(p))
                   1749:          /* !! actually what we care about is if and where
                   1750:           * compile_prim_dyn() puts NEXTs */
                   1751:          tc=tc2;
1.155     anton    1752:        no_transition = ts[i]->inst[nextstate].no_transition;
1.125     anton    1753:        nextstate = c->state_out;
                   1754:        nextdyn += c->length;
                   1755:       }
                   1756:     } else {
                   1757: #if defined(GFORTH_DEBUGGING)
                   1758:       assert(0);
                   1759: #endif
                   1760:       tc=0;
1.155     anton    1761:       /* tc= (Cell)vm_prims[ts[i]->inst[CANONICAL_STATE].inst]; */
1.125     anton    1762:     }
                   1763:     *(instps[i]) = tc;
                   1764:   }      
                   1765:   if (!no_transition) {
1.155     anton    1766:     PrimNum p = ts[i]->trans[nextstate].inst;
1.125     anton    1767:     struct cost *c = super_costs+p;
                   1768:     assert(c->state_in==nextstate);
1.155     anton    1769:     assert(ts[i]->trans[nextstate].cost != INF_COST);
1.125     anton    1770:     assert(i==nextdyn);
1.128     anton    1771:     (void)compile_prim_dyn(p,NULL);
1.125     anton    1772:     nextstate = c->state_out;
1.107     anton    1773:   }
1.125     anton    1774:   assert(nextstate==CANONICAL_STATE);
1.107     anton    1775: }
1.162     pazsan   1776: #endif
1.107     anton    1777: 
1.105     anton    1778: /* compile *start, possibly rewriting it into a static and/or dynamic
                   1779:    superinstruction */
                   1780: void compile_prim1(Cell *start)
1.70      anton    1781: {
1.108     anton    1782: #if defined(DOUBLY_INDIRECT)
1.125     anton    1783:   Label prim;
                   1784: 
                   1785:   if (start==NULL)
                   1786:     return;
                   1787:   prim = (Label)*start;
1.108     anton    1788:   if (prim<((Label)(xts+DOESJUMP)) || prim>((Label)(xts+npriminfos))) {
                   1789:     fprintf(stderr,"compile_prim encountered xt %p\n", prim);
                   1790:     *start=(Cell)prim;
                   1791:     return;
                   1792:   } else {
                   1793:     *start = (Cell)(prim-((Label)xts)+((Label)vm_prims));
                   1794:     return;
                   1795:   }
                   1796: #elif defined(INDIRECT_THREADED)
                   1797:   return;
1.112     anton    1798: #else /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
1.128     anton    1799:   /* !! does not work, for unknown reasons; but something like this is
                   1800:      probably needed to ensure that we don't call compile_prim_dyn
                   1801:      before the inline arguments are there */
                   1802:   static Cell *instps[MAX_BB];
                   1803:   static PrimNum origs[MAX_BB];
                   1804:   static int ninsts=0;
                   1805:   PrimNum prim_num;
                   1806: 
                   1807:   if (start==NULL || ninsts >= MAX_BB ||
                   1808:       (ninsts>0 && superend[origs[ninsts-1]])) {
                   1809:     /* after bb, or at the start of the next bb */
                   1810:     optimize_rewrite(instps,origs,ninsts);
                   1811:     /* fprintf(stderr,"optimize_rewrite(...,%d)\n",ninsts); */
                   1812:     ninsts=0;
1.185     anton    1813:     if (start==NULL) {
                   1814:       align_code();
1.128     anton    1815:       return;
1.185     anton    1816:     }
1.128     anton    1817:   }
                   1818:   prim_num = ((Xt)*start)-vm_prims;
                   1819:   if(prim_num >= npriminfos) {
                   1820:     optimize_rewrite(instps,origs,ninsts);
1.129     anton    1821:     /* fprintf(stderr,"optimize_rewrite(...,%d)\n",ninsts);*/
1.128     anton    1822:     ninsts=0;
                   1823:     return;
                   1824:   }    
                   1825:   assert(ninsts<MAX_BB);
                   1826:   instps[ninsts] = start;
                   1827:   origs[ninsts] = prim_num;
                   1828:   ninsts++;
1.112     anton    1829: #endif /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
1.47      anton    1830: }
                   1831: 
1.176     pazsan   1832: #ifndef STANDALONE
1.161     pazsan   1833: Address gforth_loader(FILE *imagefile, char* filename)
1.1       anton    1834: /* returns the address of the image proper (after the preamble) */
                   1835: {
                   1836:   ImageHeader header;
                   1837:   Address image;
                   1838:   Address imp; /* image+preamble */
1.17      anton    1839:   Char magic[8];
                   1840:   char magic7; /* size byte of magic number */
1.1       anton    1841:   Cell preamblesize=0;
1.6       pazsan   1842:   Cell data_offset = offset_image ? 56*sizeof(Cell) : 0;
1.1       anton    1843:   UCell check_sum;
1.15      pazsan   1844:   Cell ausize = ((RELINFOBITS ==  8) ? 0 :
                   1845:                 (RELINFOBITS == 16) ? 1 :
                   1846:                 (RELINFOBITS == 32) ? 2 : 3);
                   1847:   Cell charsize = ((sizeof(Char) == 1) ? 0 :
                   1848:                   (sizeof(Char) == 2) ? 1 :
                   1849:                   (sizeof(Char) == 4) ? 2 : 3) + ausize;
                   1850:   Cell cellsize = ((sizeof(Cell) == 1) ? 0 :
                   1851:                   (sizeof(Cell) == 2) ? 1 :
                   1852:                   (sizeof(Cell) == 4) ? 2 : 3) + ausize;
1.21      anton    1853:   Cell sizebyte = (ausize << 5) + (charsize << 3) + (cellsize << 1) +
                   1854: #ifdef WORDS_BIGENDIAN
                   1855:        0
                   1856: #else
                   1857:        1
                   1858: #endif
                   1859:     ;
1.1       anton    1860: 
1.164     pazsan   1861:   vm_prims = gforth_engine(0,0,0,0,0);
1.47      anton    1862:   check_prims(vm_prims);
1.106     anton    1863:   prepare_super_table();
1.1       anton    1864: #ifndef DOUBLY_INDIRECT
1.59      anton    1865: #ifdef PRINT_SUPER_LENGTHS
                   1866:   print_super_lengths();
                   1867: #endif
1.43      anton    1868:   check_sum = checksum(vm_prims);
1.1       anton    1869: #else /* defined(DOUBLY_INDIRECT) */
1.43      anton    1870:   check_sum = (UCell)vm_prims;
1.1       anton    1871: #endif /* defined(DOUBLY_INDIRECT) */
1.155     anton    1872: #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
                   1873:   termstate = make_termstate();
                   1874: #endif /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
1.10      pazsan   1875:   
                   1876:   do {
                   1877:     if(fread(magic,sizeof(Char),8,imagefile) < 8) {
1.84      anton    1878:       fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.6) image.\n",
1.10      pazsan   1879:              progname, filename);
                   1880:       exit(1);
1.1       anton    1881:     }
1.10      pazsan   1882:     preamblesize+=8;
1.84      anton    1883:   } while(memcmp(magic,"Gforth3",7));
1.17      anton    1884:   magic7 = magic[7];
1.1       anton    1885:   if (debug) {
1.17      anton    1886:     magic[7]='\0';
1.21      anton    1887:     fprintf(stderr,"Magic found: %s ", magic);
                   1888:     print_sizes(magic7);
1.1       anton    1889:   }
                   1890: 
1.21      anton    1891:   if (magic7 != sizebyte)
                   1892:     {
                   1893:       fprintf(stderr,"This image is:         ");
                   1894:       print_sizes(magic7);
                   1895:       fprintf(stderr,"whereas the machine is ");
                   1896:       print_sizes(sizebyte);
1.1       anton    1897:       exit(-2);
                   1898:     };
                   1899: 
                   1900:   fread((void *)&header,sizeof(ImageHeader),1,imagefile);
1.10      pazsan   1901: 
                   1902:   set_stack_sizes(&header);
1.1       anton    1903:   
                   1904: #if HAVE_GETPAGESIZE
                   1905:   pagesize=getpagesize(); /* Linux/GNU libc offers this */
                   1906: #elif HAVE_SYSCONF && defined(_SC_PAGESIZE)
                   1907:   pagesize=sysconf(_SC_PAGESIZE); /* POSIX.4 */
                   1908: #elif PAGESIZE
                   1909:   pagesize=PAGESIZE; /* in limits.h according to Gallmeister's POSIX.4 book */
                   1910: #endif
1.144     pazsan   1911:   debugp(stderr,"pagesize=%ld\n",(unsigned long) pagesize);
1.1       anton    1912: 
1.34      anton    1913:   image = dict_alloc_read(imagefile, preamblesize+header.image_size,
                   1914:                          preamblesize+dictsize, data_offset);
1.33      anton    1915:   imp=image+preamblesize;
1.178     pazsan   1916: 
1.57      anton    1917:   alloc_stacks((ImageHeader *)imp);
1.1       anton    1918:   if (clear_dictionary)
1.33      anton    1919:     memset(imp+header.image_size, 0, dictsize-header.image_size);
1.90      anton    1920:   if(header.base==0 || header.base  == (Address)0x100) {
1.1       anton    1921:     Cell reloc_size=((header.image_size-1)/sizeof(Cell))/8+1;
1.162     pazsan   1922:     Char reloc_bits[reloc_size];
1.33      anton    1923:     fseek(imagefile, preamblesize+header.image_size, SEEK_SET);
1.10      pazsan   1924:     fread(reloc_bits, 1, reloc_size, imagefile);
1.161     pazsan   1925:     gforth_relocate((Cell *)imp, reloc_bits, header.image_size, (Cell)header.base, vm_prims);
1.1       anton    1926: #if 0
                   1927:     { /* let's see what the relocator did */
                   1928:       FILE *snapshot=fopen("snapshot.fi","wb");
                   1929:       fwrite(image,1,imagesize,snapshot);
                   1930:       fclose(snapshot);
                   1931:     }
                   1932: #endif
1.46      jwilke   1933:   }
                   1934:   else if(header.base!=imp) {
                   1935:     fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\n",
                   1936:            progname, (unsigned long)header.base, (unsigned long)imp);
                   1937:     exit(1);
1.1       anton    1938:   }
                   1939:   if (header.checksum==0)
                   1940:     ((ImageHeader *)imp)->checksum=check_sum;
                   1941:   else if (header.checksum != check_sum) {
                   1942:     fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\n",
                   1943:            progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
                   1944:     exit(1);
                   1945:   }
1.53      anton    1946: #ifdef DOUBLY_INDIRECT
                   1947:   ((ImageHeader *)imp)->xt_base = xts;
                   1948: #endif
1.1       anton    1949:   fclose(imagefile);
                   1950: 
1.56      anton    1951:   /* unnecessary, except maybe for CODE words */
                   1952:   /* FLUSH_ICACHE(imp, header.image_size);*/
1.1       anton    1953: 
                   1954:   return imp;
                   1955: }
1.176     pazsan   1956: #endif
1.1       anton    1957: 
1.72      anton    1958: /* pointer to last '/' or '\' in file, 0 if there is none. */
1.161     pazsan   1959: static char *onlypath(char *filename)
1.10      pazsan   1960: {
1.72      anton    1961:   return strrchr(filename, DIRSEP);
1.1       anton    1962: }
                   1963: 
1.161     pazsan   1964: static FILE *openimage(char *fullfilename)
1.10      pazsan   1965: {
                   1966:   FILE *image_file;
1.162     pazsan   1967:   char * expfilename = tilde_cstr((Char *)fullfilename, strlen(fullfilename), 1);
1.10      pazsan   1968: 
1.28      anton    1969:   image_file=fopen(expfilename,"rb");
1.1       anton    1970:   if (image_file!=NULL && debug)
1.28      anton    1971:     fprintf(stderr, "Opened image file: %s\n", expfilename);
1.10      pazsan   1972:   return image_file;
1.1       anton    1973: }
                   1974: 
1.28      anton    1975: /* try to open image file concat(path[0:len],imagename) */
1.161     pazsan   1976: static FILE *checkimage(char *path, int len, char *imagename)
1.10      pazsan   1977: {
                   1978:   int dirlen=len;
1.162     pazsan   1979:   char fullfilename[dirlen+strlen((char *)imagename)+2];
1.10      pazsan   1980: 
1.1       anton    1981:   memcpy(fullfilename, path, dirlen);
1.71      pazsan   1982:   if (fullfilename[dirlen-1]!=DIRSEP)
                   1983:     fullfilename[dirlen++]=DIRSEP;
1.1       anton    1984:   strcpy(fullfilename+dirlen,imagename);
1.10      pazsan   1985:   return openimage(fullfilename);
1.1       anton    1986: }
                   1987: 
1.161     pazsan   1988: static FILE * open_image_file(char * imagename, char * path)
1.1       anton    1989: {
1.10      pazsan   1990:   FILE * image_file=NULL;
1.28      anton    1991:   char *origpath=path;
1.10      pazsan   1992:   
1.71      pazsan   1993:   if(strchr(imagename, DIRSEP)==NULL) {
1.10      pazsan   1994:     /* first check the directory where the exe file is in !! 01may97jaw */
                   1995:     if (onlypath(progname))
1.72      anton    1996:       image_file=checkimage(progname, onlypath(progname)-progname, imagename);
1.10      pazsan   1997:     if (!image_file)
                   1998:       do {
                   1999:        char *pend=strchr(path, PATHSEP);
                   2000:        if (pend==NULL)
                   2001:          pend=path+strlen(path);
                   2002:        if (strlen(path)==0) break;
                   2003:        image_file=checkimage(path, pend-path, imagename);
                   2004:        path=pend+(*pend==PATHSEP);
                   2005:       } while (image_file==NULL);
                   2006:   } else {
                   2007:     image_file=openimage(imagename);
                   2008:   }
1.1       anton    2009: 
1.10      pazsan   2010:   if (!image_file) {
                   2011:     fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
1.28      anton    2012:            progname, imagename, origpath);
1.10      pazsan   2013:     exit(1);
1.7       anton    2014:   }
                   2015: 
1.10      pazsan   2016:   return image_file;
                   2017: }
1.11      pazsan   2018: #endif
                   2019: 
1.178     pazsan   2020: #ifdef STANDALONE_ALLOC
1.177     pazsan   2021: Address gforth_alloc(Cell size)
                   2022: {
                   2023:   Address r;
                   2024:   /* leave a little room (64B) for stack underflows */
                   2025:   if ((r = malloc(size+64))==NULL) {
                   2026:     perror(progname);
                   2027:     exit(1);
                   2028:   }
                   2029:   r = (Address)((((Cell)r)+(sizeof(Float)-1))&(-sizeof(Float)));
                   2030:   debugp(stderr, "malloc succeeds, address=$%lx\n", (long)r);
                   2031:   return r;
                   2032: }
                   2033: #endif
                   2034: 
1.11      pazsan   2035: #ifdef HAS_OS
1.161     pazsan   2036: static UCell convsize(char *s, UCell elemsize)
1.11      pazsan   2037: /* converts s of the format [0-9]+[bekMGT]? (e.g. 25k) into the number
                   2038:    of bytes.  the letter at the end indicates the unit, where e stands
                   2039:    for the element size. default is e */
                   2040: {
                   2041:   char *endp;
                   2042:   UCell n,m;
                   2043: 
                   2044:   m = elemsize;
                   2045:   n = strtoul(s,&endp,0);
                   2046:   if (endp!=NULL) {
                   2047:     if (strcmp(endp,"b")==0)
                   2048:       m=1;
                   2049:     else if (strcmp(endp,"k")==0)
                   2050:       m=1024;
                   2051:     else if (strcmp(endp,"M")==0)
                   2052:       m=1024*1024;
                   2053:     else if (strcmp(endp,"G")==0)
                   2054:       m=1024*1024*1024;
                   2055:     else if (strcmp(endp,"T")==0) {
                   2056: #if (SIZEOF_CHAR_P > 4)
1.24      anton    2057:       m=1024L*1024*1024*1024;
1.11      pazsan   2058: #else
                   2059:       fprintf(stderr,"%s: size specification \"%s\" too large for this machine\n", progname, endp);
                   2060:       exit(1);
                   2061: #endif
                   2062:     } else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
                   2063:       fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
                   2064:       exit(1);
                   2065:     }
                   2066:   }
                   2067:   return n*m;
                   2068: }
1.10      pazsan   2069: 
1.109     anton    2070: enum {
                   2071:   ss_number = 256,
1.125     anton    2072:   ss_states,
1.109     anton    2073:   ss_min_codesize,
                   2074:   ss_min_ls,
                   2075:   ss_min_lsu,
                   2076:   ss_min_nexts,
                   2077: };
                   2078: 
1.179     pazsan   2079: #ifndef STANDALONE
1.10      pazsan   2080: void gforth_args(int argc, char ** argv, char ** path, char ** imagename)
                   2081: {
                   2082:   int c;
                   2083: 
1.1       anton    2084:   opterr=0;
                   2085:   while (1) {
                   2086:     int option_index=0;
                   2087:     static struct option opts[] = {
1.29      anton    2088:       {"appl-image", required_argument, NULL, 'a'},
1.1       anton    2089:       {"image-file", required_argument, NULL, 'i'},
                   2090:       {"dictionary-size", required_argument, NULL, 'm'},
                   2091:       {"data-stack-size", required_argument, NULL, 'd'},
                   2092:       {"return-stack-size", required_argument, NULL, 'r'},
                   2093:       {"fp-stack-size", required_argument, NULL, 'f'},
                   2094:       {"locals-stack-size", required_argument, NULL, 'l'},
1.181     anton    2095:       {"vm-commit", no_argument, &map_noreserve, 0},
1.1       anton    2096:       {"path", required_argument, NULL, 'p'},
                   2097:       {"version", no_argument, NULL, 'v'},
                   2098:       {"help", no_argument, NULL, 'h'},
                   2099:       /* put something != 0 into offset_image */
                   2100:       {"offset-image", no_argument, &offset_image, 1},
                   2101:       {"no-offset-im", no_argument, &offset_image, 0},
                   2102:       {"clear-dictionary", no_argument, &clear_dictionary, 1},
1.4       anton    2103:       {"die-on-signal", no_argument, &die_on_signal, 1},
1.169     anton    2104:       {"ignore-async-signals", no_argument, &ignore_async_signals, 1},
1.1       anton    2105:       {"debug", no_argument, &debug, 1},
1.144     pazsan   2106:       {"diag", no_argument, &diag, 1},
1.60      anton    2107:       {"no-super", no_argument, &no_super, 1},
                   2108:       {"no-dynamic", no_argument, &no_dynamic, 1},
1.66      anton    2109:       {"dynamic", no_argument, &no_dynamic, 0},
1.110     anton    2110:       {"print-metrics", no_argument, &print_metrics, 1},
1.189     anton    2111:       {"print-sequences", no_argument, &print_sequences, 1},
1.109     anton    2112:       {"ss-number", required_argument, NULL, ss_number},
1.125     anton    2113:       {"ss-states", required_argument, NULL, ss_states},
1.109     anton    2114: #ifndef NO_DYNAMIC
                   2115:       {"ss-min-codesize", no_argument, NULL, ss_min_codesize},
                   2116: #endif
                   2117:       {"ss-min-ls",       no_argument, NULL, ss_min_ls},
                   2118:       {"ss-min-lsu",      no_argument, NULL, ss_min_lsu},
                   2119:       {"ss-min-nexts",    no_argument, NULL, ss_min_nexts},
1.110     anton    2120:       {"ss-greedy",       no_argument, &ss_greedy, 1},
1.158     anton    2121:       {"tpa-noequiv",     no_argument, &tpa_noequiv, 1},
                   2122:       {"tpa-noautomaton", no_argument, &tpa_noautomaton, 1},
                   2123:       {"tpa-trace",      no_argument, &tpa_trace, 1},
1.1       anton    2124:       {0,0,0,0}
                   2125:       /* no-init-file, no-rc? */
                   2126:     };
                   2127:     
1.36      pazsan   2128:     c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vhoncsx", opts, &option_index);
1.1       anton    2129:     
                   2130:     switch (c) {
1.29      anton    2131:     case EOF: return;
                   2132:     case '?': optind--; return;
                   2133:     case 'a': *imagename = optarg; return;
1.10      pazsan   2134:     case 'i': *imagename = optarg; break;
1.1       anton    2135:     case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
                   2136:     case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
                   2137:     case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
                   2138:     case 'f': fsize = convsize(optarg,sizeof(Float)); break;
                   2139:     case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
1.10      pazsan   2140:     case 'p': *path = optarg; break;
1.36      pazsan   2141:     case 'o': offset_image = 1; break;
                   2142:     case 'n': offset_image = 0; break;
                   2143:     case 'c': clear_dictionary = 1; break;
                   2144:     case 's': die_on_signal = 1; break;
                   2145:     case 'x': debug = 1; break;
1.83      anton    2146:     case 'v': fputs(PACKAGE_STRING"\n", stderr); exit(0);
1.109     anton    2147:     case ss_number: static_super_number = atoi(optarg); break;
1.125     anton    2148:     case ss_states: maxstates = max(min(atoi(optarg),MAX_STATE),1); break;
1.109     anton    2149: #ifndef NO_DYNAMIC
                   2150:     case ss_min_codesize: ss_cost = cost_codesize; break;
                   2151: #endif
                   2152:     case ss_min_ls:       ss_cost = cost_ls;       break;
                   2153:     case ss_min_lsu:      ss_cost = cost_lsu;      break;
                   2154:     case ss_min_nexts:    ss_cost = cost_nexts;    break;
1.1       anton    2155:     case 'h': 
1.29      anton    2156:       fprintf(stderr, "Usage: %s [engine options] ['--'] [image arguments]\n\
1.1       anton    2157: Engine Options:\n\
1.181     anton    2158:   --appl-image FILE                Equivalent to '--image-file=FILE --'\n\
1.10      pazsan   2159:   --clear-dictionary               Initialize the dictionary with 0 bytes\n\
                   2160:   -d SIZE, --data-stack-size=SIZE   Specify data stack size\n\
                   2161:   --debug                          Print debugging information during startup\n\
1.144     pazsan   2162:   --diag                           Print diagnostic information during startup\n\
1.181     anton    2163:   --die-on-signal                  Exit instead of THROWing some signals\n\
                   2164:   --dynamic                        Use dynamic native code\n\
1.10      pazsan   2165:   -f SIZE, --fp-stack-size=SIZE            Specify floating point stack size\n\
                   2166:   -h, --help                       Print this message and exit\n\
1.181     anton    2167:   --ignore-async-signals           Ignore instead of THROWing async. signals\n\
1.10      pazsan   2168:   -i FILE, --image-file=FILE       Use image FILE instead of `gforth.fi'\n\
                   2169:   -l SIZE, --locals-stack-size=SIZE Specify locals stack size\n\
                   2170:   -m SIZE, --dictionary-size=SIZE   Specify Forth dictionary size\n\
1.60      anton    2171:   --no-dynamic                     Use only statically compiled primitives\n\
1.10      pazsan   2172:   --no-offset-im                   Load image at normal position\n\
1.181     anton    2173:   --no-super                       No dynamically formed superinstructions\n\
1.10      pazsan   2174:   --offset-image                   Load image at a different position\n\
                   2175:   -p PATH, --path=PATH             Search path for finding image and sources\n\
1.110     anton    2176:   --print-metrics                  Print some code generation metrics on exit\n\
1.189     anton    2177:   --print-sequences                 Print primitive sequences for optimization\n\
1.10      pazsan   2178:   -r SIZE, --return-stack-size=SIZE Specify return stack size\n\
1.181     anton    2179:   --ss-greedy                      Greedy, not optimal superinst selection\n\
                   2180:   --ss-min-codesize                Select superinsts for smallest native code\n\
                   2181:   --ss-min-ls                      Minimize loads and stores\n\
                   2182:   --ss-min-lsu                     Minimize loads, stores, and pointer updates\n\
                   2183:   --ss-min-nexts                   Minimize the number of static superinsts\n\
                   2184:   --ss-number=N                            Use N static superinsts (default max)\n\
                   2185:   --ss-states=N                            N states for stack caching (default max)\n\
                   2186:   --tpa-noequiv                            Automaton without state equivalence\n\
                   2187:   --tpa-noautomaton                Dynamic programming only\n\
                   2188:   --tpa-trace                      Report new states etc.\n\
1.66      anton    2189:   -v, --version                            Print engine version and exit\n\
1.181     anton    2190:   --vm-commit                      Use OS default for memory overcommit\n\
1.1       anton    2191: SIZE arguments consist of an integer followed by a unit. The unit can be\n\
1.10      pazsan   2192:   `b' (byte), `e' (element; default), `k' (KB), `M' (MB), `G' (GB) or `T' (TB).\n",
                   2193:              argv[0]);
                   2194:       optind--;
                   2195:       return;
1.1       anton    2196:     }
                   2197:   }
1.10      pazsan   2198: }
1.11      pazsan   2199: #endif
1.179     pazsan   2200: #endif
1.10      pazsan   2201: 
1.161     pazsan   2202: static void print_diag()
1.144     pazsan   2203: {
                   2204: 
1.153     pazsan   2205: #if !defined(HAVE_GETRUSAGE) || (!defined(HAS_FFCALL) && !defined(HAS_LIBFFI))
1.145     pazsan   2206:   fprintf(stderr, "*** missing functionality ***\n"
1.144     pazsan   2207: #ifndef HAVE_GETRUSAGE
                   2208:          "    no getrusage -> CPUTIME broken\n"
                   2209: #endif
1.153     pazsan   2210: #if !defined(HAS_FFCALL) && !defined(HAS_LIBFFI)
1.144     pazsan   2211:          "    no ffcall -> only old-style foreign function calls (no fflib.fs)\n"
                   2212: #endif
                   2213:          );
                   2214: #endif
                   2215:   if((relocs < nonrelocs) ||
                   2216: #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)
                   2217:      1
                   2218: #else
                   2219:      0
                   2220: #endif
                   2221:      )
                   2222:     debugp(stderr, "relocs: %d:%d\n", relocs, nonrelocs);
1.165     pazsan   2223:     fprintf(stderr, "*** %sperformance problems ***\n%s",
                   2224: #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)
                   2225:            "",
                   2226: #else
                   2227:            "no ",
                   2228: #endif
1.144     pazsan   2229: #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)
                   2230:            "    double-cell integer type buggy ->\n        "
                   2231: #ifdef BUGGY_LL_CMP
                   2232:            "CMP, "
                   2233: #endif
                   2234: #ifdef BUGGY_LL_MUL
                   2235:            "MUL, "
                   2236: #endif
                   2237: #ifdef BUGGY_LL_DIV
                   2238:            "DIV, "
                   2239: #endif
                   2240: #ifdef BUGGY_LL_ADD
                   2241:            "ADD, "
                   2242: #endif
                   2243: #ifdef BUGGY_LL_SHIFT
                   2244:            "SHIFT, "
                   2245: #endif
                   2246: #ifdef BUGGY_LL_D2F
                   2247:            "D2F, "
                   2248: #endif
                   2249: #ifdef BUGGY_LL_F2D
                   2250:            "F2D, "
                   2251: #endif
                   2252:            "\b\b slow\n"
1.145     pazsan   2253: #endif
                   2254: #ifndef FORCE_REG
                   2255:            "    automatic register allocation: performance degradation possible\n"
                   2256: #endif
                   2257: #if !defined(FORCE_REG) || defined(BUGGY_LONG_LONG)
                   2258:            "*** Suggested remedy: try ./configure"
                   2259: #ifndef FORCE_REG
                   2260:            " --enable-force-reg"
                   2261: #endif
                   2262: #ifdef BUGGY_LONG_LONG
                   2263:            " --enable-force-ll"
                   2264: #endif
                   2265:            "\n"
1.166     pazsan   2266: #else
                   2267:            ""
1.144     pazsan   2268: #endif
                   2269:            ,
                   2270:            (relocs < nonrelocs) ? "    gcc PR 15242 -> no dynamic code generation (use gcc-2.95 instead)\n" : "");
                   2271: }
                   2272: 
1.179     pazsan   2273: #ifdef STANDALONE
                   2274: Cell data_abort_pc;
                   2275: 
                   2276: void data_abort_C(void)
                   2277: {
                   2278:   while(1) {
                   2279:   }
                   2280: }
1.10      pazsan   2281: #endif
1.67      pazsan   2282: 
1.10      pazsan   2283: int main(int argc, char **argv, char **env)
                   2284: {
1.30      pazsan   2285: #ifdef HAS_OS
1.10      pazsan   2286:   char *path = getenv("GFORTHPATH") ? : DEFAULTPATH;
1.30      pazsan   2287: #else
                   2288:   char *path = DEFAULTPATH;
                   2289: #endif
1.13      pazsan   2290: #ifndef INCLUDE_IMAGE
1.10      pazsan   2291:   char *imagename="gforth.fi";
                   2292:   FILE *image_file;
                   2293:   Address image;
                   2294: #endif
                   2295:   int retvalue;
                   2296:          
1.56      anton    2297: #if defined(i386) && defined(ALIGNMENT_CHECK)
1.10      pazsan   2298:   /* turn on alignment checks on the 486.
                   2299:    * on the 386 this should have no effect. */
                   2300:   __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
                   2301:   /* this is unusable with Linux' libc.4.6.27, because this library is
                   2302:      not alignment-clean; we would have to replace some library
                   2303:      functions (e.g., memcpy) to make it work. Also GCC doesn't try to keep
                   2304:      the stack FP-aligned. */
                   2305: #endif
                   2306: 
1.179     pazsan   2307: #ifndef STANDALONE
1.10      pazsan   2308:   /* buffering of the user output device */
1.11      pazsan   2309: #ifdef _IONBF
1.10      pazsan   2310:   if (isatty(fileno(stdout))) {
                   2311:     fflush(stdout);
                   2312:     setvbuf(stdout,NULL,_IONBF,0);
1.1       anton    2313:   }
1.11      pazsan   2314: #endif
1.180     pazsan   2315: #else
                   2316:   prep_terminal();
1.179     pazsan   2317: #endif
1.1       anton    2318: 
1.10      pazsan   2319:   progname = argv[0];
                   2320: 
1.191     anton    2321:   if (lt_dlinit()!=0) {
                   2322:     fprintf(stderr,"%s: lt_dlinit failed", progname);
                   2323:     exit(1);
                   2324:   }
1.179     pazsan   2325: #ifndef STANDALONE
1.11      pazsan   2326: #ifdef HAS_OS
1.10      pazsan   2327:   gforth_args(argc, argv, &path, &imagename);
1.109     anton    2328: #ifndef NO_DYNAMIC
1.148     anton    2329:   init_ss_cost();
1.109     anton    2330: #endif /* !defined(NO_DYNAMIC) */
                   2331: #endif /* defined(HAS_OS) */
1.179     pazsan   2332: #endif
1.10      pazsan   2333: 
1.175     pazsan   2334: #ifdef STANDALONE
                   2335:   image = gforth_engine(0, 0, 0, 0, 0);
1.10      pazsan   2336:   alloc_stacks((ImageHeader *)image);
                   2337: #else
                   2338:   image_file = open_image_file(imagename, path);
1.161     pazsan   2339:   image = gforth_loader(image_file, imagename);
1.10      pazsan   2340: #endif
1.24      anton    2341:   gforth_header=(ImageHeader *)image; /* used in SIGSEGV handler */
1.1       anton    2342: 
1.144     pazsan   2343:   if (diag)
                   2344:     print_diag();
1.1       anton    2345:   {
1.10      pazsan   2346:     char path2[strlen(path)+1];
1.1       anton    2347:     char *p1, *p2;
                   2348:     Cell environ[]= {
                   2349:       (Cell)argc-(optind-1),
                   2350:       (Cell)(argv+(optind-1)),
1.10      pazsan   2351:       (Cell)strlen(path),
1.1       anton    2352:       (Cell)path2};
                   2353:     argv[optind-1] = progname;
                   2354:     /*
                   2355:        for (i=0; i<environ[0]; i++)
                   2356:        printf("%s\n", ((char **)(environ[1]))[i]);
                   2357:        */
                   2358:     /* make path OS-independent by replacing path separators with NUL */
1.10      pazsan   2359:     for (p1=path, p2=path2; *p1!='\0'; p1++, p2++)
1.1       anton    2360:       if (*p1==PATHSEP)
                   2361:        *p2 = '\0';
                   2362:       else
                   2363:        *p2 = *p1;
                   2364:     *p2='\0';
1.161     pazsan   2365:     retvalue = gforth_go(image, 4, environ);
1.178     pazsan   2366: #if defined(SIGPIPE) && !defined(STANDALONE)
1.102     anton    2367:     bsd_signal(SIGPIPE, SIG_IGN);
                   2368: #endif
1.42      anton    2369: #ifdef VM_PROFILING
                   2370:     vm_print_profile(stderr);
                   2371: #endif
1.1       anton    2372:     deprep_terminal();
1.191     anton    2373:     if (lt_dlexit()!=0)
                   2374:       fprintf(stderr,"%s: lt_dlexit failed", progname);
1.104     anton    2375:   }
1.110     anton    2376:   if (print_metrics) {
                   2377:     int i;
                   2378:     fprintf(stderr, "code size = %8ld\n", dyncodesize());
1.177     pazsan   2379: #ifndef STANDALONE
1.110     anton    2380:     for (i=0; i<sizeof(cost_sums)/sizeof(cost_sums[0]); i++)
                   2381:       fprintf(stderr, "metric %8s: %8ld\n",
                   2382:              cost_sums[i].metricname, cost_sums[i].sum);
1.177     pazsan   2383: #endif
1.158     anton    2384:     fprintf(stderr,"lb_basic_blocks = %ld\n", lb_basic_blocks);
                   2385:     fprintf(stderr,"lb_labeler_steps = %ld\n", lb_labeler_steps);
                   2386:     fprintf(stderr,"lb_labeler_automaton = %ld\n", lb_labeler_automaton);
                   2387:     fprintf(stderr,"lb_labeler_dynprog = %ld\n", lb_labeler_dynprog);
                   2388:     fprintf(stderr,"lb_newstate_equiv = %ld\n", lb_newstate_equiv);
                   2389:     fprintf(stderr,"lb_newstate_new = %ld\n", lb_newstate_new);
                   2390:     fprintf(stderr,"lb_applicable_base_rules = %ld\n", lb_applicable_base_rules);
                   2391:     fprintf(stderr,"lb_applicable_chain_rules = %ld\n", lb_applicable_chain_rules);
                   2392:   }
                   2393:   if (tpa_trace) {
                   2394:     fprintf(stderr, "%ld %ld lb_states\n", lb_labeler_steps, lb_newstate_new);
                   2395:     fprintf(stderr, "%ld %ld lb_table_entries\n", lb_labeler_steps, lb_labeler_dynprog);
1.1       anton    2396:   }
1.13      pazsan   2397:   return retvalue;
1.1       anton    2398: }

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