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

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

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