Digital Research
This commit is contained in:
2020-11-06 18:50:37 +01:00
parent 621ed8ccaf
commit 31738079c4
8481 changed files with 1888323 additions and 0 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,311 @@
/*
Copyright 1982
Alcyon Corporation
8716 Production Ave.
San Diego, Ca. 92121
*/
#include "parser.h"
int bol 1;
int inittype;
int begseq;
/* int onepass; */
/*
This interfaces the Parser and the Code Generator, note that these
allow you to link together the Parser and the Code Generator.
*/
/* outbdata - set up for byte data*/
outbdata() /* returns - none*/
{
inittype = CHAR;
printf("\t.dc.b ");
}
/* outc - output a constant*/
outc(type,value) /* returns - none*/
int type;
int value;
{
if( type == CHAR )
outbdata();
else
outwdata();
printf("%d\n",value);
}
/* outwdata - set up for word data*/
outwdata() /* returns - none*/
{
inittype = INT;
printf("\t.dc.w ");
}
/* outdata - set up for data output*/
outdata() /* returns - none*/
{
inittype = INT;
printf("\t.data\n");
}
/* outldata - set up for long data output*/
outldata() /* returns - none*/
{
inittype = LONG;
printf("\t.data\n");
}
/* outfpdata - set up for floating point data output*/
outfpdata() /*[vlh] 3.4 returns - none*/
{
inittype = FLOAT;
printf("\t.data\n");
}
/* outbentry - outputs block/function entry code*/
outbentry(nlocs,nds,nas) /* returns - none*/
int nlocs; /* local size*/
int nds; /* number of D registers*/
int nas; /* number of A registers*/
{
if( !nds && !nas ) /* adjust for 1 arg*/
nlocs =+ 4;
printf("\tlink R14,#%d\n",-nlocs);
if( nds || nas ) {
printf("\tmovem.l R%d-R7",7-nds); /*7 for one arg*/
if( nas ) {
putchar('/');
printf("R%d-R13",14-nas);
}
printf(",-(sp)\n");
}
}
/* outbexit - output function exit code*/
outbexit(nds,nas) /* returns - none*/
int nds; /* number of D registers*/
int nas; /* number of A registers*/
{
if( nds || nas ) {
printf("\ttst.l (sp)+\n\tmovem.l (sp)+,"); /*1 arg stuff*/
if( nds ) {
printf("R%d-R7",8-nds);
if( nas )
putchar('/');
}
if( nas )
printf("R%d-R13",14-nas);
putchar('\n');
}
printf("\tunlk R14\n\trts\n");
}
/* outlocal - output local symbol for debugger*/
outlocal(type,sc,sym,val)
int type; /* local name type*/
int sc; /* storage type*/
char *sym; /* symbol name*/
int val;
{
switch( sc ) {
case STATIC:
if( notfunction(type) )
printf("\t~%.8s=L%d\n",sym,val);
break;
case REGISTER:
printf("\t~%.8s=R%d\n",sym,val);
break;
case AUTO:
printf("\t~%.8s=%d\n",sym,val);
break;
}
}
/* outswitch - output switch table info*/
outswitch(ncases,deflab,sp) /* returns - none*/
int ncases; /* number of cases in switch*/
int deflab; /* default label*/
struct swtch *sp; /* switch table pointer*/
{
register int vdif, val, hval, i, tlab;
register struct swtch *s;
val = sp->sw_value;
hval = sp[ncases-1].sw_value;
vdif = hval - val;
if( ncases <= 4 ) {
/*
*simple switch, do compares and brances, followed by branch to default
*/
for( s = sp; --ncases >= 0; s++ ) {
if( !s->sw_value )
printf("\ttst R0\n");
else
printf("\tcmp #%d,R0\n",s->sw_value);
printf("\tbeq L%d\n",s->sw_label);
}
outgoto(deflab);
}
else if( vdif > 0 && vdif <= ncases*3 ) {
/*jump switch, uses value in R0 to index into table of labels*/
if( val )
printf("\tsub #%d,R0\n",val);
tlab = nextlabel++;
printf("\tcmp #%d,R0\n\tbhi L%d\n",vdif,deflab); /*check for max*/
printf("\tasl #2,R0\n\tmove R0,R8\n\tadd.l #L%d,R8\n",tlab);
printf("\tmove.l (R8),R8\n\tjmp (R8)\n");
outdata();
outlab(tlab);
for( s = sp; val <= hval; val++ ) {
if( val == s->sw_value ) {
outclab(s->sw_label);
s++;
}
else
outclab(deflab);
}
outtext();
}
else {
/*
* direct switch, searches down table of values for match, if match
* found, branches to corresponding label in label table.
*/
tlab = nextlabel++;
printf("\text.l R0\n\tmove.l #L%d,R8\n\tmove #%d,R1\n",tlab,ncases);
i = nextlabel++;
outlab(i); /*loop label*/
printf("\tcmp.l (R8)+,R0\n\tdbeq R1,L%d\n",i);
printf("\tmove.l %d(R8),R8\n\tjmp (R8)\n",ncases*4);
outdata();
outlab(tlab);
for( s = sp, i = ncases; --i >= 0; s++ )
outlcon(s->sw_value);
outlcon(0); /* mark for default label*/
for( s = sp, i = ncases; --i >= 0; s++ )
outclab(s->sw_label);
outclab(deflab);
outtext();
}
}
outeof()
{
register int c;
v6flush(&sbuf);
v6flush(&obuf);
}
/* copysfile - copy string file to end of output file*/
copysfile(fname)
char *fname;
{
register int c;
close(sbuf.io_fd);
if( fopen(fname,&sbuf,0) < 0 ) /* 3rd arg for versados */
ferror("can't copy %s",fname);
while( (c=getc(&sbuf)) > 0 )
putc(c,&obuf);
v6flush(&obuf);
}
/* outword - output a word of data*/
outword(w) /* word expression*/
int w;
{
if( begseq )
putchar(',');
begseq++;
printf("%d",w);
}
/* outlong - output a long data*/
outlong(l) /* returns - none*/
long l; /* long data to output*/
{
outwdata();
outword(l.hiword);
outword(l.loword);
outendseq();
}
/* outfp - output floating point data*/
outfp(l) /*[vlh] 3.4 returns - none*/
long l; /* floating point data to output*/
{
outwdata();
outword(l.hiword);
outword(l.loword);
outendseq();
}
outendseq() /* returns - none*/
{
begseq = 0;
putchar('\n');
}
/*
* outtstr - output text string
* This outputs a string to the string file, this is used wherever
* you cannot output the string directly to data space, such as in
* the middle of expressions.
*/
outtstr(lab)
int lab;
{
char *savep;
int sbol;
savep = obp; /*save to restore later...*/
obp = &sbuf;
sbol = bol;
bol = 1;
printf("\tL%d:",lab);
outstr();
obp = savep;
bol = sbol;
}
/* outstr - output a string as a sequence of bytes*/
/* Outputs ".dc.b <byte1>,<byte2>,...,<0>*/
outstr()
{
register char *s;
register int i;
outbdata();
for( s = cstr, i = cstrsize; i > 0; i-- )
outword(*s++ & 0xff);
outendseq();
}
/*
* putchar - handle outputting to intermediate or error files
* This catches tabs to allow for the integration of the parser
* and code generator into one pass. By merely throwing away the
* tabs here, the output will be OK for the assembler.
*/
putchar(c)
char c;
{
if( !obp )
write(1,&c,1);
else if( c == '\t' ) {
if( bol ) /* not used && !onepass ) */
putc('(',obp); /*for code generator*/
}
else {
bol = (c == '\n');
putc(c,obp);
}
}

View File

@@ -0,0 +1,251 @@
/*
Copyright 1982
Alcyon Corporation
8716 Production Ave.
San Diego, Ca. 92121
*/
#include "machine.h"
/*
* intermediate code operators
* 0=>EOF, special operator
*/
#define EOF 0
/*1-59=>operators that generate code (entries in code gen optab)*/
#define ADD 1
#define SUB 2
#define MULT 3
#define DIV 4
#define MOD 5
#define RSH 6
#define LSH 7
#define AND 8
#define OR 9
#define XOR 10
#define NOT 11
#define UMINUS 12
#define COMPL 13
#define PREDEC 14
#define PREINC 15
#define POSTDEC 16
#define POSTINC 17
#define ASSIGN 18
#define EQADD 19
#define EQSUB 20
#define EQMULT 21
#define EQDIV 22
#define EQMOD 23
#define EQRSH 24
#define EQLSH 25
#define EQAND 26
#define EQOR 27
#define EQXOR 28
#define FJSR 29
#define EQUALS 30
#define NEQUALS 31
#define GREAT 32
#define GREATEQ 33
#define LESS 34
#define LESSEQ 35
#define INT2L 36
#define LONG2I 37
/*machine dependent operators that generate code*/
#define BTST 38
#define LOAD 39
#define LMULT 40
#define LDIV 41
#define LMOD 42
#define LEQMULT 43
#define LEQDIV 44
#define LEQMOD 45
#define EQADDR 46
#define EQNOT 47
#define EQNEG 48
#define DOCAST 49
#define STASSIGN 50 /*[vlh]*/
#define LONG2F 51 /*[vlh] 3.4*/
#define FLOAT2L 52 /*[vlh] 3.4*/
#define INT2F 53 /*[vlh] 3.4*/
#define FLOAT2I 54 /*[vlh] 3.4*/
#define LCGENOP 55 /*change if adding more operators...*/
/*intermediate code operators that do not generate code*/
#define ADDR 60
#define INDR 61
#define LAND 62
#define LOR 63
#define QMARK 64
#define COLON 65
#define COMMA 66
#define CINT 67
#define CLONG 68
#define SYMBOL 69
#define AUTOINC 70
#define AUTODEC 71
#define CALL 72
#define NACALL 73
#define BFIELD 74
#define IFGOTO 75
#define INIT 76
#define CFORREG 77
#define DCLONG 78
#define CFLOAT 79 /*[vlh] 3.4*/
/*operators local to parser*/
#define CAST 80
#define SEMI 81
#define LCURBR 82
#define RCURBR 83
#define LBRACK 84
#define RBRACK 85
#define LPAREN 86
#define RPAREN 87
#define STRING 88
#define RESWORD 89
#define APTR 90
#define PERIOD 91
#define SIZEOF 92
#define MPARENS 93
#define FRETURN 94
#define STACKEND 100
/*data types*/
#define TYPELESS 0
#define CHAR 1
#define SHORT 2
#define INT 3
#define LONG 4
#define UCHAR 5
#define USHORT 6
#define UNSIGNED 7
#define ULONG 8
#define FLOAT 9
#define DOUBLE 10
/*data types local to parser*/
#define STRUCT 11
#define FRSTRUCT 12
#define LLABEL 13
/*type flags and definitions*/
#define TYPE 017
#define SUPTYP 060
#define ALLTYPE 077
#define POINTER 020
#define FUNCTION 040
#define ARRAY 060
#define SUTYPLEN 2
/*data registers*/
#define DREG0 0
#define DREG2 2
#define DREG3 3
#define DREG4 4
#define DREG5 5
#define DREG6 6
#define DREG7 7
#define AREG3 11
#define AREG4 12
#define AREG5 13
/*storage classes*/
#define AUTO 1
#define REGISTER 2
#define EXTERNAL 3
#define STATIC 4
#define REGOFF 5
#define EXTOFF 6
#define STATOFF 7
#define INDEXED 8
/*exclusively code generator storage classes*/
#define CINDR 9
#define CLINDR 10
#define CFINDR 11 /* [vlh] 3.4 */
/*exclusively parser storage classes*/
#define STRPROTO 9
#define PDECLIST 10
#define PARMLIST 11
#define BFIELDCL 12
#define UNELCL 13
#define STELCL 14
/*opinfo table bits*/
#define OPPRI 077
#define OPBIN 0100
#define OPLVAL 0200
#define OPREL 0400
#define OPASSIGN 01000
#define OPLWORD 02000
#define OPRWORD 04000
#define OPCOM 010000
#define OPRAS 020000
#define OPTERM 040000
#define OPCONVS 0100000
/*68000 definitions*/
#define PTRSIZE 4
#define INTSIZE 2
#define LONGSIZE 4
#define SSIZE 8 /* chars per symbol */
#define TRUE 1
#define FALSE 0
#define TABC '\t' /* tab character */
#define EOLC '\n' /* end of line character */
#define BITSPBYTE 8
/*operator class priorities*/
#define TRMPRI 0 /* terminal nodes */
#define RPNPRI 1 /* ) and ] */
#define CALPRI 2 /* in-stack call, ( or [ */
#define COLPRI 3 /* init or case priority for : or , */
#define STKPRI 4 /* priority of end of stack */
#define COMPRI 5 /* normal priority for , */
#define ASGPRI 6 /* =, +=, -=, *=, /=, %=, ... */
#define QMKPRI 7 /* ?: */
#define LORPRI 8 /* || */
#define LNDPRI 9 /* && */
#define ORPRI 10 /* |, ! */
#define ANDPRI 11 /* & */
#define EQLPRI 12 /* ==, != */
#define RELPRI 13 /* >, <, >=, <= */
#define SHFPRI 14 /* <<, >> */
#define ADDPRI 15 /* +, - */
#define MULPRI 16 /* *, /, % */
#define UNOPRI 17 /* ++, --, &, *, -, ~, sizeof */
#define LPNPRI 18 /* ., ->, [, (, function call */
#define PSTPRI 19 /* in-stack post--, post++ */
struct io_buf {
int io_fd;
int io_nc;
char *io_p;
char io_b[512];
};
#ifdef PDP11
struct { short hiword; short loword; };
#endif
#ifdef MC68000
struct { short hiword; short loword; };
#endif
#ifdef VAX
struct { short loword; short hiword; };
#endif
#define EXPSIZE 1024
int exprarea[EXPSIZE];
/* v6io buffer declaration */
#define BLEN 512
struct iobuf{
int fildes;
int nunused;
char *xfree;
char buff[BLEN];
};

View File

@@ -0,0 +1,317 @@
1File: ICODE.C Page 1
1 /*
2 Copyright 1982
3 Alcyon Corporation
4 8716 Production Ave.
5 San Diego, Ca. 92121
6 */
7
8 #include "parser.h"
9
10 int bol 1;
11 int inittype;
12 int begseq;
13 /* int onepass; */
14
15 /*
16 This interfaces the Parser and the Code Generator, note that these
17 allow you to link together the Parser and the Code Generator.
18 */
19
20 /* outbdata - set up for byte data*/
21 outbdata() /* returns - none*/
22 {
23 inittype = CHAR;
24 printf("\t.dc.b ");
25 }
26
27 /* outc - output a constant*/
28 outc(type,value) /* returns - none*/
29 int type;
30 int value;
31 {
32 if( type == CHAR )
33 outbdata();
34 else
35 outwdata();
36 printf("%d\n",value);
37 }
38
39 /* outwdata - set up for word data*/
40 outwdata() /* returns - none*/
41 {
42 inittype = INT;
43 printf("\t.dc.w ");
44 }
45
46 /* outdata - set up for data output*/
47 outdata() /* returns - none*/
48 {
49 inittype = INT;
50 printf("\t.data\n");
51 }
52
53 /* outldata - set up for long data output*/
54 outldata() /* returns - none*/
55 {
56 inittype = LONG;
57 printf("\t.data\n");
58 }
59
1File: ICODE.C Page 2
60 /* outfpdata - set up for floating point data output*/
61 outfpdata() /*[vlh] 3.4 returns - none*/
62 {
63 inittype = FLOAT;
64 printf("\t.data\n");
65 }
66
67 /* outbentry - outputs block/function entry code*/
68 outbentry(nlocs,nds,nas) /* returns - none*/
69 int nlocs; /* local size*/
70 int nds; /* number of D registers*/
71 int nas; /* number of A registers*/
72 {
73 if( !nds && !nas ) /* adjust for 1 arg*/
74 nlocs =+ 4;
75 printf("\tlink R14,#%d\n",-nlocs);
76 if( nds || nas ) {
77 printf("\tmovem.l R%d-R7",7-nds); /*7 for one arg*/
78 if( nas ) {
79 putchar('/');
80 printf("R%d-R13",14-nas);
81 }
82 printf(",-(sp)\n");
83 }
84 }
85
86 /* outbexit - output function exit code*/
87 outbexit(nds,nas) /* returns - none*/
88 int nds; /* number of D registers*/
89 int nas; /* number of A registers*/
90 {
91 if( nds || nas ) {
92 printf("\ttst.l (sp)+\n\tmovem.l (sp)+,"); /*1 arg stuff*/
93 if( nds ) {
94 printf("R%d-R7",8-nds);
95 if( nas )
96 putchar('/');
97 }
98 if( nas )
99 printf("R%d-R13",14-nas);
100 putchar('\n');
101 }
102 printf("\tunlk R14\n\trts\n");
103 }
104
105 /* outlocal - output local symbol for debugger*/
106 outlocal(type,sc,sym,val)
107 int type; /* local name type*/
108 int sc; /* storage type*/
109 char *sym; /* symbol name*/
110 int val;
111 {
112 switch( sc ) {
113
114 case STATIC:
115 if( notfunction(type) )
116 printf("\t~%.8s=L%d\n",sym,val);
117 break;
118
1File: ICODE.C Page 3
119 case REGISTER:
120 printf("\t~%.8s=R%d\n",sym,val);
121 break;
122
123 case AUTO:
124 printf("\t~%.8s=%d\n",sym,val);
125 break;
126 }
127 }
128
129 /* outswitch - output switch table info*/
130 outswitch(ncases,deflab,sp) /* returns - none*/
131 int ncases; /* number of cases in switch*/
132 int deflab; /* default label*/
133 struct swtch *sp; /* switch table pointer*/
134 {
135 register int vdif, val, hval, i, tlab;
136 register struct swtch *s;
137
138 val = sp->sw_value;
139 hval = sp[ncases-1].sw_value;
140 vdif = hval - val;
141 if( ncases <= 4 ) {
142 /*
143 *simple switch, do compares and brances, followed by branch to default
144 */
145 for( s = sp; --ncases >= 0; s++ ) {
146 if( !s->sw_value )
147 printf("\ttst R0\n");
148 else
149 printf("\tcmp #%d,R0\n",s->sw_value);
150 printf("\tbeq L%d\n",s->sw_label);
151 }
152 outgoto(deflab);
153 }
154 else if( vdif > 0 && vdif <= ncases*3 ) {
155
156 /*jump switch, uses value in R0 to index into table of labels*/
157
158 if( val )
159 printf("\tsub #%d,R0\n",val);
160 tlab = nextlabel++;
161 printf("\tcmp #%d,R0\n\tbhi L%d\n",vdif,deflab); /*check for max*/
162 printf("\tasl #2,R0\n\tmove R0,R8\n\tadd.l #L%d,R8\n",tlab);
163 printf("\tmove.l (R8),R8\n\tjmp (R8)\n");
164 outdata();
165 outlab(tlab);
166 for( s = sp; val <= hval; val++ ) {
167 if( val == s->sw_value ) {
168 outclab(s->sw_label);
169 s++;
170 }
171 else
172 outclab(deflab);
173 }
174 outtext();
175 }
176 else {
177 /*
1File: ICODE.C Page 4
178 * direct switch, searches down table of values for match, if match
179 * found, branches to corresponding label in label table.
180 */
181 tlab = nextlabel++;
182 printf("\text.l R0\n\tmove.l #L%d,R8\n\tmove #%d,R1\n",tlab,ncases);
183 i = nextlabel++;
184 outlab(i); /*loop label*/
185 printf("\tcmp.l (R8)+,R0\n\tdbeq R1,L%d\n",i);
186 printf("\tmove.l %d(R8),R8\n\tjmp (R8)\n",ncases*4);
187 outdata();
188 outlab(tlab);
189 for( s = sp, i = ncases; --i >= 0; s++ )
190 outlcon(s->sw_value);
191 outlcon(0); /* mark for default label*/
192 for( s = sp, i = ncases; --i >= 0; s++ )
193 outclab(s->sw_label);
194 outclab(deflab);
195 outtext();
196 }
197 }
198
199 outeof()
200 {
201 register int c;
202
203 v6flush(&sbuf);
204 v6flush(&obuf);
205 }
206
207 /* copysfile - copy string file to end of output file*/
208 copysfile(fname)
209 char *fname;
210 {
211 register int c;
212
213 close(sbuf.io_fd);
214 if( fopen(fname,&sbuf,0) < 0 ) /* 3rd arg for versados */
215 ferror("can't copy %s",fname);
216 while( (c=getc(&sbuf)) > 0 )
217 putc(c,&obuf);
218 v6flush(&obuf);
219 }
220
221 /* outword - output a word of data*/
222 outword(w) /* word expression*/
223 int w;
224 {
225 if( begseq )
226 putchar(',');
227 begseq++;
228 printf("%d",w);
229 }
230
231 /* outlong - output a long data*/
232 outlong(l) /* returns - none*/
233 long l; /* long data to output*/
234 {
235 outwdata();
236 outword(l.hiword);
1File: ICODE.C Page 5
237 outword(l.loword);
238 outendseq();
239 }
240
241 /* outfp - output floating point data*/
242 outfp(l) /*[vlh] 3.4 returns - none*/
243 long l; /* floating point data to output*/
244 {
245 outwdata();
246 outword(l.hiword);
247 outword(l.loword);
248 outendseq();
249 }
250
251 outendseq() /* returns - none*/
252 {
253 begseq = 0;
254 putchar('\n');
255 }
256
257 /*
258 * outtstr - output text string
259 * This outputs a string to the string file, this is used wherever
260 * you cannot output the string directly to data space, such as in
261 * the middle of expressions.
262 */
263 outtstr(lab)
264 int lab;
265 {
266 char *savep;
267 int sbol;
268
269 savep = obp; /*save to restore later...*/
270 obp = &sbuf;
271 sbol = bol;
272 bol = 1;
273 printf("\tL%d:",lab);
274 outstr();
275 obp = savep;
276 bol = sbol;
277 }
278
279 /* outstr - output a string as a sequence of bytes*/
280 /* Outputs ".dc.b <byte1>,<byte2>,...,<0>*/
281 outstr()
282 {
283 register char *s;
284 register int i;
285
286 outbdata();
287 for( s = cstr, i = cstrsize; i > 0; i-- )
288 outword(*s++ & 0xff);
289 outendseq();
290 }
291
292 /*
293 * putchar - handle outputting to intermediate or error files
294 * This catches tabs to allow for the integration of the parser
295 * and code generator into one pass. By merely throwing away the
1File: ICODE.C Page 6
296 * tabs here, the output will be OK for the assembler.
297 */
298 putchar(c)
299 char c;
300 {
301 if( !obp )
302 write(1,&c,1);
303 else if( c == '\t' ) {
304 if( bol ) /* not used && !onepass ) */
305 putc('(',obp); /*for code generator*/
306 }
307 else {
308 bol = (c == '\n');
309 putc(c,obp);
310 }
311 }

View File

