Commit | Line | Data |
---|---|---|
1a3b21a9 DR |
1 | # |
2 | /* | |
3 | * C compiler-- first pass header | |
4 | */ | |
5 | ||
6 | #include <stdio.h> | |
7 | ||
8 | /* | |
9 | * parameters | |
10 | */ | |
11 | ||
12 | #define LTYPE long /* change to int if no long consts */ | |
13 | #define MAXINT 077777 /* Largest positive short integer */ | |
14 | #define MAXUINT 0177777 /* largest unsigned integer */ | |
15 | #define NCPS 8 /* # chars per symbol */ | |
16 | #define HSHSIZ 400 /* # entries in hash table for names */ | |
17 | #define CMSIZ 40 /* size of expression stack */ | |
18 | #define SSIZE 20 /* size of other expression stack */ | |
19 | #define SWSIZ 230 /* size of switch table */ | |
20 | #define NMEMS 128 /* Number of members in a structure */ | |
21 | #define NBPW 16 /* bits per word, object machine */ | |
22 | #define NBPC 8 /* bits per character, object machine */ | |
23 | #define NCPW 2 /* chars per word, object machine */ | |
24 | #define LNCPW 2 /* chars per word, compiler's machine */ | |
25 | #define STAUTO (-6) /* offset of first auto variable */ | |
26 | #define STARG 4 /* offset of first argument */ | |
27 | ||
28 | ||
29 | /* | |
30 | * # bytes in primitive types | |
31 | */ | |
32 | #define SZCHAR 1 | |
33 | #define SZINT 2 | |
34 | #define SZPTR 2 | |
35 | #define SZFLOAT 4 | |
36 | #define SZLONG 4 | |
37 | #define SZDOUB 8 | |
38 | ||
39 | /* | |
40 | * format of a structure description | |
41 | */ | |
42 | struct str { | |
43 | int ssize; /* structure size */ | |
44 | struct hshtab **memlist; /* member list */ | |
45 | }; | |
46 | ||
47 | /* | |
48 | * For fields, strp points here instead. | |
49 | */ | |
50 | struct field { | |
51 | int flen; /* field width in bits */ | |
52 | int bitoffs; /* shift count */ | |
53 | }; | |
54 | ||
55 | /* | |
56 | * Structure of tree nodes for operators | |
57 | */ | |
58 | struct tnode { | |
59 | int op; /* operator */ | |
60 | int type; /* data type */ | |
61 | int *subsp; /* subscript list (for arrays) */ | |
62 | struct str *strp; /* structure description for structs */ | |
63 | struct tnode *tr1; /* left operand */ | |
64 | struct tnode *tr2; /* right operand */ | |
65 | }; | |
66 | ||
67 | /* | |
68 | * Tree node for constants | |
69 | */ | |
70 | struct cnode { | |
71 | int op; | |
72 | int type; | |
73 | int *subsp; | |
74 | struct str *strp; | |
75 | int value; | |
76 | }; | |
77 | ||
78 | /* | |
79 | * Tree node for long constants | |
80 | */ | |
81 | struct lnode { | |
82 | int op; | |
83 | int type; | |
84 | int *subsp; | |
85 | struct str *strp; | |
86 | LTYPE lvalue; | |
87 | }; | |
88 | ||
89 | /* | |
90 | * tree node for floating | |
91 | * constants | |
92 | */ | |
93 | struct fnode { | |
94 | int op; | |
95 | int type; | |
96 | int *subsp; | |
97 | struct str *strp; | |
98 | char *cstr; | |
99 | }; | |
100 | ||
101 | /* | |
102 | * Structure of namelist | |
103 | */ | |
104 | /* | |
105 | * Pushed-down entry for block structure | |
106 | */ | |
107 | struct phshtab { | |
108 | char hclass; | |
109 | char hflag; | |
110 | int htype; | |
111 | int *hsubsp; | |
112 | struct str *hstrp; | |
113 | int hoffset; | |
114 | struct phshtab *hpdown; | |
115 | char hblklev; | |
116 | }; | |
117 | ||
118 | /* | |
119 | * Top-level namelist | |
120 | */ | |
121 | struct hshtab { | |
122 | char hclass; /* storage class */ | |
123 | char hflag; /* various flags */ | |
124 | int htype; /* type */ | |
125 | int *hsubsp; /* subscript list */ | |
126 | struct str *hstrp; /* structure description */ | |
127 | int hoffset; /* post-allocation location */ | |
128 | struct phshtab *hpdown; /* Pushed-down name in outer block */ | |
129 | char hblklev; /* Block level of definition */ | |
130 | char name[NCPS]; /* ASCII name */ | |
131 | }; | |
132 | ||
133 | /* | |
134 | * Place used to keep dimensions | |
135 | * during declarations | |
136 | */ | |
137 | struct tdim { | |
138 | int rank; | |
139 | int dimens[5]; | |
140 | }; | |
141 | ||
142 | /* | |
143 | * Table for recording switches. | |
144 | */ | |
145 | struct swtab { | |
146 | int swlab; | |
147 | int swval; | |
148 | }; | |
149 | ||
150 | char cvtab[4][4]; | |
151 | char filename[64]; | |
152 | int opdope[]; | |
153 | char ctab[]; | |
154 | char symbuf[NCPS+2]; | |
155 | int hshused; | |
156 | struct hshtab hshtab[HSHSIZ]; | |
157 | struct tnode **cp; | |
158 | int isn; | |
159 | struct swtab swtab[SWSIZ]; | |
160 | struct swtab *swp; | |
161 | int contlab; | |
162 | int brklab; | |
163 | int retlab; | |
164 | int deflab; | |
165 | unsigned autolen; /* make these int if necessary */ | |
166 | unsigned maxauto; /* ... will only cause trouble rarely */ | |
167 | int peeksym; | |
168 | int peekc; | |
169 | int eof; | |
170 | int line; | |
171 | char *funcbase; | |
172 | char *curbase; | |
173 | char *coremax; | |
174 | char *maxdecl; | |
175 | struct hshtab *defsym; | |
176 | struct hshtab *funcsym; | |
177 | int proflg; | |
178 | struct hshtab *csym; | |
179 | int cval; | |
180 | LTYPE lcval; | |
181 | int nchstr; | |
182 | int nerror; | |
183 | struct hshtab **paraml; | |
184 | struct hshtab **parame; | |
185 | int strflg; | |
186 | int mosflg; | |
187 | int initflg; | |
188 | int inhdr; | |
189 | char sbuf[BUFSIZ]; | |
190 | FILE *sbufp; | |
191 | int regvar; | |
192 | int bitoffs; | |
193 | struct tnode funcblk; | |
194 | char cvntab[]; | |
195 | char numbuf[64]; | |
196 | struct hshtab **memlist; | |
197 | int nmems; | |
198 | struct hshtab structhole; | |
199 | int blklev; | |
200 | int mossym; | |
201 | ||
202 | /* | |
203 | operators | |
204 | */ | |
205 | #define EOFC 0 | |
206 | #define NULLOP 218 | |
207 | #define SEMI 1 | |
208 | #define LBRACE 2 | |
209 | #define RBRACE 3 | |
210 | #define LBRACK 4 | |
211 | #define RBRACK 5 | |
212 | #define LPARN 6 | |
213 | #define RPARN 7 | |
214 | #define COLON 8 | |
215 | #define COMMA 9 | |
216 | #define FSEL 10 | |
217 | #define CAST 11 | |
218 | #define ETYPE 12 | |
219 | ||
220 | #define KEYW 19 | |
221 | #define NAME 20 | |
222 | #define CON 21 | |
223 | #define STRING 22 | |
224 | #define FCON 23 | |
225 | #define SFCON 24 | |
226 | #define LCON 25 | |
227 | #define SLCON 26 | |
228 | ||
229 | #define SIZEOF 91 | |
230 | #define INCBEF 30 | |
231 | #define DECBEF 31 | |
232 | #define INCAFT 32 | |
233 | #define DECAFT 33 | |
234 | #define EXCLA 34 | |
235 | #define AMPER 35 | |
236 | #define STAR 36 | |
237 | #define NEG 37 | |
238 | #define COMPL 38 | |
239 | ||
240 | #define DOT 39 | |
241 | #define PLUS 40 | |
242 | #define MINUS 41 | |
243 | #define TIMES 42 | |
244 | #define DIVIDE 43 | |
245 | #define MOD 44 | |
246 | #define RSHIFT 45 | |
247 | #define LSHIFT 46 | |
248 | #define AND 47 | |
249 | #define OR 48 | |
250 | #define EXOR 49 | |
251 | #define ARROW 50 | |
252 | #define ITOF 51 | |
253 | #define FTOI 52 | |
254 | #define LOGAND 53 | |
255 | #define LOGOR 54 | |
256 | #define FTOL 56 | |
257 | #define LTOF 57 | |
258 | #define ITOL 58 | |
259 | #define LTOI 59 | |
260 | #define ITOP 13 | |
261 | #define PTOI 14 | |
262 | #define LTOP 15 | |
263 | ||
264 | #define EQUAL 60 | |
265 | #define NEQUAL 61 | |
266 | #define LESSEQ 62 | |
267 | #define LESS 63 | |
268 | #define GREATEQ 64 | |
269 | #define GREAT 65 | |
270 | #define LESSEQP 66 | |
271 | #define LESSP 67 | |
272 | #define GREATQP 68 | |
273 | #define GREATP 69 | |
274 | ||
275 | #define ASPLUS 70 | |
276 | #define ASMINUS 71 | |
277 | #define ASTIMES 72 | |
278 | #define ASDIV 73 | |
279 | #define ASMOD 74 | |
280 | #define ASRSH 75 | |
281 | #define ASLSH 76 | |
282 | #define ASSAND 77 | |
283 | #define ASOR 78 | |
284 | #define ASXOR 79 | |
285 | #define ASSIGN 80 | |
286 | ||
287 | #define QUEST 90 | |
288 | #define MAX 93 | |
289 | #define MAXP 94 | |
290 | #define MIN 95 | |
291 | #define MINP 96 | |
292 | #define SEQNC 97 | |
293 | #define CALL 100 | |
294 | #define MCALL 101 | |
295 | #define JUMP 102 | |
296 | #define CBRANCH 103 | |
297 | #define INIT 104 | |
298 | #define SETREG 105 | |
299 | #define RFORCE 110 | |
300 | #define BRANCH 111 | |
301 | #define LABEL 112 | |
302 | #define NLABEL 113 | |
303 | #define RLABEL 114 | |
304 | #define STRASG 115 | |
305 | #define ITOC 109 | |
306 | #define SEOF 200 /* stack EOF marker in expr compilation */ | |
307 | ||
308 | /* | |
309 | types | |
310 | */ | |
311 | #define INT 0 | |
312 | #define CHAR 1 | |
313 | #define FLOAT 2 | |
314 | #define DOUBLE 3 | |
315 | #define STRUCT 4 | |
316 | #define LONG 6 | |
317 | #define UNSIGN 7 | |
318 | #define UNION 8 /* adjusted later to struct */ | |
319 | ||
320 | #define ALIGN 01 | |
321 | #define TYPE 07 | |
322 | #define BIGTYPE 060000 | |
323 | #define TYLEN 2 | |
324 | #define XTYPE (03<<3) | |
325 | #define PTR 010 | |
326 | #define FUNC 020 | |
327 | #define ARRAY 030 | |
328 | ||
329 | /* | |
330 | storage classes | |
331 | */ | |
332 | #define KEYWC 1 | |
333 | #define DEFXTRN 20 | |
334 | #define TYPEDEF 9 | |
335 | #define MOS 10 | |
336 | #define AUTO 11 | |
337 | #define EXTERN 12 | |
338 | #define STATIC 13 | |
339 | #define REG 14 | |
340 | #define STRTAG 15 | |
341 | #define ARG 16 | |
342 | #define ARG1 17 | |
343 | #define AREG 18 | |
344 | #define MOU 21 | |
345 | #define ENUMTAG 22 | |
346 | #define ENUMCON 24 | |
347 | ||
348 | /* | |
349 | keywords | |
350 | */ | |
351 | #define GOTO 20 | |
352 | #define RETURN 21 | |
353 | #define IF 22 | |
354 | #define WHILE 23 | |
355 | #define ELSE 24 | |
356 | #define SWITCH 25 | |
357 | #define CASE 26 | |
358 | #define BREAK 27 | |
359 | #define CONTIN 28 | |
360 | #define DO 29 | |
361 | #define DEFAULT 30 | |
362 | #define FOR 31 | |
363 | #define ENUM 32 | |
364 | ||
365 | /* | |
366 | characters | |
367 | */ | |
368 | #define BSLASH 117 | |
369 | #define SHARP 118 | |
370 | #define INSERT 119 | |
371 | #define PERIOD 120 | |
372 | #define SQUOTE 121 | |
373 | #define DQUOTE 122 | |
374 | #define LETTER 123 | |
375 | #define DIGIT 124 | |
376 | #define NEWLN 125 | |
377 | #define SPACE 126 | |
378 | #define UNKN 127 | |
379 | ||
380 | /* | |
381 | * Special operators in intermediate code | |
382 | */ | |
383 | #define BDATA 200 | |
384 | #define WDATA 201 | |
385 | #define PROG 202 | |
386 | #define DATA 203 | |
387 | #define BSS 204 | |
388 | #define CSPACE 205 | |
389 | #define SSPACE 206 | |
390 | #define SYMDEF 207 | |
391 | #define SAVE 208 | |
392 | #define RETRN 209 | |
393 | #define EVEN 210 | |
394 | #define PROFIL 212 | |
395 | #define SWIT 213 | |
396 | #define EXPR 214 | |
397 | #define SNAME 215 | |
398 | #define RNAME 216 | |
399 | #define ANAME 217 | |
400 | #define SETSTK 219 | |
401 | #define SINIT 220 | |
402 | ||
403 | /* | |
404 | Flag bits | |
405 | */ | |
406 | ||
407 | #define BINARY 01 | |
408 | #define LVALUE 02 | |
409 | #define RELAT 04 | |
410 | #define ASSGOP 010 | |
411 | #define LWORD 020 | |
412 | #define RWORD 040 | |
413 | #define COMMUTE 0100 | |
414 | #define RASSOC 0200 | |
415 | #define LEAF 0400 | |
416 | ||
417 | /* | |
418 | * Conversion codes | |
419 | */ | |
420 | #define ITF 1 | |
421 | #define ITL 2 | |
422 | #define LTF 3 | |
423 | #define ITP 4 | |
424 | #define PTI 5 | |
425 | #define FTI 6 | |
426 | #define LTI 7 | |
427 | #define FTL 8 | |
428 | #define LTP 9 | |
429 | #define ITC 10 | |
430 | #define XX 15 | |
431 | ||
432 | /* | |
433 | * symbol table flags | |
434 | */ | |
435 | ||
436 | #define FMOS 01 | |
437 | #define FKEYW 04 | |
438 | #define FFIELD 020 | |
439 | #define FINIT 040 | |
440 | #define FLABL 0100 | |
441 | ||
442 | /* | |
443 | * functions | |
444 | */ | |
445 | char *sbrk(); | |
446 | struct tnode *tree(); | |
447 | char *copnum(); | |
448 | struct tnode *convert(); | |
449 | struct tnode *chkfun(); | |
450 | struct tnode *disarray(); | |
451 | struct tnode *block(); | |
452 | struct cnode *cblock(); | |
453 | struct fnode *fblock(); | |
454 | char *gblock(); | |
455 | struct tnode *pexpr(); | |
456 | struct str *strdec(); | |
457 | struct hshtab *xprtype(); | |
458 | struct tnode *nblock(); |