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

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

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