@@ -0,0 +1,256 @@
1File: ICODE.H Page 1
1 /*
2 Copyright 1982
3 Alcyon Corporation
4 8716 Production Ave.
5 San Diego, Ca. 92121
6 */
7 #include "machine.h"
8 /*
9 * intermediate code operators
10 * 0=>EOF, special operator
11 */
12 #define EOF 0
13
14 /*1-59=>operators that generate code (entries in code gen optab)*/
15 #define ADD 1
16 #define SUB 2
17 #define MULT 3
18 #define DIV 4
19 #define MOD 5
20 #define RSH 6
21 #define LSH 7
22 #define AND 8
23 #define OR 9
24 #define XOR 10
25 #define NOT 11
26 #define UMINUS 12
27 #define COMPL 13
28 #define PREDEC 14
29 #define PREINC 15
30 #define POSTDEC 16
31 #define POSTINC 17
32 #define ASSIGN 18
33 #define EQADD 19
34 #define EQSUB 20
35 #define EQMULT 21
36 #define EQDIV 22
37 #define EQMOD 23
38 #define EQRSH 24
39 #define EQLSH 25
40 #define EQAND 26
41 #define EQOR 27
42 #define EQXOR 28
43 #define FJSR 29
44 #define EQUALS 30
45 #define NEQUALS 31
46 #define GREAT 32
47 #define GREATEQ 33
48 #define LESS 34
49 #define LESSEQ 35
50 #define INT2L 36
51 #define LONG2I 37
52
53 /*machine dependent operators that generate code*/
54 #define BTST 38
55 #define LOAD 39
56 #define LMULT 40
57 #define LDIV 41
58 #define LMOD 42
59 #define LEQMULT 43
1File: ICODE.H Page 2
60 #define LEQDIV 44
61 #define LEQMOD 45
62 #define EQADDR 46
63 #define EQNOT 47
64 #define EQNEG 48
65 #define DOCAST 49
66
67 #define STASSIGN 50 /*[vlh]*/
68 #define LONG2F 51 /*[vlh] 3.4*/
69 #define FLOAT2L 52 /*[vlh] 3.4*/
70 #define INT2F 53 /*[vlh] 3.4*/
71 #define FLOAT2I 54 /*[vlh] 3.4*/
72 #define LCGENOP 55 /*change if adding more operators...*/
73
74 /*intermediate code operators that do not generate code*/
75 #define ADDR 60
76 #define INDR 61
77 #define LAND 62
78 #define LOR 63
79 #define QMARK 64
80 #define COLON 65
81 #define COMMA 66
82 #define CINT 67
83 #define CLONG 68
84 #define SYMBOL 69
85 #define AUTOINC 70
86 #define AUTODEC 71
87 #define CALL 72
88 #define NACALL 73
89 #define BFIELD 74
90 #define IFGOTO 75
91 #define INIT 76
92 #define CFORREG 77
93 #define DCLONG 78
94 #define CFLOAT 79 /*[vlh] 3.4*/
95
96 /*operators local to parser*/
97 #define CAST 80
98 #define SEMI 81
99 #define LCURBR 82
100 #define RCURBR 83
101 #define LBRACK 84
102 #define RBRACK 85
103 #define LPAREN 86
104 #define RPAREN 87
105 #define STRING 88
106 #define RESWORD 89
107 #define APTR 90
108 #define PERIOD 91
109 #define SIZEOF 92
110 #define MPARENS 93
111 #define FRETURN 94
112 #define STACKEND 100
113
114 /*data types*/
115 #define TYPELESS 0
116 #define CHAR 1
117 #define SHORT 2
118 #define INT 3
1File: ICODE.H Page 3
119 #define LONG 4
120 #define UCHAR 5
121 #define USHORT 6
122 #define UNSIGNED 7
123 #define ULONG 8
124 #define FLOAT 9
125 #define DOUBLE 10
126
127 /*data types local to parser*/
128 #define STRUCT 11
129 #define FRSTRUCT 12
130 #define LLABEL 13
131
132 /*type flags and definitions*/
133 #define TYPE 017
134 #define SUPTYP 060
135 #define ALLTYPE 077
136 #define POINTER 020
137 #define FUNCTION 040
138 #define ARRAY 060
139 #define SUTYPLEN 2
140
141 /*data registers*/
142 #define DREG0 0
143 #define DREG2 2
144 #define DREG3 3
145 #define DREG4 4
146 #define DREG5 5
147 #define DREG6 6
148 #define DREG7 7
149 #define AREG3 11
150 #define AREG4 12
151 #define AREG5 13
152
153 /*storage classes*/
154 #define AUTO 1
155 #define REGISTER 2
156 #define EXTERNAL 3
157 #define STATIC 4
158 #define REGOFF 5
159 #define EXTOFF 6
160 #define STATOFF 7
161 #define INDEXED 8
162
163 /*exclusively code generator storage classes*/
164 #define CINDR 9
165 #define CLINDR 10
166 #define CFINDR 11 /* [vlh] 3.4 */
167
168 /*exclusively parser storage classes*/
169 #define STRPROTO 9
170 #define PDECLIST 10
171 #define PARMLIST 11
172 #define BFIELDCL 12
173 #define UNELCL 13
174 #define STELCL 14
175
176
177 /*opinfo table bits*/
1File: ICODE.H Page 4
178 #define OPPRI 077
179 #define OPBIN 0100
180 #define OPLVAL 0200
181 #define OPREL 0400
182 #define OPASSIGN 01000
183 #define OPLWORD 02000
184 #define OPRWORD 04000
185 #define OPCOM 010000
186 #define OPRAS 020000
187 #define OPTERM 040000
188 #define OPCONVS 0100000
189
190 /*68000 definitions*/
191 #define PTRSIZE 4
192 #define INTSIZE 2
193 #define LONGSIZE 4
194 #define SSIZE 8 /* chars per symbol */
195 #define TRUE 1
196 #define FALSE 0
197 #define TABC '\t' /* tab character */
198 #define EOLC '\n' /* end of line character */
199 #define BITSPBYTE 8
200
201 /*operator class priorities*/
202 #define TRMPRI 0 /* terminal nodes */
203 #define RPNPRI 1 /* ) and ] */
204 #define CALPRI 2 /* in-stack call, ( or [ */
205 #define COLPRI 3 /* init or case priority for : or , */
206 #define STKPRI 4 /* priority of end of stack */
207 #define COMPRI 5 /* normal priority for , */
208 #define ASGPRI 6 /* =, +=, -=, *=, /=, %=, ... */
209 #define QMKPRI 7 /* ?: */
210 #define LORPRI 8 /* || */
211 #define LNDPRI 9 /* && */
212 #define ORPRI 10 /* |, ! */
213 #define ANDPRI 11 /* & */
214 #define EQLPRI 12 /* ==, != */
215 #define RELPRI 13 /* >, <, >=, <= */
216 #define SHFPRI 14 /* <<, >> */
217 #define ADDPRI 15 /* +, - */
218 #define MULPRI 16 /* *, /, % */
219 #define UNOPRI 17 /* ++, --, &, *, -, ~, sizeof */
220 #define LPNPRI 18 /* ., ->, [, (, function call */
221 #define PSTPRI 19 /* in-stack post--, post++ */
222
223 struct io_buf {
224 int io_fd;
225 int io_nc;
226 char *io_p;
227 char io_b[512];
228 };
229
230 #ifdef PDP11
231 struct { short hiword; short loword; };
232 #endif
233 #ifdef MC68000
234 struct { short hiword; short loword; };
235 #endif
236 #ifdef VAX
1File: ICODE.H Page 5
237 struct { short loword; short hiword; };
238 #endif
239
240 #define EXPSIZE 1024
241 int exprarea[EXPSIZE];
242
243 /* v6io buffer declaration */
244 #define BLEN 512
245
246 struct iobuf{
247 int fildes;
248 int nunused;
249 char *xfree;
250 char buff[BLEN];
251 };

View File

@@ -0,0 +1,323 @@
/*
Copyright 1982
Alcyon Corporation
8716 Production Ave.
San Diego, Ca. 92121
*/
/*
C68 Parser - include file
*/
#include "icode.h"
/*symbol attribute fields*/
#define SRESWORD 001 /*is symbol a reserved word?*/
#define SGLOBAL 002 /*is symbol global?*/
#define STYPEDEF 004 /*typedef declaration?*/
#define SDEFINED 010 /*symbol defined?*/
/*reserved words*/
#define R_AUTO 1
#define R_BREAK 2
#define R_CASE 3
#define R_CHAR 4
#define R_CONTINUE 5
#define R_DO 6
#define R_DEFAULT 7
#define R_DOUBLE 8
#define R_GOTO 9
#define R_ELSE 10
#define R_EXTERNAL 11
#define R_FLOAT 12
#define R_FOR 13
#define R_IF 14
#define R_INT 15
#define R_LONG 16
#define R_REGISTER 17
#define R_RETURN 18
#define R_SHORT 19
#define R_SIZEOF 20
#define R_STATIC 21
#define R_STRUCT 22
#define R_SWITCH 23
#define R_TYPEDEF 24
#define R_UNION 25
#define R_UNSIGNED 26
#define R_WHILE 27
/*
* mixed-mode conversions, entries in 2-d array indexed by:
* (int,unsn,long,doub,ptr)
*/
#define INT_CHAR 1
#define UNSN_CHAR 1
#define LONG_CHAR 1
#define DOUB_CHAR 1
#define PTR_CHAR 1
#define INT_UNSN 0 /*no conversion is generated*/
#define INT_LONG 2
#define INT_DOUB 3
#define INT_PTR 4
#define UNSN_INT 0 /*no conversion is generated*/
#define UNSN_LONG 6
#define UNSN_DOUB 7
#define UNSN_PTR 8
#define LONG_INT 9
#define LONG_UNSN 10
#define LONG_DOUB 11
#define LONG_PTR 12
#define DOUB_INT 13
#define DOUB_UNSN 14
#define DOUB_LONG 15
#define PTR_INT 16
#define PTR_UNSN 17
#define PTR_LONG 18
#define PTR_PTR 19
#define BADCONV 20
/* miscellaneous constants */
#define OPSSIZE 40 /*operator stack size*/
#define OPDSIZE 80 /*operand stack size*/
#define HSIZE 517 /*hash table size, 3.4 made prime */
#define SYMSIZE 1024 /*size to alloc for symbol structures*/
#define SWSIZE 256 /*max no. of cases in a switch*/
#define DSIZE 1000 /*dimension table size*/
#define BITSPWORD 16 /*bits per word*/
#define AREGLO 010 /*A reg flag*/
#define HICREG 2 /*highest reg # used for code generation*/
#define BITSPCHAR 8 /*bits per char*/
#define CHRSPWORD 2 /*chars per word*/
#define STRSIZE 300 /*max string length*/
#define NFARGS 40 /*max no. of args to function*/
#define NFRSTR 20 /*max no. of forward ref struct proto*/
/*symbol table node*/
struct symbol {
char s_attrib; /* defined, resword, global, typedef */
char s_sc; /* auto, static, external, register */
int s_type; /* 4bits specified, 2 bit fields for ptr... */
int s_dp; /* index into dimension table */
int s_ssp; /* dimension table/function arg table */
int s_offset; /* offset inside of structure */
char s_symbol[SSIZE]; /* symbol identifier, to SSIZE chars */
struct symbol *s_struc; /* if struct, ptr to parent (sys III) */
struct symbol *s_next; /* next symbol table entry */
};
/*expression tree operator node*/
struct tnode {
int t_op;
int t_type;
int t_dp;
int t_ssp;
struct tnode *t_left;
struct tnode *t_right;
};
/*expression tree node for symbol - only keeps location*/
struct symnode {
int t_op;
int t_type; /*data type of symbol*/
int t_dp; /*dimension pointer of symbol*/
int t_ssp; /*structure size index to dtab*/
int t_sc; /*storage class of symbol*/
int t_offset; /*offset of symbol*/
int t_label;
};
/*expressioon tree node for external symbol - need to keep name*/
struct extnode {
int t_op;
int t_type;
int t_dp;
int t_ssp;
int t_sc;
int t_offset;
int t_reg;
int t_symbol[SSIZE]; /*symbol name*/
};
/*expression tree node for integer constant*/
struct conode {
int t_op;
int t_type;
int t_dp;
int t_ssp;
int t_value; /*constant value*/
};
struct lconode {
int t_op;
int t_type;
int t_dp;
int t_ssp;
long t_lvalue; /*constant value*/
};
struct swtch {
int sw_label;
int sw_value;
} swtab[SWSIZE]=0;
/*operator and operand stack used by expr*/
struct ops { /*operator stack*/
int o_op; /*operator*/
int o_pri; /*priority*/
} opstack[OPSSIZE]=0, *opp=0;
char *opdstack[OPDSIZE]=0; /*operand stack*/
char **opdp=0; /*operand stack pointer*/
char *opap=0; /*ptr to next available loc in exprarea*/
struct tnode *frp=0; /*pointer to function return info node*/
int cswp=0; /*current low switch table index*/
int clabel=0; /*continue label*/
int blabel=0; /*break label*/
int rlabel=0; /*return label*/
int dlabel=0; /*default label*/
int lineno=0; /*current line number of input*/
int errcnt=0; /*count of errors*/
int inclflag=0; /*in include file, don't incr line #'s*/
int inclline=0; /*[vlh]line# in incl file for err rpting*/
char inclfile[13]=0; /*[vlh]include filename for err rpting*/
int equalstop=0; /*stop lex at '=', used for external init*/
int commastop=0; /*stop parse @ comma(used for const expr)*/
int colonstop=0; /*stop parse @ colon(used for case value)*/
int instruct=0; /*set when in structure declaration*/
int smember=0; /*set when seen . or ->*/
int infunc=0; /*set when in function body*/
int tdflag=0; /*declaration is a typedef proto*/
char *tdp=0; /*points to typedef prototype*/
int localsize=0; /*length of local variables*/
int naregs=0; /*keeps track of ptr registers alloc'd*/
int ndregs=0; /*keep track of data registers alloc'd*/
int loadreg=0; /*need to load registers...*/
int boffset=0; /*current bit offset in structure*/
int eflag=0; /*[vlh] 3.4 IEEE floats */
int fflag=0; /*[vlh] 3.4 FFP floats */
int xflag=0; /*translate int's to long's*/
int wflag=0; /*[vlh] don't generate warning mesgs*/
int reducep=0; /*[vlh] if(procid); reduction*/
int peektok=0; /*peeked at token*/
/*dimension table*/
long dtab[DSIZE]=0; /* [vlh] 3.4 int => long */
int cdp=0; /*next entry in dtab to alloc*/
/*lexical analyzer values*/
int cvalue=0; /*current token value if keyword or CINT*/
int cstrsize=0; /*current string size*/
long clvalue=0; /*current token value if long constant*/
struct symbol *csp=0; /*current token symbol ptr if SYMBOL*/
char cstr[STRSIZE]=0; /*current token value if CSTRING*/
struct symbol *dsp=0; /*declarator symbol pointer*/
/* -1 -> not instruct, 0 -> unnamed struct */
struct symbol *strucptr[10]=0; /*[vlh] ptrs to struc symbols*/
/*function argument table, used to collect function parameters*/
struct farg {
struct symbol *f_sp;
int f_offset;
} fargtab[NFARGS]=0;
/*forward referenced structure prototype names*/
struct symbol *frstab[NFRSTR]=0;
int frstp=0;
/*output buffers for intermediate code and strings*/
struct io_buf obuf=0, sbuf=0, ibuf=0, *obp=0;
#define stypedef(sp) (sp->s_attrib&STYPEDEF)
#define walign(add) ((add+1)&(~1))
#define array(type) ((type&SUPTYP)==ARRAY)
#define function(type) ((type&SUPTYP)==FUNCTION)
#define pointer(type) ((type&SUPTYP)==POINTER)
#define notarray(type) ((type&SUPTYP)!=ARRAY)
#define notfunction(type) ((type&SUPTYP)!=FUNCTION)
#define notpointer(type) ((type&SUPTYP)!=POINTER)
#define btype(type) (type&TYPE)
#define suptype(type) (type&SUPTYP)
#define alltype(type) (type&(SUPTYP|TYPE))
#define asgop(op) ((opinfo[op]&OPASSIGN)!=0)
#define relop(op) ((opinfo[op]&OPREL)!=0)
#define lintegral(op) ((opinfo[op]&OPLWORD)!=0)
#define rintegral(op) ((opinfo[op]&OPRWORD)!=0)
#define rasop(op) ((opinfo[op]&OPRAS)!=0)
#define binop(op) ((opinfo[op]&OPBIN)!=0)
#define unaryop(op) ((opinfo[op]&OPBIN)==0)
#define leaf(op) ((opinfo[op]&OPTERM)!=0)
#define lvalop(op) ((opinfo[op]&OPLVAL)!=0)
#define oppriority(op) (opinfo[op]&OPPRI)
#define makeiop(op) (op|(0254<<8))
/* checks for symbol with structure element storage class */
#define isstel(tp) (tp->t_op==SYMBOL && (sesc(tp)))
#define sesc(t) (t->t_sc==STELCL||t->t_sc==UNELCL||t->t_sc==BFIELDCL)
/* peek at next token, if not read token put back, else delete */
/* 1 if matched, 0 otherwise */
#define peek(tok) ( (peektok=gettok()) == tok )
#define outcommon(sym,size) printf("\t.comm _%.8s,%ld\n",sym,size)
#define outgoto(lab) if( lab > 0 ) printf("\tbra L%d\n",lab)
/* change to text segment */
#define outtext() printf("\t.text\n")
/* change segment to bss */
#define outbss() printf("\t.bss\n")
/* output global symbol references */
#define outextdef(sym) printf("\t.globl _%.8s\n",sym)
/* outputs reserved memory [vlh] 3.4 %d => %ld */
#define outresmem(size) printf("\t.ds.b %ld\n",size)
/* output padding for word alignments */
#define outpad() printf("\t.even\n")
/* output long constant to assembler */
#define outlcon(val) printf("\t.dc.l %d\n",val)
/* output label constant */
#define outclab(lab) printf("\t.dc.l L%d\n",lab)
/* output a label */
#define outlab(lab) printf("\tL%d:",lab)
/* output function label */
#define outflab(sym) printf("\t_%.8s:\n\t~~%.8s:\n",sym,sym)
/* output data label */
#define outdlab(sym) printf("\t_%.8s:\n",sym)
/*functions returning pointers*/
char *expr();
char *talloc();
char *tnalloc();
char *enalloc();
char *snalloc();
char *cnalloc();
char *lcnalloc();
char *fpcnalloc();
char *popopd();
char *cvopgen();
char *arrayref();
char *funcref();
char *install();
char *lookup();
char *balpar();
char *sbrk();
long initlist();
long dsize();
long psize();
long dodecl();
long dlist();
long getdec();
long gethex();
long getoct();
long getfp();
long toieee();
long toffp();
int inittype=0;
int strassign=0;
int begseq=0;
#define EXPSIZE 1024
int exprarea[EXPSIZE]=0;
int opdontop=0;
int pbchar=0;
struct symbol *symbols=0;
struct symbol *symtab[HSIZE]=0;

View File

@@ -0,0 +1,329 @@
1File: INIT.C Page 1
1 /*
2 Copyright 1982
3 Alcyon Corporation
4 8716 Production Ave.
5 San Diego, Ca. 92121
6 */
7
8 /*
9 C68 Parser - include file
10 */
11 #include "icode.h"
12
13 /*symbol attribute fields*/
14 #define SRESWORD 001 /*is symbol a reserved word?*/
15 #define SGLOBAL 002 /*is symbol global?*/
16 #define STYPEDEF 004 /*typedef declaration?*/
17 #define SDEFINED 010 /*symbol defined?*/
18
19 /*reserved words*/
20 #define R_AUTO 1
21 #define R_BREAK 2
22 #define R_CASE 3
23 #define R_CHAR 4
24 #define R_CONTINUE 5
25 #define R_DO 6
26 #define R_DEFAULT 7
27 #define R_DOUBLE 8
28 #define R_GOTO 9
29 #define R_ELSE 10
30 #define R_EXTERNAL 11
31 #define R_FLOAT 12
32 #define R_FOR 13
33 #define R_IF 14
34 #define R_INT 15
35 #define R_LONG 16
36 #define R_REGISTER 17
37 #define R_RETURN 18
38 #define R_SHORT 19
39 #define R_SIZEOF 20
40 #define R_STATIC 21
41 #define R_STRUCT 22
42 #define R_SWITCH 23
43 #define R_TYPEDEF 24
44 #define R_UNION 25
45 #define R_UNSIGNED 26
46 #define R_WHILE 27
47
48 /*
49 * mixed-mode conversions, entries in 2-d array indexed by:
50 * (int,unsn,long,doub,ptr)
51 */
52 #define INT_CHAR 1
53 #define UNSN_CHAR 1
54 #define LONG_CHAR 1
55 #define DOUB_CHAR 1
56 #define PTR_CHAR 1
57 #define INT_UNSN 0 /*no conversion is generated*/
58 #define INT_LONG 2
59 #define INT_DOUB 3
1File: INIT.C Page 2
60 #define INT_PTR 4
61 #define UNSN_INT 0 /*no conversion is generated*/
62 #define UNSN_LONG 6
63 #define UNSN_DOUB 7
64 #define UNSN_PTR 8
65 #define LONG_INT 9
66 #define LONG_UNSN 10
67 #define LONG_DOUB 11
68 #define LONG_PTR 12
69 #define DOUB_INT 13
70 #define DOUB_UNSN 14
71 #define DOUB_LONG 15
72 #define PTR_INT 16
73 #define PTR_UNSN 17
74 #define PTR_LONG 18
75 #define PTR_PTR 19
76 #define BADCONV 20
77
78 /* miscellaneous constants */
79 #define OPSSIZE 40 /*operator stack size*/
80 #define OPDSIZE 80 /*operand stack size*/
81 #define HSIZE 517 /*hash table size, 3.4 made prime */
82 #define SYMSIZE 1024 /*size to alloc for symbol structures*/
83 #define SWSIZE 256 /*max no. of cases in a switch*/
84 #define DSIZE 1000 /*dimension table size*/
85 #define BITSPWORD 16 /*bits per word*/
86 #define AREGLO 010 /*A reg flag*/
87 #define HICREG 2 /*highest reg # used for code generation*/
88 #define BITSPCHAR 8 /*bits per char*/
89 #define CHRSPWORD 2 /*chars per word*/
90 #define STRSIZE 300 /*max string length*/
91 #define NFARGS 40 /*max no. of args to function*/
92 #define NFRSTR 20 /*max no. of forward ref struct proto*/
93
94 /*symbol table node*/
95 struct symbol {
96 char s_attrib; /* defined, resword, global, typedef */
97 char s_sc; /* auto, static, external, register */
98 int s_type; /* 4bits specified, 2 bit fields for ptr... */
99 int s_dp; /* index into dimension table */
100 int s_ssp; /* dimension table/function arg table */
101 int s_offset; /* offset inside of structure */
102 char s_symbol[SSIZE]; /* symbol identifier, to SSIZE chars */
103 struct symbol *s_struc; /* if struct, ptr to parent (sys III) */
104 struct symbol *s_next; /* next symbol table entry */
105 };
106
107 /*expression tree operator node*/
108 struct tnode {
109 int t_op;
110 int t_type;
111 int t_dp;
112 int t_ssp;
113 struct tnode *t_left;
114 struct tnode *t_right;
115 };
116
117 /*expression tree node for symbol - only keeps location*/
118 struct symnode {
1File: INIT.C Page 3
119 int t_op;
120 int t_type; /*data type of symbol*/
121 int t_dp; /*dimension pointer of symbol*/
122 int t_ssp; /*structure size index to dtab*/
123 int t_sc; /*storage class of symbol*/
124 int t_offset; /*offset of symbol*/
125 int t_label;
126 };
127
128 /*expressioon tree node for external symbol - need to keep name*/
129 struct extnode {
130 int t_op;
131 int t_type;
132 int t_dp;
133 int t_ssp;
134 int t_sc;
135 int t_offset;
136 int t_reg;
137 int t_symbol[SSIZE]; /*symbol name*/
138 };
139
140 /*expression tree node for integer constant*/
141 struct conode {
142 int t_op;
143 int t_type;
144 int t_dp;
145 int t_ssp;
146 int t_value; /*constant value*/
147 };
148
149 struct lconode {
150 int t_op;
151 int t_type;
152 int t_dp;
153 int t_ssp;
154 long t_lvalue; /*constant value*/
155 };
156
157 struct swtch {
158 int sw_label;
159 int sw_value;
160 } swtab[SWSIZE]=0;
161
162 /*operator and operand stack used by expr*/
163 struct ops { /*operator stack*/
164 int o_op; /*operator*/
165 int o_pri; /*priority*/
166 } opstack[OPSSIZE]=0, *opp=0;
167
168 char *opdstack[OPDSIZE]=0; /*operand stack*/
169 char **opdp=0; /*operand stack pointer*/
170 char *opap=0; /*ptr to next available loc in exprarea*/
171 struct tnode *frp=0; /*pointer to function return info node*/
172 int cswp=0; /*current low switch table index*/
173 int clabel=0; /*continue label*/
174 int blabel=0; /*break label*/
175 int rlabel=0; /*return label*/
176 int dlabel=0; /*default label*/
177 int lineno=0; /*current line number of input*/
1File: INIT.C Page 4
178 int errcnt=0; /*count of errors*/
179 int inclflag=0; /*in include file, don't incr line #'s*/
180 int inclline=0; /*[vlh]line# in incl file for err rpting*/
181 char inclfile[13]=0; /*[vlh]include filename for err rpting*/
182 int equalstop=0; /*stop lex at '=', used for external init*/
183 int commastop=0; /*stop parse @ comma(used for const expr)*/
184 int colonstop=0; /*stop parse @ colon(used for case value)*/
185 int instruct=0; /*set when in structure declaration*/
186 int smember=0; /*set when seen . or ->*/
187 int infunc=0; /*set when in function body*/
188 int tdflag=0; /*declaration is a typedef proto*/
189 char *tdp=0; /*points to typedef prototype*/
190 int localsize=0; /*length of local variables*/
191 int naregs=0; /*keeps track of ptr registers alloc'd*/
192 int ndregs=0; /*keep track of data registers alloc'd*/
193 int loadreg=0; /*need to load registers...*/
194 int boffset=0; /*current bit offset in structure*/
195 int eflag=0; /*[vlh] 3.4 IEEE floats */
196 int fflag=0; /*[vlh] 3.4 FFP floats */
197 int xflag=0; /*translate int's to long's*/
198 int wflag=0; /*[vlh] don't generate warning mesgs*/
199 int reducep=0; /*[vlh] if(procid); reduction*/
200 int peektok=0; /*peeked at token*/
201
202 /*dimension table*/
203 long dtab[DSIZE]=0; /* [vlh] 3.4 int => long */
204 int cdp=0; /*next entry in dtab to alloc*/
205
206 /*lexical analyzer values*/
207 int cvalue=0; /*current token value if keyword or CINT*/
208 int cstrsize=0; /*current string size*/
209 long clvalue=0; /*current token value if long constant*/
210 struct symbol *csp=0; /*current token symbol ptr if SYMBOL*/
211 char cstr[STRSIZE]=0; /*current token value if CSTRING*/
212 struct symbol *dsp=0; /*declarator symbol pointer*/
213
214 /* -1 -> not instruct, 0 -> unnamed struct */
215 struct symbol *strucptr[10]=0; /*[vlh] ptrs to struc symbols*/
216
217 /*function argument table, used to collect function parameters*/
218 struct farg {
219 struct symbol *f_sp;
220 int f_offset;
221 } fargtab[NFARGS]=0;
222
223 /*forward referenced structure prototype names*/
224 struct symbol *frstab[NFRSTR]=0;
225 int frstp=0;
226
227 /*output buffers for intermediate code and strings*/
228 struct io_buf obuf=0, sbuf=0, ibuf=0, *obp=0;
229
230 #define stypedef(sp) (sp->s_attrib&STYPEDEF)
231 #define walign(add) ((add+1)&(~1))
232 #define array(type) ((type&SUPTYP)==ARRAY)
233 #define function(type) ((type&SUPTYP)==FUNCTION)
234 #define pointer(type) ((type&SUPTYP)==POINTER)
235 #define notarray(type) ((type&SUPTYP)!=ARRAY)
236 #define notfunction(type) ((type&SUPTYP)!=FUNCTION)
1File: INIT.C Page 5
237 #define notpointer(type) ((type&SUPTYP)!=POINTER)
238 #define btype(type) (type&TYPE)
239 #define suptype(type) (type&SUPTYP)
240 #define alltype(type) (type&(SUPTYP|TYPE))
241 #define asgop(op) ((opinfo[op]&OPASSIGN)!=0)
242 #define relop(op) ((opinfo[op]&OPREL)!=0)
243 #define lintegral(op) ((opinfo[op]&OPLWORD)!=0)
244 #define rintegral(op) ((opinfo[op]&OPRWORD)!=0)
245 #define rasop(op) ((opinfo[op]&OPRAS)!=0)
246 #define binop(op) ((opinfo[op]&OPBIN)!=0)
247 #define unaryop(op) ((opinfo[op]&OPBIN)==0)
248 #define leaf(op) ((opinfo[op]&OPTERM)!=0)
249 #define lvalop(op) ((opinfo[op]&OPLVAL)!=0)
250 #define oppriority(op) (opinfo[op]&OPPRI)
251 #define makeiop(op) (op|(0254<<8))
252 /* checks for symbol with structure element storage class */
253 #define isstel(tp) (tp->t_op==SYMBOL && (sesc(tp)))
254 #define sesc(t) (t->t_sc==STELCL||t->t_sc==UNELCL||t->t_sc==BFIELDCL)
255 /* peek at next token, if not read token put back, else delete */
256 /* 1 if matched, 0 otherwise */
257 #define peek(tok) ( (peektok=gettok()) == tok )
258
259 #define outcommon(sym,size) printf("\t.comm _%.8s,%ld\n",sym,size)
260 #define outgoto(lab) if( lab > 0 ) printf("\tbra L%d\n",lab)
261 /* change to text segment */
262 #define outtext() printf("\t.text\n")
263 /* change segment to bss */
264 #define outbss() printf("\t.bss\n")
265 /* output global symbol references */
266 #define outextdef(sym) printf("\t.globl _%.8s\n",sym)
267 /* outputs reserved memory [vlh] 3.4 %d => %ld */
268 #define outresmem(size) printf("\t.ds.b %ld\n",size)
269 /* output padding for word alignments */
270 #define outpad() printf("\t.even\n")
271 /* output long constant to assembler */
272 #define outlcon(val) printf("\t.dc.l %d\n",val)
273 /* output label constant */
274 #define outclab(lab) printf("\t.dc.l L%d\n",lab)
275 /* output a label */
276 #define outlab(lab) printf("\tL%d:",lab)
277 /* output function label */
278 #define outflab(sym) printf("\t_%.8s:\n\t~~%.8s:\n",sym,sym)
279 /* output data label */
280 #define outdlab(sym) printf("\t_%.8s:\n",sym)
281
282 /*functions returning pointers*/
283 char *expr();
284 char *talloc();
285 char *tnalloc();
286 char *enalloc();
287 char *snalloc();
288 char *cnalloc();
289 char *lcnalloc();
290 char *fpcnalloc();
291 char *popopd();
292 char *cvopgen();
293 char *arrayref();
294 char *funcref();
295 char *install();
1File: INIT.C Page 6
296 char *lookup();
297 char *balpar();
298 char *sbrk();
299
300 long initlist();
301 long dsize();
302 long psize();
303 long dodecl();
304 long dlist();
305 long getdec();
306 long gethex();
307 long getoct();
308 long getfp();
309 long toieee();
310 long toffp();
311
312 int inittype=0;
313 int strassign=0;
314 int begseq=0;
315
316 #define EXPSIZE 1024
317 int exprarea[EXPSIZE]=0;
318
319 int opdontop=0;
320 int pbchar=0;
321
322 struct symbol *symbols=0;
323 struct symbol *symtab[HSIZE]=0;

