[gforth] / gforth / Attic / main.c  

gforth: gforth/Attic/main.c


1 : anton 1.1 /*
2 :     $Id: main.c,v 1.8 1993/11/02 13:34:38 anton Exp $
3 :     Copyright 1993 by the ANSI figForth Development Group
4 :     */
5 :    
6 :     #include <ctype.h>
7 :     #include <stdio.h>
8 :     #include <string.h>
9 :     #include <math.h>
10 :     #include <sys/types.h>
11 :     #include <sys/stat.h>
12 :     #include <fcntl.h>
13 :     #include <assert.h>
14 :     #include <stdlib.h>
15 :     #include "forth.h"
16 :    
17 :     #ifdef DIRECT_THREADED
18 :     #define CA(n) (symbols[(n)])
19 :     #else
20 :     #define CA(n) ((int)(symbols+(n)))
21 :     #endif
22 :    
23 :     /* image file format:
24 :     * size of image with stacks without tags (in bytes)
25 :     * size of image without stacks and tags (in bytes)
26 :     * size of data and FP stack (in bytes)
27 :     * pointer to start of code
28 :     * data (size in image[1])
29 :     * tags (1 bit/data cell)
30 :     *
31 :     * tag==1 mean that the corresponding word is an address;
32 :     * If the word is >=0, the address is within the image;
33 :     * addresses within the image are given relative to the start of the image.
34 :     * If the word is =-1, the address is NIL,
35 :     * If the word is between -2 and -4, it's a CFA (:, Create, Constant)
36 :     * If the word is -5, it's a DOES> CFA
37 :     * If the word is <-5, it's a primitive
38 :     */
39 :    
40 :     void relocate(int *image, char *bitstring, int size, Label symbols[])
41 :     {
42 :     int i;
43 :     static char bits[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
44 :     Label DODOES=symbols[3];
45 :    
46 :     for(i=0;i<size/sizeof(Cell);i++)
47 :     if(bitstring[i >> 3] & bits[i & 7])
48 :     if(image[i]<0)
49 :     if(image[i]==-1)
50 :     image[i]=0;
51 :     else if(image[i]>-5)
52 :     MAKE_CF(image+i,symbols[-image[i]-2]);
53 :     else if(image[i]==-5)
54 :     {
55 :     MAKE_DOES_CF(image+i,image[i+1]+((int)image));
56 :     i++; /* is this necessary? */
57 :     }
58 :     else
59 :     image[i]=(Cell)CA(-image[i]-2);
60 :     else
61 :     image[i]+=(Cell)image;
62 :     }
63 :    
64 :     int* loader(const char* filename)
65 :     { int header[2];
66 :     FILE *imagefile;
67 :     int *image;
68 :    
69 :     if(!(int)(imagefile=fopen(filename,"rb")))
70 :     {
71 :     fprintf(stderr,"Can't open image file '%s'",filename);
72 :     exit(1);
73 :     }
74 :    
75 :     fread(header,1,2*sizeof(int),imagefile);
76 :    
77 :     image=malloc(header[0]+((header[0]-1)/sizeof(Cell))/8+1);
78 :    
79 :     memset(image,0,header[0]+((header[0]-1)/sizeof(Cell))/8+1);
80 :    
81 :     image[0]=header[0];
82 :     image[1]=header[1];
83 :    
84 :     fread(image+2,1,header[1]-2*sizeof(Cell),imagefile);
85 :     fread(((void *)image)+header[0],1,((header[1]-1)/sizeof(Cell))/8+1,
86 :     imagefile);
87 :     fclose(imagefile);
88 :    
89 :     relocate(image,(char *)image+header[0],header[1],engine(0,0,0,0));
90 :    
91 :     return(image);
92 :     }
93 :    
94 :     int go_forth(int *image, int stack, Cell *entries)
95 :     {
96 :     Cell* rp=(Cell*)((void *)image+image[0]);
97 :     double* fp=(double*)((void *)rp-image[2]);
98 :     Cell* sp=(Cell*)((void *)fp-image[2]);
99 :     Cell* ip=(Cell*)(image[3]);
100 :    
101 :     for(;stack>0;stack--)
102 :     *--sp=entries[stack-1];
103 :    
104 :     install_signal_handlers(); /* right place? */
105 :    
106 :     return((int)engine(ip,sp,rp,fp));
107 :     }
108 :    
109 :     int main(int argc, char **argv, char **env)
110 :     {
111 :     char imagefile[256];
112 :     Cell environ[3] = {(Cell)argc, (Cell)argv, (Cell)env};
113 :     char* imagepath;
114 :    
115 :     if((int)(imagepath=getenv("FORTHBIN")))
116 :     {
117 :     strcpy(imagefile,imagepath);
118 :    
119 :     if(imagefile[strlen(imagefile)-1]!='/')
120 :     imagefile[strlen(imagefile)]='/';
121 :     }
122 :     else
123 :     imagefile[0]='\0';
124 :    
125 :     if(argc>1 && argv[1][0]=='@')
126 :     {
127 :     if(argv[1][1]=='/')
128 :     strcpy(imagefile,argv[1]+1);
129 :     else
130 :     strcpy(imagefile+strlen(imagefile),argv[1]+1);
131 :    
132 :     environ[0]-=1;
133 :     environ[1]+=sizeof(argv);
134 :     argv[1]=argv[0];
135 :     }
136 :     else
137 :     strcpy(imagefile+strlen(imagefile),"kernal.fi");
138 :    
139 :     exit(go_forth(loader(imagefile),3,environ));
140 :     }

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help