home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 357_01 / cstar1.exe / NODE.H < prev    next >
C/C++ Source or Header  |  1991-06-18  |  12KB  |  557 lines

  1. /*
  2.     C* -- Header file for node declarations.
  3.  
  4.     source:  node.h
  5.     started: June 20, 1986
  6.     version: March 5, 1987
  7.  
  8.     PUBLIC DOMAIN SOFTWARE
  9.  
  10.     The CSTAR program was placed in    the public domain on June 15, 1991,
  11.     by its author and sole owner,
  12.  
  13.         Edward K. Ream
  14.         1617 Monroe Street
  15.         Madison, WI 53711
  16.         (608) 257-0802
  17.  
  18.     CSTAR may be used for any commercial or non-commercial purpose.
  19.  
  20.     See cstar.h or cstar.c for a DISCLAIMER OF WARRANTIES.
  21. */
  22.  
  23. /*
  24.     ----- MACROS -----
  25. */
  26. struct mst_node {
  27.     struct mst_node * mst_next; /* Pointer to next bucket.    */
  28.     int    mst_nargs;    /* Number of args in text.    */
  29.     char *    mst_name;        /* Pointer to name.        */
  30.     char *    mst_text;        /* Pointer to replacement text. */
  31. };
  32.  
  33. /*
  34.     Global "initial undef list" used by the preprocessor.
  35. */
  36. struct u_node {
  37.     struct u_node *    u_next;
  38.     char *        u_name;
  39. };
  40.  
  41. /*
  42.     ----- PARSING -----
  43. */
  44. #define NODE_HEAD\
  45.     int            codetype;\
  46.     struct node *        nnext;\
  47.     int            nlinno;\
  48.     struct type_node *    ncltype
  49.  
  50. /*
  51.     Global describing the "current scope."
  52. */
  53. struct scope_node {
  54.     int s_scope;
  55.     struct scope_node * s_snext;
  56.     struct type_node *  s_fntype;
  57. };
  58.  
  59. /*
  60.     Define the format of symbol table nodes.
  61. */
  62. struct st_node {
  63.     struct st_node *     st_next;
  64.     char *            st_name;
  65.     char *            st_alias;
  66.     struct iblock *        st_iniz;
  67.     folded            st_offset;    /* this is NOT unsigned */
  68.     struct type_node *    st_type;
  69.     int            st_sclass;
  70.     int            st_misc;
  71. };
  72.  
  73. /*
  74.     Type is ID_TOK (this is not a st_node, and it replaces id_node
  75.     and const_node)
  76.  
  77.     Note that NODE_HEAD includes the n_cltype field.
  78.     Note that n_cid is a name IN ADDITION to the registers
  79.     designated by the register fields, and a loc_node meant
  80.     to designate a register is to have no n_cid set!
  81. */
  82. struct loc_node {
  83.     NODE_HEAD;
  84.     char        nmode;        /* EA_MODE, etc. */
  85.     char        nscflag;    /* reg2 usage/scaling indicator */
  86.     struct st_node * ncid;        /* symbolic reference, if any */
  87.     folded        nconst;        /* constant, if any */
  88.     int        nreg1;        /* register reference */
  89.     int        nreg2;        /* second register reference */
  90. };
  91.  
  92. #define n_mode     u.locn.nmode
  93. #define n_scflag u.locn.nscflag
  94. #define n_cid     u.locn.ncid
  95. #define n_const     u.locn.nconst
  96. #define n_reg1     u.locn.nreg1
  97. #define n_reg2     u.locn.nreg2
  98.  
  99. /*
  100.     Define "type nodes" used to describe a type.
  101.     These nodes are produced when declarations are parsed.
  102.     These nodes are attached to symbol table nodes.
  103.  
  104.     t_typtok: INT_TYPE, CHAR_TYPE, POINTER_TYPE, etc.
  105.     
  106.     t_link:    creates the structure (list) which describes a type.
  107.         means array-of, function-returning, pointer-to,
  108.         or element-consisting-of, and is NULL at the base type.
  109.  
  110.     t_list:    provides the "backbone" of one of three kinds of lists
  111.         depending on t_typtok:
  112.  
  113.         S_ELEMENT_TYPE:    list of elements of a structure.
  114.         U_ELEMENT_TYPE:    list of elements of a union.
  115.         D_ELEMENT_TYPE:    list of declarations.
  116.  
  117.     t_mclass:  modifier bits.  NOT storage class.
  118.         Note that CONST and VOLATILE resemble storage class in
  119.         some respects.
  120.  
  121.     t_dim:    dimension of arrays, or 1 for other base types.
  122.         For D_ELEMENT_TYPE, as for S_ELEMENT_TYPE.
  123.         For S_ELEMENT_TYPE, the "offset" of the structure element
  124.         from the beginning of the struct.
  125.         For U_ELEMENT_TYPE, the "offset", which is always 0.
  126.  
  127.     t_size:    the size of one object in bytes (what sizeof returns).
  128.         t_tsize of an array of 200 longs will be 800.
  129.         t_tsize of a struct may yield surprises because of
  130.             even-boundary padding
  131.         for elements, t_size is zero.
  132.  
  133.     t_parent: pointer to symbol table entry of an ELEMENT
  134. */
  135. struct type_node {
  136.     int        t_typtok;    /* Primary storage type     */
  137.     struct type_node * t_link;    /* Subtype tree (list)        */
  138.     struct type_node * t_list;    /* Struct/union/decl list    */
  139.     int        t_mclass;    /* Modifier bits        */
  140.     unsigned long    t_tdim;        /* Dimension/internal offsets    */    
  141.     unsigned long    t_tsize;    /* What sizeof() returns    */
  142.     struct st_node     * t_parent;    /* Element name/aggregate tag    */
  143. };
  144.  
  145. /*
  146.     Define "cons nodes" used to create trees.
  147.     They take their name from "cons cells" in the LISP language.
  148.     The n_next and n_car fields are perfectly symmetrical.
  149.  
  150.     n_type is COMMA_TOK or SEMICOLON_TOK.
  151. */
  152. struct cons_node {
  153.     NODE_HEAD;
  154.     struct node * ncar;        /* Pointer to second subtree */
  155. };
  156.  
  157. #define n_car    u.consn.ncar
  158.  
  159. /*
  160.     Define the node produced by parsing an if statement.
  161.  
  162.     n_type is K_IF.
  163. */
  164. struct if_node {
  165.     NODE_HEAD;
  166.     struct node * nibool;
  167.     struct node * nithen;
  168.     struct node * nielse;
  169. };
  170.  
  171. #define n_ibool u.ifn.nibool
  172. #define n_ithen u.ifn.nithen
  173. #define n_ielse u.ifn.nielse
  174.  
  175. /*
  176.     Define the node produce by parsing a do statement.
  177.  
  178.     n_type is K_DO.
  179. */
  180. struct do_node {
  181.     NODE_HEAD;
  182.     struct node * ndbdy;
  183.     struct node * ndbool;
  184. };
  185.  
  186. #define n_dbdy  u.don.ndbdy
  187. #define n_dbool u.don.ndbool
  188.  
  189. /*
  190.     Define the node produced by parsing a while statement.
  191.  
  192.     n_type is K_WHILE.
  193. */
  194. struct while_node {
  195.     NODE_HEAD;
  196.     struct node * nwbdy;
  197.     struct node * nwbool;
  198. };
  199.  
  200. #define n_wbdy  u.whilen.nwbdy
  201. #define n_wbool u.whilen.nwbool
  202.  
  203. /*
  204.     Define the node produced by parsing a for statement.
  205.  
  206.     n_type is K_FOR.
  207. */
  208. struct for_node {
  209.     NODE_HEAD;
  210.     struct node * nf1list;
  211.     struct node * nfbool;
  212.     struct node * nf2list;
  213.     struct node * nfbdy;
  214. };
  215.  
  216. #define n_f1list u.forn.nf1list
  217. #define n_fbool  u.forn.nfbool
  218. #define n_f2list u.forn.nf2list
  219. #define n_fbdy   u.forn.nfbdy
  220.  
  221. /*
  222.     Define the node produced by parsing a switch statement.
  223.  
  224.     n_type is K_SWITCH.
  225. */
  226. struct switch_node {
  227.     NODE_HEAD;
  228.     struct node * nsval;
  229.     struct node * nsbdy;
  230.     struct node * nslist;
  231.     struct node * nsdef;
  232. };
  233.  
  234. #define n_sval  u.switchn.nsval
  235. #define n_sbdy  u.switchn.nsbdy
  236. #define n_slist u.switchn.nslist
  237. #define n_sdef  u.switchn.nsdef
  238.  
  239. /*
  240.     Define the node produce by parsing a return statement.
  241.  
  242.     n_type is K_RETURN.
  243. */
  244. struct ret_node {
  245.     NODE_HEAD;
  246.     struct node * nrval;
  247. };
  248.  
  249. #define n_rval u.retn.nrval
  250.  
  251. /*
  252.     Define the node produced by parsing a case in a switch.
  253.  
  254.     n_type is K_CASE, K_DEFAULT.
  255. */
  256. struct case_node {
  257.     NODE_HEAD;
  258.     long        nccon;        /* case constant.      */
  259.     struct node *    nclab;        /* ptr to label node.      */
  260.     struct node *    nclist;        /* list of cases.      */
  261. };
  262.  
  263. #define n_ccon  u.casen.nccon
  264. #define n_clab  u.casen.nclab
  265. #define n_clist u.casen.nclist
  266.  
  267. /*
  268.     Define the node produced by parsing a break or continue.
  269.  
  270.     n_type is K_BREAK, K_CONTINUE.
  271. */
  272. struct bc_node {
  273.     NODE_HEAD;
  274.     struct node * nbcparent;    /* Backpointer to parent node. */
  275. };
  276.  
  277. #define n_bcparent u.bcn.nbcparent
  278.  
  279. /*
  280.     Define parse node for goto's and labels.
  281.     n_type is LABEL_TOK or K_GOTO.
  282.  
  283.     NOTE:   The PARSE node for a user label is different from
  284.         the CODE node for user and internal labels!!!
  285. */
  286. struct plabel_node {
  287.     NODE_HEAD;
  288.     struct node * nplab;
  289. };
  290.  
  291. #define n_plab u.plabeln.nplab
  292.  
  293. /* Type is Z_TOK. */
  294. struct ztok_node {
  295.     NODE_HEAD;
  296.     int        nztype;
  297.     struct node *    nzarg1;
  298. };
  299.  
  300. #define n_ztype u.ztokn.nztype
  301. #define n_zarg1 u.ztokn.nzarg1
  302.  
  303. /* Type is X_TOK ... and CC_TOK?? */
  304. struct xtok_node {
  305.     NODE_HEAD;
  306.     int        nxtype;
  307.     struct node *    nxarg1;
  308.     struct node *    nxarg2;
  309. };
  310.  
  311. #define n_xtype u.xtokn.nxtype
  312. #define n_xarg1 u.xtokn.nxarg1
  313. #define n_xarg2 u.xtokn.nxarg2
  314.  
  315. /*     
  316.     CAUTION:
  317.     g2.c (gen_pp) and exp.c (fold2) make some assumptions as 
  318.     to similarities between these op_nodes; also, the call_node
  319.     uses the same form and so must also have the flags
  320.  
  321.     Nodes of this form all contain n_oflags, and are meant to
  322.     be the is_argop() subenumeration, which is n-ary ops
  323. */
  324.  
  325. /* Type is ternary operator. */
  326. struct ternop_node {
  327.     NODE_HEAD;
  328.     int        noflags;
  329.     struct node *    narg1;
  330.     struct node *    narg2;
  331.     struct node *    narg3;
  332. };
  333.  
  334. #define n_oflags u.ternopn.noflags
  335. #define n_arg1     u.ternopn.narg1
  336. #define n_arg2     u.ternopn.narg2
  337. #define n_arg3     u.ternopn.narg3
  338.  
  339. /* Type is any binary operator. */
  340. struct binop_node {
  341.     NODE_HEAD;
  342.     int        noflags;
  343.     struct node *    narg1;
  344.     struct node *    narg2;
  345. };
  346.  
  347. /* Type is any unary operator. */
  348. struct unop_node {
  349.     NODE_HEAD;
  350.     int        noflags;
  351.     struct node *    narg1;
  352. };
  353.  
  354. /* Type is CALL_TOK, formally a binop, and actually an n-op */
  355. struct call_node {
  356.     NODE_HEAD;
  357.     int        noflags;
  358.     struct node *    narg1;    /* expression serving as name */
  359.     struct node *    narg2;    /* argument list */
  360. };
  361.  
  362. /*
  363.     ----------    CODE GENERATION    ----------
  364. */
  365.  
  366. /*
  367.     Define code nodes:
  368.  
  369.     Please NOTE:  the code sometimes uses the fact that
  370.     the c_code field is an alias for the n_type field in parse nodes.
  371.  
  372. */
  373. #define CODE_HEADER\
  374.     int        codetype;\
  375.     struct node *    cnext;\
  376.     struct node *    cback
  377.  
  378. /*
  379.     Code node for machine instructions.
  380.     Keep these with an even number of bytes.
  381.  
  382.     c_code is X_xxx.
  383. */
  384. struct code_node {
  385.     CODE_HEADER;
  386.     struct node *    carg1;    /* loc node for arg 1 */
  387.     struct node *    carg2;    /* loc node for arg 2 */
  388.     char        clen1;
  389.     char        clen2;
  390. };
  391.  
  392. #define c_arg1 u.coden.carg1
  393. #define c_arg2 u.coden.carg2
  394. #define c_len1 u.coden.clen1
  395. #define c_len2 u.coden.clen2
  396.  
  397. /*
  398.     Code node for compiler generated labels.
  399.  
  400.     c_code is O_LABEL.
  401. */
  402. struct clabel_node {
  403.     CODE_HEADER;
  404.     int    cmark;
  405.     int    crefcount;
  406.     int    clabnum;
  407. };
  408.  
  409. #define c_mark     u.clabn.cmark
  410. #define c_refcount u.clabn.crefcount
  411. #define c_labnum   u.clabn.clabnum
  412.  
  413. /*
  414.     Code node for user generated labels.
  415.  
  416.     c_code is O_ULABEL.
  417. */
  418. struct culabel_node {
  419.     CODE_HEADER;
  420.     int    cmark;
  421.     int    crefcount;
  422.     int    clabnum;
  423.     char *    clabsym;
  424. };
  425.  
  426. #define c_labsym   u.culabn.clabsym
  427.  
  428. /*
  429.     Code node for user-generated literal code.
  430.  
  431.     c_code is O_LITERAL.
  432. */
  433. struct lit_node {
  434.     CODE_HEADER;
  435.     char *    clit;
  436. };
  437.  
  438. #define c_lit   u.litn.clit
  439.  
  440. /*
  441.     Code node for line number flag
  442.  
  443.     c_code is O_LINENUM
  444. */
  445. struct line_node {
  446.     CODE_HEADER;
  447.     int    clinenum;
  448. };
  449. #define c_linenum   u.linen.clinenum
  450.  
  451. /* Define "generic node" */
  452.  
  453. /* NODE_HEAD fields. Note aliasing of n_type and c_code. */
  454. #define n_type     u.locn.codetype
  455. #define n_next     u.locn.nnext
  456. #define n_linno     u.locn.nlinno
  457. #define n_cltype u.locn.ncltype
  458.  
  459. /* CODE_HEADER fields. */
  460. #define c_code     u.coden.codetype
  461. #define c_next     u.coden.cnext
  462. #define c_back     u.coden.cback
  463.  
  464. /*
  465.     WARNING:  struct node must contain ONLY the union.
  466.           The code assumes that one can allocate any struct n mentioned
  467.           in node by calling m?_alloc(sizeof(struct n)).
  468. */
  469. struct node {
  470.     union {
  471.         /* Parse nodes. */
  472.         struct loc_node        locn;
  473.         struct cons_node    consn;
  474.         struct if_node        ifn;
  475.         struct do_node        don;
  476.         struct while_node    whilen;
  477.         struct for_node        forn;
  478.         struct switch_node    switchn;
  479.         struct ret_node        retn;
  480.         struct case_node    casen;
  481.         struct bc_node        bcn;
  482.         struct plabel_node    plabeln;
  483.         struct xtok_node    xtokn;
  484.         struct ztok_node    ztokn;
  485.         struct ternop_node    ternopn;
  486.         struct binop_node    binopn;
  487.         struct unop_node    unopn;
  488.         struct call_node    calln;
  489.  
  490.         /* Code nodes. */
  491.         struct code_node    coden;
  492.         struct clabel_node    clabn;
  493.         struct culabel_node    culabn;
  494.         struct lit_node        litn;
  495.         struct line_node    linen;
  496.     }u;
  497. };
  498.  
  499. /*
  500.     Op node for boolean generation
  501. */
  502. struct diop {
  503.     int o_true;
  504.     int o_false;
  505. };
  506.  
  507. /*
  508.     Function-body node
  509. */
  510. struct fbody {
  511.     struct node *        parse;
  512.     struct type_node *    locals;
  513.     unsigned long        lcl_size;
  514.     char *            fname;
  515.     struct type_node *    formals;
  516.     unsigned long        fml_size;
  517.     struct type_node *    ftype;
  518.     int            fclass;
  519. };
  520.  
  521. /*
  522.     Initializer block
  523. */
  524. struct iblock {
  525.     byte        itype;
  526.     byte        isize;
  527.     struct iblock *    ilink;
  528.     unsigned long    idim;
  529.     struct {
  530.         long icn;
  531.         char * ipt;    /* ekr: 3/7/89 */
  532.     } idata[/* EKR-----0----- */ 2];
  533. };
  534.  
  535. /*
  536.     Existence block, from x_exists
  537. */
  538. struct x_ex {
  539.     byte xok;
  540.     byte xlen;
  541.     int  xopcode;
  542. };
  543.  
  544. /* Tokens. */
  545.  
  546. struct t1_entry_struct {
  547.     char *    e_string;
  548.     int    e_arg1;
  549.     int    e_arg2;
  550. };
  551.  
  552. struct t1_tab_struct {
  553.     struct t1_entry_struct * t_entry;
  554.     short t_strlen;
  555.     short t_entries;
  556. };
  557.