3 * Ullrich von Bassewitz, 21.06.1998
13 #include "debugflag.h"
20 #include "assignment.h"
32 #include "shiftexpr.h"
43 /*****************************************************************************/
45 /*****************************************************************************/
49 /* Generator attributes */
50 #define GEN_NOPUSH 0x01 /* Don't push lhs */
52 /* Map a generator function and its attributes to a token */
54 token_t Tok; /* Token to map to */
55 unsigned Flags; /* Flags for generator function */
56 void (*Func) (unsigned, unsigned long); /* Generator func */
59 /* Descriptors for the operations */
60 static GenDesc GenPASGN = { TOK_PLUS_ASSIGN, GEN_NOPUSH, g_add };
61 static GenDesc GenSASGN = { TOK_MINUS_ASSIGN, GEN_NOPUSH, g_sub };
62 static GenDesc GenMASGN = { TOK_MUL_ASSIGN, GEN_NOPUSH, g_mul };
63 static GenDesc GenDASGN = { TOK_DIV_ASSIGN, GEN_NOPUSH, g_div };
64 static GenDesc GenMOASGN = { TOK_MOD_ASSIGN, GEN_NOPUSH, g_mod };
65 static GenDesc GenSLASGN = { TOK_SHL_ASSIGN, GEN_NOPUSH, g_asl };
66 static GenDesc GenSRASGN = { TOK_SHR_ASSIGN, GEN_NOPUSH, g_asr };
67 static GenDesc GenAASGN = { TOK_AND_ASSIGN, GEN_NOPUSH, g_and };
68 static GenDesc GenXOASGN = { TOK_XOR_ASSIGN, GEN_NOPUSH, g_xor };
69 static GenDesc GenOASGN = { TOK_OR_ASSIGN, GEN_NOPUSH, g_or };
73 /*****************************************************************************/
74 /* Helper functions */
75 /*****************************************************************************/
79 static unsigned GlobalModeFlags (const ExprDesc* Expr)
80 /* Return the addressing mode flags for the given expression */
82 switch (ED_GetLoc (Expr)) {
83 case E_LOC_ABS: return CF_ABSOLUTE;
84 case E_LOC_GLOBAL: return CF_EXTERNAL;
85 case E_LOC_STATIC: return CF_STATIC;
86 case E_LOC_REGISTER: return CF_REGVAR;
87 case E_LOC_STACK: return CF_NONE;
88 case E_LOC_PRIMARY: return CF_NONE;
89 case E_LOC_EXPR: return CF_NONE;
90 case E_LOC_LITERAL: return CF_STATIC; /* Same as static */
92 Internal ("GlobalModeFlags: Invalid location flags value: 0x%04X", Expr->Flags);
100 void ExprWithCheck (void (*Func) (ExprDesc*), ExprDesc *Expr)
101 /* Call an expression function with checks. */
103 /* Remember the stack pointer */
104 int OldSP = StackPtr;
106 /* Call the expression function */
109 /* Do some checks if code generation is still constistent */
110 if (StackPtr != OldSP) {
113 "Code generation messed up!\n"
114 "StackPtr is %d, should be %d",
117 Internal ("StackPtr is %d, should be %d\n", StackPtr, OldSP);
124 static Type* promoteint (Type* lhst, Type* rhst)
125 /* In an expression with two ints, return the type of the result */
127 /* Rules for integer types:
128 * - If one of the values is a long, the result is long.
129 * - If one of the values is unsigned, the result is also unsigned.
130 * - Otherwise the result is an int.
132 if (IsTypeLong (lhst) || IsTypeLong (rhst)) {
133 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
139 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
149 static unsigned typeadjust (ExprDesc* lhs, ExprDesc* rhs, int NoPush)
150 /* Adjust the two values for a binary operation. lhs is expected on stack or
151 * to be constant, rhs is expected to be in the primary register or constant.
152 * The function will put the type of the result into lhs and return the
153 * code generator flags for the operation.
154 * If NoPush is given, it is assumed that the operation does not expect the lhs
155 * to be on stack, and that lhs is in a register instead.
156 * Beware: The function does only accept int types.
159 unsigned ltype, rtype;
162 /* Get the type strings */
163 Type* lhst = lhs->Type;
164 Type* rhst = rhs->Type;
166 /* Generate type adjustment code if needed */
167 ltype = TypeOf (lhst);
168 if (ED_IsLocAbs (lhs)) {
172 /* Value is in primary register*/
175 rtype = TypeOf (rhst);
176 if (ED_IsLocAbs (rhs)) {
179 flags = g_typeadjust (ltype, rtype);
181 /* Set the type of the result */
182 lhs->Type = promoteint (lhst, rhst);
184 /* Return the code generator flags */
190 static const GenDesc* FindGen (token_t Tok, const GenDesc* Table)
191 /* Find a token in a generator table */
193 while (Table->Tok != TOK_INVALID) {
194 if (Table->Tok == Tok) {
204 static int TypeSpecAhead (void)
205 /* Return true if some sort of type is waiting (helper for cast and sizeof()
211 /* There's a type waiting if:
213 * We have an opening paren, and
214 * a. the next token is a type, or
215 * b. the next token is a type qualifier, or
216 * c. the next token is a typedef'd type
218 return CurTok.Tok == TOK_LPAREN && (
219 TokIsType (&NextTok) ||
220 TokIsTypeQual (&NextTok) ||
221 (NextTok.Tok == TOK_IDENT &&
222 (Entry = FindSym (NextTok.Ident)) != 0 &&
223 SymIsTypeDef (Entry)));
228 void PushAddr (const ExprDesc* Expr)
229 /* If the expression contains an address that was somehow evaluated,
230 * push this address on the stack. This is a helper function for all
231 * sorts of implicit or explicit assignment functions where the lvalue
232 * must be saved if it's not constant, before evaluating the rhs.
235 /* Get the address on stack if needed */
236 if (ED_IsLocExpr (Expr)) {
237 /* Push the address (always a pointer) */
244 /*****************************************************************************/
246 /*****************************************************************************/
250 static unsigned FunctionParamList (FuncDesc* Func, int IsFastcall)
251 /* Parse a function parameter list and pass the parameters to the called
252 * function. Depending on several criteria this may be done by just pushing
253 * each parameter separately, or creating the parameter frame once and then
254 * storing into this frame.
255 * The function returns the size of the parameters pushed.
260 /* Initialize variables */
261 SymEntry* Param = 0; /* Keep gcc silent */
262 unsigned ParamSize = 0; /* Size of parameters pushed */
263 unsigned ParamCount = 0; /* Number of parameters pushed */
264 unsigned FrameSize = 0; /* Size of parameter frame */
265 unsigned FrameParams = 0; /* Number of params in frame */
266 int FrameOffs = 0; /* Offset into parameter frame */
267 int Ellipsis = 0; /* Function is variadic */
269 /* As an optimization, we may allocate the complete parameter frame at
270 * once instead of pushing each parameter as it comes. We may do that,
273 * - optimizations that increase code size are enabled (allocating the
274 * stack frame at once gives usually larger code).
275 * - we have more than one parameter to push (don't count the last param
276 * for __fastcall__ functions).
278 * The FrameSize variable will contain a value > 0 if storing into a frame
279 * (instead of pushing) is enabled.
282 if (IS_Get (&CodeSizeFactor) >= 200) {
284 /* Calculate the number and size of the parameters */
285 FrameParams = Func->ParamCount;
286 FrameSize = Func->ParamSize;
287 if (FrameParams > 0 && IsFastcall) {
288 /* Last parameter is not pushed */
289 FrameSize -= CheckedSizeOf (Func->LastParam->Type);
293 /* Do we have more than one parameter in the frame? */
294 if (FrameParams > 1) {
295 /* Okeydokey, setup the frame */
296 FrameOffs = StackPtr;
298 StackPtr -= FrameSize;
300 /* Don't use a preallocated frame */
305 /* Parse the actual parameter list */
306 while (CurTok.Tok != TOK_RPAREN) {
310 /* Count arguments */
313 /* Fetch the pointer to the next argument, check for too many args */
314 if (ParamCount <= Func->ParamCount) {
315 /* Beware: If there are parameters with identical names, they
316 * cannot go into the same symbol table, which means that in this
317 * case of errorneous input, the number of nodes in the symbol
318 * table and ParamCount are NOT equal. We have to handle this case
319 * below to avoid segmentation violations. Since we know that this
320 * problem can only occur if there is more than one parameter,
321 * we will just use the last one.
323 if (ParamCount == 1) {
325 Param = Func->SymTab->SymHead;
326 } else if (Param->NextSym != 0) {
328 Param = Param->NextSym;
329 CHECK ((Param->Flags & SC_PARAM) != 0);
331 } else if (!Ellipsis) {
332 /* Too many arguments. Do we have an open param list? */
333 if ((Func->Flags & FD_VARIADIC) == 0) {
334 /* End of param list reached, no ellipsis */
335 Error ("Too many arguments in function call");
337 /* Assume an ellipsis even in case of errors to avoid an error
338 * message for each other argument.
343 /* Evaluate the parameter expression */
346 /* If we don't have an argument spec, accept anything, otherwise
347 * convert the actual argument to the type needed.
352 /* Convert the argument to the parameter type if needed */
353 TypeConversion (&Expr, Param->Type);
355 /* If we have a prototype, chars may be pushed as chars */
356 Flags |= CF_FORCECHAR;
360 /* No prototype available. Convert array to "pointer to first
361 * element", and function to "pointer to function".
363 Expr.Type = PtrConversion (Expr.Type);
367 /* Load the value into the primary if it is not already there */
368 LoadExpr (Flags, &Expr);
370 /* Use the type of the argument for the push */
371 Flags |= TypeOf (Expr.Type);
373 /* If this is a fastcall function, don't push the last argument */
374 if (ParamCount != Func->ParamCount || !IsFastcall) {
375 unsigned ArgSize = sizeofarg (Flags);
377 /* We have the space already allocated, store in the frame.
378 * Because of invalid type conversions (that have produced an
379 * error before), we can end up here with a non aligned stack
380 * frame. Since no output will be generated anyway, handle
381 * these cases gracefully instead of doing a CHECK.
383 if (FrameSize >= ArgSize) {
384 FrameSize -= ArgSize;
388 FrameOffs -= ArgSize;
390 g_putlocal (Flags | CF_NOKEEP, FrameOffs, Expr.IVal);
392 /* Push the argument */
393 g_push (Flags, Expr.IVal);
396 /* Calculate total parameter size */
397 ParamSize += ArgSize;
400 /* Check for end of argument list */
401 if (CurTok.Tok != TOK_COMMA) {
407 /* Check if we had enough parameters */
408 if (ParamCount < Func->ParamCount) {
409 Error ("Too few arguments in function call");
412 /* The function returns the size of all parameters pushed onto the stack.
413 * However, if there are parameters missing (which is an error and was
414 * flagged by the compiler) AND a stack frame was preallocated above,
415 * we would loose track of the stackpointer and generate an internal error
416 * later. So we correct the value by the parameters that should have been
417 * pushed to avoid an internal compiler error. Since an error was
418 * generated before, no code will be output anyway.
420 return ParamSize + FrameSize;
425 static void FunctionCall (ExprDesc* Expr)
426 /* Perform a function call. */
428 FuncDesc* Func; /* Function descriptor */
429 int IsFuncPtr; /* Flag */
430 unsigned ParamSize; /* Number of parameter bytes */
432 int PtrOffs = 0; /* Offset of function pointer on stack */
433 int IsFastcall = 0; /* True if it's a fast call function */
434 int PtrOnStack = 0; /* True if a pointer copy is on stack */
436 /* Skip the left paren */
439 /* Get a pointer to the function descriptor from the type string */
440 Func = GetFuncDesc (Expr->Type);
442 /* Handle function pointers transparently */
443 IsFuncPtr = IsTypeFuncPtr (Expr->Type);
446 /* Check wether it's a fastcall function that has parameters */
447 IsFastcall = IsQualFastcall (Expr->Type + 1) && (Func->ParamCount > 0);
449 /* Things may be difficult, depending on where the function pointer
450 * resides. If the function pointer is an expression of some sort
451 * (not a local or global variable), we have to evaluate this
452 * expression now and save the result for later. Since calls to
453 * function pointers may be nested, we must save it onto the stack.
454 * For fastcall functions we do also need to place a copy of the
455 * pointer on stack, since we cannot use a/x.
457 PtrOnStack = IsFastcall || !ED_IsConst (Expr);
460 /* Not a global or local variable, or a fastcall function. Load
461 * the pointer into the primary and mark it as an expression.
463 LoadExpr (CF_NONE, Expr);
464 ED_MakeRValExpr (Expr);
466 /* Remember the code position */
469 /* Push the pointer onto the stack and remember the offset */
475 /* Check for known standard functions and inline them */
476 if (Expr->Name != 0) {
477 int StdFunc = FindStdFunc ((const char*) Expr->Name);
479 /* Inline this function */
480 HandleStdFunc (StdFunc, Func, Expr);
485 /* If we didn't inline the function, get fastcall info */
486 IsFastcall = IsQualFastcall (Expr->Type);
489 /* Parse the parameter list */
490 ParamSize = FunctionParamList (Func, IsFastcall);
492 /* We need the closing paren here */
495 /* Special handling for function pointers */
498 /* If the function is not a fastcall function, load the pointer to
499 * the function into the primary.
503 /* Not a fastcall function - we may use the primary */
505 /* If we have no parameters, the pointer is still in the
506 * primary. Remove the code to push it and correct the
509 if (ParamSize == 0) {
513 /* Load from the saved copy */
514 g_getlocal (CF_PTR, PtrOffs);
517 /* Load from original location */
518 LoadExpr (CF_NONE, Expr);
521 /* Call the function */
522 g_callind (TypeOf (Expr->Type+1), ParamSize, PtrOffs);
526 /* Fastcall function. We cannot use the primary for the function
527 * pointer and must therefore use an offset to the stack location.
528 * Since fastcall functions may never be variadic, we can use the
529 * index register for this purpose.
531 g_callind (CF_LOCAL, ParamSize, PtrOffs);
534 /* If we have a pointer on stack, remove it */
536 g_space (- (int) sizeofarg (CF_PTR));
545 /* Normal function */
546 g_call (TypeOf (Expr->Type), (const char*) Expr->Name, ParamSize);
550 /* The function result is an rvalue in the primary register */
551 ED_MakeRValExpr (Expr);
552 Expr->Type = GetFuncReturn (Expr->Type);
557 static void Primary (ExprDesc* E)
558 /* This is the lowest level of the expression parser. */
562 /* Initialize fields in the expression stucture */
565 /* Character and integer constants. */
566 if (CurTok.Tok == TOK_ICONST || CurTok.Tok == TOK_CCONST) {
567 E->IVal = CurTok.IVal;
568 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
569 E->Type = CurTok.Type;
574 /* Floating point constant */
575 if (CurTok.Tok == TOK_FCONST) {
576 E->FVal = CurTok.FVal;
577 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
578 E->Type = CurTok.Type;
583 /* Process parenthesized subexpression by calling the whole parser
586 if (CurTok.Tok == TOK_LPAREN) {
593 /* If we run into an identifier in preprocessing mode, we assume that this
594 * is an undefined macro and replace it by a constant value of zero.
596 if (Preprocessing && CurTok.Tok == TOK_IDENT) {
598 ED_MakeConstAbsInt (E, 0);
602 /* All others may only be used if the expression evaluation is not called
603 * recursively by the preprocessor.
606 /* Illegal expression in PP mode */
607 Error ("Preprocessor expression expected");
608 ED_MakeConstAbsInt (E, 1);
612 switch (CurTok.Tok) {
615 /* Identifier. Get a pointer to the symbol table entry */
616 Sym = E->Sym = FindSym (CurTok.Ident);
618 /* Is the symbol known? */
621 /* We found the symbol - skip the name token */
624 /* Check for illegal symbol types */
625 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
626 if (Sym->Flags & SC_TYPE) {
627 /* Cannot use type symbols */
628 Error ("Variable identifier expected");
629 /* Assume an int type to make E valid */
630 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
635 /* Mark the symbol as referenced */
636 Sym->Flags |= SC_REF;
638 /* The expression type is the symbol type */
641 /* Check for legal symbol types */
642 if ((Sym->Flags & SC_CONST) == SC_CONST) {
643 /* Enum or some other numeric constant */
644 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
645 E->IVal = Sym->V.ConstVal;
646 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
648 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
649 E->Name = (unsigned long) Sym->Name;
650 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
651 /* Local variable. If this is a parameter for a variadic
652 * function, we have to add some address calculations, and the
653 * address is not const.
655 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
656 /* Variadic parameter */
657 g_leavariadic (Sym->V.Offs - F_GetParamSize (CurrentFunc));
658 E->Flags = E_LOC_EXPR | E_RTYPE_LVAL;
660 /* Normal parameter */
661 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
662 E->IVal = Sym->V.Offs;
664 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
665 /* Register variable, zero page based */
666 E->Flags = E_LOC_REGISTER | E_RTYPE_LVAL;
667 E->Name = Sym->V.R.RegOffs;
668 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
669 /* Static variable */
670 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
671 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
672 E->Name = (unsigned long) Sym->Name;
674 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
675 E->Name = Sym->V.Label;
678 /* Local static variable */
679 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
680 E->Name = Sym->V.Offs;
683 /* We've made all variables lvalues above. However, this is
684 * not always correct: An array is actually the address of its
685 * first element, which is a rvalue, and a function is a
686 * rvalue, too, because we cannot store anything in a function.
687 * So fix the flags depending on the type.
689 if (IsTypeArray (E->Type) || IsTypeFunc (E->Type)) {
695 /* We did not find the symbol. Remember the name, then skip it */
697 strcpy (Ident, CurTok.Ident);
700 /* IDENT is either an auto-declared function or an undefined variable. */
701 if (CurTok.Tok == TOK_LPAREN) {
702 /* C99 doesn't allow calls to undefined functions, so
703 * generate an error and otherwise a warning. Declare a
704 * function returning int. For that purpose, prepare a
705 * function signature for a function having an empty param
706 * list and returning int.
708 if (IS_Get (&Standard) >= STD_C99) {
709 Error ("Call to undefined function `%s'", Ident);
711 Warning ("Call to undefined function `%s'", Ident);
713 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
715 E->Flags = E_LOC_GLOBAL | E_RTYPE_RVAL;
716 E->Name = (unsigned long) Sym->Name;
718 /* Undeclared Variable */
719 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
720 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
722 Error ("Undefined symbol: `%s'", Ident);
730 E->Type = GetCharArrayType (GetLiteralPoolOffs () - CurTok.IVal);
731 E->Flags = E_LOC_LITERAL | E_RTYPE_RVAL;
732 E->IVal = CurTok.IVal;
733 E->Name = LiteralPoolLabel;
740 E->Flags = E_LOC_EXPR | E_RTYPE_RVAL;
745 /* Register pseudo variable */
746 E->Type = type_uchar;
747 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
752 /* Register pseudo variable */
754 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
759 /* Register pseudo variable */
760 E->Type = type_ulong;
761 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
766 /* Illegal primary. Be sure to skip the token to avoid endless
769 Error ("Expression expected");
771 ED_MakeConstAbsInt (E, 1);
778 static void ArrayRef (ExprDesc* Expr)
779 /* Handle an array reference. This function needs a rewrite. */
789 /* Skip the bracket */
792 /* Get the type of left side */
795 /* We can apply a special treatment for arrays that have a const base
796 * address. This is true for most arrays and will produce a lot better
797 * code. Check if this is a const base address.
799 ConstBaseAddr = ED_IsRVal (Expr) &&
800 (ED_IsLocConst (Expr) || ED_IsLocStack (Expr));
802 /* If we have a constant base, we delay the address fetch */
804 if (!ConstBaseAddr) {
805 /* Get a pointer to the array into the primary */
806 LoadExpr (CF_NONE, Expr);
808 /* Get the array pointer on stack. Do not push more than 16
809 * bit, even if this value is greater, since we cannot handle
810 * other than 16bit stuff when doing indexing.
816 /* TOS now contains ptr to array elements. Get the subscript. */
817 ExprWithCheck (hie0, &SubScript);
819 /* Check the types of array and subscript. We can either have a
820 * pointer/array to the left, in which case the subscript must be of an
821 * integer type, or we have an integer to the left, in which case the
822 * subscript must be a pointer/array.
823 * Since we do the necessary checking here, we can rely later on the
826 if (IsClassPtr (Expr->Type)) {
827 if (!IsClassInt (SubScript.Type)) {
828 Error ("Array subscript is not an integer");
829 /* To avoid any compiler errors, make the expression a valid int */
830 ED_MakeConstAbsInt (&SubScript, 0);
832 ElementType = Indirect (Expr->Type);
833 } else if (IsClassInt (Expr->Type)) {
834 if (!IsClassPtr (SubScript.Type)) {
835 Error ("Subscripted value is neither array nor pointer");
836 /* To avoid compiler errors, make the subscript a char[] at
839 ED_MakeConstAbs (&SubScript, 0, GetCharArrayType (1));
841 ElementType = Indirect (SubScript.Type);
843 Error ("Cannot subscript");
844 /* To avoid compiler errors, fake both the array and the subscript, so
845 * we can just proceed.
847 ED_MakeConstAbs (Expr, 0, GetCharArrayType (1));
848 ED_MakeConstAbsInt (&SubScript, 0);
849 ElementType = Indirect (Expr->Type);
852 /* Check if the subscript is constant absolute value */
853 if (ED_IsConstAbs (&SubScript)) {
855 /* The array subscript is a numeric constant. If we had pushed the
856 * array base address onto the stack before, we can remove this value,
857 * since we can generate expression+offset.
859 if (!ConstBaseAddr) {
862 /* Get an array pointer into the primary */
863 LoadExpr (CF_NONE, Expr);
866 if (IsClassPtr (Expr->Type)) {
868 /* Lhs is pointer/array. Scale the subscript value according to
871 SubScript.IVal *= CheckedSizeOf (ElementType);
873 /* Remove the address load code */
876 /* In case of an array, we can adjust the offset of the expression
877 * already in Expr. If the base address was a constant, we can even
878 * remove the code that loaded the address into the primary.
880 if (IsTypeArray (Expr->Type)) {
882 /* Adjust the offset */
883 Expr->IVal += SubScript.IVal;
887 /* It's a pointer, so we do have to load it into the primary
888 * first (if it's not already there).
890 if (ConstBaseAddr || ED_IsLVal (Expr)) {
891 LoadExpr (CF_NONE, Expr);
892 ED_MakeRValExpr (Expr);
896 Expr->IVal = SubScript.IVal;
901 /* Scale the rhs value according to the element type */
902 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
904 /* Add the subscript. Since arrays are indexed by integers,
905 * we will ignore the true type of the subscript here and
906 * use always an int. #### Use offset but beware of LoadExpr!
908 g_inc (CF_INT | CF_CONST, SubScript.IVal);
914 /* Array subscript is not constant. Load it into the primary */
916 LoadExpr (CF_NONE, &SubScript);
919 if (IsClassPtr (Expr->Type)) {
921 /* Indexing is based on unsigneds, so we will just use the integer
922 * portion of the index (which is in (e)ax, so there's no further
925 g_scale (CF_INT, CheckedSizeOf (ElementType));
929 /* Get the int value on top. If we come here, we're sure, both
930 * values are 16 bit (the first one was truncated if necessary
931 * and the second one is a pointer). Note: If ConstBaseAddr is
932 * true, we don't have a value on stack, so to "swap" both, just
933 * push the subscript.
937 LoadExpr (CF_NONE, Expr);
944 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
948 /* The offset is now in the primary register. It we didn't have a
949 * constant base address for the lhs, the lhs address is already
950 * on stack, and we must add the offset. If the base address was
951 * constant, we call special functions to add the address to the
954 if (!ConstBaseAddr) {
956 /* The array base address is on stack and the subscript is in the
963 /* The subscript is in the primary, and the array base address is
964 * in Expr. If the subscript has itself a constant address, it is
965 * often a better idea to reverse again the order of the
966 * evaluation. This will generate better code if the subscript is
967 * a byte sized variable. But beware: This is only possible if the
968 * subscript was not scaled, that is, if this was a byte array
971 if ((ED_IsLocConst (&SubScript) || ED_IsLocStack (&SubScript)) &&
972 CheckedSizeOf (ElementType) == SIZEOF_CHAR) {
976 /* Reverse the order of evaluation */
977 if (CheckedSizeOf (SubScript.Type) == SIZEOF_CHAR) {
984 /* Get a pointer to the array into the primary. */
985 LoadExpr (CF_NONE, Expr);
987 /* Add the variable */
988 if (ED_IsLocStack (&SubScript)) {
989 g_addlocal (Flags, SubScript.IVal);
991 Flags |= GlobalModeFlags (&SubScript);
992 g_addstatic (Flags, SubScript.Name, SubScript.IVal);
996 if (ED_IsLocAbs (Expr)) {
997 /* Constant numeric address. Just add it */
998 g_inc (CF_INT, Expr->IVal);
999 } else if (ED_IsLocStack (Expr)) {
1000 /* Base address is a local variable address */
1001 if (IsTypeArray (Expr->Type)) {
1002 g_addaddr_local (CF_INT, Expr->IVal);
1004 g_addlocal (CF_PTR, Expr->IVal);
1007 /* Base address is a static variable address */
1008 unsigned Flags = CF_INT | GlobalModeFlags (Expr);
1009 if (ED_IsRVal (Expr)) {
1010 /* Add the address of the location */
1011 g_addaddr_static (Flags, Expr->Name, Expr->IVal);
1013 /* Add the contents of the location */
1014 g_addstatic (Flags, Expr->Name, Expr->IVal);
1022 /* The result is an expression in the primary */
1023 ED_MakeRValExpr (Expr);
1027 /* Result is of element type */
1028 Expr->Type = ElementType;
1030 /* An array element is actually a variable. So the rules for variables
1031 * with respect to the reference type apply: If it's an array, it is
1032 * a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1033 * but an array cannot contain functions).
1035 if (IsTypeArray (Expr->Type)) {
1041 /* Consume the closing bracket */
1047 static void StructRef (ExprDesc* Expr)
1048 /* Process struct field after . or ->. */
1053 /* Skip the token and check for an identifier */
1055 if (CurTok.Tok != TOK_IDENT) {
1056 Error ("Identifier expected");
1057 Expr->Type = type_int;
1061 /* Get the symbol table entry and check for a struct field */
1062 strcpy (Ident, CurTok.Ident);
1064 Field = FindStructField (Expr->Type, Ident);
1066 Error ("Struct/union has no field named `%s'", Ident);
1067 Expr->Type = type_int;
1071 /* If we have a struct pointer that is an lvalue and not already in the
1072 * primary, load it now.
1074 if (ED_IsLVal (Expr) && IsTypePtr (Expr->Type)) {
1076 /* Load into the primary */
1077 LoadExpr (CF_NONE, Expr);
1079 /* Make it an lvalue expression */
1080 ED_MakeLValExpr (Expr);
1083 /* Set the struct field offset */
1084 Expr->IVal += Field->V.Offs;
1086 /* The type is now the type of the field */
1087 Expr->Type = Field->Type;
1089 /* An struct member is actually a variable. So the rules for variables
1090 * with respect to the reference type apply: If it's an array, it is
1091 * a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1092 * but a struct field cannot be a function).
1094 if (IsTypeArray (Expr->Type)) {
1103 static void hie11 (ExprDesc *Expr)
1104 /* Handle compound types (structs and arrays) */
1106 /* Name value used in invalid function calls */
1107 static const char IllegalFunc[] = "illegal_function_call";
1109 /* Evaluate the lhs */
1112 /* Check for a rhs */
1113 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN ||
1114 CurTok.Tok == TOK_DOT || CurTok.Tok == TOK_PTR_REF) {
1116 switch (CurTok.Tok) {
1119 /* Array reference */
1124 /* Function call. */
1125 if (!IsTypeFunc (Expr->Type) && !IsTypeFuncPtr (Expr->Type)) {
1126 /* Not a function */
1127 Error ("Illegal function call");
1128 /* Force the type to be a implicitly defined function, one
1129 * returning an int and taking any number of arguments.
1130 * Since we don't have a name, invent one.
1132 ED_MakeConstAbs (Expr, 0, GetImplicitFuncType ());
1133 Expr->Name = (long) IllegalFunc;
1135 /* Call the function */
1136 FunctionCall (Expr);
1140 if (!IsClassStruct (Expr->Type)) {
1141 Error ("Struct expected");
1147 /* If we have an array, convert it to pointer to first element */
1148 if (IsTypeArray (Expr->Type)) {
1149 Expr->Type = ArrayToPtr (Expr->Type);
1151 if (!IsClassPtr (Expr->Type) || !IsClassStruct (Indirect (Expr->Type))) {
1152 Error ("Struct pointer expected");
1158 Internal ("Invalid token in hie11: %d", CurTok.Tok);
1166 void Store (ExprDesc* Expr, const Type* StoreType)
1167 /* Store the primary register into the location denoted by Expr. If StoreType
1168 * is given, use this type when storing instead of Expr->Type. If StoreType
1169 * is NULL, use Expr->Type instead.
1174 /* If StoreType was not given, use Expr->Type instead */
1175 if (StoreType == 0) {
1176 StoreType = Expr->Type;
1179 /* Prepare the code generator flags */
1180 Flags = TypeOf (StoreType) | GlobalModeFlags (Expr);
1182 /* Do the store depending on the location */
1183 switch (ED_GetLoc (Expr)) {
1186 /* Absolute: numeric address or const */
1187 g_putstatic (Flags, Expr->IVal, 0);
1191 /* Global variable */
1192 g_putstatic (Flags, Expr->Name, Expr->IVal);
1197 /* Static variable or literal in the literal pool */
1198 g_putstatic (Flags, Expr->Name, Expr->IVal);
1201 case E_LOC_REGISTER:
1202 /* Register variable */
1203 g_putstatic (Flags, Expr->Name, Expr->IVal);
1207 /* Value on the stack */
1208 g_putlocal (Flags, Expr->IVal, 0);
1212 /* The primary register (value is already there) */
1216 /* An expression in the primary register */
1217 g_putind (Flags, Expr->IVal);
1221 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
1224 /* Assume that each one of the stores will invalidate CC */
1225 ED_MarkAsUntested (Expr);
1230 static void PreInc (ExprDesc* Expr)
1231 /* Handle the preincrement operators */
1236 /* Skip the operator token */
1239 /* Evaluate the expression and check that it is an lvalue */
1241 if (!ED_IsLVal (Expr)) {
1242 Error ("Invalid lvalue");
1246 /* We cannot modify const values */
1247 if (IsQualConst (Expr->Type)) {
1248 Error ("Increment of read-only variable");
1251 /* Get the data type */
1252 Flags = TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR | CF_CONST;
1254 /* Get the increment value in bytes */
1255 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1257 /* Check the location of the data */
1258 switch (ED_GetLoc (Expr)) {
1261 /* Absolute: numeric address or const */
1262 g_addeqstatic (Flags, Expr->IVal, 0, Val);
1266 /* Global variable */
1267 g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1272 /* Static variable or literal in the literal pool */
1273 g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1276 case E_LOC_REGISTER:
1277 /* Register variable */
1278 g_addeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1282 /* Value on the stack */
1283 g_addeqlocal (Flags, Expr->IVal, Val);
1287 /* The primary register */
1292 /* An expression in the primary register */
1293 g_addeqind (Flags, Expr->IVal, Val);
1297 Internal ("Invalid location in PreInc(): 0x%04X", ED_GetLoc (Expr));
1300 /* Result is an expression, no reference */
1301 ED_MakeRValExpr (Expr);
1306 static void PreDec (ExprDesc* Expr)
1307 /* Handle the predecrement operators */
1312 /* Skip the operator token */
1315 /* Evaluate the expression and check that it is an lvalue */
1317 if (!ED_IsLVal (Expr)) {
1318 Error ("Invalid lvalue");
1322 /* We cannot modify const values */
1323 if (IsQualConst (Expr->Type)) {
1324 Error ("Decrement of read-only variable");
1327 /* Get the data type */
1328 Flags = TypeOf (Expr->Type) | GlobalModeFlags (Expr) | CF_FORCECHAR | CF_CONST;
1330 /* Get the increment value in bytes */
1331 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1333 /* Check the location of the data */
1334 switch (ED_GetLoc (Expr)) {
1337 /* Absolute: numeric address or const */
1338 g_subeqstatic (Flags, Expr->IVal, 0, Val);
1342 /* Global variable */
1343 g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1348 /* Static variable or literal in the literal pool */
1349 g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1352 case E_LOC_REGISTER:
1353 /* Register variable */
1354 g_subeqstatic (Flags, Expr->Name, Expr->IVal, Val);
1358 /* Value on the stack */
1359 g_subeqlocal (Flags, Expr->IVal, Val);
1363 /* The primary register */
1368 /* An expression in the primary register */
1369 g_subeqind (Flags, Expr->IVal, Val);
1373 Internal ("Invalid location in PreDec(): 0x%04X", ED_GetLoc (Expr));
1376 /* Result is an expression, no reference */
1377 ED_MakeRValExpr (Expr);
1382 static void PostInc (ExprDesc* Expr)
1383 /* Handle the postincrement operator */
1389 /* The expression to increment must be an lvalue */
1390 if (!ED_IsLVal (Expr)) {
1391 Error ("Invalid lvalue");
1395 /* We cannot modify const values */
1396 if (IsQualConst (Expr->Type)) {
1397 Error ("Increment of read-only variable");
1400 /* Get the data type */
1401 Flags = TypeOf (Expr->Type);
1403 /* Push the address if needed */
1406 /* Fetch the value and save it (since it's the result of the expression) */
1407 LoadExpr (CF_NONE, Expr);
1408 g_save (Flags | CF_FORCECHAR);
1410 /* If we have a pointer expression, increment by the size of the type */
1411 if (IsTypePtr (Expr->Type)) {
1412 g_inc (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1414 g_inc (Flags | CF_CONST | CF_FORCECHAR, 1);
1417 /* Store the result back */
1420 /* Restore the original value in the primary register */
1421 g_restore (Flags | CF_FORCECHAR);
1423 /* The result is always an expression, no reference */
1424 ED_MakeRValExpr (Expr);
1429 static void PostDec (ExprDesc* Expr)
1430 /* Handle the postdecrement operator */
1436 /* The expression to increment must be an lvalue */
1437 if (!ED_IsLVal (Expr)) {
1438 Error ("Invalid lvalue");
1442 /* We cannot modify const values */
1443 if (IsQualConst (Expr->Type)) {
1444 Error ("Decrement of read-only variable");
1447 /* Get the data type */
1448 Flags = TypeOf (Expr->Type);
1450 /* Push the address if needed */
1453 /* Fetch the value and save it (since it's the result of the expression) */
1454 LoadExpr (CF_NONE, Expr);
1455 g_save (Flags | CF_FORCECHAR);
1457 /* If we have a pointer expression, increment by the size of the type */
1458 if (IsTypePtr (Expr->Type)) {
1459 g_dec (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1461 g_dec (Flags | CF_CONST | CF_FORCECHAR, 1);
1464 /* Store the result back */
1467 /* Restore the original value in the primary register */
1468 g_restore (Flags | CF_FORCECHAR);
1470 /* The result is always an expression, no reference */
1471 ED_MakeRValExpr (Expr);
1476 static void UnaryOp (ExprDesc* Expr)
1477 /* Handle unary -/+ and ~ */
1481 /* Remember the operator token and skip it */
1482 token_t Tok = CurTok.Tok;
1485 /* Get the expression */
1488 /* We can only handle integer types */
1489 if (!IsClassInt (Expr->Type)) {
1490 Error ("Argument must have integer type");
1491 ED_MakeConstAbsInt (Expr, 1);
1494 /* Check for a constant expression */
1495 if (ED_IsConstAbs (Expr)) {
1496 /* Value is constant */
1498 case TOK_MINUS: Expr->IVal = -Expr->IVal; break;
1499 case TOK_PLUS: break;
1500 case TOK_COMP: Expr->IVal = ~Expr->IVal; break;
1501 default: Internal ("Unexpected token: %d", Tok);
1504 /* Value is not constant */
1505 LoadExpr (CF_NONE, Expr);
1507 /* Get the type of the expression */
1508 Flags = TypeOf (Expr->Type);
1510 /* Handle the operation */
1512 case TOK_MINUS: g_neg (Flags); break;
1513 case TOK_PLUS: break;
1514 case TOK_COMP: g_com (Flags); break;
1515 default: Internal ("Unexpected token: %d", Tok);
1518 /* The result is a rvalue in the primary */
1519 ED_MakeRValExpr (Expr);
1525 void hie10 (ExprDesc* Expr)
1526 /* Handle ++, --, !, unary - etc. */
1530 switch (CurTok.Tok) {
1548 if (evalexpr (CF_NONE, hie10, Expr) == 0) {
1549 /* Constant expression */
1550 Expr->IVal = !Expr->IVal;
1552 g_bneg (TypeOf (Expr->Type));
1553 ED_MakeRValExpr (Expr);
1554 ED_TestDone (Expr); /* bneg will set cc */
1560 ExprWithCheck (hie10, Expr);
1561 if (ED_IsLVal (Expr) || !(ED_IsLocConst (Expr) || ED_IsLocStack (Expr))) {
1562 /* Not a const, load it into the primary and make it a
1565 LoadExpr (CF_NONE, Expr);
1566 ED_MakeRValExpr (Expr);
1568 /* If the expression is already a pointer to function, the
1569 * additional dereferencing operator must be ignored.
1571 if (IsTypeFuncPtr (Expr->Type)) {
1572 /* Expression not storable */
1575 if (IsClassPtr (Expr->Type)) {
1576 Expr->Type = Indirect (Expr->Type);
1578 Error ("Illegal indirection");
1580 /* The * operator yields an lvalue */
1587 ExprWithCheck (hie10, Expr);
1588 /* The & operator may be applied to any lvalue, and it may be
1589 * applied to functions, even if they're no lvalues.
1591 if (ED_IsRVal (Expr) && !IsTypeFunc (Expr->Type) && !IsTypeArray (Expr->Type)) {
1592 Error ("Illegal address");
1594 Expr->Type = PointerTo (Expr->Type);
1595 /* The & operator yields an rvalue */
1602 if (TypeSpecAhead ()) {
1605 Size = CheckedSizeOf (ParseType (T));
1608 /* Remember the output queue pointer */
1612 Size = CheckedSizeOf (Expr->Type);
1613 /* Remove any generated code */
1616 ED_MakeConstAbs (Expr, Size, type_size_t);
1617 ED_MarkAsUntested (Expr);
1621 if (TypeSpecAhead ()) {
1631 /* Handle post increment */
1632 switch (CurTok.Tok) {
1633 case TOK_INC: PostInc (Expr); break;
1634 case TOK_DEC: PostDec (Expr); break;
1645 static void hie_internal (const GenDesc* Ops, /* List of generators */
1647 void (*hienext) (ExprDesc*),
1649 /* Helper function */
1655 token_t Tok; /* The operator token */
1656 unsigned ltype, type;
1657 int rconst; /* Operand is a constant */
1663 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1665 /* Tell the caller that we handled it's ops */
1668 /* All operators that call this function expect an int on the lhs */
1669 if (!IsClassInt (Expr->Type)) {
1670 Error ("Integer expression expected");
1671 /* To avoid further errors, make Expr a valid int expression */
1672 ED_MakeConstAbsInt (Expr, 1);
1675 /* Remember the operator token, then skip it */
1679 /* Get the lhs on stack */
1680 GetCodePos (&Mark1);
1681 ltype = TypeOf (Expr->Type);
1682 if (ED_IsConstAbs (Expr)) {
1683 /* Constant value */
1684 GetCodePos (&Mark2);
1685 g_push (ltype | CF_CONST, Expr->IVal);
1687 /* Value not constant */
1688 LoadExpr (CF_NONE, Expr);
1689 GetCodePos (&Mark2);
1693 /* Get the right hand side */
1694 rconst = (evalexpr (CF_NONE, hienext, &Expr2) == 0);
1696 /* Check the type of the rhs */
1697 if (!IsClassInt (Expr2.Type)) {
1698 Error ("Integer expression expected");
1701 /* Check for const operands */
1702 if (ED_IsConstAbs (Expr) && rconst) {
1704 /* Both operands are constant, remove the generated code */
1705 RemoveCode (&Mark1);
1707 /* Get the type of the result */
1708 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1710 /* Handle the op differently for signed and unsigned types */
1711 if (IsSignSigned (Expr->Type)) {
1713 /* Evaluate the result for signed operands */
1714 signed long Val1 = Expr->IVal;
1715 signed long Val2 = Expr2.IVal;
1718 Expr->IVal = (Val1 | Val2);
1721 Expr->IVal = (Val1 ^ Val2);
1724 Expr->IVal = (Val1 & Val2);
1727 Expr->IVal = (Val1 * Val2);
1731 Error ("Division by zero");
1732 Expr->IVal = 0x7FFFFFFF;
1734 Expr->IVal = (Val1 / Val2);
1739 Error ("Modulo operation with zero");
1742 Expr->IVal = (Val1 % Val2);
1746 Internal ("hie_internal: got token 0x%X\n", Tok);
1750 /* Evaluate the result for unsigned operands */
1751 unsigned long Val1 = Expr->IVal;
1752 unsigned long Val2 = Expr2.IVal;
1755 Expr->IVal = (Val1 | Val2);
1758 Expr->IVal = (Val1 ^ Val2);
1761 Expr->IVal = (Val1 & Val2);
1764 Expr->IVal = (Val1 * Val2);
1768 Error ("Division by zero");
1769 Expr->IVal = 0xFFFFFFFF;
1771 Expr->IVal = (Val1 / Val2);
1776 Error ("Modulo operation with zero");
1779 Expr->IVal = (Val1 % Val2);
1783 Internal ("hie_internal: got token 0x%X\n", Tok);
1789 /* If the right hand side is constant, and the generator function
1790 * expects the lhs in the primary, remove the push of the primary
1793 unsigned rtype = TypeOf (Expr2.Type);
1796 /* Second value is constant - check for div */
1799 if (Tok == TOK_DIV && Expr2.IVal == 0) {
1800 Error ("Division by zero");
1801 } else if (Tok == TOK_MOD && Expr2.IVal == 0) {
1802 Error ("Modulo operation with zero");
1804 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1805 RemoveCode (&Mark2);
1806 ltype |= CF_REG; /* Value is in register */
1810 /* Determine the type of the operation result. */
1811 type |= g_typeadjust (ltype, rtype);
1812 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1815 Gen->Func (type, Expr2.IVal);
1817 /* We have a rvalue in the primary now */
1818 ED_MakeRValExpr (Expr);
1825 static void hie_compare (const GenDesc* Ops, /* List of generators */
1827 void (*hienext) (ExprDesc*))
1828 /* Helper function for the compare operators */
1834 token_t Tok; /* The operator token */
1836 int rconst; /* Operand is a constant */
1841 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1843 /* Remember the operator token, then skip it */
1847 /* Get the lhs on stack */
1848 GetCodePos (&Mark1);
1849 ltype = TypeOf (Expr->Type);
1850 if (ED_IsConstAbs (Expr)) {
1851 /* Constant value */
1852 GetCodePos (&Mark2);
1853 g_push (ltype | CF_CONST, Expr->IVal);
1855 /* Value not constant */
1856 LoadExpr (CF_NONE, Expr);
1857 GetCodePos (&Mark2);
1861 /* Get the right hand side */
1862 rconst = (evalexpr (CF_NONE, hienext, &Expr2) == 0);
1864 /* Make sure, the types are compatible */
1865 if (IsClassInt (Expr->Type)) {
1866 if (!IsClassInt (Expr2.Type) && !(IsClassPtr(Expr2.Type) && ED_IsNullPtr(Expr))) {
1867 Error ("Incompatible types");
1869 } else if (IsClassPtr (Expr->Type)) {
1870 if (IsClassPtr (Expr2.Type)) {
1871 /* Both pointers are allowed in comparison if they point to
1872 * the same type, or if one of them is a void pointer.
1874 Type* left = Indirect (Expr->Type);
1875 Type* right = Indirect (Expr2.Type);
1876 if (TypeCmp (left, right) < TC_EQUAL && left->C != T_VOID && right->C != T_VOID) {
1877 /* Incomatible pointers */
1878 Error ("Incompatible types");
1880 } else if (!ED_IsNullPtr (&Expr2)) {
1881 Error ("Incompatible types");
1885 /* Check for const operands */
1886 if (ED_IsConstAbs (Expr) && rconst) {
1888 /* Both operands are constant, remove the generated code */
1889 RemoveCode (&Mark1);
1891 /* Determine if this is a signed or unsigned compare */
1892 if (IsClassInt (Expr->Type) && IsSignSigned (Expr->Type) &&
1893 IsClassInt (Expr2.Type) && IsSignSigned (Expr2.Type)) {
1895 /* Evaluate the result for signed operands */
1896 signed long Val1 = Expr->IVal;
1897 signed long Val2 = Expr2.IVal;
1899 case TOK_EQ: Expr->IVal = (Val1 == Val2); break;
1900 case TOK_NE: Expr->IVal = (Val1 != Val2); break;
1901 case TOK_LT: Expr->IVal = (Val1 < Val2); break;
1902 case TOK_LE: Expr->IVal = (Val1 <= Val2); break;
1903 case TOK_GE: Expr->IVal = (Val1 >= Val2); break;
1904 case TOK_GT: Expr->IVal = (Val1 > Val2); break;
1905 default: Internal ("hie_compare: got token 0x%X\n", Tok);
1910 /* Evaluate the result for unsigned operands */
1911 unsigned long Val1 = Expr->IVal;
1912 unsigned long Val2 = Expr2.IVal;
1914 case TOK_EQ: Expr->IVal = (Val1 == Val2); break;
1915 case TOK_NE: Expr->IVal = (Val1 != Val2); break;
1916 case TOK_LT: Expr->IVal = (Val1 < Val2); break;
1917 case TOK_LE: Expr->IVal = (Val1 <= Val2); break;
1918 case TOK_GE: Expr->IVal = (Val1 >= Val2); break;
1919 case TOK_GT: Expr->IVal = (Val1 > Val2); break;
1920 default: Internal ("hie_compare: got token 0x%X\n", Tok);
1926 /* If the right hand side is constant, and the generator function
1927 * expects the lhs in the primary, remove the push of the primary
1933 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1934 RemoveCode (&Mark2);
1935 ltype |= CF_REG; /* Value is in register */
1939 /* Determine the type of the operation result. If the left
1940 * operand is of type char and the right is a constant, or
1941 * if both operands are of type char, we will encode the
1942 * operation as char operation. Otherwise the default
1943 * promotions are used.
1945 if (IsTypeChar (Expr->Type) && (IsTypeChar (Expr2.Type) || rconst)) {
1947 if (IsSignUnsigned (Expr->Type) || IsSignUnsigned (Expr2.Type)) {
1948 flags |= CF_UNSIGNED;
1951 flags |= CF_FORCECHAR;
1954 unsigned rtype = TypeOf (Expr2.Type) | (flags & CF_CONST);
1955 flags |= g_typeadjust (ltype, rtype);
1959 Gen->Func (flags, Expr2.IVal);
1961 /* The result is an rvalue in the primary */
1962 ED_MakeRValExpr (Expr);
1965 /* Result type is always int */
1966 Expr->Type = type_int;
1968 /* Condition codes are set */
1975 static void hie9 (ExprDesc *Expr)
1976 /* Process * and / operators. */
1978 static const GenDesc hie9_ops[] = {
1979 { TOK_STAR, GEN_NOPUSH, g_mul },
1980 { TOK_DIV, GEN_NOPUSH, g_div },
1981 { TOK_MOD, GEN_NOPUSH, g_mod },
1982 { TOK_INVALID, 0, 0 }
1986 hie_internal (hie9_ops, Expr, hie10, &UsedGen);
1991 static void parseadd (ExprDesc* Expr)
1992 /* Parse an expression with the binary plus operator. Expr contains the
1993 * unprocessed left hand side of the expression and will contain the
1994 * result of the expression on return.
1998 unsigned flags; /* Operation flags */
1999 CodeMark Mark; /* Remember code position */
2000 Type* lhst; /* Type of left hand side */
2001 Type* rhst; /* Type of right hand side */
2004 /* Skip the PLUS token */
2007 /* Get the left hand side type, initialize operation flags */
2011 /* Check for constness on both sides */
2012 if (ED_IsConst (Expr)) {
2014 /* The left hand side is a constant of some sort. Good. Get rhs */
2016 if (ED_IsConstAbs (&Expr2)) {
2018 /* Right hand side is a constant numeric value. Get the rhs type */
2021 /* Both expressions are constants. Check for pointer arithmetic */
2022 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2023 /* Left is pointer, right is int, must scale rhs */
2024 Expr->IVal += Expr2.IVal * CheckedPSizeOf (lhst);
2025 /* Result type is a pointer */
2026 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2027 /* Left is int, right is pointer, must scale lhs */
2028 Expr->IVal = Expr->IVal * CheckedPSizeOf (rhst) + Expr2.IVal;
2029 /* Result type is a pointer */
2030 Expr->Type = Expr2.Type;
2031 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2032 /* Integer addition */
2033 Expr->IVal += Expr2.IVal;
2034 typeadjust (Expr, &Expr2, 1);
2037 Error ("Invalid operands for binary operator `+'");
2042 /* lhs is a constant and rhs is not constant. Load rhs into
2045 LoadExpr (CF_NONE, &Expr2);
2047 /* Beware: The check above (for lhs) lets not only pass numeric
2048 * constants, but also constant addresses (labels), maybe even
2049 * with an offset. We have to check for that here.
2052 /* First, get the rhs type. */
2056 if (ED_IsLocAbs (Expr)) {
2057 /* A numerical constant */
2060 /* Constant address label */
2061 flags |= GlobalModeFlags (Expr) | CF_CONSTADDR;
2064 /* Check for pointer arithmetic */
2065 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2066 /* Left is pointer, right is int, must scale rhs */
2067 g_scale (CF_INT, CheckedPSizeOf (lhst));
2068 /* Operate on pointers, result type is a pointer */
2070 /* Generate the code for the add */
2071 if (ED_GetLoc (Expr) == E_LOC_ABS) {
2072 /* Numeric constant */
2073 g_inc (flags, Expr->IVal);
2075 /* Constant address */
2076 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2078 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2080 /* Left is int, right is pointer, must scale lhs. */
2081 unsigned ScaleFactor = CheckedPSizeOf (rhst);
2083 /* Operate on pointers, result type is a pointer */
2085 Expr->Type = Expr2.Type;
2087 /* Since we do already have rhs in the primary, if lhs is
2088 * not a numeric constant, and the scale factor is not one
2089 * (no scaling), we must take the long way over the stack.
2091 if (ED_IsLocAbs (Expr)) {
2092 /* Numeric constant, scale lhs */
2093 Expr->IVal *= ScaleFactor;
2094 /* Generate the code for the add */
2095 g_inc (flags, Expr->IVal);
2096 } else if (ScaleFactor == 1) {
2097 /* Constant address but no need to scale */
2098 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2100 /* Constant address that must be scaled */
2101 g_push (TypeOf (Expr2.Type), 0); /* rhs --> stack */
2102 g_getimmed (flags, Expr->Name, Expr->IVal);
2103 g_scale (CF_PTR, ScaleFactor);
2106 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2107 /* Integer addition */
2108 flags |= typeadjust (Expr, &Expr2, 1);
2109 /* Generate the code for the add */
2110 if (ED_IsLocAbs (Expr)) {
2111 /* Numeric constant */
2112 g_inc (flags, Expr->IVal);
2114 /* Constant address */
2115 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2119 Error ("Invalid operands for binary operator `+'");
2123 /* Result is a rvalue in primary register */
2124 ED_MakeRValExpr (Expr);
2129 /* Left hand side is not constant. Get the value onto the stack. */
2130 LoadExpr (CF_NONE, Expr); /* --> primary register */
2132 g_push (TypeOf (Expr->Type), 0); /* --> stack */
2134 /* Evaluate the rhs */
2135 if (evalexpr (CF_NONE, hie9, &Expr2) == 0) {
2137 /* Right hand side is a constant. Get the rhs type */
2140 /* Remove pushed value from stack */
2143 /* Check for pointer arithmetic */
2144 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2145 /* Left is pointer, right is int, must scale rhs */
2146 Expr2.IVal *= CheckedPSizeOf (lhst);
2147 /* Operate on pointers, result type is a pointer */
2149 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2150 /* Left is int, right is pointer, must scale lhs (ptr only) */
2151 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2152 /* Operate on pointers, result type is a pointer */
2154 Expr->Type = Expr2.Type;
2155 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2156 /* Integer addition */
2157 flags = typeadjust (Expr, &Expr2, 1);
2160 Error ("Invalid operands for binary operator `+'");
2164 /* Generate code for the add */
2165 g_inc (flags | CF_CONST, Expr2.IVal);
2169 /* lhs and rhs are not constant. Get the rhs type. */
2172 /* Check for pointer arithmetic */
2173 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2174 /* Left is pointer, right is int, must scale rhs */
2175 g_scale (CF_INT, CheckedPSizeOf (lhst));
2176 /* Operate on pointers, result type is a pointer */
2178 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2179 /* Left is int, right is pointer, must scale lhs */
2180 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2181 g_swap (CF_INT); /* Swap TOS and primary */
2182 g_scale (CF_INT, CheckedPSizeOf (rhst));
2183 /* Operate on pointers, result type is a pointer */
2185 Expr->Type = Expr2.Type;
2186 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2187 /* Integer addition. Note: Result is never constant.
2188 * Problem here is that typeadjust does not know if the
2189 * variable is an rvalue or lvalue, so if both operands
2190 * are dereferenced constant numeric addresses, typeadjust
2191 * thinks the operation works on constants. Removing
2192 * CF_CONST here means handling the symptoms, however, the
2193 * whole parser is such a mess that I fear to break anything
2194 * when trying to apply another solution.
2196 flags = typeadjust (Expr, &Expr2, 0) & ~CF_CONST;
2199 Error ("Invalid operands for binary operator `+'");
2203 /* Generate code for the add */
2208 /* Result is a rvalue in primary register */
2209 ED_MakeRValExpr (Expr);
2212 /* Condition codes not set */
2213 ED_MarkAsUntested (Expr);
2219 static void parsesub (ExprDesc* Expr)
2220 /* Parse an expression with the binary minus operator. Expr contains the
2221 * unprocessed left hand side of the expression and will contain the
2222 * result of the expression on return.
2226 unsigned flags; /* Operation flags */
2227 Type* lhst; /* Type of left hand side */
2228 Type* rhst; /* Type of right hand side */
2229 CodeMark Mark1; /* Save position of output queue */
2230 CodeMark Mark2; /* Another position in the queue */
2231 int rscale; /* Scale factor for the result */
2234 /* Skip the MINUS token */
2237 /* Get the left hand side type, initialize operation flags */
2239 rscale = 1; /* Scale by 1, that is, don't scale */
2241 /* Remember the output queue position, then bring the value onto the stack */
2242 GetCodePos (&Mark1);
2243 LoadExpr (CF_NONE, Expr); /* --> primary register */
2244 GetCodePos (&Mark2);
2245 g_push (TypeOf (lhst), 0); /* --> stack */
2247 /* Parse the right hand side */
2248 if (evalexpr (CF_NONE, hie9, &Expr2) == 0) {
2250 /* The right hand side is constant. Get the rhs type. */
2253 /* Check left hand side */
2254 if (ED_IsConstAbs (Expr)) {
2256 /* Both sides are constant, remove generated code */
2257 RemoveCode (&Mark1);
2259 /* Check for pointer arithmetic */
2260 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2261 /* Left is pointer, right is int, must scale rhs */
2262 Expr->IVal -= Expr2.IVal * CheckedPSizeOf (lhst);
2263 /* Operate on pointers, result type is a pointer */
2264 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2265 /* Left is pointer, right is pointer, must scale result */
2266 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2267 Error ("Incompatible pointer types");
2269 Expr->IVal = (Expr->IVal - Expr2.IVal) /
2270 CheckedPSizeOf (lhst);
2272 /* Operate on pointers, result type is an integer */
2273 Expr->Type = type_int;
2274 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2275 /* Integer subtraction */
2276 typeadjust (Expr, &Expr2, 1);
2277 Expr->IVal -= Expr2.IVal;
2280 Error ("Invalid operands for binary operator `-'");
2283 /* Result is constant, condition codes not set */
2284 ED_MarkAsUntested (Expr);
2288 /* Left hand side is not constant, right hand side is.
2289 * Remove pushed value from stack.
2291 RemoveCode (&Mark2);
2293 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2294 /* Left is pointer, right is int, must scale rhs */
2295 Expr2.IVal *= CheckedPSizeOf (lhst);
2296 /* Operate on pointers, result type is a pointer */
2298 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2299 /* Left is pointer, right is pointer, must scale result */
2300 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2301 Error ("Incompatible pointer types");
2303 rscale = CheckedPSizeOf (lhst);
2305 /* Operate on pointers, result type is an integer */
2307 Expr->Type = type_int;
2308 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2309 /* Integer subtraction */
2310 flags = typeadjust (Expr, &Expr2, 1);
2313 Error ("Invalid operands for binary operator `-'");
2317 /* Do the subtraction */
2318 g_dec (flags | CF_CONST, Expr2.IVal);
2320 /* If this was a pointer subtraction, we must scale the result */
2322 g_scale (flags, -rscale);
2325 /* Result is a rvalue in the primary register */
2326 ED_MakeRValExpr (Expr);
2327 ED_MarkAsUntested (Expr);
2333 /* Right hand side is not constant. Get the rhs type. */
2336 /* Check for pointer arithmetic */
2337 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2338 /* Left is pointer, right is int, must scale rhs */
2339 g_scale (CF_INT, CheckedPSizeOf (lhst));
2340 /* Operate on pointers, result type is a pointer */
2342 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2343 /* Left is pointer, right is pointer, must scale result */
2344 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2345 Error ("Incompatible pointer types");
2347 rscale = CheckedPSizeOf (lhst);
2349 /* Operate on pointers, result type is an integer */
2351 Expr->Type = type_int;
2352 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2353 /* Integer subtraction. If the left hand side descriptor says that
2354 * the lhs is const, we have to remove this mark, since this is no
2355 * longer true, lhs is on stack instead.
2357 if (ED_IsLocAbs (Expr)) {
2358 ED_MakeRValExpr (Expr);
2360 /* Adjust operand types */
2361 flags = typeadjust (Expr, &Expr2, 0);
2364 Error ("Invalid operands for binary operator `-'");
2368 /* Generate code for the sub (the & is a hack here) */
2369 g_sub (flags & ~CF_CONST, 0);
2371 /* If this was a pointer subtraction, we must scale the result */
2373 g_scale (flags, -rscale);
2376 /* Result is a rvalue in the primary register */
2377 ED_MakeRValExpr (Expr);
2378 ED_MarkAsUntested (Expr);
2384 void hie8 (ExprDesc* Expr)
2385 /* Process + and - binary operators. */
2388 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2389 if (CurTok.Tok == TOK_PLUS) {
2399 static void hie6 (ExprDesc* Expr)
2400 /* Handle greater-than type comparators */
2402 static const GenDesc hie6_ops [] = {
2403 { TOK_LT, GEN_NOPUSH, g_lt },
2404 { TOK_LE, GEN_NOPUSH, g_le },
2405 { TOK_GE, GEN_NOPUSH, g_ge },
2406 { TOK_GT, GEN_NOPUSH, g_gt },
2407 { TOK_INVALID, 0, 0 }
2409 hie_compare (hie6_ops, Expr, ShiftExpr);
2414 static void hie5 (ExprDesc* Expr)
2415 /* Handle == and != */
2417 static const GenDesc hie5_ops[] = {
2418 { TOK_EQ, GEN_NOPUSH, g_eq },
2419 { TOK_NE, GEN_NOPUSH, g_ne },
2420 { TOK_INVALID, 0, 0 }
2422 hie_compare (hie5_ops, Expr, hie6);
2427 static void hie4 (ExprDesc* Expr)
2428 /* Handle & (bitwise and) */
2430 static const GenDesc hie4_ops[] = {
2431 { TOK_AND, GEN_NOPUSH, g_and },
2432 { TOK_INVALID, 0, 0 }
2436 hie_internal (hie4_ops, Expr, hie5, &UsedGen);
2441 static void hie3 (ExprDesc* Expr)
2442 /* Handle ^ (bitwise exclusive or) */
2444 static const GenDesc hie3_ops[] = {
2445 { TOK_XOR, GEN_NOPUSH, g_xor },
2446 { TOK_INVALID, 0, 0 }
2450 hie_internal (hie3_ops, Expr, hie4, &UsedGen);
2455 static void hie2 (ExprDesc* Expr)
2456 /* Handle | (bitwise or) */
2458 static const GenDesc hie2_ops[] = {
2459 { TOK_OR, GEN_NOPUSH, g_or },
2460 { TOK_INVALID, 0, 0 }
2464 hie_internal (hie2_ops, Expr, hie3, &UsedGen);
2469 static void hieAndPP (ExprDesc* Expr)
2470 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2471 * called recursively from the preprocessor.
2476 ConstAbsIntExpr (hie2, Expr);
2477 while (CurTok.Tok == TOK_BOOL_AND) {
2483 ConstAbsIntExpr (hie2, &Expr2);
2485 /* Combine the two */
2486 Expr->IVal = (Expr->IVal && Expr2.IVal);
2492 static void hieOrPP (ExprDesc *Expr)
2493 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2494 * called recursively from the preprocessor.
2499 ConstAbsIntExpr (hieAndPP, Expr);
2500 while (CurTok.Tok == TOK_BOOL_OR) {
2506 ConstAbsIntExpr (hieAndPP, &Expr2);
2508 /* Combine the two */
2509 Expr->IVal = (Expr->IVal || Expr2.IVal);
2515 static void hieAnd (ExprDesc* Expr, unsigned TrueLab, int* BoolOp)
2516 /* Process "exp && exp" */
2522 if (CurTok.Tok == TOK_BOOL_AND) {
2524 /* Tell our caller that we're evaluating a boolean */
2527 /* Get a label that we will use for false expressions */
2528 lab = GetLocalLabel ();
2530 /* If the expr hasn't set condition codes, set the force-test flag */
2531 if (!ED_IsTested (Expr)) {
2532 ED_MarkForTest (Expr);
2535 /* Load the value */
2536 LoadExpr (CF_FORCECHAR, Expr);
2538 /* Generate the jump */
2539 g_falsejump (CF_NONE, lab);
2541 /* Parse more boolean and's */
2542 while (CurTok.Tok == TOK_BOOL_AND) {
2549 if (!ED_IsTested (&Expr2)) {
2550 ED_MarkForTest (&Expr2);
2552 LoadExpr (CF_FORCECHAR, &Expr2);
2554 /* Do short circuit evaluation */
2555 if (CurTok.Tok == TOK_BOOL_AND) {
2556 g_falsejump (CF_NONE, lab);
2558 /* Last expression - will evaluate to true */
2559 g_truejump (CF_NONE, TrueLab);
2563 /* Define the false jump label here */
2564 g_defcodelabel (lab);
2566 /* The result is an rvalue in primary */
2567 ED_MakeRValExpr (Expr);
2568 ED_TestDone (Expr); /* Condition codes are set */
2574 static void hieOr (ExprDesc *Expr)
2575 /* Process "exp || exp". */
2578 int BoolOp = 0; /* Did we have a boolean op? */
2579 int AndOp; /* Did we have a && operation? */
2580 unsigned TrueLab; /* Jump to this label if true */
2584 TrueLab = GetLocalLabel ();
2586 /* Call the next level parser */
2587 hieAnd (Expr, TrueLab, &BoolOp);
2589 /* Any boolean or's? */
2590 if (CurTok.Tok == TOK_BOOL_OR) {
2592 /* If the expr hasn't set condition codes, set the force-test flag */
2593 if (!ED_IsTested (Expr)) {
2594 ED_MarkForTest (Expr);
2597 /* Get first expr */
2598 LoadExpr (CF_FORCECHAR, Expr);
2600 /* For each expression jump to TrueLab if true. Beware: If we
2601 * had && operators, the jump is already in place!
2604 g_truejump (CF_NONE, TrueLab);
2607 /* Remember that we had a boolean op */
2610 /* while there's more expr */
2611 while (CurTok.Tok == TOK_BOOL_OR) {
2618 hieAnd (&Expr2, TrueLab, &AndOp);
2619 if (!ED_IsTested (&Expr2)) {
2620 ED_MarkForTest (&Expr2);
2622 LoadExpr (CF_FORCECHAR, &Expr2);
2624 /* If there is more to come, add shortcut boolean eval. */
2625 g_truejump (CF_NONE, TrueLab);
2629 /* The result is an rvalue in primary */
2630 ED_MakeRValExpr (Expr);
2631 ED_TestDone (Expr); /* Condition codes are set */
2634 /* If we really had boolean ops, generate the end sequence */
2636 DoneLab = GetLocalLabel ();
2637 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2638 g_falsejump (CF_NONE, DoneLab);
2639 g_defcodelabel (TrueLab);
2640 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2641 g_defcodelabel (DoneLab);
2647 static void hieQuest (ExprDesc* Expr)
2648 /* Parse the ternary operator */
2652 ExprDesc Expr2; /* Expression 2 */
2653 ExprDesc Expr3; /* Expression 3 */
2654 int Expr2IsNULL; /* Expression 2 is a NULL pointer */
2655 int Expr3IsNULL; /* Expression 3 is a NULL pointer */
2656 Type* ResultType; /* Type of result */
2659 /* Call the lower level eval routine */
2660 if (Preprocessing) {
2666 /* Check if it's a ternary expression */
2667 if (CurTok.Tok == TOK_QUEST) {
2669 if (!ED_IsTested (Expr)) {
2670 /* Condition codes not set, request a test */
2671 ED_MarkForTest (Expr);
2673 LoadExpr (CF_NONE, Expr);
2674 labf = GetLocalLabel ();
2675 g_falsejump (CF_NONE, labf);
2677 /* Parse second expression. Remember for later if it is a NULL pointer
2678 * expression, then load it into the primary.
2680 ExprWithCheck (hie1, &Expr2);
2681 Expr2IsNULL = ED_IsNullPtr (&Expr2);
2682 if (!IsTypeVoid (Expr2.Type)) {
2683 /* Load it into the primary */
2684 LoadExpr (CF_NONE, &Expr2);
2685 ED_MakeRValExpr (&Expr2);
2686 Expr2.Type = PtrConversion (Expr2.Type);
2688 labt = GetLocalLabel ();
2692 /* Jump here if the first expression was false */
2693 g_defcodelabel (labf);
2695 /* Parse second expression. Remember for later if it is a NULL pointer
2696 * expression, then load it into the primary.
2698 ExprWithCheck (hie1, &Expr3);
2699 Expr3IsNULL = ED_IsNullPtr (&Expr3);
2700 if (!IsTypeVoid (Expr3.Type)) {
2701 /* Load it into the primary */
2702 LoadExpr (CF_NONE, &Expr3);
2703 ED_MakeRValExpr (&Expr3);
2704 Expr3.Type = PtrConversion (Expr3.Type);
2707 /* Check if any conversions are needed, if so, do them.
2708 * Conversion rules for ?: expression are:
2709 * - if both expressions are int expressions, default promotion
2710 * rules for ints apply.
2711 * - if both expressions are pointers of the same type, the
2712 * result of the expression is of this type.
2713 * - if one of the expressions is a pointer and the other is
2714 * a zero constant, the resulting type is that of the pointer
2716 * - if both expressions are void expressions, the result is of
2718 * - all other cases are flagged by an error.
2720 if (IsClassInt (Expr2.Type) && IsClassInt (Expr3.Type)) {
2722 /* Get common type */
2723 ResultType = promoteint (Expr2.Type, Expr3.Type);
2725 /* Convert the third expression to this type if needed */
2726 TypeConversion (&Expr3, ResultType);
2728 /* Setup a new label so that the expr3 code will jump around
2729 * the type cast code for expr2.
2731 labf = GetLocalLabel (); /* Get new label */
2732 g_jump (labf); /* Jump around code */
2734 /* The jump for expr2 goes here */
2735 g_defcodelabel (labt);
2737 /* Create the typecast code for expr2 */
2738 TypeConversion (&Expr2, ResultType);
2740 /* Jump here around the typecase code. */
2741 g_defcodelabel (labf);
2742 labt = 0; /* Mark other label as invalid */
2744 } else if (IsClassPtr (Expr2.Type) && IsClassPtr (Expr3.Type)) {
2745 /* Must point to same type */
2746 if (TypeCmp (Indirect (Expr2.Type), Indirect (Expr3.Type)) < TC_EQUAL) {
2747 Error ("Incompatible pointer types");
2749 /* Result has the common type */
2750 ResultType = Expr2.Type;
2751 } else if (IsClassPtr (Expr2.Type) && Expr3IsNULL) {
2752 /* Result type is pointer, no cast needed */
2753 ResultType = Expr2.Type;
2754 } else if (Expr2IsNULL && IsClassPtr (Expr3.Type)) {
2755 /* Result type is pointer, no cast needed */
2756 ResultType = Expr3.Type;
2757 } else if (IsTypeVoid (Expr2.Type) && IsTypeVoid (Expr3.Type)) {
2758 /* Result type is void */
2759 ResultType = Expr3.Type;
2761 Error ("Incompatible types");
2762 ResultType = Expr2.Type; /* Doesn't matter here */
2765 /* If we don't have the label defined until now, do it */
2767 g_defcodelabel (labt);
2770 /* Setup the target expression */
2771 ED_MakeRValExpr (Expr);
2772 Expr->Type = ResultType;
2778 static void opeq (const GenDesc* Gen, ExprDesc* Expr)
2779 /* Process "op=" operators. */
2786 /* op= can only be used with lvalues */
2787 if (!ED_IsLVal (Expr)) {
2788 Error ("Invalid lvalue in assignment");
2792 /* The left side must not be const qualified */
2793 if (IsQualConst (Expr->Type)) {
2794 Error ("Assignment to const");
2797 /* There must be an integer or pointer on the left side */
2798 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
2799 Error ("Invalid left operand type");
2800 /* Continue. Wrong code will be generated, but the compiler won't
2801 * break, so this is the best error recovery.
2805 /* Skip the operator token */
2808 /* Determine the type of the lhs */
2809 flags = TypeOf (Expr->Type);
2810 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) && IsTypePtr (Expr->Type);
2812 /* Get the lhs address on stack (if needed) */
2815 /* Fetch the lhs into the primary register if needed */
2816 LoadExpr (CF_NONE, Expr);
2818 /* Bring the lhs on stack */
2822 /* Evaluate the rhs */
2823 if (evalexpr (CF_NONE, hie1, &Expr2) == 0) {
2824 /* The resulting value is a constant. If the generator has the NOPUSH
2825 * flag set, don't push the lhs.
2827 if (Gen->Flags & GEN_NOPUSH) {
2831 /* lhs is a pointer, scale rhs */
2832 Expr2.IVal *= CheckedSizeOf (Expr->Type+1);
2835 /* If the lhs is character sized, the operation may be later done
2838 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
2839 flags |= CF_FORCECHAR;
2842 /* Special handling for add and sub - some sort of a hack, but short code */
2843 if (Gen->Func == g_add) {
2844 g_inc (flags | CF_CONST, Expr2.IVal);
2845 } else if (Gen->Func == g_sub) {
2846 g_dec (flags | CF_CONST, Expr2.IVal);
2848 if (Expr2.IVal == 0) {
2849 /* Check for div by zero/mod by zero */
2850 if (Gen->Func == g_div) {
2851 Error ("Division by zero");
2852 } else if (Gen->Func == g_mod) {
2853 Error ("Modulo operation with zero");
2856 Gen->Func (flags | CF_CONST, Expr2.IVal);
2859 /* rhs is not constant and already in the primary register */
2861 /* lhs is a pointer, scale rhs */
2862 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Expr->Type+1));
2865 /* If the lhs is character sized, the operation may be later done
2868 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
2869 flags |= CF_FORCECHAR;
2872 /* Adjust the types of the operands if needed */
2873 Gen->Func (g_typeadjust (flags, TypeOf (Expr2.Type)), 0);
2876 ED_MakeRValExpr (Expr);
2881 static void addsubeq (const GenDesc* Gen, ExprDesc *Expr)
2882 /* Process the += and -= operators */
2890 /* We're currently only able to handle some adressing modes */
2891 if (ED_GetLoc (Expr) == E_LOC_EXPR || ED_GetLoc (Expr) == E_LOC_PRIMARY) {
2892 /* Use generic routine */
2897 /* We must have an lvalue */
2898 if (ED_IsRVal (Expr)) {
2899 Error ("Invalid lvalue in assignment");
2903 /* The left side must not be const qualified */
2904 if (IsQualConst (Expr->Type)) {
2905 Error ("Assignment to const");
2908 /* There must be an integer or pointer on the left side */
2909 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
2910 Error ("Invalid left operand type");
2911 /* Continue. Wrong code will be generated, but the compiler won't
2912 * break, so this is the best error recovery.
2916 /* Skip the operator */
2919 /* Check if we have a pointer expression and must scale rhs */
2920 MustScale = IsTypePtr (Expr->Type);
2922 /* Initialize the code generator flags */
2926 /* Evaluate the rhs */
2928 if (ED_IsConstAbs (&Expr2)) {
2929 /* The resulting value is a constant. Scale it. */
2931 Expr2.IVal *= CheckedSizeOf (Indirect (Expr->Type));
2936 /* Not constant, load into the primary */
2937 LoadExpr (CF_NONE, &Expr2);
2939 /* lhs is a pointer, scale rhs */
2940 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Indirect (Expr->Type)));
2944 /* Setup the code generator flags */
2945 lflags |= TypeOf (Expr->Type) | GlobalModeFlags (Expr);
2946 rflags |= TypeOf (Expr2.Type) | CF_FORCECHAR;
2948 /* Convert the type of the lhs to that of the rhs */
2949 g_typecast (lflags, rflags);
2951 /* Output apropriate code depending on the location */
2952 switch (ED_GetLoc (Expr)) {
2955 /* Absolute: numeric address or const */
2956 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2957 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2959 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2964 /* Global variable */
2965 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2966 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2968 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2974 /* Static variable or literal in the literal pool */
2975 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2976 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2978 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2982 case E_LOC_REGISTER:
2983 /* Register variable */
2984 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2985 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2987 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2992 /* Value on the stack */
2993 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2994 g_addeqlocal (lflags, Expr->IVal, Expr2.IVal);
2996 g_subeqlocal (lflags, Expr->IVal, Expr2.IVal);
3001 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
3004 /* Expression is a rvalue in the primary now */
3005 ED_MakeRValExpr (Expr);
3010 void hie1 (ExprDesc* Expr)
3011 /* Parse first level of expression hierarchy. */
3014 switch (CurTok.Tok) {
3020 case TOK_PLUS_ASSIGN:
3021 addsubeq (&GenPASGN, Expr);
3024 case TOK_MINUS_ASSIGN:
3025 addsubeq (&GenSASGN, Expr);
3028 case TOK_MUL_ASSIGN:
3029 opeq (&GenMASGN, Expr);
3032 case TOK_DIV_ASSIGN:
3033 opeq (&GenDASGN, Expr);
3036 case TOK_MOD_ASSIGN:
3037 opeq (&GenMOASGN, Expr);
3040 case TOK_SHL_ASSIGN:
3041 opeq (&GenSLASGN, Expr);
3044 case TOK_SHR_ASSIGN:
3045 opeq (&GenSRASGN, Expr);
3048 case TOK_AND_ASSIGN:
3049 opeq (&GenAASGN, Expr);
3052 case TOK_XOR_ASSIGN:
3053 opeq (&GenXOASGN, Expr);
3057 opeq (&GenOASGN, Expr);
3067 void hie0 (ExprDesc *Expr)
3068 /* Parse comma operator. */
3071 while (CurTok.Tok == TOK_COMMA) {
3079 int evalexpr (unsigned Flags, void (*Func) (ExprDesc*), ExprDesc* Expr)
3080 /* Will evaluate an expression via the given function. If the result is a
3081 * constant, 0 is returned and the value is put in the Expr struct. If the
3082 * result is not constant, LoadExpr is called to bring the value into the
3083 * primary register and 1 is returned.
3087 ExprWithCheck (Func, Expr);
3089 /* Check for a constant expression */
3090 if (ED_IsConstAbs (Expr)) {
3091 /* Constant expression */
3094 /* Not constant, load into the primary */
3095 LoadExpr (Flags, Expr);
3102 void Expression0 (ExprDesc* Expr)
3103 /* Evaluate an expression via hie0 and put the result into the primary register */
3105 ExprWithCheck (hie0, Expr);
3106 LoadExpr (CF_NONE, Expr);
3111 void ConstExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3112 /* Will evaluate an expression via the given function. If the result is not
3113 * a constant of some sort, a diagnostic will be printed, and the value is
3114 * replaced by a constant one to make sure there are no internal errors that
3115 * result from this input error.
3118 ExprWithCheck (Func, Expr);
3119 if (!ED_IsConst (Expr)) {
3120 Error ("Constant expression expected");
3121 /* To avoid any compiler errors, make the expression a valid const */
3122 ED_MakeConstAbsInt (Expr, 1);
3128 void BoolExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3129 /* Will evaluate an expression via the given function. If the result is not
3130 * something that may be evaluated in a boolean context, a diagnostic will be
3131 * printed, and the value is replaced by a constant one to make sure there
3132 * are no internal errors that result from this input error.
3135 ExprWithCheck (Func, Expr);
3136 if (!ED_IsBool (Expr)) {
3137 Error ("Boolean expression expected");
3138 /* To avoid any compiler errors, make the expression a valid int */
3139 ED_MakeConstAbsInt (Expr, 1);
3145 void ConstAbsIntExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3146 /* Will evaluate an expression via the given function. If the result is not
3147 * a constant numeric integer value, a diagnostic will be printed, and the
3148 * value is replaced by a constant one to make sure there are no internal
3149 * errors that result from this input error.
3152 ExprWithCheck (Func, Expr);
3153 if (!ED_IsConstAbsInt (Expr)) {
3154 Error ("Constant integer expression expected");
3155 /* To avoid any compiler errors, make the expression a valid const */
3156 ED_MakeConstAbsInt (Expr, 1);