3 ** 1998-06-21, Ullrich von Bassewitz
4 ** 2015-06-26, Greg King
14 #include "debugflag.h"
21 #include "assignment.h"
33 #include "shiftexpr.h"
44 /*****************************************************************************/
46 /*****************************************************************************/
50 /* Generator attributes */
51 #define GEN_NOPUSH 0x01 /* Don't push lhs */
52 #define GEN_COMM 0x02 /* Operator is commutative */
53 #define GEN_NOFUNC 0x04 /* Not allowed for function pointers */
55 /* Map a generator function and its attributes to a token */
57 token_t Tok; /* Token to map to */
58 unsigned Flags; /* Flags for generator function */
59 void (*Func) (unsigned, unsigned long); /* Generator func */
62 /* Descriptors for the operations */
63 static GenDesc GenPASGN = { TOK_PLUS_ASSIGN, GEN_NOPUSH, g_add };
64 static GenDesc GenSASGN = { TOK_MINUS_ASSIGN, GEN_NOPUSH, g_sub };
65 static GenDesc GenMASGN = { TOK_MUL_ASSIGN, GEN_NOPUSH, g_mul };
66 static GenDesc GenDASGN = { TOK_DIV_ASSIGN, GEN_NOPUSH, g_div };
67 static GenDesc GenMOASGN = { TOK_MOD_ASSIGN, GEN_NOPUSH, g_mod };
68 static GenDesc GenSLASGN = { TOK_SHL_ASSIGN, GEN_NOPUSH, g_asl };
69 static GenDesc GenSRASGN = { TOK_SHR_ASSIGN, GEN_NOPUSH, g_asr };
70 static GenDesc GenAASGN = { TOK_AND_ASSIGN, GEN_NOPUSH, g_and };
71 static GenDesc GenXOASGN = { TOK_XOR_ASSIGN, GEN_NOPUSH, g_xor };
72 static GenDesc GenOASGN = { TOK_OR_ASSIGN, GEN_NOPUSH, g_or };
76 /*****************************************************************************/
77 /* Helper functions */
78 /*****************************************************************************/
82 static unsigned GlobalModeFlags (const ExprDesc* Expr)
83 /* Return the addressing mode flags for the given expression */
85 switch (ED_GetLoc (Expr)) {
86 case E_LOC_ABS: return CF_ABSOLUTE;
87 case E_LOC_GLOBAL: return CF_EXTERNAL;
88 case E_LOC_STATIC: return CF_STATIC;
89 case E_LOC_REGISTER: return CF_REGVAR;
90 case E_LOC_STACK: return CF_NONE;
91 case E_LOC_PRIMARY: return CF_NONE;
92 case E_LOC_EXPR: return CF_NONE;
93 case E_LOC_LITERAL: return CF_STATIC; /* Same as static */
95 Internal ("GlobalModeFlags: Invalid location flags value: 0x%04X", Expr->Flags);
103 void ExprWithCheck (void (*Func) (ExprDesc*), ExprDesc* Expr)
104 /* Call an expression function with checks. */
106 /* Remember the stack pointer */
107 int OldSP = StackPtr;
109 /* Call the expression function */
112 /* Do some checks to see if code generation is still consistent */
113 if (StackPtr != OldSP) {
115 Error ("Code generation messed up: "
116 "StackPtr is %d, should be %d",
119 Internal ("Code generation messed up: "
120 "StackPtr is %d, should be %d",
128 void MarkedExprWithCheck (void (*Func) (ExprDesc*), ExprDesc* Expr)
129 /* Call an expression function with checks and record start and end of the
135 ExprWithCheck (Func, Expr);
137 ED_SetCodeRange (Expr, &Start, &End);
142 static Type* promoteint (Type* lhst, Type* rhst)
143 /* In an expression with two ints, return the type of the result */
145 /* Rules for integer types:
146 ** - If one of the values is a long, the result is long.
147 ** - If one of the values is unsigned, the result is also unsigned.
148 ** - Otherwise the result is an int.
150 if (IsTypeLong (lhst) || IsTypeLong (rhst)) {
151 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
157 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
167 static unsigned typeadjust (ExprDesc* lhs, ExprDesc* rhs, int NoPush)
168 /* Adjust the two values for a binary operation. lhs is expected on stack or
169 ** to be constant, rhs is expected to be in the primary register or constant.
170 ** The function will put the type of the result into lhs and return the
171 ** code generator flags for the operation.
172 ** If NoPush is given, it is assumed that the operation does not expect the lhs
173 ** to be on stack, and that lhs is in a register instead.
174 ** Beware: The function does only accept int types.
177 unsigned ltype, rtype;
180 /* Get the type strings */
181 Type* lhst = lhs->Type;
182 Type* rhst = rhs->Type;
184 /* Generate type adjustment code if needed */
185 ltype = TypeOf (lhst);
186 if (ED_IsLocAbs (lhs)) {
190 /* Value is in primary register*/
193 rtype = TypeOf (rhst);
194 if (ED_IsLocAbs (rhs)) {
197 flags = g_typeadjust (ltype, rtype);
199 /* Set the type of the result */
200 lhs->Type = promoteint (lhst, rhst);
202 /* Return the code generator flags */
208 static const GenDesc* FindGen (token_t Tok, const GenDesc* Table)
209 /* Find a token in a generator table */
211 while (Table->Tok != TOK_INVALID) {
212 if (Table->Tok == Tok) {
222 static int TypeSpecAhead (void)
223 /* Return true if some sort of type is waiting (helper for cast and sizeof()
229 /* There's a type waiting if:
231 ** We have an opening paren, and
232 ** a. the next token is a type, or
233 ** b. the next token is a type qualifier, or
234 ** c. the next token is a typedef'd type
236 return CurTok.Tok == TOK_LPAREN && (
237 TokIsType (&NextTok) ||
238 TokIsTypeQual (&NextTok) ||
239 (NextTok.Tok == TOK_IDENT &&
240 (Entry = FindSym (NextTok.Ident)) != 0 &&
241 SymIsTypeDef (Entry)));
246 void PushAddr (const ExprDesc* Expr)
247 /* If the expression contains an address that was somehow evaluated,
248 ** push this address on the stack. This is a helper function for all
249 ** sorts of implicit or explicit assignment functions where the lvalue
250 ** must be saved if it's not constant, before evaluating the rhs.
253 /* Get the address on stack if needed */
254 if (ED_IsLocExpr (Expr)) {
255 /* Push the address (always a pointer) */
262 static void WarnConstCompareResult (void)
263 /* If the result of a comparison is constant, this is suspicious when not in
264 ** preprocessor mode.
267 if (!Preprocessing && IS_Get (&WarnConstComparison) != 0) {
268 Warning ("Result of comparison is constant");
274 /*****************************************************************************/
276 /*****************************************************************************/
280 static unsigned FunctionParamList (FuncDesc* Func, int IsFastcall)
281 /* Parse a function parameter list and pass the parameters to the called
282 ** function. Depending on several criteria this may be done by just pushing
283 ** each parameter separately, or creating the parameter frame once and then
284 ** storing into this frame.
285 ** The function returns the size of the parameters pushed.
290 /* Initialize variables */
291 SymEntry* Param = 0; /* Keep gcc silent */
292 unsigned ParamSize = 0; /* Size of parameters pushed */
293 unsigned ParamCount = 0; /* Number of parameters pushed */
294 unsigned FrameSize = 0; /* Size of parameter frame */
295 unsigned FrameParams = 0; /* Number of params in frame */
296 int FrameOffs = 0; /* Offset into parameter frame */
297 int Ellipsis = 0; /* Function is variadic */
299 /* As an optimization, we may allocate the complete parameter frame at
300 ** once instead of pushing each parameter as it comes. We may do that,
303 ** - optimizations that increase code size are enabled (allocating the
304 ** stack frame at once gives usually larger code).
305 ** - we have more than one parameter to push (don't count the last param
306 ** for __fastcall__ functions).
308 ** The FrameSize variable will contain a value > 0 if storing into a frame
309 ** (instead of pushing) is enabled.
312 if (IS_Get (&CodeSizeFactor) >= 200) {
314 /* Calculate the number and size of the parameters */
315 FrameParams = Func->ParamCount;
316 FrameSize = Func->ParamSize;
317 if (FrameParams > 0 && IsFastcall) {
318 /* Last parameter is not pushed */
319 FrameSize -= CheckedSizeOf (Func->LastParam->Type);
323 /* Do we have more than one parameter in the frame? */
324 if (FrameParams > 1) {
325 /* Okeydokey, setup the frame */
326 FrameOffs = StackPtr;
328 StackPtr -= FrameSize;
330 /* Don't use a preallocated frame */
335 /* Parse the actual parameter list */
336 while (CurTok.Tok != TOK_RPAREN) {
340 /* Count arguments */
343 /* Fetch the pointer to the next argument, check for too many args */
344 if (ParamCount <= Func->ParamCount) {
345 /* Beware: If there are parameters with identical names, they
346 ** cannot go into the same symbol table, which means that in this
347 ** case of errorneous input, the number of nodes in the symbol
348 ** table and ParamCount are NOT equal. We have to handle this case
349 ** below to avoid segmentation violations. Since we know that this
350 ** problem can only occur if there is more than one parameter,
351 ** we will just use the last one.
353 if (ParamCount == 1) {
355 Param = Func->SymTab->SymHead;
356 } else if (Param->NextSym != 0) {
358 Param = Param->NextSym;
359 CHECK ((Param->Flags & SC_PARAM) != 0);
361 } else if (!Ellipsis) {
362 /* Too many arguments. Do we have an open param list? */
363 if ((Func->Flags & FD_VARIADIC) == 0) {
364 /* End of param list reached, no ellipsis */
365 Error ("Too many arguments in function call");
367 /* Assume an ellipsis even in case of errors to avoid an error
368 ** message for each other argument.
373 /* Evaluate the parameter expression */
376 /* If we don't have an argument spec, accept anything, otherwise
377 ** convert the actual argument to the type needed.
382 /* Convert the argument to the parameter type if needed */
383 TypeConversion (&Expr, Param->Type);
385 /* If we have a prototype, chars may be pushed as chars */
386 Flags |= CF_FORCECHAR;
390 /* No prototype available. Convert array to "pointer to first
391 ** element", and function to "pointer to function".
393 Expr.Type = PtrConversion (Expr.Type);
397 /* Load the value into the primary if it is not already there */
398 LoadExpr (Flags, &Expr);
400 /* Use the type of the argument for the push */
401 Flags |= TypeOf (Expr.Type);
403 /* If this is a fastcall function, don't push the last argument */
404 if (ParamCount != Func->ParamCount || !IsFastcall) {
405 unsigned ArgSize = sizeofarg (Flags);
407 /* We have the space already allocated, store in the frame.
408 ** Because of invalid type conversions (that have produced an
409 ** error before), we can end up here with a non-aligned stack
410 ** frame. Since no output will be generated anyway, handle
411 ** these cases gracefully instead of doing a CHECK.
413 if (FrameSize >= ArgSize) {
414 FrameSize -= ArgSize;
418 FrameOffs -= ArgSize;
420 g_putlocal (Flags | CF_NOKEEP, FrameOffs, Expr.IVal);
422 /* Push the argument */
423 g_push (Flags, Expr.IVal);
426 /* Calculate total parameter size */
427 ParamSize += ArgSize;
430 /* Check for end of argument list */
431 if (CurTok.Tok != TOK_COMMA) {
437 /* Check if we had enough parameters */
438 if (ParamCount < Func->ParamCount) {
439 Error ("Too few arguments in function call");
442 /* The function returns the size of all parameters pushed onto the stack.
443 ** However, if there are parameters missing (which is an error and was
444 ** flagged by the compiler) AND a stack frame was preallocated above,
445 ** we would loose track of the stackpointer and generate an internal error
446 ** later. So we correct the value by the parameters that should have been
447 ** pushed to avoid an internal compiler error. Since an error was
448 ** generated before, no code will be output anyway.
450 return ParamSize + FrameSize;
455 static void FunctionCall (ExprDesc* Expr)
456 /* Perform a function call. */
458 FuncDesc* Func; /* Function descriptor */
459 int IsFuncPtr; /* Flag */
460 unsigned ParamSize; /* Number of parameter bytes */
462 int PtrOffs = 0; /* Offset of function pointer on stack */
463 int IsFastcall = 0; /* True if it's a fast-call function */
464 int PtrOnStack = 0; /* True if a pointer copy is on stack */
466 /* Skip the left paren */
469 /* Get a pointer to the function descriptor from the type string */
470 Func = GetFuncDesc (Expr->Type);
472 /* Handle function pointers transparently */
473 IsFuncPtr = IsTypeFuncPtr (Expr->Type);
475 /* Check whether it's a fastcall function that has parameters */
476 IsFastcall = (Func->Flags & FD_VARIADIC) == 0 && Func->ParamCount > 0 &&
478 IsQualFastcall (Expr->Type + 1) :
479 !IsQualCDecl (Expr->Type + 1));
481 /* Things may be difficult, depending on where the function pointer
482 ** resides. If the function pointer is an expression of some sort
483 ** (not a local or global variable), we have to evaluate this
484 ** expression now and save the result for later. Since calls to
485 ** function pointers may be nested, we must save it onto the stack.
486 ** For fastcall functions we do also need to place a copy of the
487 ** pointer on stack, since we cannot use a/x.
489 PtrOnStack = IsFastcall || !ED_IsConst (Expr);
492 /* Not a global or local variable, or a fastcall function. Load
493 ** the pointer into the primary and mark it as an expression.
495 LoadExpr (CF_NONE, Expr);
496 ED_MakeRValExpr (Expr);
498 /* Remember the code position */
501 /* Push the pointer onto the stack and remember the offset */
507 /* Check function attributes */
508 if (Expr->Sym && SymHasAttr (Expr->Sym, atNoReturn)) {
509 /* For now, handle as if a return statement was encountered */
510 F_ReturnFound (CurrentFunc);
513 /* Check for known standard functions and inline them */
514 if (Expr->Name != 0) {
515 int StdFunc = FindStdFunc ((const char*) Expr->Name);
517 /* Inline this function */
518 HandleStdFunc (StdFunc, Func, Expr);
523 /* If we didn't inline the function, get fastcall info */
524 IsFastcall = (Func->Flags & FD_VARIADIC) == 0 &&
526 IsQualFastcall (Expr->Type) :
527 !IsQualCDecl (Expr->Type));
530 /* Parse the parameter list */
531 ParamSize = FunctionParamList (Func, IsFastcall);
533 /* We need the closing paren here */
536 /* Special handling for function pointers */
539 /* If the function is not a fastcall function, load the pointer to
540 ** the function into the primary.
544 /* Not a fastcall function - we may use the primary */
546 /* If we have no parameters, the pointer is still in the
547 ** primary. Remove the code to push it and correct the
550 if (ParamSize == 0) {
554 /* Load from the saved copy */
555 g_getlocal (CF_PTR, PtrOffs);
558 /* Load from original location */
559 LoadExpr (CF_NONE, Expr);
562 /* Call the function */
563 g_callind (TypeOf (Expr->Type+1), ParamSize, PtrOffs);
567 /* Fastcall function. We cannot use the primary for the function
568 ** pointer and must therefore use an offset to the stack location.
569 ** Since fastcall functions may never be variadic, we can use the
570 ** index register for this purpose.
572 g_callind (CF_LOCAL, ParamSize, PtrOffs);
575 /* If we have a pointer on stack, remove it */
586 /* Normal function */
587 g_call (TypeOf (Expr->Type), (const char*) Expr->Name, ParamSize);
591 /* The function result is an rvalue in the primary register */
592 ED_MakeRValExpr (Expr);
593 Expr->Type = GetFuncReturn (Expr->Type);
598 static void Primary (ExprDesc* E)
599 /* This is the lowest level of the expression parser. */
603 /* Initialize fields in the expression stucture */
606 /* Character and integer constants. */
607 if (CurTok.Tok == TOK_ICONST || CurTok.Tok == TOK_CCONST) {
608 E->IVal = CurTok.IVal;
609 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
610 E->Type = CurTok.Type;
615 /* Floating point constant */
616 if (CurTok.Tok == TOK_FCONST) {
617 E->FVal = CurTok.FVal;
618 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
619 E->Type = CurTok.Type;
624 /* Process parenthesized subexpression by calling the whole parser
627 if (CurTok.Tok == TOK_LPAREN) {
634 /* If we run into an identifier in preprocessing mode, we assume that this
635 ** is an undefined macro and replace it by a constant value of zero.
637 if (Preprocessing && CurTok.Tok == TOK_IDENT) {
639 ED_MakeConstAbsInt (E, 0);
643 /* All others may only be used if the expression evaluation is not called
644 ** recursively by the preprocessor.
647 /* Illegal expression in PP mode */
648 Error ("Preprocessor expression expected");
649 ED_MakeConstAbsInt (E, 1);
653 switch (CurTok.Tok) {
656 /* Identifier. Get a pointer to the symbol table entry */
657 Sym = E->Sym = FindSym (CurTok.Ident);
659 /* Is the symbol known? */
662 /* We found the symbol - skip the name token */
665 /* Check for illegal symbol types */
666 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
667 if (Sym->Flags & SC_TYPE) {
668 /* Cannot use type symbols */
669 Error ("Variable identifier expected");
670 /* Assume an int type to make E valid */
671 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
676 /* Mark the symbol as referenced */
677 Sym->Flags |= SC_REF;
679 /* The expression type is the symbol type */
682 /* Check for legal symbol types */
683 if ((Sym->Flags & SC_CONST) == SC_CONST) {
684 /* Enum or some other numeric constant */
685 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
686 E->IVal = Sym->V.ConstVal;
687 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
689 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
690 E->Name = (unsigned long) Sym->Name;
691 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
692 /* Local variable. If this is a parameter for a variadic
693 ** function, we have to add some address calculations, and the
694 ** address is not const.
696 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
697 /* Variadic parameter */
698 g_leavariadic (Sym->V.Offs - F_GetParamSize (CurrentFunc));
699 E->Flags = E_LOC_EXPR | E_RTYPE_LVAL;
701 /* Normal parameter */
702 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
703 E->IVal = Sym->V.Offs;
705 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
706 /* Register variable, zero page based */
707 E->Flags = E_LOC_REGISTER | E_RTYPE_LVAL;
708 E->Name = Sym->V.R.RegOffs;
709 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
710 /* Static variable */
711 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
712 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
713 E->Name = (unsigned long) Sym->Name;
715 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
716 E->Name = Sym->V.Label;
719 /* Local static variable */
720 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
721 E->Name = Sym->V.Offs;
724 /* We've made all variables lvalues above. However, this is
725 ** not always correct: An array is actually the address of its
726 ** first element, which is a rvalue, and a function is a
727 ** rvalue, too, because we cannot store anything in a function.
728 ** So fix the flags depending on the type.
730 if (IsTypeArray (E->Type) || IsTypeFunc (E->Type)) {
736 /* We did not find the symbol. Remember the name, then skip it */
738 strcpy (Ident, CurTok.Ident);
741 /* IDENT is either an auto-declared function or an undefined variable. */
742 if (CurTok.Tok == TOK_LPAREN) {
743 /* C99 doesn't allow calls to undefined functions, so
744 ** generate an error and otherwise a warning. Declare a
745 ** function returning int. For that purpose, prepare a
746 ** function signature for a function having an empty param
747 ** list and returning int.
749 if (IS_Get (&Standard) >= STD_C99) {
750 Error ("Call to undefined function `%s'", Ident);
752 Warning ("Call to undefined function `%s'", Ident);
754 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
756 E->Flags = E_LOC_GLOBAL | E_RTYPE_RVAL;
757 E->Name = (unsigned long) Sym->Name;
759 /* Undeclared Variable */
760 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
761 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
763 Error ("Undefined symbol: `%s'", Ident);
772 E->LVal = UseLiteral (CurTok.SVal);
773 E->Type = GetCharArrayType (GetLiteralSize (CurTok.SVal));
774 E->Flags = E_LOC_LITERAL | E_RTYPE_RVAL;
776 E->Name = GetLiteralLabel (CurTok.SVal);
783 E->Flags = E_LOC_EXPR | E_RTYPE_RVAL;
788 /* Register pseudo variable */
789 E->Type = type_uchar;
790 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
795 /* Register pseudo variable */
797 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
802 /* Register pseudo variable */
803 E->Type = type_ulong;
804 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
809 /* Illegal primary. Be sure to skip the token to avoid endless
812 Error ("Expression expected");
814 ED_MakeConstAbsInt (E, 1);
821 static void ArrayRef (ExprDesc* Expr)
822 /* Handle an array reference. This function needs a rewrite. */
833 /* Skip the bracket */
836 /* Get the type of left side */
839 /* We can apply a special treatment for arrays that have a const base
840 ** address. This is true for most arrays and will produce a lot better
841 ** code. Check if this is a const base address.
843 ConstBaseAddr = ED_IsRVal (Expr) &&
844 (ED_IsLocConst (Expr) || ED_IsLocStack (Expr));
846 /* If we have a constant base, we delay the address fetch */
848 if (!ConstBaseAddr) {
849 /* Get a pointer to the array into the primary */
850 LoadExpr (CF_NONE, Expr);
852 /* Get the array pointer on stack. Do not push more than 16
853 ** bit, even if this value is greater, since we cannot handle
854 ** other than 16bit stuff when doing indexing.
860 /* TOS now contains ptr to array elements. Get the subscript. */
861 MarkedExprWithCheck (hie0, &Subscript);
863 /* Check the types of array and subscript. We can either have a
864 ** pointer/array to the left, in which case the subscript must be of an
865 ** integer type, or we have an integer to the left, in which case the
866 ** subscript must be a pointer/array.
867 ** Since we do the necessary checking here, we can rely later on the
870 Qualifiers = T_QUAL_NONE;
871 if (IsClassPtr (Expr->Type)) {
872 if (!IsClassInt (Subscript.Type)) {
873 Error ("Array subscript is not an integer");
874 /* To avoid any compiler errors, make the expression a valid int */
875 ED_MakeConstAbsInt (&Subscript, 0);
877 if (IsTypeArray (Expr->Type)) {
878 Qualifiers = GetQualifier (Expr->Type);
880 ElementType = Indirect (Expr->Type);
881 } else if (IsClassInt (Expr->Type)) {
882 if (!IsClassPtr (Subscript.Type)) {
883 Error ("Subscripted value is neither array nor pointer");
884 /* To avoid compiler errors, make the subscript a char[] at
887 ED_MakeConstAbs (&Subscript, 0, GetCharArrayType (1));
888 } else if (IsTypeArray (Subscript.Type)) {
889 Qualifiers = GetQualifier (Subscript.Type);
891 ElementType = Indirect (Subscript.Type);
893 Error ("Cannot subscript");
894 /* To avoid compiler errors, fake both the array and the subscript, so
895 ** we can just proceed.
897 ED_MakeConstAbs (Expr, 0, GetCharArrayType (1));
898 ED_MakeConstAbsInt (&Subscript, 0);
899 ElementType = Indirect (Expr->Type);
902 /* The element type has the combined qualifiers from itself and the array,
903 ** it is a member of (if any).
905 if (GetQualifier (ElementType) != (GetQualifier (ElementType) | Qualifiers)) {
906 ElementType = TypeDup (ElementType);
907 ElementType->C |= Qualifiers;
910 /* If the subscript is a bit-field, load it and make it an rvalue */
911 if (ED_IsBitField (&Subscript)) {
912 LoadExpr (CF_NONE, &Subscript);
913 ED_MakeRValExpr (&Subscript);
916 /* Check if the subscript is constant absolute value */
917 if (ED_IsConstAbs (&Subscript) && ED_CodeRangeIsEmpty (&Subscript)) {
919 /* The array subscript is a numeric constant. If we had pushed the
920 ** array base address onto the stack before, we can remove this value,
921 ** since we can generate expression+offset.
923 if (!ConstBaseAddr) {
926 /* Get an array pointer into the primary */
927 LoadExpr (CF_NONE, Expr);
930 if (IsClassPtr (Expr->Type)) {
932 /* Lhs is pointer/array. Scale the subscript value according to
935 Subscript.IVal *= CheckedSizeOf (ElementType);
937 /* Remove the address load code */
940 /* In case of an array, we can adjust the offset of the expression
941 ** already in Expr. If the base address was a constant, we can even
942 ** remove the code that loaded the address into the primary.
944 if (IsTypeArray (Expr->Type)) {
946 /* Adjust the offset */
947 Expr->IVal += Subscript.IVal;
951 /* It's a pointer, so we do have to load it into the primary
952 ** first (if it's not already there).
954 if (ConstBaseAddr || ED_IsLVal (Expr)) {
955 LoadExpr (CF_NONE, Expr);
956 ED_MakeRValExpr (Expr);
960 Expr->IVal = Subscript.IVal;
965 /* Scale the rhs value according to the element type */
966 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
968 /* Add the subscript. Since arrays are indexed by integers,
969 ** we will ignore the true type of the subscript here and
970 ** use always an int. #### Use offset but beware of LoadExpr!
972 g_inc (CF_INT | CF_CONST, Subscript.IVal);
978 /* Array subscript is not constant. Load it into the primary */
980 LoadExpr (CF_NONE, &Subscript);
983 if (IsClassPtr (Expr->Type)) {
985 /* Indexing is based on unsigneds, so we will just use the integer
986 ** portion of the index (which is in (e)ax, so there's no further
989 g_scale (CF_INT, CheckedSizeOf (ElementType));
993 /* Get the int value on top. If we come here, we're sure, both
994 ** values are 16 bit (the first one was truncated if necessary
995 ** and the second one is a pointer). Note: If ConstBaseAddr is
996 ** true, we don't have a value on stack, so to "swap" both, just
997 ** push the subscript.
1001 LoadExpr (CF_NONE, Expr);
1008 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
1012 /* The offset is now in the primary register. It we didn't have a
1013 ** constant base address for the lhs, the lhs address is already
1014 ** on stack, and we must add the offset. If the base address was
1015 ** constant, we call special functions to add the address to the
1018 if (!ConstBaseAddr) {
1020 /* The array base address is on stack and the subscript is in the
1021 ** primary. Add both.
1027 /* The subscript is in the primary, and the array base address is
1028 ** in Expr. If the subscript has itself a constant address, it is
1029 ** often a better idea to reverse again the order of the
1030 ** evaluation. This will generate better code if the subscript is
1031 ** a byte sized variable. But beware: This is only possible if the
1032 ** subscript was not scaled, that is, if this was a byte array
1035 if ((ED_IsLocConst (&Subscript) || ED_IsLocStack (&Subscript)) &&
1036 CheckedSizeOf (ElementType) == SIZEOF_CHAR) {
1040 /* Reverse the order of evaluation */
1041 if (CheckedSizeOf (Subscript.Type) == SIZEOF_CHAR) {
1046 RemoveCode (&Mark2);
1048 /* Get a pointer to the array into the primary. */
1049 LoadExpr (CF_NONE, Expr);
1051 /* Add the variable */
1052 if (ED_IsLocStack (&Subscript)) {
1053 g_addlocal (Flags, Subscript.IVal);
1055 Flags |= GlobalModeFlags (&Subscript);
1056 g_addstatic (Flags, Subscript.Name, Subscript.IVal);
1060 if (ED_IsLocAbs (Expr)) {
1061 /* Constant numeric address. Just add it */
1062 g_inc (CF_INT, Expr->IVal);
1063 } else if (ED_IsLocStack (Expr)) {
1064 /* Base address is a local variable address */
1065 if (IsTypeArray (Expr->Type)) {
1066 g_addaddr_local (CF_INT, Expr->IVal);
1068 g_addlocal (CF_PTR, Expr->IVal);
1071 /* Base address is a static variable address */
1072 unsigned Flags = CF_INT | GlobalModeFlags (Expr);
1073 if (ED_IsRVal (Expr)) {
1074 /* Add the address of the location */
1075 g_addaddr_static (Flags, Expr->Name, Expr->IVal);
1077 /* Add the contents of the location */
1078 g_addstatic (Flags, Expr->Name, Expr->IVal);
1086 /* The result is an expression in the primary */
1087 ED_MakeRValExpr (Expr);
1091 /* Result is of element type */
1092 Expr->Type = ElementType;
1094 /* An array element is actually a variable. So the rules for variables
1095 ** with respect to the reference type apply: If it's an array, it is
1096 ** a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1097 ** but an array cannot contain functions).
1099 if (IsTypeArray (Expr->Type)) {
1105 /* Consume the closing bracket */
1111 static void StructRef (ExprDesc* Expr)
1112 /* Process struct field after . or ->. */
1119 /* Skip the token and check for an identifier */
1121 if (CurTok.Tok != TOK_IDENT) {
1122 Error ("Identifier expected");
1123 /* Make the expression an integer at address zero */
1124 ED_MakeConstAbs (Expr, 0, type_int);
1128 /* Get the symbol table entry and check for a struct field */
1129 strcpy (Ident, CurTok.Ident);
1131 Field = FindStructField (Expr->Type, Ident);
1133 Error ("Struct/union has no field named `%s'", Ident);
1134 /* Make the expression an integer at address zero */
1135 ED_MakeConstAbs (Expr, 0, type_int);
1139 /* If we have a struct pointer that is an lvalue and not already in the
1140 ** primary, load it now.
1142 if (ED_IsLVal (Expr) && IsTypePtr (Expr->Type)) {
1144 /* Load into the primary */
1145 LoadExpr (CF_NONE, Expr);
1147 /* Make it an lvalue expression */
1148 ED_MakeLValExpr (Expr);
1151 /* The type is the type of the field plus any qualifiers from the struct */
1152 if (IsClassStruct (Expr->Type)) {
1153 Q = GetQualifier (Expr->Type);
1155 Q = GetQualifier (Indirect (Expr->Type));
1157 if (GetQualifier (Field->Type) == (GetQualifier (Field->Type) | Q)) {
1158 FinalType = Field->Type;
1160 FinalType = TypeDup (Field->Type);
1164 /* A struct is usually an lvalue. If not, it is a struct in the primary
1167 if (ED_IsRVal (Expr) && ED_IsLocExpr (Expr) && !IsTypePtr (Expr->Type)) {
1172 /* Get the size of the type */
1173 unsigned Size = SizeOf (Expr->Type);
1176 CHECK (Field->V.Offs + Size <= SIZEOF_LONG);
1178 /* The type of the operation depends on the type of the struct */
1180 case 1: Flags = CF_CHAR | CF_UNSIGNED | CF_CONST; break;
1181 case 2: Flags = CF_INT | CF_UNSIGNED | CF_CONST; break;
1182 case 3: /* FALLTHROUGH */
1183 case 4: Flags = CF_LONG | CF_UNSIGNED | CF_CONST; break;
1184 default: Internal ("Invalid struct size: %u", Size); break;
1187 /* Generate a shift to get the field in the proper position in the
1188 ** primary. For bit fields, mask the value.
1190 BitOffs = Field->V.Offs * CHAR_BITS;
1191 if (SymIsBitField (Field)) {
1192 BitOffs += Field->V.B.BitOffs;
1193 g_asr (Flags, BitOffs);
1194 /* Mask the value. This is unnecessary if the shift executed above
1195 ** moved only zeroes into the value.
1197 if (BitOffs + Field->V.B.BitWidth != Size * CHAR_BITS) {
1198 g_and (CF_INT | CF_UNSIGNED | CF_CONST,
1199 (0x0001U << Field->V.B.BitWidth) - 1U);
1202 g_asr (Flags, BitOffs);
1205 /* Use the new type */
1206 Expr->Type = FinalType;
1210 /* Set the struct field offset */
1211 Expr->IVal += Field->V.Offs;
1213 /* Use the new type */
1214 Expr->Type = FinalType;
1216 /* An struct member is actually a variable. So the rules for variables
1217 ** with respect to the reference type apply: If it's an array, it is
1218 ** a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1219 ** but a struct field cannot be a function).
1221 if (IsTypeArray (Expr->Type)) {
1227 /* Make the expression a bit field if necessary */
1228 if (SymIsBitField (Field)) {
1229 ED_MakeBitField (Expr, Field->V.B.BitOffs, Field->V.B.BitWidth);
1237 static void hie11 (ExprDesc *Expr)
1238 /* Handle compound types (structs and arrays) */
1240 /* Name value used in invalid function calls */
1241 static const char IllegalFunc[] = "illegal_function_call";
1243 /* Evaluate the lhs */
1246 /* Check for a rhs */
1247 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN ||
1248 CurTok.Tok == TOK_DOT || CurTok.Tok == TOK_PTR_REF) {
1250 switch (CurTok.Tok) {
1253 /* Array reference */
1258 /* Function call. */
1259 if (!IsTypeFunc (Expr->Type) && !IsTypeFuncPtr (Expr->Type)) {
1260 /* Not a function */
1261 Error ("Illegal function call");
1262 /* Force the type to be a implicitly defined function, one
1263 ** returning an int and taking any number of arguments.
1264 ** Since we don't have a name, invent one.
1266 ED_MakeConstAbs (Expr, 0, GetImplicitFuncType ());
1267 Expr->Name = (long) IllegalFunc;
1269 /* Call the function */
1270 FunctionCall (Expr);
1274 if (!IsClassStruct (Expr->Type)) {
1275 Error ("Struct expected");
1281 /* If we have an array, convert it to pointer to first element */
1282 if (IsTypeArray (Expr->Type)) {
1283 Expr->Type = ArrayToPtr (Expr->Type);
1285 if (!IsClassPtr (Expr->Type) || !IsClassStruct (Indirect (Expr->Type))) {
1286 Error ("Struct pointer expected");
1292 Internal ("Invalid token in hie11: %d", CurTok.Tok);
1300 void Store (ExprDesc* Expr, const Type* StoreType)
1301 /* Store the primary register into the location denoted by Expr. If StoreType
1302 ** is given, use this type when storing instead of Expr->Type. If StoreType
1303 ** is NULL, use Expr->Type instead.
1308 /* If StoreType was not given, use Expr->Type instead */
1309 if (StoreType == 0) {
1310 StoreType = Expr->Type;
1313 /* Prepare the code generator flags */
1314 Flags = TypeOf (StoreType) | GlobalModeFlags (Expr);
1316 /* Do the store depending on the location */
1317 switch (ED_GetLoc (Expr)) {
1320 /* Absolute: numeric address or const */
1321 g_putstatic (Flags, Expr->IVal, 0);
1325 /* Global variable */
1326 g_putstatic (Flags, Expr->Name, Expr->IVal);
1331 /* Static variable or literal in the literal pool */
1332 g_putstatic (Flags, Expr->Name, Expr->IVal);
1335 case E_LOC_REGISTER:
1336 /* Register variable */
1337 g_putstatic (Flags, Expr->Name, Expr->IVal);
1341 /* Value on the stack */
1342 g_putlocal (Flags, Expr->IVal, 0);
1346 /* The primary register (value is already there) */
1350 /* An expression in the primary register */
1351 g_putind (Flags, Expr->IVal);
1355 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
1358 /* Assume that each one of the stores will invalidate CC */
1359 ED_MarkAsUntested (Expr);
1364 static void PreInc (ExprDesc* Expr)
1365 /* Handle the preincrement operators */
1370 /* Skip the operator token */
1373 /* Evaluate the expression and check that it is an lvalue */
1375 if (!ED_IsLVal (Expr)) {
1376 Error ("Invalid lvalue");
1380 /* We cannot modify const values */
1381 if (IsQualConst (Expr->Type)) {
1382 Error ("Increment of read-only variable");
1385 /* Get the data type */
1386 Flags = TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR | CF_CONST;
1388 /* Get the increment value in bytes */
1389 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1391 /* Check the location of the data */
1392 switch (ED_GetLoc (Expr)) {
1395 /* Absolute: numeric address or const */
1396 g_addeqstatic (Flags, Expr->IVal, 0, Val);
1400 /* Global variable */
1401 g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1406 /* Static variable or literal in the literal pool */
1407 g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1410 case E_LOC_REGISTER:
1411 /* Register variable */
1412 g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1416 /* Value on the stack */
1417 g_addeqlocal (Flags, Expr->IVal, Val);
1421 /* The primary register */
1426 /* An expression in the primary register */
1427 g_addeqind (Flags, Expr->IVal, Val);
1431 Internal ("Invalid location in PreInc(): 0x%04X", ED_GetLoc (Expr));
1434 /* Result is an expression, no reference */
1435 ED_MakeRValExpr (Expr);
1440 static void PreDec (ExprDesc* Expr)
1441 /* Handle the predecrement operators */
1446 /* Skip the operator token */
1449 /* Evaluate the expression and check that it is an lvalue */
1451 if (!ED_IsLVal (Expr)) {
1452 Error ("Invalid lvalue");
1456 /* We cannot modify const values */
1457 if (IsQualConst (Expr->Type)) {
1458 Error ("Decrement of read-only variable");
1461 /* Get the data type */
1462 Flags = TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR | CF_CONST;
1464 /* Get the increment value in bytes */
1465 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1467 /* Check the location of the data */
1468 switch (ED_GetLoc (Expr)) {
1471 /* Absolute: numeric address or const */
1472 g_subeqstatic (Flags, Expr->IVal, 0, Val);
1476 /* Global variable */
1477 g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1482 /* Static variable or literal in the literal pool */
1483 g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1486 case E_LOC_REGISTER:
1487 /* Register variable */
1488 g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1492 /* Value on the stack */
1493 g_subeqlocal (Flags, Expr->IVal, Val);
1497 /* The primary register */
1502 /* An expression in the primary register */
1503 g_subeqind (Flags, Expr->IVal, Val);
1507 Internal ("Invalid location in PreDec(): 0x%04X", ED_GetLoc (Expr));
1510 /* Result is an expression, no reference */
1511 ED_MakeRValExpr (Expr);
1516 static void PostInc (ExprDesc* Expr)
1517 /* Handle the postincrement operator */
1523 /* The expression to increment must be an lvalue */
1524 if (!ED_IsLVal (Expr)) {
1525 Error ("Invalid lvalue");
1529 /* We cannot modify const values */
1530 if (IsQualConst (Expr->Type)) {
1531 Error ("Increment of read-only variable");
1534 /* Get the data type */
1535 Flags = TypeOf (Expr->Type);
1537 /* Push the address if needed */
1540 /* Fetch the value and save it (since it's the result of the expression) */
1541 LoadExpr (CF_NONE, Expr);
1542 g_save (Flags | CF_FORCECHAR);
1544 /* If we have a pointer expression, increment by the size of the type */
1545 if (IsTypePtr (Expr->Type)) {
1546 g_inc (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1548 g_inc (Flags | CF_CONST | CF_FORCECHAR, 1);
1551 /* Store the result back */
1554 /* Restore the original value in the primary register */
1555 g_restore (Flags | CF_FORCECHAR);
1557 /* The result is always an expression, no reference */
1558 ED_MakeRValExpr (Expr);
1563 static void PostDec (ExprDesc* Expr)
1564 /* Handle the postdecrement operator */
1570 /* The expression to increment must be an lvalue */
1571 if (!ED_IsLVal (Expr)) {
1572 Error ("Invalid lvalue");
1576 /* We cannot modify const values */
1577 if (IsQualConst (Expr->Type)) {
1578 Error ("Decrement of read-only variable");
1581 /* Get the data type */
1582 Flags = TypeOf (Expr->Type);
1584 /* Push the address if needed */
1587 /* Fetch the value and save it (since it's the result of the expression) */
1588 LoadExpr (CF_NONE, Expr);
1589 g_save (Flags | CF_FORCECHAR);
1591 /* If we have a pointer expression, increment by the size of the type */
1592 if (IsTypePtr (Expr->Type)) {
1593 g_dec (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1595 g_dec (Flags | CF_CONST | CF_FORCECHAR, 1);
1598 /* Store the result back */
1601 /* Restore the original value in the primary register */
1602 g_restore (Flags | CF_FORCECHAR);
1604 /* The result is always an expression, no reference */
1605 ED_MakeRValExpr (Expr);
1610 static void UnaryOp (ExprDesc* Expr)
1611 /* Handle unary -/+ and ~ */
1615 /* Remember the operator token and skip it */
1616 token_t Tok = CurTok.Tok;
1619 /* Get the expression */
1622 /* We can only handle integer types */
1623 if (!IsClassInt (Expr->Type)) {
1624 Error ("Argument must have integer type");
1625 ED_MakeConstAbsInt (Expr, 1);
1628 /* Check for a constant expression */
1629 if (ED_IsConstAbs (Expr)) {
1630 /* Value is constant */
1632 case TOK_MINUS: Expr->IVal = -Expr->IVal; break;
1633 case TOK_PLUS: break;
1634 case TOK_COMP: Expr->IVal = ~Expr->IVal; break;
1635 default: Internal ("Unexpected token: %d", Tok);
1638 /* Value is not constant */
1639 LoadExpr (CF_NONE, Expr);
1641 /* Get the type of the expression */
1642 Flags = TypeOf (Expr->Type);
1644 /* Handle the operation */
1646 case TOK_MINUS: g_neg (Flags); break;
1647 case TOK_PLUS: break;
1648 case TOK_COMP: g_com (Flags); break;
1649 default: Internal ("Unexpected token: %d", Tok);
1652 /* The result is a rvalue in the primary */
1653 ED_MakeRValExpr (Expr);
1659 void hie10 (ExprDesc* Expr)
1660 /* Handle ++, --, !, unary - etc. */
1664 switch (CurTok.Tok) {
1682 if (evalexpr (CF_NONE, hie10, Expr) == 0) {
1683 /* Constant expression */
1684 Expr->IVal = !Expr->IVal;
1686 g_bneg (TypeOf (Expr->Type));
1687 ED_MakeRValExpr (Expr);
1688 ED_TestDone (Expr); /* bneg will set cc */
1694 ExprWithCheck (hie10, Expr);
1695 if (ED_IsLVal (Expr) || !(ED_IsLocConst (Expr) || ED_IsLocStack (Expr))) {
1696 /* Not a const, load it into the primary and make it a
1697 ** calculated value.
1699 LoadExpr (CF_NONE, Expr);
1700 ED_MakeRValExpr (Expr);
1702 /* If the expression is already a pointer to function, the
1703 ** additional dereferencing operator must be ignored. A function
1704 ** itself is represented as "pointer to function", so any number
1705 ** of dereference operators is legal, since the result will
1706 ** always be converted to "pointer to function".
1708 if (IsTypeFuncPtr (Expr->Type) || IsTypeFunc (Expr->Type)) {
1709 /* Expression not storable */
1712 if (IsClassPtr (Expr->Type)) {
1713 Expr->Type = Indirect (Expr->Type);
1715 Error ("Illegal indirection");
1717 /* If the expression points to an array, then don't convert the
1718 ** address -- it already is the location of the first element.
1720 if (!IsTypeArray (Expr->Type)) {
1721 /* The * operator yields an lvalue */
1729 ExprWithCheck (hie10, Expr);
1730 /* The & operator may be applied to any lvalue, and it may be
1731 ** applied to functions, even if they're no lvalues.
1733 if (ED_IsRVal (Expr) && !IsTypeFunc (Expr->Type) && !IsTypeArray (Expr->Type)) {
1734 Error ("Illegal address");
1736 if (ED_IsBitField (Expr)) {
1737 Error ("Cannot take address of bit-field");
1738 /* Do it anyway, just to avoid further warnings */
1739 Expr->Flags &= ~E_BITFIELD;
1741 Expr->Type = PointerTo (Expr->Type);
1742 /* The & operator yields an rvalue */
1749 if (TypeSpecAhead ()) {
1752 Size = CheckedSizeOf (ParseType (T));
1755 /* Remember the output queue pointer */
1759 /* If the expression is a literal string, release it, so it
1760 ** won't be output as data if not used elsewhere.
1762 if (ED_IsLocLiteral (Expr)) {
1763 ReleaseLiteral (Expr->LVal);
1765 /* Calculate the size */
1766 Size = CheckedSizeOf (Expr->Type);
1767 /* Remove any generated code */
1770 ED_MakeConstAbs (Expr, Size, type_size_t);
1771 ED_MarkAsUntested (Expr);
1775 if (TypeSpecAhead ()) {
1785 /* Handle post increment */
1786 switch (CurTok.Tok) {
1787 case TOK_INC: PostInc (Expr); break;
1788 case TOK_DEC: PostDec (Expr); break;
1799 static void hie_internal (const GenDesc* Ops, /* List of generators */
1801 void (*hienext) (ExprDesc*),
1803 /* Helper function */
1809 token_t Tok; /* The operator token */
1810 unsigned ltype, type;
1811 int lconst; /* Left operand is a constant */
1812 int rconst; /* Right operand is a constant */
1815 ExprWithCheck (hienext, Expr);
1818 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1820 /* Tell the caller that we handled it's ops */
1823 /* All operators that call this function expect an int on the lhs */
1824 if (!IsClassInt (Expr->Type)) {
1825 Error ("Integer expression expected");
1826 /* To avoid further errors, make Expr a valid int expression */
1827 ED_MakeConstAbsInt (Expr, 1);
1830 /* Remember the operator token, then skip it */
1834 /* Get the lhs on stack */
1835 GetCodePos (&Mark1);
1836 ltype = TypeOf (Expr->Type);
1837 lconst = ED_IsConstAbs (Expr);
1839 /* Constant value */
1840 GetCodePos (&Mark2);
1841 /* If the operator is commutative, don't push the left side, if
1842 ** it's a constant, since we will exchange both operands.
1844 if ((Gen->Flags & GEN_COMM) == 0) {
1845 g_push (ltype | CF_CONST, Expr->IVal);
1848 /* Value not constant */
1849 LoadExpr (CF_NONE, Expr);
1850 GetCodePos (&Mark2);
1854 /* Get the right hand side */
1855 MarkedExprWithCheck (hienext, &Expr2);
1857 /* Check for a constant expression */
1858 rconst = (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2));
1860 /* Not constant, load into the primary */
1861 LoadExpr (CF_NONE, &Expr2);
1864 /* Check the type of the rhs */
1865 if (!IsClassInt (Expr2.Type)) {
1866 Error ("Integer expression expected");
1869 /* Check for const operands */
1870 if (lconst && rconst) {
1872 /* Both operands are constant, remove the generated code */
1873 RemoveCode (&Mark1);
1875 /* Get the type of the result */
1876 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1878 /* Handle the op differently for signed and unsigned types */
1879 if (IsSignSigned (Expr->Type)) {
1881 /* Evaluate the result for signed operands */
1882 signed long Val1 = Expr->IVal;
1883 signed long Val2 = Expr2.IVal;
1886 Expr->IVal = (Val1 | Val2);
1889 Expr->IVal = (Val1 ^ Val2);
1892 Expr->IVal = (Val1 & Val2);
1895 Expr->IVal = (Val1 * Val2);
1899 Error ("Division by zero");
1900 Expr->IVal = 0x7FFFFFFF;
1902 Expr->IVal = (Val1 / Val2);
1907 Error ("Modulo operation with zero");
1910 Expr->IVal = (Val1 % Val2);
1914 Internal ("hie_internal: got token 0x%X\n", Tok);
1918 /* Evaluate the result for unsigned operands */
1919 unsigned long Val1 = Expr->IVal;
1920 unsigned long Val2 = Expr2.IVal;
1923 Expr->IVal = (Val1 | Val2);
1926 Expr->IVal = (Val1 ^ Val2);
1929 Expr->IVal = (Val1 & Val2);
1932 Expr->IVal = (Val1 * Val2);
1936 Error ("Division by zero");
1937 Expr->IVal = 0xFFFFFFFF;
1939 Expr->IVal = (Val1 / Val2);
1944 Error ("Modulo operation with zero");
1947 Expr->IVal = (Val1 % Val2);
1951 Internal ("hie_internal: got token 0x%X\n", Tok);
1955 } else if (lconst && (Gen->Flags & GEN_COMM) && !rconst) {
1957 /* The left side is constant, the right side is not, and the
1958 ** operator allows swapping the operands. We haven't pushed the
1959 ** left side onto the stack in this case, and will reverse the
1960 ** operation because this allows for better code.
1962 unsigned rtype = ltype | CF_CONST;
1963 ltype = TypeOf (Expr2.Type); /* Expr2 is now left */
1965 if ((Gen->Flags & GEN_NOPUSH) == 0) {
1968 ltype |= CF_REG; /* Value is in register */
1971 /* Determine the type of the operation result. */
1972 type |= g_typeadjust (ltype, rtype);
1973 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1976 Gen->Func (type, Expr->IVal);
1978 /* We have a rvalue in the primary now */
1979 ED_MakeRValExpr (Expr);
1983 /* If the right hand side is constant, and the generator function
1984 ** expects the lhs in the primary, remove the push of the primary
1987 unsigned rtype = TypeOf (Expr2.Type);
1990 /* Second value is constant - check for div */
1993 if (Tok == TOK_DIV && Expr2.IVal == 0) {
1994 Error ("Division by zero");
1995 } else if (Tok == TOK_MOD && Expr2.IVal == 0) {
1996 Error ("Modulo operation with zero");
1998 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1999 RemoveCode (&Mark2);
2000 ltype |= CF_REG; /* Value is in register */
2004 /* Determine the type of the operation result. */
2005 type |= g_typeadjust (ltype, rtype);
2006 Expr->Type = promoteint (Expr->Type, Expr2.Type);
2009 Gen->Func (type, Expr2.IVal);
2011 /* We have a rvalue in the primary now */
2012 ED_MakeRValExpr (Expr);
2019 static void hie_compare (const GenDesc* Ops, /* List of generators */
2021 void (*hienext) (ExprDesc*))
2022 /* Helper function for the compare operators */
2029 token_t Tok; /* The operator token */
2031 int rconst; /* Operand is a constant */
2034 GetCodePos (&Mark0);
2035 ExprWithCheck (hienext, Expr);
2037 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
2039 /* Remember the generator function */
2040 void (*GenFunc) (unsigned, unsigned long) = Gen->Func;
2042 /* Remember the operator token, then skip it */
2046 /* If lhs is a function, convert it to pointer to function */
2047 if (IsTypeFunc (Expr->Type)) {
2048 Expr->Type = PointerTo (Expr->Type);
2051 /* Get the lhs on stack */
2052 GetCodePos (&Mark1);
2053 ltype = TypeOf (Expr->Type);
2054 if (ED_IsConstAbs (Expr)) {
2055 /* Constant value */
2056 GetCodePos (&Mark2);
2057 g_push (ltype | CF_CONST, Expr->IVal);
2059 /* Value not constant */
2060 LoadExpr (CF_NONE, Expr);
2061 GetCodePos (&Mark2);
2065 /* Get the right hand side */
2066 MarkedExprWithCheck (hienext, &Expr2);
2068 /* If rhs is a function, convert it to pointer to function */
2069 if (IsTypeFunc (Expr2.Type)) {
2070 Expr2.Type = PointerTo (Expr2.Type);
2073 /* Check for a constant expression */
2074 rconst = (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2));
2076 /* Not constant, load into the primary */
2077 LoadExpr (CF_NONE, &Expr2);
2080 /* Some operations aren't allowed on function pointers */
2081 if ((Gen->Flags & GEN_NOFUNC) != 0) {
2082 /* Output only one message even if both sides are wrong */
2083 if (IsTypeFuncPtr (Expr->Type)) {
2084 Error ("Invalid left operand for relational operator");
2085 /* Avoid further errors */
2086 ED_MakeConstAbsInt (Expr, 0);
2087 ED_MakeConstAbsInt (&Expr2, 0);
2088 } else if (IsTypeFuncPtr (Expr2.Type)) {
2089 Error ("Invalid right operand for relational operator");
2090 /* Avoid further errors */
2091 ED_MakeConstAbsInt (Expr, 0);
2092 ED_MakeConstAbsInt (&Expr2, 0);
2096 /* Make sure, the types are compatible */
2097 if (IsClassInt (Expr->Type)) {
2098 if (!IsClassInt (Expr2.Type) && !(IsClassPtr(Expr2.Type) && ED_IsNullPtr(Expr))) {
2099 Error ("Incompatible types");
2101 } else if (IsClassPtr (Expr->Type)) {
2102 if (IsClassPtr (Expr2.Type)) {
2103 /* Both pointers are allowed in comparison if they point to
2104 ** the same type, or if one of them is a void pointer.
2106 Type* left = Indirect (Expr->Type);
2107 Type* right = Indirect (Expr2.Type);
2108 if (TypeCmp (left, right) < TC_EQUAL && left->C != T_VOID && right->C != T_VOID) {
2109 /* Incomatible pointers */
2110 Error ("Incompatible types");
2112 } else if (!ED_IsNullPtr (&Expr2)) {
2113 Error ("Incompatible types");
2117 /* Check for const operands */
2118 if (ED_IsConstAbs (Expr) && rconst) {
2120 /* If the result is constant, this is suspicious when not in
2121 ** preprocessor mode.
2123 WarnConstCompareResult ();
2125 /* Both operands are constant, remove the generated code */
2126 RemoveCode (&Mark1);
2128 /* Determine if this is a signed or unsigned compare */
2129 if (IsClassInt (Expr->Type) && IsSignSigned (Expr->Type) &&
2130 IsClassInt (Expr2.Type) && IsSignSigned (Expr2.Type)) {
2132 /* Evaluate the result for signed operands */
2133 signed long Val1 = Expr->IVal;
2134 signed long Val2 = Expr2.IVal;
2136 case TOK_EQ: Expr->IVal = (Val1 == Val2); break;
2137 case TOK_NE: Expr->IVal = (Val1 != Val2); break;
2138 case TOK_LT: Expr->IVal = (Val1 < Val2); break;
2139 case TOK_LE: Expr->IVal = (Val1 <= Val2); break;
2140 case TOK_GE: Expr->IVal = (Val1 >= Val2); break;
2141 case TOK_GT: Expr->IVal = (Val1 > Val2); break;
2142 default: Internal ("hie_compare: got token 0x%X\n", Tok);
2147 /* Evaluate the result for unsigned operands */
2148 unsigned long Val1 = Expr->IVal;
2149 unsigned long Val2 = Expr2.IVal;
2151 case TOK_EQ: Expr->IVal = (Val1 == Val2); break;
2152 case TOK_NE: Expr->IVal = (Val1 != Val2); break;
2153 case TOK_LT: Expr->IVal = (Val1 < Val2); break;
2154 case TOK_LE: Expr->IVal = (Val1 <= Val2); break;
2155 case TOK_GE: Expr->IVal = (Val1 >= Val2); break;
2156 case TOK_GT: Expr->IVal = (Val1 > Val2); break;
2157 default: Internal ("hie_compare: got token 0x%X\n", Tok);
2163 /* Determine the signedness of the operands */
2164 int LeftSigned = IsSignSigned (Expr->Type);
2165 int RightSigned = IsSignSigned (Expr2.Type);
2167 /* If the right hand side is constant, and the generator function
2168 ** expects the lhs in the primary, remove the push of the primary
2174 if ((Gen->Flags & GEN_NOPUSH) != 0) {
2175 RemoveCode (&Mark2);
2176 ltype |= CF_REG; /* Value is in register */
2180 /* Determine the type of the operation. */
2181 if (IsTypeChar (Expr->Type) && rconst) {
2183 /* Left side is unsigned char, right side is constant.
2184 ** Determine the minimum and maximum values
2186 int LeftMin, LeftMax;
2194 /* An integer value is always represented as a signed in the
2195 ** ExprDesc structure. This may lead to false results below,
2196 ** if it is actually unsigned, but interpreted as signed
2197 ** because of the representation. Fortunately, in this case,
2198 ** the actual value doesn't matter, since it's always greater
2199 ** than what can be represented in a char. So correct the
2200 ** value accordingly.
2202 if (!RightSigned && Expr2.IVal < 0) {
2203 /* Correct the value so it is an unsigned. It will then
2204 ** anyway match one of the cases below.
2206 Expr2.IVal = LeftMax + 1;
2209 /* Comparing a char against a constant may have a constant
2210 ** result. Please note: It is not possible to remove the code
2211 ** for the compare alltogether, because it may have side
2217 if (Expr2.IVal < LeftMin || Expr2.IVal > LeftMax) {
2218 ED_MakeConstAbsInt (Expr, 0);
2219 WarnConstCompareResult ();
2225 if (Expr2.IVal < LeftMin || Expr2.IVal > LeftMax) {
2226 ED_MakeConstAbsInt (Expr, 1);
2227 WarnConstCompareResult ();
2233 if (Expr2.IVal <= LeftMin || Expr2.IVal > LeftMax) {
2234 ED_MakeConstAbsInt (Expr, Expr2.IVal > LeftMax);
2235 WarnConstCompareResult ();
2241 if (Expr2.IVal < LeftMin || Expr2.IVal >= LeftMax) {
2242 ED_MakeConstAbsInt (Expr, Expr2.IVal >= LeftMax);
2243 WarnConstCompareResult ();
2249 if (Expr2.IVal <= LeftMin || Expr2.IVal > LeftMax) {
2250 ED_MakeConstAbsInt (Expr, Expr2.IVal <= LeftMin);
2251 WarnConstCompareResult ();
2257 if (Expr2.IVal < LeftMin || Expr2.IVal >= LeftMax) {
2258 ED_MakeConstAbsInt (Expr, Expr2.IVal < LeftMin);
2259 WarnConstCompareResult ();
2265 Internal ("hie_compare: got token 0x%X\n", Tok);
2268 /* If the result is not already constant (as evaluated in the
2269 ** switch above), we can execute the operation as a char op,
2270 ** since the right side constant is in a valid range.
2272 flags |= (CF_CHAR | CF_FORCECHAR);
2274 flags |= CF_UNSIGNED;
2277 } else if (IsTypeChar (Expr->Type) && IsTypeChar (Expr2.Type) &&
2278 GetSignedness (Expr->Type) == GetSignedness (Expr2.Type)) {
2280 /* Both are chars with the same signedness. We can encode the
2281 ** operation as a char operation.
2285 flags |= CF_FORCECHAR;
2288 flags |= CF_UNSIGNED;
2291 unsigned rtype = TypeOf (Expr2.Type) | (flags & CF_CONST);
2292 flags |= g_typeadjust (ltype, rtype);
2295 /* If the left side is an unsigned and the right is a constant,
2296 ** we may be able to change the compares to something more
2299 if (!LeftSigned && rconst) {
2304 if (Expr2.IVal == 1) {
2305 /* An unsigned compare to one means that the value
2314 if (Expr2.IVal == 0) {
2315 /* An unsigned compare to zero means that the value
2323 if (Expr2.IVal == 1) {
2324 /* An unsigned compare to one means that the value
2325 ** must not be zero.
2333 if (Expr2.IVal == 0) {
2334 /* An unsigned compare to zero means that the value
2335 ** must not be zero.
2349 GenFunc (flags, Expr2.IVal);
2351 /* The result is an rvalue in the primary */
2352 ED_MakeRValExpr (Expr);
2355 /* Result type is always int */
2356 Expr->Type = type_int;
2358 Done: /* Condition codes are set */
2365 static void hie9 (ExprDesc *Expr)
2366 /* Process * and / operators. */
2368 static const GenDesc hie9_ops[] = {
2369 { TOK_STAR, GEN_NOPUSH | GEN_COMM, g_mul },
2370 { TOK_DIV, GEN_NOPUSH, g_div },
2371 { TOK_MOD, GEN_NOPUSH, g_mod },
2372 { TOK_INVALID, 0, 0 }
2376 hie_internal (hie9_ops, Expr, hie10, &UsedGen);
2381 static void parseadd (ExprDesc* Expr)
2382 /* Parse an expression with the binary plus operator. Expr contains the
2383 ** unprocessed left hand side of the expression and will contain the
2384 ** result of the expression on return.
2388 unsigned flags; /* Operation flags */
2389 CodeMark Mark; /* Remember code position */
2390 Type* lhst; /* Type of left hand side */
2391 Type* rhst; /* Type of right hand side */
2394 /* Skip the PLUS token */
2397 /* Get the left hand side type, initialize operation flags */
2401 /* Check for constness on both sides */
2402 if (ED_IsConst (Expr)) {
2404 /* The left hand side is a constant of some sort. Good. Get rhs */
2405 ExprWithCheck (hie9, &Expr2);
2406 if (ED_IsConstAbs (&Expr2)) {
2408 /* Right hand side is a constant numeric value. Get the rhs type */
2411 /* Both expressions are constants. Check for pointer arithmetic */
2412 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2413 /* Left is pointer, right is int, must scale rhs */
2414 Expr->IVal += Expr2.IVal * CheckedPSizeOf (lhst);
2415 /* Result type is a pointer */
2416 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2417 /* Left is int, right is pointer, must scale lhs */
2418 Expr->IVal = Expr->IVal * CheckedPSizeOf (rhst) + Expr2.IVal;
2419 /* Result type is a pointer */
2420 Expr->Type = Expr2.Type;
2421 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2422 /* Integer addition */
2423 Expr->IVal += Expr2.IVal;
2424 typeadjust (Expr, &Expr2, 1);
2427 Error ("Invalid operands for binary operator `+'");
2432 /* lhs is a constant and rhs is not constant. Load rhs into
2435 LoadExpr (CF_NONE, &Expr2);
2437 /* Beware: The check above (for lhs) lets not only pass numeric
2438 ** constants, but also constant addresses (labels), maybe even
2439 ** with an offset. We have to check for that here.
2442 /* First, get the rhs type. */
2446 if (ED_IsLocAbs (Expr)) {
2447 /* A numerical constant */
2450 /* Constant address label */
2451 flags |= GlobalModeFlags (Expr) | CF_CONSTADDR;
2454 /* Check for pointer arithmetic */
2455 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2456 /* Left is pointer, right is int, must scale rhs */
2457 g_scale (CF_INT, CheckedPSizeOf (lhst));
2458 /* Operate on pointers, result type is a pointer */
2460 /* Generate the code for the add */
2461 if (ED_GetLoc (Expr) == E_LOC_ABS) {
2462 /* Numeric constant */
2463 g_inc (flags, Expr->IVal);
2465 /* Constant address */
2466 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2468 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2470 /* Left is int, right is pointer, must scale lhs. */
2471 unsigned ScaleFactor = CheckedPSizeOf (rhst);
2473 /* Operate on pointers, result type is a pointer */
2475 Expr->Type = Expr2.Type;
2477 /* Since we do already have rhs in the primary, if lhs is
2478 ** not a numeric constant, and the scale factor is not one
2479 ** (no scaling), we must take the long way over the stack.
2481 if (ED_IsLocAbs (Expr)) {
2482 /* Numeric constant, scale lhs */
2483 Expr->IVal *= ScaleFactor;
2484 /* Generate the code for the add */
2485 g_inc (flags, Expr->IVal);
2486 } else if (ScaleFactor == 1) {
2487 /* Constant address but no need to scale */
2488 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2490 /* Constant address that must be scaled */
2491 g_push (TypeOf (Expr2.Type), 0); /* rhs --> stack */
2492 g_getimmed (flags, Expr->Name, Expr->IVal);
2493 g_scale (CF_PTR, ScaleFactor);
2496 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2497 /* Integer addition */
2498 flags |= typeadjust (Expr, &Expr2, 1);
2499 /* Generate the code for the add */
2500 if (ED_IsLocAbs (Expr)) {
2501 /* Numeric constant */
2502 g_inc (flags, Expr->IVal);
2504 /* Constant address */
2505 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2509 Error ("Invalid operands for binary operator `+'");
2513 /* Result is a rvalue in primary register */
2514 ED_MakeRValExpr (Expr);
2519 /* Left hand side is not constant. Get the value onto the stack. */
2520 LoadExpr (CF_NONE, Expr); /* --> primary register */
2522 g_push (TypeOf (Expr->Type), 0); /* --> stack */
2524 /* Evaluate the rhs */
2525 MarkedExprWithCheck (hie9, &Expr2);
2527 /* Check for a constant rhs expression */
2528 if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
2530 /* Right hand side is a constant. Get the rhs type */
2533 /* Remove pushed value from stack */
2536 /* Check for pointer arithmetic */
2537 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2538 /* Left is pointer, right is int, must scale rhs */
2539 Expr2.IVal *= CheckedPSizeOf (lhst);
2540 /* Operate on pointers, result type is a pointer */
2542 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2543 /* Left is int, right is pointer, must scale lhs (ptr only) */
2544 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2545 /* Operate on pointers, result type is a pointer */
2547 Expr->Type = Expr2.Type;
2548 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2549 /* Integer addition */
2550 flags = typeadjust (Expr, &Expr2, 1);
2553 Error ("Invalid operands for binary operator `+'");
2557 /* Generate code for the add */
2558 g_inc (flags | CF_CONST, Expr2.IVal);
2562 /* Not constant, load into the primary */
2563 LoadExpr (CF_NONE, &Expr2);
2565 /* lhs and rhs are not constant. Get the rhs type. */
2568 /* Check for pointer arithmetic */
2569 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2570 /* Left is pointer, right is int, must scale rhs */
2571 g_scale (CF_INT, CheckedPSizeOf (lhst));
2572 /* Operate on pointers, result type is a pointer */
2574 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2575 /* Left is int, right is pointer, must scale lhs */
2576 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2577 g_swap (CF_INT); /* Swap TOS and primary */
2578 g_scale (CF_INT, CheckedPSizeOf (rhst));
2579 /* Operate on pointers, result type is a pointer */
2581 Expr->Type = Expr2.Type;
2582 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2583 /* Integer addition. Note: Result is never constant.
2584 ** Problem here is that typeadjust does not know if the
2585 ** variable is an rvalue or lvalue, so if both operands
2586 ** are dereferenced constant numeric addresses, typeadjust
2587 ** thinks the operation works on constants. Removing
2588 ** CF_CONST here means handling the symptoms, however, the
2589 ** whole parser is such a mess that I fear to break anything
2590 ** when trying to apply another solution.
2592 flags = typeadjust (Expr, &Expr2, 0) & ~CF_CONST;
2595 Error ("Invalid operands for binary operator `+'");
2599 /* Generate code for the add */
2604 /* Result is a rvalue in primary register */
2605 ED_MakeRValExpr (Expr);
2608 /* Condition codes not set */
2609 ED_MarkAsUntested (Expr);
2615 static void parsesub (ExprDesc* Expr)
2616 /* Parse an expression with the binary minus operator. Expr contains the
2617 ** unprocessed left hand side of the expression and will contain the
2618 ** result of the expression on return.
2622 unsigned flags; /* Operation flags */
2623 Type* lhst; /* Type of left hand side */
2624 Type* rhst; /* Type of right hand side */
2625 CodeMark Mark1; /* Save position of output queue */
2626 CodeMark Mark2; /* Another position in the queue */
2627 int rscale; /* Scale factor for the result */
2630 /* lhs cannot be function or pointer to function */
2631 if (IsTypeFunc (Expr->Type) || IsTypeFuncPtr (Expr->Type)) {
2632 Error ("Invalid left operand for binary operator `-'");
2633 /* Make it pointer to char to avoid further errors */
2634 Expr->Type = type_uchar;
2637 /* Skip the MINUS token */
2640 /* Get the left hand side type, initialize operation flags */
2642 rscale = 1; /* Scale by 1, that is, don't scale */
2644 /* Remember the output queue position, then bring the value onto the stack */
2645 GetCodePos (&Mark1);
2646 LoadExpr (CF_NONE, Expr); /* --> primary register */
2647 GetCodePos (&Mark2);
2648 g_push (TypeOf (lhst), 0); /* --> stack */
2650 /* Parse the right hand side */
2651 MarkedExprWithCheck (hie9, &Expr2);
2653 /* rhs cannot be function or pointer to function */
2654 if (IsTypeFunc (Expr2.Type) || IsTypeFuncPtr (Expr2.Type)) {
2655 Error ("Invalid right operand for binary operator `-'");
2656 /* Make it pointer to char to avoid further errors */
2657 Expr2.Type = type_uchar;
2660 /* Check for a constant rhs expression */
2661 if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
2663 /* The right hand side is constant. Get the rhs type. */
2666 /* Check left hand side */
2667 if (ED_IsConstAbs (Expr)) {
2669 /* Both sides are constant, remove generated code */
2670 RemoveCode (&Mark1);
2672 /* Check for pointer arithmetic */
2673 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2674 /* Left is pointer, right is int, must scale rhs */
2675 Expr->IVal -= Expr2.IVal * CheckedPSizeOf (lhst);
2676 /* Operate on pointers, result type is a pointer */
2677 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2678 /* Left is pointer, right is pointer, must scale result */
2679 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2680 Error ("Incompatible pointer types");
2682 Expr->IVal = (Expr->IVal - Expr2.IVal) /
2683 CheckedPSizeOf (lhst);
2685 /* Operate on pointers, result type is an integer */
2686 Expr->Type = type_int;
2687 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2688 /* Integer subtraction */
2689 typeadjust (Expr, &Expr2, 1);
2690 Expr->IVal -= Expr2.IVal;
2693 Error ("Invalid operands for binary operator `-'");
2696 /* Result is constant, condition codes not set */
2697 ED_MarkAsUntested (Expr);
2701 /* Left hand side is not constant, right hand side is.
2702 ** Remove pushed value from stack.
2704 RemoveCode (&Mark2);
2706 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2707 /* Left is pointer, right is int, must scale rhs */
2708 Expr2.IVal *= CheckedPSizeOf (lhst);
2709 /* Operate on pointers, result type is a pointer */
2711 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2712 /* Left is pointer, right is pointer, must scale result */
2713 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2714 Error ("Incompatible pointer types");
2716 rscale = CheckedPSizeOf (lhst);
2718 /* Operate on pointers, result type is an integer */
2720 Expr->Type = type_int;
2721 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2722 /* Integer subtraction */
2723 flags = typeadjust (Expr, &Expr2, 1);
2726 Error ("Invalid operands for binary operator `-'");
2730 /* Do the subtraction */
2731 g_dec (flags | CF_CONST, Expr2.IVal);
2733 /* If this was a pointer subtraction, we must scale the result */
2735 g_scale (flags, -rscale);
2738 /* Result is a rvalue in the primary register */
2739 ED_MakeRValExpr (Expr);
2740 ED_MarkAsUntested (Expr);
2746 /* Not constant, load into the primary */
2747 LoadExpr (CF_NONE, &Expr2);
2749 /* Right hand side is not constant. Get the rhs type. */
2752 /* Check for pointer arithmetic */
2753 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2754 /* Left is pointer, right is int, must scale rhs */
2755 g_scale (CF_INT, CheckedPSizeOf (lhst));
2756 /* Operate on pointers, result type is a pointer */
2758 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2759 /* Left is pointer, right is pointer, must scale result */
2760 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2761 Error ("Incompatible pointer types");
2763 rscale = CheckedPSizeOf (lhst);
2765 /* Operate on pointers, result type is an integer */
2767 Expr->Type = type_int;
2768 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2769 /* Integer subtraction. If the left hand side descriptor says that
2770 ** the lhs is const, we have to remove this mark, since this is no
2771 ** longer true, lhs is on stack instead.
2773 if (ED_IsLocAbs (Expr)) {
2774 ED_MakeRValExpr (Expr);
2776 /* Adjust operand types */
2777 flags = typeadjust (Expr, &Expr2, 0);
2780 Error ("Invalid operands for binary operator `-'");
2784 /* Generate code for the sub (the & is a hack here) */
2785 g_sub (flags & ~CF_CONST, 0);
2787 /* If this was a pointer subtraction, we must scale the result */
2789 g_scale (flags, -rscale);
2792 /* Result is a rvalue in the primary register */
2793 ED_MakeRValExpr (Expr);
2794 ED_MarkAsUntested (Expr);
2800 void hie8 (ExprDesc* Expr)
2801 /* Process + and - binary operators. */
2803 ExprWithCheck (hie9, Expr);
2804 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2805 if (CurTok.Tok == TOK_PLUS) {
2815 static void hie6 (ExprDesc* Expr)
2816 /* Handle greater-than type comparators */
2818 static const GenDesc hie6_ops [] = {
2819 { TOK_LT, GEN_NOPUSH | GEN_NOFUNC, g_lt },
2820 { TOK_LE, GEN_NOPUSH | GEN_NOFUNC, g_le },
2821 { TOK_GE, GEN_NOPUSH | GEN_NOFUNC, g_ge },
2822 { TOK_GT, GEN_NOPUSH | GEN_NOFUNC, g_gt },
2823 { TOK_INVALID, 0, 0 }
2825 hie_compare (hie6_ops, Expr, ShiftExpr);
2830 static void hie5 (ExprDesc* Expr)
2831 /* Handle == and != */
2833 static const GenDesc hie5_ops[] = {
2834 { TOK_EQ, GEN_NOPUSH, g_eq },
2835 { TOK_NE, GEN_NOPUSH, g_ne },
2836 { TOK_INVALID, 0, 0 }
2838 hie_compare (hie5_ops, Expr, hie6);
2843 static void hie4 (ExprDesc* Expr)
2844 /* Handle & (bitwise and) */
2846 static const GenDesc hie4_ops[] = {
2847 { TOK_AND, GEN_NOPUSH | GEN_COMM, g_and },
2848 { TOK_INVALID, 0, 0 }
2852 hie_internal (hie4_ops, Expr, hie5, &UsedGen);
2857 static void hie3 (ExprDesc* Expr)
2858 /* Handle ^ (bitwise exclusive or) */
2860 static const GenDesc hie3_ops[] = {
2861 { TOK_XOR, GEN_NOPUSH | GEN_COMM, g_xor },
2862 { TOK_INVALID, 0, 0 }
2866 hie_internal (hie3_ops, Expr, hie4, &UsedGen);
2871 static void hie2 (ExprDesc* Expr)
2872 /* Handle | (bitwise or) */
2874 static const GenDesc hie2_ops[] = {
2875 { TOK_OR, GEN_NOPUSH | GEN_COMM, g_or },
2876 { TOK_INVALID, 0, 0 }
2880 hie_internal (hie2_ops, Expr, hie3, &UsedGen);
2885 static void hieAndPP (ExprDesc* Expr)
2886 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2887 ** called recursively from the preprocessor.
2892 ConstAbsIntExpr (hie2, Expr);
2893 while (CurTok.Tok == TOK_BOOL_AND) {
2899 ConstAbsIntExpr (hie2, &Expr2);
2901 /* Combine the two */
2902 Expr->IVal = (Expr->IVal && Expr2.IVal);
2908 static void hieOrPP (ExprDesc *Expr)
2909 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2910 ** called recursively from the preprocessor.
2915 ConstAbsIntExpr (hieAndPP, Expr);
2916 while (CurTok.Tok == TOK_BOOL_OR) {
2922 ConstAbsIntExpr (hieAndPP, &Expr2);
2924 /* Combine the two */
2925 Expr->IVal = (Expr->IVal || Expr2.IVal);
2931 static void hieAnd (ExprDesc* Expr, unsigned TrueLab, int* BoolOp)
2932 /* Process "exp && exp" */
2937 ExprWithCheck (hie2, Expr);
2938 if (CurTok.Tok == TOK_BOOL_AND) {
2940 /* Tell our caller that we're evaluating a boolean */
2943 /* Get a label that we will use for false expressions */
2944 FalseLab = GetLocalLabel ();
2946 /* If the expr hasn't set condition codes, set the force-test flag */
2947 if (!ED_IsTested (Expr)) {
2948 ED_MarkForTest (Expr);
2951 /* Load the value */
2952 LoadExpr (CF_FORCECHAR, Expr);
2954 /* Generate the jump */
2955 g_falsejump (CF_NONE, FalseLab);
2957 /* Parse more boolean and's */
2958 while (CurTok.Tok == TOK_BOOL_AND) {
2965 if (!ED_IsTested (&Expr2)) {
2966 ED_MarkForTest (&Expr2);
2968 LoadExpr (CF_FORCECHAR, &Expr2);
2970 /* Do short circuit evaluation */
2971 if (CurTok.Tok == TOK_BOOL_AND) {
2972 g_falsejump (CF_NONE, FalseLab);
2974 /* Last expression - will evaluate to true */
2975 g_truejump (CF_NONE, TrueLab);
2979 /* Define the false jump label here */
2980 g_defcodelabel (FalseLab);
2982 /* The result is an rvalue in primary */
2983 ED_MakeRValExpr (Expr);
2984 ED_TestDone (Expr); /* Condition codes are set */
2990 static void hieOr (ExprDesc *Expr)
2991 /* Process "exp || exp". */
2994 int BoolOp = 0; /* Did we have a boolean op? */
2995 int AndOp; /* Did we have a && operation? */
2996 unsigned TrueLab; /* Jump to this label if true */
3000 TrueLab = GetLocalLabel ();
3002 /* Call the next level parser */
3003 hieAnd (Expr, TrueLab, &BoolOp);
3005 /* Any boolean or's? */
3006 if (CurTok.Tok == TOK_BOOL_OR) {
3008 /* If the expr hasn't set condition codes, set the force-test flag */
3009 if (!ED_IsTested (Expr)) {
3010 ED_MarkForTest (Expr);
3013 /* Get first expr */
3014 LoadExpr (CF_FORCECHAR, Expr);
3016 /* For each expression jump to TrueLab if true. Beware: If we
3017 ** had && operators, the jump is already in place!
3020 g_truejump (CF_NONE, TrueLab);
3023 /* Remember that we had a boolean op */
3026 /* while there's more expr */
3027 while (CurTok.Tok == TOK_BOOL_OR) {
3034 hieAnd (&Expr2, TrueLab, &AndOp);
3035 if (!ED_IsTested (&Expr2)) {
3036 ED_MarkForTest (&Expr2);
3038 LoadExpr (CF_FORCECHAR, &Expr2);
3040 /* If there is more to come, add shortcut boolean eval. */
3041 g_truejump (CF_NONE, TrueLab);
3045 /* The result is an rvalue in primary */
3046 ED_MakeRValExpr (Expr);
3047 ED_TestDone (Expr); /* Condition codes are set */
3050 /* If we really had boolean ops, generate the end sequence */
3052 DoneLab = GetLocalLabel ();
3053 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
3054 g_falsejump (CF_NONE, DoneLab);
3055 g_defcodelabel (TrueLab);
3056 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
3057 g_defcodelabel (DoneLab);
3063 static void hieQuest (ExprDesc* Expr)
3064 /* Parse the ternary operator */
3068 CodeMark TrueCodeEnd;
3069 ExprDesc Expr2; /* Expression 2 */
3070 ExprDesc Expr3; /* Expression 3 */
3071 int Expr2IsNULL; /* Expression 2 is a NULL pointer */
3072 int Expr3IsNULL; /* Expression 3 is a NULL pointer */
3073 Type* ResultType; /* Type of result */
3076 /* Call the lower level eval routine */
3077 if (Preprocessing) {
3078 ExprWithCheck (hieOrPP, Expr);
3080 ExprWithCheck (hieOr, Expr);
3083 /* Check if it's a ternary expression */
3084 if (CurTok.Tok == TOK_QUEST) {
3086 if (!ED_IsTested (Expr)) {
3087 /* Condition codes not set, request a test */
3088 ED_MarkForTest (Expr);
3090 LoadExpr (CF_NONE, Expr);
3091 FalseLab = GetLocalLabel ();
3092 g_falsejump (CF_NONE, FalseLab);
3094 /* Parse second expression. Remember for later if it is a NULL pointer
3095 ** expression, then load it into the primary.
3097 ExprWithCheck (hie1, &Expr2);
3098 Expr2IsNULL = ED_IsNullPtr (&Expr2);
3099 if (!IsTypeVoid (Expr2.Type)) {
3100 /* Load it into the primary */
3101 LoadExpr (CF_NONE, &Expr2);
3102 ED_MakeRValExpr (&Expr2);
3103 Expr2.Type = PtrConversion (Expr2.Type);
3106 /* Remember the current code position */
3107 GetCodePos (&TrueCodeEnd);
3109 /* Jump around the evaluation of the third expression */
3110 TrueLab = GetLocalLabel ();
3114 /* Jump here if the first expression was false */
3115 g_defcodelabel (FalseLab);
3117 /* Parse third expression. Remember for later if it is a NULL pointer
3118 ** expression, then load it into the primary.
3120 ExprWithCheck (hie1, &Expr3);
3121 Expr3IsNULL = ED_IsNullPtr (&Expr3);
3122 if (!IsTypeVoid (Expr3.Type)) {
3123 /* Load it into the primary */
3124 LoadExpr (CF_NONE, &Expr3);
3125 ED_MakeRValExpr (&Expr3);
3126 Expr3.Type = PtrConversion (Expr3.Type);
3129 /* Check if any conversions are needed, if so, do them.
3130 ** Conversion rules for ?: expression are:
3131 ** - if both expressions are int expressions, default promotion
3132 ** rules for ints apply.
3133 ** - if both expressions are pointers of the same type, the
3134 ** result of the expression is of this type.
3135 ** - if one of the expressions is a pointer and the other is
3136 ** a zero constant, the resulting type is that of the pointer
3138 ** - if both expressions are void expressions, the result is of
3140 ** - all other cases are flagged by an error.
3142 if (IsClassInt (Expr2.Type) && IsClassInt (Expr3.Type)) {
3144 CodeMark CvtCodeStart;
3145 CodeMark CvtCodeEnd;
3148 /* Get common type */
3149 ResultType = promoteint (Expr2.Type, Expr3.Type);
3151 /* Convert the third expression to this type if needed */
3152 TypeConversion (&Expr3, ResultType);
3154 /* Emit conversion code for the second expression, but remember
3155 ** where it starts end ends.
3157 GetCodePos (&CvtCodeStart);
3158 TypeConversion (&Expr2, ResultType);
3159 GetCodePos (&CvtCodeEnd);
3161 /* If we had conversion code, move it to the right place */
3162 if (!CodeRangeIsEmpty (&CvtCodeStart, &CvtCodeEnd)) {
3163 MoveCode (&CvtCodeStart, &CvtCodeEnd, &TrueCodeEnd);
3166 } else if (IsClassPtr (Expr2.Type) && IsClassPtr (Expr3.Type)) {
3167 /* Must point to same type */
3168 if (TypeCmp (Indirect (Expr2.Type), Indirect (Expr3.Type)) < TC_EQUAL) {
3169 Error ("Incompatible pointer types");
3171 /* Result has the common type */
3172 ResultType = Expr2.Type;
3173 } else if (IsClassPtr (Expr2.Type) && Expr3IsNULL) {
3174 /* Result type is pointer, no cast needed */
3175 ResultType = Expr2.Type;
3176 } else if (Expr2IsNULL && IsClassPtr (Expr3.Type)) {
3177 /* Result type is pointer, no cast needed */
3178 ResultType = Expr3.Type;
3179 } else if (IsTypeVoid (Expr2.Type) && IsTypeVoid (Expr3.Type)) {
3180 /* Result type is void */
3181 ResultType = Expr3.Type;
3183 Error ("Incompatible types");
3184 ResultType = Expr2.Type; /* Doesn't matter here */
3187 /* Define the final label */
3188 g_defcodelabel (TrueLab);
3190 /* Setup the target expression */
3191 ED_MakeRValExpr (Expr);
3192 Expr->Type = ResultType;
3198 static void opeq (const GenDesc* Gen, ExprDesc* Expr, const char* Op)
3199 /* Process "op=" operators. */
3206 /* op= can only be used with lvalues */
3207 if (!ED_IsLVal (Expr)) {
3208 Error ("Invalid lvalue in assignment");
3212 /* The left side must not be const qualified */
3213 if (IsQualConst (Expr->Type)) {
3214 Error ("Assignment to const");
3217 /* There must be an integer or pointer on the left side */
3218 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
3219 Error ("Invalid left operand type");
3220 /* Continue. Wrong code will be generated, but the compiler won't
3221 ** break, so this is the best error recovery.
3225 /* Skip the operator token */
3228 /* Determine the type of the lhs */
3229 flags = TypeOf (Expr->Type);
3230 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) && IsTypePtr (Expr->Type);
3232 /* Get the lhs address on stack (if needed) */
3235 /* Fetch the lhs into the primary register if needed */
3236 LoadExpr (CF_NONE, Expr);
3238 /* Bring the lhs on stack */
3242 /* Evaluate the rhs */
3243 MarkedExprWithCheck (hie1, &Expr2);
3245 /* The rhs must be an integer (or a float, but we don't support that yet */
3246 if (!IsClassInt (Expr2.Type)) {
3247 Error ("Invalid right operand for binary operator `%s'", Op);
3248 /* Continue. Wrong code will be generated, but the compiler won't
3249 ** break, so this is the best error recovery.
3253 /* Check for a constant expression */
3254 if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
3255 /* The resulting value is a constant. If the generator has the NOPUSH
3256 ** flag set, don't push the lhs.
3258 if (Gen->Flags & GEN_NOPUSH) {
3262 /* lhs is a pointer, scale rhs */
3263 Expr2.IVal *= CheckedSizeOf (Expr->Type+1);
3266 /* If the lhs is character sized, the operation may be later done
3269 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
3270 flags |= CF_FORCECHAR;
3273 /* Special handling for add and sub - some sort of a hack, but short code */
3274 if (Gen->Func == g_add) {
3275 g_inc (flags | CF_CONST, Expr2.IVal);
3276 } else if (Gen->Func == g_sub) {
3277 g_dec (flags | CF_CONST, Expr2.IVal);
3279 if (Expr2.IVal == 0) {
3280 /* Check for div by zero/mod by zero */
3281 if (Gen->Func == g_div) {
3282 Error ("Division by zero");
3283 } else if (Gen->Func == g_mod) {
3284 Error ("Modulo operation with zero");
3287 Gen->Func (flags | CF_CONST, Expr2.IVal);
3291 /* rhs is not constant. Load into the primary */
3292 LoadExpr (CF_NONE, &Expr2);
3294 /* lhs is a pointer, scale rhs */
3295 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Expr->Type+1));
3298 /* If the lhs is character sized, the operation may be later done
3301 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
3302 flags |= CF_FORCECHAR;
3305 /* Adjust the types of the operands if needed */
3306 Gen->Func (g_typeadjust (flags, TypeOf (Expr2.Type)), 0);
3309 ED_MakeRValExpr (Expr);
3314 static void addsubeq (const GenDesc* Gen, ExprDesc *Expr, const char* Op)
3315 /* Process the += and -= operators */
3323 /* We're currently only able to handle some adressing modes */
3324 if (ED_GetLoc (Expr) == E_LOC_EXPR || ED_GetLoc (Expr) == E_LOC_PRIMARY) {
3325 /* Use generic routine */
3326 opeq (Gen, Expr, Op);
3330 /* We must have an lvalue */
3331 if (ED_IsRVal (Expr)) {
3332 Error ("Invalid lvalue in assignment");
3336 /* The left side must not be const qualified */
3337 if (IsQualConst (Expr->Type)) {
3338 Error ("Assignment to const");
3341 /* There must be an integer or pointer on the left side */
3342 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
3343 Error ("Invalid left operand type");
3344 /* Continue. Wrong code will be generated, but the compiler won't
3345 ** break, so this is the best error recovery.
3349 /* Skip the operator */
3352 /* Check if we have a pointer expression and must scale rhs */
3353 MustScale = IsTypePtr (Expr->Type);
3355 /* Initialize the code generator flags */
3359 /* Evaluate the rhs. We expect an integer here, since float is not
3363 if (!IsClassInt (Expr2.Type)) {
3364 Error ("Invalid right operand for binary operator `%s'", Op);
3365 /* Continue. Wrong code will be generated, but the compiler won't
3366 ** break, so this is the best error recovery.
3369 if (ED_IsConstAbs (&Expr2)) {
3370 /* The resulting value is a constant. Scale it. */
3372 Expr2.IVal *= CheckedSizeOf (Indirect (Expr->Type));
3377 /* Not constant, load into the primary */
3378 LoadExpr (CF_NONE, &Expr2);
3380 /* lhs is a pointer, scale rhs */
3381 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Indirect (Expr->Type)));
3385 /* Setup the code generator flags */
3386 lflags |= TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR;
3387 rflags |= TypeOf (Expr2.Type) | CF_FORCECHAR;
3389 /* Convert the type of the lhs to that of the rhs */
3390 g_typecast (lflags, rflags);
3392 /* Output apropriate code depending on the location */
3393 switch (ED_GetLoc (Expr)) {
3396 /* Absolute: numeric address or const */
3397 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3398 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3400 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3405 /* Global variable */
3406 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3407 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3409 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3415 /* Static variable or literal in the literal pool */
3416 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3417 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3419 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3423 case E_LOC_REGISTER:
3424 /* Register variable */
3425 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3426 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3428 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3433 /* Value on the stack */
3434 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3435 g_addeqlocal (lflags, Expr->IVal, Expr2.IVal);
3437 g_subeqlocal (lflags, Expr->IVal, Expr2.IVal);
3442 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
3445 /* Expression is a rvalue in the primary now */
3446 ED_MakeRValExpr (Expr);
3451 void hie1 (ExprDesc* Expr)
3452 /* Parse first level of expression hierarchy. */
3455 switch (CurTok.Tok) {
3461 case TOK_PLUS_ASSIGN:
3462 addsubeq (&GenPASGN, Expr, "+=");
3465 case TOK_MINUS_ASSIGN:
3466 addsubeq (&GenSASGN, Expr, "-=");
3469 case TOK_MUL_ASSIGN:
3470 opeq (&GenMASGN, Expr, "*=");
3473 case TOK_DIV_ASSIGN:
3474 opeq (&GenDASGN, Expr, "/=");
3477 case TOK_MOD_ASSIGN:
3478 opeq (&GenMOASGN, Expr, "%=");
3481 case TOK_SHL_ASSIGN:
3482 opeq (&GenSLASGN, Expr, "<<=");
3485 case TOK_SHR_ASSIGN:
3486 opeq (&GenSRASGN, Expr, ">>=");
3489 case TOK_AND_ASSIGN:
3490 opeq (&GenAASGN, Expr, "&=");
3493 case TOK_XOR_ASSIGN:
3494 opeq (&GenXOASGN, Expr, "^=");
3498 opeq (&GenOASGN, Expr, "|=");
3508 void hie0 (ExprDesc *Expr)
3509 /* Parse comma operator. */
3512 while (CurTok.Tok == TOK_COMMA) {
3520 int evalexpr (unsigned Flags, void (*Func) (ExprDesc*), ExprDesc* Expr)
3521 /* Will evaluate an expression via the given function. If the result is a
3522 ** constant, 0 is returned and the value is put in the Expr struct. If the
3523 ** result is not constant, LoadExpr is called to bring the value into the
3524 ** primary register and 1 is returned.
3528 ExprWithCheck (Func, Expr);
3530 /* Check for a constant expression */
3531 if (ED_IsConstAbs (Expr)) {
3532 /* Constant expression */
3535 /* Not constant, load into the primary */
3536 LoadExpr (Flags, Expr);
3543 void Expression0 (ExprDesc* Expr)
3544 /* Evaluate an expression via hie0 and put the result into the primary register */
3546 ExprWithCheck (hie0, Expr);
3547 LoadExpr (CF_NONE, Expr);
3552 void ConstExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3553 /* Will evaluate an expression via the given function. If the result is not
3554 ** a constant of some sort, a diagnostic will be printed, and the value is
3555 ** replaced by a constant one to make sure there are no internal errors that
3556 ** result from this input error.
3559 ExprWithCheck (Func, Expr);
3560 if (!ED_IsConst (Expr)) {
3561 Error ("Constant expression expected");
3562 /* To avoid any compiler errors, make the expression a valid const */
3563 ED_MakeConstAbsInt (Expr, 1);
3569 void BoolExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3570 /* Will evaluate an expression via the given function. If the result is not
3571 ** something that may be evaluated in a boolean context, a diagnostic will be
3572 ** printed, and the value is replaced by a constant one to make sure there
3573 ** are no internal errors that result from this input error.
3576 ExprWithCheck (Func, Expr);
3577 if (!ED_IsBool (Expr)) {
3578 Error ("Boolean expression expected");
3579 /* To avoid any compiler errors, make the expression a valid int */
3580 ED_MakeConstAbsInt (Expr, 1);
3586 void ConstAbsIntExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3587 /* Will evaluate an expression via the given function. If the result is not
3588 ** a constant numeric integer value, a diagnostic will be printed, and the
3589 ** value is replaced by a constant one to make sure there are no internal
3590 ** errors that result from this input error.
3593 ExprWithCheck (Func, Expr);
3594 if (!ED_IsConstAbsInt (Expr)) {
3595 Error ("Constant integer expression expected");
3596 /* To avoid any compiler errors, make the expression a valid const */
3597 ED_MakeConstAbsInt (Expr, 1);