View File

@@ -0,0 +1,106 @@
/*
Copyright 1982
Alcyon Corporation
8716 Production Ave.
San Diego, Ca. 92121
*/
#include "parser.h"
int bol;
outinit(tp,type) /* returns - none*/
struct tnode *tp;
{
outexpr(tnalloc(INIT,type,0,0,tp));
}
outcforreg(tp)
struct tnode *tp;
{
outexpr(tnalloc(CFORREG,tp->t_type,0,0,tp));
}
outifgoto(tp,dir,lab)
struct tnode *tp;
int dir;
int lab;
{
outexpr(tnalloc(IFGOTO,dir,lab,0,tp));
}
outexpr(tp)
struct tnode *tp;
{
if( !bol )
putchar('\n');
printf(".%x\n",lineno);
outtree(tp);
}
outtree(tp)
struct tnode *tp;
{
if( !tp )
return;
printf("%x.%x",tp->t_op,tp->t_type);
switch( tp->t_op ) {
case CINT:
printf(".%x\n",tp->t_value);
break;
case CLONG:
printf(".%x.%x\n",tp->t_lvalue.hiword,tp->t_lvalue.loword);
break;
case CFLOAT: /*[vlh] 3.4*/
printf(".%x.%x\n",tp->t_lvalue.hiword,tp->t_lvalue.loword);
break;
case SYMBOL:
printf(".%x",tp->t_sc);
if( tp->t_sc == EXTERNAL )
printf(".%.8s\n",tp->t_symbol);
else
printf(".%x\n",tp->t_offset);
break;
case 0:
putchar('\n');
break;
case IFGOTO:
case BFIELD:
printf(".%x\n",tp->t_dp);
outtree(tp->t_left);
break;
default:
putchar('\n');
outtree(tp->t_left);
if( binop(tp->t_op) )
outtree(tp->t_right);
break;
}
}
/* snalloc - symbol node allocation*/
/* Allocates a tree symbol node and sets the info in it*/
char *snalloc(type,sc,off,dp,ssp) /* returns pointer to node alloc'ed*/
int type; /* symbol type*/
int sc; /* storage class*/
int off; /* offset*/
int dp; /* dimension pointer or other info*/
int ssp; /* structure size pointer*/
{
register struct symnode *snp;
snp = talloc(sizeof(*snp));
snp->t_op = SYMBOL;
snp->t_sc = sc;
snp->t_type = type;
snp->t_dp = dp;
snp->t_ssp = ssp;
snp->t_offset = off;
return(snp);
}

View File

@@ -0,0 +1,108 @@
1File: INTERF.C Page 1
1 /*
2 Copyright 1982
3 Alcyon Corporation
4 8716 Production Ave.
5 San Diego, Ca. 92121
6 */
7
8 #include "parser.h"
9 int bol;
10
11 outinit(tp,type) /* returns - none*/
12 struct tnode *tp;
13 {
14 outexpr(tnalloc(INIT,type,0,0,tp));
15 }
16
17 outcforreg(tp)
18 struct tnode *tp;
19 {
20 outexpr(tnalloc(CFORREG,tp->t_type,0,0,tp));
21 }
22
23 outifgoto(tp,dir,lab)
24 struct tnode *tp;
25 int dir;
26 int lab;
27 {
28 outexpr(tnalloc(IFGOTO,dir,lab,0,tp));
29 }
30
31 outexpr(tp)
32 struct tnode *tp;
33 {
34 if( !bol )
35 putchar('\n');
36 printf(".%x\n",lineno);
37 outtree(tp);
38 }
39
40 outtree(tp)
41 struct tnode *tp;
42 {
43 if( !tp )
44 return;
45 printf("%x.%x",tp->t_op,tp->t_type);
46 switch( tp->t_op ) {
47
48 case CINT:
49 printf(".%x\n",tp->t_value);
50 break;
51
52 case CLONG:
53 printf(".%x.%x\n",tp->t_lvalue.hiword,tp->t_lvalue.loword);
54 break;
55
56 case CFLOAT: /*[vlh] 3.4*/
57 printf(".%x.%x\n",tp->t_lvalue.hiword,tp->t_lvalue.loword);
58 break;
59
1File: INTERF.C Page 2
60 case SYMBOL:
61 printf(".%x",tp->t_sc);
62 if( tp->t_sc == EXTERNAL )
63 printf(".%.8s\n",tp->t_symbol);
64 else
65 printf(".%x\n",tp->t_offset);
66 break;
67
68 case 0:
69 putchar('\n');
70 break;
71
72 case IFGOTO:
73 case BFIELD:
74 printf(".%x\n",tp->t_dp);
75 outtree(tp->t_left);
76 break;
77
78 default:
79 putchar('\n');
80 outtree(tp->t_left);
81 if( binop(tp->t_op) )
82 outtree(tp->t_right);
83 break;
84 }
85 }
86
87 /* snalloc - symbol node allocation*/
88 /* Allocates a tree symbol node and sets the info in it*/
89 char *snalloc(type,sc,off,dp,ssp) /* returns pointer to node alloc'ed*/
90 int type; /* symbol type*/
91 int sc; /* storage class*/
92 int off; /* offset*/
93 int dp; /* dimension pointer or other info*/
94 int ssp; /* structure size pointer*/
95 {
96 register struct symnode *snp;
97
98 snp = talloc(sizeof(*snp));
99 snp->t_op = SYMBOL;
100 snp->t_sc = sc;
101 snp->t_type = type;
102 snp->t_dp = dp;
103 snp->t_ssp = ssp;
104 snp->t_offset = off;
105 return(snp);
106 }

View File

@@ -0,0 +1,827 @@
/*
Copyright 1982
Alcyon Corporation
8716 Production Ave.
San Diego, Ca. 92121
*/
#include "parser.h"
#define SOI '\01'
#define STEL HSIZE/2
/*
* the following are the cases within gettok, all other cases are
* single character unambiguous tokens. Note that we need to take
* special care not to interfere with the single character unambiguous
* operators, this is why there is a gap between WHITSP and EXCLAM.
*/
#define BADC 0 /*bad character*/
#define WHITSP 101 /*white space*/
#define EXCLAM 102 /*exlamation point*/
#define DQUOTE 103 /*double quote*/
#define PERCNT 104 /*percent sign*/
#define AMPER 105 /*ampersand*/
#define SQUOTE 106 /*single quote*/
#define STAR 107 /*asterisk or mult sign*/
#define PLUS 108 /*plus sign*/
#define MINUS 109 /*minus sign*/
#define SLASH 110 /*divide sign*/
#define DIGIT 111 /*0..9*/
#define LCAROT 112 /*less than sign*/
#define EQUAL 113 /*equals sign*/
#define RCAROT 114 /*greater than*/
#define ALPHA 115 /*a..z,A..Z and underbar*/
#define CAROT 116 /*^*/
#define BAR 117 /*vertical bar*/
char ctype[] {
BADC, BADC, BADC, BADC, BADC, BADC, BADC, BADC,
BADC, WHITSP, WHITSP, WHITSP, WHITSP, WHITSP, BADC, BADC,
BADC, BADC, BADC, BADC, WHITSP, BADC, BADC, BADC,
BADC, BADC, BADC, BADC, BADC, BADC, BADC, BADC,
WHITSP, EXCLAM, DQUOTE, BADC, BADC, PERCNT, AMPER, SQUOTE,
LPAREN, RPAREN, STAR, PLUS, COMMA, MINUS, PERIOD, SLASH,
DIGIT, DIGIT, DIGIT, DIGIT, DIGIT, DIGIT, DIGIT, DIGIT,
DIGIT, DIGIT, COLON, SEMI, LCAROT, EQUAL, RCAROT, QMARK,
BADC, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA,
ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA,
ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA,
ALPHA, ALPHA, ALPHA, LBRACK, BADC, RBRACK, CAROT, ALPHA,
BADC, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA,
ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA,
ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA,
ALPHA, ALPHA, ALPHA, LCURBR, BAR, RCURBR, COMPL, BADC
};
/*key word table*/
struct resword {
char *r_name;
int r_value;
} reswords[] {
"auto", R_AUTO,
"break", R_BREAK,
"case", R_CASE,
"char", R_CHAR,
"continue", R_CONTINUE,
"do", R_DO,
"default", R_DEFAULT,
"double", R_DOUBLE,
"goto", R_GOTO,
"else", R_ELSE,
"extern", R_EXTERNAL,
"float", R_FLOAT,
"for", R_FOR,
"if", R_IF,
"int", R_INT,
"long", R_LONG,
"register", R_REGISTER,
"return", R_RETURN,
"short", R_SHORT,
"sizeof", R_SIZEOF,
"static", R_STATIC,
"struct", R_STRUCT,
"switch", R_SWITCH,
"typedef", R_TYPEDEF,
"union", R_UNION,
"unsigned", R_UNSIGNED,
"while", R_WHILE,
0,
};
#define SELFMOD 0200
#define ASMASK 0177
/*
* this table is used to check for an operator after an equals sign.
* note that =-, =* and =& may all have an ambiguous meaning if not
* followed by a space, this is checked for in gettok.
*/
char asmap[] {
EQUALS, /*==*/
EQADD, /*=+*/
EQSUB|SELFMOD, /*=-*/
EQMULT|SELFMOD, /*=**/
EQDIV, /*=/*/
EQOR, /*=|*/
EQAND|SELFMOD, /*=&*/
EQXOR, /*=^*/
EQMOD, /*=%*/
};
char escmap[] "\b\n\r\t";
int pbchar; /*pushed back character*/
struct symbol *symtab[HSIZE]; /*hash table*/
struct symbol *symbols; /*pointer to next avail symbol buf*/
int nsyms; /*number of symbol bufs in memory*/
/*
* getdec - get a decimal number
* Uses Horner's method to get decimal number. Note that
* multiplication by 10 is cleverly programmed as two shifts and two
* adds. This is because long multiplies are painful on both the
* PDP-11 and 68000.
*/
long getdec() /* returns number*/
{
register long value;
register char c;
for( value = 0; (c=ngetch()) >= '0' && c <= '9'; ) {
value =<< 1; /*value = value*2*/
value =+ value << 2; /*value*2 + value*8 = value*10*/
value =+ (c-'0');
}
putback(c);
return(value);
}
#define BIAS 127L
#define EXPSIZ 4
#define FRACSIZ 20
long toieee();
long toffp();
float power10();
/*
* getfp - get a floating point constant
* we've already gotten the significant digits, now build a
* floating point number with possible decimal digits and an
* exponent, yields an ieee formated floating point number,
* unless the fflag is on, then a ffp constant is generated.
*/
long
getfp(significant)
long significant;
{
register char c;
register long places; /* decimal places */
int esign;
float exp, fraction, fp;
places = 0L; esign = 0; fraction = significant; exp = 0.0;
if ((c = ngetch()) == '.') /* get decimal places */
for( ; (c=ngetch()) >= '0' && c <= '9';) {
fraction = fraction * 10.0;
fraction = fraction + (c - '0');
places++;
}
if (c=='e' || c=='E') { /* exponent exists */
esign = (peekis('-')) ? 1 : (peekis('+')) ? 0 : 0;
for( ; (c=ngetch()) >= '0' && c <= '9'; ) {
exp = exp * 10.0;
exp = exp + (c - '0');
}
}
putback(c);
if (esign)
exp = -exp;
places = exp - places;
fp = fraction * power10(places);
if (fflag)
return( toffp(fp) );
else
return ( toieee(fp) );
}
float
power10(pwr) /* used by getfp, 10^pwr */
long pwr;
{
float f;
if (pwr < 0L) /* negative power */
for (f = 1.0; pwr < 0L; pwr++)
f = f / 10.0;
else /* positive power */
for (f = 1.0; pwr > 0L; pwr--)
f = f * 10.0;
return(f);
}
long
toffp(f) /* converts current machine float to ffp rep */
float f;
{
register long exp;
register int sign, count;
long l;
if (f == 0.0)
return(0L);
if (f < 0.0) {
sign = 1;
f = -f;
}
else
sign = 0;
exp = 0L;
for( ; f >= 1.0; f = f / 2.0)
exp++;
for( ; f < 0.5; f = f * 2.0)
exp--;
f = f * 16777216.0; /* 2 ^ 24 */
l = f;
l =<< 8;
if (sign)
l =| 0x80;
exp =+ 0x40;
l =| (exp & 0x7f);
return(l);
}
long
toieee(f) /* converts current machine float to ieee rep */
float f;
{
register long exp;
register int sign, count;
long l;
if (f == 0.0)
return(0L);
if (f < 0.0) {
sign = 1;
f = -f;
}
else
sign = 0;
exp = 0L;
for( ; f >= 2.0; f = f / 2.0)
exp++;
for( ; f < 1.0; f = f * 2.0)
exp--;
f = f - 1.0;
f = f * 8388608.0; /* 2 ^ 23 */
l = f;
if (sign)
l =| 0x80000000;
exp = (exp + BIAS)<<23;
l =| (exp & 0x7f800000);
return(l);
}
#define toupper(c) ((c) & ~32)
/* gethex - get an hexidecimal number*/
/* Uses Horner's method to get hexidecimal number*/
long gethex() /* returns number*/
{
register long value;
register char c, ch;
value = 0;
while( 1 ) {
if( (c=ngetch()) >= '0' && c <= '9' )
c =- '0';
else if((ch=toupper(c)) >= 'A' && ch <= 'F' ) /* [vlh] */
c = ch - ('A'-10);
else
break;
value = (value<<4) + c;
}
putback(c);
return(value);
}
/* getoct - get an octal number*/
/* Uses Horner's method to get octal number*/
long getoct(flag) /* returns number*/
int flag; /* string flag 1=>in string, else 0*/
{
register long value;
register char c;
register int count;
count = 0;
for( value = 0; (c=ngetch()) >= '0' && c <= '7'; ) {
if( flag && ++count > 3 )
break;
value = (value<<3) + (c-'0');
}
putback(c);
return(value);
}
/*
* gettok - get next token from input
* Checks pushed-packed token buffer, supresses / * * / comments,
* folds multiple character special symbols into single word token.
*/
gettok() /* returns token type*/
{
register int c, nextc, i;
register char *p;
register long value;
char sym[SSIZE];
if( peektok ) {
i = peektok;
peektok = 0;
return(i);
}
while( (c=ngetch()) != EOF ) {
switch(ctype[c]) {
case BADC: /*bad character*/
error("invalid character");
break;
case SEMI:
cvalue = 0; /* [vlh] not reserved word... */
default:
return( ctype[c] );
case WHITSP: /*skip all white space*/
break;
case EXCLAM: /*!= or !*/
return( peekis('=') ? NEQUALS : NOT );
case DQUOTE: /*quoted string*/
getstr(cstr,STRSIZE,'"');
cvalue = nextlabel++;
return(STRING);
case PERCNT: /*%= or %*/
return( peekis('=') ? EQMOD : MOD );
case AMPER: /*&=, && or &*/
return( peekis('=') ? EQAND : peekis('&') ? LAND : AND );
case SQUOTE: /*character constant*/
getstr(cstr,STRSIZE,'\'');
if( cstrsize > CHRSPWORD+1 ) {
error("character constant too long");
cstrsize = CHRSPWORD + 1;
}
cvalue = 0;
for( p = cstr; --cstrsize > 0; ) {
cvalue =<< BITSPCHAR;
cvalue =| (*p++ & 0377);
}
return(CINT);
case STAR: /**= or **/
return( peekis('=') ? EQMULT : MULT );
case PLUS: /*+=, ++ or +*/
return( peekis('=') ? EQADD : peekis('+') ? PREINC : ADD );
case MINUS: /*-=, --, -> or -*/
return( peekis('=') ? EQSUB : peekis('-') ? PREDEC :
peekis('>') ? APTR : SUB );
case SLASH: /*/ *..* /, //..., /= or /*/
if( peekis('*') ) {
while( (c=ngetch()) != EOF )
if( c == '*' && peekis('/') )
break;
if( c == EOF ) {
error("no */ before EOF");
return(EOF);
}
continue;
}
if( peekis('/') ) {
while( (c=ngetch()) != EOF && c != EOLC )
;
continue;
}
return( peekis('=') ? EQDIV : DIV );
case DIGIT: /*number constant (long or reg)*/
i = 0; /*flags if long constant*/
if( c != '0' ) {
putback(c);
dofp:
value = getdec();
if ((c=ngetch())=='.' || c=='e' || c=='E') { /*[vlh] 3.4 */
putback(c);
clvalue = getfp(value);
return(CFLOAT);
}
putback(c);
if( value > 32767 || value < -32768 )
i++;
}
else if( peekis('x') || peekis('X') ) {
value = gethex();
if( value < 0 || value >= 0x10000L )
i++;
}
else {
if (peekis('.')) {
putback('.');
goto dofp;
}
value = getoct(0);
if( value < 0 || value >= 0x10000L )
i++;
}
if( peekis('l') || peekis('L') || i ) {
clvalue = value;
return(CLONG);
}
cvalue = value;
return(CINT);
case LCAROT: /*<=, <<, <<= or <*/
return( peekis('=') ? LESSEQ : peekis('<') ?
(peekis('=') ? EQLSH : LSH) : LESS );
case EQUAL: /*==, =<<, =>>, =+, ..., =*/
if( peekis('<') ) {
if( peekis('<') )
return(EQLSH);
}
else if( peekis('>') ) {
if( peekis('>') )
return(EQRSH);
}
else if( (i=index("=+-*/|&^%",(c=ngetch()))) >= 0 ) {
i = asmap[i];
if( i & SELFMOD ) {
if( (nextc=ngetch()) != ' ' )
if (!wflag) /*[vlh] old fashion initialization*/
error("=%c assumed",c);
putback(nextc);
}
return( i & ASMASK );
}
else
putback(c);
return(ASSIGN);
case RCAROT: /*>=, >>, >>= or >*/
return( peekis('=') ? GREATEQ : peekis('>') ?
(peekis('=') ? EQRSH : RSH) : GREAT );
case ALPHA: /*[A-Za-z][A-Za-z0-9]**/
p = &sym[0];
i = SSIZE;
for(; ctype[c] == ALPHA || ctype[c] == DIGIT; c=ngetch(),i-- )
if( i > 0 )
*p++ = c;
if( i > 0 )
*p = '\0';
putback(c);
csp = lookup(sym);
if( csp->s_attrib & SRESWORD ) {
cvalue = csp->s_offset;
return(RESWORD);
}
smember = 0;
return(SYMBOL);
case CAROT: /*^= or ^*/
return( peekis('=') ? EQXOR : XOR );
case BAR: /*|=, || or |*/
return( peekis('=') ? EQOR : peekis('|') ? LOR : OR );
}
}
return(EOF);
}
/*
* peekis - peeks at next character for specific character
* Gets next (possibly pushed back) character, if it matches
* the given character 1 is returned, otherwise the character
* is put back.
*/
peekis(tc) /* returns 1 if match, 0 otherwise*/
int tc; /* test character*/
{
register int c;
if( (c=ngetch()) == tc )
return(1);
putback(c);
return(0);
}
/* ngetch - get a possibly pushed back character*/
/* Checks pbchar variable, returns it if non-zero, handles counting*/
/* of new lines and whether you are in an include or not.*/
ngetch() /* returns character read or EOF*/
{
register int c;
register char *ifile;
if( pbchar ) {
c = pbchar;
pbchar = 0;
}
else if( (c=getc(&ibuf)) == EOLC ) {
if( inclflag )
inclflag = 0;
else
lineno++;
}
else if( c == SOI) { /*[vlh]add incl filename & line # */
inclflag++;
ifile = &inclfile;
while ((c=getc(&ibuf)) != SOI)
*ifile++ = c&0377;
*ifile = 0;
inclline = getdec() & 077777;
c = ' ';
}
else if( c < 0 )
c = EOF;
return(c);
}
/*
* peekc - peek at the next non-whitespace character after token
* This allows for the problem of having to look at two tokens
* at once. The second token is always a semi-colon or colon,
* so we only look at the single character, rather than going
* thru gettok.
*/
peekc(tc) /* returns 1 if match, 0 otherwise*/
int tc; /* character to look for*/
{
register int c;
while( ctype[(c=ngetch())] == WHITSP) ;
if( c == tc )
return(1);
putback(c);
return(0);
}
/* putback - puts back a single character*/
/* Checks pbchar for error condition.*/
putback(c) /* returns - none*/
int c;
{
if( pbchar )
error("too many chars pushed back");
else
pbchar = c;
}
/* getstr - get a quoted (single or double) character string*/
/* Gets specified number of characters, handling escapes.*/
getstr(str,nchars,endc) /* returns - none*/
char *str; /* pointer to string buffer*/
int nchars; /* max number of characters*/
char endc; /* ending string character*/
{
register char *p;
register int i;
register int c;
register int j;
cstrsize = 1;
p = str;
for( i = nchars; (c=ngetch()) != endc; i-- ) {
if( c == EOF || c == EOLC ) {
error("string cannot cross line");
break;
}
if( c == '\\' ) {
if( (c=ngetch()) >= '0' && c <= '7' ) {
putback(c);
if( (c=getoct(1)) < 0 || c > 255 ) {
error("bad character constant");
continue;
}
}
else if( (j=index("bnrt",c)) >= 0 )
c = escmap[j];
else if( c == EOLC ) /*escape followed by nl->ignore*/
continue;
}
if( i > 0 ) { /*room left in string?*/
cstrsize++;
*p++ = c;
}
else if( !i ) /*only say error once...*/
error("string too long");
}
if( i <= 0 ) /*string overflow?*/
p--;
*p = '\0';
}
/* syminit - initialize the symbol table, install reswords*/
/* Goes thru the resword table and installs them into the symbol*/
/* table.*/
syminit() /* returns - none*/
{
register struct resword *rp;
for( rp = &reswords[0]; rp->r_name != 0; rp++ )
install(rp->r_name,SRESWORD|SDEFINED,rp->r_value);
}
/* install - install a symbol in the symbol table*/
/* Allocates a symbol entry, copies info into it and links it*/
/* into the hash table chain.*/
char *install(sym,attrib,offset) /* returns pointer to symbol struct*/
char *sym; /* symbol to install*/
int attrib; /* attribues of symbol*/
int offset; /* symbol offset (resword value)*/
{
register struct symbol *sp;
register int i;
while( !(sp=symbols) ) {
if( !(sp=sbrk(SYMSIZE)) )
ferror("symbol table overflow");
for( i = SYMSIZE/(sizeof *symbols); --i >= 0; ) {
sp->s_next = symbols;
symbols = sp++;
}
}
symbols = sp->s_next;
sp->s_attrib = attrib;
sp->s_sc = 0; sp->s_type = 0; sp->s_dp = 0; sp->s_ssp = 0;
sp->s_offset = offset;
sp->s_struc = (instruct) ? strucptr[smember+instruct] : 0;
symcopy(sym,sp->s_symbol); /*copy symbol to symbol struct*/
i = symhash(sym,instruct|smember); /*link into chain list*/
sp->s_next = symtab[i];
symtab[i] = sp;
return(sp);
}
/* lookup - looks up a symbol in symbol table*/
/* Hashes symbol, then goes thru chain, if not found, then*/
/* installs the symbol.*/
char *lookup(sym) /* returns pointer to symbol buffer*/
char *sym; /* pointer to symbol*/
{
register struct symbol *sp, *hold;
register char *p;
int exact; /* same name, diff type or offset */
p = sym;
for( sp = symtab[symhash(p,0)]; sp != 0; sp = sp->s_next )
if((sp->s_attrib&(SRESWORD|STYPEDEF)) && symequal(p,sp->s_symbol))
return(sp);
if (!(smember|instruct)) { /*[vlh]*/
for( sp=symtab[symhash(p,0)]; sp!=0; sp=sp->s_next )
if( symequal(p,sp->s_symbol) ) return(sp);
}
else { /* doing a declaration or an expression */
hold = 0; exact = 0;
for( sp=symtab[symhash(p,instruct|smember)]; sp!=0; sp=sp->s_next )
if( symequal(p,sp->s_symbol) )
if (symsame(sp,hold,&exact)) return(sp);
else if (!hold && !exact) hold = sp;
if (hold && !exact) return(hold);
}
return(install(p,0,0));
}
/* freesyms - frees all local symbols at end of function declaration*/
/* Searches thru symbol table, deleting all symbols marked as locals*/
freesyms() /* returns - none*/
{
register int i, tinfo;
register struct symbol *sp, *tp, *nextp, **htp;
for( htp = &symtab[0], i = HSIZE; --i >= 0; htp++ )
for( tp = 0, sp = *htp; sp != 0; sp = nextp ) {
nextp = sp->s_next;
if( !(sp->s_attrib&SDEFINED) ) {
error("undefined label: %.8s",sp->s_symbol);
sp->s_attrib =| SDEFINED;
}
if( sp->s_attrib & (SGLOBAL|SRESWORD) )
tp = sp;
else {
if( tp )
tp->s_next = sp->s_next;
else
*htp = sp->s_next;
sp->s_next = symbols;
symbols = sp;
}
}
}
/* chksyms - checks symbol table for undefined symbols, etc.*/
/* Goes thru the symbol table checking for undeclared forward*/
/* referenced structures, and outputs local symbols for debugger.*/
chksyms() /* returns - none*/
{
register struct symbol **htp, *sp;
register int i, sc;
for( htp = &symtab[0], i = HSIZE; --i >= 0; htp++ )
for( sp = *htp; sp != 0; sp = sp->s_next ) {
sc = sp->s_sc;
if(sc!=0 && sp->s_ssp>=0 && (btype(sp->s_type))==FRSTRUCT) {
sp->s_ssp = frstab[sp->s_ssp]->s_ssp; /* 3.4 ssp>0 */
sp->s_type = (sp->s_type&~TYPE) | STRUCT;
}
if( sc == PDECLIST ) {
error("not in parameter list: %.8s",sp->s_symbol);
sp->s_sc = AUTO;
}
if( infunc )
outlocal(sp->s_type,sp->s_sc,sp->s_symbol,sp->s_offset);
}
}
/* symhash - compute hash value for symbol*/
/* Sums the symbols characters and takes that modulus the hash table*/
/* size.*/
symhash(sym,stel) /* returns hash value for symbol*/
char *sym; /* pointer to symbol*/
int stel; /* structure element flag*/
{
register char *p;
register int hashval, i;
hashval = (stel ? STEL : 0 );
for( p = sym, i = SSIZE; *p != '\0' && i > 0; i-- )
hashval =+ *p++;
return( hashval % HSIZE );
}
/* symequal - check for symbol equality*/
/* Does comparison between two symbols.*/
symequal(sym1,sym2) /* returns 1 if equal, 0 otherwise*/
char *sym1; /* pointer to first symbol*/
char *sym2; /* pointer to second symbol*/
{
register char *p, *q;
register int i;
for( p = sym1, q = sym2, i = SSIZE; *p == *q++; )
if( *p++ == '\0' || --i == 0 )
return(1);
return(0);
}
/* symsame - symbol member same as declared */
symsame(sp,hold,exact) /* [vlh] */
struct symbol *sp, *hold;
int *exact;
{
if (strucptr[smember+instruct])
if (strucptr[smember+instruct]==sp->s_struc) return(1);
if (hold)
if (sp->s_type != hold->s_type || sp->s_offset != hold->s_offset)
*exact = 1;
return(0);
}
/* symcopy - symbol copy*/
/* Copies one symbol to another.*/
symcopy(sym1,sym2) /* returns - none*/
char *sym1; /* pointer to symbol to copy*/
char *sym2; /* pointer to area to copy to*/
{
register char *p, *q;
register int i;
for( p = sym1, q = sym2, i = SSIZE; --i >= 0; )
*q++ = ( *p ? *p++ : '\0');
}
/* index - find the index of a character in a string*/
/* This is identical to Software Tools index.*/
index(str,chr) /* returns index of c in str or -1*/
char *str; /* pointer to string to search*/
char chr; /* character to search for*/
{
register char *s;
register int i;
for( s = str, i = 0; *s != '\0'; i++ )
if( *s++ == chr )
return(i);
return(-1);
}
/* next - if next token matches given token, skip and return success*/
/* This allows for clean parsing of declarations.*/
next(tok) /* returns 1 if matched, 0 otherwise*/
int tok;
{
register int token;
if( (token=gettok()) == tok )
return(1);
peektok = token;
return(0);
}
/* pbtok - put back the given token*/
/* This merely sets the peektok variable*/
pbtok(tok) /* returns - none*/
int tok;
{
if( peektok )
error("too many tokens pushed back");
peektok = tok;
}

