1 /* Id: pass1.h,v 1.19 2015/11/24 17:30:20 ragge Exp */ 2 /* $NetBSD: pass1.h,v 1.1.1.4 2016/02/09 20:28:59 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 #include <stdlib.h> 46 47 #ifndef MKEXT 48 #include "external.h" 49 #else 50 typedef unsigned int bittype; /* XXX - for basicblock */ 51 #endif 52 #include "manifest.h" 53 #include "softfloat.h" 54 55 /* 56 * Storage classes 57 */ 58 #define SNULL 0 59 #define AUTO 1 60 #define EXTERN 2 61 #define STATIC 3 62 #define REGISTER 4 63 #define EXTDEF 5 64 /* #define LABEL 6*/ 65 /* #define ULABEL 7*/ 66 #define MOS 8 67 #define PARAM 9 68 #define STNAME 10 69 #define MOU 11 70 #define UNAME 12 71 #define TYPEDEF 13 72 /* #define FORTRAN 14 */ 73 #define ENAME 15 74 #define MOE 16 75 /* #define UFORTRAN 17 */ 76 #define USTATIC 18 77 #define MAXSTCL 20 78 79 /* field size is ORed in */ 80 #define FIELD 0200 81 #define FLDSIZ 0177 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 STLS 00010 /* Thread Local Support variable */ 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 SBUILTIN 02000 /* this is a builtin function */ 103 #define SASG 04000 /* symbol is assigned to already */ 104 #define SLOCAL1 010000 105 #define SLOCAL2 020000 106 #define SLOCAL3 040000 107 108 /* alignment of initialized quantities */ 109 #ifndef AL_INIT 110 #define AL_INIT ALINT 111 #endif 112 113 struct rstack; 114 struct symtab; 115 union arglist; 116 #ifdef GCC_COMPAT 117 struct gcc_attr_pack; 118 #endif 119 120 struct namespace; 121 122 /* 123 * Dimension/prototype information. 124 * ddim > 0 holds the dimension of an array. 125 * ddim < 0 is a dynamic array and refers to a tempnode. 126 * ...unless: 127 * ddim == NOOFFSET, an array without dimenston, "[]" 128 * ddim == -1, dynamic array while building before defid. 129 */ 130 union dimfun { 131 int ddim; /* Dimension of an array */ 132 union arglist *dfun; /* Prototype index */ 133 }; 134 135 /* 136 * Argument list member info when storing prototypes. 137 */ 138 union arglist { 139 TWORD type; 140 union dimfun *df; 141 struct attr *sap; 142 }; 143 #define TNULL INCREF(FARG) /* pointer to FARG -- impossible type */ 144 #define TELLIPSIS INCREF(INCREF(FARG)) 145 146 /* 147 * Symbol table definition. 148 */ 149 struct symtab { 150 struct symtab *snext; /* link to other symbols in the same scope */ 151 struct symtab *sdown; /* link to parent class */ 152 struct symtab *sup; /* link to child class */ 153 int soffset; /* offset or value */ 154 char sclass; /* storage class */ 155 char slevel; /* scope level */ 156 short sflags; /* flags, see below */ 157 char *sname; /* Symbol name */ 158 char *soname; /* Written-out name */ 159 TWORD stype; /* type word */ 160 TWORD squal; /* qualifier word */ 161 union dimfun *sdf; /* ptr to the dimension/prototype array */ 162 struct attr *sap; /* the base type attribute list */ 163 }; 164 165 #define ISSOU(ty) ((ty) == STRTY || (ty) == UNIONTY) 166 167 /* 168 * External definitions 169 */ 170 struct swents { /* switch table */ 171 struct swents *next; /* Next struct in linked list */ 172 CONSZ sval; /* case value */ 173 int slab; /* associated label */ 174 }; 175 int mygenswitch(int, TWORD, struct swents **, int); 176 177 extern int blevel; 178 extern int oldstyle; 179 180 extern int lineno, nerrors; 181 182 extern char *ftitle; 183 extern struct symtab *cftnsp; 184 extern int autooff, maxautooff, argoff; 185 186 extern OFFSZ inoff; 187 188 extern int reached; 189 extern int isinlining; 190 extern int xinline, xgnu89, xgnu99; 191 extern int bdebug, ddebug, edebug, idebug, ndebug; 192 extern int odebug, pdebug, sdebug, tdebug, xdebug; 193 194 /* various labels */ 195 extern int brklab; 196 extern int contlab; 197 extern int flostat; 198 extern int retlab; 199 extern int doing_init, statinit; 200 extern short sztable[]; 201 extern char *astypnames[]; 202 203 /* pragma globals */ 204 extern int pragma_allpacked, pragma_packed, pragma_aligned; 205 extern char *pragma_renamed; 206 207 /* 208 * Flags used in the (elementary) flow analysis ... 209 */ 210 #define FBRK 02 211 #define FCONT 04 212 #define FDEF 010 213 #define FLOOP 020 214 215 /* 216 * Location counters 217 */ 218 #define NOSEG -1 219 #define PROG 0 /* (ro) program segment */ 220 #define DATA 1 /* (rw) data segment */ 221 #define RDATA 2 /* (ro) data segment */ 222 #define LDATA 3 /* (rw) local data */ 223 #define UDATA 4 /* (rw) uninitialized data */ 224 #define STRNG 5 /* (ro) string segment */ 225 #define PICDATA 6 /* (rw) relocatable data segment */ 226 #define PICRDATA 7 /* (ro) relocatable data segment */ 227 #define PICLDATA 8 /* (rw) local relocatable data */ 228 #define TLSDATA 9 /* (rw) TLS data segment */ 229 #define TLSUDATA 10 /* (rw) TLS uninitialized segment */ 230 #define CTORS 11 /* constructor */ 231 #define DTORS 12 /* destructor */ 232 #define NMSEG 13 /* other (named) segment */ 233 234 extern int lastloc; 235 void locctr(int type, struct symtab *sp); 236 void setseg(int type, char *name); 237 void defalign(int al); 238 void symdirec(struct symtab *sp); 239 240 /* mark an offset which is undefined */ 241 242 #define NOOFFSET (-10201) 243 244 /* declarations of various functions */ 245 extern NODE 246 *buildtree(int, NODE *, NODE *r), 247 *mkty(unsigned, union dimfun *, struct attr *), 248 *rstruct(char *, int), 249 *dclstruct(struct rstack *), 250 *strend(int gtype, char *), 251 *tymerge(NODE *, NODE *), 252 *stref(NODE *), 253 #ifdef WORD_ADDRESSED 254 *offcon(OFFSZ, TWORD, union dimfun *, struct attr *), 255 #endif 256 *bcon(int), 257 *xbcon(CONSZ, struct symtab *, TWORD), 258 *bpsize(NODE *), 259 *convert(NODE *, int), 260 *pconvert(NODE *), 261 *oconvert(NODE *), 262 *ptmatch(NODE *), 263 *makety(NODE *, TWORD, TWORD, union dimfun *, struct attr *), 264 *block(int, NODE *, NODE *, TWORD, union dimfun *, struct attr *), 265 *doszof(NODE *), 266 *talloc(void), 267 *optim(NODE *), 268 *clocal(NODE *), 269 *ccopy(NODE *), 270 *tempnode(int, TWORD, union dimfun *, struct attr *), 271 *eve(NODE *), 272 *doacall(struct symtab *, NODE *, NODE *, int); 273 NODE *intprom(NODE *); 274 OFFSZ tsize(TWORD, union dimfun *, struct attr *), 275 psize(NODE *); 276 NODE * typenode(NODE *new); 277 void spalloc(NODE *, NODE *, OFFSZ); 278 char *exname(char *); 279 NODE *floatcon(char *); 280 NODE *fhexcon(char *); 281 NODE *bdty(int op, ...); 282 extern struct rstack *rpole; 283 284 int oalloc(struct symtab *, int *); 285 void deflabel(char *, NODE *); 286 void gotolabel(char *); 287 unsigned int esccon(char **); 288 void inline_start(struct symtab *); 289 void inline_end(void); 290 void inline_addarg(struct interpass *); 291 void inline_ref(struct symtab *); 292 void inline_prtout(void); 293 void inline_args(struct symtab **, int); 294 NODE *inlinetree(struct symtab *, NODE *, NODE *); 295 void ftnarg(NODE *); 296 struct rstack *bstruct(char *, int, NODE *); 297 void moedef(char *); 298 void beginit(struct symtab *); 299 void simpleinit(struct symtab *, NODE *); 300 struct symtab *lookup(char *, int); 301 struct symtab *getsymtab(char *, int); 302 char *addstring(char *); 303 char *addname(char *); 304 void symclear(int); 305 struct symtab *hide(struct symtab *); 306 void soumemb(NODE *, char *, int); 307 int talign(unsigned int, struct attr *); 308 void bfcode(struct symtab **, int); 309 int chkftn(union arglist *, union arglist *); 310 void branch(int); 311 void cbranch(NODE *, NODE *); 312 void extdec(struct symtab *); 313 void defzero(struct symtab *); 314 int falloc(struct symtab *, int, NODE *); 315 TWORD ctype(TWORD); 316 void inval(CONSZ, int, NODE *); 317 int ninval(CONSZ, int, NODE *); 318 void infld(CONSZ, int, CONSZ); 319 void zbits(CONSZ, int); 320 void instring(struct symtab *); 321 void inwstring(struct symtab *); 322 void plabel(int); 323 void bjobcode(void); 324 void ejobcode(int); 325 void calldec(NODE *, NODE *); 326 int cisreg(TWORD); 327 void asginit(NODE *); 328 void desinit(NODE *); 329 void endinit(int); 330 void endictx(void); 331 void sspinit(void); 332 void sspstart(void); 333 void sspend(void); 334 void ilbrace(void); 335 void irbrace(void); 336 CONSZ scalinit(NODE *); 337 void p1print(char *, ...); 338 char *copst(int); 339 int cdope(int); 340 void myp2tree(NODE *); 341 void lcommprint(void); 342 void lcommdel(struct symtab *); 343 NODE *funcode(NODE *); 344 struct symtab *enumhd(char *); 345 NODE *enumdcl(struct symtab *); 346 NODE *enumref(char *); 347 CONSZ icons(NODE *); 348 CONSZ valcast(CONSZ v, TWORD t); 349 int mypragma(char *); 350 char *pragtok(char *); 351 int eat(int); 352 void fixdef(struct symtab *); 353 int cqual(TWORD, TWORD); 354 void defloc(struct symtab *); 355 int fldchk(int); 356 int nncon(NODE *); 357 void cunput(char); 358 NODE *nametree(struct symtab *sp); 359 void *inlalloc(int size); 360 void *blkalloc(int size); 361 void pass1_lastchance(struct interpass *); 362 void fldty(struct symtab *p); 363 int getlab(void); 364 struct suedef *sueget(struct suedef *p); 365 void complinit(void); 366 NODE *structref(NODE *p, int f, char *name); 367 NODE *cxop(int op, NODE *l, NODE *r); 368 NODE *imop(int op, NODE *l, NODE *r); 369 NODE *cxelem(int op, NODE *p); 370 NODE *cxconj(NODE *p); 371 NODE *cxret(NODE *p, NODE *q); 372 NODE *cast(NODE *p, TWORD t, TWORD q); 373 NODE *ccast(NODE *p, TWORD t, TWORD u, union dimfun *df, struct attr *sue); 374 int andable(NODE *); 375 int conval(NODE *, int, NODE *); 376 int ispow2(CONSZ); 377 void defid(NODE *q, int class); 378 void efcode(void); 379 void ecomp(NODE *p); 380 int upoff(int size, int alignment, int *poff); 381 void nidcl(NODE *p, int class); 382 void eprint(NODE *, int, int *, int *); 383 int uclass(int class); 384 int notlval(NODE *); 385 void ecode(NODE *p); 386 void ftnend(void); 387 void dclargs(void); 388 int suemeq(struct attr *s1, struct attr *s2); 389 struct symtab *strmemb(struct attr *ap); 390 int yylex(void); 391 void yyerror(char *); 392 int pragmas_gcc(char *t); 393 NODE *cstknode(TWORD t, union dimfun *df, struct attr *ap); 394 int concast(NODE *p, TWORD t); 395 #ifdef WORD_ADDRESSED 396 #define rmpconv(p) (p) 397 #else 398 NODE *rmpconv(NODE *); 399 #endif 400 NODE *nlabel(int label); 401 int isbuiltin(char *n); 402 char *getexname(struct symtab *); 403 404 enum { ATTR_FIRST = ATTR_MI_MAX + 1, 405 406 /* PCC used attributes */ 407 ATTR_COMPLEX, /* Internal definition of complex */ 408 xxxATTR_BASETYP, /* Internal; see below */ 409 ATTR_QUALTYP, /* Internal; const/volatile, see below */ 410 ATTR_ALIGNED, 411 ATTR_STRUCT, /* Internal; element list */ 412 #define ATTR_MAX ATTR_STRUCT 413 414 ATTR_SONAME, 415 416 #ifdef GCC_COMPAT 417 /* type attributes */ 418 GCC_ATYP_PACKED, 419 GCC_ATYP_SECTION, 420 GCC_ATYP_TRANSP_UNION, 421 GCC_ATYP_UNUSED, 422 GCC_ATYP_DEPRECATED, 423 GCC_ATYP_MAYALIAS, 424 425 /* variable attributes */ 426 GCC_ATYP_MODE, 427 428 /* function attributes */ 429 GCC_ATYP_NORETURN, 430 GCC_ATYP_FORMAT, 431 GCC_ATYP_NONNULL, 432 GCC_ATYP_SENTINEL, 433 GCC_ATYP_WEAK, 434 GCC_ATYP_FORMATARG, 435 GCC_ATYP_GNU_INLINE, 436 GCC_ATYP_MALLOC, 437 GCC_ATYP_NOTHROW, 438 GCC_ATYP_CONST, 439 GCC_ATYP_PURE, 440 GCC_ATYP_CONSTRUCTOR, 441 GCC_ATYP_DESTRUCTOR, 442 GCC_ATYP_VISIBILITY, 443 GCC_ATYP_WARN_UNUSED_RESULT, 444 GCC_ATYP_USED, 445 GCC_ATYP_NO_INSTR_FUN, 446 GCC_ATYP_NOINLINE, 447 GCC_ATYP_ALIAS, 448 GCC_ATYP_WEAKREF, 449 GCC_ATYP_ALLOCSZ, 450 GCC_ATYP_ALW_INL, 451 GCC_ATYP_TLSMODEL, 452 GCC_ATYP_ALIASWEAK, 453 GCC_ATYP_REGPARM, 454 GCC_ATYP_FASTCALL, 455 456 /* other stuff */ 457 GCC_ATYP_BOUNDED, /* OpenBSD extra boundary checks */ 458 459 GCC_ATYP_MAX, 460 #endif 461 #ifdef ATTR_P1_TARGET 462 ATTR_P1_TARGET, 463 #endif 464 ATTR_P1_MAX 465 466 }; 467 468 469 /* 470 #ifdef notdef 471 * ATTR_BASETYP has the following layout: 472 * aa[0].iarg has size 473 * aa[1].iarg has alignment 474 #endif 475 * ATTR_QUALTYP has the following layout: 476 * aa[0].iarg has CON/VOL + FUN/ARY/PTR 477 * Not defined yet... 478 * aa[3].iarg is dimension for arrays (XXX future) 479 * aa[3].varg is function defs for functions. 480 */ 481 #ifdef notdef 482 #define atypsz aa[0].iarg 483 #define aalign aa[1].iarg 484 #endif 485 486 /* 487 * ATTR_STRUCT member list. 488 */ 489 #define amlist aa[0].varg 490 #define amsize aa[1].iarg 491 #define strattr(x) (attr_find(x, ATTR_STRUCT)) 492 493 #define iarg(x) aa[x].iarg 494 #define sarg(x) aa[x].sarg 495 #define varg(x) aa[x].varg 496 497 void gcc_init(void); 498 int gcc_keyword(char *, NODE **); 499 struct attr *gcc_attr_parse(NODE *); 500 void gcc_tcattrfix(NODE *); 501 struct gcc_attrib *gcc_get_attr(struct suedef *, int); 502 void dump_attr(struct attr *gap); 503 504 #ifndef NO_C_BUILTINS 505 struct bitable { 506 char *name; 507 NODE *(*fun)(const struct bitable *, NODE *a); 508 short flags; 509 #define BTNOPROTO 001 510 #define BTNORVAL 002 511 #define BTNOEVE 004 512 short narg; 513 TWORD *tp; 514 TWORD rt; 515 }; 516 517 NODE *builtin_check(struct symtab *, NODE *a); 518 void builtin_init(void); 519 520 /* Some builtins targets need to implement */ 521 NODE *builtin_frame_address(const struct bitable *bt, NODE *a); 522 NODE *builtin_return_address(const struct bitable *bt, NODE *a); 523 NODE *builtin_cfa(const struct bitable *bt, NODE *a); 524 #endif 525 526 527 #ifdef STABS 528 void stabs_init(void); 529 void stabs_file(char *); 530 void stabs_efile(char *); 531 void stabs_line(int); 532 void stabs_rbrac(int); 533 void stabs_lbrac(int); 534 void stabs_func(struct symtab *); 535 void stabs_newsym(struct symtab *); 536 void stabs_chgsym(struct symtab *); 537 void stabs_struct(struct symtab *, struct attr *); 538 #endif 539 540 #ifndef CHARCAST 541 /* to make character constants into character connstants */ 542 /* this is a macro to defend against cross-compilers, etc. */ 543 #define CHARCAST(x) (char)(x) 544 #endif 545 546 /* sometimes int is smaller than pointers */ 547 #if SZPOINT(CHAR) <= SZINT 548 #define INTPTR INT 549 #elif SZPOINT(CHAR) <= SZLONG 550 #define INTPTR LONG 551 #elif SZPOINT(CHAR) <= SZLONGLONG 552 #define INTPTR LONGLONG 553 #else 554 #error int size unknown 555 #endif 556 557 #ifdef TWOPASS 558 #define PRTPREF "* " 559 #else 560 #define PRTPREF "" 561 #endif 562 563 /* Generate a bitmask from a given type size */ 564 #define SZMASK(y) ((((1LL << ((y)-1))-1) << 1) | 1) 565 566 /* 567 * C compiler first pass extra defines. 568 */ 569 #define QUALIFIER (MAXOP+1) 570 #define CLASS (MAXOP+2) 571 #define RB (MAXOP+3) 572 #define DOT (MAXOP+4) 573 #define ELLIPSIS (MAXOP+5) 574 #define TYPE (MAXOP+6) 575 #define LB (MAXOP+7) 576 #define COMOP (MAXOP+8) 577 #define QUEST (MAXOP+9) 578 #define COLON (MAXOP+10) 579 #define ANDAND (MAXOP+11) 580 #define OROR (MAXOP+12) 581 #define NOT (MAXOP+13) 582 #define CAST (MAXOP+14) 583 #define STRING (MAXOP+15) 584 585 /* The following must be in the same order as their NOASG counterparts */ 586 #define PLUSEQ (MAXOP+16) 587 #define MINUSEQ (MAXOP+17) 588 #define DIVEQ (MAXOP+18) 589 #define MODEQ (MAXOP+19) 590 #define MULEQ (MAXOP+20) 591 #define ANDEQ (MAXOP+21) 592 #define OREQ (MAXOP+22) 593 #define EREQ (MAXOP+23) 594 #define LSEQ (MAXOP+24) 595 #define RSEQ (MAXOP+25) 596 597 #define UNASG (-(PLUSEQ-PLUS))+ 598 599 #define INCR (MAXOP+26) 600 #define DECR (MAXOP+27) 601 #define SZOF (MAXOP+28) 602 #define CLOP (MAXOP+29) 603 #define ATTRIB (MAXOP+30) 604 #define XREAL (MAXOP+31) 605 #define XIMAG (MAXOP+32) 606 #define TYMERGE (MAXOP+33) 607 #define LABEL (MAXOP+34) 608 #define STREF (MAXOP+35) 609 610 /* 611 * The following types are only used in pass1. 612 */ 613 #define SIGNED (MAXTYPES+1) 614 #define FARG (MAXTYPES+2) 615 #define FIMAG (MAXTYPES+3) 616 #define IMAG (MAXTYPES+4) 617 #define LIMAG (MAXTYPES+5) 618 #define FCOMPLEX (MAXTYPES+6) 619 #define COMPLEX (MAXTYPES+7) 620 #define LCOMPLEX (MAXTYPES+8) 621 #define ENUMTY (MAXTYPES+9) 622 623 #define ISFTY(x) ((x) >= FLOAT && (x) <= LDOUBLE) 624 #define ISCTY(x) ((x) >= FCOMPLEX && (x) <= LCOMPLEX) 625 #define ISITY(x) ((x) >= FIMAG && (x) <= LIMAG) 626 #define ANYCX(p) (p->n_type == STRTY && attr_find(p->n_ap, ATTR_COMPLEX)) 627 628 #define coptype(o) (cdope(o)&TYFLG) 629 #define clogop(o) (cdope(o)&LOGFLG) 630 #define casgop(o) (cdope(o)&ASGFLG) 631 632 #define slval setlval 633 #define glval getlval 634 635 #include <cxxdefs.h> 636 637