]> git.sur5r.net Git - cc65/blob - src/cc65/expr.c
Merge pull request #164 from greg-king5/testsuite
[cc65] / src / cc65 / expr.c
1 /* expr.c
2 **
3 ** 1998-06-21, Ullrich von Bassewitz
4 ** 2015-06-26, 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                 /* If the expression points to an array, then don't convert the
1717                 ** address -- it already is the location of the first element.
1718                 */
1719                 if (!IsTypeArray (Expr->Type)) {
1720                     /* The * operator yields an lvalue */
1721                     ED_MakeLVal (Expr);
1722                 }
1723             }
1724             break;
1725
1726         case TOK_AND:
1727             NextToken ();
1728             ExprWithCheck (hie10, Expr);
1729             /* The & operator may be applied to any lvalue, and it may be
1730             ** applied to functions, even if they're no lvalues.
1731             */
1732             if (ED_IsRVal (Expr) && !IsTypeFunc (Expr->Type) && !IsTypeArray (Expr->Type)) {
1733                 Error ("Illegal address");
1734             } else {
1735                 if (ED_IsBitField (Expr)) {
1736                     Error ("Cannot take address of bit-field");
1737                     /* Do it anyway, just to avoid further warnings */
1738                     Expr->Flags &= ~E_BITFIELD;
1739                 }
1740                 Expr->Type = PointerTo (Expr->Type);
1741                 /* The & operator yields an rvalue */
1742                 ED_MakeRVal (Expr);
1743             }
1744             break;
1745
1746         case TOK_SIZEOF:
1747             NextToken ();
1748             if (TypeSpecAhead ()) {
1749                 Type T[MAXTYPELEN];
1750                 NextToken ();
1751                 Size = CheckedSizeOf (ParseType (T));
1752                 ConsumeRParen ();
1753             } else {
1754                 /* Remember the output queue pointer */
1755                 CodeMark Mark;
1756                 GetCodePos (&Mark);
1757                 hie10 (Expr);
1758                 /* If the expression is a literal string, release it, so it
1759                 ** won't be output as data if not used elsewhere.
1760                 */
1761                 if (ED_IsLocLiteral (Expr)) {
1762                     ReleaseLiteral (Expr->LVal);
1763                 }
1764                 /* Calculate the size */
1765                 Size = CheckedSizeOf (Expr->Type);
1766                 /* Remove any generated code */
1767                 RemoveCode (&Mark);
1768             }
1769             ED_MakeConstAbs (Expr, Size, type_size_t);
1770             ED_MarkAsUntested (Expr);
1771             break;
1772
1773         default:
1774             if (TypeSpecAhead ()) {
1775
1776                 /* A typecast */
1777                 TypeCast (Expr);
1778
1779             } else {
1780
1781                 /* An expression */
1782                 hie11 (Expr);
1783
1784                 /* Handle post increment */
1785                 switch (CurTok.Tok) {
1786                     case TOK_INC:   PostInc (Expr); break;
1787                     case TOK_DEC:   PostDec (Expr); break;
1788                     default:                        break;
1789                 }
1790
1791             }
1792             break;
1793     }
1794 }
1795
1796
1797
1798 static void hie_internal (const GenDesc* Ops,   /* List of generators */
1799                           ExprDesc* Expr,
1800                           void (*hienext) (ExprDesc*),
1801                           int* UsedGen)
1802 /* Helper function */
1803 {
1804     ExprDesc Expr2;
1805     CodeMark Mark1;
1806     CodeMark Mark2;
1807     const GenDesc* Gen;
1808     token_t Tok;                        /* The operator token */
1809     unsigned ltype, type;
1810     int lconst;                         /* Left operand is a constant */
1811     int rconst;                         /* Right operand is a constant */
1812
1813
1814     ExprWithCheck (hienext, Expr);
1815
1816     *UsedGen = 0;
1817     while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1818
1819         /* Tell the caller that we handled it's ops */
1820         *UsedGen = 1;
1821
1822         /* All operators that call this function expect an int on the lhs */
1823         if (!IsClassInt (Expr->Type)) {
1824             Error ("Integer expression expected");
1825             /* To avoid further errors, make Expr a valid int expression */
1826             ED_MakeConstAbsInt (Expr, 1);
1827         }
1828
1829         /* Remember the operator token, then skip it */
1830         Tok = CurTok.Tok;
1831         NextToken ();
1832
1833         /* Get the lhs on stack */
1834         GetCodePos (&Mark1);
1835         ltype = TypeOf (Expr->Type);
1836         lconst = ED_IsConstAbs (Expr);
1837         if (lconst) {
1838             /* Constant value */
1839             GetCodePos (&Mark2);
1840             /* If the operator is commutative, don't push the left side, if
1841             ** it's a constant, since we will exchange both operands.
1842             */
1843             if ((Gen->Flags & GEN_COMM) == 0) {
1844                 g_push (ltype | CF_CONST, Expr->IVal);
1845             }
1846         } else {
1847             /* Value not constant */
1848             LoadExpr (CF_NONE, Expr);
1849             GetCodePos (&Mark2);
1850             g_push (ltype, 0);
1851         }
1852
1853         /* Get the right hand side */
1854         MarkedExprWithCheck (hienext, &Expr2);
1855
1856         /* Check for a constant expression */
1857         rconst = (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2));
1858         if (!rconst) {
1859             /* Not constant, load into the primary */
1860             LoadExpr (CF_NONE, &Expr2);
1861         }
1862
1863         /* Check the type of the rhs */
1864         if (!IsClassInt (Expr2.Type)) {
1865             Error ("Integer expression expected");
1866         }
1867
1868         /* Check for const operands */
1869         if (lconst && rconst) {
1870
1871             /* Both operands are constant, remove the generated code */
1872             RemoveCode (&Mark1);
1873
1874             /* Get the type of the result */
1875             Expr->Type = promoteint (Expr->Type, Expr2.Type);
1876
1877             /* Handle the op differently for signed and unsigned types */
1878             if (IsSignSigned (Expr->Type)) {
1879
1880                 /* Evaluate the result for signed operands */
1881                 signed long Val1 = Expr->IVal;
1882                 signed long Val2 = Expr2.IVal;
1883                 switch (Tok) {
1884                     case TOK_OR:
1885                         Expr->IVal = (Val1 | Val2);
1886                         break;
1887                     case TOK_XOR:
1888                         Expr->IVal = (Val1 ^ Val2);
1889                         break;
1890                     case TOK_AND:
1891                         Expr->IVal = (Val1 & Val2);
1892                         break;
1893                     case TOK_STAR:
1894                         Expr->IVal = (Val1 * Val2);
1895                         break;
1896                     case TOK_DIV:
1897                         if (Val2 == 0) {
1898                             Error ("Division by zero");
1899                             Expr->IVal = 0x7FFFFFFF;
1900                         } else {
1901                             Expr->IVal = (Val1 / Val2);
1902                         }
1903                         break;
1904                     case TOK_MOD:
1905                         if (Val2 == 0) {
1906                             Error ("Modulo operation with zero");
1907                             Expr->IVal = 0;
1908                         } else {
1909                             Expr->IVal = (Val1 % Val2);
1910                         }
1911                         break;
1912                     default:
1913                         Internal ("hie_internal: got token 0x%X\n", Tok);
1914                 }
1915             } else {
1916
1917                 /* Evaluate the result for unsigned operands */
1918                 unsigned long Val1 = Expr->IVal;
1919                 unsigned long Val2 = Expr2.IVal;
1920                 switch (Tok) {
1921                     case TOK_OR:
1922                         Expr->IVal = (Val1 | Val2);
1923                         break;
1924                     case TOK_XOR:
1925                         Expr->IVal = (Val1 ^ Val2);
1926                         break;
1927                     case TOK_AND:
1928                         Expr->IVal = (Val1 & Val2);
1929                         break;
1930                     case TOK_STAR:
1931                         Expr->IVal = (Val1 * Val2);
1932                         break;
1933                     case TOK_DIV:
1934                         if (Val2 == 0) {
1935                             Error ("Division by zero");
1936                             Expr->IVal = 0xFFFFFFFF;
1937                         } else {
1938                             Expr->IVal = (Val1 / Val2);
1939                         }
1940                         break;
1941                     case TOK_MOD:
1942                         if (Val2 == 0) {
1943                             Error ("Modulo operation with zero");
1944                             Expr->IVal = 0;
1945                         } else {
1946                             Expr->IVal = (Val1 % Val2);
1947                         }
1948                         break;
1949                     default:
1950                         Internal ("hie_internal: got token 0x%X\n", Tok);
1951                 }
1952             }
1953
1954         } else if (lconst && (Gen->Flags & GEN_COMM) && !rconst) {
1955
1956             /* The left side is constant, the right side is not, and the
1957             ** operator allows swapping the operands. We haven't pushed the
1958             ** left side onto the stack in this case, and will reverse the
1959             ** operation because this allows for better code.
1960             */
1961             unsigned rtype = ltype | CF_CONST;
1962             ltype = TypeOf (Expr2.Type);       /* Expr2 is now left */
1963             type = CF_CONST;
1964             if ((Gen->Flags & GEN_NOPUSH) == 0) {
1965                 g_push (ltype, 0);
1966             } else {
1967                 ltype |= CF_REG;        /* Value is in register */
1968             }
1969
1970             /* Determine the type of the operation result. */
1971             type |= g_typeadjust (ltype, rtype);
1972             Expr->Type = promoteint (Expr->Type, Expr2.Type);
1973
1974             /* Generate code */
1975             Gen->Func (type, Expr->IVal);
1976
1977             /* We have a rvalue in the primary now */
1978             ED_MakeRValExpr (Expr);
1979
1980         } else {
1981
1982             /* If the right hand side is constant, and the generator function
1983             ** expects the lhs in the primary, remove the push of the primary
1984             ** now.
1985             */
1986             unsigned rtype = TypeOf (Expr2.Type);
1987             type = 0;
1988             if (rconst) {
1989                 /* Second value is constant - check for div */
1990                 type |= CF_CONST;
1991                 rtype |= CF_CONST;
1992                 if (Tok == TOK_DIV && Expr2.IVal == 0) {
1993                     Error ("Division by zero");
1994                 } else if (Tok == TOK_MOD && Expr2.IVal == 0) {
1995                     Error ("Modulo operation with zero");
1996                 }
1997                 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1998                     RemoveCode (&Mark2);
1999                     ltype |= CF_REG;    /* Value is in register */
2000                 }
2001             }
2002
2003             /* Determine the type of the operation result. */
2004             type |= g_typeadjust (ltype, rtype);
2005             Expr->Type = promoteint (Expr->Type, Expr2.Type);
2006
2007             /* Generate code */
2008             Gen->Func (type, Expr2.IVal);
2009
2010             /* We have a rvalue in the primary now */
2011             ED_MakeRValExpr (Expr);
2012         }
2013     }
2014 }
2015
2016
2017
2018 static void hie_compare (const GenDesc* Ops,    /* List of generators */
2019                          ExprDesc* Expr,
2020                          void (*hienext) (ExprDesc*))
2021 /* Helper function for the compare operators */
2022 {
2023     ExprDesc Expr2;
2024     CodeMark Mark0;
2025     CodeMark Mark1;
2026     CodeMark Mark2;
2027     const GenDesc* Gen;
2028     token_t Tok;                        /* The operator token */
2029     unsigned ltype;
2030     int rconst;                         /* Operand is a constant */
2031
2032
2033     GetCodePos (&Mark0);
2034     ExprWithCheck (hienext, Expr);
2035
2036     while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
2037
2038         /* Remember the generator function */
2039         void (*GenFunc) (unsigned, unsigned long) = Gen->Func;
2040
2041         /* Remember the operator token, then skip it */
2042         Tok = CurTok.Tok;
2043         NextToken ();
2044
2045         /* Get the lhs on stack */
2046         GetCodePos (&Mark1);
2047         ltype = TypeOf (Expr->Type);
2048         if (ED_IsConstAbs (Expr)) {
2049             /* Constant value */
2050             GetCodePos (&Mark2);
2051             g_push (ltype | CF_CONST, Expr->IVal);
2052         } else {
2053             /* Value not constant */
2054             LoadExpr (CF_NONE, Expr);
2055             GetCodePos (&Mark2);
2056             g_push (ltype, 0);
2057         }
2058
2059         /* Get the right hand side */
2060         MarkedExprWithCheck (hienext, &Expr2);
2061
2062         /* Check for a constant expression */
2063         rconst = (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2));
2064         if (!rconst) {
2065             /* Not constant, load into the primary */
2066             LoadExpr (CF_NONE, &Expr2);
2067         }
2068
2069         /* Make sure, the types are compatible */
2070         if (IsClassInt (Expr->Type)) {
2071             if (!IsClassInt (Expr2.Type) && !(IsClassPtr(Expr2.Type) && ED_IsNullPtr(Expr))) {
2072                 Error ("Incompatible types");
2073             }
2074         } else if (IsClassPtr (Expr->Type)) {
2075             if (IsClassPtr (Expr2.Type)) {
2076                 /* Both pointers are allowed in comparison if they point to
2077                 ** the same type, or if one of them is a void pointer.
2078                 */
2079                 Type* left  = Indirect (Expr->Type);
2080                 Type* right = Indirect (Expr2.Type);
2081                 if (TypeCmp (left, right) < TC_EQUAL && left->C != T_VOID && right->C != T_VOID) {
2082                     /* Incomatible pointers */
2083                     Error ("Incompatible types");
2084                 }
2085             } else if (!ED_IsNullPtr (&Expr2)) {
2086                 Error ("Incompatible types");
2087             }
2088         }
2089
2090         /* Check for const operands */
2091         if (ED_IsConstAbs (Expr) && rconst) {
2092
2093             /* If the result is constant, this is suspicious when not in
2094             ** preprocessor mode.
2095             */
2096             WarnConstCompareResult ();
2097
2098             /* Both operands are constant, remove the generated code */
2099             RemoveCode (&Mark1);
2100
2101             /* Determine if this is a signed or unsigned compare */
2102             if (IsClassInt (Expr->Type) && IsSignSigned (Expr->Type) &&
2103                 IsClassInt (Expr2.Type) && IsSignSigned (Expr2.Type)) {
2104
2105                 /* Evaluate the result for signed operands */
2106                 signed long Val1 = Expr->IVal;
2107                 signed long Val2 = Expr2.IVal;
2108                 switch (Tok) {
2109                     case TOK_EQ: Expr->IVal = (Val1 == Val2);   break;
2110                     case TOK_NE: Expr->IVal = (Val1 != Val2);   break;
2111                     case TOK_LT: Expr->IVal = (Val1 < Val2);    break;
2112                     case TOK_LE: Expr->IVal = (Val1 <= Val2);   break;
2113                     case TOK_GE: Expr->IVal = (Val1 >= Val2);   break;
2114                     case TOK_GT: Expr->IVal = (Val1 > Val2);    break;
2115                     default:     Internal ("hie_compare: got token 0x%X\n", Tok);
2116                 }
2117
2118             } else {
2119
2120                 /* Evaluate the result for unsigned operands */
2121                 unsigned long Val1 = Expr->IVal;
2122                 unsigned long Val2 = Expr2.IVal;
2123                 switch (Tok) {
2124                     case TOK_EQ: Expr->IVal = (Val1 == Val2);   break;
2125                     case TOK_NE: Expr->IVal = (Val1 != Val2);   break;
2126                     case TOK_LT: Expr->IVal = (Val1 < Val2);    break;
2127                     case TOK_LE: Expr->IVal = (Val1 <= Val2);   break;
2128                     case TOK_GE: Expr->IVal = (Val1 >= Val2);   break;
2129                     case TOK_GT: Expr->IVal = (Val1 > Val2);    break;
2130                     default:     Internal ("hie_compare: got token 0x%X\n", Tok);
2131                 }
2132             }
2133
2134         } else {
2135
2136             /* Determine the signedness of the operands */
2137             int LeftSigned  = IsSignSigned (Expr->Type);
2138             int RightSigned = IsSignSigned (Expr2.Type);
2139
2140             /* If the right hand side is constant, and the generator function
2141             ** expects the lhs in the primary, remove the push of the primary
2142             ** now.
2143             */
2144             unsigned flags = 0;
2145             if (rconst) {
2146                 flags |= CF_CONST;
2147                 if ((Gen->Flags & GEN_NOPUSH) != 0) {
2148                     RemoveCode (&Mark2);
2149                     ltype |= CF_REG;    /* Value is in register */
2150                 }
2151             }
2152
2153             /* Determine the type of the operation. */
2154             if (IsTypeChar (Expr->Type) && rconst) {
2155
2156                 /* Left side is unsigned char, right side is constant.
2157                 ** Determine the minimum and maximum values
2158                 */
2159                 int LeftMin, LeftMax;
2160                 if (LeftSigned) {
2161                     LeftMin = -128;
2162                     LeftMax = 127;
2163                 } else {
2164                     LeftMin = 0;
2165                     LeftMax = 255;
2166                 }
2167                 /* An integer value is always represented as a signed in the
2168                 ** ExprDesc structure. This may lead to false results below,
2169                 ** if it is actually unsigned, but interpreted as signed
2170                 ** because of the representation. Fortunately, in this case,
2171                 ** the actual value doesn't matter, since it's always greater
2172                 ** than what can be represented in a char. So correct the
2173                 ** value accordingly.
2174                 */
2175                 if (!RightSigned && Expr2.IVal < 0) {
2176                     /* Correct the value so it is an unsigned. It will then
2177                     ** anyway match one of the cases below.
2178                     */
2179                     Expr2.IVal = LeftMax + 1;
2180                 }
2181
2182                 /* Comparing a char against a constant may have a constant
2183                 ** result. Please note: It is not possible to remove the code
2184                 ** for the compare alltogether, because it may have side
2185                 ** effects.
2186                 */
2187                 switch (Tok) {
2188
2189                     case TOK_EQ:
2190                         if (Expr2.IVal < LeftMin || Expr2.IVal > LeftMax) {
2191                             ED_MakeConstAbsInt (Expr, 0);
2192                             WarnConstCompareResult ();
2193                             goto Done;
2194                         }
2195                         break;
2196
2197                     case TOK_NE:
2198                         if (Expr2.IVal < LeftMin || Expr2.IVal > LeftMax) {
2199                             ED_MakeConstAbsInt (Expr, 1);
2200                             WarnConstCompareResult ();
2201                             goto Done;
2202                         }
2203                         break;
2204
2205                     case TOK_LT:
2206                         if (Expr2.IVal <= LeftMin || Expr2.IVal > LeftMax) {
2207                             ED_MakeConstAbsInt (Expr, Expr2.IVal > LeftMax);
2208                             WarnConstCompareResult ();
2209                             goto Done;
2210                         }
2211                         break;
2212
2213                     case TOK_LE:
2214                         if (Expr2.IVal < LeftMin || Expr2.IVal >= LeftMax) {
2215                             ED_MakeConstAbsInt (Expr, Expr2.IVal >= LeftMax);
2216                             WarnConstCompareResult ();
2217                             goto Done;
2218                         }
2219                         break;
2220
2221                     case TOK_GE:
2222                         if (Expr2.IVal <= LeftMin || Expr2.IVal > LeftMax) {
2223                             ED_MakeConstAbsInt (Expr, Expr2.IVal <= LeftMin);
2224                             WarnConstCompareResult ();
2225                             goto Done;
2226                         }
2227                         break;
2228
2229                     case TOK_GT:
2230                         if (Expr2.IVal < LeftMin || Expr2.IVal >= LeftMax) {
2231                             ED_MakeConstAbsInt (Expr, Expr2.IVal < LeftMin);
2232                             WarnConstCompareResult ();
2233                             goto Done;
2234                         }
2235                         break;
2236
2237                     default:
2238                         Internal ("hie_compare: got token 0x%X\n", Tok);
2239                 }
2240
2241                 /* If the result is not already constant (as evaluated in the
2242                 ** switch above), we can execute the operation as a char op,
2243                 ** since the right side constant is in a valid range.
2244                 */
2245                 flags |= (CF_CHAR | CF_FORCECHAR);
2246                 if (!LeftSigned) {
2247                     flags |= CF_UNSIGNED;
2248                 }
2249
2250             } else if (IsTypeChar (Expr->Type) && IsTypeChar (Expr2.Type) &&
2251                 GetSignedness (Expr->Type) == GetSignedness (Expr2.Type)) {
2252
2253                 /* Both are chars with the same signedness. We can encode the
2254                 ** operation as a char operation.
2255                 */
2256                 flags |= CF_CHAR;
2257                 if (rconst) {
2258                     flags |= CF_FORCECHAR;
2259                 }
2260                 if (!LeftSigned) {
2261                     flags |= CF_UNSIGNED;
2262                 }
2263             } else {
2264                 unsigned rtype = TypeOf (Expr2.Type) | (flags & CF_CONST);
2265                 flags |= g_typeadjust (ltype, rtype);
2266             }
2267
2268             /* If the left side is an unsigned and the right is a constant,
2269             ** we may be able to change the compares to something more
2270             ** effective.
2271             */
2272             if (!LeftSigned && rconst) {
2273
2274                 switch (Tok) {
2275
2276                     case TOK_LT:
2277                         if (Expr2.IVal == 1) {
2278                             /* An unsigned compare to one means that the value
2279                             ** must be zero.
2280                             */
2281                             GenFunc = g_eq;
2282                             Expr2.IVal = 0;
2283                         }
2284                         break;
2285
2286                     case TOK_LE:
2287                         if (Expr2.IVal == 0) {
2288                             /* An unsigned compare to zero means that the value
2289                             ** must be zero.
2290                             */
2291                             GenFunc = g_eq;
2292                         }
2293                         break;
2294
2295                     case TOK_GE:
2296                         if (Expr2.IVal == 1) {
2297                             /* An unsigned compare to one means that the value
2298                             ** must not be zero.
2299                             */
2300                             GenFunc = g_ne;
2301                             Expr2.IVal = 0;
2302                         }
2303                         break;
2304
2305                     case TOK_GT:
2306                         if (Expr2.IVal == 0) {
2307                             /* An unsigned compare to zero means that the value
2308                             ** must not be zero.
2309                             */
2310                             GenFunc = g_ne;
2311                         }
2312                         break;
2313
2314                     default:
2315                         break;
2316
2317                 }
2318
2319             }
2320
2321             /* Generate code */
2322             GenFunc (flags, Expr2.IVal);
2323
2324             /* The result is an rvalue in the primary */
2325             ED_MakeRValExpr (Expr);
2326         }
2327
2328         /* Result type is always int */
2329         Expr->Type = type_int;
2330
2331 Done:   /* Condition codes are set */
2332         ED_TestDone (Expr);
2333     }
2334 }
2335
2336
2337
2338 static void hie9 (ExprDesc *Expr)
2339 /* Process * and / operators. */
2340 {
2341     static const GenDesc hie9_ops[] = {
2342         { TOK_STAR,     GEN_NOPUSH | GEN_COMM,  g_mul   },
2343         { TOK_DIV,      GEN_NOPUSH,             g_div   },
2344         { TOK_MOD,      GEN_NOPUSH,             g_mod   },
2345         { TOK_INVALID,  0,                      0       }
2346     };
2347     int UsedGen;
2348
2349     hie_internal (hie9_ops, Expr, hie10, &UsedGen);
2350 }
2351
2352
2353
2354 static void parseadd (ExprDesc* Expr)
2355 /* Parse an expression with the binary plus operator. Expr contains the
2356 ** unprocessed left hand side of the expression and will contain the
2357 ** result of the expression on return.
2358 */
2359 {
2360     ExprDesc Expr2;
2361     unsigned flags;             /* Operation flags */
2362     CodeMark Mark;              /* Remember code position */
2363     Type* lhst;                 /* Type of left hand side */
2364     Type* rhst;                 /* Type of right hand side */
2365
2366
2367     /* Skip the PLUS token */
2368     NextToken ();
2369
2370     /* Get the left hand side type, initialize operation flags */
2371     lhst = Expr->Type;
2372     flags = 0;
2373
2374     /* Check for constness on both sides */
2375     if (ED_IsConst (Expr)) {
2376
2377         /* The left hand side is a constant of some sort. Good. Get rhs */
2378         ExprWithCheck (hie9, &Expr2);
2379         if (ED_IsConstAbs (&Expr2)) {
2380
2381             /* Right hand side is a constant numeric value. Get the rhs type */
2382             rhst = Expr2.Type;
2383
2384             /* Both expressions are constants. Check for pointer arithmetic */
2385             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2386                 /* Left is pointer, right is int, must scale rhs */
2387                 Expr->IVal += Expr2.IVal * CheckedPSizeOf (lhst);
2388                 /* Result type is a pointer */
2389             } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2390                 /* Left is int, right is pointer, must scale lhs */
2391                 Expr->IVal = Expr->IVal * CheckedPSizeOf (rhst) + Expr2.IVal;
2392                 /* Result type is a pointer */
2393                 Expr->Type = Expr2.Type;
2394             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2395                 /* Integer addition */
2396                 Expr->IVal += Expr2.IVal;
2397                 typeadjust (Expr, &Expr2, 1);
2398             } else {
2399                 /* OOPS */
2400                 Error ("Invalid operands for binary operator `+'");
2401             }
2402
2403         } else {
2404
2405             /* lhs is a constant and rhs is not constant. Load rhs into
2406             ** the primary.
2407             */
2408             LoadExpr (CF_NONE, &Expr2);
2409
2410             /* Beware: The check above (for lhs) lets not only pass numeric
2411             ** constants, but also constant addresses (labels), maybe even
2412             ** with an offset. We have to check for that here.
2413             */
2414
2415             /* First, get the rhs type. */
2416             rhst = Expr2.Type;
2417
2418             /* Setup flags */
2419             if (ED_IsLocAbs (Expr)) {
2420                 /* A numerical constant */
2421                 flags |= CF_CONST;
2422             } else {
2423                 /* Constant address label */
2424                 flags |= GlobalModeFlags (Expr) | CF_CONSTADDR;
2425             }
2426
2427             /* Check for pointer arithmetic */
2428             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2429                 /* Left is pointer, right is int, must scale rhs */
2430                 g_scale (CF_INT, CheckedPSizeOf (lhst));
2431                 /* Operate on pointers, result type is a pointer */
2432                 flags |= CF_PTR;
2433                 /* Generate the code for the add */
2434                 if (ED_GetLoc (Expr) == E_LOC_ABS) {
2435                     /* Numeric constant */
2436                     g_inc (flags, Expr->IVal);
2437                 } else {
2438                     /* Constant address */
2439                     g_addaddr_static (flags, Expr->Name, Expr->IVal);
2440                 }
2441             } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2442
2443                 /* Left is int, right is pointer, must scale lhs. */
2444                 unsigned ScaleFactor = CheckedPSizeOf (rhst);
2445
2446                 /* Operate on pointers, result type is a pointer */
2447                 flags |= CF_PTR;
2448                 Expr->Type = Expr2.Type;
2449
2450                 /* Since we do already have rhs in the primary, if lhs is
2451                 ** not a numeric constant, and the scale factor is not one
2452                 ** (no scaling), we must take the long way over the stack.
2453                 */
2454                 if (ED_IsLocAbs (Expr)) {
2455                     /* Numeric constant, scale lhs */
2456                     Expr->IVal *= ScaleFactor;
2457                     /* Generate the code for the add */
2458                     g_inc (flags, Expr->IVal);
2459                 } else if (ScaleFactor == 1) {
2460                     /* Constant address but no need to scale */
2461                     g_addaddr_static (flags, Expr->Name, Expr->IVal);
2462                 } else {
2463                     /* Constant address that must be scaled */
2464                     g_push (TypeOf (Expr2.Type), 0);    /* rhs --> stack */
2465                     g_getimmed (flags, Expr->Name, Expr->IVal);
2466                     g_scale (CF_PTR, ScaleFactor);
2467                     g_add (CF_PTR, 0);
2468                 }
2469             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2470                 /* Integer addition */
2471                 flags |= typeadjust (Expr, &Expr2, 1);
2472                 /* Generate the code for the add */
2473                 if (ED_IsLocAbs (Expr)) {
2474                     /* Numeric constant */
2475                     g_inc (flags, Expr->IVal);
2476                 } else {
2477                     /* Constant address */
2478                     g_addaddr_static (flags, Expr->Name, Expr->IVal);
2479                 }
2480             } else {
2481                 /* OOPS */
2482                 Error ("Invalid operands for binary operator `+'");
2483                 flags = CF_INT;
2484             }
2485
2486             /* Result is a rvalue in primary register */
2487             ED_MakeRValExpr (Expr);
2488         }
2489
2490     } else {
2491
2492         /* Left hand side is not constant. Get the value onto the stack. */
2493         LoadExpr (CF_NONE, Expr);              /* --> primary register */
2494         GetCodePos (&Mark);
2495         g_push (TypeOf (Expr->Type), 0);        /* --> stack */
2496
2497         /* Evaluate the rhs */
2498         MarkedExprWithCheck (hie9, &Expr2);
2499
2500         /* Check for a constant rhs expression */
2501         if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
2502
2503             /* Right hand side is a constant. Get the rhs type */
2504             rhst = Expr2.Type;
2505
2506             /* Remove pushed value from stack */
2507             RemoveCode (&Mark);
2508
2509             /* Check for pointer arithmetic */
2510             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2511                 /* Left is pointer, right is int, must scale rhs */
2512                 Expr2.IVal *= CheckedPSizeOf (lhst);
2513                 /* Operate on pointers, result type is a pointer */
2514                 flags = CF_PTR;
2515             } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2516                 /* Left is int, right is pointer, must scale lhs (ptr only) */
2517                 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2518                 /* Operate on pointers, result type is a pointer */
2519                 flags = CF_PTR;
2520                 Expr->Type = Expr2.Type;
2521             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2522                 /* Integer addition */
2523                 flags = typeadjust (Expr, &Expr2, 1);
2524             } else {
2525                 /* OOPS */
2526                 Error ("Invalid operands for binary operator `+'");
2527                 flags = CF_INT;
2528             }
2529
2530             /* Generate code for the add */
2531             g_inc (flags | CF_CONST, Expr2.IVal);
2532
2533         } else {
2534
2535             /* Not constant, load into the primary */
2536             LoadExpr (CF_NONE, &Expr2);
2537
2538             /* lhs and rhs are not constant. Get the rhs type. */
2539             rhst = Expr2.Type;
2540
2541             /* Check for pointer arithmetic */
2542             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2543                 /* Left is pointer, right is int, must scale rhs */
2544                 g_scale (CF_INT, CheckedPSizeOf (lhst));
2545                 /* Operate on pointers, result type is a pointer */
2546                 flags = CF_PTR;
2547             } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2548                 /* Left is int, right is pointer, must scale lhs */
2549                 g_tosint (TypeOf (rhst));       /* Make sure, TOS is int */
2550                 g_swap (CF_INT);                /* Swap TOS and primary */
2551                 g_scale (CF_INT, CheckedPSizeOf (rhst));
2552                 /* Operate on pointers, result type is a pointer */
2553                 flags = CF_PTR;
2554                 Expr->Type = Expr2.Type;
2555             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2556                 /* Integer addition. Note: Result is never constant.
2557                 ** Problem here is that typeadjust does not know if the
2558                 ** variable is an rvalue or lvalue, so if both operands
2559                 ** are dereferenced constant numeric addresses, typeadjust
2560                 ** thinks the operation works on constants. Removing
2561                 ** CF_CONST here means handling the symptoms, however, the
2562                 ** whole parser is such a mess that I fear to break anything
2563                 ** when trying to apply another solution.
2564                 */
2565                 flags = typeadjust (Expr, &Expr2, 0) & ~CF_CONST;
2566             } else {
2567                 /* OOPS */
2568                 Error ("Invalid operands for binary operator `+'");
2569                 flags = CF_INT;
2570             }
2571
2572             /* Generate code for the add */
2573             g_add (flags, 0);
2574
2575         }
2576
2577         /* Result is a rvalue in primary register */
2578         ED_MakeRValExpr (Expr);
2579     }
2580
2581     /* Condition codes not set */
2582     ED_MarkAsUntested (Expr);
2583
2584 }
2585
2586
2587
2588 static void parsesub (ExprDesc* Expr)
2589 /* Parse an expression with the binary minus operator. Expr contains the
2590 ** unprocessed left hand side of the expression and will contain the
2591 ** result of the expression on return.
2592 */
2593 {
2594     ExprDesc Expr2;
2595     unsigned flags;             /* Operation flags */
2596     Type* lhst;                 /* Type of left hand side */
2597     Type* rhst;                 /* Type of right hand side */
2598     CodeMark Mark1;             /* Save position of output queue */
2599     CodeMark Mark2;             /* Another position in the queue */
2600     int rscale;                 /* Scale factor for the result */
2601
2602
2603     /* Skip the MINUS token */
2604     NextToken ();
2605
2606     /* Get the left hand side type, initialize operation flags */
2607     lhst = Expr->Type;
2608     rscale = 1;                 /* Scale by 1, that is, don't scale */
2609
2610     /* Remember the output queue position, then bring the value onto the stack */
2611     GetCodePos (&Mark1);
2612     LoadExpr (CF_NONE, Expr);  /* --> primary register */
2613     GetCodePos (&Mark2);
2614     g_push (TypeOf (lhst), 0);  /* --> stack */
2615
2616     /* Parse the right hand side */
2617     MarkedExprWithCheck (hie9, &Expr2);
2618
2619     /* Check for a constant rhs expression */
2620     if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
2621
2622         /* The right hand side is constant. Get the rhs type. */
2623         rhst = Expr2.Type;
2624
2625         /* Check left hand side */
2626         if (ED_IsConstAbs (Expr)) {
2627
2628             /* Both sides are constant, remove generated code */
2629             RemoveCode (&Mark1);
2630
2631             /* Check for pointer arithmetic */
2632             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2633                 /* Left is pointer, right is int, must scale rhs */
2634                 Expr->IVal -= Expr2.IVal * CheckedPSizeOf (lhst);
2635                 /* Operate on pointers, result type is a pointer */
2636             } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2637                 /* Left is pointer, right is pointer, must scale result */
2638                 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2639                     Error ("Incompatible pointer types");
2640                 } else {
2641                     Expr->IVal = (Expr->IVal - Expr2.IVal) /
2642                                       CheckedPSizeOf (lhst);
2643                 }
2644                 /* Operate on pointers, result type is an integer */
2645                 Expr->Type = type_int;
2646             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2647                 /* Integer subtraction */
2648                 typeadjust (Expr, &Expr2, 1);
2649                 Expr->IVal -= Expr2.IVal;
2650             } else {
2651                 /* OOPS */
2652                 Error ("Invalid operands for binary operator `-'");
2653             }
2654
2655             /* Result is constant, condition codes not set */
2656             ED_MarkAsUntested (Expr);
2657
2658         } else {
2659
2660             /* Left hand side is not constant, right hand side is.
2661             ** Remove pushed value from stack.
2662             */
2663             RemoveCode (&Mark2);
2664
2665             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2666                 /* Left is pointer, right is int, must scale rhs */
2667                 Expr2.IVal *= CheckedPSizeOf (lhst);
2668                 /* Operate on pointers, result type is a pointer */
2669                 flags = CF_PTR;
2670             } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2671                 /* Left is pointer, right is pointer, must scale result */
2672                 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2673                     Error ("Incompatible pointer types");
2674                 } else {
2675                     rscale = CheckedPSizeOf (lhst);
2676                 }
2677                 /* Operate on pointers, result type is an integer */
2678                 flags = CF_PTR;
2679                 Expr->Type = type_int;
2680             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2681                 /* Integer subtraction */
2682                 flags = typeadjust (Expr, &Expr2, 1);
2683             } else {
2684                 /* OOPS */
2685                 Error ("Invalid operands for binary operator `-'");
2686                 flags = CF_INT;
2687             }
2688
2689             /* Do the subtraction */
2690             g_dec (flags | CF_CONST, Expr2.IVal);
2691
2692             /* If this was a pointer subtraction, we must scale the result */
2693             if (rscale != 1) {
2694                 g_scale (flags, -rscale);
2695             }
2696
2697             /* Result is a rvalue in the primary register */
2698             ED_MakeRValExpr (Expr);
2699             ED_MarkAsUntested (Expr);
2700
2701         }
2702
2703     } else {
2704
2705         /* Not constant, load into the primary */
2706         LoadExpr (CF_NONE, &Expr2);
2707
2708         /* Right hand side is not constant. Get the rhs type. */
2709         rhst = Expr2.Type;
2710
2711         /* Check for pointer arithmetic */
2712         if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2713             /* Left is pointer, right is int, must scale rhs */
2714             g_scale (CF_INT, CheckedPSizeOf (lhst));
2715             /* Operate on pointers, result type is a pointer */
2716             flags = CF_PTR;
2717         } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2718             /* Left is pointer, right is pointer, must scale result */
2719             if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2720                 Error ("Incompatible pointer types");
2721             } else {
2722                 rscale = CheckedPSizeOf (lhst);
2723             }
2724             /* Operate on pointers, result type is an integer */
2725             flags = CF_PTR;
2726             Expr->Type = type_int;
2727         } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2728             /* Integer subtraction. If the left hand side descriptor says that
2729             ** the lhs is const, we have to remove this mark, since this is no
2730             ** longer true, lhs is on stack instead.
2731             */
2732             if (ED_IsLocAbs (Expr)) {
2733                 ED_MakeRValExpr (Expr);
2734             }
2735             /* Adjust operand types */
2736             flags = typeadjust (Expr, &Expr2, 0);
2737         } else {
2738             /* OOPS */
2739             Error ("Invalid operands for binary operator `-'");
2740             flags = CF_INT;
2741         }
2742
2743         /* Generate code for the sub (the & is a hack here) */
2744         g_sub (flags & ~CF_CONST, 0);
2745
2746         /* If this was a pointer subtraction, we must scale the result */
2747         if (rscale != 1) {
2748             g_scale (flags, -rscale);
2749         }
2750
2751         /* Result is a rvalue in the primary register */
2752         ED_MakeRValExpr (Expr);
2753         ED_MarkAsUntested (Expr);
2754     }
2755 }
2756
2757
2758
2759 void hie8 (ExprDesc* Expr)
2760 /* Process + and - binary operators. */
2761 {
2762     ExprWithCheck (hie9, Expr);
2763     while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2764         if (CurTok.Tok == TOK_PLUS) {
2765             parseadd (Expr);
2766         } else {
2767             parsesub (Expr);
2768         }
2769     }
2770 }
2771
2772
2773
2774 static void hie6 (ExprDesc* Expr)
2775 /* Handle greater-than type comparators */
2776 {
2777     static const GenDesc hie6_ops [] = {
2778         { TOK_LT,       GEN_NOPUSH,     g_lt    },
2779         { TOK_LE,       GEN_NOPUSH,     g_le    },
2780         { TOK_GE,       GEN_NOPUSH,     g_ge    },
2781         { TOK_GT,       GEN_NOPUSH,     g_gt    },
2782         { TOK_INVALID,  0,              0       }
2783     };
2784     hie_compare (hie6_ops, Expr, ShiftExpr);
2785 }
2786
2787
2788
2789 static void hie5 (ExprDesc* Expr)
2790 /* Handle == and != */
2791 {
2792     static const GenDesc hie5_ops[] = {
2793         { TOK_EQ,       GEN_NOPUSH,     g_eq    },
2794         { TOK_NE,       GEN_NOPUSH,     g_ne    },
2795         { TOK_INVALID,  0,              0       }
2796     };
2797     hie_compare (hie5_ops, Expr, hie6);
2798 }
2799
2800
2801
2802 static void hie4 (ExprDesc* Expr)
2803 /* Handle & (bitwise and) */
2804 {
2805     static const GenDesc hie4_ops[] = {
2806         { TOK_AND,      GEN_NOPUSH | GEN_COMM,  g_and   },
2807         { TOK_INVALID,  0,                      0       }
2808     };
2809     int UsedGen;
2810
2811     hie_internal (hie4_ops, Expr, hie5, &UsedGen);
2812 }
2813
2814
2815
2816 static void hie3 (ExprDesc* Expr)
2817 /* Handle ^ (bitwise exclusive or) */
2818 {
2819     static const GenDesc hie3_ops[] = {
2820         { TOK_XOR,      GEN_NOPUSH | GEN_COMM,  g_xor   },
2821         { TOK_INVALID,  0,                      0       }
2822     };
2823     int UsedGen;
2824
2825     hie_internal (hie3_ops, Expr, hie4, &UsedGen);
2826 }
2827
2828
2829
2830 static void hie2 (ExprDesc* Expr)
2831 /* Handle | (bitwise or) */
2832 {
2833     static const GenDesc hie2_ops[] = {
2834         { TOK_OR,       GEN_NOPUSH | GEN_COMM,  g_or    },
2835         { TOK_INVALID,  0,                      0       }
2836     };
2837     int UsedGen;
2838
2839     hie_internal (hie2_ops, Expr, hie3, &UsedGen);
2840 }
2841
2842
2843
2844 static void hieAndPP (ExprDesc* Expr)
2845 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2846 ** called recursively from the preprocessor.
2847 */
2848 {
2849     ExprDesc Expr2;
2850
2851     ConstAbsIntExpr (hie2, Expr);
2852     while (CurTok.Tok == TOK_BOOL_AND) {
2853
2854         /* Skip the && */
2855         NextToken ();
2856
2857         /* Get rhs */
2858         ConstAbsIntExpr (hie2, &Expr2);
2859
2860         /* Combine the two */
2861         Expr->IVal = (Expr->IVal && Expr2.IVal);
2862     }
2863 }
2864
2865
2866
2867 static void hieOrPP (ExprDesc *Expr)
2868 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2869 ** called recursively from the preprocessor.
2870 */
2871 {
2872     ExprDesc Expr2;
2873
2874     ConstAbsIntExpr (hieAndPP, Expr);
2875     while (CurTok.Tok == TOK_BOOL_OR) {
2876
2877         /* Skip the && */
2878         NextToken ();
2879
2880         /* Get rhs */
2881         ConstAbsIntExpr (hieAndPP, &Expr2);
2882
2883         /* Combine the two */
2884         Expr->IVal = (Expr->IVal || Expr2.IVal);
2885     }
2886 }
2887
2888
2889
2890 static void hieAnd (ExprDesc* Expr, unsigned TrueLab, int* BoolOp)
2891 /* Process "exp && exp" */
2892 {
2893     int FalseLab;
2894     ExprDesc Expr2;
2895
2896     ExprWithCheck (hie2, Expr);
2897     if (CurTok.Tok == TOK_BOOL_AND) {
2898
2899         /* Tell our caller that we're evaluating a boolean */
2900         *BoolOp = 1;
2901
2902         /* Get a label that we will use for false expressions */
2903         FalseLab = GetLocalLabel ();
2904
2905         /* If the expr hasn't set condition codes, set the force-test flag */
2906         if (!ED_IsTested (Expr)) {
2907             ED_MarkForTest (Expr);
2908         }
2909
2910         /* Load the value */
2911         LoadExpr (CF_FORCECHAR, Expr);
2912
2913         /* Generate the jump */
2914         g_falsejump (CF_NONE, FalseLab);
2915
2916         /* Parse more boolean and's */
2917         while (CurTok.Tok == TOK_BOOL_AND) {
2918
2919             /* Skip the && */
2920             NextToken ();
2921
2922             /* Get rhs */
2923             hie2 (&Expr2);
2924             if (!ED_IsTested (&Expr2)) {
2925                 ED_MarkForTest (&Expr2);
2926             }
2927             LoadExpr (CF_FORCECHAR, &Expr2);
2928
2929             /* Do short circuit evaluation */
2930             if (CurTok.Tok == TOK_BOOL_AND) {
2931                 g_falsejump (CF_NONE, FalseLab);
2932             } else {
2933                 /* Last expression - will evaluate to true */
2934                 g_truejump (CF_NONE, TrueLab);
2935             }
2936         }
2937
2938         /* Define the false jump label here */
2939         g_defcodelabel (FalseLab);
2940
2941         /* The result is an rvalue in primary */
2942         ED_MakeRValExpr (Expr);
2943         ED_TestDone (Expr);     /* Condition codes are set */
2944     }
2945 }
2946
2947
2948
2949 static void hieOr (ExprDesc *Expr)
2950 /* Process "exp || exp". */
2951 {
2952     ExprDesc Expr2;
2953     int BoolOp = 0;             /* Did we have a boolean op? */
2954     int AndOp;                  /* Did we have a && operation? */
2955     unsigned TrueLab;           /* Jump to this label if true */
2956     unsigned DoneLab;
2957
2958     /* Get a label */
2959     TrueLab = GetLocalLabel ();
2960
2961     /* Call the next level parser */
2962     hieAnd (Expr, TrueLab, &BoolOp);
2963
2964     /* Any boolean or's? */
2965     if (CurTok.Tok == TOK_BOOL_OR) {
2966
2967         /* If the expr hasn't set condition codes, set the force-test flag */
2968         if (!ED_IsTested (Expr)) {
2969             ED_MarkForTest (Expr);
2970         }
2971
2972         /* Get first expr */
2973         LoadExpr (CF_FORCECHAR, Expr);
2974
2975         /* For each expression jump to TrueLab if true. Beware: If we
2976         ** had && operators, the jump is already in place!
2977         */
2978         if (!BoolOp) {
2979             g_truejump (CF_NONE, TrueLab);
2980         }
2981
2982         /* Remember that we had a boolean op */
2983         BoolOp = 1;
2984
2985         /* while there's more expr */
2986         while (CurTok.Tok == TOK_BOOL_OR) {
2987
2988             /* skip the || */
2989             NextToken ();
2990
2991             /* Get a subexpr */
2992             AndOp = 0;
2993             hieAnd (&Expr2, TrueLab, &AndOp);
2994             if (!ED_IsTested (&Expr2)) {
2995                 ED_MarkForTest (&Expr2);
2996             }
2997             LoadExpr (CF_FORCECHAR, &Expr2);
2998
2999             /* If there is more to come, add shortcut boolean eval. */
3000             g_truejump (CF_NONE, TrueLab);
3001
3002         }
3003
3004         /* The result is an rvalue in primary */
3005         ED_MakeRValExpr (Expr);
3006         ED_TestDone (Expr);                     /* Condition codes are set */
3007     }
3008
3009     /* If we really had boolean ops, generate the end sequence */
3010     if (BoolOp) {
3011         DoneLab = GetLocalLabel ();
3012         g_getimmed (CF_INT | CF_CONST, 0, 0);   /* Load FALSE */
3013         g_falsejump (CF_NONE, DoneLab);
3014         g_defcodelabel (TrueLab);
3015         g_getimmed (CF_INT | CF_CONST, 1, 0);   /* Load TRUE */
3016         g_defcodelabel (DoneLab);
3017     }
3018 }
3019
3020
3021
3022 static void hieQuest (ExprDesc* Expr)
3023 /* Parse the ternary operator */
3024 {
3025     int         FalseLab;
3026     int         TrueLab;
3027     CodeMark    TrueCodeEnd;
3028     ExprDesc    Expr2;          /* Expression 2 */
3029     ExprDesc    Expr3;          /* Expression 3 */
3030     int         Expr2IsNULL;    /* Expression 2 is a NULL pointer */
3031     int         Expr3IsNULL;    /* Expression 3 is a NULL pointer */
3032     Type*       ResultType;     /* Type of result */
3033
3034
3035     /* Call the lower level eval routine */
3036     if (Preprocessing) {
3037         ExprWithCheck (hieOrPP, Expr);
3038     } else {
3039         ExprWithCheck (hieOr, Expr);
3040     }
3041
3042     /* Check if it's a ternary expression */
3043     if (CurTok.Tok == TOK_QUEST) {
3044         NextToken ();
3045         if (!ED_IsTested (Expr)) {
3046             /* Condition codes not set, request a test */
3047             ED_MarkForTest (Expr);
3048         }
3049         LoadExpr (CF_NONE, Expr);
3050         FalseLab = GetLocalLabel ();
3051         g_falsejump (CF_NONE, FalseLab);
3052
3053         /* Parse second expression. Remember for later if it is a NULL pointer
3054         ** expression, then load it into the primary.
3055         */
3056         ExprWithCheck (hie1, &Expr2);
3057         Expr2IsNULL = ED_IsNullPtr (&Expr2);
3058         if (!IsTypeVoid (Expr2.Type)) {
3059             /* Load it into the primary */
3060             LoadExpr (CF_NONE, &Expr2);
3061             ED_MakeRValExpr (&Expr2);
3062             Expr2.Type = PtrConversion (Expr2.Type);
3063         }
3064
3065         /* Remember the current code position */
3066         GetCodePos (&TrueCodeEnd);
3067
3068         /* Jump around the evaluation of the third expression */
3069         TrueLab = GetLocalLabel ();
3070         ConsumeColon ();
3071         g_jump (TrueLab);
3072
3073         /* Jump here if the first expression was false */
3074         g_defcodelabel (FalseLab);
3075
3076         /* Parse third expression. Remember for later if it is a NULL pointer
3077         ** expression, then load it into the primary.
3078         */
3079         ExprWithCheck (hie1, &Expr3);
3080         Expr3IsNULL = ED_IsNullPtr (&Expr3);
3081         if (!IsTypeVoid (Expr3.Type)) {
3082             /* Load it into the primary */
3083             LoadExpr (CF_NONE, &Expr3);
3084             ED_MakeRValExpr (&Expr3);
3085             Expr3.Type = PtrConversion (Expr3.Type);
3086         }
3087
3088         /* Check if any conversions are needed, if so, do them.
3089         ** Conversion rules for ?: expression are:
3090         **   - if both expressions are int expressions, default promotion
3091         **     rules for ints apply.
3092         **   - if both expressions are pointers of the same type, the
3093         **     result of the expression is of this type.
3094         **   - if one of the expressions is a pointer and the other is
3095         **     a zero constant, the resulting type is that of the pointer
3096         **     type.
3097         **   - if both expressions are void expressions, the result is of
3098         **     type void.
3099         **   - all other cases are flagged by an error.
3100         */
3101         if (IsClassInt (Expr2.Type) && IsClassInt (Expr3.Type)) {
3102
3103             CodeMark    CvtCodeStart;
3104             CodeMark    CvtCodeEnd;
3105
3106
3107             /* Get common type */
3108             ResultType = promoteint (Expr2.Type, Expr3.Type);
3109
3110             /* Convert the third expression to this type if needed */
3111             TypeConversion (&Expr3, ResultType);
3112
3113             /* Emit conversion code for the second expression, but remember
3114             ** where it starts end ends.
3115             */
3116             GetCodePos (&CvtCodeStart);
3117             TypeConversion (&Expr2, ResultType);
3118             GetCodePos (&CvtCodeEnd);
3119
3120             /* If we had conversion code, move it to the right place */
3121             if (!CodeRangeIsEmpty (&CvtCodeStart, &CvtCodeEnd)) {
3122                 MoveCode (&CvtCodeStart, &CvtCodeEnd, &TrueCodeEnd);
3123             }
3124
3125         } else if (IsClassPtr (Expr2.Type) && IsClassPtr (Expr3.Type)) {
3126             /* Must point to same type */
3127             if (TypeCmp (Indirect (Expr2.Type), Indirect (Expr3.Type)) < TC_EQUAL) {
3128                 Error ("Incompatible pointer types");
3129             }
3130             /* Result has the common type */
3131             ResultType = Expr2.Type;
3132         } else if (IsClassPtr (Expr2.Type) && Expr3IsNULL) {
3133             /* Result type is pointer, no cast needed */
3134             ResultType = Expr2.Type;
3135         } else if (Expr2IsNULL && IsClassPtr (Expr3.Type)) {
3136             /* Result type is pointer, no cast needed */
3137             ResultType = Expr3.Type;
3138         } else if (IsTypeVoid (Expr2.Type) && IsTypeVoid (Expr3.Type)) {
3139             /* Result type is void */
3140             ResultType = Expr3.Type;
3141         } else {
3142             Error ("Incompatible types");
3143             ResultType = Expr2.Type;            /* Doesn't matter here */
3144         }
3145
3146         /* Define the final label */
3147         g_defcodelabel (TrueLab);
3148
3149         /* Setup the target expression */
3150         ED_MakeRValExpr (Expr);
3151         Expr->Type  = ResultType;
3152     }
3153 }
3154
3155
3156
3157 static void opeq (const GenDesc* Gen, ExprDesc* Expr, const char* Op)
3158 /* Process "op=" operators. */
3159 {
3160     ExprDesc Expr2;
3161     unsigned flags;
3162     CodeMark Mark;
3163     int MustScale;
3164
3165     /* op= can only be used with lvalues */
3166     if (!ED_IsLVal (Expr)) {
3167         Error ("Invalid lvalue in assignment");
3168         return;
3169     }
3170
3171     /* The left side must not be const qualified */
3172     if (IsQualConst (Expr->Type)) {
3173         Error ("Assignment to const");
3174     }
3175
3176     /* There must be an integer or pointer on the left side */
3177     if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
3178         Error ("Invalid left operand type");
3179         /* Continue. Wrong code will be generated, but the compiler won't
3180         ** break, so this is the best error recovery.
3181         */
3182     }
3183
3184     /* Skip the operator token */
3185     NextToken ();
3186
3187     /* Determine the type of the lhs */
3188     flags = TypeOf (Expr->Type);
3189     MustScale = (Gen->Func == g_add || Gen->Func == g_sub) && IsTypePtr (Expr->Type);
3190
3191     /* Get the lhs address on stack (if needed) */
3192     PushAddr (Expr);
3193
3194     /* Fetch the lhs into the primary register if needed */
3195     LoadExpr (CF_NONE, Expr);
3196
3197     /* Bring the lhs on stack */
3198     GetCodePos (&Mark);
3199     g_push (flags, 0);
3200
3201     /* Evaluate the rhs */
3202     MarkedExprWithCheck (hie1, &Expr2);
3203
3204     /* The rhs must be an integer (or a float, but we don't support that yet */
3205     if (!IsClassInt (Expr2.Type)) {
3206         Error ("Invalid right operand for binary operator `%s'", Op);
3207         /* Continue. Wrong code will be generated, but the compiler won't
3208         ** break, so this is the best error recovery.
3209         */
3210     }
3211
3212     /* Check for a constant expression */
3213     if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
3214         /* The resulting value is a constant. If the generator has the NOPUSH
3215         ** flag set, don't push the lhs.
3216         */
3217         if (Gen->Flags & GEN_NOPUSH) {
3218             RemoveCode (&Mark);
3219         }
3220         if (MustScale) {
3221             /* lhs is a pointer, scale rhs */
3222             Expr2.IVal *= CheckedSizeOf (Expr->Type+1);
3223         }
3224
3225         /* If the lhs is character sized, the operation may be later done
3226         ** with characters.
3227         */
3228         if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
3229             flags |= CF_FORCECHAR;
3230         }
3231
3232         /* Special handling for add and sub - some sort of a hack, but short code */
3233         if (Gen->Func == g_add) {
3234             g_inc (flags | CF_CONST, Expr2.IVal);
3235         } else if (Gen->Func == g_sub) {
3236             g_dec (flags | CF_CONST, Expr2.IVal);
3237         } else {
3238             if (Expr2.IVal == 0) {
3239                 /* Check for div by zero/mod by zero */
3240                 if (Gen->Func == g_div) {
3241                     Error ("Division by zero");
3242                 } else if (Gen->Func == g_mod) {
3243                     Error ("Modulo operation with zero");
3244                 }
3245             }
3246             Gen->Func (flags | CF_CONST, Expr2.IVal);
3247         }
3248     } else {
3249
3250         /* rhs is not constant. Load into the primary */
3251         LoadExpr (CF_NONE, &Expr2);
3252         if (MustScale) {
3253             /* lhs is a pointer, scale rhs */
3254             g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Expr->Type+1));
3255         }
3256
3257         /* If the lhs is character sized, the operation may be later done
3258         ** with characters.
3259         */
3260         if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
3261             flags |= CF_FORCECHAR;
3262         }
3263
3264         /* Adjust the types of the operands if needed */
3265         Gen->Func (g_typeadjust (flags, TypeOf (Expr2.Type)), 0);
3266     }
3267     Store (Expr, 0);
3268     ED_MakeRValExpr (Expr);
3269 }
3270
3271
3272
3273 static void addsubeq (const GenDesc* Gen, ExprDesc *Expr, const char* Op)
3274 /* Process the += and -= operators */
3275 {
3276     ExprDesc Expr2;
3277     unsigned lflags;
3278     unsigned rflags;
3279     int      MustScale;
3280
3281
3282     /* We're currently only able to handle some adressing modes */
3283     if (ED_GetLoc (Expr) == E_LOC_EXPR || ED_GetLoc (Expr) == E_LOC_PRIMARY) {
3284         /* Use generic routine */
3285         opeq (Gen, Expr, Op);
3286         return;
3287     }
3288
3289     /* We must have an lvalue */
3290     if (ED_IsRVal (Expr)) {
3291         Error ("Invalid lvalue in assignment");
3292         return;
3293     }
3294
3295     /* The left side must not be const qualified */
3296     if (IsQualConst (Expr->Type)) {
3297         Error ("Assignment to const");
3298     }
3299
3300     /* There must be an integer or pointer on the left side */
3301     if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
3302         Error ("Invalid left operand type");
3303         /* Continue. Wrong code will be generated, but the compiler won't
3304         ** break, so this is the best error recovery.
3305         */
3306     }
3307
3308     /* Skip the operator */
3309     NextToken ();
3310
3311     /* Check if we have a pointer expression and must scale rhs */
3312     MustScale = IsTypePtr (Expr->Type);
3313
3314     /* Initialize the code generator flags */
3315     lflags = 0;
3316     rflags = 0;
3317
3318     /* Evaluate the rhs. We expect an integer here, since float is not
3319     ** supported
3320     */
3321     hie1 (&Expr2);
3322     if (!IsClassInt (Expr2.Type)) {
3323         Error ("Invalid right operand for binary operator `%s'", Op);
3324         /* Continue. Wrong code will be generated, but the compiler won't
3325         ** break, so this is the best error recovery.
3326         */
3327     }
3328     if (ED_IsConstAbs (&Expr2)) {
3329         /* The resulting value is a constant. Scale it. */
3330         if (MustScale) {
3331             Expr2.IVal *= CheckedSizeOf (Indirect (Expr->Type));
3332         }
3333         rflags |= CF_CONST;
3334         lflags |= CF_CONST;
3335     } else {
3336         /* Not constant, load into the primary */
3337         LoadExpr (CF_NONE, &Expr2);
3338         if (MustScale) {
3339             /* lhs is a pointer, scale rhs */
3340             g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Indirect (Expr->Type)));
3341         }
3342     }
3343
3344     /* Setup the code generator flags */
3345     lflags |= TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR;
3346     rflags |= TypeOf (Expr2.Type) | CF_FORCECHAR;
3347
3348     /* Convert the type of the lhs to that of the rhs */
3349     g_typecast (lflags, rflags);
3350
3351     /* Output apropriate code depending on the location */
3352     switch (ED_GetLoc (Expr)) {
3353
3354         case E_LOC_ABS:
3355             /* Absolute: numeric address or const */
3356             if (Gen->Tok == TOK_PLUS_ASSIGN) {
3357                 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3358             } else {
3359                 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3360             }
3361             break;
3362
3363         case E_LOC_GLOBAL:
3364             /* Global variable */
3365             if (Gen->Tok == TOK_PLUS_ASSIGN) {
3366                 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3367             } else {
3368                 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3369             }
3370             break;
3371
3372         case E_LOC_STATIC:
3373         case E_LOC_LITERAL:
3374             /* Static variable or literal in the literal pool */
3375             if (Gen->Tok == TOK_PLUS_ASSIGN) {
3376                 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3377             } else {
3378                 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3379             }
3380             break;
3381
3382         case E_LOC_REGISTER:
3383             /* Register variable */
3384             if (Gen->Tok == TOK_PLUS_ASSIGN) {
3385                 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3386             } else {
3387                 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3388             }
3389             break;
3390
3391         case E_LOC_STACK:
3392             /* Value on the stack */
3393             if (Gen->Tok == TOK_PLUS_ASSIGN) {
3394                 g_addeqlocal (lflags, Expr->IVal, Expr2.IVal);
3395             } else {
3396                 g_subeqlocal (lflags, Expr->IVal, Expr2.IVal);
3397             }
3398             break;
3399
3400         default:
3401             Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
3402     }
3403
3404     /* Expression is a rvalue in the primary now */
3405     ED_MakeRValExpr (Expr);
3406 }
3407
3408
3409
3410 void hie1 (ExprDesc* Expr)
3411 /* Parse first level of expression hierarchy. */
3412 {
3413     hieQuest (Expr);
3414     switch (CurTok.Tok) {
3415
3416         case TOK_ASSIGN:
3417             Assignment (Expr);
3418             break;
3419
3420         case TOK_PLUS_ASSIGN:
3421             addsubeq (&GenPASGN, Expr, "+=");
3422             break;
3423
3424         case TOK_MINUS_ASSIGN:
3425             addsubeq (&GenSASGN, Expr, "-=");
3426             break;
3427
3428         case TOK_MUL_ASSIGN:
3429             opeq (&GenMASGN, Expr, "*=");
3430             break;
3431
3432         case TOK_DIV_ASSIGN:
3433             opeq (&GenDASGN, Expr, "/=");
3434             break;
3435
3436         case TOK_MOD_ASSIGN:
3437             opeq (&GenMOASGN, Expr, "%=");
3438             break;
3439
3440         case TOK_SHL_ASSIGN:
3441             opeq (&GenSLASGN, Expr, "<<=");
3442             break;
3443
3444         case TOK_SHR_ASSIGN:
3445             opeq (&GenSRASGN, Expr, ">>=");
3446             break;
3447
3448         case TOK_AND_ASSIGN:
3449             opeq (&GenAASGN, Expr, "&=");
3450             break;
3451
3452         case TOK_XOR_ASSIGN:
3453             opeq (&GenXOASGN, Expr, "^=");
3454             break;
3455
3456         case TOK_OR_ASSIGN:
3457             opeq (&GenOASGN, Expr, "|=");
3458             break;
3459
3460         default:
3461             break;
3462     }
3463 }
3464
3465
3466
3467 void hie0 (ExprDesc *Expr)
3468 /* Parse comma operator. */
3469 {
3470     hie1 (Expr);
3471     while (CurTok.Tok == TOK_COMMA) {
3472         NextToken ();
3473         hie1 (Expr);
3474     }
3475 }
3476
3477
3478
3479 int evalexpr (unsigned Flags, void (*Func) (ExprDesc*), ExprDesc* Expr)
3480 /* Will evaluate an expression via the given function. If the result is a
3481 ** constant, 0 is returned and the value is put in the Expr struct. If the
3482 ** result is not constant, LoadExpr is called to bring the value into the
3483 ** primary register and 1 is returned.
3484 */
3485 {
3486     /* Evaluate */
3487     ExprWithCheck (Func, Expr);
3488
3489     /* Check for a constant expression */
3490     if (ED_IsConstAbs (Expr)) {
3491         /* Constant expression */
3492         return 0;
3493     } else {
3494         /* Not constant, load into the primary */
3495         LoadExpr (Flags, Expr);
3496         return 1;
3497     }
3498 }
3499
3500
3501
3502 void Expression0 (ExprDesc* Expr)
3503 /* Evaluate an expression via hie0 and put the result into the primary register */
3504 {
3505     ExprWithCheck (hie0, Expr);
3506     LoadExpr (CF_NONE, Expr);
3507 }
3508
3509
3510
3511 void ConstExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3512 /* Will evaluate an expression via the given function. If the result is not
3513 ** a constant of some sort, a diagnostic will be printed, and the value is
3514 ** replaced by a constant one to make sure there are no internal errors that
3515 ** result from this input error.
3516 */
3517 {
3518     ExprWithCheck (Func, Expr);
3519     if (!ED_IsConst (Expr)) {
3520         Error ("Constant expression expected");
3521         /* To avoid any compiler errors, make the expression a valid const */
3522         ED_MakeConstAbsInt (Expr, 1);
3523     }
3524 }
3525
3526
3527
3528 void BoolExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3529 /* Will evaluate an expression via the given function. If the result is not
3530 ** something that may be evaluated in a boolean context, a diagnostic will be
3531 ** printed, and the value is replaced by a constant one to make sure there
3532 ** are no internal errors that result from this input error.
3533 */
3534 {
3535     ExprWithCheck (Func, Expr);
3536     if (!ED_IsBool (Expr)) {
3537         Error ("Boolean expression expected");
3538         /* To avoid any compiler errors, make the expression a valid int */
3539         ED_MakeConstAbsInt (Expr, 1);
3540     }
3541 }
3542
3543
3544
3545 void ConstAbsIntExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3546 /* Will evaluate an expression via the given function. If the result is not
3547 ** a constant numeric integer value, a diagnostic will be printed, and the
3548 ** value is replaced by a constant one to make sure there are no internal
3549 ** errors that result from this input error.
3550 */
3551 {
3552     ExprWithCheck (Func, Expr);
3553     if (!ED_IsConstAbsInt (Expr)) {
3554         Error ("Constant integer expression expected");
3555         /* To avoid any compiler errors, make the expression a valid const */
3556         ED_MakeConstAbsInt (Expr, 1);
3557     }
3558 }