View File

@@ -0,0 +1,842 @@
1File: LEX.C Page 1
1 /*
2 Copyright 1982
3 Alcyon Corporation
4 8716 Production Ave.
5 San Diego, Ca. 92121
6 */
7
8 #include "parser.h"
9 #define SOI '\01'
10 #define STEL HSIZE/2
11
12 /*
13 * the following are the cases within gettok, all other cases are
14 * single character unambiguous tokens. Note that we need to take
15 * special care not to interfere with the single character unambiguous
16 * operators, this is why there is a gap between WHITSP and EXCLAM.
17 */
18 #define BADC 0 /*bad character*/
19 #define WHITSP 101 /*white space*/
20 #define EXCLAM 102 /*exlamation point*/
21 #define DQUOTE 103 /*double quote*/
22 #define PERCNT 104 /*percent sign*/
23 #define AMPER 105 /*ampersand*/
24 #define SQUOTE 106 /*single quote*/
25 #define STAR 107 /*asterisk or mult sign*/
26 #define PLUS 108 /*plus sign*/
27 #define MINUS 109 /*minus sign*/
28 #define SLASH 110 /*divide sign*/
29 #define DIGIT 111 /*0..9*/
30 #define LCAROT 112 /*less than sign*/
31 #define EQUAL 113 /*equals sign*/
32 #define RCAROT 114 /*greater than*/
33 #define ALPHA 115 /*a..z,A..Z and underbar*/
34 #define CAROT 116 /*^*/
35 #define BAR 117 /*vertical bar*/
36
37 char ctype[] {
38 BADC, BADC, BADC, BADC, BADC, BADC, BADC, BADC,
39 BADC, WHITSP, WHITSP, WHITSP, WHITSP, WHITSP, BADC, BADC,
40 BADC, BADC, BADC, BADC, WHITSP, BADC, BADC, BADC,
41 BADC, BADC, BADC, BADC, BADC, BADC, BADC, BADC,
42 WHITSP, EXCLAM, DQUOTE, BADC, BADC, PERCNT, AMPER, SQUOTE,
43 LPAREN, RPAREN, STAR, PLUS, COMMA, MINUS, PERIOD, SLASH,
44 DIGIT, DIGIT, DIGIT, DIGIT, DIGIT, DIGIT, DIGIT, DIGIT,
45 DIGIT, DIGIT, COLON, SEMI, LCAROT, EQUAL, RCAROT, QMARK,
46 BADC, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA,
47 ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA,
48 ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA,
49 ALPHA, ALPHA, ALPHA, LBRACK, BADC, RBRACK, CAROT, ALPHA,
50 BADC, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA,
51 ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA,
52 ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA, ALPHA,
53 ALPHA, ALPHA, ALPHA, LCURBR, BAR, RCURBR, COMPL, BADC
54 };
55
56 /*key word table*/
57 struct resword {
58 char *r_name;
59 int r_value;
1File: LEX.C Page 2
60 } reswords[] {
61 "auto", R_AUTO,
62 "break", R_BREAK,
63 "case", R_CASE,
64 "char", R_CHAR,
65 "continue", R_CONTINUE,
66 "do", R_DO,
67 "default", R_DEFAULT,
68 "double", R_DOUBLE,
69 "goto", R_GOTO,
70 "else", R_ELSE,
71 "extern", R_EXTERNAL,
72 "float", R_FLOAT,
73 "for", R_FOR,
74 "if", R_IF,
75 "int", R_INT,
76 "long", R_LONG,
77 "register", R_REGISTER,
78 "return", R_RETURN,
79 "short", R_SHORT,
80 "sizeof", R_SIZEOF,
81 "static", R_STATIC,
82 "struct", R_STRUCT,
83 "switch", R_SWITCH,
84 "typedef", R_TYPEDEF,
85 "union", R_UNION,
86 "unsigned", R_UNSIGNED,
87 "while", R_WHILE,
88 0,
89 };
90
91 #define SELFMOD 0200
92 #define ASMASK 0177
93
94 /*
95 * this table is used to check for an operator after an equals sign.
96 * note that =-, =* and =& may all have an ambiguous meaning if not
97 * followed by a space, this is checked for in gettok.
98 */
99 char asmap[] {
100 EQUALS, /*==*/
101 EQADD, /*=+*/
102 EQSUB|SELFMOD, /*=-*/
103 EQMULT|SELFMOD, /*=**/
104 EQDIV, /*=/*/
105 EQOR, /*=|*/
106 EQAND|SELFMOD, /*=&*/
107 EQXOR, /*=^*/
108 EQMOD, /*=%*/
109 };
110
111 char escmap[] "\b\n\r\t";
112 int pbchar; /*pushed back character*/
113 struct symbol *symtab[HSIZE]; /*hash table*/
114 struct symbol *symbols; /*pointer to next avail symbol buf*/
115 int nsyms; /*number of symbol bufs in memory*/
116
117 /*
118 * getdec - get a decimal number
1File: LEX.C Page 3
119 * Uses Horner's method to get decimal number. Note that
120 * multiplication by 10 is cleverly programmed as two shifts and two
121 * adds. This is because long multiplies are painful on both the
122 * PDP-11 and 68000.
123 */
124 long getdec() /* returns number*/
125 {
126 register long value;
127 register char c;
128
129 for( value = 0; (c=ngetch()) >= '0' && c <= '9'; ) {
130 value =<< 1; /*value = value*2*/
131 value =+ value << 2; /*value*2 + value*8 = value*10*/
132 value =+ (c-'0');
133 }
134 putback(c);
135 return(value);
136 }
137
138 #define BIAS 127L
139 #define EXPSIZ 4
140 #define FRACSIZ 20
141
142 long toieee();
143 long toffp();
144 float power10();
145
146 /*
147 * getfp - get a floating point constant
148 * we've already gotten the significant digits, now build a
149 * floating point number with possible decimal digits and an
150 * exponent, yields an ieee formated floating point number,
151 * unless the fflag is on, then a ffp constant is generated.
152 */
153 long
154 getfp(significant)
155 long significant;
156 {
157 register char c;
158 register long places; /* decimal places */
159 int esign;
160 float exp, fraction, fp;
161
162 places = 0L; esign = 0; fraction = significant; exp = 0.0;
163 if ((c = ngetch()) == '.') /* get decimal places */
164 for( ; (c=ngetch()) >= '0' && c <= '9';) {
165 fraction = fraction * 10.0;
166 fraction = fraction + (c - '0');
167 places++;
168 }
169
170 if (c=='e' || c=='E') { /* exponent exists */
171 esign = (peekis('-')) ? 1 : (peekis('+')) ? 0 : 0;
172 for( ; (c=ngetch()) >= '0' && c <= '9'; ) {
173 exp = exp * 10.0;
174 exp = exp + (c - '0');
175 }
176 }
177
1File: LEX.C Page 4
178 putback(c);
179 if (esign)
180 exp = -exp;
181 places = exp - places;
182 fp = fraction * power10(places);
183 if (fflag)
184 return( toffp(fp) );
185 else
186 return ( toieee(fp) );
187 }
188
189 float
190 power10(pwr) /* used by getfp, 10^pwr */
191 long pwr;
192 {
193 float f;
194
195 if (pwr < 0L) /* negative power */
196 for (f = 1.0; pwr < 0L; pwr++)
197 f = f / 10.0;
198 else /* positive power */
199 for (f = 1.0; pwr > 0L; pwr--)
200 f = f * 10.0;
201 return(f);
202 }
203
204 long
205 toffp(f) /* converts current machine float to ffp rep */
206 float f;
207 {
208 register long exp;
209 register int sign, count;
210 long l;
211
212 if (f == 0.0)
213 return(0L);
214 if (f < 0.0) {
215 sign = 1;
216 f = -f;
217 }
218 else
219 sign = 0;
220 exp = 0L;
221 for( ; f >= 1.0; f = f / 2.0)
222 exp++;
223 for( ; f < 0.5; f = f * 2.0)
224 exp--;
225 f = f * 16777216.0; /* 2 ^ 24 */
226 l = f;
227 l =<< 8;
228 if (sign)
229 l =| 0x80;
230 exp =+ 0x40;
231 l =| (exp & 0x7f);
232 return(l);
233 }
234
235 long
236 toieee(f) /* converts current machine float to ieee rep */
1File: LEX.C Page 5
237 float f;
238 {
239 register long exp;
240 register int sign, count;
241 long l;
242
243 if (f == 0.0)
244 return(0L);
245 if (f < 0.0) {
246 sign = 1;
247 f = -f;
248 }
249 else
250 sign = 0;
251 exp = 0L;
252 for( ; f >= 2.0; f = f / 2.0)
253 exp++;
254 for( ; f < 1.0; f = f * 2.0)
255 exp--;
256 f = f - 1.0;
257 f = f * 8388608.0; /* 2 ^ 23 */
258 l = f;
259 if (sign)
260 l =| 0x80000000;
261 exp = (exp + BIAS)<<23;
262 l =| (exp & 0x7f800000);
263 return(l);
264 }
265
266 #define toupper(c) ((c) & ~32)
267 /* gethex - get an hexidecimal number*/
268 /* Uses Horner's method to get hexidecimal number*/
269 long gethex() /* returns number*/
270 {
271 register long value;
272 register char c, ch;
273
274 value = 0;
275 while( 1 ) {
276 if( (c=ngetch()) >= '0' && c <= '9' )
277 c =- '0';
278 else if((ch=toupper(c)) >= 'A' && ch <= 'F' ) /* [vlh] */
279 c = ch - ('A'-10);
280 else
281 break;
282 value = (value<<4) + c;
283 }
284 putback(c);
285 return(value);
286 }
287
288 /* getoct - get an octal number*/
289 /* Uses Horner's method to get octal number*/
290 long getoct(flag) /* returns number*/
291 int flag; /* string flag 1=>in string, else 0*/
292 {
293 register long value;
294 register char c;
295 register int count;
1File: LEX.C Page 6
296
297 count = 0;
298 for( value = 0; (c=ngetch()) >= '0' && c <= '7'; ) {
299 if( flag && ++count > 3 )
300 break;
301 value = (value<<3) + (c-'0');
302 }
303 putback(c);
304 return(value);
305 }
306
307 /*
308 * gettok - get next token from input
309 * Checks pushed-packed token buffer, supresses / * * / comments,
310 * folds multiple character special symbols into single word token.
311 */
312 gettok() /* returns token type*/
313 {
314 register int c, nextc, i;
315 register char *p;
316 register long value;
317 char sym[SSIZE];
318
319 if( peektok ) {
320 i = peektok;
321 peektok = 0;
322 return(i);
323 }
324 while( (c=ngetch()) != EOF ) {
325 switch(ctype[c]) {
326
327 case BADC: /*bad character*/
328 error("invalid character");
329 break;
330
331 case SEMI:
332 cvalue = 0; /* [vlh] not reserved word... */
333 default:
334 return( ctype[c] );
335
336 case WHITSP: /*skip all white space*/
337 break;
338
339 case EXCLAM: /*!= or !*/
340 return( peekis('=') ? NEQUALS : NOT );
341
342 case DQUOTE: /*quoted string*/
343 getstr(cstr,STRSIZE,'"');
344 cvalue = nextlabel++;
345 return(STRING);
346
347 case PERCNT: /*%= or %*/
348 return( peekis('=') ? EQMOD : MOD );
349
350 case AMPER: /*&=, && or &*/
351 return( peekis('=') ? EQAND : peekis('&') ? LAND : AND );
352
353 case SQUOTE: /*character constant*/
354 getstr(cstr,STRSIZE,'\'');
1File: LEX.C Page 7
355 if( cstrsize > CHRSPWORD+1 ) {
356 error("character constant too long");
357 cstrsize = CHRSPWORD + 1;
358 }
359 cvalue = 0;
360 for( p = cstr; --cstrsize > 0; ) {
361 cvalue =<< BITSPCHAR;
362 cvalue =| (*p++ & 0377);
363 }
364 return(CINT);
365
366 case STAR: /**= or **/
367 return( peekis('=') ? EQMULT : MULT );
368
369 case PLUS: /*+=, ++ or +*/
370 return( peekis('=') ? EQADD : peekis('+') ? PREINC : ADD );
371
372 case MINUS: /*-=, --, -> or -*/
373 return( peekis('=') ? EQSUB : peekis('-') ? PREDEC :
374 peekis('>') ? APTR : SUB );
375
376 case SLASH: /*/ *..* /, //..., /= or /*/
377 if( peekis('*') ) {
378 while( (c=ngetch()) != EOF )
379 if( c == '*' && peekis('/') )
380 break;
381 if( c == EOF ) {
382 error("no */ before EOF");
383 return(EOF);
384 }
385 continue;
386 }
387 if( peekis('/') ) {
388 while( (c=ngetch()) != EOF && c != EOLC )
389 ;
390 continue;
391 }
392 return( peekis('=') ? EQDIV : DIV );
393
394 case DIGIT: /*number constant (long or reg)*/
395 i = 0; /*flags if long constant*/
396 if( c != '0' ) {
397 putback(c);
398 dofp:
399 value = getdec();
400 if ((c=ngetch())=='.' || c=='e' || c=='E') { /*[vlh] 3.4 */
401 putback(c);
402 clvalue = getfp(value);
403 return(CFLOAT);
404 }
405 putback(c);
406 if( value > 32767 || value < -32768 )
407 i++;
408 }
409 else if( peekis('x') || peekis('X') ) {
410 value = gethex();
411 if( value < 0 || value >= 0x10000L )
412 i++;
413 }
1File: LEX.C Page 8
414 else {
415 if (peekis('.')) {
416 putback('.');
417 goto dofp;
418 }
419 value = getoct(0);
420 if( value < 0 || value >= 0x10000L )
421 i++;
422 }
423 if( peekis('l') || peekis('L') || i ) {
424 clvalue = value;
425 return(CLONG);
426 }
427 cvalue = value;
428 return(CINT);
429
430 case LCAROT: /*<=, <<, <<= or <*/
431 return( peekis('=') ? LESSEQ : peekis('<') ?
432 (peekis('=') ? EQLSH : LSH) : LESS );
433
434 case EQUAL: /*==, =<<, =>>, =+, ..., =*/
435 if( peekis('<') ) {
436 if( peekis('<') )
437 return(EQLSH);
438 }
439 else if( peekis('>') ) {
440 if( peekis('>') )
441 return(EQRSH);
442 }
443 else if( (i=index("=+-*/|&^%",(c=ngetch()))) >= 0 ) {
444 i = asmap[i];
445 if( i & SELFMOD ) {
446 if( (nextc=ngetch()) != ' ' )
447 if (!wflag) /*[vlh] old fashion initialization*/
448 error("=%c assumed",c);
449 putback(nextc);
450 }
451 return( i & ASMASK );
452 }
453 else
454 putback(c);
455 return(ASSIGN);
456
457 case RCAROT: /*>=, >>, >>= or >*/
458 return( peekis('=') ? GREATEQ : peekis('>') ?
459 (peekis('=') ? EQRSH : RSH) : GREAT );
460
461 case ALPHA: /*[A-Za-z][A-Za-z0-9]**/
462 p = &sym[0];
463 i = SSIZE;
464 for(; ctype[c] == ALPHA || ctype[c] == DIGIT; c=ngetch(),i-- )
465 if( i > 0 )
466 *p++ = c;
467 if( i > 0 )
468 *p = '\0';
469 putback(c);
470 csp = lookup(sym);
471 if( csp->s_attrib & SRESWORD ) {
472 cvalue = csp->s_offset;
1File: LEX.C Page 9
473 return(RESWORD);
474 }
475 smember = 0;
476 return(SYMBOL);
477
478 case CAROT: /*^= or ^*/
479 return( peekis('=') ? EQXOR : XOR );
480
481 case BAR: /*|=, || or |*/
482 return( peekis('=') ? EQOR : peekis('|') ? LOR : OR );
483
484 }
485 }
486 return(EOF);
487 }
488
489 /*
490 * peekis - peeks at next character for specific character
491 * Gets next (possibly pushed back) character, if it matches
492 * the given character 1 is returned, otherwise the character
493 * is put back.
494 */
495 peekis(tc) /* returns 1 if match, 0 otherwise*/
496 int tc; /* test character*/
497 {
498 register int c;
499
500 if( (c=ngetch()) == tc )
501 return(1);
502 putback(c);
503 return(0);
504 }
505
506 /* ngetch - get a possibly pushed back character*/
507 /* Checks pbchar variable, returns it if non-zero, handles counting*/
508 /* of new lines and whether you are in an include or not.*/
509 ngetch() /* returns character read or EOF*/
510 {
511 register int c;
512 register char *ifile;
513
514 if( pbchar ) {
515 c = pbchar;
516 pbchar = 0;
517 }
518 else if( (c=getc(&ibuf)) == EOLC ) {
519 if( inclflag )
520 inclflag = 0;
521 else
522 lineno++;
523 }
524 else if( c == SOI) { /*[vlh]add incl filename & line # */
525 inclflag++;
526 ifile = &inclfile;
527 while ((c=getc(&ibuf)) != SOI)
528 *ifile++ = c&0377;
529 *ifile = 0;
530 inclline = getdec() & 077777;
531 c = ' ';
1File: LEX.C Page 10
532 }
533 else if( c < 0 )
534 c = EOF;
535 return(c);
536 }
537
538 /*
539 * peekc - peek at the next non-whitespace character after token
540 * This allows for the problem of having to look at two tokens
541 * at once. The second token is always a semi-colon or colon,
542 * so we only look at the single character, rather than going
543 * thru gettok.
544 */
545 peekc(tc) /* returns 1 if match, 0 otherwise*/
546 int tc; /* character to look for*/
547 {
548 register int c;
549
550 while( ctype[(c=ngetch())] == WHITSP) ;
551 if( c == tc )
552 return(1);
553 putback(c);
554 return(0);
555 }
556
557 /* putback - puts back a single character*/
558 /* Checks pbchar for error condition.*/
559 putback(c) /* returns - none*/
560 int c;
561 {
562 if( pbchar )
563 error("too many chars pushed back");
564 else
565 pbchar = c;
566 }
567
568 /* getstr - get a quoted (single or double) character string*/
569 /* Gets specified number of characters, handling escapes.*/
570 getstr(str,nchars,endc) /* returns - none*/
571 char *str; /* pointer to string buffer*/
572 int nchars; /* max number of characters*/
573 char endc; /* ending string character*/
574 {
575 register char *p;
576 register int i;
577 register int c;
578 register int j;
579
580 cstrsize = 1;
581 p = str;
582 for( i = nchars; (c=ngetch()) != endc; i-- ) {
583 if( c == EOF || c == EOLC ) {
584 error("string cannot cross line");
585 break;
586 }
587 if( c == '\\' ) {
588 if( (c=ngetch()) >= '0' && c <= '7' ) {
589 putback(c);
590 if( (c=getoct(1)) < 0 || c > 255 ) {
1File: LEX.C Page 11
591 error("bad character constant");
592 continue;
593 }
594 }
595 else if( (j=index("bnrt",c)) >= 0 )
596 c = escmap[j];
597 else if( c == EOLC ) /*escape followed by nl->ignore*/
598 continue;
599 }
600 if( i > 0 ) { /*room left in string?*/
601 cstrsize++;
602 *p++ = c;
603 }
604 else if( !i ) /*only say error once...*/
605 error("string too long");
606 }
607 if( i <= 0 ) /*string overflow?*/
608 p--;
609 *p = '\0';
610 }
611
612 /* syminit - initialize the symbol table, install reswords*/
613 /* Goes thru the resword table and installs them into the symbol*/
614 /* table.*/
615 syminit() /* returns - none*/
616 {
617 register struct resword *rp;
618
619 for( rp = &reswords[0]; rp->r_name != 0; rp++ )
620 install(rp->r_name,SRESWORD|SDEFINED,rp->r_value);
621 }
622
623 /* install - install a symbol in the symbol table*/
624 /* Allocates a symbol entry, copies info into it and links it*/
625 /* into the hash table chain.*/
626 char *install(sym,attrib,offset) /* returns pointer to symbol struct*/
627 char *sym; /* symbol to install*/
628 int attrib; /* attribues of symbol*/
629 int offset; /* symbol offset (resword value)*/
630 {
631 register struct symbol *sp;
632 register int i;
633
634 while( !(sp=symbols) ) {
635 if( !(sp=sbrk(SYMSIZE)) )
636 ferror("symbol table overflow");
637 for( i = SYMSIZE/(sizeof *symbols); --i >= 0; ) {
638 sp->s_next = symbols;
639 symbols = sp++;
640 }
641 }
642 symbols = sp->s_next;
643 sp->s_attrib = attrib;
644 sp->s_sc = 0; sp->s_type = 0; sp->s_dp = 0; sp->s_ssp = 0;
645 sp->s_offset = offset;
646 sp->s_struc = (instruct) ? strucptr[smember+instruct] : 0;
647 symcopy(sym,sp->s_symbol); /*copy symbol to symbol struct*/
648 i = symhash(sym,instruct|smember); /*link into chain list*/
649 sp->s_next = symtab[i];
1File: LEX.C Page 12
650 symtab[i] = sp;
651 return(sp);
652 }
653
654 /* lookup - looks up a symbol in symbol table*/
655 /* Hashes symbol, then goes thru chain, if not found, then*/
656 /* installs the symbol.*/
657 char *lookup(sym) /* returns pointer to symbol buffer*/
658 char *sym; /* pointer to symbol*/
659 {
660 register struct symbol *sp, *hold;
661 register char *p;
662 int exact; /* same name, diff type or offset */
663
664 p = sym;
665 for( sp = symtab[symhash(p,0)]; sp != 0; sp = sp->s_next )
666 if((sp->s_attrib&(SRESWORD|STYPEDEF)) && symequal(p,sp->s_symbol))
667 return(sp);
668 if (!(smember|instruct)) { /*[vlh]*/
669 for( sp=symtab[symhash(p,0)]; sp!=0; sp=sp->s_next )
670 if( symequal(p,sp->s_symbol) ) return(sp);
671 }
672 else { /* doing a declaration or an expression */
673 hold = 0; exact = 0;
674 for( sp=symtab[symhash(p,instruct|smember)]; sp!=0; sp=sp->s_next )
675 if( symequal(p,sp->s_symbol) )
676 if (symsame(sp,hold,&exact)) return(sp);
677 else if (!hold && !exact) hold = sp;
678 if (hold && !exact) return(hold);
679 }
680 return(install(p,0,0));
681 }
682
683 /* freesyms - frees all local symbols at end of function declaration*/
684 /* Searches thru symbol table, deleting all symbols marked as locals*/
685 freesyms() /* returns - none*/
686 {
687 register int i, tinfo;
688 register struct symbol *sp, *tp, *nextp, **htp;
689
690 for( htp = &symtab[0], i = HSIZE; --i >= 0; htp++ )
691 for( tp = 0, sp = *htp; sp != 0; sp = nextp ) {
692 nextp = sp->s_next;
693 if( !(sp->s_attrib&SDEFINED) ) {
694 error("undefined label: %.8s",sp->s_symbol);
695 sp->s_attrib =| SDEFINED;
696 }
697 if( sp->s_attrib & (SGLOBAL|SRESWORD) )
698 tp = sp;
699 else {
700 if( tp )
701 tp->s_next = sp->s_next;
702 else
703 *htp = sp->s_next;
704 sp->s_next = symbols;
705 symbols = sp;
706 }
707 }
708 }
1File: LEX.C Page 13
709
710 /* chksyms - checks symbol table for undefined symbols, etc.*/
711 /* Goes thru the symbol table checking for undeclared forward*/
712 /* referenced structures, and outputs local symbols for debugger.*/
713 chksyms() /* returns - none*/
714 {
715 register struct symbol **htp, *sp;
716 register int i, sc;
717
718 for( htp = &symtab[0], i = HSIZE; --i >= 0; htp++ )
719 for( sp = *htp; sp != 0; sp = sp->s_next ) {
720 sc = sp->s_sc;
721 if(sc!=0 && sp->s_ssp>=0 && (btype(sp->s_type))==FRSTRUCT) {
722 sp->s_ssp = frstab[sp->s_ssp]->s_ssp; /* 3.4 ssp>0 */
723 sp->s_type = (sp->s_type&~TYPE) | STRUCT;
724 }
725 if( sc == PDECLIST ) {
726 error("not in parameter list: %.8s",sp->s_symbol);
727 sp->s_sc = AUTO;
728 }
729 if( infunc )
730 outlocal(sp->s_type,sp->s_sc,sp->s_symbol,sp->s_offset);
731 }
732 }
733
734 /* symhash - compute hash value for symbol*/
735 /* Sums the symbols characters and takes that modulus the hash table*/
736 /* size.*/
737 symhash(sym,stel) /* returns hash value for symbol*/
738 char *sym; /* pointer to symbol*/
739 int stel; /* structure element flag*/
740 {
741 register char *p;
742 register int hashval, i;
743
744 hashval = (stel ? STEL : 0 );
745 for( p = sym, i = SSIZE; *p != '\0' && i > 0; i-- )
746 hashval =+ *p++;
747 return( hashval % HSIZE );
748 }
749
750 /* symequal - check for symbol equality*/
751 /* Does comparison between two symbols.*/
752 symequal(sym1,sym2) /* returns 1 if equal, 0 otherwise*/
753 char *sym1; /* pointer to first symbol*/
754 char *sym2; /* pointer to second symbol*/
755 {
756 register char *p, *q;
757 register int i;
758
759 for( p = sym1, q = sym2, i = SSIZE; *p == *q++; )
760 if( *p++ == '\0' || --i == 0 )
761 return(1);
762 return(0);
763 }
764
765 /* symsame - symbol member same as declared */
766 symsame(sp,hold,exact) /* [vlh] */
767 struct symbol *sp, *hold;
1File: LEX.C Page 14
768 int *exact;
769 {
770 if (strucptr[smember+instruct])
771 if (strucptr[smember+instruct]==sp->s_struc) return(1);
772 if (hold)
773 if (sp->s_type != hold->s_type || sp->s_offset != hold->s_offset)
774 *exact = 1;
775 return(0);
776 }
777
778 /* symcopy - symbol copy*/
779 /* Copies one symbol to another.*/
780 symcopy(sym1,sym2) /* returns - none*/
781 char *sym1; /* pointer to symbol to copy*/
782 char *sym2; /* pointer to area to copy to*/
783 {
784 register char *p, *q;
785 register int i;
786
787 for( p = sym1, q = sym2, i = SSIZE; --i >= 0; )
788 *q++ = ( *p ? *p++ : '\0');
789 }
790
791 /* index - find the index of a character in a string*/
792 /* This is identical to Software Tools index.*/
793 index(str,chr) /* returns index of c in str or -1*/
794 char *str; /* pointer to string to search*/
795 char chr; /* character to search for*/
796 {
797 register char *s;
798 register int i;
799
800 for( s = str, i = 0; *s != '\0'; i++ )
801 if( *s++ == chr )
802 return(i);
803 return(-1);
804 }
805
806 /* next - if next token matches given token, skip and return success*/
807 /* This allows for clean parsing of declarations.*/
808 next(tok) /* returns 1 if matched, 0 otherwise*/
809 int tok;
810 {
811 register int token;
812
813 if( (token=gettok()) == tok )
814 return(1);
815 peektok = token;
816 return(0);
817 }
818
819 /* pbtok - put back the given token*/
820 /* This merely sets the peektok variable*/
821 pbtok(tok) /* returns - none*/
822 int tok;
823 {
824 if( peektok )
825 error("too many tokens pushed back");
826 peektok = tok;
1File: LEX.C Page 15
827 }

