1 /* Copyright (c) 1979 Regents of the University of California */ 2 3 /* static char sccsid[] = "@(#)0.h 1.22 09/19/83"; */ 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 } nl_un; 252 # ifdef PTREE 253 pPointer inTree; 254 # endif PTREE 255 }; 256 257 #define class info[0] 258 #define nl_flags info[1] 259 #define nl_block info[1] 260 #define extra_flags info[2] 261 #define align_info info[3] 262 263 #define range nl_un.un_range 264 #define value nl_un.un_value 265 #define ptr nl_un.un_ptr 266 #define real nl_un.un_real 267 268 extern struct nl *nlp, *disptab[077+1], *Fp; 269 extern struct nl nl[INL]; 270 271 272 /* 273 * NL FLAGS BITS 274 * 275 * Definitions of the usage of the bits in 276 * the nl_flags byte. Note that the low 5 bits of the 277 * byte are the "nl_block" and that some classes make use 278 * of this byte as a "width". 279 * 280 * The only non-obvious bit definition here is "NFILES" 281 * which records whether a structure contains any files. 282 * Such structures are not allowed to be dynamically allocated. 283 */ 284 285 #define BLOCKNO( flag ) ( flag & 037 ) 286 #define NLFLAGS( flag ) ( flag &~ 037 ) 287 288 #define NUSED 0100 289 #define NMOD 0040 290 #define NFORWD 0200 291 #define NFILES 0200 292 #ifdef PC 293 #define NEXTERN 0001 /* flag used to mark external funcs and procs */ 294 #define NLOCAL 0002 /* variable is a local */ 295 #define NPARAM 0004 /* variable is a parameter */ 296 #define NGLOBAL 0010 /* variable is a global */ 297 #define NREGVAR 0020 /* or'ed in if variable is in a register */ 298 #define NNLOCAL 0040 /* named local variable, not used in symbol table */ 299 #endif PC 300 301 /* 302 * used to mark value[ NL_FORV ] for loop variables 303 */ 304 #define FORVAR 1 305 306 /* 307 * Definition of the commonly used "value" fields. 308 * The most important one is NL_OFFS which gives 309 * the offset of a variable in its stack mark. 310 */ 311 #define NL_OFFS 0 312 313 #define NL_CNTR 1 314 #define NL_NLSTRT 2 315 #define NL_LINENO 3 316 #define NL_FVAR 3 317 #define NL_ENTLOC 4 /* FUNC, PROC - entry point */ 318 #define NL_FCHAIN 4 /* FFUNC, FPROC - ptr to formals */ 319 320 #define NL_GOLEV 2 321 #define NL_GOLINE 3 322 #define NL_FORV 1 323 324 /* 325 * nlp -> nl_un.un_ptr[] subscripts for records 326 * NL_FIELDLIST the chain of fixed fields of a record, in order. 327 * the fields are also chained through ptr[NL_FIELDLIST]. 328 * this does not include the tag, or fields of variants. 329 * NL_VARNT pointer to the variants of a record, 330 * these are then chained through the .chain field. 331 * NL_VTOREC pointer from a VARNT to the RECORD that is the variant. 332 * NL_TAG pointer from a RECORD to the tagfield 333 * if there are any variants. 334 * align_info the alignment of a RECORD is in info[3]. 335 */ 336 #define NL_FIELDLIST 1 337 #define NL_VARNT 2 338 #define NL_VTOREC 2 339 #define NL_TAG 3 340 /* and align_info is info[3]. #defined above */ 341 342 #define NL_ELABEL 4 /* SCAL - ptr to definition of enums */ 343 344 /* 345 * For BADUSE nl structures, NL_KINDS is a bit vector 346 * indicating the kinds of illegal usages complained about 347 * so far. For kind of bad use "kind", "1 << kind" is set. 348 * The low bit is reserved as ISUNDEF to indicate whether 349 * this identifier is totally undefined. 350 */ 351 #define NL_KINDS 0 352 353 #define ISUNDEF 1 354 355 /* 356 * variables come in three flavors: globals, parameters, locals; 357 * they can also hide in registers, but that's a different flag 358 */ 359 #define PARAMVAR 1 360 #define LOCALVAR 2 361 #define GLOBALVAR 3 362 #define NAMEDLOCALVAR 4 363 364 /* 365 * NAMELIST CLASSES 366 * 367 * The following are the namelist classes. 368 * Different classes make use of the value fields 369 * of the namelist in different ways. 370 * 371 * The namelist should be redesigned by providing 372 * a number of structure definitions with one corresponding 373 * to each namelist class, ala a variant record in Pascal. 374 */ 375 #define BADUSE 0 376 #define CONST 1 377 #define TYPE 2 378 #define VAR 3 379 #define ARRAY 4 380 #define PTRFILE 5 381 #define RECORD 6 382 #define FIELD 7 383 #define PROC 8 384 #define FUNC 9 385 #define FVAR 10 386 #define REF 11 387 #define PTR 12 388 #define FILET 13 389 #define SET 14 390 #define RANGE 15 391 #define LABEL 16 392 #define WITHPTR 17 393 #define SCAL 18 394 #define STR 19 395 #define PROG 20 396 #define IMPROPER 21 397 #define VARNT 22 398 #define FPROC 23 399 #define FFUNC 24 400 401 /* 402 * Clnames points to an array of names for the 403 * namelist classes. 404 */ 405 char **clnames; 406 407 /* 408 * PRE-DEFINED NAMELIST OFFSETS 409 * 410 * The following are the namelist offsets for the 411 * primitive types. The ones which are negative 412 * don't actually exist, but are generated and tested 413 * internally. These definitions are sensitive to the 414 * initializations in nl.c. 415 */ 416 #define TFIRST -7 417 #define TFILE -7 418 #define TREC -6 419 #define TARY -5 420 #define TSCAL -4 421 #define TPTR -3 422 #define TSET -2 423 #define TSTR -1 424 #define NIL 0 425 #define TBOOL 1 426 #define TCHAR 2 427 #define TINT 3 428 #define TDOUBLE 4 429 #define TNIL 5 430 #define T1INT 6 431 #define T2INT 7 432 #define T4INT 8 433 #define T1CHAR 9 434 #define T1BOOL 10 435 #define T8REAL 11 436 #define TLAST 11 437 438 /* 439 * SEMANTIC DEFINITIONS 440 */ 441 442 /* 443 * NOCON and SAWCON are flags in the tree telling whether 444 * a constant set is part of an expression. 445 * these are no longer used, 446 * since we now do constant sets at compile time. 447 */ 448 #define NOCON 0 449 #define SAWCON 1 450 451 /* 452 * The variable cbn gives the current block number, 453 * the variable bn is set as a side effect of a call to 454 * lookup, and is the block number of the variable which 455 * was found. 456 */ 457 short bn, cbn; 458 459 /* 460 * The variable line is the current semantic 461 * line and is set in stat.c from the numbers 462 * embedded in statement type tree nodes. 463 */ 464 short line; 465 466 /* 467 * The size of the display 468 * which defines the maximum nesting 469 * of procedures and functions allowed. 470 * Because of the flags in the current namelist 471 * this must be no greater than 32. 472 */ 473 #define DSPLYSZ 20 474 475 /* 476 * the following structure records whether a level declares 477 * any variables which are (or contain) files. 478 * this so that the runtime routines for file cleanup can be invoked. 479 */ 480 bool dfiles[ DSPLYSZ ]; 481 482 /* 483 * Structure recording information about a constant 484 * declaration. It is actually the return value from 485 * the routine "gconst", but since C doesn't support 486 * record valued functions, this is more convenient. 487 */ 488 struct { 489 struct nl *ctype; 490 short cival; 491 double crval; 492 char *cpval; /* note used to be int * */ 493 } con; 494 495 /* 496 * The set structure records the lower bound 497 * and upper bound with the lower bound normalized 498 * to zero when working with a set. It is set by 499 * the routine setran in var.c. 500 */ 501 struct { 502 short lwrb, uprbp; 503 } set; 504 505 /* 506 * structures of this kind are filled in by precset and used by postcset 507 * to indicate things about constant sets. 508 */ 509 struct csetstr { 510 struct nl *csettype; 511 long paircnt; 512 long singcnt; 513 bool comptime; 514 }; 515 /* 516 * The following flags are passed on calls to lvalue 517 * to indicate how the reference is to affect the usage 518 * information for the variable being referenced. 519 * MOD is used to set the NMOD flag in the namelist 520 * entry for the variable, ASGN permits diagnostics 521 * to be formed when a for variable is assigned to in 522 * the range of the loop. 523 */ 524 #define NOFLAGS 0 525 #define MOD 01 526 #define ASGN 02 527 #define NOUSE 04 528 529 /* 530 * the following flags are passed to lvalue and rvalue 531 * to tell them whether an lvalue or rvalue is required. 532 * the semantics checking is done according to the function called, 533 * but for pc, lvalue may put out an rvalue by indirecting afterwards, 534 * and rvalue may stop short of putting out the indirection. 535 */ 536 #define LREQ 01 537 #define RREQ 02 538 539 double MAXINT; 540 double MININT; 541 542 /* 543 * Variables for generation of profile information. 544 * Monflg is set when we want to generate a profile. 545 * Gocnt record the total number of goto's and 546 * cnts records the current counter for generating 547 * COUNT operators. 548 */ 549 short gocnt; 550 short cnts; 551 552 /* 553 * Most routines call "incompat" rather than asking "!compat" 554 * for historical reasons. 555 */ 556 #define incompat !compat 557 558 /* 559 * Parts records which declaration parts have been seen. 560 * The grammar allows the "label" "const" "type" "var" and routine 561 * parts to be repeated and to be in any order, so that 562 * they can be detected semantically to give better 563 * error diagnostics. 564 * 565 * The flag NONLOCALVAR indicates that a non-local var has actually 566 * been used hence the display must be saved; NONLOCALGOTO indicates 567 * that a non-local goto has been done hence that a setjmp must be done. 568 */ 569 int parts[ DSPLYSZ ]; 570 571 #define LPRT 0x0001 572 #define CPRT 0x0002 573 #define TPRT 0x0004 574 #define VPRT 0x0008 575 #define RPRT 0x0010 576 577 #define NONLOCALVAR 0x0020 578 #define NONLOCALGOTO 0x0040 579 580 /* 581 * Flags for the "you used / instead of div" diagnostic 582 */ 583 bool divchk; 584 bool divflg; 585 586 bool errcnt[DSPLYSZ]; 587 588 /* 589 * Forechain links those types which are 590 * ^ sometype 591 * so that they can be evaluated later, permitting 592 * circular, recursive list structures to be defined. 593 */ 594 struct nl *forechain; 595 596 /* 597 * Withlist links all the records which are currently 598 * opened scopes because of with statements. 599 */ 600 struct nl *withlist; 601 602 struct nl *intset; 603 struct nl *input, *output; 604 struct nl *program; 605 606 /* progseen flag used by PC to determine if 607 * a routine segment is being compiled (and 608 * therefore no program statement seen) 609 */ 610 bool progseen; 611 612 613 /* 614 * STRUCTURED STATEMENT GOTO CHECKING 615 * 616 * The variable level keeps track of the current 617 * "structured statement level" when processing the statement 618 * body of blocks. This is used in the detection of goto's into 619 * structured statements in a block. 620 * 621 * Each label's namelist entry contains two pieces of information 622 * related to this check. The first `NL_GOLEV' either contains 623 * the level at which the label was declared, `NOTYET' if the label 624 * has not yet been declared, or `DEAD' if the label is dead, i.e. 625 * if we have exited the level in which the label was defined. 626 * 627 * When we discover a "goto" statement, if the label has not 628 * been defined yet, then we record the current level and the current line 629 * for a later error check. If the label has been already become "DEAD" 630 * then a reference to it is an error. Now the compiler maintains, 631 * for each block, a linked list of the labels headed by "gotos[bn]". 632 * When we exit a structured level, we perform the routine 633 * ungoto in stat.c. It notices labels whose definition levels have been 634 * exited and makes them be dead. For labels which have not yet been 635 * defined, ungoto will maintain NL_GOLEV as the minimum structured level 636 * since the first usage of the label. It is not hard to see that the label 637 * must eventually be declared at this level or an outer level to this 638 * one or a goto into a structured statement will exist. 639 */ 640 short level; 641 struct nl *gotos[DSPLYSZ]; 642 643 #define NOTYET 10000 644 #define DEAD 10000 645 646 /* 647 * Noreach is true when the next statement will 648 * be unreachable unless something happens along 649 * (like exiting a looping construct) to save 650 * the day. 651 */ 652 bool noreach; 653 654 /* 655 * UNDEFINED VARIABLE REFERENCE STRUCTURES 656 */ 657 struct udinfo { 658 int ud_line; 659 struct udinfo *ud_next; 660 char nullch; 661 }; 662 663 /* 664 * CODE GENERATION DEFINITIONS 665 */ 666 667 /* 668 * NSTAND is or'ed onto the abstract machine opcode 669 * for non-standard built-in procedures and functions. 670 */ 671 #define NSTAND 0400 672 673 #define codeon() cgenflg++ 674 #define codeoff() --cgenflg 675 #define CGENNING ( cgenflg >= 0 ) 676 677 /* 678 * Codeline is the last lino output in the code generator. 679 * It used to be used to suppress LINO operators but no 680 * more since we now count statements. 681 * Lc is the intepreter code location counter. 682 * 683 short codeline; 684 */ 685 #ifdef OBJ 686 char *lc; 687 #endif 688 689 690 /* 691 * Routines which need types 692 * other than "integer" to be 693 * assumed by the compiler. 694 */ 695 double atof(); 696 long lwidth(); 697 long leven(); 698 long aryconst(); 699 long a8tol(); 700 long roundup(); 701 struct nl *tmpalloc(); 702 struct nl *lookup(); 703 double atof(); 704 int *hash(); 705 char *alloc(); 706 int *pcalloc(); 707 char *savestr(); 708 char *esavestr(); 709 char *parnam(); 710 char *malloc(); 711 char *getlab(); 712 char *getnext(); 713 char *skipbl(); 714 char *nameof(); 715 char *pstrcpy(); 716 char *myctime(); 717 char *putlab(); 718 bool fcompat(); 719 bool constval(); 720 bool precset(); 721 bool nilfnil(); 722 struct nl *funccod(); 723 struct nl *pcfunccod(); 724 struct nl *lookup1(); 725 struct nl *hdefnl(); 726 struct nl *defnl(); 727 struct nl *flvalue(); 728 struct nl *plist(); 729 struct nl *enter(); 730 struct nl *nlcopy(); 731 struct nl *tyrec(); 732 struct nl *tyary(); 733 struct nl *tyrang(); 734 struct nl *tyscal(); 735 struct nl *deffld(); 736 struct nl *stklval(); 737 struct nl *scalar(); 738 struct nl *gen(); 739 struct nl *stkrval(); 740 struct nl *funcext(); 741 struct nl *funchdr(); 742 struct nl *funcbody(); 743 struct nl *yybaduse(); 744 struct nl *stackRV(); 745 struct nl *defvnt(); 746 struct nl *tyrec1(); 747 struct nl *reclook(); 748 struct nl *asgnop1(); 749 struct nl *gtype(); 750 struct nl *call(); 751 struct nl *lvalue(); 752 struct nl *pclvalue(); 753 struct nl *rvalue(); 754 struct nl *cset(); 755 struct tnode *newlist(); 756 struct tnode *addlist(); 757 struct tnode *fixlist(); 758 struct tnode *setupvar(); 759 struct tnode *setuptyrec(); 760 struct tnode *setupfield(); 761 struct tnode *tree(); 762 struct tnode *tree1(); 763 struct tnode *tree2(); 764 struct tnode *tree3(); 765 struct tnode *tree4(); 766 struct tnode *tree5(); 767 768 /* 769 * type cast NIL to keep lint happy (which is not so bad) 770 */ 771 #define NLNIL ( (struct nl *) NIL ) 772 #define TR_NIL ( (struct tnode *) NIL) 773 774 /* 775 * Funny structures to use 776 * pointers in wild and wooly ways 777 */ 778 struct cstruct{ 779 char pchar; 780 }; 781 struct { 782 short pint; 783 short pint2; 784 }; 785 struct lstruct { 786 long plong; 787 }; 788 struct { 789 double pdouble; 790 }; 791 792 #define OCT 1 793 #define HEX 2 794 795 /* 796 * MAIN PROGRAM VARIABLES, MISCELLANY 797 */ 798 799 /* 800 * Variables forming a data base referencing 801 * the command line arguments with the "i" option, e.g. 802 * in "pi -i scanner.i compiler.p". 803 */ 804 char **pflist; 805 short pflstc; 806 short pfcnt; 807 808 char *filename; /* current source file name */ 809 long tvec; 810 extern char *snark; /* SNARK */ 811 extern char *classes[ ]; /* maps namelist classes to string names */ 812 813 #define derror error 814 815 #ifdef PC 816 817 /* 818 * the current function number, for [ lines 819 */ 820 int ftnno; 821 822 /* 823 * the pc output stream 824 */ 825 FILE *pcstream; 826 827 #endif PC 828