]> git.sur5r.net Git - cc65/blob - src/cc65/expr.c
Moved verbose output to a shared module in the common/ directory.
[cc65] / src / cc65 / expr.c
1 /*
2  * expr.c
3  *
4  * Ullrich von Bassewitz, 21.06.1998
5  */
6
7
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 /* common */
14 #include "check.h"
15 #include "xmalloc.h"
16
17 /* cc65 */
18 #include "asmcode.h"
19 #include "asmlabel.h"
20 #include "codegen.h"
21 #include "datatype.h"
22 #include "declare.h"
23 #include "error.h"
24 #include "funcdesc.h"
25 #include "function.h"
26 #include "global.h"
27 #include "litpool.h"
28 #include "macrotab.h"
29 #include "preproc.h"
30 #include "scanner.h"
31 #include "stdfunc.h"
32 #include "symtab.h"
33 #include "typecmp.h"
34 #include "expr.h"
35
36
37
38 /*****************************************************************************/
39 /*                                   Data                                    */
40 /*****************************************************************************/
41
42
43
44 /* Generator attributes */
45 #define GEN_NOPUSH      0x01            /* Don't push lhs */
46
47 /* Map a generator function and its attributes to a token */
48 typedef struct {
49     unsigned char Tok;                  /* Token to map to */
50     unsigned char Flags;                /* Flags for generator function */
51     void          (*Func) (unsigned, unsigned long);    /* Generator func */
52 } GenDesc;
53
54 /* Descriptors for the operations */
55 static GenDesc GenMUL    = { TOK_STAR,          GEN_NOPUSH,     g_mul };
56 static GenDesc GenDIV    = { TOK_DIV,           GEN_NOPUSH,     g_div };
57 static GenDesc GenMOD    = { TOK_MOD,           GEN_NOPUSH,     g_mod };
58 static GenDesc GenASL    = { TOK_SHL,           GEN_NOPUSH,     g_asl };
59 static GenDesc GenASR    = { TOK_SHR,           GEN_NOPUSH,     g_asr };
60 static GenDesc GenLT     = { TOK_LT,            GEN_NOPUSH,     g_lt  };
61 static GenDesc GenLE     = { TOK_LE,            GEN_NOPUSH,     g_le  };
62 static GenDesc GenGE     = { TOK_GE,            GEN_NOPUSH,     g_ge  };
63 static GenDesc GenGT     = { TOK_GT,            GEN_NOPUSH,     g_gt  };
64 static GenDesc GenEQ     = { TOK_EQ,            GEN_NOPUSH,     g_eq  };
65 static GenDesc GenNE     = { TOK_NE,            GEN_NOPUSH,     g_ne  };
66 static GenDesc GenAND    = { TOK_AND,           GEN_NOPUSH,     g_and };
67 static GenDesc GenXOR    = { TOK_XOR,           GEN_NOPUSH,     g_xor };
68 static GenDesc GenOR     = { TOK_OR,            GEN_NOPUSH,     g_or  };
69 static GenDesc GenPASGN  = { TOK_PLUS_ASSIGN,   GEN_NOPUSH,     g_add };
70 static GenDesc GenSASGN  = { TOK_MINUS_ASSIGN,  GEN_NOPUSH,     g_sub };
71 static GenDesc GenMASGN  = { TOK_MUL_ASSIGN,    GEN_NOPUSH,     g_mul };
72 static GenDesc GenDASGN  = { TOK_DIV_ASSIGN,    GEN_NOPUSH,     g_div };
73 static GenDesc GenMOASGN = { TOK_MOD_ASSIGN,    GEN_NOPUSH,     g_mod };
74 static GenDesc GenSLASGN = { TOK_SHL_ASSIGN,    GEN_NOPUSH,     g_asl };
75 static GenDesc GenSRASGN = { TOK_SHR_ASSIGN,    GEN_NOPUSH,     g_asr };
76 static GenDesc GenAASGN  = { TOK_AND_ASSIGN,    GEN_NOPUSH,     g_and };
77 static GenDesc GenXOASGN = { TOK_XOR_ASSIGN,    GEN_NOPUSH,     g_xor };
78 static GenDesc GenOASGN  = { TOK_OR_ASSIGN,     GEN_NOPUSH,     g_or  };
79
80
81
82 /*****************************************************************************/
83 /*                             Function forwards                             */
84 /*****************************************************************************/
85
86
87
88 static int hie10 (struct expent* lval);
89 /* Handle ++, --, !, unary - etc. */
90
91
92
93 /*****************************************************************************/
94 /*                             Helper functions                              */
95 /*****************************************************************************/
96
97
98
99 static unsigned GlobalModeFlags (unsigned flags)
100 /* Return the addressing mode flags for the variable with the given flags */
101 {
102     flags &= E_MCTYPE;
103     if (flags == E_TGLAB) {
104         /* External linkage */
105         return CF_EXTERNAL;
106     } else if (flags == E_TREGISTER) {
107         /* Register variable */
108         return CF_REGVAR;
109     } else {
110         /* Static */
111         return CF_STATIC;
112     }
113 }
114
115
116
117 static int IsNullPtr (struct expent* lval)
118 /* Return true if this is the NULL pointer constant */
119 {
120     return (IsClassInt (lval->e_tptr) &&        /* Is it an int? */
121             lval->e_flags == E_MCONST &&        /* Is it constant? */
122             lval->e_const == 0);                /* And is it's value zero? */
123 }
124
125
126
127 static type* promoteint (type* lhst, type* rhst)
128 /* In an expression with two ints, return the type of the result */
129 {
130     /* Rules for integer types:
131      *   - If one of the values is a long, the result is long.
132      *   - If one of the values is unsigned, the result is also unsigned.
133      *   - Otherwise the result is an int.
134      */
135     if (IsTypeLong (lhst) || IsTypeLong (rhst)) {
136         if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
137             return type_ulong;
138         } else {
139             return type_long;
140         }
141     } else {
142         if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
143             return type_uint;
144         } else {
145             return type_int;
146         }
147     }
148 }
149
150
151
152 static unsigned typeadjust (struct expent* lhs, struct expent* rhs, int NoPush)
153 /* Adjust the two values for a binary operation. lhs is expected on stack or
154  * to be constant, rhs is expected to be in the primary register or constant.
155  * The function will put the type of the result into lhs and return the
156  * code generator flags for the operation.
157  * If NoPush is given, it is assumed that the operation does not expect the lhs
158  * to be on stack, and that lhs is in a register instead.
159  * Beware: The function does only accept int types.
160  */
161 {
162     unsigned ltype, rtype;
163     unsigned flags;
164
165     /* Get the type strings */
166     type* lhst = lhs->e_tptr;
167     type* rhst = rhs->e_tptr;
168
169     /* Generate type adjustment code if needed */
170     ltype = TypeOf (lhst);
171     if (lhs->e_flags == E_MCONST) {
172         ltype |= CF_CONST;
173     }
174     if (NoPush) {
175         /* Value is in primary register*/
176         ltype |= CF_REG;
177     }
178     rtype = TypeOf (rhst);
179     if (rhs->e_flags == E_MCONST) {
180         rtype |= CF_CONST;
181     }
182     flags = g_typeadjust (ltype, rtype);
183
184     /* Set the type of the result */
185     lhs->e_tptr = promoteint (lhst, rhst);
186
187     /* Return the code generator flags */
188     return flags;
189 }
190
191
192
193 unsigned assignadjust (type* lhst, struct expent* rhs)
194 /* Adjust the type of the right hand expression so that it can be assigned to
195  * the type on the left hand side. This function is used for assignment and
196  * for converting parameters in a function call. It returns the code generator
197  * flags for the operation. The type string of the right hand side will be
198  * set to the type of the left hand side.
199  */
200 {
201     /* Get the type of the right hand side. Treat function types as
202      * pointer-to-function
203      */
204     type* rhst = rhs->e_tptr;
205     if (IsTypeFunc (rhst)) {
206         rhst = PointerTo (rhst);
207     }
208
209     /* After calling this function, rhs will have the type of the lhs */
210     rhs->e_tptr = lhst;
211
212     /* First, do some type checking */
213     if (IsTypeVoid (lhst) || IsTypeVoid (rhst)) {
214         /* If one of the sides are of type void, output a more apropriate
215          * error message.
216          */
217         Error ("Illegal type");
218     } else if (IsClassInt (lhst)) {
219         if (IsClassPtr (rhst)) {
220             /* Pointer -> int conversion */
221             Warning ("Converting pointer to integer without a cast");
222         } else if (!IsClassInt (rhst)) {
223             Error ("Incompatible types");
224         } else {
225             /* Adjust the int types. To avoid manipulation of TOS mark lhs
226              * as const.
227              */
228             unsigned flags = TypeOf (rhst);
229             if (rhs->e_flags & E_MCONST) {
230                 flags |= CF_CONST;
231             }
232             return g_typeadjust (TypeOf (lhst) | CF_CONST, flags);
233         }
234     } else if (IsClassPtr (lhst)) {
235         if (IsClassPtr (rhst)) {
236             /* Pointer to pointer assignment is valid, if:
237              *   - both point to the same types, or
238              *   - the rhs pointer is a void pointer, or
239              *   - the lhs pointer is a void pointer.
240              */
241             if (!IsTypeVoid (Indirect (lhst)) && !IsTypeVoid (Indirect (rhst))) {
242                 /* Compare the types */
243                 switch (TypeCmp (lhst, rhst)) {
244
245                     case TC_INCOMPATIBLE:
246                         Error ("Incompatible pointer types");
247                         break;
248
249                     case TC_QUAL_DIFF:
250                         Error ("Pointer types differ in type qualifiers");
251                         break;
252
253                     default:
254                         /* Ok */
255                         break;
256                 }
257             }
258         } else if (IsClassInt (rhst)) {
259             /* Int to pointer assignment is valid only for constant zero */
260             if ((rhs->e_flags & E_MCONST) == 0 || rhs->e_const != 0) {
261                 Warning ("Converting integer to pointer without a cast");
262             }
263         } else if (IsTypeFuncPtr (lhst) && IsTypeFunc(rhst)) {
264             /* Assignment of function to function pointer is allowed, provided
265              * that both functions have the same parameter list.
266              */
267             if (TypeCmp (Indirect (lhst), rhst) < TC_EQUAL) {
268                 Error ("Incompatible types");
269             }
270         } else {
271             Error ("Incompatible types");
272         }
273     } else {
274         Error ("Incompatible types");
275     }
276
277     /* Return an int value in all cases where the operands are not both ints */
278     return CF_INT;
279 }
280
281
282
283 void DefineData (struct expent* lval)
284 /* Output a data definition for the given expression */
285 {
286     unsigned flags = lval->e_flags;
287
288     switch (flags & E_MCTYPE) {
289
290         case E_TCONST:
291             /* Number */
292             g_defdata (TypeOf (lval->e_tptr) | CF_CONST, lval->e_const, 0);
293             break;
294
295         case E_TREGISTER:
296             /* Register variable. Taking the address is usually not
297              * allowed.
298              */
299             if (!AllowRegVarAddr) {
300                 Error ("Cannot take the address of a register variable");
301             }
302             /* FALLTHROUGH */
303
304         case E_TGLAB:
305         case E_TLLAB:
306             /* Local or global symbol */
307             g_defdata (GlobalModeFlags (flags), lval->e_name, lval->e_const);
308             break;
309
310         case E_TLIT:
311             /* a literal of some kind */
312             g_defdata (CF_STATIC, LiteralLabel, lval->e_const);
313             break;
314
315         default:
316             Internal ("Unknown constant type: %04X", flags);
317     }
318 }
319
320
321
322 static void lconst (unsigned flags, struct expent* lval)
323 /* Load primary reg with some constant value. */
324 {
325     switch (lval->e_flags & E_MCTYPE) {
326
327         case E_TLOFFS:
328             g_leasp (lval->e_const);
329             break;
330
331         case E_TCONST:
332             /* Number constant */
333             g_getimmed (flags | TypeOf (lval->e_tptr) | CF_CONST, lval->e_const, 0);
334             break;
335
336         case E_TREGISTER:
337             /* Register variable. Taking the address is usually not
338              * allowed.
339              */
340             if (!AllowRegVarAddr) {
341                 Error ("Cannot take the address of a register variable");
342             }
343             /* FALLTHROUGH */
344
345         case E_TGLAB:
346         case E_TLLAB:
347             /* Local or global symbol, load address */
348             flags |= GlobalModeFlags (lval->e_flags);
349             flags &= ~CF_CONST;
350             g_getimmed (flags, lval->e_name, lval->e_const);
351             break;
352
353         case E_TLIT:
354             /* Literal string */
355             g_getimmed (CF_STATIC, LiteralLabel, lval->e_const);
356             break;
357
358         default:
359             Internal ("Unknown constant type: %04X", lval->e_flags);
360     }
361 }
362
363
364
365 static int kcalc (int tok, long val1, long val2)
366 /* Calculate an operation with left and right operand constant. */
367 {
368     switch (tok) {
369         case TOK_EQ:
370             return (val1 == val2);
371         case TOK_NE:
372             return (val1 != val2);
373         case TOK_LT:
374             return (val1 < val2);
375         case TOK_LE:
376             return (val1 <= val2);
377         case TOK_GE:
378             return (val1 >= val2);
379         case TOK_GT:
380             return (val1 > val2);
381         case TOK_OR:
382             return (val1 | val2);
383         case TOK_XOR:
384             return (val1 ^ val2);
385         case TOK_AND:
386             return (val1 & val2);
387         case TOK_SHR:
388             return (val1 >> val2);
389         case TOK_SHL:
390             return (val1 << val2);
391         case TOK_STAR:
392             return (val1 * val2);
393         case TOK_DIV:
394             if (val2 == 0) {
395                 Error ("Division by zero");
396                 return 0x7FFFFFFF;
397             }
398             return (val1 / val2);
399         case TOK_MOD:
400             if (val2 == 0) {
401                 Error ("Modulo operation with zero");
402                 return 0;
403             }
404             return (val1 % val2);
405         default:
406             Internal ("kcalc: got token 0x%X\n", tok);
407             return 0;
408     }
409 }
410
411
412
413 static GenDesc* FindGen (int Tok, GenDesc** Table)
414 {
415     GenDesc* G;
416     while ((G = *Table) != 0) {
417         if (G->Tok == Tok) {
418             return G;
419         }
420         ++Table;
421     }
422     return 0;
423 }
424
425
426
427 static int istypeexpr (void)
428 /* Return true if some sort of variable or type is waiting (helper for cast
429  * and sizeof() in hie10).
430  */
431 {
432     SymEntry* Entry;
433
434     return curtok == TOK_LPAREN && (
435             (nxttok >= TOK_FIRSTTYPE && nxttok <= TOK_LASTTYPE) ||
436             (nxttok == TOK_CONST)                               ||
437             (nxttok  == TOK_IDENT                               &&
438             (Entry = FindSym (NextTok.Ident)) != 0              &&
439             IsTypeDef (Entry))
440            );
441 }
442
443
444
445 static void PushAddr (struct expent* lval)
446 /* If the expression contains an address that was somehow evaluated,
447  * push this address on the stack. This is a helper function for all
448  * sorts of implicit or explicit assignment functions where the lvalue
449  * must be saved if it's not constant, before evaluating the rhs.
450  */
451 {
452     /* Get the address on stack if needed */
453     if (lval->e_flags != E_MREG && (lval->e_flags & E_MEXPR)) {
454         /* Push the address (always a pointer) */
455         g_push (CF_PTR, 0);
456     }
457 }
458
459
460
461 /*****************************************************************************/
462 /*                                   code                                    */
463 /*****************************************************************************/
464
465
466
467 void exprhs (unsigned flags, int k, struct expent *lval)
468 /* Put the result of an expression into the primary register */
469 {
470     int f;
471
472     f = lval->e_flags;
473     if (k) {
474         /* Dereferenced lvalue */
475         flags |= TypeOf (lval->e_tptr);
476         if (lval->e_test & E_FORCETEST) {
477             flags |= CF_TEST;
478             lval->e_test &= ~E_FORCETEST;
479         }
480         if (f & E_MGLOBAL) {    /* ref to globalvar */
481             /* Generate code */
482             flags |= GlobalModeFlags (f);
483             g_getstatic (flags, lval->e_name, lval->e_const);
484         } else if (f & E_MLOCAL) {
485             /* ref to localvar */
486             g_getlocal (flags, lval->e_const);
487         } else if (f & E_MCONST) {
488             /* ref to absolute address */
489             g_getstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
490         } else if (f == E_MEOFFS) {
491             g_getind (flags, lval->e_const);
492         } else if (f != E_MREG) {
493             g_getind (flags, 0);
494         }
495     } else if (f == E_MEOFFS) {
496         /* reference not storable */
497         flags |= TypeOf (lval->e_tptr);
498         g_inc (flags | CF_CONST, lval->e_const);
499     } else if ((f & E_MEXPR) == 0) {
500         /* Constant of some sort, load it into the primary */
501         lconst (flags, lval);
502     }
503     if (lval->e_test & E_FORCETEST) {   /* we testing this value? */
504         /* debug... */
505         AddCodeHint ("forcetest");
506         flags |= TypeOf (lval->e_tptr);
507         g_test (flags);                 /* yes, force a test */
508         lval->e_test &= ~E_FORCETEST;
509     }
510 }
511
512
513 static void callfunction (struct expent* lval)
514 /* Perform a function call.  Called from hie11, this routine will
515  * either call the named function, or if the supplied ptr is zero,
516  * will call the contents of P.
517  */
518 {
519     struct expent lval2;
520     FuncDesc*     Func;         /* Function descriptor */
521     int           Ellipsis;     /* True if we have an open param list */
522     SymEntry*     Param;        /* Current formal parameter */
523     unsigned      ParamCount;   /* Actual parameter count */
524     unsigned      ParamSize;    /* Number of parameter bytes */
525     unsigned      Flags;
526     unsigned      CFlags;
527     CodeMark      Mark;
528
529
530     /* Get a pointer to the function descriptor from the type string */
531     Func = GetFuncDesc (lval->e_tptr);
532
533     /* Initialize vars to keep gcc silent */
534     Param = 0;
535     Mark  = 0;
536
537     /* Check if this is a function pointer. If so, save it. If not, check for
538      * special known library functions that may be inlined.
539      */
540     if (lval->e_flags & E_MEXPR) {
541         /* Function pointer is in primary register, save it */
542         Mark = GetCodePos ();
543         g_save (CF_PTR);
544     } else if (InlineStdFuncs && IsStdFunc ((const char*) lval->e_name)) {
545         /* Inline this function */
546         HandleStdFunc (lval);
547         return;
548     }
549
550     /* Parse the actual parameter list */
551     ParamSize  = 0;
552     ParamCount = 0;
553     Ellipsis   = 0;
554     while (curtok != TOK_RPAREN) {
555
556         /* Add a hint for the optimizer */
557         AddCodeHint ("param:start");
558
559         /* Count arguments */
560         ++ParamCount;
561
562         /* Fetch the pointer to the next argument, check for too many args */
563         if (ParamCount <= Func->ParamCount) {
564             if (ParamCount == 1) {
565                 /* First argument */
566                 Param = Func->SymTab->SymHead;
567             } else {
568                 /* Next argument */
569                 Param = Param->NextSym;
570                 CHECK ((Param->Flags & SC_PARAM) != 0);
571             }
572         } else if (!Ellipsis) {
573             /* Too many arguments. Do we have an open param list? */
574             if ((Func->Flags & FD_ELLIPSIS) == 0) {
575                 /* End of param list reached, no ellipsis */
576                 Error ("Too many arguments in function call");
577             }
578             /* Assume an ellipsis even in case of errors to avoid an error
579              * message for each other argument.
580              */
581             Ellipsis = 1;
582         }
583
584         /* Do some optimization: If we have a constant value to push,
585          * use a special function that may optimize.
586          */
587         CFlags = CF_NONE;
588         if (!Ellipsis && SizeOf (Param->Type) == 1) {
589             CFlags = CF_FORCECHAR;
590         }
591         Flags = 0;
592         if (evalexpr (CFlags, hie1, &lval2) == 0) {
593             /* A constant value */
594             Flags |= CF_CONST;
595         }
596
597         /* If we don't have an argument spec, accept anything, otherwise
598          * convert the actual argument to the type needed.
599          */
600         if (!Ellipsis) {
601             /* Promote the argument if needed */
602             assignadjust (Param->Type, &lval2);
603
604             /* If we have a prototype, chars may be pushed as chars */
605             Flags |= CF_FORCECHAR;
606         }
607
608         /* Use the type of the argument for the push */
609         Flags |= TypeOf (lval2.e_tptr);
610
611         /* If this is a fastcall function, don't push the last argument */
612         if (ParamCount == Func->ParamCount && (Func->Flags & FD_FASTCALL) != 0) {
613             /* Just load the argument into the primary. This is only needed if
614              * we have a constant argument, otherwise the value is already in
615              * the primary.
616              */
617             if (Flags & CF_CONST) {
618                 exprhs (CF_FORCECHAR, 0, &lval2);
619             }
620         } else {
621             /* Push the argument, count the argument size */
622             g_push (Flags, lval2.e_const);
623             ParamSize += sizeofarg (Flags);
624         }
625
626         /* Add an optimizer hint */
627         AddCodeHint ("param:end");
628
629         /* Check for end of argument list */
630         if (curtok != TOK_COMMA) {
631             break;
632         }
633         NextToken ();
634     }
635
636     /* We need the closing bracket here */
637     ConsumeRParen ();
638
639     /* Check if we had enough parameters */
640     if (ParamCount < Func->ParamCount) {
641         Error ("Too few arguments in function call");
642     }
643
644     /* */
645     if (lval->e_flags & E_MEXPR) {
646         /* Function called via pointer: Restore it and call function */
647         if (ParamSize != 0) {
648             g_restore (CF_PTR);
649         } else {
650             /* We had no parameters - remove save code */
651             RemoveCode (Mark);
652         }
653         g_callind (TypeOf (lval->e_tptr), ParamSize);
654     } else {
655         g_call (TypeOf (lval->e_tptr), (char*) lval->e_name, ParamSize);
656     }
657 }
658
659
660
661 void doasm (void)
662 /* This function parses ASM statements. The syntax of the ASM directive
663  * looks like the one defined for C++ (C has no ASM directive), that is,
664  * a string literal in parenthesis.
665  */
666 {
667     /* Skip the ASM */
668     NextToken ();
669
670     /* Need left parenthesis */
671     ConsumeLParen ();
672
673     /* String literal */
674     if (curtok != TOK_SCONST) {
675         Error ("String literal expected");
676     } else {
677         /* Write the string directly into the output, followed by a newline */
678         AddCodeLine (GetLiteral (curval));
679
680         /* Reset the string pointer, effectivly clearing the string from the
681          * string table. Since we're working with one token lookahead, this
682          * will fail if the next token is also a string token, but that's a
683          * syntax error anyway, because we expect a right paren.
684          */
685         ResetLiteralOffs (curval);
686     }
687
688     /* Skip the string token */
689     NextToken ();
690
691     /* Closing paren needed */
692     ConsumeRParen ();
693 }
694
695
696
697 static int primary (struct expent* lval)
698 /* This is the lowest level of the expression parser. */
699 {
700     int k;
701
702     /* not a test at all, yet */
703     lval->e_test = 0;
704
705     /* Character and integer constants. */
706     if (curtok == TOK_ICONST || curtok == TOK_CCONST) {
707         lval->e_flags = E_MCONST | E_TCONST;
708         lval->e_tptr  = curtype;
709         lval->e_const = curval;
710         NextToken ();
711         return 0;
712     }
713
714     /* Process parenthesized subexpression by calling the whole parser
715      * recursively.
716      */
717     if (curtok == TOK_LPAREN) {
718         NextToken ();
719         memset (lval, 0, sizeof (*lval));       /* Remove any attributes */
720         k = hie0 (lval);
721         ConsumeRParen ();
722         return k;
723     }
724
725     /* All others may only be used if the expression evaluation is not called
726      * recursively by the preprocessor.
727      */
728     if (Preprocessing) {
729         /* Illegal expression in PP mode */
730         Error ("Preprocessor expression expected");
731         lval->e_flags = E_MCONST;
732         lval->e_tptr = type_int;
733         return 0;
734     }
735
736     /* Identifier? */
737     if (curtok == TOK_IDENT) {
738
739         SymEntry* Sym;
740         ident Ident;
741
742         /* Get a pointer to the symbol table entry */
743         Sym = FindSym (CurTok.Ident);
744
745         /* Is the symbol known? */
746         if (Sym) {
747
748             /* We found the symbol - skip the name token */
749             NextToken ();
750
751             /* The expression type is the symbol type */
752             lval->e_tptr = Sym->Type;
753
754             /* Check for illegal symbol types */
755             CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
756             if (Sym->Flags & SC_TYPE) {
757                 /* Cannot use type symbols */
758                 Error ("Variable identifier expected");
759                 /* Assume an int type to make lval valid */
760                 lval->e_flags = E_MLOCAL | E_TLOFFS;
761                 lval->e_tptr = type_int;
762                 lval->e_const = 0;
763                 return 0;
764             }
765
766             /* Check for legal symbol types */
767             if ((Sym->Flags & SC_ENUM) == SC_ENUM) {
768                 lval->e_flags = E_MCONST;
769                 lval->e_const = Sym->V.EnumVal;
770                 return 0;
771             } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
772                 /* Function */
773                 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
774                 lval->e_name = (unsigned long) Sym->Name;
775                 lval->e_const = 0;
776             } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
777                 /* Local variable */
778                 lval->e_flags = E_MLOCAL | E_TLOFFS;
779                 lval->e_const = Sym->V.Offs;
780             } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
781                 /* Static variable */
782                 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
783                     lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
784                     lval->e_name = (unsigned long) Sym->Name;
785                 } else {
786                     lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
787                     lval->e_name = Sym->V.Label;
788                 }
789                 lval->e_const = 0;
790             } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
791                 /* Register variable, zero page based */
792                 lval->e_flags = E_MGLOBAL | E_MCONST | E_TREGISTER;
793                 lval->e_name  = Sym->V.Offs;
794                 lval->e_const = 0;
795             } else {
796                 /* Local static variable */
797                 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
798                 lval->e_name  = Sym->V.Offs;
799                 lval->e_const = 0;
800             }
801
802             /* The symbol is referenced now */
803             Sym->Flags |= SC_REF;
804             if (IsTypeFunc (lval->e_tptr) || IsTypeArray (lval->e_tptr)) {
805                 return 0;
806             }
807             return 1;
808         }
809
810         /* We did not find the symbol. Remember the name, then skip it */
811         strcpy (Ident, CurTok.Ident);
812         NextToken ();
813
814         /* IDENT is either an auto-declared function or an undefined variable. */
815         if (curtok == TOK_LPAREN) {
816             /* Declare a function returning int. For that purpose, prepare a
817              * function signature for a function having an empty param list
818              * and returning int.
819              */
820             Warning ("Function call without a prototype");
821             Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
822             lval->e_tptr  = Sym->Type;
823             lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
824             lval->e_name  = (unsigned long) Sym->Name;
825             lval->e_const = 0;
826             return 0;
827
828         } else {
829
830             /* Undeclared Variable */
831             Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
832             lval->e_flags = E_MLOCAL | E_TLOFFS;
833             lval->e_tptr = type_int;
834             lval->e_const = 0;
835             Error ("Undefined symbol: `%s'", Ident);
836             return 1;
837
838         }
839     }
840
841     /* String literal? */
842     if (curtok == TOK_SCONST) {
843         lval->e_flags = E_MCONST | E_TLIT;
844         lval->e_const = curval;
845         lval->e_tptr  = GetCharArrayType (strlen (GetLiteral (curval)));
846         NextToken ();
847         return 0;
848     }
849
850     /* ASM statement? */
851     if (curtok == TOK_ASM) {
852         doasm ();
853         lval->e_tptr  = type_void;
854         lval->e_flags = E_MEXPR;
855         lval->e_const = 0;
856         return 0;
857     }
858
859     /* __AX__ and __EAX__ pseudo values? */
860     if (curtok == TOK_AX || curtok == TOK_EAX) {
861         lval->e_tptr  = (curtok == TOK_AX)? type_uint : type_ulong;
862         lval->e_flags = E_MREG;
863         lval->e_test &= ~E_CC;
864         lval->e_const = 0;
865         NextToken ();
866         return 1;               /* May be used as lvalue */
867     }
868
869     /* Illegal primary. */
870     Error ("Expression expected");
871     lval->e_flags = E_MCONST;
872     lval->e_tptr = type_int;
873     return 0;
874 }
875
876
877
878 static int arrayref (int k, struct expent* lval)
879 /* Handle an array reference */
880 {
881     unsigned lflags;
882     unsigned rflags;
883     int ConstBaseAddr;
884     int ConstSubAddr;
885     int l;
886     struct expent lval2;
887     CodeMark Mark1;
888     CodeMark Mark2;
889     type* tptr1;
890     type* tptr2;
891
892
893     /* Skip the bracket */
894     NextToken ();
895
896     /* Get the type of left side */
897     tptr1 = lval->e_tptr;
898
899     /* We can apply a special treatment for arrays that have a const base
900      * address. This is true for most arrays and will produce a lot better
901      * code. Check if this is a const base address.
902      */
903     lflags = lval->e_flags & ~E_MCTYPE;
904     ConstBaseAddr = (lflags == E_MCONST)       || /* Constant numeric address */
905                      (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
906                      lflags == E_MLOCAL;          /* Local array */
907
908     /* If we have a constant base, we delay the address fetch */
909     Mark1 = GetCodePos ();
910     Mark2 = 0;          /* Silence gcc */
911     if (!ConstBaseAddr) {
912         /* Get a pointer to the array into the primary */
913         exprhs (CF_NONE, k, lval);
914
915         /* Get the array pointer on stack. Do not push more than 16
916          * bit, even if this value is greater, since we cannot handle
917          * other than 16bit stuff when doing indexing.
918          */
919         Mark2 = GetCodePos ();
920         g_push (CF_PTR, 0);
921     }
922
923     /* TOS now contains ptr to array elements. Get the subscript. */
924     l = hie0 (&lval2);
925     if (l == 0 && lval2.e_flags == E_MCONST) {
926
927         /* The array subscript is a constant - remove value from stack */
928         if (!ConstBaseAddr) {
929             RemoveCode (Mark2);
930             pop (CF_PTR);
931         } else {
932             /* Get an array pointer into the primary */
933             exprhs (CF_NONE, k, lval);
934         }
935
936         if (IsClassPtr (tptr1)) {
937
938             /* Scale the subscript value according to element size */
939             lval2.e_const *= PSizeOf (tptr1);
940
941             /* Remove code for lhs load */
942             RemoveCode (Mark1);
943
944             /* Handle constant base array on stack. Be sure NOT to
945              * handle pointers the same way, this won't work.
946              */
947             if (IsTypeArray (tptr1) &&
948                 ((lval->e_flags & ~E_MCTYPE) == E_MCONST ||
949                 (lval->e_flags & ~E_MCTYPE) == E_MLOCAL ||
950                 (lval->e_flags & E_MGLOBAL) != 0 ||
951                 (lval->e_flags == E_MEOFFS))) {
952                 lval->e_const += lval2.e_const;
953
954             } else {
955                 /* Pointer - load into primary and remember offset */
956                 if ((lval->e_flags & E_MEXPR) == 0 || k != 0) {
957                     exprhs (CF_NONE, k, lval);
958                 }
959                 lval->e_const = lval2.e_const;
960                 lval->e_flags = E_MEOFFS;
961             }
962
963             /* Result is of element type */
964             lval->e_tptr = Indirect (tptr1);
965
966             /* Done */
967             goto end_array;
968
969         } else if (IsClassPtr (tptr2 = lval2.e_tptr)) {
970             /* Subscript is pointer, get element type */
971             lval2.e_tptr = Indirect (tptr2);
972
973             /* Scale the rhs value in the primary register */
974             g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
975             /* */
976             lval->e_tptr = lval2.e_tptr;
977         } else {
978             Error ("Cannot subscript");
979         }
980
981         /* Add the subscript. Since arrays are indexed by integers,
982          * we will ignore the true type of the subscript here and
983          * use always an int.
984          */
985         g_inc (CF_INT | CF_CONST, lval2.e_const);
986
987     } else {
988
989         /* Array subscript is not constant. Load it into the primary */
990         Mark2 = GetCodePos ();
991         exprhs (CF_NONE, l, &lval2);
992
993         tptr2 = lval2.e_tptr;
994         if (IsClassPtr (tptr1)) {
995
996             /* Get the element type */
997             lval->e_tptr = Indirect (tptr1);
998
999             /* Indexing is based on int's, so we will just use the integer
1000              * portion of the index (which is in (e)ax, so there's no further
1001              * action required).
1002              */
1003             g_scale (CF_INT, SizeOf (lval->e_tptr));
1004
1005         } else if (IsClassPtr (tptr2)) {
1006
1007             /* Get the element type */
1008             lval2.e_tptr = Indirect (tptr2);
1009
1010             /* Get the int value on top. If we go here, we're sure,
1011              * both values are 16 bit (the first one was truncated
1012              * if necessary and the second one is a pointer).
1013              * Note: If ConstBaseAddr is true, we don't have a value on
1014              * stack, so to "swap" both, just push the subscript.
1015              */
1016             if (ConstBaseAddr) {
1017                 g_push (CF_INT, 0);
1018                 exprhs (CF_NONE, k, lval);
1019                 ConstBaseAddr = 0;
1020             } else {
1021                 g_swap (CF_INT);
1022             }
1023
1024             /* Scale it */
1025             g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
1026             lval->e_tptr = lval2.e_tptr;
1027         } else {
1028             Error ("Cannot subscript");
1029         }
1030
1031         /* The offset is now in the primary register. It didn't have a
1032          * constant base address for the lhs, the lhs address is already
1033          * on stack, and we must add the offset. If the base address was
1034          * constant, we call special functions to add the address to the
1035          * offset value.
1036          */
1037         if (!ConstBaseAddr) {
1038             /* Add the subscript. Both values are int sized. */
1039             g_add (CF_INT, 0);
1040         } else {
1041
1042             /* If the subscript has itself a constant address, it is often
1043              * a better idea to reverse again the order of the evaluation.
1044              * This will generate better code if the subscript is a byte
1045              * sized variable. But beware: This is only possible if the
1046              * subscript was not scaled, that is, if this was a byte array
1047              * or pointer.
1048              */
1049             rflags = lval2.e_flags & ~E_MCTYPE;
1050             ConstSubAddr = (rflags == E_MCONST)       || /* Constant numeric address */
1051                             (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1052                             rflags == E_MLOCAL;          /* Local array */
1053
1054             if (ConstSubAddr && SizeOf (lval->e_tptr) == 1) {
1055
1056                 type* SavedType;
1057
1058                 /* Reverse the order of evaluation */
1059                 unsigned flags = (SizeOf (lval2.e_tptr) == 1)? CF_CHAR : CF_INT;
1060                 RemoveCode (Mark2);
1061
1062                 /* Get a pointer to the array into the primary. We have changed
1063                  * e_tptr above but we need the original type to load the
1064                  * address, so restore it temporarily.
1065                  */
1066                 SavedType = lval->e_tptr;
1067                 lval->e_tptr = tptr1;
1068                 exprhs (CF_NONE, k, lval);
1069                 lval->e_tptr = SavedType;
1070
1071                 /* Add the variable */
1072                 if (rflags == E_MLOCAL) {
1073                     g_addlocal (flags, lval2.e_const);
1074                 } else {
1075                     flags |= GlobalModeFlags (lval2.e_flags);
1076                     g_addstatic (flags, lval2.e_name, lval2.e_const);
1077                 }
1078             } else {
1079                 if (lflags == E_MCONST) {
1080                     /* Constant numeric address. Just add it */
1081                     g_inc (CF_INT | CF_UNSIGNED, lval->e_const);
1082                 } else if (lflags == E_MLOCAL) {
1083                     /* Base address is a local variable address */
1084                     if (IsTypeArray (tptr1)) {
1085                         g_addaddr_local (CF_INT, lval->e_const);
1086                     } else {
1087                         g_addlocal (CF_PTR, lval->e_const);
1088                     }
1089                 } else {
1090                     /* Base address is a static variable address */
1091                     unsigned flags = CF_INT;
1092                     flags |= GlobalModeFlags (lval->e_flags);
1093                     if (IsTypeArray (tptr1)) {
1094                         g_addaddr_static (flags, lval->e_name, lval->e_const);
1095                     } else {
1096                         g_addstatic (flags, lval->e_name, lval->e_const);
1097                     }
1098                 }
1099             }
1100         }
1101     }
1102     lval->e_flags = E_MEXPR;
1103 end_array:
1104     ConsumeRBrack ();
1105     return !IsTypeArray (lval->e_tptr);
1106
1107 }
1108
1109
1110
1111 static int structref (int k, struct expent* lval)
1112 /* Process struct field after . or ->. */
1113 {
1114     ident Ident;
1115     SymEntry* Field;
1116     int flags;
1117
1118     /* Skip the token and check for an identifier */
1119     NextToken ();
1120     if (curtok != TOK_IDENT) {
1121         Error ("Identifier expected");
1122         lval->e_tptr = type_int;
1123         return 0;
1124     }
1125
1126     /* Get the symbol table entry and check for a struct field */
1127     strcpy (Ident, CurTok.Ident);
1128     NextToken ();
1129     Field = FindStructField (lval->e_tptr, Ident);
1130     if (Field == 0) {
1131         Error ("Struct/union has no field named `%s'", Ident);
1132         lval->e_tptr = type_int;
1133         return 0;
1134     }
1135
1136     /* If we have constant input data, the result is also constant */
1137     flags = lval->e_flags & ~E_MCTYPE;
1138     if (flags == E_MCONST ||
1139         (k == 0 && (flags == E_MLOCAL ||
1140                     (flags & E_MGLOBAL) != 0 ||
1141                     lval->e_flags  == E_MEOFFS))) {
1142         lval->e_const += Field->V.Offs;
1143     } else {
1144         if ((flags & E_MEXPR) == 0 || k != 0) {
1145             exprhs (CF_NONE, k, lval);
1146         }
1147         lval->e_const = Field->V.Offs;
1148         lval->e_flags = E_MEOFFS;
1149     }
1150     lval->e_tptr = Field->Type;
1151     return !IsTypeArray (Field->Type);
1152 }
1153
1154
1155
1156 static int hie11 (struct expent *lval)
1157 /* Handle compound types (structs and arrays) */
1158 {
1159     int k;
1160     type* tptr;
1161
1162
1163     k = primary (lval);
1164     if (curtok < TOK_LBRACK || curtok > TOK_PTR_REF) {
1165         /* Not for us */
1166         return k;
1167     }
1168
1169     while (1) {
1170
1171         if (curtok == TOK_LBRACK) {
1172
1173             /* Array reference */
1174             k = arrayref (k, lval);
1175
1176         } else if (curtok == TOK_LPAREN) {
1177
1178             /* Function call. Skip the opening parenthesis */
1179             NextToken ();
1180             tptr = lval->e_tptr;
1181             if (IsTypeFunc (tptr) || IsTypeFuncPtr (tptr)) {
1182                 if (IsTypeFuncPtr (tptr)) {
1183                     /* Pointer to function. Handle transparently */
1184                     exprhs (CF_NONE, k, lval);  /* Function pointer to A/X */
1185                     ++lval->e_tptr;             /* Skip T_PTR */
1186                     lval->e_flags |= E_MEXPR;
1187                 }
1188                 callfunction (lval);
1189                 lval->e_flags = E_MEXPR;
1190                 lval->e_tptr += DECODE_SIZE + 1;        /* Set to result */
1191             } else {
1192                 Error ("Illegal function call");
1193             }
1194             k = 0;
1195
1196         } else if (curtok == TOK_DOT) {
1197
1198             if (!IsClassStruct (lval->e_tptr)) {
1199                 Error ("Struct expected");
1200             }
1201             k = structref (0, lval);
1202
1203         } else if (curtok == TOK_PTR_REF) {
1204
1205             tptr = lval->e_tptr;
1206             if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1207                 Error ("Struct pointer expected");
1208             }
1209             k = structref (k, lval);
1210
1211         } else {
1212             return k;
1213         }
1214     }
1215 }
1216
1217
1218
1219 static void store (struct expent* lval)
1220 /* Store primary reg into this reference */
1221 {
1222     int f;
1223     unsigned flags;
1224
1225     f = lval->e_flags;
1226     flags = TypeOf (lval->e_tptr);
1227     if (f & E_MGLOBAL) {
1228         flags |= GlobalModeFlags (f);
1229         if (lval->e_test) {
1230             /* Just testing */
1231             flags |= CF_TEST;
1232         }
1233
1234         /* Generate code */
1235         g_putstatic (flags, lval->e_name, lval->e_const);
1236
1237     } else if (f & E_MLOCAL) {
1238         g_putlocal (flags, lval->e_const);
1239     } else if (f == E_MEOFFS) {
1240         g_putind (flags, lval->e_const);
1241     } else if (f != E_MREG) {
1242         if (f & E_MEXPR) {
1243             g_putind (flags, 0);
1244         } else {
1245             /* Store into absolute address */
1246             g_putstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
1247         }
1248     }
1249
1250     /* Assume that each one of the stores will invalidate CC */
1251     lval->e_test &= ~E_CC;
1252 }
1253
1254
1255
1256 static void pre_incdec (struct expent* lval, void (*inc) (unsigned, unsigned long))
1257 /* Handle --i and ++i */
1258 {
1259     int k;
1260     unsigned flags;
1261     unsigned long val;
1262
1263     NextToken ();
1264     if ((k = hie10 (lval)) == 0) {
1265         Error ("Invalid lvalue");
1266         return;
1267     }
1268
1269     /* Get the data type */
1270     flags = TypeOf (lval->e_tptr) | CF_FORCECHAR | CF_CONST;
1271
1272     /* Get the increment value in bytes */
1273     val = (lval->e_tptr [0] == T_PTR)? PSizeOf (lval->e_tptr) : 1;
1274
1275     /* We're currently only able to handle some adressing modes */
1276     if ((lval->e_flags & E_MGLOBAL) == 0 &&     /* Global address? */
1277         (lval->e_flags & E_MLOCAL) == 0  &&     /* Local address? */
1278         (lval->e_flags & E_MCONST) == 0  &&     /* Constant address? */
1279         (lval->e_flags & E_MEXPR) == 0) {       /* Address in a/x? */
1280
1281         /* Use generic code. Push the address if needed */
1282         PushAddr (lval);
1283
1284         /* Fetch the value */
1285         exprhs (CF_NONE, k, lval);
1286
1287         /* Increment value in primary */
1288         inc (flags, val);
1289
1290         /* Store the result back */
1291         store (lval);
1292
1293     } else {
1294
1295         /* Special code for some addressing modes - use the special += ops */
1296         if (lval->e_flags & E_MGLOBAL) {
1297             flags |= GlobalModeFlags (lval->e_flags);
1298             if (inc == g_inc) {
1299                 g_addeqstatic (flags, lval->e_name, lval->e_const, val);
1300             } else {
1301                 g_subeqstatic (flags, lval->e_name, lval->e_const, val);
1302             }
1303         } else if (lval->e_flags & E_MLOCAL) {
1304             /* ref to localvar */
1305             if (inc == g_inc) {
1306                 g_addeqlocal (flags, lval->e_const, val);
1307             } else {
1308                 g_subeqlocal (flags, lval->e_const, val);
1309             }
1310         } else if (lval->e_flags & E_MCONST) {
1311             /* ref to absolute address */
1312             flags |= CF_ABSOLUTE;
1313             if (inc == g_inc) {
1314                 g_addeqstatic (flags, lval->e_const, 0, val);
1315             } else {
1316                 g_subeqstatic (flags, lval->e_const, 0, val);
1317             }
1318         } else if (lval->e_flags & E_MEXPR) {
1319             /* Address in a/x, check if we have an offset */
1320             unsigned Offs = (lval->e_flags == E_MEOFFS)? lval->e_const : 0;
1321             if (inc == g_inc) {
1322                 g_addeqind (flags, Offs, val);
1323             } else {
1324                 g_subeqind (flags, Offs, val);
1325             }
1326         } else {
1327             Internal ("Invalid addressing mode");
1328         }
1329
1330     }
1331
1332     /* Result is an expression */
1333     lval->e_flags = E_MEXPR;
1334 }
1335
1336
1337
1338 static void post_incdec (struct expent *lval, int k, void (*inc) (unsigned, unsigned long))
1339 /* Handle i-- and i++ */
1340 {
1341     unsigned flags;
1342
1343     NextToken ();
1344     if (k == 0) {
1345         Error ("Invalid lvalue");
1346         return;
1347     }
1348
1349     /* Get the data type */
1350     flags = TypeOf (lval->e_tptr);
1351
1352     /* Push the address if needed */
1353     PushAddr (lval);
1354
1355     /* Fetch the value and save it (since it's the result of the expression) */
1356     exprhs (CF_NONE, 1, lval);
1357     g_save (flags | CF_FORCECHAR);
1358
1359     /* If we have a pointer expression, increment by the size of the type */
1360     if (lval->e_tptr[0] == T_PTR) {
1361         inc (flags | CF_CONST | CF_FORCECHAR, SizeOf (lval->e_tptr + 1));
1362     } else {
1363         inc (flags | CF_CONST | CF_FORCECHAR, 1);
1364     }
1365
1366     /* Store the result back */
1367     store (lval);
1368
1369     /* Restore the original value */
1370     g_restore (flags | CF_FORCECHAR);
1371     lval->e_flags = E_MEXPR;
1372 }
1373
1374
1375
1376 static void unaryop (int tok, struct expent* lval)
1377 /* Handle unary -/+ and ~ */
1378 {
1379     int k;
1380     unsigned flags;
1381
1382     NextToken ();
1383     k = hie10 (lval);
1384     if (k == 0 && lval->e_flags & E_MCONST) {
1385         /* Value is constant */
1386         switch (tok) {
1387             case TOK_MINUS: lval->e_const =     -lval->e_const; break;
1388             case TOK_PLUS:                                      break;
1389             case TOK_COMP:  lval->e_const = ~lval->e_const;     break;
1390             default:        Internal ("Unexpected token: %d", tok);
1391         }
1392     } else {
1393         /* Value is not constant */
1394         exprhs (CF_NONE, k, lval);
1395
1396         /* Get the type of the expression */
1397         flags = TypeOf (lval->e_tptr);
1398
1399         /* Handle the operation */
1400         switch (tok) {
1401             case TOK_MINUS: g_neg (flags);  break;
1402             case TOK_PLUS:                  break;
1403             case TOK_COMP:  g_com (flags);  break;
1404             default:    Internal ("Unexpected token: %d", tok);
1405         }
1406         lval->e_flags = E_MEXPR;
1407     }
1408 }
1409
1410
1411
1412 static int typecast (struct expent* lval)
1413 /* Handle an explicit cast */
1414 {
1415     int k;
1416     type Type[MAXTYPELEN];
1417
1418     /* Skip the left paren */
1419     NextToken ();
1420
1421     /* Read the type */
1422     ParseType (Type);
1423
1424     /* Closing paren */
1425     ConsumeRParen ();
1426
1427     /* Read the expression we have to cast */
1428     k = hie10 (lval);
1429
1430     /* If the expression is a function, treat it as pointer-to-function */
1431     if (IsTypeFunc (lval->e_tptr)) {
1432         lval->e_tptr = PointerTo (lval->e_tptr);
1433     }
1434
1435     /* Check for a constant on the right side */
1436     if (k == 0 && lval->e_flags == E_MCONST) {
1437
1438         /* A cast of a constant to something else. If the new type is an int,
1439          * be sure to handle the size extension correctly. If the new type is
1440          * not an int, the cast is implementation specific anyway, so leave
1441          * the value alone.
1442          */
1443         if (IsClassInt (Type)) {
1444
1445             /* Get the current and new size of the value */
1446             unsigned OldSize = SizeOf (lval->e_tptr);
1447             unsigned NewSize = SizeOf (Type);
1448             unsigned OldBits = OldSize * 8;
1449             unsigned NewBits = NewSize * 8;
1450
1451             /* Check if the new datatype will have a smaller range */
1452             if (NewSize < OldSize) {
1453
1454                 /* Cut the value to the new size */
1455                 lval->e_const &= (0xFFFFFFFFUL >> (32 - NewBits));
1456
1457                 /* If the new value is signed, sign extend the value */
1458                 if (!IsSignUnsigned (Type)) {
1459                     lval->e_const |= ((~0L) << NewBits);
1460                 }
1461
1462             } else if (NewSize > OldSize) {
1463
1464                 /* Sign extend the value if needed */
1465                 if (!IsSignUnsigned (Type) && !IsSignUnsigned (lval->e_tptr)) {
1466                     if (lval->e_const & (0x01UL << (OldBits-1))) {
1467                         lval->e_const |= ((~0L) << OldBits);
1468                     }
1469                 }
1470             }
1471         }
1472
1473     } else {
1474
1475         /* Not a constant. Be sure to ignore casts to void */
1476         if (!IsTypeVoid (Type)) {
1477
1478             /* If the size does not change, leave the value alone. Otherwise,
1479              * we have to load the value into the primary and generate code to
1480              * cast the value in the primary register.
1481              */
1482             if (SizeOf (Type) != SizeOf (lval->e_tptr)) {
1483
1484                 /* Load the value into the primary */
1485                 exprhs (CF_NONE, k, lval);
1486
1487                 /* Mark the lhs as const to avoid a manipulation of TOS */
1488                 g_typecast (TypeOf (Type) | CF_CONST, TypeOf (lval->e_tptr));
1489
1490                 /* Value is now in primary */
1491                 lval->e_flags = E_MEXPR;
1492                 k = 0;
1493             }
1494         }
1495     }
1496
1497     /* In any case, use the new type */
1498     lval->e_tptr = TypeDup (Type);
1499
1500     /* Done */
1501     return k;
1502 }
1503
1504
1505
1506 static int hie10 (struct expent* lval)
1507 /* Handle ++, --, !, unary - etc. */
1508 {
1509     int k;
1510     type* t;
1511
1512     switch (curtok) {
1513
1514         case TOK_INC:
1515             pre_incdec (lval, g_inc);
1516             return 0;
1517
1518         case TOK_DEC:
1519             pre_incdec (lval, g_dec);
1520             return 0;
1521
1522         case TOK_PLUS:
1523         case TOK_MINUS:
1524         case TOK_COMP:
1525             unaryop (curtok, lval);
1526             return 0;
1527
1528         case TOK_BOOL_NOT:
1529             NextToken ();
1530             if (evalexpr (CF_NONE, hie10, lval) == 0) {
1531                 /* Constant expression */
1532                 lval->e_const = !lval->e_const;
1533             } else {
1534                 g_bneg (TypeOf (lval->e_tptr));
1535                 lval->e_test |= E_CC;                   /* bneg will set cc */
1536                 lval->e_flags = E_MEXPR;                /* say it's an expr */
1537             }
1538             return 0;                           /* expr not storable */
1539
1540         case TOK_STAR:
1541             NextToken ();
1542             if (evalexpr (CF_NONE, hie10, lval) != 0) {
1543                 /* Expression is not const, indirect value loaded into primary */
1544                 lval->e_flags = E_MEXPR;
1545                 lval->e_const = 0;              /* Offset is zero now */
1546             }
1547             t = lval->e_tptr;
1548             if (IsClassPtr (t)) {
1549                 lval->e_tptr = Indirect (t);
1550             } else {
1551                 Error ("Illegal indirection");
1552             }
1553             return 1;
1554
1555         case TOK_AND:
1556             NextToken ();
1557             k = hie10 (lval);
1558             /* The & operator may be applied to any lvalue, and it may be
1559              * applied to functions, even if they're no lvalues.
1560              */
1561             if (k == 0 && !IsTypeFunc (lval->e_tptr)) {
1562                 /* Allow the & operator with an array */
1563                 if (!IsTypeArray (lval->e_tptr)) {
1564                     Error ("Illegal address");
1565                 }
1566             } else {
1567                 t = TypeAlloc (TypeLen (lval->e_tptr) + 2);
1568                 t [0] = T_PTR;
1569                 TypeCpy (t + 1, lval->e_tptr);
1570                 lval->e_tptr = t;
1571             }
1572             return 0;
1573
1574         case TOK_SIZEOF:
1575             NextToken ();
1576             if (istypeexpr ()) {
1577                 type Type[MAXTYPELEN];
1578                 NextToken ();
1579                 lval->e_const = SizeOf (ParseType (Type));
1580                 ConsumeRParen ();
1581             } else {
1582                 /* Remember the output queue pointer */
1583                 CodeMark Mark = GetCodePos ();
1584                 hie10 (lval);
1585                 lval->e_const = SizeOf (lval->e_tptr);
1586                 /* Remove any generated code */
1587                 RemoveCode (Mark);
1588             }
1589             lval->e_flags = E_MCONST | E_TCONST;
1590             lval->e_tptr = type_uint;
1591             lval->e_test &= ~E_CC;
1592             return 0;
1593
1594         default:
1595             if (istypeexpr ()) {
1596                 /* A cast */
1597                 return typecast (lval);
1598             }
1599     }
1600
1601     k = hie11 (lval);
1602     switch (curtok) {
1603         case TOK_INC:
1604             post_incdec (lval, k, g_inc);
1605             return 0;
1606
1607         case TOK_DEC:
1608             post_incdec (lval, k, g_dec);
1609             return 0;
1610
1611         default:
1612             return k;
1613     }
1614 }
1615
1616
1617
1618 static int hie_internal (GenDesc** ops,         /* List of generators */
1619                          struct expent* lval,   /* parent expr's lval */
1620                          int (*hienext) (struct expent*),
1621                          int* UsedGen)          /* next higher level */
1622 /* Helper function */
1623 {
1624     int k;
1625     struct expent lval2;
1626     CodeMark Mark1;
1627     CodeMark Mark2;
1628     GenDesc* Gen;
1629     token_t tok;                        /* The operator token */
1630     unsigned ltype, type;
1631     int rconst;                         /* Operand is a constant */
1632
1633
1634     k = hienext (lval);
1635
1636     *UsedGen = 0;
1637     while ((Gen = FindGen (curtok, ops)) != 0) {
1638
1639         /* Tell the caller that we handled it's ops */
1640         *UsedGen = 1;
1641
1642         /* All operators that call this function expect an int on the lhs */
1643         if (!IsClassInt (lval->e_tptr)) {
1644             Error ("Integer expression expected");
1645         }
1646
1647         /* Remember the operator token, then skip it */
1648         tok = curtok;
1649         NextToken ();
1650
1651         /* Get the lhs on stack */
1652         Mark1 = GetCodePos ();
1653         ltype = TypeOf (lval->e_tptr);
1654         if (k == 0 && lval->e_flags == E_MCONST) {
1655             /* Constant value */
1656             Mark2 = GetCodePos ();
1657             g_push (ltype | CF_CONST, lval->e_const);
1658         } else {
1659             /* Value not constant */
1660             exprhs (CF_NONE, k, lval);
1661             Mark2 = GetCodePos ();
1662             g_push (ltype, 0);
1663         }
1664
1665         /* Get the right hand side */
1666         rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1667
1668         /* Check the type of the rhs */
1669         if (!IsClassInt (lval2.e_tptr)) {
1670             Error ("Integer expression expected");
1671         }
1672
1673         /* Check for const operands */
1674         if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1675
1676             /* Both operands are constant, remove the generated code */
1677             RemoveCode (Mark1);
1678             pop (ltype);
1679
1680             /* Evaluate the result */
1681             lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1682
1683             /* Get the type of the result */
1684             lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1685
1686         } else {
1687
1688             /* If the right hand side is constant, and the generator function
1689              * expects the lhs in the primary, remove the push of the primary
1690              * now.
1691              */
1692             unsigned rtype = TypeOf (lval2.e_tptr);
1693             type = 0;
1694             if (rconst) {
1695                 /* Second value is constant - check for div */
1696                 type |= CF_CONST;
1697                 rtype |= CF_CONST;
1698                 if (tok == TOK_DIV && lval2.e_const == 0) {
1699                     Error ("Division by zero");
1700                 } else if (tok == TOK_MOD && lval2.e_const == 0) {
1701                     Error ("Modulo operation with zero");
1702                 }
1703                 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1704                     RemoveCode (Mark2);
1705                     pop (ltype);
1706                     ltype |= CF_REG;    /* Value is in register */
1707                 }
1708             }
1709
1710             /* Determine the type of the operation result. */
1711             type |= g_typeadjust (ltype, rtype);
1712             lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1713
1714             /* Generate code */
1715             Gen->Func (type, lval2.e_const);
1716             lval->e_flags = E_MEXPR;
1717         }
1718
1719         /* We have a rvalue now */
1720         k = 0;
1721     }
1722
1723     return k;
1724 }
1725
1726
1727
1728 static int hie_compare (GenDesc** ops,          /* List of generators */
1729                         struct expent* lval,    /* parent expr's lval */
1730                         int (*hienext) (struct expent*))
1731 /* Helper function for the compare operators */
1732 {
1733     int k;
1734     struct expent lval2;
1735     CodeMark Mark1;
1736     CodeMark Mark2;
1737     GenDesc* Gen;
1738     token_t tok;                        /* The operator token */
1739     unsigned ltype;
1740     int rconst;                         /* Operand is a constant */
1741
1742
1743     k = hienext (lval);
1744
1745     while ((Gen = FindGen (curtok, ops)) != 0) {
1746
1747         /* Remember the operator token, then skip it */
1748         tok = curtok;
1749         NextToken ();
1750
1751         /* Get the lhs on stack */
1752         Mark1 = GetCodePos ();
1753         ltype = TypeOf (lval->e_tptr);
1754         if (k == 0 && lval->e_flags == E_MCONST) {
1755             /* Constant value */
1756             Mark2 = GetCodePos ();
1757             g_push (ltype | CF_CONST, lval->e_const);
1758         } else {
1759             /* Value not constant */
1760             exprhs (CF_NONE, k, lval);
1761             Mark2 = GetCodePos ();
1762             g_push (ltype, 0);
1763         }
1764
1765         /* Get the right hand side */
1766         rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1767
1768         /* Make sure, the types are compatible */
1769         if (IsClassInt (lval->e_tptr)) {
1770             if (!IsClassInt (lval2.e_tptr) && !(IsClassPtr(lval2.e_tptr) && IsNullPtr(lval))) {
1771                 Error ("Incompatible types");
1772             }
1773         } else if (IsClassPtr (lval->e_tptr)) {
1774             if (IsClassPtr (lval2.e_tptr)) {
1775                 /* Both pointers are allowed in comparison if they point to
1776                  * the same type, or if one of them is a void pointer.
1777                  */
1778                 type* left  = Indirect (lval->e_tptr);
1779                 type* right = Indirect (lval2.e_tptr);
1780                 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1781                     /* Incomatible pointers */
1782                     Error ("Incompatible types");
1783                 }
1784             } else if (!IsNullPtr (&lval2)) {
1785                 Error ("Incompatible types");
1786             }
1787         }
1788
1789         /* Check for const operands */
1790         if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1791
1792             /* Both operands are constant, remove the generated code */
1793             RemoveCode (Mark1);
1794             pop (ltype);
1795
1796             /* Evaluate the result */
1797             lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1798
1799         } else {
1800
1801             /* If the right hand side is constant, and the generator function
1802              * expects the lhs in the primary, remove the push of the primary
1803              * now.
1804              */
1805             unsigned flags = 0;
1806             if (rconst) {
1807                 flags |= CF_CONST;
1808                 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1809                     RemoveCode (Mark2);
1810                     pop (ltype);
1811                     ltype |= CF_REG;    /* Value is in register */
1812                 }
1813             }
1814
1815             /* Determine the type of the operation result. If the left
1816              * operand is of type char and the right is a constant, or
1817              * if both operands are of type char, we will encode the
1818              * operation as char operation. Otherwise the default
1819              * promotions are used.
1820              */
1821             if (IsTypeChar (lval->e_tptr) && (IsTypeChar (lval2.e_tptr) || rconst)) {
1822                 flags |= CF_CHAR;
1823                 if (IsSignUnsigned (lval->e_tptr) || IsSignUnsigned (lval2.e_tptr)) {
1824                     flags |= CF_UNSIGNED;
1825                 }
1826                 if (rconst) {
1827                     flags |= CF_FORCECHAR;
1828                 }
1829             } else {
1830                 unsigned rtype = TypeOf (lval2.e_tptr) | (flags & CF_CONST);
1831                 flags |= g_typeadjust (ltype, rtype);
1832             }
1833
1834             /* Generate code */
1835             Gen->Func (flags, lval2.e_const);
1836             lval->e_flags = E_MEXPR;
1837         }
1838
1839         /* Result type is always int */
1840         lval->e_tptr = type_int;
1841
1842         /* We have a rvalue now, condition codes are set */
1843         k = 0;
1844         lval->e_test |= E_CC;
1845     }
1846
1847     return k;
1848 }
1849
1850
1851
1852 static int hie9 (struct expent *lval)
1853 /* Process * and / operators. */
1854 {
1855     static GenDesc* hie9_ops [] = {
1856         &GenMUL, &GenDIV, &GenMOD, 0
1857     };
1858     int UsedGen;
1859
1860     return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1861 }
1862
1863
1864
1865 static void parseadd (int k, struct expent* lval)
1866 /* Parse an expression with the binary plus operator. lval contains the
1867  * unprocessed left hand side of the expression and will contain the
1868  * result of the expression on return.
1869  */
1870 {
1871     struct expent lval2;
1872     unsigned flags;             /* Operation flags */
1873     CodeMark Mark;              /* Remember code position */
1874     type* lhst;                 /* Type of left hand side */
1875     type* rhst;                 /* Type of right hand side */
1876
1877
1878     /* Skip the PLUS token */
1879     NextToken ();
1880
1881     /* Get the left hand side type, initialize operation flags */
1882     lhst = lval->e_tptr;
1883     flags = 0;
1884
1885     /* Check for constness on both sides */
1886     if (k == 0 && lval->e_flags == E_MCONST) {
1887
1888         /* The left hand side is a constant. Good. Get rhs */
1889         if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1890
1891             /* Right hand side is also constant. Get the rhs type */
1892             rhst = lval2.e_tptr;
1893
1894             /* Both expressions are constants. Check for pointer arithmetic */
1895             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1896                 /* Left is pointer, right is int, must scale rhs */
1897                 lval->e_const = lval->e_const + lval2.e_const * PSizeOf (lhst);
1898                 /* Result type is a pointer */
1899             } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1900                 /* Left is int, right is pointer, must scale lhs */
1901                 lval->e_const = lval->e_const * PSizeOf (rhst) + lval2.e_const;
1902                 /* Result type is a pointer */
1903                 lval->e_tptr = lval2.e_tptr;
1904             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1905                 /* Integer addition */
1906                 lval->e_const += lval2.e_const;
1907                 typeadjust (lval, &lval2, 1);
1908             } else {
1909                 /* OOPS */
1910                 Error ("Invalid operands for binary operator `+'");
1911             }
1912
1913             /* Result is constant, condition codes not set */
1914             lval->e_test = E_MCONST;
1915
1916         } else {
1917
1918             /* lhs is constant, rhs is not. Get the rhs type. */
1919             rhst = lval2.e_tptr;
1920
1921             /* Check for pointer arithmetic */
1922             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1923                 /* Left is pointer, right is int, must scale rhs */
1924                 g_scale (CF_INT, PSizeOf (lhst));
1925                 /* Operate on pointers, result type is a pointer */
1926                 flags = CF_PTR;
1927             } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1928                 /* Left is int, right is pointer, must scale lhs */
1929                 lval->e_const *= PSizeOf (rhst);
1930                 /* Operate on pointers, result type is a pointer */
1931                 flags = CF_PTR;
1932                 lval->e_tptr = lval2.e_tptr;
1933             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1934                 /* Integer addition */
1935                 flags = typeadjust (lval, &lval2, 1);
1936             } else {
1937                 /* OOPS */
1938                 Error ("Invalid operands for binary operator `+'");
1939             }
1940
1941             /* Generate code for the add */
1942             g_inc (flags | CF_CONST, lval->e_const);
1943
1944             /* Result is in primary register */
1945             lval->e_flags = E_MEXPR;
1946             lval->e_test &= ~E_CC;
1947
1948         }
1949
1950     } else {
1951
1952         /* Left hand side is not constant. Get the value onto the stack. */
1953         exprhs (CF_NONE, k, lval);              /* --> primary register */
1954         Mark = GetCodePos ();
1955         g_push (TypeOf (lval->e_tptr), 0);      /* --> stack */
1956
1957         /* Evaluate the rhs */
1958         if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1959
1960             /* Right hand side is a constant. Get the rhs type */
1961             rhst = lval2.e_tptr;
1962
1963             /* Remove pushed value from stack */
1964             RemoveCode (Mark);
1965             pop (TypeOf (lval->e_tptr));
1966
1967             /* Check for pointer arithmetic */
1968             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1969                 /* Left is pointer, right is int, must scale rhs */
1970                 lval2.e_const *= PSizeOf (lhst);
1971                 /* Operate on pointers, result type is a pointer */
1972                 flags = CF_PTR;
1973             } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1974                 /* Left is int, right is pointer, must scale lhs (ptr only) */
1975                 g_scale (CF_INT | CF_CONST, PSizeOf (rhst));
1976                 /* Operate on pointers, result type is a pointer */
1977                 flags = CF_PTR;
1978                 lval->e_tptr = lval2.e_tptr;
1979             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1980                 /* Integer addition */
1981                 flags = typeadjust (lval, &lval2, 1);
1982             } else {
1983                 /* OOPS */
1984                 Error ("Invalid operands for binary operator `+'");
1985             }
1986
1987             /* Generate code for the add */
1988             g_inc (flags | CF_CONST, lval2.e_const);
1989
1990             /* Result is in primary register */
1991             lval->e_flags = E_MEXPR;
1992             lval->e_test &= ~E_CC;
1993
1994         } else {
1995
1996             /* lhs and rhs are not constant. Get the rhs type. */
1997             rhst = lval2.e_tptr;
1998
1999             /* Check for pointer arithmetic */
2000             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2001                 /* Left is pointer, right is int, must scale rhs */
2002                 g_scale (CF_INT, PSizeOf (lhst));
2003                 /* Operate on pointers, result type is a pointer */
2004                 flags = CF_PTR;
2005             } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2006                 /* Left is int, right is pointer, must scale lhs */
2007                 g_tosint (TypeOf (rhst));       /* Make sure, TOS is int */
2008                 g_swap (CF_INT);                /* Swap TOS and primary */
2009                 g_scale (CF_INT, PSizeOf (rhst));
2010                 /* Operate on pointers, result type is a pointer */
2011                 flags = CF_PTR;
2012                 lval->e_tptr = lval2.e_tptr;
2013             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2014                 /* Integer addition */
2015                 flags = typeadjust (lval, &lval2, 0);
2016             } else {
2017                 /* OOPS */
2018                 Error ("Invalid operands for binary operator `+'");
2019             }
2020
2021             /* Generate code for the add */
2022             g_add (flags, 0);
2023
2024             /* Result is in primary register */
2025             lval->e_flags = E_MEXPR;
2026             lval->e_test &= ~E_CC;
2027
2028         }
2029
2030     }
2031 }
2032
2033
2034
2035 static void parsesub (int k, struct expent* lval)
2036 /* Parse an expression with the binary minus operator. lval contains the
2037  * unprocessed left hand side of the expression and will contain the
2038  * result of the expression on return.
2039  */
2040 {
2041     struct expent lval2;
2042     unsigned flags;             /* Operation flags */
2043     type* lhst;                 /* Type of left hand side */
2044     type* rhst;                 /* Type of right hand side */
2045     CodeMark Mark1;             /* Save position of output queue */
2046     CodeMark Mark2;             /* Another position in the queue */
2047     int rscale;                 /* Scale factor for the result */
2048
2049
2050     /* Skip the MINUS token */
2051     NextToken ();
2052
2053     /* Get the left hand side type, initialize operation flags */
2054     lhst = lval->e_tptr;
2055     flags = 0;
2056     rscale = 1;                 /* Scale by 1, that is, don't scale */
2057
2058     /* Remember the output queue position, then bring the value onto the stack */
2059     Mark1 = GetCodePos ();
2060     exprhs (CF_NONE, k, lval);  /* --> primary register */
2061     Mark2 = GetCodePos ();
2062     g_push (TypeOf (lhst), 0);  /* --> stack */
2063
2064     /* Parse the right hand side */
2065     if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2066
2067         /* The right hand side is constant. Get the rhs type. */
2068         rhst = lval2.e_tptr;
2069
2070         /* Check left hand side */
2071         if (k == 0 && lval->e_flags & E_MCONST) {
2072
2073             /* Both sides are constant, remove generated code */
2074             RemoveCode (Mark1);
2075             pop (TypeOf (lhst));        /* Clean up the stack */
2076
2077             /* Check for pointer arithmetic */
2078             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2079                 /* Left is pointer, right is int, must scale rhs */
2080                 lval->e_const -= lval2.e_const * PSizeOf (lhst);
2081                 /* Operate on pointers, result type is a pointer */
2082             } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2083                 /* Left is pointer, right is pointer, must scale result */
2084                 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2085                     Error ("Incompatible pointer types");
2086                 } else {
2087                     lval->e_const = (lval->e_const - lval2.e_const) / PSizeOf (lhst);
2088                 }
2089                 /* Operate on pointers, result type is an integer */
2090                 lval->e_tptr = type_int;
2091             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2092                 /* Integer subtraction */
2093                 typeadjust (lval, &lval2, 1);
2094                 lval->e_const -= lval2.e_const;
2095             } else {
2096                 /* OOPS */
2097                 Error ("Invalid operands for binary operator `-'");
2098             }
2099
2100             /* Result is constant, condition codes not set */
2101             lval->e_flags = E_MCONST;
2102             lval->e_test &= ~E_CC;
2103
2104         } else {
2105
2106             /* Left hand side is not constant, right hand side is.
2107              * Remove pushed value from stack.
2108              */
2109             RemoveCode (Mark2);
2110             pop (TypeOf (lhst));
2111
2112             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2113                 /* Left is pointer, right is int, must scale rhs */
2114                 lval2.e_const *= PSizeOf (lhst);
2115                 /* Operate on pointers, result type is a pointer */
2116                 flags = CF_PTR;
2117             } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2118                 /* Left is pointer, right is pointer, must scale result */
2119                 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2120                     Error ("Incompatible pointer types");
2121                 } else {
2122                     rscale = PSizeOf (lhst);
2123                 }
2124                 /* Operate on pointers, result type is an integer */
2125                 flags = CF_PTR;
2126                 lval->e_tptr = type_int;
2127             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2128                 /* Integer subtraction */
2129                 flags = typeadjust (lval, &lval2, 1);
2130             } else {
2131                 /* OOPS */
2132                 Error ("Invalid operands for binary operator `-'");
2133             }
2134
2135             /* Do the subtraction */
2136             g_dec (flags | CF_CONST, lval2.e_const);
2137
2138             /* If this was a pointer subtraction, we must scale the result */
2139             if (rscale != 1) {
2140                 g_scale (flags, -rscale);
2141             }
2142
2143             /* Result is in primary register */
2144             lval->e_flags = E_MEXPR;
2145             lval->e_test &= ~E_CC;
2146
2147         }
2148
2149     } else {
2150
2151         /* Right hand side is not constant. Get the rhs type. */
2152         rhst = lval2.e_tptr;
2153
2154         /* Check for pointer arithmetic */
2155         if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2156             /* Left is pointer, right is int, must scale rhs */
2157             g_scale (CF_INT, PSizeOf (lhst));
2158             /* Operate on pointers, result type is a pointer */
2159             flags = CF_PTR;
2160         } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2161             /* Left is pointer, right is pointer, must scale result */
2162             if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2163                 Error ("Incompatible pointer types");
2164             } else {
2165                 rscale = PSizeOf (lhst);
2166             }
2167             /* Operate on pointers, result type is an integer */
2168             flags = CF_PTR;
2169             lval->e_tptr = type_int;
2170         } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2171             /* Integer subtraction. If the left hand side descriptor says that
2172              * the lhs is const, we have to remove this mark, since this is no
2173              * longer true, lhs is on stack instead.
2174              */
2175             if (lval->e_flags == E_MCONST) {
2176                 lval->e_flags = E_MEXPR;
2177             }
2178             /* Adjust operand types */
2179             flags = typeadjust (lval, &lval2, 0);
2180         } else {
2181             /* OOPS */
2182             Error ("Invalid operands for binary operator `-'");
2183         }
2184
2185         /* Generate code for the sub (the & is a hack here) */
2186         g_sub (flags & ~CF_CONST, 0);
2187
2188         /* If this was a pointer subtraction, we must scale the result */
2189         if (rscale != 1) {
2190             g_scale (flags, -rscale);
2191         }
2192
2193         /* Result is in primary register */
2194         lval->e_flags = E_MEXPR;
2195         lval->e_test &= ~E_CC;
2196     }
2197 }
2198
2199
2200
2201 static int hie8 (struct expent* lval)
2202 /* Process + and - binary operators. */
2203 {
2204     int k = hie9 (lval);
2205     while (curtok == TOK_PLUS || curtok == TOK_MINUS) {
2206
2207         if (curtok == TOK_PLUS) {
2208             parseadd (k, lval);
2209         } else {
2210             parsesub (k, lval);
2211         }
2212         k = 0;
2213     }
2214     return k;
2215 }
2216
2217
2218
2219
2220 static int hie7 (struct expent *lval)
2221 /* Parse << and >>. */
2222 {
2223     static GenDesc* hie7_ops [] = {
2224         &GenASL, &GenASR, 0
2225     };
2226     int UsedGen;
2227
2228     return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2229 }
2230
2231
2232
2233 static int hie6 (struct expent *lval)
2234 /* process greater-than type comparators */
2235 {
2236     static GenDesc* hie6_ops [] = {
2237         &GenLT, &GenLE, &GenGE, &GenGT, 0
2238     };
2239     return hie_compare (hie6_ops, lval, hie7);
2240 }
2241
2242
2243
2244 static int hie5 (struct expent *lval)
2245 {
2246     static GenDesc* hie5_ops[] = {
2247         &GenEQ, &GenNE, 0
2248     };
2249     return hie_compare (hie5_ops, lval, hie6);
2250 }
2251
2252
2253
2254 static int hie4 (struct expent* lval)
2255 /* Handle & (bitwise and) */
2256 {
2257     static GenDesc* hie4_ops [] = {
2258         &GenAND, 0
2259     };
2260     int UsedGen;
2261
2262     return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2263 }
2264
2265
2266
2267 static int hie3 (struct expent *lval)
2268 /* Handle ^ (bitwise exclusive or) */
2269 {
2270     static GenDesc* hie3_ops [] = {
2271         &GenXOR, 0
2272     };
2273     int UsedGen;
2274
2275     return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2276 }
2277
2278
2279
2280 static int hie2 (struct expent *lval)
2281 /* Handle | (bitwise or) */
2282 {
2283     static GenDesc* hie2_ops [] = {
2284         &GenOR, 0
2285     };
2286     int UsedGen;
2287
2288     return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2289 }
2290
2291
2292
2293 static int hieAnd (struct expent* lval, unsigned TrueLab, int* BoolOp)
2294 /* Process "exp && exp" */
2295 {
2296     int k;
2297     int lab;
2298     struct expent lval2;
2299
2300     k = hie2 (lval);
2301     if (curtok == TOK_BOOL_AND) {
2302
2303         /* Tell our caller that we're evaluating a boolean */
2304         *BoolOp = 1;
2305
2306         /* Get a label that we will use for false expressions */
2307         lab = GetLabel ();
2308
2309         /* If the expr hasn't set condition codes, set the force-test flag */
2310         if ((lval->e_test & E_CC) == 0) {
2311             lval->e_test |= E_FORCETEST;
2312         }
2313
2314         /* Load the value */
2315         exprhs (CF_FORCECHAR, k, lval);
2316
2317         /* Generate the jump */
2318         g_falsejump (CF_NONE, lab);
2319
2320         /* Parse more boolean and's */
2321         while (curtok == TOK_BOOL_AND) {
2322
2323             /* Skip the && */
2324             NextToken ();
2325
2326             /* Get rhs */
2327             k = hie2 (&lval2);
2328             if ((lval2.e_test & E_CC) == 0) {
2329                 lval2.e_test |= E_FORCETEST;
2330             }
2331             exprhs (CF_FORCECHAR, k, &lval2);
2332
2333             /* Do short circuit evaluation */
2334             if (curtok == TOK_BOOL_AND) {
2335                 g_falsejump (CF_NONE, lab);
2336             } else {
2337                 /* Last expression - will evaluate to true */
2338                 g_truejump (CF_NONE, TrueLab);
2339             }
2340         }
2341
2342         /* Define the false jump label here */
2343         g_defloclabel (lab);
2344
2345         /* Define the label */
2346         lval->e_flags = E_MEXPR;
2347         lval->e_test |= E_CC;   /* Condition codes are set */
2348         k = 0;
2349     }
2350     return k;
2351 }
2352
2353
2354
2355 static int hieOr (struct expent *lval)
2356 /* Process "exp || exp". */
2357 {
2358     int k;
2359     struct expent lval2;
2360     int BoolOp = 0;             /* Did we have a boolean op? */
2361     int AndOp;                  /* Did we have a && operation? */
2362     unsigned TrueLab;           /* Jump to this label if true */
2363     unsigned DoneLab;
2364
2365     /* Get a label */
2366     TrueLab = GetLabel ();
2367
2368     /* Call the next level parser */
2369     k = hieAnd (lval, TrueLab, &BoolOp);
2370
2371     /* Any boolean or's? */
2372     if (curtok == TOK_BOOL_OR) {
2373
2374         /* If the expr hasn't set condition codes, set the force-test flag */
2375         if ((lval->e_test & E_CC) == 0) {
2376             lval->e_test |= E_FORCETEST;
2377         }
2378
2379         /* Get first expr */
2380         exprhs (CF_FORCECHAR, k, lval);
2381
2382         /* For each expression jump to TrueLab if true. Beware: If we
2383          * had && operators, the jump is already in place!
2384          */
2385         if (!BoolOp) {
2386             g_truejump (CF_NONE, TrueLab);
2387         }
2388
2389         /* Remember that we had a boolean op */
2390         BoolOp = 1;
2391
2392         /* while there's more expr */
2393         while (curtok == TOK_BOOL_OR) {
2394
2395             /* skip the || */
2396             NextToken ();
2397
2398             /* Get a subexpr */
2399             AndOp = 0;
2400             k = hieAnd (&lval2, TrueLab, &AndOp);
2401             if ((lval2.e_test & E_CC) == 0) {
2402                 lval2.e_test |= E_FORCETEST;
2403             }
2404             exprhs (CF_FORCECHAR, k, &lval2);
2405
2406             /* If there is more to come, add shortcut boolean eval.
2407              * Beware: If we had && operators, the jump is already
2408              * in place!
2409              */
2410 #if     0
2411 /* Seems this sometimes generates wrong code */
2412             if (curtok == TOK_BOOL_OR && !AndOp) {
2413                 g_truejump (CF_NONE, TrueLab);
2414             }
2415 #else
2416             g_truejump (CF_NONE, TrueLab);
2417 #endif
2418         }
2419         lval->e_flags = E_MEXPR;
2420         lval->e_test |= E_CC;                   /* Condition codes are set */
2421         k = 0;
2422     }
2423
2424     /* If we really had boolean ops, generate the end sequence */
2425     if (BoolOp) {
2426         DoneLab = GetLabel ();
2427         g_getimmed (CF_INT | CF_CONST, 0, 0);   /* Load FALSE */
2428         g_falsejump (CF_NONE, DoneLab);
2429         g_defloclabel (TrueLab);
2430         g_getimmed (CF_INT | CF_CONST, 1, 0);   /* Load TRUE */
2431         g_defloclabel (DoneLab);
2432     }
2433     return k;
2434 }
2435
2436
2437
2438 static int hieQuest (struct expent *lval)
2439 /* Parse "lvalue ? exp : exp" */
2440 {
2441     int k;
2442     int labf;
2443     int labt;
2444     struct expent lval2;        /* Expression 2 */
2445     struct expent lval3;        /* Expression 3 */
2446     type* type2;                /* Type of expression 2 */
2447     type* type3;                /* Type of expression 3 */
2448     type* rtype;                /* Type of result */
2449     CodeMark Mark1;             /* Save position in output code */
2450     CodeMark Mark2;             /* Save position in output code */
2451
2452
2453
2454     k = hieOr (lval);
2455     if (curtok == TOK_QUEST) {
2456         NextToken ();
2457         if ((lval->e_test & E_CC) == 0) {
2458             /* Condition codes not set, force a test */
2459             lval->e_test |= E_FORCETEST;
2460         }
2461         exprhs (CF_NONE, k, lval);
2462         labf = GetLabel ();
2463         g_falsejump (CF_NONE, labf);
2464
2465         /* Parse second and third expression */
2466         expression1 (&lval2);
2467         labt = GetLabel ();
2468         ConsumeColon ();
2469         g_jump (labt);
2470         g_defloclabel (labf);
2471         expression1 (&lval3);
2472
2473         /* Check if any conversions are needed, if so, do them.
2474          * Conversion rules for ?: expression are:
2475          *   - if both expressions are int expressions, default promotion
2476          *     rules for ints apply.
2477          *   - if both expressions are pointers of the same type, the
2478          *     result of the expression is of this type.
2479          *   - if one of the expressions is a pointer and the other is
2480          *     a zero constant, the resulting type is that of the pointer
2481          *     type.
2482          *   - all other cases are flagged by an error.
2483          */
2484         type2 = lval2.e_tptr;
2485         type3 = lval3.e_tptr;
2486         if (IsClassInt (type2) && IsClassInt (type3)) {
2487
2488             /* Get common type */
2489             rtype = promoteint (type2, type3);
2490
2491             /* Convert the third expression to this type if needed */
2492             g_typecast (TypeOf (rtype), TypeOf (type3));
2493
2494             /* Setup a new label so that the expr3 code will jump around
2495              * the type cast code for expr2.
2496              */
2497             labf = GetLabel ();         /* Get new label */
2498             Mark1 = GetCodePos ();      /* Remember current position */
2499             g_jump (labf);              /* Jump around code */
2500
2501             /* The jump for expr2 goes here */
2502             g_defloclabel (labt);
2503
2504             /* Create the typecast code for expr2 */
2505             Mark2 = GetCodePos ();      /* Remember position */
2506             g_typecast (TypeOf (rtype), TypeOf (type2));
2507
2508             /* If the typecast did not produce code, remove the jump,
2509              * otherwise output the label.
2510              */
2511             if (GetCodePos() == Mark2) {
2512                 RemoveCode (Mark1);     /* Remove code */
2513             } else {
2514                 /* We have typecast code, output label */
2515                 g_defloclabel (labf);
2516                 labt = 0;               /* Mark other label as invalid */
2517             }
2518
2519         } else if (IsClassPtr (type2) && IsClassPtr (type3)) {
2520             /* Must point to same type */
2521             if (TypeCmp (Indirect (type2), Indirect (type3)) < TC_EQUAL) {
2522                 Error ("Incompatible pointer types");
2523             }
2524             /* Result has the common type */
2525             rtype = lval2.e_tptr;
2526         } else if (IsClassPtr (type2) && IsNullPtr (&lval3)) {
2527             /* Result type is pointer, no cast needed */
2528             rtype = lval2.e_tptr;
2529         } else if (IsNullPtr (&lval2) && IsClassPtr (type3)) {
2530             /* Result type is pointer, no cast needed */
2531             rtype = lval3.e_tptr;
2532         } else {
2533             Error ("Incompatible types");
2534             rtype = lval2.e_tptr;               /* Doesn't matter here */
2535         }
2536
2537         /* If we don't have the label defined until now, do it */
2538         if (labt) {
2539             g_defloclabel (labt);
2540         }
2541
2542         /* Setup the target expression */
2543         lval->e_flags = E_MEXPR;
2544         lval->e_tptr = rtype;
2545         k = 0;
2546     }
2547     return k;
2548 }
2549
2550
2551
2552 static void opeq (GenDesc* Gen, struct expent *lval, int k)
2553 /* Process "op=" operators. */
2554 {
2555     struct expent lval2;
2556     unsigned flags;
2557     CodeMark Mark;
2558     int MustScale;
2559
2560     NextToken ();
2561     if (k == 0) {
2562         Error ("Invalid lvalue in assignment");
2563         return;
2564     }
2565
2566     /* Determine the type of the lhs */
2567     flags = TypeOf (lval->e_tptr);
2568     MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2569                 lval->e_tptr [0] == T_PTR;
2570
2571     /* Get the lhs address on stack (if needed) */
2572     PushAddr (lval);
2573
2574     /* Fetch the lhs into the primary register if needed */
2575     exprhs (CF_NONE, k, lval);
2576
2577     /* Bring the lhs on stack */
2578     Mark = GetCodePos ();
2579     g_push (flags, 0);
2580
2581     /* Evaluate the rhs */
2582     if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2583         /* The resulting value is a constant. If the generator has the NOPUSH
2584          * flag set, don't push the lhs.
2585          */
2586         if (Gen->Flags & GEN_NOPUSH) {
2587             RemoveCode (Mark);
2588             pop (flags);
2589         }
2590         if (MustScale) {
2591             /* lhs is a pointer, scale rhs */
2592             lval2.e_const *= SizeOf (lval->e_tptr+1);
2593         }
2594
2595         /* If the lhs is character sized, the operation may be later done
2596          * with characters.
2597          */
2598         if (SizeOf (lval->e_tptr) == 1) {
2599             flags |= CF_FORCECHAR;
2600         }
2601
2602         /* Special handling for add and sub - some sort of a hack, but short code */
2603         if (Gen->Func == g_add) {
2604             g_inc (flags | CF_CONST, lval2.e_const);
2605         } else if (Gen->Func == g_sub) {
2606             g_dec (flags | CF_CONST, lval2.e_const);
2607         } else {
2608             Gen->Func (flags | CF_CONST, lval2.e_const);
2609         }
2610     } else {
2611         /* rhs is not constant and already in the primary register */
2612         if (MustScale) {
2613             /* lhs is a pointer, scale rhs */
2614             g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2615         }
2616
2617         /* If the lhs is character sized, the operation may be later done
2618          * with characters.
2619          */
2620         if (SizeOf (lval->e_tptr) == 1) {
2621             flags |= CF_FORCECHAR;
2622         }
2623
2624         /* Adjust the types of the operands if needed */
2625         Gen->Func (g_typeadjust (flags, TypeOf (lval2.e_tptr)), 0);
2626     }
2627     store (lval);
2628     lval->e_flags = E_MEXPR;
2629 }
2630
2631
2632
2633 static void addsubeq (GenDesc* Gen, struct expent *lval, int k)
2634 /* Process the += and -= operators */
2635 {
2636     struct expent lval2;
2637     unsigned flags;
2638     int MustScale;
2639
2640
2641     if (k == 0) {
2642         Error ("Invalid lvalue in assignment");
2643         return;
2644     }
2645
2646
2647     /* We're currently only able to handle some adressing modes */
2648     if ((lval->e_flags & E_MGLOBAL) == 0 &&     /* Global address? */
2649         (lval->e_flags & E_MLOCAL) == 0  &&     /* Local address? */
2650         (lval->e_flags & E_MCONST) == 0) {      /* Constant address? */
2651         /* Use generic routine */
2652         opeq (Gen, lval, k);
2653         return;
2654     }
2655
2656     /* Skip the operator */
2657     NextToken ();
2658
2659     /* Check if we have a pointer expression and must scale rhs */
2660     MustScale = (lval->e_tptr [0] == T_PTR);
2661
2662     /* Determine the code generator flags */
2663     flags = TypeOf (lval->e_tptr) | CF_FORCECHAR;
2664
2665     /* Evaluate the rhs */
2666     if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2667         /* The resulting value is a constant. */
2668         if (MustScale) {
2669             /* lhs is a pointer, scale rhs */
2670             lval2.e_const *= SizeOf (lval->e_tptr+1);
2671         }
2672         flags |= CF_CONST;
2673     } else {
2674         /* rhs is not constant and already in the primary register */
2675         if (MustScale) {
2676             /* lhs is a pointer, scale rhs */
2677             g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2678         }
2679     }
2680
2681     /* Adjust the rhs to the lhs */
2682     g_typeadjust (flags, TypeOf (lval2.e_tptr));
2683
2684     /* Output apropriate code */
2685     if (lval->e_flags & E_MGLOBAL) {
2686         /* Static variable */
2687         flags |= GlobalModeFlags (lval->e_flags);
2688         if (Gen->Tok == TOK_PLUS_ASSIGN) {
2689             g_addeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2690         } else {
2691             g_subeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2692         }
2693     } else if (lval->e_flags & E_MLOCAL) {
2694         /* ref to localvar */
2695         if (Gen->Tok == TOK_PLUS_ASSIGN) {
2696             g_addeqlocal (flags, lval->e_const, lval2.e_const);
2697         } else {
2698             g_subeqlocal (flags, lval->e_const, lval2.e_const);
2699         }
2700     } else if (lval->e_flags & E_MCONST) {
2701         /* ref to absolute address */
2702         flags |= CF_ABSOLUTE;
2703         if (Gen->Tok == TOK_PLUS_ASSIGN) {
2704             g_addeqstatic (flags, lval->e_const, 0, lval2.e_const);
2705         } else {
2706             g_subeqstatic (flags, lval->e_const, 0, lval2.e_const);
2707         }
2708     } else if (lval->e_flags & E_MEXPR) {
2709         /* Address in a/x. */
2710         if (Gen->Tok == TOK_PLUS_ASSIGN) {
2711             g_addeqind (flags, lval->e_const, lval2.e_const);
2712         } else {
2713             g_subeqind (flags, lval->e_const, lval2.e_const);
2714         }
2715     } else {
2716         Internal ("Invalid addressing mode");
2717     }
2718
2719     /* Expression is in the primary now */
2720     lval->e_flags = E_MEXPR;
2721 }
2722
2723
2724
2725 static void Assignment (struct expent* lval)
2726 /* Parse an assignment */
2727 {
2728     int k;
2729     struct expent lval2;
2730     unsigned flags;
2731     type* ltype = lval->e_tptr;
2732
2733     /* Check for assignment to const */
2734     if (IsQualConst (ltype)) {
2735         Error ("Assignment to const");
2736     }
2737
2738     /* cc65 does not have full support for handling structs by value. Since
2739      * assigning structs is one of the more useful operations from this
2740      * familiy, allow it here.
2741      */
2742     if (IsClassStruct (ltype)) {
2743
2744         /* Bring the address of the lhs into the primary and push it */
2745         exprhs (0, 0, lval);
2746         g_push (CF_PTR | CF_UNSIGNED, 0);
2747
2748         /* Get the expression on the right of the '=' into the primary */
2749         k = hie1 (&lval2);
2750         if (k) {
2751             /* Get the address */
2752             exprhs (0, 0, &lval2);
2753         } else {
2754             /* We need an lvalue */
2755             Error ("Invalid lvalue in assignment");
2756         }
2757
2758         /* Push the address (or whatever is in ax in case of errors) */
2759         g_push (CF_PTR | CF_UNSIGNED, 0);
2760
2761         /* Check for equality of the structs */
2762         if (TypeCmp (ltype, lval2.e_tptr) < TC_EQUAL) {
2763             Error ("Incompatible types");
2764         }
2765
2766         /* Load the size of the struct into the primary */
2767         g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, SizeOf (ltype), 0);
2768
2769         /* Call the memcpy function */
2770         g_call (CF_FIXARGC, "memcpy", 4);
2771
2772     } else {
2773
2774         /* Get the address on stack if needed */
2775         PushAddr (lval);
2776
2777         /* No struct, setup flags for the load */
2778         flags = SizeOf (ltype) == 1? CF_FORCECHAR : CF_NONE;
2779
2780         /* Get the expression on the right of the '=' into the primary */
2781         if (evalexpr (flags, hie1, &lval2) == 0) {
2782             /* Constant expression. Adjust the types */
2783             assignadjust (ltype, &lval2);
2784             /* Put the value into the primary register */
2785             lconst (flags, &lval2);
2786         } else {
2787             /* Expression is not constant and already in the primary */
2788             assignadjust (ltype, &lval2);
2789         }
2790
2791         /* Generate a store instruction */
2792         store (lval);
2793
2794     }
2795
2796     /* Value is still in primary */
2797     lval->e_flags = E_MEXPR;
2798 }
2799
2800
2801
2802 int hie1 (struct expent* lval)
2803 /* Parse first level of expression hierarchy. */
2804 {
2805     int k;
2806
2807     k = hieQuest (lval);
2808     switch (curtok) {
2809
2810         case TOK_RPAREN:
2811         case TOK_SEMI:
2812             return k;
2813
2814         case TOK_ASSIGN:
2815             NextToken ();
2816             if (k == 0) {
2817                 Error ("Invalid lvalue in assignment");
2818             } else {
2819                 Assignment (lval);
2820             }
2821             break;
2822
2823         case TOK_PLUS_ASSIGN:
2824             addsubeq (&GenPASGN, lval, k);
2825             break;
2826
2827         case TOK_MINUS_ASSIGN:
2828             addsubeq (&GenSASGN, lval, k);
2829             break;
2830
2831         case TOK_MUL_ASSIGN:
2832             opeq (&GenMASGN, lval, k);
2833             break;
2834
2835         case TOK_DIV_ASSIGN:
2836             opeq (&GenDASGN, lval, k);
2837             break;
2838
2839         case TOK_MOD_ASSIGN:
2840             opeq (&GenMOASGN, lval, k);
2841             break;
2842
2843         case TOK_SHL_ASSIGN:
2844             opeq (&GenSLASGN, lval, k);
2845             break;
2846
2847         case TOK_SHR_ASSIGN:
2848             opeq (&GenSRASGN, lval, k);
2849             break;
2850
2851         case TOK_AND_ASSIGN:
2852             opeq (&GenAASGN, lval, k);
2853             break;
2854
2855         case TOK_XOR_ASSIGN:
2856             opeq (&GenXOASGN, lval, k);
2857             break;
2858
2859         case TOK_OR_ASSIGN:
2860             opeq (&GenOASGN, lval, k);
2861             break;
2862
2863         default:
2864             return k;
2865     }
2866     return 0;
2867 }
2868
2869
2870
2871 int hie0 (struct expent *lval)
2872 /* Parse comma operator. */
2873 {
2874     int k;
2875
2876     k = hie1 (lval);
2877     while (curtok == TOK_COMMA) {
2878         NextToken ();
2879         k = hie1 (lval);
2880     }
2881     return k;
2882 }
2883
2884
2885
2886 int evalexpr (unsigned flags, int (*f) (struct expent*), struct expent* lval)
2887 /* Will evaluate an expression via the given function. If the result is a
2888  * constant, 0 is returned and the value is put in the lval struct. If the
2889  * result is not constant, exprhs is called to bring the value into the
2890  * primary register and 1 is returned.
2891  */
2892 {
2893     int k;
2894
2895     /* Evaluate */
2896     k = f (lval);
2897     if (k == 0 && lval->e_flags == E_MCONST) {
2898         /* Constant expression */
2899         return 0;
2900     } else {
2901         /* Not constant, load into the primary */
2902         exprhs (flags, k, lval);
2903         return 1;
2904     }
2905 }
2906
2907
2908
2909 int expr (int (*func) (), struct expent *lval)
2910 /* Expression parser; func is either hie0 or hie1. */
2911 {
2912     int k;
2913     int savsp;
2914
2915     savsp = oursp;
2916
2917     k = (*func) (lval);
2918
2919     /* Do some checks if code generation is still constistent */
2920     if (savsp != oursp) {
2921         if (Debug) {
2922             fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
2923         } else {
2924             Internal ("oursp != savsp (%d != %d)", oursp, savsp);
2925         }
2926     }
2927     return k;
2928 }
2929
2930
2931
2932 void expression1 (struct expent* lval)
2933 /* Evaluate an expression on level 1 (no comma operator) and put it into
2934  * the primary register
2935  */
2936 {
2937     memset (lval, 0, sizeof (*lval));
2938     exprhs (CF_NONE, expr (hie1, lval), lval);
2939 }
2940
2941
2942
2943 void expression (struct expent* lval)
2944 /* Evaluate an expression and put it into the primary register */
2945 {
2946     memset (lval, 0, sizeof (*lval));
2947     exprhs (CF_NONE, expr (hie0, lval), lval);
2948 }
2949
2950
2951
2952 void constexpr (struct expent* lval)
2953 /* Get a constant value */
2954 {
2955     memset (lval, 0, sizeof (*lval));
2956     if (expr (hie1, lval) != 0 || (lval->e_flags & E_MCONST) == 0) {
2957         Error ("Constant expression expected");
2958         /* To avoid any compiler errors, make the expression a valid const */
2959         lval->e_flags = E_MCONST;
2960         lval->e_tptr = type_int;
2961         lval->e_const = 0;
2962     }
2963 }
2964
2965
2966
2967 void intexpr (struct expent* lval)
2968 /* Get an integer expression */
2969 {
2970     expression (lval);
2971     if (!IsClassInt (lval->e_tptr)) {
2972         Error ("Integer expression expected");
2973         /* To avoid any compiler errors, make the expression a valid int */
2974         lval->e_flags = E_MCONST;
2975         lval->e_tptr = type_int;
2976         lval->e_const = 0;
2977     }
2978 }
2979
2980
2981
2982 void boolexpr (struct expent* lval)
2983 /* Get a boolean expression */
2984 {
2985     /* Read an expression */
2986     expression (lval);
2987
2988     /* If it's an integer, it's ok. If it's not an integer, but a pointer,
2989      * the pointer used in a boolean context is also ok
2990      */
2991     if (!IsClassInt (lval->e_tptr) && !IsClassPtr (lval->e_tptr)) {
2992         Error ("Boolean expression expected");
2993         /* To avoid any compiler errors, make the expression a valid int */
2994         lval->e_flags = E_MCONST;
2995         lval->e_tptr = type_int;
2996         lval->e_const = 0;
2997     }
2998 }
2999
3000
3001
3002 void test (unsigned label, int cond)
3003 /* Generate code to perform test and jump if false. */
3004 {
3005     int k;
3006     struct expent lval;
3007
3008     /* Eat the parenthesis */
3009     ConsumeLParen ();
3010
3011     /* Prepare the expression, setup labels */
3012     memset (&lval, 0, sizeof (lval));
3013
3014     /* Generate code to eval the expr */
3015     k = expr (hie0, &lval);
3016     if (k == 0 && lval.e_flags == E_MCONST) {
3017         /* Constant rvalue */
3018         if (cond == 0 && lval.e_const == 0) {
3019             g_jump (label);
3020             Warning ("Unreachable code");
3021         } else if (cond && lval.e_const) {
3022             g_jump (label);
3023         }
3024         ConsumeRParen ();
3025         return;
3026     }
3027
3028     /* If the expr hasn't set condition codes, set the force-test flag */
3029     if ((lval.e_test & E_CC) == 0) {
3030         lval.e_test |= E_FORCETEST;
3031     }
3032
3033     /* Load the value into the primary register */
3034     exprhs (CF_FORCECHAR, k, &lval);
3035
3036     /* Check for the closing brace */
3037     ConsumeRParen ();
3038
3039     /* Generate the jump */
3040     if (cond) {
3041         g_truejump (CF_NONE, label);
3042     } else {
3043         /* Special case (putting this here is a small hack - but hey, the
3044          * compiler itself is one big hack...): If a semicolon follows, we
3045          * don't have a statement and may omit the jump.
3046          */
3047         if (curtok != TOK_SEMI) {
3048             g_falsejump (CF_NONE, label);
3049         }
3050     }
3051 }
3052
3053
3054
3055