View File

@@ -0,0 +1,6 @@
$1lo68 -r -f $1 -unofloat 0$1s.o DECL.o EXPR.o ICODE.o INTERF.o LEX.o MAIN.o STMT.o TABL.o 0$1lib6.a 0$1libF.a 0$1clib
era *.o
era c068.rel
ren c068.rel=c.out
user 13!make $1

View File

@@ -0,0 +1,39 @@
$ num
DECL.C
DECL.lis
$ num
EXPR.C
EXPR.lis
$ num
ICODE.C
ICODE.lis
$ num
INIT.C
INIT.lis
$ num
INTERF.C
INTERF.lis
$ num
LEX.C
LEX.lis
$ num
MAIN.C
MAIN.lis
$ num
STMT.C
STMT.lis
$ num
TABL.C
TABL.lis
$ num
VERSION.C
VERSION.lis
$ num
ICODE.H
ICODE.lst
$ num
MACHINE.H
MACHINE.lst
$ num
PARSER.H
PARSER.lst

View File

@@ -0,0 +1,10 @@
/*
* Use this file to determine what kind of machine you want the
* Alcyon stuff to run on ....
*/
/*#define MC68000 1*/ /* 68000 version */
/*#define VAX 1*/ /* VAX Version */
#define PDP11 1 /* PDP-11 Version*/
/*#define CPM 1*/ /* CP/M Operating System*/
#define UNIX 1 /* UNIX Operating System*/
/*#define VMS 1*/ /* VMS Operating System*/

View File

@@ -0,0 +1,10 @@
/*
* Use this file to determine what kind of machine you want the
* Alcyon stuff to run on ....
*/
#define MC68000 1 /* 68000 version */
/*#define VAX 1*/ /* VAX Version */
/*#define PDP11 1*/ /* PDP-11 Version*/
#define CPM 1 /* CP/M Operating System*/
/*#define UNIX 1*/ /* UNIX Operating System*/
/*#define VMS 1*/ /* VMS Operating System*/

View File

@@ -0,0 +1,10 @@
/*
* Use this file to determine what kind of machine you want the
* Alcyon stuff to run on ....
*/
/*#define MC68000 1*/ /* 68000 version */
#define VAX 1 /* VAX Version */
/*#define PDP11 1*/ /* PDP-11 Version*/
/*#define CPM 1*/ /* CP/M Operating System*/
/*#define UNIX 1*/ /* UNIX Operating System*/
#define VMS 1 /* VMS Operating System*/

View File

@@ -0,0 +1,11 @@
1File: MACHINE.H Page 1
1 /*
2 * Use this file to determine what kind of machine you want the
3 * Alcyon stuff to run on ....
4 */
5 /*#define MC68000 1*/ /* 68000 version */
6 #define VAX 1 /* VAX Version */
7 /*#define PDP11 1*/ /* PDP-11 Version*/
8 /*#define CPM 1*/ /* CP/M Operating System*/
9 /*#define UNIX 1*/ /* UNIX Operating System*/
10 #define VMS 1 /* VMS Operating System*/

View File

@@ -0,0 +1,10 @@
/*
* Use this file to determine what kind of machine you want the
* Alcyon stuff to run on ....
*/
/*#define MC68000 1*/ /* 68000 version */
#define VAX 1 /* VAX Version */
/*#define PDP11 1*/ /* PDP-11 Version*/
/*#define CPM 1*/ /* CP/M Operating System*/
/*#define UNIX 1*/ /* UNIX Operating System*/
#define VMS 1 /* VMS Operating System*/

View File

@@ -0,0 +1,173 @@
/*
Copyright 1982
Alcyon Corporation
8716 Production Ave.
San Diego, Ca. 92121
*/
char *version "@(#) c068 parser 4.0 - Feb 11, 1983";
/*
ALCYON C Compiler for the Motorola 68000 - Parser
Called from c68:
c068 source icode strings
source: input source code, preprocessed with comments stripped
icode: contains the intermediate code for the code generator,
for a detailed explanaion see ../doc/icode.
strings: contains all the string constants.
The basic structure of the parser is as follows:
main main driver for parser
syminit initializes symbol table
doextdef external definition syntax
getatt get type attributes
dlist declaration list for structures/unions
getatt recursive gettype call
dodecl do one declaration
declarator handle declarator syntax
dodecl do one external declaraion
initlist external initialization list
cexpr constant expressions
expr arithmetic expressions
maketree build operator tree
funcbody function body
dlist declaration list
stmt function statements
stmt recursive stmt call
expr arithmetic expressions
*/
#include "parser.h"
int nextlabel 1;
int lineno;
char *exprp &exprarea[0];
/*
* main - main routine for parser
* Checks arguments, opens input and output files, does main loop
* for external declarations and blocks.
*/
main(argc,argv) /* returns - none*/
int argc; /* argument count*/
char *argv[]; /* argument pointers*/
{
register char *q;
register int i;
for( i = 4; i < argc; i++ ) {
q = argv[i];
if( *q++ != '-' )
usage();
while( 1 ) {
switch( *q++ ) {
case 'F':
case 'f':
fflag++;
continue;
case 'E':
case 'e':
eflag++;
continue;
case 'W':
case 'w':
wflag++;
continue;
case '\0':
break;
default:
usage();
}
break;
}
}
if( argc < 4 )
usage();
if( fopen(argv[1],&ibuf,0) < 0 ) /* 3rd arg for versados */
ferror("can't open %s",argv[i]);
if( fcreat(argv[2],&obuf,0) < 0 || fcreat(argv[3],&sbuf,0) < 0 )
ferror("temp creation error");
obp = &obuf;
lineno++;
frstp = -1; /* [vlh] 3.4 - initialize only once */
syminit();
while( !peek(EOF) )
doextdef();
outeof();
outdata();
copysfile(argv[3]);
exit(errcnt!=0);
}
/* usage - output usage error message and die*/
usage()
{
ferror("usage: c068 source icode str [-e] [-f] [-w]");
}
/* error - report an error message*/
/* outputs current line number and error message*/
error(s,x1,x2,x3,x4,x5,x6) /* returns - none*/
char *s; /* error message*/
int x1, x2, x3, x4, x5, x6; /* args for printf*/
{
register char *savep;
savep = obp;
obp = 0;
errcnt++;
if (!inclflag) {
if( lineno )
printf("* %d: ",lineno);
}
else /*[vlh] generate filename and approp line #*/
printf("%s: * %d: ",inclfile,inclline);
printf(s,x1,x2,x3,x4,x5,x6);
printf("\n");
obp = savep;
}
/* ferror - fatal error*/
/* Outputs error message and exits*/
ferror(s,x1,x2,x3,x4,x5,x6) /* returns - none*/
char *s; /* error message*/
int x1, x2, x3, x4, x5, x6; /* args for printf*/
{
error(s,x1,x2,x3,x4,x5,x6);
exit(1);
}
v6flush(v6buf)
struct iobuf *v6buf;
{
register i;
i = BLEN - v6buf->nunused;
v6buf->nunused = BLEN;
v6buf->xfree = &(v6buf->buff[0]);
if(write(v6buf->fildes,v6buf->xfree,i) != i)
return(-1);
return(0);
}
printf(string,a,b,c,d,e,f,g)
char *string;
int a,b,c,d,e,f,g;
{
char area[256];
register char *p;
sprintf(area,string,a,b,c,d,e,f,g);
for(p = &area[0]; *p; p++)
putchar(*p);
}

View File

@@ -0,0 +1,176 @@
1File: MAIN.C Page 1
1 /*
2 Copyright 1982
3 Alcyon Corporation
4 8716 Production Ave.
5 San Diego, Ca. 92121
6 */
7
8 char *version "@(#) c068 parser 4.0 - Feb 11, 1983";
9 /*
10 ALCYON C Compiler for the Motorola 68000 - Parser
11
12 Called from c68:
13
14 c068 source icode strings
15
16 source: input source code, preprocessed with comments stripped
17
18 icode: contains the intermediate code for the code generator,
19 for a detailed explanaion see ../doc/icode.
20
21 strings: contains all the string constants.
22
23 The basic structure of the parser is as follows:
24
25 main main driver for parser
26 syminit initializes symbol table
27 doextdef external definition syntax
28 getatt get type attributes
29 dlist declaration list for structures/unions
30 getatt recursive gettype call
31 dodecl do one declaration
32 declarator handle declarator syntax
33 dodecl do one external declaraion
34 initlist external initialization list
35 cexpr constant expressions
36 expr arithmetic expressions
37 maketree build operator tree
38 funcbody function body
39 dlist declaration list
40 stmt function statements
41 stmt recursive stmt call
42 expr arithmetic expressions
43
44 */
45
46 #include "parser.h"
47
48 int nextlabel 1;
49 int lineno;
50 char *exprp &exprarea[0];
51
52 /*
53 * main - main routine for parser
54 * Checks arguments, opens input and output files, does main loop
55 * for external declarations and blocks.
56 */
57 main(argc,argv) /* returns - none*/
58 int argc; /* argument count*/
59 char *argv[]; /* argument pointers*/
1File: MAIN.C Page 2
60 {
61 register char *q;
62 register int i;
63
64 for( i = 4; i < argc; i++ ) {
65 q = argv[i];
66 if( *q++ != '-' )
67 usage();
68 while( 1 ) {
69 switch( *q++ ) {
70
71 case 'F':
72 case 'f':
73 fflag++;
74 continue;
75
76 case 'E':
77 case 'e':
78 eflag++;
79 continue;
80
81 case 'W':
82 case 'w':
83 wflag++;
84 continue;
85
86 case '\0':
87 break;
88
89 default:
90 usage();
91 }
92 break;
93 }
94 }
95 if( argc < 4 )
96 usage();
97 if( fopen(argv[1],&ibuf,0) < 0 ) /* 3rd arg for versados */
98 ferror("can't open %s",argv[i]);
99 if( fcreat(argv[2],&obuf,0) < 0 || fcreat(argv[3],&sbuf,0) < 0 )
100 ferror("temp creation error");
101 obp = &obuf;
102 lineno++;
103 frstp = -1; /* [vlh] 3.4 - initialize only once */
104 syminit();
105 while( !peek(EOF) )
106 doextdef();
107 outeof();
108 outdata();
109 copysfile(argv[3]);
110 exit(errcnt!=0);
111 }
112
113 /* usage - output usage error message and die*/
114 usage()
115 {
116 ferror("usage: c068 source icode str [-e] [-f] [-w]");
117 }
118
1File: MAIN.C Page 3
119 /* error - report an error message*/
120 /* outputs current line number and error message*/
121 error(s,x1,x2,x3,x4,x5,x6) /* returns - none*/
122 char *s; /* error message*/
123 int x1, x2, x3, x4, x5, x6; /* args for printf*/
124 {
125 register char *savep;
126
127 savep = obp;
128 obp = 0;
129 errcnt++;
130 if (!inclflag) {
131 if( lineno )
132 printf("* %d: ",lineno);
133 }
134 else /*[vlh] generate filename and approp line #*/
135 printf("%s: * %d: ",inclfile,inclline);
136 printf(s,x1,x2,x3,x4,x5,x6);
137 printf("\n");
138 obp = savep;
139 }
140
141 /* ferror - fatal error*/
142 /* Outputs error message and exits*/
143 ferror(s,x1,x2,x3,x4,x5,x6) /* returns - none*/
144 char *s; /* error message*/
145 int x1, x2, x3, x4, x5, x6; /* args for printf*/
146 {
147 error(s,x1,x2,x3,x4,x5,x6);
148 exit(1);
149 }
150
151 v6flush(v6buf)
152 struct iobuf *v6buf;
153 {
154 register i;
155
156 i = BLEN - v6buf->nunused;
157 v6buf->nunused = BLEN;
158 v6buf->xfree = &(v6buf->buff[0]);
159 if(write(v6buf->fildes,v6buf->xfree,i) != i)
160 return(-1);
161 return(0);
162 }
163 printf(string,a,b,c,d,e,f,g)
164 char *string;
165 int a,b,c,d,e,f,g;
166 {
167 char area[256];
168 register char *p;
169
170 sprintf(area,string,a,b,c,d,e,f,g);
171 for(p = &area[0]; *p; p++)
172 putchar(*p);
173 }

View File

