3 * Ullrich von Bassewitz, 21.06.1998
13 #include "debugflag.h"
20 #include "assignment.h"
39 /*****************************************************************************/
41 /*****************************************************************************/
45 /* Generator attributes */
46 #define GEN_NOPUSH 0x01 /* Don't push lhs */
48 /* Map a generator function and its attributes to a token */
50 token_t Tok; /* Token to map to */
51 unsigned Flags; /* Flags for generator function */
52 void (*Func) (unsigned, unsigned long); /* Generator func */
55 /* Descriptors for the operations */
56 static GenDesc GenPASGN = { TOK_PLUS_ASSIGN, GEN_NOPUSH, g_add };
57 static GenDesc GenSASGN = { TOK_MINUS_ASSIGN, GEN_NOPUSH, g_sub };
58 static GenDesc GenMASGN = { TOK_MUL_ASSIGN, GEN_NOPUSH, g_mul };
59 static GenDesc GenDASGN = { TOK_DIV_ASSIGN, GEN_NOPUSH, g_div };
60 static GenDesc GenMOASGN = { TOK_MOD_ASSIGN, GEN_NOPUSH, g_mod };
61 static GenDesc GenSLASGN = { TOK_SHL_ASSIGN, GEN_NOPUSH, g_asl };
62 static GenDesc GenSRASGN = { TOK_SHR_ASSIGN, GEN_NOPUSH, g_asr };
63 static GenDesc GenAASGN = { TOK_AND_ASSIGN, GEN_NOPUSH, g_and };
64 static GenDesc GenXOASGN = { TOK_XOR_ASSIGN, GEN_NOPUSH, g_xor };
65 static GenDesc GenOASGN = { TOK_OR_ASSIGN, GEN_NOPUSH, g_or };
69 /*****************************************************************************/
70 /* Function forwards */
71 /*****************************************************************************/
75 void hie0 (ExprDesc *lval);
76 /* Parse comma operator. */
80 /*****************************************************************************/
81 /* Helper functions */
82 /*****************************************************************************/
86 static unsigned GlobalModeFlags (unsigned Flags)
87 /* Return the addressing mode flags for the variable with the given flags */
89 switch (Flags & E_MASK_LOC) {
90 case E_LOC_GLOBAL: return CF_EXTERNAL;
91 case E_LOC_STATIC: return CF_STATIC;
92 case E_LOC_REGISTER: return CF_REGVAR;
94 Internal ("GlobalModeFlags: Invalid flags value: %u", Flags);
100 static 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 void DefineData (ExprDesc* Expr)
191 /* Output a data definition for the given expression */
193 switch (ED_GetLoc (Expr)) {
196 /* Absolute: numeric address or const */
197 g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->Val, 0);
201 /* Global variable */
202 g_defdata (CF_EXTERNAL, Expr->Name, Expr->Val);
207 /* Static variable or literal in the literal pool */
208 g_defdata (CF_STATIC, Expr->Name, Expr->Val);
212 /* Register variable. Taking the address is usually not
215 if (IS_Get (&AllowRegVarAddr) == 0) {
216 Error ("Cannot take the address of a register variable");
218 g_defdata (CF_REGVAR, Expr->Name, Expr->Val);
222 Internal ("Unknown constant type: 0x%04X", ED_GetLoc (Expr));
228 static void LoadConstant (unsigned Flags, ExprDesc* Expr)
229 /* Load the primary register with some constant value. */
231 switch (ED_GetLoc (Expr)) {
234 /* Number constant */
235 g_getimmed (Flags | TypeOf (Expr->Type) | CF_CONST, Expr->Val, 0);
239 /* Global symbol, load address */
240 g_getimmed ((Flags | CF_EXTERNAL) & ~CF_CONST, Expr->Name, Expr->Val);
245 /* Static symbol or literal, load address */
246 g_getimmed ((Flags | CF_STATIC) & ~CF_CONST, Expr->Name, Expr->Val);
250 /* Register variable. Taking the address is usually not
253 if (IS_Get (&AllowRegVarAddr) == 0) {
254 Error ("Cannot take the address of a register variable");
256 g_getimmed ((Flags | CF_REGVAR) & ~CF_CONST, Expr->Name, Expr->Val);
263 Internal ("Unknown constant type: %04X", Expr->Flags);
269 static int kcalc (token_t tok, long val1, long val2)
270 /* Calculate an operation with left and right operand constant. */
274 return (val1 == val2);
276 return (val1 != val2);
278 return (val1 < val2);
280 return (val1 <= val2);
282 return (val1 >= val2);
284 return (val1 > val2);
286 return (val1 | val2);
288 return (val1 ^ val2);
290 return (val1 & val2);
292 return (val1 >> val2);
294 return (val1 << val2);
296 return (val1 * val2);
299 Error ("Division by zero");
302 return (val1 / val2);
305 Error ("Modulo operation with zero");
308 return (val1 % val2);
310 Internal ("kcalc: got token 0x%X\n", tok);
317 static const GenDesc* FindGen (token_t Tok, const GenDesc* Table)
318 /* Find a token in a generator table */
320 while (Table->Tok != TOK_INVALID) {
321 if (Table->Tok == Tok) {
331 static int TypeSpecAhead (void)
332 /* Return true if some sort of type is waiting (helper for cast and sizeof()
338 /* There's a type waiting if:
340 * We have an opening paren, and
341 * a. the next token is a type, or
342 * b. the next token is a type qualifier, or
343 * c. the next token is a typedef'd type
345 return CurTok.Tok == TOK_LPAREN && (
346 TokIsType (&NextTok) ||
347 TokIsTypeQual (&NextTok) ||
348 (NextTok.Tok == TOK_IDENT &&
349 (Entry = FindSym (NextTok.Ident)) != 0 &&
350 SymIsTypeDef (Entry)));
355 void PushAddr (const ExprDesc* Expr)
356 /* If the expression contains an address that was somehow evaluated,
357 * push this address on the stack. This is a helper function for all
358 * sorts of implicit or explicit assignment functions where the lvalue
359 * must be saved if it's not constant, before evaluating the rhs.
362 /* Get the address on stack if needed */
363 if (ED_IsLocExpr (Expr)) {
364 /* Push the address (always a pointer) */
371 /*****************************************************************************/
373 /*****************************************************************************/
377 void ExprLoad (unsigned Flags, ExprDesc* Expr)
378 /* Place the result of an expression into the primary register if it is not
382 if (ED_IsLVal (Expr)) {
384 /* Dereferenced lvalue */
385 Flags |= TypeOf (Expr->Type);
386 if (Expr->Test & E_FORCETEST) {
388 Expr->Test &= ~E_FORCETEST;
391 switch (ED_GetLoc (Expr)) {
394 /* Absolute: numeric address or const */
395 g_getstatic (Flags | CF_ABSOLUTE, Expr->Val, 0);
399 /* Global variable */
400 g_getstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->Val);
405 /* Static variable or literal in the literal pool */
406 g_getstatic (Flags | CF_STATIC, Expr->Name, Expr->Val);
410 /* Register variable */
411 g_getstatic (Flags | CF_REGVAR, Expr->Name, Expr->Val);
415 /* Value on the stack */
416 g_getlocal (Flags, Expr->Val);
420 /* The primary register - just test if necessary */
421 if (Flags & CF_TEST) {
427 /* Reference to address in primary with offset in Expr */
428 g_getind (Flags, Expr->Val);
432 Internal ("Invalid location in ExprLoad: 0x%04X", ED_GetLoc (Expr));
437 if (ED_IsLocExpr (Expr)) {
438 if (Expr->Val != 0) {
439 /* We have an expression in the primary plus a constant
440 * offset. Adjust the value in the primary accordingly.
442 Flags |= TypeOf (Expr->Type);
443 g_inc (Flags | CF_CONST, Expr->Val);
446 /* Constant of some sort, load it into the primary */
447 LoadConstant (Flags, Expr);
450 /* Are we testing this value? */
451 if (Expr->Test & E_FORCETEST) {
452 /* Yes, force a test */
453 Flags |= TypeOf (Expr->Type);
455 Expr->Test &= ~E_FORCETEST;
460 /* Regardless of the original contents, Expr is now an rvalue in the
461 * primary. ### Later...
463 ED_MakeRValExpr (Expr);
469 static unsigned FunctionParamList (FuncDesc* Func)
470 /* Parse a function parameter list and pass the parameters to the called
471 * function. Depending on several criteria this may be done by just pushing
472 * each parameter separately, or creating the parameter frame once and then
473 * storing into this frame.
474 * The function returns the size of the parameters pushed.
479 /* Initialize variables */
480 SymEntry* Param = 0; /* Keep gcc silent */
481 unsigned ParamSize = 0; /* Size of parameters pushed */
482 unsigned ParamCount = 0; /* Number of parameters pushed */
483 unsigned FrameSize = 0; /* Size of parameter frame */
484 unsigned FrameParams = 0; /* Number of params in frame */
485 int FrameOffs = 0; /* Offset into parameter frame */
486 int Ellipsis = 0; /* Function is variadic */
488 /* As an optimization, we may allocate the complete parameter frame at
489 * once instead of pushing each parameter as it comes. We may do that,
492 * - optimizations that increase code size are enabled (allocating the
493 * stack frame at once gives usually larger code).
494 * - we have more than one parameter to push (don't count the last param
495 * for __fastcall__ functions).
497 * The FrameSize variable will contain a value > 0 if storing into a frame
498 * (instead of pushing) is enabled.
501 if (CodeSizeFactor >= 200) {
503 /* Calculate the number and size of the parameters */
504 FrameParams = Func->ParamCount;
505 FrameSize = Func->ParamSize;
506 if (FrameParams > 0 && (Func->Flags & FD_FASTCALL) != 0) {
507 /* Last parameter is not pushed */
508 FrameSize -= CheckedSizeOf (Func->LastParam->Type);
512 /* Do we have more than one parameter in the frame? */
513 if (FrameParams > 1) {
514 /* Okeydokey, setup the frame */
515 FrameOffs = StackPtr;
517 StackPtr -= FrameSize;
519 /* Don't use a preallocated frame */
524 /* Parse the actual parameter list */
525 while (CurTok.Tok != TOK_RPAREN) {
529 /* Count arguments */
532 /* Fetch the pointer to the next argument, check for too many args */
533 if (ParamCount <= Func->ParamCount) {
534 /* Beware: If there are parameters with identical names, they
535 * cannot go into the same symbol table, which means that in this
536 * case of errorneous input, the number of nodes in the symbol
537 * table and ParamCount are NOT equal. We have to handle this case
538 * below to avoid segmentation violations. Since we know that this
539 * problem can only occur if there is more than one parameter,
540 * we will just use the last one.
542 if (ParamCount == 1) {
544 Param = Func->SymTab->SymHead;
545 } else if (Param->NextSym != 0) {
547 Param = Param->NextSym;
548 CHECK ((Param->Flags & SC_PARAM) != 0);
550 } else if (!Ellipsis) {
551 /* Too many arguments. Do we have an open param list? */
552 if ((Func->Flags & FD_VARIADIC) == 0) {
553 /* End of param list reached, no ellipsis */
554 Error ("Too many arguments in function call");
556 /* Assume an ellipsis even in case of errors to avoid an error
557 * message for each other argument.
562 /* Evaluate the parameter expression */
565 /* If we don't have an argument spec, accept anything, otherwise
566 * convert the actual argument to the type needed.
570 /* Convert the argument to the parameter type if needed */
571 TypeConversion (&Expr, Param->Type);
573 /* If we have a prototype, chars may be pushed as chars */
574 Flags |= CF_FORCECHAR;
577 /* Load the value into the primary if it is not already there */
578 ExprLoad (Flags, &Expr);
580 /* Use the type of the argument for the push */
581 Flags |= TypeOf (Expr.Type);
583 /* If this is a fastcall function, don't push the last argument */
584 if (ParamCount != Func->ParamCount || (Func->Flags & FD_FASTCALL) == 0) {
585 unsigned ArgSize = sizeofarg (Flags);
587 /* We have the space already allocated, store in the frame.
588 * Because of invalid type conversions (that have produced an
589 * error before), we can end up here with a non aligned stack
590 * frame. Since no output will be generated anyway, handle
591 * these cases gracefully instead of doing a CHECK.
593 if (FrameSize >= ArgSize) {
594 FrameSize -= ArgSize;
598 FrameOffs -= ArgSize;
600 g_putlocal (Flags | CF_NOKEEP, FrameOffs, Expr.Val);
602 /* Push the argument */
603 g_push (Flags, Expr.Val);
606 /* Calculate total parameter size */
607 ParamSize += ArgSize;
610 /* Check for end of argument list */
611 if (CurTok.Tok != TOK_COMMA) {
617 /* Check if we had enough parameters */
618 if (ParamCount < Func->ParamCount) {
619 Error ("Too few arguments in function call");
622 /* The function returns the size of all parameters pushed onto the stack.
623 * However, if there are parameters missing (which is an error and was
624 * flagged by the compiler) AND a stack frame was preallocated above,
625 * we would loose track of the stackpointer and generate an internal error
626 * later. So we correct the value by the parameters that should have been
627 * pushed to avoid an internal compiler error. Since an error was
628 * generated before, no code will be output anyway.
630 return ParamSize + FrameSize;
635 static void FunctionCall (ExprDesc* Expr)
636 /* Perform a function call. */
638 FuncDesc* Func; /* Function descriptor */
639 int IsFuncPtr; /* Flag */
640 unsigned ParamSize; /* Number of parameter bytes */
641 CodeMark Mark = 0; /* Initialize to keep gcc silent */
642 int PtrOffs = 0; /* Offset of function pointer on stack */
643 int IsFastCall = 0; /* True if it's a fast call function */
644 int PtrOnStack = 0; /* True if a pointer copy is on stack */
646 /* Skip the left paren */
649 /* Get a pointer to the function descriptor from the type string */
650 Func = GetFuncDesc (Expr->Type);
652 /* Handle function pointers transparently */
653 IsFuncPtr = IsTypeFuncPtr (Expr->Type);
656 /* Check wether it's a fastcall function that has parameters */
657 IsFastCall = IsFastCallFunc (Expr->Type + 1) && (Func->ParamCount > 0);
659 /* Things may be difficult, depending on where the function pointer
660 * resides. If the function pointer is an expression of some sort
661 * (not a local or global variable), we have to evaluate this
662 * expression now and save the result for later. Since calls to
663 * function pointers may be nested, we must save it onto the stack.
664 * For fastcall functions we do also need to place a copy of the
665 * pointer on stack, since we cannot use a/x.
667 PtrOnStack = IsFastCall || !ED_IsConst (Expr);
670 /* Not a global or local variable, or a fastcall function. Load
671 * the pointer into the primary and mark it as an expression.
673 ExprLoad (CF_NONE, Expr);
674 ED_MakeRValExpr (Expr);
676 /* Remember the code position */
677 Mark = GetCodePos ();
679 /* Push the pointer onto the stack and remember the offset */
684 /* Check for known standard functions and inline them if requested */
685 } else if (IS_Get (&InlineStdFuncs) && IsStdFunc ((const char*) Expr->Name)) {
687 /* Inline this function */
688 HandleStdFunc (Func, Expr);
693 /* Parse the parameter list */
694 ParamSize = FunctionParamList (Func);
696 /* We need the closing paren here */
699 /* Special handling for function pointers */
702 /* If the function is not a fastcall function, load the pointer to
703 * the function into the primary.
707 /* Not a fastcall function - we may use the primary */
709 /* If we have no parameters, the pointer is still in the
710 * primary. Remove the code to push it and correct the
713 if (ParamSize == 0) {
718 /* Load from the saved copy */
719 g_getlocal (CF_PTR, PtrOffs);
722 /* Load from original location */
723 ExprLoad (CF_NONE, Expr);
726 /* Call the function */
727 g_callind (TypeOf (Expr->Type+1), ParamSize, PtrOffs);
731 /* Fastcall function. We cannot use the primary for the function
732 * pointer and must therefore use an offset to the stack location.
733 * Since fastcall functions may never be variadic, we can use the
734 * index register for this purpose.
736 g_callind (CF_LOCAL, ParamSize, PtrOffs);
739 /* If we have a pointer on stack, remove it */
741 g_space (- (int) sizeofarg (CF_PTR));
750 /* Normal function */
751 g_call (TypeOf (Expr->Type), (const char*) Expr->Name, ParamSize);
756 /* The function result is an rvalue in the primary register */
757 ED_MakeRValExpr (Expr);
758 Expr->Type = GetFuncReturn (Expr->Type);
763 static void Primary (ExprDesc* E)
764 /* This is the lowest level of the expression parser. */
768 /* Initialize fields in the expression stucture */
771 /* Character and integer constants. */
772 if (CurTok.Tok == TOK_ICONST || CurTok.Tok == TOK_CCONST) {
773 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
774 E->Type = CurTok.Type;
775 E->Val = CurTok.IVal;
780 /* Process parenthesized subexpression by calling the whole parser
783 if (CurTok.Tok == TOK_LPAREN) {
790 /* If we run into an identifier in preprocessing mode, we assume that this
791 * is an undefined macro and replace it by a constant value of zero.
793 if (Preprocessing && CurTok.Tok == TOK_IDENT) {
794 ED_MakeConstAbsInt (E, 0);
798 /* All others may only be used if the expression evaluation is not called
799 * recursively by the preprocessor.
802 /* Illegal expression in PP mode */
803 Error ("Preprocessor expression expected");
804 ED_MakeConstAbsInt (E, 1);
808 switch (CurTok.Tok) {
811 /* Identifier. Get a pointer to the symbol table entry */
812 Sym = E->Sym = FindSym (CurTok.Ident);
814 /* Is the symbol known? */
817 /* We found the symbol - skip the name token */
820 /* Check for illegal symbol types */
821 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
822 if (Sym->Flags & SC_TYPE) {
823 /* Cannot use type symbols */
824 Error ("Variable identifier expected");
825 /* Assume an int type to make E valid */
826 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
831 /* Mark the symbol as referenced */
832 Sym->Flags |= SC_REF;
834 /* The expression type is the symbol type */
837 /* Check for legal symbol types */
838 if ((Sym->Flags & SC_CONST) == SC_CONST) {
839 /* Enum or some other numeric constant */
840 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
841 E->Val = Sym->V.ConstVal;
842 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
844 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
845 E->Name = (unsigned long) Sym->Name;
846 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
847 /* Local variable. If this is a parameter for a variadic
848 * function, we have to add some address calculations, and the
849 * address is not const.
851 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
852 /* Variadic parameter */
853 g_leavariadic (Sym->V.Offs - F_GetParamSize (CurrentFunc));
854 E->Flags = E_LOC_EXPR | E_RTYPE_LVAL;
856 /* Normal parameter */
857 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
858 E->Val = Sym->V.Offs;
860 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
861 /* Register variable, zero page based */
862 E->Flags = E_LOC_REGISTER | E_RTYPE_LVAL;
863 E->Name = Sym->V.R.RegOffs;
864 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
865 /* Static variable */
866 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
867 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
868 E->Name = (unsigned long) Sym->Name;
870 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
871 E->Name = Sym->V.Label;
874 /* Local static variable */
875 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
876 E->Name = Sym->V.Offs;
879 /* We've made all variables lvalues above. However, this is
880 * not always correct: An array is actually the address of its
881 * first element, which is a rvalue, and a function is a
882 * rvalue, too, because we cannot store anything in a function.
883 * So fix the flags depending on the type.
885 if (IsTypeArray (E->Type) || IsTypeFunc (E->Type)) {
891 /* We did not find the symbol. Remember the name, then skip it */
893 strcpy (Ident, CurTok.Ident);
896 /* IDENT is either an auto-declared function or an undefined variable. */
897 if (CurTok.Tok == TOK_LPAREN) {
898 /* Declare a function returning int. For that purpose, prepare a
899 * function signature for a function having an empty param list
902 Warning ("Function call without a prototype");
903 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
905 E->Flags = E_LOC_GLOBAL | E_RTYPE_RVAL;
906 E->Name = (unsigned long) Sym->Name;
908 /* Undeclared Variable */
909 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
910 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
912 Error ("Undefined symbol: `%s'", Ident);
920 E->Type = GetCharArrayType (GetLiteralPoolOffs () - CurTok.IVal);
921 E->Flags = E_LOC_LITERAL | E_RTYPE_RVAL;
922 E->Val = CurTok.IVal;
923 E->Name = LiteralPoolLabel;
930 E->Flags = E_LOC_EXPR | E_RTYPE_RVAL;
935 /* Register pseudo variable */
936 E->Type = type_uchar;
937 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
942 /* Register pseudo variable */
944 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
949 /* Register pseudo variable */
950 E->Type = type_ulong;
951 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
956 /* Illegal primary. */
957 Error ("Expression expected");
958 ED_MakeConstAbsInt (E, 1);
965 static void ArrayRef (ExprDesc* Expr)
966 /* Handle an array reference */
976 /* Skip the bracket */
979 /* Get the type of left side */
982 /* We can apply a special treatment for arrays that have a const base
983 * address. This is true for most arrays and will produce a lot better
984 * code. Check if this is a const base address.
986 ConstBaseAddr = (ED_IsLocConst (Expr) || ED_IsLocStack (Expr));
988 /* If we have a constant base, we delay the address fetch */
989 Mark1 = GetCodePos ();
990 Mark2 = 0; /* Silence gcc */
991 if (!ConstBaseAddr) {
992 /* Get a pointer to the array into the primary */
993 ExprLoad (CF_NONE, Expr);
995 /* Get the array pointer on stack. Do not push more than 16
996 * bit, even if this value is greater, since we cannot handle
997 * other than 16bit stuff when doing indexing.
999 Mark2 = GetCodePos ();
1003 /* TOS now contains ptr to array elements. Get the subscript. */
1004 ExprWithCheck (hie0, &SubScript);
1006 /* Check the types of array and subscript. We can either have a
1007 * pointer/array to the left, in which case the subscript must be of an
1008 * integer type, or we have an integer to the left, in which case the
1009 * subscript must be a pointer/array.
1010 * Since we do the necessary checking here, we can rely later on the
1013 if (IsClassPtr (Expr->Type)) {
1014 if (!IsClassInt (SubScript.Type)) {
1015 Error ("Array subscript is not an integer");
1016 /* To avoid any compiler errors, make the expression a valid int */
1017 ED_MakeConstAbsInt (&SubScript, 0);
1019 ElementType = Indirect (Expr->Type);
1020 } else if (IsClassInt (Expr->Type)) {
1021 if (!IsClassPtr (SubScript.Type)) {
1022 Error ("Subscripted value is neither array nor pointer");
1023 /* To avoid compiler errors, make the subscript a char[] at
1026 ED_MakeConstAbs (&SubScript, 0, GetCharArrayType (1));
1028 ElementType = Indirect (SubScript.Type);
1030 Error ("Cannot subscript");
1031 /* To avoid compiler errors, fake both the array and the subscript, so
1032 * we can just proceed.
1034 ED_MakeConstAbs (Expr, 0, GetCharArrayType (1));
1035 ED_MakeConstAbsInt (&SubScript, 0);
1036 ElementType = Indirect (Expr->Type);
1039 /* Check if the subscript is constant absolute value */
1040 if (ED_IsConstAbs (&SubScript)) {
1042 /* The array subscript is a numeric constant. If we had pushed the
1043 * array base address onto the stack before, we can remove this value,
1044 * since we can generate expression+offset.
1046 if (!ConstBaseAddr) {
1050 /* Get an array pointer into the primary */
1051 ExprLoad (CF_NONE, Expr);
1054 if (IsClassPtr (Expr->Type)) {
1056 /* Lhs is pointer/array. Scale the subscript value according to
1059 SubScript.Val *= CheckedSizeOf (ElementType);
1061 /* Remove the address load code */
1064 /* In case of an array, we can adjust the offset of the expression
1065 * already in Expr. If the base address was a constant, we can even
1066 * remove the code that loaded the address into the primary.
1068 if (IsTypeArray (Expr->Type)) {
1070 /* Adjust the offset */
1071 Expr->Val += SubScript.Val;
1075 /* It's a pointer, so we do have to load it into the primary
1076 * first (if it's not already there).
1078 if (ConstBaseAddr) {
1079 ExprLoad (CF_NONE, Expr);
1080 ED_MakeRValExpr (Expr);
1083 /* Use the offset */
1084 Expr->Val = SubScript.Val;
1089 /* Scale the rhs value according to the element type */
1090 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
1092 /* Add the subscript. Since arrays are indexed by integers,
1093 * we will ignore the true type of the subscript here and
1094 * use always an int. #### Use offset but beware of ExprLoad!
1096 g_inc (CF_INT | CF_CONST, SubScript.Val);
1102 /* Array subscript is not constant. Load it into the primary */
1103 Mark2 = GetCodePos ();
1104 ExprLoad (CF_NONE, &SubScript);
1107 if (IsClassPtr (Expr->Type)) {
1109 /* Indexing is based on unsigneds, so we will just use the integer
1110 * portion of the index (which is in (e)ax, so there's no further
1113 g_scale (CF_INT | CF_UNSIGNED, CheckedSizeOf (ElementType));
1117 /* Get the int value on top. If we come here, we're sure, both
1118 * values are 16 bit (the first one was truncated if necessary
1119 * and the second one is a pointer). Note: If ConstBaseAddr is
1120 * true, we don't have a value on stack, so to "swap" both, just
1121 * push the subscript.
1123 if (ConstBaseAddr) {
1125 ExprLoad (CF_NONE, Expr);
1132 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
1136 /* The offset is now in the primary register. It we didn't have a
1137 * constant base address for the lhs, the lhs address is already
1138 * on stack, and we must add the offset. If the base address was
1139 * constant, we call special functions to add the address to the
1142 if (!ConstBaseAddr) {
1144 /* The array base address is on stack and the subscript is in the
1145 * primary. Add both.
1147 g_add (CF_INT | CF_UNSIGNED, 0);
1151 /* The subscript is in the primary, and the array base address is
1152 * in Expr. If the subscript has itself a constant address, it is
1153 * often a better idea to reverse again the order of the
1154 * evaluation. This will generate better code if the subscript is
1155 * a byte sized variable. But beware: This is only possible if the
1156 * subscript was not scaled, that is, if this was a byte array
1159 if ((ED_IsLocConst (&SubScript) || ED_IsLocStack (&SubScript)) &&
1160 CheckedSizeOf (ElementType) == SIZEOF_CHAR) {
1164 /* Reverse the order of evaluation */
1165 if (CheckedSizeOf (SubScript.Type) == SIZEOF_CHAR) {
1172 /* Get a pointer to the array into the primary. */
1173 ExprLoad (CF_NONE, Expr);
1175 /* Add the variable */
1176 if (ED_IsLocStack (&SubScript)) {
1177 g_addlocal (Flags, SubScript.Val);
1179 Flags |= GlobalModeFlags (SubScript.Flags);
1180 g_addstatic (Flags, SubScript.Name, SubScript.Val);
1183 if (ED_IsLocAbs (Expr)) {
1184 /* Constant numeric address. Just add it */
1185 g_inc (CF_INT | CF_UNSIGNED, Expr->Val);
1186 } else if (ED_IsLocStack (Expr)) {
1187 /* Base address is a local variable address */
1188 if (IsTypeArray (Expr->Type)) {
1189 g_addaddr_local (CF_INT, Expr->Val);
1191 g_addlocal (CF_PTR, Expr->Val);
1194 /* Base address is a static variable address */
1195 unsigned Flags = CF_INT | GlobalModeFlags (Expr->Flags);
1196 if (IsTypeArray (Expr->Type)) {
1197 g_addaddr_static (Flags, Expr->Name, Expr->Val);
1199 g_addstatic (Flags, Expr->Name, Expr->Val);
1207 /* The result is an expression in the primary */
1208 ED_MakeRValExpr (Expr);
1212 /* Result is of element type */
1213 Expr->Type = ElementType;
1215 /* An array element is actually a variable. So the rules for variables
1216 * with respect to the reference type apply: If it's an array, it is
1217 * a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1218 * but an array cannot contain functions).
1220 if (IsTypeArray (Expr->Type)) {
1226 /* Consume the closing bracket */
1232 static void StructRef (ExprDesc* Expr)
1233 /* Process struct field after . or ->. */
1238 /* Skip the token and check for an identifier */
1240 if (CurTok.Tok != TOK_IDENT) {
1241 Error ("Identifier expected");
1242 Expr->Type = type_int;
1246 /* Get the symbol table entry and check for a struct field */
1247 strcpy (Ident, CurTok.Ident);
1249 Field = FindStructField (Expr->Type, Ident);
1251 Error ("Struct/union has no field named `%s'", Ident);
1252 Expr->Type = type_int;
1256 /* If we have a struct pointer that is not already in the primary, load
1259 if (IsTypePtr (Expr->Type)) {
1261 /* Load into the primary */
1262 ExprLoad (CF_NONE, Expr);
1264 /* Make it an lvalue expression */
1265 ED_MakeLValExpr (Expr);
1268 /* Set the struct field offset */
1269 Expr->Val += Field->V.Offs;
1271 /* The type is now the type of the field */
1272 Expr->Type = Field->Type;
1274 /* An struct member is actually a variable. So the rules for variables
1275 * with respect to the reference type apply: If it's an array, it is
1276 * a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1277 * but a struct field cannot be a function).
1279 if (IsTypeArray (Expr->Type)) {
1288 static void hie11 (ExprDesc *Expr)
1289 /* Handle compound types (structs and arrays) */
1291 /* Evaluate the lhs */
1294 /* Check for a rhs */
1295 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN ||
1296 CurTok.Tok == TOK_DOT || CurTok.Tok == TOK_PTR_REF) {
1298 switch (CurTok.Tok) {
1301 /* Array reference */
1306 /* Function call. */
1307 if (!IsTypeFunc (Expr->Type) && !IsTypeFuncPtr (Expr->Type)) {
1308 /* Not a function */
1309 Error ("Illegal function call");
1310 /* Force the type to be a implicitly defined function, one
1311 * returning an int and taking any number of arguments.
1312 * Since we don't have a name, place it at absolute address
1315 ED_MakeConstAbs (Expr, 0, GetImplicitFuncType ());
1317 /* Call the function */
1318 FunctionCall (Expr);
1322 if (!IsClassStruct (Expr->Type)) {
1323 Error ("Struct expected");
1329 /* If we have an array, convert it to pointer to first element */
1330 if (IsTypeArray (Expr->Type)) {
1331 Expr->Type = ArrayToPtr (Expr->Type);
1333 if (!IsClassPtr (Expr->Type) || !IsClassStruct (Indirect (Expr->Type))) {
1334 Error ("Struct pointer expected");
1340 Internal ("Invalid token in hie11: %d", CurTok.Tok);
1348 void Store (ExprDesc* Expr, const type* StoreType)
1349 /* Store the primary register into the location denoted by Expr. If StoreType
1350 * is given, use this type when storing instead of Expr->Type. If StoreType
1351 * is NULL, use Expr->Type instead.
1356 /* If StoreType was not given, use Expr->Type instead */
1357 if (StoreType == 0) {
1358 StoreType = Expr->Type;
1361 /* Prepare the code generator flags */
1362 Flags = TypeOf (StoreType);
1364 /* Testing the value */
1368 /* Do the store depending on the location */
1369 switch (ED_GetLoc (Expr)) {
1372 /* Absolute: numeric address or const */
1373 g_putstatic (Flags | CF_ABSOLUTE, Expr->Val, 0);
1377 /* Global variable */
1378 g_putstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->Val);
1383 /* Static variable or literal in the literal pool */
1384 g_putstatic (Flags | CF_STATIC, Expr->Name, Expr->Val);
1387 case E_LOC_REGISTER:
1388 /* Register variable */
1389 g_putstatic (Flags | CF_REGVAR, Expr->Name, Expr->Val);
1393 /* Value on the stack */
1394 g_putlocal (Flags, Expr->Val, 0);
1398 /* The primary register (value is already there) */
1399 /* ### Do we need a test here if the flag is set? */
1403 /* An expression in the primary register */
1404 g_putind (Flags, Expr->Val);
1408 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
1411 /* Assume that each one of the stores will invalidate CC */
1412 Expr->Test &= ~E_CC;
1417 static void PreInc (ExprDesc* Expr)
1418 /* Handle the preincrement operators */
1423 /* Skip the operator token */
1426 /* Evaluate the expression and check that it is an lvalue */
1428 if (!ED_IsLVal (Expr)) {
1429 Error ("Invalid lvalue");
1433 /* Get the data type */
1434 Flags = TypeOf (Expr->Type) | CF_FORCECHAR | CF_CONST;
1436 /* Get the increment value in bytes */
1437 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1439 /* Check the location of the data */
1440 switch (ED_GetLoc (Expr)) {
1443 /* Absolute: numeric address or const */
1444 g_addeqstatic (Flags | CF_ABSOLUTE, Expr->Val, 0, Val);
1448 /* Global variable */
1449 g_addeqstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->Val, Val);
1454 /* Static variable or literal in the literal pool */
1455 g_addeqstatic (Flags | CF_STATIC, Expr->Name, Expr->Val, Val);
1458 case E_LOC_REGISTER:
1459 /* Register variable */
1460 g_addeqstatic (Flags | CF_REGVAR, Expr->Name, Expr->Val, Val);
1464 /* Value on the stack */
1465 g_addeqlocal (Flags, Expr->Val, Val);
1469 /* The primary register */
1474 /* An expression in the primary register */
1475 g_addeqind (Flags, Expr->Val, Val);
1479 Internal ("Invalid location in PreInc(): 0x%04X", ED_GetLoc (Expr));
1482 /* Result is an expression, no reference */
1483 ED_MakeRValExpr (Expr);
1488 static void PreDec (ExprDesc* Expr)
1489 /* Handle the predecrement operators */
1494 /* Skip the operator token */
1497 /* Evaluate the expression and check that it is an lvalue */
1499 if (!ED_IsLVal (Expr)) {
1500 Error ("Invalid lvalue");
1504 /* Get the data type */
1505 Flags = TypeOf (Expr->Type) | CF_FORCECHAR | CF_CONST;
1507 /* Get the increment value in bytes */
1508 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1510 /* Check the location of the data */
1511 switch (ED_GetLoc (Expr)) {
1514 /* Absolute: numeric address or const */
1515 g_subeqstatic (Flags | CF_ABSOLUTE, Expr->Val, 0, Val);
1519 /* Global variable */
1520 g_subeqstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->Val, Val);
1525 /* Static variable or literal in the literal pool */
1526 g_subeqstatic (Flags | CF_STATIC, Expr->Name, Expr->Val, Val);
1529 case E_LOC_REGISTER:
1530 /* Register variable */
1531 g_subeqstatic (Flags | CF_REGVAR, Expr->Name, Expr->Val, Val);
1535 /* Value on the stack */
1536 g_subeqlocal (Flags, Expr->Val, Val);
1540 /* The primary register */
1545 /* An expression in the primary register */
1546 g_subeqind (Flags, Expr->Val, Val);
1550 Internal ("Invalid location in PreDec(): 0x%04X", ED_GetLoc (Expr));
1553 /* Result is an expression, no reference */
1554 ED_MakeRValExpr (Expr);
1559 static void PostIncDec (ExprDesc* Expr, void (*inc) (unsigned, unsigned long))
1560 /* Handle i-- and i++ */
1566 /* The expression to increment must be an lvalue */
1567 if (!ED_IsLVal (Expr)) {
1568 Error ("Invalid lvalue");
1572 /* Get the data type */
1573 Flags = TypeOf (Expr->Type);
1575 /* Push the address if needed */
1578 /* Fetch the value and save it (since it's the result of the expression) */
1579 ExprLoad (CF_NONE, Expr);
1580 g_save (Flags | CF_FORCECHAR);
1582 /* If we have a pointer expression, increment by the size of the type */
1583 if (IsTypePtr (Expr->Type)) {
1584 inc (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1586 inc (Flags | CF_CONST | CF_FORCECHAR, 1);
1589 /* Store the result back */
1592 /* Restore the original value in the primary register */
1593 g_restore (Flags | CF_FORCECHAR);
1595 /* The result is always an expression, no reference */
1596 ED_MakeRValExpr (Expr);
1601 static void UnaryOp (ExprDesc* Expr)
1602 /* Handle unary -/+ and ~ */
1606 /* Remember the operator token and skip it */
1607 token_t Tok = CurTok.Tok;
1610 /* Get the expression */
1613 /* We can only handle integer types */
1614 if (!IsClassInt (Expr->Type)) {
1615 Error ("Argument must have integer type");
1616 ED_MakeConstAbsInt (Expr, 1);
1619 /* Check for a constant expression */
1620 if (ED_IsConstAbs (Expr)) {
1621 /* Value is constant */
1623 case TOK_MINUS: Expr->Val = -Expr->Val; break;
1624 case TOK_PLUS: break;
1625 case TOK_COMP: Expr->Val = ~Expr->Val; break;
1626 default: Internal ("Unexpected token: %d", Tok);
1629 /* Value is not constant */
1630 ExprLoad (CF_NONE, Expr);
1632 /* Get the type of the expression */
1633 Flags = TypeOf (Expr->Type);
1635 /* Handle the operation */
1637 case TOK_MINUS: g_neg (Flags); break;
1638 case TOK_PLUS: break;
1639 case TOK_COMP: g_com (Flags); break;
1640 default: Internal ("Unexpected token: %d", Tok);
1643 /* The result is a rvalue in the primary */
1644 ED_MakeRValExpr (Expr);
1650 void hie10 (ExprDesc* Expr)
1651 /* Handle ++, --, !, unary - etc. */
1655 switch (CurTok.Tok) {
1673 if (evalexpr (CF_NONE, hie10, Expr) == 0) {
1674 /* Constant expression */
1675 Expr->Val = !Expr->Val;
1677 g_bneg (TypeOf (Expr->Type));
1678 ED_MakeRValExpr (Expr);
1679 Expr->Test |= E_CC; /* bneg will set cc */
1685 if (evalexpr (CF_NONE, hie10, Expr) != 0) {
1686 /* Expression is not const, indirect value loaded into primary */
1687 ED_MakeRValExpr (Expr);
1689 /* If the expression is already a pointer to function, the
1690 * additional dereferencing operator must be ignored.
1692 if (IsTypeFuncPtr (Expr->Type)) {
1693 /* Expression not storable */
1696 if (IsClassPtr (Expr->Type)) {
1697 Expr->Type = Indirect (Expr->Type);
1699 Error ("Illegal indirection");
1708 /* The & operator may be applied to any lvalue, and it may be
1709 * applied to functions, even if they're no lvalues.
1711 if (ED_IsRVal (Expr) && !IsTypeFunc (Expr->Type)) {
1712 /* Allow the & operator with an array */
1713 if (!IsTypeArray (Expr->Type)) {
1714 Error ("Illegal address");
1717 Expr->Type = PointerTo (Expr->Type);
1724 if (TypeSpecAhead ()) {
1725 type Type[MAXTYPELEN];
1727 Size = CheckedSizeOf (ParseType (Type));
1730 /* Remember the output queue pointer */
1731 CodeMark Mark = GetCodePos ();
1733 Size = CheckedSizeOf (Expr->Type);
1734 /* Remove any generated code */
1737 ED_MakeConstAbs (Expr, Size, type_size_t);
1738 Expr->Test &= ~E_CC;
1742 if (TypeSpecAhead ()) {
1752 /* Handle post increment */
1753 if (CurTok.Tok == TOK_INC) {
1754 PostIncDec (Expr, g_inc);
1755 } else if (CurTok.Tok == TOK_DEC) {
1756 PostIncDec (Expr, g_dec);
1766 static void hie_internal (const GenDesc* Ops, /* List of generators */
1768 void (*hienext) (ExprDesc*),
1770 /* Helper function */
1776 token_t Tok; /* The operator token */
1777 unsigned ltype, type;
1778 int rconst; /* Operand is a constant */
1784 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1786 /* Tell the caller that we handled it's ops */
1789 /* All operators that call this function expect an int on the lhs */
1790 if (!IsClassInt (Expr->Type)) {
1791 Error ("Integer expression expected");
1794 /* Remember the operator token, then skip it */
1798 /* Get the lhs on stack */
1799 Mark1 = GetCodePos ();
1800 ltype = TypeOf (Expr->Type);
1801 if (ED_IsConstAbs (Expr)) {
1802 /* Constant value */
1803 Mark2 = GetCodePos ();
1804 g_push (ltype | CF_CONST, Expr->Val);
1806 /* Value not constant */
1807 ExprLoad (CF_NONE, Expr);
1808 Mark2 = GetCodePos ();
1812 /* Get the right hand side */
1813 rconst = (evalexpr (CF_NONE, hienext, &Expr2) == 0);
1815 /* Check the type of the rhs */
1816 if (!IsClassInt (Expr2.Type)) {
1817 Error ("Integer expression expected");
1820 /* Check for const operands */
1821 if (ED_IsConstAbs (Expr) && rconst) {
1823 /* Both operands are constant, remove the generated code */
1827 /* Evaluate the result */
1828 Expr->Val = kcalc (Tok, Expr->Val, Expr2.Val);
1830 /* Get the type of the result */
1831 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1835 /* If the right hand side is constant, and the generator function
1836 * expects the lhs in the primary, remove the push of the primary
1839 unsigned rtype = TypeOf (Expr2.Type);
1842 /* Second value is constant - check for div */
1845 if (Tok == TOK_DIV && Expr2.Val == 0) {
1846 Error ("Division by zero");
1847 } else if (Tok == TOK_MOD && Expr2.Val == 0) {
1848 Error ("Modulo operation with zero");
1850 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1853 ltype |= CF_REG; /* Value is in register */
1857 /* Determine the type of the operation result. */
1858 type |= g_typeadjust (ltype, rtype);
1859 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1862 Gen->Func (type, Expr2.Val);
1864 /* We have a rvalue in the primary now */
1865 ED_MakeRValExpr (Expr);
1872 static void hie_compare (const GenDesc* Ops, /* List of generators */
1874 void (*hienext) (ExprDesc*))
1875 /* Helper function for the compare operators */
1881 token_t tok; /* The operator token */
1883 int rconst; /* Operand is a constant */
1888 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1890 /* Remember the operator token, then skip it */
1894 /* Get the lhs on stack */
1895 Mark1 = GetCodePos ();
1896 ltype = TypeOf (Expr->Type);
1897 if (ED_IsConstAbs (Expr)) {
1898 /* Constant value */
1899 Mark2 = GetCodePos ();
1900 g_push (ltype | CF_CONST, Expr->Val);
1902 /* Value not constant */
1903 ExprLoad (CF_NONE, Expr);
1904 Mark2 = GetCodePos ();
1908 /* Get the right hand side */
1909 rconst = (evalexpr (CF_NONE, hienext, &Expr2) == 0);
1911 /* Make sure, the types are compatible */
1912 if (IsClassInt (Expr->Type)) {
1913 if (!IsClassInt (Expr2.Type) && !(IsClassPtr(Expr2.Type) && ED_IsNullPtr(Expr))) {
1914 Error ("Incompatible types");
1916 } else if (IsClassPtr (Expr->Type)) {
1917 if (IsClassPtr (Expr2.Type)) {
1918 /* Both pointers are allowed in comparison if they point to
1919 * the same type, or if one of them is a void pointer.
1921 type* left = Indirect (Expr->Type);
1922 type* right = Indirect (Expr2.Type);
1923 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1924 /* Incomatible pointers */
1925 Error ("Incompatible types");
1927 } else if (!ED_IsNullPtr (&Expr2)) {
1928 Error ("Incompatible types");
1932 /* Check for const operands */
1933 if (ED_IsConstAbs (Expr) && rconst) {
1935 /* Both operands are constant, remove the generated code */
1939 /* Evaluate the result */
1940 Expr->Val = kcalc (tok, Expr->Val, Expr2.Val);
1944 /* If the right hand side is constant, and the generator function
1945 * expects the lhs in the primary, remove the push of the primary
1951 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1954 ltype |= CF_REG; /* Value is in register */
1958 /* Determine the type of the operation result. If the left
1959 * operand is of type char and the right is a constant, or
1960 * if both operands are of type char, we will encode the
1961 * operation as char operation. Otherwise the default
1962 * promotions are used.
1964 if (IsTypeChar (Expr->Type) && (IsTypeChar (Expr2.Type) || rconst)) {
1966 if (IsSignUnsigned (Expr->Type) || IsSignUnsigned (Expr2.Type)) {
1967 flags |= CF_UNSIGNED;
1970 flags |= CF_FORCECHAR;
1973 unsigned rtype = TypeOf (Expr2.Type) | (flags & CF_CONST);
1974 flags |= g_typeadjust (ltype, rtype);
1978 Gen->Func (flags, Expr2.Val);
1980 /* The result is an rvalue in the primary */
1981 ED_MakeRValExpr (Expr);
1984 /* Result type is always int */
1985 Expr->Type = type_int;
1987 /* Condition codes are set */
1994 static void hie9 (ExprDesc *Expr)
1995 /* Process * and / operators. */
1997 static const GenDesc hie9_ops[] = {
1998 { TOK_STAR, GEN_NOPUSH, g_mul },
1999 { TOK_DIV, GEN_NOPUSH, g_div },
2000 { TOK_MOD, GEN_NOPUSH, g_mod },
2001 { TOK_INVALID, 0, 0 }
2005 hie_internal (hie9_ops, Expr, hie10, &UsedGen);
2010 static void parseadd (ExprDesc* Expr)
2011 /* Parse an expression with the binary plus operator. Expr contains the
2012 * unprocessed left hand side of the expression and will contain the
2013 * result of the expression on return.
2017 unsigned flags; /* Operation flags */
2018 CodeMark Mark; /* Remember code position */
2019 type* lhst; /* Type of left hand side */
2020 type* rhst; /* Type of right hand side */
2023 /* Skip the PLUS token */
2026 /* Get the left hand side type, initialize operation flags */
2030 /* Check for constness on both sides */
2031 if (ED_IsConst (Expr)) {
2033 /* The left hand side is a constant of some sort. Good. Get rhs */
2035 if (ED_IsConstAbs (&Expr2)) {
2037 /* Right hand side is a constant numeric value. Get the rhs type */
2040 /* Both expressions are constants. Check for pointer arithmetic */
2041 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2042 /* Left is pointer, right is int, must scale rhs */
2043 Expr->Val += Expr2.Val * CheckedPSizeOf (lhst);
2044 /* Result type is a pointer */
2045 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2046 /* Left is int, right is pointer, must scale lhs */
2047 Expr->Val = Expr->Val * CheckedPSizeOf (rhst) + Expr2.Val;
2048 /* Result type is a pointer */
2049 Expr->Type = Expr2.Type;
2050 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2051 /* Integer addition */
2052 Expr->Val += Expr2.Val;
2053 typeadjust (Expr, &Expr2, 1);
2056 Error ("Invalid operands for binary operator `+'");
2061 /* lhs is a constant and rhs is not constant. Load rhs into
2064 ExprLoad (CF_NONE, &Expr2);
2066 /* Beware: The check above (for lhs) lets not only pass numeric
2067 * constants, but also constant addresses (labels), maybe even
2068 * with an offset. We have to check for that here.
2071 /* First, get the rhs type. */
2075 if (ED_IsLocAbs (Expr)) {
2076 /* A numerical constant */
2079 /* Constant address label */
2080 flags |= GlobalModeFlags (Expr->Flags) | CF_CONSTADDR;
2083 /* Check for pointer arithmetic */
2084 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2085 /* Left is pointer, right is int, must scale rhs */
2086 g_scale (CF_INT, CheckedPSizeOf (lhst));
2087 /* Operate on pointers, result type is a pointer */
2089 /* Generate the code for the add */
2090 if (ED_GetLoc (Expr) == E_LOC_ABS) {
2091 /* Numeric constant */
2092 g_inc (flags, Expr->Val);
2094 /* Constant address */
2095 g_addaddr_static (flags, Expr->Name, Expr->Val);
2097 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2099 /* Left is int, right is pointer, must scale lhs. */
2100 unsigned ScaleFactor = CheckedPSizeOf (rhst);
2102 /* Operate on pointers, result type is a pointer */
2104 Expr->Type = Expr2.Type;
2106 /* Since we do already have rhs in the primary, if lhs is
2107 * not a numeric constant, and the scale factor is not one
2108 * (no scaling), we must take the long way over the stack.
2110 if (ED_IsLocAbs (Expr)) {
2111 /* Numeric constant, scale lhs */
2112 Expr->Val *= ScaleFactor;
2113 /* Generate the code for the add */
2114 g_inc (flags, Expr->Val);
2115 } else if (ScaleFactor == 1) {
2116 /* Constant address but no need to scale */
2117 g_addaddr_static (flags, Expr->Name, Expr->Val);
2119 /* Constant address that must be scaled */
2120 g_push (TypeOf (Expr2.Type), 0); /* rhs --> stack */
2121 g_getimmed (flags, Expr->Name, Expr->Val);
2122 g_scale (CF_PTR, ScaleFactor);
2125 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2126 /* Integer addition */
2127 flags |= typeadjust (Expr, &Expr2, 1);
2128 /* Generate the code for the add */
2129 if (ED_IsLocAbs (Expr)) {
2130 /* Numeric constant */
2131 g_inc (flags, Expr->Val);
2133 /* Constant address */
2134 g_addaddr_static (flags, Expr->Name, Expr->Val);
2138 Error ("Invalid operands for binary operator `+'");
2141 /* Result is a rvalue in primary register */
2142 ED_MakeRValExpr (Expr);
2147 /* Left hand side is not constant. Get the value onto the stack. */
2148 ExprLoad (CF_NONE, Expr); /* --> primary register */
2149 Mark = GetCodePos ();
2150 g_push (TypeOf (Expr->Type), 0); /* --> stack */
2152 /* Evaluate the rhs */
2153 if (evalexpr (CF_NONE, hie9, &Expr2) == 0) {
2155 /* Right hand side is a constant. Get the rhs type */
2158 /* Remove pushed value from stack */
2160 pop (TypeOf (Expr->Type));
2162 /* Check for pointer arithmetic */
2163 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2164 /* Left is pointer, right is int, must scale rhs */
2165 Expr2.Val *= CheckedPSizeOf (lhst);
2166 /* Operate on pointers, result type is a pointer */
2168 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2169 /* Left is int, right is pointer, must scale lhs (ptr only) */
2170 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2171 /* Operate on pointers, result type is a pointer */
2173 Expr->Type = Expr2.Type;
2174 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2175 /* Integer addition */
2176 flags = typeadjust (Expr, &Expr2, 1);
2179 Error ("Invalid operands for binary operator `+'");
2182 /* Generate code for the add */
2183 g_inc (flags | CF_CONST, Expr2.Val);
2187 /* lhs and rhs are not constant. Get the rhs type. */
2190 /* Check for pointer arithmetic */
2191 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2192 /* Left is pointer, right is int, must scale rhs */
2193 g_scale (CF_INT, CheckedPSizeOf (lhst));
2194 /* Operate on pointers, result type is a pointer */
2196 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2197 /* Left is int, right is pointer, must scale lhs */
2198 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2199 g_swap (CF_INT); /* Swap TOS and primary */
2200 g_scale (CF_INT, CheckedPSizeOf (rhst));
2201 /* Operate on pointers, result type is a pointer */
2203 Expr->Type = Expr2.Type;
2204 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2205 /* Integer addition. Note: Result is never constant.
2206 * Problem here is that typeadjust does not know if the
2207 * variable is an rvalue or lvalue, so if both operands
2208 * are dereferenced constant numeric addresses, typeadjust
2209 * thinks the operation works on constants. Removing
2210 * CF_CONST here means handling the symptoms, however, the
2211 * whole parser is such a mess that I fear to break anything
2212 * when trying to apply another solution.
2214 flags = typeadjust (Expr, &Expr2, 0) & ~CF_CONST;
2217 Error ("Invalid operands for binary operator `+'");
2220 /* Generate code for the add */
2225 /* Result is a rvalue in primary register */
2226 ED_MakeRValExpr (Expr);
2229 /* Condition codes not set */
2230 Expr->Test &= ~E_CC;
2236 static void parsesub (ExprDesc* Expr)
2237 /* Parse an expression with the binary minus operator. Expr contains the
2238 * unprocessed left hand side of the expression and will contain the
2239 * result of the expression on return.
2243 unsigned flags; /* Operation flags */
2244 type* lhst; /* Type of left hand side */
2245 type* rhst; /* Type of right hand side */
2246 CodeMark Mark1; /* Save position of output queue */
2247 CodeMark Mark2; /* Another position in the queue */
2248 int rscale; /* Scale factor for the result */
2251 /* Skip the MINUS token */
2254 /* Get the left hand side type, initialize operation flags */
2257 rscale = 1; /* Scale by 1, that is, don't scale */
2259 /* Remember the output queue position, then bring the value onto the stack */
2260 Mark1 = GetCodePos ();
2261 ExprLoad (CF_NONE, Expr); /* --> primary register */
2262 Mark2 = GetCodePos ();
2263 g_push (TypeOf (lhst), 0); /* --> stack */
2265 /* Parse the right hand side */
2266 if (evalexpr (CF_NONE, hie9, &Expr2) == 0) {
2268 /* The right hand side is constant. Get the rhs type. */
2271 /* Check left hand side */
2272 if (ED_IsConstAbs (Expr)) {
2274 /* Both sides are constant, remove generated code */
2276 pop (TypeOf (lhst)); /* Clean up the stack */
2278 /* Check for pointer arithmetic */
2279 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2280 /* Left is pointer, right is int, must scale rhs */
2281 Expr->Val -= Expr2.Val * CheckedPSizeOf (lhst);
2282 /* Operate on pointers, result type is a pointer */
2283 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2284 /* Left is pointer, right is pointer, must scale result */
2285 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2286 Error ("Incompatible pointer types");
2288 Expr->Val = (Expr->Val - Expr2.Val) /
2289 CheckedPSizeOf (lhst);
2291 /* Operate on pointers, result type is an integer */
2292 Expr->Type = type_int;
2293 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2294 /* Integer subtraction */
2295 typeadjust (Expr, &Expr2, 1);
2296 Expr->Val -= Expr2.Val;
2299 Error ("Invalid operands for binary operator `-'");
2302 /* Result is constant, condition codes not set */
2303 Expr->Test &= ~E_CC;
2307 /* Left hand side is not constant, right hand side is.
2308 * Remove pushed value from stack.
2311 pop (TypeOf (lhst));
2313 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2314 /* Left is pointer, right is int, must scale rhs */
2315 Expr2.Val *= CheckedPSizeOf (lhst);
2316 /* Operate on pointers, result type is a pointer */
2318 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2319 /* Left is pointer, right is pointer, must scale result */
2320 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2321 Error ("Incompatible pointer types");
2323 rscale = CheckedPSizeOf (lhst);
2325 /* Operate on pointers, result type is an integer */
2327 Expr->Type = type_int;
2328 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2329 /* Integer subtraction */
2330 flags = typeadjust (Expr, &Expr2, 1);
2333 Error ("Invalid operands for binary operator `-'");
2336 /* Do the subtraction */
2337 g_dec (flags | CF_CONST, Expr2.Val);
2339 /* If this was a pointer subtraction, we must scale the result */
2341 g_scale (flags, -rscale);
2344 /* Result is a rvalue in the primary register */
2345 ED_MakeRValExpr (Expr);
2346 Expr->Test &= ~E_CC;
2352 /* Right hand side is not constant. Get the rhs type. */
2355 /* Check for pointer arithmetic */
2356 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2357 /* Left is pointer, right is int, must scale rhs */
2358 g_scale (CF_INT, CheckedPSizeOf (lhst));
2359 /* Operate on pointers, result type is a pointer */
2361 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2362 /* Left is pointer, right is pointer, must scale result */
2363 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2364 Error ("Incompatible pointer types");
2366 rscale = CheckedPSizeOf (lhst);
2368 /* Operate on pointers, result type is an integer */
2370 Expr->Type = type_int;
2371 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2372 /* Integer subtraction. If the left hand side descriptor says that
2373 * the lhs is const, we have to remove this mark, since this is no
2374 * longer true, lhs is on stack instead.
2376 if (ED_IsLocAbs (Expr)) {
2377 ED_MakeRValExpr (Expr);
2379 /* Adjust operand types */
2380 flags = typeadjust (Expr, &Expr2, 0);
2383 Error ("Invalid operands for binary operator `-'");
2386 /* Generate code for the sub (the & is a hack here) */
2387 g_sub (flags & ~CF_CONST, 0);
2389 /* If this was a pointer subtraction, we must scale the result */
2391 g_scale (flags, -rscale);
2394 /* Result is a rvalue in the primary register */
2395 ED_MakeRValExpr (Expr);
2396 Expr->Test &= ~E_CC;
2402 static void hie8 (ExprDesc* Expr)
2403 /* Process + and - binary operators. */
2406 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2407 if (CurTok.Tok == TOK_PLUS) {
2417 static void hie7 (ExprDesc* Expr)
2418 /* Parse << and >>. */
2420 static const GenDesc hie7_ops [] = {
2421 { TOK_SHL, GEN_NOPUSH, g_asl },
2422 { TOK_SHR, GEN_NOPUSH, g_asr },
2423 { TOK_INVALID, 0, 0 }
2427 hie_internal (hie7_ops, Expr, hie8, &UsedGen);
2432 static void hie6 (ExprDesc* Expr)
2433 /* Handle greater-than type comparators */
2435 static const GenDesc hie6_ops [] = {
2436 { TOK_LT, GEN_NOPUSH, g_lt },
2437 { TOK_LE, GEN_NOPUSH, g_le },
2438 { TOK_GE, GEN_NOPUSH, g_ge },
2439 { TOK_GT, GEN_NOPUSH, g_gt },
2440 { TOK_INVALID, 0, 0 }
2442 hie_compare (hie6_ops, Expr, hie7);
2447 static void hie5 (ExprDesc* Expr)
2448 /* Handle == and != */
2450 static const GenDesc hie5_ops[] = {
2451 { TOK_EQ, GEN_NOPUSH, g_eq },
2452 { TOK_NE, GEN_NOPUSH, g_ne },
2453 { TOK_INVALID, 0, 0 }
2455 hie_compare (hie5_ops, Expr, hie6);
2460 static void hie4 (ExprDesc* Expr)
2461 /* Handle & (bitwise and) */
2463 static const GenDesc hie4_ops[] = {
2464 { TOK_AND, GEN_NOPUSH, g_and },
2465 { TOK_INVALID, 0, 0 }
2469 hie_internal (hie4_ops, Expr, hie5, &UsedGen);
2474 static void hie3 (ExprDesc* Expr)
2475 /* Handle ^ (bitwise exclusive or) */
2477 static const GenDesc hie3_ops[] = {
2478 { TOK_XOR, GEN_NOPUSH, g_xor },
2479 { TOK_INVALID, 0, 0 }
2483 hie_internal (hie3_ops, Expr, hie4, &UsedGen);
2488 static void hie2 (ExprDesc* Expr)
2489 /* Handle | (bitwise or) */
2491 static const GenDesc hie2_ops[] = {
2492 { TOK_OR, GEN_NOPUSH, g_or },
2493 { TOK_INVALID, 0, 0 }
2497 hie_internal (hie2_ops, Expr, hie3, &UsedGen);
2502 static void hieAndPP (ExprDesc* Expr)
2503 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2504 * called recursively from the preprocessor.
2509 ConstAbsIntExpr (hie2, Expr);
2510 while (CurTok.Tok == TOK_BOOL_AND) {
2516 ConstAbsIntExpr (hie2, &Expr2);
2518 /* Combine the two */
2519 Expr->Val = (Expr->Val && Expr2.Val);
2525 static void hieOrPP (ExprDesc *Expr)
2526 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2527 * called recursively from the preprocessor.
2532 ConstAbsIntExpr (hieAndPP, Expr);
2533 while (CurTok.Tok == TOK_BOOL_OR) {
2539 ConstAbsIntExpr (hieAndPP, &Expr2);
2541 /* Combine the two */
2542 Expr->Val = (Expr->Val || Expr2.Val);
2548 static void hieAnd (ExprDesc* Expr, unsigned TrueLab, int* BoolOp)
2549 /* Process "exp && exp" */
2555 if (CurTok.Tok == TOK_BOOL_AND) {
2557 /* Tell our caller that we're evaluating a boolean */
2560 /* Get a label that we will use for false expressions */
2561 lab = GetLocalLabel ();
2563 /* If the expr hasn't set condition codes, set the force-test flag */
2564 if ((Expr->Test & E_CC) == 0) {
2565 Expr->Test |= E_FORCETEST;
2568 /* Load the value */
2569 ExprLoad (CF_FORCECHAR, Expr);
2571 /* Generate the jump */
2572 g_falsejump (CF_NONE, lab);
2574 /* Parse more boolean and's */
2575 while (CurTok.Tok == TOK_BOOL_AND) {
2582 if ((Expr2.Test & E_CC) == 0) {
2583 Expr2.Test |= E_FORCETEST;
2585 ExprLoad (CF_FORCECHAR, &Expr2);
2587 /* Do short circuit evaluation */
2588 if (CurTok.Tok == TOK_BOOL_AND) {
2589 g_falsejump (CF_NONE, lab);
2591 /* Last expression - will evaluate to true */
2592 g_truejump (CF_NONE, TrueLab);
2596 /* Define the false jump label here */
2597 g_defcodelabel (lab);
2599 /* The result is an rvalue in primary */
2600 ED_MakeRValExpr (Expr);
2601 Expr->Test |= E_CC; /* Condition codes are set */
2607 static void hieOr (ExprDesc *Expr)
2608 /* Process "exp || exp". */
2611 int BoolOp = 0; /* Did we have a boolean op? */
2612 int AndOp; /* Did we have a && operation? */
2613 unsigned TrueLab; /* Jump to this label if true */
2617 TrueLab = GetLocalLabel ();
2619 /* Call the next level parser */
2620 hieAnd (Expr, TrueLab, &BoolOp);
2622 /* Any boolean or's? */
2623 if (CurTok.Tok == TOK_BOOL_OR) {
2625 /* If the expr hasn't set condition codes, set the force-test flag */
2626 if ((Expr->Test & E_CC) == 0) {
2627 Expr->Test |= E_FORCETEST;
2630 /* Get first expr */
2631 ExprLoad (CF_FORCECHAR, Expr);
2633 /* For each expression jump to TrueLab if true. Beware: If we
2634 * had && operators, the jump is already in place!
2637 g_truejump (CF_NONE, TrueLab);
2640 /* Remember that we had a boolean op */
2643 /* while there's more expr */
2644 while (CurTok.Tok == TOK_BOOL_OR) {
2651 hieAnd (&Expr2, TrueLab, &AndOp);
2652 if ((Expr2.Test & E_CC) == 0) {
2653 Expr2.Test |= E_FORCETEST;
2655 ExprLoad (CF_FORCECHAR, &Expr2);
2657 /* If there is more to come, add shortcut boolean eval. */
2658 g_truejump (CF_NONE, TrueLab);
2662 /* The result is an rvalue in primary */
2663 ED_MakeRValExpr (Expr);
2664 Expr->Test |= E_CC; /* Condition codes are set */
2667 /* If we really had boolean ops, generate the end sequence */
2669 DoneLab = GetLocalLabel ();
2670 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2671 g_falsejump (CF_NONE, DoneLab);
2672 g_defcodelabel (TrueLab);
2673 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2674 g_defcodelabel (DoneLab);
2680 static void hieQuest (ExprDesc* Expr)
2681 /* Parse the ternary operator */
2685 ExprDesc Expr2; /* Expression 2 */
2686 ExprDesc Expr3; /* Expression 3 */
2687 int Expr2IsNULL; /* Expression 2 is a NULL pointer */
2688 int Expr3IsNULL; /* Expression 3 is a NULL pointer */
2689 type* ResultType; /* Type of result */
2692 /* Call the lower level eval routine */
2693 if (Preprocessing) {
2699 /* Check if it's a ternary expression */
2700 if (CurTok.Tok == TOK_QUEST) {
2702 if ((Expr->Test & E_CC) == 0) {
2703 /* Condition codes not set, force a test */
2704 Expr->Test |= E_FORCETEST;
2706 ExprLoad (CF_NONE, Expr);
2707 labf = GetLocalLabel ();
2708 g_falsejump (CF_NONE, labf);
2710 /* Parse second expression. Remember for later if it is a NULL pointer
2711 * expression, then load it into the primary.
2713 ExprWithCheck (hie1, &Expr2);
2714 Expr2IsNULL = ED_IsNullPtr (&Expr2);
2715 if (!IsTypeVoid (Expr2.Type)) {
2716 /* Load it into the primary */
2717 ExprLoad (CF_NONE, &Expr2);
2718 ED_MakeRValExpr (&Expr2);
2720 labt = GetLocalLabel ();
2724 /* Jump here if the first expression was false */
2725 g_defcodelabel (labf);
2727 /* Parse second expression. Remember for later if it is a NULL pointer
2728 * expression, then load it into the primary.
2730 ExprWithCheck (hie1, &Expr3);
2731 Expr3IsNULL = ED_IsNullPtr (&Expr3);
2732 if (!IsTypeVoid (Expr3.Type)) {
2733 /* Load it into the primary */
2734 ExprLoad (CF_NONE, &Expr3);
2735 ED_MakeRValExpr (&Expr3);
2738 /* Check if any conversions are needed, if so, do them.
2739 * Conversion rules for ?: expression are:
2740 * - if both expressions are int expressions, default promotion
2741 * rules for ints apply.
2742 * - if both expressions are pointers of the same type, the
2743 * result of the expression is of this type.
2744 * - if one of the expressions is a pointer and the other is
2745 * a zero constant, the resulting type is that of the pointer
2747 * - if both expressions are void expressions, the result is of
2749 * - all other cases are flagged by an error.
2751 if (IsClassInt (Expr2.Type) && IsClassInt (Expr3.Type)) {
2753 /* Get common type */
2754 ResultType = promoteint (Expr2.Type, Expr3.Type);
2756 /* Convert the third expression to this type if needed */
2757 TypeConversion (&Expr3, ResultType);
2759 /* Setup a new label so that the expr3 code will jump around
2760 * the type cast code for expr2.
2762 labf = GetLocalLabel (); /* Get new label */
2763 g_jump (labf); /* Jump around code */
2765 /* The jump for expr2 goes here */
2766 g_defcodelabel (labt);
2768 /* Create the typecast code for expr2 */
2769 TypeConversion (&Expr2, ResultType);
2771 /* Jump here around the typecase code. */
2772 g_defcodelabel (labf);
2773 labt = 0; /* Mark other label as invalid */
2775 } else if (IsClassPtr (Expr2.Type) && IsClassPtr (Expr3.Type)) {
2776 /* Must point to same type */
2777 if (TypeCmp (Indirect (Expr2.Type), Indirect (Expr3.Type)) < TC_EQUAL) {
2778 Error ("Incompatible pointer types");
2780 /* Result has the common type */
2781 ResultType = Expr2.Type;
2782 } else if (IsClassPtr (Expr2.Type) && Expr3IsNULL) {
2783 /* Result type is pointer, no cast needed */
2784 ResultType = Expr2.Type;
2785 } else if (Expr2IsNULL && IsClassPtr (Expr3.Type)) {
2786 /* Result type is pointer, no cast needed */
2787 ResultType = Expr3.Type;
2788 } else if (IsTypeVoid (Expr2.Type) && IsTypeVoid (Expr3.Type)) {
2789 /* Result type is void */
2790 ResultType = Expr3.Type;
2792 Error ("Incompatible types");
2793 ResultType = Expr2.Type; /* Doesn't matter here */
2796 /* If we don't have the label defined until now, do it */
2798 g_defcodelabel (labt);
2801 /* Setup the target expression */
2802 ED_MakeRValExpr (Expr);
2803 Expr->Type = ResultType;
2809 static void opeq (const GenDesc* Gen, ExprDesc* Expr)
2810 /* Process "op=" operators. */
2817 /* op= can only be used with lvalues */
2818 if (!ED_IsLVal (Expr)) {
2819 Error ("Invalid lvalue in assignment");
2823 /* There must be an integer or pointer on the left side */
2824 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
2825 Error ("Invalid left operand type");
2826 /* Continue. Wrong code will be generated, but the compiler won't
2827 * break, so this is the best error recovery.
2831 /* Skip the operator token */
2834 /* Determine the type of the lhs */
2835 flags = TypeOf (Expr->Type);
2836 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) && IsTypePtr (Expr->Type);
2838 /* Get the lhs address on stack (if needed) */
2841 /* Fetch the lhs into the primary register if needed */
2842 ExprLoad (CF_NONE, Expr);
2844 /* Bring the lhs on stack */
2845 Mark = GetCodePos ();
2848 /* Evaluate the rhs */
2849 if (evalexpr (CF_NONE, hie1, &Expr2) == 0) {
2850 /* The resulting value is a constant. If the generator has the NOPUSH
2851 * flag set, don't push the lhs.
2853 if (Gen->Flags & GEN_NOPUSH) {
2858 /* lhs is a pointer, scale rhs */
2859 Expr2.Val *= CheckedSizeOf (Expr->Type+1);
2862 /* If the lhs is character sized, the operation may be later done
2865 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
2866 flags |= CF_FORCECHAR;
2869 /* Special handling for add and sub - some sort of a hack, but short code */
2870 if (Gen->Func == g_add) {
2871 g_inc (flags | CF_CONST, Expr2.Val);
2872 } else if (Gen->Func == g_sub) {
2873 g_dec (flags | CF_CONST, Expr2.Val);
2875 Gen->Func (flags | CF_CONST, Expr2.Val);
2878 /* rhs is not constant and already in the primary register */
2880 /* lhs is a pointer, scale rhs */
2881 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Expr->Type+1));
2884 /* If the lhs is character sized, the operation may be later done
2887 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
2888 flags |= CF_FORCECHAR;
2891 /* Adjust the types of the operands if needed */
2892 Gen->Func (g_typeadjust (flags, TypeOf (Expr2.Type)), 0);
2895 ED_MakeRValExpr (Expr);
2900 static void addsubeq (const GenDesc* Gen, ExprDesc *Expr)
2901 /* Process the += and -= operators */
2909 /* We're currently only able to handle some adressing modes */
2910 if (ED_GetLoc (Expr) == E_LOC_EXPR || ED_GetLoc (Expr) == E_LOC_PRIMARY) {
2911 /* Use generic routine */
2916 /* We must have an lvalue */
2917 if (ED_IsRVal (Expr)) {
2918 Error ("Invalid lvalue in assignment");
2922 /* There must be an integer or pointer on the left side */
2923 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
2924 Error ("Invalid left operand type");
2925 /* Continue. Wrong code will be generated, but the compiler won't
2926 * break, so this is the best error recovery.
2930 /* Skip the operator */
2933 /* Check if we have a pointer expression and must scale rhs */
2934 MustScale = IsTypePtr (Expr->Type);
2936 /* Initialize the code generator flags */
2940 /* Evaluate the rhs */
2942 if (ED_IsConstAbs (&Expr2)) {
2943 /* The resulting value is a constant. Scale it. */
2945 Expr2.Val *= CheckedSizeOf (Indirect (Expr->Type));
2950 /* Not constant, load into the primary */
2951 ExprLoad (CF_NONE, &Expr2);
2953 /* lhs is a pointer, scale rhs */
2954 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Indirect (Expr->Type)));
2958 /* Setup the code generator flags */
2959 lflags |= TypeOf (Expr->Type) | CF_FORCECHAR;
2960 rflags |= TypeOf (Expr2.Type);
2962 /* Convert the type of the lhs to that of the rhs */
2963 g_typecast (lflags, rflags);
2965 /* Output apropriate code depending on the location */
2966 switch (ED_GetLoc (Expr)) {
2969 /* Absolute: numeric address or const */
2970 lflags |= CF_ABSOLUTE;
2971 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2972 g_addeqstatic (lflags, Expr->Name, Expr->Val, Expr2.Val);
2974 g_subeqstatic (lflags, Expr->Name, Expr->Val, Expr2.Val);
2979 /* Global variable */
2980 lflags |= CF_EXTERNAL;
2981 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2982 g_addeqstatic (lflags, Expr->Name, Expr->Val, Expr2.Val);
2984 g_subeqstatic (lflags, Expr->Name, Expr->Val, Expr2.Val);
2990 /* Static variable or literal in the literal pool */
2991 lflags |= CF_STATIC;
2992 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2993 g_addeqstatic (lflags, Expr->Name, Expr->Val, Expr2.Val);
2995 g_subeqstatic (lflags, Expr->Name, Expr->Val, Expr2.Val);
2999 case E_LOC_REGISTER:
3000 /* Register variable */
3001 lflags |= CF_REGVAR;
3002 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3003 g_addeqstatic (lflags, Expr->Name, Expr->Val, Expr2.Val);
3005 g_subeqstatic (lflags, Expr->Name, Expr->Val, Expr2.Val);
3010 /* Value on the stack */
3011 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3012 g_addeqlocal (lflags, Expr->Val, Expr2.Val);
3014 g_subeqlocal (lflags, Expr->Val, Expr2.Val);
3019 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
3022 /* Expression is a rvalue in the primary now */
3023 ED_MakeRValExpr (Expr);
3028 void hie1 (ExprDesc* Expr)
3029 /* Parse first level of expression hierarchy. */
3032 switch (CurTok.Tok) {
3038 case TOK_PLUS_ASSIGN:
3039 addsubeq (&GenPASGN, Expr);
3042 case TOK_MINUS_ASSIGN:
3043 addsubeq (&GenSASGN, Expr);
3046 case TOK_MUL_ASSIGN:
3047 opeq (&GenMASGN, Expr);
3050 case TOK_DIV_ASSIGN:
3051 opeq (&GenDASGN, Expr);
3054 case TOK_MOD_ASSIGN:
3055 opeq (&GenMOASGN, Expr);
3058 case TOK_SHL_ASSIGN:
3059 opeq (&GenSLASGN, Expr);
3062 case TOK_SHR_ASSIGN:
3063 opeq (&GenSRASGN, Expr);
3066 case TOK_AND_ASSIGN:
3067 opeq (&GenAASGN, Expr);
3070 case TOK_XOR_ASSIGN:
3071 opeq (&GenXOASGN, Expr);
3075 opeq (&GenOASGN, Expr);
3085 void hie0 (ExprDesc *Expr)
3086 /* Parse comma operator. */
3089 while (CurTok.Tok == TOK_COMMA) {
3097 int evalexpr (unsigned Flags, void (*Func) (ExprDesc*), ExprDesc* Expr)
3098 /* Will evaluate an expression via the given function. If the result is a
3099 * constant, 0 is returned and the value is put in the Expr struct. If the
3100 * result is not constant, ExprLoad is called to bring the value into the
3101 * primary register and 1 is returned.
3105 ExprWithCheck (Func, Expr);
3107 /* Check for a constant expression */
3108 if (ED_IsConstAbs (Expr)) {
3109 /* Constant expression */
3112 /* Not constant, load into the primary */
3113 ExprLoad (Flags, Expr);
3120 void Expression0 (ExprDesc* Expr)
3121 /* Evaluate an expression via hie0 and put the result into the primary register */
3123 ExprWithCheck (hie0, Expr);
3124 ExprLoad (CF_NONE, Expr);
3129 void ConstExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3130 /* Will evaluate an expression via the given function. If the result is not
3131 * a constant of some sort, a diagnostic will be printed, and the value is
3132 * replaced by a constant one to make sure there are no internal errors that
3133 * result from this input error.
3136 ExprWithCheck (Func, Expr);
3137 if (!ED_IsConst (Expr)) {
3138 Error ("Constant expression expected");
3139 /* To avoid any compiler errors, make the expression a valid const */
3140 ED_MakeConstAbsInt (Expr, 1);
3146 void BoolExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3147 /* Will evaluate an expression via the given function. If the result is not
3148 * something that may be evaluated in a boolean context, a diagnostic will be
3149 * printed, and the value is replaced by a constant one to make sure there
3150 * are no internal errors that result from this input error.
3153 ExprWithCheck (Func, Expr);
3154 if (!ED_IsBool (Expr)) {
3155 Error ("Boolean expression expected");
3156 /* To avoid any compiler errors, make the expression a valid int */
3157 ED_MakeConstAbsInt (Expr, 1);
3163 void ConstAbsIntExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3164 /* Will evaluate an expression via the given function. If the result is not
3165 * a constant numeric integer value, a diagnostic will be printed, and the
3166 * value is replaced by a constant one to make sure there are no internal
3167 * errors that result from this input error.
3170 ExprWithCheck (Func, Expr);
3171 if (!ED_IsConstAbsInt (Expr)) {
3172 Error ("Constant integer expression expected");
3173 /* To avoid any compiler errors, make the expression a valid const */
3174 ED_MakeConstAbsInt (Expr, 1);