1 /* Copyright (c) 1979 Regents of the University of California */ 2 3 /* static char sccsid[] = "@(#)0.h 2.1 02/08/84"; */ 4 5 #define DEBUG 6 #define CONSETS 7 #define CHAR 8 #define STATIC 9 #define hp21mx 0 10 11 #include <stdio.h> 12 #include <sys/types.h> 13 14 typedef enum {FALSE, TRUE} bool; 15 16 /* 17 * Option flags 18 * 19 * The following options are recognized in the text of the program 20 * and also on the command line: 21 * 22 * b block buffer the file output 23 * 24 * i make a listing of the procedures and functions in 25 * the following include files 26 * 27 * l make a listing of the program 28 * 29 * n place each include file on a new page with a header 30 * 31 * p disable post mortem and statement limit counting 32 * 33 * t disable run-time tests 34 * 35 * u card image mode; only first 72 chars of input count 36 * 37 * w suppress special diagnostic warnings 38 * 39 * z generate counters for an execution profile 40 */ 41 #ifdef DEBUG 42 bool fulltrace, errtrace, testtrace, yyunique; 43 #endif DEBUG 44 45 /* 46 * Each option has a stack of 17 option values, with opts giving 47 * the current, top value, and optstk the value beneath it. 48 * One refers to option `l' as, e.g., opt('l') in the text for clarity. 49 */ 50 char opts[ 'z' - 'A' + 1]; 51 short optstk[ 'z' - 'A' + 1]; 52 53 #define opt(c) opts[c-'A'] 54 55 /* 56 * Monflg is set when we are generating 57 * a pxp profile. this is set by the -z command line option. 58 */ 59 bool monflg; 60 61 /* 62 * profflag is set when we are generating a prof profile. 63 * this is set by the -p command line option. 64 */ 65 #ifdef PC 66 bool profflag; 67 #endif 68 69 70 /* 71 * NOTES ON THE DYNAMIC NATURE OF THE DATA STRUCTURES 72 * 73 * Pi uses expandable tables for 74 * its namelist (symbol table), string table 75 * hash table, and parse tree space. The following 76 * definitions specify the size of the increments 77 * for these items in fundamental units so that 78 * each uses approximately 1024 bytes. 79 */ 80 81 #define STRINC 1024 /* string space increment */ 82 #define TRINC 512 /* tree space increment */ 83 #define HASHINC 509 /* hash table size in words, each increment */ 84 #define NLINC 56 /* namelist increment size in nl structs */ 85 86 /* 87 * The initial sizes of the structures. 88 * These should be large enough to compile 89 * an "average" sized program so as to minimize 90 * storage requests. 91 * On a small system or and 11/34 or 11/40 92 * these numbers can be trimmed to make the 93 * compiler smaller. 94 */ 95 #define ITREE 2000 96 #define INL 200 97 #define IHASH 509 98 99 /* 100 * The following limits on hash and tree tables currently 101 * allow approximately 1200 symbols and 20k words of tree 102 * space. The fundamental limit of 64k total data space 103 * should be exceeded well before these are full. 104 */ 105 /* 106 * TABLE_MULTIPLIER is for uniformly increasing the sizes of the tables 107 */ 108 #ifdef ADDR32 109 #define TABLE_MULTIPLIER 8 110 #endif ADDR32 111 #ifdef ADDR16 112 #define TABLE_MULTIPLIER 1 113 #endif ADDR16 114 #define MAXHASH (4 * TABLE_MULTIPLIER) 115 #define MAXNL (12 * TABLE_MULTIPLIER) 116 #define MAXTREE (30 * TABLE_MULTIPLIER) 117 /* 118 * MAXDEPTH is the depth of the parse stack. 119 * STACK_MULTIPLIER is for increasing its size. 120 */ 121 #ifdef ADDR32 122 #define STACK_MULTIPLIER 8 123 #endif ADDR32 124 #ifdef ADDR16 125 #define STACK_MULTIPLIER 1 126 #endif ADDR16 127 #define MAXDEPTH ( 150 * STACK_MULTIPLIER ) 128 129 /* 130 * ERROR RELATED DEFINITIONS 131 */ 132 133 /* 134 * Exit statuses to pexit 135 * 136 * AOK 137 * ERRS Compilation errors inhibit obj productin 138 * NOSTART Errors before we ever got started 139 * DIED We ran out of memory or some such 140 */ 141 #define AOK 0 142 #define ERRS 1 143 #define NOSTART 2 144 #define DIED 3 145 146 bool Recovery; 147 148 #define eholdnl() Eholdnl = TRUE 149 #define nocascade() Enocascade = TRUE 150 151 bool Eholdnl, Enocascade; 152 153 154 /* 155 * The flag eflg is set whenever we have a hard error. 156 * The character in errpfx will precede the next error message. 157 * When cgenflg is set code generation is suppressed. 158 * This happens whenver we have an error (i.e. if eflg is set) 159 * and when we are walking the tree to determine types only. 160 */ 161 bool eflg; 162 char errpfx; 163 164 #define setpfx(x) errpfx = x 165 166 #define standard() setpfx('s') 167 #define warning() setpfx('w') 168 #define recovered() setpfx('e') 169 #define continuation() setpfx(' ') 170 171 int cgenflg; 172 173 174 /* 175 * The flag syneflg is used to suppress the diagnostics of the form 176 * E 10 a, defined in someprocedure, is neither used nor set 177 * when there were syntax errors in "someprocedure". 178 * In this case, it is likely that these warinings would be spurious. 179 */ 180 bool syneflg; 181 182 /* 183 * The compiler keeps its error messages in a file. 184 * The variable efil is the unit number on which 185 * this file is open for reading of error message text. 186 * Similarly, the file ofil is the unit of the file 187 * "obj" where we write the interpreter code. 188 */ 189 short efil; 190 191 #ifdef OBJ 192 short ofil; 193 194 short obuf[518]; 195 #endif 196 197 bool Enoline; 198 #define elineoff() Enoline = TRUE 199 #define elineon() Enoline = FALSE 200 201 202 /* 203 * SYMBOL TABLE STRUCTURE DEFINITIONS 204 * 205 * The symbol table is henceforth referred to as the "namelist". 206 * It consists of a number of structures of the form "nl" below. 207 * These are contained in a number of segments of the symbol 208 * table which are dynamically allocated as needed. 209 * The major namelist manipulation routines are contained in the 210 * file "nl.c". 211 * 212 * The major components of a namelist entry are the "symbol", giving 213 * a pointer into the string table for the string associated with this 214 * entry and the "class" which tells which of the (currently 19) 215 * possible types of structure this is. 216 * 217 * Many of the classes use the "type" field for a pointer to the type 218 * which the entry has. 219 * 220 * Other pieces of information in more than one class include the block 221 * in which the symbol is defined, flags indicating whether the symbol 222 * has been used and whether it has been assigned to, etc. 223 * 224 * A more complete discussion of the features of the namelist is impossible 225 * here as it would be too voluminous. Refer to the "PI 1.0 Implementation 226 * Notes" for more details. 227 */ 228 229 /* 230 * The basic namelist structure. 231 * There is a union of data types defining the stored information 232 * as pointers, integers, longs, or a double. 233 * 234 * The array disptab defines the hash header for the symbol table. 235 * Symbols are hashed based on the low 6 bits of their pointer into 236 * the string table; see the routines in the file "lookup.c" and also "fdec.c" 237 * especially "funcend". 238 */ 239 extern int pnumcnt; 240 241 struct nl { 242 char *symbol; 243 char info[4]; 244 struct nl *type; 245 struct nl *chain, *nl_next; 246 union { 247 struct nl *un_ptr[5]; 248 int un_value[5]; 249 long un_range[2]; 250 double un_real; 251 struct nl *un_nptr[5]; /* Points to conformant array bounds */ 252 } nl_un; 253 # ifdef PTREE 254 pPointer inTree; 255 # endif PTREE 256 }; 257 258 #define class info[0] 259 #define nl_flags info[1] 260 #define nl_block info[1] 261 #define extra_flags info[2] 262 #define align_info info[3] 263 264 #define range nl_un.un_range 265 #define value nl_un.un_value 266 #define ptr nl_un.un_ptr 267 #define real nl_un.un_real 268 #define nptr nl_un.un_nptr 269 270 extern struct nl *nlp, *disptab[077+1], *Fp; 271 extern struct nl nl[INL]; 272 273 274 /* 275 * NL FLAGS BITS 276 * 277 * Definitions of the usage of the bits in 278 * the nl_flags byte. Note that the low 5 bits of the 279 * byte are the "nl_block" and that some classes make use 280 * of this byte as a "width". 281 * 282 * The only non-obvious bit definition here is "NFILES" 283 * which records whether a structure contains any files. 284 * Such structures are not allowed to be dynamically allocated. 285 */ 286 287 #define BLOCKNO( flag ) ( flag & 037 ) 288 #define NLFLAGS( flag ) ( flag &~ 037 ) 289 290 #define NUSED 0100 291 #define NMOD 0040 292 #define NFORWD 0200 293 #define NFILES 0200 294 #ifdef PC 295 #define NEXTERN 0001 /* flag used to mark external funcs and procs */ 296 #define NLOCAL 0002 /* variable is a local */ 297 #define NPARAM 0004 /* variable is a parameter */ 298 #define NGLOBAL 0010 /* variable is a global */ 299 #define NREGVAR 0020 /* or'ed in if variable is in a register */ 300 #define NNLOCAL 0040 /* named local variable, not used in symbol table */ 301 #endif PC 302 303 /* 304 * used to mark value[ NL_FORV ] for loop variables 305 */ 306 #define FORVAR 1 307 308 /* 309 * Definition of the commonly used "value" fields. 310 * The most important one is NL_OFFS which gives 311 * the offset of a variable in its stack mark. 312 */ 313 #define NL_OFFS 0 314 315 #define NL_CNTR 1 316 #define NL_NLSTRT 2 317 #define NL_LINENO 3 318 #define NL_FVAR 3 319 #define NL_ENTLOC 4 /* FUNC, PROC - entry point */ 320 #define NL_FCHAIN 4 /* FFUNC, FPROC - ptr to formals */ 321 322 #define NL_GOLEV 2 323 #define NL_GOLINE 3 324 #define NL_FORV 1 325 326 /* 327 * nlp -> nl_un.un_ptr[] subscripts for records 328 * NL_FIELDLIST the chain of fixed fields of a record, in order. 329 * the fields are also chained through ptr[NL_FIELDLIST]. 330 * this does not include the tag, or fields of variants. 331 * NL_VARNT pointer to the variants of a record, 332 * these are then chained through the .chain field. 333 * NL_VTOREC pointer from a VARNT to the RECORD that is the variant. 334 * NL_TAG pointer from a RECORD to the tagfield 335 * if there are any variants. 336 * align_info the alignment of a RECORD is in info[3]. 337 */ 338 #define NL_FIELDLIST 1 339 #define NL_VARNT 2 340 #define NL_VTOREC 2 341 #define NL_TAG 3 342 /* and align_info is info[3]. #defined above */ 343 344 #define NL_ELABEL 4 /* SCAL - ptr to definition of enums */ 345 346 /* 347 * For BADUSE nl structures, NL_KINDS is a bit vector 348 * indicating the kinds of illegal usages complained about 349 * so far. For kind of bad use "kind", "1 << kind" is set. 350 * The low bit is reserved as ISUNDEF to indicate whether 351 * this identifier is totally undefined. 352 */ 353 #define NL_KINDS 0 354 355 #define ISUNDEF 1 356 357 /* 358 * variables come in three flavors: globals, parameters, locals; 359 * they can also hide in registers, but that's a different flag 360 */ 361 #define PARAMVAR 1 362 #define LOCALVAR 2 363 #define GLOBALVAR 3 364 #define NAMEDLOCALVAR 4 365 366 /* 367 * NAMELIST CLASSES 368 * 369 * The following are the namelist classes. 370 * Different classes make use of the value fields 371 * of the namelist in different ways. 372 * 373 * The namelist should be redesigned by providing 374 * a number of structure definitions with one corresponding 375 * to each namelist class, ala a variant record in Pascal. 376 */ 377 #define BADUSE 0 378 #define CONST 1 379 #define TYPE 2 380 #define VAR 3 381 #define ARRAY 4 382 #define PTRFILE 5 383 #define RECORD 6 384 #define FIELD 7 385 #define PROC 8 386 #define FUNC 9 387 #define FVAR 10 388 #define REF 11 389 #define PTR 12 390 #define FILET 13 391 #define SET 14 392 #define RANGE 15 393 #define LABEL 16 394 #define WITHPTR 17 395 #define SCAL 18 396 #define STR 19 397 #define PROG 20 398 #define IMPROPER 21 399 #define VARNT 22 400 #define FPROC 23 401 #define FFUNC 24 402 #define CRANGE 25 403 404 /* 405 * Clnames points to an array of names for the 406 * namelist classes. 407 */ 408 char **clnames; 409 410 /* 411 * PRE-DEFINED NAMELIST OFFSETS 412 * 413 * The following are the namelist offsets for the 414 * primitive types. The ones which are negative 415 * don't actually exist, but are generated and tested 416 * internally. These definitions are sensitive to the 417 * initializations in nl.c. 418 */ 419 #define TFIRST -7 420 #define TFILE -7 421 #define TREC -6 422 #define TARY -5 423 #define TSCAL -4 424 #define TPTR -3 425 #define TSET -2 426 #define TSTR -1 427 #define NIL 0 428 #define TBOOL 1 429 #define TCHAR 2 430 #define TINT 3 431 #define TDOUBLE 4 432 #define TNIL 5 433 #define T1INT 6 434 #define T2INT 7 435 #define T4INT 8 436 #define T1CHAR 9 437 #define T1BOOL 10 438 #define T8REAL 11 439 #define TLAST 11 440 441 /* 442 * SEMANTIC DEFINITIONS 443 */ 444 445 /* 446 * NOCON and SAWCON are flags in the tree telling whether 447 * a constant set is part of an expression. 448 * these are no longer used, 449 * since we now do constant sets at compile time. 450 */ 451 #define NOCON 0 452 #define SAWCON 1 453 454 /* 455 * The variable cbn gives the current block number, 456 * the variable bn is set as a side effect of a call to 457 * lookup, and is the block number of the variable which 458 * was found. 459 */ 460 short bn, cbn; 461 462 /* 463 * The variable line is the current semantic 464 * line and is set in stat.c from the numbers 465 * embedded in statement type tree nodes. 466 */ 467 short line; 468 469 /* 470 * The size of the display 471 * which defines the maximum nesting 472 * of procedures and functions allowed. 473 * Because of the flags in the current namelist 474 * this must be no greater than 32. 475 */ 476 #define DSPLYSZ 20 477 478 /* 479 * the following structure records whether a level declares 480 * any variables which are (or contain) files. 481 * this so that the runtime routines for file cleanup can be invoked. 482 */ 483 bool dfiles[ DSPLYSZ ]; 484 485 /* 486 * Structure recording information about a constant 487 * declaration. It is actually the return value from 488 * the routine "gconst", but since C doesn't support 489 * record valued functions, this is more convenient. 490 */ 491 struct { 492 struct nl *ctype; 493 short cival; 494 double crval; 495 char *cpval; /* note used to be int * */ 496 } con; 497 498 /* 499 * The set structure records the lower bound 500 * and upper bound with the lower bound normalized 501 * to zero when working with a set. It is set by 502 * the routine setran in var.c. 503 */ 504 struct { 505 short lwrb, uprbp; 506 } set; 507 508 /* 509 * structures of this kind are filled in by precset and used by postcset 510 * to indicate things about constant sets. 511 */ 512 struct csetstr { 513 struct nl *csettype; 514 long paircnt; 515 long singcnt; 516 bool comptime; 517 }; 518 /* 519 * The following flags are passed on calls to lvalue 520 * to indicate how the reference is to affect the usage 521 * information for the variable being referenced. 522 * MOD is used to set the NMOD flag in the namelist 523 * entry for the variable, ASGN permits diagnostics 524 * to be formed when a for variable is assigned to in 525 * the range of the loop. 526 */ 527 #define NOFLAGS 0 528 #define MOD 01 529 #define ASGN 02 530 #define NOUSE 04 531 532 /* 533 * the following flags are passed to lvalue and rvalue 534 * to tell them whether an lvalue or rvalue is required. 535 * the semantics checking is done according to the function called, 536 * but for pc, lvalue may put out an rvalue by indirecting afterwards, 537 * and rvalue may stop short of putting out the indirection. 538 */ 539 #define LREQ 01 540 #define RREQ 02 541 542 double MAXINT; 543 double MININT; 544 545 /* 546 * Variables for generation of profile information. 547 * Monflg is set when we want to generate a profile. 548 * Gocnt record the total number of goto's and 549 * cnts records the current counter for generating 550 * COUNT operators. 551 */ 552 short gocnt; 553 short cnts; 554 555 /* 556 * Most routines call "incompat" rather than asking "!compat" 557 * for historical reasons. 558 */ 559 #define incompat !compat 560 561 /* 562 * Parts records which declaration parts have been seen. 563 * The grammar allows the "label" "const" "type" "var" and routine 564 * parts to be repeated and to be in any order, so that 565 * they can be detected semantically to give better 566 * error diagnostics. 567 * 568 * The flag NONLOCALVAR indicates that a non-local var has actually 569 * been used hence the display must be saved; NONLOCALGOTO indicates 570 * that a non-local goto has been done hence that a setjmp must be done. 571 */ 572 int parts[ DSPLYSZ ]; 573 574 #define LPRT 0x0001 575 #define CPRT 0x0002 576 #define TPRT 0x0004 577 #define VPRT 0x0008 578 #define RPRT 0x0010 579 580 #define NONLOCALVAR 0x0020 581 #define NONLOCALGOTO 0x0040 582 583 /* 584 * Flags for the "you used / instead of div" diagnostic 585 */ 586 bool divchk; 587 bool divflg; 588 589 bool errcnt[DSPLYSZ]; 590 591 /* 592 * Forechain links those types which are 593 * ^ sometype 594 * so that they can be evaluated later, permitting 595 * circular, recursive list structures to be defined. 596 */ 597 struct nl *forechain; 598 599 /* 600 * Withlist links all the records which are currently 601 * opened scopes because of with statements. 602 */ 603 struct nl *withlist; 604 605 struct nl *intset; 606 struct nl *input, *output; 607 struct nl *program; 608 609 /* progseen flag used by PC to determine if 610 * a routine segment is being compiled (and 611 * therefore no program statement seen) 612 */ 613 bool progseen; 614 615 616 /* 617 * STRUCTURED STATEMENT GOTO CHECKING 618 * 619 * The variable level keeps track of the current 620 * "structured statement level" when processing the statement 621 * body of blocks. This is used in the detection of goto's into 622 * structured statements in a block. 623 * 624 * Each label's namelist entry contains two pieces of information 625 * related to this check. The first `NL_GOLEV' either contains 626 * the level at which the label was declared, `NOTYET' if the label 627 * has not yet been declared, or `DEAD' if the label is dead, i.e. 628 * if we have exited the level in which the label was defined. 629 * 630 * When we discover a "goto" statement, if the label has not 631 * been defined yet, then we record the current level and the current line 632 * for a later error check. If the label has been already become "DEAD" 633 * then a reference to it is an error. Now the compiler maintains, 634 * for each block, a linked list of the labels headed by "gotos[bn]". 635 * When we exit a structured level, we perform the routine 636 * ungoto in stat.c. It notices labels whose definition levels have been 637 * exited and makes them be dead. For labels which have not yet been 638 * defined, ungoto will maintain NL_GOLEV as the minimum structured level 639 * since the first usage of the label. It is not hard to see that the label 640 * must eventually be declared at this level or an outer level to this 641 * one or a goto into a structured statement will exist. 642 */ 643 short level; 644 struct nl *gotos[DSPLYSZ]; 645 646 #define NOTYET 10000 647 #define DEAD 10000 648 649 /* 650 * Noreach is true when the next statement will 651 * be unreachable unless something happens along 652 * (like exiting a looping construct) to save 653 * the day. 654 */ 655 bool noreach; 656 657 /* 658 * UNDEFINED VARIABLE REFERENCE STRUCTURES 659 */ 660 struct udinfo { 661 int ud_line; 662 struct udinfo *ud_next; 663 char nullch; 664 }; 665 666 /* 667 * CODE GENERATION DEFINITIONS 668 */ 669 670 /* 671 * NSTAND is or'ed onto the abstract machine opcode 672 * for non-standard built-in procedures and functions. 673 */ 674 #define NSTAND 0400 675 676 #define codeon() cgenflg++ 677 #define codeoff() --cgenflg 678 #define CGENNING ( cgenflg >= 0 ) 679 680 /* 681 * Codeline is the last lino output in the code generator. 682 * It used to be used to suppress LINO operators but no 683 * more since we now count statements. 684 * Lc is the intepreter code location counter. 685 * 686 short codeline; 687 */ 688 #ifdef OBJ 689 char *lc; 690 #endif 691 692 693 /* 694 * Routines which need types 695 * other than "integer" to be 696 * assumed by the compiler. 697 */ 698 double atof(); 699 long lwidth(); 700 long leven(); 701 long aryconst(); 702 long a8tol(); 703 long roundup(); 704 struct nl *tmpalloc(); 705 struct nl *lookup(); 706 double atof(); 707 int *hash(); 708 char *alloc(); 709 int *pcalloc(); 710 char *savestr(); 711 char *esavestr(); 712 char *parnam(); 713 char *malloc(); 714 char *getlab(); 715 char *getnext(); 716 char *skipbl(); 717 char *nameof(); 718 char *pstrcpy(); 719 char *myctime(); 720 char *putlab(); 721 bool fcompat(); 722 bool constval(); 723 bool precset(); 724 bool nilfnil(); 725 struct nl *funccod(); 726 struct nl *pcfunccod(); 727 struct nl *lookup1(); 728 struct nl *hdefnl(); 729 struct nl *defnl(); 730 struct nl *flvalue(); 731 struct nl *plist(); 732 struct nl *enter(); 733 struct nl *nlcopy(); 734 struct nl *tyrec(); 735 struct nl *tyary(); 736 struct nl *tyrang(); 737 struct nl *tyscal(); 738 struct nl *deffld(); 739 struct nl *stklval(); 740 struct nl *scalar(); 741 struct nl *gen(); 742 struct nl *stkrval(); 743 struct nl *funcext(); 744 struct nl *funchdr(); 745 struct nl *funcbody(); 746 struct nl *yybaduse(); 747 struct nl *stackRV(); 748 struct nl *defvnt(); 749 struct nl *tyrec1(); 750 struct nl *reclook(); 751 struct nl *asgnop1(); 752 struct nl *pcasgconf(); 753 struct nl *gtype(); 754 struct nl *call(); 755 struct nl *lvalue(); 756 struct nl *pclvalue(); 757 struct nl *rvalue(); 758 struct nl *cset(); 759 struct nl *tycrang(); 760 struct tnode *newlist(); 761 struct tnode *addlist(); 762 struct tnode *fixlist(); 763 struct tnode *setupvar(); 764 struct tnode *setuptyrec(); 765 struct tnode *setupfield(); 766 struct tnode *tree(); 767 struct tnode *tree1(); 768 struct tnode *tree2(); 769 struct tnode *tree3(); 770 struct tnode *tree4(); 771 struct tnode *tree5(); 772 773 /* 774 * type cast NIL to keep lint happy (which is not so bad) 775 */ 776 #define NLNIL ( (struct nl *) NIL ) 777 #define TR_NIL ( (struct tnode *) NIL) 778 779 /* 780 * Funny structures to use 781 * pointers in wild and wooly ways 782 */ 783 struct cstruct{ 784 char pchar; 785 }; 786 struct { 787 short pint; 788 short pint2; 789 }; 790 struct lstruct { 791 long plong; 792 }; 793 struct { 794 double pdouble; 795 }; 796 797 #define OCT 1 798 #define HEX 2 799 800 /* 801 * MAIN PROGRAM VARIABLES, MISCELLANY 802 */ 803 804 /* 805 * Variables forming a data base referencing 806 * the command line arguments with the "i" option, e.g. 807 * in "pi -i scanner.i compiler.p". 808 */ 809 char **pflist; 810 short pflstc; 811 short pfcnt; 812 813 char *filename; /* current source file name */ 814 long tvec; 815 extern char *snark; /* SNARK */ 816 extern char *classes[ ]; /* maps namelist classes to string names */ 817 818 #define derror error 819 820 #ifdef PC 821 822 /* 823 * the current function number, for [ lines 824 */ 825 int ftnno; 826 827 /* 828 * the pc output stream 829 */ 830 FILE *pcstream; 831 832 #endif PC 833