3 ** 1998-06-21, Ullrich von Bassewitz
4 ** 2017-12-05, 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 /* A computed goto label address */
701 if (IS_Get (&Standard) >= STD_CC65) {
704 Entry = AddLabelSym (CurTok.Ident, SC_REF | SC_GOTO_IND);
705 /* output its label */
706 E->Flags = E_RTYPE_RVAL | E_LOC_STATIC;
707 E->Name = Entry->V.L.Label;
708 E->Type = PointerTo (type_void);
711 Error ("Computed gotos are a C extension, not supported with this --standard");
712 ED_MakeConstAbsInt (E, 1);
717 /* Identifier. Get a pointer to the symbol table entry */
718 Sym = E->Sym = FindSym (CurTok.Ident);
720 /* Is the symbol known? */
723 /* We found the symbol - skip the name token */
726 /* Check for illegal symbol types */
727 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
728 if (Sym->Flags & SC_TYPE) {
729 /* Cannot use type symbols */
730 Error ("Variable identifier expected");
731 /* Assume an int type to make E valid */
732 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
737 /* Mark the symbol as referenced */
738 Sym->Flags |= SC_REF;
740 /* The expression type is the symbol type */
743 /* Check for legal symbol types */
744 if ((Sym->Flags & SC_CONST) == SC_CONST) {
745 /* Enum or some other numeric constant */
746 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
747 E->IVal = Sym->V.ConstVal;
748 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
750 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
751 E->Name = (uintptr_t) Sym->Name;
752 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
753 /* Local variable. If this is a parameter for a variadic
754 ** function, we have to add some address calculations, and the
755 ** address is not const.
757 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
758 /* Variadic parameter */
759 g_leavariadic (Sym->V.Offs - F_GetParamSize (CurrentFunc));
760 E->Flags = E_LOC_EXPR | E_RTYPE_LVAL;
762 /* Normal parameter */
763 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
764 E->IVal = Sym->V.Offs;
766 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
767 /* Register variable, zero page based */
768 E->Flags = E_LOC_REGISTER | E_RTYPE_LVAL;
769 E->Name = Sym->V.R.RegOffs;
770 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
771 /* Static variable */
772 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
773 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
774 E->Name = (uintptr_t) Sym->Name;
776 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
777 E->Name = Sym->V.L.Label;
780 /* Local static variable */
781 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
782 E->Name = Sym->V.Offs;
785 /* We've made all variables lvalues above. However, this is
786 ** not always correct: An array is actually the address of its
787 ** first element, which is a rvalue, and a function is a
788 ** rvalue, too, because we cannot store anything in a function.
789 ** So fix the flags depending on the type.
791 if (IsTypeArray (E->Type) || IsTypeFunc (E->Type)) {
797 /* We did not find the symbol. Remember the name, then skip it */
799 strcpy (Ident, CurTok.Ident);
802 /* IDENT is either an auto-declared function or an undefined variable. */
803 if (CurTok.Tok == TOK_LPAREN) {
804 /* C99 doesn't allow calls to undefined functions, so
805 ** generate an error and otherwise a warning. Declare a
806 ** function returning int. For that purpose, prepare a
807 ** function signature for a function having an empty param
808 ** list and returning int.
810 if (IS_Get (&Standard) >= STD_C99) {
811 Error ("Call to undefined function '%s'", Ident);
813 Warning ("Call to undefined function '%s'", Ident);
815 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
817 E->Flags = E_LOC_GLOBAL | E_RTYPE_RVAL;
818 E->Name = (uintptr_t) Sym->Name;
820 /* Undeclared Variable */
821 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
822 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
824 Error ("Undefined symbol: '%s'", Ident);
833 E->LVal = UseLiteral (CurTok.SVal);
834 E->Type = GetCharArrayType (GetLiteralSize (CurTok.SVal));
835 E->Flags = E_LOC_LITERAL | E_RTYPE_RVAL;
837 E->Name = GetLiteralLabel (CurTok.SVal);
844 E->Flags = E_LOC_EXPR | E_RTYPE_RVAL;
849 /* Register pseudo variable */
850 E->Type = type_uchar;
851 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
856 /* Register pseudo variable */
858 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
863 /* Register pseudo variable */
864 E->Type = type_ulong;
865 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
870 /* Illegal primary. Be sure to skip the token to avoid endless
873 Error ("Expression expected");
875 ED_MakeConstAbsInt (E, 1);
882 static void ArrayRef (ExprDesc* Expr)
883 /* Handle an array reference. This function needs a rewrite. */
894 /* Skip the bracket */
897 /* Get the type of left side */
900 /* We can apply a special treatment for arrays that have a const base
901 ** address. This is true for most arrays and will produce a lot better
902 ** code. Check if this is a const base address.
904 ConstBaseAddr = ED_IsRVal (Expr) &&
905 (ED_IsLocConst (Expr) || ED_IsLocStack (Expr));
907 /* If we have a constant base, we delay the address fetch */
909 if (!ConstBaseAddr) {
910 /* Get a pointer to the array into the primary */
911 LoadExpr (CF_NONE, Expr);
913 /* Get the array pointer on stack. Do not push more than 16
914 ** bit, even if this value is greater, since we cannot handle
915 ** other than 16bit stuff when doing indexing.
921 /* TOS now contains ptr to array elements. Get the subscript. */
922 MarkedExprWithCheck (hie0, &Subscript);
924 /* Check the types of array and subscript. We can either have a
925 ** pointer/array to the left, in which case the subscript must be of an
926 ** integer type, or we have an integer to the left, in which case the
927 ** subscript must be a pointer/array.
928 ** Since we do the necessary checking here, we can rely later on the
931 Qualifiers = T_QUAL_NONE;
932 if (IsClassPtr (Expr->Type)) {
933 if (!IsClassInt (Subscript.Type)) {
934 Error ("Array subscript is not an integer");
935 /* To avoid any compiler errors, make the expression a valid int */
936 ED_MakeConstAbsInt (&Subscript, 0);
938 if (IsTypeArray (Expr->Type)) {
939 Qualifiers = GetQualifier (Expr->Type);
941 ElementType = Indirect (Expr->Type);
942 } else if (IsClassInt (Expr->Type)) {
943 if (!IsClassPtr (Subscript.Type)) {
944 Error ("Subscripted value is neither array nor pointer");
945 /* To avoid compiler errors, make the subscript a char[] at
948 ED_MakeConstAbs (&Subscript, 0, GetCharArrayType (1));
949 } else if (IsTypeArray (Subscript.Type)) {
950 Qualifiers = GetQualifier (Subscript.Type);
952 ElementType = Indirect (Subscript.Type);
954 Error ("Cannot subscript");
955 /* To avoid compiler errors, fake both the array and the subscript, so
956 ** we can just proceed.
958 ED_MakeConstAbs (Expr, 0, GetCharArrayType (1));
959 ED_MakeConstAbsInt (&Subscript, 0);
960 ElementType = Indirect (Expr->Type);
963 /* The element type has the combined qualifiers from itself and the array,
964 ** it is a member of (if any).
966 if (GetQualifier (ElementType) != (GetQualifier (ElementType) | Qualifiers)) {
967 ElementType = TypeDup (ElementType);
968 ElementType->C |= Qualifiers;
971 /* If the subscript is a bit-field, load it and make it an rvalue */
972 if (ED_IsBitField (&Subscript)) {
973 LoadExpr (CF_NONE, &Subscript);
974 ED_MakeRValExpr (&Subscript);
977 /* Check if the subscript is constant absolute value */
978 if (ED_IsConstAbs (&Subscript) && ED_CodeRangeIsEmpty (&Subscript)) {
980 /* The array subscript is a numeric constant. If we had pushed the
981 ** array base address onto the stack before, we can remove this value,
982 ** since we can generate expression+offset.
984 if (!ConstBaseAddr) {
987 /* Get an array pointer into the primary */
988 LoadExpr (CF_NONE, Expr);
991 if (IsClassPtr (Expr->Type)) {
993 /* Lhs is pointer/array. Scale the subscript value according to
996 Subscript.IVal *= CheckedSizeOf (ElementType);
998 /* Remove the address load code */
1001 /* In case of an array, we can adjust the offset of the expression
1002 ** already in Expr. If the base address was a constant, we can even
1003 ** remove the code that loaded the address into the primary.
1005 if (IsTypeArray (Expr->Type)) {
1007 /* Adjust the offset */
1008 Expr->IVal += Subscript.IVal;
1012 /* It's a pointer, so we do have to load it into the primary
1013 ** first (if it's not already there).
1015 if (ConstBaseAddr || ED_IsLVal (Expr)) {
1016 LoadExpr (CF_NONE, Expr);
1017 ED_MakeRValExpr (Expr);
1020 /* Use the offset */
1021 Expr->IVal = Subscript.IVal;
1026 /* Scale the rhs value according to the element type */
1027 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
1029 /* Add the subscript. Since arrays are indexed by integers,
1030 ** we will ignore the true type of the subscript here and
1031 ** use always an int. #### Use offset but beware of LoadExpr!
1033 g_inc (CF_INT | CF_CONST, Subscript.IVal);
1039 /* Array subscript is not constant. Load it into the primary */
1040 GetCodePos (&Mark2);
1041 LoadExpr (CF_NONE, &Subscript);
1044 if (IsClassPtr (Expr->Type)) {
1046 /* Indexing is based on unsigneds, so we will just use the integer
1047 ** portion of the index (which is in (e)ax, so there's no further
1048 ** action required).
1050 g_scale (CF_INT, CheckedSizeOf (ElementType));
1054 /* Get the int value on top. If we come here, we're sure, both
1055 ** values are 16 bit (the first one was truncated if necessary
1056 ** and the second one is a pointer). Note: If ConstBaseAddr is
1057 ** true, we don't have a value on stack, so to "swap" both, just
1058 ** push the subscript.
1060 if (ConstBaseAddr) {
1062 LoadExpr (CF_NONE, Expr);
1069 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
1073 /* The offset is now in the primary register. It we didn't have a
1074 ** constant base address for the lhs, the lhs address is already
1075 ** on stack, and we must add the offset. If the base address was
1076 ** constant, we call special functions to add the address to the
1079 if (!ConstBaseAddr) {
1081 /* The array base address is on stack and the subscript is in the
1082 ** primary. Add both.
1088 /* The subscript is in the primary, and the array base address is
1089 ** in Expr. If the subscript has itself a constant address, it is
1090 ** often a better idea to reverse again the order of the
1091 ** evaluation. This will generate better code if the subscript is
1092 ** a byte sized variable. But beware: This is only possible if the
1093 ** subscript was not scaled, that is, if this was a byte array
1096 if ((ED_IsLocConst (&Subscript) || ED_IsLocStack (&Subscript)) &&
1097 CheckedSizeOf (ElementType) == SIZEOF_CHAR) {
1101 /* Reverse the order of evaluation */
1102 if (CheckedSizeOf (Subscript.Type) == SIZEOF_CHAR) {
1107 RemoveCode (&Mark2);
1109 /* Get a pointer to the array into the primary. */
1110 LoadExpr (CF_NONE, Expr);
1112 /* Add the variable */
1113 if (ED_IsLocStack (&Subscript)) {
1114 g_addlocal (Flags, Subscript.IVal);
1116 Flags |= GlobalModeFlags (&Subscript);
1117 g_addstatic (Flags, Subscript.Name, Subscript.IVal);
1121 if (ED_IsLocAbs (Expr)) {
1122 /* Constant numeric address. Just add it */
1123 g_inc (CF_INT, Expr->IVal);
1124 } else if (ED_IsLocStack (Expr)) {
1125 /* Base address is a local variable address */
1126 if (IsTypeArray (Expr->Type)) {
1127 g_addaddr_local (CF_INT, Expr->IVal);
1129 g_addlocal (CF_PTR, Expr->IVal);
1132 /* Base address is a static variable address */
1133 unsigned Flags = CF_INT | GlobalModeFlags (Expr);
1134 if (ED_IsRVal (Expr)) {
1135 /* Add the address of the location */
1136 g_addaddr_static (Flags, Expr->Name, Expr->IVal);
1138 /* Add the contents of the location */
1139 g_addstatic (Flags, Expr->Name, Expr->IVal);
1147 /* The result is an expression in the primary */
1148 ED_MakeRValExpr (Expr);
1152 /* Result is of element type */
1153 Expr->Type = ElementType;
1155 /* An array element is actually a variable. So the rules for variables
1156 ** with respect to the reference type apply: If it's an array, it is
1157 ** a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1158 ** but an array cannot contain functions).
1160 if (IsTypeArray (Expr->Type)) {
1166 /* Consume the closing bracket */
1172 static void StructRef (ExprDesc* Expr)
1173 /* Process struct field after . or ->. */
1180 /* Skip the token and check for an identifier */
1182 if (CurTok.Tok != TOK_IDENT) {
1183 Error ("Identifier expected");
1184 /* Make the expression an integer at address zero */
1185 ED_MakeConstAbs (Expr, 0, type_int);
1189 /* Get the symbol table entry and check for a struct field */
1190 strcpy (Ident, CurTok.Ident);
1192 Field = FindStructField (Expr->Type, Ident);
1194 Error ("Struct/union has no field named '%s'", Ident);
1195 /* Make the expression an integer at address zero */
1196 ED_MakeConstAbs (Expr, 0, type_int);
1200 /* If we have a struct pointer that is an lvalue and not already in the
1201 ** primary, load it now.
1203 if (ED_IsLVal (Expr) && IsTypePtr (Expr->Type)) {
1205 /* Load into the primary */
1206 LoadExpr (CF_NONE, Expr);
1208 /* Make it an lvalue expression */
1209 ED_MakeLValExpr (Expr);
1212 /* The type is the type of the field plus any qualifiers from the struct */
1213 if (IsClassStruct (Expr->Type)) {
1214 Q = GetQualifier (Expr->Type);
1216 Q = GetQualifier (Indirect (Expr->Type));
1218 if (GetQualifier (Field->Type) == (GetQualifier (Field->Type) | Q)) {
1219 FinalType = Field->Type;
1221 FinalType = TypeDup (Field->Type);
1225 /* A struct is usually an lvalue. If not, it is a struct in the primary
1228 if (ED_IsRVal (Expr) && ED_IsLocExpr (Expr) && !IsTypePtr (Expr->Type)) {
1233 /* Get the size of the type */
1234 unsigned Size = SizeOf (Expr->Type);
1237 CHECK (Field->V.Offs + Size <= SIZEOF_LONG);
1239 /* The type of the operation depends on the type of the struct */
1241 case 1: Flags = CF_CHAR | CF_UNSIGNED | CF_CONST; break;
1242 case 2: Flags = CF_INT | CF_UNSIGNED | CF_CONST; break;
1243 case 3: /* FALLTHROUGH */
1244 case 4: Flags = CF_LONG | CF_UNSIGNED | CF_CONST; break;
1245 default: Internal ("Invalid struct size: %u", Size); break;
1248 /* Generate a shift to get the field in the proper position in the
1249 ** primary. For bit fields, mask the value.
1251 BitOffs = Field->V.Offs * CHAR_BITS;
1252 if (SymIsBitField (Field)) {
1253 BitOffs += Field->V.B.BitOffs;
1254 g_asr (Flags, BitOffs);
1255 /* Mask the value. This is unnecessary if the shift executed above
1256 ** moved only zeroes into the value.
1258 if (BitOffs + Field->V.B.BitWidth != Size * CHAR_BITS) {
1259 g_and (CF_INT | CF_UNSIGNED | CF_CONST,
1260 (0x0001U << Field->V.B.BitWidth) - 1U);
1263 g_asr (Flags, BitOffs);
1266 /* Use the new type */
1267 Expr->Type = FinalType;
1271 /* Set the struct field offset */
1272 Expr->IVal += Field->V.Offs;
1274 /* Use the new type */
1275 Expr->Type = FinalType;
1277 /* An struct member is actually a variable. So the rules for variables
1278 ** with respect to the reference type apply: If it's an array, it is
1279 ** a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1280 ** but a struct field cannot be a function).
1282 if (IsTypeArray (Expr->Type)) {
1288 /* Make the expression a bit field if necessary */
1289 if (SymIsBitField (Field)) {
1290 ED_MakeBitField (Expr, Field->V.B.BitOffs, Field->V.B.BitWidth);
1298 static void hie11 (ExprDesc *Expr)
1299 /* Handle compound types (structs and arrays) */
1301 /* Name value used in invalid function calls */
1302 static const char IllegalFunc[] = "illegal_function_call";
1304 /* Evaluate the lhs */
1307 /* Check for a rhs */
1308 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN ||
1309 CurTok.Tok == TOK_DOT || CurTok.Tok == TOK_PTR_REF) {
1311 switch (CurTok.Tok) {
1314 /* Array reference */
1319 /* Function call. */
1320 if (!IsTypeFunc (Expr->Type) && !IsTypeFuncPtr (Expr->Type)) {
1321 /* Not a function */
1322 Error ("Illegal function call");
1323 /* Force the type to be a implicitly defined function, one
1324 ** returning an int and taking any number of arguments.
1325 ** Since we don't have a name, invent one.
1327 ED_MakeConstAbs (Expr, 0, GetImplicitFuncType ());
1328 Expr->Name = (uintptr_t) IllegalFunc;
1330 /* Call the function */
1331 FunctionCall (Expr);
1335 if (!IsClassStruct (Expr->Type)) {
1336 Error ("Struct expected");
1342 /* If we have an array, convert it to pointer to first element */
1343 if (IsTypeArray (Expr->Type)) {
1344 Expr->Type = ArrayToPtr (Expr->Type);
1346 if (!IsClassPtr (Expr->Type) || !IsClassStruct (Indirect (Expr->Type))) {
1347 Error ("Struct pointer expected");
1353 Internal ("Invalid token in hie11: %d", CurTok.Tok);
1361 void Store (ExprDesc* Expr, const Type* StoreType)
1362 /* Store the primary register into the location denoted by Expr. If StoreType
1363 ** is given, use this type when storing instead of Expr->Type. If StoreType
1364 ** is NULL, use Expr->Type instead.
1369 /* If StoreType was not given, use Expr->Type instead */
1370 if (StoreType == 0) {
1371 StoreType = Expr->Type;
1374 /* Prepare the code generator flags */
1375 Flags = TypeOf (StoreType) | GlobalModeFlags (Expr);
1377 /* Do the store depending on the location */
1378 switch (ED_GetLoc (Expr)) {
1381 /* Absolute: numeric address or const */
1382 g_putstatic (Flags, Expr->IVal, 0);
1386 /* Global variable */
1387 g_putstatic (Flags, Expr->Name, Expr->IVal);
1392 /* Static variable or literal in the literal pool */
1393 g_putstatic (Flags, Expr->Name, Expr->IVal);
1396 case E_LOC_REGISTER:
1397 /* Register variable */
1398 g_putstatic (Flags, Expr->Name, Expr->IVal);
1402 /* Value on the stack */
1403 g_putlocal (Flags, Expr->IVal, 0);
1407 /* The primary register (value is already there) */
1411 /* An expression in the primary register */
1412 g_putind (Flags, Expr->IVal);
1416 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
1419 /* Assume that each one of the stores will invalidate CC */
1420 ED_MarkAsUntested (Expr);
1425 static void PreInc (ExprDesc* Expr)
1426 /* Handle the preincrement operators */
1431 /* Skip the operator token */
1434 /* Evaluate the expression and check that it is an lvalue */
1436 if (!ED_IsLVal (Expr)) {
1437 Error ("Invalid lvalue");
1441 /* We cannot modify const values */
1442 if (IsQualConst (Expr->Type)) {
1443 Error ("Increment of read-only variable");
1446 /* Get the data type */
1447 Flags = TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR | CF_CONST;
1449 /* Get the increment value in bytes */
1450 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1452 /* Check the location of the data */
1453 switch (ED_GetLoc (Expr)) {
1456 /* Absolute: numeric address or const */
1457 g_addeqstatic (Flags, Expr->IVal, 0, Val);
1461 /* Global variable */
1462 g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1467 /* Static variable or literal in the literal pool */
1468 g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1471 case E_LOC_REGISTER:
1472 /* Register variable */
1473 g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1477 /* Value on the stack */
1478 g_addeqlocal (Flags, Expr->IVal, Val);
1482 /* The primary register */
1487 /* An expression in the primary register */
1488 g_addeqind (Flags, Expr->IVal, Val);
1492 Internal ("Invalid location in PreInc(): 0x%04X", ED_GetLoc (Expr));
1495 /* Result is an expression, no reference */
1496 ED_MakeRValExpr (Expr);
1501 static void PreDec (ExprDesc* Expr)
1502 /* Handle the predecrement operators */
1507 /* Skip the operator token */
1510 /* Evaluate the expression and check that it is an lvalue */
1512 if (!ED_IsLVal (Expr)) {
1513 Error ("Invalid lvalue");
1517 /* We cannot modify const values */
1518 if (IsQualConst (Expr->Type)) {
1519 Error ("Decrement of read-only variable");
1522 /* Get the data type */
1523 Flags = TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR | CF_CONST;
1525 /* Get the increment value in bytes */
1526 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1528 /* Check the location of the data */
1529 switch (ED_GetLoc (Expr)) {
1532 /* Absolute: numeric address or const */
1533 g_subeqstatic (Flags, Expr->IVal, 0, Val);
1537 /* Global variable */
1538 g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1543 /* Static variable or literal in the literal pool */
1544 g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1547 case E_LOC_REGISTER:
1548 /* Register variable */
1549 g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1553 /* Value on the stack */
1554 g_subeqlocal (Flags, Expr->IVal, Val);
1558 /* The primary register */
1563 /* An expression in the primary register */
1564 g_subeqind (Flags, Expr->IVal, Val);
1568 Internal ("Invalid location in PreDec(): 0x%04X", ED_GetLoc (Expr));
1571 /* Result is an expression, no reference */
1572 ED_MakeRValExpr (Expr);
1577 static void PostInc (ExprDesc* Expr)
1578 /* Handle the postincrement operator */
1584 /* The expression to increment must be an lvalue */
1585 if (!ED_IsLVal (Expr)) {
1586 Error ("Invalid lvalue");
1590 /* We cannot modify const values */
1591 if (IsQualConst (Expr->Type)) {
1592 Error ("Increment of read-only variable");
1595 /* Get the data type */
1596 Flags = TypeOf (Expr->Type);
1598 /* Emit smaller code if a char variable is at a constant location */
1599 if ((Flags & CF_CHAR) == CF_CHAR && ED_IsLocConst(Expr)) {
1601 LoadExpr (CF_NONE, Expr);
1602 AddCodeLine ("inc %s", ED_GetLabelName(Expr, 0));
1606 /* Push the address if needed */
1609 /* Fetch the value and save it (since it's the result of the expression) */
1610 LoadExpr (CF_NONE, Expr);
1611 g_save (Flags | CF_FORCECHAR);
1613 /* If we have a pointer expression, increment by the size of the type */
1614 if (IsTypePtr (Expr->Type)) {
1615 g_inc (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1617 g_inc (Flags | CF_CONST | CF_FORCECHAR, 1);
1620 /* Store the result back */
1623 /* Restore the original value in the primary register */
1624 g_restore (Flags | CF_FORCECHAR);
1627 /* The result is always an expression, no reference */
1628 ED_MakeRValExpr (Expr);
1633 static void PostDec (ExprDesc* Expr)
1634 /* Handle the postdecrement operator */
1640 /* The expression to increment must be an lvalue */
1641 if (!ED_IsLVal (Expr)) {
1642 Error ("Invalid lvalue");
1646 /* We cannot modify const values */
1647 if (IsQualConst (Expr->Type)) {
1648 Error ("Decrement of read-only variable");
1651 /* Get the data type */
1652 Flags = TypeOf (Expr->Type);
1654 /* Emit smaller code if a char variable is at a constant location */
1655 if ((Flags & CF_CHAR) == CF_CHAR && ED_IsLocConst(Expr)) {
1657 LoadExpr (CF_NONE, Expr);
1658 AddCodeLine ("dec %s", ED_GetLabelName(Expr, 0));
1662 /* Push the address if needed */
1665 /* Fetch the value and save it (since it's the result of the expression) */
1666 LoadExpr (CF_NONE, Expr);
1667 g_save (Flags | CF_FORCECHAR);
1669 /* If we have a pointer expression, increment by the size of the type */
1670 if (IsTypePtr (Expr->Type)) {
1671 g_dec (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1673 g_dec (Flags | CF_CONST | CF_FORCECHAR, 1);
1676 /* Store the result back */
1679 /* Restore the original value in the primary register */
1680 g_restore (Flags | CF_FORCECHAR);
1683 /* The result is always an expression, no reference */
1684 ED_MakeRValExpr (Expr);
1689 static void UnaryOp (ExprDesc* Expr)
1690 /* Handle unary -/+ and ~ */
1694 /* Remember the operator token and skip it */
1695 token_t Tok = CurTok.Tok;
1698 /* Get the expression */
1701 /* We can only handle integer types */
1702 if (!IsClassInt (Expr->Type)) {
1703 Error ("Argument must have integer type");
1704 ED_MakeConstAbsInt (Expr, 1);
1707 /* Check for a constant expression */
1708 if (ED_IsConstAbs (Expr)) {
1709 /* Value is constant */
1711 case TOK_MINUS: Expr->IVal = -Expr->IVal; break;
1712 case TOK_PLUS: break;
1713 case TOK_COMP: Expr->IVal = ~Expr->IVal; break;
1714 default: Internal ("Unexpected token: %d", Tok);
1717 /* Value is not constant */
1718 LoadExpr (CF_NONE, Expr);
1720 /* Get the type of the expression */
1721 Flags = TypeOf (Expr->Type);
1723 /* Handle the operation */
1725 case TOK_MINUS: g_neg (Flags); break;
1726 case TOK_PLUS: break;
1727 case TOK_COMP: g_com (Flags); break;
1728 default: Internal ("Unexpected token: %d", Tok);
1731 /* The result is a rvalue in the primary */
1732 ED_MakeRValExpr (Expr);
1738 void hie10 (ExprDesc* Expr)
1739 /* Handle ++, --, !, unary - etc. */
1743 switch (CurTok.Tok) {
1761 if (evalexpr (CF_NONE, hie10, Expr) == 0) {
1762 /* Constant expression */
1763 Expr->IVal = !Expr->IVal;
1765 g_bneg (TypeOf (Expr->Type));
1766 ED_MakeRValExpr (Expr);
1767 ED_TestDone (Expr); /* bneg will set cc */
1773 ExprWithCheck (hie10, Expr);
1774 if (ED_IsLVal (Expr) || !(ED_IsLocConst (Expr) || ED_IsLocStack (Expr))) {
1775 /* Not a const, load it into the primary and make it a
1776 ** calculated value.
1778 LoadExpr (CF_NONE, Expr);
1779 ED_MakeRValExpr (Expr);
1781 /* If the expression is already a pointer to function, the
1782 ** additional dereferencing operator must be ignored. A function
1783 ** itself is represented as "pointer to function", so any number
1784 ** of dereference operators is legal, since the result will
1785 ** always be converted to "pointer to function".
1787 if (IsTypeFuncPtr (Expr->Type) || IsTypeFunc (Expr->Type)) {
1788 /* Expression not storable */
1791 if (IsClassPtr (Expr->Type)) {
1792 Expr->Type = Indirect (Expr->Type);
1794 Error ("Illegal indirection");
1796 /* If the expression points to an array, then don't convert the
1797 ** address -- it already is the location of the first element.
1799 if (!IsTypeArray (Expr->Type)) {
1800 /* The * operator yields an lvalue */
1808 ExprWithCheck (hie10, Expr);
1809 /* The & operator may be applied to any lvalue, and it may be
1810 ** applied to functions, even if they're no lvalues.
1812 if (ED_IsRVal (Expr) && !IsTypeFunc (Expr->Type) && !IsTypeArray (Expr->Type)) {
1813 Error ("Illegal address");
1815 if (ED_IsBitField (Expr)) {
1816 Error ("Cannot take address of bit-field");
1817 /* Do it anyway, just to avoid further warnings */
1818 Expr->Flags &= ~E_BITFIELD;
1820 Expr->Type = PointerTo (Expr->Type);
1821 /* The & operator yields an rvalue */
1828 if (TypeSpecAhead ()) {
1831 Size = CheckedSizeOf (ParseType (T));
1834 /* Remember the output queue pointer */
1838 /* If the expression is a literal string, release it, so it
1839 ** won't be output as data if not used elsewhere.
1841 if (ED_IsLocLiteral (Expr)) {
1842 ReleaseLiteral (Expr->LVal);
1844 /* Calculate the size */
1845 Size = CheckedSizeOf (Expr->Type);
1846 /* Remove any generated code */
1849 ED_MakeConstAbs (Expr, Size, type_size_t);
1850 ED_MarkAsUntested (Expr);
1854 if (TypeSpecAhead ()) {
1864 /* Handle post increment */
1865 switch (CurTok.Tok) {
1866 case TOK_INC: PostInc (Expr); break;
1867 case TOK_DEC: PostDec (Expr); break;
1878 static void hie_internal (const GenDesc* Ops, /* List of generators */
1880 void (*hienext) (ExprDesc*),
1882 /* Helper function */
1888 token_t Tok; /* The operator token */
1889 unsigned ltype, type;
1890 int lconst; /* Left operand is a constant */
1891 int rconst; /* Right operand is a constant */
1894 ExprWithCheck (hienext, Expr);
1897 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1899 /* Tell the caller that we handled it's ops */
1902 /* All operators that call this function expect an int on the lhs */
1903 if (!IsClassInt (Expr->Type)) {
1904 Error ("Integer expression expected");
1905 /* To avoid further errors, make Expr a valid int expression */
1906 ED_MakeConstAbsInt (Expr, 1);
1909 /* Remember the operator token, then skip it */
1913 /* Get the lhs on stack */
1914 GetCodePos (&Mark1);
1915 ltype = TypeOf (Expr->Type);
1916 lconst = ED_IsConstAbs (Expr);
1918 /* Constant value */
1919 GetCodePos (&Mark2);
1920 /* If the operator is commutative, don't push the left side, if
1921 ** it's a constant, since we will exchange both operands.
1923 if ((Gen->Flags & GEN_COMM) == 0) {
1924 g_push (ltype | CF_CONST, Expr->IVal);
1927 /* Value not constant */
1928 LoadExpr (CF_NONE, Expr);
1929 GetCodePos (&Mark2);
1933 /* Get the right hand side */
1934 MarkedExprWithCheck (hienext, &Expr2);
1936 /* Check for a constant expression */
1937 rconst = (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2));
1939 /* Not constant, load into the primary */
1940 LoadExpr (CF_NONE, &Expr2);
1943 /* Check the type of the rhs */
1944 if (!IsClassInt (Expr2.Type)) {
1945 Error ("Integer expression expected");
1948 /* Check for const operands */
1949 if (lconst && rconst) {
1951 /* Both operands are constant, remove the generated code */
1952 RemoveCode (&Mark1);
1954 /* Get the type of the result */
1955 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1957 /* Handle the op differently for signed and unsigned types */
1958 if (IsSignSigned (Expr->Type)) {
1960 /* Evaluate the result for signed operands */
1961 signed long Val1 = Expr->IVal;
1962 signed long Val2 = Expr2.IVal;
1965 Expr->IVal = (Val1 | Val2);
1968 Expr->IVal = (Val1 ^ Val2);
1971 Expr->IVal = (Val1 & Val2);
1974 Expr->IVal = (Val1 * Val2);
1978 Error ("Division by zero");
1979 Expr->IVal = 0x7FFFFFFF;
1981 Expr->IVal = (Val1 / Val2);
1986 Error ("Modulo operation with zero");
1989 Expr->IVal = (Val1 % Val2);
1993 Internal ("hie_internal: got token 0x%X\n", Tok);
1997 /* Evaluate the result for unsigned operands */
1998 unsigned long Val1 = Expr->IVal;
1999 unsigned long Val2 = Expr2.IVal;
2002 Expr->IVal = (Val1 | Val2);
2005 Expr->IVal = (Val1 ^ Val2);
2008 Expr->IVal = (Val1 & Val2);
2011 Expr->IVal = (Val1 * Val2);
2015 Error ("Division by zero");
2016 Expr->IVal = 0xFFFFFFFF;
2018 Expr->IVal = (Val1 / Val2);
2023 Error ("Modulo operation with zero");
2026 Expr->IVal = (Val1 % Val2);
2030 Internal ("hie_internal: got token 0x%X\n", Tok);
2034 } else if (lconst && (Gen->Flags & GEN_COMM) && !rconst) {
2036 /* The left side is constant, the right side is not, and the
2037 ** operator allows swapping the operands. We haven't pushed the
2038 ** left side onto the stack in this case, and will reverse the
2039 ** operation because this allows for better code.
2041 unsigned rtype = ltype | CF_CONST;
2042 ltype = TypeOf (Expr2.Type); /* Expr2 is now left */
2044 if ((Gen->Flags & GEN_NOPUSH) == 0) {
2047 ltype |= CF_REG; /* Value is in register */
2050 /* Determine the type of the operation result. */
2051 type |= g_typeadjust (ltype, rtype);
2052 Expr->Type = promoteint (Expr->Type, Expr2.Type);
2055 Gen->Func (type, Expr->IVal);
2057 /* We have a rvalue in the primary now */
2058 ED_MakeRValExpr (Expr);
2062 /* If the right hand side is constant, and the generator function
2063 ** expects the lhs in the primary, remove the push of the primary
2066 unsigned rtype = TypeOf (Expr2.Type);
2069 /* Second value is constant - check for div */
2072 if (Tok == TOK_DIV && Expr2.IVal == 0) {
2073 Error ("Division by zero");
2074 } else if (Tok == TOK_MOD && Expr2.IVal == 0) {
2075 Error ("Modulo operation with zero");
2077 if ((Gen->Flags & GEN_NOPUSH) != 0) {
2078 RemoveCode (&Mark2);
2079 ltype |= CF_REG; /* Value is in register */
2083 /* Determine the type of the operation result. */
2084 type |= g_typeadjust (ltype, rtype);
2085 Expr->Type = promoteint (Expr->Type, Expr2.Type);
2088 Gen->Func (type, Expr2.IVal);
2090 /* We have a rvalue in the primary now */
2091 ED_MakeRValExpr (Expr);
2098 static void hie_compare (const GenDesc* Ops, /* List of generators */
2100 void (*hienext) (ExprDesc*))
2101 /* Helper function for the compare operators */
2108 token_t Tok; /* The operator token */
2110 int rconst; /* Operand is a constant */
2113 GetCodePos (&Mark0);
2114 ExprWithCheck (hienext, Expr);
2116 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
2118 /* Remember the generator function */
2119 void (*GenFunc) (unsigned, unsigned long) = Gen->Func;
2121 /* Remember the operator token, then skip it */
2125 /* If lhs is a function, convert it to pointer to function */
2126 if (IsTypeFunc (Expr->Type)) {
2127 Expr->Type = PointerTo (Expr->Type);
2130 /* Get the lhs on stack */
2131 GetCodePos (&Mark1);
2132 ltype = TypeOf (Expr->Type);
2133 if (ED_IsConstAbs (Expr)) {
2134 /* Constant value */
2135 GetCodePos (&Mark2);
2136 g_push (ltype | CF_CONST, Expr->IVal);
2138 /* Value not constant */
2139 LoadExpr (CF_NONE, Expr);
2140 GetCodePos (&Mark2);
2144 /* Get the right hand side */
2145 MarkedExprWithCheck (hienext, &Expr2);
2147 /* If rhs is a function, convert it to pointer to function */
2148 if (IsTypeFunc (Expr2.Type)) {
2149 Expr2.Type = PointerTo (Expr2.Type);
2152 /* Check for a constant expression */
2153 rconst = (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2));
2155 /* Not constant, load into the primary */
2156 LoadExpr (CF_NONE, &Expr2);
2159 /* Some operations aren't allowed on function pointers */
2160 if ((Gen->Flags & GEN_NOFUNC) != 0) {
2161 /* Output only one message even if both sides are wrong */
2162 if (IsTypeFuncPtr (Expr->Type)) {
2163 Error ("Invalid left operand for relational operator");
2164 /* Avoid further errors */
2165 ED_MakeConstAbsInt (Expr, 0);
2166 ED_MakeConstAbsInt (&Expr2, 0);
2167 } else if (IsTypeFuncPtr (Expr2.Type)) {
2168 Error ("Invalid right operand for relational operator");
2169 /* Avoid further errors */
2170 ED_MakeConstAbsInt (Expr, 0);
2171 ED_MakeConstAbsInt (&Expr2, 0);
2175 /* Make sure, the types are compatible */
2176 if (IsClassInt (Expr->Type)) {
2177 if (!IsClassInt (Expr2.Type) && !(IsClassPtr(Expr2.Type) && ED_IsNullPtr(Expr))) {
2178 Error ("Incompatible types");
2180 } else if (IsClassPtr (Expr->Type)) {
2181 if (IsClassPtr (Expr2.Type)) {
2182 /* Both pointers are allowed in comparison if they point to
2183 ** the same type, or if one of them is a void pointer.
2185 Type* left = Indirect (Expr->Type);
2186 Type* right = Indirect (Expr2.Type);
2187 if (TypeCmp (left, right) < TC_QUAL_DIFF && left->C != T_VOID && right->C != T_VOID) {
2188 /* Incompatible pointers */
2189 Error ("Incompatible types");
2191 } else if (!ED_IsNullPtr (&Expr2)) {
2192 Error ("Incompatible types");
2196 /* Check for const operands */
2197 if (ED_IsConstAbs (Expr) && rconst) {
2199 /* If the result is constant, this is suspicious when not in
2200 ** preprocessor mode.
2202 WarnConstCompareResult ();
2204 /* Both operands are constant, remove the generated code */
2205 RemoveCode (&Mark1);
2207 /* Determine if this is a signed or unsigned compare */
2208 if (IsClassInt (Expr->Type) && IsSignSigned (Expr->Type) &&
2209 IsClassInt (Expr2.Type) && IsSignSigned (Expr2.Type)) {
2211 /* Evaluate the result for signed operands */
2212 signed long Val1 = Expr->IVal;
2213 signed long Val2 = Expr2.IVal;
2215 case TOK_EQ: Expr->IVal = (Val1 == Val2); break;
2216 case TOK_NE: Expr->IVal = (Val1 != Val2); break;
2217 case TOK_LT: Expr->IVal = (Val1 < Val2); break;
2218 case TOK_LE: Expr->IVal = (Val1 <= Val2); break;
2219 case TOK_GE: Expr->IVal = (Val1 >= Val2); break;
2220 case TOK_GT: Expr->IVal = (Val1 > Val2); break;
2221 default: Internal ("hie_compare: got token 0x%X\n", Tok);
2226 /* Evaluate the result for unsigned operands */
2227 unsigned long Val1 = Expr->IVal;
2228 unsigned long Val2 = Expr2.IVal;
2230 case TOK_EQ: Expr->IVal = (Val1 == Val2); break;
2231 case TOK_NE: Expr->IVal = (Val1 != Val2); break;
2232 case TOK_LT: Expr->IVal = (Val1 < Val2); break;
2233 case TOK_LE: Expr->IVal = (Val1 <= Val2); break;
2234 case TOK_GE: Expr->IVal = (Val1 >= Val2); break;
2235 case TOK_GT: Expr->IVal = (Val1 > Val2); break;
2236 default: Internal ("hie_compare: got token 0x%X\n", Tok);
2242 /* Determine the signedness of the operands */
2243 int LeftSigned = IsSignSigned (Expr->Type);
2244 int RightSigned = IsSignSigned (Expr2.Type);
2246 /* If the right hand side is constant, and the generator function
2247 ** expects the lhs in the primary, remove the push of the primary
2253 if ((Gen->Flags & GEN_NOPUSH) != 0) {
2254 RemoveCode (&Mark2);
2255 ltype |= CF_REG; /* Value is in register */
2259 /* Determine the type of the operation. */
2260 if (IsTypeChar (Expr->Type) && rconst) {
2262 /* Left side is unsigned char, right side is constant.
2263 ** Determine the minimum and maximum values
2265 int LeftMin, LeftMax;
2273 /* An integer value is always represented as a signed in the
2274 ** ExprDesc structure. This may lead to false results below,
2275 ** if it is actually unsigned, but interpreted as signed
2276 ** because of the representation. Fortunately, in this case,
2277 ** the actual value doesn't matter, since it's always greater
2278 ** than what can be represented in a char. So correct the
2279 ** value accordingly.
2281 if (!RightSigned && Expr2.IVal < 0) {
2282 /* Correct the value so it is an unsigned. It will then
2283 ** anyway match one of the cases below.
2285 Expr2.IVal = LeftMax + 1;
2288 /* Comparing a char against a constant may have a constant
2289 ** result. Please note: It is not possible to remove the code
2290 ** for the compare alltogether, because it may have side
2296 if (Expr2.IVal < LeftMin || Expr2.IVal > LeftMax) {
2297 ED_MakeConstAbsInt (Expr, 0);
2298 WarnConstCompareResult ();
2304 if (Expr2.IVal < LeftMin || Expr2.IVal > LeftMax) {
2305 ED_MakeConstAbsInt (Expr, 1);
2306 WarnConstCompareResult ();
2312 if (Expr2.IVal <= LeftMin || Expr2.IVal > LeftMax) {
2313 ED_MakeConstAbsInt (Expr, Expr2.IVal > LeftMax);
2314 WarnConstCompareResult ();
2320 if (Expr2.IVal < LeftMin || Expr2.IVal >= LeftMax) {
2321 ED_MakeConstAbsInt (Expr, Expr2.IVal >= LeftMax);
2322 WarnConstCompareResult ();
2328 if (Expr2.IVal <= LeftMin || Expr2.IVal > LeftMax) {
2329 ED_MakeConstAbsInt (Expr, Expr2.IVal <= LeftMin);
2330 WarnConstCompareResult ();
2336 if (Expr2.IVal < LeftMin || Expr2.IVal >= LeftMax) {
2337 ED_MakeConstAbsInt (Expr, Expr2.IVal < LeftMin);
2338 WarnConstCompareResult ();
2344 Internal ("hie_compare: got token 0x%X\n", Tok);
2347 /* If the result is not already constant (as evaluated in the
2348 ** switch above), we can execute the operation as a char op,
2349 ** since the right side constant is in a valid range.
2351 flags |= (CF_CHAR | CF_FORCECHAR);
2353 flags |= CF_UNSIGNED;
2356 } else if (IsTypeChar (Expr->Type) && IsTypeChar (Expr2.Type) &&
2357 GetSignedness (Expr->Type) == GetSignedness (Expr2.Type)) {
2359 /* Both are chars with the same signedness. We can encode the
2360 ** operation as a char operation.
2364 flags |= CF_FORCECHAR;
2367 flags |= CF_UNSIGNED;
2370 unsigned rtype = TypeOf (Expr2.Type) | (flags & CF_CONST);
2371 flags |= g_typeadjust (ltype, rtype);
2374 /* If the left side is an unsigned and the right is a constant,
2375 ** we may be able to change the compares to something more
2378 if (!LeftSigned && rconst) {
2383 if (Expr2.IVal == 1) {
2384 /* An unsigned compare to one means that the value
2393 if (Expr2.IVal == 0) {
2394 /* An unsigned compare to zero means that the value
2402 if (Expr2.IVal == 1) {
2403 /* An unsigned compare to one means that the value
2404 ** must not be zero.
2412 if (Expr2.IVal == 0) {
2413 /* An unsigned compare to zero means that the value
2414 ** must not be zero.
2428 GenFunc (flags, Expr2.IVal);
2430 /* The result is an rvalue in the primary */
2431 ED_MakeRValExpr (Expr);
2434 /* Result type is always int */
2435 Expr->Type = type_int;
2437 Done: /* Condition codes are set */
2444 static void hie9 (ExprDesc *Expr)
2445 /* Process * and / operators. */
2447 static const GenDesc hie9_ops[] = {
2448 { TOK_STAR, GEN_NOPUSH | GEN_COMM, g_mul },
2449 { TOK_DIV, GEN_NOPUSH, g_div },
2450 { TOK_MOD, GEN_NOPUSH, g_mod },
2451 { TOK_INVALID, 0, 0 }
2455 hie_internal (hie9_ops, Expr, hie10, &UsedGen);
2460 static void parseadd (ExprDesc* Expr)
2461 /* Parse an expression with the binary plus operator. Expr contains the
2462 ** unprocessed left hand side of the expression and will contain the
2463 ** result of the expression on return.
2467 unsigned flags; /* Operation flags */
2468 CodeMark Mark; /* Remember code position */
2469 Type* lhst; /* Type of left hand side */
2470 Type* rhst; /* Type of right hand side */
2472 /* Skip the PLUS token */
2475 /* Get the left hand side type, initialize operation flags */
2479 /* Check for constness on both sides */
2480 if (ED_IsConst (Expr)) {
2482 /* The left hand side is a constant of some sort. Good. Get rhs */
2483 ExprWithCheck (hie9, &Expr2);
2484 if (ED_IsConstAbs (&Expr2)) {
2486 /* Right hand side is a constant numeric value. Get the rhs type */
2489 /* Both expressions are constants. Check for pointer arithmetic */
2490 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2491 /* Left is pointer, right is int, must scale rhs */
2492 Expr->IVal += Expr2.IVal * CheckedPSizeOf (lhst);
2493 /* Result type is a pointer */
2494 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2495 /* Left is int, right is pointer, must scale lhs */
2496 Expr->IVal = Expr->IVal * CheckedPSizeOf (rhst) + Expr2.IVal;
2497 /* Result type is a pointer */
2498 Expr->Type = Expr2.Type;
2499 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2500 /* Integer addition */
2501 Expr->IVal += Expr2.IVal;
2502 typeadjust (Expr, &Expr2, 1);
2505 Error ("Invalid operands for binary operator '+'");
2510 /* lhs is a constant and rhs is not constant. Load rhs into
2513 LoadExpr (CF_NONE, &Expr2);
2515 /* Beware: The check above (for lhs) lets not only pass numeric
2516 ** constants, but also constant addresses (labels), maybe even
2517 ** with an offset. We have to check for that here.
2520 /* First, get the rhs type. */
2524 if (ED_IsLocAbs (Expr)) {
2525 /* A numerical constant */
2528 /* Constant address label */
2529 flags |= GlobalModeFlags (Expr) | CF_CONSTADDR;
2532 /* Check for pointer arithmetic */
2533 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2534 /* Left is pointer, right is int, must scale rhs */
2535 g_scale (CF_INT, CheckedPSizeOf (lhst));
2536 /* Operate on pointers, result type is a pointer */
2538 /* Generate the code for the add */
2539 if (ED_GetLoc (Expr) == E_LOC_ABS) {
2540 /* Numeric constant */
2541 g_inc (flags, Expr->IVal);
2543 /* Constant address */
2544 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2546 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2548 /* Left is int, right is pointer, must scale lhs. */
2549 unsigned ScaleFactor = CheckedPSizeOf (rhst);
2551 /* Operate on pointers, result type is a pointer */
2553 Expr->Type = Expr2.Type;
2555 /* Since we do already have rhs in the primary, if lhs is
2556 ** not a numeric constant, and the scale factor is not one
2557 ** (no scaling), we must take the long way over the stack.
2559 if (ED_IsLocAbs (Expr)) {
2560 /* Numeric constant, scale lhs */
2561 Expr->IVal *= ScaleFactor;
2562 /* Generate the code for the add */
2563 g_inc (flags, Expr->IVal);
2564 } else if (ScaleFactor == 1) {
2565 /* Constant address but no need to scale */
2566 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2568 /* Constant address that must be scaled */
2569 g_push (TypeOf (Expr2.Type), 0); /* rhs --> stack */
2570 g_getimmed (flags, Expr->Name, Expr->IVal);
2571 g_scale (CF_PTR, ScaleFactor);
2574 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2575 /* Integer addition */
2576 flags |= typeadjust (Expr, &Expr2, 1);
2577 /* Generate the code for the add */
2578 if (ED_IsLocAbs (Expr)) {
2579 /* Numeric constant */
2580 g_inc (flags, Expr->IVal);
2582 /* Constant address */
2583 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2587 Error ("Invalid operands for binary operator '+'");
2591 /* Result is a rvalue in primary register */
2592 ED_MakeRValExpr (Expr);
2597 /* Left hand side is not constant. Get the value onto the stack. */
2598 LoadExpr (CF_NONE, Expr); /* --> primary register */
2600 g_push (TypeOf (Expr->Type), 0); /* --> stack */
2602 /* Evaluate the rhs */
2603 MarkedExprWithCheck (hie9, &Expr2);
2605 /* Check for a constant rhs expression */
2606 if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
2608 /* Right hand side is a constant. Get the rhs type */
2611 /* Remove pushed value from stack */
2614 /* Check for pointer arithmetic */
2615 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2616 /* Left is pointer, right is int, must scale rhs */
2617 Expr2.IVal *= CheckedPSizeOf (lhst);
2618 /* Operate on pointers, result type is a pointer */
2620 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2621 /* Left is int, right is pointer, must scale lhs (ptr only) */
2622 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2623 /* Operate on pointers, result type is a pointer */
2625 Expr->Type = Expr2.Type;
2626 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2627 /* Integer addition */
2628 flags = typeadjust (Expr, &Expr2, 1);
2631 Error ("Invalid operands for binary operator '+'");
2635 /* Generate code for the add */
2636 g_inc (flags | CF_CONST, Expr2.IVal);
2640 /* Not constant, load into the primary */
2641 LoadExpr (CF_NONE, &Expr2);
2643 /* lhs and rhs are not constant. Get the rhs type. */
2646 /* Check for pointer arithmetic */
2647 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2648 /* Left is pointer, right is int, must scale rhs */
2649 g_scale (CF_INT, CheckedPSizeOf (lhst));
2650 /* Operate on pointers, result type is a pointer */
2652 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2653 /* Left is int, right is pointer, must scale lhs */
2654 g_tosint (TypeOf (lhst)); /* Make sure TOS is int */
2655 g_swap (CF_INT); /* Swap TOS and primary */
2656 g_scale (CF_INT, CheckedPSizeOf (rhst));
2657 /* Operate on pointers, result type is a pointer */
2659 Expr->Type = Expr2.Type;
2660 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2661 /* Integer addition. Note: Result is never constant.
2662 ** Problem here is that typeadjust does not know if the
2663 ** variable is an rvalue or lvalue, so if both operands
2664 ** are dereferenced constant numeric addresses, typeadjust
2665 ** thinks the operation works on constants. Removing
2666 ** CF_CONST here means handling the symptoms, however, the
2667 ** whole parser is such a mess that I fear to break anything
2668 ** when trying to apply another solution.
2670 flags = typeadjust (Expr, &Expr2, 0) & ~CF_CONST;
2673 Error ("Invalid operands for binary operator '+'");
2677 /* Generate code for the add */
2682 /* Result is a rvalue in primary register */
2683 ED_MakeRValExpr (Expr);
2686 /* Condition codes not set */
2687 ED_MarkAsUntested (Expr);
2692 static void parsesub (ExprDesc* Expr)
2693 /* Parse an expression with the binary minus operator. Expr contains the
2694 ** unprocessed left hand side of the expression and will contain the
2695 ** result of the expression on return.
2699 unsigned flags; /* Operation flags */
2700 Type* lhst; /* Type of left hand side */
2701 Type* rhst; /* Type of right hand side */
2702 CodeMark Mark1; /* Save position of output queue */
2703 CodeMark Mark2; /* Another position in the queue */
2704 int rscale; /* Scale factor for the result */
2707 /* lhs cannot be function or pointer to function */
2708 if (IsTypeFunc (Expr->Type) || IsTypeFuncPtr (Expr->Type)) {
2709 Error ("Invalid left operand for binary operator '-'");
2710 /* Make it pointer to char to avoid further errors */
2711 Expr->Type = type_uchar;
2714 /* Skip the MINUS token */
2717 /* Get the left hand side type, initialize operation flags */
2719 rscale = 1; /* Scale by 1, that is, don't scale */
2721 /* Remember the output queue position, then bring the value onto the stack */
2722 GetCodePos (&Mark1);
2723 LoadExpr (CF_NONE, Expr); /* --> primary register */
2724 GetCodePos (&Mark2);
2725 g_push (TypeOf (lhst), 0); /* --> stack */
2727 /* Parse the right hand side */
2728 MarkedExprWithCheck (hie9, &Expr2);
2730 /* rhs cannot be function or pointer to function */
2731 if (IsTypeFunc (Expr2.Type) || IsTypeFuncPtr (Expr2.Type)) {
2732 Error ("Invalid right operand for binary operator '-'");
2733 /* Make it pointer to char to avoid further errors */
2734 Expr2.Type = type_uchar;
2737 /* Check for a constant rhs expression */
2738 if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
2740 /* The right hand side is constant. Get the rhs type. */
2743 /* Check left hand side */
2744 if (ED_IsConstAbs (Expr)) {
2746 /* Both sides are constant, remove generated code */
2747 RemoveCode (&Mark1);
2749 /* Check for pointer arithmetic */
2750 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2751 /* Left is pointer, right is int, must scale rhs */
2752 Expr->IVal -= Expr2.IVal * CheckedPSizeOf (lhst);
2753 /* Operate on pointers, result type is a pointer */
2754 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2755 /* Left is pointer, right is pointer, must scale result */
2756 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2757 Error ("Incompatible pointer types");
2759 Expr->IVal = (Expr->IVal - Expr2.IVal) /
2760 CheckedPSizeOf (lhst);
2762 /* Operate on pointers, result type is an integer */
2763 Expr->Type = type_int;
2764 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2765 /* Integer subtraction */
2766 typeadjust (Expr, &Expr2, 1);
2767 Expr->IVal -= Expr2.IVal;
2770 Error ("Invalid operands for binary operator '-'");
2773 /* Result is constant, condition codes not set */
2774 ED_MarkAsUntested (Expr);
2778 /* Left hand side is not constant, right hand side is.
2779 ** Remove pushed value from stack.
2781 RemoveCode (&Mark2);
2783 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2784 /* Left is pointer, right is int, must scale rhs */
2785 Expr2.IVal *= CheckedPSizeOf (lhst);
2786 /* Operate on pointers, result type is a pointer */
2788 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2789 /* Left is pointer, right is pointer, must scale result */
2790 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2791 Error ("Incompatible pointer types");
2793 rscale = CheckedPSizeOf (lhst);
2795 /* Operate on pointers, result type is an integer */
2797 Expr->Type = type_int;
2798 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2799 /* Integer subtraction */
2800 flags = typeadjust (Expr, &Expr2, 1);
2803 Error ("Invalid operands for binary operator '-'");
2807 /* Do the subtraction */
2808 g_dec (flags | CF_CONST, Expr2.IVal);
2810 /* If this was a pointer subtraction, we must scale the result */
2812 g_scale (flags, -rscale);
2815 /* Result is a rvalue in the primary register */
2816 ED_MakeRValExpr (Expr);
2817 ED_MarkAsUntested (Expr);
2823 /* Not constant, load into the primary */
2824 LoadExpr (CF_NONE, &Expr2);
2826 /* Right hand side is not constant. Get the rhs type. */
2829 /* Check for pointer arithmetic */
2830 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2831 /* Left is pointer, right is int, must scale rhs */
2832 g_scale (CF_INT, CheckedPSizeOf (lhst));
2833 /* Operate on pointers, result type is a pointer */
2835 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2836 /* Left is pointer, right is pointer, must scale result */
2837 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2838 Error ("Incompatible pointer types");
2840 rscale = CheckedPSizeOf (lhst);
2842 /* Operate on pointers, result type is an integer */
2844 Expr->Type = type_int;
2845 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2846 /* Integer subtraction. If the left hand side descriptor says that
2847 ** the lhs is const, we have to remove this mark, since this is no
2848 ** longer true, lhs is on stack instead.
2850 if (ED_IsLocAbs (Expr)) {
2851 ED_MakeRValExpr (Expr);
2853 /* Adjust operand types */
2854 flags = typeadjust (Expr, &Expr2, 0);
2857 Error ("Invalid operands for binary operator '-'");
2861 /* Generate code for the sub (the & is a hack here) */
2862 g_sub (flags & ~CF_CONST, 0);
2864 /* If this was a pointer subtraction, we must scale the result */
2866 g_scale (flags, -rscale);
2869 /* Result is a rvalue in the primary register */
2870 ED_MakeRValExpr (Expr);
2871 ED_MarkAsUntested (Expr);
2877 void hie8 (ExprDesc* Expr)
2878 /* Process + and - binary operators. */
2880 ExprWithCheck (hie9, Expr);
2881 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2882 if (CurTok.Tok == TOK_PLUS) {
2892 static void hie6 (ExprDesc* Expr)
2893 /* Handle greater-than type comparators */
2895 static const GenDesc hie6_ops [] = {
2896 { TOK_LT, GEN_NOPUSH | GEN_NOFUNC, g_lt },
2897 { TOK_LE, GEN_NOPUSH | GEN_NOFUNC, g_le },
2898 { TOK_GE, GEN_NOPUSH | GEN_NOFUNC, g_ge },
2899 { TOK_GT, GEN_NOPUSH | GEN_NOFUNC, g_gt },
2900 { TOK_INVALID, 0, 0 }
2902 hie_compare (hie6_ops, Expr, ShiftExpr);
2907 static void hie5 (ExprDesc* Expr)
2908 /* Handle == and != */
2910 static const GenDesc hie5_ops[] = {
2911 { TOK_EQ, GEN_NOPUSH, g_eq },
2912 { TOK_NE, GEN_NOPUSH, g_ne },
2913 { TOK_INVALID, 0, 0 }
2915 hie_compare (hie5_ops, Expr, hie6);
2920 static void hie4 (ExprDesc* Expr)
2921 /* Handle & (bitwise and) */
2923 static const GenDesc hie4_ops[] = {
2924 { TOK_AND, GEN_NOPUSH | GEN_COMM, g_and },
2925 { TOK_INVALID, 0, 0 }
2929 hie_internal (hie4_ops, Expr, hie5, &UsedGen);
2934 static void hie3 (ExprDesc* Expr)
2935 /* Handle ^ (bitwise exclusive or) */
2937 static const GenDesc hie3_ops[] = {
2938 { TOK_XOR, GEN_NOPUSH | GEN_COMM, g_xor },
2939 { TOK_INVALID, 0, 0 }
2943 hie_internal (hie3_ops, Expr, hie4, &UsedGen);
2948 static void hie2 (ExprDesc* Expr)
2949 /* Handle | (bitwise or) */
2951 static const GenDesc hie2_ops[] = {
2952 { TOK_OR, GEN_NOPUSH | GEN_COMM, g_or },
2953 { TOK_INVALID, 0, 0 }
2957 hie_internal (hie2_ops, Expr, hie3, &UsedGen);
2962 static void hieAndPP (ExprDesc* Expr)
2963 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2964 ** called recursively from the preprocessor.
2969 ConstAbsIntExpr (hie2, Expr);
2970 while (CurTok.Tok == TOK_BOOL_AND) {
2976 ConstAbsIntExpr (hie2, &Expr2);
2978 /* Combine the two */
2979 Expr->IVal = (Expr->IVal && Expr2.IVal);
2985 static void hieOrPP (ExprDesc *Expr)
2986 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2987 ** called recursively from the preprocessor.
2992 ConstAbsIntExpr (hieAndPP, Expr);
2993 while (CurTok.Tok == TOK_BOOL_OR) {
2999 ConstAbsIntExpr (hieAndPP, &Expr2);
3001 /* Combine the two */
3002 Expr->IVal = (Expr->IVal || Expr2.IVal);
3008 static void hieAnd (ExprDesc* Expr, unsigned TrueLab, int* BoolOp)
3009 /* Process "exp && exp" */
3014 ExprWithCheck (hie2, Expr);
3015 if (CurTok.Tok == TOK_BOOL_AND) {
3017 /* Tell our caller that we're evaluating a boolean */
3020 /* Get a label that we will use for false expressions */
3021 FalseLab = GetLocalLabel ();
3023 /* If the expr hasn't set condition codes, set the force-test flag */
3024 if (!ED_IsTested (Expr)) {
3025 ED_MarkForTest (Expr);
3028 /* Load the value */
3029 LoadExpr (CF_FORCECHAR, Expr);
3031 /* Generate the jump */
3032 g_falsejump (CF_NONE, FalseLab);
3034 /* Parse more boolean and's */
3035 while (CurTok.Tok == TOK_BOOL_AND) {
3042 if (!ED_IsTested (&Expr2)) {
3043 ED_MarkForTest (&Expr2);
3045 LoadExpr (CF_FORCECHAR, &Expr2);
3047 /* Do short circuit evaluation */
3048 if (CurTok.Tok == TOK_BOOL_AND) {
3049 g_falsejump (CF_NONE, FalseLab);
3051 /* Last expression - will evaluate to true */
3052 g_truejump (CF_NONE, TrueLab);
3056 /* Define the false jump label here */
3057 g_defcodelabel (FalseLab);
3059 /* The result is an rvalue in primary */
3060 ED_MakeRValExpr (Expr);
3061 ED_TestDone (Expr); /* Condition codes are set */
3067 static void hieOr (ExprDesc *Expr)
3068 /* Process "exp || exp". */
3071 int BoolOp = 0; /* Did we have a boolean op? */
3072 int AndOp; /* Did we have a && operation? */
3073 unsigned TrueLab; /* Jump to this label if true */
3077 TrueLab = GetLocalLabel ();
3079 /* Call the next level parser */
3080 hieAnd (Expr, TrueLab, &BoolOp);
3082 /* Any boolean or's? */
3083 if (CurTok.Tok == TOK_BOOL_OR) {
3085 /* If the expr hasn't set condition codes, set the force-test flag */
3086 if (!ED_IsTested (Expr)) {
3087 ED_MarkForTest (Expr);
3090 /* Get first expr */
3091 LoadExpr (CF_FORCECHAR, Expr);
3093 /* For each expression jump to TrueLab if true. Beware: If we
3094 ** had && operators, the jump is already in place!
3097 g_truejump (CF_NONE, TrueLab);
3100 /* Remember that we had a boolean op */
3103 /* while there's more expr */
3104 while (CurTok.Tok == TOK_BOOL_OR) {
3111 hieAnd (&Expr2, TrueLab, &AndOp);
3112 if (!ED_IsTested (&Expr2)) {
3113 ED_MarkForTest (&Expr2);
3115 LoadExpr (CF_FORCECHAR, &Expr2);
3117 /* If there is more to come, add shortcut boolean eval. */
3118 g_truejump (CF_NONE, TrueLab);
3122 /* The result is an rvalue in primary */
3123 ED_MakeRValExpr (Expr);
3124 ED_TestDone (Expr); /* Condition codes are set */
3127 /* If we really had boolean ops, generate the end sequence */
3129 DoneLab = GetLocalLabel ();
3130 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
3131 g_falsejump (CF_NONE, DoneLab);
3132 g_defcodelabel (TrueLab);
3133 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
3134 g_defcodelabel (DoneLab);
3140 static void hieQuest (ExprDesc* Expr)
3141 /* Parse the ternary operator */
3145 CodeMark TrueCodeEnd;
3146 ExprDesc Expr2; /* Expression 2 */
3147 ExprDesc Expr3; /* Expression 3 */
3148 int Expr2IsNULL; /* Expression 2 is a NULL pointer */
3149 int Expr3IsNULL; /* Expression 3 is a NULL pointer */
3150 Type* ResultType; /* Type of result */
3153 /* Call the lower level eval routine */
3154 if (Preprocessing) {
3155 ExprWithCheck (hieOrPP, Expr);
3157 ExprWithCheck (hieOr, Expr);
3160 /* Check if it's a ternary expression */
3161 if (CurTok.Tok == TOK_QUEST) {
3163 if (!ED_IsTested (Expr)) {
3164 /* Condition codes not set, request a test */
3165 ED_MarkForTest (Expr);
3167 LoadExpr (CF_NONE, Expr);
3168 FalseLab = GetLocalLabel ();
3169 g_falsejump (CF_NONE, FalseLab);
3171 /* Parse second expression. Remember for later if it is a NULL pointer
3172 ** expression, then load it into the primary.
3174 ExprWithCheck (hie1, &Expr2);
3175 Expr2IsNULL = ED_IsNullPtr (&Expr2);
3176 if (!IsTypeVoid (Expr2.Type)) {
3177 /* Load it into the primary */
3178 LoadExpr (CF_NONE, &Expr2);
3179 ED_MakeRValExpr (&Expr2);
3180 Expr2.Type = PtrConversion (Expr2.Type);
3183 /* Remember the current code position */
3184 GetCodePos (&TrueCodeEnd);
3186 /* Jump around the evaluation of the third expression */
3187 TrueLab = GetLocalLabel ();
3191 /* Jump here if the first expression was false */
3192 g_defcodelabel (FalseLab);
3194 /* Parse third expression. Remember for later if it is a NULL pointer
3195 ** expression, then load it into the primary.
3197 ExprWithCheck (hie1, &Expr3);
3198 Expr3IsNULL = ED_IsNullPtr (&Expr3);
3199 if (!IsTypeVoid (Expr3.Type)) {
3200 /* Load it into the primary */
3201 LoadExpr (CF_NONE, &Expr3);
3202 ED_MakeRValExpr (&Expr3);
3203 Expr3.Type = PtrConversion (Expr3.Type);
3206 /* Check if any conversions are needed, if so, do them.
3207 ** Conversion rules for ?: expression are:
3208 ** - if both expressions are int expressions, default promotion
3209 ** rules for ints apply.
3210 ** - if both expressions are pointers of the same type, the
3211 ** result of the expression is of this type.
3212 ** - if one of the expressions is a pointer and the other is
3213 ** a zero constant, the resulting type is that of the pointer
3215 ** - if both expressions are void expressions, the result is of
3217 ** - all other cases are flagged by an error.
3219 if (IsClassInt (Expr2.Type) && IsClassInt (Expr3.Type)) {
3221 CodeMark CvtCodeStart;
3222 CodeMark CvtCodeEnd;
3225 /* Get common type */
3226 ResultType = promoteint (Expr2.Type, Expr3.Type);
3228 /* Convert the third expression to this type if needed */
3229 TypeConversion (&Expr3, ResultType);
3231 /* Emit conversion code for the second expression, but remember
3232 ** where it starts end ends.
3234 GetCodePos (&CvtCodeStart);
3235 TypeConversion (&Expr2, ResultType);
3236 GetCodePos (&CvtCodeEnd);
3238 /* If we had conversion code, move it to the right place */
3239 if (!CodeRangeIsEmpty (&CvtCodeStart, &CvtCodeEnd)) {
3240 MoveCode (&CvtCodeStart, &CvtCodeEnd, &TrueCodeEnd);
3243 } else if (IsClassPtr (Expr2.Type) && IsClassPtr (Expr3.Type)) {
3244 /* Must point to same type */
3245 if (TypeCmp (Indirect (Expr2.Type), Indirect (Expr3.Type)) < TC_EQUAL) {
3246 Error ("Incompatible pointer types");
3248 /* Result has the common type */
3249 ResultType = Expr2.Type;
3250 } else if (IsClassPtr (Expr2.Type) && Expr3IsNULL) {
3251 /* Result type is pointer, no cast needed */
3252 ResultType = Expr2.Type;
3253 } else if (Expr2IsNULL && IsClassPtr (Expr3.Type)) {
3254 /* Result type is pointer, no cast needed */
3255 ResultType = Expr3.Type;
3256 } else if (IsTypeVoid (Expr2.Type) && IsTypeVoid (Expr3.Type)) {
3257 /* Result type is void */
3258 ResultType = Expr3.Type;
3260 Error ("Incompatible types");
3261 ResultType = Expr2.Type; /* Doesn't matter here */
3264 /* Define the final label */
3265 g_defcodelabel (TrueLab);
3267 /* Setup the target expression */
3268 ED_MakeRValExpr (Expr);
3269 Expr->Type = ResultType;
3275 static void opeq (const GenDesc* Gen, ExprDesc* Expr, const char* Op)
3276 /* Process "op=" operators. */
3283 /* op= can only be used with lvalues */
3284 if (!ED_IsLVal (Expr)) {
3285 Error ("Invalid lvalue in assignment");
3289 /* The left side must not be const qualified */
3290 if (IsQualConst (Expr->Type)) {
3291 Error ("Assignment to const");
3294 /* There must be an integer or pointer on the left side */
3295 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
3296 Error ("Invalid left operand type");
3297 /* Continue. Wrong code will be generated, but the compiler won't
3298 ** break, so this is the best error recovery.
3302 /* Skip the operator token */
3305 /* Determine the type of the lhs */
3306 flags = TypeOf (Expr->Type);
3307 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) && IsTypePtr (Expr->Type);
3309 /* Get the lhs address on stack (if needed) */
3312 /* Fetch the lhs into the primary register if needed */
3313 LoadExpr (CF_NONE, Expr);
3315 /* Bring the lhs on stack */
3319 /* Evaluate the rhs */
3320 MarkedExprWithCheck (hie1, &Expr2);
3322 /* The rhs must be an integer (or a float, but we don't support that yet */
3323 if (!IsClassInt (Expr2.Type)) {
3324 Error ("Invalid right operand for binary operator '%s'", Op);
3325 /* Continue. Wrong code will be generated, but the compiler won't
3326 ** break, so this is the best error recovery.
3330 /* Check for a constant expression */
3331 if (ED_IsConstAbs (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
3332 /* The resulting value is a constant. If the generator has the NOPUSH
3333 ** flag set, don't push the lhs.
3335 if (Gen->Flags & GEN_NOPUSH) {
3339 /* lhs is a pointer, scale rhs */
3340 Expr2.IVal *= CheckedSizeOf (Expr->Type+1);
3343 /* If the lhs is character sized, the operation may be later done
3346 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
3347 flags |= CF_FORCECHAR;
3350 /* Special handling for add and sub - some sort of a hack, but short code */
3351 if (Gen->Func == g_add) {
3352 g_inc (flags | CF_CONST, Expr2.IVal);
3353 } else if (Gen->Func == g_sub) {
3354 g_dec (flags | CF_CONST, Expr2.IVal);
3356 if (Expr2.IVal == 0) {
3357 /* Check for div by zero/mod by zero */
3358 if (Gen->Func == g_div) {
3359 Error ("Division by zero");
3360 } else if (Gen->Func == g_mod) {
3361 Error ("Modulo operation with zero");
3364 Gen->Func (flags | CF_CONST, Expr2.IVal);
3368 /* rhs is not constant. Load into the primary */
3369 LoadExpr (CF_NONE, &Expr2);
3371 /* lhs is a pointer, scale rhs */
3372 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Expr->Type+1));
3375 /* If the lhs is character sized, the operation may be later done
3378 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
3379 flags |= CF_FORCECHAR;
3382 /* Adjust the types of the operands if needed */
3383 Gen->Func (g_typeadjust (flags, TypeOf (Expr2.Type)), 0);
3386 ED_MakeRValExpr (Expr);
3391 static void addsubeq (const GenDesc* Gen, ExprDesc *Expr, const char* Op)
3392 /* Process the += and -= operators */
3400 /* We're currently only able to handle some adressing modes */
3401 if (ED_GetLoc (Expr) == E_LOC_EXPR || ED_GetLoc (Expr) == E_LOC_PRIMARY) {
3402 /* Use generic routine */
3403 opeq (Gen, Expr, Op);
3407 /* We must have an lvalue */
3408 if (ED_IsRVal (Expr)) {
3409 Error ("Invalid lvalue in assignment");
3413 /* The left side must not be const qualified */
3414 if (IsQualConst (Expr->Type)) {
3415 Error ("Assignment to const");
3418 /* There must be an integer or pointer on the left side */
3419 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
3420 Error ("Invalid left operand type");
3421 /* Continue. Wrong code will be generated, but the compiler won't
3422 ** break, so this is the best error recovery.
3426 /* Skip the operator */
3429 /* Check if we have a pointer expression and must scale rhs */
3430 MustScale = IsTypePtr (Expr->Type);
3432 /* Initialize the code generator flags */
3436 /* Evaluate the rhs. We expect an integer here, since float is not
3440 if (!IsClassInt (Expr2.Type)) {
3441 Error ("Invalid right operand for binary operator '%s'", Op);
3442 /* Continue. Wrong code will be generated, but the compiler won't
3443 ** break, so this is the best error recovery.
3446 if (ED_IsConstAbs (&Expr2)) {
3447 /* The resulting value is a constant. Scale it. */
3449 Expr2.IVal *= CheckedSizeOf (Indirect (Expr->Type));
3454 /* Not constant, load into the primary */
3455 LoadExpr (CF_NONE, &Expr2);
3457 /* lhs is a pointer, scale rhs */
3458 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Indirect (Expr->Type)));
3462 /* Setup the code generator flags */
3463 lflags |= TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR;
3464 rflags |= TypeOf (Expr2.Type) | CF_FORCECHAR;
3466 /* Convert the type of the lhs to that of the rhs */
3467 g_typecast (lflags, rflags);
3469 /* Output apropriate code depending on the location */
3470 switch (ED_GetLoc (Expr)) {
3473 /* Absolute: numeric address or const */
3474 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3475 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3477 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3482 /* Global variable */
3483 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3484 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3486 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3492 /* Static variable or literal in the literal pool */
3493 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3494 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3496 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3500 case E_LOC_REGISTER:
3501 /* Register variable */
3502 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3503 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3505 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3510 /* Value on the stack */
3511 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3512 g_addeqlocal (lflags, Expr->IVal, Expr2.IVal);
3514 g_subeqlocal (lflags, Expr->IVal, Expr2.IVal);
3519 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
3522 /* Expression is a rvalue in the primary now */
3523 ED_MakeRValExpr (Expr);
3528 void hie1 (ExprDesc* Expr)
3529 /* Parse first level of expression hierarchy. */
3532 switch (CurTok.Tok) {
3538 case TOK_PLUS_ASSIGN:
3539 addsubeq (&GenPASGN, Expr, "+=");
3542 case TOK_MINUS_ASSIGN:
3543 addsubeq (&GenSASGN, Expr, "-=");
3546 case TOK_MUL_ASSIGN:
3547 opeq (&GenMASGN, Expr, "*=");
3550 case TOK_DIV_ASSIGN:
3551 opeq (&GenDASGN, Expr, "/=");
3554 case TOK_MOD_ASSIGN:
3555 opeq (&GenMOASGN, Expr, "%=");
3558 case TOK_SHL_ASSIGN:
3559 opeq (&GenSLASGN, Expr, "<<=");
3562 case TOK_SHR_ASSIGN:
3563 opeq (&GenSRASGN, Expr, ">>=");
3566 case TOK_AND_ASSIGN:
3567 opeq (&GenAASGN, Expr, "&=");
3570 case TOK_XOR_ASSIGN:
3571 opeq (&GenXOASGN, Expr, "^=");
3575 opeq (&GenOASGN, Expr, "|=");
3585 void hie0 (ExprDesc *Expr)
3586 /* Parse comma operator. */
3589 while (CurTok.Tok == TOK_COMMA) {
3597 int evalexpr (unsigned Flags, void (*Func) (ExprDesc*), ExprDesc* Expr)
3598 /* Will evaluate an expression via the given function. If the result is a
3599 ** constant, 0 is returned and the value is put in the Expr struct. If the
3600 ** result is not constant, LoadExpr is called to bring the value into the
3601 ** primary register and 1 is returned.
3605 ExprWithCheck (Func, Expr);
3607 /* Check for a constant expression */
3608 if (ED_IsConstAbs (Expr)) {
3609 /* Constant expression */
3612 /* Not constant, load into the primary */
3613 LoadExpr (Flags, Expr);
3620 void Expression0 (ExprDesc* Expr)
3621 /* Evaluate an expression via hie0 and put the result into the primary register */
3623 ExprWithCheck (hie0, Expr);
3624 LoadExpr (CF_NONE, Expr);
3629 void ConstExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3630 /* Will evaluate an expression via the given function. If the result is not
3631 ** a constant of some sort, a diagnostic will be printed, and the value is
3632 ** replaced by a constant one to make sure there are no internal errors that
3633 ** result from this input error.
3636 ExprWithCheck (Func, Expr);
3637 if (!ED_IsConst (Expr)) {
3638 Error ("Constant expression expected");
3639 /* To avoid any compiler errors, make the expression a valid const */
3640 ED_MakeConstAbsInt (Expr, 1);
3646 void BoolExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3647 /* Will evaluate an expression via the given function. If the result is not
3648 ** something that may be evaluated in a boolean context, a diagnostic will be
3649 ** printed, and the value is replaced by a constant one to make sure there
3650 ** are no internal errors that result from this input error.
3653 ExprWithCheck (Func, Expr);
3654 if (!ED_IsBool (Expr)) {
3655 Error ("Boolean expression expected");
3656 /* To avoid any compiler errors, make the expression a valid int */
3657 ED_MakeConstAbsInt (Expr, 1);
3663 void ConstAbsIntExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3664 /* Will evaluate an expression via the given function. If the result is not
3665 ** a constant numeric integer value, a diagnostic will be printed, and the
3666 ** value is replaced by a constant one to make sure there are no internal
3667 ** errors that result from this input error.
3670 ExprWithCheck (Func, Expr);
3671 if (!ED_IsConstAbsInt (Expr)) {
3672 Error ("Constant integer expression expected");
3673 /* To avoid any compiler errors, make the expression a valid const */
3674 ED_MakeConstAbsInt (Expr, 1);