File:  [gforth] / gforth / engine / main.c
Revision 1.90: download - view: text, annotated - select for diffs
Tue Jan 7 22:38:36 2003 UTC (21 years, 3 months ago) by anton
Branches: MAIN
CVS tags: HEAD
changes to eliminate gcc warnings

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

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