@@ -0,0 +1,86 @@
$1stat machine.h=rw
$1pip machine.h=machine.68k
$1cp68 -i 0$1 DECL.c $1x.i
$1c068 $1x.i $1x.ic $1x.st -f
$1c168 $1x.ic DECL.s -LD
era $1x.i
era $1x.ic
era $1x.st
$1as68 -s 0$1 -f $1 -l -u DECL.s
era DECL.s
$1cp68 -i 0$1 EXPR.c $1x.i
$1c068 $1x.i $1x.ic $1x.st -f
$1c168 $1x.ic EXPR.s -LD
era $1x.i
era $1x.ic
era $1x.st
$1as68 -s 0$1 -f $1 -l -u EXPR.s
era EXPR.s
$1cp68 -i 0$1 ICODE.c $1x.i
$1c068 $1x.i $1x.ic $1x.st -f
$1c168 $1x.ic ICODE.s -LD
era $1x.i
era $1x.ic
era $1x.st
$1as68 -s 0$1 -f $1 -l -u ICODE.s
era ICODE.s
$1cp68 -i 0$1 INTERF.c $1x.i
$1c068 $1x.i $1x.ic $1x.st -f
$1c168 $1x.ic INTERF.s -LD
era $1x.i
era $1x.ic
era $1x.st
$1as68 -s 0$1 -f $1 -l -u INTERF.s
era INTERF.s
$1cp68 -i 0$1 LEX.c $1x.i
$1c068 $1x.i $1x.ic $1x.st -f
$1c168 $1x.ic LEX.s -LD
era $1x.i
era $1x.ic
era $1x.st
$1as68 -s 0$1 -f $1 -l -u LEX.s
era LEX.s
$1cp68 -i 0$1 MAIN.c $1x.i
$1c068 $1x.i $1x.ic $1x.st -f
$1c168 $1x.ic MAIN.s -LD
era $1x.i
era $1x.ic
era $1x.st
$1as68 -s 0$1 -f $1 -l -u MAIN.s
era MAIN.s
$1cp68 -i 0$1 STMT.c $1x.i
$1c068 $1x.i $1x.ic $1x.st -f
$1c168 $1x.ic STMT.s -LD
era $1x.i
era $1x.ic
era $1x.st
$1as68 -s 0$1 -f $1 -l -u STMT.s
era STMT.s
$1cp68 -i 0$1 TABL.c $1x.i
$1c068 $1x.i $1x.ic $1x.st -f
$1c168 $1x.ic TABL.s -LD
era $1x.i
era $1x.ic
era $1x.st
$1as68 -s 0$1 -f $1 -l -u TABL.s
era TABL.s
$1cp68 -i 0$1 VERSION.c $1x.i
$1c068 $1x.i $1x.ic $1x.st -f
$1c168 $1x.ic VERSION.s -LD
era $1x.i
era $1x.ic
era $1x.st
$1as68 -s 0$1 -f $1 -l -u VERSION.s
era VERSION.s
link $1

View File

@@ -0,0 +1,320 @@
/*
Copyright 1982
Alcyon Corporation
8716 Production Ave.
San Diego, Ca. 92121
*/
/*
C68 Parser - include file
*/
#include <stdio.h>
#include <klib.h>
#undef putchar
#define putchar xputchar
#undef ferror
#define printf xprintf
#include "icode.h"
/*symbol attribute fields*/
#define SRESWORD 001 /*is symbol a reserved word?*/
#define SGLOBAL 002 /*is symbol global?*/
#define STYPEDEF 004 /*typedef declaration?*/
#define SDEFINED 010 /*symbol defined?*/
/*reserved words*/
#define R_AUTO 1
#define R_BREAK 2
#define R_CASE 3
#define R_CHAR 4
#define R_CONTINUE 5
#define R_DO 6
#define R_DEFAULT 7
#define R_DOUBLE 8
#define R_GOTO 9
#define R_ELSE 10
#define R_EXTERNAL 11
#define R_FLOAT 12
#define R_FOR 13
#define R_IF 14
#define R_INT 15
#define R_LONG 16
#define R_REGISTER 17
#define R_RETURN 18
#define R_SHORT 19
#define R_SIZEOF 20
#define R_STATIC 21
#define R_STRUCT 22
#define R_SWITCH 23
#define R_TYPEDEF 24
#define R_UNION 25
#define R_UNSIGNED 26
#define R_WHILE 27
/*
* mixed-mode conversions, entries in 2-d array indexed by:
* (int,unsn,long,doub,ptr)
*/
#define INT_CHAR 1
#define UNSN_CHAR 1
#define LONG_CHAR 1
#define DOUB_CHAR 1
#define PTR_CHAR 1
#define INT_UNSN 0 /*no conversion is generated*/
#define INT_LONG 2
#define INT_DOUB 3
#define INT_PTR 4
#define UNSN_INT 0 /*no conversion is generated*/
#define UNSN_LONG 6
#define UNSN_DOUB 7
#define UNSN_PTR 8
#define LONG_INT 9
#define LONG_UNSN 10
#define LONG_DOUB 11
#define LONG_PTR 12
#define DOUB_INT 13
#define DOUB_UNSN 14
#define DOUB_LONG 15
#define PTR_INT 16
#define PTR_UNSN 17
#define PTR_LONG 18
#define PTR_PTR 19
#define BADCONV 20
/* miscellaneous constants */
#define OPSSIZE 40 /*operator stack size*/
#define OPDSIZE 80 /*operand stack size*/
#define HSIZE 517 /*hash table size, 3.4 made prime */
#define SYMSIZE 1024 /*size to alloc for symbol structures*/
#define SWSIZE 256 /*max no. of cases in a switch*/
#define DSIZE 1000 /*dimension table size*/
#define BITSPWORD 16 /*bits per word*/
#define AREGLO 010 /*A reg flag*/
#define HICREG 2 /*highest reg # used for code generation*/
#define BITSPCHAR 8 /*bits per char*/
#define CHRSPWORD 2 /*chars per word*/
#define STRSIZE 300 /*max string length*/
#define NFARGS 40 /*max no. of args to function*/
#define NFRSTR 20 /*max no. of forward ref struct proto*/
/*symbol table node*/
struct symbol {
char s_attrib; /* defined, resword, global, typedef */
char s_sc; /* auto, static, external, register */
int s_type; /* 4bits specified, 2 bit fields for ptr... */
int s_dp; /* index into dimension table */
int s_ssp; /* dimension table/function arg table */
int s_offset; /* offset inside of structure */
char s_symbol[SSIZE]; /* symbol identifier, to SSIZE chars */
struct symbol *s_struc; /* if struct, ptr to parent (sys III) */
struct symbol *s_next; /* next symbol table entry */
};
/*expression tree operator node*/
struct tnode {
int t_op;
int t_type;
int t_dp;
int t_ssp;
struct tnode *t_left;
struct tnode *t_right;
};
/*expression tree node for symbol - only keeps location*/
struct symnode {
int t_op;
int t_type; /*data type of symbol*/
int t_dp; /*dimension pointer of symbol*/
int t_ssp; /*structure size index to dtab*/
int t_sc; /*storage class of symbol*/
int t_offset; /*offset of symbol*/
int t_label;
};
/*expressioon tree node for external symbol - need to keep name*/
struct extnode {
int t_op;
int t_type;
int t_dp;
int t_ssp;
int t_sc;
int t_offset;
int t_reg;
int t_symbol[SSIZE]; /*symbol name*/
};
/*expression tree node for integer constant*/
struct conode {
int t_op;
int t_type;
int t_dp;
int t_ssp;
int t_value; /*constant value*/
};
struct lconode {
int t_op;
int t_type;
int t_dp;
int t_ssp;
long t_lvalue; /*constant value*/
};
struct swtch {
int sw_label;
int sw_value;
} swtab[SWSIZE];
/*operator and operand stack used by expr*/
struct ops { /*operator stack*/
int o_op; /*operator*/
int o_pri; /*priority*/
} opstack[OPSSIZE], *opp;
char *opdstack[OPDSIZE]; /*operand stack*/
char **opdp; /*operand stack pointer*/
char *opap; /*ptr to next available loc in exprarea*/
char *exprp; /*place to start building expression*/
int opinfo[]; /*operator info table*/
struct tnode *frp; /*pointer to function return info node*/
int swp; /*current entry in switch table*/
int cswp; /*current low switch table index*/
int nextlabel; /*generates unique label numbers*/
int clabel; /*continue label*/
int blabel; /*break label*/
int rlabel; /*return label*/
int dlabel; /*default label*/
int lineno; /*current line number of input*/
int errcnt; /*count of errors*/
int inclflag; /*in include file, don't incr line #'s*/
int inclline; /*[vlh]line# in incl file for err rpting*/
char inclfile[13]; /*[vlh]include filename for err rpting*/
int equalstop; /*stop lex at '=', used for external init*/
int commastop; /*stop parse @ comma(used for const expr)*/
int colonstop; /*stop parse @ colon(used for case value)*/
int instruct; /*set when in structure declaration*/
int smember; /*set when seen . or ->*/
int infunc; /*set when in function body*/
int tdflag; /*declaration is a typedef proto*/
char *tdp; /*points to typedef prototype*/
int localsize; /*length of local variables*/
int naregs; /*keeps track of ptr registers alloc'd*/
int ndregs; /*keep track of data registers alloc'd*/
int loadreg; /*need to load registers...*/
int boffset; /*current bit offset in structure*/
int eflag; /*[vlh] 3.4 IEEE floats */
int fflag; /*[vlh] 3.4 FFP floats */
int xflag; /*translate int's to long's*/
int wflag; /*[vlh] don't generate warning mesgs*/
int reducep; /*[vlh] if(procid); reduction*/
int peektok; /*peeked at token*/
/*dimension table*/
long dtab[DSIZE]; /* [vlh] 3.4 int => long */
int cdp; /*next entry in dtab to alloc*/
/*lexical analyzer values*/
int cvalue; /*current token value if keyword or CINT*/
int cstrsize; /*current string size*/
long clvalue; /*current token value if long constant*/
struct symbol *csp; /*current token symbol ptr if SYMBOL*/
char cstr[STRSIZE]; /*current token value if CSTRING*/
struct symbol *dsp; /*declarator symbol pointer*/
/* -1 -> not instruct, 0 -> unnamed struct */
struct symbol *strucptr[10]; /*[vlh] ptrs to struc symbols*/
/*function argument table, used to collect function parameters*/
struct farg {
struct symbol *f_sp;
int f_offset;
} fargtab[NFARGS];
/*forward referenced structure prototype names*/
struct symbol *frstab[NFRSTR];
int frstp;
/*output buffers for intermediate code and strings*/
struct io_buf obuf, sbuf, ibuf, *obp;
#define stypedef(sp) (sp->s_attrib&STYPEDEF)
#define walign(add) ((add+1)&(~1))
#define array(type) ((type&SUPTYP)==ARRAY)
#define function(type) ((type&SUPTYP)==FUNCTION)
#define pointer(type) ((type&SUPTYP)==POINTER)
#define notarray(type) ((type&SUPTYP)!=ARRAY)
#define notfunction(type) ((type&SUPTYP)!=FUNCTION)
#define notpointer(type) ((type&SUPTYP)!=POINTER)
#define btype(type) (type&TYPE)
#define suptype(type) (type&SUPTYP)
#define alltype(type) (type&(SUPTYP|TYPE))
#define asgop(op) ((opinfo[op]&OPASSIGN)!=0)
#define relop(op) ((opinfo[op]&OPREL)!=0)
#define lintegral(op) ((opinfo[op]&OPLWORD)!=0)
#define rintegral(op) ((opinfo[op]&OPRWORD)!=0)
#define rasop(op) ((opinfo[op]&OPRAS)!=0)
#define binop(op) ((opinfo[op]&OPBIN)!=0)
#define unaryop(op) ((opinfo[op]&OPBIN)==0)
#define leaf(op) ((opinfo[op]&OPTERM)!=0)
#define lvalop(op) ((opinfo[op]&OPLVAL)!=0)
#define oppriority(op) (opinfo[op]&OPPRI)
#define makeiop(op) (op|(0254<<8))
/* checks for symbol with structure element storage class */
#define isstel(tp) (tp->t_op==SYMBOL && (sesc(tp)))
#define sesc(t) (t->t_sc==STELCL||t->t_sc==UNELCL||t->t_sc==BFIELDCL)
/* peek at next token, if not read token put back, else delete */
/* 1 if matched, 0 otherwise */
#define peek(tok) ( (peektok=gettok()) == tok )
#define outcommon(sym,size) printf("\t.comm _%.8s,%ld\n",sym,size)
#define outgoto(lab) if( lab > 0 ) printf("\tbra L%d\n",lab)
/* change to text segment */
#define outtext() printf("\t.text\n")
/* change segment to bss */
#define outbss() printf("\t.bss\n")
/* output global symbol references */
#define outextdef(sym) printf("\t.globl _%.8s\n",sym)
/* outputs reserved memory [vlh] 3.4 %d => %ld */
#define outresmem(size) printf("\t.ds.b %ld\n",size)
/* output padding for word alignments */
#define outpad() printf("\t.even\n")
/* output long constant to assembler */
#define outlcon(val) printf("\t.dc.l %d\n",val)
/* output label constant */
#define outclab(lab) printf("\t.dc.l L%d\n",lab)
/* output a label */
#define outlab(lab) printf("\tL%d:",lab)
/* output function label */
#define outflab(sym) printf("\t_%.8s:\n\t~~%.8s:\n",sym,sym)
/* output data label */
#define outdlab(sym) printf("\t_%.8s:\n",sym)
/*functions returning pointers*/
char *expr();
char *talloc();
char *tnalloc();
char *enalloc();
char *snalloc();
char *cnalloc();
char *lcnalloc();
char *fpcnalloc();
char *popopd();
char *cvopgen();
char *arrayref();
char *funcref();
char *install();
char *lookup();
char *balpar();
char *sbrk();
long initlist();
long dsize();
long psize();
long dodecl();
long dlist();
long getdec();
long gethex();
long getoct();
long getfp();
long toieee();
long toffp();

View File

@@ -0,0 +1,326 @@
1File: PARSER.H Page 1
1 /*
2 Copyright 1982
3 Alcyon Corporation
4 8716 Production Ave.
5 San Diego, Ca. 92121
6 */
7
8 /*
9 C68 Parser - include file
10 */
11 #include <stdio.h>
12 #include <klib.h>
13 #undef putchar
14 #define putchar xputchar
15 #undef ferror
16 #define printf xprintf
17 #include "icode.h"
18
19 /*symbol attribute fields*/
20 #define SRESWORD 001 /*is symbol a reserved word?*/
21 #define SGLOBAL 002 /*is symbol global?*/
22 #define STYPEDEF 004 /*typedef declaration?*/
23 #define SDEFINED 010 /*symbol defined?*/
24
25 /*reserved words*/
26 #define R_AUTO 1
27 #define R_BREAK 2
28 #define R_CASE 3
29 #define R_CHAR 4
30 #define R_CONTINUE 5
31 #define R_DO 6
32 #define R_DEFAULT 7
33 #define R_DOUBLE 8
34 #define R_GOTO 9
35 #define R_ELSE 10
36 #define R_EXTERNAL 11
37 #define R_FLOAT 12
38 #define R_FOR 13
39 #define R_IF 14
40 #define R_INT 15
41 #define R_LONG 16
42 #define R_REGISTER 17
43 #define R_RETURN 18
44 #define R_SHORT 19
45 #define R_SIZEOF 20
46 #define R_STATIC 21
47 #define R_STRUCT 22
48 #define R_SWITCH 23
49 #define R_TYPEDEF 24
50 #define R_UNION 25
51 #define R_UNSIGNED 26
52 #define R_WHILE 27
53
54 /*
55 * mixed-mode conversions, entries in 2-d array indexed by:
56 * (int,unsn,long,doub,ptr)
57 */
58 #define INT_CHAR 1
59 #define UNSN_CHAR 1
1File: PARSER.H Page 2
60 #define LONG_CHAR 1
61 #define DOUB_CHAR 1
62 #define PTR_CHAR 1
63 #define INT_UNSN 0 /*no conversion is generated*/
64 #define INT_LONG 2
65 #define INT_DOUB 3
66 #define INT_PTR 4
67 #define UNSN_INT 0 /*no conversion is generated*/
68 #define UNSN_LONG 6
69 #define UNSN_DOUB 7
70 #define UNSN_PTR 8
71 #define LONG_INT 9
72 #define LONG_UNSN 10
73 #define LONG_DOUB 11
74 #define LONG_PTR 12
75 #define DOUB_INT 13
76 #define DOUB_UNSN 14
77 #define DOUB_LONG 15
78 #define PTR_INT 16
79 #define PTR_UNSN 17
80 #define PTR_LONG 18
81 #define PTR_PTR 19
82 #define BADCONV 20
83
84 /* miscellaneous constants */
85 #define OPSSIZE 40 /*operator stack size*/
86 #define OPDSIZE 80 /*operand stack size*/
87 #define HSIZE 517 /*hash table size, 3.4 made prime */
88 #define SYMSIZE 1024 /*size to alloc for symbol structures*/
89 #define SWSIZE 256 /*max no. of cases in a switch*/
90 #define DSIZE 1000 /*dimension table size*/
91 #define BITSPWORD 16 /*bits per word*/
92 #define AREGLO 010 /*A reg flag*/
93 #define HICREG 2 /*highest reg # used for code generation*/
94 #define BITSPCHAR 8 /*bits per char*/
95 #define CHRSPWORD 2 /*chars per word*/
96 #define STRSIZE 300 /*max string length*/
97 #define NFARGS 40 /*max no. of args to function*/
98 #define NFRSTR 20 /*max no. of forward ref struct proto*/
99
100 /*symbol table node*/
101 struct symbol {
102 char s_attrib; /* defined, resword, global, typedef */
103 char s_sc; /* auto, static, external, register */
104 int s_type; /* 4bits specified, 2 bit fields for ptr... */
105 int s_dp; /* index into dimension table */
106 int s_ssp; /* dimension table/function arg table */
107 int s_offset; /* offset inside of structure */
108 char s_symbol[SSIZE]; /* symbol identifier, to SSIZE chars */
109 struct symbol *s_struc; /* if struct, ptr to parent (sys III) */
110 struct symbol *s_next; /* next symbol table entry */
111 };
112
113 /*expression tree operator node*/
114 struct tnode {
115 int t_op;
116 int t_type;
117 int t_dp;
118 int t_ssp;
1File: PARSER.H Page 3
119 struct tnode *t_left;
120 struct tnode *t_right;
121 };
122
123 /*expression tree node for symbol - only keeps location*/
124 struct symnode {
125 int t_op;
126 int t_type; /*data type of symbol*/
127 int t_dp; /*dimension pointer of symbol*/
128 int t_ssp; /*structure size index to dtab*/
129 int t_sc; /*storage class of symbol*/
130 int t_offset; /*offset of symbol*/
131 int t_label;
132 };
133
134 /*expressioon tree node for external symbol - need to keep name*/
135 struct extnode {
136 int t_op;
137 int t_type;
138 int t_dp;
139 int t_ssp;
140 int t_sc;
141 int t_offset;
142 int t_reg;
143 int t_symbol[SSIZE]; /*symbol name*/
144 };
145
146 /*expression tree node for integer constant*/
147 struct conode {
148 int t_op;
149 int t_type;
150 int t_dp;
151 int t_ssp;
152 int t_value; /*constant value*/
153 };
154
155 struct lconode {
156 int t_op;
157 int t_type;
158 int t_dp;
159 int t_ssp;
160 long t_lvalue; /*constant value*/
161 };
162
163 struct swtch {
164 int sw_label;
165 int sw_value;
166 } swtab[SWSIZE];
167
168 /*operator and operand stack used by expr*/
169 struct ops { /*operator stack*/
170 int o_op; /*operator*/
171 int o_pri; /*priority*/
172 } opstack[OPSSIZE], *opp;
173
174 char *opdstack[OPDSIZE]; /*operand stack*/
175 char **opdp; /*operand stack pointer*/
176 char *opap; /*ptr to next available loc in exprarea*/
177 char *exprp; /*place to start building expression*/
1File: PARSER.H Page 4
178 int opinfo[]; /*operator info table*/
179 struct tnode *frp; /*pointer to function return info node*/
180 int swp; /*current entry in switch table*/
181 int cswp; /*current low switch table index*/
182 int nextlabel; /*generates unique label numbers*/
183 int clabel; /*continue label*/
184 int blabel; /*break label*/
185 int rlabel; /*return label*/
186 int dlabel; /*default label*/
187 int lineno; /*current line number of input*/
188 int errcnt; /*count of errors*/
189 int inclflag; /*in include file, don't incr line #'s*/
190 int inclline; /*[vlh]line# in incl file for err rpting*/
191 char inclfile[13]; /*[vlh]include filename for err rpting*/
192 int equalstop; /*stop lex at '=', used for external init*/
193 int commastop; /*stop parse @ comma(used for const expr)*/
194 int colonstop; /*stop parse @ colon(used for case value)*/
195 int instruct; /*set when in structure declaration*/
196 int smember; /*set when seen . or ->*/
197 int infunc; /*set when in function body*/
198 int tdflag; /*declaration is a typedef proto*/
199 char *tdp; /*points to typedef prototype*/
200 int localsize; /*length of local variables*/
201 int naregs; /*keeps track of ptr registers alloc'd*/
202 int ndregs; /*keep track of data registers alloc'd*/
203 int loadreg; /*need to load registers...*/
204 int boffset; /*current bit offset in structure*/
205 int eflag; /*[vlh] 3.4 IEEE floats */
206 int fflag; /*[vlh] 3.4 FFP floats */
207 int xflag; /*translate int's to long's*/
208 int wflag; /*[vlh] don't generate warning mesgs*/
209 int reducep; /*[vlh] if(procid); reduction*/
210 int peektok; /*peeked at token*/
211
212 /*dimension table*/
213 long dtab[DSIZE]; /* [vlh] 3.4 int => long */
214 int cdp; /*next entry in dtab to alloc*/
215
216 /*lexical analyzer values*/
217 int cvalue; /*current token value if keyword or CINT*/
218 int cstrsize; /*current string size*/
219 long clvalue; /*current token value if long constant*/
220 struct symbol *csp; /*current token symbol ptr if SYMBOL*/
221 char cstr[STRSIZE]; /*current token value if CSTRING*/
222 struct symbol *dsp; /*declarator symbol pointer*/
223
224 /* -1 -> not instruct, 0 -> unnamed struct */
225 struct symbol *strucptr[10]; /*[vlh] ptrs to struc symbols*/
226
227 /*function argument table, used to collect function parameters*/
228 struct farg {
229 struct symbol *f_sp;
230 int f_offset;
231 } fargtab[NFARGS];
232
233 /*forward referenced structure prototype names*/
234 struct symbol *frstab[NFRSTR];
235 int frstp;
236
1File: PARSER.H Page 5
237 /*output buffers for intermediate code and strings*/
238 struct io_buf obuf, sbuf, ibuf, *obp;
239
240 #define stypedef(sp) (sp->s_attrib&STYPEDEF)
241 #define walign(add) ((add+1)&(~1))
242 #define array(type) ((type&SUPTYP)==ARRAY)
243 #define function(type) ((type&SUPTYP)==FUNCTION)
244 #define pointer(type) ((type&SUPTYP)==POINTER)
245 #define notarray(type) ((type&SUPTYP)!=ARRAY)
246 #define notfunction(type) ((type&SUPTYP)!=FUNCTION)
247 #define notpointer(type) ((type&SUPTYP)!=POINTER)
248 #define btype(type) (type&TYPE)
249 #define suptype(type) (type&SUPTYP)
250 #define alltype(type) (type&(SUPTYP|TYPE))
251 #define asgop(op) ((opinfo[op]&OPASSIGN)!=0)
252 #define relop(op) ((opinfo[op]&OPREL)!=0)
253 #define lintegral(op) ((opinfo[op]&OPLWORD)!=0)
254 #define rintegral(op) ((opinfo[op]&OPRWORD)!=0)
255 #define rasop(op) ((opinfo[op]&OPRAS)!=0)
256 #define binop(op) ((opinfo[op]&OPBIN)!=0)
257 #define unaryop(op) ((opinfo[op]&OPBIN)==0)
258 #define leaf(op) ((opinfo[op]&OPTERM)!=0)
259 #define lvalop(op) ((opinfo[op]&OPLVAL)!=0)
260 #define oppriority(op) (opinfo[op]&OPPRI)
261 #define makeiop(op) (op|(0254<<8))
262 /* checks for symbol with structure element storage class */
263 #define isstel(tp) (tp->t_op==SYMBOL && (sesc(tp)))
264 #define sesc(t) (t->t_sc==STELCL||t->t_sc==UNELCL||t->t_sc==BFIELDCL)
265 /* peek at next token, if not read token put back, else delete */
266 /* 1 if matched, 0 otherwise */
267 #define peek(tok) ( (peektok=gettok()) == tok )
268
269 #define outcommon(sym,size) printf("\t.comm _%.8s,%ld\n",sym,size)
270 #define outgoto(lab) if( lab > 0 ) printf("\tbra L%d\n",lab)
271 /* change to text segment */
272 #define outtext() printf("\t.text\n")
273 /* change segment to bss */
274 #define outbss() printf("\t.bss\n")
275 /* output global symbol references */
276 #define outextdef(sym) printf("\t.globl _%.8s\n",sym)
277 /* outputs reserved memory [vlh] 3.4 %d => %ld */
278 #define outresmem(size) printf("\t.ds.b %ld\n",size)
279 /* output padding for word alignments */
280 #define outpad() printf("\t.even\n")
281 /* output long constant to assembler */
282 #define outlcon(val) printf("\t.dc.l %d\n",val)
283 /* output label constant */
284 #define outclab(lab) printf("\t.dc.l L%d\n",lab)
285 /* output a label */
286 #define outlab(lab) printf("\tL%d:",lab)
287 /* output function label */
288 #define outflab(sym) printf("\t_%.8s:\n\t~~%.8s:\n",sym,sym)
289 /* output data label */
290 #define outdlab(sym) printf("\t_%.8s:\n",sym)
291
292 /*functions returning pointers*/
293 char *expr();
294 char *talloc();
295 char *tnalloc();
1File: PARSER.H Page 6
296 char *enalloc();
297 char *snalloc();
298 char *cnalloc();
299 char *lcnalloc();
300 char *fpcnalloc();
301 char *popopd();
302 char *cvopgen();
303 char *arrayref();
304 char *funcref();
305 char *install();
306 char *lookup();
307 char *balpar();
308 char *sbrk();
309
310 long initlist();
311 long dsize();
312 long psize();
313 long dodecl();
314 long dlist();
315 long getdec();
316 long gethex();
317 long getoct();
318 long getfp();
319 long toieee();
320 long toffp();

