1 #include <assert.h>
2 #include <stdarg.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <limits.h>
6 #include <string.h>
7 
8 #define NEW(p,a) ((p) = allocate(sizeof *(p), (a)))
9 #define NEW0(p,a) memset(NEW((p),(a)), 0, sizeof *(p))
10 #define isaddrop(op) (specific(op)==ADDRG+P || specific(op)==ADDRL+P \
11 	|| specific(op)==ADDRF+P)
12 
13 #define	MAXLINE  512
14 #define	BUFSIZE 4096
15 
16 #define istypename(t,tsym) (kind[t] == CHAR \
17 	|| (t == ID && tsym && tsym->sclass == TYPEDEF))
18 #define sizeop(n) ((n)<<10)
19 #define generic(op)  ((op)&0x3F0)
20 #define specific(op) ((op)&0x3FF)
21 #define opindex(op) (((op)>>4)&0x3F)
22 #define opkind(op)  ((op)&~0x3F0)
23 #define opsize(op)  ((op)>>10)
24 #define optype(op)  ((op)&0xF)
25 #ifdef __LCC__
26 #ifndef __STDC__
27 #define __STDC__
28 #endif
29 #endif
30 #define NELEMS(a) ((int)(sizeof (a)/sizeof ((a)[0])))
31 #undef roundup
32 #define roundup(x,n) (((x)+((n)-1))&(~((n)-1)))
33 #define mkop(op,ty) (specific((op) + ttob(ty)))
34 
35 #define extend(x,ty) ((x)&(1<<(8*(ty)->size-1)) ? (x)|((~0UL)<<(8*(ty)->size-1)) : (x)&ones(8*(ty)->size))
36 #define ones(n) ((n)>=8*sizeof (unsigned long) ? ~0UL : ~((~0UL)<<(n)))
37 
38 #define isqual(t)     ((t)->op >= CONST)
39 #define unqual(t)     (isqual(t) ? (t)->type : (t))
40 
41 #define isvolatile(t) ((t)->op == VOLATILE \
42                     || (t)->op == CONST+VOLATILE)
43 #define isconst(t)    ((t)->op == CONST \
44                     || (t)->op == CONST+VOLATILE)
45 #define isarray(t)    (unqual(t)->op == ARRAY)
46 #define isstruct(t)   (unqual(t)->op == STRUCT \
47                     || unqual(t)->op == UNION)
48 #define isunion(t)    (unqual(t)->op == UNION)
49 #define isfunc(t)     (unqual(t)->op == FUNCTION)
50 #define isptr(t)      (unqual(t)->op == POINTER)
51 #define ischar(t)     ((t)->size == 1 && isint(t))
52 #define isint(t)      (unqual(t)->op == INT \
53                     || unqual(t)->op == UNSIGNED)
54 #define isfloat(t)    (unqual(t)->op == FLOAT)
55 #define isarith(t)    (unqual(t)->op <= UNSIGNED)
56 #define isunsigned(t) (unqual(t)->op == UNSIGNED)
57 #define isscalar(t)   (unqual(t)->op <= POINTER \
58                     || unqual(t)->op == ENUM)
59 #define isenum(t)     (unqual(t)->op == ENUM)
60 #define fieldsize(p)  (p)->bitsize
61 #define fieldright(p) ((p)->lsb - 1)
62 #define fieldleft(p)  (8*(p)->type->size - \
63                         fieldsize(p) - fieldright(p))
64 #define fieldmask(p)  (~(~(unsigned)0<<fieldsize(p)))
65 typedef struct node *Node;
66 
67 typedef struct list *List;
68 
69 typedef struct code *Code;
70 
71 typedef struct swtch *Swtch;
72 
73 typedef struct symbol *Symbol;
74 
75 typedef struct coord {
76 	char *file;
77 	unsigned x, y;
78 } Coordinate;
79 typedef struct table *Table;
80 
81 typedef union value {
82 	long i;
83 	unsigned long u;
84 	double d;
85 	void *p;
86 	void (*g)(void);
87 } Value;
88 typedef struct tree *Tree;
89 
90 typedef struct type *Type;
91 
92 typedef struct field *Field;
93 
94 typedef struct {
95 	unsigned printed:1;
96 	unsigned marked;
97 	unsigned short typeno;
98 	void *xt;
99 } Xtype;
100 
101 #include "config.h"
102 typedef struct metrics {
103 	unsigned char size, align, outofline;
104 } Metrics;
105 typedef struct interface {
106 	Metrics charmetric;
107 	Metrics shortmetric;
108 	Metrics intmetric;
109 	Metrics longmetric;
110 	Metrics longlongmetric;
111 	Metrics floatmetric;
112 	Metrics doublemetric;
113 	Metrics longdoublemetric;
114 	Metrics ptrmetric;
115 	Metrics structmetric;
116 	unsigned little_endian:1;
117 	unsigned mulops_calls:1;
118 	unsigned wants_callb:1;
119 	unsigned wants_argb:1;
120 	unsigned left_to_right:1;
121 	unsigned wants_dag:1;
122 	unsigned unsigned_char:1;
123 void (*address)(Symbol p, Symbol q, long n);
124 void (*blockbeg)(Env *);
125 void (*blockend)(Env *);
126 void (*defaddress)(Symbol);
127 void (*defconst)  (int suffix, int size, Value v);
128 void (*defstring)(int n, char *s);
129 void (*defsymbol)(Symbol);
130 void (*emit)    (Node);
131 void (*export)(Symbol);
132 void (*function)(Symbol, Symbol[], Symbol[], int);
133 Node (*gen)     (Node);
134 void (*global)(Symbol);
135 void (*import)(Symbol);
136 void (*local)(Symbol);
137 void (*progbeg)(int argc, char *argv[]);
138 void (*progend)(void);
139 void (*segment)(int);
140 void (*space)(int);
141 void (*stabblock)(int, int, Symbol*);
142 void (*stabend)  (Coordinate *, Symbol, Coordinate **, Symbol *, Symbol *);
143 void (*stabfend) (Symbol, int);
144 void (*stabinit) (char *, int, char *[]);
145 void (*stabline) (Coordinate *);
146 void (*stabsym)  (Symbol);
147 void (*stabtype) (Symbol);
148 	Xinterface x;
149 } Interface;
150 typedef struct binding {
151 	char *name;
152 	Interface *ir;
153 } Binding;
154 
155 extern Binding bindings[];
156 extern Interface *IR;
157 typedef struct {
158 	List blockentry;
159 	List blockexit;
160 	List entry;
161 	List exit;
162 	List returns;
163 	List points;
164 	List calls;
165 	List end;
166 } Events;
167 
168 enum {
169 #define xx(a,b,c,d,e,f,g) a=b,
170 #define yy(a,b,c,d,e,f,g)
171 #include "token.h"
172 	LAST
173 };
174 struct node {
175 	short op;
176 	short count;
177  	Symbol syms[3];
178 	Node kids[2];
179 	Node link;
180 	Xnode x;
181 };
182 enum {
183 	F=FLOAT,
184 	I=INT,
185 	U=UNSIGNED,
186 	P=POINTER,
187 	V=VOID,
188 	B=STRUCT
189 };
190 #define gop(name,value) name=value<<4,
191 #define op(name,type,sizes)
192 
193 enum { gop(CNST,1)
194        	op(CNST,F,fdx)
195        	op(CNST,I,csilh)
196        	op(CNST,P,p)
197        	op(CNST,U,csilh)
198        gop(ARG,2)
199        	op(ARG,B,-)
200        	op(ARG,F,fdx)
201        	op(ARG,I,ilh)
202        	op(ARG,P,p)
203        	op(ARG,U,ilh)
204        gop(ASGN,3)
205        	op(ASGN,B,-)
206        	op(ASGN,F,fdx)
207        	op(ASGN,I,csilh)
208        	op(ASGN,P,p)
209        	op(ASGN,U,csilh)
210        gop(INDIR,4)
211        	op(INDIR,B,-)
212        	op(INDIR,F,fdx)
213        	op(INDIR,I,csilh)
214        	op(INDIR,P,p)
215        	op(INDIR,U,csilh)
216        gop(CVF,7)
217        	op(CVF,F,fdx)
218        	op(CVF,I,ilh)
219        gop(CVI,8)
220        	op(CVI,F,fdx)
221        	op(CVI,I,csilh)
222        	op(CVI,U,csilhp)
223        gop(CVP,9)
224        	op(CVP,U,p)
225        gop(CVU,11)
226        	op(CVU,I,csilh)
227        	op(CVU,P,p)
228        	op(CVU,U,csilh)
229        gop(NEG,12)
230        	op(NEG,F,fdx)
231        	op(NEG,I,ilh)
232        gop(CALL,13)
233        	op(CALL,B,-)
234        	op(CALL,F,fdx)
235        	op(CALL,I,ilh)
236        	op(CALL,P,p)
237        	op(CALL,U,ilh)
238        	op(CALL,V,-)
239        gop(RET,15)
240        	op(RET,F,fdx)
241        	op(RET,I,ilh)
242        	op(RET,P,p)
243        	op(RET,U,ilh)
244        	op(RET,V,-)
245        gop(ADDRG,16)
246        	op(ADDRG,P,p)
247        gop(ADDRF,17)
248        	op(ADDRF,P,p)
249        gop(ADDRL,18)
250        	op(ADDRL,P,p)
251        gop(ADD,19)
252        	op(ADD,F,fdx)
253        	op(ADD,I,ilh)
254        	op(ADD,P,p)
255        	op(ADD,U,ilhp)
256        gop(SUB,20)
257        	op(SUB,F,fdx)
258        	op(SUB,I,ilh)
259        	op(SUB,P,p)
260        	op(SUB,U,ilhp)
261        gop(LSH,21)
262        	op(LSH,I,ilh)
263        	op(LSH,U,ilh)
264        gop(MOD,22)
265        	op(MOD,I,ilh)
266        	op(MOD,U,ilh)
267        gop(RSH,23)
268        	op(RSH,I,ilh)
269        	op(RSH,U,ilh)
270        gop(BAND,24)
271        	op(BAND,I,ilh)
272        	op(BAND,U,ilh)
273        gop(BCOM,25)
274        	op(BCOM,I,ilh)
275        	op(BCOM,U,ilh)
276        gop(BOR,26)
277        	op(BOR,I,ilh)
278        	op(BOR,U,ilh)
279        gop(BXOR,27)
280        	op(BXOR,I,ilh)
281        	op(BXOR,U,ilh)
282        gop(DIV,28)
283        	op(DIV,F,fdx)
284        	op(DIV,I,ilh)
285        	op(DIV,U,ilh)
286        gop(MUL,29)
287        	op(MUL,F,fdx)
288        	op(MUL,I,ilh)
289        	op(MUL,U,ilh)
290        gop(EQ,30)
291        	op(EQ,F,fdx)
292        	op(EQ,I,ilh)
293        	op(EQ,U,ilhp)
294        gop(GE,31)
295        	op(GE,F,fdx)
296        	op(GE,I,ilh)
297        	op(GE,U,ilhp)
298        gop(GT,32)
299        	op(GT,F,fdx)
300        	op(GT,I,ilh)
301        	op(GT,U,ilhp)
302        gop(LE,33)
303        	op(LE,F,fdx)
304        	op(LE,I,ilh)
305        	op(LE,U,ilhp)
306        gop(LT,34)
307        	op(LT,F,fdx)
308        	op(LT,I,ilh)
309        	op(LT,U,ilhp)
310        gop(NE,35)
311        	op(NE,F,fdx)
312        	op(NE,I,ilh)
313        	op(NE,U,ilhp)
314        gop(JUMP,36)
315        	op(JUMP,V,-)
316        gop(LABEL,37)
317        	op(LABEL,V,-)
318        gop(LOAD,14)
319        	op(LOAD,B,-)
320        	op(LOAD,F,fdx)
321        	op(LOAD,I,csilh)
322        	op(LOAD,P,p)
323        	op(LOAD,U,csilhp) LASTOP };
324 
325 #undef gop
326 #undef op
327 enum { CODE=1, BSS, DATA, LIT };
328 enum { PERM=0, FUNC, STMT };
329 struct list {
330 	void *x;
331 	List link;
332 };
333 
334 struct code {
335 	enum { Blockbeg, Blockend, Local, Address, Defpoint,
336 	       Label,    Start,    Gen,   Jump,    Switch
337 	} kind;
338 	Code prev, next;
339 	union {
340 		struct {
341 			int level;
342 			Symbol *locals;
343 			Table identifiers, types;
344 			Env x;
345 		} block;
346 		Code begin;
347 		Symbol var;
348 
349 		struct {
350 			Symbol sym;
351 			Symbol base;
352 			long offset;
353 		} addr;
354 		struct {
355 			Coordinate src;
356 			int point;
357 		} point;
358 		Node forest;
359 		struct {
360 			Symbol sym;
361 			Symbol table;
362 			Symbol deflab;
363 			int size;
364 			long *values;
365 			Symbol *labels;
366 		} swtch;
367 
368 	} u;
369 };
370 struct swtch {
371 	Symbol sym;
372 	int lab;
373 	Symbol deflab;
374 	int ncases;
375 	int size;
376 	long *values;
377 	Symbol *labels;
378 };
379 struct symbol {
380 	char *name;
381 	int scope;
382 	Coordinate src;
383 	Symbol up;
384 	List uses;
385 	int sclass;
386 	unsigned structarg:1;
387 
388 	unsigned addressed:1;
389 	unsigned computed:1;
390 	unsigned temporary:1;
391 	unsigned generated:1;
392 	unsigned defined:1;
393 	Type type;
394 	float ref;
395 	union {
396 		struct {
397 			int label;
398 			Symbol equatedto;
399 		} l;
400 		struct {
401 			unsigned cfields:1;
402 			unsigned vfields:1;
403 			Table ftab;		/* omit */
404 			Field flist;
405 		} s;
406 		int value;
407 		Symbol *idlist;
408 		struct {
409 			Value min, max;
410 		} limits;
411 		struct {
412 			Value v;
413 			Symbol loc;
414 		} c;
415 		struct {
416 			Coordinate pt;
417 			int label;
418 			int ncalls;
419 			Symbol *callee;
420 		} f;
421 		int seg;
422 		Symbol alias;
423 		struct {
424 			Node cse;
425 			int replace;
426 			Symbol next;
427 		} t;
428 	} u;
429 	Xsymbol x;
430 };
431 enum { CONSTANTS=1, LABELS, GLOBAL, PARAM, LOCAL };
432 struct tree {
433 	int op;
434 	Type type;
435 	Tree kids[2];
436 	Node node;
437 	union {
438 		Value v;
439 		Symbol sym;
440 
441 		Field field;
442 	} u;
443 };
444 enum {
445 	AND=38<<4,
446 	NOT=39<<4,
447 	OR=40<<4,
448 	COND=41<<4,
449 	RIGHT=42<<4,
450 	FIELD=43<<4
451 };
452 struct type {
453 	int op;
454 	Type type;
455 	int align;
456 	int size;
457 	union {
458 		Symbol sym;
459 		struct {
460 			unsigned oldstyle:1;
461 			Type *proto;
462 		} f;
463 	} u;
464 	Xtype x;
465 };
466 struct field {
467 	char *name;
468 	Type type;
469 	int offset;
470 	short bitsize;
471 	short lsb;
472 	Field link;
473 };
474 extern int assignargs;
475 extern int prunetemps;
476 extern int nodecount;
477 extern Symbol cfunc;
478 extern Symbol retv;
479 extern Tree (*optree[])(int, Tree, Tree);
480 
481 extern char kind[];
482 extern int errcnt;
483 extern int errlimit;
484 extern int wflag;
485 extern Events events;
486 extern float refinc;
487 
488 extern unsigned char *cp;
489 extern unsigned char *limit;
490 extern char *firstfile;
491 extern char *file;
492 extern char *line;
493 extern int lineno;
494 extern int t;
495 extern char *token;
496 extern Symbol tsym;
497 extern Coordinate src;
498 extern int Aflag;
499 extern int Pflag;
500 extern Symbol YYnull;
501 extern Symbol YYcheck;
502 extern int glevel;
503 extern int xref;
504 
505 extern int ncalled;
506 extern int npoints;
507 
508 extern int needconst;
509 extern int explicitCast;
510 extern struct code codehead;
511 extern Code codelist;
512 extern Table stmtlabs;
513 extern float density;
514 extern Table constants;
515 extern Table externals;
516 extern Table globals;
517 extern Table identifiers;
518 extern Table labels;
519 extern Table types;
520 extern int level;
521 
522 extern List loci, symbols;
523 
524 extern List symbols;
525 
526 extern int where;
527 extern Type chartype;
528 extern Type doubletype;
529 extern Type floattype;
530 extern Type inttype;
531 extern Type longdouble;
532 extern Type longtype;
533 extern Type longlong;
534 extern Type shorttype;
535 extern Type signedchar;
536 extern Type unsignedchar;
537 extern Type unsignedlonglong;
538 extern Type unsignedlong;
539 extern Type unsignedshort;
540 extern Type unsignedtype;
541 extern Type charptype;
542 extern Type funcptype;
543 extern Type voidptype;
544 extern Type voidtype;
545 extern Type unsignedptr;
546 extern Type signedptr;
547 extern Type widechar;
548 extern void  *allocate(unsigned long n, unsigned a);
549 extern void deallocate(unsigned a);
550 extern void *newarray(unsigned long m, unsigned long n, unsigned a);
551 extern void walk(Tree e, int tlab, int flab);
552 extern Node listnodes(Tree e, int tlab, int flab);
553 extern Node newnode(int op, Node left, Node right, Symbol p);
554 extern Tree cvtconst(Tree);
555 extern void printdag(Node, int);
556 extern void compound(int, Swtch, int);
557 extern void defglobal(Symbol, int);
558 extern void finalize(void);
559 extern void program(void);
560 
561 extern Tree vcall(Symbol func, Type ty, ...);
562 extern Tree addrof(Tree);
563 extern Tree asgn(Symbol, Tree);
564 extern Tree asgntree(int, Tree, Tree);
565 extern Type assign(Type, Tree);
566 extern Tree bittree(int, Tree, Tree);
567 extern Tree call(Tree, Type, Coordinate);
568 extern Tree calltree(Tree, Type, Tree, Symbol);
569 extern Tree condtree(Tree, Tree, Tree);
570 extern Tree cnsttree(Type, ...);
571 extern Tree consttree(unsigned int, Type);
572 extern Tree eqtree(int, Tree, Tree);
573 extern int iscallb(Tree);
574 extern Tree shtree(int, Tree, Tree);
575 extern void typeerror(int, Tree, Tree);
576 
577 extern void test(int tok, char set[]);
578 extern void expect(int tok);
579 extern void skipto(int tok, char set[]);
580 extern void error(const char *, ...);
581 extern int fatal(const char *, const char *, int);
582 extern void warning(const char *, ...);
583 
584 typedef void (*Apply)(void *, void *, void *);
585 extern void attach(Apply, void *, List *);
586 extern void apply(List event, void *arg1, void *arg2);
587 extern Tree retype(Tree p, Type ty);
588 extern Tree rightkid(Tree p);
589 extern int hascall(Tree p);
590 extern Type binary(Type, Type);
591 extern Tree cast(Tree, Type);
592 extern Tree cond(Tree);
593 extern Tree expr0(int);
594 extern Tree expr(int);
595 extern Tree expr1(int);
596 extern Tree field(Tree, const char *);
597 extern char *funcname(Tree);
598 extern Tree idtree(Symbol);
599 extern Tree incr(int, Tree, Tree);
600 extern Tree lvalue(Tree);
601 extern Tree nullcall(Type, Symbol, Tree, Tree);
602 extern Tree pointer(Tree);
603 extern Tree rvalue(Tree);
604 extern Tree value(Tree);
605 
606 extern void defpointer(Symbol);
607 extern Type initializer(Type, int);
608 extern void swtoseg(int);
609 
610 extern void input_init(int, char *[]);
611 extern void fillbuf(void);
612 extern void nextline(void);
613 
614 extern int getchr(void);
615 extern int gettok(void);
616 
617 extern void emitcode(void);
618 extern void gencode (Symbol[], Symbol[]);
619 extern void fprint(FILE *f, const char *fmt, ...);
620 extern char *stringf(const char *, ...);
621 extern void check(Node);
622 extern void print(const char *, ...);
623 
624 extern List append(void *x, List list);
625 extern int  length(List list);
626 extern void *ltov (List *list, unsigned a);
627 extern void init(int, char *[]);
628 
629 extern Type typename(void);
630 extern void checklab(Symbol p, void *cl);
631 extern Type enumdcl(void);
632 extern void main_init(int, char *[]);
633 extern int main(int, char *[]);
634 
635 extern void vfprint(FILE *, char *, const char *, va_list);
636 
637 extern int process(char *);
638 extern int findfunc(char *, char *);
639 extern int findcount(char *, int, int);
640 
641 extern Tree constexpr(int);
642 extern int intexpr(int, int);
643 extern Tree simplify(int, Type, Tree, Tree);
644 extern int ispow2(unsigned long u);
645 
646 extern int reachable(int);
647 
648 extern void addlocal(Symbol);
649 extern void branch(int);
650 extern Code code(int);
651 extern void definelab(int);
652 extern void definept(Coordinate *);
653 extern void equatelab(Symbol, Symbol);
654 extern Node jump(int);
655 extern void retcode(Tree);
656 extern void statement(int, Swtch, int);
657 extern void swcode(Swtch, int *, int, int);
658 extern void swgen(Swtch);
659 
660 extern char * string(const char *str);
661 extern char *stringn(const char *str, int len);
662 extern char *stringd(long n);
663 extern Symbol relocate(const char *name, Table src, Table dst);
664 extern void use(Symbol p, Coordinate src);
665 extern void locus(Table tp, Coordinate *cp);
666 extern Symbol allsymbols(Table);
667 
668 extern Symbol constant(Type, Value);
669 extern void enterscope(void);
670 extern void exitscope(void);
671 extern Symbol findlabel(int);
672 extern Symbol findtype(Type);
673 extern void foreach(Table, int, void (*)(Symbol, void *), void *);
674 extern Symbol genident(int, Type, int);
675 extern int genlabel(int);
676 extern Symbol install(const char *, Table *, int, int);
677 extern Symbol intconst(int);
678 extern Symbol lookup(const char *, Table);
679 extern Symbol mkstr(char *);
680 extern Symbol mksymbol(int, const char *, Type);
681 extern Symbol newtemp(int, int, int);
682 extern Table table(Table, int);
683 extern Symbol temporary(int, Type);
684 extern char *vtoa(Type, Value);
685 
686 extern int nodeid(Tree);
687 extern char *opname(int);
688 extern int *printed(int);
689 extern void printtree(Tree, int);
690 extern Tree root(Tree);
691 extern Tree texpr(Tree (*)(int), int, int);
692 extern Tree tree(int, Type, Tree, Tree);
693 
694 extern void type_init(int, char *[]);
695 
696 extern Type signedint(Type);
697 
698 extern int hasproto(Type);
699 extern void outtype(Type, FILE *);
700 extern void printdecl (Symbol p, Type ty);
701 extern void printproto(Symbol p, Symbol args[]);
702 extern char *typestring(Type ty, char *id);
703 extern Field fieldref(const char *name, Type ty);
704 extern Type array(Type, int, int);
705 extern Type atop(Type);
706 extern Type btot(int, int);
707 extern Type compose(Type, Type);
708 extern Type deref(Type);
709 extern int eqtype(Type, Type, int);
710 extern Field fieldlist(Type);
711 extern Type freturn(Type);
712 extern Type ftype(Type, Type);
713 extern Type func(Type, Type *, int);
714 extern Field newfield(char *, Type, Type);
715 extern Type newstruct(int, char *);
716 extern void printtype(Type, int);
717 extern Type promote(Type);
718 extern Type ptr(Type);
719 extern Type qual(int, Type);
720 extern void rmtypes(int);
721 extern int ttob(Type);
722 extern int variadic(Type);
723 
724