home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Distributions / ucb / spencer_2bsd.tar.gz / 2bsd.tar / src / pi0 / 0.h next >
C/C++ Source or Header  |  1980-02-17  |  13KB  |  553 lines

  1. /* Copyright (c) 1979 Regents of the University of California */
  2. #include "send.h"
  3. /* #define DEBUG */
  4. #define    CHAR
  5. #define    STATIC
  6. /*
  7.  * pi - Pascal interpreter code translator
  8.  *
  9.  * Charles Haley, Bill Joy
  10.  * University of California, Berkeley (UCB)
  11.  * Version 1.2 January 1979
  12.  */
  13.  
  14. /*
  15.  * Option flags
  16.  *
  17.  * The following options are recognized in the text of the program
  18.  * and also on the command line:
  19.  *
  20.  *    b    block buffer the file output
  21.  *
  22.  *    i    make a listing of the procedures and functions in
  23.  *        the following include files
  24.  *
  25.  *    l    make a listing of the program
  26.  *
  27.  *    n    place each include file on a new page with a header
  28.  *
  29.  *    p    disable post mortem and statement limit counting
  30.  *
  31.  *    t    disable run-time tests
  32.  *
  33.  *    u    card image mode; only first 72 chars of input count
  34.  *
  35.  *    w    suppress special diagnostic warnings
  36.  *
  37.  *    z    generate counters for an execution profile
  38.  */
  39. #ifdef DEBUG
  40. char    fulltrace, errtrace, testtrace, yyunique;
  41. #endif
  42.  
  43. /*
  44.  * Each option has a stack of 17 option values, with opts giving
  45.  * the current, top value, and optstk the value beneath it.
  46.  * One refers to option `l' as, e.g., opt('l') in the text for clarity.
  47.  */
  48. char    opts[26];
  49. int    optstk[26];
  50.  
  51. #define opt(c) opts[c-'a']
  52.  
  53. /*
  54.  * Monflg is set when we are generating
  55.  * a profile
  56.  */
  57. char    monflg;
  58. /*
  59.  * NOTES ON THE DYNAMIC NATURE OF THE DATA STRUCTURES
  60.  *
  61.  * Pi uses expandable tables for
  62.  * its namelist (symbol table), string table
  63.  * hash table, and parse tree space.  The following
  64.  * definitions specify the size of the increments
  65.  * for these items in fundamental units so that
  66.  * each uses approximately 1024 bytes.
  67.  */
  68.  
  69. #define    STRINC    1024        /* string space increment */
  70. #define    TRINC    512        /* tree space increment */
  71. #define    HASHINC    509        /* hash table size in words, each increment */
  72. #define    NLINC    56        /* namelist increment size in nl structs */
  73.  
  74. /*
  75.  * The initial sizes of the structures.
  76.  * These should be large enough to compile
  77.  * an "average" sized program so as to minimize
  78.  * storage requests.
  79.  * On a small system or and 11/34 or 11/40
  80.  * these numbers can be trimmed to make the
  81.  * compiler smaller.
  82.  */
  83. #define    ITREE    512        /* Must be the same as TRINC */
  84. #define    INL    200
  85. #define    IHASH    509
  86.  
  87. /*
  88.  * The following limits on hash and tree tables currently
  89.  * allow approximately 1200 symbols and 20k words of tree
  90.  * space.  The fundamental limit of 64k total data space
  91.  * should be exceeded well before these are full.
  92.  */
  93. #define    MAXHASH    4
  94. #define    MAXNL    12
  95. #define    MAXTREE    30
  96. #define    MAXDEPTH 150
  97.  
  98. /*
  99.  * ERROR RELATED DEFINITIONS
  100.  */
  101.  
  102. /*
  103.  * Exit statuses to pexit
  104.  *
  105.  * AOK
  106.  * ERRS        Compilation errors inhibit obj productin
  107.  * NOSTART    Errors before we ever got started
  108.  * DIED        We ran out of memory or some such
  109.  */
  110. #define    AOK    0
  111. #define    ERRS    1
  112. #define    NOSTART    2
  113. #define    DIED    3
  114.  
  115. char    Recovery;
  116.  
  117. #define    eholdnl()    Eholdnl = 1
  118. #define    nocascade()    Enocascade = 1
  119.  
  120. char    Eholdnl, Enocascade;
  121.  
  122.  
  123. /*
  124.  * The flag eflg is set whenever we have a hard error.
  125.  * The character in errpfx will precede the next error message.
  126.  */
  127. int    eflg;
  128. char    errpfx;
  129.  
  130. #define    setpfx(x)    errpfx = x
  131.  
  132. #define    standard()    setpfx('s')
  133. #define    warning()    setpfx('w')
  134. #define    recovered()    setpfx('e')
  135.  
  136.  
  137. /*
  138.  * The flag syneflg is used to suppress the diagnostics of the form
  139.  *    E 10 a, defined in someprocedure, is neither used nor set
  140.  * when there were syntax errors in "someprocedure".
  141.  * In this case, it is likely that these warinings would be spurious.
  142.  */
  143. char    syneflg;
  144.  
  145. /*
  146.  * The compiler keeps its error messages in a file.
  147.  * The variable efil is the unit number on which
  148.  * this file is open for reading of error message text.
  149.  * Similarly, the file ofil is the unit of the file
  150.  * "obj" where we write the interpreter code.
  151.  */
  152. char    efil, ofil;
  153. /* int    obuf[259]; */
  154.  
  155. #define    elineoff()    Enoline++
  156. #define    elineon()    Enoline = 0
  157.  
  158. char    Enoline;
  159.  
  160. /*
  161.  * SYMBOL TABLE STRUCTURE DEFINITIONS
  162.  *
  163.  * The symbol table is henceforth referred to as the "namelist".
  164.  * It consists of a number of structures of the form "nl" below.
  165.  * These are contained in a number of segments of the symbol
  166.  * table which are dynamically allocated as needed.
  167.  * The major namelist manipulation routines are contained in the
  168.  * file "nl.c".
  169.  *
  170.  * The major components of a namelist entry are the "symbol", giving
  171.  * a pointer into the string table for the string associated with this
  172.  * entry and the "class" which tells which of the (currently 19)
  173.  * possible types of structure this is.
  174.  *
  175.  * Many of the classes use the "type" field for a pointer to the type
  176.  * which the entry has.
  177.  *
  178.  * Other pieces of information in more than one class include the block
  179.  * in which the symbol is defined, flags indicating whether the symbol
  180.  * has been used and whether it has been assigned to, etc.
  181.  *
  182.  * A more complete discussion of the features of the namelist is impossible
  183.  * here as it would be too voluminous.  Refer to the "PI 1.0 Implementation
  184.  * Notes" for more details.
  185.  */
  186.  
  187. /*
  188.  * The basic namelist structure.
  189.  * There are also two other variants, defining the real
  190.  * field as longs or integers given below.
  191.  *
  192.  * The array disptab defines the hash header for the symbol table.
  193.  * Symbols are hashed based on the low 6 bits of their pointer into
  194.  * the string table; see the routines in the file "lookup.c" and also "fdec.c"
  195.  * especially "funcend".
  196.  */
  197. struct    nl {
  198.     char    *symbol;
  199.     char    class, nl_flags;
  200.     struct    nl *type;
  201.     struct    nl *chain, *nl_next;
  202.     double    real;
  203. } nl[], *nlp, *disptab[077+1];
  204.  
  205. struct {
  206.     char    *symbol;
  207.     char    class, nl_block;
  208.     struct    nl *type;
  209.     struct    nl *chain, *nl_next;
  210.     long    range[2];
  211. };
  212.  
  213. struct {
  214.     char    *symbol;
  215.     char    class, nl_flags;
  216.     struct    nl *type;
  217.     struct    nl *chain, *nl_next;
  218.     int    value[4];
  219. };
  220.  
  221. /*
  222.  * NL FLAGS BITS
  223.  *
  224.  * Definitions of the usage of the bits in
  225.  * the nl_flags byte. Note that the low 5 bits of the
  226.  * byte are the "nl_block" and that some classes make use
  227.  * of this byte as a "width".
  228.  *
  229.  * The only non-obvious bit definition here is "NFILES"
  230.  * which records whether a structure contains any files.
  231.  * Such structures are not allowed to be dynamically allocated.
  232.  */
  233. #define    NPACKED    0200
  234. #define    NFORWD    0200
  235. #define    NFILES    0200
  236.  
  237. /*
  238.  * Definition of the commonly used "value" fields.
  239.  * The most important ones are NL_LOC which gives the location
  240.  * in the code of a label or procedure, and NL_OFFS which gives
  241.  * the offset of a variable in its stack mark.
  242.  */
  243. #define NL_OFFS    0
  244. #define NL_LOC    1
  245.  
  246. #define    NL_FVAR    3
  247.  
  248. #define NL_GOLEV 2
  249. #define NL_GOLINE 3
  250. #define NL_FORV 1
  251.  
  252. #define    NL_FLDSZ 1
  253. #define    NL_VARNT 2
  254. #define    NL_VTOREC 2
  255. #define    NL_TAG    3
  256.  
  257. /*
  258.  * For BADUSE nl structures, NL_KINDS is a bit vector
  259.  * indicating the kinds of illegal usages complained about
  260.  * so far.  For kind of bad use "kind", "1 << kind" is set.
  261.  * The low bit is reserved as ISUNDEF to indicate whether
  262.  * this identifier is totally undefined.
  263.  */
  264. #define    NL_KINDS    0
  265.  
  266. #define    ISUNDEF        1
  267.  
  268. /*
  269.  * NAMELIST CLASSES
  270.  *
  271.  * The following are the namelist classes.
  272.  * Different classes make use of the value fields
  273.  * of the namelist in different ways.
  274.  *
  275.  * The namelist should be redesigned by providing
  276.  * a number of structure definitions with one corresponding
  277.  * to each namelist class, ala a variant record in Pascal.
  278.  */
  279. #define    BADUSE    0
  280. #define    CONST    1
  281. #define    TYPE    2
  282. #define    VAR    3
  283. #define    ARRAY    4
  284. #define    PTRFILE    5
  285. #define    RECORD    6
  286. #define    FIELD    7
  287. #define    PROC    8
  288. #define    FUNC    9
  289. #define    FVAR    10
  290. #define    REF    11
  291. #define    PTR    12
  292. #define    FILE    13
  293. #define    SET    14
  294. #define    RANGE    15
  295. #define    LABEL    16
  296. #define    WITHPTR 17
  297. #define    SCAL    18
  298. #define    STR    19
  299. #define    PROG    20
  300. #define    IMPROPER 21
  301. #define    VARNT    22
  302.  
  303. /*
  304.  * Clnames points to an array of names for the
  305.  * namelist classes.
  306.  */
  307. char    **clnames;
  308.  
  309. /*
  310.  * PRE-DEFINED NAMELIST OFFSETS
  311.  *
  312.  * The following are the namelist offsets for the
  313.  * primitive types. The ones which are negative
  314.  * don't actually exist, but are generated and tested
  315.  * internally. These definitions are sensitive to the
  316.  * initializations in nl.c.
  317.  */
  318. #define    TFIRST -7
  319. #define    TFILE  -7
  320. #define    TREC   -6
  321. #define    TARY   -5
  322. #define    TSCAL  -4
  323. #define    TPTR   -3
  324. #define    TSET   -2
  325. #define    TSTR   -1
  326. #define    NIL    0
  327. #define    TBOOL    1
  328. #define    TCHAR    2
  329. #define    TINT    3
  330. #define    TDOUBLE    4
  331. #define    TNIL    5
  332. #define    T1INT    6
  333. #define    T2INT    7
  334. #define    T4INT    8
  335. #define    T1CHAR    9
  336. #define    T1BOOL    10
  337. #define    T8REAL    11
  338. #define TLAST    11
  339.  
  340. /*
  341.  * SEMANTIC DEFINITIONS
  342.  */
  343.  
  344. /*
  345.  * NOCON and SAWCON are flags in the tree telling whether
  346.  * a constant set is part of an expression.
  347.  */
  348. #define NOCON    0
  349. #define SAWCON    1
  350.  
  351. /*
  352.  * The variable cbn gives the current block number,
  353.  * the variable bn is set as a side effect of a call to
  354.  * lookup, and is the block number of the variable which
  355.  * was found.
  356.  */
  357. int    bn, cbn;
  358.  
  359. /*
  360.  * The variable line is the current semantic
  361.  * line and is set in stat.c from the numbers
  362.  * embedded in statement type tree nodes.
  363.  */
  364. int    line;
  365.  
  366. /*
  367.  * The size of the display
  368.  * which defines the maximum nesting
  369.  * of procedures and functions allowed.
  370.  * Because of the flags in the current namelist
  371.  * this must be no greater than 32.
  372.  */
  373. #define    DSPLYSZ 20
  374.  
  375. /*
  376.  * The following structure is used
  377.  * to keep track of the amount of variable
  378.  * storage required by each block.
  379.  * "Max" is the high water mark, "off"
  380.  * the current need. Temporaries for "for"
  381.  * loops and "with" statements are allocated
  382.  * in the local variable area and these
  383.  * numbers are thereby changed if necessary.
  384.  */
  385. /* struct om { */
  386. /*     long    om_off; */
  387. /*     long    om_max; */
  388. /* } sizes[DSPLYSZ]; */
  389.  
  390. /*
  391.  * Structure recording information about a constant
  392.  * declaration.  It is actually the return value from
  393.  * the routine "gconst", but since C doesn't support
  394.  * record valued functions, this is more convenient.
  395.  */
  396. struct {
  397.     int    ctype;
  398.     int    cival;
  399.     double    crval;
  400. } con;
  401.  
  402. /*
  403.  * The set structure records the lower bound
  404.  * and upper bound with the lower bound normalized
  405.  * to zero when working with a set. It is set by
  406.  * the routine setran in var.c.
  407.  */
  408. struct {
  409.     int lwrb, uprbp;
  410. } set;
  411.  
  412. /*
  413.  * The following flags are passed on calls to lvalue
  414.  * to indicate how the reference is to affect the usage
  415.  * information for the variable being referenced.
  416.  * MOD is used to set the NMOD flag in the namelist
  417.  * entry for the variable, ASGN permits diagnostics
  418.  * to be formed when a for variable is assigned to in
  419.  * the range of the loop.
  420.  */
  421. #define    NOMOD    0
  422. #define    MOD    01
  423. #define    ASGN    02
  424. #define    NOUSE    04
  425.  
  426. double    MAXINT, MININT;
  427.  
  428. /*
  429.  * Variables for generation of profile information.
  430.  * Monflg is set when we want to generate a profile.
  431.  * Gocnt record the total number of goto's and
  432.  * cnts records the current counter for generating
  433.  * COUNT operators.
  434.  */
  435. int    gocnt;
  436. int    cnts;
  437.  
  438. /*
  439.  * Most routines call "incompat" rather than asking "!compat"
  440.  * for historical reasons.
  441.  */
  442. #define incompat     !compat
  443.  
  444. /*
  445.  * Parts records which declaration parts have been seen.
  446.  * The grammar allows the "const" "type" and "var"
  447.  * parts to be repeated and to be in any order, so that
  448.  * they can be detected semantically to give better
  449.  * error diagnostics.
  450.  */
  451. int    parts;
  452.  
  453. #define    LPRT    01
  454. #define    CPRT    02
  455. #define    TPRT    04
  456. #define    VPRT    08
  457.  
  458. /*
  459.  * Flags for the "you used / instead of div" diagnostic
  460.  */
  461. /* char    divchk; */
  462. /* char    divflg; */
  463.  
  464. int    errcnt[DSPLYSZ];
  465.  
  466. /*
  467.  * Forechain links those types which are
  468.  *    ^ sometype
  469.  * so that they can be evaluated later, permitting
  470.  * circular, recursive list structures to be defined.
  471.  */
  472. struct    nl *forechain;
  473.  
  474. /*
  475.  * Withlist links all the records which are currently
  476.  * opened scopes because of with statements.
  477.  */
  478. /* struct  nl *withlist; */
  479.  
  480. char    *intset;
  481. char    *input, *output;
  482. struct    nl *program;
  483.  
  484. /*
  485.  * UNDEFINED VARIABLE REFERENCE STRUCTURES
  486.  */
  487. struct    udinfo {
  488.     int    ud_line;
  489.     struct    udinfo *ud_next;
  490.     char    nullch;
  491. };
  492.  
  493.  
  494. /*
  495.  * Routines which need types
  496.  * other than "integer" to be
  497.  * assumed by the compiler.
  498.  */
  499. /* double  atof(); */
  500. long    lwidth();
  501. long    aryconst();
  502. /* long    a8tol(); */
  503. struct    nl *lookup();
  504. double    atof();
  505. int    *tree();
  506. int    *hash();
  507. char    *alloc();
  508.  
  509. /*
  510.  * Funny structures to use
  511.  * pointers in wild and wooly ways
  512.  */
  513. struct {
  514.     char    pchar;
  515. };
  516. struct {
  517.     int    pint;
  518.     int    pint2;
  519. };
  520. struct {
  521.     long    plong;
  522. };
  523. struct {
  524.     double    pdouble;
  525. };
  526.  
  527. #define    OCT    1
  528. #define    HEX    2
  529.  
  530. /*
  531.  * MAIN PROGRAM VARIABLES, MISCELLANY
  532.  */
  533.  
  534. /*
  535.  * Variables forming a data base referencing
  536.  * the command line arguments with the "i" option, e.g.
  537.  * in "pi -i scanner.i compiler.p".
  538.  */
  539. char    **pflist;
  540. int    pflstc;
  541. int    pfcnt;
  542.  
  543. char    *filename;        /* current source file name */
  544. int    tvec[2];        /* mod time of the source file */
  545. char    snark[];        /* SNARK */
  546. char    *classes[];        /* maps namelist classes to string names */
  547. char    *errfile;
  548.  
  549. #define derror error
  550. #ifdef    DEBUG
  551. char    hp21mx;
  552. #endif
  553.