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