xref: /netbsd/external/bsd/pcc/dist/pcc/cc/ccom/pass1.h (revision 6550d01e)
1 /*	Id: pass1.h,v 1.195 2010/06/02 10:39:51 ragge Exp 	*/
2 /*	$NetBSD: pass1.h,v 1.1.1.3 2010/06/03 18:57:41 plunky Exp $	*/
3 /*
4  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * Redistributions of source code and documentation must retain the above
11  * copyright notice, this list of conditions and the following disclaimer.
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditionsand the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * All advertising materials mentioning features or use of this software
16  * must display the following acknowledgement:
17  * 	This product includes software developed or owned by Caldera
18  *	International, Inc.
19  * Neither the name of Caldera International, Inc. nor the names of other
20  * contributors may be used to endorse or promote products derived from
21  * this software without specific prior written permission.
22  *
23  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
28  * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
32  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #include "config.h"
38 
39 #include <sys/types.h>
40 #include <stdarg.h>
41 #include <string.h>
42 #ifdef HAVE_STDINT_H
43 #include <stdint.h>
44 #endif
45 
46 #ifndef MKEXT
47 #include "external.h"
48 #else
49 typedef unsigned int bittype; /* XXX - for basicblock */
50 #endif
51 #include "manifest.h"
52 
53 #include "protos.h"
54 #include "ccconfig.h"
55 
56 /*
57  * Storage classes
58  */
59 #define SNULL		0
60 #define AUTO		1
61 #define EXTERN		2
62 #define STATIC		3
63 #define REGISTER	4
64 #define EXTDEF		5
65 /* #define LABEL	6*/
66 /* #define ULABEL	7*/
67 #define MOS		8
68 #define PARAM		9
69 #define STNAME		10
70 #define MOU		11
71 #define UNAME		12
72 #define TYPEDEF		13
73 #define FORTRAN		14
74 #define ENAME		15
75 #define MOE		16
76 #define UFORTRAN 	17
77 #define USTATIC		18
78 
79 	/* field size is ORed in */
80 #define FIELD		0100
81 #define FLDSIZ		077
82 extern	char *scnames(int);
83 
84 /*
85  * Symbol table flags
86  */
87 #define	SNORMAL		0
88 #define	STAGNAME	01
89 #define	SLBLNAME	02
90 #define	SMOSNAME	03
91 #define	SSTRING		04
92 #define	NSTYPES		05
93 #define	SMASK		07
94 
95 /* #define SSET		00010 */
96 /* #define SREF		00020 */
97 #define SNOCREAT	00040	/* don't create a symbol in lookup() */
98 #define STEMP		00100	/* Allocate symtab from temp or perm mem */
99 #define	SDYNARRAY	00200	/* symbol is dynamic array on stack */
100 #define	SINLINE		00400	/* function is of type inline */
101 #define	STNODE		01000	/* symbol shall be a temporary node */
102 #define	SASG		04000	/* symbol is assigned to already */
103 #define	SLOCAL1		010000
104 #define	SLOCAL2		020000
105 #define	SLOCAL3		040000
106 
107 	/* alignment of initialized quantities */
108 #ifndef AL_INIT
109 #define	AL_INIT ALINT
110 #endif
111 
112 struct rstack;
113 struct symtab;
114 union arglist;
115 #ifdef GCC_COMPAT
116 struct gcc_attr_pack;
117 #endif
118 
119 /*
120  * Dimension/prototype information.
121  * 	ddim > 0 holds the dimension of an array.
122  *	ddim < 0 is a dynamic array and refers to a tempnode.
123  */
124 union dimfun {
125 	int	ddim;		/* Dimension of an array */
126 	union arglist *dfun;	/* Prototype index */
127 };
128 
129 /*
130  * Struct/union/enum definition.
131  * The first element (size) is used for other types as well.
132  */
133 struct suedef {
134 	int	suesize;	/* Size of the struct */
135 	int	suealign;	/* Alignment of this struct */
136 	struct	symtab *suem;	/* the list of elements in struct/unions */
137 	struct	suedef *suep;	/* parent, if any */
138 #ifdef GCC_COMPAT
139 	struct	gcc_attr_pack *suega;
140 #endif
141 };
142 #define GETSUE(x, y)	for (x = y; x->suep; x = x->suep)
143 
144 /*
145  * Argument list member info when storing prototypes.
146  */
147 union arglist {
148 	TWORD type;
149 	union dimfun *df;
150 	struct suedef *sue;
151 };
152 #define TNULL		INCREF(FARG) /* pointer to FARG -- impossible type */
153 #define TELLIPSIS 	INCREF(INCREF(FARG))
154 
155 /*
156  * Symbol table definition.
157  */
158 struct	symtab {
159 	struct	symtab *snext;	/* link to other symbols in the same scope */
160 	int	soffset;	/* offset or value */
161 	char	sclass;		/* storage class */
162 	char	slevel;		/* scope level */
163 	short	sflags;		/* flags, see below */
164 	char	*sname;		/* Symbol name */
165 	char	*soname;	/* Written-out name */
166 	TWORD	stype;		/* type word */
167 	TWORD	squal;		/* qualifier word */
168 	union	dimfun *sdf;	/* ptr to the dimension/prototype array */
169 	struct	suedef *ssue;	/* ptr to the definition table */
170 };
171 
172 #define	MKSUE(type)  &btdims[type]
173 extern struct suedef btdims[];
174 
175 /*
176  * External definitions
177  */
178 struct swents {			/* switch table */
179 	struct swents *next;	/* Next struct in linked list */
180 	CONSZ	sval;		/* case value */
181 	int	slab;		/* associated label */
182 };
183 int mygenswitch(int, TWORD, struct swents **, int);
184 
185 extern	int blevel;
186 extern	int instruct, got_type;
187 extern	int oldstyle;
188 extern	int oflag;
189 
190 extern	int lineno, nerrors;
191 
192 extern	char *ftitle;
193 extern	struct symtab *cftnsp;
194 extern	int autooff, maxautooff, argoff, strucoff;
195 extern	int brkflag;
196 
197 extern	OFFSZ inoff;
198 
199 extern	int reached;
200 extern	int isinlining;
201 extern	int xinline;
202 
203 extern	int sdebug, idebug, pdebug;
204 
205 /* various labels */
206 extern	int brklab;
207 extern	int contlab;
208 extern	int flostat;
209 extern	int retlab;
210 
211 /* pragma globals */
212 extern int pragma_allpacked, pragma_packed, pragma_aligned;
213 extern char *pragma_renamed;
214 
215 /*
216  * Flags used in the (elementary) flow analysis ...
217  */
218 #define FBRK		02
219 #define FCONT		04
220 #define FDEF		010
221 #define FLOOP		020
222 
223 /*	mark an offset which is undefined */
224 
225 #define NOOFFSET	(-10201)
226 
227 /* declarations of various functions */
228 extern	NODE
229 	*buildtree(int, NODE *, NODE *r),
230 	*mkty(unsigned, union dimfun *, struct suedef *),
231 	*rstruct(char *, int),
232 	*dclstruct(struct rstack *),
233 	*strend(int gtype, char *),
234 	*tymerge(NODE *, NODE *),
235 	*stref(NODE *),
236 	*offcon(OFFSZ, TWORD, union dimfun *, struct suedef *),
237 	*bcon(int),
238 	*xbcon(CONSZ, struct symtab *, TWORD),
239 	*bpsize(NODE *),
240 	*convert(NODE *, int),
241 	*pconvert(NODE *),
242 	*oconvert(NODE *),
243 	*ptmatch(NODE *),
244 	*tymatch(NODE *),
245 	*makety(NODE *, TWORD, TWORD, union dimfun *, struct suedef *),
246 	*block(int, NODE *, NODE *, TWORD, union dimfun *, struct suedef *),
247 	*doszof(NODE *),
248 	*talloc(void),
249 	*optim(NODE *),
250 	*clocal(NODE *),
251 	*ccopy(NODE *),
252 	*tempnode(int, TWORD, union dimfun *, struct suedef *),
253 	*eve(NODE *),
254 	*doacall(struct symtab *, NODE *, NODE *);
255 NODE	*intprom(NODE *);
256 OFFSZ	tsize(TWORD, union dimfun *, struct suedef *),
257 	psize(NODE *);
258 NODE *	typenode(NODE *new);
259 void	spalloc(NODE *, NODE *, OFFSZ);
260 char	*exname(char *);
261 NODE	*floatcon(char *);
262 NODE	*fhexcon(char *);
263 NODE	*bdty(int op, ...);
264 extern struct rstack *rpole;
265 
266 int oalloc(struct symtab *, int *);
267 void deflabel(char *);
268 void gotolabel(char *);
269 unsigned int esccon(char **);
270 void inline_start(struct symtab *);
271 void inline_end(void);
272 void inline_addarg(struct interpass *);
273 void inline_ref(struct symtab *);
274 void inline_prtout(void);
275 void inline_args(struct symtab **, int);
276 NODE *inlinetree(struct symtab *, NODE *, NODE *);
277 void ftnarg(NODE *);
278 struct rstack *bstruct(char *, int, NODE *);
279 void moedef(char *);
280 void beginit(struct symtab *);
281 void simpleinit(struct symtab *, NODE *);
282 struct symtab *lookup(char *, int);
283 struct symtab *getsymtab(char *, int);
284 char *addstring(char *);
285 char *addname(char *);
286 void symclear(int);
287 struct symtab *hide(struct symtab *);
288 void soumemb(NODE *, char *, int);
289 int talign(unsigned int, struct suedef *);
290 void bfcode(struct symtab **, int);
291 int chkftn(union arglist *, union arglist *);
292 void branch(int);
293 void cbranch(NODE *, NODE *);
294 void extdec(struct symtab *);
295 void defzero(struct symtab *);
296 int falloc(struct symtab *, int, NODE *);
297 TWORD ctype(TWORD);
298 void ninval(CONSZ, int, NODE *);
299 void infld(CONSZ, int, CONSZ);
300 void zbits(CONSZ, int);
301 void instring(struct symtab *);
302 void inwstring(struct symtab *);
303 void plabel(int);
304 void bjobcode(void);
305 void ejobcode(int);
306 void calldec(NODE *, NODE *);
307 int cisreg(TWORD);
308 char *tmpsprintf(char *, ...);
309 char *tmpvsprintf(char *, va_list);
310 void asginit(NODE *);
311 void desinit(NODE *);
312 void endinit(void);
313 void endictx(void);
314 void sspinit(void);
315 void sspstart(void);
316 void sspend(void);
317 void ilbrace(void);
318 void irbrace(void);
319 CONSZ scalinit(NODE *);
320 void p1print(char *, ...);
321 char *copst(int);
322 int cdope(int);
323 void myp2tree(NODE *);
324 void lcommprint(void);
325 void lcommdel(struct symtab *);
326 NODE *funcode(NODE *);
327 struct symtab *enumhd(char *);
328 NODE *enumdcl(struct symtab *);
329 NODE *enumref(char *);
330 CONSZ icons(NODE *);
331 int mypragma(char **);
332 void fixdef(struct symtab *);
333 int cqual(TWORD, TWORD);
334 void defloc(struct symtab *);
335 int fldchk(int);
336 int nncon(NODE *);
337 void cunput(char);
338 NODE *nametree(struct symtab *sp);
339 void *inlalloc(int size);
340 void pass1_lastchance(struct interpass *);
341 void fldty(struct symtab *p);
342 int getlab(void);
343 struct suedef *sueget(struct suedef *p);
344 void complinit(void);
345 NODE *structref(NODE *p, int f, char *name);
346 NODE *cxop(int op, NODE *l, NODE *r);
347 NODE *imop(int op, NODE *l, NODE *r);
348 NODE *cxelem(int op, NODE *p);
349 NODE *cxconj(NODE *p);
350 NODE *cxret(NODE *p, NODE *q);
351 NODE *cast(NODE *p, TWORD t, TWORD q);
352 NODE *ccast(NODE *p, TWORD t, TWORD u, union dimfun *df, struct suedef *sue);
353 
354 NODE *builtin_check(NODE *f, NODE *a);
355 
356 
357 #ifdef SOFTFLOAT
358 typedef struct softfloat SF;
359 SF soft_neg(SF);
360 SF soft_cast(CONSZ v, TWORD);
361 SF soft_plus(SF, SF);
362 SF soft_minus(SF, SF);
363 SF soft_mul(SF, SF);
364 SF soft_div(SF, SF);
365 int soft_cmp_eq(SF, SF);
366 int soft_cmp_ne(SF, SF);
367 int soft_cmp_ge(SF, SF);
368 int soft_cmp_gt(SF, SF);
369 int soft_cmp_le(SF, SF);
370 int soft_cmp_lt(SF, SF);
371 int soft_isz(SF);
372 CONSZ soft_val(SF);
373 #define FLOAT_NEG(sf)		soft_neg(sf)
374 #define	FLOAT_CAST(v,t)		soft_cast(v, t)
375 #define	FLOAT_PLUS(x1,x2)	soft_plus(x1, x2)
376 #define	FLOAT_MINUS(x1,x2)	soft_minus(x1, x2)
377 #define	FLOAT_MUL(x1,x2)	soft_mul(x1, x2)
378 #define	FLOAT_DIV(x1,x2)	soft_div(x1, x2)
379 #define	FLOAT_ISZERO(sf)	soft_isz(sf)
380 #define	FLOAT_VAL(sf)		soft_val(sf)
381 #define FLOAT_EQ(x1,x2)		soft_cmp_eq(x1, x2)
382 #define FLOAT_NE(x1,x2)		soft_cmp_ne(x1, x2)
383 #define FLOAT_GE(x1,x2)		soft_cmp_ge(x1, x2)
384 #define FLOAT_GT(x1,x2)		soft_cmp_gt(x1, x2)
385 #define FLOAT_LE(x1,x2)		soft_cmp_le(x1, x2)
386 #define FLOAT_LT(x1,x2)		soft_cmp_lt(x1, x2)
387 #else
388 #define	FLOAT_NEG(p)		-(p)
389 #define	FLOAT_CAST(p,v)		(ISUNSIGNED(v) ? \
390 		(long double)(U_CONSZ)(p) : (long double)(CONSZ)(p))
391 #define	FLOAT_PLUS(x1,x2)	(x1) + (x2)
392 #define	FLOAT_MINUS(x1,x2)	(x1) - (x2)
393 #define	FLOAT_MUL(x1,x2)	(x1) * (x2)
394 #define	FLOAT_DIV(x1,x2)	(x1) / (x2)
395 #define	FLOAT_ISZERO(p)		(p) == 0.0
396 #define FLOAT_VAL(p)		(CONSZ)(p)
397 #define FLOAT_EQ(x1,x2)		(x1) == (x2)
398 #define FLOAT_NE(x1,x2)		(x1) != (x2)
399 #define FLOAT_GE(x1,x2)		(x1) >= (x2)
400 #define FLOAT_GT(x1,x2)		(x1) > (x2)
401 #define FLOAT_LE(x1,x2)		(x1) <= (x2)
402 #define FLOAT_LT(x1,x2)		(x1) < (x2)
403 #endif
404 
405 #ifdef GCC_COMPAT
406 enum {	GCC_ATYP_NONE,
407 
408 	/* type attributes */
409 	GCC_ATYP_ALIGNED,
410 	GCC_ATYP_PACKED,
411 	GCC_ATYP_SECTION,
412 	GCC_ATYP_TRANSP_UNION,
413 	GCC_ATYP_UNUSED,
414 	GCC_ATYP_DEPRECATED,
415 	GCC_ATYP_MAYALIAS,
416 
417 	/* variable attributes */
418 	GCC_ATYP_MODE,
419 
420 	/* function attributes */
421 	GCC_ATYP_NORETURN,
422 	GCC_ATYP_FORMAT,
423 	GCC_ATYP_NONNULL,
424 	GCC_ATYP_SENTINEL,
425 	GCC_ATYP_WEAK,
426 	GCC_ATYP_FORMATARG,
427 	GCC_ATYP_GNU_INLINE,
428 	GCC_ATYP_MALLOC,
429 	GCC_ATYP_NOTHROW,
430 	GCC_ATYP_CONST,
431 	GCC_ATYP_PURE,
432 	GCC_ATYP_CONSTRUCTOR,
433 	GCC_ATYP_DESTRUCTOR,
434 	GCC_ATYP_VISIBILITY,
435 	GCC_ATYP_STDCALL,
436 	GCC_ATYP_CDECL,
437 	GCC_ATYP_WARN_UNUSED_RESULT,
438 	GCC_ATYP_USED,
439 
440 	/* other stuff */
441 	GCC_ATYP_BOUNDED,	/* OpenBSD extra boundary checks */
442 	ATTR_COMPLEX,		/* Internal definition of complex */
443 
444 	GCC_ATYP_MAX
445 };
446 
447 union gcc_aarg {
448 	int iarg;
449 	char *sarg;
450 };
451 
452 struct gcc_attrib {
453 	int atype;
454 	union gcc_aarg a1, a2, a3;
455 };
456 
457 struct gcc_attr_pack {
458 	int num;
459 	struct gcc_attrib ga[];
460 };
461 
462 typedef struct gcc_attr_pack gcc_ap_t;
463 
464 void gcc_init(void);
465 int gcc_keyword(char *, NODE **);
466 gcc_ap_t *gcc_attr_parse(NODE *);
467 void gcc_tcattrfix(NODE *, NODE *);
468 struct gcc_attrib *gcc_get_attr(struct suedef *, int);
469 void dump_attr(gcc_ap_t *gap);
470 #endif
471 
472 #ifdef STABS
473 void stabs_init(void);
474 void stabs_file(char *);
475 void stabs_efile(char *);
476 void stabs_line(int);
477 void stabs_rbrac(int);
478 void stabs_lbrac(int);
479 void stabs_func(struct symtab *);
480 void stabs_newsym(struct symtab *);
481 void stabs_chgsym(struct symtab *);
482 void stabs_struct(struct symtab *, struct suedef *);
483 #endif
484 
485 #ifndef CHARCAST
486 /* to make character constants into character connstants */
487 /* this is a macro to defend against cross-compilers, etc. */
488 #define CHARCAST(x) (char)(x)
489 #endif
490 
491 /* sometimes int is smaller than pointers */
492 #if SZPOINT(CHAR) <= SZINT
493 #define INTPTR  INT
494 #elif SZPOINT(CHAR) <= SZLONG
495 #define INTPTR  LONG
496 #elif SZPOINT(CHAR) <= SZLONGLONG
497 #define INTPTR  LONGLONG
498 #else
499 #error int size unknown
500 #endif
501 
502 /*
503  * C compiler first pass extra defines.
504  */
505 #define	QUALIFIER	(MAXOP+1)
506 #define	CLASS		(MAXOP+2)
507 #define	RB		(MAXOP+3)
508 #define	DOT		(MAXOP+4)
509 #define	ELLIPSIS	(MAXOP+5)
510 #define	TYPE		(MAXOP+6)
511 #define	LB		(MAXOP+7)
512 #define	COMOP		(MAXOP+8)
513 #define	QUEST		(MAXOP+9)
514 #define	COLON		(MAXOP+10)
515 #define	ANDAND		(MAXOP+11)
516 #define	OROR		(MAXOP+12)
517 #define	NOT		(MAXOP+13)
518 #define	CAST		(MAXOP+14)
519 #define	STRING		(MAXOP+15)
520 
521 /* The following must be in the same order as their NOASG counterparts */
522 #define	PLUSEQ		(MAXOP+16)
523 #define	MINUSEQ		(MAXOP+17)
524 #define	DIVEQ		(MAXOP+18)
525 #define	MODEQ		(MAXOP+19)
526 #define	MULEQ		(MAXOP+20)
527 #define	ANDEQ		(MAXOP+21)
528 #define	OREQ		(MAXOP+22)
529 #define	EREQ		(MAXOP+23)
530 #define	LSEQ		(MAXOP+24)
531 #define	RSEQ		(MAXOP+25)
532 
533 #define	UNASG		(-(PLUSEQ-PLUS))+
534 
535 #define INCR		(MAXOP+26)
536 #define DECR		(MAXOP+27)
537 #define SZOF		(MAXOP+28)
538 #define CLOP		(MAXOP+29)
539 #define ATTRIB		(MAXOP+30)
540 #define XREAL		(MAXOP+31)
541 #define XIMAG		(MAXOP+32)
542 
543 
544 /*
545  * The following types are only used in pass1.
546  */
547 #define SIGNED		(MAXTYPES+1)
548 #define BOOL		(MAXTYPES+2)
549 #define	FIMAG		(MAXTYPES+3)
550 #define	IMAG		(MAXTYPES+4)
551 #define	LIMAG		(MAXTYPES+5)
552 #define	FCOMPLEX	(MAXTYPES+6)
553 #define	COMPLEX		(MAXTYPES+7)
554 #define	LCOMPLEX	(MAXTYPES+8)
555 #define	ENUMTY		(MAXTYPES+9)
556 
557 #define	ISFTY(x)	((x) >= FLOAT && (x) <= LDOUBLE)
558 #define	ISCTY(x)	((x) >= FCOMPLEX && (x) <= LCOMPLEX)
559 #define	ISITY(x)	((x) >= FIMAG && (x) <= LIMAG)
560 #define ANYCX(p) (p->n_type == STRTY && gcc_get_attr(p->n_sue, ATTR_COMPLEX))
561 
562 #define coptype(o)	(cdope(o)&TYFLG)
563 #define clogop(o)	(cdope(o)&LOGFLG)
564 #define casgop(o)	(cdope(o)&ASGFLG)
565 
566