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

  1. /* Copyright (c) 1979 Regents of the University of California */
  2. #define DEBUG
  3. #define    CHAR
  4. #define    STATIC
  5. /*
  6.  * pi - Pascal interpreter code translator
  7.  *
  8.  * Charles Haley, Bill Joy
  9.  * University of California, Berkeley (UCB)
  10.  * Version 1.2 January 1979
  11.  */
  12.  
  13. /*
  14.  * Option flags
  15.  *
  16.  * The following options are recognized in the text of the program
  17.  * and also on the command line:
  18.  *
  19.  *    b    block buffer the file output
  20.  *
  21.  *    i    make a listing of the procedures and functions in
  22.  *        the following include files
  23.  *
  24.  *    l    make a listing of the program
  25.  *
  26.  *    n    place each include file on a new page with a header
  27.  *
  28.  *    p    disable post mortem and statement limit counting
  29.  *
  30.  *    t    disable run-time tests
  31.  *
  32.  *    u    card image mode; only first 72 chars of input count
  33.  *
  34.  *    w    suppress special diagnostic warnings
  35.  *
  36.  *    z    generate counters for an execution profile
  37.  */
  38. #ifdef DEBUG
  39. char    fulltrace, errtrace, testtrace, yyunique;
  40. #endif
  41.  
  42. /*
  43.  * Each option has a stack of 17 option values, with opts giving
  44.  * the current, top value, and optstk the value beneath it.
  45.  * One refers to option `l' as, e.g., opt('l') in the text for clarity.
  46.  */
  47. char    opts[26];
  48. int    optstk[26];
  49.  
  50. #define opt(c) opts[c-'a']
  51.  
  52. /*
  53.  * Monflg is set when we are generating
  54.  * a profile
  55.  */
  56. char    monflg;
  57.  
  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    2000
  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.  * When cgenflg is set code generation is suppressed.
  127.  * This happens whenver we have an error (i.e. if eflg is set)
  128.  * and when we are walking the tree to determine types only.
  129.  */
  130. int    eflg;
  131. char    errpfx;
  132.  
  133. #define    setpfx(x)    errpfx = x
  134.  
  135. #define    standard()    setpfx('s')
  136. #define    warning()    setpfx('w')
  137. #define    recovered()    setpfx('e')
  138.  
  139. int    cgenflg;
  140.  
  141.  
  142. /*
  143.  * The flag syneflg is used to suppress the diagnostics of the form
  144.  *    E 10 a, defined in someprocedure, is neither used nor set
  145.  * when there were syntax errors in "someprocedure".
  146.  * In this case, it is likely that these warinings would be spurious.
  147.  */
  148. char    syneflg;
  149.  
  150. /*
  151.  * The compiler keeps its error messages in a file.
  152.  * The variable efil is the unit number on which
  153.  * this file is open for reading of error message text.
  154.  * Similarly, the file ofil is the unit of the file
  155.  * "obj" where we write the interpreter code.
  156.  */
  157. char    efil, ofil;
  158. int    obuf[259];
  159.  
  160. #define    elineoff()    Enoline++
  161. #define    elineon()    Enoline = 0
  162.  
  163. char    Enoline;
  164.  
  165. /*
  166.  * SYMBOL TABLE STRUCTURE DEFINITIONS
  167.  *
  168.  * The symbol table is henceforth referred to as the "namelist".
  169.  * It consists of a number of structures of the form "nl" below.
  170.  * These are contained in a number of segments of the symbol
  171.  * table which are dynamically allocated as needed.
  172.  * The major namelist manipulation routines are contained in the
  173.  * file "nl.c".
  174.  *
  175.  * The major components of a namelist entry are the "symbol", giving
  176.  * a pointer into the string table for the string associated with this
  177.  * entry and the "class" which tells which of the (currently 19)
  178.  * possible types of structure this is.
  179.  *
  180.  * Many of the classes use the "type" field for a pointer to the type
  181.  * which the entry has.
  182.  *
  183.  * Other pieces of information in more than one class include the block
  184.  * in which the symbol is defined, flags indicating whether the symbol
  185.  * has been used and whether it has been assigned to, etc.
  186.  *
  187.  * A more complete discussion of the features of the namelist is impossible
  188.  * here as it would be too voluminous.  Refer to the "PI 1.0 Implementation
  189.  * Notes" for more details.
  190.  */
  191.  
  192. /*
  193.  * The basic namelist structure.
  194.  * There are also two other variants, defining the real
  195.  * field as longs or integers given below.
  196.  *
  197.  * The array disptab defines the hash header for the symbol table.
  198.  * Symbols are hashed based on the low 6 bits of their pointer into
  199.  * the string table; see the routines in the file "lookup.c" and also "fdec.c"
  200.  * especially "funcend".
  201.  */
  202. struct    nl {
  203.     char    *symbol;
  204.     char    class, nl_flags;
  205.     struct    nl *type;
  206.     struct    nl *chain, *nl_next;
  207.     double    real;
  208. } nl[], *nlp, *disptab[077+1];
  209.  
  210. struct {
  211.     char    *symbol;
  212.     char    class, nl_block;
  213.     struct    nl *type;
  214.     struct    nl *chain, *nl_next;
  215.     long    range[2];
  216. };
  217.  
  218. struct {
  219.     char    *symbol;
  220.     char    class, nl_flags;
  221.     struct    nl *type;
  222.     struct    nl *chain, *nl_next;
  223.     int    value[4];
  224. };
  225.  
  226. /*
  227.  * NL FLAGS BITS
  228.  *
  229.  * Definitions of the usage of the bits in
  230.  * the nl_flags byte. Note that the low 5 bits of the
  231.  * byte are the "nl_block" and that some classes make use
  232.  * of this byte as a "width".
  233.  *
  234.  * The only non-obvious bit definition here is "NFILES"
  235.  * which records whether a structure contains any files.
  236.  * Such structures are not allowed to be dynamically allocated.
  237.  */
  238. #define    NPACKED    0200
  239. #define    NUSED    0100
  240. #define    NMOD    0040
  241. #define    NFORWD    0200
  242. #define    NFILES    0200
  243.  
  244. /*
  245.  * Definition of the commonly used "value" fields.
  246.  * The most important ones are NL_LOC which gives the location
  247.  * in the code of a label or procedure, and NL_OFFS which gives
  248.  * the offset of a variable in its stack mark.
  249.  */
  250. #define NL_OFFS    0
  251. #define NL_LOC    1
  252.  
  253. #define    NL_FVAR    3
  254.  
  255. #define NL_GOLEV 2
  256. #define NL_GOLINE 3
  257. #define NL_FORV 1
  258.  
  259. #define    NL_FLDSZ 1
  260. #define    NL_VARNT 2
  261. #define    NL_VTOREC 2
  262. #define    NL_TAG    3
  263.  
  264. /*
  265.  * For BADUSE nl structures, NL_KINDS is a bit vector
  266.  * indicating the kinds of illegal usages complained about
  267.  * so far.  For kind of bad use "kind", "1 << kind" is set.
  268.  * The low bit is reserved as ISUNDEF to indicate whether
  269.  * this identifier is totally undefined.
  270.  */
  271. #define    NL_KINDS    0
  272.  
  273. #define    ISUNDEF        1
  274.  
  275. /*
  276.  * NAMELIST CLASSES
  277.  *
  278.  * The following are the namelist classes.
  279.  * Different classes make use of the value fields
  280.  * of the namelist in different ways.
  281.  *
  282.  * The namelist should be redesigned by providing
  283.  * a number of structure definitions with one corresponding
  284.  * to each namelist class, ala a variant record in Pascal.
  285.  */
  286. #define    BADUSE    0
  287. #define    CONST    1
  288. #define    TYPE    2
  289. #define    VAR    3
  290. #define    ARRAY    4
  291. #define    PTRFILE    5
  292. #define    RECORD    6
  293. #define    FIELD    7
  294. #define    PROC    8
  295. #define    FUNC    9
  296. #define    FVAR    10
  297. #define    REF    11
  298. #define    PTR    12
  299. #define    FILE    13
  300. #define    SET    14
  301. #define    RANGE    15
  302. #define    LABEL    16
  303. #define    WITHPTR 17
  304. #define    SCAL    18
  305. #define    STR    19
  306. #define    PROG    20
  307. #define    IMPROPER 21
  308. #define    VARNT    22
  309.  
  310. /*
  311.  * Clnames points to an array of names for the
  312.  * namelist classes.
  313.  */
  314. char    **clnames;
  315.  
  316. /*
  317.  * PRE-DEFINED NAMELIST OFFSETS
  318.  *
  319.  * The following are the namelist offsets for the
  320.  * primitive types. The ones which are negative
  321.  * don't actually exist, but are generated and tested
  322.  * internally. These definitions are sensitive to the
  323.  * initializations in nl.c.
  324.  */
  325. #define    TFIRST -7
  326. #define    TFILE  -7
  327. #define    TREC   -6
  328. #define    TARY   -5
  329. #define    TSCAL  -4
  330. #define    TPTR   -3
  331. #define    TSET   -2
  332. #define    TSTR   -1
  333. #define    NIL    0
  334. #define    TBOOL    1
  335. #define    TCHAR    2
  336. #define    TINT    3
  337. #define    TDOUBLE    4
  338. #define    TNIL    5
  339. #define    T1INT    6
  340. #define    T2INT    7
  341. #define    T4INT    8
  342. #define    T1CHAR    9
  343. #define    T1BOOL    10
  344. #define    T8REAL    11
  345. #define TLAST    11
  346.  
  347. /*
  348.  * SEMANTIC DEFINITIONS
  349.  */
  350.  
  351. /*
  352.  * NOCON and SAWCON are flags in the tree telling whether
  353.  * a constant set is part of an expression.
  354.  */
  355. #define NOCON    0
  356. #define SAWCON    1
  357.  
  358. /*
  359.  * The variable cbn gives the current block number,
  360.  * the variable bn is set as a side effect of a call to
  361.  * lookup, and is the block number of the variable which
  362.  * was found.
  363.  */
  364. int    bn, cbn;
  365.  
  366. /*
  367.  * The variable line is the current semantic
  368.  * line and is set in stat.c from the numbers
  369.  * embedded in statement type tree nodes.
  370.  */
  371. int    line;
  372.  
  373. /*
  374.  * The size of the display
  375.  * which defines the maximum nesting
  376.  * of procedures and functions allowed.
  377.  * Because of the flags in the current namelist
  378.  * this must be no greater than 32.
  379.  */
  380. #define    DSPLYSZ 20
  381.  
  382. /*
  383.  * The following structure is used
  384.  * to keep track of the amount of variable
  385.  * storage required by each block.
  386.  * "Max" is the high water mark, "off"
  387.  * the current need. Temporaries for "for"
  388.  * loops and "with" statements are allocated
  389.  * in the local variable area and these
  390.  * numbers are thereby changed if necessary.
  391.  */
  392. struct om {
  393.     long    om_off;
  394.     long    om_max;
  395. } sizes[DSPLYSZ];
  396.  
  397. /*
  398.  * Structure recording information about a constant
  399.  * declaration.  It is actually the return value from
  400.  * the routine "gconst", but since C doesn't support
  401.  * record valued functions, this is more convenient.
  402.  */
  403. struct {
  404.     int    ctype;
  405.     int    cival;
  406.     double    crval;
  407. } con;
  408.  
  409. /*
  410.  * The set structure records the lower bound
  411.  * and upper bound with the lower bound normalized
  412.  * to zero when working with a set. It is set by
  413.  * the routine setran in var.c.
  414.  */
  415. struct {
  416.     int lwrb, uprbp;
  417. } set;
  418.  
  419. /*
  420.  * The following flags are passed on calls to lvalue
  421.  * to indicate how the reference is to affect the usage
  422.  * information for the variable being referenced.
  423.  * MOD is used to set the NMOD flag in the namelist
  424.  * entry for the variable, ASGN permits diagnostics
  425.  * to be formed when a for variable is assigned to in
  426.  * the range of the loop.
  427.  */
  428. #define    NOMOD    0
  429. #define    MOD    01
  430. #define    ASGN    02
  431. #define    NOUSE    04
  432.  
  433. double    MININT;
  434. double    MAXINT;
  435.  
  436. /*
  437.  * Variables for generation of profile information.
  438.  * Monflg is set when we want to generate a profile.
  439.  * Gocnt record the total number of goto's and
  440.  * cnts records the current counter for generating
  441.  * COUNT operators.
  442.  */
  443. int    gocnt;
  444. int    cnts;
  445.  
  446. /*
  447.  * Most routines call "incompat" rather than asking "!compat"
  448.  * for historical reasons.
  449.  */
  450. #define incompat     !compat
  451.  
  452. /*
  453.  * Parts records which declaration parts have been seen.
  454.  * The grammar allows the "const" "type" and "var"
  455.  * parts to be repeated and to be in any order, so that
  456.  * they can be detected semantically to give better
  457.  * error diagnostics.
  458.  */
  459. int    parts;
  460.  
  461. #define    LPRT    01
  462. #define    CPRT    02
  463. #define    TPRT    04
  464. #define    VPRT    08
  465.  
  466. /*
  467.  * Flags for the "you used / instead of div" diagnostic
  468.  */
  469. char    divchk;
  470. char    divflg;
  471.  
  472. int    errcnt[DSPLYSZ];
  473.  
  474. /*
  475.  * Forechain links those types which are
  476.  *    ^ sometype
  477.  * so that they can be evaluated later, permitting
  478.  * circular, recursive list structures to be defined.
  479.  */
  480. struct    nl *forechain;
  481.  
  482. /*
  483.  * Withlist links all the records which are currently
  484.  * opened scopes because of with statements.
  485.  */
  486. struct    nl *withlist;
  487.  
  488. char    *intset;
  489. char    *input, *output;
  490. struct    nl *program;
  491.  
  492. /*
  493.  * STRUCTURED STATEMENT GOTO CHECKING
  494.  *
  495.  * The variable level keeps track of the current
  496.  * "structured statement level" when processing the statement
  497.  * body of blocks.  This is used in the detection of goto's into
  498.  * structured statements in a block.
  499.  *
  500.  * Each label's namelist entry contains two pieces of information
  501.  * related to this check. The first `NL_GOLEV' either contains
  502.  * the level at which the label was declared, `NOTYET' if the label
  503.  * has not yet been declared, or `DEAD' if the label is dead, i.e.
  504.  * if we have exited the level in which the label was defined.
  505.  *
  506.  * When we discover a "goto" statement, if the label has not
  507.  * been defined yet, then we record the current level and the current line
  508.  * for a later error check.  If the label has been already become "DEAD"
  509.  * then a reference to it is an error.  Now the compiler maintains,
  510.  * for each block, a linked list of the labels headed by "gotos[bn]".
  511.  * When we exit a structured level, we perform the routine
  512.  * ungoto in stat.c. It notices labels whose definition levels have been
  513.  * exited and makes them be dead. For labels which have not yet been
  514.  * defined, ungoto will maintain NL_GOLEV as the minimum structured level
  515.  * since the first usage of the label. It is not hard to see that the label
  516.  * must eventually be declared at this level or an outer level to this
  517.  * one or a goto into a structured statement will exist.
  518.  */
  519. int    level;
  520. struct    nl *gotos[DSPLYSZ];
  521.  
  522. #define    NOTYET    10000
  523. #define    DEAD    10000
  524.  
  525. /*
  526.  * Noreach is true when the next statement will
  527.  * be unreachable unless something happens along
  528.  * (like exiting a looping construct) to save
  529.  * the day.
  530.  */
  531. int    noreach;
  532.  
  533. /*
  534.  * UNDEFINED VARIABLE REFERENCE STRUCTURES
  535.  */
  536. struct    udinfo {
  537.     int    ud_line;
  538.     struct    udinfo *ud_next;
  539.     char    nullch;
  540. };
  541.  
  542. /*
  543.  * CODE GENERATION DEFINITIONS
  544.  */
  545.  
  546. /*
  547.  * NSTAND is or'ed onto the abstract machine opcode
  548.  * for non-standard built-in procedures and functions.
  549.  */
  550. #define    NSTAND    0400
  551.  
  552. #define    codeon()    cgenflg++
  553. #define    codeoff()    --cgenflg
  554.  
  555. /*
  556.  * Offsets due to the structure of the runtime stack.
  557.  * DPOFF1 is the amount of fixed storage in each block allocated
  558.  * as local variables for the runtime system.
  559.  * DPOFF2 is the size of the block mark.
  560.  */
  561. #define DPOFF1    0
  562. #define DPOFF2    16
  563.  
  564. /*
  565.  * Codeline is the last lino output in the code generator.
  566.  * It used to be used to suppress LINO operators but no
  567.  * more since we now count statements.
  568.  * Lc is the intepreter code location counter.
  569.  *
  570. int    codeline;
  571.  */
  572. char    *lc;
  573.  
  574.  
  575. /*
  576.  * Routines which need types
  577.  * other than "integer" to be
  578.  * assumed by the compiler.
  579.  */
  580. double    atof();
  581. long    lwidth();
  582. long    aryconst();
  583. long    a8tol();
  584. struct    nl *lookup();
  585. double    atof();
  586. int    *tree();
  587. int    *hash();
  588. char    *alloc();
  589.  
  590. /*
  591.  * Funny structures to use
  592.  * pointers in wild and wooly ways
  593.  */
  594. struct {
  595.     char    pchar;
  596. };
  597. struct {
  598.     int    pint;
  599.     int    pint2;
  600. };
  601. struct {
  602.     long    plong;
  603. };
  604. struct {
  605.     double    pdouble;
  606. };
  607.  
  608. #define    OCT    1
  609. #define    HEX    2
  610.  
  611. /*
  612.  * MAIN PROGRAM VARIABLES, MISCELLANY
  613.  */
  614.  
  615. /*
  616.  * Variables forming a data base referencing
  617.  * the command line arguments with the "i" option, e.g.
  618.  * in "pi -i scanner.i compiler.p".
  619.  */
  620. char    **pflist;
  621. int    pflstc;
  622. int    pfcnt;
  623.  
  624. char    *filename;        /* current source file name */
  625. int    tvec[2];        /* mod time of the source file */
  626. char    snark[];        /* SNARK */
  627. char    *classes[];        /* maps namelist classes to string names */
  628.  
  629. #define    derror error
  630. char    hp21mx;
  631.