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

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

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