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

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

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