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

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