]> git.sur5r.net Git - cc65/blob - src/cc65/expr.c
Removed unneeded include files.
[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 && IS_Get (&WarnConstComparison) != 0) {
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     Type* FinalType;
1109     TypeCode Q;
1110
1111     /* Skip the token and check for an identifier */
1112     NextToken ();
1113     if (CurTok.Tok != TOK_IDENT) {
1114         Error ("Identifier expected");
1115         /* Make the expression an integer at address zero */
1116         ED_MakeConstAbs (Expr, 0, type_int);
1117         return;
1118     }
1119
1120     /* Get the symbol table entry and check for a struct field */
1121     strcpy (Ident, CurTok.Ident);
1122     NextToken ();
1123     Field = FindStructField (Expr->Type, Ident);
1124     if (Field == 0) {
1125         Error ("Struct/union has no field named `%s'", Ident);
1126         /* Make the expression an integer at address zero */
1127         ED_MakeConstAbs (Expr, 0, type_int);
1128         return;
1129     }
1130
1131     /* If we have a struct pointer that is an lvalue and not already in the
1132      * primary, load it now.
1133      */
1134     if (ED_IsLVal (Expr) && IsTypePtr (Expr->Type)) {
1135
1136         /* Load into the primary */
1137         LoadExpr (CF_NONE, Expr);
1138
1139         /* Make it an lvalue expression */
1140         ED_MakeLValExpr (Expr);
1141     }
1142
1143     /* The type is the type of the field plus any qualifiers from the struct */
1144     if (IsClassStruct (Expr->Type)) {
1145         Q = GetQualifier (Expr->Type);
1146     } else {
1147         Q = GetQualifier (Indirect (Expr->Type));
1148     }
1149     if (GetQualifier (Field->Type) == (GetQualifier (Field->Type) | Q)) {
1150         FinalType = Field->Type;
1151     } else {
1152         FinalType = TypeDup (Field->Type);
1153         FinalType->C |= Q;
1154     }
1155
1156     /* A struct is usually an lvalue. If not, it is a struct in the primary
1157      * register.
1158      */
1159     if (ED_IsRVal (Expr) && ED_IsLocExpr (Expr) && !IsTypePtr (Expr->Type)) {
1160
1161         unsigned Flags = 0;
1162         unsigned BitOffs;
1163
1164         /* Get the size of the type */
1165         unsigned Size = SizeOf (Expr->Type);
1166
1167         /* Safety check */
1168         CHECK (Field->V.Offs + Size <= SIZEOF_LONG);
1169
1170         /* The type of the operation depends on the type of the struct */
1171         switch (Size) {
1172             case 1:     Flags = CF_CHAR | CF_UNSIGNED | CF_CONST;       break;
1173             case 2:     Flags = CF_INT  | CF_UNSIGNED | CF_CONST;       break;
1174             case 3:     /* FALLTHROUGH */
1175             case 4:     Flags = CF_LONG | CF_UNSIGNED | CF_CONST;       break;
1176             default:    Internal ("Invalid struct size: %u", Size);     break;
1177         }
1178
1179         /* Generate a shift to get the field in the proper position in the
1180          * primary. For bit fields, mask the value.
1181          */
1182         BitOffs = Field->V.Offs * CHAR_BITS;
1183         if (SymIsBitField (Field)) {
1184             BitOffs += Field->V.B.BitOffs;
1185             g_asr (Flags, BitOffs);
1186             /* Mask the value. This is unnecessary if the shift executed above
1187              * moved only zeroes into the value.
1188              */
1189             if (BitOffs + Field->V.B.BitWidth != Size * CHAR_BITS) {
1190                 g_and (CF_INT | CF_UNSIGNED | CF_CONST,
1191                        (0x0001U << Field->V.B.BitWidth) - 1U);
1192             }
1193         } else {
1194             g_asr (Flags, BitOffs);
1195         }
1196
1197         /* Use the new type */
1198         Expr->Type = FinalType;
1199
1200     } else {
1201
1202         /* Set the struct field offset */
1203         Expr->IVal += Field->V.Offs;
1204
1205         /* Use the new type */
1206         Expr->Type = FinalType;
1207
1208         /* An struct member is actually a variable. So the rules for variables
1209          * with respect to the reference type apply: If it's an array, it is
1210          * a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1211          * but a struct field cannot be a function).
1212          */
1213         if (IsTypeArray (Expr->Type)) {
1214             ED_MakeRVal (Expr);
1215         } else {
1216             ED_MakeLVal (Expr);
1217         }
1218
1219         /* Make the expression a bit field if necessary */
1220         if (SymIsBitField (Field)) {
1221             ED_MakeBitField (Expr, Field->V.B.BitOffs, Field->V.B.BitWidth);
1222         }
1223     }
1224
1225 }
1226
1227
1228
1229 static void hie11 (ExprDesc *Expr)
1230 /* Handle compound types (structs and arrays) */
1231 {
1232     /* Name value used in invalid function calls */
1233     static const char IllegalFunc[] = "illegal_function_call";
1234
1235     /* Evaluate the lhs */
1236     Primary (Expr);
1237
1238     /* Check for a rhs */
1239     while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN ||
1240            CurTok.Tok == TOK_DOT    || CurTok.Tok == TOK_PTR_REF) {
1241
1242         switch (CurTok.Tok) {
1243
1244             case TOK_LBRACK:
1245                 /* Array reference */
1246                 ArrayRef (Expr);
1247                 break;
1248
1249             case TOK_LPAREN:
1250                 /* Function call. */
1251                 if (!IsTypeFunc (Expr->Type) && !IsTypeFuncPtr (Expr->Type)) {
1252                     /* Not a function */
1253                     Error ("Illegal function call");
1254                     /* Force the type to be a implicitly defined function, one
1255                      * returning an int and taking any number of arguments.
1256                      * Since we don't have a name, invent one.
1257                      */
1258                     ED_MakeConstAbs (Expr, 0, GetImplicitFuncType ());
1259                     Expr->Name = (long) IllegalFunc;
1260                 }
1261                 /* Call the function */
1262                 FunctionCall (Expr);
1263                 break;
1264
1265             case TOK_DOT:
1266                 if (!IsClassStruct (Expr->Type)) {
1267                     Error ("Struct expected");
1268                 }
1269                 StructRef (Expr);
1270                 break;
1271
1272             case TOK_PTR_REF:
1273                 /* If we have an array, convert it to pointer to first element */
1274                 if (IsTypeArray (Expr->Type)) {
1275                     Expr->Type = ArrayToPtr (Expr->Type);
1276                 }
1277                 if (!IsClassPtr (Expr->Type) || !IsClassStruct (Indirect (Expr->Type))) {
1278                     Error ("Struct pointer expected");
1279                 }
1280                 StructRef (Expr);
1281                 break;
1282
1283             default:
1284                 Internal ("Invalid token in hie11: %d", CurTok.Tok);
1285
1286         }
1287     }
1288 }
1289
1290
1291
1292 void Store (ExprDesc* Expr, const Type* StoreType)
1293 /* Store the primary register into the location denoted by Expr. If StoreType
1294  * is given, use this type when storing instead of Expr->Type. If StoreType
1295  * is NULL, use Expr->Type instead.
1296  */
1297 {
1298     unsigned Flags;
1299
1300     /* If StoreType was not given, use Expr->Type instead */
1301     if (StoreType == 0) {
1302         StoreType = Expr->Type;
1303     }
1304
1305     /* Prepare the code generator flags */
1306     Flags = TypeOf (StoreType) | GlobalModeFlags (Expr);
1307
1308     /* Do the store depending on the location */
1309     switch (ED_GetLoc (Expr)) {
1310
1311         case E_LOC_ABS:
1312             /* Absolute: numeric address or const */
1313             g_putstatic (Flags, Expr->IVal, 0);
1314             break;
1315
1316         case E_LOC_GLOBAL:
1317             /* Global variable */
1318             g_putstatic (Flags, Expr->Name, Expr->IVal);
1319             break;
1320
1321         case E_LOC_STATIC:
1322         case E_LOC_LITERAL:
1323             /* Static variable or literal in the literal pool */
1324             g_putstatic (Flags, Expr->Name, Expr->IVal);
1325             break;
1326
1327         case E_LOC_REGISTER:
1328             /* Register variable */
1329             g_putstatic (Flags, Expr->Name, Expr->IVal);
1330             break;
1331
1332         case E_LOC_STACK:
1333             /* Value on the stack */
1334             g_putlocal (Flags, Expr->IVal, 0);
1335             break;
1336
1337         case E_LOC_PRIMARY:
1338             /* The primary register (value is already there) */
1339             break;
1340
1341         case E_LOC_EXPR:
1342             /* An expression in the primary register */
1343             g_putind (Flags, Expr->IVal);
1344             break;
1345
1346         default:
1347             Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
1348     }
1349
1350     /* Assume that each one of the stores will invalidate CC */
1351     ED_MarkAsUntested (Expr);
1352 }
1353
1354
1355
1356 static void PreInc (ExprDesc* Expr)
1357 /* Handle the preincrement operators */
1358 {
1359     unsigned Flags;
1360     unsigned long Val;
1361
1362     /* Skip the operator token */
1363     NextToken ();
1364
1365     /* Evaluate the expression and check that it is an lvalue */
1366     hie10 (Expr);
1367     if (!ED_IsLVal (Expr)) {
1368         Error ("Invalid lvalue");
1369         return;
1370     }
1371
1372     /* We cannot modify const values */
1373     if (IsQualConst (Expr->Type)) {
1374         Error ("Increment of read-only variable");
1375     }
1376
1377     /* Get the data type */
1378     Flags = TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR | CF_CONST;
1379
1380     /* Get the increment value in bytes */
1381     Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1382
1383     /* Check the location of the data */
1384     switch (ED_GetLoc (Expr)) {
1385
1386         case E_LOC_ABS:
1387             /* Absolute: numeric address or const */
1388             g_addeqstatic (Flags, Expr->IVal, 0, Val);
1389             break;
1390
1391         case E_LOC_GLOBAL:
1392             /* Global variable */
1393             g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1394             break;
1395
1396         case E_LOC_STATIC:
1397         case E_LOC_LITERAL:
1398             /* Static variable or literal in the literal pool */
1399             g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1400             break;
1401
1402         case E_LOC_REGISTER:
1403             /* Register variable */
1404             g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1405             break;
1406
1407         case E_LOC_STACK:
1408             /* Value on the stack */
1409             g_addeqlocal (Flags, Expr->IVal, Val);
1410             break;
1411
1412         case E_LOC_PRIMARY:
1413             /* The primary register */
1414             g_inc (Flags, Val);
1415             break;
1416
1417         case E_LOC_EXPR:
1418             /* An expression in the primary register */
1419             g_addeqind (Flags, Expr->IVal, Val);
1420             break;
1421
1422         default:
1423             Internal ("Invalid location in PreInc(): 0x%04X", ED_GetLoc (Expr));
1424     }
1425
1426     /* Result is an expression, no reference */
1427     ED_MakeRValExpr (Expr);
1428 }
1429
1430
1431
1432 static void PreDec (ExprDesc* Expr)
1433 /* Handle the predecrement operators */
1434 {
1435     unsigned Flags;
1436     unsigned long Val;
1437
1438     /* Skip the operator token */
1439     NextToken ();
1440
1441     /* Evaluate the expression and check that it is an lvalue */
1442     hie10 (Expr);
1443     if (!ED_IsLVal (Expr)) {
1444         Error ("Invalid lvalue");
1445         return;
1446     }
1447
1448     /* We cannot modify const values */
1449     if (IsQualConst (Expr->Type)) {
1450         Error ("Decrement of read-only variable");
1451     }
1452
1453     /* Get the data type */
1454     Flags = TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR | CF_CONST;
1455
1456     /* Get the increment value in bytes */
1457     Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1458
1459     /* Check the location of the data */
1460     switch (ED_GetLoc (Expr)) {
1461
1462         case E_LOC_ABS:
1463             /* Absolute: numeric address or const */
1464             g_subeqstatic (Flags, Expr->IVal, 0, Val);
1465             break;
1466
1467         case E_LOC_GLOBAL:
1468             /* Global variable */
1469             g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1470             break;
1471
1472         case E_LOC_STATIC:
1473         case E_LOC_LITERAL:
1474             /* Static variable or literal in the literal pool */
1475             g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1476             break;
1477
1478         case E_LOC_REGISTER:
1479             /* Register variable */
1480             g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1481             break;
1482
1483         case E_LOC_STACK:
1484             /* Value on the stack */
1485             g_subeqlocal (Flags, Expr->IVal, Val);
1486             break;
1487
1488         case E_LOC_PRIMARY:
1489             /* The primary register */
1490             g_inc (Flags, Val);
1491             break;
1492
1493         case E_LOC_EXPR:
1494             /* An expression in the primary register */
1495             g_subeqind (Flags, Expr->IVal, Val);
1496             break;
1497
1498         default:
1499             Internal ("Invalid location in PreDec(): 0x%04X", ED_GetLoc (Expr));
1500     }
1501
1502     /* Result is an expression, no reference */
1503     ED_MakeRValExpr (Expr);
1504 }
1505
1506
1507
1508 static void PostInc (ExprDesc* Expr)
1509 /* Handle the postincrement operator */
1510 {
1511     unsigned Flags;
1512
1513     NextToken ();
1514
1515     /* The expression to increment must be an lvalue */
1516     if (!ED_IsLVal (Expr)) {
1517         Error ("Invalid lvalue");
1518         return;
1519     }
1520
1521     /* We cannot modify const values */
1522     if (IsQualConst (Expr->Type)) {
1523         Error ("Increment of read-only variable");
1524     }
1525
1526     /* Get the data type */
1527     Flags = TypeOf (Expr->Type);
1528
1529     /* Push the address if needed */
1530     PushAddr (Expr);
1531
1532     /* Fetch the value and save it (since it's the result of the expression) */
1533     LoadExpr (CF_NONE, Expr);
1534     g_save (Flags | CF_FORCECHAR);
1535
1536     /* If we have a pointer expression, increment by the size of the type */
1537     if (IsTypePtr (Expr->Type)) {
1538         g_inc (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1539     } else {
1540         g_inc (Flags | CF_CONST | CF_FORCECHAR, 1);
1541     }
1542
1543     /* Store the result back */
1544     Store (Expr, 0);
1545
1546     /* Restore the original value in the primary register */
1547     g_restore (Flags | CF_FORCECHAR);
1548
1549     /* The result is always an expression, no reference */
1550     ED_MakeRValExpr (Expr);
1551 }
1552
1553
1554
1555 static void PostDec (ExprDesc* Expr)
1556 /* Handle the postdecrement operator */
1557 {
1558     unsigned Flags;
1559
1560     NextToken ();
1561
1562     /* The expression to increment must be an lvalue */
1563     if (!ED_IsLVal (Expr)) {
1564         Error ("Invalid lvalue");
1565         return;
1566     }
1567
1568     /* We cannot modify const values */
1569     if (IsQualConst (Expr->Type)) {
1570         Error ("Decrement of read-only variable");
1571     }
1572
1573     /* Get the data type */
1574     Flags = TypeOf (Expr->Type);
1575
1576     /* Push the address if needed */
1577     PushAddr (Expr);
1578
1579     /* Fetch the value and save it (since it's the result of the expression) */
1580     LoadExpr (CF_NONE, Expr);
1581     g_save (Flags | CF_FORCECHAR);
1582
1583     /* If we have a pointer expression, increment by the size of the type */
1584     if (IsTypePtr (Expr->Type)) {
1585         g_dec (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1586     } else {
1587         g_dec (Flags | CF_CONST | CF_FORCECHAR, 1);
1588     }
1589
1590     /* Store the result back */
1591     Store (Expr, 0);
1592
1593     /* Restore the original value in the primary register */
1594     g_restore (Flags | CF_FORCECHAR);
1595
1596     /* The result is always an expression, no reference */
1597     ED_MakeRValExpr (Expr);
1598 }
1599
1600
1601
1602 static void UnaryOp (ExprDesc* Expr)
1603 /* Handle unary -/+ and ~ */
1604 {
1605     unsigned Flags;
1606
1607     /* Remember the operator token and skip it */
1608     token_t Tok = CurTok.Tok;
1609     NextToken ();
1610
1611     /* Get the expression */
1612     hie10 (Expr);
1613
1614     /* We can only handle integer types */
1615     if (!IsClassInt (Expr->Type)) {
1616         Error ("Argument must have integer type");
1617         ED_MakeConstAbsInt (Expr, 1);
1618     }
1619
1620     /* Check for a constant expression */
1621     if (ED_IsConstAbs (Expr)) {
1622         /* Value is constant */
1623         switch (Tok) {
1624             case TOK_MINUS: Expr->IVal = -Expr->IVal;   break;
1625             case TOK_PLUS:                              break;
1626             case TOK_COMP:  Expr->IVal = ~Expr->IVal;   break;
1627             default:        Internal ("Unexpected token: %d", Tok);
1628         }
1629     } else {
1630         /* Value is not constant */
1631         LoadExpr (CF_NONE, Expr);
1632
1633         /* Get the type of the expression */
1634         Flags = TypeOf (Expr->Type);
1635
1636         /* Handle the operation */
1637         switch (Tok) {
1638             case TOK_MINUS: g_neg (Flags);  break;
1639             case TOK_PLUS:                  break;
1640             case TOK_COMP:  g_com (Flags);  break;
1641             default:        Internal ("Unexpected token: %d", Tok);
1642         }
1643
1644         /* The result is a rvalue in the primary */
1645         ED_MakeRValExpr (Expr);
1646     }
1647 }
1648
1649
1650
1651 void hie10 (ExprDesc* Expr)
1652 /* Handle ++, --, !, unary - etc. */
1653 {
1654     unsigned long Size;
1655
1656     switch (CurTok.Tok) {
1657
1658         case TOK_INC:
1659             PreInc (Expr);
1660             break;
1661
1662         case TOK_DEC:
1663             PreDec (Expr);
1664             break;
1665
1666         case TOK_PLUS:
1667         case TOK_MINUS:
1668         case TOK_COMP:
1669             UnaryOp (Expr);
1670             break;
1671
1672         case TOK_BOOL_NOT:
1673             NextToken ();
1674             if (evalexpr (CF_NONE, hie10, Expr) == 0) {
1675                 /* Constant expression */
1676                 Expr->IVal = !Expr->IVal;
1677             } else {
1678                 g_bneg (TypeOf (Expr->Type));
1679                 ED_MakeRValExpr (Expr);
1680                 ED_TestDone (Expr);             /* bneg will set cc */
1681             }
1682             break;
1683
1684         case TOK_STAR:
1685             NextToken ();
1686             ExprWithCheck (hie10, Expr);
1687             if (ED_IsLVal (Expr) || !(ED_IsLocConst (Expr) || ED_IsLocStack (Expr))) {
1688                 /* Not a const, load it into the primary and make it a
1689                  * calculated value.
1690                  */
1691                 LoadExpr (CF_NONE, Expr);
1692                 ED_MakeRValExpr (Expr);
1693             }
1694             /* If the expression is already a pointer to function, the
1695              * additional dereferencing operator must be ignored. A function
1696              * itself is represented as "pointer to function", so any number
1697              * of dereference operators is legal, since the result will
1698              * always be converted to "pointer to function".
1699              */
1700             if (IsTypeFuncPtr (Expr->Type) || IsTypeFunc (Expr->Type)) {
1701                 /* Expression not storable */
1702                 ED_MakeRVal (Expr);
1703             } else {
1704                 if (IsClassPtr (Expr->Type)) {
1705                     Expr->Type = Indirect (Expr->Type);
1706                 } else {
1707                     Error ("Illegal indirection");
1708                 }
1709                 /* The * operator yields an lvalue */
1710                 ED_MakeLVal (Expr);
1711             }
1712             break;
1713
1714         case TOK_AND:
1715             NextToken ();
1716             ExprWithCheck (hie10, Expr);
1717             /* The & operator may be applied to any lvalue, and it may be
1718              * applied to functions, even if they're no lvalues.
1719              */
1720             if (ED_IsRVal (Expr) && !IsTypeFunc (Expr->Type) && !IsTypeArray (Expr->Type)) {
1721                 Error ("Illegal address");
1722             } else {
1723                 if (ED_IsBitField (Expr)) {
1724                     Error ("Cannot take address of bit-field");
1725                     /* Do it anyway, just to avoid further warnings */
1726                     Expr->Flags &= ~E_BITFIELD;
1727                 }
1728                 Expr->Type = PointerTo (Expr->Type);
1729                 /* The & operator yields an rvalue */
1730                 ED_MakeRVal (Expr);
1731             }
1732             break;
1733
1734         case TOK_SIZEOF:
1735             NextToken ();
1736             if (TypeSpecAhead ()) {
1737                 Type T[MAXTYPELEN];
1738                 NextToken ();
1739                 Size = CheckedSizeOf (ParseType (T));
1740                 ConsumeRParen ();
1741             } else {
1742                 /* Remember the output queue pointer */
1743                 CodeMark Mark;
1744                 GetCodePos (&Mark);
1745                 hie10 (Expr);
1746                 Size = CheckedSizeOf (Expr->Type);
1747                 /* Remove any generated code */
1748                 RemoveCode (&Mark);
1749             }
1750             ED_MakeConstAbs (Expr, Size, type_size_t);
1751             ED_MarkAsUntested (Expr);
1752             break;
1753
1754         default:
1755             if (TypeSpecAhead ()) {
1756
1757                 /* A typecast */
1758                 TypeCast (Expr);
1759
1760             } else {
1761
1762                 /* An expression */
1763                 hie11 (Expr);
1764
1765                 /* Handle post increment */
1766                 switch (CurTok.Tok) {
1767                     case TOK_INC:   PostInc (Expr); break;
1768                     case TOK_DEC:   PostDec (Expr); break;
1769                     default:                        break;
1770                 }
1771
1772             }
1773             break;
1774     }
1775 }
1776
1777
1778
1779 static void hie_internal (const GenDesc* Ops,   /* List of generators */
1780                           ExprDesc* Expr,
1781                           void (*hienext) (ExprDesc*),
1782                           int* UsedGen)
1783 /* Helper function */
1784 {
1785     ExprDesc Expr2;
1786     CodeMark Mark1;
1787     CodeMark Mark2;
1788     const GenDesc* Gen;
1789     token_t Tok;                        /* The operator token */
1790     unsigned ltype, type;
1791     int rconst;                         /* Operand is a constant */
1792
1793
1794     hienext (Expr);
1795
1796     *UsedGen = 0;
1797     while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1798
1799         /* Tell the caller that we handled it's ops */
1800         *UsedGen = 1;
1801
1802         /* All operators that call this function expect an int on the lhs */
1803         if (!IsClassInt (Expr->Type)) {
1804             Error ("Integer expression expected");
1805             /* To avoid further errors, make Expr a valid int expression */
1806             ED_MakeConstAbsInt (Expr, 1);
1807         }
1808
1809         /* Remember the operator token, then skip it */
1810         Tok = CurTok.Tok;
1811         NextToken ();
1812
1813         /* Get the lhs on stack */
1814         GetCodePos (&Mark1);
1815         ltype = TypeOf (Expr->Type);
1816         if (ED_IsConstAbs (Expr)) {
1817             /* Constant value */
1818             GetCodePos (&Mark2);
1819             g_push (ltype | CF_CONST, Expr->IVal);
1820         } else {
1821             /* Value not constant */
1822             LoadExpr (CF_NONE, Expr);
1823             GetCodePos (&Mark2);
1824             g_push (ltype, 0);
1825         }
1826
1827         /* Get the right hand side */
1828         MarkedExprWithCheck (hienext, &Expr2);
1829
1830         /* Check for a constant expression */
1831         rconst = (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2));
1832         if (!rconst) {
1833             /* Not constant, load into the primary */
1834             LoadExpr (CF_NONE, &Expr2);
1835         }
1836
1837         /* Check the type of the rhs */
1838         if (!IsClassInt (Expr2.Type)) {
1839             Error ("Integer expression expected");
1840         }
1841
1842         /* Check for const operands */
1843         if (ED_IsConstAbs (Expr) && rconst) {
1844
1845             /* Both operands are constant, remove the generated code */
1846             RemoveCode (&Mark1);
1847
1848             /* Get the type of the result */
1849             Expr->Type = promoteint (Expr->Type, Expr2.Type);
1850
1851             /* Handle the op differently for signed and unsigned types */
1852             if (IsSignSigned (Expr->Type)) {
1853
1854                 /* Evaluate the result for signed operands */
1855                 signed long Val1 = Expr->IVal;
1856                 signed long Val2 = Expr2.IVal;
1857                 switch (Tok) {
1858                     case TOK_OR:
1859                         Expr->IVal = (Val1 | Val2);
1860                         break;
1861                     case TOK_XOR:
1862                         Expr->IVal = (Val1 ^ Val2);
1863                         break;
1864                     case TOK_AND:
1865                         Expr->IVal = (Val1 & Val2);
1866                         break;
1867                     case TOK_STAR:
1868                         Expr->IVal = (Val1 * Val2);
1869                         break;
1870                     case TOK_DIV:
1871                         if (Val2 == 0) {
1872                             Error ("Division by zero");
1873                             Expr->IVal = 0x7FFFFFFF;
1874                         } else {
1875                             Expr->IVal = (Val1 / Val2);
1876                         }
1877                         break;
1878                     case TOK_MOD:
1879                         if (Val2 == 0) {
1880                             Error ("Modulo operation with zero");
1881                             Expr->IVal = 0;
1882                         } else {
1883                             Expr->IVal = (Val1 % Val2);
1884                         }
1885                         break;
1886                     default:
1887                         Internal ("hie_internal: got token 0x%X\n", Tok);
1888                 }
1889             } else {
1890
1891                 /* Evaluate the result for unsigned operands */
1892                 unsigned long Val1 = Expr->IVal;
1893                 unsigned long Val2 = Expr2.IVal;
1894                 switch (Tok) {
1895                     case TOK_OR:
1896                         Expr->IVal = (Val1 | Val2);
1897                         break;
1898                     case TOK_XOR:
1899                         Expr->IVal = (Val1 ^ Val2);
1900                         break;
1901                     case TOK_AND:
1902                         Expr->IVal = (Val1 & Val2);
1903                         break;
1904                     case TOK_STAR:
1905                         Expr->IVal = (Val1 * Val2);
1906                         break;
1907                     case TOK_DIV:
1908                         if (Val2 == 0) {
1909                             Error ("Division by zero");
1910                             Expr->IVal = 0xFFFFFFFF;
1911                         } else {
1912                             Expr->IVal = (Val1 / Val2);
1913                         }
1914                         break;
1915                     case TOK_MOD:
1916                         if (Val2 == 0) {
1917                             Error ("Modulo operation with zero");
1918                             Expr->IVal = 0;
1919                         } else {
1920                             Expr->IVal = (Val1 % Val2);
1921                         }
1922                         break;
1923                     default:
1924                         Internal ("hie_internal: got token 0x%X\n", Tok);
1925                 }
1926             }
1927
1928         } else {
1929
1930             /* If the right hand side is constant, and the generator function
1931              * expects the lhs in the primary, remove the push of the primary
1932              * now.
1933              */
1934             unsigned rtype = TypeOf (Expr2.Type);
1935             type = 0;
1936             if (rconst) {
1937                 /* Second value is constant - check for div */
1938                 type |= CF_CONST;
1939                 rtype |= CF_CONST;
1940                 if (Tok == TOK_DIV && Expr2.IVal == 0) {
1941                     Error ("Division by zero");
1942                 } else if (Tok == TOK_MOD && Expr2.IVal == 0) {
1943                     Error ("Modulo operation with zero");
1944                 }
1945                 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1946                     RemoveCode (&Mark2);
1947                     ltype |= CF_REG;    /* Value is in register */
1948                 }
1949             }
1950
1951             /* Determine the type of the operation result. */
1952             type |= g_typeadjust (ltype, rtype);
1953             Expr->Type = promoteint (Expr->Type, Expr2.Type);
1954
1955             /* Generate code */
1956             Gen->Func (type, Expr2.IVal);
1957
1958             /* We have a rvalue in the primary now */
1959             ED_MakeRValExpr (Expr);
1960         }
1961     }
1962 }
1963
1964
1965
1966 static void hie_compare (const GenDesc* Ops,    /* List of generators */
1967                          ExprDesc* Expr,
1968                          void (*hienext) (ExprDesc*))
1969 /* Helper function for the compare operators */
1970 {
1971     ExprDesc Expr2;
1972     CodeMark Mark0;
1973     CodeMark Mark1;
1974     CodeMark Mark2;
1975     const GenDesc* Gen;
1976     token_t Tok;                        /* The operator token */
1977     unsigned ltype;
1978     int rconst;                         /* Operand is a constant */
1979
1980
1981     GetCodePos (&Mark0);
1982     hienext (Expr);
1983
1984     while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1985
1986         /* Remember the generator function */
1987         void (*GenFunc) (unsigned, unsigned long) = Gen->Func;
1988
1989         /* Remember the operator token, then skip it */
1990         Tok = CurTok.Tok;
1991         NextToken ();
1992
1993         /* Get the lhs on stack */
1994         GetCodePos (&Mark1);
1995         ltype = TypeOf (Expr->Type);
1996         if (ED_IsConstAbs (Expr)) {
1997             /* Constant value */
1998             GetCodePos (&Mark2);
1999             g_push (ltype | CF_CONST, Expr->IVal);
2000         } else {
2001             /* Value not constant */
2002             LoadExpr (CF_NONE, Expr);
2003             GetCodePos (&Mark2);
2004             g_push (ltype, 0);
2005         }
2006
2007         /* Get the right hand side */
2008         MarkedExprWithCheck (hienext, &Expr2);
2009
2010         /* Check for a constant expression */
2011         rconst = (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2));
2012         if (!rconst) {
2013             /* Not constant, load into the primary */
2014             LoadExpr (CF_NONE, &Expr2);
2015         }
2016
2017         /* Make sure, the types are compatible */
2018         if (IsClassInt (Expr->Type)) {
2019             if (!IsClassInt (Expr2.Type) && !(IsClassPtr(Expr2.Type) && ED_IsNullPtr(Expr))) {
2020                 Error ("Incompatible types");
2021             }
2022         } else if (IsClassPtr (Expr->Type)) {
2023             if (IsClassPtr (Expr2.Type)) {
2024                 /* Both pointers are allowed in comparison if they point to
2025                  * the same type, or if one of them is a void pointer.
2026                  */
2027                 Type* left  = Indirect (Expr->Type);
2028                 Type* right = Indirect (Expr2.Type);
2029                 if (TypeCmp (left, right) < TC_EQUAL && left->C != T_VOID && right->C != T_VOID) {
2030                     /* Incomatible pointers */
2031                     Error ("Incompatible types");
2032                 }
2033             } else if (!ED_IsNullPtr (&Expr2)) {
2034                 Error ("Incompatible types");
2035             }
2036         }
2037
2038         /* Check for const operands */
2039         if (ED_IsConstAbs (Expr) && rconst) {
2040
2041             /* If the result is constant, this is suspicious when not in
2042              * preprocessor mode.
2043              */
2044             WarnConstCompareResult ();
2045
2046             /* Both operands are constant, remove the generated code */
2047             RemoveCode (&Mark1);
2048
2049             /* Determine if this is a signed or unsigned compare */
2050             if (IsClassInt (Expr->Type) && IsSignSigned (Expr->Type) &&
2051                 IsClassInt (Expr2.Type) && IsSignSigned (Expr2.Type)) {
2052
2053                 /* Evaluate the result for signed operands */
2054                 signed long Val1 = Expr->IVal;
2055                 signed long Val2 = Expr2.IVal;
2056                 switch (Tok) {
2057                     case TOK_EQ: Expr->IVal = (Val1 == Val2);   break;
2058                     case TOK_NE: Expr->IVal = (Val1 != Val2);   break;
2059                     case TOK_LT: Expr->IVal = (Val1 < Val2);    break;
2060                     case TOK_LE: Expr->IVal = (Val1 <= Val2);   break;
2061                     case TOK_GE: Expr->IVal = (Val1 >= Val2);   break;
2062                     case TOK_GT: Expr->IVal = (Val1 > Val2);    break;
2063                     default:     Internal ("hie_compare: got token 0x%X\n", Tok);
2064                 }
2065
2066             } else {
2067
2068                 /* Evaluate the result for unsigned operands */
2069                 unsigned long Val1 = Expr->IVal;
2070                 unsigned long Val2 = Expr2.IVal;
2071                 switch (Tok) {
2072                     case TOK_EQ: Expr->IVal = (Val1 == Val2);   break;
2073                     case TOK_NE: Expr->IVal = (Val1 != Val2);   break;
2074                     case TOK_LT: Expr->IVal = (Val1 < Val2);    break;
2075                     case TOK_LE: Expr->IVal = (Val1 <= Val2);   break;
2076                     case TOK_GE: Expr->IVal = (Val1 >= Val2);   break;
2077                     case TOK_GT: Expr->IVal = (Val1 > Val2);    break;
2078                     default:     Internal ("hie_compare: got token 0x%X\n", Tok);
2079                 }
2080             }
2081
2082         } else {
2083
2084             /* Determine the signedness of the operands */
2085             int LeftSigned  = IsSignSigned (Expr->Type);
2086             int RightSigned = IsSignSigned (Expr2.Type);
2087
2088             /* If the right hand side is constant, and the generator function
2089              * expects the lhs in the primary, remove the push of the primary
2090              * now.
2091              */
2092             unsigned flags = 0;
2093             if (rconst) {
2094                 flags |= CF_CONST;
2095                 if ((Gen->Flags & GEN_NOPUSH) != 0) {
2096                     RemoveCode (&Mark2);
2097                     ltype |= CF_REG;    /* Value is in register */
2098                 }
2099             }
2100
2101             /* Determine the type of the operation. */
2102             if (IsTypeChar (Expr->Type) && rconst) {
2103
2104                 /* Left side is unsigned char, right side is constant.
2105                  * Determine the minimum and maximum values
2106                  */
2107                 int LeftMin, LeftMax;
2108                 if (LeftSigned) {
2109                     LeftMin = -128;
2110                     LeftMax = 127;
2111                 } else {
2112                     LeftMin = 0;
2113                     LeftMax = 255;
2114                 }
2115                 /* An integer value is always represented as a signed in the
2116                  * ExprDesc structure. This may lead to false results below,
2117                  * if it is actually unsigned, but interpreted as signed
2118                  * because of the representation. Fortunately, in this case,
2119                  * the actual value doesn't matter, since it's always greater
2120                  * than what can be represented in a char. So correct the
2121                  * value accordingly.
2122                  */
2123                 if (!RightSigned && Expr2.IVal < 0) {
2124                     /* Correct the value so it is an unsigned. It will then
2125                      * anyway match one of the cases below.
2126                      */
2127                     Expr2.IVal = LeftMax + 1;
2128                 }
2129
2130                 /* Comparing a char against a constant may have a constant
2131                  * result. Please note: It is not possible to remove the code
2132                  * for the compare alltogether, because it may have side
2133                  * effects.
2134                  */
2135                 switch (Tok) {
2136
2137                     case TOK_EQ:
2138                         if (Expr2.IVal < LeftMin || Expr2.IVal > LeftMax) {
2139                             ED_MakeConstAbsInt (Expr, 0);
2140                             WarnConstCompareResult ();
2141                             goto Done;
2142                         }
2143                         break;
2144
2145                     case TOK_NE:
2146                         if (Expr2.IVal < LeftMin || Expr2.IVal > LeftMax) {
2147                             ED_MakeConstAbsInt (Expr, 1);
2148                             WarnConstCompareResult ();
2149                             goto Done;
2150                         }
2151                         break;
2152
2153                     case TOK_LT:
2154                         if (Expr2.IVal <= LeftMin || Expr2.IVal > LeftMax) {
2155                             ED_MakeConstAbsInt (Expr, Expr2.IVal > LeftMax);
2156                             WarnConstCompareResult ();
2157                             goto Done;
2158                         }
2159                         break;
2160
2161                     case TOK_LE:
2162                         if (Expr2.IVal < LeftMin || Expr2.IVal >= LeftMax) {
2163                             ED_MakeConstAbsInt (Expr, Expr2.IVal >= LeftMax);
2164                             WarnConstCompareResult ();
2165                             goto Done;
2166                         }
2167                         break;
2168
2169                     case TOK_GE:
2170                         if (Expr2.IVal <= LeftMin || Expr2.IVal > LeftMax) {
2171                             ED_MakeConstAbsInt (Expr, Expr2.IVal <= LeftMin);
2172                             WarnConstCompareResult ();
2173                             goto Done;
2174                         }
2175                         break;
2176
2177                     case TOK_GT:
2178                         if (Expr2.IVal < LeftMin || Expr2.IVal >= LeftMax) {
2179                             ED_MakeConstAbsInt (Expr, Expr2.IVal < LeftMin);
2180                             WarnConstCompareResult ();
2181                             goto Done;
2182                         }
2183                         break;
2184
2185                     default:
2186                         Internal ("hie_compare: got token 0x%X\n", Tok);
2187                 }
2188
2189                 /* If the result is not already constant (as evaluated in the
2190                  * switch above), we can execute the operation as a char op,
2191                  * since the right side constant is in a valid range.
2192                  */
2193                 flags |= (CF_CHAR | CF_FORCECHAR);
2194                 if (!LeftSigned) {
2195                     flags |= CF_UNSIGNED;
2196                 }
2197
2198             } else if (IsTypeChar (Expr->Type) && IsTypeChar (Expr2.Type) &&
2199                 GetSignedness (Expr->Type) == GetSignedness (Expr2.Type)) {
2200
2201                 /* Both are chars with the same signedness. We can encode the
2202                  * operation as a char operation.
2203                  */
2204                 flags |= CF_CHAR;
2205                 if (rconst) {
2206                     flags |= CF_FORCECHAR;
2207                 }
2208                 if (!LeftSigned) {
2209                     flags |= CF_UNSIGNED;
2210                 }
2211             } else {
2212                 unsigned rtype = TypeOf (Expr2.Type) | (flags & CF_CONST);
2213                 flags |= g_typeadjust (ltype, rtype);
2214             }
2215
2216             /* If the left side is an unsigned and the right is a constant,
2217              * we may be able to change the compares to something more
2218              * effective.
2219              */
2220             if (!LeftSigned && rconst) {
2221
2222                 switch (Tok) {
2223
2224                     case TOK_LT:
2225                         if (Expr2.IVal == 1) {
2226                             /* An unsigned compare to one means that the value
2227                              * must be zero.
2228                              */
2229                             GenFunc = g_eq;
2230                             Expr2.IVal = 0;
2231                         }
2232                         break;
2233
2234                     case TOK_LE:
2235                         if (Expr2.IVal == 0) {
2236                             /* An unsigned compare to zero means that the value
2237                              * must be zero.
2238                              */
2239                             GenFunc = g_eq;
2240                         }
2241                         break;
2242
2243                     case TOK_GE:
2244                         if (Expr2.IVal == 1) {
2245                             /* An unsigned compare to one means that the value
2246                              * must not be zero.
2247                              */
2248                             GenFunc = g_ne;
2249                             Expr2.IVal = 0;
2250                         }
2251                         break;
2252
2253                     case TOK_GT:
2254                         if (Expr2.IVal == 0) {
2255                             /* An unsigned compare to zero means that the value
2256                              * must not be zero.
2257                              */
2258                             GenFunc = g_ne;
2259                         }
2260                         break;
2261
2262                     default:
2263                         break;
2264
2265                 }
2266
2267             }
2268
2269             /* Generate code */
2270             GenFunc (flags, Expr2.IVal);
2271
2272             /* The result is an rvalue in the primary */
2273             ED_MakeRValExpr (Expr);
2274         }
2275
2276         /* Result type is always int */
2277         Expr->Type = type_int;
2278
2279 Done:   /* Condition codes are set */
2280         ED_TestDone (Expr);
2281     }
2282 }
2283
2284
2285
2286 static void hie9 (ExprDesc *Expr)
2287 /* Process * and / operators. */
2288 {
2289     static const GenDesc hie9_ops[] = {
2290         { TOK_STAR,     GEN_NOPUSH,     g_mul   },
2291         { TOK_DIV,      GEN_NOPUSH,     g_div   },
2292         { TOK_MOD,      GEN_NOPUSH,     g_mod   },
2293         { TOK_INVALID,  0,              0       }
2294     };
2295     int UsedGen;
2296
2297     hie_internal (hie9_ops, Expr, hie10, &UsedGen);
2298 }
2299
2300
2301
2302 static void parseadd (ExprDesc* Expr)
2303 /* Parse an expression with the binary plus operator. Expr contains the
2304  * unprocessed left hand side of the expression and will contain the
2305  * result of the expression on return.
2306  */
2307 {
2308     ExprDesc Expr2;
2309     unsigned flags;             /* Operation flags */
2310     CodeMark Mark;              /* Remember code position */
2311     Type* lhst;                 /* Type of left hand side */
2312     Type* rhst;                 /* Type of right hand side */
2313
2314
2315     /* Skip the PLUS token */
2316     NextToken ();
2317
2318     /* Get the left hand side type, initialize operation flags */
2319     lhst = Expr->Type;
2320     flags = 0;
2321
2322     /* Check for constness on both sides */
2323     if (ED_IsConst (Expr)) {
2324
2325         /* The left hand side is a constant of some sort. Good. Get rhs */
2326         hie9 (&Expr2);
2327         if (ED_IsConstAbs (&Expr2)) {
2328
2329             /* Right hand side is a constant numeric value. Get the rhs type */
2330             rhst = Expr2.Type;
2331
2332             /* Both expressions are constants. Check for pointer arithmetic */
2333             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2334                 /* Left is pointer, right is int, must scale rhs */
2335                 Expr->IVal += Expr2.IVal * CheckedPSizeOf (lhst);
2336                 /* Result type is a pointer */
2337             } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2338                 /* Left is int, right is pointer, must scale lhs */
2339                 Expr->IVal = Expr->IVal * CheckedPSizeOf (rhst) + Expr2.IVal;
2340                 /* Result type is a pointer */
2341                 Expr->Type = Expr2.Type;
2342             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2343                 /* Integer addition */
2344                 Expr->IVal += Expr2.IVal;
2345                 typeadjust (Expr, &Expr2, 1);
2346             } else {
2347                 /* OOPS */
2348                 Error ("Invalid operands for binary operator `+'");
2349             }
2350
2351         } else {
2352
2353             /* lhs is a constant and rhs is not constant. Load rhs into
2354              * the primary.
2355              */
2356             LoadExpr (CF_NONE, &Expr2);
2357
2358             /* Beware: The check above (for lhs) lets not only pass numeric
2359              * constants, but also constant addresses (labels), maybe even
2360              * with an offset. We have to check for that here.
2361              */
2362
2363             /* First, get the rhs type. */
2364             rhst = Expr2.Type;
2365
2366             /* Setup flags */
2367             if (ED_IsLocAbs (Expr)) {
2368                 /* A numerical constant */
2369                 flags |= CF_CONST;
2370             } else {
2371                 /* Constant address label */
2372                 flags |= GlobalModeFlags (Expr) | CF_CONSTADDR;
2373             }
2374
2375             /* Check for pointer arithmetic */
2376             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2377                 /* Left is pointer, right is int, must scale rhs */
2378                 g_scale (CF_INT, CheckedPSizeOf (lhst));
2379                 /* Operate on pointers, result type is a pointer */
2380                 flags |= CF_PTR;
2381                 /* Generate the code for the add */
2382                 if (ED_GetLoc (Expr) == E_LOC_ABS) {
2383                     /* Numeric constant */
2384                     g_inc (flags, Expr->IVal);
2385                 } else {
2386                     /* Constant address */
2387                     g_addaddr_static (flags, Expr->Name, Expr->IVal);
2388                 }
2389             } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2390
2391                 /* Left is int, right is pointer, must scale lhs. */
2392                 unsigned ScaleFactor = CheckedPSizeOf (rhst);
2393
2394                 /* Operate on pointers, result type is a pointer */
2395                 flags |= CF_PTR;
2396                 Expr->Type = Expr2.Type;
2397
2398                 /* Since we do already have rhs in the primary, if lhs is
2399                  * not a numeric constant, and the scale factor is not one
2400                  * (no scaling), we must take the long way over the stack.
2401                  */
2402                 if (ED_IsLocAbs (Expr)) {
2403                     /* Numeric constant, scale lhs */
2404                     Expr->IVal *= ScaleFactor;
2405                     /* Generate the code for the add */
2406                     g_inc (flags, Expr->IVal);
2407                 } else if (ScaleFactor == 1) {
2408                     /* Constant address but no need to scale */
2409                     g_addaddr_static (flags, Expr->Name, Expr->IVal);
2410                 } else {
2411                     /* Constant address that must be scaled */
2412                     g_push (TypeOf (Expr2.Type), 0);    /* rhs --> stack */
2413                     g_getimmed (flags, Expr->Name, Expr->IVal);
2414                     g_scale (CF_PTR, ScaleFactor);
2415                     g_add (CF_PTR, 0);
2416                 }
2417             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2418                 /* Integer addition */
2419                 flags |= typeadjust (Expr, &Expr2, 1);
2420                 /* Generate the code for the add */
2421                 if (ED_IsLocAbs (Expr)) {
2422                     /* Numeric constant */
2423                     g_inc (flags, Expr->IVal);
2424                 } else {
2425                     /* Constant address */
2426                     g_addaddr_static (flags, Expr->Name, Expr->IVal);
2427                 }
2428             } else {
2429                 /* OOPS */
2430                 Error ("Invalid operands for binary operator `+'");
2431                 flags = CF_INT;
2432             }
2433
2434             /* Result is a rvalue in primary register */
2435             ED_MakeRValExpr (Expr);
2436         }
2437
2438     } else {
2439
2440         /* Left hand side is not constant. Get the value onto the stack. */
2441         LoadExpr (CF_NONE, Expr);              /* --> primary register */
2442         GetCodePos (&Mark);
2443         g_push (TypeOf (Expr->Type), 0);        /* --> stack */
2444
2445         /* Evaluate the rhs */
2446         MarkedExprWithCheck (hie9, &Expr2);
2447
2448         /* Check for a constant rhs expression */
2449         if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
2450
2451             /* Right hand side is a constant. Get the rhs type */
2452             rhst = Expr2.Type;
2453
2454             /* Remove pushed value from stack */
2455             RemoveCode (&Mark);
2456
2457             /* Check for pointer arithmetic */
2458             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2459                 /* Left is pointer, right is int, must scale rhs */
2460                 Expr2.IVal *= CheckedPSizeOf (lhst);
2461                 /* Operate on pointers, result type is a pointer */
2462                 flags = CF_PTR;
2463             } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2464                 /* Left is int, right is pointer, must scale lhs (ptr only) */
2465                 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2466                 /* Operate on pointers, result type is a pointer */
2467                 flags = CF_PTR;
2468                 Expr->Type = Expr2.Type;
2469             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2470                 /* Integer addition */
2471                 flags = typeadjust (Expr, &Expr2, 1);
2472             } else {
2473                 /* OOPS */
2474                 Error ("Invalid operands for binary operator `+'");
2475                 flags = CF_INT;
2476             }
2477
2478             /* Generate code for the add */
2479             g_inc (flags | CF_CONST, Expr2.IVal);
2480
2481         } else {
2482
2483             /* Not constant, load into the primary */
2484             LoadExpr (CF_NONE, &Expr2);
2485
2486             /* lhs and rhs are not constant. Get the rhs type. */
2487             rhst = Expr2.Type;
2488
2489             /* Check for pointer arithmetic */
2490             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2491                 /* Left is pointer, right is int, must scale rhs */
2492                 g_scale (CF_INT, CheckedPSizeOf (lhst));
2493                 /* Operate on pointers, result type is a pointer */
2494                 flags = CF_PTR;
2495             } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2496                 /* Left is int, right is pointer, must scale lhs */
2497                 g_tosint (TypeOf (rhst));       /* Make sure, TOS is int */
2498                 g_swap (CF_INT);                /* Swap TOS and primary */
2499                 g_scale (CF_INT, CheckedPSizeOf (rhst));
2500                 /* Operate on pointers, result type is a pointer */
2501                 flags = CF_PTR;
2502                 Expr->Type = Expr2.Type;
2503             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2504                 /* Integer addition. Note: Result is never constant.
2505                  * Problem here is that typeadjust does not know if the
2506                  * variable is an rvalue or lvalue, so if both operands
2507                  * are dereferenced constant numeric addresses, typeadjust
2508                  * thinks the operation works on constants. Removing
2509                  * CF_CONST here means handling the symptoms, however, the
2510                  * whole parser is such a mess that I fear to break anything
2511                  * when trying to apply another solution.
2512                  */
2513                 flags = typeadjust (Expr, &Expr2, 0) & ~CF_CONST;
2514             } else {
2515                 /* OOPS */
2516                 Error ("Invalid operands for binary operator `+'");
2517                 flags = CF_INT;
2518             }
2519
2520             /* Generate code for the add */
2521             g_add (flags, 0);
2522
2523         }
2524
2525         /* Result is a rvalue in primary register */
2526         ED_MakeRValExpr (Expr);
2527     }
2528
2529     /* Condition codes not set */
2530     ED_MarkAsUntested (Expr);
2531
2532 }
2533
2534
2535
2536 static void parsesub (ExprDesc* Expr)
2537 /* Parse an expression with the binary minus operator. Expr contains the
2538  * unprocessed left hand side of the expression and will contain the
2539  * result of the expression on return.
2540  */
2541 {
2542     ExprDesc Expr2;
2543     unsigned flags;             /* Operation flags */
2544     Type* lhst;                 /* Type of left hand side */
2545     Type* rhst;                 /* Type of right hand side */
2546     CodeMark Mark1;             /* Save position of output queue */
2547     CodeMark Mark2;             /* Another position in the queue */
2548     int rscale;                 /* Scale factor for the result */
2549
2550
2551     /* Skip the MINUS token */
2552     NextToken ();
2553
2554     /* Get the left hand side type, initialize operation flags */
2555     lhst = Expr->Type;
2556     rscale = 1;                 /* Scale by 1, that is, don't scale */
2557
2558     /* Remember the output queue position, then bring the value onto the stack */
2559     GetCodePos (&Mark1);
2560     LoadExpr (CF_NONE, Expr);  /* --> primary register */
2561     GetCodePos (&Mark2);
2562     g_push (TypeOf (lhst), 0);  /* --> stack */
2563
2564     /* Parse the right hand side */
2565     MarkedExprWithCheck (hie9, &Expr2);
2566
2567     /* Check for a constant rhs expression */
2568     if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
2569
2570         /* The right hand side is constant. Get the rhs type. */
2571         rhst = Expr2.Type;
2572
2573         /* Check left hand side */
2574         if (ED_IsConstAbs (Expr)) {
2575
2576             /* Both sides are constant, remove generated code */
2577             RemoveCode (&Mark1);
2578
2579             /* Check for pointer arithmetic */
2580             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2581                 /* Left is pointer, right is int, must scale rhs */
2582                 Expr->IVal -= Expr2.IVal * CheckedPSizeOf (lhst);
2583                 /* Operate on pointers, result type is a pointer */
2584             } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2585                 /* Left is pointer, right is pointer, must scale result */
2586                 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2587                     Error ("Incompatible pointer types");
2588                 } else {
2589                     Expr->IVal = (Expr->IVal - Expr2.IVal) /
2590                                       CheckedPSizeOf (lhst);
2591                 }
2592                 /* Operate on pointers, result type is an integer */
2593                 Expr->Type = type_int;
2594             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2595                 /* Integer subtraction */
2596                 typeadjust (Expr, &Expr2, 1);
2597                 Expr->IVal -= Expr2.IVal;
2598             } else {
2599                 /* OOPS */
2600                 Error ("Invalid operands for binary operator `-'");
2601             }
2602
2603             /* Result is constant, condition codes not set */
2604             ED_MarkAsUntested (Expr);
2605
2606         } else {
2607
2608             /* Left hand side is not constant, right hand side is.
2609              * Remove pushed value from stack.
2610              */
2611             RemoveCode (&Mark2);
2612
2613             if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2614                 /* Left is pointer, right is int, must scale rhs */
2615                 Expr2.IVal *= CheckedPSizeOf (lhst);
2616                 /* Operate on pointers, result type is a pointer */
2617                 flags = CF_PTR;
2618             } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2619                 /* Left is pointer, right is pointer, must scale result */
2620                 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2621                     Error ("Incompatible pointer types");
2622                 } else {
2623                     rscale = CheckedPSizeOf (lhst);
2624                 }
2625                 /* Operate on pointers, result type is an integer */
2626                 flags = CF_PTR;
2627                 Expr->Type = type_int;
2628             } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2629                 /* Integer subtraction */
2630                 flags = typeadjust (Expr, &Expr2, 1);
2631             } else {
2632                 /* OOPS */
2633                 Error ("Invalid operands for binary operator `-'");
2634                 flags = CF_INT;
2635             }
2636
2637             /* Do the subtraction */
2638             g_dec (flags | CF_CONST, Expr2.IVal);
2639
2640             /* If this was a pointer subtraction, we must scale the result */
2641             if (rscale != 1) {
2642                 g_scale (flags, -rscale);
2643             }
2644
2645             /* Result is a rvalue in the primary register */
2646             ED_MakeRValExpr (Expr);
2647             ED_MarkAsUntested (Expr);
2648
2649         }
2650
2651     } else {
2652
2653         /* Not constant, load into the primary */
2654         LoadExpr (CF_NONE, &Expr2);
2655
2656         /* Right hand side is not constant. Get the rhs type. */
2657         rhst = Expr2.Type;
2658
2659         /* Check for pointer arithmetic */
2660         if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2661             /* Left is pointer, right is int, must scale rhs */
2662             g_scale (CF_INT, CheckedPSizeOf (lhst));
2663             /* Operate on pointers, result type is a pointer */
2664             flags = CF_PTR;
2665         } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2666             /* Left is pointer, right is pointer, must scale result */
2667             if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2668                 Error ("Incompatible pointer types");
2669             } else {
2670                 rscale = CheckedPSizeOf (lhst);
2671             }
2672             /* Operate on pointers, result type is an integer */
2673             flags = CF_PTR;
2674             Expr->Type = type_int;
2675         } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2676             /* Integer subtraction. If the left hand side descriptor says that
2677              * the lhs is const, we have to remove this mark, since this is no
2678              * longer true, lhs is on stack instead.
2679              */
2680             if (ED_IsLocAbs (Expr)) {
2681                 ED_MakeRValExpr (Expr);
2682             }
2683             /* Adjust operand types */
2684             flags = typeadjust (Expr, &Expr2, 0);
2685         } else {
2686             /* OOPS */
2687             Error ("Invalid operands for binary operator `-'");
2688             flags = CF_INT;
2689         }
2690
2691         /* Generate code for the sub (the & is a hack here) */
2692         g_sub (flags & ~CF_CONST, 0);
2693
2694         /* If this was a pointer subtraction, we must scale the result */
2695         if (rscale != 1) {
2696             g_scale (flags, -rscale);
2697         }
2698
2699         /* Result is a rvalue in the primary register */
2700         ED_MakeRValExpr (Expr);
2701         ED_MarkAsUntested (Expr);
2702     }
2703 }
2704
2705
2706
2707 void hie8 (ExprDesc* Expr)
2708 /* Process + and - binary operators. */
2709 {
2710     hie9 (Expr);
2711     while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2712         if (CurTok.Tok == TOK_PLUS) {
2713             parseadd (Expr);
2714         } else {
2715             parsesub (Expr);
2716         }
2717     }
2718 }
2719
2720
2721
2722 static void hie6 (ExprDesc* Expr)
2723 /* Handle greater-than type comparators */
2724 {
2725     static const GenDesc hie6_ops [] = {
2726         { TOK_LT,       GEN_NOPUSH,     g_lt    },
2727         { TOK_LE,       GEN_NOPUSH,     g_le    },
2728         { TOK_GE,       GEN_NOPUSH,     g_ge    },
2729         { TOK_GT,       GEN_NOPUSH,     g_gt    },
2730         { TOK_INVALID,  0,              0       }
2731     };
2732     hie_compare (hie6_ops, Expr, ShiftExpr);
2733 }
2734
2735
2736
2737 static void hie5 (ExprDesc* Expr)
2738 /* Handle == and != */
2739 {
2740     static const GenDesc hie5_ops[] = {
2741         { TOK_EQ,       GEN_NOPUSH,     g_eq    },
2742         { TOK_NE,       GEN_NOPUSH,     g_ne    },
2743         { TOK_INVALID,  0,              0       }
2744     };
2745     hie_compare (hie5_ops, Expr, hie6);
2746 }
2747
2748
2749
2750 static void hie4 (ExprDesc* Expr)
2751 /* Handle & (bitwise and) */
2752 {
2753     static const GenDesc hie4_ops[] = {
2754         { TOK_AND,      GEN_NOPUSH,     g_and   },
2755         { TOK_INVALID,  0,              0       }
2756     };
2757     int UsedGen;
2758
2759     hie_internal (hie4_ops, Expr, hie5, &UsedGen);
2760 }
2761
2762
2763
2764 static void hie3 (ExprDesc* Expr)
2765 /* Handle ^ (bitwise exclusive or) */
2766 {
2767     static const GenDesc hie3_ops[] = {
2768         { TOK_XOR,      GEN_NOPUSH,     g_xor   },
2769         { TOK_INVALID,  0,              0       }
2770     };
2771     int UsedGen;
2772
2773     hie_internal (hie3_ops, Expr, hie4, &UsedGen);
2774 }
2775
2776
2777
2778 static void hie2 (ExprDesc* Expr)
2779 /* Handle | (bitwise or) */
2780 {
2781     static const GenDesc hie2_ops[] = {
2782         { TOK_OR,       GEN_NOPUSH,     g_or    },
2783         { TOK_INVALID,  0,              0       }
2784     };
2785     int UsedGen;
2786
2787     hie_internal (hie2_ops, Expr, hie3, &UsedGen);
2788 }
2789
2790
2791
2792 static void hieAndPP (ExprDesc* Expr)
2793 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2794  * called recursively from the preprocessor.
2795  */
2796 {
2797     ExprDesc Expr2;
2798
2799     ConstAbsIntExpr (hie2, Expr);
2800     while (CurTok.Tok == TOK_BOOL_AND) {
2801
2802         /* Skip the && */
2803         NextToken ();
2804
2805         /* Get rhs */
2806         ConstAbsIntExpr (hie2, &Expr2);
2807
2808         /* Combine the two */
2809         Expr->IVal = (Expr->IVal && Expr2.IVal);
2810     }
2811 }
2812
2813
2814
2815 static void hieOrPP (ExprDesc *Expr)
2816 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2817  * called recursively from the preprocessor.
2818  */
2819 {
2820     ExprDesc Expr2;
2821
2822     ConstAbsIntExpr (hieAndPP, Expr);
2823     while (CurTok.Tok == TOK_BOOL_OR) {
2824
2825         /* Skip the && */
2826         NextToken ();
2827
2828         /* Get rhs */
2829         ConstAbsIntExpr (hieAndPP, &Expr2);
2830
2831         /* Combine the two */
2832         Expr->IVal = (Expr->IVal || Expr2.IVal);
2833     }
2834 }
2835
2836
2837
2838 static void hieAnd (ExprDesc* Expr, unsigned TrueLab, int* BoolOp)
2839 /* Process "exp && exp" */
2840 {
2841     int FalseLab;
2842     ExprDesc Expr2;
2843
2844     hie2 (Expr);
2845     if (CurTok.Tok == TOK_BOOL_AND) {
2846
2847         /* Tell our caller that we're evaluating a boolean */
2848         *BoolOp = 1;
2849
2850         /* Get a label that we will use for false expressions */
2851         FalseLab = GetLocalLabel ();
2852
2853         /* If the expr hasn't set condition codes, set the force-test flag */
2854         if (!ED_IsTested (Expr)) {
2855             ED_MarkForTest (Expr);
2856         }
2857
2858         /* Load the value */
2859         LoadExpr (CF_FORCECHAR, Expr);
2860
2861         /* Generate the jump */
2862         g_falsejump (CF_NONE, FalseLab);
2863
2864         /* Parse more boolean and's */
2865         while (CurTok.Tok == TOK_BOOL_AND) {
2866
2867             /* Skip the && */
2868             NextToken ();
2869
2870             /* Get rhs */
2871             hie2 (&Expr2);
2872             if (!ED_IsTested (&Expr2)) {
2873                 ED_MarkForTest (&Expr2);
2874             }
2875             LoadExpr (CF_FORCECHAR, &Expr2);
2876
2877             /* Do short circuit evaluation */
2878             if (CurTok.Tok == TOK_BOOL_AND) {
2879                 g_falsejump (CF_NONE, FalseLab);
2880             } else {
2881                 /* Last expression - will evaluate to true */
2882                 g_truejump (CF_NONE, TrueLab);
2883             }
2884         }
2885
2886         /* Define the false jump label here */
2887         g_defcodelabel (FalseLab);
2888
2889         /* The result is an rvalue in primary */
2890         ED_MakeRValExpr (Expr);
2891         ED_TestDone (Expr);     /* Condition codes are set */
2892     }
2893 }
2894
2895
2896
2897 static void hieOr (ExprDesc *Expr)
2898 /* Process "exp || exp". */
2899 {
2900     ExprDesc Expr2;
2901     int BoolOp = 0;             /* Did we have a boolean op? */
2902     int AndOp;                  /* Did we have a && operation? */
2903     unsigned TrueLab;           /* Jump to this label if true */
2904     unsigned DoneLab;
2905
2906     /* Get a label */
2907     TrueLab = GetLocalLabel ();
2908
2909     /* Call the next level parser */
2910     hieAnd (Expr, TrueLab, &BoolOp);
2911
2912     /* Any boolean or's? */
2913     if (CurTok.Tok == TOK_BOOL_OR) {
2914
2915         /* If the expr hasn't set condition codes, set the force-test flag */
2916         if (!ED_IsTested (Expr)) {
2917             ED_MarkForTest (Expr);
2918         }
2919
2920         /* Get first expr */
2921         LoadExpr (CF_FORCECHAR, Expr);
2922
2923         /* For each expression jump to TrueLab if true. Beware: If we
2924          * had && operators, the jump is already in place!
2925          */
2926         if (!BoolOp) {
2927             g_truejump (CF_NONE, TrueLab);
2928         }
2929
2930         /* Remember that we had a boolean op */
2931         BoolOp = 1;
2932
2933         /* while there's more expr */
2934         while (CurTok.Tok == TOK_BOOL_OR) {
2935
2936             /* skip the || */
2937             NextToken ();
2938
2939             /* Get a subexpr */
2940             AndOp = 0;
2941             hieAnd (&Expr2, TrueLab, &AndOp);
2942             if (!ED_IsTested (&Expr2)) {
2943                 ED_MarkForTest (&Expr2);
2944             }
2945             LoadExpr (CF_FORCECHAR, &Expr2);
2946
2947             /* If there is more to come, add shortcut boolean eval. */
2948             g_truejump (CF_NONE, TrueLab);
2949
2950         }
2951
2952         /* The result is an rvalue in primary */
2953         ED_MakeRValExpr (Expr);
2954         ED_TestDone (Expr);                     /* Condition codes are set */
2955     }
2956
2957     /* If we really had boolean ops, generate the end sequence */
2958     if (BoolOp) {
2959         DoneLab = GetLocalLabel ();
2960         g_getimmed (CF_INT | CF_CONST, 0, 0);   /* Load FALSE */
2961         g_falsejump (CF_NONE, DoneLab);
2962         g_defcodelabel (TrueLab);
2963         g_getimmed (CF_INT | CF_CONST, 1, 0);   /* Load TRUE */
2964         g_defcodelabel (DoneLab);
2965     }
2966 }
2967
2968
2969
2970 static void hieQuest (ExprDesc* Expr)
2971 /* Parse the ternary operator */
2972 {
2973     int         FalseLab;
2974     int         TrueLab;
2975     CodeMark    TrueCodeEnd;
2976     ExprDesc    Expr2;          /* Expression 2 */
2977     ExprDesc    Expr3;          /* Expression 3 */
2978     int         Expr2IsNULL;    /* Expression 2 is a NULL pointer */
2979     int         Expr3IsNULL;    /* Expression 3 is a NULL pointer */
2980     Type*       ResultType;     /* Type of result */
2981
2982
2983     /* Call the lower level eval routine */
2984     if (Preprocessing) {
2985         hieOrPP (Expr);
2986     } else {
2987         hieOr (Expr);
2988     }
2989
2990     /* Check if it's a ternary expression */
2991     if (CurTok.Tok == TOK_QUEST) {
2992         NextToken ();
2993         if (!ED_IsTested (Expr)) {
2994             /* Condition codes not set, request a test */
2995             ED_MarkForTest (Expr);
2996         }
2997         LoadExpr (CF_NONE, Expr);
2998         FalseLab = GetLocalLabel ();
2999         g_falsejump (CF_NONE, FalseLab);
3000
3001         /* Parse second expression. Remember for later if it is a NULL pointer
3002          * expression, then load it into the primary.
3003          */
3004         ExprWithCheck (hie1, &Expr2);
3005         Expr2IsNULL = ED_IsNullPtr (&Expr2);
3006         if (!IsTypeVoid (Expr2.Type)) {
3007             /* Load it into the primary */
3008             LoadExpr (CF_NONE, &Expr2);
3009             ED_MakeRValExpr (&Expr2);
3010             Expr2.Type = PtrConversion (Expr2.Type);
3011         }
3012
3013         /* Remember the current code position */
3014         GetCodePos (&TrueCodeEnd);
3015
3016         /* Jump around the evaluation of the third expression */
3017         TrueLab = GetLocalLabel ();
3018         ConsumeColon ();
3019         g_jump (TrueLab);
3020
3021         /* Jump here if the first expression was false */
3022         g_defcodelabel (FalseLab);
3023
3024         /* Parse third expression. Remember for later if it is a NULL pointer
3025          * expression, then load it into the primary.
3026          */
3027         ExprWithCheck (hie1, &Expr3);
3028         Expr3IsNULL = ED_IsNullPtr (&Expr3);
3029         if (!IsTypeVoid (Expr3.Type)) {
3030             /* Load it into the primary */
3031             LoadExpr (CF_NONE, &Expr3);
3032             ED_MakeRValExpr (&Expr3);
3033             Expr3.Type = PtrConversion (Expr3.Type);
3034         }
3035
3036         /* Check if any conversions are needed, if so, do them.
3037          * Conversion rules for ?: expression are:
3038          *   - if both expressions are int expressions, default promotion
3039          *     rules for ints apply.
3040          *   - if both expressions are pointers of the same type, the
3041          *     result of the expression is of this type.
3042          *   - if one of the expressions is a pointer and the other is
3043          *     a zero constant, the resulting type is that of the pointer
3044          *     type.
3045          *   - if both expressions are void expressions, the result is of
3046          *     type void.
3047          *   - all other cases are flagged by an error.
3048          */
3049         if (IsClassInt (Expr2.Type) && IsClassInt (Expr3.Type)) {
3050
3051             CodeMark    CvtCodeStart;
3052             CodeMark    CvtCodeEnd;
3053
3054
3055             /* Get common type */
3056             ResultType = promoteint (Expr2.Type, Expr3.Type);
3057
3058             /* Convert the third expression to this type if needed */
3059             TypeConversion (&Expr3, ResultType);
3060
3061             /* Emit conversion code for the second expression, but remember
3062              * where it starts end ends.
3063              */
3064             GetCodePos (&CvtCodeStart);
3065             TypeConversion (&Expr2, ResultType);
3066             GetCodePos (&CvtCodeEnd);
3067
3068             /* If we had conversion code, move it to the right place */
3069             if (!CodeRangeIsEmpty (&CvtCodeStart, &CvtCodeEnd)) {
3070                 MoveCode (&CvtCodeStart, &CvtCodeEnd, &TrueCodeEnd);
3071             }
3072
3073         } else if (IsClassPtr (Expr2.Type) && IsClassPtr (Expr3.Type)) {
3074             /* Must point to same type */
3075             if (TypeCmp (Indirect (Expr2.Type), Indirect (Expr3.Type)) < TC_EQUAL) {
3076                 Error ("Incompatible pointer types");
3077             }
3078             /* Result has the common type */
3079             ResultType = Expr2.Type;
3080         } else if (IsClassPtr (Expr2.Type) && Expr3IsNULL) {
3081             /* Result type is pointer, no cast needed */
3082             ResultType = Expr2.Type;
3083         } else if (Expr2IsNULL && IsClassPtr (Expr3.Type)) {
3084             /* Result type is pointer, no cast needed */
3085             ResultType = Expr3.Type;
3086         } else if (IsTypeVoid (Expr2.Type) && IsTypeVoid (Expr3.Type)) {
3087             /* Result type is void */
3088             ResultType = Expr3.Type;
3089         } else {
3090             Error ("Incompatible types");
3091             ResultType = Expr2.Type;            /* Doesn't matter here */
3092         }
3093
3094         /* Define the final label */
3095         g_defcodelabel (TrueLab);
3096
3097         /* Setup the target expression */
3098         ED_MakeRValExpr (Expr);
3099         Expr->Type  = ResultType;
3100     }
3101 }
3102
3103
3104
3105 static void opeq (const GenDesc* Gen, ExprDesc* Expr, const char* Op)
3106 /* Process "op=" operators. */
3107 {
3108     ExprDesc Expr2;
3109     unsigned flags;
3110     CodeMark Mark;
3111     int MustScale;
3112
3113     /* op= can only be used with lvalues */
3114     if (!ED_IsLVal (Expr)) {
3115         Error ("Invalid lvalue in assignment");
3116         return;
3117     }
3118
3119     /* The left side must not be const qualified */
3120     if (IsQualConst (Expr->Type)) {
3121         Error ("Assignment to const");
3122     }
3123
3124     /* There must be an integer or pointer on the left side */
3125     if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
3126         Error ("Invalid left operand type");
3127         /* Continue. Wrong code will be generated, but the compiler won't
3128          * break, so this is the best error recovery.
3129          */
3130     }
3131
3132     /* Skip the operator token */
3133     NextToken ();
3134
3135     /* Determine the type of the lhs */
3136     flags = TypeOf (Expr->Type);
3137     MustScale = (Gen->Func == g_add || Gen->Func == g_sub) && IsTypePtr (Expr->Type);
3138
3139     /* Get the lhs address on stack (if needed) */
3140     PushAddr (Expr);
3141
3142     /* Fetch the lhs into the primary register if needed */
3143     LoadExpr (CF_NONE, Expr);
3144
3145     /* Bring the lhs on stack */
3146     GetCodePos (&Mark);
3147     g_push (flags, 0);
3148
3149     /* Evaluate the rhs */
3150     MarkedExprWithCheck (hie1, &Expr2);
3151
3152     /* The rhs must be an integer (or a float, but we don't support that yet */
3153     if (!IsClassInt (Expr2.Type)) {
3154         Error ("Invalid right operand for binary operator `%s'", Op);
3155         /* Continue. Wrong code will be generated, but the compiler won't
3156          * break, so this is the best error recovery.
3157          */
3158     }
3159
3160     /* Check for a constant expression */
3161     if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
3162         /* The resulting value is a constant. If the generator has the NOPUSH
3163          * flag set, don't push the lhs.
3164          */
3165         if (Gen->Flags & GEN_NOPUSH) {
3166             RemoveCode (&Mark);
3167         }
3168         if (MustScale) {
3169             /* lhs is a pointer, scale rhs */
3170             Expr2.IVal *= CheckedSizeOf (Expr->Type+1);
3171         }
3172
3173         /* If the lhs is character sized, the operation may be later done
3174          * with characters.
3175          */
3176         if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
3177             flags |= CF_FORCECHAR;
3178         }
3179
3180         /* Special handling for add and sub - some sort of a hack, but short code */
3181         if (Gen->Func == g_add) {
3182             g_inc (flags | CF_CONST, Expr2.IVal);
3183         } else if (Gen->Func == g_sub) {
3184             g_dec (flags | CF_CONST, Expr2.IVal);
3185         } else {
3186             if (Expr2.IVal == 0) {
3187                 /* Check for div by zero/mod by zero */
3188                 if (Gen->Func == g_div) {
3189                     Error ("Division by zero");
3190                 } else if (Gen->Func == g_mod) {
3191                     Error ("Modulo operation with zero");
3192                 }
3193             }
3194             Gen->Func (flags | CF_CONST, Expr2.IVal);
3195         }
3196     } else {
3197
3198         /* rhs is not constant. Load into the primary */
3199         LoadExpr (CF_NONE, &Expr2);
3200         if (MustScale) {
3201             /* lhs is a pointer, scale rhs */
3202             g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Expr->Type+1));
3203         }
3204
3205         /* If the lhs is character sized, the operation may be later done
3206          * with characters.
3207          */
3208         if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
3209             flags |= CF_FORCECHAR;
3210         }
3211
3212         /* Adjust the types of the operands if needed */
3213         Gen->Func (g_typeadjust (flags, TypeOf (Expr2.Type)), 0);
3214     }
3215     Store (Expr, 0);
3216     ED_MakeRValExpr (Expr);
3217 }
3218
3219
3220
3221 static void addsubeq (const GenDesc* Gen, ExprDesc *Expr, const char* Op)
3222 /* Process the += and -= operators */
3223 {
3224     ExprDesc Expr2;
3225     unsigned lflags;
3226     unsigned rflags;
3227     int      MustScale;
3228
3229
3230     /* We're currently only able to handle some adressing modes */
3231     if (ED_GetLoc (Expr) == E_LOC_EXPR || ED_GetLoc (Expr) == E_LOC_PRIMARY) {
3232         /* Use generic routine */
3233         opeq (Gen, Expr, Op);
3234         return;
3235     }
3236
3237     /* We must have an lvalue */
3238     if (ED_IsRVal (Expr)) {
3239         Error ("Invalid lvalue in assignment");
3240         return;
3241     }
3242
3243     /* The left side must not be const qualified */
3244     if (IsQualConst (Expr->Type)) {
3245         Error ("Assignment to const");
3246     }
3247
3248     /* There must be an integer or pointer on the left side */
3249     if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
3250         Error ("Invalid left operand type");
3251         /* Continue. Wrong code will be generated, but the compiler won't
3252          * break, so this is the best error recovery.
3253          */
3254     }
3255
3256     /* Skip the operator */
3257     NextToken ();
3258
3259     /* Check if we have a pointer expression and must scale rhs */
3260     MustScale = IsTypePtr (Expr->Type);
3261
3262     /* Initialize the code generator flags */
3263     lflags = 0;
3264     rflags = 0;
3265
3266     /* Evaluate the rhs. We expect an integer here, since float is not
3267      * supported
3268      */
3269     hie1 (&Expr2);
3270     if (!IsClassInt (Expr2.Type)) {
3271         Error ("Invalid right operand for binary operator `%s'", Op);
3272         /* Continue. Wrong code will be generated, but the compiler won't
3273          * break, so this is the best error recovery.
3274          */
3275     }
3276     if (ED_IsConstAbs (&Expr2)) {
3277         /* The resulting value is a constant. Scale it. */
3278         if (MustScale) {
3279             Expr2.IVal *= CheckedSizeOf (Indirect (Expr->Type));
3280         }
3281         rflags |= CF_CONST;
3282         lflags |= CF_CONST;
3283     } else {
3284         /* Not constant, load into the primary */
3285         LoadExpr (CF_NONE, &Expr2);
3286         if (MustScale) {
3287             /* lhs is a pointer, scale rhs */
3288             g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Indirect (Expr->Type)));
3289         }
3290     }
3291
3292     /* Setup the code generator flags */
3293     lflags |= TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR;
3294     rflags |= TypeOf (Expr2.Type) | CF_FORCECHAR;
3295
3296     /* Convert the type of the lhs to that of the rhs */
3297     g_typecast (lflags, rflags);
3298
3299     /* Output apropriate code depending on the location */
3300     switch (ED_GetLoc (Expr)) {
3301
3302         case E_LOC_ABS:
3303             /* Absolute: numeric address or const */
3304             if (Gen->Tok == TOK_PLUS_ASSIGN) {
3305                 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3306             } else {
3307                 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3308             }
3309             break;
3310
3311         case E_LOC_GLOBAL:
3312             /* Global variable */
3313             if (Gen->Tok == TOK_PLUS_ASSIGN) {
3314                 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3315             } else {
3316                 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3317             }
3318             break;
3319
3320         case E_LOC_STATIC:
3321         case E_LOC_LITERAL:
3322             /* Static variable or literal in the literal pool */
3323             if (Gen->Tok == TOK_PLUS_ASSIGN) {
3324                 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3325             } else {
3326                 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3327             }
3328             break;
3329
3330         case E_LOC_REGISTER:
3331             /* Register variable */
3332             if (Gen->Tok == TOK_PLUS_ASSIGN) {
3333                 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3334             } else {
3335                 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3336             }
3337             break;
3338
3339         case E_LOC_STACK:
3340             /* Value on the stack */
3341             if (Gen->Tok == TOK_PLUS_ASSIGN) {
3342                 g_addeqlocal (lflags, Expr->IVal, Expr2.IVal);
3343             } else {
3344                 g_subeqlocal (lflags, Expr->IVal, Expr2.IVal);
3345             }
3346             break;
3347
3348         default:
3349             Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
3350     }
3351
3352     /* Expression is a rvalue in the primary now */
3353     ED_MakeRValExpr (Expr);
3354 }
3355
3356
3357
3358 void hie1 (ExprDesc* Expr)
3359 /* Parse first level of expression hierarchy. */
3360 {
3361     hieQuest (Expr);
3362     switch (CurTok.Tok) {
3363
3364         case TOK_ASSIGN:
3365             Assignment (Expr);
3366             break;
3367
3368         case TOK_PLUS_ASSIGN:
3369             addsubeq (&GenPASGN, Expr, "+=");
3370             break;
3371
3372         case TOK_MINUS_ASSIGN:
3373             addsubeq (&GenSASGN, Expr, "-=");
3374             break;
3375
3376         case TOK_MUL_ASSIGN:
3377             opeq (&GenMASGN, Expr, "*=");
3378             break;
3379
3380         case TOK_DIV_ASSIGN:
3381             opeq (&GenDASGN, Expr, "/=");
3382             break;
3383
3384         case TOK_MOD_ASSIGN:
3385             opeq (&GenMOASGN, Expr, "%=");
3386             break;
3387
3388         case TOK_SHL_ASSIGN:
3389             opeq (&GenSLASGN, Expr, "<<=");
3390             break;
3391
3392         case TOK_SHR_ASSIGN:
3393             opeq (&GenSRASGN, Expr, ">>=");
3394             break;
3395
3396         case TOK_AND_ASSIGN:
3397             opeq (&GenAASGN, Expr, "&=");
3398             break;
3399
3400         case TOK_XOR_ASSIGN:
3401             opeq (&GenXOASGN, Expr, "^=");
3402             break;
3403
3404         case TOK_OR_ASSIGN:
3405             opeq (&GenOASGN, Expr, "|=");
3406             break;
3407
3408         default:
3409             break;
3410     }
3411 }
3412
3413
3414
3415 void hie0 (ExprDesc *Expr)
3416 /* Parse comma operator. */
3417 {
3418     hie1 (Expr);
3419     while (CurTok.Tok == TOK_COMMA) {
3420         NextToken ();
3421         hie1 (Expr);
3422     }
3423 }
3424
3425
3426
3427 int evalexpr (unsigned Flags, void (*Func) (ExprDesc*), ExprDesc* Expr)
3428 /* Will evaluate an expression via the given function. If the result is a
3429  * constant, 0 is returned and the value is put in the Expr struct. If the
3430  * result is not constant, LoadExpr is called to bring the value into the
3431  * primary register and 1 is returned.
3432  */
3433 {
3434     /* Evaluate */
3435     ExprWithCheck (Func, Expr);
3436
3437     /* Check for a constant expression */
3438     if (ED_IsConstAbs (Expr)) {
3439         /* Constant expression */
3440         return 0;
3441     } else {
3442         /* Not constant, load into the primary */
3443         LoadExpr (Flags, Expr);
3444         return 1;
3445     }
3446 }
3447
3448
3449
3450 void Expression0 (ExprDesc* Expr)
3451 /* Evaluate an expression via hie0 and put the result into the primary register */
3452 {
3453     ExprWithCheck (hie0, Expr);
3454     LoadExpr (CF_NONE, Expr);
3455 }
3456
3457
3458
3459 void ConstExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3460 /* Will evaluate an expression via the given function. If the result is not
3461  * a constant of some sort, a diagnostic will be printed, and the value is
3462  * replaced by a constant one to make sure there are no internal errors that
3463  * result from this input error.
3464  */
3465 {
3466     ExprWithCheck (Func, Expr);
3467     if (!ED_IsConst (Expr)) {
3468         Error ("Constant expression expected");
3469         /* To avoid any compiler errors, make the expression a valid const */
3470         ED_MakeConstAbsInt (Expr, 1);
3471     }
3472 }
3473
3474
3475
3476 void BoolExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3477 /* Will evaluate an expression via the given function. If the result is not
3478  * something that may be evaluated in a boolean context, a diagnostic will be
3479  * printed, and the value is replaced by a constant one to make sure there
3480  * are no internal errors that result from this input error.
3481  */
3482 {
3483     ExprWithCheck (Func, Expr);
3484     if (!ED_IsBool (Expr)) {
3485         Error ("Boolean expression expected");
3486         /* To avoid any compiler errors, make the expression a valid int */
3487         ED_MakeConstAbsInt (Expr, 1);
3488     }
3489 }
3490
3491
3492
3493 void ConstAbsIntExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3494 /* Will evaluate an expression via the given function. If the result is not
3495  * a constant numeric integer value, a diagnostic will be printed, and the
3496  * value is replaced by a constant one to make sure there are no internal
3497  * errors that result from this input error.
3498  */
3499 {
3500     ExprWithCheck (Func, Expr);
3501     if (!ED_IsConstAbsInt (Expr)) {
3502         Error ("Constant integer expression expected");
3503         /* To avoid any compiler errors, make the expression a valid const */
3504         ED_MakeConstAbsInt (Expr, 1);
3505     }
3506 }
3507
3508
3509