View File

@@ -0,0 +1,16 @@
e:send DECL.C
e:send EXPR.C
e:send ICODE.C
e:send INTERF.C
e:send LEX.C
e:send MAIN.C
e:send STMT.C
e:send TABL.C
e:send VERSION.C
e:send ICODE.H
e:send PARSER.H
e:send MACHINE.68K
e:send LINK.SUB
e:send MAKE.SUB
e:send MACHINE.H
e:send SEND12.SU

View File

@@ -0,0 +1,507 @@
/*
Copyright 1982
Alcyon Corporation
8716 Production Ave.
San Diego, Ca. 92121
*/
#include "parser.h"
#define labgen(l,sl) sl=l;l=nextlabel++;
int swp -1;
/* stmt - process a single statement*/
stmt() /* returns - none*/
{
register int token, lab, i;
register struct tnode *tp;
register char *p;
while( 1 ) {
switch(token=gettok()) {
case LCURBR: /*handle { ... }*/
while( !next(EOF) ) {
if( next(RCURBR) )
return;
stmt();
}
case EOF:
error("{ not matched by }");
case SEMI: /*null statement*/
return;
case RCURBR:
pbtok(token);
return;
case SYMBOL: /*symbol: statement*/
if( peekc(':') ) {
dolabel();
continue;
}
default: /*anything else...*/
pbtok(token);
outexpr(expr());
break;
case RESWORD:
switch(cvalue) {
case R_BREAK:
lab = brklabel();
outgoto(lab); /*branch to break label*/
break;
case R_CASE:
docase();
continue;
case R_CONTINUE:
lab = contlabel(); /*branch to continue label*/
outgoto(lab);
break;
case R_DEFAULT:
dodefault();
continue;
case R_DO:
dodo();
break;
case R_FOR:
dofor();
return;
case R_GOTO:
lab = gotolabel();
outgoto(lab);
break;
case R_IF:
doif();
return;
case R_RETURN:
doreturn();
break;
case R_SWITCH:
doswitch();
return;
case R_WHILE:
dowhile();
return;
default:
synerr("invalid keyword");
return;
}
}
if( !next(SEMI) )
synerr("missing semicolon");
return;
}
}
/* balpar - handle expression within parenthesis for while and if*/
/* Merely checks for left and right parens and builds expression.*/
char *balpar() /* returns pointer to expression*/
{
register struct tnode *tp;
if( next(LPAREN) ) {
reducep = 1;
tp = expr();
reducep = 0;
if( next(RPAREN) )
return(tp);
}
synerr("parenthesized expression syntax");
return(0);
}
/* synerr - syntax error*/
/* Outputs error message and tries to resyncronize input.*/
synerr(s,x1,x2,x3,x4,x5,x6) /* returns - none*/
char *s; /* printf format string*/
int x1, x2, x3, x4, x5, x6; /* printf arguments*/
{
register int token;
error(s,x1,x2,x3,x4,x5,x6);
while( (token=gettok()) != SEMI && token != EOF && token != LCURBR &&
token != RCURBR )
;
pbtok(token);
}
/* gotolabel - gets label id for goto*/
/* This is used for both: goto symbol and if(...)goto symbol*/
gotolabel() /* returns 0 if not, else label id*/
{
register struct symbol *sp;
if( !next(SYMBOL) )
synerr("expected label");
else {
sp = csp;
if( !(sp->s_sc) ) {
sp->s_type = LLABEL;
if( !sp->s_offset )
sp->s_offset = nextlabel++;
}
if( (!sp->s_sc || sp->s_sc == STATIC ) && sp->s_type == LLABEL )
return(sp->s_offset);
synerr("invalid label");
}
return(0);
}
/* dolabel - do statement label*/
/* Checks current symbol for already being defined, then sets*/
/* symbol attributes for label.*/
dolabel() /* returns - none*/
{
register struct symbol *sp;
sp = csp;
if( sp->s_sc )
error("label redeclaration: %.8s",sp->s_symbol);
else {
sp->s_attrib =| SDEFINED;
sp->s_sc = STATIC;
sp->s_type = LLABEL;
if( !sp->s_offset )
sp->s_offset = nextlabel++;
outlab(sp->s_offset);
}
}
/* brklabel - generate break label*/
/* Checks if break label is undefined, and if so, generates message*/
brklabel() /* returns label number*/
{
if( !blabel )
error("invalid break statement");
return(blabel);
}
/* contlabel - generate continue label*/
/* Checks if continue label is undefined, and if so, generates message*/
contlabel() /* returns label number*/
{
if( !clabel )
error("invalid continue statement");
return(clabel);
}
/* docase - handles: case constant : statement*/
/* Checks for being in a switch statement, adds entry to switch table*/
docase() /* returns - none*/
{
register int value, lab;
colonstop++;
value = cexpr(); /*get case value*/
colonstop--;
if( !next(COLON) ) /*check for colon*/
synerr("missing colon");
if( swp < 0 )
error("case not inside a switch block");
else if( swp >= (SWSIZE-1) )
error("too many cases in switch");
else {
addswitch(&swtab[cswp],swp-cswp,value,lab=nextlabel++);
outlab(lab);
swp++;
}
}
/* dodefault - handles: default : statement*/
/* Checks for colon and being in a switch statement*/
dodefault() /* returns - none*/
{
if( !next(COLON) )
error("missing colon");
if( swp < 0 )
error("default not inside a switch block");
else {
dlabel = nextlabel++; /*allocate default label*/
outlab(dlabel); /*output default label*/
}
}
/* dodo - handles: do statement while ( expression )*/
dodo() /* returns - none*/
{
register int lab, saveblab, saveclab;
labgen(blabel,saveblab);
labgen(clabel,saveclab);
lab = nextlabel++;
outlab(lab); /*branch back to here*/
stmt(); /*do statement*/
outlab(clabel); /*continue label*/
if( !nextrw(R_WHILE) ) {
error("missing while"); /*only advisory...*/
outgoto(lab);
}
else
outifgoto(balpar(),TRUE,lab); /*while expression*/
outlab(blabel); /*break label*/
blabel = saveblab; /*restore labels*/
clabel = saveclab;
}
/*
* dofor - handle: for ( expression ; expression ; expression ) statement
* Hard part is handling re-initialization expression, which is
* parsed and saved, then the statement is parsed, then the reinit
* clause expression tree is output.
*/
dofor() /* returns - none*/
{
register int lab, saveblab, saveclab, reinit, clineno;
register char *savep;
register struct tnode *tp;
labgen(blabel,saveblab);
labgen(clabel,saveclab);
if( !next(LPAREN) ) {
forerr:
synerr("invalid for statement");
return;
}
if( !next(SEMI) ) { /*do init expression*/
outexpr(expr());
if( !next(SEMI) )
goto forerr;
}
outlab(clabel); /*branch back to here*/
if( !next(SEMI) ) { /*do for condition*/
outifgoto(expr(),FALSE,blabel);
if( !next(SEMI) )
goto forerr;
}
if( next(RPAREN) ) { /*no re-init - easy case*/
stmt(); /*output statement*/
outgoto(clabel); /*output continue label*/
}
else { /*there is a re-init clause*/
labgen(clabel,lab);
savep = exprp;
tp = expr(); /*save re-init tree until done*/
exprp = opap; /*remember until reinit is output*/
reinit = lineno;
if( !next(RPAREN) )
goto forerr;
stmt(); /*do statment*/
clineno = lineno;
lineno = reinit;
outlab(clabel); /*we branch to here for reinit*/
outexpr(tp); /*output re-init clause*/
exprp = savep;
lineno = clineno;
outgoto(lab); /*branch back to top of loop*/
}
outlab(blabel); /*break to here*/
blabel = saveblab;
clabel = saveclab; /*restore labels*/
}
/* doif - handles: if ( expression ) statement [ else statement ]*/
/* Handles special cases for goto, break, continue and return.*/
doif() /* returns - none*/
{
register struct tnode *tp;
register int elselab, exitlab;
tp = balpar(); /*if( expr )...*/
exitlab = 0;
if( nextrw(R_GOTO) )
exitlab = gotolabel();
else if( nextrw(R_BREAK) )
exitlab = brklabel();
else if( nextrw(R_CONTINUE) )
exitlab = contlabel();
else if( nextrw(R_RETURN) ) {
if( peekc(';') ) {
exitlab = rlabel;
putback(';');
}
else
pbtok(RESWORD);
}
if( exitlab ) { /*easy goto, do branch if true*/
outifgoto(tp,TRUE,exitlab);
if( !next(SEMI) )
synerr("missing semicolon");
if( nextrw(R_ELSE) ) /*else clause, just output it*/
stmt();
}
else { /*hard goto, branch over statement*/
elselab = nextlabel++;
outifgoto(tp,FALSE,elselab);
stmt();
if( nextrw(R_ELSE) ) {
exitlab = nextlabel++; /*branches over else clause*/
outgoto(exitlab); /*branch out of then clause*/
outlab(elselab); /*label to start else clause*/
stmt(); /*else statement*/
outlab(exitlab);
}
else
outlab(elselab); /*no else, just branch out*/
}
}
/*
* doreturn - handles: return [ expression ] ;
* Expression is the hard part, must create an assignment expression
* to assign expression to the type of the function, then get it
* loaded into a specific register.
*/
doreturn() /* returns - none*/
{
register struct tnode *tp;
if( !peekc(';') ) /*need to compute return?*/
outforreg(FRETURN,frp,expr());
else
putback(';');
outgoto(rlabel); /*branch to the return label*/
}
/*
* doswitch - handles: switch ( expression ) statement
* Evaluates the expression, forces the result into a known register
* collects the case statements in swtab, then outputs the switch
* operator and switch cases.
*/
doswitch() /* returns - none*/
{
register int saveblab, swlab, savedlab, saveswp, i;
register struct tnode *tp;
labgen(blabel,saveblab);
tp = balpar();
integral(tp,-1); /*must be integral type result*/
outforreg(ASSIGN,snalloc(INT,AUTO,0,0,0),tp);
saveswp = swp; /*remember old switch pointer*/
if( saveswp < 0 )
swp++;
i = cswp;
cswp = swp; /*remember real first entry*/
swlab = nextlabel++;
outgoto(swlab); /*branch to switch code*/
savedlab = dlabel;
dlabel = 0;
stmt(); /*do switch statement*/
outgoto(blabel); /*output branch just in case*/
outlab(swlab); /*here we now do the switch code*/
if( !dlabel )
dlabel = blabel;
outswitch(swp-cswp,dlabel,&swtab[cswp]);
outlab(blabel); /*break to here*/
cswp = i;
swp = saveswp;
blabel = saveblab;
dlabel = savedlab;
}
/* dowhile - handles: while ( expression ) statement*/
/* This is fairly straight-forward.*/
dowhile() /* returns - none*/
{
register int saveclab, saveblab;
labgen(blabel,saveblab);
labgen(clabel,saveclab);
outlab(clabel); /*continue label*/
outifgoto(balpar(),FALSE,blabel); /*condition clause*/
stmt(); /*statement*/
outgoto(clabel); /*branch back to top of loop*/
outlab(blabel); /*break to here*/
blabel = saveblab;
clabel = saveclab; /*restore labels*/
}
/* nextrw - is next token the specified reserved word?*/
nextrw(rw) /* returns 1 if match, 0 otherwise*/
int rw; /* reserved word to match*/
{
register int token;
if( (token=gettok()) != RESWORD || cvalue != rw ) {
pbtok(token);
return(0);
}
return(1);
}
/*
* addswitch - add an entry into current switch table, bubble sorting
* This makes it easier on the code generator and also checks for
* duplicate labels at the "right" time.
*/
addswitch(sp,ncases,nval,nlab) /* returns - none*/
struct swtch *sp; /* switch table pointer*/
int ncases; /* number of cases in switch*/
int nval; /* new value*/
int nlab; /* new label*/
{
register struct swtch *nswp, *s;
register int temp, i;
nswp = &sp[ncases];
nswp->sw_value = nval;
nswp->sw_label = nlab;
s = nswp--;
for( ; --ncases >= 0; s--, nswp-- ) {
if( s->sw_value == nswp->sw_value )
error("duplicate case value");
if( s->sw_value < nswp->sw_value ) {
temp = s->sw_value;
s->sw_value = nswp->sw_value;
nswp->sw_value = temp;
temp = s->sw_label;
s->sw_label = nswp->sw_label;
nswp->sw_label = temp;
}
}
}
/* outforreg - generate assignment for switch and return*/
outforreg(op,ltp,rtp) /*returns - none*/
int op; /*operator for build tree*/
struct tnode *ltp; /*left expression tree*/
struct tnode *rtp; /*right expression tree*/
{
register struct tnode *tp;
opp = opstack;
opdp = opdstack;
pushopd(ltp);
pushopd(rtp);
maketree(op);
if( tp = popopd() )
outcforreg(tp->t_right);
opp = opdp = 0;
}
/* outassign - generate assignment for function args*/
outassign(ltp,rtp) /*returns - none*/
struct tnode *ltp; /*left expression tree*/
struct tnode *rtp; /*right expression tree*/
{
opp = opstack;
opdp = opdstack;
pushopd(ltp);
pushopd(rtp);
maketree(ASSIGN);
outexpr(popopd());
opp = opdp = 0;
}

View File

@@ -0,0 +1,516 @@
1File: STMT.C Page 1
1 /*
2 Copyright 1982
3 Alcyon Corporation
4 8716 Production Ave.
5 San Diego, Ca. 92121
6 */
7
8 #include "parser.h"
9 #define labgen(l,sl) sl=l;l=nextlabel++;
10 int swp -1;
11
12
13 /* stmt - process a single statement*/
14 stmt() /* returns - none*/
15 {
16 register int token, lab, i;
17 register struct tnode *tp;
18 register char *p;
19
20 while( 1 ) {
21 switch(token=gettok()) {
22
23 case LCURBR: /*handle { ... }*/
24 while( !next(EOF) ) {
25 if( next(RCURBR) )
26 return;
27 stmt();
28 }
29 case EOF:
30 error("{ not matched by }");
31 case SEMI: /*null statement*/
32 return;
33
34 case RCURBR:
35 pbtok(token);
36 return;
37
38 case SYMBOL: /*symbol: statement*/
39 if( peekc(':') ) {
40 dolabel();
41 continue;
42 }
43 default: /*anything else...*/
44 pbtok(token);
45 outexpr(expr());
46 break;
47
48 case RESWORD:
49 switch(cvalue) {
50
51 case R_BREAK:
52 lab = brklabel();
53 outgoto(lab); /*branch to break label*/
54 break;
55
56 case R_CASE:
57 docase();
58 continue;
59
1File: STMT.C Page 2
60 case R_CONTINUE:
61 lab = contlabel(); /*branch to continue label*/
62 outgoto(lab);
63 break;
64
65 case R_DEFAULT:
66 dodefault();
67 continue;
68
69 case R_DO:
70 dodo();
71 break;
72
73 case R_FOR:
74 dofor();
75 return;
76
77 case R_GOTO:
78 lab = gotolabel();
79 outgoto(lab);
80 break;
81
82 case R_IF:
83 doif();
84 return;
85
86 case R_RETURN:
87 doreturn();
88 break;
89
90 case R_SWITCH:
91 doswitch();
92 return;
93
94 case R_WHILE:
95 dowhile();
96 return;
97
98 default:
99 synerr("invalid keyword");
100 return;
101 }
102 }
103 if( !next(SEMI) )
104 synerr("missing semicolon");
105 return;
106 }
107 }
108
109 /* balpar - handle expression within parenthesis for while and if*/
110 /* Merely checks for left and right parens and builds expression.*/
111 char *balpar() /* returns pointer to expression*/
112 {
113 register struct tnode *tp;
114
115 if( next(LPAREN) ) {
116 reducep = 1;
117 tp = expr();
118 reducep = 0;
1File: STMT.C Page 3
119 if( next(RPAREN) )
120 return(tp);
121 }
122 synerr("parenthesized expression syntax");
123 return(0);
124 }
125
126 /* synerr - syntax error*/
127 /* Outputs error message and tries to resyncronize input.*/
128 synerr(s,x1,x2,x3,x4,x5,x6) /* returns - none*/
129 char *s; /* printf format string*/
130 int x1, x2, x3, x4, x5, x6; /* printf arguments*/
131 {
132 register int token;
133
134 error(s,x1,x2,x3,x4,x5,x6);
135 while( (token=gettok()) != SEMI && token != EOF && token != LCURBR &&
136 token != RCURBR )
137 ;
138 pbtok(token);
139 }
140
141 /* gotolabel - gets label id for goto*/
142 /* This is used for both: goto symbol and if(...)goto symbol*/
143 gotolabel() /* returns 0 if not, else label id*/
144 {
145 register struct symbol *sp;
146
147 if( !next(SYMBOL) )
148 synerr("expected label");
149 else {
150 sp = csp;
151 if( !(sp->s_sc) ) {
152 sp->s_type = LLABEL;
153 if( !sp->s_offset )
154 sp->s_offset = nextlabel++;
155 }
156 if( (!sp->s_sc || sp->s_sc == STATIC ) && sp->s_type == LLABEL )
157 return(sp->s_offset);
158 synerr("invalid label");
159 }
160 return(0);
161 }
162
163 /* dolabel - do statement label*/
164 /* Checks current symbol for already being defined, then sets*/
165 /* symbol attributes for label.*/
166 dolabel() /* returns - none*/
167 {
168 register struct symbol *sp;
169
170 sp = csp;
171 if( sp->s_sc )
172 error("label redeclaration: %.8s",sp->s_symbol);
173 else {
174 sp->s_attrib =| SDEFINED;
175 sp->s_sc = STATIC;
176 sp->s_type = LLABEL;
177 if( !sp->s_offset )
1File: STMT.C Page 4
178 sp->s_offset = nextlabel++;
179 outlab(sp->s_offset);
180 }
181 }
182
183 /* brklabel - generate break label*/
184 /* Checks if break label is undefined, and if so, generates message*/
185 brklabel() /* returns label number*/
186 {
187 if( !blabel )
188 error("invalid break statement");
189 return(blabel);
190 }
191
192 /* contlabel - generate continue label*/
193 /* Checks if continue label is undefined, and if so, generates message*/
194 contlabel() /* returns label number*/
195 {
196 if( !clabel )
197 error("invalid continue statement");
198 return(clabel);
199 }
200
201 /* docase - handles: case constant : statement*/
202 /* Checks for being in a switch statement, adds entry to switch table*/
203 docase() /* returns - none*/
204 {
205 register int value, lab;
206
207 colonstop++;
208 value = cexpr(); /*get case value*/
209 colonstop--;
210 if( !next(COLON) ) /*check for colon*/
211 synerr("missing colon");
212 if( swp < 0 )
213 error("case not inside a switch block");
214 else if( swp >= (SWSIZE-1) )
215 error("too many cases in switch");
216 else {
217 addswitch(&swtab[cswp],swp-cswp,value,lab=nextlabel++);
218 outlab(lab);
219 swp++;
220 }
221 }
222
223 /* dodefault - handles: default : statement*/
224 /* Checks for colon and being in a switch statement*/
225 dodefault() /* returns - none*/
226 {
227 if( !next(COLON) )
228 error("missing colon");
229 if( swp < 0 )
230 error("default not inside a switch block");
231 else {
232 dlabel = nextlabel++; /*allocate default label*/
233 outlab(dlabel); /*output default label*/
234 }
235 }
236
1File: STMT.C Page 5
237 /* dodo - handles: do statement while ( expression )*/
238 dodo() /* returns - none*/
239 {
240 register int lab, saveblab, saveclab;
241
242 labgen(blabel,saveblab);
243 labgen(clabel,saveclab);
244 lab = nextlabel++;
245 outlab(lab); /*branch back to here*/
246 stmt(); /*do statement*/
247 outlab(clabel); /*continue label*/
248 if( !nextrw(R_WHILE) ) {
249 error("missing while"); /*only advisory...*/
250 outgoto(lab);
251 }
252 else
253 outifgoto(balpar(),TRUE,lab); /*while expression*/
254 outlab(blabel); /*break label*/
255 blabel = saveblab; /*restore labels*/
256 clabel = saveclab;
257 }
258
259 /*
260 * dofor - handle: for ( expression ; expression ; expression ) statement
261 * Hard part is handling re-initialization expression, which is
262 * parsed and saved, then the statement is parsed, then the reinit
263 * clause expression tree is output.
264 */
265 dofor() /* returns - none*/
266 {
267 register int lab, saveblab, saveclab, reinit, clineno;
268 register char *savep;
269 register struct tnode *tp;
270
271 labgen(blabel,saveblab);
272 labgen(clabel,saveclab);
273 if( !next(LPAREN) ) {
274 forerr:
275 synerr("invalid for statement");
276 return;
277 }
278 if( !next(SEMI) ) { /*do init expression*/
279 outexpr(expr());
280 if( !next(SEMI) )
281 goto forerr;
282 }
283 outlab(clabel); /*branch back to here*/
284 if( !next(SEMI) ) { /*do for condition*/
285 outifgoto(expr(),FALSE,blabel);
286 if( !next(SEMI) )
287 goto forerr;
288 }
289 if( next(RPAREN) ) { /*no re-init - easy case*/
290 stmt(); /*output statement*/
291 outgoto(clabel); /*output continue label*/
292 }
293 else { /*there is a re-init clause*/
294 labgen(clabel,lab);
295 savep = exprp;
1File: STMT.C Page 6
296 tp = expr(); /*save re-init tree until done*/
297 exprp = opap; /*remember until reinit is output*/
298 reinit = lineno;
299 if( !next(RPAREN) )
300 goto forerr;
301 stmt(); /*do statment*/
302 clineno = lineno;
303 lineno = reinit;
304 outlab(clabel); /*we branch to here for reinit*/
305 outexpr(tp); /*output re-init clause*/
306 exprp = savep;
307 lineno = clineno;
308 outgoto(lab); /*branch back to top of loop*/
309 }
310 outlab(blabel); /*break to here*/
311 blabel = saveblab;
312 clabel = saveclab; /*restore labels*/
313 }
314
315 /* doif - handles: if ( expression ) statement [ else statement ]*/
316 /* Handles special cases for goto, break, continue and return.*/
317 doif() /* returns - none*/
318 {
319 register struct tnode *tp;
320 register int elselab, exitlab;
321
322 tp = balpar(); /*if( expr )...*/
323 exitlab = 0;
324 if( nextrw(R_GOTO) )
325 exitlab = gotolabel();
326 else if( nextrw(R_BREAK) )
327 exitlab = brklabel();
328 else if( nextrw(R_CONTINUE) )
329 exitlab = contlabel();
330 else if( nextrw(R_RETURN) ) {
331 if( peekc(';') ) {
332 exitlab = rlabel;
333 putback(';');
334 }
335 else
336 pbtok(RESWORD);
337 }
338 if( exitlab ) { /*easy goto, do branch if true*/
339 outifgoto(tp,TRUE,exitlab);
340 if( !next(SEMI) )
341 synerr("missing semicolon");
342 if( nextrw(R_ELSE) ) /*else clause, just output it*/
343 stmt();
344 }
345 else { /*hard goto, branch over statement*/
346 elselab = nextlabel++;
347 outifgoto(tp,FALSE,elselab);
348 stmt();
349 if( nextrw(R_ELSE) ) {
350 exitlab = nextlabel++; /*branches over else clause*/
351 outgoto(exitlab); /*branch out of then clause*/
352 outlab(elselab); /*label to start else clause*/
353 stmt(); /*else statement*/
354 outlab(exitlab);
1File: STMT.C Page 7
355 }
356 else
357 outlab(elselab); /*no else, just branch out*/
358 }
359 }
360
361 /*
362 * doreturn - handles: return [ expression ] ;
363 * Expression is the hard part, must create an assignment expression
364 * to assign expression to the type of the function, then get it
365 * loaded into a specific register.
366 */
367 doreturn() /* returns - none*/
368 {
369 register struct tnode *tp;
370
371 if( !peekc(';') ) /*need to compute return?*/
372 outforreg(FRETURN,frp,expr());
373 else
374 putback(';');
375 outgoto(rlabel); /*branch to the return label*/
376 }
377
378 /*
379 * doswitch - handles: switch ( expression ) statement
380 * Evaluates the expression, forces the result into a known register
381 * collects the case statements in swtab, then outputs the switch
382 * operator and switch cases.
383 */
384 doswitch() /* returns - none*/
385 {
386 register int saveblab, swlab, savedlab, saveswp, i;
387 register struct tnode *tp;
388
389 labgen(blabel,saveblab);
390 tp = balpar();
391 integral(tp,-1); /*must be integral type result*/
392 outforreg(ASSIGN,snalloc(INT,AUTO,0,0,0),tp);
393 saveswp = swp; /*remember old switch pointer*/
394 if( saveswp < 0 )
395 swp++;
396 i = cswp;
397 cswp = swp; /*remember real first entry*/
398 swlab = nextlabel++;
399 outgoto(swlab); /*branch to switch code*/
400 savedlab = dlabel;
401 dlabel = 0;
402 stmt(); /*do switch statement*/
403 outgoto(blabel); /*output branch just in case*/
404 outlab(swlab); /*here we now do the switch code*/
405 if( !dlabel )
406 dlabel = blabel;
407 outswitch(swp-cswp,dlabel,&swtab[cswp]);
408 outlab(blabel); /*break to here*/
409 cswp = i;
410 swp = saveswp;
411 blabel = saveblab;
412 dlabel = savedlab;
413 }
1File: STMT.C Page 8
414
415 /* dowhile - handles: while ( expression ) statement*/
416 /* This is fairly straight-forward.*/
417 dowhile() /* returns - none*/
418 {
419 register int saveclab, saveblab;
420
421 labgen(blabel,saveblab);
422 labgen(clabel,saveclab);
423 outlab(clabel); /*continue label*/
424 outifgoto(balpar(),FALSE,blabel); /*condition clause*/
425 stmt(); /*statement*/
426 outgoto(clabel); /*branch back to top of loop*/
427 outlab(blabel); /*break to here*/
428 blabel = saveblab;
429 clabel = saveclab; /*restore labels*/
430 }
431
432 /* nextrw - is next token the specified reserved word?*/
433 nextrw(rw) /* returns 1 if match, 0 otherwise*/
434 int rw; /* reserved word to match*/
435 {
436 register int token;
437
438 if( (token=gettok()) != RESWORD || cvalue != rw ) {
439 pbtok(token);
440 return(0);
441 }
442 return(1);
443 }
444
445 /*
446 * addswitch - add an entry into current switch table, bubble sorting
447 * This makes it easier on the code generator and also checks for
448 * duplicate labels at the "right" time.
449 */
450 addswitch(sp,ncases,nval,nlab) /* returns - none*/
451 struct swtch *sp; /* switch table pointer*/
452 int ncases; /* number of cases in switch*/
453 int nval; /* new value*/
454 int nlab; /* new label*/
455 {
456 register struct swtch *nswp, *s;
457 register int temp, i;
458
459 nswp = &sp[ncases];
460 nswp->sw_value = nval;
461 nswp->sw_label = nlab;
462 s = nswp--;
463 for( ; --ncases >= 0; s--, nswp-- ) {
464 if( s->sw_value == nswp->sw_value )
465 error("duplicate case value");
466 if( s->sw_value < nswp->sw_value ) {
467 temp = s->sw_value;
468 s->sw_value = nswp->sw_value;
469 nswp->sw_value = temp;
470 temp = s->sw_label;
471 s->sw_label = nswp->sw_label;
472 nswp->sw_label = temp;
1File: STMT.C Page 9
473 }
474 }
475 }
476
477 /* outforreg - generate assignment for switch and return*/
478 outforreg(op,ltp,rtp) /*returns - none*/
479 int op; /*operator for build tree*/
480 struct tnode *ltp; /*left expression tree*/
481 struct tnode *rtp; /*right expression tree*/
482 {
483 register struct tnode *tp;
484
485 opp = opstack;
486 opdp = opdstack;
487 pushopd(ltp);
488 pushopd(rtp);
489 maketree(op);
490 if( tp = popopd() )
491 outcforreg(tp->t_right);
492 opp = opdp = 0;
493 }
494
495 /* outassign - generate assignment for function args*/
496 outassign(ltp,rtp) /*returns - none*/
497 struct tnode *ltp; /*left expression tree*/
498 struct tnode *rtp; /*right expression tree*/
499 {
500 opp = opstack;
501 opdp = opdstack;
502 pushopd(ltp);
503 pushopd(rtp);
504 maketree(ASSIGN);
505 outexpr(popopd());
506 opp = opdp = 0;
507 }

