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

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

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