home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 338_02 / expr.c < prev    next >
Text File  |  1989-10-04  |  36KB  |  948 lines

  1. #include        <stdio.h>
  2. #include        "c.h"
  3. #include        "expr.h"
  4. #include        "gen.h"
  5. #include        "cglbdec.h"
  6.  
  7. /*
  8.  *    68000 C compiler
  9.  *
  10.  *    Copyright 1984, 1985, 1986 Matthew Brandt.
  11.  *  all commercial rights reserved.
  12.  *
  13.  *    This compiler is intended as an instructive tool for personal use. Any
  14.  *    use for profit without the written consent of the author is prohibited.
  15.  *
  16.  *    This compiler may be distributed freely for non-commercial use as long
  17.  *    as this notice stays intact. Please forward any enhancements or questions
  18.  *    to:
  19.  *
  20.  *        Matthew Brandt
  21.  *        Box 920337
  22.  *        Norcross, Ga 30092
  23.  */
  24.  
  25. TYP             stdint = { bt_long, 0, 4, {0, 0}, 0, 0 };
  26. TYP             stdchar = {bt_char, 0, 1, {0, 0}, 0, 0 };
  27. TYP             stdstring = {bt_pointer, 1, 4, {0, 0}, &stdchar, 0};
  28. TYP             stdfunc = {bt_func, 1, 0, {0, 0}, &stdint, 0};
  29. extern TYP      *head;          /* shared with decl */
  30.  
  31. /*
  32.  *      expression evaluation
  33.  *
  34.  *      this set of routines builds a parse tree for an expression.
  35.  *      no code is generated for the expressions during the build,
  36.  *      this is the job of the codegen module. for most purposes
  37.  *      expression() is the routine to call. it will allow all of
  38.  *      the C operators. for the case where the comma operator is
  39.  *      not valid (function parameters for instance) call exprnc().
  40.  *
  41.  *      each of the routines returns a pointer to a describing type
  42.  *      structure. each routine also takes one parameter which is a
  43.  *      pointer to an expression node by reference (address of pointer).
  44.  *      the completed expression is returned in this pointer. all
  45.  *      routines return either a pointer to a valid type or NULL if
  46.  *      the hierarchy of the next operator is too low or the next
  47.  *      symbol is not part of an expression.
  48.  */
  49.  
  50. TYP     *expression();  /* forward declaration */
  51. TYP     *exprnc();      /* forward declaration */
  52. TYP     *unary();       /* forward declaration */
  53.  
  54. struct enode    *makenode(nt, v1, v2)
  55. /*
  56.  *      build an expression node with a node type of nt and values
  57.  *      v1 and v2.
  58.  */
  59. int                 nt;
  60. char            *v1, *v2;
  61. {       struct enode    *ep;
  62.         ep = (struct enode *)xalloc(sizeof(struct enode));
  63.         ep->nodetype = nt;
  64.         ep->constflag = 0;
  65.         ep->v.p[0] = v1;
  66.         ep->v.p[1] = v2;
  67.         return ep;
  68. }
  69.  
  70. deref(node,tp)
  71. /*
  72.  *      build the proper dereference operation for a node using the
  73.  *      type pointer tp.
  74.  */
  75. struct enode    **node;
  76. TYP             *tp;
  77. {       switch( tp->type ) {
  78.                 case bt_char:
  79.                         *node = makenode(en_b_ref,*node,0);
  80.                         tp = &stdint;
  81.                         break;
  82.                 case bt_short:
  83.                 case bt_enum:
  84.                         *node = makenode(en_w_ref,*node,0);
  85.                         tp = &stdint;
  86.                         break;
  87.                 case bt_long:
  88.                 case bt_pointer:
  89.                 case bt_unsigned:
  90.                         *node = makenode(en_l_ref,*node,0);
  91.                         break;
  92.                 default:
  93.                         error(ERR_DEREF);
  94.                         break;
  95.                 }
  96.         return tp;
  97. }
  98.  
  99. TYP     *nameref(node)
  100. /*
  101.  *      nameref will build an expression tree that references an
  102.  *      identifier. if the identifier is not in the global or
  103.  *      local symbol table then a look-ahead to the next character
  104.  *      is done and if it indicates a function call the identifier
  105.  *      is coerced to an external function name. non-value references
  106.  *      generate an additional level of indirection.
  107.  */
  108. struct enode    **node;
  109. {       SYM             *sp;
  110.         TYP             *tp;
  111.         sp = (SYM *) gsearch(lastid);
  112.         if( sp == 0 ) {
  113.                 while( isspace(lastch) )
  114.                         cgetch();
  115.                 if( lastch == '(') {
  116.                         ++global_flag;
  117.                         sp = (SYM *)xalloc(sizeof(SYM));
  118.                         sp->tp = &stdfunc;
  119.                         sp->name = litlate(lastid);
  120.                         sp->storage_class = sc_external;
  121.                         insert(sp,&gsyms);
  122.                         --global_flag;
  123.                         tp = &stdfunc;
  124.                         *node = makenode(en_nacon,sp->name,0);
  125.                         (*node)->constflag = 1;
  126.                         }
  127.                 else    {
  128.                         tp = 0;
  129.                         error(ERR_UNDEFINED);
  130.                         }
  131.                 }
  132.         else    {
  133.                 if( (tp = sp->tp) == 0 ) {
  134.                         error(ERR_UNDEFINED);
  135.                         return 0;       /* guard against untyped entries */
  136.                         }
  137.                 switch( sp->storage_class ) {
  138.                         case sc_static:
  139.                                 *node = makenode(en_labcon,sp->value.i,0);
  140.                                 (*node)->constflag = 1;
  141.                                 break;
  142.                         case sc_global:
  143.                         case sc_external:
  144.                                 *node = makenode(en_nacon,sp->name,0);
  145.                                 (*node)->constflag = 1;
  146.                                 break;
  147.                         case sc_const:
  148.                                 *node = makenode(en_icon,sp->value.i,0);
  149.                                 (*node)->constflag = 1;
  150.                                 break;
  151.                         default:        /* auto and any errors */
  152.                                 if( sp->storage_class != sc_auto)
  153.                                         error(ERR_ILLCLASS);
  154.                                 *node = makenode(en_autocon,sp->value.i,0);
  155.                                 break;
  156.                         }
  157.                 if( tp->val_flag == 0)
  158.                         tp = deref(node,tp);
  159.                 }
  160.         getsym();
  161.         return tp;
  162. }
  163.  
  164. struct enode    *parmlist()
  165. /*
  166.  *      parmlist will build a list of parameter expressions in
  167.  *      a function call and return a pointer to the last expression
  168.  *      parsed. since parameters are generally pushed from right
  169.  *      to left we get just what we asked for...
  170.  */
  171. {       struct enode    *ep1, *ep2;
  172.         ep1 = 0;
  173.         while( lastst != closepa) {
  174.                 exprnc( &ep2);          /* evaluate a parameter */
  175.                 ep1 = makenode(en_void,ep2,ep1);
  176.                 if( lastst != comma)
  177.                         break;
  178.                 getsym();
  179.                 }
  180.         return ep1;
  181. }
  182.  
  183. int     castbegin(st)
  184. /*
  185.  *      return 1 if st in set of [ kw_char, kw_short, kw_long, kw_int,
  186.  *      kw_float, kw_double, kw_struct, kw_union ]
  187.  */
  188. int     st;
  189. {       return  st == kw_char || st == kw_short || st == kw_int ||
  190.                 st == kw_long || st == kw_float || st == kw_double ||
  191.                 st == kw_struct || st == kw_union || st== kw_unsigned;
  192. }
  193.  
  194. TYP     *primary(node)
  195. /*
  196.  *      primary will parse a primary expression and set the node pointer
  197.  *      returning the type of the expression parsed. primary expressions
  198.  *      are any of:
  199.  *                      id
  200.  *                      constant
  201.  *                      string
  202.  *                      ( expression )
  203.  *                      primary[ expression ]
  204.  *                      primary.id
  205.  *                      primary->id
  206.  *                      primary( parameter list )
  207.  */
  208. struct enode    **node;
  209. {       struct enode    *pnode, *qnode, *rnode;
  210.         SYM             *sp;
  211.         TYP             *tptr;
  212.         switch( lastst ) {
  213.  
  214.                 case id:
  215.                         tptr = nameref(&pnode);
  216.                         break;
  217.                 case iconst:
  218.                         tptr = &stdint;
  219.                         pnode = makenode(en_icon,ival,0);
  220.                         pnode->constflag = 1;
  221.                         getsym();
  222.                         break;
  223.                 case sconst:
  224.                         tptr = &stdstring;
  225.                         pnode = makenode(en_labcon,stringlit(laststr),0);
  226.                         pnode->constflag = 1;
  227.                         getsym();
  228.                         break;
  229.                 case openpa:
  230.                         getsym();
  231.                         if( !castbegin(lastst) ) {
  232.                                 tptr = expression(&pnode);
  233.                                 needpunc(closepa);
  234.                                 }
  235.                         else    {       /* cast operator */
  236.                                 decl(); /* do cast declaration */
  237.                                 decl1();
  238.                                 tptr = head;
  239.                                 needpunc(closepa);
  240.                                 if( unary(&pnode) == 0 ) {
  241.                                         error(ERR_IDEXPECT);
  242.                                         tptr = 0;
  243.                                         }
  244.                                 }
  245.                         break;
  246.                 default:
  247.                         return 0;
  248.                 }
  249.         for(;;) {
  250.                 switch( lastst ) {
  251.                         case openbr:    /* build a subscript reference */
  252.                                 if( tptr->type != bt_pointer )
  253.                                         error(ERR_NOPOINTER);
  254.                                 else
  255.                                         tptr = tptr->btp;
  256.                                 getsym();
  257.                                 qnode = makenode(en_icon,tptr->size,0);
  258.                                 qnode->constflag = 1;
  259.                                 expression(&rnode);
  260. /*
  261.  *      we could check the type of the expression here...
  262.  */
  263.                                 qnode = makenode(en_mul,qnode,rnode);
  264.                                 qnode->constflag = rnode->constflag &&
  265.                                         qnode->v.p[0]->constflag;
  266.                                 pnode = makenode(en_add,qnode,pnode);
  267.                                 pnode->constflag = qnode->constflag &&
  268.                                         pnode->v.p[1]->constflag;
  269.                                 if( tptr->val_flag == 0 )
  270.                                         tptr = deref(&pnode,tptr);
  271.                                 needpunc(closebr);
  272.                                 break;
  273.                         case pointsto:
  274.                                 if( tptr->type != bt_pointer )
  275.                                         error(ERR_NOPOINTER);
  276.                                 else
  277.                                         tptr = tptr->btp;
  278.                                 if( tptr->val_flag == 0 )
  279.                                         pnode = makenode(en_l_ref,pnode,0);
  280. /*
  281.  *      fall through to dot operation
  282.  */
  283.                         case dot:
  284.                                 getsym();       /* past -> or . */
  285.                                 if( lastst != id )
  286.                                         error(ERR_IDEXPECT);
  287.                                 else    {
  288.                                         sp = search(lastid,tptr->lst.head);
  289.                                         if( sp == 0 )
  290.                                                 error(ERR_NOMEMBER);
  291.                                         else    {
  292.                                                 tptr = sp->tp;
  293.                                                 qnode = makenode(en_icon,sp->value.i,0);
  294.                                                 qnode->constflag = 1;
  295.                                                 pnode = makenode(en_add,pnode,qnode);
  296.                                                 pnode->constflag = pnode->v.p[0]->constflag;
  297.                                                 if( tptr->val_flag == 0 )
  298.                                                     tptr = deref(&pnode,tptr);
  299.                                                 }
  300.                                         getsym();       /* past id */
  301.                                         }
  302.                                 break;
  303.                         case openpa:    /* function reference */
  304.                                 getsym();
  305.                                 if( tptr->type != bt_func &&
  306.                                         tptr->type != bt_ifunc )
  307.                                         error(ERR_NOFUNC);
  308.                                 else
  309.                                         tptr = tptr->btp;
  310.                                 pnode = makenode(en_fcall,pnode,parmlist());
  311.                                 needpunc(closepa);
  312.                                 break;
  313.                         default:
  314.                                 goto fini;
  315.                         }
  316.                 }
  317. fini:   *node = pnode;
  318.         return tptr;
  319. }
  320.  
  321. int     lvalue(node)
  322. /*
  323.  *      this function returns true if the node passed is an lvalue.
  324.  *      this can be qualified by the fact that an lvalue must have
  325.  *      one of the dereference operators as it's top node.
  326.  */
  327. struct enode    *node;
  328. {       switch( node->nodetype ) {
  329.                 case en_b_ref:
  330.                 case en_w_ref:
  331.                 case en_l_ref:
  332.                 case en_ub_ref:
  333.                 case en_uw_ref:
  334.                 case en_ul_ref:
  335.                         return 1;
  336.                 case en_cbl:
  337.                 case en_cwl:
  338.                 case en_cbw:
  339.                         return lvalue(node->v.p[0]);
  340.                 }
  341.         return 0;
  342. }
  343.  
  344. TYP     *unary(node)
  345. /*
  346.  *      unary evaluates unary expressions and returns the type of the
  347.  *      expression evaluated. unary expressions are any of:
  348.  *
  349.  *                      primary
  350.  *                      primary++
  351.  *                      primary--
  352.  *                      !unary
  353.  *                      ~unary
  354.  *                      ++unary
  355.  *                      --unary
  356.  *                      -unary
  357.  *                      *unary
  358.  *                      &unary
  359.  *                      (typecast)unary
  360.  *                      sizeof(typecast)
  361.  *
  362.  */
  363. struct enode    **node;
  364. {       TYP             *tp, *tp1;
  365.         struct enode    *ep1, *ep2;
  366.         int             flag, i;
  367.         flag = 0;
  368.         switch( lastst ) {
  369.                 case autodec:
  370.                         flag = 1;
  371.                 /* fall through to common increment */
  372.                 case autoinc:
  373.                         getsym();
  374.                         tp = unary(&ep1);
  375.                         if( tp == 0 ) {
  376.                                 error(ERR_IDEXPECT);
  377.                                 return 0;
  378.                                 }
  379.                         if( lvalue(ep1)) {
  380.                                 if( tp->type == bt_pointer )
  381.                                         ep2 = makenode(en_icon,tp->btp->size,0);
  382.                                 else
  383.                                         ep2 = makenode(en_icon,1,0);
  384.                                 ep2->constflag = 1;
  385.                                 ep1 = makenode(flag ? en_assub : en_asadd,ep1,ep2);
  386.                                 }
  387.                         else
  388.                                 error(ERR_LVALUE);
  389.                         break;
  390.                 case minus:
  391.                         getsym();
  392.                         tp = unary(&ep1);
  393.                         if( tp == 0 ) {
  394.                                 error(ERR_IDEXPECT);
  395.                                 return 0;
  396.                                 }
  397.                         ep1 = makenode(en_uminus,ep1,0);
  398.                         ep1->constflag = ep1->v.p[0]->constflag;
  399.                         break;
  400.                 case not:
  401.                         getsym();
  402.                         tp = unary(&ep1);
  403.                         if( tp == 0 ) {
  404.                                 error(ERR_IDEXPECT);
  405.                                 return 0;
  406.                                 }
  407.                         ep1 = makenode(en_not,ep1,0);
  408.                         ep1->constflag = ep1->v.p[0]->constflag;
  409.                         break;
  410.                 case compl:
  411.                         getsym();
  412.                         tp = unary(&ep1);
  413.                         if( tp == 0 ) {
  414.                                 error(ERR_IDEXPECT);
  415.                                 return 0;
  416.                                 }
  417.                         ep1 = makenode(en_compl,ep1,0);
  418.                         ep1->constflag = ep1->v.p[0]->constflag;
  419.                         break;
  420.                 case star:
  421.                         getsym();
  422.                         tp = unary(&ep1);
  423.                         if( tp == 0 ) {
  424.                                 error(ERR_IDEXPECT);
  425.                                 return 0;
  426.                                 }
  427.                         if( tp->btp == 0 )
  428.                                 error(ERR_DEREF);
  429.                         else
  430.                                 tp = tp->btp;
  431.                         if( tp->val_flag == 0 )
  432.                                 tp = deref(&ep1,tp);
  433.                         break;
  434.                 case and:
  435.                         getsym();
  436.                         tp = unary(&ep1);
  437.                         if( tp == 0 ) {
  438.                                 error(ERR_IDEXPECT);
  439.                                 return 0;
  440.                                 }
  441.                         if( lvalue(ep1))
  442.                                 ep1 = ep1->v.p[0];
  443.                         tp1 = xalloc(sizeof(TYP));
  444.                         tp1->size = 4;
  445.                         tp1->type = bt_pointer;
  446.                         tp1->btp = tp;
  447.                         tp1->val_flag = 0;
  448.                         tp1->lst.head = 0;
  449.                         tp1->sname = 0;
  450.                         tp = tp1;
  451.                         break;
  452.                 case kw_sizeof:
  453.                         getsym();
  454.                         needpunc(openpa);
  455.                         decl();
  456.                         decl1();
  457.                         if( head != 0 )
  458.                                 ep1 = makenode(en_icon,head->size,0);
  459.                         else    {
  460.                                 error(ERR_IDEXPECT);
  461.                                 ep1 = makenode(en_icon,1,0);
  462.                                 }
  463.                         ep1->constflag = 1;
  464.                         tp = &stdint;
  465.                         needpunc(closepa);
  466.                         break;
  467.                 default:
  468.                         tp = primary(&ep1);
  469.                         if( tp != 0 ) {
  470.                                 if( tp->type == bt_pointer )
  471.                                         i = tp->btp->size;
  472.                                 else
  473.                                         i = 1;
  474.                                 if( lastst == autoinc) {
  475.                                         if( lvalue(ep1) )
  476.                                                 ep1 = makenode(en_ainc,ep1,i);
  477.                                         else
  478.                                                 error(ERR_LVALUE);
  479.                                         getsym();
  480.                                         }
  481.                                 else if( lastst == autodec ) {
  482.                                         if( lvalue(ep1) )
  483.                                                 ep1 = makenode(en_adec,ep1,i);
  484.                                         else
  485.                                                 error(ERR_LVALUE);
  486.                                         getsym();
  487.                                         }
  488.                                 }
  489.                         break;
  490.                 }
  491.         *node = ep1;
  492.         return tp;
  493. }
  494.  
  495. TYP     *forcefit(node1,tp1,node2,tp2)
  496. /*
  497.  *      forcefit will coerce the nodes passed into compatable
  498.  *      types and return the type of the resulting expression.
  499.  */
  500. struct enode    **node1, **node2;
  501. TYP             *tp1, *tp2;
  502. {       switch( tp1->type ) {
  503.                 case bt_char:
  504.                 case bt_short:
  505.                 case bt_long:
  506.                         if( tp2->type == bt_long ||
  507.                                 tp2->type == bt_short ||
  508.                                 tp2->type == bt_char)
  509.                                 return &stdint;
  510.                         else if( tp2->type == bt_pointer ||
  511.                                 tp2->type == bt_unsigned )
  512.                                 return tp2;
  513.                         break;
  514.                 case bt_pointer:
  515.                         if( isscalar(tp2) || tp2->type == bt_pointer)
  516.                                 return tp1;
  517.                         break;
  518.                 case bt_unsigned:
  519.                         if( tp2->type == bt_pointer )
  520.                                 return tp2;
  521.                         else if( isscalar(tp2) )
  522.                                 return tp1;
  523.                         break;
  524.                 }
  525.         error( ERR_MISMATCH );
  526.         return tp1;
  527. }
  528.  
  529. int     isscalar(tp)
  530. /*
  531.  *      this function returns true when the type of the argument is
  532.  *      one of char, short, unsigned, or long.
  533.  */
  534. TYP             *tp;
  535. {       return  tp->type == bt_char ||
  536.                 tp->type == bt_short ||
  537.                 tp->type == bt_long ||
  538.                 tp->type == bt_unsigned;
  539. }
  540.  
  541. TYP     *multops(node)
  542. /*
  543.  *      multops parses the multiply priority operators. the syntax of
  544.  *      this group is:
  545.  *
  546.  *              unary
  547.  *              multop * unary
  548.  *              multop / unary
  549.  *              multop % unary
  550.  */
  551. struct enode    **node;
  552. {       struct enode    *ep1, *ep2;
  553.         TYP             *tp1, *tp2;
  554.         int              oper;
  555.         tp1 = unary(&ep1);
  556.         if( tp1 == 0 )
  557.                 return 0;
  558.         while( lastst == star || lastst == divide || lastst == modop ) {
  559.                 oper = lastst;
  560.                 getsym();       /* move on to next unary op */
  561.                 tp2 = unary(&ep2);
  562.                 if( tp2 == 0 ) {
  563.                         error(ERR_IDEXPECT);
  564.                         *node = ep1;
  565.                         return tp1;
  566.                         }
  567.                 tp1 = forcefit(&ep1,tp1,&ep2,tp2);
  568.                 switch( oper ) {
  569.                         case star:
  570.                                 if( tp1->type == bt_unsigned )
  571.                                         ep1 = makenode(en_umul,ep1,ep2);
  572.                                 else
  573.                                         ep1 = makenode(en_mul,ep1,ep2);
  574.                                 break;
  575.                         case divide:
  576.                                 if( tp1->type == bt_unsigned )
  577.                                         ep1 = makenode(en_udiv,ep1,ep2);
  578.                                 else
  579.                                         ep1 = makenode(en_div,ep1,ep2);
  580.                                 break;
  581.                         case modop:
  582.                                 if( tp1->type == bt_unsigned )
  583.                                         ep1 = makenode(en_umod,ep1,ep2);
  584.                                 else
  585.                                         ep1 = makenode(en_mod,ep1,ep2);
  586.                                 break;
  587.                         }
  588.                 ep1->constflag = ep1->v.p[0]->constflag &&
  589.                         ep1->v.p[1]->constflag;
  590.                 }
  591.         *node = ep1;
  592.         return tp1;
  593. }
  594.  
  595. TYP     *addops(node)
  596. /*
  597.  *      addops handles the addition and subtraction operators.
  598.  */
  599. struct enode    **node;
  600. {       struct enode    *ep1, *ep2, *ep3;
  601.         TYP             *tp1, *tp2;
  602.         int             oper, i;
  603.         tp1 = multops(&ep1);
  604.         if( tp1 == 0 )
  605.                 return 0;
  606.         while( lastst == plus || lastst == minus ) {
  607.                 oper = (lastst == plus);
  608.                 getsym();
  609.                 tp2 = multops(&ep2);
  610.                 if( tp2 == 0 ) {
  611.                         error(ERR_IDEXPECT);
  612.                         *node = ep1;
  613.                         return tp1;
  614.                         }
  615.                 if( tp1->type == bt_pointer ) {
  616.                         tp2 = forcefit(0,&stdint,&ep2,tp2);
  617.                         ep3 = makenode(en_icon,tp1->btp->size,0);
  618.                         ep3->constflag = 1;
  619.                         ep2 = makenode(en_mul,ep3,ep2);
  620.                         ep2->constflag = ep2->v.p[1]->constflag;
  621.                         }
  622.                 else if( tp2->type == bt_pointer ) {
  623.                         tp1 = forcefit(0,&stdint,&ep1,tp1);
  624.                         ep3 = makenode(en_icon,tp2->btp->size,0);
  625.                         ep3->constflag = 1;
  626.                         ep1 = makenode(en_mul,ep3,ep1);
  627.                         ep1->constflag = ep1->v.p[1]->constflag;
  628.                         }
  629.                 tp1 = forcefit(&ep1,tp1,&ep2,tp2);
  630.                 ep1 = makenode( oper ? en_add : en_sub,ep1,ep2);
  631.                 ep1->constflag = ep1->v.p[0]->constflag &&
  632.                         ep1->v.p[1]->constflag;
  633.                 }
  634.         *node = ep1;
  635.         return tp1;
  636. }
  637.  
  638. TYP     *shiftop(node)
  639. /*
  640.  *      shiftop handles the shift operators << and >>.
  641.  */
  642. struct enode    **node;
  643. {       struct enode    *ep1, *ep2;
  644.         TYP             *tp1, *tp2;
  645.         int             oper;
  646.         tp1 = addops(&ep1);
  647.         if( tp1 == 0)
  648.                 return 0;
  649.         while( lastst == lshift || lastst == rshift) {
  650.                 oper = (lastst == lshift);
  651.                 getsym();
  652.                 tp2 = addops(&ep2);
  653.                 if( tp2 == 0 )
  654.                         error(ERR_IDEXPECT);
  655.                 else    {
  656.                         tp1 = forcefit(&ep1,tp1,&ep2,tp2);
  657.                         ep1 = makenode(oper ? en_lsh : en_rsh,ep1,ep2);
  658.                         ep1->constflag = ep1->v.p[0]->constflag &&
  659.                                 ep1->v.p[1]->constflag;
  660.                         }
  661.                 }
  662.         *node = ep1;
  663.         return tp1;
  664. }
  665.  
  666. TYP     *relation(node)
  667. /*
  668.  *      relation handles the relational operators < <= > and >=.
  669.  */
  670. struct enode    **node;
  671. {       struct enode    *ep1, *ep2;
  672.         TYP             *tp1, *tp2;
  673.         int             nt;
  674.         tp1 = shiftop(&ep1);
  675.         if( tp1 == 0 )
  676.                 return 0;
  677.         for(;;) {
  678.                 switch( lastst ) {
  679.  
  680.                         case lt:
  681.                                 if( tp1->type == bt_unsigned )
  682.                                         nt = en_ult;
  683.                                 else
  684.                                         nt = en_lt;
  685.                                 break;
  686.                         case gt:
  687.                                 if( tp1->type == bt_unsigned )
  688.                                         nt = en_ugt;
  689.                                 else
  690.                                         nt = en_gt;
  691.                                 break;
  692.                         case leq:
  693.                                 if( tp1->type == bt_unsigned )
  694.                                         nt = en_ule;
  695.                                 else
  696.                                         nt = en_le;
  697.                                 break;
  698.                         case geq:
  699.                                 if( tp1->type == bt_unsigned )
  700.                                         nt = en_uge;
  701.                                 else
  702.                                         nt = en_ge;
  703.                                 break;
  704.                         default:
  705.                                 goto fini;
  706.                         }
  707.                 getsym();
  708.                 tp2 = shiftop(&ep2);
  709.                 if( tp2 == 0 )
  710.                         error(ERR_IDEXPECT);
  711.                 else    {
  712.                         tp1 = forcefit(&ep1,tp1,&ep2,tp2);
  713.                         ep1 = makenode(nt,ep1,ep2);
  714.                         ep1->constflag = ep1->v.p[0]->constflag &&
  715.                                 ep1->v.p[1]->constflag;
  716.                         }
  717.                 }
  718. fini:   *node = ep1;
  719.         return tp1;
  720. }
  721.  
  722. TYP     *equalops(node)
  723. /*
  724.  *      equalops handles the equality and inequality operators.
  725.  */
  726. struct enode    **node;
  727. {       struct enode    *ep1, *ep2;
  728.         TYP             *tp1, *tp2;
  729.         int             oper;
  730.         tp1 = relation(&ep1);
  731.         if( tp1 == 0 )
  732.                 return 0;
  733.         while( lastst == eq || lastst == neq ) {
  734.                 oper = (lastst == eq);
  735.                 getsym();
  736.                 tp2 = relation(&ep2);
  737.                 if( tp2 == 0 )
  738.                         error(ERR_IDEXPECT);
  739.                 else    {
  740.                         tp1 = forcefit(&ep1,tp1,&ep2,tp2);
  741.                         ep1 = makenode( oper ? en_eq : en_ne,ep1,ep2);
  742.                         ep1->constflag = ep1->v.p[0]->constflag &&
  743.                                 ep1->v.p[1]->constflag;
  744.                         }
  745.                 }
  746.         *node = ep1;
  747.         return tp1;
  748. }
  749.  
  750. TYP     *binop(node,xfunc,nt,sy)
  751. /*
  752.  *      binop is a common routine to handle all of the legwork and
  753.  *      error checking for bitand, bitor, bitxor, andop, and orop.
  754.  */
  755. struct enode    **node;
  756. TYP             *(*xfunc)();
  757. int             nt, sy;
  758. {       struct enode    *ep1, *ep2;
  759.         TYP             *tp1, *tp2;
  760.         tp1 = (*xfunc)(&ep1);
  761.         if( tp1 == 0 )
  762.                 return 0;
  763.         while( lastst == sy ) {
  764.                 getsym();
  765.                 tp2 = (*xfunc)(&ep2);
  766.                 if( tp2 == 0 )
  767.                         error(ERR_IDEXPECT);
  768.                 else    {
  769.                         tp1 = forcefit(&ep1,tp1,&ep2,tp2);
  770.                         ep1 = makenode(nt,ep1,ep2);
  771.                         ep1->constflag = ep1->v.p[0]->constflag &&
  772.                                 ep1->v.p[1]->constflag;
  773.                         }
  774.                 }
  775.         *node = ep1;
  776.         return tp1;
  777. }
  778.  
  779. TYP     *bitand(node)
  780. /*
  781.  *      the bitwise and operator...
  782.  */
  783. struct enode    **node;
  784. {       return binop(node,equalops,en_and,and);
  785. }
  786.  
  787. TYP     *bitxor(node)
  788. struct enode    **node;
  789. {       return binop(node,bitand,en_xor,uparrow);
  790. }
  791.  
  792. TYP     *bitor(node)
  793. struct enode    **node;
  794. {       return binop(node,bitxor,en_or,or);
  795. }
  796.  
  797. TYP     *andop(node)
  798. struct enode    **node;
  799. {       return binop(node,bitor,en_land,land);
  800. }
  801.  
  802. TYP     *orop(node)
  803. struct enode    **node;
  804. {       return binop(node,andop,en_lor,lor);
  805. }
  806.  
  807. TYP     *conditional(node)
  808. /*
  809.  *      this routine processes the hook operator.
  810.  */
  811. struct enode    **node;
  812. {       TYP             *tp1, *tp2, *tp3;
  813.         struct enode    *ep1, *ep2, *ep3;
  814.         tp1 = orop(&ep1);       /* get condition */
  815.         if( tp1 == 0 )
  816.                 return 0;
  817.         if( lastst == hook ) {
  818.                 getsym();
  819.                 if( (tp2 = conditional(&ep2)) == 0) {
  820.                         error(ERR_IDEXPECT);
  821.                         goto cexit;
  822.                         }
  823.                 needpunc(colon);
  824.                 if( (tp3 = conditional(&ep3)) == 0) {
  825.                         error(ERR_IDEXPECT);
  826.                         goto cexit;
  827.                         }
  828.                 tp1 = forcefit(&ep2,tp2,&ep3,tp3);
  829.                 ep2 = makenode(en_void,ep2,ep3);
  830.                 ep1 = makenode(en_cond,ep1,ep2);
  831.                 }
  832. cexit:  *node = ep1;
  833.         return tp1;
  834. }
  835.  
  836. TYP     *asnop(node)
  837. /*
  838.  *      asnop handles the assignment operators. currently only the
  839.  *      simple assignment is implemented.
  840.  */
  841. struct enode    **node;
  842. {       struct enode    *ep1, *ep2, *ep3;
  843.         TYP             *tp1, *tp2;
  844.         int             op;
  845.         tp1 = conditional(&ep1);
  846.         if( tp1 == 0 )
  847.                 return 0;
  848.         for(;;) {
  849.                 switch( lastst ) {
  850.                         case assign:
  851.                                 op = en_assign;
  852. ascomm:                         getsym();
  853.                                 tp2 = asnop(&ep2);
  854. ascomm2:                        if( tp2 == 0 || !lvalue(ep1) )
  855.                                         error(ERR_LVALUE);
  856.                                 else    {
  857.                                         tp1 = forcefit(&ep1,tp1,&ep2,tp2);
  858.                                         ep1 = makenode(op,ep1,ep2);
  859.                                         }
  860.                                 break;
  861.                         case asplus:
  862.                                 op = en_asadd;
  863. ascomm3:                        tp2 = asnop(&ep2);
  864.                                 if( tp1->type == bt_pointer ) {
  865.                                         ep3 = makenode(en_icon,tp1->btp->size,0);
  866.                                         ep2 = makenode(en_mul,ep2,ep3);
  867.                                         }
  868.                                 goto ascomm;
  869.                         case asminus:
  870.                                 op = en_assub;
  871.                                 goto ascomm3;
  872.                         case astimes:
  873.                                 op = en_asmul;
  874.                                 goto ascomm;
  875.                         case asdivide:
  876.                                 op = en_asdiv;
  877.                                 goto ascomm;
  878.                         case asmodop:
  879.                                 op = en_asmod;
  880.                                 goto ascomm;
  881.                         case aslshift:
  882.                                 op = en_aslsh;
  883.                                 goto ascomm;
  884.                         case asrshift:
  885.                                 op = en_asrsh;
  886.                                 goto ascomm;
  887.                         case asand:
  888.                                 op = en_asand;
  889.                                 goto ascomm;
  890.                         case asor:
  891.                                 op = en_asor;
  892.                                 goto ascomm;
  893.                         default:
  894.                                 goto asexit;
  895.                         }
  896.                 }
  897. asexit: *node = ep1;
  898.         return tp1;
  899. }
  900.  
  901. TYP     *exprnc(node)
  902. /*
  903.  *      evaluate an expression where the comma operator is not legal.
  904.  */
  905. struct enode    **node;
  906. {       TYP     *tp;
  907.         tp = asnop(node);
  908.         if( tp == 0 )
  909.                 *node = 0;
  910.         return tp;
  911. }
  912.  
  913. TYP     *commaop(node)
  914. /*
  915.  *      evaluate the comma operator. comma operators are kept as
  916.  *      void nodes.
  917.  */
  918. struct enode    **node;
  919. {       TYP             *tp1;
  920.         struct enode    *ep1, *ep2;
  921.         tp1 = asnop(&ep1);
  922.         if( tp1 == 0 )
  923.                 return 0;
  924.         if( lastst == comma ) {
  925.                 tp1 = commaop(&ep2);
  926.                 if( tp1 == 0 ) {
  927.                         error(ERR_IDEXPECT);
  928.                         goto coexit;
  929.                         }
  930.                 ep1 = makenode(en_void,ep1,ep2);
  931.                 }
  932. coexit: *node = ep1;
  933.         return tp1;
  934. }
  935.  
  936. TYP     *expression(node)
  937. /*
  938.  *      evaluate an expression where all operators are legal.
  939.  */
  940. struct enode    **node;
  941. {       TYP     *tp;
  942.         tp = commaop(node);
  943.         if( tp == 0 )
  944.                 *node = 0;
  945.         return tp;
  946. }
  947.  
  948.