1 /*	$Id: pass2.h,v 1.138 2014/10/11 09:50:21 ragge Exp $	*/
2 /*
3  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code and documentation must retain the above
10  * copyright notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditionsand the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * All advertising materials mentioning features or use of this software
15  * must display the following acknowledgement:
16  * 	This product includes software developed or owned by Caldera
17  *	International, Inc.
18  * Neither the name of Caldera International, Inc. nor the names of other
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27  * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 #include <sys/types.h>
36 
37 #ifndef MKEXT
38 #include "external.h"
39 #else
40 typedef unsigned int bittype; /* XXX - for basicblock */
41 #define	BIT2BYTE(a)	(((a) + 31) / 32)
42 #endif
43 #include "manifest.h"
44 
45 /* cookies, used as arguments to codgen */
46 #define FOREFF	01		/* compute for effects only */
47 #define INAREG	02		/* compute into a register */
48 #define INBREG	04		/* compute into a register */
49 #define INCREG	010		/* compute into a register */
50 #define INDREG	020		/* compute into a register */
51 #define	INREGS	(INAREG|INBREG|INCREG|INDREG)
52 #define FORCC	040		/* compute for condition codes only */
53 #define QUIET	0100		/* tell geninsn() to not complain if fail */
54 #define INTEMP	010000		/* compute into a temporary location */
55 #define FORREW	040000		/* search the table for a rewrite rule */
56 #define INEREG	0x10000		/* compute into a register, > 16 bits */
57 #define INFREG	0x20000		/* compute into a register, > 16 bits */
58 #define INGREG	0x40000		/* compute into a register, > 16 bits */
59 
60 /*
61  * OP descriptors,
62  * the ASG operator may be used on some of these
63  */
64 #define OPSIMP	010000		/* +, -, &, |, ^ */
65 #define OPCOMM	010002		/* +, &, |, ^ */
66 #define OPMUL	010004		/* *, / */
67 #define OPDIV	010006		/* /, % */
68 #define OPUNARY	010010		/* unary ops */
69 #define OPLEAF	010012		/* leaves */
70 #define OPANY	010014		/* any op... */
71 #define OPLOG	010016		/* logical ops */
72 #define OPFLOAT	010020		/* +, -, *, or / (for floats) */
73 #define OPSHFT	010022		/* <<, >> */
74 #define OPLTYPE	010024		/* leaf type nodes (e.g, NAME, ICON, etc.) */
75 
76 /* shapes */
77 #define SANY	01		/* same as FOREFF */
78 #define SAREG	02		/* same as INAREG */
79 #define SBREG	04		/* same as INBREG */
80 #define SCREG	010		/* same as INCREG */
81 #define SDREG	020		/* same as INDREG */
82 #define SCC	040		/* same as FORCC */
83 #define SNAME	0100
84 #define SCON	0200
85 #define SFLD	0400
86 #define SOREG	01000
87 #define STARNM	02000
88 #define STARREG	04000
89 #define SWADD	040000
90 #define SPECIAL	0100000
91 #define SZERO	SPECIAL
92 #define SONE	(SPECIAL|1)
93 #define SMONE	(SPECIAL|2)
94 #define SCCON	(SPECIAL|3)	/* -256 <= constant < 256 */
95 #define SSCON	(SPECIAL|4)	/* -32768 <= constant < 32768 */
96 #define SSOREG	(SPECIAL|5)	/* non-indexed OREG */
97 #define	MAXSPECIAL	(SPECIAL|5)
98 #define SEREG	0x10000		/* same as INEREG */
99 #define SFREG	0x20000		/* same as INFREG */
100 #define SGREG	0x40000		/* same as INGREG */
101 
102 /* These are used in rstatus[] in conjunction with SxREG */
103 #define	TEMPREG	01000
104 #define	PERMREG	02000
105 
106 /* tshape() return values */
107 #define	SRNOPE	0		/* Cannot match any shape */
108 #define	SRDIR	1		/* Direct match */
109 #define	SROREG	2		/* Can convert into OREG */
110 #define	SRREG	3		/* Must put into REG */
111 
112 /* find*() return values */
113 #define	FRETRY	-2
114 #define	FFAIL	-1
115 
116 /* INTEMP is carefully not conflicting with shapes */
117 
118 /* types */
119 #define TCHAR		01	/* char */
120 #define TSHORT		02	/* short */
121 #define TINT		04	/* int */
122 #define TLONG		010	/* long */
123 #define TFLOAT		020	/* float */
124 #define TDOUBLE		040	/* double */
125 #define TPOINT		0100	/* pointer to something */
126 #define TUCHAR		0200	/* unsigned char */
127 #define TUSHORT		0400	/* unsigned short */
128 #define TUNSIGNED	01000	/* unsigned int */
129 #define TULONG		02000	/* unsigned long */
130 #define TPTRTO		04000	/* pointer to one of the above */
131 #define TANY		010000	/* matches anything within reason */
132 #define TSTRUCT		020000	/* structure or union */
133 #define	TLONGLONG	040000	/* long long */
134 #define	TULONGLONG	0100000	/* unsigned long long */
135 #define	TLDOUBLE	0200000	/* long double; exceeds 16 bit */
136 #define	TFTN		0400000	/* function pointer; exceeds 16 bit */
137 
138 /* reclamation cookies */
139 #define RNULL		0	/* clobber result */
140 #define RLEFT		01
141 #define RRIGHT		02
142 #define RESC1		04
143 #define RESC2		010
144 #define RESC3		020
145 #define RDEST		040
146 #define RESCC		04000
147 #define RNOP		010000	/* DANGER: can cause loops.. */
148 
149 /* needs */
150 #define NASL		0x0001	/* may share left register */
151 #define NASR		0x0002	/* may share right register */
152 #define NAREG		0x0004
153 #define NACOUNT		0x000c
154 #define NBSL		0x0010
155 #define NBSR		0x0020
156 #define NBREG		0x0040
157 #define NBCOUNT		0x00c0
158 #define	NCSL		0x0100
159 #define	NCSR		0x0200
160 #define	NCREG		0x0400
161 #define	NCCOUNT		0x0c00
162 #define NTEMP		0x1000
163 #define NTMASK		0x3000
164 #define NSPECIAL	0x4000	/* need special register treatment */
165 #define REWRITE		0x8000
166 #define	NDSL		0x00010000	/* Above 16 bit */
167 #define	NDSR		0x00020000	/* Above 16 bit */
168 #define	NDREG		0x00040000	/* Above 16 bit */
169 #define	NDCOUNT		0x000c0000
170 #define	NESL		0x00100000	/* Above 16 bit */
171 #define	NESR		0x00200000	/* Above 16 bit */
172 #define	NEREG		0x00400000	/* Above 16 bit */
173 #define	NECOUNT		0x00c00000
174 #define	NFSL		0x01000000	/* Above 16 bit */
175 #define	NFSR		0x02000000	/* Above 16 bit */
176 #define	NFREG		0x04000000	/* Above 16 bit */
177 #define	NFCOUNT		0x0c000000
178 #define	NGSL		0x10000000	/* Above 16 bit */
179 #define	NGSR		0x20000000	/* Above 16 bit */
180 #undef	NGREG	/* XXX - linux exposes NGREG to public */
181 #define	NGREG		0x40000000	/* Above 16 bit */
182 #define	NGCOUNT		0xc0000000
183 
184 /* special treatment */
185 #define	NLEFT		(0001)	/* left leg register (moveadd) */
186 #define	NOLEFT		(0002)	/* avoid regs for left (addedge) */
187 #define	NRIGHT		(0004)	/* right leg register */
188 #define	NORIGHT		(0010)	/* avoid reg for right */
189 #define	NEVER		(0020)	/* registers trashed (addalledges) */
190 #define	NRES		(0040)	/* result register (moveadd) */
191 #define	NMOVTO		(0100)	/* move between classes */
192 
193 
194 #define MUSTDO		010000	/* force register requirements */
195 #define NOPREF		020000	/* no preference for register assignment */
196 
197 #define	isreg(p)	(p->n_op == REG || p->n_op == TEMP)
198 
199 extern	int fregs;
200 
201 /* code tables */
202 extern	struct optab {
203 	int	op;
204 	int	visit;
205 	int	lshape;
206 	int	ltype;
207 	int	rshape;
208 	int	rtype;
209 	int	needs;
210 	int	rewrite;
211 	char	*cstring;
212 } table[];
213 
214 /* Special needs for register allocations */
215 struct rspecial {
216 	int op, num;
217 #if 0
218 	int left;	/* left leg register */
219 	int noleft;	/* avoid regs for left */
220 	int right;	/* right leg register */
221 	int noright;	/* avoid right leg register */
222 	int *rmask;	/* array of destroyed registers */
223 	int res;	/* Result ends up here */
224 //	void (*rew)(struct optab *, NODE *);	/* special rewrite */
225 #endif
226 };
227 
228 struct p2env;
229 #define	NRESC 4
230 extern	NODE resc[];
231 extern	int p2autooff, p2maxautooff;
232 
233 extern	NODE
234 	*talloc(void),
235 	*eread(void),
236 	*mklnode(int, CONSZ, int, TWORD),
237 	*mkbinode(int, NODE *, NODE *, TWORD),
238 	*mkunode(int, NODE *, int, TWORD),
239 	*getlr(NODE *p, int);
240 
241 void eoftn(struct interpass_prolog *);
242 void prologue(struct interpass_prolog *);
243 void e2print(NODE *p, int down, int *a, int *b);
244 void myoptim(struct interpass *);
245 void cbgen(int op, int label);
246 int match(NODE *p, int cookie);
247 int acceptable(struct optab *);
248 int special(NODE *, int);
249 int setasg(NODE *, int);
250 int setuni(NODE *, int);
251 int sucomp(NODE *);
252 int nsucomp(NODE *);
253 int setorder(NODE *);
254 int geninsn(NODE *, int cookie);
255 void adrput(FILE *, NODE *);
256 void comperr(char *str, ...);
257 void genregs(NODE *p);
258 void ngenregs(struct p2env *);
259 NODE *store(NODE *);
260 struct interpass *ipnode(NODE *);
261 void deflab(int);
262 void rmove(int, int, TWORD);
263 int rspecial(struct optab *, int);
264 struct rspecial *nspecial(struct optab *q);
265 void printip(struct interpass *pole);
266 int findops(NODE *p, int);
267 int findasg(NODE *p, int);
268 int finduni(NODE *p, int);
269 int findumul(NODE *p, int);
270 int findleaf(NODE *p, int);
271 int relops(NODE *p);
272 #ifdef FINDMOPS
273 int findmops(NODE *p, int);
274 int treecmp(NODE *p1, NODE *p2);
275 #endif
276 void offstar(NODE *p, int shape);
277 int gclass(TWORD);
278 void lastcall(NODE *);
279 void myreader(struct interpass *pole);
280 int oregok(NODE *p, int sharp);
281 void myormake(NODE *);
282 int *livecall(NODE *);
283 void prtreg(NODE *);
284 char *prcook(int);
285 int myxasm(struct interpass *ip, NODE *p);
286 int xasmcode(char *s);
287 int freetemp(int k);
288 NODE *storenode(TWORD t, int k);
289 void storemod(NODE *, int k, int reg);
290 int rewfld(NODE *p);
291 void canon(NODE *);
292 void mycanon(NODE *);
293 void oreg2(NODE *p, void *);
294 int shumul(NODE *p, int);
295 NODE *deluseless(NODE *p);
296 int getlab2(void);
297 int tshape(NODE *, int);
298 void conput(FILE *, NODE *);
299 int shtemp(NODE *p);
300 int ttype(TWORD t, int tword);
301 void expand(NODE *, int, char *);
302 void hopcode(int, int);
303 void adrcon(CONSZ);
304 void zzzcode(NODE *, int);
305 void insput(NODE *);
306 void upput(NODE *, int);
307 int tlen(NODE *p);
308 int setbin(NODE *);
309 int notoff(TWORD, int, CONSZ, char *);
310 int fldexpand(NODE *, int, char **);
311 void p2tree(NODE *p);
312 int flshape(NODE *p);
313 int ncnt(int needs);
314 
315 extern	char *rnames[];
316 
317 extern int classmask(int), tclassmask(int);
318 extern void cmapinit(void);
319 extern int aliasmap(int adjclass, int regnum);
320 extern int regK[];
321 #define	CLASSA	1
322 #define	CLASSB	2
323 #define	CLASSC	3
324 #define	CLASSD	4
325 #define	CLASSE	5
326 #define	CLASSF	6
327 #define	CLASSG	7
328 
329 /* used when parsing xasm codes */
330 #define	XASMVAL(x)	((x) & 0377)		/* get val from codeword */
331 #define	XASMVAL1(x)	(((x) >> 16) & 0377)	/* get val from codeword */
332 #define	XASMVAL2(x)	(((x) >> 24) & 0377)	/* get val from codeword */
333 #define	XASMASG		0x100	/* = */
334 #define	XASMCONSTR	0x200	/* & */
335 #define	XASMINOUT	0x400	/* + */
336 #define XASMALL		(XASMASG|XASMCONSTR|XASMINOUT)
337 #define	XASMISINP(cw)	(((cw) & XASMASG) == 0)		/* input operand */
338 #define	XASMISOUT(cw)	((cw) & (XASMASG|XASMINOUT))	/* output operand */
339 
340 /* routines to handle double indirection */
341 #ifdef R2REGS
342 void makeor2(NODE *p, NODE *q, int, int);
343 int base(NODE *);
344 int offset(NODE *p, int);
345 #endif
346 
347 extern	int lineno;
348 extern	int ndebug;
349 extern	int b2debug, c2debug, e2debug, f2debug, g2debug, o2debug;
350 extern	int r2debug, s2debug, t2debug, u2debug, x2debug;
351 
352 extern	int dope[];	/* a vector containing operator information */
353 extern	char *opst[];	/* a vector containing names for ops */
354 
355 #ifdef PCC_DEBUG
356 
357 static inline int
optype(int o)358 optype(int o)
359 {
360 	if (o >= MAXOP+1)
361 		cerror("optype");
362 	return (dope[o]&TYFLG);
363 }
364 
365 static inline int
asgop(int o)366 asgop(int o)
367 {
368 	if (o >= MAXOP+1)
369 		cerror("asgop");
370 	return (dope[o]&ASGFLG);
371 }
372 
373 static inline int
logop(int o)374 logop(int o)
375 {
376 	if (o >= MAXOP+1)
377 		cerror("logop");
378 	return (dope[o]&LOGFLG);
379 }
380 
381 static inline int
callop(int o)382 callop(int o)
383 {
384 	if (o >= MAXOP+1)
385 		cerror("callop");
386 	return (dope[o]&CALLFLG);
387 }
388 
389 #else
390 
391 #define optype(o)	(dope[o]&TYFLG)
392 #define asgop(o)	(dope[o]&ASGFLG)
393 #define logop(o)	(dope[o]&LOGFLG)
394 #define callop(o)	(dope[o]&CALLFLG)
395 
396 #endif
397 
398 	/* macros for doing double indexing */
399 #define R2PACK(x,y,z)	(0200*((x)+1)+y+040000*z)
400 #define R2UPK1(x)	((((x)>>7)-1)&0177)
401 #define R2UPK2(x)	((x)&0177)
402 #define R2UPK3(x)	(x>>14)
403 #define R2TEST(x)	((x)>=0200)
404 
405 /*
406  * Layout of findops() return value:
407  *      bit 0 whether left shall go into a register.
408  *      bit 1 whether right shall go into a register.
409  *      bit 2 entry is only used for side effects.
410  *      bit 3 if condition codes are used.
411  *
412  * These values should be synced with FOREFF/FORCC.
413  */
414 #define LREG		001
415 #define RREG		002
416 #define	RVEFF		004
417 #define	RVCC		010
418 #define DORIGHT		020
419 #define	SCLASS(v,x)	((v) |= ((x) << 5))
420 #define	TCLASS(x)	(((x) >> 5) & 7)
421 #define	TBSH		8
422 #define TBLIDX(idx)	((idx) >> TBSH)
423 #define MKIDX(tbl,mod)	(((tbl) << TBSH) | (mod))
424 
425 #ifndef	BREGS
426 #define	BREGS	0
427 #define	TBREGS	0
428 #endif
429 #define	REGBIT(x) (1 << (x))
430 #ifndef PERMTYPE
431 #define	PERMTYPE(a)	(INT)
432 #endif
433 
434 /* Flags for the dataflow code */
435 /* do the live/dead analysis */
436 #define DO_LIVEDEAD  0x01
437 /* compute avail expressions */
438 #define DO_AVAILEXPR 0x02
439 /* Do an update on the live/dead. One variable only */
440 #define DO_UPDATELD  0x04
441 /* Do an update on available expressions, one variable has changed */
442 #define DO_UPDATEEX  0x08
443 
444 void emit(struct interpass *);
445 void optimize(struct p2env *);
446 
447 struct basicblock {
448 	DLIST_ENTRY(basicblock) bbelem;
449 	SLIST_HEAD(, cfgnode) parents;	/* CFG - parents to this node */
450 	SLIST_HEAD(, cfgnode) child;	/* Children, usually max 2 of them */
451 	int bbnum;	/* this basic block number */
452 	unsigned int dfnum; /* DFS-number */
453 	unsigned int dfparent; /* Parent in DFS */
454 	unsigned int semi;
455 	unsigned int ancestor;
456 	unsigned int idom;
457 	unsigned int samedom;
458 	bittype *bucket;
459 	bittype *df;
460 	bittype *dfchildren;
461 	bittype *Aorig;
462 	bittype *Aphi;
463 	SLIST_HEAD(, phiinfo) phi;
464 
465 	bittype *gen, *killed, *in, *out;	/* Liveness analysis */
466 
467 	struct interpass *first; /* first element of basic block */
468 	struct interpass *last;  /* last element of basic block */
469 };
470 
471 struct labelinfo {
472 	struct basicblock **arr;
473 	int size;
474 	unsigned int low;
475 };
476 
477 struct bblockinfo {
478 	int size;
479 	struct basicblock **arr;
480 };
481 
482 struct varinfo {
483 	struct pvarinfo **arr;
484 	SLIST_HEAD(, varstack) *stack;
485 	int size;
486 	int low;
487 };
488 
489 struct pvarinfo {
490 	struct pvarinfo *next;
491 	struct basicblock *bb;
492 	TWORD n_type;
493 };
494 
495 struct varstack {
496 	SLIST_ENTRY(varstack) varstackelem;
497 	int tmpregno;
498 };
499 
500 
501 struct cfgnode {
502 	SLIST_ENTRY(cfgnode) cfgelem;
503 	SLIST_ENTRY(cfgnode) chld;
504 	struct basicblock *bblock;
505 };
506 
507 struct phiinfo {
508 	SLIST_ENTRY(phiinfo) phielem;
509 	int tmpregno;
510 	int newtmpregno;
511 	TWORD n_type;
512 	int size;
513 	int *intmpregno;
514 };
515 
516 /*
517  * Description of the pass2 environment.
518  * There will be only one of these structs.  It is used to keep
519  * all state descriptions during the compilation of a function
520  * in one place.
521  */
522 struct p2env {
523 	struct interpass ipole;			/* all statements */
524 	struct interpass_prolog *ipp, *epp;	/* quick references */
525 	struct bblockinfo bbinfo;
526 	struct labelinfo labinfo;
527 	struct basicblock bblocks;
528 	int nbblocks;
529 };
530 
531 extern struct p2env p2env;
532 
533 /*
534  * C compiler second pass extra defines.
535  */
536 #define PHI (MAXOP + 1)		/* Used in SSA trees */
537