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