| int die_on_signal=0; |
int die_on_signal=0; |
| #ifndef INCLUDE_IMAGE |
#ifndef INCLUDE_IMAGE |
| static int clear_dictionary=0; |
static int clear_dictionary=0; |
| static size_t pagesize=0; |
UCell pagesize=1; |
| char *progname; |
char *progname; |
| #else |
#else |
| char *progname = "gforth"; |
char *progname = "gforth"; |
| int optind = 1; |
int optind = 1; |
| #endif |
#endif |
| static int debug=0; |
static int debug=0; |
| |
ImageHeader *gforth_header; |
| |
|
| /* image file format: |
/* image file format: |
| * "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.4.0 -i\n") |
* "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.4.0 -i\n") |
| * bit 0: endian, big=0, little=1. |
* bit 0: endian, big=0, little=1. |
| * The magic are always 8 octets, no matter what the native AU/character size is |
* The magic are always 8 octets, no matter what the native AU/character size is |
| * padding to max alignment (no padding necessary on current machines) |
* padding to max alignment (no padding necessary on current machines) |
| * ImageHeader structure (see below) |
* ImageHeader structure (see forth.h) |
| * data (size in ImageHeader.image_size) |
* data (size in ImageHeader.image_size) |
| * tags ((if relocatable, 1 bit/data cell) |
* tags ((if relocatable, 1 bit/data cell) |
| * |
* |
| * If the word is <CF(DOESJUMP), it's a primitive |
* If the word is <CF(DOESJUMP), it's a primitive |
| */ |
*/ |
| |
|
| typedef struct { |
|
| Address base; /* base address of image (0 if relocatable) */ |
|
| UCell checksum; /* checksum of ca's to protect against some |
|
| incompatible binary/executable combinations |
|
| (0 if relocatable) */ |
|
| UCell image_size; /* all sizes in bytes */ |
|
| UCell dict_size; |
|
| UCell data_stack_size; |
|
| UCell fp_stack_size; |
|
| UCell return_stack_size; |
|
| UCell locals_stack_size; |
|
| Xt *boot_entry; /* initial ip for booting (in BOOT) */ |
|
| Xt *throw_entry; /* ip after signal (in THROW) */ |
|
| Cell unused1; /* possibly tib stack size */ |
|
| Cell unused2; |
|
| Address data_stack_base; /* this and the following fields are initialized by the loader */ |
|
| Address fp_stack_base; |
|
| Address return_stack_base; |
|
| Address locals_stack_base; |
|
| } ImageHeader; |
|
| /* the image-header is created in main.fs */ |
|
| |
|
| void relocate(Cell *image, const char *bitstring, int size, Label symbols[]) |
void relocate(Cell *image, const char *bitstring, int size, Label symbols[]) |
| { |
{ |
| int i=0, j, k, steps=(size/sizeof(Cell))/RELINFOBITS; |
int i=0, j, k, steps=(size/sizeof(Cell))/RELINFOBITS; |
| image[i]+=(Cell)image; |
image[i]+=(Cell)image; |
| } |
} |
| } |
} |
| ((ImageHeader*)(image))->base = image; |
((ImageHeader*)(image))->base = (Address) image; |
| } |
} |
| |
|
| UCell checksum(Label symbols[]) |
UCell checksum(Label symbols[]) |
| if (r != (Address)-1) { |
if (r != (Address)-1) { |
| if (debug) |
if (debug) |
| fprintf(stderr, "success, address=$%lx\n", (long) r); |
fprintf(stderr, "success, address=$%lx\n", (long) r); |
| if (pagesize != 0) |
if (pagesize != 1) |
| next_address = (Address)(((((Cell)r)+size-1)&-pagesize)+2*pagesize); /* leave one page unmapped */ |
next_address = (Address)(((((Cell)r)+size-1)&-pagesize)+2*pagesize); /* leave one page unmapped */ |
| return r; |
return r; |
| } |
} |
| if (rp <= rp0 && rp > (Cell *)(image_header->return_stack_base+5)) { |
if (rp <= rp0 && rp > (Cell *)(image_header->return_stack_base+5)) { |
| /* no rstack overflow or underflow */ |
/* no rstack overflow or underflow */ |
| rp0 = rp; |
rp0 = rp; |
| *--rp0 = ip; |
*--rp0 = (Cell)ip; |
| } |
} |
| else /* I love non-syntactic ifdefs :-) */ |
else /* I love non-syntactic ifdefs :-) */ |
| #endif |
#endif |
| rp0 = signal_return_stack+8; |
rp0 = signal_return_stack+8; |
| |
/* fprintf(stderr, "rp=$%x\n",rp0);*/ |
| |
|
| return((int)engine(image_header->throw_entry, signal_data_stack+7, |
return((int)engine(image_header->throw_entry, signal_data_stack+7, |
| rp0, signal_fp_stack, 0)); |
rp0, signal_fp_stack, 0)); |
| return imp; |
return imp; |
| } |
} |
| |
|
| |
/* index of last '/' or '\' in file, 0 if there is none. !! Hmm, could |
| |
be implemented with strrchr and the separator should be |
| |
OS-dependent */ |
| int onlypath(char *file) |
int onlypath(char *file) |
| { |
{ |
| int i; |
int i; |
| FILE *openimage(char *fullfilename) |
FILE *openimage(char *fullfilename) |
| { |
{ |
| FILE *image_file; |
FILE *image_file; |
| |
char * expfilename = tilde_cstr(fullfilename, strlen(fullfilename), 1); |
| |
|
| image_file=fopen(fullfilename,"rb"); |
image_file=fopen(expfilename,"rb"); |
| if (image_file!=NULL && debug) |
if (image_file!=NULL && debug) |
| fprintf(stderr, "Opened image file: %s\n", fullfilename); |
fprintf(stderr, "Opened image file: %s\n", expfilename); |
| return image_file; |
return image_file; |
| } |
} |
| |
|
| |
/* try to open image file concat(path[0:len],imagename) */ |
| FILE *checkimage(char *path, int len, char *imagename) |
FILE *checkimage(char *path, int len, char *imagename) |
| { |
{ |
| int dirlen=len; |
int dirlen=len; |
| FILE * open_image_file(char * imagename, char * path) |
FILE * open_image_file(char * imagename, char * path) |
| { |
{ |
| FILE * image_file=NULL; |
FILE * image_file=NULL; |
| |
char *origpath=path; |
| |
|
| if(strchr(imagename, '/')==NULL) { |
if(strchr(imagename, '/')==NULL) { |
| /* first check the directory where the exe file is in !! 01may97jaw */ |
/* first check the directory where the exe file is in !! 01may97jaw */ |
| |
|
| if (!image_file) { |
if (!image_file) { |
| fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n", |
fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n", |
| progname, imagename, path); |
progname, imagename, origpath); |
| exit(1); |
exit(1); |
| } |
} |
| |
|
| m=1024*1024*1024; |
m=1024*1024*1024; |
| else if (strcmp(endp,"T")==0) { |
else if (strcmp(endp,"T")==0) { |
| #if (SIZEOF_CHAR_P > 4) |
#if (SIZEOF_CHAR_P > 4) |
| m=1024*1024*1024*1024; |
m=1024L*1024*1024*1024; |
| #else |
#else |
| fprintf(stderr,"%s: size specification \"%s\" too large for this machine\n", progname, endp); |
fprintf(stderr,"%s: size specification \"%s\" too large for this machine\n", progname, endp); |
| exit(1); |
exit(1); |
| while (1) { |
while (1) { |
| int option_index=0; |
int option_index=0; |
| static struct option opts[] = { |
static struct option opts[] = { |
| |
{"appl-image", required_argument, NULL, 'a'}, |
| {"image-file", required_argument, NULL, 'i'}, |
{"image-file", required_argument, NULL, 'i'}, |
| {"dictionary-size", required_argument, NULL, 'm'}, |
{"dictionary-size", required_argument, NULL, 'm'}, |
| {"data-stack-size", required_argument, NULL, 'd'}, |
{"data-stack-size", required_argument, NULL, 'd'}, |
| |
|
| c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vh", opts, &option_index); |
c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vh", opts, &option_index); |
| |
|
| if (c==EOF) |
|
| break; |
|
| if (c=='?') { |
|
| optind--; |
|
| break; |
|
| } |
|
| switch (c) { |
switch (c) { |
| |
case EOF: return; |
| |
case '?': optind--; return; |
| |
case 'a': *imagename = optarg; return; |
| case 'i': *imagename = optarg; break; |
case 'i': *imagename = optarg; break; |
| case 'm': dictsize = convsize(optarg,sizeof(Cell)); break; |
case 'm': dictsize = convsize(optarg,sizeof(Cell)); break; |
| case 'd': dsize = convsize(optarg,sizeof(Cell)); break; |
case 'd': dsize = convsize(optarg,sizeof(Cell)); break; |
| case 'p': *path = optarg; break; |
case 'p': *path = optarg; break; |
| case 'v': fprintf(stderr, "gforth %s\n", VERSION); exit(0); |
case 'v': fprintf(stderr, "gforth %s\n", VERSION); exit(0); |
| case 'h': |
case 'h': |
| fprintf(stderr, "Usage: %s [engine options] [image arguments]\n\ |
fprintf(stderr, "Usage: %s [engine options] ['--'] [image arguments]\n\ |
| Engine Options:\n\ |
Engine Options:\n\ |
| |
--appl-image FILE equivalent to '--image-file=FILE --'\n\ |
| --clear-dictionary Initialize the dictionary with 0 bytes\n\ |
--clear-dictionary Initialize the dictionary with 0 bytes\n\ |
| -d SIZE, --data-stack-size=SIZE Specify data stack size\n\ |
-d SIZE, --data-stack-size=SIZE Specify data stack size\n\ |
| --debug Print debugging information during startup\n\ |
--debug Print debugging information during startup\n\ |
| argv[0]); |
argv[0]); |
| optind--; |
optind--; |
| return; |
return; |
| exit(0); |
|
| } |
} |
| } |
} |
| } |
} |
| image_file = open_image_file(imagename, path); |
image_file = open_image_file(imagename, path); |
| image = loader(image_file, imagename); |
image = loader(image_file, imagename); |
| #endif |
#endif |
| |
gforth_header=(ImageHeader *)image; /* used in SIGSEGV handler */ |
| |
|
| { |
{ |
| char path2[strlen(path)+1]; |
char path2[strlen(path)+1]; |