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