]> git.sur5r.net Git - cc65/blob - src/cc65/expr.c
Added a command-line option to compile a program, with __cdecl__ as the default calli...
[cc65] / src / cc65 / expr.c
1 /* expr.c
2 **
3 ** 1998-06-21, Ullrich von Bassewitz
4 ** 2015-04-19, Greg King
5 */
6
7
8
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 /* common */
13 #include "check.h"
14 #include "debugflag.h"
15 #include "xmalloc.h"
16
17 /* cc65 */
18 #include "asmcode.h"
19 #include "asmlabel.h"
20 #include "asmstmt.h"
21 #include "assignment.h"
22 #include "codegen.h"
23 #include "declare.h"
24 #include "error.h"
25 #include "funcdesc.h"
26 #include "function.h"
27 #include "global.h"
28 #include "litpool.h"
29 #include "loadexpr.h"
30 #include "macrotab.h"
31 #include "preproc.h"
32 #include "scanner.h"
33 #include "shiftexpr.h"
34 #include "stackptr.h"
35 #include "standard.h"
36 #include "stdfunc.h"
37 #include "symtab.h"
38 #include "typecmp.h"
39 #include "typeconv.h"
40 #include "expr.h"
41
42
43
44 /*****************************************************************************/
45 /*                                   Data                                    */
46 /*****************************************************************************/
47
48
49
50 /* Generator attributes */
51 #define GEN_NOPUSH      0x01            /* Don't push lhs */
52 #define GEN_COMM        0x02            /* Operator is commutative */
53
54 /* Map a generator function and its attributes to a token */
55 typedef struct {
56     token_t       Tok;                  /* Token to map to */
57     unsigned      Flags;                /* Flags for generator function */
58     void          (*Func) (unsigned, unsigned long);    /* Generator func */
59 } GenDesc;
60
61 /* Descriptors for the operations */
62 static GenDesc GenPASGN  = { TOK_PLUS_ASSIGN,   GEN_NOPUSH,     g_add };
63 static GenDesc GenSASGN  = { TOK_MINUS_ASSIGN,  GEN_NOPUSH,     g_sub };
64 static GenDesc GenMASGN  = { TOK_MUL_ASSIGN,    GEN_NOPUSH,     g_mul };
65 static GenDesc GenDASGN  = { TOK_DIV_ASSIGN,    GEN_NOPUSH,     g_div };
66 static GenDesc GenMOASGN = { TOK_MOD_ASSIGN,    GEN_NOPUSH,     g_mod };
67 static GenDesc GenSLASGN = { TOK_SHL_ASSIGN,    GEN_NOPUSH,     g_asl };
68 static GenDesc GenSRASGN = { TOK_SHR_ASSIGN,    GEN_NOPUSH,     g_asr };
69 static GenDesc GenAASGN  = { TOK_AND_ASSIGN,    GEN_NOPUSH,     g_and };
70 static GenDesc GenXOASGN = { TOK_XOR_ASSIGN,    GEN_NOPUSH,     g_xor };
71 static GenDesc GenOASGN  = { TOK_OR_ASSIGN,     GEN_NOPUSH,     g_or  };
72
73
74
75 /*****************************************************************************/
76 /*                             Helper functions                              */
77 /*****************************************************************************/
78
79
80
81 static unsigned GlobalModeFlags (const ExprDesc* Expr)
82 /* Return the addressing mode flags for the given expression */
83 {
84     switch (ED_GetLoc (Expr)) {
85         case E_LOC_ABS:         return CF_ABSOLUTE;
86         case E_LOC_GLOBAL:      return CF_EXTERNAL;
87         case E_LOC_STATIC:      return CF_STATIC;
88         case E_LOC_REGISTER:    return CF_REGVAR;
89         case E_LOC_STACK:       return CF_NONE;
90         case E_LOC_PRIMARY:     return CF_NONE;
91         case E_LOC_EXPR:        return CF_NONE;
92         case E_LOC_LITERAL:     return CF_STATIC;       /* Same as static */
93         default:
94             Internal ("GlobalModeFlags: Invalid location flags value: 0x%04X", Expr->Flags);
95             /* NOTREACHED */
96             return 0;
97     }
98 }
99
100
101
102 void ExprWithCheck (void (*Func) (ExprDesc*), ExprDesc* Expr)
103 /* Call an expression function with checks. */
104 {
105     /* Remember the stack pointer */
106     int OldSP = StackPtr;
107
108     /* Call the expression function */
109     (*Func) (Expr);
110
111     /* Do some checks to see if code generation is still consistent */
112     if (StackPtr != OldSP) {
113         if (Debug) {
114             Error ("Code generation messed up: "
115                    "StackPtr is %d, should be %d",
116                    StackPtr, OldSP);
117         } else {
118             Internal ("Code generation messed up: "
119                       "StackPtr is %d, should be %d",
120                       StackPtr, OldSP);
121         }
122     }
123 }
124
125
126
127 void MarkedExprWithCheck (void (*Func) (ExprDesc*), ExprDesc* Expr)
128 /* Call an expression function with checks and record start and end of the
129 ** generated code.
130 */
131 {
132     CodeMark Start, End;
133     GetCodePos (&Start);
134     ExprWithCheck (Func, Expr);
135     GetCodePos (&End);
136     ED_SetCodeRange (Expr, &Start, &End);
137 }
138
139
140
141 static Type* promoteint (Type* lhst, Type* rhst)
142 /* In an expression with two ints, return the type of the result */
143 {
144     /* Rules for integer types:
145     **   - If one of the values is a long, the result is long.
146     **   - If one of the values is unsigned, the result is also unsigned.
147     **   - Otherwise the result is an int.
148     */
149     if (IsTypeLong (lhst) || IsTypeLong (rhst)) {
150         if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
151             return type_ulong;
152         } else {
153             return type_long;
154         }
155     } else {
156         if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
157             return type_uint;
158         } else {
159             return type_int;
160         }
161     }
162 }
163
164
165
166 static unsigned typeadjust (ExprDesc* lhs, ExprDesc* rhs, int NoPush)
167 /* Adjust the two values for a binary operation. lhs is expected on stack or
168 ** to be constant, rhs is expected to be in the primary register or constant.
169 ** The function will put the type of the result into lhs and return the
170 ** code generator flags for the operation.
171 ** If NoPush is given, it is assumed that the operation does not expect the lhs
172 ** to be on stack, and that lhs is in a register instead.
173 ** Beware: The function does only accept int types.
174 */
175 {
176     unsigned ltype, rtype;
177     unsigned flags;
178
179     /* Get the type strings */
180     Type* lhst = lhs->Type;
181     Type* rhst = rhs->Type;
182
183     /* Generate type adjustment code if needed */
184     ltype = TypeOf (lhst);
185     if (ED_IsLocAbs (lhs)) {
186         ltype |= CF_CONST;
187     }
188     if (NoPush) {
189         /* Value is in primary register*/
190         ltype |= CF_REG;
191     }
192     rtype = TypeOf (rhst);
193     if (ED_IsLocAbs (rhs)) {
194         rtype |= CF_CONST;
195     }
196     flags = g_typeadjust (ltype, rtype);
197
198     /* Set the type of the result */
199     lhs->Type = promoteint (lhst, rhst);
200
201     /* Return the code generator flags */
202     return flags;
203 }
204
205
206
207 static const GenDesc* FindGen (token_t Tok, const GenDesc* Table)
208 /* Find a token in a generator table */
209 {
210     while (Table->Tok != TOK_INVALID) {
211         if (Table->Tok == Tok) {
212             return Table;
213         }
214         ++Table;
215     }
216     return 0;
217 }
218
219
220
221 static int TypeSpecAhead (void)
222 /* Return true if some sort of type is waiting (helper for cast and sizeof()
223 ** in hie10).
224 */
225 {
226     SymEntry* Entry;
227
228     /* There's a type waiting if:
229     **
230     ** We have an opening paren, and
231     **   a.  the next token is a type, or
232     **   b.  the next token is a type qualifier, or
233     **   c.  the next token is a typedef'd type
234     */
235     return CurTok.Tok == TOK_LPAREN && (
236            TokIsType (&NextTok)                         ||
237            TokIsTypeQual (&NextTok)                     ||
238            (NextTok.Tok  == TOK_IDENT                   &&
239            (Entry = FindSym (NextTok.Ident)) != 0       &&
240            SymIsTypeDef (Entry)));
241 }
242
243
244
245 void PushAddr (const ExprDesc* Expr)
246 /* If the expression contains an address that was somehow evaluated,
247 ** push this address on the stack. This is a helper function for all
248 ** sorts of implicit or explicit assignment functions where the lvalue
249 ** must be saved if it's not constant, before evaluating the rhs.
250 */
251 {
252     /* Get the address on stack if needed */
253     if (ED_IsLocExpr (Expr)) {
254         /* Push the address (always a pointer) */
255         g_push (CF_PTR, 0);
256     }
257 }
258
259
260
261 static void WarnConstCompareResult (void)
262 /* If the result of a comparison is constant, this is suspicious when not in
263 ** preprocessor mode.
264 */
265 {
266     if (!Preprocessing && IS_Get (&WarnConstComparison) != 0) {
267         Warning ("Result of comparison is constant");
268     }
269 }
270
271
272
273 /*****************************************************************************/
274 /*                                   code                                    */
275 /*****************************************************************************/
276
277
278
279 static unsigned FunctionParamList (FuncDesc* Func, int IsFastcall)
280 /* Parse a function parameter list and pass the parameters to the called
281 ** function. Depending on several criteria this may be done by just pushing
282 ** each parameter separately, or creating the parameter frame once and then
283 ** storing into this frame.
284 ** The function returns the size of the parameters pushed.
285 */
286 {
287     ExprDesc Expr;
288
289     /* Initialize variables */
290     SymEntry* Param       = 0;  /* Keep gcc silent */
291     unsigned  ParamSize   = 0;  /* Size of parameters pushed */
292     unsigned  ParamCount  = 0;  /* Number of parameters pushed */
293     unsigned  FrameSize   = 0;  /* Size of parameter frame */
294     unsigned  FrameParams = 0;  /* Number of params in frame */
295     int       FrameOffs   = 0;  /* Offset into parameter frame */
296     int       Ellipsis    = 0;  /* Function is variadic */
297
298     /* As an optimization, we may allocate the complete parameter frame at
299     ** once instead of pushing each parameter as it comes. We may do that,
300     ** if...
301     **
302     **  - optimizations that increase code size are enabled (allocating the
303     **    stack frame at once gives usually larger code).
304     **  - we have more than one parameter to push (don't count the last param
305     **    for __fastcall__ functions).
306     **
307     ** The FrameSize variable will contain a value > 0 if storing into a frame
308     ** (instead of pushing) is enabled.
309     **
310     */
311     if (IS_Get (&CodeSizeFactor) >= 200) {
312
313         /* Calculate the number and size of the parameters */
314         FrameParams = Func->ParamCount;
315         FrameSize   = Func->ParamSize;
316         if (FrameParams > 0 && IsFastcall) {
317             /* Last parameter is not pushed */
318             FrameSize -= CheckedSizeOf (Func->LastParam->Type);
319             --FrameParams;
320         }
321
322         /* Do we have more than one parameter in the frame? */
323         if (FrameParams > 1) {
324             /* Okeydokey, setup the frame */
325             FrameOffs = StackPtr;
326             g_space (FrameSize);
327             StackPtr -= FrameSize;
328         } else {
329             /* Don't use a preallocated frame */
330             FrameSize = 0;
331         }
332     }
333
334     /* Parse the actual parameter list */
335     while (CurTok.Tok != TOK_RPAREN) {
336
337         unsigned Flags;
338
339         /* Count arguments */
340         ++ParamCount;
341
342         /* Fetch the pointer to the next argument, check for too many args */
343         if (ParamCount <= Func->ParamCount) {
344             /* Beware: If there are parameters with identical names, they
345             ** cannot go into the same symbol table, which means that in this
346             ** case of errorneous input, the number of nodes in the symbol
347             ** table and ParamCount are NOT equal. We have to handle this case
348             ** below to avoid segmentation violations. Since we know that this
349             ** problem can only occur if there is more than one parameter,
350             ** we will just use the last one.
351             */
352             if (ParamCount == 1) {
353                 /* First argument */
354                 Param = Func->SymTab->SymHead;
355             } else if (Param->NextSym != 0) {
356                 /* Next argument */
357                 Param = Param->NextSym;
358                 CHECK ((Param->Flags & SC_PARAM) != 0);
359             }
360         } else if (!Ellipsis) {
361             /* Too many arguments. Do we have an open param list? */
362             if ((Func->Flags & FD_VARIADIC) == 0) {
363                 /* End of param list reached, no ellipsis */
364                 Error ("Too many arguments in function call");
365             }
366             /* Assume an ellipsis even in case of errors to avoid an error
367             ** message for each other argument.
368             */
369             Ellipsis = 1;
370         }
371
372         /* Evaluate the parameter expression */
373         hie1 (&Expr);
374
375         /* If we don't have an argument spec, accept anything, otherwise
376         ** convert the actual argument to the type needed.
377         */
378         Flags = CF_NONE;
379         if (!Ellipsis) {
380
381             /* Convert the argument to the parameter type if needed */
382             TypeConversion (&Expr, Param->Type);
383
384             /* If we have a prototype, chars may be pushed as chars */
385             Flags |= CF_FORCECHAR;
386
387         } else {
388
389             /* No prototype available. Convert array to "pointer to first
390             ** element", and function to "pointer to function".
391             */
392             Expr.Type = PtrConversion (Expr.Type);
393
394         }
395
396         /* Load the value into the primary if it is not already there */
397         LoadExpr (Flags, &Expr);
398
399         /* Use the type of the argument for the push */
400         Flags |= TypeOf (Expr.Type);
401
402         /* If this is a fastcall function, don't push the last argument */
403         if (ParamCount != Func->ParamCount || !IsFastcall) {
404             unsigned ArgSize = sizeofarg (Flags);
405             if (FrameSize > 0) {
406                 /* We have the space already allocated, store in the frame.
407                 ** Because of invalid type conversions (that have produced an
408                 ** error before), we can end up here with a non-aligned stack
409                 ** frame. Since no output will be generated anyway, handle
410                 ** these cases gracefully instead of doing a CHECK.
411                 */
412                 if (FrameSize >= ArgSize) {
413                     FrameSize -= ArgSize;
414                 } else {
415                     FrameSize = 0;
416                 }
417                 FrameOffs -= ArgSize;
418                 /* Store */
419                 g_putlocal (Flags | CF_NOKEEP, FrameOffs, Expr.IVal);
420             } else {
421                 /* Push the argument */
422                 g_push (Flags, Expr.IVal);
423             }
424
425             /* Calculate total parameter size */
426             ParamSize += ArgSize;
427         }
428
429         /* Check for end of argument list */
430         if (CurTok.Tok != TOK_COMMA) {
431             break;
432         }
433         NextToken ();
434     }
435
436     /* Check if we had enough parameters */
437     if (ParamCount < Func->ParamCount) {
438         Error ("Too few arguments in function call");
439     }
440
441     /* The function returns the size of all parameters pushed onto the stack.
442     ** However, if there are parameters missing (which is an error and was
443     ** flagged by the compiler) AND a stack frame was preallocated above,
444     ** we would loose track of the stackpointer and generate an internal error
445     ** later. So we correct the value by the parameters that should have been
446     ** pushed to avoid an internal compiler error. Since an error was
447     ** generated before, no code will be output anyway.
448     */
449     return ParamSize + FrameSize;
450 }
451
452
453
454 static void FunctionCall (ExprDesc* Expr)
455 /* Perform a function call. */
456 {
457     FuncDesc*     Func;           /* Function descriptor */
458     int           IsFuncPtr;      /* Flag */
459     unsigned      ParamSize;      /* Number of parameter bytes */
460     CodeMark      Mark;
461     int           PtrOffs = 0;    /* Offset of function pointer on stack */
462     int           IsFastcall = 0; /* True if it's a fast-call function */
463     int           PtrOnStack = 0; /* True if a pointer copy is on stack */
464
465     /* Skip the left paren */
466     NextToken ();
467
468     /* Get a pointer to the function descriptor from the type string */
469     Func = GetFuncDesc (Expr->Type);
470
471     /* Handle function pointers transparently */
472     IsFuncPtr = IsTypeFuncPtr (Expr->Type);
473     if (IsFuncPtr) {
474         /* Check whether it's a fastcall function that has parameters */
475         IsFastcall = (Func->Flags & FD_VARIADIC) == 0 && Func->ParamCount > 0 &&
476             (AutoCDecl ?
477              IsQualFastcall (Expr->Type + 1) :
478              !IsQualCDecl (Expr->Type + 1));
479
480         /* Things may be difficult, depending on where the function pointer
481         ** resides. If the function pointer is an expression of some sort
482         ** (not a local or global variable), we have to evaluate this
483         ** expression now and save the result for later. Since calls to
484         ** function pointers may be nested, we must save it onto the stack.
485         ** For fastcall functions we do also need to place a copy of the
486         ** pointer on stack, since we cannot use a/x.
487         */
488         PtrOnStack = IsFastcall || !ED_IsConst (Expr);
489         if (PtrOnStack) {
490
491             /* Not a global or local variable, or a fastcall function. Load
492             ** the pointer into the primary and mark it as an expression.
493             */
494             LoadExpr (CF_NONE, Expr);
495             ED_MakeRValExpr (Expr);
496
497             /* Remember the code position */
498             GetCodePos (&Mark);
499
500             /* Push the pointer onto the stack and remember the offset */
501             g_push (CF_PTR, 0);
502             PtrOffs = StackPtr;
503         }
504
505     } else {
506         /* Check function attributes */
507         if (Expr->Sym && SymHasAttr (Expr->Sym, atNoReturn)) {
508             /* For now, handle as if a return statement was encountered */
509             F_ReturnFound (CurrentFunc);
510         }
511
512         /* Check for known standard functions and inline them */
513         if (Expr->Name != 0) {
514             int StdFunc = FindStdFunc ((const char*) Expr->Name);
515             if (StdFunc >= 0) {
516                 /* Inline this function */
517                 HandleStdFunc (StdFunc, Func, Expr);
518                 return;
519             }
520         }
521
522         /* If we didn't inline the function, get fastcall info */
523         IsFastcall = (Func->Flags & FD_VARIADIC) == 0 &&
524             (AutoCDecl ?
525              IsQualFastcall (Expr->Type) :
526              !IsQualCDecl (Expr->Type));
527     }
528
529     /* Parse the parameter list */
530     ParamSize = FunctionParamList (Func, IsFastcall);
531
532     /* We need the closing paren here */
533     ConsumeRParen ();
534
535     /* Special handling for function pointers */
536     if (IsFuncPtr) {
537
538         /* If the function is not a fastcall function, load the pointer to
539         ** the function into the primary.
540         */
541         if (!IsFastcall) {
542
543             /* Not a fastcall function - we may use the primary */
544             if (PtrOnStack) {
545                 /* If we have no parameters, the pointer is still in the
546                 ** primary. Remove the code to push it and correct the
547                 ** stack pointer.
548                 */
549                 if (ParamSize == 0) {
550                     RemoveCode (&Mark);
551                     PtrOnStack = 0;
552                 } else {
553                     /* Load from the saved copy */
554                     g_getlocal (CF_PTR, PtrOffs);
555                 }
556             } else {
557                 /* Load from original location */
558                 LoadExpr (CF_NONE, Expr);
559             }
560
561             /* Call the function */
562             g_callind (TypeOf (Expr->Type+1), ParamSize, PtrOffs);
563
564         } else {
565
566             /* Fastcall function. We cannot use the primary for the function
567             ** pointer and must therefore use an offset to the stack location.
568             ** Since fastcall functions may never be variadic, we can use the
569             ** index register for this purpose.
570             */
571             g_callind (CF_LOCAL, ParamSize, PtrOffs);
572         }
573
574         /* If we have a pointer on stack, remove it */
575         if (PtrOnStack) {
576             g_drop (SIZEOF_PTR);
577             pop (CF_PTR);
578         }
579
580         /* Skip T_PTR */
581         ++Expr->Type;
582
583     } else {
584
585         /* Normal function */
586         g_call (TypeOf (Expr->Type), (const char*) Expr->Name, ParamSize);
587
588     }
589
590     /* The function result is an rvalue in the primary register */
591     ED_MakeRValExpr (Expr);
592     Expr->Type = GetFuncReturn (Expr->Type);
593 }
594
595
596
597 static void Primary (ExprDesc* E)
598 /* This is the lowest level of the expression parser. */
599 {
600     SymEntry* Sym;
601
602     /* Initialize fields in the expression stucture */
603     ED_Init (E);
604
605     /* Character and integer constants. */
606     if (CurTok.Tok == TOK_ICONST || CurTok.Tok == TOK_CCONST) {
607         E->IVal  = CurTok.IVal;
608         E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
609         E->Type  = CurTok.Type;
610         NextToken ();
611         return;
612     }
613
614     /* Floating point constant */
615     if (CurTok.Tok == TOK_FCONST) {
616         E->FVal  = CurTok.FVal;
617         E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
618         E->Type  = CurTok.Type;
619         NextToken ();
620         return;
621     }
622
623     /* Process parenthesized subexpression by calling the whole parser
624     ** recursively.
625     */
626     if (CurTok.Tok == TOK_LPAREN) {
627         NextToken ();
628         hie0 (E);
629         ConsumeRParen ();
630         return;
631     }
632
633     /* If we run into an identifier in preprocessing mode, we assume that this
634     ** is an undefined macro and replace it by a constant value of zero.
635     */
636     if (Preprocessing && CurTok.Tok == TOK_IDENT) {
637         NextToken ();
638         ED_MakeConstAbsInt (E, 0);
639         return;
640     }
641
642     /* All others may only be used if the expression evaluation is not called
643     ** recursively by the preprocessor.
644     */
645     if (Preprocessing) {
646         /* Illegal expression in PP mode */
647         Error ("Preprocessor expression expected");
648         ED_MakeConstAbsInt (E, 1);
649         return;
650     }
651
652     switch (CurTok.Tok) {
653
654         case TOK_IDENT:
655             /* Identifier. Get a pointer to the symbol table entry */
656             Sym = E->Sym = FindSym (CurTok.Ident);
657
658             /* Is the symbol known? */
659             if (Sym) {
660
661                 /* We found the symbol - skip the name token */
662                 NextToken ();
663
664                 /* Check for illegal symbol types */
665                 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
666                 if (Sym->Flags & SC_TYPE) {
667                     /* Cannot use type symbols */
668                     Error ("Variable identifier expected");
669                     /* Assume an int type to make E valid */
670                     E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
671                     E->Type  = type_int;
672                     return;
673                 }
674
675                 /* Mark the symbol as referenced */
676                 Sym->Flags |= SC_REF;
677
678                 /* The expression type is the symbol type */
679                 E->Type = Sym->Type;
680
681                 /* Check for legal symbol types */
682                 if ((Sym->Flags & SC_CONST) == SC_CONST) {
683                     /* Enum or some other numeric constant */
684                     E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
685                     E->IVal = Sym->V.ConstVal;
686                 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
687                     /* Function */
688                     E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
689                     E->Name = (unsigned long) Sym->Name;
690                 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
691                     /* Local variable. If this is a parameter for a variadic
692                     ** function, we have to add some address calculations, and the
693                     ** address is not const.
694                     */
695                     if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
696                         /* Variadic parameter */
697                         g_leavariadic (Sym->V.Offs - F_GetParamSize (CurrentFunc));
698                         E->Flags = E_LOC_EXPR | E_RTYPE_LVAL;
699                     } else {
700                         /* Normal parameter */
701                         E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
702                         E->IVal  = Sym->V.Offs;
703                     }
704                 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
705                     /* Register variable, zero page based */
706                     E->Flags = E_LOC_REGISTER | E_RTYPE_LVAL;
707                     E->Name  = Sym->V.R.RegOffs;
708                 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
709                     /* Static variable */
710                     if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
711                         E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
712                         E->Name = (unsigned long) Sym->Name;
713                     } else {
714                         E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
715                         E->Name = Sym->V.Label;
716                     }
717                 } else {
718                     /* Local static variable */
719                     E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
720                     E->Name  = Sym->V.Offs;
721                 }
722
723                 /* We've made all variables lvalues above. However, this is
724                 ** not always correct: An array is actually the address of its
725                 ** first element, which is a rvalue, and a function is a
726                 ** rvalue, too, because we cannot store anything in a function.
727                 ** So fix the flags depending on the type.
728                 */
729                 if (IsTypeArray (E->Type) || IsTypeFunc (E->Type)) {
730                     ED_MakeRVal (E);
731                 }
732
733             } else {
734
735                 /* We did not find the symbol. Remember the name, then skip it */
736                 ident Ident;
737                 strcpy (Ident, CurTok.Ident);
738                 NextToken ();
739
740                 /* IDENT is either an auto-declared function or an undefined variable. */
741                 if (CurTok.Tok == TOK_LPAREN) {
742                     /* C99 doesn't allow calls to undefined functions, so
743                     ** generate an error and otherwise a warning. Declare a
744                     ** function returning int. For that purpose, prepare a
745                     ** function signature for a function having an empty param
746                     ** list and returning int.
747                     */
748                     if (IS_Get (&Standard) >= STD_C99) {
749                         Error ("Call to undefined function `%s'", Ident);
750                     } else {
751                         Warning ("Call to undefined function `%s'", Ident);
752                     }
753                     Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
754                     E->Type  = Sym->Type;
755                     E->Flags = E_LOC_GLOBAL | E_RTYPE_RVAL;
756                     E->Name  = (unsigned long) Sym->Name;
757                 } else {
758                     /* Undeclared Variable */
759                     Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
760                     E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
761                     E->Type = type_int;
762                     Error ("Undefined symbol: `%s'", Ident);
763                 }
764
765             }
766             break;
767
768         case TOK_SCONST:
769         case TOK_WCSCONST:
770             /* String literal */
771             E->LVal  = UseLiteral (CurTok.SVal);
772             E->Type  = GetCharArrayType (GetLiteralSize (CurTok.SVal));
773             E->Flags = E_LOC_LITERAL | E_RTYPE_RVAL;
774             E->IVal  = 0;
775             E->Name  = GetLiteralLabel (CurTok.SVal);
776             NextToken ();
777             break;
778
779         case TOK_ASM:
780             /* ASM statement */
781             AsmStatement ();
782             E->Flags = E_LOC_EXPR | E_RTYPE_RVAL;
783             E->Type  = type_void;
784             break;
785
786         case TOK_A:
787             /* Register pseudo variable */
788             E->Type  = type_uchar;
789             E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
790             NextToken ();
791             break;
792
793         case TOK_AX:
794             /* Register pseudo variable */
795             E->Type  = type_uint;
796             E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
797             NextToken ();
798             break;
799
800         case TOK_EAX:
801             /* Register pseudo variable */
802             E->Type  = type_ulong;
803             E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
804             NextToken ();
805             break;
806
807         default:
808             /* Illegal primary. Be sure to skip the token to avoid endless
809             ** error loops.
810             */
811             Error ("Expression expected");
812             NextToken ();
813             ED_MakeConstAbsInt (E, 1);
814             break;
815     }
816 }
817
818
819
820 static void ArrayRef (ExprDesc* Expr)
821 /* Handle an array reference. This function needs a rewrite. */
822 {
823     int         ConstBaseAddr;
824     ExprDesc    Subscript;
825     CodeMark    Mark1;
826     CodeMark    Mark2;
827     TypeCode    Qualifiers;
828     Type*       ElementType;
829     Type*       tptr1;
830
831
832     /* Skip the bracket */
833     NextToken ();
834
835     /* Get the type of left side */
836     tptr1 = Expr->Type;
837
838     /* We can apply a special treatment for arrays that have a const base
839     ** address. This is true for most arrays and will produce a lot better
840     ** code. Check if this is a const base address.
841     */
842     ConstBaseAddr = ED_IsRVal (Expr) &&
843                     (ED_IsLocConst (Expr) || ED_IsLocStack (Expr));
844
845     /* If we have a constant base, we delay the address fetch */
846     GetCodePos (&Mark1);
847     if (!ConstBaseAddr) {
848         /* Get a pointer to the array into the primary */
849         LoadExpr (CF_NONE, Expr);
850
851         /* Get the array pointer on stack. Do not push more than 16
852         ** bit, even if this value is greater, since we cannot handle
853         ** other than 16bit stuff when doing indexing.
854         */
855         GetCodePos (&Mark2);
856         g_push (CF_PTR, 0);
857     }
858
859     /* TOS now contains ptr to array elements. Get the subscript. */
860     MarkedExprWithCheck (hie0, &Subscript);
861
862     /* Check the types of array and subscript. We can either have a
863     ** pointer/array to the left, in which case the subscript must be of an
864     ** integer type, or we have an integer to the left, in which case the
865     ** subscript must be a pointer/array.
866     ** Since we do the necessary checking here, we can rely later on the
867     ** correct types.
868     */
869     Qualifiers = T_QUAL_NONE;
870     if (IsClassPtr (Expr->Type)) {
871         if (!IsClassInt (Subscript.Type))  {
872             Error ("Array subscript is not an integer");
873             /* To avoid any compiler errors, make the expression a valid int */
874             ED_MakeConstAbsInt (&Subscript, 0);
875         }
876         if (IsTypeArray (Expr->Type)) {
877             Qualifiers = GetQualifier (Expr->Type);
878         }
879         ElementType = Indirect (Expr->Type);
880     } else if (IsClassInt (Expr->Type)) {
881         if (!IsClassPtr (Subscript.Type)) {
882             Error ("Subscripted value is neither array nor pointer");
883             /* To avoid compiler errors, make the subscript a char[] at
884             ** address 0.
885             */
886             ED_MakeConstAbs (&Subscript, 0, GetCharArrayType (1));
887         } else if (IsTypeArray (Subscript.Type)) {
888             Qualifiers = GetQualifier (Subscript.Type);
889         }
890         ElementType = Indirect (Subscript.Type);
891     } else {
892         Error ("Cannot subscript");
893         /* To avoid compiler errors, fake both the array and the subscript, so
894         ** we can just proceed.
895         */
896         ED_MakeConstAbs (Expr, 0, GetCharArrayType (1));
897         ED_MakeConstAbsInt (&Subscript, 0);
898         ElementType = Indirect (Expr->Type);
899     }
900
901     /* The element type has the combined qualifiers from itself and the array,
902     ** it is a member of (if any).
903     */
904     if (GetQualifier (ElementType) != (GetQualifier (ElementType) | Qualifiers)) {
905         ElementType = TypeDup (ElementType);
906         ElementType->C |= Qualifiers;
907     }
908
909     /* If the subscript is a bit-field, load it and make it an rvalue */
910     if (ED_IsBitField (&Subscript)) {
911         LoadExpr (CF_NONE, &Subscript);
912         ED_MakeRValExpr (&Subscript);
913     }
914
915     /* Check if the subscript is constant absolute value */
916     if (ED_IsConstAbs (&Subscript) && ED_CodeRangeIsEmpty (&Subscript)) {
917
918         /* The array subscript is a numeric constant. If we had pushed the
919         ** array base address onto the stack before, we can remove this value,
920         ** since we can generate expression+offset.
921         */
922         if (!ConstBaseAddr) {
923             RemoveCode (&Mark2);
924         } else {
925             /* Get an array pointer into the primary */
926             LoadExpr (CF_NONE, Expr);
927         }
928
929         if (IsClassPtr (Expr->Type)) {
930
931             /* Lhs is pointer/array. Scale the subscript value according to
932             ** the element size.
933             */
934             Subscript.IVal *= CheckedSizeOf (ElementType);
935
936             /* Remove the address load code */
937             RemoveCode (&Mark1);
938
939             /* In case of an array, we can adjust the offset of the expression
940             ** already in Expr. If the base address was a constant, we can even
941             ** remove the code that loaded the address into the primary.
942             */
943             if (IsTypeArray (Expr->Type)) {
944
945                 /* Adjust the offset */
946                 Expr->IVal += Subscript.IVal;
947
948             } else {
949
950                 /* It's a pointer, so we do have to load it into the primary
951                 ** first (if it's not already there).
952                 */
953                 if (ConstBaseAddr || ED_IsLVal (Expr)) {
954                     LoadExpr (CF_NONE, Expr);
955                     ED_MakeRValExpr (Expr);
956                 }
957
958                 /* Use the offset */
959                 Expr->IVal = Subscript.IVal;
960             }
961
962         } else {
963
964             /* Scale the rhs value according to the element type */
965             g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
966
967             /* Add the subscript. Since arrays are indexed by integers,
968             ** we will ignore the true type of the subscript here and
969             ** use always an int. #### Use offset but beware of LoadExpr!
970             */
971             g_inc (CF_INT | CF_CONST, Subscript.IVal);
972
973         }
974
975     } else {
976
977         /* Array subscript is not constant. Load it into the primary */
978         GetCodePos (&Mark2);
979         LoadExpr (CF_NONE, &Subscript);
980
981         /* Do scaling */
982         if (IsClassPtr (Expr->Type)) {
983
984             /* Indexing is based on unsigneds, so we will just use the integer
985             ** portion of the index (which is in (e)ax, so there's no further
986             ** action required).
987             */
988             g_scale (CF_INT, CheckedSizeOf (ElementType));
989
990         } else {
991
992             /* Get the int value on top. If we come here, we're sure, both
993             ** values are 16 bit (the first one was truncated if necessary
994             ** and the second one is a pointer). Note: If ConstBaseAddr is
995             ** true, we don't have a value on stack, so to "swap" both, just
996             ** push the subscript.
997             */
998             if (ConstBaseAddr) {
999                 g_push (CF_INT, 0);
1000                 LoadExpr (CF_NONE, Expr);
1001                 ConstBaseAddr = 0;
1002             } else {
1003                 g_swap (CF_INT);
1004             }
1005
1006             /* Scale it */
1007             g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
1008
1009         }
1010
1011         /* The offset is now in the primary register. It we didn't have a
1012         ** constant base address for the lhs, the lhs address is already
1013         ** on stack, and we must add the offset. If the base address was
1014         ** constant, we call special functions to add the address to the
1015         ** offset value.
1016         */
1017         if (!ConstBaseAddr) {
1018
1019             /* The array base address is on stack and the subscript is in the
1020             ** primary. Add both.
1021             */
1022             g_add (CF_INT, 0);
1023
1024         } else {
1025
1026             /* The subscript is in the primary, and the array base address is
1027             ** in Expr. If the subscript has itself a constant address, it is
1028             ** often a better idea to reverse again the order of the
1029             ** evaluation. This will generate better code if the subscript is
1030             ** a byte sized variable. But beware: This is only possible if the
1031             ** subscript was not scaled, that is, if this was a byte array
1032             ** or pointer.
1033             */
1034             if ((ED_IsLocConst (&Subscript) || ED_IsLocStack (&Subscript)) &&
1035                 CheckedSizeOf (ElementType) == SIZEOF_CHAR) {
1036
1037                 unsigned Flags;
1038
1039                 /* Reverse the order of evaluation */
1040                 if (CheckedSizeOf (Subscript.Type) == SIZEOF_CHAR) {
1041                     Flags = CF_CHAR;
1042                 } else {
1043                     Flags = CF_INT;
1044                 }
1045                 RemoveCode (&Mark2);
1046
1047                 /* Get a pointer to the array into the primary. */
1048                 LoadExpr (CF_NONE, Expr);
1049
1050                 /* Add the variable */
1051                 if (ED_IsLocStack (&Subscript)) {
1052                     g_addlocal (Flags, Subscript.IVal);
1053                 } else {
1054                     Flags |= GlobalModeFlags (&Subscript);
1055                     g_addstatic (Flags, Subscript.Name, Subscript.IVal);
1056                 }
1057             } else {
1058
1059                 if (ED_IsLocAbs (Expr)) {
1060                     /* Constant numeric address. Just add it */
1061                     g_inc (CF_INT, Expr->IVal);
1062                 } else if (ED_IsLocStack (Expr)) {
1063                     /* Base address is a local variable address */
1064                     if (IsTypeArray (Expr->Type)) {
1065                         g_addaddr_local (CF_INT, Expr->IVal);
1066                     } else {
1067                         g_addlocal (CF_PTR, Expr->IVal);
1068                     }
1069                 } else {
1070                     /* Base address is a static variable address */
1071                     unsigned Flags = CF_INT | GlobalModeFlags (Expr);
1072                     if (ED_IsRVal (Expr)) {
1073                         /* Add the address of the location */
1074                         g_addaddr_static (Flags, Expr->Name, Expr->IVal);
1075                     } else {
1076                         /* Add the contents of the location */
1077                         g_addstatic (Flags, Expr->Name, Expr->IVal);
1078                     }
1079                 }
1080             }
1081
1082
1083         }
1084
1085         /* The result is an expression in the primary */
1086         ED_MakeRValExpr (Expr);
1087
1088     }
1089
1090     /* Result is of element type */
1091     Expr->Type = ElementType;
1092
1093     /* An array element is actually a variable. So the rules for variables
1094     ** with respect to the reference type apply: If it's an array, it is
1095     ** a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1096     ** but an array cannot contain functions).
1097     */
1098     if (IsTypeArray (Expr->Type)) {
1099         ED_MakeRVal (Expr);
1100     } else {
1101         ED_MakeLVal (Expr);
1102     }
1103
1104     /* Consume the closing bracket */
1105     ConsumeRBrack ();
1106 }
1107
1108
1109
1110 static void StructRef (ExprDesc* Expr)
1111 /* Process struct field after . or ->. */
1112 {
1113     ident Ident;
1114     SymEntry* Field;
1115     Type* FinalType;
1116     TypeCode Q;
1117
1118     /* Skip the token and check for an identifier */
1119     NextToken ();
1120     if (CurTok.Tok != TOK_IDENT) {
1121         Error ("Identifier expected");
1122         /* Make the expression an integer at address zero */
1123         ED_MakeConstAbs (Expr, 0, type_int);
1124         return;
1125     }
1126
1127     /* Get the symbol table entry and check for a struct field */
1128     strcpy (Ident, CurTok.Ident);
1129     NextToken ();
1130     Field = FindStructField (Expr->Type, Ident);
1131     if (Field == 0) {
1132         Error ("Struct/union has no field named `%s'", Ident);
1133         /* Make the expression an integer at address zero */
1134         ED_MakeConstAbs (Expr, 0, type_int);
1135         return;
1136     }
1137
1138     /* If we have a struct pointer that is an lvalue and not already in the
1139     ** primary, load it now.
1140     */
1141     if (ED_IsLVal (Expr) && IsTypePtr (Expr->Type)) {
1142
1143         /* Load into the primary */
1144         LoadExpr (CF_NONE, Expr);
1145
1146         /* Make it an lvalue expression */
1147         ED_MakeLValExpr (Expr);
1148     }
1149
1150     /* The type is the type of the field plus any qualifiers from the struct */
1151     if (IsClassStruct (Expr->Type)) {
1152         Q = GetQualifier (Expr->Type);
1153     } else {
1154         Q = GetQualifier (Indirect (Expr->Type));
1155     }
1156     if (GetQualifier (Field->Type) == (GetQualifier (Field->Type) | Q)) {
1157         FinalType = Field->Type;
1158     } else {
1159         FinalType = TypeDup (Field->Type);
1160         FinalType->C |= Q;
1161     }
1162
1163     /* A struct is usually an lvalue. If not, it is a struct in the primary
1164     ** register.
1165     */
1166     if (ED_IsRVal (Expr) && ED_IsLocExpr (Expr) && !IsTypePtr (Expr->Type)) {
1167
1168         unsigned Flags = 0;
1169         unsigned BitOffs;
1170
1171         /* Get the size of the type */
1172         unsigned Size = SizeOf (Expr->Type);
1173
1174         /* Safety check */
1175         CHECK (Field->V.Offs + Size <= SIZEOF_LONG);
1176
1177         /* The type of the operation depends on the type of the struct */
1178         switch (Size) {
1179             case 1:     Flags = CF_CHAR | CF_UNSIGNED | CF_CONST;       break;
1180             case 2:     Flags = CF_INT  | CF_UNSIGNED | CF_CONST;       break;
1181             case 3:     /* FALLTHROUGH */
1182             case 4:     Flags = CF_LONG | CF_UNSIGNED | CF_CONST;       break;
1183             default:    Internal ("Invalid struct size: %u", Size);     break;
1184         }
1185
1186         /* Generate a shift to get the field in the proper position in the
1187         ** primary. For bit fields, mask the value.
1188         */
1189         BitOffs = Field->V.Offs * CHAR_BITS;
1190         if (SymIsBitField (Field)) {
1191             BitOffs += Field->V.B.BitOffs;
1192             g_asr (Flags, BitOffs);
1193             /* Mask the value. This is unnecessary if the shift executed above
1194             ** moved only zeroes into the value.
1195             */
1196             if (BitOffs + Field->V.B.BitWidth != Size * CHAR_BITS) {
1197                 g_and (CF_INT | CF_UNSIGNED | CF_CONST,
1198                        (0x0001U << Field->V.B.BitWidth) - 1U);
1199             }
1200         } else {
1201             g_asr (Flags, BitOffs);
1202         }
1203
1204         /* Use the new type */
1205         Expr->Type = FinalType;
1206
1207     } else {
1208
1209         /* Set the struct field offset */
1210         Expr->IVal += Field->V.Offs;
1211
1212         /* Use the new type */
1213         Expr->Type = FinalType;
1214
1215         /* An struct member is actually a variable. So the rules for variables
1216         ** with respect to the reference type apply: If it's an array, it is
1217         ** a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1218         ** but a struct field cannot be a function).
1219         */
1220         if (IsTypeArray (Expr->Type)) {
1221             ED_MakeRVal (Expr);
1222         } else {
1223             ED_MakeLVal (Expr);
1224         }
1225
1226         /* Make the expression a bit field if necessary */
1227         if (SymIsBitField (Field)) {
1228             ED_MakeBitField (Expr, Field->V.B.BitOffs, Field->V.B.BitWidth);
1229         }
1230     }
1231
1232 }
1233
1234
1235
1236 static void hie11 (ExprDesc *Expr)
1237 /* Handle compound types (structs and arrays) */
1238 {
1239     /* Name value used in invalid function calls */
1240     static const char IllegalFunc[] = "illegal_function_call";
1241
1242     /* Evaluate the lhs */
1243     Primary (Expr);
1244
1245     /* Check for a rhs */
1246     while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN ||
1247            CurTok.Tok == TOK_DOT    || CurTok.Tok == TOK_PTR_REF) {
1248
1249         switch (CurTok.Tok) {
1250
1251             case TOK_LBRACK:
1252                 /* Array reference */
1253                 ArrayRef (Expr);
1254                 break;
1255
1256             case TOK_LPAREN:
1257                 /* Function call. */
1258                 if (!IsTypeFunc (Expr->Type) && !IsTypeFuncPtr (Expr->Type)) {
1259                     /* Not a function */
1260                     Error ("Illegal function call");
1261                     /* Force the type to be a implicitly defined function, one
1262                     ** returning an int and taking any number of arguments.
1263                     ** Since we don't have a name, invent one.
1264                     */
1265                     ED_MakeConstAbs (Expr, 0, GetImplicitFuncType ());
1266                     Expr->Name = (long) IllegalFunc;
1267                 }
1268                 /* Call the function */
1269                 FunctionCall (Expr);
1270                 break;
1271
1272             case TOK_DOT:
1273                 if (!IsClassStruct (Expr->Type)) {
1274                     Error ("Struct expected");
1275                 }
1276                 StructRef (Expr);
1277                 break;
1278
1279             case TOK_PTR_REF:
1280                 /* If we have an array, convert it to pointer to first element */
1281                 if (IsTypeArray (Expr->Type)) {
1282                     Expr->Type = ArrayToPtr (Expr->Type);
1283                 }
1284                 if (!IsClassPtr (Expr->Type) || !IsClassStruct (Indirect (Expr->Type))) {
1285                     Error ("Struct pointer expected");
1286                 }
1287                 StructRef (Expr);
1288                 break;
1289
1290             default:
1291                 Internal ("Invalid token in hie11: %d", CurTok.Tok);
1292
1293         }
1294     }
1295 }
1296
1297
1298
1299 void Store (ExprDesc* Expr, const Type* StoreType)
1300 /* Store the primary register into the location denoted by Expr. If StoreType
1301 ** is given, use this type when storing instead of Expr->Type. If StoreType
1302 ** is NULL, use Expr->Type instead.
1303 */
1304 {
1305     unsigned Flags;
1306
1307     /* If StoreType was not given, use Expr->Type instead */
1308     if (StoreType == 0) {
1309         StoreType = Expr->Type;
1310     }
1311
1312     /* Prepare the code generator flags */
1313     Flags = TypeOf (StoreType) | GlobalModeFlags (Expr);
1314
1315     /* Do the store depending on the location */
1316     switch (ED_GetLoc (Expr)) {
1317
1318         case E_LOC_ABS:
1319             /* Absolute: numeric address or const */
1320             g_putstatic (Flags, Expr->IVal, 0);
1321             break;
1322
1323         case E_LOC_GLOBAL:
1324             /* Global variable */
1325             g_putstatic (Flags, Expr->Name, Expr->IVal);
1326             break;
1327
1328         case E_LOC_STATIC:
1329         case E_LOC_LITERAL:
1330             /* Static variable or literal in the literal pool */
1331             g_putstatic (Flags, Expr->Name, Expr->IVal);
1332             break;
1333
1334         case E_LOC_REGISTER:
1335             /* Register variable */
1336             g_putstatic (Flags, Expr->Name, Expr->IVal);
1337             break;
1338
1339         case E_LOC_STACK:
1340             /* Value on the stack */
1341             g_putlocal (Flags, Expr->IVal, 0);
1342             break;
1343
1344         case E_LOC_PRIMARY:
1345             /* The primary register (value is already there) */
1346             break;
1347
1348         case E_LOC_EXPR:
1349             /* An expression in the primary register */
1350             g_putind (Flags, Expr->IVal);
1351             break;
1352
1353         default:
1354             Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
1355     }
1356
1357     /* Assume that each one of the stores will invalidate CC */
1358     ED_MarkAsUntested (Expr);
1359 }
1360
1361
1362
1363 static void PreInc (ExprDesc* Expr)
1364 /* Handle the preincrement operators */
1365 {
1366     unsigned Flags;
1367     unsigned long Val;
1368
1369     /* Skip the operator token */
1370     NextToken ();
1371
1372     /* Evaluate the expression and check that it is an lvalue */
1373     hie10 (Expr);
1374     if (!ED_IsLVal (Expr)) {
1375         Error ("Invalid lvalue");
1376         return;
1377     }
1378
1379     /* We cannot modify const values */
1380     if (IsQualConst (Expr->Type)) {
1381         Error ("Increment of read-only variable");
1382     }
1383
1384     /* Get the data type */
1385     Flags = TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR | CF_CONST;
1386
1387     /* Get the increment value in bytes */
1388     Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1389
1390     /* Check the location of the data */
1391     switch (ED_GetLoc (Expr)) {
1392
1393         case E_LOC_ABS:
1394             /* Absolute: numeric address or const */
1395             g_addeqstatic (Flags, Expr->IVal, 0, Val);
1396             break;
1397
1398         case E_LOC_GLOBAL:
1399             /* Global variable */
1400             g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1401             break;
1402
1403         case E_LOC_STATIC:
1404         case E_LOC_LITERAL:
1405             /* Static variable or literal in the literal pool */
1406             g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1407             break;
1408
1409         case E_LOC_REGISTER:
1410             /* Register variable */
1411             g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1412             break;
1413
1414         case E_LOC_STACK:
1415             /* Value on the stack */
1416             g_addeqlocal (Flags, Expr->IVal, Val);
1417             break;
1418
1419         case E_LOC_PRIMARY:
1420             /* The primary register */
1421             g_inc (Flags, Val);
1422             break;
1423
1424         case E_LOC_EXPR:
1425             /* An expression in the primary register */
1426             g_addeqind (Flags, Expr->IVal, Val);
1427             break;
1428
1429         default:
1430             Internal ("Invalid location in PreInc(): 0x%04X", ED_GetLoc (Expr));
1431     }
1432
1433     /* Result is an expression, no reference */
1434     ED_MakeRValExpr (Expr);
1435 }
1436
1437
1438
1439 static void PreDec (ExprDesc* Expr)
1440 /* Handle the predecrement operators */
1441 {
1442     unsigned Flags;
1443     unsigned long Val;
1444
1445     /* Skip the operator token */
1446     NextToken ();
1447
1448     /* Evaluate the expression and check that it is an lvalue */
1449     hie10 (Expr);
1450     if (!ED_IsLVal (Expr)) {
1451         Error ("Invalid lvalue");
1452         return;
1453     }
1454
1455     /* We cannot modify const values */
1456     if (IsQualConst (Expr->Type)) {
1457         Error ("Decrement of read-only variable");
1458     }
1459
1460     /* Get the data type */
1461     Flags = TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR | CF_CONST;
1462
1463     /* Get the increment value in bytes */
1464     Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1465
1466     /* Check the location of the data */
1467     switch (ED_GetLoc (Expr)) {
1468
1469         case E_LOC_ABS:
1470             /* Absolute: numeric address or const */
1471             g_subeqstatic (Flags, Expr->IVal, 0, Val);
1472             break;
1473
1474         case E_LOC_GLOBAL:
1475             /* Global variable */
1476             g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1477             break;
1478
1479         case E_LOC_STATIC:
1480         case E_LOC_LITERAL:
1481             /* Static variable or literal in the literal pool */
1482             g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1483             break;
1484
1485         case E_LOC_REGISTER:
1486             /* Register variable */
1487             g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1488             break;
1489
1490         case E_LOC_STACK:
1491             /* Value on the stack */
1492             g_subeqlocal (Flags, Expr->IVal, Val);
1493             break;
1494
1495         case E_LOC_PRIMARY:
1496             /* The primary register */
1497             g_inc (Flags, Val);
1498             break;
1499
1500         case E_LOC_EXPR:
1501             /* An expression in the primary register */
1502             g_subeqind (Flags, Expr->IVal, Val);
1503             break;
1504
1505         default:
1506             Internal ("Invalid location in PreDec(): 0x%04X", ED_GetLoc (Expr));
1507     }
1508
1509     /* Result is an expression, no reference */
1510     ED_MakeRValExpr (Expr);
1511 }
1512
1513
1514
1515 static void PostInc (ExprDesc* Expr)
1516 /* Handle the postincrement operator */
1517 {
1518     unsigned Flags;
1519
1520     NextToken ();
1521
1522     /* The expression to increment must be an lvalue */
1523     if (!ED_IsLVal (Expr)) {
1524         Error ("Invalid lvalue");
1525         return;
1526     }
1527
1528     /* We cannot modify const values */
1529     if (IsQualConst (Expr->Type)) {
1530         Error ("Increment of read-only variable");
1531     }
1532
1533     /* Get the data type */
1534     Flags = TypeOf (Expr->Type);
1535
1536     /* Push the address if needed */
1537     PushAddr (Expr);
1538
1539     /* Fetch the value and save it (since it's the result of the expression) */
1540     LoadExpr (CF_NONE, Expr);
1541     g_save (Flags | CF_FORCECHAR);
1542
1543     /* If we have a pointer expression, increment by the size of the type */
1544     if (IsTypePtr (Expr->Type)) {
1545         g_inc (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1546     } else {
1547         g_inc (Flags | CF_CONST | CF_FORCECHAR, 1);
1548     }
1549
1550     /* Store the result back */
1551     Store (Expr, 0);
1552
1553     /* Restore the original value in the primary register */
1554     g_restore (Flags | CF_FORCECHAR);
1555
1556     /* The result is always an expression, no reference */
1557     ED_MakeRValExpr (Expr);
1558 }
1559
1560
1561
1562 static void PostDec (ExprDesc* Expr)
1563 /* Handle the postdecrement operator */
1564 {
1565     unsigned Flags;
1566
1567     NextToken ();
1568
1569     /* The expression to increment must be an lvalue */
1570     if (!ED_IsLVal (Expr)) {
1571         Error ("Invalid lvalue");
1572         return;
1573     }
1574
1575     /* We cannot modify const values */
1576     if (IsQualConst (Expr->Type)) {
1577         Error ("Decrement of read-only variable");
1578     }
1579
1580     /* Get the data type */
1581     Flags = TypeOf (Expr->Type);
1582
1583     /* Push the address if needed */
1584     PushAddr (Expr);
1585
1586     /* Fetch the value and save it (since it's the result of the expression) */
1587     LoadExpr (CF_NONE, Expr);
1588     g_save (Flags | CF_FORCECHAR);
1589
1590     /* If we have a pointer expression, increment by the size of the type */
1591     if (IsTypePtr (Expr->Type)) {
1592         g_dec (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1593     } else {
1594         g_dec (Flags | CF_CONST | CF_FORCECHAR, 1);
1595     }
1596
1597     /* Store the result back */
1598     Store (Expr, 0);
1599
1600     /* Restore the original value in the primary register */
1601     g_restore (Flags | CF_FORCECHAR);
1602
1603     /* The result is always an expression, no reference */
1604     ED_MakeRValExpr (Expr);
1605 }
1606
1607
1608
1609 static void UnaryOp (ExprDesc* Expr)
1610 /* Handle unary -/+ and ~ */
1611 {
1612     unsigned Flags;
1613
1614     /* Remember the operator token and skip it */
1615     token_t Tok = CurTok.Tok;
1616     NextToken ();
1617
1618     /* Get the expression */
1619     hie10 (Expr);
1620
1621     /* We can only handle integer types */
1622     if (!IsClassInt (Expr->Type)) {
1623         Error ("Argument must have integer type");
1624         ED_MakeConstAbsInt (Expr, 1);
1625     }
1626
1627     /* Check for a constant expression */
1628     if (ED_IsConstAbs (Expr)) {
1629         /* Value is constant */
1630         switch (Tok) {
1631             case TOK_MINUS: Expr->IVal = -Expr->IVal;   break;
1632             case TOK_PLUS:                              break;
1633             case TOK_COMP:  Expr->IVal = ~Expr->IVal;   break;
1634             default:        Internal ("Unexpected token: %d", Tok);
1635         }
1636     } else {
1637         /* Value is not constant */
1638         LoadExpr (CF_NONE, Expr);
1639
1640         /* Get the type of the expression */
1641         Flags = TypeOf (Expr->Type);
1642
1643         /* Handle the operation */
1644         switch (Tok) {
1645             case TOK_MINUS: g_neg (Flags);  break;
1646             case TOK_PLUS:                  break;
1647             case TOK_COMP:  g_com (Flags);  break;
1648             default:        Internal ("Unexpected token: %d", Tok);
1649         }
1650
1651         /* The result is a rvalue in the primary */
1652         ED_MakeRValExpr (Expr);
1653     }
1654 }
1655
1656
1657
1658 void hie10 (ExprDesc* Expr)
1659 /* Handle ++, --, !, unary - etc. */
1660 {
1661     unsigned long Size;
1662
1663     switch (CurTok.Tok) {
1664
1665         case TOK_INC:
1666             PreInc (Expr);
1667             break;
1668
1669         case TOK_DEC:
1670             PreDec (Expr);
1671             break;
1672
1673         case TOK_PLUS:
1674         case TOK_MINUS:
1675         case TOK_COMP:
1676             UnaryOp (Expr);
1677             break;
1678
1679         case TOK_BOOL_NOT:
1680             NextToken ();
1681             if (evalexpr (CF_NONE, hie10, Expr) == 0) {
1682                 /* Constant expression */
1683                 Expr->IVal = !Expr->IVal;
1684             } else {
1685                 g_bneg (TypeOf (Expr->Type));
1686                 ED_MakeRValExpr (Expr);
1687                 ED_TestDone (Expr);             /* bneg will set cc */
1688             }
1689             break;
1690
1691         case TOK_STAR:
1692             NextToken ();
1693             ExprWithCheck (hie10, Expr);
1694             if (ED_IsLVal (Expr) || !(ED_IsLocConst (Expr) || ED_IsLocStack (Expr))) {
1695                 /* Not a const, load it into the primary and make it a
1696                 ** calculated value.
1697                 */
1698                 LoadExpr (CF_NONE, Expr);
1699                 ED_MakeRValExpr (Expr);
1700             }
1701             /* If the expression is already a pointer to function, the
1702             ** additional dereferencing operator must be ignored. A function
1703             ** itself is represented as "pointer to function", so any number
1704             ** of dereference operators is legal, since the result will
1705             ** always be converted to "pointer to function".
1706             */
1707             if (IsTypeFuncPtr (Expr->Type) || IsTypeFunc (Expr->Type)) {
1708                 /* Expression not storable */
1709                 ED_MakeRVal (Expr);
1710             } else {
1711                 if (IsClassPtr (Expr->Type)) {
1712                     Expr->Type = Indirect (Expr->Type);
1713                 } else {
1714                     Error ("Illegal indirection");
1715                 }
1716                 /* The * operator yields an lvalue */
1717                 ED_MakeLVal (Expr);
1718             }
1719             break;
1720
1721         case TOK_AND:
1722             NextToken ();
1723             ExprWithCheck (hie10, Expr);
1724             /* The & operator may be applied to any lvalue, and it may be
1725             ** applied to functions, even if they're no lvalues.
1726             */
1727             if (ED_IsRVal (Expr) && !IsTypeFunc (Expr->Type) && !IsTypeArray (Expr->Type)) {
1728                 Error ("Illegal address");
1729             } else {
1730                 if (ED_IsBitField (Expr)) {
1731                     Error ("Cannot take address of bit-field");
1732                     /* Do it anyway, just to avoid further warnings */
1733                     Expr->Flags &= ~E_BITFIELD;
1734                 }
1735                 Expr->Type = PointerTo (Expr->Type);
1736                 /* The & operator yields an rvalue */
1737                 ED_MakeRVal (Expr);
1738             }
1739             break;
1740
1741         case TOK_SIZEOF:
1742             NextToken ();
1743             if (TypeSpecAhead ()) {
1744                 Type T[MAXTYPELEN];
1745                 NextToken ();
1746                 Size = CheckedSizeOf (ParseType (T));
1747                 ConsumeRParen ();
1748             } else {
1749                 /* Remember the output queue pointer */
1750                 CodeMark Mark;
1751                 GetCodePos (&Mark);
1752                 hie10 (Expr);
1753                 /* If the expression is a literal string, release it, so it
1754                 ** won't be output as data if not used elsewhere.
1755                 */
1756                 if (ED_IsLocLiteral (Expr)) {
1757                     ReleaseLiteral (Expr->LVal);
1758                 }
1759                 /* Calculate the size */
1760                 Size = CheckedSizeOf (Expr->Type);
1761                 /* Remove any generated code */
1762                 RemoveCode (&Mark);
1763             }
1764             ED_MakeConstAbs (Expr, Size, type_size_t);
1765             ED_MarkAsUntested (Expr);
1766             break;
1767
1768         default:
1769             if (TypeSpecAhead ()) {
1770
1771                 /* A typecast */
1772                 TypeCast (Expr);
1773
1774             } else {
1775
1776                 /* An expression */
1777                 hie11 (Expr);
1778
1779                 /* Handle post increment */
1780                 switch (CurTok.Tok) {
1781                     case TOK_INC:   PostInc (Expr); break;
1782                     case TOK_DEC:   PostDec (Expr); break;
1783                     default:                        break;
1784                 }
1785
1786             }
1787             break;
1788     }
1789 }
1790
1791
1792
1793 static void hie_internal (const GenDesc* Ops,   /* List of generators */
1794                           ExprDesc* Expr,
1795                           void (*hienext) (ExprDesc*),
1796                           int* UsedGen)
1797 /* Helper function */
1798 {
1799     ExprDesc Expr2;
1800     CodeMark Mark1;
1801     CodeMark Mark2;
1802     const GenDesc* Gen;
1803     token_t Tok;                        /* The operator token */
1804     unsigned ltype, type;
1805     int lconst;                         /* Left operand is a constant */
1806     int rconst;                         /* Right operand is a constant */
1807
1808
1809     ExprWithCheck (hienext, Expr);
1810
1811     *UsedGen = 0;
1812     while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1813
1814         /* Tell the caller that we handled it's ops */
1815         *UsedGen = 1;
1816
1817         /* All operators that call this function expect an int on the lhs */
1818         if (!IsClassInt (Expr->Type)) {
1819             Error ("Integer expression expected");
1820             /* To avoid further errors, make Expr a valid int expression */
1821             ED_MakeConstAbsInt (Expr, 1);
1822         }
1823
1824         /* Remember the operator token, then skip it */
1825         Tok = CurTok.Tok;
1826         NextToken ();
1827
1828         /* Get the lhs on stack */
1829         GetCodePos (&Mark1);
1830         ltype = TypeOf (Expr->Type);
1831         lconst = ED_IsConstAbs (Expr);
1832         if (lconst) {
1833             /* Constant value */
1834             GetCodePos (&Mark2);
1835             /* If the operator is commutative, don't push the left side, if
1836             ** it's a constant, since we will exchange both operands.
1837             */
1838             if ((Gen->Flags & GEN_COMM) == 0) {
1839                 g_push (ltype | CF_CONST, Expr->IVal);
1840             }
1841         } else {
1842             /* Value not constant */
1843             LoadExpr (CF_NONE, Expr);
1844             GetCodePos (&Mark2);
1845             g_push (ltype, 0);
1846         }
1847
1848         /* Get the right hand side */
1849         MarkedExprWithCheck (hienext, &Expr2);
1850
1851         /* Check for a constant expression */
1852         rconst = (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2));
1853         if (!rconst) {
1854             /* Not constant, load into the primary */
1855             LoadExpr (CF_NONE, &Expr2);
1856         }
1857
1858         /* Check the type of the rhs */
1859         if (!IsClassInt (Expr2.Type)) {
1860             Error ("Integer expression expected");
1861         }
1862
1863         /* Check for const operands */
1864         if (lconst && rconst) {
1865
1866             /* Both operands are constant, remove the generated code */
1867             RemoveCode (&Mark1);
1868
1869             /* Get the type of the result */
1870             Expr->Type = promoteint (Expr->Type, Expr2.Type);
1871
1872             /* Handle the op differently for signed and unsigned types */
1873             if (IsSignSigned (Expr->Type)) {
1874
1875                 /* Evaluate the result for signed operands */
1876                 signed long Val1 = Expr->IVal;
1877                 signed long Val2 = Expr2.IVal;
1878                 switch (Tok) {
1879                     case TOK_OR:
1880                         Expr->IVal = (Val1 | Val2);
1881                         break;
1882                     case TOK_XOR:
1883                         Expr->IVal = (Val1 ^ Val2);
1884                         break;
1885                     case TOK_AND:
1886                         Expr->IVal = (Val1 & Val2);
1887                         break;
1888                     case TOK_STAR:
1889                         Expr->IVal = (Val1 * Val2);
1890                         break;
1891                     case TOK_DIV:
1892                         if (Val2 == 0) {
1893                             Error ("Division by zero");
1894                             Expr->IVal = 0x7FFFFFFF;
1895                         } else {
1896                             Expr->IVal = (Val1 / Val2);
1897                         }
1898                         break;
1899                     case TOK_MOD:
1900                         if (Val2 == 0) {
1901                             Error ("Modulo operation with zero");
1902                             Expr->IVal = 0;
1903                         } else {
1904                             Expr->IVal = (Val1 % Val2);
1905                         }
1906                         break;
1907                     default:
1908                         Internal ("hie_internal: got token 0x%X\n", Tok);
1909                 }
1910             } else {
1911
1912                 /* Evaluate the result for unsigned operands */
1913                 unsigned long Val1 = Expr->IVal;
1914                 unsigned long Val2 = Expr2.IVal;
1915                 switch (Tok) {
1916                     case TOK_OR:
1917                         Expr->IVal = (Val1 | Val2);
1918                         break;
1919                     case TOK_XOR:
1920                         Expr->IVal = (Val1 ^ Val2);
1921                         break;
1922                     case TOK_AND:
1923                         Expr->IVal = (Val1 & Val2);
1924                         break;
1925                     case TOK_STAR:
1926                         Expr->IVal = (Val1 * Val2);
1927                         break;
1928                     case TOK_DIV:
1929                         if (Val2 == 0) {
1930                             Error ("Division by zero");
1931                             Expr->IVal = 0xFFFFFFFF;
1932                         } else {
1933                             Expr->IVal = (Val1 / Val2);
1934                         }
1935                         break;
1936                     case TOK_MOD:
1937                         if (Val2 == 0) {
1938                             Error ("Modulo operation with zero");
1939                             Expr->IVal = 0;
1940                         } else {
1941                             Expr->IVal = (Val1 % Val2);
1942                         }
1943                         break;
1944                     default:
1945                         Internal ("hie_internal: got token 0x%X\n", Tok);
1946                 }
1947             }
1948
1949         } else if (lconst && (Gen->Flags & GEN_COMM) && !rconst) {
1950
1951             /* The left side is constant, the right side is not, and the
1952             ** operator allows swapping the operands. We haven't pushed the
1953             ** left side onto the stack in this case, and will reverse the
1954             ** operation because this allows for better code.
1955             */
1956             unsigned rtype = ltype | CF_CONST;
1957             ltype = TypeOf (Expr2.Type);       /* Expr2 is now left */
1958             type = CF_CONST;
1959             if ((Gen->Flags & GEN_NOPUSH) == 0) {
1960                 g_push (ltype, 0);
1961             } else {
1962                 ltype |= CF_REG;        /* Value is in register */
1963             }
1964
1965             /* Determine the type of the operation result. */
1966             type |= g_typeadjust (ltype, rtype);
1967             Expr->Type = promoteint (Expr->Type, Expr2.Type);
1968
1969             /* Generate code */
1970             Gen->Func (type, Expr->IVal);
1971
1972             /* We have a rvalue in the primary now */
1973             ED_MakeRValExpr (Expr);
1974
1975         } else {
1976
1977             /* If the right hand side is constant, and the generator function
1978             ** expects the lhs in the primary, remove the push of the primary
1979             ** now.
1980             */
1981             unsigned rtype = TypeOf (Expr2.Type);
1982             type = 0;
1983             if (rconst) {
1984                 /* Second value is constant - check for div */
1985                 type |= CF_CONST;
1986                 rtype |= CF_CONST;
1987                 if (Tok == TOK_DIV && Expr2.IVal == 0) {
1988                     Error ("Division by zero");
1989                 } else if (Tok == TOK_MOD && Expr2.IVal == 0) {
1990                     Error ("Modulo operation with zero");
1991                 }
1992                 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1993                     RemoveCode (&Mark2);
1994                     ltype |= CF_REG;    /* Value is in register */
1995                 }
1996             }
1997
1998             /* Determine the type of the operation result. */
1999             type |= g_typeadjust (ltype, rtype);
2000             Expr->Type = promoteint (Expr->Type, Expr2.Type);
2001
2002             /* Generate code */
2003             Gen->Func (type, Expr2.IVal);
2004
2005             /* We have a rvalue in the primary now */
2006             ED_MakeRValExpr (Expr);
2007         }
2008     }
2009 }
2010
2011
2012
2013 static void hie_compare (const GenDesc* Ops,    /* List of generators */
2014                          ExprDesc* Expr,
2015                          void (*hienext) (ExprDesc*))
2016 /* Helper function for the compare operators */
2017 {
2018     ExprDesc Expr2;
2019     CodeMark Mark0;
2020     CodeMark Mark1;
2021     CodeMark Mark2;
2022     const GenDesc* Gen;
2023     token_t Tok;                        /* The operator token */
2024     unsigned ltype;
2025     int rconst;                         /* Operand is a constant */
2026
2027
2028     GetCodePos (&Mark0);
2029     ExprWithCheck (hienext, Expr);
2030
2031     while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
2032
2033         /* Remember the generator function */
2034         void (*GenFunc) (unsigned, unsigned long) = Gen->Func;
2035
2036         /* Remember the operator token, then skip it */
2037         Tok = CurTok.Tok;
2038         NextToken ();
2039
2040         /* Get the lhs on stack */
2041         GetCodePos (&Mark1);
2042         ltype = TypeOf (Expr->Type);
2043         if (ED_IsConstAbs (Expr)) {
2044             /* Constant value */
2045             GetCodePos (&Mark2);
2046             g_push (ltype | CF_CONST, Expr->IVal);
2047         } else {
2048             /* Value not constant */
2049             LoadExpr (CF_NONE, Expr);
2050             GetCodePos (&Mark2);
2051             g_push (ltype, 0);
2052         }
2053
2054         /* Get the right hand side */
2055         MarkedExprWithCheck (hienext, &Expr2);
2056
2057         /* Check for a constant expression */
2058         rconst = (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2));
2059         if (!rconst) {
2060             /* Not constant, load into the primary */
2061             LoadExpr (CF_NONE, &Expr2);
2062         }
2063
2064         /* Make sure, the types are compatible */
2065         if (IsClassInt (Expr->Type)) {
2066             if (!IsClassInt (Expr2.Type) && !(IsClassPtr(Expr2.Type) && ED_IsNullPtr(Expr))) {
2067                 Error ("Incompatible types");
2068             }
2069         } else if (IsClassPtr (Expr->Type)) {
2070             if (IsClassPtr (Expr2.Type)) {
2071                 /* Both pointers are allowed in comparison if they point to
2072                 ** the same type, or if one of them is a void pointer.
2073                 */
2074                 Type* left  = Indirect (Expr->Type);
2075                 Type* right = Indirect (Expr2.Type);
2076                 if (TypeCmp (left, right) < TC_EQUAL && left->C != T_VOID && right->C != T_VOID) {
2077                     /* Incomatible pointers */
2078                     Error ("Incompatible types");
2079                 }
2080             } else if (!ED_IsNullPtr (&Expr2)) {
2081                 Error ("Incompatible types");
2082             }
2083         }
2084
2085         /* Check for const operands */
2086         if (ED_IsConstAbs (Expr) && rconst) {
2087
2088             /* If the result is constant, this is suspicious when not in
2089             ** preprocessor mode.
2090             */
2091             WarnConstCompareResult ();
2092
2093             /* Both operands are constant, remove the generated code */
2094             RemoveCode (&Mark1);
2095
2096             /* Determine if this is a signed or unsigned compare */
2097             if (IsClassInt (Expr->Type) && IsSignSigned (Expr->Type) &&
2098                 IsClassInt (Expr2.Type) && IsSignSigned (Expr2.Type)) {
2099
2100                 /* Evaluate the result for signed operands */
2101                 signed long Val1 = Expr->IVal;
2102                 signed long Val2 = Expr2.IVal;
2103                 switch (Tok) {
2104                     case TOK_EQ: Expr->IVal = (Val1 == Val2);   break;
2105                     case TOK_NE: Expr->IVal = (Val1 != Val2);   break;
2106                     case TOK_LT: Expr->IVal = (Val1 < Val2);    break;
2107                     case TOK_LE: Expr->IVal = (Val1 <= Val2);   break;
2108                     case TOK_GE: Expr->IVal = (Val1 >= Val2);   break;
2109                     case TOK_GT: Expr->IVal = (Val1 > Val2);    break;
2110                     default:     Internal ("hie_compare: got token 0x%X\n", Tok);
2111                 }
2112
2113             } else {
2114
2115                 /* Evaluate the result for unsigned operands */
2116                 unsigned long Val1 = Expr->IVal;
2117                 unsigned long Val2 = Expr2.IVal;
2118                 switch (Tok) {
2119                     case TOK_EQ: Expr->IVal = (Val1 == Val2);   break;
2120                     case TOK_NE: Expr->IVal = (Val1 != Val2);   break;
2121                     case TOK_LT: Expr->IVal = (Val1 < Val2);    break;
2122                     case TOK_LE: Expr->IVal = (Val1 <= Val2);   break;
2123                     case TOK_GE: Expr->IVal = (Val1 >= Val2);   break;
2124                     case TOK_GT: Expr->IVal = (Val1 > Val2);    break;
2125                     default:     Internal ("hie_compare: got token 0x%X\n", Tok);
2126                 }
2127             }
2128
2129         } else {
2130
2131             /* Determine the signedness of the operands */
2132             int LeftSigned  = IsSignSigned (Expr->Type);
2133             int RightSigned = IsSignSigned (Expr2.Type);
2134
2135             /* If the right hand side is constant, and the generator function
2136             ** expects the lhs in the primary, remove the push of the primary
2137             ** now.
2138             */
2139             unsigned flags = 0;
2140             if (rconst) {
2141                 flags |= CF_CONST;
2142                 if ((Gen->Flags & GEN_NOPUSH) != 0) {
2143                     RemoveCode (&Mark2);
2144                     ltype |= CF_REG;    /* Value is in register */
2145                 }
2146             }
2147
2148             /* Determine the type of the operation. */
2149             if (IsTypeChar (Expr->Type) && rconst) {
2150
2151                 /* Left side is unsigned char, right side is constant.
2152                 ** Determine the minimum and maximum values
2153                 */
2154                 int LeftMin, LeftMax;
2155                 if (LeftSigned) {
2156                     LeftMin = -128;
2157                     LeftMax = 127;
2158                 } else {
2159                     LeftMin = 0;
2160                     LeftMax = 255;
2161                 }
2162                 /* An integer value is always represented as a signed in the
2163                 ** ExprDesc structure. This may lead to false results below,
2164                 ** if it is actually unsigned, but interpreted as signed
2165                 ** because of the representation. Fortunately, in this case,
2166                 ** the actual value doesn't matter, since it's always greater
2167                 ** than what can be represented in a char. So correct the
2168                 ** value accordingly.
2169                 */
2170                 if (!RightSigned && Expr2.IVal < 0) {
2171                     /* Correct the value so it is an unsigned. It will then
2172                     ** anyway match one of the cases below.
2173                     */
2174                     Expr2.IVal = LeftMax + 1;
2175                 }
2176
2177                 /* Comparing a char against a constant may have a constant
2178                 ** result. Please note: It is not possible to remove the code
2179                 ** for the compare alltogether, because it may have side
2180                 ** effects.
2181                 */
2182                 switch (Tok) {
2183
2184                     case TOK_EQ:
2185                         if (Expr2.IVal < LeftMin || Expr2.IVal > LeftMax) {
2186                             ED_MakeConstAbsInt (Expr, 0);
2187                             WarnConstCompareResult ();
2188                             goto Done;
2189                         }
2190                         break;
2191
2192                     case TOK_NE:
2193                         if (Expr2.IVal < LeftMin || Expr2.IVal > LeftMax) {
2194                             ED_MakeConstAbsInt (Expr, 1);
2195                             WarnConstCompareResult ();
2196                             goto Done;
2197                         }
2198                         break;
2199
2200                     case TOK_LT:
2201                         if (Expr2.IVal <= LeftMin || Expr2.IVal > LeftMax) {
2202                             ED_MakeConstAbsInt (Expr, Expr2.IVal > LeftMax);
2203                             WarnConstCompareResult ();
2204                             goto Done;
2205                         }
2206                         break;
2207
2208                     case TOK_LE:
2209                         if (Expr2.IVal < LeftMin || Expr2.IVal >= LeftMax) {
2210                             ED_MakeConstAbsInt (Expr, Expr2.IVal >= LeftMax);
2211                             WarnConstCompareResult ();
2212                             goto Done;
2213                         }
2214                         break;
2215
2216                     case TOK_GE:
2217                         if (Expr2.IVal <= LeftMin || Expr2.IVal > LeftMax) {
2218                             ED_MakeConstAbsInt (Expr, Expr2.IVal <= LeftMin);
2219                             WarnConstCompareResult ();
2220                             goto Done;
2221                         }
2222                         break;
2223
2224                     case TOK_GT:
2225                         if (Expr2.IVal < LeftMin || Expr2.IVal >= LeftMax) {
2226                             ED_MakeConstAbsInt (Expr, Expr2.IVal < LeftMin);
2227                             WarnConstCompareResult ();
2228                             goto Done;
2229                         }
2230                         break;
2231
2232                     default:
2233                         Internal ("hie_compare: got token 0x%X\n", Tok);
2234                 }
2235
2236                 /* If the result is not already constant (as evaluated in the
2237                 ** switch above), we can execute the operation as a char op,
2238                 ** since the right side constant is in a valid range.
2239                 */
2240                 flags |= (CF_CHAR | CF_FORCECHAR);
2241                 if (!LeftSigned) {
2242                     flags |= CF_UNSIGNED;
2243                 }
2244
2245             } else if (IsTypeChar (Expr->Type) && IsTypeChar (Expr2.Type) &&
2246                 GetSignedness (Expr->Type) == GetSignedness (Expr2.Type)) {
2247
2248                 /* Both are chars with the same signedness. We can encode the
2249                 ** operation as a char operation.
2250                 */
2251                 flags |= CF_CHAR;
2252                 if (rconst) {
2253                     flags |= CF_FORCECHAR;
2254                 }
2255                 if (!LeftSigned) {
2256                     flags |= CF_UNSIGNED;
2257                 }
2258             } else {
2259                 unsigned rtype = TypeOf (Expr2.Type) | (flags & CF_CONST);
2260                 flags |= g_typeadjust (ltype, rtype);
2261             }
2262
2263             /* If the left side is an unsigned and the right is a constant,
2264             ** we may be able to change the compares to something more
2265             ** effective.
2266             */
2267             if (!LeftSigned && rconst) {
2268
2269                 switch (Tok) {
2270
2271                     case TOK_LT:
2272                         if (Expr2.IVal == 1) {
2273                             /* An unsigned compare to one means that the value
2274                             ** must be zero.
2275                             */
2276                             GenFunc = g_eq;
2277                             Expr2.IVal = 0;
2278                         }
2279                         break;
2280
2281                     case TOK_LE:
2282                         if (Expr2.IVal == 0) {
2283                             /* An unsigned compare to zero means that the value
2284                             ** must be zero.
2285                             */
2286                             GenFunc = g_eq;
2287                         }
2288                         break;
2289
2290                     case TOK_GE:
2291                         if (Expr2.IVal == 1) {
2292                             /* An unsigned compare to one means that the value
2293                             ** must not be zero.
2294                             */
2295                             GenFunc = g_ne;
2296                             Expr2.IVal = 0;
2297                         }
2298                         break;
2299
2300                     case TOK_GT:
2301                         if (Expr2.IVal == 0) {
2302                             /* An unsigned compare to zero means that the value
2303                             ** must not be zero.
2304                             */
2305                             GenFunc = g_ne;
2306                         }
2307                         break;
2308
2309                     default:
2310                         break;
2311
2312                 }
2313
2314             }
2315
2316             /* Generate code */
2317             GenFunc (flags, Expr2.IVal);
2318
2319             /* The result is an rvalue in the primary */
2320             ED_MakeRValExpr (Expr);
2321         }
2322
2323         /* Result type is always int */
2324         Expr->Type = type_int;
2325
2326 Done:   /* Condition codes are set */
2327         ED_TestDone (Expr);
2328     }
2329 }
2330
2331
2332
2333 static void hie9 (ExprDesc *Expr)
2334 /* Process * and / operators. */
2335 {
2336     static const GenDesc hie9_ops[] = {
2337         { TOK_STAR,     GEN_NOPUSH | GEN_COMM,  g_mul   },
2338         { TOK_DIV,      GEN_NOPUSH,             g_div   },
2339         { TOK_MOD,      GEN_NOPUSH,             g_mod   },
2340         { TOK_INVALID,  0,                      0       }
2341     };
2342     int UsedGen;
2343
2344     hie_internal (hie9_ops, Expr, hie10, &UsedGen);
2345 }
2346
2347
2348
2349 static void parseadd (ExprDesc* Expr)
2350 /* Parse an expression with the binary plus operator. Expr contains the
2351 ** unprocessed left hand side of the expression and will contain the
2352 ** result of the expression on return.
2353 */
2354 {
2355     ExprDesc Expr2;
2356     unsigned flags;             /* Operation flags */
2357     CodeMark Mark;              /* Remember code position */
2358     Type* lhst;                 /* Type of left hand side */
2359     Type* rhst;                 /* Type of right hand side */
2360
2361
2362     /* Skip the PLUS token */
2363     NextToken ();
2364
2365     /* Get the left hand side type, initialize operation flags */
2366     lhst = Expr->Type;
2367     flags = 0;
2368
2369     /* Check for constness on both sides */
2370     if (ED_IsConst (Expr)) {
2371
2372         /* The left hand side is a constant of some sort. Good. Get rhs */
2373         ExprWithCheck (hie9, &Expr2);
2374         if (ED_IsConstAbs (&Expr2)) {
2375
2376             /* Right hand side is a constant numeric value. Get the rhs type */
2377             rhst = Expr2.Type;
2378
2379             /* Both expressions are constants. Check for pointer arithmetic */
2380             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2381                 /* Left is pointer, right is int, must scale rhs */
2382                 Expr->IVal += Expr2.IVal * CheckedPSizeOf (lhst);
2383                 /* Result type is a pointer */
2384             } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2385                 /* Left is int, right is pointer, must scale lhs */
2386                 Expr->IVal = Expr->IVal * CheckedPSizeOf (rhst) + Expr2.IVal;
2387                 /* Result type is a pointer */
2388                 Expr->Type = Expr2.Type;
2389             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2390                 /* Integer addition */
2391                 Expr->IVal += Expr2.IVal;
2392                 typeadjust (Expr, &Expr2, 1);
2393             } else {
2394                 /* OOPS */
2395                 Error ("Invalid operands for binary operator `+'");
2396             }
2397
2398         } else {
2399
2400             /* lhs is a constant and rhs is not constant. Load rhs into
2401             ** the primary.
2402             */
2403             LoadExpr (CF_NONE, &Expr2);
2404
2405             /* Beware: The check above (for lhs) lets not only pass numeric
2406             ** constants, but also constant addresses (labels), maybe even
2407             ** with an offset. We have to check for that here.
2408             */
2409
2410             /* First, get the rhs type. */
2411             rhst = Expr2.Type;
2412
2413             /* Setup flags */
2414             if (ED_IsLocAbs (Expr)) {
2415                 /* A numerical constant */
2416                 flags |= CF_CONST;
2417             } else {
2418                 /* Constant address label */
2419                 flags |= GlobalModeFlags (Expr) | CF_CONSTADDR;
2420             }
2421
2422             /* Check for pointer arithmetic */
2423             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2424                 /* Left is pointer, right is int, must scale rhs */
2425                 g_scale (CF_INT, CheckedPSizeOf (lhst));
2426                 /* Operate on pointers, result type is a pointer */
2427                 flags |= CF_PTR;
2428                 /* Generate the code for the add */
2429                 if (ED_GetLoc (Expr) == E_LOC_ABS) {
2430                     /* Numeric constant */
2431                     g_inc (flags, Expr->IVal);
2432                 } else {
2433                     /* Constant address */
2434                     g_addaddr_static (flags, Expr->Name, Expr->IVal);
2435                 }
2436             } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2437
2438                 /* Left is int, right is pointer, must scale lhs. */
2439                 unsigned ScaleFactor = CheckedPSizeOf (rhst);
2440
2441                 /* Operate on pointers, result type is a pointer */
2442                 flags |= CF_PTR;
2443                 Expr->Type = Expr2.Type;
2444
2445                 /* Since we do already have rhs in the primary, if lhs is
2446                 ** not a numeric constant, and the scale factor is not one
2447                 ** (no scaling), we must take the long way over the stack.
2448                 */
2449                 if (ED_IsLocAbs (Expr)) {
2450                     /* Numeric constant, scale lhs */
2451                     Expr->IVal *= ScaleFactor;
2452                     /* Generate the code for the add */
2453                     g_inc (flags, Expr->IVal);
2454                 } else if (ScaleFactor == 1) {
2455                     /* Constant address but no need to scale */
2456                     g_addaddr_static (flags, Expr->Name, Expr->IVal);
2457                 } else {
2458                     /* Constant address that must be scaled */
2459                     g_push (TypeOf (Expr2.Type), 0);    /* rhs --> stack */
2460                     g_getimmed (flags, Expr->Name, Expr->IVal);
2461                     g_scale (CF_PTR, ScaleFactor);
2462                     g_add (CF_PTR, 0);
2463                 }
2464             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2465                 /* Integer addition */
2466                 flags |= typeadjust (Expr, &Expr2, 1);
2467                 /* Generate the code for the add */
2468                 if (ED_IsLocAbs (Expr)) {
2469                     /* Numeric constant */
2470                     g_inc (flags, Expr->IVal);
2471                 } else {
2472                     /* Constant address */
2473                     g_addaddr_static (flags, Expr->Name, Expr->IVal);
2474                 }
2475             } else {
2476                 /* OOPS */
2477                 Error ("Invalid operands for binary operator `+'");
2478                 flags = CF_INT;
2479             }
2480
2481             /* Result is a rvalue in primary register */
2482             ED_MakeRValExpr (Expr);
2483         }
2484
2485     } else {
2486
2487         /* Left hand side is not constant. Get the value onto the stack. */
2488         LoadExpr (CF_NONE, Expr);              /* --> primary register */
2489         GetCodePos (&Mark);
2490         g_push (TypeOf (Expr->Type), 0);        /* --> stack */
2491
2492         /* Evaluate the rhs */
2493         MarkedExprWithCheck (hie9, &Expr2);
2494
2495         /* Check for a constant rhs expression */
2496         if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
2497
2498             /* Right hand side is a constant. Get the rhs type */
2499             rhst = Expr2.Type;
2500
2501             /* Remove pushed value from stack */
2502             RemoveCode (&Mark);
2503
2504             /* Check for pointer arithmetic */
2505             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2506                 /* Left is pointer, right is int, must scale rhs */
2507                 Expr2.IVal *= CheckedPSizeOf (lhst);
2508                 /* Operate on pointers, result type is a pointer */
2509                 flags = CF_PTR;
2510             } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2511                 /* Left is int, right is pointer, must scale lhs (ptr only) */
2512                 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2513                 /* Operate on pointers, result type is a pointer */
2514                 flags = CF_PTR;
2515                 Expr->Type = Expr2.Type;
2516             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2517                 /* Integer addition */
2518                 flags = typeadjust (Expr, &Expr2, 1);
2519             } else {
2520                 /* OOPS */
2521                 Error ("Invalid operands for binary operator `+'");
2522                 flags = CF_INT;
2523             }
2524
2525             /* Generate code for the add */
2526             g_inc (flags | CF_CONST, Expr2.IVal);
2527
2528         } else {
2529
2530             /* Not constant, load into the primary */
2531             LoadExpr (CF_NONE, &Expr2);
2532
2533             /* lhs and rhs are not constant. Get the rhs type. */
2534             rhst = Expr2.Type;
2535
2536             /* Check for pointer arithmetic */
2537             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2538                 /* Left is pointer, right is int, must scale rhs */
2539                 g_scale (CF_INT, CheckedPSizeOf (lhst));
2540                 /* Operate on pointers, result type is a pointer */
2541                 flags = CF_PTR;
2542             } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2543                 /* Left is int, right is pointer, must scale lhs */
2544                 g_tosint (TypeOf (rhst));       /* Make sure, TOS is int */
2545                 g_swap (CF_INT);                /* Swap TOS and primary */
2546                 g_scale (CF_INT, CheckedPSizeOf (rhst));
2547                 /* Operate on pointers, result type is a pointer */
2548                 flags = CF_PTR;
2549                 Expr->Type = Expr2.Type;
2550             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2551                 /* Integer addition. Note: Result is never constant.
2552                 ** Problem here is that typeadjust does not know if the
2553                 ** variable is an rvalue or lvalue, so if both operands
2554                 ** are dereferenced constant numeric addresses, typeadjust
2555                 ** thinks the operation works on constants. Removing
2556                 ** CF_CONST here means handling the symptoms, however, the
2557                 ** whole parser is such a mess that I fear to break anything
2558                 ** when trying to apply another solution.
2559                 */
2560                 flags = typeadjust (Expr, &Expr2, 0) & ~CF_CONST;
2561             } else {
2562                 /* OOPS */
2563                 Error ("Invalid operands for binary operator `+'");
2564                 flags = CF_INT;
2565             }
2566
2567             /* Generate code for the add */
2568             g_add (flags, 0);
2569
2570         }
2571
2572         /* Result is a rvalue in primary register */
2573         ED_MakeRValExpr (Expr);
2574     }
2575
2576     /* Condition codes not set */
2577     ED_MarkAsUntested (Expr);
2578
2579 }
2580
2581
2582
2583 static void parsesub (ExprDesc* Expr)
2584 /* Parse an expression with the binary minus operator. Expr contains the
2585 ** unprocessed left hand side of the expression and will contain the
2586 ** result of the expression on return.
2587 */
2588 {
2589     ExprDesc Expr2;
2590     unsigned flags;             /* Operation flags */
2591     Type* lhst;                 /* Type of left hand side */
2592     Type* rhst;                 /* Type of right hand side */
2593     CodeMark Mark1;             /* Save position of output queue */
2594     CodeMark Mark2;             /* Another position in the queue */
2595     int rscale;                 /* Scale factor for the result */
2596
2597
2598     /* Skip the MINUS token */
2599     NextToken ();
2600
2601     /* Get the left hand side type, initialize operation flags */
2602     lhst = Expr->Type;
2603     rscale = 1;                 /* Scale by 1, that is, don't scale */
2604
2605     /* Remember the output queue position, then bring the value onto the stack */
2606     GetCodePos (&Mark1);
2607     LoadExpr (CF_NONE, Expr);  /* --> primary register */
2608     GetCodePos (&Mark2);
2609     g_push (TypeOf (lhst), 0);  /* --> stack */
2610
2611     /* Parse the right hand side */
2612     MarkedExprWithCheck (hie9, &Expr2);
2613
2614     /* Check for a constant rhs expression */
2615     if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
2616
2617         /* The right hand side is constant. Get the rhs type. */
2618         rhst = Expr2.Type;
2619
2620         /* Check left hand side */
2621         if (ED_IsConstAbs (Expr)) {
2622
2623             /* Both sides are constant, remove generated code */
2624             RemoveCode (&Mark1);
2625
2626             /* Check for pointer arithmetic */
2627             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2628                 /* Left is pointer, right is int, must scale rhs */
2629                 Expr->IVal -= Expr2.IVal * CheckedPSizeOf (lhst);
2630                 /* Operate on pointers, result type is a pointer */
2631             } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2632                 /* Left is pointer, right is pointer, must scale result */
2633                 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2634                     Error ("Incompatible pointer types");
2635                 } else {
2636                     Expr->IVal = (Expr->IVal - Expr2.IVal) /
2637                                       CheckedPSizeOf (lhst);
2638                 }
2639                 /* Operate on pointers, result type is an integer */
2640                 Expr->Type = type_int;
2641             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2642                 /* Integer subtraction */
2643                 typeadjust (Expr, &Expr2, 1);
2644                 Expr->IVal -= Expr2.IVal;
2645             } else {
2646                 /* OOPS */
2647                 Error ("Invalid operands for binary operator `-'");
2648             }
2649
2650             /* Result is constant, condition codes not set */
2651             ED_MarkAsUntested (Expr);
2652
2653         } else {
2654
2655             /* Left hand side is not constant, right hand side is.
2656             ** Remove pushed value from stack.
2657             */
2658             RemoveCode (&Mark2);
2659
2660             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2661                 /* Left is pointer, right is int, must scale rhs */
2662                 Expr2.IVal *= CheckedPSizeOf (lhst);
2663                 /* Operate on pointers, result type is a pointer */
2664                 flags = CF_PTR;
2665             } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2666                 /* Left is pointer, right is pointer, must scale result */
2667                 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2668                     Error ("Incompatible pointer types");
2669                 } else {
2670                     rscale = CheckedPSizeOf (lhst);
2671                 }
2672                 /* Operate on pointers, result type is an integer */
2673                 flags = CF_PTR;
2674                 Expr->Type = type_int;
2675             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2676                 /* Integer subtraction */
2677                 flags = typeadjust (Expr, &Expr2, 1);
2678             } else {
2679                 /* OOPS */
2680                 Error ("Invalid operands for binary operator `-'");
2681                 flags = CF_INT;
2682             }
2683
2684             /* Do the subtraction */
2685             g_dec (flags | CF_CONST, Expr2.IVal);
2686
2687             /* If this was a pointer subtraction, we must scale the result */
2688             if (rscale != 1) {
2689                 g_scale (flags, -rscale);
2690             }
2691
2692             /* Result is a rvalue in the primary register */
2693             ED_MakeRValExpr (Expr);
2694             ED_MarkAsUntested (Expr);
2695
2696         }
2697
2698     } else {
2699
2700         /* Not constant, load into the primary */
2701         LoadExpr (CF_NONE, &Expr2);
2702
2703         /* Right hand side is not constant. Get the rhs type. */
2704         rhst = Expr2.Type;
2705
2706         /* Check for pointer arithmetic */
2707         if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2708             /* Left is pointer, right is int, must scale rhs */
2709             g_scale (CF_INT, CheckedPSizeOf (lhst));
2710             /* Operate on pointers, result type is a pointer */
2711             flags = CF_PTR;
2712         } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2713             /* Left is pointer, right is pointer, must scale result */
2714             if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2715                 Error ("Incompatible pointer types");
2716             } else {
2717                 rscale = CheckedPSizeOf (lhst);
2718             }
2719             /* Operate on pointers, result type is an integer */
2720             flags = CF_PTR;
2721             Expr->Type = type_int;
2722         } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2723             /* Integer subtraction. If the left hand side descriptor says that
2724             ** the lhs is const, we have to remove this mark, since this is no
2725             ** longer true, lhs is on stack instead.
2726             */
2727             if (ED_IsLocAbs (Expr)) {
2728                 ED_MakeRValExpr (Expr);
2729             }
2730             /* Adjust operand types */
2731             flags = typeadjust (Expr, &Expr2, 0);
2732         } else {
2733             /* OOPS */
2734             Error ("Invalid operands for binary operator `-'");
2735             flags = CF_INT;
2736         }
2737
2738         /* Generate code for the sub (the & is a hack here) */
2739         g_sub (flags & ~CF_CONST, 0);
2740
2741         /* If this was a pointer subtraction, we must scale the result */
2742         if (rscale != 1) {
2743             g_scale (flags, -rscale);
2744         }
2745
2746         /* Result is a rvalue in the primary register */
2747         ED_MakeRValExpr (Expr);
2748         ED_MarkAsUntested (Expr);
2749     }
2750 }
2751
2752
2753
2754 void hie8 (ExprDesc* Expr)
2755 /* Process + and - binary operators. */
2756 {
2757     ExprWithCheck (hie9, Expr);
2758     while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2759         if (CurTok.Tok == TOK_PLUS) {
2760             parseadd (Expr);
2761         } else {
2762             parsesub (Expr);
2763         }
2764     }
2765 }
2766
2767
2768
2769 static void hie6 (ExprDesc* Expr)
2770 /* Handle greater-than type comparators */
2771 {
2772     static const GenDesc hie6_ops [] = {
2773         { TOK_LT,       GEN_NOPUSH,     g_lt    },
2774         { TOK_LE,       GEN_NOPUSH,     g_le    },
2775         { TOK_GE,       GEN_NOPUSH,     g_ge    },
2776         { TOK_GT,       GEN_NOPUSH,     g_gt    },
2777         { TOK_INVALID,  0,              0       }
2778     };
2779     hie_compare (hie6_ops, Expr, ShiftExpr);
2780 }
2781
2782
2783
2784 static void hie5 (ExprDesc* Expr)
2785 /* Handle == and != */
2786 {
2787     static const GenDesc hie5_ops[] = {
2788         { TOK_EQ,       GEN_NOPUSH,     g_eq    },
2789         { TOK_NE,       GEN_NOPUSH,     g_ne    },
2790         { TOK_INVALID,  0,              0       }
2791     };
2792     hie_compare (hie5_ops, Expr, hie6);
2793 }
2794
2795
2796
2797 static void hie4 (ExprDesc* Expr)
2798 /* Handle & (bitwise and) */
2799 {
2800     static const GenDesc hie4_ops[] = {
2801         { TOK_AND,      GEN_NOPUSH | GEN_COMM,  g_and   },
2802         { TOK_INVALID,  0,                      0       }
2803     };
2804     int UsedGen;
2805
2806     hie_internal (hie4_ops, Expr, hie5, &UsedGen);
2807 }
2808
2809
2810
2811 static void hie3 (ExprDesc* Expr)
2812 /* Handle ^ (bitwise exclusive or) */
2813 {
2814     static const GenDesc hie3_ops[] = {
2815         { TOK_XOR,      GEN_NOPUSH | GEN_COMM,  g_xor   },
2816         { TOK_INVALID,  0,                      0       }
2817     };
2818     int UsedGen;
2819
2820     hie_internal (hie3_ops, Expr, hie4, &UsedGen);
2821 }
2822
2823
2824
2825 static void hie2 (ExprDesc* Expr)
2826 /* Handle | (bitwise or) */
2827 {
2828     static const GenDesc hie2_ops[] = {
2829         { TOK_OR,       GEN_NOPUSH | GEN_COMM,  g_or    },
2830         { TOK_INVALID,  0,                      0       }
2831     };
2832     int UsedGen;
2833
2834     hie_internal (hie2_ops, Expr, hie3, &UsedGen);
2835 }
2836
2837
2838
2839 static void hieAndPP (ExprDesc* Expr)
2840 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2841 ** called recursively from the preprocessor.
2842 */
2843 {
2844     ExprDesc Expr2;
2845
2846     ConstAbsIntExpr (hie2, Expr);
2847     while (CurTok.Tok == TOK_BOOL_AND) {
2848
2849         /* Skip the && */
2850         NextToken ();
2851
2852         /* Get rhs */
2853         ConstAbsIntExpr (hie2, &Expr2);
2854
2855         /* Combine the two */
2856         Expr->IVal = (Expr->IVal && Expr2.IVal);
2857     }
2858 }
2859
2860
2861
2862 static void hieOrPP (ExprDesc *Expr)
2863 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2864 ** called recursively from the preprocessor.
2865 */
2866 {
2867     ExprDesc Expr2;
2868
2869     ConstAbsIntExpr (hieAndPP, Expr);
2870     while (CurTok.Tok == TOK_BOOL_OR) {
2871
2872         /* Skip the && */
2873         NextToken ();
2874
2875         /* Get rhs */
2876         ConstAbsIntExpr (hieAndPP, &Expr2);
2877
2878         /* Combine the two */
2879         Expr->IVal = (Expr->IVal || Expr2.IVal);
2880     }
2881 }
2882
2883
2884
2885 static void hieAnd (ExprDesc* Expr, unsigned TrueLab, int* BoolOp)
2886 /* Process "exp && exp" */
2887 {
2888     int FalseLab;
2889     ExprDesc Expr2;
2890
2891     ExprWithCheck (hie2, Expr);
2892     if (CurTok.Tok == TOK_BOOL_AND) {
2893
2894         /* Tell our caller that we're evaluating a boolean */
2895         *BoolOp = 1;
2896
2897         /* Get a label that we will use for false expressions */
2898         FalseLab = GetLocalLabel ();
2899
2900         /* If the expr hasn't set condition codes, set the force-test flag */
2901         if (!ED_IsTested (Expr)) {
2902             ED_MarkForTest (Expr);
2903         }
2904
2905         /* Load the value */
2906         LoadExpr (CF_FORCECHAR, Expr);
2907
2908         /* Generate the jump */
2909         g_falsejump (CF_NONE, FalseLab);
2910
2911         /* Parse more boolean and's */
2912         while (CurTok.Tok == TOK_BOOL_AND) {
2913
2914             /* Skip the && */
2915             NextToken ();
2916
2917             /* Get rhs */
2918             hie2 (&Expr2);
2919             if (!ED_IsTested (&Expr2)) {
2920                 ED_MarkForTest (&Expr2);
2921             }
2922             LoadExpr (CF_FORCECHAR, &Expr2);
2923
2924             /* Do short circuit evaluation */
2925             if (CurTok.Tok == TOK_BOOL_AND) {
2926                 g_falsejump (CF_NONE, FalseLab);
2927             } else {
2928                 /* Last expression - will evaluate to true */
2929                 g_truejump (CF_NONE, TrueLab);
2930             }
2931         }
2932
2933         /* Define the false jump label here */
2934         g_defcodelabel (FalseLab);
2935
2936         /* The result is an rvalue in primary */
2937         ED_MakeRValExpr (Expr);
2938         ED_TestDone (Expr);     /* Condition codes are set */
2939     }
2940 }
2941
2942
2943
2944 static void hieOr (ExprDesc *Expr)
2945 /* Process "exp || exp". */
2946 {
2947     ExprDesc Expr2;
2948     int BoolOp = 0;             /* Did we have a boolean op? */
2949     int AndOp;                  /* Did we have a && operation? */
2950     unsigned TrueLab;           /* Jump to this label if true */
2951     unsigned DoneLab;
2952
2953     /* Get a label */
2954     TrueLab = GetLocalLabel ();
2955
2956     /* Call the next level parser */
2957     hieAnd (Expr, TrueLab, &BoolOp);
2958
2959     /* Any boolean or's? */
2960     if (CurTok.Tok == TOK_BOOL_OR) {
2961
2962         /* If the expr hasn't set condition codes, set the force-test flag */
2963         if (!ED_IsTested (Expr)) {
2964             ED_MarkForTest (Expr);
2965         }
2966
2967         /* Get first expr */
2968         LoadExpr (CF_FORCECHAR, Expr);
2969
2970         /* For each expression jump to TrueLab if true. Beware: If we
2971         ** had && operators, the jump is already in place!
2972         */
2973         if (!BoolOp) {
2974             g_truejump (CF_NONE, TrueLab);
2975         }
2976
2977         /* Remember that we had a boolean op */
2978         BoolOp = 1;
2979
2980         /* while there's more expr */
2981         while (CurTok.Tok == TOK_BOOL_OR) {
2982
2983             /* skip the || */
2984             NextToken ();
2985
2986             /* Get a subexpr */
2987             AndOp = 0;
2988             hieAnd (&Expr2, TrueLab, &AndOp);
2989             if (!ED_IsTested (&Expr2)) {
2990                 ED_MarkForTest (&Expr2);
2991             }
2992             LoadExpr (CF_FORCECHAR, &Expr2);
2993
2994             /* If there is more to come, add shortcut boolean eval. */
2995             g_truejump (CF_NONE, TrueLab);
2996
2997         }
2998
2999         /* The result is an rvalue in primary */
3000         ED_MakeRValExpr (Expr);
3001         ED_TestDone (Expr);                     /* Condition codes are set */
3002     }
3003
3004     /* If we really had boolean ops, generate the end sequence */
3005     if (BoolOp) {
3006         DoneLab = GetLocalLabel ();
3007         g_getimmed (CF_INT | CF_CONST, 0, 0);   /* Load FALSE */
3008         g_falsejump (CF_NONE, DoneLab);
3009         g_defcodelabel (TrueLab);
3010         g_getimmed (CF_INT | CF_CONST, 1, 0);   /* Load TRUE */
3011         g_defcodelabel (DoneLab);
3012     }
3013 }
3014
3015
3016
3017 static void hieQuest (ExprDesc* Expr)
3018 /* Parse the ternary operator */
3019 {
3020     int         FalseLab;
3021     int         TrueLab;
3022     CodeMark    TrueCodeEnd;
3023     ExprDesc    Expr2;          /* Expression 2 */
3024     ExprDesc    Expr3;          /* Expression 3 */
3025     int         Expr2IsNULL;    /* Expression 2 is a NULL pointer */
3026     int         Expr3IsNULL;    /* Expression 3 is a NULL pointer */
3027     Type*       ResultType;     /* Type of result */
3028
3029
3030     /* Call the lower level eval routine */
3031     if (Preprocessing) {
3032         ExprWithCheck (hieOrPP, Expr);
3033     } else {
3034         ExprWithCheck (hieOr, Expr);
3035     }
3036
3037     /* Check if it's a ternary expression */
3038     if (CurTok.Tok == TOK_QUEST) {
3039         NextToken ();
3040         if (!ED_IsTested (Expr)) {
3041             /* Condition codes not set, request a test */
3042             ED_MarkForTest (Expr);
3043         }
3044         LoadExpr (CF_NONE, Expr);
3045         FalseLab = GetLocalLabel ();
3046         g_falsejump (CF_NONE, FalseLab);
3047
3048         /* Parse second expression. Remember for later if it is a NULL pointer
3049         ** expression, then load it into the primary.
3050         */
3051         ExprWithCheck (hie1, &Expr2);
3052         Expr2IsNULL = ED_IsNullPtr (&Expr2);
3053         if (!IsTypeVoid (Expr2.Type)) {
3054             /* Load it into the primary */
3055             LoadExpr (CF_NONE, &Expr2);
3056             ED_MakeRValExpr (&Expr2);
3057             Expr2.Type = PtrConversion (Expr2.Type);
3058         }
3059
3060         /* Remember the current code position */
3061         GetCodePos (&TrueCodeEnd);
3062
3063         /* Jump around the evaluation of the third expression */
3064         TrueLab = GetLocalLabel ();
3065         ConsumeColon ();
3066         g_jump (TrueLab);
3067
3068         /* Jump here if the first expression was false */
3069         g_defcodelabel (FalseLab);
3070
3071         /* Parse third expression. Remember for later if it is a NULL pointer
3072         ** expression, then load it into the primary.
3073         */
3074         ExprWithCheck (hie1, &Expr3);
3075         Expr3IsNULL = ED_IsNullPtr (&Expr3);
3076         if (!IsTypeVoid (Expr3.Type)) {
3077             /* Load it into the primary */
3078             LoadExpr (CF_NONE, &Expr3);
3079             ED_MakeRValExpr (&Expr3);
3080             Expr3.Type = PtrConversion (Expr3.Type);
3081         }
3082
3083         /* Check if any conversions are needed, if so, do them.
3084         ** Conversion rules for ?: expression are:
3085         **   - if both expressions are int expressions, default promotion
3086         **     rules for ints apply.
3087         **   - if both expressions are pointers of the same type, the
3088         **     result of the expression is of this type.
3089         **   - if one of the expressions is a pointer and the other is
3090         **     a zero constant, the resulting type is that of the pointer
3091         **     type.
3092         **   - if both expressions are void expressions, the result is of
3093         **     type void.
3094         **   - all other cases are flagged by an error.
3095         */
3096         if (IsClassInt (Expr2.Type) && IsClassInt (Expr3.Type)) {
3097
3098             CodeMark    CvtCodeStart;
3099             CodeMark    CvtCodeEnd;
3100
3101
3102             /* Get common type */
3103             ResultType = promoteint (Expr2.Type, Expr3.Type);
3104
3105             /* Convert the third expression to this type if needed */
3106             TypeConversion (&Expr3, ResultType);
3107
3108             /* Emit conversion code for the second expression, but remember
3109             ** where it starts end ends.
3110             */
3111             GetCodePos (&CvtCodeStart);
3112             TypeConversion (&Expr2, ResultType);
3113             GetCodePos (&CvtCodeEnd);
3114
3115             /* If we had conversion code, move it to the right place */
3116             if (!CodeRangeIsEmpty (&CvtCodeStart, &CvtCodeEnd)) {
3117                 MoveCode (&CvtCodeStart, &CvtCodeEnd, &TrueCodeEnd);
3118             }
3119
3120         } else if (IsClassPtr (Expr2.Type) && IsClassPtr (Expr3.Type)) {
3121             /* Must point to same type */
3122             if (TypeCmp (Indirect (Expr2.Type), Indirect (Expr3.Type)) < TC_EQUAL) {
3123                 Error ("Incompatible pointer types");
3124             }
3125             /* Result has the common type */
3126             ResultType = Expr2.Type;
3127         } else if (IsClassPtr (Expr2.Type) && Expr3IsNULL) {
3128             /* Result type is pointer, no cast needed */
3129             ResultType = Expr2.Type;
3130         } else if (Expr2IsNULL && IsClassPtr (Expr3.Type)) {
3131             /* Result type is pointer, no cast needed */
3132             ResultType = Expr3.Type;
3133         } else if (IsTypeVoid (Expr2.Type) && IsTypeVoid (Expr3.Type)) {
3134             /* Result type is void */
3135             ResultType = Expr3.Type;
3136         } else {
3137             Error ("Incompatible types");
3138             ResultType = Expr2.Type;            /* Doesn't matter here */
3139         }
3140
3141         /* Define the final label */
3142         g_defcodelabel (TrueLab);
3143
3144         /* Setup the target expression */
3145         ED_MakeRValExpr (Expr);
3146         Expr->Type  = ResultType;
3147     }
3148 }
3149
3150
3151
3152 static void opeq (const GenDesc* Gen, ExprDesc* Expr, const char* Op)
3153 /* Process "op=" operators. */
3154 {
3155     ExprDesc Expr2;
3156     unsigned flags;
3157     CodeMark Mark;
3158     int MustScale;
3159
3160     /* op= can only be used with lvalues */
3161     if (!ED_IsLVal (Expr)) {
3162         Error ("Invalid lvalue in assignment");
3163         return;
3164     }
3165
3166     /* The left side must not be const qualified */
3167     if (IsQualConst (Expr->Type)) {
3168         Error ("Assignment to const");
3169     }
3170
3171     /* There must be an integer or pointer on the left side */
3172     if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
3173         Error ("Invalid left operand type");
3174         /* Continue. Wrong code will be generated, but the compiler won't
3175         ** break, so this is the best error recovery.
3176         */
3177     }
3178
3179     /* Skip the operator token */
3180     NextToken ();
3181
3182     /* Determine the type of the lhs */
3183     flags = TypeOf (Expr->Type);
3184     MustScale = (Gen->Func == g_add || Gen->Func == g_sub) && IsTypePtr (Expr->Type);
3185
3186     /* Get the lhs address on stack (if needed) */
3187     PushAddr (Expr);
3188
3189     /* Fetch the lhs into the primary register if needed */
3190     LoadExpr (CF_NONE, Expr);
3191
3192     /* Bring the lhs on stack */
3193     GetCodePos (&Mark);
3194     g_push (flags, 0);
3195
3196     /* Evaluate the rhs */
3197     MarkedExprWithCheck (hie1, &Expr2);
3198
3199     /* The rhs must be an integer (or a float, but we don't support that yet */
3200     if (!IsClassInt (Expr2.Type)) {
3201         Error ("Invalid right operand for binary operator `%s'", Op);
3202         /* Continue. Wrong code will be generated, but the compiler won't
3203         ** break, so this is the best error recovery.
3204         */
3205     }
3206
3207     /* Check for a constant expression */
3208     if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
3209         /* The resulting value is a constant. If the generator has the NOPUSH
3210         ** flag set, don't push the lhs.
3211         */
3212         if (Gen->Flags & GEN_NOPUSH) {
3213             RemoveCode (&Mark);
3214         }
3215         if (MustScale) {
3216             /* lhs is a pointer, scale rhs */
3217             Expr2.IVal *= CheckedSizeOf (Expr->Type+1);
3218         }
3219
3220         /* If the lhs is character sized, the operation may be later done
3221         ** with characters.
3222         */
3223         if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
3224             flags |= CF_FORCECHAR;
3225         }
3226
3227         /* Special handling for add and sub - some sort of a hack, but short code */
3228         if (Gen->Func == g_add) {
3229             g_inc (flags | CF_CONST, Expr2.IVal);
3230         } else if (Gen->Func == g_sub) {
3231             g_dec (flags | CF_CONST, Expr2.IVal);
3232         } else {
3233             if (Expr2.IVal == 0) {
3234                 /* Check for div by zero/mod by zero */
3235                 if (Gen->Func == g_div) {
3236                     Error ("Division by zero");
3237                 } else if (Gen->Func == g_mod) {
3238                     Error ("Modulo operation with zero");
3239                 }
3240             }
3241             Gen->Func (flags | CF_CONST, Expr2.IVal);
3242         }
3243     } else {
3244
3245         /* rhs is not constant. Load into the primary */
3246         LoadExpr (CF_NONE, &Expr2);
3247         if (MustScale) {
3248             /* lhs is a pointer, scale rhs */
3249             g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Expr->Type+1));
3250         }
3251
3252         /* If the lhs is character sized, the operation may be later done
3253         ** with characters.
3254         */
3255         if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
3256             flags |= CF_FORCECHAR;
3257         }
3258
3259         /* Adjust the types of the operands if needed */
3260         Gen->Func (g_typeadjust (flags, TypeOf (Expr2.Type)), 0);
3261     }
3262     Store (Expr, 0);
3263     ED_MakeRValExpr (Expr);
3264 }
3265
3266
3267
3268 static void addsubeq (const GenDesc* Gen, ExprDesc *Expr, const char* Op)
3269 /* Process the += and -= operators */
3270 {
3271     ExprDesc Expr2;
3272     unsigned lflags;
3273     unsigned rflags;
3274     int      MustScale;
3275
3276
3277     /* We're currently only able to handle some adressing modes */
3278     if (ED_GetLoc (Expr) == E_LOC_EXPR || ED_GetLoc (Expr) == E_LOC_PRIMARY) {
3279         /* Use generic routine */
3280         opeq (Gen, Expr, Op);
3281         return;
3282     }
3283
3284     /* We must have an lvalue */
3285     if (ED_IsRVal (Expr)) {
3286         Error ("Invalid lvalue in assignment");
3287         return;
3288     }
3289
3290     /* The left side must not be const qualified */
3291     if (IsQualConst (Expr->Type)) {
3292         Error ("Assignment to const");
3293     }
3294
3295     /* There must be an integer or pointer on the left side */
3296     if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
3297         Error ("Invalid left operand type");
3298         /* Continue. Wrong code will be generated, but the compiler won't
3299         ** break, so this is the best error recovery.
3300         */
3301     }
3302
3303     /* Skip the operator */
3304     NextToken ();
3305
3306     /* Check if we have a pointer expression and must scale rhs */
3307     MustScale = IsTypePtr (Expr->Type);
3308
3309     /* Initialize the code generator flags */
3310     lflags = 0;
3311     rflags = 0;
3312
3313     /* Evaluate the rhs. We expect an integer here, since float is not
3314     ** supported
3315     */
3316     hie1 (&Expr2);
3317     if (!IsClassInt (Expr2.Type)) {
3318         Error ("Invalid right operand for binary operator `%s'", Op);
3319         /* Continue. Wrong code will be generated, but the compiler won't
3320         ** break, so this is the best error recovery.
3321         */
3322     }
3323     if (ED_IsConstAbs (&Expr2)) {
3324         /* The resulting value is a constant. Scale it. */
3325         if (MustScale) {
3326             Expr2.IVal *= CheckedSizeOf (Indirect (Expr->Type));
3327         }
3328         rflags |= CF_CONST;
3329         lflags |= CF_CONST;
3330     } else {
3331         /* Not constant, load into the primary */
3332         LoadExpr (CF_NONE, &Expr2);
3333         if (MustScale) {
3334             /* lhs is a pointer, scale rhs */
3335             g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Indirect (Expr->Type)));
3336         }
3337     }
3338
3339     /* Setup the code generator flags */
3340     lflags |= TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR;
3341     rflags |= TypeOf (Expr2.Type) | CF_FORCECHAR;
3342
3343     /* Convert the type of the lhs to that of the rhs */
3344     g_typecast (lflags, rflags);
3345
3346     /* Output apropriate code depending on the location */
3347     switch (ED_GetLoc (Expr)) {
3348
3349         case E_LOC_ABS:
3350             /* Absolute: numeric address or const */
3351             if (Gen->Tok == TOK_PLUS_ASSIGN) {
3352                 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3353             } else {
3354                 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3355             }
3356             break;
3357
3358         case E_LOC_GLOBAL:
3359             /* Global variable */
3360             if (Gen->Tok == TOK_PLUS_ASSIGN) {
3361                 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3362             } else {
3363                 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3364             }
3365             break;
3366
3367         case E_LOC_STATIC:
3368         case E_LOC_LITERAL:
3369             /* Static variable or literal in the literal pool */
3370             if (Gen->Tok == TOK_PLUS_ASSIGN) {
3371                 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3372             } else {
3373                 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3374             }
3375             break;
3376
3377         case E_LOC_REGISTER:
3378             /* Register variable */
3379             if (Gen->Tok == TOK_PLUS_ASSIGN) {
3380                 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3381             } else {
3382                 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3383             }
3384             break;
3385
3386         case E_LOC_STACK:
3387             /* Value on the stack */
3388             if (Gen->Tok == TOK_PLUS_ASSIGN) {
3389                 g_addeqlocal (lflags, Expr->IVal, Expr2.IVal);
3390             } else {
3391                 g_subeqlocal (lflags, Expr->IVal, Expr2.IVal);
3392             }
3393             break;
3394
3395         default:
3396             Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
3397     }
3398
3399     /* Expression is a rvalue in the primary now */
3400     ED_MakeRValExpr (Expr);
3401 }
3402
3403
3404
3405 void hie1 (ExprDesc* Expr)
3406 /* Parse first level of expression hierarchy. */
3407 {
3408     hieQuest (Expr);
3409     switch (CurTok.Tok) {
3410
3411         case TOK_ASSIGN:
3412             Assignment (Expr);
3413             break;
3414
3415         case TOK_PLUS_ASSIGN:
3416             addsubeq (&GenPASGN, Expr, "+=");
3417             break;
3418
3419         case TOK_MINUS_ASSIGN:
3420             addsubeq (&GenSASGN, Expr, "-=");
3421             break;
3422
3423         case TOK_MUL_ASSIGN:
3424             opeq (&GenMASGN, Expr, "*=");
3425             break;
3426
3427         case TOK_DIV_ASSIGN:
3428             opeq (&GenDASGN, Expr, "/=");
3429             break;
3430
3431         case TOK_MOD_ASSIGN:
3432             opeq (&GenMOASGN, Expr, "%=");
3433             break;
3434
3435         case TOK_SHL_ASSIGN:
3436             opeq (&GenSLASGN, Expr, "<<=");
3437             break;
3438
3439         case TOK_SHR_ASSIGN:
3440             opeq (&GenSRASGN, Expr, ">>=");
3441             break;
3442
3443         case TOK_AND_ASSIGN:
3444             opeq (&GenAASGN, Expr, "&=");
3445             break;
3446
3447         case TOK_XOR_ASSIGN:
3448             opeq (&GenXOASGN, Expr, "^=");
3449             break;
3450
3451         case TOK_OR_ASSIGN:
3452             opeq (&GenOASGN, Expr, "|=");
3453             break;
3454
3455         default:
3456             break;
3457     }
3458 }
3459
3460
3461
3462 void hie0 (ExprDesc *Expr)
3463 /* Parse comma operator. */
3464 {
3465     hie1 (Expr);
3466     while (CurTok.Tok == TOK_COMMA) {
3467         NextToken ();
3468         hie1 (Expr);
3469     }
3470 }
3471
3472
3473
3474 int evalexpr (unsigned Flags, void (*Func) (ExprDesc*), ExprDesc* Expr)
3475 /* Will evaluate an expression via the given function. If the result is a
3476 ** constant, 0 is returned and the value is put in the Expr struct. If the
3477 ** result is not constant, LoadExpr is called to bring the value into the
3478 ** primary register and 1 is returned.
3479 */
3480 {
3481     /* Evaluate */
3482     ExprWithCheck (Func, Expr);
3483
3484     /* Check for a constant expression */
3485     if (ED_IsConstAbs (Expr)) {
3486         /* Constant expression */
3487         return 0;
3488     } else {
3489         /* Not constant, load into the primary */
3490         LoadExpr (Flags, Expr);
3491         return 1;
3492     }
3493 }
3494
3495
3496
3497 void Expression0 (ExprDesc* Expr)
3498 /* Evaluate an expression via hie0 and put the result into the primary register */
3499 {
3500     ExprWithCheck (hie0, Expr);
3501     LoadExpr (CF_NONE, Expr);
3502 }
3503
3504
3505
3506 void ConstExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3507 /* Will evaluate an expression via the given function. If the result is not
3508 ** a constant of some sort, a diagnostic will be printed, and the value is
3509 ** replaced by a constant one to make sure there are no internal errors that
3510 ** result from this input error.
3511 */
3512 {
3513     ExprWithCheck (Func, Expr);
3514     if (!ED_IsConst (Expr)) {
3515         Error ("Constant expression expected");
3516         /* To avoid any compiler errors, make the expression a valid const */
3517         ED_MakeConstAbsInt (Expr, 1);
3518     }
3519 }
3520
3521
3522
3523 void BoolExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3524 /* Will evaluate an expression via the given function. If the result is not
3525 ** something that may be evaluated in a boolean context, a diagnostic will be
3526 ** printed, and the value is replaced by a constant one to make sure there
3527 ** are no internal errors that result from this input error.
3528 */
3529 {
3530     ExprWithCheck (Func, Expr);
3531     if (!ED_IsBool (Expr)) {
3532         Error ("Boolean expression expected");
3533         /* To avoid any compiler errors, make the expression a valid int */
3534         ED_MakeConstAbsInt (Expr, 1);
3535     }
3536 }
3537
3538
3539
3540 void ConstAbsIntExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3541 /* Will evaluate an expression via the given function. If the result is not
3542 ** a constant numeric integer value, a diagnostic will be printed, and the
3543 ** value is replaced by a constant one to make sure there are no internal
3544 ** errors that result from this input error.
3545 */
3546 {
3547     ExprWithCheck (Func, Expr);
3548     if (!ED_IsConstAbsInt (Expr)) {
3549         Error ("Constant integer expression expected");
3550         /* To avoid any compiler errors, make the expression a valid const */
3551         ED_MakeConstAbsInt (Expr, 1);
3552     }
3553 }