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 (Func->WrappedCall) {
540 Warning("Calling a wrapped function via a pointer, wrapped-call will not be used");
543 /* If the function is not a fastcall function, load the pointer to
544 ** the function into the primary.
548 /* Not a fastcall function - we may use the primary */
550 /* If we have no parameters, the pointer is still in the
551 ** primary. Remove the code to push it and correct the
554 if (ParamSize == 0) {
558 /* Load from the saved copy */
559 g_getlocal (CF_PTR, PtrOffs);
562 /* Load from original location */
563 LoadExpr (CF_NONE, Expr);
566 /* Call the function */
567 g_callind (TypeOf (Expr->Type+1), ParamSize, PtrOffs);
571 /* Fastcall function. We cannot use the primary for the function
572 ** pointer and must therefore use an offset to the stack location.
573 ** Since fastcall functions may never be variadic, we can use the
574 ** index register for this purpose.
576 g_callind (CF_LOCAL, ParamSize, PtrOffs);
579 /* If we have a pointer on stack, remove it */
590 /* Normal function */
591 if (Func->WrappedCall) {
593 StrBuf S = AUTO_STRBUF_INITIALIZER;
595 /* Store the WrappedCall data in tmp4 */
596 sprintf(tmp, "ldy #%u", Func->WrappedCallData);
597 SB_AppendStr (&S, tmp);
601 SB_AppendStr (&S, "sty tmp4");
605 /* Store the original function address in ptr4 */
606 SB_AppendStr (&S, "ldy #<(_");
607 SB_AppendStr (&S, (const char*) Expr->Name);
608 SB_AppendChar (&S, ')');
612 SB_AppendStr (&S, "sty ptr4");
616 SB_AppendStr (&S, "ldy #>(_");
617 SB_AppendStr (&S, (const char*) Expr->Name);
618 SB_AppendChar (&S, ')');
622 SB_AppendStr (&S, "sty ptr4+1");
628 g_call (TypeOf (Expr->Type), Func->WrappedCall->Name, ParamSize);
630 g_call (TypeOf (Expr->Type), (const char*) Expr->Name, ParamSize);
635 /* The function result is an rvalue in the primary register */
636 ED_MakeRValExpr (Expr);
637 Expr->Type = GetFuncReturn (Expr->Type);
642 static void Primary (ExprDesc* E)
643 /* This is the lowest level of the expression parser. */
647 /* Initialize fields in the expression stucture */
650 /* Character and integer constants. */
651 if (CurTok.Tok == TOK_ICONST || CurTok.Tok == TOK_CCONST) {
652 E->IVal = CurTok.IVal;
653 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
654 E->Type = CurTok.Type;
659 /* Floating point constant */
660 if (CurTok.Tok == TOK_FCONST) {
661 E->FVal = CurTok.FVal;
662 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
663 E->Type = CurTok.Type;
668 /* Process parenthesized subexpression by calling the whole parser
671 if (CurTok.Tok == TOK_LPAREN) {
678 /* If we run into an identifier in preprocessing mode, we assume that this
679 ** is an undefined macro and replace it by a constant value of zero.
681 if (Preprocessing && CurTok.Tok == TOK_IDENT) {
683 ED_MakeConstAbsInt (E, 0);
687 /* All others may only be used if the expression evaluation is not called
688 ** recursively by the preprocessor.
691 /* Illegal expression in PP mode */
692 Error ("Preprocessor expression expected");
693 ED_MakeConstAbsInt (E, 1);
697 switch (CurTok.Tok) {
700 /* Identifier. Get a pointer to the symbol table entry */
701 Sym = E->Sym = FindSym (CurTok.Ident);
703 /* Is the symbol known? */
706 /* We found the symbol - skip the name token */
709 /* Check for illegal symbol types */
710 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
711 if (Sym->Flags & SC_TYPE) {
712 /* Cannot use type symbols */
713 Error ("Variable identifier expected");
714 /* Assume an int type to make E valid */
715 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
720 /* Mark the symbol as referenced */
721 Sym->Flags |= SC_REF;
723 /* The expression type is the symbol type */
726 /* Check for legal symbol types */
727 if ((Sym->Flags & SC_CONST) == SC_CONST) {
728 /* Enum or some other numeric constant */
729 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
730 E->IVal = Sym->V.ConstVal;
731 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
733 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
734 E->Name = (unsigned long) Sym->Name;
735 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
736 /* Local variable. If this is a parameter for a variadic
737 ** function, we have to add some address calculations, and the
738 ** address is not const.
740 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
741 /* Variadic parameter */
742 g_leavariadic (Sym->V.Offs - F_GetParamSize (CurrentFunc));
743 E->Flags = E_LOC_EXPR | E_RTYPE_LVAL;
745 /* Normal parameter */
746 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
747 E->IVal = Sym->V.Offs;
749 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
750 /* Register variable, zero page based */
751 E->Flags = E_LOC_REGISTER | E_RTYPE_LVAL;
752 E->Name = Sym->V.R.RegOffs;
753 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
754 /* Static variable */
755 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
756 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
757 E->Name = (unsigned long) Sym->Name;
759 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
760 E->Name = Sym->V.Label;
763 /* Local static variable */
764 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
765 E->Name = Sym->V.Offs;
768 /* We've made all variables lvalues above. However, this is
769 ** not always correct: An array is actually the address of its
770 ** first element, which is a rvalue, and a function is a
771 ** rvalue, too, because we cannot store anything in a function.
772 ** So fix the flags depending on the type.
774 if (IsTypeArray (E->Type) || IsTypeFunc (E->Type)) {
780 /* We did not find the symbol. Remember the name, then skip it */
782 strcpy (Ident, CurTok.Ident);
785 /* IDENT is either an auto-declared function or an undefined variable. */
786 if (CurTok.Tok == TOK_LPAREN) {
787 /* C99 doesn't allow calls to undefined functions, so
788 ** generate an error and otherwise a warning. Declare a
789 ** function returning int. For that purpose, prepare a
790 ** function signature for a function having an empty param
791 ** list and returning int.
793 if (IS_Get (&Standard) >= STD_C99) {
794 Error ("Call to undefined function `%s'", Ident);
796 Warning ("Call to undefined function `%s'", Ident);
798 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
800 E->Flags = E_LOC_GLOBAL | E_RTYPE_RVAL;
801 E->Name = (unsigned long) Sym->Name;
803 /* Undeclared Variable */
804 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
805 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
807 Error ("Undefined symbol: `%s'", Ident);
816 E->LVal = UseLiteral (CurTok.SVal);
817 E->Type = GetCharArrayType (GetLiteralSize (CurTok.SVal));
818 E->Flags = E_LOC_LITERAL | E_RTYPE_RVAL;
820 E->Name = GetLiteralLabel (CurTok.SVal);
827 E->Flags = E_LOC_EXPR | E_RTYPE_RVAL;
832 /* Register pseudo variable */
833 E->Type = type_uchar;
834 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
839 /* Register pseudo variable */
841 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
846 /* Register pseudo variable */
847 E->Type = type_ulong;
848 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
853 /* Illegal primary. Be sure to skip the token to avoid endless
856 Error ("Expression expected");
858 ED_MakeConstAbsInt (E, 1);
865 static void ArrayRef (ExprDesc* Expr)
866 /* Handle an array reference. This function needs a rewrite. */
877 /* Skip the bracket */
880 /* Get the type of left side */
883 /* We can apply a special treatment for arrays that have a const base
884 ** address. This is true for most arrays and will produce a lot better
885 ** code. Check if this is a const base address.
887 ConstBaseAddr = ED_IsRVal (Expr) &&
888 (ED_IsLocConst (Expr) || ED_IsLocStack (Expr));
890 /* If we have a constant base, we delay the address fetch */
892 if (!ConstBaseAddr) {
893 /* Get a pointer to the array into the primary */
894 LoadExpr (CF_NONE, Expr);
896 /* Get the array pointer on stack. Do not push more than 16
897 ** bit, even if this value is greater, since we cannot handle
898 ** other than 16bit stuff when doing indexing.
904 /* TOS now contains ptr to array elements. Get the subscript. */
905 MarkedExprWithCheck (hie0, &Subscript);
907 /* Check the types of array and subscript. We can either have a
908 ** pointer/array to the left, in which case the subscript must be of an
909 ** integer type, or we have an integer to the left, in which case the
910 ** subscript must be a pointer/array.
911 ** Since we do the necessary checking here, we can rely later on the
914 Qualifiers = T_QUAL_NONE;
915 if (IsClassPtr (Expr->Type)) {
916 if (!IsClassInt (Subscript.Type)) {
917 Error ("Array subscript is not an integer");
918 /* To avoid any compiler errors, make the expression a valid int */
919 ED_MakeConstAbsInt (&Subscript, 0);
921 if (IsTypeArray (Expr->Type)) {
922 Qualifiers = GetQualifier (Expr->Type);
924 ElementType = Indirect (Expr->Type);
925 } else if (IsClassInt (Expr->Type)) {
926 if (!IsClassPtr (Subscript.Type)) {
927 Error ("Subscripted value is neither array nor pointer");
928 /* To avoid compiler errors, make the subscript a char[] at
931 ED_MakeConstAbs (&Subscript, 0, GetCharArrayType (1));
932 } else if (IsTypeArray (Subscript.Type)) {
933 Qualifiers = GetQualifier (Subscript.Type);
935 ElementType = Indirect (Subscript.Type);
937 Error ("Cannot subscript");
938 /* To avoid compiler errors, fake both the array and the subscript, so
939 ** we can just proceed.
941 ED_MakeConstAbs (Expr, 0, GetCharArrayType (1));
942 ED_MakeConstAbsInt (&Subscript, 0);
943 ElementType = Indirect (Expr->Type);
946 /* The element type has the combined qualifiers from itself and the array,
947 ** it is a member of (if any).
949 if (GetQualifier (ElementType) != (GetQualifier (ElementType) | Qualifiers)) {
950 ElementType = TypeDup (ElementType);
951 ElementType->C |= Qualifiers;
954 /* If the subscript is a bit-field, load it and make it an rvalue */
955 if (ED_IsBitField (&Subscript)) {
956 LoadExpr (CF_NONE, &Subscript);
957 ED_MakeRValExpr (&Subscript);
960 /* Check if the subscript is constant absolute value */
961 if (ED_IsConstAbs (&Subscript) && ED_CodeRangeIsEmpty (&Subscript)) {
963 /* The array subscript is a numeric constant. If we had pushed the
964 ** array base address onto the stack before, we can remove this value,
965 ** since we can generate expression+offset.
967 if (!ConstBaseAddr) {
970 /* Get an array pointer into the primary */
971 LoadExpr (CF_NONE, Expr);
974 if (IsClassPtr (Expr->Type)) {
976 /* Lhs is pointer/array. Scale the subscript value according to
979 Subscript.IVal *= CheckedSizeOf (ElementType);
981 /* Remove the address load code */
984 /* In case of an array, we can adjust the offset of the expression
985 ** already in Expr. If the base address was a constant, we can even
986 ** remove the code that loaded the address into the primary.
988 if (IsTypeArray (Expr->Type)) {
990 /* Adjust the offset */
991 Expr->IVal += Subscript.IVal;
995 /* It's a pointer, so we do have to load it into the primary
996 ** first (if it's not already there).
998 if (ConstBaseAddr || ED_IsLVal (Expr)) {
999 LoadExpr (CF_NONE, Expr);
1000 ED_MakeRValExpr (Expr);
1003 /* Use the offset */
1004 Expr->IVal = Subscript.IVal;
1009 /* Scale the rhs value according to the element type */
1010 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
1012 /* Add the subscript. Since arrays are indexed by integers,
1013 ** we will ignore the true type of the subscript here and
1014 ** use always an int. #### Use offset but beware of LoadExpr!
1016 g_inc (CF_INT | CF_CONST, Subscript.IVal);
1022 /* Array subscript is not constant. Load it into the primary */
1023 GetCodePos (&Mark2);
1024 LoadExpr (CF_NONE, &Subscript);
1027 if (IsClassPtr (Expr->Type)) {
1029 /* Indexing is based on unsigneds, so we will just use the integer
1030 ** portion of the index (which is in (e)ax, so there's no further
1031 ** action required).
1033 g_scale (CF_INT, CheckedSizeOf (ElementType));
1037 /* Get the int value on top. If we come here, we're sure, both
1038 ** values are 16 bit (the first one was truncated if necessary
1039 ** and the second one is a pointer). Note: If ConstBaseAddr is
1040 ** true, we don't have a value on stack, so to "swap" both, just
1041 ** push the subscript.
1043 if (ConstBaseAddr) {
1045 LoadExpr (CF_NONE, Expr);
1052 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
1056 /* The offset is now in the primary register. It we didn't have a
1057 ** constant base address for the lhs, the lhs address is already
1058 ** on stack, and we must add the offset. If the base address was
1059 ** constant, we call special functions to add the address to the
1062 if (!ConstBaseAddr) {
1064 /* The array base address is on stack and the subscript is in the
1065 ** primary. Add both.
1071 /* The subscript is in the primary, and the array base address is
1072 ** in Expr. If the subscript has itself a constant address, it is
1073 ** often a better idea to reverse again the order of the
1074 ** evaluation. This will generate better code if the subscript is
1075 ** a byte sized variable. But beware: This is only possible if the
1076 ** subscript was not scaled, that is, if this was a byte array
1079 if ((ED_IsLocConst (&Subscript) || ED_IsLocStack (&Subscript)) &&
1080 CheckedSizeOf (ElementType) == SIZEOF_CHAR) {
1084 /* Reverse the order of evaluation */
1085 if (CheckedSizeOf (Subscript.Type) == SIZEOF_CHAR) {
1090 RemoveCode (&Mark2);
1092 /* Get a pointer to the array into the primary. */
1093 LoadExpr (CF_NONE, Expr);
1095 /* Add the variable */
1096 if (ED_IsLocStack (&Subscript)) {
1097 g_addlocal (Flags, Subscript.IVal);
1099 Flags |= GlobalModeFlags (&Subscript);
1100 g_addstatic (Flags, Subscript.Name, Subscript.IVal);
1104 if (ED_IsLocAbs (Expr)) {
1105 /* Constant numeric address. Just add it */
1106 g_inc (CF_INT, Expr->IVal);
1107 } else if (ED_IsLocStack (Expr)) {
1108 /* Base address is a local variable address */
1109 if (IsTypeArray (Expr->Type)) {
1110 g_addaddr_local (CF_INT, Expr->IVal);
1112 g_addlocal (CF_PTR, Expr->IVal);
1115 /* Base address is a static variable address */
1116 unsigned Flags = CF_INT | GlobalModeFlags (Expr);
1117 if (ED_IsRVal (Expr)) {
1118 /* Add the address of the location */
1119 g_addaddr_static (Flags, Expr->Name, Expr->IVal);
1121 /* Add the contents of the location */
1122 g_addstatic (Flags, Expr->Name, Expr->IVal);
1130 /* The result is an expression in the primary */
1131 ED_MakeRValExpr (Expr);
1135 /* Result is of element type */
1136 Expr->Type = ElementType;
1138 /* An array element is actually a variable. So the rules for variables
1139 ** with respect to the reference type apply: If it's an array, it is
1140 ** a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1141 ** but an array cannot contain functions).
1143 if (IsTypeArray (Expr->Type)) {
1149 /* Consume the closing bracket */
1155 static void StructRef (ExprDesc* Expr)
1156 /* Process struct field after . or ->. */
1163 /* Skip the token and check for an identifier */
1165 if (CurTok.Tok != TOK_IDENT) {
1166 Error ("Identifier expected");
1167 /* Make the expression an integer at address zero */
1168 ED_MakeConstAbs (Expr, 0, type_int);
1172 /* Get the symbol table entry and check for a struct field */
1173 strcpy (Ident, CurTok.Ident);
1175 Field = FindStructField (Expr->Type, Ident);
1177 Error ("Struct/union has no field named `%s'", Ident);
1178 /* Make the expression an integer at address zero */
1179 ED_MakeConstAbs (Expr, 0, type_int);
1183 /* If we have a struct pointer that is an lvalue and not already in the
1184 ** primary, load it now.
1186 if (ED_IsLVal (Expr) && IsTypePtr (Expr->Type)) {
1188 /* Load into the primary */
1189 LoadExpr (CF_NONE, Expr);
1191 /* Make it an lvalue expression */
1192 ED_MakeLValExpr (Expr);
1195 /* The type is the type of the field plus any qualifiers from the struct */
1196 if (IsClassStruct (Expr->Type)) {
1197 Q = GetQualifier (Expr->Type);
1199 Q = GetQualifier (Indirect (Expr->Type));
1201 if (GetQualifier (Field->Type) == (GetQualifier (Field->Type) | Q)) {
1202 FinalType = Field->Type;
1204 FinalType = TypeDup (Field->Type);
1208 /* A struct is usually an lvalue. If not, it is a struct in the primary
1211 if (ED_IsRVal (Expr) && ED_IsLocExpr (Expr) && !IsTypePtr (Expr->Type)) {
1216 /* Get the size of the type */
1217 unsigned Size = SizeOf (Expr->Type);
1220 CHECK (Field->V.Offs + Size <= SIZEOF_LONG);
1222 /* The type of the operation depends on the type of the struct */
1224 case 1: Flags = CF_CHAR | CF_UNSIGNED | CF_CONST; break;
1225 case 2: Flags = CF_INT | CF_UNSIGNED | CF_CONST; break;
1226 case 3: /* FALLTHROUGH */
1227 case 4: Flags = CF_LONG | CF_UNSIGNED | CF_CONST; break;
1228 default: Internal ("Invalid struct size: %u", Size); break;
1231 /* Generate a shift to get the field in the proper position in the
1232 ** primary. For bit fields, mask the value.
1234 BitOffs = Field->V.Offs * CHAR_BITS;
1235 if (SymIsBitField (Field)) {
1236 BitOffs += Field->V.B.BitOffs;
1237 g_asr (Flags, BitOffs);
1238 /* Mask the value. This is unnecessary if the shift executed above
1239 ** moved only zeroes into the value.
1241 if (BitOffs + Field->V.B.BitWidth != Size * CHAR_BITS) {
1242 g_and (CF_INT | CF_UNSIGNED | CF_CONST,
1243 (0x0001U << Field->V.B.BitWidth) - 1U);
1246 g_asr (Flags, BitOffs);
1249 /* Use the new type */
1250 Expr->Type = FinalType;
1254 /* Set the struct field offset */
1255 Expr->IVal += Field->V.Offs;
1257 /* Use the new type */
1258 Expr->Type = FinalType;
1260 /* An struct member is actually a variable. So the rules for variables
1261 ** with respect to the reference type apply: If it's an array, it is
1262 ** a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1263 ** but a struct field cannot be a function).
1265 if (IsTypeArray (Expr->Type)) {
1271 /* Make the expression a bit field if necessary */
1272 if (SymIsBitField (Field)) {
1273 ED_MakeBitField (Expr, Field->V.B.BitOffs, Field->V.B.BitWidth);
1281 static void hie11 (ExprDesc *Expr)
1282 /* Handle compound types (structs and arrays) */
1284 /* Name value used in invalid function calls */
1285 static const char IllegalFunc[] = "illegal_function_call";
1287 /* Evaluate the lhs */
1290 /* Check for a rhs */
1291 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN ||
1292 CurTok.Tok == TOK_DOT || CurTok.Tok == TOK_PTR_REF) {
1294 switch (CurTok.Tok) {
1297 /* Array reference */
1302 /* Function call. */
1303 if (!IsTypeFunc (Expr->Type) && !IsTypeFuncPtr (Expr->Type)) {
1304 /* Not a function */
1305 Error ("Illegal function call");
1306 /* Force the type to be a implicitly defined function, one
1307 ** returning an int and taking any number of arguments.
1308 ** Since we don't have a name, invent one.
1310 ED_MakeConstAbs (Expr, 0, GetImplicitFuncType ());
1311 Expr->Name = (long) IllegalFunc;
1313 /* Call the function */
1314 FunctionCall (Expr);
1318 if (!IsClassStruct (Expr->Type)) {
1319 Error ("Struct expected");
1325 /* If we have an array, convert it to pointer to first element */
1326 if (IsTypeArray (Expr->Type)) {
1327 Expr->Type = ArrayToPtr (Expr->Type);
1329 if (!IsClassPtr (Expr->Type) || !IsClassStruct (Indirect (Expr->Type))) {
1330 Error ("Struct pointer expected");
1336 Internal ("Invalid token in hie11: %d", CurTok.Tok);
1344 void Store (ExprDesc* Expr, const Type* StoreType)
1345 /* Store the primary register into the location denoted by Expr. If StoreType
1346 ** is given, use this type when storing instead of Expr->Type. If StoreType
1347 ** is NULL, use Expr->Type instead.
1352 /* If StoreType was not given, use Expr->Type instead */
1353 if (StoreType == 0) {
1354 StoreType = Expr->Type;
1357 /* Prepare the code generator flags */
1358 Flags = TypeOf (StoreType) | GlobalModeFlags (Expr);
1360 /* Do the store depending on the location */
1361 switch (ED_GetLoc (Expr)) {
1364 /* Absolute: numeric address or const */
1365 g_putstatic (Flags, Expr->IVal, 0);
1369 /* Global variable */
1370 g_putstatic (Flags, Expr->Name, Expr->IVal);
1375 /* Static variable or literal in the literal pool */
1376 g_putstatic (Flags, Expr->Name, Expr->IVal);
1379 case E_LOC_REGISTER:
1380 /* Register variable */
1381 g_putstatic (Flags, Expr->Name, Expr->IVal);
1385 /* Value on the stack */
1386 g_putlocal (Flags, Expr->IVal, 0);
1390 /* The primary register (value is already there) */
1394 /* An expression in the primary register */
1395 g_putind (Flags, Expr->IVal);
1399 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
1402 /* Assume that each one of the stores will invalidate CC */
1403 ED_MarkAsUntested (Expr);
1408 static void PreInc (ExprDesc* Expr)
1409 /* Handle the preincrement operators */
1414 /* Skip the operator token */
1417 /* Evaluate the expression and check that it is an lvalue */
1419 if (!ED_IsLVal (Expr)) {
1420 Error ("Invalid lvalue");
1424 /* We cannot modify const values */
1425 if (IsQualConst (Expr->Type)) {
1426 Error ("Increment of read-only variable");
1429 /* Get the data type */
1430 Flags = TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR | CF_CONST;
1432 /* Get the increment value in bytes */
1433 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1435 /* Check the location of the data */
1436 switch (ED_GetLoc (Expr)) {
1439 /* Absolute: numeric address or const */
1440 g_addeqstatic (Flags, Expr->IVal, 0, Val);
1444 /* Global variable */
1445 g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1450 /* Static variable or literal in the literal pool */
1451 g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1454 case E_LOC_REGISTER:
1455 /* Register variable */
1456 g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1460 /* Value on the stack */
1461 g_addeqlocal (Flags, Expr->IVal, Val);
1465 /* The primary register */
1470 /* An expression in the primary register */
1471 g_addeqind (Flags, Expr->IVal, Val);
1475 Internal ("Invalid location in PreInc(): 0x%04X", ED_GetLoc (Expr));
1478 /* Result is an expression, no reference */
1479 ED_MakeRValExpr (Expr);
1484 static void PreDec (ExprDesc* Expr)
1485 /* Handle the predecrement operators */
1490 /* Skip the operator token */
1493 /* Evaluate the expression and check that it is an lvalue */
1495 if (!ED_IsLVal (Expr)) {
1496 Error ("Invalid lvalue");
1500 /* We cannot modify const values */
1501 if (IsQualConst (Expr->Type)) {
1502 Error ("Decrement of read-only variable");
1505 /* Get the data type */
1506 Flags = TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR | CF_CONST;
1508 /* Get the increment value in bytes */
1509 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1511 /* Check the location of the data */
1512 switch (ED_GetLoc (Expr)) {
1515 /* Absolute: numeric address or const */
1516 g_subeqstatic (Flags, Expr->IVal, 0, Val);
1520 /* Global variable */
1521 g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1526 /* Static variable or literal in the literal pool */
1527 g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1530 case E_LOC_REGISTER:
1531 /* Register variable */
1532 g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1536 /* Value on the stack */
1537 g_subeqlocal (Flags, Expr->IVal, Val);
1541 /* The primary register */
1546 /* An expression in the primary register */
1547 g_subeqind (Flags, Expr->IVal, Val);
1551 Internal ("Invalid location in PreDec(): 0x%04X", ED_GetLoc (Expr));
1554 /* Result is an expression, no reference */
1555 ED_MakeRValExpr (Expr);
1560 static void PostInc (ExprDesc* Expr)
1561 /* Handle the postincrement operator */
1567 /* The expression to increment must be an lvalue */
1568 if (!ED_IsLVal (Expr)) {
1569 Error ("Invalid lvalue");
1573 /* We cannot modify const values */
1574 if (IsQualConst (Expr->Type)) {
1575 Error ("Increment of read-only variable");
1578 /* Get the data type */
1579 Flags = TypeOf (Expr->Type);
1581 /* Emit smaller code if a char variable is at a constant location */
1582 if ((Flags & CF_CHAR) == CF_CHAR && ED_IsLocConst(Expr)) {
1584 LoadExpr (CF_NONE, Expr);
1585 AddCodeLine ("inc %s", ED_GetLabelName(Expr, 0));
1589 /* Push the address if needed */
1592 /* Fetch the value and save it (since it's the result of the expression) */
1593 LoadExpr (CF_NONE, Expr);
1594 g_save (Flags | CF_FORCECHAR);
1596 /* If we have a pointer expression, increment by the size of the type */
1597 if (IsTypePtr (Expr->Type)) {
1598 g_inc (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1600 g_inc (Flags | CF_CONST | CF_FORCECHAR, 1);
1603 /* Store the result back */
1606 /* Restore the original value in the primary register */
1607 g_restore (Flags | CF_FORCECHAR);
1610 /* The result is always an expression, no reference */
1611 ED_MakeRValExpr (Expr);
1616 static void PostDec (ExprDesc* Expr)
1617 /* Handle the postdecrement operator */
1623 /* The expression to increment must be an lvalue */
1624 if (!ED_IsLVal (Expr)) {
1625 Error ("Invalid lvalue");
1629 /* We cannot modify const values */
1630 if (IsQualConst (Expr->Type)) {
1631 Error ("Decrement of read-only variable");
1634 /* Get the data type */
1635 Flags = TypeOf (Expr->Type);
1637 /* Emit smaller code if a char variable is at a constant location */
1638 if ((Flags & CF_CHAR) == CF_CHAR && ED_IsLocConst(Expr)) {
1640 LoadExpr (CF_NONE, Expr);
1641 AddCodeLine ("dec %s", ED_GetLabelName(Expr, 0));
1645 /* Push the address if needed */
1648 /* Fetch the value and save it (since it's the result of the expression) */
1649 LoadExpr (CF_NONE, Expr);
1650 g_save (Flags | CF_FORCECHAR);
1652 /* If we have a pointer expression, increment by the size of the type */
1653 if (IsTypePtr (Expr->Type)) {
1654 g_dec (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1656 g_dec (Flags | CF_CONST | CF_FORCECHAR, 1);
1659 /* Store the result back */
1662 /* Restore the original value in the primary register */
1663 g_restore (Flags | CF_FORCECHAR);
1666 /* The result is always an expression, no reference */
1667 ED_MakeRValExpr (Expr);
1672 static void UnaryOp (ExprDesc* Expr)
1673 /* Handle unary -/+ and ~ */
1677 /* Remember the operator token and skip it */
1678 token_t Tok = CurTok.Tok;
1681 /* Get the expression */
1684 /* We can only handle integer types */
1685 if (!IsClassInt (Expr->Type)) {
1686 Error ("Argument must have integer type");
1687 ED_MakeConstAbsInt (Expr, 1);
1690 /* Check for a constant expression */
1691 if (ED_IsConstAbs (Expr)) {
1692 /* Value is constant */
1694 case TOK_MINUS: Expr->IVal = -Expr->IVal; break;
1695 case TOK_PLUS: break;
1696 case TOK_COMP: Expr->IVal = ~Expr->IVal; break;
1697 default: Internal ("Unexpected token: %d", Tok);
1700 /* Value is not constant */
1701 LoadExpr (CF_NONE, Expr);
1703 /* Get the type of the expression */
1704 Flags = TypeOf (Expr->Type);
1706 /* Handle the operation */
1708 case TOK_MINUS: g_neg (Flags); break;
1709 case TOK_PLUS: break;
1710 case TOK_COMP: g_com (Flags); break;
1711 default: Internal ("Unexpected token: %d", Tok);
1714 /* The result is a rvalue in the primary */
1715 ED_MakeRValExpr (Expr);
1721 void hie10 (ExprDesc* Expr)
1722 /* Handle ++, --, !, unary - etc. */
1726 switch (CurTok.Tok) {
1744 if (evalexpr (CF_NONE, hie10, Expr) == 0) {
1745 /* Constant expression */
1746 Expr->IVal = !Expr->IVal;
1748 g_bneg (TypeOf (Expr->Type));
1749 ED_MakeRValExpr (Expr);
1750 ED_TestDone (Expr); /* bneg will set cc */
1756 ExprWithCheck (hie10, Expr);
1757 if (ED_IsLVal (Expr) || !(ED_IsLocConst (Expr) || ED_IsLocStack (Expr))) {
1758 /* Not a const, load it into the primary and make it a
1759 ** calculated value.
1761 LoadExpr (CF_NONE, Expr);
1762 ED_MakeRValExpr (Expr);
1764 /* If the expression is already a pointer to function, the
1765 ** additional dereferencing operator must be ignored. A function
1766 ** itself is represented as "pointer to function", so any number
1767 ** of dereference operators is legal, since the result will
1768 ** always be converted to "pointer to function".
1770 if (IsTypeFuncPtr (Expr->Type) || IsTypeFunc (Expr->Type)) {
1771 /* Expression not storable */
1774 if (IsClassPtr (Expr->Type)) {
1775 Expr->Type = Indirect (Expr->Type);
1777 Error ("Illegal indirection");
1779 /* If the expression points to an array, then don't convert the
1780 ** address -- it already is the location of the first element.
1782 if (!IsTypeArray (Expr->Type)) {
1783 /* The * operator yields an lvalue */
1791 ExprWithCheck (hie10, Expr);
1792 /* The & operator may be applied to any lvalue, and it may be
1793 ** applied to functions, even if they're no lvalues.
1795 if (ED_IsRVal (Expr) && !IsTypeFunc (Expr->Type) && !IsTypeArray (Expr->Type)) {
1796 Error ("Illegal address");
1798 if (ED_IsBitField (Expr)) {
1799 Error ("Cannot take address of bit-field");
1800 /* Do it anyway, just to avoid further warnings */
1801 Expr->Flags &= ~E_BITFIELD;
1803 Expr->Type = PointerTo (Expr->Type);
1804 /* The & operator yields an rvalue */
1811 if (TypeSpecAhead ()) {
1814 Size = CheckedSizeOf (ParseType (T));
1817 /* Remember the output queue pointer */
1821 /* If the expression is a literal string, release it, so it
1822 ** won't be output as data if not used elsewhere.
1824 if (ED_IsLocLiteral (Expr)) {
1825 ReleaseLiteral (Expr->LVal);
1827 /* Calculate the size */
1828 Size = CheckedSizeOf (Expr->Type);
1829 /* Remove any generated code */
1832 ED_MakeConstAbs (Expr, Size, type_size_t);
1833 ED_MarkAsUntested (Expr);
1837 if (TypeSpecAhead ()) {
1847 /* Handle post increment */
1848 switch (CurTok.Tok) {
1849 case TOK_INC: PostInc (Expr); break;
1850 case TOK_DEC: PostDec (Expr); break;
1861 static void hie_internal (const GenDesc* Ops, /* List of generators */
1863 void (*hienext) (ExprDesc*),
1865 /* Helper function */
1871 token_t Tok; /* The operator token */
1872 unsigned ltype, type;
1873 int lconst; /* Left operand is a constant */
1874 int rconst; /* Right operand is a constant */
1877 ExprWithCheck (hienext, Expr);
1880 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1882 /* Tell the caller that we handled it's ops */
1885 /* All operators that call this function expect an int on the lhs */
1886 if (!IsClassInt (Expr->Type)) {
1887 Error ("Integer expression expected");
1888 /* To avoid further errors, make Expr a valid int expression */
1889 ED_MakeConstAbsInt (Expr, 1);
1892 /* Remember the operator token, then skip it */
1896 /* Get the lhs on stack */
1897 GetCodePos (&Mark1);
1898 ltype = TypeOf (Expr->Type);
1899 lconst = ED_IsConstAbs (Expr);
1901 /* Constant value */
1902 GetCodePos (&Mark2);
1903 /* If the operator is commutative, don't push the left side, if
1904 ** it's a constant, since we will exchange both operands.
1906 if ((Gen->Flags & GEN_COMM) == 0) {
1907 g_push (ltype | CF_CONST, Expr->IVal);
1910 /* Value not constant */
1911 LoadExpr (CF_NONE, Expr);
1912 GetCodePos (&Mark2);
1916 /* Get the right hand side */
1917 MarkedExprWithCheck (hienext, &Expr2);
1919 /* Check for a constant expression */
1920 rconst = (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2));
1922 /* Not constant, load into the primary */
1923 LoadExpr (CF_NONE, &Expr2);
1926 /* Check the type of the rhs */
1927 if (!IsClassInt (Expr2.Type)) {
1928 Error ("Integer expression expected");
1931 /* Check for const operands */
1932 if (lconst && rconst) {
1934 /* Both operands are constant, remove the generated code */
1935 RemoveCode (&Mark1);
1937 /* Get the type of the result */
1938 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1940 /* Handle the op differently for signed and unsigned types */
1941 if (IsSignSigned (Expr->Type)) {
1943 /* Evaluate the result for signed operands */
1944 signed long Val1 = Expr->IVal;
1945 signed long Val2 = Expr2.IVal;
1948 Expr->IVal = (Val1 | Val2);
1951 Expr->IVal = (Val1 ^ Val2);
1954 Expr->IVal = (Val1 & Val2);
1957 Expr->IVal = (Val1 * Val2);
1961 Error ("Division by zero");
1962 Expr->IVal = 0x7FFFFFFF;
1964 Expr->IVal = (Val1 / Val2);
1969 Error ("Modulo operation with zero");
1972 Expr->IVal = (Val1 % Val2);
1976 Internal ("hie_internal: got token 0x%X\n", Tok);
1980 /* Evaluate the result for unsigned operands */
1981 unsigned long Val1 = Expr->IVal;
1982 unsigned long Val2 = Expr2.IVal;
1985 Expr->IVal = (Val1 | Val2);
1988 Expr->IVal = (Val1 ^ Val2);
1991 Expr->IVal = (Val1 & Val2);
1994 Expr->IVal = (Val1 * Val2);
1998 Error ("Division by zero");
1999 Expr->IVal = 0xFFFFFFFF;
2001 Expr->IVal = (Val1 / Val2);
2006 Error ("Modulo operation with zero");
2009 Expr->IVal = (Val1 % Val2);
2013 Internal ("hie_internal: got token 0x%X\n", Tok);
2017 } else if (lconst && (Gen->Flags & GEN_COMM) && !rconst) {
2019 /* The left side is constant, the right side is not, and the
2020 ** operator allows swapping the operands. We haven't pushed the
2021 ** left side onto the stack in this case, and will reverse the
2022 ** operation because this allows for better code.
2024 unsigned rtype = ltype | CF_CONST;
2025 ltype = TypeOf (Expr2.Type); /* Expr2 is now left */
2027 if ((Gen->Flags & GEN_NOPUSH) == 0) {
2030 ltype |= CF_REG; /* Value is in register */
2033 /* Determine the type of the operation result. */
2034 type |= g_typeadjust (ltype, rtype);
2035 Expr->Type = promoteint (Expr->Type, Expr2.Type);
2038 Gen->Func (type, Expr->IVal);
2040 /* We have a rvalue in the primary now */
2041 ED_MakeRValExpr (Expr);
2045 /* If the right hand side is constant, and the generator function
2046 ** expects the lhs in the primary, remove the push of the primary
2049 unsigned rtype = TypeOf (Expr2.Type);
2052 /* Second value is constant - check for div */
2055 if (Tok == TOK_DIV && Expr2.IVal == 0) {
2056 Error ("Division by zero");
2057 } else if (Tok == TOK_MOD && Expr2.IVal == 0) {
2058 Error ("Modulo operation with zero");
2060 if ((Gen->Flags & GEN_NOPUSH) != 0) {
2061 RemoveCode (&Mark2);
2062 ltype |= CF_REG; /* Value is in register */
2066 /* Determine the type of the operation result. */
2067 type |= g_typeadjust (ltype, rtype);
2068 Expr->Type = promoteint (Expr->Type, Expr2.Type);
2071 Gen->Func (type, Expr2.IVal);
2073 /* We have a rvalue in the primary now */
2074 ED_MakeRValExpr (Expr);
2081 static void hie_compare (const GenDesc* Ops, /* List of generators */
2083 void (*hienext) (ExprDesc*))
2084 /* Helper function for the compare operators */
2091 token_t Tok; /* The operator token */
2093 int rconst; /* Operand is a constant */
2096 GetCodePos (&Mark0);
2097 ExprWithCheck (hienext, Expr);
2099 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
2101 /* Remember the generator function */
2102 void (*GenFunc) (unsigned, unsigned long) = Gen->Func;
2104 /* Remember the operator token, then skip it */
2108 /* If lhs is a function, convert it to pointer to function */
2109 if (IsTypeFunc (Expr->Type)) {
2110 Expr->Type = PointerTo (Expr->Type);
2113 /* Get the lhs on stack */
2114 GetCodePos (&Mark1);
2115 ltype = TypeOf (Expr->Type);
2116 if (ED_IsConstAbs (Expr)) {
2117 /* Constant value */
2118 GetCodePos (&Mark2);
2119 g_push (ltype | CF_CONST, Expr->IVal);
2121 /* Value not constant */
2122 LoadExpr (CF_NONE, Expr);
2123 GetCodePos (&Mark2);
2127 /* Get the right hand side */
2128 MarkedExprWithCheck (hienext, &Expr2);
2130 /* If rhs is a function, convert it to pointer to function */
2131 if (IsTypeFunc (Expr2.Type)) {
2132 Expr2.Type = PointerTo (Expr2.Type);
2135 /* Check for a constant expression */
2136 rconst = (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2));
2138 /* Not constant, load into the primary */
2139 LoadExpr (CF_NONE, &Expr2);
2142 /* Some operations aren't allowed on function pointers */
2143 if ((Gen->Flags & GEN_NOFUNC) != 0) {
2144 /* Output only one message even if both sides are wrong */
2145 if (IsTypeFuncPtr (Expr->Type)) {
2146 Error ("Invalid left operand for relational operator");
2147 /* Avoid further errors */
2148 ED_MakeConstAbsInt (Expr, 0);
2149 ED_MakeConstAbsInt (&Expr2, 0);
2150 } else if (IsTypeFuncPtr (Expr2.Type)) {
2151 Error ("Invalid right operand for relational operator");
2152 /* Avoid further errors */
2153 ED_MakeConstAbsInt (Expr, 0);
2154 ED_MakeConstAbsInt (&Expr2, 0);
2158 /* Make sure, the types are compatible */
2159 if (IsClassInt (Expr->Type)) {
2160 if (!IsClassInt (Expr2.Type) && !(IsClassPtr(Expr2.Type) && ED_IsNullPtr(Expr))) {
2161 Error ("Incompatible types");
2163 } else if (IsClassPtr (Expr->Type)) {
2164 if (IsClassPtr (Expr2.Type)) {
2165 /* Both pointers are allowed in comparison if they point to
2166 ** the same type, or if one of them is a void pointer.
2168 Type* left = Indirect (Expr->Type);
2169 Type* right = Indirect (Expr2.Type);
2170 if (TypeCmp (left, right) < TC_QUAL_DIFF && left->C != T_VOID && right->C != T_VOID) {
2171 /* Incompatible pointers */
2172 Error ("Incompatible types");
2174 } else if (!ED_IsNullPtr (&Expr2)) {
2175 Error ("Incompatible types");
2179 /* Check for const operands */
2180 if (ED_IsConstAbs (Expr) && rconst) {
2182 /* If the result is constant, this is suspicious when not in
2183 ** preprocessor mode.
2185 WarnConstCompareResult ();
2187 /* Both operands are constant, remove the generated code */
2188 RemoveCode (&Mark1);
2190 /* Determine if this is a signed or unsigned compare */
2191 if (IsClassInt (Expr->Type) && IsSignSigned (Expr->Type) &&
2192 IsClassInt (Expr2.Type) && IsSignSigned (Expr2.Type)) {
2194 /* Evaluate the result for signed operands */
2195 signed long Val1 = Expr->IVal;
2196 signed long Val2 = Expr2.IVal;
2198 case TOK_EQ: Expr->IVal = (Val1 == Val2); break;
2199 case TOK_NE: Expr->IVal = (Val1 != Val2); break;
2200 case TOK_LT: Expr->IVal = (Val1 < Val2); break;
2201 case TOK_LE: Expr->IVal = (Val1 <= Val2); break;
2202 case TOK_GE: Expr->IVal = (Val1 >= Val2); break;
2203 case TOK_GT: Expr->IVal = (Val1 > Val2); break;
2204 default: Internal ("hie_compare: got token 0x%X\n", Tok);
2209 /* Evaluate the result for unsigned operands */
2210 unsigned long Val1 = Expr->IVal;
2211 unsigned long Val2 = Expr2.IVal;
2213 case TOK_EQ: Expr->IVal = (Val1 == Val2); break;
2214 case TOK_NE: Expr->IVal = (Val1 != Val2); break;
2215 case TOK_LT: Expr->IVal = (Val1 < Val2); break;
2216 case TOK_LE: Expr->IVal = (Val1 <= Val2); break;
2217 case TOK_GE: Expr->IVal = (Val1 >= Val2); break;
2218 case TOK_GT: Expr->IVal = (Val1 > Val2); break;
2219 default: Internal ("hie_compare: got token 0x%X\n", Tok);
2225 /* Determine the signedness of the operands */
2226 int LeftSigned = IsSignSigned (Expr->Type);
2227 int RightSigned = IsSignSigned (Expr2.Type);
2229 /* If the right hand side is constant, and the generator function
2230 ** expects the lhs in the primary, remove the push of the primary
2236 if ((Gen->Flags & GEN_NOPUSH) != 0) {
2237 RemoveCode (&Mark2);
2238 ltype |= CF_REG; /* Value is in register */
2242 /* Determine the type of the operation. */
2243 if (IsTypeChar (Expr->Type) && rconst) {
2245 /* Left side is unsigned char, right side is constant.
2246 ** Determine the minimum and maximum values
2248 int LeftMin, LeftMax;
2256 /* An integer value is always represented as a signed in the
2257 ** ExprDesc structure. This may lead to false results below,
2258 ** if it is actually unsigned, but interpreted as signed
2259 ** because of the representation. Fortunately, in this case,
2260 ** the actual value doesn't matter, since it's always greater
2261 ** than what can be represented in a char. So correct the
2262 ** value accordingly.
2264 if (!RightSigned && Expr2.IVal < 0) {
2265 /* Correct the value so it is an unsigned. It will then
2266 ** anyway match one of the cases below.
2268 Expr2.IVal = LeftMax + 1;
2271 /* Comparing a char against a constant may have a constant
2272 ** result. Please note: It is not possible to remove the code
2273 ** for the compare alltogether, because it may have side
2279 if (Expr2.IVal < LeftMin || Expr2.IVal > LeftMax) {
2280 ED_MakeConstAbsInt (Expr, 0);
2281 WarnConstCompareResult ();
2287 if (Expr2.IVal < LeftMin || Expr2.IVal > LeftMax) {
2288 ED_MakeConstAbsInt (Expr, 1);
2289 WarnConstCompareResult ();
2295 if (Expr2.IVal <= LeftMin || Expr2.IVal > LeftMax) {
2296 ED_MakeConstAbsInt (Expr, Expr2.IVal > LeftMax);
2297 WarnConstCompareResult ();
2303 if (Expr2.IVal < LeftMin || Expr2.IVal >= LeftMax) {
2304 ED_MakeConstAbsInt (Expr, Expr2.IVal >= LeftMax);
2305 WarnConstCompareResult ();
2311 if (Expr2.IVal <= LeftMin || Expr2.IVal > LeftMax) {
2312 ED_MakeConstAbsInt (Expr, Expr2.IVal <= LeftMin);
2313 WarnConstCompareResult ();
2319 if (Expr2.IVal < LeftMin || Expr2.IVal >= LeftMax) {
2320 ED_MakeConstAbsInt (Expr, Expr2.IVal < LeftMin);
2321 WarnConstCompareResult ();
2327 Internal ("hie_compare: got token 0x%X\n", Tok);
2330 /* If the result is not already constant (as evaluated in the
2331 ** switch above), we can execute the operation as a char op,
2332 ** since the right side constant is in a valid range.
2334 flags |= (CF_CHAR | CF_FORCECHAR);
2336 flags |= CF_UNSIGNED;
2339 } else if (IsTypeChar (Expr->Type) && IsTypeChar (Expr2.Type) &&
2340 GetSignedness (Expr->Type) == GetSignedness (Expr2.Type)) {
2342 /* Both are chars with the same signedness. We can encode the
2343 ** operation as a char operation.
2347 flags |= CF_FORCECHAR;
2350 flags |= CF_UNSIGNED;
2353 unsigned rtype = TypeOf (Expr2.Type) | (flags & CF_CONST);
2354 flags |= g_typeadjust (ltype, rtype);
2357 /* If the left side is an unsigned and the right is a constant,
2358 ** we may be able to change the compares to something more
2361 if (!LeftSigned && rconst) {
2366 if (Expr2.IVal == 1) {
2367 /* An unsigned compare to one means that the value
2376 if (Expr2.IVal == 0) {
2377 /* An unsigned compare to zero means that the value
2385 if (Expr2.IVal == 1) {
2386 /* An unsigned compare to one means that the value
2387 ** must not be zero.
2395 if (Expr2.IVal == 0) {
2396 /* An unsigned compare to zero means that the value
2397 ** must not be zero.
2411 GenFunc (flags, Expr2.IVal);
2413 /* The result is an rvalue in the primary */
2414 ED_MakeRValExpr (Expr);
2417 /* Result type is always int */
2418 Expr->Type = type_int;
2420 Done: /* Condition codes are set */
2427 static void hie9 (ExprDesc *Expr)
2428 /* Process * and / operators. */
2430 static const GenDesc hie9_ops[] = {
2431 { TOK_STAR, GEN_NOPUSH | GEN_COMM, g_mul },
2432 { TOK_DIV, GEN_NOPUSH, g_div },
2433 { TOK_MOD, GEN_NOPUSH, g_mod },
2434 { TOK_INVALID, 0, 0 }
2438 hie_internal (hie9_ops, Expr, hie10, &UsedGen);
2443 static void parseadd (ExprDesc* Expr)
2444 /* Parse an expression with the binary plus operator. Expr contains the
2445 ** unprocessed left hand side of the expression and will contain the
2446 ** result of the expression on return.
2450 unsigned flags; /* Operation flags */
2451 CodeMark Mark; /* Remember code position */
2452 Type* lhst; /* Type of left hand side */
2453 Type* rhst; /* Type of right hand side */
2455 /* Skip the PLUS token */
2458 /* Get the left hand side type, initialize operation flags */
2462 /* Check for constness on both sides */
2463 if (ED_IsConst (Expr)) {
2465 /* The left hand side is a constant of some sort. Good. Get rhs */
2466 ExprWithCheck (hie9, &Expr2);
2467 if (ED_IsConstAbs (&Expr2)) {
2469 /* Right hand side is a constant numeric value. Get the rhs type */
2472 /* Both expressions are constants. Check for pointer arithmetic */
2473 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2474 /* Left is pointer, right is int, must scale rhs */
2475 Expr->IVal += Expr2.IVal * CheckedPSizeOf (lhst);
2476 /* Result type is a pointer */
2477 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2478 /* Left is int, right is pointer, must scale lhs */
2479 Expr->IVal = Expr->IVal * CheckedPSizeOf (rhst) + Expr2.IVal;
2480 /* Result type is a pointer */
2481 Expr->Type = Expr2.Type;
2482 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2483 /* Integer addition */
2484 Expr->IVal += Expr2.IVal;
2485 typeadjust (Expr, &Expr2, 1);
2488 Error ("Invalid operands for binary operator `+'");
2493 /* lhs is a constant and rhs is not constant. Load rhs into
2496 LoadExpr (CF_NONE, &Expr2);
2498 /* Beware: The check above (for lhs) lets not only pass numeric
2499 ** constants, but also constant addresses (labels), maybe even
2500 ** with an offset. We have to check for that here.
2503 /* First, get the rhs type. */
2507 if (ED_IsLocAbs (Expr)) {
2508 /* A numerical constant */
2511 /* Constant address label */
2512 flags |= GlobalModeFlags (Expr) | CF_CONSTADDR;
2515 /* Check for pointer arithmetic */
2516 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2517 /* Left is pointer, right is int, must scale rhs */
2518 g_scale (CF_INT, CheckedPSizeOf (lhst));
2519 /* Operate on pointers, result type is a pointer */
2521 /* Generate the code for the add */
2522 if (ED_GetLoc (Expr) == E_LOC_ABS) {
2523 /* Numeric constant */
2524 g_inc (flags, Expr->IVal);
2526 /* Constant address */
2527 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2529 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2531 /* Left is int, right is pointer, must scale lhs. */
2532 unsigned ScaleFactor = CheckedPSizeOf (rhst);
2534 /* Operate on pointers, result type is a pointer */
2536 Expr->Type = Expr2.Type;
2538 /* Since we do already have rhs in the primary, if lhs is
2539 ** not a numeric constant, and the scale factor is not one
2540 ** (no scaling), we must take the long way over the stack.
2542 if (ED_IsLocAbs (Expr)) {
2543 /* Numeric constant, scale lhs */
2544 Expr->IVal *= ScaleFactor;
2545 /* Generate the code for the add */
2546 g_inc (flags, Expr->IVal);
2547 } else if (ScaleFactor == 1) {
2548 /* Constant address but no need to scale */
2549 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2551 /* Constant address that must be scaled */
2552 g_push (TypeOf (Expr2.Type), 0); /* rhs --> stack */
2553 g_getimmed (flags, Expr->Name, Expr->IVal);
2554 g_scale (CF_PTR, ScaleFactor);
2557 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2558 /* Integer addition */
2559 flags |= typeadjust (Expr, &Expr2, 1);
2560 /* Generate the code for the add */
2561 if (ED_IsLocAbs (Expr)) {
2562 /* Numeric constant */
2563 g_inc (flags, Expr->IVal);
2565 /* Constant address */
2566 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2570 Error ("Invalid operands for binary operator `+'");
2574 /* Result is a rvalue in primary register */
2575 ED_MakeRValExpr (Expr);
2580 /* Left hand side is not constant. Get the value onto the stack. */
2581 LoadExpr (CF_NONE, Expr); /* --> primary register */
2583 g_push (TypeOf (Expr->Type), 0); /* --> stack */
2585 /* Evaluate the rhs */
2586 MarkedExprWithCheck (hie9, &Expr2);
2588 /* Check for a constant rhs expression */
2589 if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
2591 /* Right hand side is a constant. Get the rhs type */
2594 /* Remove pushed value from stack */
2597 /* Check for pointer arithmetic */
2598 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2599 /* Left is pointer, right is int, must scale rhs */
2600 Expr2.IVal *= CheckedPSizeOf (lhst);
2601 /* Operate on pointers, result type is a pointer */
2603 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2604 /* Left is int, right is pointer, must scale lhs (ptr only) */
2605 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2606 /* Operate on pointers, result type is a pointer */
2608 Expr->Type = Expr2.Type;
2609 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2610 /* Integer addition */
2611 flags = typeadjust (Expr, &Expr2, 1);
2614 Error ("Invalid operands for binary operator `+'");
2618 /* Generate code for the add */
2619 g_inc (flags | CF_CONST, Expr2.IVal);
2623 /* Not constant, load into the primary */
2624 LoadExpr (CF_NONE, &Expr2);
2626 /* lhs and rhs are not constant. Get the rhs type. */
2629 /* Check for pointer arithmetic */
2630 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2631 /* Left is pointer, right is int, must scale rhs */
2632 g_scale (CF_INT, CheckedPSizeOf (lhst));
2633 /* Operate on pointers, result type is a pointer */
2635 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2636 /* Left is int, right is pointer, must scale lhs */
2637 g_tosint (TypeOf (lhst)); /* Make sure TOS is int */
2638 g_swap (CF_INT); /* Swap TOS and primary */
2639 g_scale (CF_INT, CheckedPSizeOf (rhst));
2640 /* Operate on pointers, result type is a pointer */
2642 Expr->Type = Expr2.Type;
2643 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2644 /* Integer addition. Note: Result is never constant.
2645 ** Problem here is that typeadjust does not know if the
2646 ** variable is an rvalue or lvalue, so if both operands
2647 ** are dereferenced constant numeric addresses, typeadjust
2648 ** thinks the operation works on constants. Removing
2649 ** CF_CONST here means handling the symptoms, however, the
2650 ** whole parser is such a mess that I fear to break anything
2651 ** when trying to apply another solution.
2653 flags = typeadjust (Expr, &Expr2, 0) & ~CF_CONST;
2656 Error ("Invalid operands for binary operator `+'");
2660 /* Generate code for the add */
2665 /* Result is a rvalue in primary register */
2666 ED_MakeRValExpr (Expr);
2669 /* Condition codes not set */
2670 ED_MarkAsUntested (Expr);
2675 static void parsesub (ExprDesc* Expr)
2676 /* Parse an expression with the binary minus operator. Expr contains the
2677 ** unprocessed left hand side of the expression and will contain the
2678 ** result of the expression on return.
2682 unsigned flags; /* Operation flags */
2683 Type* lhst; /* Type of left hand side */
2684 Type* rhst; /* Type of right hand side */
2685 CodeMark Mark1; /* Save position of output queue */
2686 CodeMark Mark2; /* Another position in the queue */
2687 int rscale; /* Scale factor for the result */
2690 /* lhs cannot be function or pointer to function */
2691 if (IsTypeFunc (Expr->Type) || IsTypeFuncPtr (Expr->Type)) {
2692 Error ("Invalid left operand for binary operator `-'");
2693 /* Make it pointer to char to avoid further errors */
2694 Expr->Type = type_uchar;
2697 /* Skip the MINUS token */
2700 /* Get the left hand side type, initialize operation flags */
2702 rscale = 1; /* Scale by 1, that is, don't scale */
2704 /* Remember the output queue position, then bring the value onto the stack */
2705 GetCodePos (&Mark1);
2706 LoadExpr (CF_NONE, Expr); /* --> primary register */
2707 GetCodePos (&Mark2);
2708 g_push (TypeOf (lhst), 0); /* --> stack */
2710 /* Parse the right hand side */
2711 MarkedExprWithCheck (hie9, &Expr2);
2713 /* rhs cannot be function or pointer to function */
2714 if (IsTypeFunc (Expr2.Type) || IsTypeFuncPtr (Expr2.Type)) {
2715 Error ("Invalid right operand for binary operator `-'");
2716 /* Make it pointer to char to avoid further errors */
2717 Expr2.Type = type_uchar;
2720 /* Check for a constant rhs expression */
2721 if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
2723 /* The right hand side is constant. Get the rhs type. */
2726 /* Check left hand side */
2727 if (ED_IsConstAbs (Expr)) {
2729 /* Both sides are constant, remove generated code */
2730 RemoveCode (&Mark1);
2732 /* Check for pointer arithmetic */
2733 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2734 /* Left is pointer, right is int, must scale rhs */
2735 Expr->IVal -= Expr2.IVal * CheckedPSizeOf (lhst);
2736 /* Operate on pointers, result type is a pointer */
2737 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2738 /* Left is pointer, right is pointer, must scale result */
2739 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2740 Error ("Incompatible pointer types");
2742 Expr->IVal = (Expr->IVal - Expr2.IVal) /
2743 CheckedPSizeOf (lhst);
2745 /* Operate on pointers, result type is an integer */
2746 Expr->Type = type_int;
2747 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2748 /* Integer subtraction */
2749 typeadjust (Expr, &Expr2, 1);
2750 Expr->IVal -= Expr2.IVal;
2753 Error ("Invalid operands for binary operator `-'");
2756 /* Result is constant, condition codes not set */
2757 ED_MarkAsUntested (Expr);
2761 /* Left hand side is not constant, right hand side is.
2762 ** Remove pushed value from stack.
2764 RemoveCode (&Mark2);
2766 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2767 /* Left is pointer, right is int, must scale rhs */
2768 Expr2.IVal *= CheckedPSizeOf (lhst);
2769 /* Operate on pointers, result type is a pointer */
2771 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2772 /* Left is pointer, right is pointer, must scale result */
2773 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2774 Error ("Incompatible pointer types");
2776 rscale = CheckedPSizeOf (lhst);
2778 /* Operate on pointers, result type is an integer */
2780 Expr->Type = type_int;
2781 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2782 /* Integer subtraction */
2783 flags = typeadjust (Expr, &Expr2, 1);
2786 Error ("Invalid operands for binary operator `-'");
2790 /* Do the subtraction */
2791 g_dec (flags | CF_CONST, Expr2.IVal);
2793 /* If this was a pointer subtraction, we must scale the result */
2795 g_scale (flags, -rscale);
2798 /* Result is a rvalue in the primary register */
2799 ED_MakeRValExpr (Expr);
2800 ED_MarkAsUntested (Expr);
2806 /* Not constant, load into the primary */
2807 LoadExpr (CF_NONE, &Expr2);
2809 /* Right hand side is not constant. Get the rhs type. */
2812 /* Check for pointer arithmetic */
2813 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2814 /* Left is pointer, right is int, must scale rhs */
2815 g_scale (CF_INT, CheckedPSizeOf (lhst));
2816 /* Operate on pointers, result type is a pointer */
2818 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2819 /* Left is pointer, right is pointer, must scale result */
2820 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2821 Error ("Incompatible pointer types");
2823 rscale = CheckedPSizeOf (lhst);
2825 /* Operate on pointers, result type is an integer */
2827 Expr->Type = type_int;
2828 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2829 /* Integer subtraction. If the left hand side descriptor says that
2830 ** the lhs is const, we have to remove this mark, since this is no
2831 ** longer true, lhs is on stack instead.
2833 if (ED_IsLocAbs (Expr)) {
2834 ED_MakeRValExpr (Expr);
2836 /* Adjust operand types */
2837 flags = typeadjust (Expr, &Expr2, 0);
2840 Error ("Invalid operands for binary operator `-'");
2844 /* Generate code for the sub (the & is a hack here) */
2845 g_sub (flags & ~CF_CONST, 0);
2847 /* If this was a pointer subtraction, we must scale the result */
2849 g_scale (flags, -rscale);
2852 /* Result is a rvalue in the primary register */
2853 ED_MakeRValExpr (Expr);
2854 ED_MarkAsUntested (Expr);
2860 void hie8 (ExprDesc* Expr)
2861 /* Process + and - binary operators. */
2863 ExprWithCheck (hie9, Expr);
2864 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2865 if (CurTok.Tok == TOK_PLUS) {
2875 static void hie6 (ExprDesc* Expr)
2876 /* Handle greater-than type comparators */
2878 static const GenDesc hie6_ops [] = {
2879 { TOK_LT, GEN_NOPUSH | GEN_NOFUNC, g_lt },
2880 { TOK_LE, GEN_NOPUSH | GEN_NOFUNC, g_le },
2881 { TOK_GE, GEN_NOPUSH | GEN_NOFUNC, g_ge },
2882 { TOK_GT, GEN_NOPUSH | GEN_NOFUNC, g_gt },
2883 { TOK_INVALID, 0, 0 }
2885 hie_compare (hie6_ops, Expr, ShiftExpr);
2890 static void hie5 (ExprDesc* Expr)
2891 /* Handle == and != */
2893 static const GenDesc hie5_ops[] = {
2894 { TOK_EQ, GEN_NOPUSH, g_eq },
2895 { TOK_NE, GEN_NOPUSH, g_ne },
2896 { TOK_INVALID, 0, 0 }
2898 hie_compare (hie5_ops, Expr, hie6);
2903 static void hie4 (ExprDesc* Expr)
2904 /* Handle & (bitwise and) */
2906 static const GenDesc hie4_ops[] = {
2907 { TOK_AND, GEN_NOPUSH | GEN_COMM, g_and },
2908 { TOK_INVALID, 0, 0 }
2912 hie_internal (hie4_ops, Expr, hie5, &UsedGen);
2917 static void hie3 (ExprDesc* Expr)
2918 /* Handle ^ (bitwise exclusive or) */
2920 static const GenDesc hie3_ops[] = {
2921 { TOK_XOR, GEN_NOPUSH | GEN_COMM, g_xor },
2922 { TOK_INVALID, 0, 0 }
2926 hie_internal (hie3_ops, Expr, hie4, &UsedGen);
2931 static void hie2 (ExprDesc* Expr)
2932 /* Handle | (bitwise or) */
2934 static const GenDesc hie2_ops[] = {
2935 { TOK_OR, GEN_NOPUSH | GEN_COMM, g_or },
2936 { TOK_INVALID, 0, 0 }
2940 hie_internal (hie2_ops, Expr, hie3, &UsedGen);
2945 static void hieAndPP (ExprDesc* Expr)
2946 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2947 ** called recursively from the preprocessor.
2952 ConstAbsIntExpr (hie2, Expr);
2953 while (CurTok.Tok == TOK_BOOL_AND) {
2959 ConstAbsIntExpr (hie2, &Expr2);
2961 /* Combine the two */
2962 Expr->IVal = (Expr->IVal && Expr2.IVal);
2968 static void hieOrPP (ExprDesc *Expr)
2969 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2970 ** called recursively from the preprocessor.
2975 ConstAbsIntExpr (hieAndPP, Expr);
2976 while (CurTok.Tok == TOK_BOOL_OR) {
2982 ConstAbsIntExpr (hieAndPP, &Expr2);
2984 /* Combine the two */
2985 Expr->IVal = (Expr->IVal || Expr2.IVal);
2991 static void hieAnd (ExprDesc* Expr, unsigned TrueLab, int* BoolOp)
2992 /* Process "exp && exp" */
2997 ExprWithCheck (hie2, Expr);
2998 if (CurTok.Tok == TOK_BOOL_AND) {
3000 /* Tell our caller that we're evaluating a boolean */
3003 /* Get a label that we will use for false expressions */
3004 FalseLab = GetLocalLabel ();
3006 /* If the expr hasn't set condition codes, set the force-test flag */
3007 if (!ED_IsTested (Expr)) {
3008 ED_MarkForTest (Expr);
3011 /* Load the value */
3012 LoadExpr (CF_FORCECHAR, Expr);
3014 /* Generate the jump */
3015 g_falsejump (CF_NONE, FalseLab);
3017 /* Parse more boolean and's */
3018 while (CurTok.Tok == TOK_BOOL_AND) {
3025 if (!ED_IsTested (&Expr2)) {
3026 ED_MarkForTest (&Expr2);
3028 LoadExpr (CF_FORCECHAR, &Expr2);
3030 /* Do short circuit evaluation */
3031 if (CurTok.Tok == TOK_BOOL_AND) {
3032 g_falsejump (CF_NONE, FalseLab);
3034 /* Last expression - will evaluate to true */
3035 g_truejump (CF_NONE, TrueLab);
3039 /* Define the false jump label here */
3040 g_defcodelabel (FalseLab);
3042 /* The result is an rvalue in primary */
3043 ED_MakeRValExpr (Expr);
3044 ED_TestDone (Expr); /* Condition codes are set */
3050 static void hieOr (ExprDesc *Expr)
3051 /* Process "exp || exp". */
3054 int BoolOp = 0; /* Did we have a boolean op? */
3055 int AndOp; /* Did we have a && operation? */
3056 unsigned TrueLab; /* Jump to this label if true */
3060 TrueLab = GetLocalLabel ();
3062 /* Call the next level parser */
3063 hieAnd (Expr, TrueLab, &BoolOp);
3065 /* Any boolean or's? */
3066 if (CurTok.Tok == TOK_BOOL_OR) {
3068 /* If the expr hasn't set condition codes, set the force-test flag */
3069 if (!ED_IsTested (Expr)) {
3070 ED_MarkForTest (Expr);
3073 /* Get first expr */
3074 LoadExpr (CF_FORCECHAR, Expr);
3076 /* For each expression jump to TrueLab if true. Beware: If we
3077 ** had && operators, the jump is already in place!
3080 g_truejump (CF_NONE, TrueLab);
3083 /* Remember that we had a boolean op */
3086 /* while there's more expr */
3087 while (CurTok.Tok == TOK_BOOL_OR) {
3094 hieAnd (&Expr2, TrueLab, &AndOp);
3095 if (!ED_IsTested (&Expr2)) {
3096 ED_MarkForTest (&Expr2);
3098 LoadExpr (CF_FORCECHAR, &Expr2);
3100 /* If there is more to come, add shortcut boolean eval. */
3101 g_truejump (CF_NONE, TrueLab);
3105 /* The result is an rvalue in primary */
3106 ED_MakeRValExpr (Expr);
3107 ED_TestDone (Expr); /* Condition codes are set */
3110 /* If we really had boolean ops, generate the end sequence */
3112 DoneLab = GetLocalLabel ();
3113 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
3114 g_falsejump (CF_NONE, DoneLab);
3115 g_defcodelabel (TrueLab);
3116 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
3117 g_defcodelabel (DoneLab);
3123 static void hieQuest (ExprDesc* Expr)
3124 /* Parse the ternary operator */
3128 CodeMark TrueCodeEnd;
3129 ExprDesc Expr2; /* Expression 2 */
3130 ExprDesc Expr3; /* Expression 3 */
3131 int Expr2IsNULL; /* Expression 2 is a NULL pointer */
3132 int Expr3IsNULL; /* Expression 3 is a NULL pointer */
3133 Type* ResultType; /* Type of result */
3136 /* Call the lower level eval routine */
3137 if (Preprocessing) {
3138 ExprWithCheck (hieOrPP, Expr);
3140 ExprWithCheck (hieOr, Expr);
3143 /* Check if it's a ternary expression */
3144 if (CurTok.Tok == TOK_QUEST) {
3146 if (!ED_IsTested (Expr)) {
3147 /* Condition codes not set, request a test */
3148 ED_MarkForTest (Expr);
3150 LoadExpr (CF_NONE, Expr);
3151 FalseLab = GetLocalLabel ();
3152 g_falsejump (CF_NONE, FalseLab);
3154 /* Parse second expression. Remember for later if it is a NULL pointer
3155 ** expression, then load it into the primary.
3157 ExprWithCheck (hie1, &Expr2);
3158 Expr2IsNULL = ED_IsNullPtr (&Expr2);
3159 if (!IsTypeVoid (Expr2.Type)) {
3160 /* Load it into the primary */
3161 LoadExpr (CF_NONE, &Expr2);
3162 ED_MakeRValExpr (&Expr2);
3163 Expr2.Type = PtrConversion (Expr2.Type);
3166 /* Remember the current code position */
3167 GetCodePos (&TrueCodeEnd);
3169 /* Jump around the evaluation of the third expression */
3170 TrueLab = GetLocalLabel ();
3174 /* Jump here if the first expression was false */
3175 g_defcodelabel (FalseLab);
3177 /* Parse third expression. Remember for later if it is a NULL pointer
3178 ** expression, then load it into the primary.
3180 ExprWithCheck (hie1, &Expr3);
3181 Expr3IsNULL = ED_IsNullPtr (&Expr3);
3182 if (!IsTypeVoid (Expr3.Type)) {
3183 /* Load it into the primary */
3184 LoadExpr (CF_NONE, &Expr3);
3185 ED_MakeRValExpr (&Expr3);
3186 Expr3.Type = PtrConversion (Expr3.Type);
3189 /* Check if any conversions are needed, if so, do them.
3190 ** Conversion rules for ?: expression are:
3191 ** - if both expressions are int expressions, default promotion
3192 ** rules for ints apply.
3193 ** - if both expressions are pointers of the same type, the
3194 ** result of the expression is of this type.
3195 ** - if one of the expressions is a pointer and the other is
3196 ** a zero constant, the resulting type is that of the pointer
3198 ** - if both expressions are void expressions, the result is of
3200 ** - all other cases are flagged by an error.
3202 if (IsClassInt (Expr2.Type) && IsClassInt (Expr3.Type)) {
3204 CodeMark CvtCodeStart;
3205 CodeMark CvtCodeEnd;
3208 /* Get common type */
3209 ResultType = promoteint (Expr2.Type, Expr3.Type);
3211 /* Convert the third expression to this type if needed */
3212 TypeConversion (&Expr3, ResultType);
3214 /* Emit conversion code for the second expression, but remember
3215 ** where it starts end ends.
3217 GetCodePos (&CvtCodeStart);
3218 TypeConversion (&Expr2, ResultType);
3219 GetCodePos (&CvtCodeEnd);
3221 /* If we had conversion code, move it to the right place */
3222 if (!CodeRangeIsEmpty (&CvtCodeStart, &CvtCodeEnd)) {
3223 MoveCode (&CvtCodeStart, &CvtCodeEnd, &TrueCodeEnd);
3226 } else if (IsClassPtr (Expr2.Type) && IsClassPtr (Expr3.Type)) {
3227 /* Must point to same type */
3228 if (TypeCmp (Indirect (Expr2.Type), Indirect (Expr3.Type)) < TC_EQUAL) {
3229 Error ("Incompatible pointer types");
3231 /* Result has the common type */
3232 ResultType = Expr2.Type;
3233 } else if (IsClassPtr (Expr2.Type) && Expr3IsNULL) {
3234 /* Result type is pointer, no cast needed */
3235 ResultType = Expr2.Type;
3236 } else if (Expr2IsNULL && IsClassPtr (Expr3.Type)) {
3237 /* Result type is pointer, no cast needed */
3238 ResultType = Expr3.Type;
3239 } else if (IsTypeVoid (Expr2.Type) && IsTypeVoid (Expr3.Type)) {
3240 /* Result type is void */
3241 ResultType = Expr3.Type;
3243 Error ("Incompatible types");
3244 ResultType = Expr2.Type; /* Doesn't matter here */
3247 /* Define the final label */
3248 g_defcodelabel (TrueLab);
3250 /* Setup the target expression */
3251 ED_MakeRValExpr (Expr);
3252 Expr->Type = ResultType;
3258 static void opeq (const GenDesc* Gen, ExprDesc* Expr, const char* Op)
3259 /* Process "op=" operators. */
3266 /* op= can only be used with lvalues */
3267 if (!ED_IsLVal (Expr)) {
3268 Error ("Invalid lvalue in assignment");
3272 /* The left side must not be const qualified */
3273 if (IsQualConst (Expr->Type)) {
3274 Error ("Assignment to const");
3277 /* There must be an integer or pointer on the left side */
3278 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
3279 Error ("Invalid left operand type");
3280 /* Continue. Wrong code will be generated, but the compiler won't
3281 ** break, so this is the best error recovery.
3285 /* Skip the operator token */
3288 /* Determine the type of the lhs */
3289 flags = TypeOf (Expr->Type);
3290 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) && IsTypePtr (Expr->Type);
3292 /* Get the lhs address on stack (if needed) */
3295 /* Fetch the lhs into the primary register if needed */
3296 LoadExpr (CF_NONE, Expr);
3298 /* Bring the lhs on stack */
3302 /* Evaluate the rhs */
3303 MarkedExprWithCheck (hie1, &Expr2);
3305 /* The rhs must be an integer (or a float, but we don't support that yet */
3306 if (!IsClassInt (Expr2.Type)) {
3307 Error ("Invalid right operand for binary operator `%s'", Op);
3308 /* Continue. Wrong code will be generated, but the compiler won't
3309 ** break, so this is the best error recovery.
3313 /* Check for a constant expression */
3314 if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
3315 /* The resulting value is a constant. If the generator has the NOPUSH
3316 ** flag set, don't push the lhs.
3318 if (Gen->Flags & GEN_NOPUSH) {
3322 /* lhs is a pointer, scale rhs */
3323 Expr2.IVal *= CheckedSizeOf (Expr->Type+1);
3326 /* If the lhs is character sized, the operation may be later done
3329 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
3330 flags |= CF_FORCECHAR;
3333 /* Special handling for add and sub - some sort of a hack, but short code */
3334 if (Gen->Func == g_add) {
3335 g_inc (flags | CF_CONST, Expr2.IVal);
3336 } else if (Gen->Func == g_sub) {
3337 g_dec (flags | CF_CONST, Expr2.IVal);
3339 if (Expr2.IVal == 0) {
3340 /* Check for div by zero/mod by zero */
3341 if (Gen->Func == g_div) {
3342 Error ("Division by zero");
3343 } else if (Gen->Func == g_mod) {
3344 Error ("Modulo operation with zero");
3347 Gen->Func (flags | CF_CONST, Expr2.IVal);
3351 /* rhs is not constant. Load into the primary */
3352 LoadExpr (CF_NONE, &Expr2);
3354 /* lhs is a pointer, scale rhs */
3355 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Expr->Type+1));
3358 /* If the lhs is character sized, the operation may be later done
3361 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
3362 flags |= CF_FORCECHAR;
3365 /* Adjust the types of the operands if needed */
3366 Gen->Func (g_typeadjust (flags, TypeOf (Expr2.Type)), 0);
3369 ED_MakeRValExpr (Expr);
3374 static void addsubeq (const GenDesc* Gen, ExprDesc *Expr, const char* Op)
3375 /* Process the += and -= operators */
3383 /* We're currently only able to handle some adressing modes */
3384 if (ED_GetLoc (Expr) == E_LOC_EXPR || ED_GetLoc (Expr) == E_LOC_PRIMARY) {
3385 /* Use generic routine */
3386 opeq (Gen, Expr, Op);
3390 /* We must have an lvalue */
3391 if (ED_IsRVal (Expr)) {
3392 Error ("Invalid lvalue in assignment");
3396 /* The left side must not be const qualified */
3397 if (IsQualConst (Expr->Type)) {
3398 Error ("Assignment to const");
3401 /* There must be an integer or pointer on the left side */
3402 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
3403 Error ("Invalid left operand type");
3404 /* Continue. Wrong code will be generated, but the compiler won't
3405 ** break, so this is the best error recovery.
3409 /* Skip the operator */
3412 /* Check if we have a pointer expression and must scale rhs */
3413 MustScale = IsTypePtr (Expr->Type);
3415 /* Initialize the code generator flags */
3419 /* Evaluate the rhs. We expect an integer here, since float is not
3423 if (!IsClassInt (Expr2.Type)) {
3424 Error ("Invalid right operand for binary operator `%s'", Op);
3425 /* Continue. Wrong code will be generated, but the compiler won't
3426 ** break, so this is the best error recovery.
3429 if (ED_IsConstAbs (&Expr2)) {
3430 /* The resulting value is a constant. Scale it. */
3432 Expr2.IVal *= CheckedSizeOf (Indirect (Expr->Type));
3437 /* Not constant, load into the primary */
3438 LoadExpr (CF_NONE, &Expr2);
3440 /* lhs is a pointer, scale rhs */
3441 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Indirect (Expr->Type)));
3445 /* Setup the code generator flags */
3446 lflags |= TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR;
3447 rflags |= TypeOf (Expr2.Type) | CF_FORCECHAR;
3449 /* Convert the type of the lhs to that of the rhs */
3450 g_typecast (lflags, rflags);
3452 /* Output apropriate code depending on the location */
3453 switch (ED_GetLoc (Expr)) {
3456 /* Absolute: numeric address or const */
3457 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3458 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3460 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3465 /* Global variable */
3466 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3467 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3469 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3475 /* Static variable or literal in the literal pool */
3476 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3477 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3479 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3483 case E_LOC_REGISTER:
3484 /* Register variable */
3485 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3486 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3488 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3493 /* Value on the stack */
3494 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3495 g_addeqlocal (lflags, Expr->IVal, Expr2.IVal);
3497 g_subeqlocal (lflags, Expr->IVal, Expr2.IVal);
3502 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
3505 /* Expression is a rvalue in the primary now */
3506 ED_MakeRValExpr (Expr);
3511 void hie1 (ExprDesc* Expr)
3512 /* Parse first level of expression hierarchy. */
3515 switch (CurTok.Tok) {
3521 case TOK_PLUS_ASSIGN:
3522 addsubeq (&GenPASGN, Expr, "+=");
3525 case TOK_MINUS_ASSIGN:
3526 addsubeq (&GenSASGN, Expr, "-=");
3529 case TOK_MUL_ASSIGN:
3530 opeq (&GenMASGN, Expr, "*=");
3533 case TOK_DIV_ASSIGN:
3534 opeq (&GenDASGN, Expr, "/=");
3537 case TOK_MOD_ASSIGN:
3538 opeq (&GenMOASGN, Expr, "%=");
3541 case TOK_SHL_ASSIGN:
3542 opeq (&GenSLASGN, Expr, "<<=");
3545 case TOK_SHR_ASSIGN:
3546 opeq (&GenSRASGN, Expr, ">>=");
3549 case TOK_AND_ASSIGN:
3550 opeq (&GenAASGN, Expr, "&=");
3553 case TOK_XOR_ASSIGN:
3554 opeq (&GenXOASGN, Expr, "^=");
3558 opeq (&GenOASGN, Expr, "|=");
3568 void hie0 (ExprDesc *Expr)
3569 /* Parse comma operator. */
3572 while (CurTok.Tok == TOK_COMMA) {
3580 int evalexpr (unsigned Flags, void (*Func) (ExprDesc*), ExprDesc* Expr)
3581 /* Will evaluate an expression via the given function. If the result is a
3582 ** constant, 0 is returned and the value is put in the Expr struct. If the
3583 ** result is not constant, LoadExpr is called to bring the value into the
3584 ** primary register and 1 is returned.
3588 ExprWithCheck (Func, Expr);
3590 /* Check for a constant expression */
3591 if (ED_IsConstAbs (Expr)) {
3592 /* Constant expression */
3595 /* Not constant, load into the primary */
3596 LoadExpr (Flags, Expr);
3603 void Expression0 (ExprDesc* Expr)
3604 /* Evaluate an expression via hie0 and put the result into the primary register */
3606 ExprWithCheck (hie0, Expr);
3607 LoadExpr (CF_NONE, Expr);
3612 void ConstExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3613 /* Will evaluate an expression via the given function. If the result is not
3614 ** a constant of some sort, a diagnostic will be printed, and the value is
3615 ** replaced by a constant one to make sure there are no internal errors that
3616 ** result from this input error.
3619 ExprWithCheck (Func, Expr);
3620 if (!ED_IsConst (Expr)) {
3621 Error ("Constant expression expected");
3622 /* To avoid any compiler errors, make the expression a valid const */
3623 ED_MakeConstAbsInt (Expr, 1);
3629 void BoolExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3630 /* Will evaluate an expression via the given function. If the result is not
3631 ** something that may be evaluated in a boolean context, a diagnostic will be
3632 ** printed, and the value is replaced by a constant one to make sure there
3633 ** are no internal errors that result from this input error.
3636 ExprWithCheck (Func, Expr);
3637 if (!ED_IsBool (Expr)) {
3638 Error ("Boolean expression expected");
3639 /* To avoid any compiler errors, make the expression a valid int */
3640 ED_MakeConstAbsInt (Expr, 1);
3646 void ConstAbsIntExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3647 /* Will evaluate an expression via the given function. If the result is not
3648 ** a constant numeric integer value, a diagnostic will be printed, and the
3649 ** value is replaced by a constant one to make sure there are no internal
3650 ** errors that result from this input error.
3653 ExprWithCheck (Func, Expr);
3654 if (!ED_IsConstAbsInt (Expr)) {
3655 Error ("Constant integer expression expected");
3656 /* To avoid any compiler errors, make the expression a valid const */
3657 ED_MakeConstAbsInt (Expr, 1);