File:  [gforth] / gforth / engine / main.c
Revision 1.80: download - view: text, annotated - select for diffs
Thu Dec 19 23:22:19 2002 UTC (21 years, 4 months ago) by anton
Branches: MAIN
CVS tags: HEAD
"compile," now uses compile-prim1 instead of compile-prim
disablesd compile-prim

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

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