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

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

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