View File

@@ -0,0 +1,119 @@
/*
Copyright 1982
Alcyon Corporation
8716 Production Ave.
San Diego, Ca. 92121
*/
#include "parser.h"
#define ASGOP OPRAS|OPASSIGN|OPLVAL|OPBIN
/*info on operators:*/
/*000077-- OPPRI - priority*/
/*000100-- OPBIN - binary operator*/
/*000200-- OPLVAL - left operand must be lvalue*/
/*000400-- OPREL - relational operator*/
/*001000-- OPASSIGN - assignment operator*/
/*002000-- OPLWORD - int required on left*/
/*004000-- OPRWORD - int required on right*/
/*010000-- OPCOM commutative*/
/*020000-- OPRAS - right associative*/
/*040000-- OPTERM - termination node*/
/*100000 - OPCONVS - conversion operator*/
int opinfo[] {
TRMPRI, /*EOF*/
ADDPRI|OPCOM|OPBIN, /*ADD - expr + expr*/
ADDPRI|OPBIN, /*SUB - expr - expr*/
MULPRI|OPCOM|OPBIN, /*MULT - expr * expr*/
MULPRI|OPBIN, /*DIV - expr / expr*/
MULPRI|OPBIN, /*MOD - expr % expr*/
SHFPRI|OPLWORD|OPRWORD|OPBIN, /*RSH - expr >> expr*/
SHFPRI|OPLWORD|OPRWORD|OPBIN, /*LSH - expr << expr*/
ANDPRI|OPCOM|OPLWORD|OPRWORD|OPBIN, /*AND - expr & expr*/
ORPRI|OPCOM|OPLWORD|OPRWORD|OPBIN, /*OR - expr | expr*/
ORPRI|OPCOM|OPLWORD|OPRWORD|OPBIN, /*XOR - expr ^ expr*/
UNOPRI|OPRAS|OPLWORD, /*NOT - ! expr*/
UNOPRI|OPRAS, /*UMINUS - - expr*/
UNOPRI|OPRAS|OPLWORD, /*COMPL - ~ expr*/
UNOPRI|OPRAS|OPLVAL|OPBIN, /*PREDEC - --lvalue*/
UNOPRI|OPRAS|OPLVAL|OPBIN, /*PREINC - ++lvalue*/
UNOPRI|OPRAS|OPLVAL|OPBIN, /*POSTDEC - lvalue--*/
UNOPRI|OPRAS|OPLVAL|OPBIN, /*POSTINC - lvalue++*/
ASGPRI|ASGOP, /*ASSIGN - lvalue = expr*/
ASGPRI|ASGOP, /*EQADD - lvalue += expr*/
ASGPRI|ASGOP, /*EQSUB - lvalue -= expr*/
ASGPRI|ASGOP, /*EQMULT - lvalue *= expr*/
ASGPRI|ASGOP, /*EQDIV - lvalue /= expr*/
ASGPRI|ASGOP, /*EQMOD - lvalue %= expr*/
ASGPRI|ASGOP|OPLWORD|OPRWORD, /*EQRSH - lvalue >>= expr*/
ASGPRI|ASGOP|OPLWORD|OPRWORD, /*EQLSH - lvalue <<= expr*/
ASGPRI|ASGOP|OPLWORD|OPRWORD, /*EQAND - lvalue &= expr*/
ASGPRI|ASGOP|OPLWORD|OPRWORD, /*EQOR - lvalue |= expr*/
ASGPRI|ASGOP|OPLWORD|OPRWORD, /*EQXOR - lvalue ^= expr*/
TRMPRI, /*FJSR - generate function jsr*/
EQLPRI|OPREL|OPBIN, /*EQUALS - expr == expr*/
EQLPRI|OPREL|OPBIN, /*NEQUALS - expr != expr*/
RELPRI|OPREL|OPBIN, /*GREAT - expr > expr*/
RELPRI|OPREL|OPBIN, /*GREATEQ - expr >= expr*/
RELPRI|OPREL|OPBIN, /*LESS - expr < expr*/
RELPRI|OPREL|OPBIN, /*LESSEQ - expr <= expr*/
TRMPRI|OPCONVS, /*INT2L*/
TRMPRI|OPCONVS, /*LONG2I*/
TRMPRI|OPBIN, /*BTST*/
TRMPRI, /*LOAD*/
TRMPRI|OPBIN, /*LMULT*/
TRMPRI|OPBIN, /*LDIV*/
TRMPRI|OPBIN, /*LMOD*/
TRMPRI|OPBIN, /*LEQMULT*/
TRMPRI|OPBIN, /*LEQDIV*/
TRMPRI|OPBIN, /*LEQMOD*/
TRMPRI|ASGOP, /*EQADDR*/
TRMPRI, /*EQNOT*/
TRMPRI, /*EQNEG*/
TRMPRI|OPBIN, /*DOCAST*/
ASGPRI|ASGOP, /*STASSIGN [vlh]*/
TRMPRI|OPCONVS, /*LONG2F [vlh] 3.4*/
TRMPRI|OPCONVS, /*FLOAT2L [vlh] 3.4*/
TRMPRI|OPCONVS, /*INT2F [vlh] 3.4*/
TRMPRI|OPCONVS, /*FLOAT2F [vlh] 3.4*/
TRMPRI, /*unused - 55*/
TRMPRI, /*unused - 56*/
TRMPRI, /*unused - 57*/
TRMPRI, /*unused - 58*/
TRMPRI, /*unused - 59*/
UNOPRI|OPRAS|OPLVAL, /*ADDR - & expr*/
UNOPRI|OPRAS|OPLWORD, /*INDR - * expr*/
LNDPRI|OPBIN, /*LAND - expr && expr*/
LORPRI|OPBIN, /*LOR - expr || expr*/
QMKPRI|OPRAS|OPBIN, /*QMARK - expr ? expr : expr*/
QMKPRI|OPRAS|OPBIN, /*COLON*/
COMPRI|OPBIN, /*COMMA*/
TRMPRI|OPTERM, /*CINT*/
TRMPRI|OPTERM, /*CLONG*/
TRMPRI|OPTERM, /*SYMBOL*/
TRMPRI|OPTERM, /*AUTOINC*/
TRMPRI|OPTERM, /*AUTODEC*/
LPNPRI|OPBIN, /*CALL - call with arguments*/
LPNPRI, /*NACALL - no argument call*/
TRMPRI, /*BFIELD - field selection*/
TRMPRI, /*IFGOTO*/
TRMPRI, /*INIT*/
TRMPRI, /*CFORREG*/
TRMPRI, /*unused - 78*/
TRMPRI|OPTERM, /*CFLOAT [vlh] 3.4*/
UNOPRI|OPRAS|OPASSIGN|OPBIN, /*CAST*/
TRMPRI, /*SEMI*/
TRMPRI, /*LCURBR - {*/
TRMPRI, /*RCURBR - }*/
LPNPRI, /*LBRACK - [*/
RPNPRI, /*RBRACK - ]*/
LPNPRI, /*LPAREN - )*/
RPNPRI, /*RPAREN - )*/
TRMPRI|OPTERM, /*STRING*/
TRMPRI, /*RESWORD*/
LPNPRI|OPBIN, /*APTR - expr -> symbol*/
LPNPRI|OPBIN, /*PERIOD - expr . symbol*/
UNOPRI|OPRAS, /*SIZEOF - sizeof expr*/
LPNPRI|OPBIN, /*MPARENS - matching parens ()*/
UNOPRI|OPRAS|OPASSIGN|OPBIN, /*FRETURN*/
};

View File

@@ -0,0 +1,122 @@
1File: TABL.C Page 1
1 /*
2 Copyright 1982
3 Alcyon Corporation
4 8716 Production Ave.
5 San Diego, Ca. 92121
6 */
7
8 #include "parser.h"
9 #define ASGOP OPRAS|OPASSIGN|OPLVAL|OPBIN
10
11 /*info on operators:*/
12 /*000077-- OPPRI - priority*/
13 /*000100-- OPBIN - binary operator*/
14 /*000200-- OPLVAL - left operand must be lvalue*/
15 /*000400-- OPREL - relational operator*/
16 /*001000-- OPASSIGN - assignment operator*/
17 /*002000-- OPLWORD - int required on left*/
18 /*004000-- OPRWORD - int required on right*/
19 /*010000-- OPCOM commutative*/
20 /*020000-- OPRAS - right associative*/
21 /*040000-- OPTERM - termination node*/
22 /*100000 - OPCONVS - conversion operator*/
23 int opinfo[] {
24 TRMPRI, /*EOF*/
25 ADDPRI|OPCOM|OPBIN, /*ADD - expr + expr*/
26 ADDPRI|OPBIN, /*SUB - expr - expr*/
27 MULPRI|OPCOM|OPBIN, /*MULT - expr * expr*/
28 MULPRI|OPBIN, /*DIV - expr / expr*/
29 MULPRI|OPBIN, /*MOD - expr % expr*/
30 SHFPRI|OPLWORD|OPRWORD|OPBIN, /*RSH - expr >> expr*/
31 SHFPRI|OPLWORD|OPRWORD|OPBIN, /*LSH - expr << expr*/
32 ANDPRI|OPCOM|OPLWORD|OPRWORD|OPBIN, /*AND - expr & expr*/
33 ORPRI|OPCOM|OPLWORD|OPRWORD|OPBIN, /*OR - expr | expr*/
34 ORPRI|OPCOM|OPLWORD|OPRWORD|OPBIN, /*XOR - expr ^ expr*/
35 UNOPRI|OPRAS|OPLWORD, /*NOT - ! expr*/
36 UNOPRI|OPRAS, /*UMINUS - - expr*/
37 UNOPRI|OPRAS|OPLWORD, /*COMPL - ~ expr*/
38 UNOPRI|OPRAS|OPLVAL|OPBIN, /*PREDEC - --lvalue*/
39 UNOPRI|OPRAS|OPLVAL|OPBIN, /*PREINC - ++lvalue*/
40 UNOPRI|OPRAS|OPLVAL|OPBIN, /*POSTDEC - lvalue--*/
41 UNOPRI|OPRAS|OPLVAL|OPBIN, /*POSTINC - lvalue++*/
42 ASGPRI|ASGOP, /*ASSIGN - lvalue = expr*/
43 ASGPRI|ASGOP, /*EQADD - lvalue += expr*/
44 ASGPRI|ASGOP, /*EQSUB - lvalue -= expr*/
45 ASGPRI|ASGOP, /*EQMULT - lvalue *= expr*/
46 ASGPRI|ASGOP, /*EQDIV - lvalue /= expr*/
47 ASGPRI|ASGOP, /*EQMOD - lvalue %= expr*/
48 ASGPRI|ASGOP|OPLWORD|OPRWORD, /*EQRSH - lvalue >>= expr*/
49 ASGPRI|ASGOP|OPLWORD|OPRWORD, /*EQLSH - lvalue <<= expr*/
50 ASGPRI|ASGOP|OPLWORD|OPRWORD, /*EQAND - lvalue &= expr*/
51 ASGPRI|ASGOP|OPLWORD|OPRWORD, /*EQOR - lvalue |= expr*/
52 ASGPRI|ASGOP|OPLWORD|OPRWORD, /*EQXOR - lvalue ^= expr*/
53 TRMPRI, /*FJSR - generate function jsr*/
54 EQLPRI|OPREL|OPBIN, /*EQUALS - expr == expr*/
55 EQLPRI|OPREL|OPBIN, /*NEQUALS - expr != expr*/
56 RELPRI|OPREL|OPBIN, /*GREAT - expr > expr*/
57 RELPRI|OPREL|OPBIN, /*GREATEQ - expr >= expr*/
58 RELPRI|OPREL|OPBIN, /*LESS - expr < expr*/
59 RELPRI|OPREL|OPBIN, /*LESSEQ - expr <= expr*/
1File: TABL.C Page 2
60 TRMPRI|OPCONVS, /*INT2L*/
61 TRMPRI|OPCONVS, /*LONG2I*/
62 TRMPRI|OPBIN, /*BTST*/
63 TRMPRI, /*LOAD*/
64 TRMPRI|OPBIN, /*LMULT*/
65 TRMPRI|OPBIN, /*LDIV*/
66 TRMPRI|OPBIN, /*LMOD*/
67 TRMPRI|OPBIN, /*LEQMULT*/
68 TRMPRI|OPBIN, /*LEQDIV*/
69 TRMPRI|OPBIN, /*LEQMOD*/
70 TRMPRI|ASGOP, /*EQADDR*/
71 TRMPRI, /*EQNOT*/
72 TRMPRI, /*EQNEG*/
73 TRMPRI|OPBIN, /*DOCAST*/
74 ASGPRI|ASGOP, /*STASSIGN [vlh]*/
75 TRMPRI|OPCONVS, /*LONG2F [vlh] 3.4*/
76 TRMPRI|OPCONVS, /*FLOAT2L [vlh] 3.4*/
77 TRMPRI|OPCONVS, /*INT2F [vlh] 3.4*/
78 TRMPRI|OPCONVS, /*FLOAT2F [vlh] 3.4*/
79 TRMPRI, /*unused - 55*/
80 TRMPRI, /*unused - 56*/
81 TRMPRI, /*unused - 57*/
82 TRMPRI, /*unused - 58*/
83 TRMPRI, /*unused - 59*/
84 UNOPRI|OPRAS|OPLVAL, /*ADDR - & expr*/
85 UNOPRI|OPRAS|OPLWORD, /*INDR - * expr*/
86 LNDPRI|OPBIN, /*LAND - expr && expr*/
87 LORPRI|OPBIN, /*LOR - expr || expr*/
88 QMKPRI|OPRAS|OPBIN, /*QMARK - expr ? expr : expr*/
89 QMKPRI|OPRAS|OPBIN, /*COLON*/
90 COMPRI|OPBIN, /*COMMA*/
91 TRMPRI|OPTERM, /*CINT*/
92 TRMPRI|OPTERM, /*CLONG*/
93 TRMPRI|OPTERM, /*SYMBOL*/
94 TRMPRI|OPTERM, /*AUTOINC*/
95 TRMPRI|OPTERM, /*AUTODEC*/
96 LPNPRI|OPBIN, /*CALL - call with arguments*/
97 LPNPRI, /*NACALL - no argument call*/
98 TRMPRI, /*BFIELD - field selection*/
99 TRMPRI, /*IFGOTO*/
100 TRMPRI, /*INIT*/
101 TRMPRI, /*CFORREG*/
102 TRMPRI, /*unused - 78*/
103 TRMPRI|OPTERM, /*CFLOAT [vlh] 3.4*/
104 UNOPRI|OPRAS|OPASSIGN|OPBIN, /*CAST*/
105 TRMPRI, /*SEMI*/
106 TRMPRI, /*LCURBR - {*/
107 TRMPRI, /*RCURBR - }*/
108 LPNPRI, /*LBRACK - [*/
109 RPNPRI, /*RBRACK - ]*/
110 LPNPRI, /*LPAREN - )*/
111 RPNPRI, /*RPAREN - )*/
112 TRMPRI|OPTERM, /*STRING*/
113 TRMPRI, /*RESWORD*/
114 LPNPRI|OPBIN, /*APTR - expr -> symbol*/
115 LPNPRI|OPBIN, /*PERIOD - expr . symbol*/
116 UNOPRI|OPRAS, /*SIZEOF - sizeof expr*/
117 LPNPRI|OPBIN, /*MPARENS - matching parens ()*/
118 UNOPRI|OPRAS|OPASSIGN|OPBIN, /*FRETURN*/
1File: TABL.C Page 3
119 };

View File

@@ -0,0 +1,16 @@
e:vax DECL.C s
e:vax EXPR.C s
e:vax ICODE.C s
e:vax INTERF.C s
e:vax LEX.C s
e:vax MAIN.C s
e:vax STMT.C s
e:vax TABL.C s
e:vax VERSION.C s
e:vax ICODE.H s
e:vax PARSER.H s
e:vax MACHINE.68K s
e:vax LINK.SUB s
e:vax MAKE.SUB s
e:vax MACHINE.H s
e:vax SEND12.SUB s

View File

@@ -0,0 +1 @@
char *compiled "@(#) parser - Thu Feb 10 13:02 1983";

View File

@@ -0,0 +1,2 @@
1File: VERSION.C Page 1
1 char *compiled "@(#) parser - Thu Feb 10 13:02 1983";

View File

@@ -0,0 +1,18 @@
$ parser
$ set noon
$ !
$ ! C068 make file for VMS
$ !
$ copy machine.vax machine.h
$ pur machine.h
$ cx DECL
$ cx EXPR
$ cx ICODE
$ cx INIT
$ cx INTERF
$ cx LEX
$ cx MAIN
$ cx STMT
$ cx TABL
$ cx VERSION
$ clink DECL,EXPR,ICODE,INTERF,LEX,MAIN,STMT,TABL,init,version,lib:klib/lib c068

View File

@@ -0,0 +1,24 @@
$ set noon
$ vsend DECL.C
$ vsend EXPR.C
$ vsend ICODE.C
$ vsend ICODE.H
$ vsend INIT.C
$ vsend INTERF.C
$ vsend LEX.C
$ vsend LINK.SUB
$ vsend LIST.COM
$ vsend MACHINE.11
$ vsend MACHINE.68K
$ vsend MACHINE.H
$ vsend MACHINE.VAX
$ vsend MAIN.C
$ vsend MAKE.SUB
$ vsend PARSER.H
$ vsend SEND12.SUB
$ vsend STMT.C
$ vsend TABL.C
$ vsend UP12.SUB
$ vsend VERSION.C
$ vsend VMAKE.COM
$ vsend done

View File

@@ -0,0 +1,28 @@
Directory DRB0:[STEVE.CPM68K.V102.C068]
DECL.C;1
EXPR.C;1
ICODE.C;1
ICODE.H;1
INIT.C;16
INTERF.C;1
LEX.C;1
LINK.SUB;1
LIST.COM;2
MACHINE.11;1
MACHINE.68K;2
MACHINE.H;9
MACHINE.VAX;1
MAIN.C;1
MAKE.SUB;1
PARSER.H;2
SEND12.SUB;1
STMT.C;1
TABL.C;1
UP12.SUB;1
VERSION.C;1
VMAKE.COM;7
VSEND.COM;1
Total of 23 files.

View File

@@ -0,0 +1,13 @@
$ xer DECL.LIS
$ xer EXPR.LIS
$ xer ICODE.LIS
$ xer INIT.LIS
$ xer INTERF.LIS
$ xer LEX.LIS
$ xer MAIN.LIS
$ xer STMT.LIS
$ xer TABL.LIS
$ xer VERSION.LIS
$ xer ICODE.LST
$ xer MACHINE.LST
$ xer PARSER.LST