1
|
1 /* Micro-C Parser Part */
|
|
2 /* $Id$ */
|
|
3
|
0
|
4 #define EXTERN /**/
|
|
5 #include "mc.h"
|
|
6
|
|
7 void ntable_consistency();
|
|
8 static void adecl(NMTBL *n);
|
13
|
9 static int decl_data(int t, NMTBL *n,int offset);
|
0
|
10 static int alpha(char c);
|
|
11 static int binop(int op, int e1, int e2, int t1, int t2);
|
|
12 static void compatible(int t1, int t2);
|
|
13 static void decl(void);
|
37
|
14 static NMTBL *def(NMTBL *n);
|
0
|
15 static int digit(char c);
|
|
16 static void docase(void);
|
|
17 static void docomp(void);
|
|
18 static void dodefault(void);
|
|
19 static void dodo(void);
|
|
20 static void dofor(void);
|
|
21 static void dogoto(void);
|
|
22 static void doif(void);
|
|
23 static void dolabel(void);
|
|
24 static void doswitch(void);
|
|
25 static void doreturn(void);
|
|
26 static void dowhile(void);
|
|
27 static void errmsg(void);
|
18
|
28 static void macro_processing();
|
32
|
29 static void macro_define();
|
|
30 static void macro_define0();
|
0
|
31 static void copy(NMTBL *nptr, char *s);
|
|
32 void error(int n);
|
|
33 static int expr(void);
|
|
34 static int expr0(void);
|
|
35 static int expr1(void);
|
|
36 static int expr2(void);
|
|
37 static int expr3(void);
|
|
38 static int expr4(void);
|
|
39 static int expr5(void);
|
|
40 static int expr6(void);
|
|
41 static int expr7(void);
|
|
42 static int expr8(void);
|
|
43 static int expr9(void);
|
|
44 static int expr10(void);
|
|
45 static int expr11(void);
|
|
46 static int expr12(void);
|
|
47 static int expr13(void);
|
|
48 static int expr14(void);
|
|
49 static int expr15(int e1);
|
|
50 static int expr16(int e1);
|
|
51 static void fcheck(NMTBL *n);
|
|
52 static void fdecl(NMTBL *n);
|
|
53 static int getch(void);
|
|
54 static int getfree(int n);
|
|
55 static void getline(void);
|
|
56 static void getstring(void);
|
|
57 static int getsym(void);
|
|
58 static int indop(int e);
|
|
59 static void init(void);
|
|
60 static int integral(int t);
|
|
61 static void lcheck(int e);
|
18
|
62 extern int glist2(int e1,int e2);
|
|
63 extern int list2(int e1, int e2);
|
|
64 extern int list3(int e1, int e2, int e3);
|
|
65 extern int list4(int e1, int e2, int e3, int e4);
|
33
|
66 extern int rplacad(int e, int n);
|
|
67 extern int rplacadd(int e, int n);
|
0
|
68 static void reserve(char *s, int d);
|
|
69 static int macroeq(char *s);
|
|
70 static int ndecl0(void);
|
|
71 static int ndecl1(void);
|
30
|
72 static int neqname(char *p,char *name);
|
0
|
73 static void newfile(void);
|
|
74 static int postequ(int s1, int s2);
|
|
75 static void reverse(int t1);
|
2
|
76 int reverse0(int t1);
|
0
|
77 static int rvalue(int e);
|
2
|
78 int scalar(int t);
|
0
|
79 static int sdecl(int s);
|
|
80 static int skipspc(void);
|
|
81 static void statement(void);
|
|
82 static int strop(int e);
|
|
83 static int typeid(int s);
|
|
84 static int typename(void);
|
|
85 static int typespec(void);
|
|
86 static int cexpr(int e);
|
|
87 static void code_decl(NMTBL *n);
|
37
|
88 static int macro_args(char **pcheapp,char* max,char **pchptr);
|
34
|
89 static int macro_function(int macrop,char **pchptr,NMTBL *nptr,int history);
|
28
|
90 static void local_define();
|
|
91 static void local_undef();
|
34
|
92 static int macro_eval(int macrop,char *body,int history);
|
|
93 static char * append(int lists);
|
37
|
94 static NMTBL *free_nptr();
|
39
|
95 static void replace_return_struct(int func,int left);
|
0
|
96
|
18
|
97 extern void display_ntable(NMTBL *n, char *s);
|
0
|
98 extern void closing(void);
|
|
99 extern void opening(char *filename);
|
|
100 extern void gen_gdecl(char *n, int gpc);
|
18
|
101 extern void emit_init(void);
|
0
|
102 extern void enter(char *name);
|
|
103 extern void enter1(int disp);
|
36
|
104 extern void leave(int control, char *name,int disp);
|
0
|
105 extern void ret(void);
|
|
106 extern void jmp(int l);
|
|
107 extern void gexpr(int e1);
|
18
|
108 extern void g_expr(int e1);
|
0
|
109 extern int get_register_var(void);
|
40
|
110 extern void use_register_var(int);
|
0
|
111 extern void bexpr(int e1, char cond, int l1);
|
|
112 extern int fwdlabel(void);
|
|
113 extern void fwddef(int l);
|
|
114 extern int backdef(void);
|
|
115 extern int def_label(int cslabel, int dlabel);
|
|
116 extern void jmp_label(int l);
|
|
117 extern void cmpdimm(int e, int csreg);
|
|
118 extern void jcond(int l, char cond);
|
|
119 extern void jmp_eq_label(int l);
|
|
120 extern void gen_comment(char *s);
|
|
121 extern void gen_source(char *s);
|
|
122 extern void code_init(void);
|
18
|
123 extern void code_enter(char *name) ;
|
36
|
124 extern void code_leave(char *name,int disp) ;
|
18
|
125 extern void code_enter1(int disp0,int args);
|
|
126
|
0
|
127 extern void emit_data_closing(NMTBL *n);
|
|
128 extern void emit_data(int e, int t, NMTBL *n);
|
|
129
|
18
|
130 extern void exit(int l);
|
|
131
|
37
|
132 static int struct_return = 0;
|
|
133
|
0
|
134 int
|
|
135 main(int argc, char **argv)
|
|
136 {
|
|
137 NMTBL *nptr;
|
|
138 int i;
|
|
139 char *ccout;
|
|
140
|
|
141 if(argc==1) exit(1);
|
|
142 lsrc = chk = asmf = 0;
|
|
143 ccout = OUTPUT_FILE_NAME;
|
|
144 ac=argc;
|
|
145 av=argv;
|
|
146 for (ac2=1; (ac2 < ac) && (*av[ac2] == '-'); ++ac2) {
|
|
147 switch (*(av[ac2]+1)) {
|
|
148 case 'S': case 's':
|
|
149 lsrc = 1;
|
|
150 break;
|
|
151 case 'O': case 'o':
|
|
152 ccout = av[ac2]+2;
|
|
153 break;
|
|
154 case 'C': case 'c':
|
|
155 chk = 1;
|
|
156 break;
|
|
157 case 'D': case 'd':
|
|
158 debug = 1;
|
|
159 break;
|
|
160 default:
|
|
161 error(OPTION);
|
|
162 exit(1);
|
|
163 }
|
|
164 }
|
|
165 if (!chk)
|
18
|
166 if ( (freopen(ccout,"w",stdout)) == NULL ) error(FILERR);
|
0
|
167 init();
|
|
168 while(1) {
|
2
|
169 for (nptr = &ntable[GSYMS],i=LSYMS; i--;) {
|
0
|
170 (nptr++)->sc = 0;
|
2
|
171 }
|
|
172 emit_init();
|
0
|
173 mode=TOP;
|
|
174 lfree= HEAPSIZE;
|
|
175 while(getsym()==SM);
|
|
176 mode=GDECL;
|
|
177 stmode=0;
|
|
178 args=0;
|
|
179 decl();
|
|
180 }
|
|
181 /*NOTREACHED*/
|
|
182 }
|
|
183
|
|
184 void
|
|
185 error(int n)
|
|
186 {
|
|
187 if(n == EOFERR) {
|
|
188 if(filep!=filestack) {
|
|
189 fclose(filep->fcb);
|
|
190 lineno=filep->ln;
|
|
191 --filep;
|
|
192 return;
|
|
193 } else if(ac2!=ac) {
|
|
194 fclose(filep->fcb);
|
|
195 newfile();
|
|
196 return;
|
|
197 } else if(mode == TOP) {
|
18
|
198 /*
|
|
199 if (!chk) fprintf(stderr,
|
|
200 "Total internal labels : %u.\n",labelno-1);
|
|
201 fprintf(stderr,
|
|
202 "Total global variables : %u bytes.\n\n",gpc);
|
|
203 */
|
0
|
204 closing();
|
|
205 exit(0);
|
|
206 }
|
|
207 }
|
25
|
208 fprintf(stderr,"%s:%d:%s\n",filep->name0,lineno,
|
0
|
209 (n==FILERR) ? "Can't open specified file" :
|
|
210 (n==DCERR) ? "Declaration syntax" :
|
|
211 (n==STERR) ? "Statement syntax" :
|
|
212 (n==EXERR) ? "Expression syntax" :
|
|
213 (n==CNERR) ? "Constant required" :
|
|
214 (n==CHERR) ? "Illegal character" :
|
|
215 (n==GSERR) ? "Too many global symbols" :
|
|
216 (n==LSERR) ? "Too many local symbols" :
|
29
|
217 (n==MSERR) ? "Too many macro symbols" :
|
0
|
218 (n==STRERR) ? "Too many strings or macros" :
|
|
219 (n==LNERR) ? "Line too long" :
|
|
220 (n==EOFERR) ? "Unexpected end of file" :
|
|
221 (n==MCERR) ? "Macro syntax" :
|
|
222 (n==INCERR) ? "Include syntax" :
|
|
223 (n==HPERR) ? "Too long expression" :
|
|
224 (n==TYERR) ? "Type mismatch" :
|
|
225 (n==LVERR) ? "Lvalue required" :
|
|
226 (n==UDERR) ? "Undeclared identifier" :
|
|
227 (n==OPTION) ? "Illegal option" :
|
|
228 (n==REG_ERR) ? "illegal register var" :
|
|
229 (n==CODE_ERR) ? "goto code is necessary" :
|
|
230 "Bug of compiler");
|
|
231 errmsg();
|
|
232 exit(1);
|
|
233 }
|
|
234
|
|
235 void
|
|
236 errmsg(void)
|
|
237 {
|
|
238 char *p,*lim;
|
|
239
|
|
240 if(lineno==0) return;
|
|
241 fprintf(stderr,"%s",linebuf);
|
29
|
242 lim=chptr;
|
34
|
243 if (chptrsave) {
|
|
244 lim = chptrsave;
|
29
|
245 }
|
0
|
246 for (p=linebuf; p < lim;)
|
|
247 fprintf(stderr,(*p++ == '\t') ? "\t" : " ");
|
|
248 fprintf (stderr,"^\n");
|
|
249 }
|
|
250
|
|
251 void
|
|
252 checksym(int s)
|
|
253 {
|
|
254 char *p;
|
|
255
|
|
256 if (sym != s) {
|
|
257 p=(s==RPAR) ? "')'": (s==RBRA) ? "']'": (s==SM) ? "';'":
|
|
258 (s==LPAR) ? "'('": (s==WHILE) ? "'while'":
|
|
259 (s==COLON) ? "':'": "Identifier";
|
|
260 fprintf(stderr,"%d:%s expected.\n",lineno,p);
|
|
261 errmsg();
|
|
262 } else
|
|
263 getsym();
|
|
264 }
|
|
265
|
|
266 void
|
|
267 init(void)
|
|
268 {
|
|
269 NMTBL *nptr;
|
|
270 int i;
|
|
271
|
|
272 cheapp=cheap;
|
|
273 for(nptr = ntable,i = GSYMS; i--;) (nptr++)->sc = 0;
|
30
|
274 for(nptr = mtable,i = MSYMS; i--;) (nptr++)->sc = 0;
|
0
|
275 reserve("int",INT);
|
|
276 reserve("void",VOID);
|
|
277 reserve("char",CHAR);
|
20
|
278 reserve("const",KONST);
|
0
|
279 reserve("struct",STRUCT);
|
|
280 reserve("union",UNION);
|
|
281 reserve("unsigned",UNSIGNED);
|
|
282 reserve("static",STATIC);
|
|
283 reserve("goto",GOTO);
|
|
284 reserve("return",RETURN);
|
|
285 reserve("break",BREAK);
|
|
286 reserve("continue",CONTINUE);
|
|
287 reserve("if",IF);
|
|
288 reserve("else",ELSE);
|
|
289 reserve("for",FOR);
|
|
290 reserve("do",DO);
|
|
291 reserve("while",WHILE);
|
|
292 reserve("switch",SWITCH);
|
|
293 reserve("case",CASE);
|
|
294 reserve("default",DEFAULT);
|
|
295 reserve("typedef",TYPEDEF);
|
|
296 reserve("sizeof",SIZEOF);
|
|
297 reserve("long",LONG);
|
|
298 reserve("short",SHORT);
|
|
299 reserve("extern",EXTRN);
|
18
|
300 reserve("defined",DEFINED);
|
0
|
301 reserve("register",REGISTER);
|
|
302 reserve("code",CODE);
|
|
303 reserve("environment",ENVIRONMENT);
|
19
|
304
|
34
|
305 gpc=glineno=0;
|
0
|
306 gfree=ilabel=1;
|
|
307 labelno=2;
|
|
308 lfree=HEAPSIZE;
|
|
309 filep=filestack;
|
|
310 code_init();
|
|
311 newfile();
|
32
|
312
|
|
313 macro_define("__micro_c__ 1\n");
|
|
314
|
0
|
315 getline();
|
|
316 getch();
|
|
317 }
|
|
318
|
|
319 void
|
|
320 newfile(void)
|
|
321 {
|
25
|
322 char *s;
|
0
|
323 lineno=0;
|
18
|
324 /* fprintf(stderr,"%s:\n",av[ac2]); */
|
0
|
325 opening(av[ac2]);
|
|
326 if ( (filep->fcb = fopen(av[ac2++],"r")) == NULL ) error(FILERR);
|
25
|
327 s = av[ac2-1];
|
|
328 filep->name0 = cheapp;
|
|
329 while((*cheapp++ = *s++));
|
0
|
330 }
|
|
331
|
|
332 void
|
|
333 reserve(char *s, int d)
|
|
334 {
|
|
335 NMTBL *nptr;
|
|
336 int i;
|
|
337
|
|
338 hash=0; name=namebuf; i=0;
|
|
339 while((name[i++] = *s)) {
|
|
340 hash=((7*hash) ^ *s++);
|
|
341 }
|
|
342 if (cheapp+i >= cheap+CHEAPSIZE) error(STRERR);
|
|
343 name[i++] = 0;
|
|
344 (nptr = gsearch())->sc = RESERVE;
|
19
|
345 if (d==0) {
|
|
346 nptr->sc = MACRO;
|
29
|
347 nptr->dsp = (int)""; nptr->ty=0;
|
19
|
348 } else {
|
|
349 nptr->dsp = d;
|
|
350 }
|
0
|
351 }
|
|
352
|
|
353 static
|
|
354 NMTBL null_nptr;
|
|
355
|
|
356 void
|
|
357 decl(void)
|
|
358 {
|
|
359 NMTBL *n;
|
|
360 int t;
|
|
361
|
|
362 if(sym==STATIC) {
|
|
363 if(mode==LDECL) {
|
|
364 getsym();
|
|
365 mode=STADECL;
|
|
366 stmode=LDECL;
|
|
367 } else if(mode==GDECL) {
|
|
368 getsym();
|
|
369 stmode=STATIC;
|
|
370 } else
|
|
371 error(DCERR);
|
|
372 } else if(sym==REGISTER) {
|
|
373 if(mode!=LDECL)
|
|
374 error(DCERR);
|
|
375 stmode=REGISTER;
|
|
376 getsym();
|
|
377 } else if(sym==EXTRN) {
|
|
378 getsym();
|
|
379 stmode=EXTRN;
|
|
380 } else if(sym==TYPEDEF) {
|
|
381 if(mode==GDECL) {
|
|
382 getsym();
|
|
383 mode=GTDECL;
|
|
384 } else if(mode==LDECL) {
|
|
385 getsym();
|
|
386 mode=LTDECL;
|
|
387 } else
|
|
388 error(DCERR);
|
|
389 }
|
|
390 if((t=typespec())==0) return;
|
|
391 if(sym==SM) return;
|
|
392 type=t;
|
|
393 n=decl0();
|
|
394 reverse(t);
|
|
395 if (n == &null_nptr) {
|
|
396 error(DCERR);
|
|
397 return;
|
|
398 }
|
|
399 if(sym==LC || ( sym!=SM && sym!=COMMA && sym!=ASS )) {
|
|
400 if (car(type)==CODE) {
|
|
401 code_decl(n); return;
|
|
402 } else if (car(type)==FUNCTION) {
|
|
403 fdecl(n); return;
|
|
404 }
|
|
405 }
|
|
406 def(n);
|
|
407 while(sym==COMMA) {
|
|
408 getsym();
|
|
409 type=t;
|
|
410 n=decl0();
|
|
411 reverse(t);
|
|
412 if(n == &null_nptr) error(DCERR);
|
|
413 /* if(args) error(DCERR); */
|
|
414 def(n);
|
|
415 }
|
|
416 if(sym!=SM) error(DCERR);
|
|
417 if(mode==GTDECL)
|
|
418 mode=GDECL;
|
|
419 if(mode==STADECL||mode==LTDECL)
|
|
420 mode=LDECL;
|
|
421 }
|
|
422
|
|
423 int
|
|
424 typespec(void)
|
|
425 {
|
|
426 int t;
|
|
427
|
20
|
428 while (sym==KONST) {
|
|
429 getsym();
|
|
430 }
|
0
|
431 switch(sym) {
|
|
432 case VOID:
|
|
433 case INT:
|
|
434 case CHAR:
|
|
435 case CODE:
|
|
436 t= sym;
|
|
437 getsym();
|
|
438 break;
|
|
439 case STRUCT:
|
|
440 case UNION:
|
|
441 t=sdecl(sym);
|
|
442 break;
|
|
443 case UNSIGNED:
|
|
444 t = UNSIGNED;
|
|
445 if(getsym()==INT) getsym();
|
|
446 break;
|
|
447 case SHORT:
|
|
448 t=CHAR;
|
|
449 if(getsym()==INT) getsym();
|
|
450 break;
|
|
451 case LONG:
|
|
452 t=INT;
|
|
453 if(getsym()==INT) getsym();
|
|
454 break;
|
|
455 default:
|
|
456 if(sym==IDENT) {
|
|
457 if(nptr->sc==TYPE) {
|
|
458 t=nptr->ty;
|
|
459 getsym();
|
|
460 break;
|
|
461 } else if(nptr->sc==EMPTY && gnptr->sc==TYPE) {
|
|
462 t=gnptr->ty;
|
|
463 getsym();
|
|
464 break;
|
|
465 }
|
|
466 }
|
20
|
467 while (sym==KONST) {
|
|
468 getsym();
|
|
469 }
|
0
|
470 if(mode==LDECL) return 0;
|
|
471 t= INT;
|
|
472 }
|
20
|
473 while (sym==KONST) {
|
|
474 getsym();
|
|
475 }
|
0
|
476 return t;
|
|
477 }
|
|
478
|
|
479 struct nametable *
|
|
480 decl0(void)
|
|
481 {
|
|
482 NMTBL *n;
|
|
483 if(sym==MUL) {
|
|
484 getsym();
|
|
485 n=decl0();
|
|
486 type=list2(POINTER,type);
|
|
487 return n;
|
|
488 }
|
|
489 return decl1();
|
|
490 }
|
|
491
|
|
492
|
|
493 NMTBL *
|
|
494 decl1(void)
|
|
495 {
|
|
496 NMTBL *n;
|
|
497 int i,t;
|
|
498
|
|
499 if(sym==LPAR) {
|
|
500 getsym();
|
|
501 n=decl0();
|
|
502 checksym(RPAR);
|
|
503 } else if (sym == IDENT) {
|
|
504 n=nptr;
|
|
505 getsym();
|
|
506 } else {
|
|
507 /* error(DCERR); */
|
|
508 n= &null_nptr;
|
|
509 }
|
|
510 while(1) {
|
|
511 if(sym==LBRA) {
|
|
512 if(getsym()==RBRA) {
|
|
513 getsym();
|
|
514 if(mode==ADECL) {
|
|
515 t=type;
|
|
516 type=list2(POINTER,type);
|
|
517 } else if (mode==GDECL) {
|
|
518 t=type;
|
|
519 type=list3(ARRAY,t,0);
|
|
520 } else {
|
|
521 error(DCERR);
|
|
522 }
|
|
523 } else {
|
|
524 t=type;
|
|
525 i=cexpr(expr());
|
|
526 checksym(RBRA);
|
|
527 type=list3(ARRAY,t,i);
|
|
528 }
|
|
529 } else if(sym==LPAR) {
|
|
530 if(mode==GDECL) {
|
|
531 mode=ADECL;getsym();mode=GDECL; /* ??? */
|
|
532 } else
|
|
533 getsym();
|
|
534 if(sym==RPAR)
|
|
535 getsym();
|
|
536 else {
|
|
537 if (type==CODE) {
|
|
538 n->sc=CODE;
|
|
539 n->dsp=0;
|
|
540 stmode=REGISTER;
|
|
541 adecl(n);
|
|
542 stmode=0;
|
|
543 n->sc=EMPTY;
|
|
544 type=list3(CODE,type,cadr(n->ty));
|
|
545 return n;
|
|
546 } else {
|
|
547 n->sc=FUNCTION;
|
|
548 n->dsp=0;
|
|
549 adecl(n);
|
|
550 n->sc=EMPTY;
|
|
551 }
|
|
552 }
|
|
553 type=list3(FUNCTION,type,cadr(n->ty));
|
|
554 } else
|
|
555 return n;
|
|
556 }
|
|
557 }
|
|
558
|
|
559
|
|
560 void
|
|
561 adecl(NMTBL *n)
|
|
562 {
|
|
563 NMTBL *arg,*sfnptr;
|
|
564 int sreg_var,t;
|
16
|
565 int stype,smode;
|
0
|
566
|
|
567 stype=type;
|
|
568 sfnptr=fnptr;
|
|
569 fnptr=n;
|
|
570 sreg_var=reg_var;
|
|
571 reg_var=0;
|
|
572 n->dsp=0;
|
16
|
573 smode = mode;
|
|
574 /* if(mode!=GDECL && mode!=ADECL)
|
|
575 error(DCERR); */
|
0
|
576 mode=ADECL;
|
|
577 args= 0;
|
|
578 for(;;) {
|
|
579 if(sym==IDENT && nptr->sc!=TYPE) {
|
16
|
580 rplacad(n->ty,glist2(INT,cadr(n->ty)));
|
0
|
581 if (stmode==REGISTER && reg_var < MAX_REGISTER_VAR) {
|
|
582 nptr->ty = INT;
|
|
583 nptr->sc = REGISTER;
|
|
584 if ((nptr->dsp = get_register_var())<0)
|
|
585 error(-1);
|
40
|
586 use_register_var(nptr->dsp); /* it has now value in it */
|
0
|
587 reg_var++;
|
|
588 } else {
|
|
589 nptr->ty = INT;
|
|
590 nptr->sc = LVAR;
|
|
591 nptr->dsp = args ;
|
3
|
592 args += size_of_int;
|
0
|
593 }
|
|
594 getsym();
|
|
595 if(sym==RPAR) break;
|
|
596 } else {
|
|
597 if(sym==DOTS) {
|
7
|
598 rplacad(n->ty,glist2(INT,cadr(n->ty)));
|
0
|
599 getsym();
|
|
600 break;
|
|
601 }
|
|
602 if((t=typespec())==0) {
|
|
603 error(DCERR);
|
|
604 break;
|
|
605 }
|
|
606 if(sym!=COMMA && sym!=RPAR) {
|
|
607 if(sym==RPAR) break;
|
|
608 type=t;
|
|
609 arg=decl0();
|
|
610 reverse(t);
|
|
611 if (arg != &null_nptr) { /* no varname typespec only */
|
|
612 def(arg);
|
|
613 }
|
|
614 }
|
16
|
615 rplacad(n->ty,glist2(t,cadr(n->ty)));
|
0
|
616 if(sym==RPAR) break;
|
|
617 }
|
|
618 if (sym!=COMMA) error(DCERR);
|
|
619 getsym();
|
|
620 }
|
|
621 checksym(RPAR);
|
16
|
622 mode=smode;
|
0
|
623 reg_var=sreg_var;
|
|
624 fnptr=sfnptr;
|
|
625 type=stype;
|
|
626 return;
|
|
627 }
|
|
628
|
|
629 void
|
|
630 reverse(int t1)
|
|
631 {
|
|
632 int t2,t3;
|
|
633 t2=t1;
|
|
634
|
|
635 while(type!=t1) {
|
|
636 t3=cadr(type);
|
|
637 rplacad(type,t2);
|
|
638 t2=type;
|
|
639 type=t3;
|
|
640 }
|
|
641 type = t2;
|
|
642 }
|
|
643
|
|
644 int
|
|
645 reverse0(int t1)
|
|
646 {
|
|
647 int t2,t3;
|
|
648
|
|
649 t2=0;
|
|
650 while(t1) {
|
|
651 t3=cadr(t1);
|
|
652 rplacad(t1,t2);
|
|
653 t2=t1;
|
|
654 t1=t3;
|
|
655 }
|
|
656 return t2;
|
|
657 }
|
|
658
|
|
659 int
|
|
660 size(int t)
|
|
661 {
|
|
662 if(t==CHAR) return 1;
|
|
663 if(t==VOID) return 0;
|
3
|
664 if(scalar(t)) return size_of_int;
|
0
|
665 if(car(t)==STRUCT||car(t)==UNION) {
|
|
666 if(cadr(t)==-1) error(DCERR);
|
|
667 return(cadr(t));
|
|
668 }
|
|
669 if(car(t)==ARRAY)
|
|
670 return(size(cadr(t))*caddr(t));
|
|
671 else
|
|
672 error(DCERR);
|
|
673 return 0;
|
|
674 }
|
|
675
|
37
|
676 NMTBL *
|
0
|
677 def(NMTBL *n)
|
|
678 {
|
18
|
679 int sz,nsc,ndsp,t;
|
0
|
680
|
37
|
681 if (n==0) {
|
|
682 n=free_nptr();
|
|
683 n->nm = "_";
|
|
684 }
|
0
|
685 nsc=ndsp=0;
|
|
686 if(car(type)==FUNCTION) {
|
|
687 fcheck(n);
|
37
|
688 return n;
|
0
|
689 }
|
|
690 if (n->sc!=EMPTY &&
|
|
691 !(n->sc==GVAR&&n->dsp==EXTRN) &&
|
|
692 !(n->sc==FUNCTION&&n->dsp==EXTRN) &&
|
|
693 (mode!=ADECL || n->sc!=LVAR || n->ty!=INT) &&
|
|
694 (mode!=ADECL || n->sc!=REGISTER || n->ty!=INT) &&
|
|
695 ((mode!=GSDECL&&mode!=LSDECL) || n->sc!=FIELD || n->dsp!=disp) &&
|
|
696 ((mode!=GUDECL&&mode!=LUDECL) || n->sc!=FIELD || n->dsp!=0) )
|
|
697 error(DCERR);
|
|
698 sz = size(n->ty = type);
|
|
699 switch(mode) {
|
|
700 case GDECL:
|
|
701 gen_gdecl(n->nm,gpc);
|
|
702 case STADECL:
|
|
703 nsc = GVAR;
|
|
704 ndsp = gpc;
|
|
705 if (stmode==EXTRN)
|
|
706 n->dsp = EXTRN;
|
|
707 else
|
|
708 n->dsp = ndsp; /* emit_data will override this */
|
|
709 n->sc = nsc;
|
|
710 if (stmode==LDECL) {
|
|
711 copy(n,n->nm);
|
|
712 cheapp[-1] = '.';
|
|
713 ndsp = ++stat_no;
|
|
714 while(ndsp>0) {
|
|
715 *cheapp++ = ndsp%10+'0';
|
|
716 ndsp /= 10;
|
|
717 }
|
|
718 *cheapp++ = 0;
|
|
719 }
|
|
720 if(sym==ASS) {
|
13
|
721 decl_data(type,n,0);
|
0
|
722 emit_data_closing(n);
|
|
723 /* gpc is incremented by emit_data */
|
|
724 } else
|
|
725 gpc +=sz;
|
37
|
726 return n;
|
0
|
727 case GSDECL:
|
|
728 nsc = FIELD;
|
|
729 ndsp = disp;
|
|
730 disp += sz;
|
|
731 break;
|
|
732 case GUDECL:
|
|
733 nsc = FIELD;
|
|
734 ndsp = 0;
|
|
735 if (disp < sz) disp = sz;
|
|
736 break;
|
|
737 case GTDECL:
|
|
738 nsc = TYPE;
|
|
739 break;
|
|
740 case ADECL:
|
|
741 if (stmode==REGISTER && reg_var <MAX_REGISTER_VAR) {
|
|
742 if (type!=CHAR && !scalar(type))
|
|
743 error(TYERR);
|
39
|
744 n->sc = REGISTER;
|
0
|
745 reg_var++;
|
39
|
746 if (n->dsp==0) {
|
|
747 if ((n->dsp = get_register_var())<0) {
|
2
|
748 error(-1);
|
|
749 }
|
40
|
750 use_register_var(nptr->dsp); /* it has now value in it */
|
2
|
751 }
|
37
|
752 return n;
|
0
|
753 }
|
39
|
754 n->sc = LVAR;
|
0
|
755 if(type==CHAR) {
|
39
|
756 /* n->ty=INT; */
|
|
757 if (n->dsp==0) {
|
|
758 n->dsp = args;
|
0
|
759 if (endian)
|
3
|
760 n->dsp += size_of_int-1;
|
0
|
761 }
|
3
|
762 args += size_of_int;
|
0
|
763 } else {
|
39
|
764 if (n->dsp==0)
|
|
765 n->dsp = args;
|
0
|
766 args += sz;
|
|
767 }
|
|
768 if(type==VOID) {
|
|
769 } else if (!scalar(type)) {
|
|
770 if((t=car(type))==STRUCT || t==UNION) {
|
39
|
771 n->ty = type;
|
0
|
772 } else
|
|
773 error(TYERR);
|
|
774 }
|
37
|
775 return n;
|
|
776 case STAT: /* of course this is wrong */
|
0
|
777 case LDECL:
|
|
778 if (stmode==REGISTER && reg_var <=MAX_REGISTER_VAR) {
|
|
779 if(!scalar(type)) /* non integer register type ... */
|
|
780 error(DCERR);
|
|
781 nsc = REGISTER;
|
|
782 reg_var++;
|
2
|
783 if ((ndsp = get_register_var())<0)
|
|
784 error(-1);
|
0
|
785 } else {
|
|
786 nsc = LVAR;
|
|
787 ndsp = (disp -= sz);
|
|
788 }
|
14
|
789 n->sc = nsc;
|
|
790 n->dsp = ndsp;
|
0
|
791 if(sym==ASS) {
|
13
|
792 decl_data(type,n,0);
|
0
|
793 }
|
37
|
794 return n;
|
0
|
795 case LSDECL:
|
|
796 nsc = FIELD;
|
|
797 ndsp = disp;
|
|
798 disp += sz;
|
|
799 break;
|
|
800 case LUDECL:
|
|
801 nsc = FIELD;
|
|
802 ndsp = 0;
|
|
803 if (disp < sz) disp = sz;
|
|
804 break;
|
|
805 case LTDECL:
|
|
806 nsc = TYPE;
|
|
807 break;
|
|
808 default:
|
|
809 error(DCERR);
|
|
810 }
|
|
811 n->sc = nsc;
|
|
812 if (stmode==EXTRN)
|
|
813 n->dsp = EXTRN;
|
|
814 else
|
|
815 n->dsp = ndsp;
|
37
|
816 return n;
|
0
|
817 }
|
|
818
|
|
819 void
|
|
820 emit_init_vars(void)
|
|
821 {
|
13
|
822 if (!init_vars) return;
|
|
823 init_vars = reverse0(init_vars);
|
0
|
824 while(init_vars) {
|
|
825 g_expr(car(init_vars));
|
|
826 init_vars = cadr(init_vars);
|
|
827 }
|
|
828 }
|
|
829
|
13
|
830 int
|
|
831 assign_data(int e, int t, NMTBL *n,int offset)
|
0
|
832 {
|
|
833 int ass;
|
|
834
|
|
835 if(mode==GDECL) {
|
13
|
836 emit_data(e,t,n);
|
|
837 return offset+size(t);
|
0
|
838 } else if(mode==LDECL) {
|
13
|
839 if(t==CHAR) {
|
32
|
840 ass =list3(CASS,list2(LVAR,n->dsp+offset),rvalue(e));
|
13
|
841 } else if (scalar(t)) {
|
32
|
842 ass = list3(ASS,list2(LVAR,n->dsp+offset),rvalue(e));
|
13
|
843 } else if (car(t)==STRUCT || car(t)==UNION || car(t)==STRING) {
|
32
|
844 ass = list4(SASS,list2(LVAR,n->dsp+offset),rvalue(e),size(t));
|
0
|
845 } else {
|
13
|
846 error(DCERR);
|
0
|
847 }
|
|
848 init_vars = list2(ass,init_vars);
|
13
|
849 return offset+size(t);
|
0
|
850 } else {
|
|
851 error(DCERR);
|
|
852 }
|
18
|
853 return 0; /* not reached */
|
0
|
854 }
|
|
855
|
13
|
856 int
|
|
857 decl_data(int t, NMTBL *n,int offset)
|
0
|
858 {
|
32
|
859 int t1,e,i,mode_save;
|
0
|
860
|
32
|
861 mode_save = mode;
|
|
862 mode=STAT;
|
0
|
863 getsym();
|
|
864 if (scalar(t)) {
|
|
865 e=expr1();
|
32
|
866 mode = mode_save;
|
0
|
867 if(car(e)!=CONST && t==CHAR)
|
|
868 error(TYERR);
|
13
|
869 offset = assign_data(e,t,n,offset);
|
0
|
870 type=t;
|
13
|
871 return offset;
|
0
|
872 }
|
|
873 t1 = car(t);
|
|
874 if (t1==ARRAY) {
|
|
875 if (sym==LC) {
|
32
|
876 mode = mode_save;
|
0
|
877 t1 = cadr(t);
|
|
878 for(i=0;;i++) {
|
|
879 if (sym!=RC)
|
32
|
880 offset=decl_data(t1,n,offset); /* array of some thing */
|
0
|
881 if (sym==COMMA) { continue;
|
|
882 } else if (sym==RC) {
|
13
|
883 if (caddr(t)==0) { /* size not defined */
|
|
884 heap[t+2]=i+1; /* define array size */
|
|
885 } else if (caddr(t)!=i+1) { /* size match? */
|
0
|
886 error(TYERR);
|
|
887 }
|
|
888 getsym();
|
13
|
889 return offset;
|
0
|
890 } else {
|
|
891 error(TYERR);
|
|
892 }
|
|
893 }
|
|
894 } else if (cadr(t)==CHAR) {
|
|
895 e=expr1();
|
32
|
896 mode = mode_save;
|
0
|
897 if(car(e)!=STRING)
|
|
898 error(TYERR);
|
13
|
899 offset=assign_data(e,list3(ARRAY,CHAR,size(type)),n,offset);
|
|
900 if (caddr(t)==0) { /* size not defined */
|
|
901 heap[t+2]=size(type); /* define array size */
|
|
902 } else if (caddr(t)!=size(type)) { /* size match? */
|
|
903 error(TYERR);
|
|
904 }
|
0
|
905 } else
|
|
906 error(DCERR);
|
|
907 } else if (t1==STRUCT) {
|
32
|
908 mode = mode_save;
|
0
|
909 if(cadr(t)==-1) error(DCERR);
|
|
910 t1 = caddr(t); /* list of fields */
|
|
911 while(t1) {
|
13
|
912 offset = decl_data(car(t1),n,offset); /* alignment? */
|
0
|
913 t1 = cadr(t1);
|
|
914 if ( t1 && sym==COMMA) continue;
|
|
915 if (!t1 && sym!=RC) error(DCERR);
|
|
916 }
|
|
917 getsym();
|
18
|
918 return offset;
|
0
|
919 } else {
|
32
|
920 mode = mode_save;
|
0
|
921 error(TYERR); /* should be initialization error */
|
|
922 }
|
18
|
923 return offset; /* not reached */
|
0
|
924 }
|
|
925
|
|
926 int
|
|
927 sdecl(int s)
|
|
928 {
|
18
|
929 int smode,sdisp,type0;
|
0
|
930 NMTBL *nptr0,*gnptr0;
|
|
931 int tags;
|
|
932
|
|
933 smode=mode;
|
|
934 if (mode==GDECL || mode==GSDECL || mode==GUDECL || mode==GTDECL)
|
|
935 mode=(s==STRUCT?GSDECL:GUDECL);
|
|
936 else
|
|
937 mode=(s==STRUCT?LSDECL:LUDECL);
|
|
938 sdisp=disp;
|
|
939 disp=0;
|
|
940 if (getsym() == IDENT) {
|
|
941 nptr0 = nptr;
|
|
942 gnptr0 = gnptr;
|
|
943 if (getsym() == LC) {
|
|
944 if (nptr0->sc != EMPTY) error(DCERR);
|
|
945 nptr0->sc = TAG;
|
|
946 tags = 0;
|
|
947 nptr0->ty = list3(s,-1,tags);
|
|
948 while (getsym() != RC) {
|
|
949 decl();
|
|
950 tags = list2(type,tags);
|
|
951 }
|
|
952 getsym();
|
|
953 tags=reverse0(tags);
|
|
954 heap[nptr0->ty+2]=tags;
|
|
955 rplacad(type0 = nptr0->ty,disp);
|
|
956 } else {
|
13
|
957 /* struct tag name */
|
0
|
958 if(nptr0->sc == EMPTY) nptr0=gnptr0;
|
|
959 if(nptr0->sc == EMPTY) error(UDERR);
|
|
960 if(nptr0->sc != TAG) error(TYERR);
|
13
|
961 tags = caddr(nptr0->ty);
|
|
962 disp = cadr(nptr0->ty);
|
0
|
963 }
|
13
|
964 type0 = list3(s,disp,tags);
|
0
|
965 } else if(sym==LC) {
|
|
966 tags = 0;
|
|
967 while(getsym() != RC) {
|
|
968 decl();
|
|
969 tags = list2(type,tags);
|
|
970 }
|
|
971 getsym();
|
|
972 tags=reverse0(tags);
|
|
973 type0 = list3(s,disp,tags);
|
|
974 }
|
|
975 else error(DCERR);
|
|
976 disp=sdisp;
|
|
977 mode=smode;
|
|
978 return type0;
|
|
979 }
|
|
980
|
|
981 void
|
|
982 code_decl(NMTBL *n)
|
|
983 {
|
3
|
984 int odisp;
|
|
985
|
0
|
986 if (n->sc==EMPTY) n->sc = CODE;
|
|
987 code_enter(n->nm);
|
|
988 fnptr=n;
|
3
|
989 disp = -args;
|
|
990 args = 0;
|
0
|
991 reg_var=0;
|
|
992 mode=ADECL;
|
|
993 stmode=REGISTER;
|
|
994 while (sym!=LC) { /* argument declaration !ANSI */
|
|
995 decl(); getsym();
|
|
996 }
|
3
|
997 if (args) disp = -args;
|
|
998 else args = -disp;
|
0
|
999 init_vars=0;
|
|
1000 /* local variable declaration */
|
|
1001 stmode=0;
|
|
1002 mode=STAT;
|
|
1003 init_vars=0;
|
3
|
1004 odisp=disp;
|
0
|
1005 while (typeid(getsym()) || sym==STATIC || sym==EXTRN || sym==TYPEDEF) {
|
|
1006 mode=LDECL;
|
|
1007 decl();
|
|
1008 mode=STAT;
|
|
1009 }
|
|
1010 control=1;
|
3
|
1011 code_enter1(disp-odisp,args);
|
0
|
1012 emit_init_vars();
|
|
1013 while(sym!=RC) statement();
|
|
1014 if(control)
|
|
1015 error(STERR);
|
|
1016 control=0;
|
36
|
1017 code_leave(n->nm,disp-odisp);
|
0
|
1018 }
|
|
1019
|
|
1020 void
|
|
1021 fdecl(NMTBL *n)
|
|
1022 {
|
37
|
1023 NMTBL str_ret;
|
|
1024 int t;
|
|
1025
|
0
|
1026 enter(n->nm);
|
|
1027 fnptr=n;
|
|
1028 retlabel=fwdlabel();
|
|
1029 retcont = 0;
|
|
1030
|
|
1031 args=0;
|
|
1032 reg_var=0;
|
|
1033 fcheck(n);
|
|
1034 mode=ADECL;
|
|
1035 while (sym!=LC) { /* argument declaration !ANSI */
|
|
1036 stmode=0;
|
|
1037 decl(); getsym();
|
|
1038 }
|
39
|
1039 t=car(fnptr->ty);
|
|
1040 if (!scalar(t) && (car(t)==STRUCT||car(t)==UNION)) {
|
37
|
1041 /* this extra dummy arguments are set at calling sequence */
|
39
|
1042 str_ret.nm = "str_ret"; str_ret.sc = EMPTY;
|
|
1043 str_ret.dsp = 0; str_ret.ty = 0;
|
|
1044 type=list2(POINTER,t);
|
|
1045 if ((t=size(t))==-1) error(TYERR);
|
|
1046 else {
|
|
1047 def(&str_ret);
|
|
1048 struct_return = list3(list2(LVAR,str_ret.dsp),t,type);
|
|
1049 }
|
|
1050 /* type is no longer valid */
|
37
|
1051 } else {
|
|
1052 struct_return = 0;
|
|
1053 }
|
0
|
1054 disp=0;
|
|
1055 init_vars=0;
|
|
1056 /* local variable declaration */
|
|
1057 mode=STAT;
|
|
1058 while (typeid(getsym()) || sym==STATIC || sym==EXTRN
|
|
1059 || sym==REGISTER || sym==TYPEDEF) {
|
|
1060 mode=LDECL;
|
|
1061 stmode=0;
|
|
1062 decl();
|
|
1063 mode=STAT;
|
|
1064 }
|
|
1065 control=1;
|
|
1066 enter1(disp);
|
|
1067 emit_init_vars();
|
|
1068 while(sym!=RC) statement();
|
|
1069
|
36
|
1070 leave(control,n->nm,disp);
|
0
|
1071 retpending = 0;
|
|
1072 control=0;
|
|
1073 }
|
|
1074
|
|
1075 void
|
|
1076 fcheck(NMTBL *n)
|
|
1077 {
|
|
1078 if(mode!=GDECL||car(type)!=FUNCTION) error(DCERR);
|
|
1079 if(n->sc==FUNCTION) compatible(car(n->ty),cadr(type));
|
|
1080 else {
|
|
1081 if(n->sc!=EMPTY)
|
|
1082 error(DCERR);
|
|
1083 else {
|
|
1084 n->sc=FUNCTION;
|
39
|
1085 n->ty=glist2(cadr(type),0); /* arglist? */
|
0
|
1086 }
|
|
1087 }
|
|
1088 }
|
|
1089
|
|
1090 void
|
|
1091 compatible(int t1, int t2)
|
|
1092 {
|
|
1093 if(integral(t1)) {
|
|
1094 if(t1!=t2) error(TYERR);
|
|
1095 }
|
|
1096 else if(car(t1)!=car(t2))
|
|
1097 error(TYERR);
|
|
1098 else if((car(t1)==STRUCT || car(t1)==UNION) && cadr(t1)!=cadr(t2))
|
|
1099 error(TYERR);
|
|
1100 else if(car(t1)==POINTER || car(t1)==ARRAY ||car(t1)==FUNCTION)
|
|
1101 compatible(cadr(t1),cadr(t2));
|
|
1102 }
|
|
1103
|
|
1104 int
|
|
1105 scalar(int t)
|
|
1106 {
|
|
1107 return(integral(t)||car(t)==POINTER);
|
|
1108 }
|
|
1109
|
|
1110 int
|
|
1111 integral(int t)
|
|
1112 {
|
|
1113 return(t==INT||t==CHAR||t==UNSIGNED);
|
|
1114 }
|
|
1115
|
|
1116 void
|
|
1117 checkret(void)
|
|
1118 {
|
|
1119 if (retpending) {
|
|
1120 ret();
|
|
1121 control=0;
|
|
1122 retpending=0;
|
|
1123 }
|
|
1124 }
|
|
1125
|
|
1126 void
|
|
1127 statement(void)
|
|
1128 {
|
|
1129 int slfree;
|
|
1130
|
|
1131 if(sym==SM) {
|
|
1132 getsym(); return;
|
|
1133 }
|
|
1134 checkret();
|
|
1135 switch(sym) {
|
|
1136 case IF:
|
|
1137 doif();
|
|
1138 return;
|
|
1139 case WHILE:
|
|
1140 dowhile();
|
|
1141 return;
|
|
1142 case DO:
|
|
1143 dodo();
|
|
1144 return;
|
|
1145 case FOR:
|
|
1146 dofor();
|
|
1147 return;
|
|
1148 case SWITCH:
|
|
1149 doswitch();
|
|
1150 return;
|
|
1151 case LC:
|
|
1152 docomp();
|
|
1153 return;
|
|
1154 case BREAK:
|
|
1155 jmp(blabel);
|
|
1156 getsym();
|
|
1157 checksym(SM);
|
|
1158 return;
|
|
1159 case CONTINUE:
|
|
1160 jmp(clabel);
|
|
1161 getsym();
|
|
1162 checksym(SM);
|
|
1163 return;
|
|
1164 case CASE:
|
|
1165 docase();
|
|
1166 statement();
|
|
1167 return;
|
|
1168 case DEFAULT:
|
|
1169 dodefault();
|
|
1170 statement();
|
|
1171 return;
|
|
1172 case RETURN:
|
|
1173 doreturn();
|
|
1174 return;
|
|
1175 case GOTO:
|
|
1176 dogoto();
|
|
1177 return;
|
|
1178 default:
|
|
1179 if(sym==IDENT&&skipspc()==':') {
|
|
1180 dolabel();
|
|
1181 statement();
|
|
1182 } else {
|
|
1183 slfree=lfree;
|
|
1184 gexpr(expr());
|
|
1185 lfree=slfree;
|
|
1186 checksym(SM);
|
|
1187 }
|
|
1188 }
|
|
1189 }
|
|
1190
|
|
1191 void
|
|
1192 doif(void)
|
|
1193 {
|
|
1194 int l1,l2,slfree;
|
|
1195 getsym();
|
|
1196 checksym(LPAR);
|
|
1197 slfree=lfree;
|
|
1198 bexpr(expr(),0,l1=fwdlabel());
|
|
1199 lfree=slfree;
|
|
1200 checksym(RPAR);
|
|
1201 statement();
|
|
1202 checkret();
|
|
1203 if(sym==ELSE) {
|
|
1204 if ((l2 = control))
|
|
1205 jmp(l2=fwdlabel());
|
|
1206 fwddef(l1);
|
|
1207 getsym();
|
|
1208 statement();
|
|
1209 checkret();
|
|
1210 if (l2) fwddef(l2);
|
|
1211 }
|
|
1212 else fwddef(l1);
|
|
1213 }
|
|
1214
|
|
1215 void
|
|
1216 dowhile(void)
|
|
1217 {
|
|
1218 int sbreak,scontinue,slfree,e;
|
|
1219
|
|
1220 sbreak=blabel;
|
|
1221 scontinue=clabel;
|
|
1222 blabel=fwdlabel();
|
|
1223 clabel=backdef();
|
|
1224 getsym();
|
|
1225 checksym(LPAR);
|
|
1226 slfree=lfree;
|
|
1227 e=expr();
|
|
1228 checksym(RPAR);
|
|
1229 if(sym==SM) {
|
|
1230 bexpr(e,1,clabel);
|
|
1231 lfree=slfree;
|
|
1232 getsym();
|
|
1233 } else {
|
|
1234 bexpr(e,0,blabel);
|
|
1235 lfree=slfree;
|
|
1236 statement();
|
|
1237 checkret();
|
|
1238 if(control)
|
|
1239 jmp(clabel);
|
|
1240 }
|
|
1241 fwddef(blabel);
|
|
1242 clabel=scontinue;
|
|
1243 blabel=sbreak;
|
|
1244 }
|
|
1245
|
|
1246 void
|
|
1247 dodo(void)
|
|
1248 {
|
|
1249 int sbreak,scontinue,l,slfree;
|
|
1250
|
|
1251 sbreak=blabel;
|
|
1252 scontinue=clabel;
|
|
1253 blabel=fwdlabel();
|
|
1254 clabel=fwdlabel();
|
|
1255 l=backdef();
|
|
1256 getsym();
|
|
1257 statement();
|
|
1258 checkret();
|
|
1259 fwddef(clabel);
|
|
1260 checksym(WHILE);
|
|
1261 checksym(LPAR);
|
|
1262 slfree=lfree;
|
|
1263 bexpr(expr(),1,l);
|
|
1264 lfree=slfree;
|
|
1265 checksym(RPAR);
|
|
1266 checksym(SM);
|
|
1267 fwddef(blabel);
|
|
1268 clabel=scontinue;
|
|
1269 blabel=sbreak;
|
|
1270 }
|
|
1271
|
|
1272 void
|
|
1273 dofor(void)
|
|
1274 {
|
|
1275 int sbreak,scontinue,l,e,slfree;
|
|
1276
|
|
1277 sbreak=blabel;
|
|
1278 scontinue=clabel;
|
|
1279 blabel=fwdlabel();
|
|
1280 getsym();
|
|
1281 checksym(LPAR);
|
|
1282 slfree=lfree;
|
|
1283 if(sym!=SM) {
|
|
1284 gexpr(expr());
|
|
1285 checksym(SM);
|
|
1286 }
|
|
1287 else getsym();
|
|
1288 lfree=slfree;
|
|
1289 l=backdef();
|
|
1290 if(sym!=SM) {
|
|
1291 bexpr(expr(),0,blabel);
|
|
1292 checksym(SM);
|
|
1293 }
|
|
1294 else getsym();
|
|
1295 lfree=slfree;
|
|
1296 if(sym==RPAR) {
|
|
1297 clabel=l;
|
|
1298 getsym();
|
|
1299 statement();
|
|
1300 checkret();
|
|
1301 } else {
|
|
1302 clabel=fwdlabel();
|
|
1303 e=expr();
|
|
1304 checksym(RPAR);
|
|
1305 statement();
|
|
1306 checkret();
|
|
1307 fwddef(clabel);
|
|
1308 gexpr(e);
|
|
1309 lfree=slfree;
|
|
1310 }
|
|
1311 jmp(l);
|
|
1312 fwddef(blabel);
|
|
1313 clabel=scontinue;
|
|
1314 blabel=sbreak;
|
|
1315 }
|
|
1316
|
|
1317 void
|
|
1318 doswitch(void)
|
|
1319 {
|
|
1320 int sbreak,scase,sdefault,slfree,svalue;
|
|
1321
|
|
1322 sbreak=blabel; /* save parents break label */
|
|
1323 blabel=fwdlabel();
|
|
1324 sdefault=dlabel; /* save parents default label */
|
|
1325 dlabel=0;
|
|
1326 scase=cslabel; /* save parents next case label */
|
|
1327 getsym();
|
|
1328 checksym(LPAR);
|
|
1329 slfree=lfree;
|
|
1330 svalue=csvalue1; /* save parents switch value */
|
|
1331 gexpr(expr());
|
|
1332 csvalue1=csvalue ;
|
|
1333 lfree=slfree;
|
|
1334 checksym(RPAR);
|
|
1335 cslabel = control = 0;
|
|
1336 /* should be case statement but... */
|
|
1337 statement();
|
|
1338 checkret();
|
|
1339 if(dlabel) def_label(cslabel,dlabel);
|
|
1340 else fwddef(cslabel);
|
|
1341 csvalue1=svalue;
|
|
1342 cslabel=scase;
|
|
1343 dlabel=sdefault;
|
|
1344 fwddef(blabel);
|
|
1345 blabel=sbreak;
|
|
1346 }
|
|
1347
|
|
1348 void
|
|
1349 docomp(void)
|
|
1350 {
|
|
1351 getsym();
|
|
1352 while(sym!=RC) { statement(); checkret();}
|
|
1353 getsym();
|
|
1354 }
|
|
1355
|
|
1356 void
|
|
1357 docase(void)
|
|
1358 {
|
|
1359 int c,l,slfree;
|
|
1360
|
|
1361 c=0;
|
|
1362 slfree=lfree;
|
|
1363 while(sym==CASE) {
|
|
1364 getsym();
|
|
1365 c=list2(cexpr(expr()),c);
|
|
1366 checksym(COLON);
|
|
1367 }
|
|
1368 l=fwdlabel();
|
|
1369 if (control) {
|
|
1370 control=0;
|
|
1371 jmp(l);
|
|
1372 }
|
|
1373 if (cslabel) fwddef(cslabel);
|
|
1374 while(cadr(c)) {
|
|
1375 cmpdimm(car(c),csvalue1);
|
|
1376 jcond(l,0);
|
|
1377 c=cadr(c);
|
|
1378 }
|
|
1379 lfree=slfree;
|
|
1380 cmpdimm(car(c),csvalue1);
|
|
1381 jcond(cslabel=fwdlabel(),1);
|
|
1382 fwddef(l);
|
|
1383 }
|
|
1384
|
|
1385 void
|
|
1386 dodefault(void)
|
|
1387 {
|
|
1388 getsym();
|
|
1389 checksym(COLON);
|
|
1390 if (dlabel) error(STERR);
|
|
1391 if (!cslabel) jmp(cslabel = fwdlabel());
|
|
1392 dlabel = backdef();
|
|
1393 }
|
|
1394
|
|
1395 void
|
|
1396 doreturn(void)
|
|
1397 {
|
39
|
1398 int slfree,e,e1;
|
0
|
1399
|
|
1400 if(getsym()==SM) {
|
|
1401 getsym();
|
|
1402 retpending = 1;
|
|
1403 return;
|
|
1404 }
|
|
1405 slfree=lfree;
|
37
|
1406 if (struct_return) {
|
|
1407 e = expr();
|
|
1408 if ((car(type)==STRUCT || car(type)==UNION)&&
|
|
1409 size(type)==cadr(struct_return)) {
|
39
|
1410 e = rvalue(e);
|
|
1411 type = caddr(struct_return);
|
|
1412 e1 = rvalue(cadr(struct_return));
|
|
1413 gexpr(list4(SASS,rvalue(car(struct_return)),e,e1));
|
37
|
1414 } else {
|
39
|
1415 error(TYERR); /* should check compatible */
|
37
|
1416 }
|
|
1417 } else {
|
|
1418 gexpr(expr());
|
|
1419 }
|
0
|
1420 lfree=slfree;
|
|
1421 checksym(SM);
|
39
|
1422 /* control = 0; still control continue until pending return emittion */
|
0
|
1423 retpending = 1;
|
|
1424 }
|
|
1425
|
39
|
1426 void
|
|
1427 replace_return_struct(int func,int left) {
|
|
1428 int e = caddr(func); /* arg lists */
|
|
1429 e = car(e); /* return_struct arg */
|
|
1430 rplacad(e,left);
|
|
1431 }
|
0
|
1432
|
|
1433 void
|
|
1434 dogoto(void)
|
|
1435 {
|
|
1436 NMTBL *nptr0;
|
|
1437 int t,e1,e2,env;
|
|
1438
|
|
1439 getsym();
|
|
1440 e1 = expr();
|
|
1441 t=car(e1);
|
|
1442 if (t==FNAME) {
|
|
1443 nptr0 = (NMTBL *)cadr(e1);
|
|
1444 t = nptr0->sc;
|
|
1445 if (t==EMPTY) {
|
|
1446 nptr0->sc = FLABEL;
|
|
1447 jmp(nptr0->dsp = fwdlabel());
|
|
1448 } else if (t==FLABEL||t==BLABEL) {
|
|
1449 jmp(nptr0->dsp);
|
|
1450 }
|
|
1451 control=0;
|
|
1452 checksym(SM);
|
|
1453 return;
|
|
1454 }
|
|
1455 if (t==COMMA) {
|
|
1456 env = caddr(e1);
|
|
1457 e1 = cadr(e1);
|
|
1458 t = car(e1);
|
|
1459 } else {
|
|
1460 env = 0;
|
|
1461 }
|
|
1462 if (t==FUNCTION) {
|
|
1463 e2 = cadr(e1);
|
|
1464 if (car(e2) == FNAME) {
|
|
1465 nptr0=(NMTBL *)cadr(e2);
|
|
1466 nptr0->sc = CODE;
|
|
1467 }
|
|
1468 gexpr(list3(CODE,e1,env));
|
|
1469 control=0;
|
|
1470 checksym(SM);
|
|
1471 return;
|
|
1472 }
|
|
1473 error(STERR);
|
|
1474 return;
|
|
1475 }
|
|
1476
|
|
1477 void
|
|
1478 dolabel(void)
|
|
1479 {
|
|
1480 if(nptr->sc == FLABEL)
|
|
1481 fwddef(nptr->dsp);
|
|
1482 else if(nptr->sc != EMPTY)
|
|
1483 error(TYERR);
|
|
1484 nptr->sc = BLABEL;
|
|
1485 nptr->dsp = backdef();
|
|
1486 getsym();
|
|
1487 checksym(COLON);
|
|
1488 }
|
|
1489
|
|
1490 int
|
|
1491 expr(void)
|
|
1492 {
|
|
1493 return(rvalue(expr0()));
|
|
1494 }
|
|
1495
|
|
1496 int
|
|
1497 expr0(void)
|
|
1498 {
|
|
1499 int e;
|
|
1500
|
|
1501 e=expr1();
|
|
1502 while(sym==COMMA) {
|
|
1503 getsym();e=list3(COMMA,e,rvalue(expr1()));
|
|
1504 }
|
|
1505 return e;
|
|
1506 }
|
|
1507
|
|
1508 int
|
|
1509 expr1(void)
|
|
1510 {
|
|
1511 int e1,e2,t,op;
|
|
1512 e1=expr2();
|
|
1513 switch (sym) {
|
|
1514 case ASS:
|
|
1515 lcheck(e1);
|
|
1516 t=type;
|
|
1517 getsym();
|
|
1518 e2=rvalue(expr1());
|
|
1519 if(t==VOID)
|
|
1520 error(TYERR);
|
|
1521 if(t==CHAR) {
|
|
1522 type= INT;return(list3(CASS,e1,e2));
|
14
|
1523 } else if(!scalar(t)&&(car(t)==STRUCT||car(t)==UNION)) {
|
39
|
1524 type= t;
|
|
1525 if(car(e2)==RSTRUCT && car(cadr(e2))==FUNCTION) {
|
|
1526 replace_return_struct(cadr(e2),e1);
|
|
1527 return cadr(e2);
|
|
1528 } else {
|
|
1529 return(list4(SASS,e1,e2,size(t)));
|
|
1530 }
|
0
|
1531 }
|
|
1532 type=t;
|
|
1533 return(list3(ASS,e1,e2));
|
|
1534 case ADD+AS: case SUB+AS: case MUL+AS: case DIV+AS: case MOD+AS:
|
|
1535 case RSHIFT+AS: case LSHIFT+AS: case BAND+AS: case EOR+AS: case BOR+AS:
|
|
1536 op = sym-AS;
|
|
1537 lcheck(e1);
|
|
1538 t=type;
|
|
1539 getsym();
|
|
1540 e2=rvalue(expr1());
|
|
1541 if(!integral(type)) error(TYERR);
|
|
1542 if((t==UNSIGNED||type==UNSIGNED)&&
|
|
1543 (op==MUL||op==DIV||op==MOD||op==RSHIFT||op==LSHIFT))
|
|
1544 op=op+US;
|
|
1545 if(t==CHAR) {
|
|
1546 type= INT;
|
|
1547 return(list4(CASSOP,e1,e2,op));
|
|
1548 }
|
|
1549 type=t;
|
|
1550 if(integral(t)) return(list4(ASSOP,e1,e2,op));
|
|
1551 if((op!=ADD&&op!=SUB)||car(t)!=POINTER) error(TYERR);
|
|
1552 e2=binop(MUL,e2,list2(CONST,size(cadr(t))),INT,UNSIGNED);
|
|
1553 type=t;
|
|
1554 return list4(ASSOP,e1,e2,op);
|
|
1555 default:
|
|
1556 return(e1);
|
|
1557 }
|
|
1558 }
|
|
1559
|
|
1560 int
|
|
1561 expr2(void)
|
|
1562 {
|
|
1563 int e1,e2,e3,t;
|
|
1564
|
|
1565 e1=expr3();
|
|
1566 if(sym==COND) {
|
|
1567 e1=rvalue(e1);
|
|
1568 getsym();
|
|
1569 e2=rvalue(expr2());
|
|
1570 t=type;
|
|
1571 checksym(COLON);
|
|
1572 e3=rvalue(expr2());
|
|
1573 if(car(e1)==CONST) {
|
|
1574 if(cadr(e1)) {
|
|
1575 type=t;return e2;
|
|
1576 } else
|
|
1577 return e3;
|
|
1578 }
|
|
1579 if(type==INT||(t!=INT&&type==UNSIGNED))
|
|
1580 type=t;
|
|
1581 return(list4(COND,e1,e2,e3));
|
|
1582 }
|
|
1583 return(e1);
|
|
1584 }
|
|
1585
|
|
1586 int
|
|
1587 expr3(void)
|
|
1588 {
|
|
1589 int e;
|
|
1590
|
|
1591 e=expr4();
|
|
1592 while(sym==LOR) {
|
|
1593 e=rvalue(e);
|
|
1594 getsym();
|
|
1595 e=list3(LOR,e,rvalue(expr4()));
|
|
1596 type= INT;
|
|
1597 }
|
|
1598 return(e);
|
|
1599 }
|
|
1600
|
|
1601 int
|
|
1602 expr4(void)
|
|
1603 {
|
|
1604 int e;
|
|
1605
|
|
1606 e=expr5();
|
|
1607 while(sym==LAND) {
|
|
1608 e=rvalue(e);
|
|
1609 getsym();
|
|
1610 e=list3(LAND,e,rvalue(expr5()));
|
|
1611 type= INT;
|
|
1612 }
|
|
1613 return(e);
|
|
1614 }
|
|
1615
|
|
1616 int
|
|
1617 expr5(void)
|
|
1618 {
|
|
1619 int e1,e2,t;
|
|
1620
|
|
1621 e1=expr6();
|
|
1622 while(sym==BOR) {
|
|
1623 e1=rvalue(e1);
|
|
1624 t=type;
|
|
1625 getsym();
|
|
1626 e2=rvalue(expr6());
|
|
1627 e1=binop(BOR,e1,e2,t,type);
|
|
1628 }
|
|
1629 return(e1);
|
|
1630 }
|
|
1631
|
|
1632 int
|
|
1633 expr6(void)
|
|
1634 {
|
|
1635 int e1,e2,t;
|
|
1636
|
|
1637 e1=expr7();
|
|
1638 while(sym==EOR) {
|
|
1639 e1=rvalue(e1);
|
|
1640 t=type;
|
|
1641 getsym();
|
|
1642 e2=rvalue(expr7());
|
|
1643 e1=binop(EOR,e1,e2,t,type);
|
|
1644 }
|
|
1645 return(e1);
|
|
1646 }
|
|
1647
|
|
1648 int
|
|
1649 expr7(void)
|
|
1650 {
|
|
1651 int e1,e2,t;
|
|
1652
|
|
1653 e1=expr8();
|
|
1654 while(sym==BAND) {
|
|
1655 e1=rvalue(e1);
|
|
1656 t=type;
|
|
1657 getsym();
|
|
1658 e2=rvalue(expr8());
|
|
1659 e1=binop(BAND,e1,e2,t,type);
|
|
1660 }
|
|
1661 return(e1);
|
|
1662 }
|
|
1663
|
|
1664 int
|
|
1665 expr8(void)
|
|
1666 {
|
|
1667 int e,op;
|
|
1668
|
|
1669 e=expr9();
|
|
1670 while((op=sym)==EQ||op==NEQ) {
|
|
1671 e=rvalue(e);
|
|
1672 getsym();
|
|
1673 e=list3(op,e,rvalue(expr9()));
|
|
1674 type= INT;
|
|
1675 }
|
|
1676 return e;
|
|
1677 }
|
|
1678
|
|
1679 int
|
|
1680 expr9(void)
|
|
1681 {
|
|
1682 int e1,e2,t,op;
|
|
1683
|
|
1684 e1=expr10();
|
|
1685 while((op=sym)==GT||op==GE||op==LT||op==LE) {
|
|
1686 e1=rvalue(e1);
|
|
1687 t=type;
|
|
1688 getsym();
|
|
1689 e2=rvalue(expr10());
|
|
1690 if(t==INT&&type==INT)
|
|
1691 e1=list3(op,e1,e2);
|
|
1692 else
|
|
1693 e1=list3(op+US,e1,e2);
|
|
1694 type= INT;
|
|
1695 }
|
|
1696 return e1;
|
|
1697 }
|
|
1698
|
|
1699 int
|
|
1700 expr10(void)
|
|
1701 {
|
|
1702 int e1,e2,t,op;
|
|
1703
|
|
1704 e1=expr11();
|
|
1705 while((op=sym)==RSHIFT||op==LSHIFT) {
|
|
1706 e1=rvalue(e1);
|
|
1707 t=type;
|
|
1708 getsym();
|
|
1709 e2=rvalue(expr11());
|
|
1710 e1=binop(op,e1,e2,t,type);
|
|
1711 }
|
|
1712 return e1;
|
|
1713 }
|
|
1714
|
|
1715 int
|
|
1716 expr11(void)
|
|
1717 {
|
|
1718 int e1,e2,t,op;
|
|
1719
|
|
1720 e1=expr12();
|
|
1721 while((op=sym)==ADD||op==SUB) {
|
|
1722 e1=rvalue(e1);
|
|
1723 t=type;
|
|
1724 getsym();
|
|
1725 e2=rvalue(expr12());
|
|
1726 e1=binop(op,e1,e2,t,type);
|
|
1727 }
|
|
1728 return e1;
|
|
1729 }
|
|
1730
|
|
1731 int
|
|
1732 expr12(void)
|
|
1733 {
|
|
1734 int e1,e2,t,op;
|
|
1735
|
|
1736 e1=expr13();
|
|
1737 while((op=sym)==MUL||op==DIV||op==MOD) {
|
|
1738 e1=rvalue(e1);
|
|
1739 t=type;
|
|
1740 getsym();
|
|
1741 e2=rvalue(expr13());
|
|
1742 e1=binop(op,e1,e2,t,type);
|
|
1743 }
|
|
1744 return e1;
|
|
1745 }
|
|
1746
|
|
1747 int
|
|
1748 expr13(void)
|
|
1749 {
|
|
1750 int e,op;
|
|
1751
|
|
1752 switch (op = sym) {
|
|
1753 case INC: case DEC:
|
|
1754 getsym();
|
|
1755 lcheck(e=expr13());
|
|
1756 if(type==CHAR) {
|
|
1757 type= INT;
|
|
1758 return(list2(op==INC?CPREINC:CPREDEC,e));
|
|
1759 }
|
|
1760 if(integral(type))
|
|
1761 return(list3(PREINC,e,op==INC?1:-1));
|
|
1762 if(car(type)!=POINTER)
|
|
1763 error(TYERR);
|
|
1764 return(list3(PREINC,e,
|
|
1765 op==INC?size(cadr(type)):-size(cadr(type)) ));
|
|
1766 case MUL:
|
|
1767 getsym();
|
|
1768 e=rvalue(expr13());
|
|
1769 return(indop(e));
|
|
1770 case BAND:
|
|
1771 getsym();
|
|
1772 switch(car(e=expr13())) {
|
|
1773 case INDIRECT:
|
|
1774 e=cadr(e);
|
|
1775 break;
|
|
1776 case GVAR:
|
|
1777 case LVAR:
|
|
1778 e=list2(ADDRESS,e);
|
|
1779 break;
|
|
1780 case FNAME:
|
|
1781 return e;
|
|
1782 default:error(LVERR);
|
|
1783 }
|
|
1784 type=list2(POINTER,type);
|
|
1785 return e;
|
|
1786 case SUB:
|
|
1787 getsym();
|
|
1788 e=rvalue(expr13());
|
|
1789 if(!integral(type))
|
|
1790 error(TYERR);
|
|
1791 return(car(e)==CONST?list2(CONST,-cadr(e)):list2(MINUS,e));
|
|
1792 case BNOT:
|
|
1793 getsym();
|
|
1794 e=rvalue(expr13());
|
|
1795 if(!integral(type))
|
|
1796 error(TYERR);
|
|
1797 return(car(e)==CONST?list2(CONST,~cadr(e)):list2(BNOT,e));
|
|
1798 case LNOT:
|
|
1799 getsym();
|
19
|
1800 e=rvalue(expr13());
|
20
|
1801 if(!scalar(type))
|
19
|
1802 error(TYERR);
|
|
1803 return(car(e)==CONST?list2(CONST,!cadr(e)):list2(LNOT,e));
|
0
|
1804 case SIZEOF:
|
|
1805 if(getsym()==LPAR) {
|
|
1806 if(typeid(getsym())) {
|
|
1807 e=list2(CONST,size(typename()));
|
|
1808 type=INT;
|
|
1809 checksym(RPAR);
|
|
1810 return e;
|
|
1811 } else {
|
|
1812 e=expr0();
|
|
1813 checksym(RPAR);
|
|
1814 expr16(e);
|
|
1815 if(sym==INC||sym==DEC) {
|
|
1816 getsym();
|
|
1817 if(type==CHAR) type=INT;
|
|
1818 else if(!scalar(type))
|
|
1819 error(TYERR);
|
|
1820 }
|
|
1821 }
|
|
1822 } else
|
|
1823 expr13();
|
|
1824 e=list2(CONST,size(type));
|
|
1825 type=INT;
|
|
1826 return e;
|
|
1827 }
|
|
1828 e=expr14();
|
|
1829 if((op=sym)==INC||op==DEC) {
|
|
1830 lcheck(e);
|
|
1831 getsym();
|
|
1832 if(type==CHAR) {
|
|
1833 type= INT;
|
|
1834 return(list2(op==INC?CPOSTINC:CPOSTDEC,e));
|
|
1835 }
|
|
1836 if(integral(type))
|
|
1837 return(list3(POSTINC,e,op==INC?1:-1));
|
|
1838 if(car(type)!=POINTER)
|
|
1839 error(TYERR);
|
|
1840 return (list3(POSTINC,e,
|
|
1841 op == INC ? size(cadr(type)): -size(cadr(type)) ));
|
|
1842 }
|
|
1843 return e;
|
|
1844 }
|
|
1845
|
|
1846 int
|
|
1847 expr14(void)
|
|
1848 {
|
|
1849 int e1,t;
|
|
1850
|
|
1851 switch(sym) {
|
|
1852 case IDENT:
|
|
1853 switch(nptr->sc) {
|
|
1854 case GVAR:
|
|
1855 e1=list3(GVAR,nptr->dsp,(int)nptr->nm);
|
|
1856 type=nptr->ty;
|
|
1857 getsym();
|
|
1858 break;
|
|
1859 case LVAR:
|
|
1860 e1=list2(LVAR,nptr->dsp);
|
|
1861 type=nptr->ty;
|
|
1862 getsym();
|
|
1863 break;
|
|
1864 case REGISTER:
|
|
1865 e1=list2(REGISTER,nptr->dsp);
|
|
1866 type=nptr->ty;
|
|
1867 getsym();
|
|
1868 break;
|
|
1869 case FLABEL: case BLABEL:
|
|
1870 case FUNCTION: case CODE:
|
|
1871 e1=list2(FNAME,(int)nptr);
|
|
1872 type=list3(nptr->sc,car(nptr->ty),cadr(nptr->ty));
|
|
1873 getsym();
|
|
1874 break;
|
|
1875 case EMPTY:
|
|
1876 if(getsym()==LPAR) {
|
|
1877 nptr->sc = FUNCTION;
|
7
|
1878 nptr->ty= glist2(INT,0);
|
0
|
1879 type= list3(FUNCTION,INT,0);
|
|
1880 e1=expr15(list2(FNAME,(int)nptr));
|
|
1881 break;
|
|
1882 } else {
|
|
1883 e1=list2(FNAME,(int)nptr);
|
|
1884 type=list3(nptr->sc,nptr->ty,0);
|
|
1885 break;
|
|
1886 }
|
|
1887 default:error(UDERR);
|
|
1888 }
|
|
1889 break;
|
|
1890 case STRING:
|
|
1891 e1=list3(STRING,(int)sptr,symval);
|
|
1892 type=list3(ARRAY,CHAR,symval);
|
|
1893 getsym();
|
|
1894 break;
|
|
1895 case CONST:
|
|
1896 type= INT;
|
|
1897 e1=list2(CONST,symval);
|
|
1898 getsym();
|
|
1899 break;
|
|
1900 case RETURN:
|
|
1901 if (fnptr->sc != FUNCTION) {
|
|
1902 error(STERR);
|
|
1903 }
|
|
1904 type=list2(POINTER,CODE);
|
|
1905 e1=list2(RETURN,(int)fnptr);
|
|
1906 getsym();
|
|
1907 break;
|
18
|
1908 case DEFINED:
|
|
1909 getsym();
|
|
1910 t = mode; mode = IFDEF;
|
|
1911 checksym(LPAR);
|
|
1912 mode = t;
|
|
1913 type= INT;
|
|
1914 e1=list2(CONST,symval);
|
|
1915 getsym();
|
|
1916 checksym(RPAR);
|
|
1917 break;
|
0
|
1918 case ENVIRONMENT:
|
2
|
1919 type=list2(POINTER,VOID);
|
0
|
1920 e1=list2(ENVIRONMENT,0);
|
|
1921 getsym();
|
|
1922 break;
|
|
1923 case LPAR:
|
|
1924 if(typeid(getsym())) {
|
|
1925 t=typename();
|
|
1926 checksym(RPAR);
|
|
1927 e1=expr13();
|
|
1928 type=t;
|
|
1929 return e1;
|
|
1930 }
|
|
1931 e1=expr0();
|
|
1932 checksym(RPAR);
|
|
1933 break;
|
|
1934 default:error(EXERR);
|
|
1935 }
|
|
1936 return expr16(e1);
|
|
1937 }
|
|
1938
|
|
1939 int
|
|
1940 expr16(int e1)
|
|
1941 {
|
|
1942 int e2,t;
|
|
1943
|
|
1944 while(1) {
|
|
1945 if(sym==LBRA) {
|
|
1946 e1=rvalue(e1);
|
|
1947 t=type;
|
|
1948 getsym();
|
|
1949 e2=rvalue(expr0());
|
|
1950 checksym(RBRA);
|
|
1951 e1=binop(ADD,e1,e2,t,type);
|
|
1952 e1=indop(e1);
|
|
1953 } else if(sym==LPAR) e1=expr15(e1);
|
|
1954 else if(sym==PERIOD) e1=strop(e1);
|
|
1955 else if(sym==ARROW) e1=strop(indop(rvalue(e1)));
|
|
1956 else break;
|
|
1957 }
|
|
1958 if(car(e1)==FNAME) type=list2(POINTER,type);
|
|
1959 return e1;
|
|
1960 }
|
|
1961
|
|
1962 int
|
|
1963 rvalue(int e)
|
|
1964 {
|
|
1965 int t;
|
|
1966 if(type==CHAR) {
|
|
1967 type= INT;
|
|
1968 switch(car(e)) {
|
|
1969 case GVAR:
|
|
1970 return(list3(CRGVAR,cadr(e),caddr(e)));
|
|
1971 case LVAR:
|
|
1972 return(list2(CRLVAR,cadr(e)));
|
|
1973 case INDIRECT:
|
|
1974 return(list2(CRINDIRECT,cadr(e)));
|
|
1975 default:return(e);
|
|
1976 }
|
|
1977 }
|
|
1978 if(!integral(type)&&type!=VOID) {
|
|
1979 if(type==CODE) { return(e);
|
|
1980 } else if((t=car(type))==ARRAY) {
|
|
1981 type=list2(POINTER,cadr(type));
|
|
1982 if(car(e)==INDIRECT) return cadr(e);
|
|
1983 return list2(ADDRESS,e);
|
37
|
1984 } else if(t==STRUCT || t==UNION) {
|
|
1985 t = cadr(type); /* size */
|
38
|
1986 return list3(RSTRUCT,e,t);
|
0
|
1987 } else if(t!=POINTER) error(TYERR);
|
|
1988 }
|
|
1989 switch(car(e)) {
|
|
1990 case GVAR:
|
|
1991 return(list3(RGVAR,cadr(e),caddr(e)));
|
|
1992 case LVAR:
|
|
1993 return(list2(RLVAR,cadr(e)));
|
|
1994 case INDIRECT:
|
|
1995 return(list2(RINDIRECT,cadr(e)));
|
|
1996 default:return(e);
|
|
1997 }
|
|
1998 }
|
|
1999
|
|
2000 void
|
|
2001 lcheck(int e)
|
|
2002 {
|
|
2003 int t;
|
|
2004 if(!scalar(type)||
|
|
2005 (car(e)!=GVAR&&car(e)!=LVAR&&car(e)!=INDIRECT&&car(e)!=REGISTER))
|
|
2006 if ((t=car(type))<0 && t!=STRUCT && t!=UNION)
|
|
2007 error(LVERR);
|
|
2008 }
|
|
2009
|
|
2010 int
|
|
2011 indop(int e)
|
|
2012 {
|
|
2013 if(type!=INT&&type!=UNSIGNED) {
|
|
2014 if(car(type)==POINTER)
|
|
2015 type=cadr(type);
|
|
2016 else error(TYERR);
|
|
2017 } else
|
|
2018 type= CHAR;
|
|
2019 if(car(e)==ADDRESS)
|
|
2020 return(cadr(e));
|
|
2021 return(list2(INDIRECT,e));
|
|
2022 }
|
|
2023
|
|
2024 int
|
|
2025 strop(int e)
|
|
2026 {
|
|
2027 getsym();
|
|
2028 if (sym!=IDENT||nptr->sc!=FIELD) error(TYERR);
|
|
2029 if (integral(type)||(car(type)!=STRUCT && car(type)!=UNION))
|
|
2030 e=rvalue(e);
|
|
2031 type = nptr->ty;
|
40
|
2032 if(nptr->dsp) {
|
|
2033 switch(car(e)) {
|
|
2034 case GVAR:
|
|
2035 e=list2(INDIRECT,list3(ADD,e,list2(CONST,nptr->dsp)));
|
|
2036 break;
|
|
2037 case LVAR:
|
|
2038 e=list2(car(e),cadr(e) + nptr->dsp);
|
|
2039 break;
|
|
2040 case INDIRECT:
|
|
2041 e=list2(INDIRECT,list3(ADD,cadr(e),list2(CONST,nptr->dsp)));
|
|
2042 break;
|
|
2043 default:
|
|
2044 e=list2(INDIRECT,list3(ADD,e,list2(CONST,nptr->dsp)));
|
|
2045 }
|
|
2046 } else {
|
|
2047 switch(car(e)) {
|
|
2048 case GVAR: case LVAR: case INDIRECT:
|
|
2049 break;
|
|
2050 default:
|
|
2051 e=list2(INDIRECT,e);
|
|
2052 }
|
0
|
2053 }
|
|
2054 getsym();
|
|
2055 return e;
|
|
2056 }
|
|
2057
|
|
2058 int
|
|
2059 binop(int op, int e1, int e2, int t1, int t2)
|
|
2060 {
|
|
2061 int e;
|
|
2062
|
|
2063 if(car(e1)==CONST&&car(e2)==CONST) {
|
|
2064 e1=cadr(e1);
|
|
2065 e2=cadr(e2);
|
|
2066 type= INT;
|
|
2067 switch(op) {
|
|
2068 case BOR:
|
|
2069 e=e1|e2;break;
|
|
2070 case EOR:
|
|
2071 e=e1^e2;break;
|
|
2072 case BAND:
|
|
2073 e=e1&e2;break;
|
|
2074 case ADD:
|
|
2075 if(integral(t1)) {
|
|
2076 if(integral(t2)) {
|
|
2077 e=e1+e2;
|
|
2078 } else {
|
|
2079 if(car(t2)!=POINTER) error(TYERR);
|
|
2080 e=size(cadr(t2))*e1+e2;
|
|
2081 type=t2;
|
|
2082 }
|
|
2083 } else {
|
|
2084 if(car(t1)!=POINTER) error(TYERR);
|
|
2085 e=e1+size(cadr(t1))*e2;
|
|
2086 type=t1;
|
|
2087 }
|
|
2088 break;
|
|
2089 case SUB:
|
|
2090 if(integral(t1)) {
|
|
2091 e=e1-e2;
|
|
2092 } else {
|
|
2093 if(car(t1)!=POINTER) error(TYERR);
|
|
2094 e=e1-size(cadr(t1))*e2;
|
|
2095 type=t1;
|
|
2096 }
|
|
2097 break;
|
|
2098 case MUL:
|
|
2099 e=e1*e2;break;
|
|
2100 case DIV:
|
|
2101 if(!e2) error(EXERR);e=e1/e2;break;
|
|
2102 case MOD:
|
|
2103 if(!e2) error(EXERR);e=e1%e2;break;
|
|
2104 case RSHIFT:
|
|
2105 e=e1>>e2;break;
|
|
2106 case LSHIFT:
|
|
2107 e=e1<<e2;
|
|
2108 }
|
|
2109 return list2(CONST,e);
|
|
2110 }
|
|
2111 if((op==ADD||op==MUL||op==BOR||op==EOR||op==BAND)&&
|
|
2112 (car(e1)==CONST||(car(e2)!=CONST&&
|
|
2113 (car(e1)==RGVAR||car(e1)==RLVAR)))) {
|
|
2114 e=e1;e1=e2;e2=e;e=t1;t1=t2;t2=e;
|
|
2115 }
|
|
2116 if(op==ADD) {
|
|
2117 if(integral(t1)) {
|
|
2118 if(integral(t2)) {
|
|
2119 if(t1==INT) type=t2;else type=t1;
|
|
2120 return(list3(ADD,e1,e2));
|
|
2121 }
|
|
2122 if(car(t2)!=POINTER) error(TYERR);
|
|
2123 e=binop(MUL,e1,list2(CONST,size(cadr(t2))),t1,INT);
|
|
2124 type=t2;
|
|
2125 return(list3(ADD,e,e2));
|
|
2126 }
|
|
2127 if(car(t1)!=POINTER||!integral(t2)) error(TYERR);
|
|
2128 e=binop(MUL,e2,list2(CONST,size(cadr(t1))),t2,INT);
|
|
2129 type=t1;
|
40
|
2130 if (car(e)==CONST && cadr(e)==0)
|
|
2131 return(e1);
|
0
|
2132 if(car(e1)==ADDRESS&&car(e)==CONST&&car(cadr(e1))!=GVAR)
|
|
2133 return(list2(ADDRESS,list2(car(cadr(e1)),
|
|
2134 cadr(cadr(e1))+cadr(e))));
|
|
2135 return(list3(ADD,e1,e));
|
|
2136 }
|
|
2137 if(op==SUB) {
|
|
2138 if(integral(t1)) {
|
|
2139 if(!integral(t2)) error(TYERR);
|
|
2140 if(t1==INT) type=t2;else type=t1;
|
|
2141 return(list3(SUB,e1,e2));
|
|
2142 }
|
|
2143 if(car(t1)!=POINTER) error(TYERR);
|
|
2144 if(integral(t2)) {
|
|
2145 e=binop(MUL,e2,list2(CONST,size(cadr(t1))),t2,INT);
|
|
2146 type=t1;
|
|
2147 return(list3(SUB,e1,e));
|
|
2148 }
|
|
2149 if(car(t2)!=POINTER)
|
|
2150 error(TYERR);
|
|
2151 compatible(t1,t2);
|
|
2152 e=list3(SUB,e1,e2);
|
|
2153 e=binop(DIV,e,list2(CONST,size(cadr(t1))),UNSIGNED,INT);
|
|
2154 type= INT;
|
|
2155 return e;
|
|
2156 }
|
|
2157 if(!integral(t1)||!integral(t2)) error(TYERR);
|
|
2158 if(t1==INT) type=t2;else type=t1;
|
|
2159 if((op==MUL||op==DIV)&&car(e2)==CONST&&cadr(e2)==1) return e1;
|
|
2160 if(op==BOR||op==EOR||op==BAND) return(list3(op,e1,e2));
|
|
2161 return(list3(type==UNSIGNED?op+US:op,e1,e2));
|
|
2162 }
|
|
2163
|
|
2164 int
|
|
2165 expr15(int e1)
|
|
2166 {
|
3
|
2167 int t,arglist,e;
|
0
|
2168
|
39
|
2169 /* function call */
|
|
2170
|
0
|
2171 t=type;
|
|
2172 if(integral(t)|| (car(t)!=FUNCTION && car(t)!=CODE))
|
|
2173 error(TYERR);
|
|
2174 getsym();
|
|
2175 arglist=0;
|
|
2176 while(sym!=RPAR) {
|
3
|
2177 e=rvalue(expr1());
|
|
2178 arglist=list3(e,arglist,type);
|
0
|
2179 if(sym!=COMMA) break;
|
|
2180 getsym();
|
|
2181 }
|
|
2182 checksym(RPAR);
|
39
|
2183 if(car(t)==CODE)
|
|
2184 return list3(FUNCTION,e1,arglist);
|
|
2185 type=cadr(t);
|
|
2186 if(type==CHAR) type=INT;
|
|
2187 else if(car(type)==STRUCT||car(type)==UNION) {
|
|
2188 /* make temporaly struct for return value */
|
|
2189 e = list2(LVAR,def(0)->dsp);
|
|
2190 /* pass the pointer as an argument */
|
|
2191 /* this is recognized by called function declaration */
|
|
2192 arglist=list3(list2(ADDRESS,e),arglist,list2(POINTER,type));
|
|
2193 /* return list3(COMMA,list3(FUNCTION,e1,arglist),rvalue(e)); */
|
0
|
2194 }
|
|
2195 return list3(FUNCTION,e1,arglist);
|
|
2196 }
|
|
2197
|
|
2198 int
|
|
2199 typeid(int s)
|
|
2200 {
|
37
|
2201 return (integral(s) || s==CODE || s==SHORT ||
|
|
2202 s==LONG || s==STRUCT || s==UNION ||
|
0
|
2203 (s==IDENT && nptr->sc==TYPE));
|
|
2204 }
|
|
2205
|
|
2206 int
|
|
2207 typename(void)
|
|
2208 {
|
|
2209 int t;
|
|
2210
|
|
2211 type=t=typespec();
|
|
2212 ndecl0();
|
|
2213 reverse(t);
|
|
2214 return type;
|
|
2215 }
|
|
2216
|
|
2217 int
|
|
2218 ndecl0(void)
|
|
2219 {
|
|
2220 if(sym==MUL) {
|
|
2221 getsym();
|
|
2222 return type=list2(POINTER,ndecl0());
|
|
2223 }
|
|
2224 return ndecl1();
|
|
2225 }
|
|
2226
|
|
2227 int
|
|
2228 ndecl1(void)
|
|
2229 {
|
|
2230 int i,t,arglist;
|
|
2231
|
|
2232 if(sym==LPAR) {
|
|
2233 if(getsym()==RPAR) {
|
|
2234 type=list3(FUNCTION,type,0); getsym();
|
|
2235 } else {
|
|
2236 ndecl0();
|
|
2237 checksym(RPAR);
|
|
2238 }
|
|
2239 }
|
|
2240 while(1) {
|
|
2241 if(sym==LBRA) {
|
|
2242 getsym();
|
|
2243 t=type;
|
|
2244 i=cexpr(expr());
|
|
2245 checksym(RBRA);
|
|
2246 type=list3(ARRAY,t,i);
|
|
2247 } else if(sym==LPAR) {
|
|
2248 t = type;
|
|
2249 getsym();
|
|
2250 arglist=0;
|
|
2251 while(sym!=RPAR) {
|
|
2252 ndecl0();
|
|
2253 arglist=list2(type,arglist);
|
|
2254 if(sym!=COMMA) break;
|
|
2255 getsym();
|
|
2256 }
|
|
2257 checksym(RPAR);
|
|
2258 type=list3(FUNCTION,t,arglist);
|
|
2259 }
|
|
2260 else return type;
|
|
2261 }
|
|
2262 }
|
|
2263
|
|
2264 int
|
|
2265 cexpr(int e)
|
|
2266 {
|
|
2267 if (car(e) != CONST) error(CNERR);
|
|
2268 return (cadr(e));
|
|
2269 }
|
|
2270
|
20
|
2271 int in_comment = 0;
|
|
2272
|
0
|
2273 int
|
|
2274 getsym(void)
|
|
2275 {
|
29
|
2276 NMTBL *nptr0,*nptr1,*nptrm;
|
34
|
2277 int i,slfree,macrop;
|
35
|
2278 char c;
|
0
|
2279
|
|
2280 if (alpha(skipspc())) {
|
|
2281 i = hash = 0;
|
|
2282 name = namebuf;
|
|
2283 while (alpha(ch) || digit(ch)) {
|
|
2284 if (i < LBUFSIZE-1)
|
|
2285 hash=(7*hash ^ (name[i++]=ch));
|
|
2286 getch();
|
|
2287 }
|
|
2288 name[i++] = '\0';
|
29
|
2289
|
|
2290 nptrm=msearch(name);
|
|
2291 if (mode==MDECL) {
|
|
2292 nptr = nptrm;
|
|
2293 return (sym==MACRO);
|
|
2294 }
|
|
2295 if (mode==IFDEF) {
|
|
2296 if (nptrm->sc == MACRO||nptrm->sc==FMACRO) {
|
18
|
2297 return (symval=1);
|
29
|
2298 } else {
|
|
2299 return (symval=0);
|
18
|
2300 }
|
29
|
2301 }
|
34
|
2302 if (nptrm->sc!=EMPTY) {
|
|
2303 i = mode;
|
|
2304 mode = STAT;
|
|
2305 macrop = 0;
|
|
2306 slfree = lfree;
|
|
2307 macropp = macro_buf;
|
29
|
2308 if (nptrm->sc == FMACRO) {
|
34
|
2309 macrop=macro_function(macrop,&chptr,nptrm,0);
|
|
2310 } else {
|
|
2311 macrop=macro_eval(macrop,(char *)car(nptrm->dsp),0);
|
29
|
2312 }
|
34
|
2313 macropp = macro_buf;
|
|
2314 append(reverse0(macrop));
|
35
|
2315 macropp[-1] ='\n';
|
|
2316 *macropp =0;
|
34
|
2317 lfree = slfree;
|
35
|
2318 if (lsrc && !asmf ) gen_comment(macro_buf);
|
|
2319 /* fprintf(stderr,"#macro: %s => %s\n",nptrm->nm,macro_buf); */
|
34
|
2320 chptrsave = chptr;
|
|
2321 chsave = ch = chptr[-1];
|
|
2322 chptr = macro_buf;
|
|
2323 ch = *chptr++;
|
|
2324 mode = i;
|
|
2325 return getsym();
|
0
|
2326 }
|
29
|
2327
|
|
2328 nptr0 = gsearch();
|
18
|
2329 if (nptr0->sc == RESERVE) return sym = nptr0->dsp;
|
0
|
2330 sym = IDENT;
|
|
2331 gnptr=nptr=nptr0;
|
|
2332 if (mode==ADECL && nptr0->sc ==TYPE) return sym;
|
|
2333 if (mode==GDECL || mode==GSDECL || mode==GUDECL ||
|
|
2334 mode==GTDECL || mode==TOP) {
|
|
2335 return sym;
|
|
2336 }
|
|
2337 nptr1=lsearch(nptr0->nm);
|
18
|
2338 if (mode==STAT) {
|
0
|
2339 if (nptr1->sc == EMPTY) return sym;
|
18
|
2340 }
|
0
|
2341 nptr=nptr1;
|
|
2342 return sym;
|
|
2343 } else if (digit(ch)) {
|
|
2344 symval=0;
|
|
2345 if (ch == '0') {
|
|
2346 if (getch() == 'x' || ch == 'X') {
|
|
2347 while(1) {
|
|
2348 if(digit(getch()))
|
|
2349 symval=symval*16+ch-'0';
|
|
2350 else if('a'<=ch&&ch<='f')
|
|
2351 symval=symval*16+ch-'a'+10;
|
|
2352 else if('A'<=ch&&ch<='F')
|
|
2353 symval=symval*16+ch-'A'+10;
|
|
2354 else break;
|
|
2355 }
|
|
2356 } else {
|
|
2357 while (digit(ch)) {
|
|
2358 symval=symval*8+ch-'0';getch();
|
|
2359 }
|
|
2360 }
|
|
2361 } else {
|
|
2362 while(digit(ch)) {
|
|
2363 symval=symval*10+ch-'0';getch();
|
|
2364 }
|
|
2365 }
|
|
2366 return sym=CONST;
|
|
2367 } else if(ch=='\'') {
|
|
2368 getch();
|
|
2369 symval=escape();
|
|
2370 if(ch!='\'') error(CHERR);
|
|
2371 getch();
|
|
2372 return sym=CONST;
|
|
2373 } else if(ch=='"') {
|
|
2374 getstring();
|
|
2375 return sym= STRING;
|
|
2376 }
|
|
2377 c=ch;
|
|
2378 getch();
|
|
2379 switch(c) {
|
|
2380 case '*':
|
|
2381 return postequ(MUL,MUL+AS);
|
|
2382 case '&':
|
|
2383 if(ch=='&') {getch();return sym=LAND;}
|
|
2384 return postequ(BAND,BAND+AS);
|
|
2385 case '-':
|
|
2386 if(ch=='>') {getch();return sym=ARROW;}
|
|
2387 if(ch=='-') {getch();return sym=DEC;}
|
|
2388 return postequ(SUB,SUB+AS);
|
|
2389 case '!':
|
|
2390 return postequ(LNOT,NEQ);
|
|
2391 case '~':
|
|
2392 return sym=BNOT;
|
|
2393 case '+':
|
|
2394 if(ch=='+') {getch();return sym=INC;}
|
|
2395 return postequ(ADD,ADD+AS);
|
|
2396 case '%':
|
|
2397 return postequ(MOD,MOD+AS);
|
|
2398 case '^':
|
|
2399 return postequ(EOR,EOR+AS);
|
|
2400 case '|':
|
|
2401 if(ch=='|') {getch();return sym=LOR;}
|
|
2402 return postequ(BOR,BOR+AS);
|
|
2403 case '=':
|
|
2404 return postequ(ASS,EQ);
|
|
2405 case '>':
|
|
2406 if(ch=='>') {getch();return postequ(RSHIFT,RSHIFT+AS);}
|
|
2407 return postequ(GT,GE);
|
|
2408 case '<':
|
|
2409 if(ch=='<') {getch();return postequ(LSHIFT,LSHIFT+AS);}
|
|
2410 return postequ(LT,LE);
|
|
2411 case '(':
|
|
2412 return sym=LPAR;
|
|
2413 case ')':
|
|
2414 return sym=RPAR;
|
|
2415 case '[':
|
|
2416 return sym=LBRA;
|
|
2417 case ']':
|
|
2418 return sym=RBRA;
|
|
2419 case '{':
|
|
2420 return sym=LC;
|
|
2421 case '}':
|
|
2422 return sym=RC;
|
|
2423 case ',':
|
|
2424 return sym=COMMA;
|
|
2425 case ';':
|
|
2426 return sym=SM;
|
|
2427 case ':':
|
|
2428 return sym=COLON;
|
|
2429 case '?':
|
|
2430 return sym=COND;
|
|
2431 case '.':
|
|
2432 if(ch=='.') {
|
|
2433 getch();
|
|
2434 if (ch=='.') {
|
|
2435 getch();
|
|
2436 return sym=DOTS;
|
|
2437 }
|
|
2438 error(CHERR);
|
|
2439 return getsym();
|
|
2440 } else
|
|
2441 return sym=PERIOD;
|
|
2442 case '/':
|
|
2443 if(ch!='*') return postequ(DIV,DIV+AS);
|
34
|
2444 if(ch=='/') {
|
|
2445 while(ch!='\n') getch();
|
|
2446 getch();
|
|
2447 return getsym();
|
|
2448 }
|
20
|
2449 in_comment = 1;
|
0
|
2450 getch();
|
|
2451 while(ch=='*'?getch()!='/':getch());
|
20
|
2452 in_comment = 0;
|
0
|
2453 getch();
|
|
2454 return getsym();
|
34
|
2455 case 0:
|
|
2456 case '\n':
|
|
2457 getch();
|
|
2458 return getsym();
|
0
|
2459 default:
|
|
2460 error(CHERR);
|
|
2461 return getsym();
|
|
2462 }
|
|
2463 }
|
|
2464
|
|
2465 int
|
|
2466 postequ(int s1, int s2)
|
|
2467 {
|
|
2468 if(ch=='=') {getch();return sym=s2;}
|
|
2469 return sym=s1;
|
|
2470 }
|
|
2471
|
|
2472 int
|
|
2473 alpha(char c)
|
|
2474 {
|
|
2475 return(('a'<=c&&c<='z')||('A'<=c&&c<='Z')||c=='_');
|
|
2476 }
|
|
2477
|
|
2478 int
|
|
2479 digit(char c)
|
|
2480 {
|
|
2481 return('0'<=c&&c<='9');
|
|
2482 }
|
|
2483
|
37
|
2484 int dummy_count = 0;
|
|
2485
|
|
2486 NMTBL *
|
|
2487 free_nptr()
|
|
2488 {
|
|
2489 NMTBL *nptr,*iptr;
|
|
2490
|
|
2491 iptr=nptr= &ntable[hash % GSYMS];
|
|
2492 while(nptr->sc!=0) {
|
|
2493 if (++nptr== &ntable[GSYMS])
|
|
2494 nptr=ntable;
|
|
2495 if (nptr==iptr) error(GSERR);
|
|
2496 }
|
|
2497 copy(nptr,"_00000");
|
|
2498 dummy_count++;
|
|
2499 if (dummy_count>999) error(STRERR);
|
|
2500 nptr->nm[5]='0'+dummy_count%10;
|
|
2501 nptr->nm[4]='0'+(dummy_count/10)%10;
|
|
2502 nptr->nm[3]='0'+(dummy_count/100)%10;
|
|
2503 nptr->sc=EMPTY;
|
|
2504 return nptr;
|
|
2505 }
|
0
|
2506
|
|
2507 NMTBL *
|
|
2508 gsearch(void)
|
|
2509 {
|
|
2510 NMTBL *nptr,*iptr;
|
|
2511
|
|
2512 iptr=nptr= &ntable[hash % GSYMS];
|
30
|
2513 while(nptr->sc!=0 && neqname(nptr->nm,name)) {
|
0
|
2514 if (++nptr== &ntable[GSYMS])
|
|
2515 nptr=ntable;
|
|
2516 if (nptr==iptr) error(GSERR);
|
|
2517 }
|
|
2518 if (nptr->sc == 0) {
|
|
2519 copy(nptr,name);
|
|
2520 nptr->sc=EMPTY;
|
|
2521 }
|
|
2522 return nptr;
|
|
2523 }
|
|
2524
|
|
2525 NMTBL *
|
|
2526 lsearch(char *name)
|
|
2527 {
|
|
2528 NMTBL *nptr,*iptr;
|
|
2529
|
|
2530 iptr=nptr= &ntable[hash%LSYMS+GSYMS];
|
30
|
2531 while(nptr->sc!=0 && neqname(nptr->nm,name)) {
|
0
|
2532 if (++nptr== &ntable[LSYMS+GSYMS])
|
|
2533 nptr= &ntable[GSYMS];
|
|
2534 if (nptr==iptr) error(LSERR);
|
|
2535 }
|
|
2536 if (nptr->sc == 0) {
|
|
2537 nptr->nm=name; /* already saved in gsearch */
|
|
2538 nptr->sc=EMPTY;
|
|
2539 nptr->dsp=0;
|
|
2540 }
|
|
2541 return nptr;
|
|
2542 }
|
|
2543
|
29
|
2544 NMTBL *
|
|
2545 msearch(char *name)
|
|
2546 {
|
|
2547 NMTBL *nptr,*iptr;
|
|
2548
|
|
2549 iptr=nptr= &mtable[hash%MSYMS];
|
30
|
2550 while(nptr->sc!=0 && neqname(nptr->nm,name)) {
|
29
|
2551 if (++nptr== &mtable[MSYMS])
|
30
|
2552 nptr= &mtable[0];
|
29
|
2553 if (nptr==iptr) error(MSERR);
|
|
2554 }
|
|
2555 if (nptr->sc == 0) {
|
|
2556 copy(nptr,name);
|
|
2557 nptr->sc=EMPTY;
|
|
2558 nptr->dsp=0;
|
|
2559 nptr->ty=0;
|
|
2560 }
|
|
2561 return nptr;
|
|
2562 }
|
|
2563
|
30
|
2564 NMTBL *
|
|
2565 msearch0(char *name)
|
|
2566 {
|
|
2567 NMTBL *nptr,*iptr;
|
|
2568 int hash,i;
|
|
2569
|
31
|
2570 i = 0; hash = 0;
|
30
|
2571 while((name[i])) {
|
|
2572 hash=((7*hash) ^ name[i++]);
|
|
2573 }
|
|
2574 iptr=nptr= &mtable[hash%MSYMS];
|
|
2575 while(nptr->sc!=0 && neqname(nptr->nm,name)) {
|
|
2576 if (++nptr== &mtable[MSYMS])
|
|
2577 nptr= &mtable[0];
|
|
2578 if (nptr==iptr) error(MSERR);
|
|
2579 }
|
|
2580 if (nptr->sc == 0) {
|
|
2581 copy(nptr,name);
|
|
2582 nptr->sc=EMPTY;
|
|
2583 nptr->dsp=0;
|
|
2584 nptr->ty=0;
|
|
2585 }
|
|
2586 return nptr;
|
|
2587 }
|
|
2588
|
|
2589
|
0
|
2590 void
|
|
2591 copy(NMTBL *nptr, char *s)
|
|
2592 {
|
|
2593 nptr->nm = cheapp;
|
18
|
2594 while((*cheapp++ = *s++));
|
0
|
2595 }
|
|
2596
|
|
2597 int
|
30
|
2598 neqname(char *p,char *q)
|
0
|
2599 {
|
|
2600 if (!p)
|
|
2601 return 0;
|
|
2602 while(*p && *p!='.')
|
|
2603 if(*p++ != *q++) return 1;
|
|
2604 return (*q!=0);
|
|
2605 }
|
|
2606
|
|
2607 void
|
|
2608 getstring(void)
|
|
2609 {
|
|
2610 getch();
|
|
2611 symval = 0;
|
|
2612 sptr = cheapp;
|
|
2613 while (ch != '"') {
|
|
2614 *cheapp++ = escape();
|
|
2615 symval++;
|
|
2616 if (cheapp >= cheap+CHEAPSIZE) error(STRERR);
|
|
2617 }
|
|
2618 getch();
|
|
2619 *cheapp++ = '\0';
|
|
2620 symval++;
|
|
2621 }
|
|
2622
|
|
2623 int
|
|
2624 skipspc(void)
|
|
2625 {
|
|
2626 while(ch=='\t'||ch=='\n'||ch==' '||ch=='\r')
|
|
2627 getch();
|
|
2628 return ch;
|
|
2629 }
|
|
2630
|
|
2631 int
|
|
2632 getch(void)
|
|
2633 {
|
31
|
2634 if(*chptr) return ch = *chptr++;
|
34
|
2635 else if (chptrsave) {
|
|
2636 chptr = chptrsave;
|
|
2637 ch = chsave;
|
|
2638 chptrsave = 0;
|
|
2639 return ch;
|
0
|
2640 }
|
|
2641 getline();
|
|
2642 return getch();
|
|
2643 }
|
|
2644
|
|
2645 char
|
|
2646 escape(void)
|
|
2647 {
|
|
2648 char c;
|
|
2649 if ((c=ch) == '\\') {
|
|
2650 if (digit(c=getch())) {
|
|
2651 c = ch-'0';
|
|
2652 if (digit(getch())) {
|
|
2653 c = c*8+ch-'0';
|
|
2654 if (digit(getch())) {
|
|
2655 c=c*8+ch-'0';getch();
|
|
2656 }
|
|
2657 }
|
|
2658 return c;
|
|
2659 }
|
|
2660 getch();
|
|
2661 switch(c) {
|
|
2662 case 'n':
|
|
2663 return '\n';
|
|
2664 case 't':
|
|
2665 return '\t';
|
|
2666 case 'b':
|
|
2667 return '\b';
|
|
2668 case 'r':
|
|
2669 return '\r';
|
|
2670 case 'f':
|
|
2671 return '\f';
|
|
2672 case '\n':
|
|
2673 return escape();
|
|
2674 default:
|
|
2675 return c;
|
|
2676 }
|
|
2677 }
|
|
2678 if (c == '\n') error(EXERR);
|
|
2679 getch();
|
|
2680 return c;
|
|
2681 }
|
|
2682
|
|
2683 FILE *
|
|
2684 getfname(void)
|
|
2685 {
|
|
2686 int i;
|
25
|
2687 char *s,name[LBUFSIZE];
|
0
|
2688 FILE *fp;
|
|
2689
|
|
2690 getch();
|
|
2691 if(skipspc()!='"') error(INCERR);
|
|
2692 for(i=0;(getch()!='"' && ch!='\n');) {
|
|
2693 if(i<LBUFSIZE-1) name[i++]=ch;
|
|
2694 }
|
|
2695 if(ch=='\n') error(INCERR);
|
|
2696 name[i]=0;
|
|
2697 fp = fopen(name,"r") ;
|
25
|
2698 s = name;
|
|
2699 (filep+1)->name0 = cheapp;
|
|
2700 while((*cheapp++ = *s++));
|
0
|
2701 return ( (filep+1)->fcb = fp );
|
|
2702 }
|
|
2703
|
18
|
2704 static int macro_if_depth ;
|
|
2705 static int macro_if_current ;
|
|
2706 static int macro_if_skip ;
|
|
2707
|
0
|
2708 void
|
|
2709 getline(void)
|
|
2710 {
|
|
2711 int i;
|
|
2712 int c;
|
|
2713
|
18
|
2714 do {
|
|
2715 lineno++;
|
|
2716 glineno++;
|
|
2717 chptr=linebuf;
|
|
2718 i=0;
|
23
|
2719 while ((*chptr++ = c = getc(filep->fcb)) != '\n') {
|
18
|
2720 if (++i > LBUFSIZE-2) error(LNERR);
|
|
2721 if (c==EOF) {
|
|
2722 error(EOFERR);
|
|
2723 --chptr;
|
|
2724 }
|
|
2725 }
|
|
2726 *chptr = '\0';
|
23
|
2727 if (lsrc && !asmf && !macro_if_skip) gen_comment(linebuf);
|
20
|
2728 if (*(chptr = linebuf) == '#' && !in_comment) {
|
18
|
2729 macro_processing();
|
|
2730 }
|
23
|
2731 } while(macro_if_skip || linebuf[0] == '#');
|
18
|
2732 }
|
|
2733
|
|
2734 void
|
|
2735 macro_processing()
|
|
2736 {
|
|
2737 int i;
|
|
2738 int c;
|
|
2739 int mode_save;
|
|
2740
|
|
2741 ++chptr;
|
19
|
2742 if (macroeq("ifdef") || macroeq("ifndef")) {
|
28
|
2743 c = (chptr[-4]=='n');
|
18
|
2744 macro_if_current++;
|
|
2745 if (!macro_if_skip) {
|
19
|
2746 mode_save = mode; mode = IFDEF;
|
|
2747 ch= *chptr;
|
|
2748 i = getsym();
|
|
2749 mode = mode_save;
|
|
2750 macro_if_depth = macro_if_current;
|
|
2751 macro_if_skip = (!i)^c;
|
|
2752 }
|
|
2753 return;
|
|
2754 } else if (macroeq("if")) {
|
|
2755 macro_if_current++;
|
|
2756 if (!macro_if_skip) {
|
23
|
2757 for(c=0;chptr[c];c++);
|
33
|
2758 chptr[c] = ';'; /* this can't happen in macro expression */
|
19
|
2759 ch= *chptr;
|
18
|
2760 getsym();
|
|
2761 i=cexpr(expr());
|
|
2762 macro_if_depth = macro_if_current;
|
|
2763 macro_if_skip = !i;
|
|
2764 }
|
19
|
2765 return;
|
18
|
2766 } else if (macroeq("else")) {
|
|
2767 if (macro_if_current==0) {
|
|
2768 error(MCERR); /* extra #else */
|
19
|
2769 return;
|
18
|
2770 }
|
|
2771 if (macro_if_current == macro_if_depth)
|
|
2772 macro_if_skip = !macro_if_skip;
|
19
|
2773 return;
|
18
|
2774 } else if (macroeq("endif")) {
|
|
2775 if (macro_if_current == macro_if_depth) {
|
|
2776 macro_if_skip = 0;
|
19
|
2777 macro_if_depth = --macro_if_current;
|
18
|
2778 } else {
|
19
|
2779 if (macro_if_current<=0) {
|
18
|
2780 error(MCERR); /* extra #if */
|
19
|
2781 return;
|
18
|
2782 }
|
19
|
2783 macro_if_current--;
|
18
|
2784 }
|
19
|
2785 return;
|
|
2786 }
|
|
2787 if (macro_if_skip) return;
|
|
2788 if (macroeq("define")) {
|
32
|
2789 macro_define0();
|
18
|
2790 *(chptr = linebuf) = '\0';
|
19
|
2791 } else if (macroeq("undef")) {
|
|
2792 i=mode;
|
28
|
2793 mode=LDECL;
|
19
|
2794 ch= *chptr;
|
|
2795 if (getsym() == IDENT) {
|
|
2796 if (nptr->sc == MACRO) {
|
|
2797 nptr->sc = EMPTY;
|
29
|
2798 } else if (nptr->sc == FMACRO) {
|
28
|
2799 nptr->sc = EMPTY;
|
29
|
2800 /* we cannot reclaim it's arg */
|
19
|
2801 } else error(MCERR);
|
|
2802 }
|
|
2803 mode=i;
|
18
|
2804 } else if (macroeq("include")) {
|
|
2805 if(filep+1 >= filestack + FILES) error(FILERR);
|
|
2806 if ( ((filep+1)->fcb=getfname()) == NULL) error(FILERR);
|
|
2807 (filep+1)->ln=lineno;
|
|
2808 lineno=0;
|
|
2809 ++filep;
|
|
2810 *(chptr = linebuf) = '\0';
|
|
2811 } else if (macroeq("asm")) {
|
|
2812 if (asmf) error(MCERR);
|
|
2813 asmf = 1;
|
|
2814 getline();
|
|
2815 while (asmf) {
|
|
2816 gen_source(linebuf);
|
0
|
2817 getline();
|
18
|
2818 }
|
|
2819 } else if (macroeq("endasm")) {
|
|
2820 if (!asmf) error(MCERR);
|
|
2821 asmf = 0;
|
|
2822 } else if (macroeq(" "))
|
|
2823 getline();
|
|
2824 else error(MCERR);
|
0
|
2825 }
|
|
2826
|
|
2827 int
|
|
2828 macroeq(char *s)
|
|
2829 {
|
|
2830 char *p;
|
|
2831
|
|
2832 for (p = chptr; *s;) if (*s++ != *p++) return 0;
|
|
2833 chptr = p;
|
|
2834 return 1;
|
|
2835 }
|
|
2836
|
32
|
2837 void
|
|
2838 macro_define(char *macro)
|
|
2839 {
|
|
2840 char *chptr_save;
|
|
2841 int chsave;
|
|
2842 chptr_save = chptr;
|
|
2843 chsave = ch;
|
|
2844 chptr = macro;
|
|
2845 ch= *chptr++;
|
|
2846 macro_define0();
|
|
2847 chptr = chptr_save;
|
|
2848 ch = chsave;
|
|
2849 }
|
|
2850
|
|
2851 void
|
|
2852 macro_define0()
|
|
2853 {
|
|
2854 int i,args,c;
|
|
2855 i=mode;
|
|
2856 mode=MDECL;
|
|
2857 ch= *chptr;
|
|
2858 getsym();
|
35
|
2859 /* fprintf(stderr,"macro def: %s =>",name); */
|
32
|
2860 if (nptr->sc != EMPTY) { /* override exisiting macro */
|
|
2861 }
|
|
2862 args = 0;
|
|
2863 if (ch=='(') {
|
|
2864 nptr->sc = FMACRO;
|
37
|
2865 args = macro_args(&cheapp,cheap+CHEAPSIZE,&chptr);
|
32
|
2866 } else {
|
|
2867 nptr->sc = MACRO;
|
33
|
2868 nptr->ty = -1;
|
32
|
2869 }
|
34
|
2870 nptr->dsp = list2((int)cheapp,args); /* macro body */
|
32
|
2871 while ((*cheapp++ = c = *chptr++)
|
34
|
2872 && c != '\n') {
|
|
2873 if (c=='\\' && chptr[1]=='\n') {
|
|
2874 cheapp--;
|
|
2875 getline();
|
|
2876 }
|
|
2877 }
|
32
|
2878 *cheapp++ = '\0';
|
|
2879 if (cheapp >= cheap+CHEAPSIZE) /* too late? */
|
|
2880 error(STRERR);
|
35
|
2881 /* fprintf(stderr,"%s\n",(char *)car(nptr->dsp)); */
|
32
|
2882 mode=i;
|
|
2883 }
|
|
2884
|
28
|
2885 int
|
37
|
2886 macro_args(char **pcheapp,char *maxcheap,char **pchptr)
|
28
|
2887 {
|
|
2888 int c;
|
|
2889 int in_quote = 0;
|
|
2890 int in_wquote = 0;
|
|
2891 int plevel = 0;
|
34
|
2892 char *cheapp = *pcheapp;
|
|
2893 char *chptr = *pchptr;
|
|
2894 int args = list2((int)cheapp,0);
|
28
|
2895 for(;;) {
|
|
2896 *cheapp++ = c = *chptr++;
|
37
|
2897 if (cheapp >= maxcheap) error(MCERR);
|
33
|
2898 if (!c) {
|
|
2899 chptr--;
|
|
2900 error(MCERR);
|
34
|
2901 *pchptr = chptr;
|
|
2902 *pcheapp = cheapp;
|
33
|
2903 return reverse0(args);
|
|
2904 }
|
28
|
2905 if (in_quote) {
|
|
2906 if (c=='\\') {
|
|
2907 if (*chptr != '\n') {
|
|
2908 *cheapp++ = *chptr++;
|
|
2909 } else {
|
|
2910 getline();
|
|
2911 }
|
|
2912 } else if (c=='\'') {
|
|
2913 in_quote = 0;
|
|
2914 }
|
|
2915 } else if (in_wquote) {
|
|
2916 if (c=='\\') {
|
|
2917 if (*chptr !='\n') {
|
|
2918 *cheapp++ = *chptr++;
|
|
2919 } else {
|
|
2920 *cheapp = '\n';
|
|
2921 getline();
|
|
2922 }
|
|
2923 } else if (c=='"') {
|
|
2924 in_wquote = 0;
|
|
2925 }
|
|
2926 } else if (c=='"') {
|
|
2927 in_wquote = 1;
|
|
2928 } else if (c=='\'') {
|
|
2929 in_quote = 1;
|
|
2930 } if (plevel==0) {
|
|
2931 if (c==',') {
|
|
2932 cheapp[-1] = 0;
|
34
|
2933 args = list2((int)cheapp,args);
|
28
|
2934 } else if (c==')') {
|
|
2935 cheapp[-1] = 0;
|
|
2936 break;
|
|
2937 } else if (c=='(') {
|
|
2938 plevel++;
|
|
2939 } else if (c=='\\') {
|
|
2940 if (*chptr=='\n') {
|
|
2941 cheapp--;
|
|
2942 getline();
|
|
2943 }
|
|
2944 } else if (c==' '||c=='\t') {
|
|
2945 cheapp--;
|
|
2946 } else if (c=='\n') {
|
|
2947 cheapp--;
|
|
2948 getline();
|
34
|
2949 chptr = *pchptr;
|
28
|
2950 }
|
|
2951 } else if (c==')') {
|
|
2952 plevel--;
|
|
2953 } else if (c=='\n') {
|
|
2954 cheapp--;
|
|
2955 getline();
|
34
|
2956 chptr = *pchptr;
|
28
|
2957 }
|
|
2958 }
|
33
|
2959 ch = *chptr;
|
|
2960 if (ch) chptr++;
|
34
|
2961 *pchptr = chptr;
|
|
2962 *pcheapp = cheapp;
|
28
|
2963 return reverse0(args);
|
|
2964 }
|
|
2965
|
34
|
2966 /* output macro expansion result into macrobuf (macropp) */
|
|
2967
|
|
2968 int
|
|
2969 macro_function(int macrop,char **pchptr,NMTBL *nptr,int history)
|
28
|
2970 {
|
34
|
2971 int args,sargs,values,evalues;
|
|
2972 char *macro;
|
|
2973
|
|
2974 sargs = args = cadr(nptr->dsp);
|
37
|
2975 values = macro_args(¯opp,macro_buf+MACROSIZE,pchptr);
|
34
|
2976 evalues = 0;
|
|
2977 while(values) {
|
|
2978 evalues = list2(macro_eval(0,(char *)car(values),history),evalues);
|
28
|
2979 values = cadr(values);
|
|
2980 }
|
34
|
2981 evalues = reverse0(evalues);
|
|
2982 while(args) {
|
|
2983 local_define((char *)car(args),append(reverse0(car(evalues))));
|
35
|
2984 /* fprintf(stderr,"%s: %s => %s\n",nptr->nm,(char *)car(args),(char *)car(msearch0((char *)car(args))->dsp)); */
|
34
|
2985 args = cadr(args);
|
|
2986 evalues = cadr(evalues);
|
29
|
2987 }
|
34
|
2988 macro = (char *)car(nptr->dsp);
|
|
2989 macrop = macro_eval(macrop,macro,list2((int)macro,history));
|
35
|
2990 /* fprintf(stderr,"%s: result %s => %s\n",nptr->nm,macro,(char *)car(macrop)); */
|
34
|
2991 args = sargs;
|
|
2992 while(args) {
|
|
2993 local_undef((char *)car(args));
|
|
2994 args = cadr(args);
|
|
2995 }
|
|
2996 return macrop;
|
28
|
2997 }
|
|
2998
|
|
2999 void
|
|
3000 local_define(char *macro,char *value)
|
|
3001 {
|
|
3002 NMTBL *nptr0;
|
30
|
3003 nptr0 = msearch0(macro);
|
34
|
3004 nptr0->ty=list3(nptr0->sc,nptr0->ty,nptr0->dsp);
|
29
|
3005 nptr0->sc=MACRO;
|
|
3006 nptr0->dsp=list2((int)value,0);
|
28
|
3007 }
|
|
3008
|
|
3009 void
|
|
3010 local_undef(char *macro)
|
|
3011 {
|
|
3012 NMTBL *nptr0;
|
|
3013 int save;
|
30
|
3014 nptr0 = msearch0(macro);
|
28
|
3015 save = nptr0->ty;
|
|
3016 nptr0->sc=car(save);
|
33
|
3017 nptr0->dsp=caddr(save);
|
|
3018 nptr0->ty=cadr(save);
|
0
|
3019 }
|
|
3020
|
|
3021 int
|
34
|
3022 macro_eval(int macrop,char *body,int history)
|
|
3023 {
|
|
3024 int c;
|
|
3025 int in_quote = 0;
|
|
3026 int in_wquote = 0;
|
|
3027 char *macro;
|
|
3028 int i;
|
|
3029 NMTBL *nptrm;
|
|
3030 c = 1;
|
|
3031 macrop = list2((int)macropp,macrop);
|
|
3032 while(c && (*macropp++ = c = *body++)) {
|
37
|
3033 if (macropp>macro_buf+MACROSIZE) error(STRERR);
|
34
|
3034 if (in_quote) {
|
|
3035 if (c=='\\') {
|
|
3036 *macropp++ = c = *body++;
|
|
3037 } else if (c=='\'') {
|
|
3038 in_quote = 0;
|
|
3039 }
|
|
3040 } else if (in_wquote) {
|
|
3041 if (c=='\\') {
|
|
3042 *macropp++ = c = *body++;
|
|
3043 } else if (c=='"') {
|
|
3044 in_wquote = 0;
|
|
3045 }
|
|
3046 } else if (c=='"') {
|
|
3047 in_wquote = 1;
|
|
3048 } else if (c=='\'') {
|
|
3049 in_quote = 1;
|
|
3050 } else if (alpha(c)) {
|
|
3051 macropp--;
|
|
3052 for(i=0;alpha(c)||digit(c);i++) { namebuf[i] = c; c=*body++;}
|
|
3053 namebuf[i]=0;
|
|
3054 nptrm = msearch0(namebuf);
|
|
3055 macro = (char *)car(nptrm->dsp);
|
|
3056 if (nptrm->sc==MACRO) {
|
|
3057 while((*macropp++ = *macro++));
|
|
3058 macropp[-1]=c;
|
|
3059 } else if (nptrm->sc==FMACRO) {
|
|
3060 if(c!='(') error(MCERR);
|
|
3061 *macropp++=0;
|
|
3062 macrop = macro_function(macrop,&body,nptrm,
|
|
3063 list2((int)macro,history));
|
|
3064 macrop = list2((int)macropp,macrop);
|
|
3065 } else {
|
|
3066 macro = namebuf;
|
|
3067 while((*macropp++ = *macro++));
|
|
3068 macropp[-1]=c;
|
|
3069 }
|
|
3070 }
|
|
3071 }
|
|
3072 *macropp++=0;
|
|
3073 return macrop;
|
|
3074 }
|
|
3075
|
|
3076 int
|
0
|
3077 list2(int e1, int e2)
|
|
3078 {
|
|
3079 int e;
|
|
3080
|
|
3081 e=getfree(2);
|
|
3082 heap[e]=e1;
|
|
3083 heap[e+1]=e2;
|
|
3084 return e;
|
|
3085 }
|
|
3086
|
|
3087 int
|
|
3088 list3(int e1, int e2, int e3)
|
|
3089 {
|
|
3090 int e;
|
|
3091
|
|
3092 e=getfree(3);
|
|
3093 heap[e]=e1;
|
|
3094 heap[e+1]=e2;
|
|
3095 heap[e+2]=e3;
|
|
3096 return e;
|
|
3097 }
|
|
3098
|
|
3099 int
|
|
3100 list4(int e1, int e2, int e3, int e4)
|
|
3101 {
|
|
3102 int e;
|
|
3103
|
|
3104 e=getfree(4);
|
|
3105 heap[e]=e1;
|
|
3106 heap[e+1]=e2;
|
|
3107 heap[e+2]=e3;
|
|
3108 heap[e+3]=e4;
|
|
3109 return e;
|
|
3110 }
|
|
3111
|
|
3112 int
|
|
3113 getfree(int n)
|
|
3114 {
|
|
3115 int e;
|
|
3116
|
|
3117 switch (mode) {
|
|
3118 case GDECL: case GSDECL: case GUDECL: case GTDECL:
|
34
|
3119 case MDECL:
|
0
|
3120 e=gfree;
|
|
3121 gfree+=n;
|
|
3122 break;
|
|
3123 default:
|
|
3124 lfree-=n;
|
|
3125 e=lfree;
|
|
3126 }
|
|
3127 if(lfree<gfree) error(HPERR);
|
|
3128 return e;
|
|
3129 }
|
|
3130
|
|
3131 int
|
33
|
3132 glist2(int e1,int e2)
|
|
3133 {
|
|
3134 int smode,ret;
|
|
3135 smode = mode;
|
|
3136 mode = GDECL;
|
|
3137 ret = list2(e1,e2);
|
|
3138 mode = smode;
|
|
3139 return ret;
|
|
3140 }
|
|
3141
|
|
3142 int
|
0
|
3143 rplacad(int e, int n)
|
|
3144 {
|
|
3145 heap[e+1]=n;
|
|
3146 return e;
|
|
3147 }
|
|
3148
|
|
3149 int
|
|
3150 rplacadd(int e, int n)
|
|
3151 {
|
|
3152 heap[e+2]=n;
|
|
3153 return e;
|
|
3154 }
|
|
3155
|
34
|
3156 char *
|
|
3157 append(int lists)
|
|
3158 {
|
|
3159 char *p;
|
|
3160 char *result = macropp;
|
|
3161 while(lists) {
|
37
|
3162 if (macropp>macro_buf+MACROSIZE) error(STRERR);
|
34
|
3163 p = (char *)car(lists);
|
35
|
3164 while((*macropp++=*p++)) if (p[-1]=='\n') macropp[-1]=' ';
|
34
|
3165 macropp--;
|
|
3166 lists = cadr(lists);
|
|
3167 }
|
|
3168 macropp++;
|
|
3169 return result;
|
|
3170 }
|
7
|
3171
|
18
|
3172 void
|
0
|
3173 display_ntable(NMTBL *n, char *s)
|
|
3174 {
|
18
|
3175 fprintf(stderr,"\n%s %0x %0x ",s,(int)n,(int)ntable);
|
0
|
3176 fprintf(stderr,"nptr->sc %d ",n->sc);
|
|
3177 fprintf(stderr,"nptr->dsp %d ",n->dsp);
|
|
3178 fprintf(stderr,"nptr->ty %d ",n->ty);
|
|
3179 fprintf(stderr,"nptr->nm %s\n",n->nm);
|
|
3180 }
|
|
3181
|
40
|
3182 int c0(int d) { fprintf(stderr,"heap[%d]=",d);return car(d); }
|
|
3183 int c1(int d) { fprintf(stderr,"heap[%d]=",d);return cadr(d); }
|
|
3184 int c2(int d) { fprintf(stderr,"heap[%d]=",d);return caddr(d); }
|
|
3185 int c3(int d) { fprintf(stderr,"heap[%d]=",d);return cadddr(d); }
|
|
3186 char *cc0(int d) { fprintf(stderr,"heap[%d]=",d);return (char *)car(d); }
|
|
3187 char *cc1(int d) { fprintf(stderr,"heap[%d]=",d);return (char *)cadr(d); }
|
|
3188 char *cc2(int d) { fprintf(stderr,"heap[%d]=",d);return (char *)caddr(d); }
|
|
3189 char *cc3(int d) { fprintf(stderr,"heap[%d]=",d);return (char *)cadddr(d); }
|
37
|
3190
|
0
|
3191 /* end */
|