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

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

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