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

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

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