github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/src/cmd/cc/cc.h (about) 1 // Inferno utils/cc/cc.h 2 // http://code.google.com/p/inferno-os/source/browse/utils/cc/cc.h 3 // 4 // Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved. 5 // Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net) 6 // Portions Copyright © 1997-1999 Vita Nuova Limited 7 // Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com) 8 // Portions Copyright © 2004,2006 Bruce Ellis 9 // Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net) 10 // Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others 11 // Portions Copyright © 2009 The Go Authors. All rights reserved. 12 // 13 // Permission is hereby granted, free of charge, to any person obtaining a copy 14 // of this software and associated documentation files (the "Software"), to deal 15 // in the Software without restriction, including without limitation the rights 16 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 // copies of the Software, and to permit persons to whom the Software is 18 // furnished to do so, subject to the following conditions: 19 // 20 // The above copyright notice and this permission notice shall be included in 21 // all copies or substantial portions of the Software. 22 // 23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 // THE SOFTWARE. 30 31 #include <libc.h> 32 #include <bio.h> 33 #include <link.h> 34 35 #ifndef EXTERN 36 #define EXTERN extern 37 #endif 38 39 #undef getc 40 #undef ungetc 41 #undef BUFSIZ 42 43 #define getc ccgetc 44 #define ungetc ccungetc 45 46 typedef struct Node Node; 47 typedef struct Sym Sym; 48 typedef struct Type Type; 49 typedef struct Funct Funct; 50 typedef struct Decl Decl; 51 typedef struct Io Io; 52 typedef struct Term Term; 53 typedef struct Init Init; 54 typedef struct Bits Bits; 55 typedef struct Bvec Bvec; 56 typedef struct Dynimp Dynimp; 57 typedef struct Dynexp Dynexp; 58 typedef struct Var Var; 59 60 typedef Rune TRune; /* target system type */ 61 62 #define BUFSIZ 8192 63 #define NSYMB 500 64 #define NHASH 1024 65 #define STRINGSZ 200 66 #define HISTSZ 20 67 #define YYMAXDEPTH 500 68 #define NTERM 10 69 #define MAXALIGN 7 70 71 #define SIGN(n) ((uvlong)1<<(n-1)) 72 #define MASK(n) (SIGN(n)|(SIGN(n)-1)) 73 74 #define BITS 5 75 #define NVAR (BITS*sizeof(uint32)*8) 76 struct Bits 77 { 78 uint32 b[BITS]; 79 }; 80 81 struct Bvec 82 { 83 int32 n; // number of bits 84 uint32 b[]; 85 }; 86 87 struct Var 88 { 89 vlong offset; 90 LSym* sym; 91 char name; 92 char etype; 93 }; 94 95 struct Node 96 { 97 Node* left; 98 Node* right; 99 void* label; 100 int32 pc; 101 int reg; 102 int32 xoffset; 103 double fconst; /* fp constant */ 104 vlong vconst; /* non fp const */ 105 char* cstring; /* character string */ 106 TRune* rstring; /* rune string */ 107 108 Sym* sym; 109 Type* type; 110 int32 lineno; 111 uchar op; 112 uchar oldop; 113 uchar xcast; 114 uchar class; 115 uchar etype; 116 uchar complex; 117 uchar addable; 118 uchar scale; 119 uchar garb; 120 }; 121 #define Z ((Node*)0) 122 123 struct Sym 124 { 125 Sym* link; 126 LSym* lsym; 127 Type* type; 128 Type* suetag; 129 Type* tenum; 130 char* macro; 131 int32 varlineno; 132 int32 offset; 133 vlong vconst; 134 double fconst; 135 Node* label; 136 ushort lexical; 137 char *name; 138 ushort block; 139 ushort sueblock; 140 uchar class; 141 uchar sym; 142 uchar aused; 143 uchar sig; 144 uchar dataflag; 145 }; 146 #define S ((Sym*)0) 147 148 enum{ 149 SIGNONE = 0, 150 SIGDONE = 1, 151 SIGINTERN = 2, 152 153 SIGNINTERN = 1729*325*1729, 154 }; 155 156 struct Decl 157 { 158 Decl* link; 159 Sym* sym; 160 Type* type; 161 int32 varlineno; 162 int32 offset; 163 short val; 164 ushort block; 165 uchar class; 166 uchar aused; 167 }; 168 #define D ((Decl*)0) 169 170 struct Type 171 { 172 Sym* sym; 173 Sym* tag; 174 Funct* funct; 175 Type* link; 176 Type* down; 177 int32 width; 178 int32 offset; 179 int32 lineno; 180 uchar shift; 181 uchar nbits; 182 uchar etype; 183 uchar garb; 184 uchar align; 185 }; 186 187 #define T ((Type*)0) 188 #define NODECL ((void(*)(int, Type*, Sym*))0) 189 190 struct Init /* general purpose initialization */ 191 { 192 int code; 193 uint32 value; 194 char* s; 195 }; 196 197 EXTERN struct 198 { 199 char* p; 200 int c; 201 } fi; 202 203 struct Io 204 { 205 Io* link; 206 char* p; 207 char b[BUFSIZ]; 208 short c; 209 short f; 210 }; 211 #define I ((Io*)0) 212 213 struct Term 214 { 215 vlong mult; 216 Node *node; 217 }; 218 219 enum 220 { 221 Axxx, 222 Ael1, 223 Ael2, 224 Asu2, 225 Aarg0, 226 Aarg1, 227 Aarg2, 228 Aaut3, 229 NALIGN, 230 }; 231 232 enum 233 { 234 DMARK, 235 DAUTO, 236 DSUE, 237 DLABEL, 238 }; 239 enum 240 { 241 OXXX, 242 OADD, 243 OADDR, 244 OAND, 245 OANDAND, 246 OARRAY, 247 OAS, 248 OASI, 249 OASADD, 250 OASAND, 251 OASASHL, 252 OASASHR, 253 OASDIV, 254 OASHL, 255 OASHR, 256 OASLDIV, 257 OASLMOD, 258 OASLMUL, 259 OASLSHR, 260 OASMOD, 261 OASMUL, 262 OASOR, 263 OASSUB, 264 OASXOR, 265 OBIT, 266 OBREAK, 267 OCASE, 268 OCAST, 269 OCOMMA, 270 OCOND, 271 OCONST, 272 OCONTINUE, 273 ODIV, 274 ODOT, 275 ODOTDOT, 276 ODWHILE, 277 OENUM, 278 OEQ, 279 OEXREG, 280 OFOR, 281 OFUNC, 282 OGE, 283 OGOTO, 284 OGT, 285 OHI, 286 OHS, 287 OIF, 288 OIND, 289 OINDREG, 290 OINIT, 291 OLABEL, 292 OLDIV, 293 OLE, 294 OLIST, 295 OLMOD, 296 OLMUL, 297 OLO, 298 OLS, 299 OLSHR, 300 OLT, 301 OMOD, 302 OMUL, 303 ONAME, 304 ONE, 305 ONOT, 306 OOR, 307 OOROR, 308 OPOSTDEC, 309 OPOSTINC, 310 OPREDEC, 311 OPREINC, 312 OPREFETCH, 313 OPROTO, 314 OREGISTER, 315 ORETURN, 316 OSET, 317 OSIGN, 318 OSIZE, 319 OSTRING, 320 OLSTRING, 321 OSTRUCT, 322 OSUB, 323 OSWITCH, 324 OUNION, 325 OUSED, 326 OWHILE, 327 OXOR, 328 ONEG, 329 OCOM, 330 OPOS, 331 OELEM, 332 333 OTST, /* used in some compilers */ 334 OINDEX, 335 OFAS, 336 OREGPAIR, 337 OROTL, 338 339 OEND 340 }; 341 enum 342 { 343 TXXX, 344 TCHAR, 345 TUCHAR, 346 TSHORT, 347 TUSHORT, 348 TINT, 349 TUINT, 350 TLONG, 351 TULONG, 352 TVLONG, 353 TUVLONG, 354 TFLOAT, 355 TDOUBLE, 356 TIND, 357 TFUNC, 358 TARRAY, 359 TVOID, 360 TSTRUCT, 361 TUNION, 362 TENUM, 363 NTYPE, 364 365 TAUTO = NTYPE, 366 TEXTERN, 367 TSTATIC, 368 TTYPEDEF, 369 TTYPESTR, 370 TREGISTER, 371 TCONSTNT, 372 TVOLATILE, 373 TUNSIGNED, 374 TSIGNED, 375 TDOT, 376 TFILE, 377 TOLD, 378 NALLTYPES, 379 380 /* adapt size of Rune to target system's size */ 381 TRUNE = sizeof(TRune)==4? TUINT: TUSHORT, 382 }; 383 enum 384 { 385 CXXX, 386 CAUTO, 387 CEXTERN, 388 CGLOBL, 389 CSTATIC, 390 CLOCAL, 391 CTYPEDEF, 392 CTYPESTR, 393 CPARAM, 394 CSELEM, 395 CLABEL, 396 CEXREG, 397 NCTYPES, 398 }; 399 enum 400 { 401 GXXX = 0, 402 GCONSTNT = 1<<0, 403 GVOLATILE = 1<<1, 404 NGTYPES = 1<<2, 405 406 GINCOMPLETE = 1<<2, 407 }; 408 enum 409 { 410 BCHAR = 1L<<TCHAR, 411 BUCHAR = 1L<<TUCHAR, 412 BSHORT = 1L<<TSHORT, 413 BUSHORT = 1L<<TUSHORT, 414 BINT = 1L<<TINT, 415 BUINT = 1L<<TUINT, 416 BLONG = 1L<<TLONG, 417 BULONG = 1L<<TULONG, 418 BVLONG = 1L<<TVLONG, 419 BUVLONG = 1L<<TUVLONG, 420 BFLOAT = 1L<<TFLOAT, 421 BDOUBLE = 1L<<TDOUBLE, 422 BIND = 1L<<TIND, 423 BFUNC = 1L<<TFUNC, 424 BARRAY = 1L<<TARRAY, 425 BVOID = 1L<<TVOID, 426 BSTRUCT = 1L<<TSTRUCT, 427 BUNION = 1L<<TUNION, 428 BENUM = 1L<<TENUM, 429 BFILE = 1L<<TFILE, 430 BDOT = 1L<<TDOT, 431 BCONSTNT = 1L<<TCONSTNT, 432 BVOLATILE = 1L<<TVOLATILE, 433 BUNSIGNED = 1L<<TUNSIGNED, 434 BSIGNED = 1L<<TSIGNED, 435 BAUTO = 1L<<TAUTO, 436 BEXTERN = 1L<<TEXTERN, 437 BSTATIC = 1L<<TSTATIC, 438 BTYPEDEF = 1L<<TTYPEDEF, 439 BTYPESTR = 1L<<TTYPESTR, 440 BREGISTER = 1L<<TREGISTER, 441 442 BINTEGER = BCHAR|BUCHAR|BSHORT|BUSHORT|BINT|BUINT| 443 BLONG|BULONG|BVLONG|BUVLONG, 444 BNUMBER = BINTEGER|BFLOAT|BDOUBLE, 445 446 /* these can be overloaded with complex types */ 447 448 BCLASS = BAUTO|BEXTERN|BSTATIC|BTYPEDEF|BTYPESTR|BREGISTER, 449 BGARB = BCONSTNT|BVOLATILE, 450 }; 451 452 struct Funct 453 { 454 Sym* sym[OEND]; 455 Sym* castto[NTYPE]; 456 Sym* castfr[NTYPE]; 457 }; 458 459 EXTERN struct 460 { 461 Type* tenum; /* type of entire enum */ 462 Type* cenum; /* type of current enum run */ 463 vlong lastenum; /* value of current enum */ 464 double floatenum; /* value of current enum */ 465 } en; 466 467 EXTERN int autobn; 468 EXTERN int32 autoffset; 469 EXTERN int blockno; 470 EXTERN Decl* dclstack; 471 EXTERN int debug[256]; 472 EXTERN int32 firstbit; 473 EXTERN Sym* firstarg; 474 EXTERN Type* firstargtype; 475 EXTERN Decl* firstdcl; 476 EXTERN int fperror; 477 EXTERN Sym* hash[NHASH]; 478 EXTERN char* hunk; 479 EXTERN char** include; 480 EXTERN Io* iofree; 481 EXTERN Io* ionext; 482 EXTERN Io* iostack; 483 EXTERN int32 lastbit; 484 EXTERN char lastclass; 485 EXTERN Type* lastdcl; 486 EXTERN int32 lastfield; 487 EXTERN Type* lasttype; 488 EXTERN int32 lineno; 489 EXTERN int32 nearln; 490 EXTERN int nerrors; 491 EXTERN int newflag; 492 EXTERN int32 nhunk; 493 EXTERN int ninclude; 494 EXTERN Node* nodproto; 495 EXTERN Node* nodcast; 496 EXTERN int32 nsymb; 497 EXTERN Biobuf outbuf; 498 EXTERN Biobuf diagbuf; 499 EXTERN char* outfile; 500 EXTERN int peekc; 501 EXTERN int32 stkoff; 502 EXTERN Type* strf; 503 EXTERN Type* strl; 504 EXTERN char* symb; 505 EXTERN Sym* symstring; 506 EXTERN int taggen; 507 EXTERN Type* tfield; 508 EXTERN Type* tufield; 509 extern int thechar; 510 extern char* thestring; 511 extern LinkArch* thelinkarch; 512 EXTERN Type* thisfn; 513 EXTERN int32 thunk; 514 EXTERN Type* types[NALLTYPES]; 515 EXTERN Type* fntypes[NALLTYPES]; 516 EXTERN Node* initlist; 517 EXTERN Term term[NTERM]; 518 EXTERN int nterm; 519 EXTERN int packflg; 520 EXTERN int fproundflg; 521 EXTERN int textflag; 522 EXTERN int dataflag; 523 EXTERN int flag_largemodel; 524 EXTERN int ncontin; 525 EXTERN int canreach; 526 EXTERN int warnreach; 527 EXTERN int nacl; 528 EXTERN Bits zbits; 529 EXTERN Fmt pragcgobuf; 530 EXTERN Biobuf bstdout; 531 EXTERN Var var[NVAR]; 532 533 extern char *onames[], *tnames[], *gnames[]; 534 extern char *cnames[], *qnames[], *bnames[]; 535 extern uchar tab[NTYPE][NTYPE]; 536 extern uchar comrel[], invrel[], logrel[]; 537 extern int32 ncast[], tadd[], tand[]; 538 extern int32 targ[], tasadd[], tasign[], tcast[]; 539 extern int32 tdot[], tfunct[], tindir[], tmul[]; 540 extern int32 tnot[], trel[], tsub[]; 541 542 extern uchar typeaf[]; 543 extern uchar typefd[]; 544 extern uchar typei[]; 545 extern uchar typesu[]; 546 extern uchar typesuv[]; 547 extern uchar typeu[]; 548 extern uchar typev[]; 549 extern uchar typec[]; 550 extern uchar typeh[]; 551 extern uchar typeil[]; 552 extern uchar typeilp[]; 553 extern uchar typechl[]; 554 extern uchar typechlv[]; 555 extern uchar typechlvp[]; 556 extern uchar typechlp[]; 557 extern uchar typechlpfd[]; 558 559 EXTERN uchar* typeword; 560 EXTERN uchar* typecmplx; 561 EXTERN Link* ctxt; 562 563 extern uint32 thash1; 564 extern uint32 thash2; 565 extern uint32 thash3; 566 extern uint32 thash[]; 567 568 /* 569 * compat.c/unix.c/windows.c 570 */ 571 int systemtype(int); 572 int pathchar(void); 573 574 /* 575 * parser 576 */ 577 int yyparse(void); 578 int mpatov(char*, vlong*); 579 580 /* 581 * lex.c 582 */ 583 void* allocn(void*, int32, int32); 584 void* alloc(int32); 585 void ensuresymb(int32); 586 void cinit(void); 587 int compile(char*, char**, int); 588 void errorexit(void); 589 int filbuf(void); 590 int getc(void); 591 int32 getr(void); 592 int getnsc(void); 593 Sym* lookup(void); 594 void main(int, char*[]); 595 void newfile(char*, int); 596 void newio(void); 597 void pushio(void); 598 int32 escchar(int32, int, int); 599 Sym* slookup(char*); 600 void syminit(Sym*); 601 void unget(int); 602 int32 yylex(void); 603 int Lconv(Fmt*); 604 int Tconv(Fmt*); 605 int FNconv(Fmt*); 606 int Oconv(Fmt*); 607 int Qconv(Fmt*); 608 int VBconv(Fmt*); 609 int Bconv(Fmt*); 610 void setinclude(char*); 611 612 /* 613 * mac.c 614 */ 615 void dodefine(char*); 616 void domacro(void); 617 Sym* getsym(void); 618 int32 getnsn(void); 619 void macdef(void); 620 void macprag(void); 621 void macend(void); 622 void macexpand(Sym*, char*); 623 void macif(int); 624 void macinc(void); 625 void maclin(void); 626 void macund(void); 627 628 /* 629 * dcl.c 630 */ 631 Node* doinit(Sym*, Type*, int32, Node*); 632 Type* tcopy(Type*); 633 Node* init1(Sym*, Type*, int32, int); 634 Node* newlist(Node*, Node*); 635 void adecl(int, Type*, Sym*); 636 int anyproto(Node*); 637 void argmark(Node*, int); 638 void dbgdecl(Sym*); 639 Node* dcllabel(Sym*, int); 640 Node* dodecl(void(*)(int, Type*, Sym*), int, Type*, Node*); 641 Sym* mkstatic(Sym*); 642 void doenum(Sym*, Node*); 643 void snap(Type*); 644 Type* dotag(Sym*, int, int); 645 void edecl(int, Type*, Sym*); 646 Type* fnproto(Node*); 647 Type* fnproto1(Node*); 648 void markdcl(void); 649 Type* paramconv(Type*, int); 650 void pdecl(int, Type*, Sym*); 651 Decl* push(void); 652 Decl* push1(Sym*); 653 Node* revertdcl(void); 654 int32 xround(int32, int); 655 int rsametype(Type*, Type*, int, int); 656 int sametype(Type*, Type*); 657 uint32 sign(Sym*); 658 uint32 signature(Type*); 659 void sualign(Type*); 660 void tmerge(Type*, Sym*); 661 void walkparam(Node*, int); 662 void xdecl(int, Type*, Sym*); 663 Node* contig(Sym*, Node*, int32); 664 665 /* 666 * com.c 667 */ 668 void ccom(Node*); 669 void complex(Node*); 670 int tcom(Node*); 671 int tcoma(Node*, Node*, Type*, int); 672 int tcomd(Node*); 673 int tcomo(Node*, int); 674 int tcomx(Node*); 675 int tlvalue(Node*); 676 void constas(Node*, Type*, Type*); 677 678 /* 679 * con.c 680 */ 681 void acom(Node*); 682 void acom1(vlong, Node*); 683 void acom2(Node*, Type*); 684 int acomcmp1(const void*, const void*); 685 int acomcmp2(const void*, const void*); 686 int addo(Node*); 687 void evconst(Node*); 688 689 /* 690 * funct.c 691 */ 692 int isfunct(Node*); 693 void dclfunct(Type*, Sym*); 694 695 /* 696 * sub.c 697 */ 698 void arith(Node*, int); 699 int deadheads(Node*); 700 Type* dotsearch(Sym*, Type*, Node*, int32*); 701 int32 dotoffset(Type*, Type*, Node*); 702 void gethunk(void); 703 Node* invert(Node*); 704 int bitno(int32); 705 void makedot(Node*, Type*, int32); 706 int mixedasop(Type*, Type*); 707 Node* new(int, Node*, Node*); 708 Node* new1(int, Node*, Node*); 709 int nilcast(Type*, Type*); 710 int nocast(Type*, Type*); 711 void prtree(Node*, char*); 712 void prtree1(Node*, int, int); 713 void relcon(Node*, Node*); 714 int relindex(int); 715 int simpleg(int32); 716 Type* garbt(Type*, int32); 717 int simplec(int32); 718 Type* simplet(int32); 719 int stcompat(Node*, Type*, Type*, int32[]); 720 int tcompat(Node*, Type*, Type*, int32[]); 721 void tinit(void); 722 Type* typ(int, Type*); 723 Type* copytyp(Type*); 724 void typeext(Type*, Node*); 725 void typeext1(Type*, Node*); 726 int side(Node*); 727 int vconst(Node*); 728 int xlog2(uvlong); 729 int vlog(Node*); 730 int topbit(uint32); 731 void simplifyshift(Node*); 732 int32 typebitor(int32, int32); 733 void diag(Node*, char*, ...); 734 void warn(Node*, char*, ...); 735 void yyerror(char*, ...); 736 void fatal(Node*, char*, ...); 737 LSym* linksym(Sym*); 738 739 /* 740 * acid.c 741 */ 742 void acidtype(Type*); 743 void acidvar(Sym*); 744 745 /* 746 * godefs.c 747 */ 748 int Uconv(Fmt*); 749 void godeftype(Type*); 750 void godefvar(Sym*); 751 752 /* 753 * bits.c 754 */ 755 Bits bor(Bits, Bits); 756 Bits band(Bits, Bits); 757 Bits bnot(Bits); 758 int bany(Bits*); 759 int bnum(Bits); 760 Bits blsh(uint); 761 int beq(Bits, Bits); 762 int bset(Bits, uint); 763 764 /* 765 * dpchk.c 766 */ 767 void dpcheck(Node*); 768 void arginit(void); 769 void pragvararg(void); 770 void pragpack(void); 771 void pragfpround(void); 772 void pragdataflag(void); 773 void pragtextflag(void); 774 void pragincomplete(void); 775 void pragcgo(char*); 776 777 /* 778 * calls to machine depend part 779 */ 780 void codgen(Node*, Node*); 781 void gclean(void); 782 void gextern(Sym*, Node*, int32, int32); 783 void ginit(void); 784 int32 outstring(char*, int32); 785 int32 outlstring(TRune*, int32); 786 void sextern(Sym*, Node*, int32, int32); 787 void xcom(Node*); 788 int32 exreg(Type*); 789 int32 align(int32, Type*, int, int32*); 790 int32 maxround(int32, int32); 791 int hasdotdotdot(Type*); 792 void linkarchinit(void); 793 794 extern schar ewidth[]; 795 796 /* 797 * com64 798 */ 799 int com64(Node*); 800 void com64init(void); 801 void bool64(Node*); 802 double convvtof(vlong); 803 vlong convftov(double); 804 double convftox(double, int); 805 vlong convvtox(vlong, int); 806 807 /* 808 * machcap 809 */ 810 int machcap(Node*); 811 812 #pragma varargck argpos warn 2 813 #pragma varargck argpos diag 2 814 #pragma varargck argpos yyerror 1 815 816 #pragma varargck type "B" Bits 817 #pragma varargck type "F" Node* 818 #pragma varargck type "L" int32 819 #pragma varargck type "Q" int32 820 #pragma varargck type "O" int 821 #pragma varargck type "O" uint 822 #pragma varargck type "T" Type* 823 #pragma varargck type "U" char* 824 #pragma varargck type "|" int 825 826 enum 827 { 828 Plan9 = 1<<0, 829 Unix = 1<<1, 830 Windows = 1<<2, 831 }; 832 int pathchar(void); 833 int systemtype(int); 834 void* alloc(int32 n); 835 void* allocn(void*, int32, int32);