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