3 * Ullrich von Bassewitz, 21.06.1998
13 #include "debugflag.h"
20 #include "assignment.h"
40 /*****************************************************************************/
42 /*****************************************************************************/
46 /* Generator attributes */
47 #define GEN_NOPUSH 0x01 /* Don't push lhs */
49 /* Map a generator function and its attributes to a token */
51 token_t Tok; /* Token to map to */
52 unsigned Flags; /* Flags for generator function */
53 void (*Func) (unsigned, unsigned long); /* Generator func */
56 /* Descriptors for the operations */
57 static GenDesc GenPASGN = { TOK_PLUS_ASSIGN, GEN_NOPUSH, g_add };
58 static GenDesc GenSASGN = { TOK_MINUS_ASSIGN, GEN_NOPUSH, g_sub };
59 static GenDesc GenMASGN = { TOK_MUL_ASSIGN, GEN_NOPUSH, g_mul };
60 static GenDesc GenDASGN = { TOK_DIV_ASSIGN, GEN_NOPUSH, g_div };
61 static GenDesc GenMOASGN = { TOK_MOD_ASSIGN, GEN_NOPUSH, g_mod };
62 static GenDesc GenSLASGN = { TOK_SHL_ASSIGN, GEN_NOPUSH, g_asl };
63 static GenDesc GenSRASGN = { TOK_SHR_ASSIGN, GEN_NOPUSH, g_asr };
64 static GenDesc GenAASGN = { TOK_AND_ASSIGN, GEN_NOPUSH, g_and };
65 static GenDesc GenXOASGN = { TOK_XOR_ASSIGN, GEN_NOPUSH, g_xor };
66 static GenDesc GenOASGN = { TOK_OR_ASSIGN, GEN_NOPUSH, g_or };
70 /*****************************************************************************/
71 /* Function forwards */
72 /*****************************************************************************/
76 void hie0 (ExprDesc *lval);
77 /* Parse comma operator. */
81 /*****************************************************************************/
82 /* Helper functions */
83 /*****************************************************************************/
87 static unsigned GlobalModeFlags (unsigned Flags)
88 /* Return the addressing mode flags for the variable with the given flags */
90 switch (Flags & E_MASK_LOC) {
91 case E_LOC_GLOBAL: return CF_EXTERNAL;
92 case E_LOC_STATIC: return CF_STATIC;
93 case E_LOC_REGISTER: return CF_REGVAR;
95 Internal ("GlobalModeFlags: Invalid flags value: %u", Flags);
101 void ExprWithCheck (void (*Func) (ExprDesc*), ExprDesc *Expr)
102 /* Call an expression function with checks. */
104 /* Remember the stack pointer */
105 int OldSP = StackPtr;
107 /* Call the expression function */
110 /* Do some checks if code generation is still constistent */
111 if (StackPtr != OldSP) {
114 "Code generation messed up!\n"
115 "StackPtr is %d, should be %d",
118 Internal ("StackPtr is %d, should be %d\n", StackPtr, OldSP);
125 static type* promoteint (type* lhst, type* rhst)
126 /* In an expression with two ints, return the type of the result */
128 /* Rules for integer types:
129 * - If one of the values is a long, the result is long.
130 * - If one of the values is unsigned, the result is also unsigned.
131 * - Otherwise the result is an int.
133 if (IsTypeLong (lhst) || IsTypeLong (rhst)) {
134 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
140 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
150 static unsigned typeadjust (ExprDesc* lhs, ExprDesc* rhs, int NoPush)
151 /* Adjust the two values for a binary operation. lhs is expected on stack or
152 * to be constant, rhs is expected to be in the primary register or constant.
153 * The function will put the type of the result into lhs and return the
154 * code generator flags for the operation.
155 * If NoPush is given, it is assumed that the operation does not expect the lhs
156 * to be on stack, and that lhs is in a register instead.
157 * Beware: The function does only accept int types.
160 unsigned ltype, rtype;
163 /* Get the type strings */
164 type* lhst = lhs->Type;
165 type* rhst = rhs->Type;
167 /* Generate type adjustment code if needed */
168 ltype = TypeOf (lhst);
169 if (ED_IsLocAbs (lhs)) {
173 /* Value is in primary register*/
176 rtype = TypeOf (rhst);
177 if (ED_IsLocAbs (rhs)) {
180 flags = g_typeadjust (ltype, rtype);
182 /* Set the type of the result */
183 lhs->Type = promoteint (lhst, rhst);
185 /* Return the code generator flags */
191 void DefineData (ExprDesc* Expr)
192 /* Output a data definition for the given expression */
194 switch (ED_GetLoc (Expr)) {
197 /* Absolute: numeric address or const */
198 g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->IVal, 0);
202 /* Global variable */
203 g_defdata (CF_EXTERNAL, Expr->Name, Expr->IVal);
208 /* Static variable or literal in the literal pool */
209 g_defdata (CF_STATIC, Expr->Name, Expr->IVal);
213 /* Register variable. Taking the address is usually not
216 if (IS_Get (&AllowRegVarAddr) == 0) {
217 Error ("Cannot take the address of a register variable");
219 g_defdata (CF_REGVAR, Expr->Name, Expr->IVal);
223 Internal ("Unknown constant type: 0x%04X", ED_GetLoc (Expr));
229 static void LoadConstant (unsigned Flags, ExprDesc* Expr)
230 /* Load the primary register with some constant value. */
232 switch (ED_GetLoc (Expr)) {
235 /* Number constant */
236 g_getimmed (Flags | TypeOf (Expr->Type) | CF_CONST, Expr->IVal, 0);
240 /* Global symbol, load address */
241 g_getimmed ((Flags | CF_EXTERNAL) & ~CF_CONST, Expr->Name, Expr->IVal);
246 /* Static symbol or literal, load address */
247 g_getimmed ((Flags | CF_STATIC) & ~CF_CONST, Expr->Name, Expr->IVal);
251 /* Register variable. Taking the address is usually not
254 if (IS_Get (&AllowRegVarAddr) == 0) {
255 Error ("Cannot take the address of a register variable");
257 g_getimmed ((Flags | CF_REGVAR) & ~CF_CONST, Expr->Name, Expr->IVal);
260 g_leasp (Expr->IVal);
264 Internal ("Unknown constant type: %04X", Expr->Flags);
270 static int kcalc (token_t tok, long val1, long val2)
271 /* Calculate an operation with left and right operand constant. */
275 return (val1 == val2);
277 return (val1 != val2);
279 return (val1 < val2);
281 return (val1 <= val2);
283 return (val1 >= val2);
285 return (val1 > val2);
287 return (val1 | val2);
289 return (val1 ^ val2);
291 return (val1 & val2);
293 return (val1 >> val2);
295 return (val1 << val2);
297 return (val1 * val2);
300 Error ("Division by zero");
303 return (val1 / val2);
306 Error ("Modulo operation with zero");
309 return (val1 % val2);
311 Internal ("kcalc: got token 0x%X\n", tok);
318 static const GenDesc* FindGen (token_t Tok, const GenDesc* Table)
319 /* Find a token in a generator table */
321 while (Table->Tok != TOK_INVALID) {
322 if (Table->Tok == Tok) {
332 static int TypeSpecAhead (void)
333 /* Return true if some sort of type is waiting (helper for cast and sizeof()
339 /* There's a type waiting if:
341 * We have an opening paren, and
342 * a. the next token is a type, or
343 * b. the next token is a type qualifier, or
344 * c. the next token is a typedef'd type
346 return CurTok.Tok == TOK_LPAREN && (
347 TokIsType (&NextTok) ||
348 TokIsTypeQual (&NextTok) ||
349 (NextTok.Tok == TOK_IDENT &&
350 (Entry = FindSym (NextTok.Ident)) != 0 &&
351 SymIsTypeDef (Entry)));
356 void PushAddr (const ExprDesc* Expr)
357 /* If the expression contains an address that was somehow evaluated,
358 * push this address on the stack. This is a helper function for all
359 * sorts of implicit or explicit assignment functions where the lvalue
360 * must be saved if it's not constant, before evaluating the rhs.
363 /* Get the address on stack if needed */
364 if (ED_IsLocExpr (Expr)) {
365 /* Push the address (always a pointer) */
372 /*****************************************************************************/
374 /*****************************************************************************/
378 void ExprLoad (unsigned Flags, ExprDesc* Expr)
379 /* Place the result of an expression into the primary register if it is not
383 if (ED_IsLVal (Expr)) {
385 /* Dereferenced lvalue */
386 Flags |= TypeOf (Expr->Type);
387 if (ED_NeedsTest (Expr)) {
391 switch (ED_GetLoc (Expr)) {
394 /* Absolute: numeric address or const */
395 g_getstatic (Flags | CF_ABSOLUTE, Expr->IVal, 0);
399 /* Global variable */
400 g_getstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->IVal);
405 /* Static variable or literal in the literal pool */
406 g_getstatic (Flags | CF_STATIC, Expr->Name, Expr->IVal);
410 /* Register variable */
411 g_getstatic (Flags | CF_REGVAR, Expr->Name, Expr->IVal);
415 /* Value on the stack */
416 g_getlocal (Flags, Expr->IVal);
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->IVal);
432 Internal ("Invalid location in ExprLoad: 0x%04X", ED_GetLoc (Expr));
435 /* Expression was tested */
440 if (ED_IsLocExpr (Expr)) {
441 if (Expr->IVal != 0) {
442 /* We have an expression in the primary plus a constant
443 * offset. Adjust the value in the primary accordingly.
445 Flags |= TypeOf (Expr->Type);
446 g_inc (Flags | CF_CONST, Expr->IVal);
449 /* Constant of some sort, load it into the primary */
450 LoadConstant (Flags, Expr);
453 /* Are we testing this value? */
454 if (ED_NeedsTest (Expr)) {
455 /* Yes, force a test */
456 Flags |= TypeOf (Expr->Type);
465 static unsigned FunctionParamList (FuncDesc* Func)
466 /* Parse a function parameter list and pass the parameters to the called
467 * function. Depending on several criteria this may be done by just pushing
468 * each parameter separately, or creating the parameter frame once and then
469 * storing into this frame.
470 * The function returns the size of the parameters pushed.
475 /* Initialize variables */
476 SymEntry* Param = 0; /* Keep gcc silent */
477 unsigned ParamSize = 0; /* Size of parameters pushed */
478 unsigned ParamCount = 0; /* Number of parameters pushed */
479 unsigned FrameSize = 0; /* Size of parameter frame */
480 unsigned FrameParams = 0; /* Number of params in frame */
481 int FrameOffs = 0; /* Offset into parameter frame */
482 int Ellipsis = 0; /* Function is variadic */
484 /* As an optimization, we may allocate the complete parameter frame at
485 * once instead of pushing each parameter as it comes. We may do that,
488 * - optimizations that increase code size are enabled (allocating the
489 * stack frame at once gives usually larger code).
490 * - we have more than one parameter to push (don't count the last param
491 * for __fastcall__ functions).
493 * The FrameSize variable will contain a value > 0 if storing into a frame
494 * (instead of pushing) is enabled.
497 if (CodeSizeFactor >= 200) {
499 /* Calculate the number and size of the parameters */
500 FrameParams = Func->ParamCount;
501 FrameSize = Func->ParamSize;
502 if (FrameParams > 0 && (Func->Flags & FD_FASTCALL) != 0) {
503 /* Last parameter is not pushed */
504 FrameSize -= CheckedSizeOf (Func->LastParam->Type);
508 /* Do we have more than one parameter in the frame? */
509 if (FrameParams > 1) {
510 /* Okeydokey, setup the frame */
511 FrameOffs = StackPtr;
513 StackPtr -= FrameSize;
515 /* Don't use a preallocated frame */
520 /* Parse the actual parameter list */
521 while (CurTok.Tok != TOK_RPAREN) {
525 /* Count arguments */
528 /* Fetch the pointer to the next argument, check for too many args */
529 if (ParamCount <= Func->ParamCount) {
530 /* Beware: If there are parameters with identical names, they
531 * cannot go into the same symbol table, which means that in this
532 * case of errorneous input, the number of nodes in the symbol
533 * table and ParamCount are NOT equal. We have to handle this case
534 * below to avoid segmentation violations. Since we know that this
535 * problem can only occur if there is more than one parameter,
536 * we will just use the last one.
538 if (ParamCount == 1) {
540 Param = Func->SymTab->SymHead;
541 } else if (Param->NextSym != 0) {
543 Param = Param->NextSym;
544 CHECK ((Param->Flags & SC_PARAM) != 0);
546 } else if (!Ellipsis) {
547 /* Too many arguments. Do we have an open param list? */
548 if ((Func->Flags & FD_VARIADIC) == 0) {
549 /* End of param list reached, no ellipsis */
550 Error ("Too many arguments in function call");
552 /* Assume an ellipsis even in case of errors to avoid an error
553 * message for each other argument.
558 /* Evaluate the parameter expression */
561 /* If we don't have an argument spec, accept anything, otherwise
562 * convert the actual argument to the type needed.
566 /* Convert the argument to the parameter type if needed */
567 TypeConversion (&Expr, Param->Type);
569 /* If we have a prototype, chars may be pushed as chars */
570 Flags |= CF_FORCECHAR;
573 /* Load the value into the primary if it is not already there */
574 ExprLoad (Flags, &Expr);
576 /* Use the type of the argument for the push */
577 Flags |= TypeOf (Expr.Type);
579 /* If this is a fastcall function, don't push the last argument */
580 if (ParamCount != Func->ParamCount || (Func->Flags & FD_FASTCALL) == 0) {
581 unsigned ArgSize = sizeofarg (Flags);
583 /* We have the space already allocated, store in the frame.
584 * Because of invalid type conversions (that have produced an
585 * error before), we can end up here with a non aligned stack
586 * frame. Since no output will be generated anyway, handle
587 * these cases gracefully instead of doing a CHECK.
589 if (FrameSize >= ArgSize) {
590 FrameSize -= ArgSize;
594 FrameOffs -= ArgSize;
596 g_putlocal (Flags | CF_NOKEEP, FrameOffs, Expr.IVal);
598 /* Push the argument */
599 g_push (Flags, Expr.IVal);
602 /* Calculate total parameter size */
603 ParamSize += ArgSize;
606 /* Check for end of argument list */
607 if (CurTok.Tok != TOK_COMMA) {
613 /* Check if we had enough parameters */
614 if (ParamCount < Func->ParamCount) {
615 Error ("Too few arguments in function call");
618 /* The function returns the size of all parameters pushed onto the stack.
619 * However, if there are parameters missing (which is an error and was
620 * flagged by the compiler) AND a stack frame was preallocated above,
621 * we would loose track of the stackpointer and generate an internal error
622 * later. So we correct the value by the parameters that should have been
623 * pushed to avoid an internal compiler error. Since an error was
624 * generated before, no code will be output anyway.
626 return ParamSize + FrameSize;
631 static void FunctionCall (ExprDesc* Expr)
632 /* Perform a function call. */
634 FuncDesc* Func; /* Function descriptor */
635 int IsFuncPtr; /* Flag */
636 int StdFunc; /* Standard function index */
637 unsigned ParamSize; /* Number of parameter bytes */
638 CodeMark Mark = 0; /* Initialize to keep gcc silent */
639 int PtrOffs = 0; /* Offset of function pointer on stack */
640 int IsFastCall = 0; /* True if it's a fast call function */
641 int PtrOnStack = 0; /* True if a pointer copy is on stack */
643 /* Skip the left paren */
646 /* Get a pointer to the function descriptor from the type string */
647 Func = GetFuncDesc (Expr->Type);
649 /* Handle function pointers transparently */
650 IsFuncPtr = IsTypeFuncPtr (Expr->Type);
653 /* Check wether it's a fastcall function that has parameters */
654 IsFastCall = IsFastCallFunc (Expr->Type + 1) && (Func->ParamCount > 0);
656 /* Things may be difficult, depending on where the function pointer
657 * resides. If the function pointer is an expression of some sort
658 * (not a local or global variable), we have to evaluate this
659 * expression now and save the result for later. Since calls to
660 * function pointers may be nested, we must save it onto the stack.
661 * For fastcall functions we do also need to place a copy of the
662 * pointer on stack, since we cannot use a/x.
664 PtrOnStack = IsFastCall || !ED_IsConst (Expr);
667 /* Not a global or local variable, or a fastcall function. Load
668 * the pointer into the primary and mark it as an expression.
670 ExprLoad (CF_NONE, Expr);
671 ED_MakeRValExpr (Expr);
673 /* Remember the code position */
674 Mark = GetCodePos ();
676 /* Push the pointer onto the stack and remember the offset */
681 /* Check for known standard functions and inline them */
682 } else if ((StdFunc = FindStdFunc ((const char*) Expr->Name)) >= 0) {
684 /* Inline this function */
685 HandleStdFunc (StdFunc, Func, Expr);
690 /* Parse the parameter list */
691 ParamSize = FunctionParamList (Func);
693 /* We need the closing paren here */
696 /* Special handling for function pointers */
699 /* If the function is not a fastcall function, load the pointer to
700 * the function into the primary.
704 /* Not a fastcall function - we may use the primary */
706 /* If we have no parameters, the pointer is still in the
707 * primary. Remove the code to push it and correct the
710 if (ParamSize == 0) {
715 /* Load from the saved copy */
716 g_getlocal (CF_PTR, PtrOffs);
719 /* Load from original location */
720 ExprLoad (CF_NONE, Expr);
723 /* Call the function */
724 g_callind (TypeOf (Expr->Type+1), ParamSize, PtrOffs);
728 /* Fastcall function. We cannot use the primary for the function
729 * pointer and must therefore use an offset to the stack location.
730 * Since fastcall functions may never be variadic, we can use the
731 * index register for this purpose.
733 g_callind (CF_LOCAL, ParamSize, PtrOffs);
736 /* If we have a pointer on stack, remove it */
738 g_space (- (int) sizeofarg (CF_PTR));
747 /* Normal function */
748 g_call (TypeOf (Expr->Type), (const char*) Expr->Name, ParamSize);
752 /* The function result is an rvalue in the primary register */
753 ED_MakeRValExpr (Expr);
754 Expr->Type = GetFuncReturn (Expr->Type);
759 static void Primary (ExprDesc* E)
760 /* This is the lowest level of the expression parser. */
764 /* Initialize fields in the expression stucture */
767 /* Character and integer constants. */
768 if (CurTok.Tok == TOK_ICONST || CurTok.Tok == TOK_CCONST) {
769 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
770 E->Type = CurTok.Type;
771 E->IVal = CurTok.IVal;
776 /* Process parenthesized subexpression by calling the whole parser
779 if (CurTok.Tok == TOK_LPAREN) {
786 /* If we run into an identifier in preprocessing mode, we assume that this
787 * is an undefined macro and replace it by a constant value of zero.
789 if (Preprocessing && CurTok.Tok == TOK_IDENT) {
790 ED_MakeConstAbsInt (E, 0);
794 /* All others may only be used if the expression evaluation is not called
795 * recursively by the preprocessor.
798 /* Illegal expression in PP mode */
799 Error ("Preprocessor expression expected");
800 ED_MakeConstAbsInt (E, 1);
804 switch (CurTok.Tok) {
807 /* Identifier. Get a pointer to the symbol table entry */
808 Sym = E->Sym = FindSym (CurTok.Ident);
810 /* Is the symbol known? */
813 /* We found the symbol - skip the name token */
816 /* Check for illegal symbol types */
817 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
818 if (Sym->Flags & SC_TYPE) {
819 /* Cannot use type symbols */
820 Error ("Variable identifier expected");
821 /* Assume an int type to make E valid */
822 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
827 /* Mark the symbol as referenced */
828 Sym->Flags |= SC_REF;
830 /* The expression type is the symbol type */
833 /* Check for legal symbol types */
834 if ((Sym->Flags & SC_CONST) == SC_CONST) {
835 /* Enum or some other numeric constant */
836 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
837 E->IVal = Sym->V.ConstVal;
838 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
840 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
841 E->Name = (unsigned long) Sym->Name;
842 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
843 /* Local variable. If this is a parameter for a variadic
844 * function, we have to add some address calculations, and the
845 * address is not const.
847 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
848 /* Variadic parameter */
849 g_leavariadic (Sym->V.Offs - F_GetParamSize (CurrentFunc));
850 E->Flags = E_LOC_EXPR | E_RTYPE_LVAL;
852 /* Normal parameter */
853 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
854 E->IVal = Sym->V.Offs;
856 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
857 /* Register variable, zero page based */
858 E->Flags = E_LOC_REGISTER | E_RTYPE_LVAL;
859 E->Name = Sym->V.R.RegOffs;
860 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
861 /* Static variable */
862 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
863 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
864 E->Name = (unsigned long) Sym->Name;
866 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
867 E->Name = Sym->V.Label;
870 /* Local static variable */
871 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
872 E->Name = Sym->V.Offs;
875 /* We've made all variables lvalues above. However, this is
876 * not always correct: An array is actually the address of its
877 * first element, which is a rvalue, and a function is a
878 * rvalue, too, because we cannot store anything in a function.
879 * So fix the flags depending on the type.
881 if (IsTypeArray (E->Type) || IsTypeFunc (E->Type)) {
887 /* We did not find the symbol. Remember the name, then skip it */
889 strcpy (Ident, CurTok.Ident);
892 /* IDENT is either an auto-declared function or an undefined variable. */
893 if (CurTok.Tok == TOK_LPAREN) {
894 /* Declare a function returning int. For that purpose, prepare a
895 * function signature for a function having an empty param list
898 Warning ("Function call without a prototype");
899 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
901 E->Flags = E_LOC_GLOBAL | E_RTYPE_RVAL;
902 E->Name = (unsigned long) Sym->Name;
904 /* Undeclared Variable */
905 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
906 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
908 Error ("Undefined symbol: `%s'", Ident);
916 E->Type = GetCharArrayType (GetLiteralPoolOffs () - CurTok.IVal);
917 E->Flags = E_LOC_LITERAL | E_RTYPE_RVAL;
918 E->IVal = CurTok.IVal;
919 E->Name = LiteralPoolLabel;
926 E->Flags = E_LOC_EXPR | E_RTYPE_RVAL;
931 /* Register pseudo variable */
932 E->Type = type_uchar;
933 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
938 /* Register pseudo variable */
940 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
945 /* Register pseudo variable */
946 E->Type = type_ulong;
947 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
952 /* Illegal primary. */
953 Error ("Expression expected");
954 ED_MakeConstAbsInt (E, 1);
961 static void ArrayRef (ExprDesc* Expr)
962 /* Handle an array reference */
972 /* Skip the bracket */
975 /* Get the type of left side */
978 /* We can apply a special treatment for arrays that have a const base
979 * address. This is true for most arrays and will produce a lot better
980 * code. Check if this is a const base address.
982 ConstBaseAddr = (ED_IsLocConst (Expr) || ED_IsLocStack (Expr));
984 /* If we have a constant base, we delay the address fetch */
985 Mark1 = GetCodePos ();
986 Mark2 = 0; /* Silence gcc */
987 if (!ConstBaseAddr) {
988 /* Get a pointer to the array into the primary */
989 ExprLoad (CF_NONE, Expr);
991 /* Get the array pointer on stack. Do not push more than 16
992 * bit, even if this value is greater, since we cannot handle
993 * other than 16bit stuff when doing indexing.
995 Mark2 = GetCodePos ();
999 /* TOS now contains ptr to array elements. Get the subscript. */
1000 ExprWithCheck (hie0, &SubScript);
1002 /* Check the types of array and subscript. We can either have a
1003 * pointer/array to the left, in which case the subscript must be of an
1004 * integer type, or we have an integer to the left, in which case the
1005 * subscript must be a pointer/array.
1006 * Since we do the necessary checking here, we can rely later on the
1009 if (IsClassPtr (Expr->Type)) {
1010 if (!IsClassInt (SubScript.Type)) {
1011 Error ("Array subscript is not an integer");
1012 /* To avoid any compiler errors, make the expression a valid int */
1013 ED_MakeConstAbsInt (&SubScript, 0);
1015 ElementType = Indirect (Expr->Type);
1016 } else if (IsClassInt (Expr->Type)) {
1017 if (!IsClassPtr (SubScript.Type)) {
1018 Error ("Subscripted value is neither array nor pointer");
1019 /* To avoid compiler errors, make the subscript a char[] at
1022 ED_MakeConstAbs (&SubScript, 0, GetCharArrayType (1));
1024 ElementType = Indirect (SubScript.Type);
1026 Error ("Cannot subscript");
1027 /* To avoid compiler errors, fake both the array and the subscript, so
1028 * we can just proceed.
1030 ED_MakeConstAbs (Expr, 0, GetCharArrayType (1));
1031 ED_MakeConstAbsInt (&SubScript, 0);
1032 ElementType = Indirect (Expr->Type);
1035 /* Check if the subscript is constant absolute value */
1036 if (ED_IsConstAbs (&SubScript)) {
1038 /* The array subscript is a numeric constant. If we had pushed the
1039 * array base address onto the stack before, we can remove this value,
1040 * since we can generate expression+offset.
1042 if (!ConstBaseAddr) {
1046 /* Get an array pointer into the primary */
1047 ExprLoad (CF_NONE, Expr);
1050 if (IsClassPtr (Expr->Type)) {
1052 /* Lhs is pointer/array. Scale the subscript value according to
1055 SubScript.IVal *= CheckedSizeOf (ElementType);
1057 /* Remove the address load code */
1060 /* In case of an array, we can adjust the offset of the expression
1061 * already in Expr. If the base address was a constant, we can even
1062 * remove the code that loaded the address into the primary.
1064 if (IsTypeArray (Expr->Type)) {
1066 /* Adjust the offset */
1067 Expr->IVal += SubScript.IVal;
1071 /* It's a pointer, so we do have to load it into the primary
1072 * first (if it's not already there).
1074 if (ConstBaseAddr) {
1075 ExprLoad (CF_NONE, Expr);
1076 ED_MakeRValExpr (Expr);
1079 /* Use the offset */
1080 Expr->IVal = SubScript.IVal;
1085 /* Scale the rhs value according to the element type */
1086 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
1088 /* Add the subscript. Since arrays are indexed by integers,
1089 * we will ignore the true type of the subscript here and
1090 * use always an int. #### Use offset but beware of ExprLoad!
1092 g_inc (CF_INT | CF_CONST, SubScript.IVal);
1098 /* Array subscript is not constant. Load it into the primary */
1099 Mark2 = GetCodePos ();
1100 ExprLoad (CF_NONE, &SubScript);
1103 if (IsClassPtr (Expr->Type)) {
1105 /* Indexing is based on unsigneds, so we will just use the integer
1106 * portion of the index (which is in (e)ax, so there's no further
1109 g_scale (CF_INT, CheckedSizeOf (ElementType));
1113 /* Get the int value on top. If we come here, we're sure, both
1114 * values are 16 bit (the first one was truncated if necessary
1115 * and the second one is a pointer). Note: If ConstBaseAddr is
1116 * true, we don't have a value on stack, so to "swap" both, just
1117 * push the subscript.
1119 if (ConstBaseAddr) {
1121 ExprLoad (CF_NONE, Expr);
1128 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
1132 /* The offset is now in the primary register. It we didn't have a
1133 * constant base address for the lhs, the lhs address is already
1134 * on stack, and we must add the offset. If the base address was
1135 * constant, we call special functions to add the address to the
1138 if (!ConstBaseAddr) {
1140 /* The array base address is on stack and the subscript is in the
1141 * primary. Add both.
1147 /* The subscript is in the primary, and the array base address is
1148 * in Expr. If the subscript has itself a constant address, it is
1149 * often a better idea to reverse again the order of the
1150 * evaluation. This will generate better code if the subscript is
1151 * a byte sized variable. But beware: This is only possible if the
1152 * subscript was not scaled, that is, if this was a byte array
1155 if ((ED_IsLocConst (&SubScript) || ED_IsLocStack (&SubScript)) &&
1156 CheckedSizeOf (ElementType) == SIZEOF_CHAR) {
1160 /* Reverse the order of evaluation */
1161 if (CheckedSizeOf (SubScript.Type) == SIZEOF_CHAR) {
1168 /* Get a pointer to the array into the primary. */
1169 ExprLoad (CF_NONE, Expr);
1171 /* Add the variable */
1172 if (ED_IsLocStack (&SubScript)) {
1173 g_addlocal (Flags, SubScript.IVal);
1175 Flags |= GlobalModeFlags (SubScript.Flags);
1176 g_addstatic (Flags, SubScript.Name, SubScript.IVal);
1179 if (ED_IsLocAbs (Expr)) {
1180 /* Constant numeric address. Just add it */
1181 g_inc (CF_INT, Expr->IVal);
1182 } else if (ED_IsLocStack (Expr)) {
1183 /* Base address is a local variable address */
1184 if (IsTypeArray (Expr->Type)) {
1185 g_addaddr_local (CF_INT, Expr->IVal);
1187 g_addlocal (CF_PTR, Expr->IVal);
1190 /* Base address is a static variable address */
1191 unsigned Flags = CF_INT | GlobalModeFlags (Expr->Flags);
1192 if (IsTypeArray (Expr->Type)) {
1193 g_addaddr_static (Flags, Expr->Name, Expr->IVal);
1195 g_addstatic (Flags, Expr->Name, Expr->IVal);
1203 /* The result is an expression in the primary */
1204 ED_MakeRValExpr (Expr);
1208 /* Result is of element type */
1209 Expr->Type = ElementType;
1211 /* An array element is actually a variable. So the rules for variables
1212 * with respect to the reference type apply: If it's an array, it is
1213 * a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1214 * but an array cannot contain functions).
1216 if (IsTypeArray (Expr->Type)) {
1222 /* Consume the closing bracket */
1228 static void StructRef (ExprDesc* Expr)
1229 /* Process struct field after . or ->. */
1234 /* Skip the token and check for an identifier */
1236 if (CurTok.Tok != TOK_IDENT) {
1237 Error ("Identifier expected");
1238 Expr->Type = type_int;
1242 /* Get the symbol table entry and check for a struct field */
1243 strcpy (Ident, CurTok.Ident);
1245 Field = FindStructField (Expr->Type, Ident);
1247 Error ("Struct/union has no field named `%s'", Ident);
1248 Expr->Type = type_int;
1252 /* If we have a struct pointer that is an lvalue and not already in the
1253 * primary, load it now.
1255 if (ED_IsLVal (Expr) && IsTypePtr (Expr->Type)) {
1257 /* Load into the primary */
1258 ExprLoad (CF_NONE, Expr);
1260 /* Make it an lvalue expression */
1261 ED_MakeLValExpr (Expr);
1264 /* Set the struct field offset */
1265 Expr->IVal += Field->V.Offs;
1267 /* The type is now the type of the field */
1268 Expr->Type = Field->Type;
1270 /* An struct member is actually a variable. So the rules for variables
1271 * with respect to the reference type apply: If it's an array, it is
1272 * a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1273 * but a struct field cannot be a function).
1275 if (IsTypeArray (Expr->Type)) {
1284 static void hie11 (ExprDesc *Expr)
1285 /* Handle compound types (structs and arrays) */
1287 /* Evaluate the lhs */
1290 /* Check for a rhs */
1291 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN ||
1292 CurTok.Tok == TOK_DOT || CurTok.Tok == TOK_PTR_REF) {
1294 switch (CurTok.Tok) {
1297 /* Array reference */
1302 /* Function call. */
1303 if (!IsTypeFunc (Expr->Type) && !IsTypeFuncPtr (Expr->Type)) {
1304 /* Not a function */
1305 Error ("Illegal function call");
1306 /* Force the type to be a implicitly defined function, one
1307 * returning an int and taking any number of arguments.
1308 * Since we don't have a name, place it at absolute address
1311 ED_MakeConstAbs (Expr, 0, GetImplicitFuncType ());
1313 /* Call the function */
1314 FunctionCall (Expr);
1318 if (!IsClassStruct (Expr->Type)) {
1319 Error ("Struct expected");
1325 /* If we have an array, convert it to pointer to first element */
1326 if (IsTypeArray (Expr->Type)) {
1327 Expr->Type = ArrayToPtr (Expr->Type);
1329 if (!IsClassPtr (Expr->Type) || !IsClassStruct (Indirect (Expr->Type))) {
1330 Error ("Struct pointer expected");
1336 Internal ("Invalid token in hie11: %d", CurTok.Tok);
1344 void Store (ExprDesc* Expr, const type* StoreType)
1345 /* Store the primary register into the location denoted by Expr. If StoreType
1346 * is given, use this type when storing instead of Expr->Type. If StoreType
1347 * is NULL, use Expr->Type instead.
1352 /* If StoreType was not given, use Expr->Type instead */
1353 if (StoreType == 0) {
1354 StoreType = Expr->Type;
1357 /* Prepare the code generator flags */
1358 Flags = TypeOf (StoreType);
1360 /* Do the store depending on the location */
1361 switch (ED_GetLoc (Expr)) {
1364 /* Absolute: numeric address or const */
1365 g_putstatic (Flags | CF_ABSOLUTE, Expr->IVal, 0);
1369 /* Global variable */
1370 g_putstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->IVal);
1375 /* Static variable or literal in the literal pool */
1376 g_putstatic (Flags | CF_STATIC, Expr->Name, Expr->IVal);
1379 case E_LOC_REGISTER:
1380 /* Register variable */
1381 g_putstatic (Flags | CF_REGVAR, Expr->Name, Expr->IVal);
1385 /* Value on the stack */
1386 g_putlocal (Flags, Expr->IVal, 0);
1390 /* The primary register (value is already there) */
1391 /* ### Do we need a test here if the flag is set? */
1395 /* An expression in the primary register */
1396 g_putind (Flags, Expr->IVal);
1400 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
1403 /* Assume that each one of the stores will invalidate CC */
1404 ED_MarkAsUntested (Expr);
1409 static void PreInc (ExprDesc* Expr)
1410 /* Handle the preincrement operators */
1415 /* Skip the operator token */
1418 /* Evaluate the expression and check that it is an lvalue */
1420 if (!ED_IsLVal (Expr)) {
1421 Error ("Invalid lvalue");
1425 /* Get the data type */
1426 Flags = TypeOf (Expr->Type) | CF_FORCECHAR | CF_CONST;
1428 /* Get the increment value in bytes */
1429 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1431 /* Check the location of the data */
1432 switch (ED_GetLoc (Expr)) {
1435 /* Absolute: numeric address or const */
1436 g_addeqstatic (Flags | CF_ABSOLUTE, Expr->IVal, 0, Val);
1440 /* Global variable */
1441 g_addeqstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->IVal, Val);
1446 /* Static variable or literal in the literal pool */
1447 g_addeqstatic (Flags | CF_STATIC, Expr->Name, Expr->IVal, Val);
1450 case E_LOC_REGISTER:
1451 /* Register variable */
1452 g_addeqstatic (Flags | CF_REGVAR, Expr->Name, Expr->IVal, Val);
1456 /* Value on the stack */
1457 g_addeqlocal (Flags, Expr->IVal, Val);
1461 /* The primary register */
1466 /* An expression in the primary register */
1467 g_addeqind (Flags, Expr->IVal, Val);
1471 Internal ("Invalid location in PreInc(): 0x%04X", ED_GetLoc (Expr));
1474 /* Result is an expression, no reference */
1475 ED_MakeRValExpr (Expr);
1480 static void PreDec (ExprDesc* Expr)
1481 /* Handle the predecrement operators */
1486 /* Skip the operator token */
1489 /* Evaluate the expression and check that it is an lvalue */
1491 if (!ED_IsLVal (Expr)) {
1492 Error ("Invalid lvalue");
1496 /* Get the data type */
1497 Flags = TypeOf (Expr->Type) | CF_FORCECHAR | CF_CONST;
1499 /* Get the increment value in bytes */
1500 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1502 /* Check the location of the data */
1503 switch (ED_GetLoc (Expr)) {
1506 /* Absolute: numeric address or const */
1507 g_subeqstatic (Flags | CF_ABSOLUTE, Expr->IVal, 0, Val);
1511 /* Global variable */
1512 g_subeqstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->IVal, Val);
1517 /* Static variable or literal in the literal pool */
1518 g_subeqstatic (Flags | CF_STATIC, Expr->Name, Expr->IVal, Val);
1521 case E_LOC_REGISTER:
1522 /* Register variable */
1523 g_subeqstatic (Flags | CF_REGVAR, Expr->Name, Expr->IVal, Val);
1527 /* Value on the stack */
1528 g_subeqlocal (Flags, Expr->IVal, Val);
1532 /* The primary register */
1537 /* An expression in the primary register */
1538 g_subeqind (Flags, Expr->IVal, Val);
1542 Internal ("Invalid location in PreDec(): 0x%04X", ED_GetLoc (Expr));
1545 /* Result is an expression, no reference */
1546 ED_MakeRValExpr (Expr);
1551 static void PostIncDec (ExprDesc* Expr, void (*inc) (unsigned, unsigned long))
1552 /* Handle i-- and i++ */
1558 /* The expression to increment must be an lvalue */
1559 if (!ED_IsLVal (Expr)) {
1560 Error ("Invalid lvalue");
1564 /* Get the data type */
1565 Flags = TypeOf (Expr->Type);
1567 /* Push the address if needed */
1570 /* Fetch the value and save it (since it's the result of the expression) */
1571 ExprLoad (CF_NONE, Expr);
1572 g_save (Flags | CF_FORCECHAR);
1574 /* If we have a pointer expression, increment by the size of the type */
1575 if (IsTypePtr (Expr->Type)) {
1576 inc (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1578 inc (Flags | CF_CONST | CF_FORCECHAR, 1);
1581 /* Store the result back */
1584 /* Restore the original value in the primary register */
1585 g_restore (Flags | CF_FORCECHAR);
1587 /* The result is always an expression, no reference */
1588 ED_MakeRValExpr (Expr);
1593 static void UnaryOp (ExprDesc* Expr)
1594 /* Handle unary -/+ and ~ */
1598 /* Remember the operator token and skip it */
1599 token_t Tok = CurTok.Tok;
1602 /* Get the expression */
1605 /* We can only handle integer types */
1606 if (!IsClassInt (Expr->Type)) {
1607 Error ("Argument must have integer type");
1608 ED_MakeConstAbsInt (Expr, 1);
1611 /* Check for a constant expression */
1612 if (ED_IsConstAbs (Expr)) {
1613 /* Value is constant */
1615 case TOK_MINUS: Expr->IVal = -Expr->IVal; break;
1616 case TOK_PLUS: break;
1617 case TOK_COMP: Expr->IVal = ~Expr->IVal; break;
1618 default: Internal ("Unexpected token: %d", Tok);
1621 /* Value is not constant */
1622 ExprLoad (CF_NONE, Expr);
1624 /* Get the type of the expression */
1625 Flags = TypeOf (Expr->Type);
1627 /* Handle the operation */
1629 case TOK_MINUS: g_neg (Flags); break;
1630 case TOK_PLUS: break;
1631 case TOK_COMP: g_com (Flags); break;
1632 default: Internal ("Unexpected token: %d", Tok);
1635 /* The result is a rvalue in the primary */
1636 ED_MakeRValExpr (Expr);
1642 void hie10 (ExprDesc* Expr)
1643 /* Handle ++, --, !, unary - etc. */
1647 switch (CurTok.Tok) {
1665 if (evalexpr (CF_NONE, hie10, Expr) == 0) {
1666 /* Constant expression */
1667 Expr->IVal = !Expr->IVal;
1669 g_bneg (TypeOf (Expr->Type));
1670 ED_MakeRValExpr (Expr);
1671 ED_TestDone (Expr); /* bneg will set cc */
1677 ExprWithCheck (hie10, Expr);
1678 if (ED_IsLVal (Expr) || !(ED_IsLocConst (Expr) || ED_IsLocStack (Expr))) {
1679 /* Not a const, load it into the primary and make it a
1682 ExprLoad (CF_NONE, Expr);
1683 ED_MakeRValExpr (Expr);
1685 /* If the expression is already a pointer to function, the
1686 * additional dereferencing operator must be ignored.
1688 if (IsTypeFuncPtr (Expr->Type)) {
1689 /* Expression not storable */
1692 if (IsClassPtr (Expr->Type)) {
1693 Expr->Type = Indirect (Expr->Type);
1695 Error ("Illegal indirection");
1703 ExprWithCheck (hie10, Expr);
1704 /* The & operator may be applied to any lvalue, and it may be
1705 * applied to functions, even if they're no lvalues.
1707 if (ED_IsRVal (Expr) && !IsTypeFunc (Expr->Type)) {
1708 /* Allow the & operator with an array */
1709 if (!IsTypeArray (Expr->Type)) {
1710 Error ("Illegal address");
1713 Expr->Type = PointerTo (Expr->Type);
1720 if (TypeSpecAhead ()) {
1721 type Type[MAXTYPELEN];
1723 Size = CheckedSizeOf (ParseType (Type));
1726 /* Remember the output queue pointer */
1727 CodeMark Mark = GetCodePos ();
1729 Size = CheckedSizeOf (Expr->Type);
1730 /* Remove any generated code */
1733 ED_MakeConstAbs (Expr, Size, type_size_t);
1734 ED_MarkAsUntested (Expr);
1738 if (TypeSpecAhead ()) {
1748 /* Handle post increment */
1749 if (CurTok.Tok == TOK_INC) {
1750 PostIncDec (Expr, g_inc);
1751 } else if (CurTok.Tok == TOK_DEC) {
1752 PostIncDec (Expr, g_dec);
1762 static void hie_internal (const GenDesc* Ops, /* List of generators */
1764 void (*hienext) (ExprDesc*),
1766 /* Helper function */
1772 token_t Tok; /* The operator token */
1773 unsigned ltype, type;
1774 int rconst; /* Operand is a constant */
1780 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1782 /* Tell the caller that we handled it's ops */
1785 /* All operators that call this function expect an int on the lhs */
1786 if (!IsClassInt (Expr->Type)) {
1787 Error ("Integer expression expected");
1790 /* Remember the operator token, then skip it */
1794 /* Get the lhs on stack */
1795 Mark1 = GetCodePos ();
1796 ltype = TypeOf (Expr->Type);
1797 if (ED_IsConstAbs (Expr)) {
1798 /* Constant value */
1799 Mark2 = GetCodePos ();
1800 g_push (ltype | CF_CONST, Expr->IVal);
1802 /* Value not constant */
1803 ExprLoad (CF_NONE, Expr);
1804 Mark2 = GetCodePos ();
1808 /* Get the right hand side */
1809 rconst = (evalexpr (CF_NONE, hienext, &Expr2) == 0);
1811 /* Check the type of the rhs */
1812 if (!IsClassInt (Expr2.Type)) {
1813 Error ("Integer expression expected");
1816 /* Check for const operands */
1817 if (ED_IsConstAbs (Expr) && rconst) {
1819 /* Both operands are constant, remove the generated code */
1823 /* Evaluate the result */
1824 Expr->IVal = kcalc (Tok, Expr->IVal, Expr2.IVal);
1826 /* Get the type of the result */
1827 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1831 /* If the right hand side is constant, and the generator function
1832 * expects the lhs in the primary, remove the push of the primary
1835 unsigned rtype = TypeOf (Expr2.Type);
1838 /* Second value is constant - check for div */
1841 if (Tok == TOK_DIV && Expr2.IVal == 0) {
1842 Error ("Division by zero");
1843 } else if (Tok == TOK_MOD && Expr2.IVal == 0) {
1844 Error ("Modulo operation with zero");
1846 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1849 ltype |= CF_REG; /* Value is in register */
1853 /* Determine the type of the operation result. */
1854 type |= g_typeadjust (ltype, rtype);
1855 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1858 Gen->Func (type, Expr2.IVal);
1860 /* We have a rvalue in the primary now */
1861 ED_MakeRValExpr (Expr);
1868 static void hie_compare (const GenDesc* Ops, /* List of generators */
1870 void (*hienext) (ExprDesc*))
1871 /* Helper function for the compare operators */
1877 token_t tok; /* The operator token */
1879 int rconst; /* Operand is a constant */
1884 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1886 /* Remember the operator token, then skip it */
1890 /* Get the lhs on stack */
1891 Mark1 = GetCodePos ();
1892 ltype = TypeOf (Expr->Type);
1893 if (ED_IsConstAbs (Expr)) {
1894 /* Constant value */
1895 Mark2 = GetCodePos ();
1896 g_push (ltype | CF_CONST, Expr->IVal);
1898 /* Value not constant */
1899 ExprLoad (CF_NONE, Expr);
1900 Mark2 = GetCodePos ();
1904 /* Get the right hand side */
1905 rconst = (evalexpr (CF_NONE, hienext, &Expr2) == 0);
1907 /* Make sure, the types are compatible */
1908 if (IsClassInt (Expr->Type)) {
1909 if (!IsClassInt (Expr2.Type) && !(IsClassPtr(Expr2.Type) && ED_IsNullPtr(Expr))) {
1910 Error ("Incompatible types");
1912 } else if (IsClassPtr (Expr->Type)) {
1913 if (IsClassPtr (Expr2.Type)) {
1914 /* Both pointers are allowed in comparison if they point to
1915 * the same type, or if one of them is a void pointer.
1917 type* left = Indirect (Expr->Type);
1918 type* right = Indirect (Expr2.Type);
1919 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1920 /* Incomatible pointers */
1921 Error ("Incompatible types");
1923 } else if (!ED_IsNullPtr (&Expr2)) {
1924 Error ("Incompatible types");
1928 /* Check for const operands */
1929 if (ED_IsConstAbs (Expr) && rconst) {
1931 /* Both operands are constant, remove the generated code */
1935 /* Evaluate the result */
1936 Expr->IVal = kcalc (tok, Expr->IVal, Expr2.IVal);
1940 /* If the right hand side is constant, and the generator function
1941 * expects the lhs in the primary, remove the push of the primary
1947 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1950 ltype |= CF_REG; /* Value is in register */
1954 /* Determine the type of the operation result. If the left
1955 * operand is of type char and the right is a constant, or
1956 * if both operands are of type char, we will encode the
1957 * operation as char operation. Otherwise the default
1958 * promotions are used.
1960 if (IsTypeChar (Expr->Type) && (IsTypeChar (Expr2.Type) || rconst)) {
1962 if (IsSignUnsigned (Expr->Type) || IsSignUnsigned (Expr2.Type)) {
1963 flags |= CF_UNSIGNED;
1966 flags |= CF_FORCECHAR;
1969 unsigned rtype = TypeOf (Expr2.Type) | (flags & CF_CONST);
1970 flags |= g_typeadjust (ltype, rtype);
1974 Gen->Func (flags, Expr2.IVal);
1976 /* The result is an rvalue in the primary */
1977 ED_MakeRValExpr (Expr);
1980 /* Result type is always int */
1981 Expr->Type = type_int;
1983 /* Condition codes are set */
1990 static void hie9 (ExprDesc *Expr)
1991 /* Process * and / operators. */
1993 static const GenDesc hie9_ops[] = {
1994 { TOK_STAR, GEN_NOPUSH, g_mul },
1995 { TOK_DIV, GEN_NOPUSH, g_div },
1996 { TOK_MOD, GEN_NOPUSH, g_mod },
1997 { TOK_INVALID, 0, 0 }
2001 hie_internal (hie9_ops, Expr, hie10, &UsedGen);
2006 static void parseadd (ExprDesc* Expr)
2007 /* Parse an expression with the binary plus operator. Expr contains the
2008 * unprocessed left hand side of the expression and will contain the
2009 * result of the expression on return.
2013 unsigned flags; /* Operation flags */
2014 CodeMark Mark; /* Remember code position */
2015 type* lhst; /* Type of left hand side */
2016 type* rhst; /* Type of right hand side */
2019 /* Skip the PLUS token */
2022 /* Get the left hand side type, initialize operation flags */
2026 /* Check for constness on both sides */
2027 if (ED_IsConst (Expr)) {
2029 /* The left hand side is a constant of some sort. Good. Get rhs */
2031 if (ED_IsConstAbs (&Expr2)) {
2033 /* Right hand side is a constant numeric value. Get the rhs type */
2036 /* Both expressions are constants. Check for pointer arithmetic */
2037 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2038 /* Left is pointer, right is int, must scale rhs */
2039 Expr->IVal += Expr2.IVal * CheckedPSizeOf (lhst);
2040 /* Result type is a pointer */
2041 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2042 /* Left is int, right is pointer, must scale lhs */
2043 Expr->IVal = Expr->IVal * CheckedPSizeOf (rhst) + Expr2.IVal;
2044 /* Result type is a pointer */
2045 Expr->Type = Expr2.Type;
2046 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2047 /* Integer addition */
2048 Expr->IVal += Expr2.IVal;
2049 typeadjust (Expr, &Expr2, 1);
2052 Error ("Invalid operands for binary operator `+'");
2057 /* lhs is a constant and rhs is not constant. Load rhs into
2060 ExprLoad (CF_NONE, &Expr2);
2062 /* Beware: The check above (for lhs) lets not only pass numeric
2063 * constants, but also constant addresses (labels), maybe even
2064 * with an offset. We have to check for that here.
2067 /* First, get the rhs type. */
2071 if (ED_IsLocAbs (Expr)) {
2072 /* A numerical constant */
2075 /* Constant address label */
2076 flags |= GlobalModeFlags (Expr->Flags) | CF_CONSTADDR;
2079 /* Check for pointer arithmetic */
2080 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2081 /* Left is pointer, right is int, must scale rhs */
2082 g_scale (CF_INT, CheckedPSizeOf (lhst));
2083 /* Operate on pointers, result type is a pointer */
2085 /* Generate the code for the add */
2086 if (ED_GetLoc (Expr) == E_LOC_ABS) {
2087 /* Numeric constant */
2088 g_inc (flags, Expr->IVal);
2090 /* Constant address */
2091 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2093 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2095 /* Left is int, right is pointer, must scale lhs. */
2096 unsigned ScaleFactor = CheckedPSizeOf (rhst);
2098 /* Operate on pointers, result type is a pointer */
2100 Expr->Type = Expr2.Type;
2102 /* Since we do already have rhs in the primary, if lhs is
2103 * not a numeric constant, and the scale factor is not one
2104 * (no scaling), we must take the long way over the stack.
2106 if (ED_IsLocAbs (Expr)) {
2107 /* Numeric constant, scale lhs */
2108 Expr->IVal *= ScaleFactor;
2109 /* Generate the code for the add */
2110 g_inc (flags, Expr->IVal);
2111 } else if (ScaleFactor == 1) {
2112 /* Constant address but no need to scale */
2113 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2115 /* Constant address that must be scaled */
2116 g_push (TypeOf (Expr2.Type), 0); /* rhs --> stack */
2117 g_getimmed (flags, Expr->Name, Expr->IVal);
2118 g_scale (CF_PTR, ScaleFactor);
2121 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2122 /* Integer addition */
2123 flags |= typeadjust (Expr, &Expr2, 1);
2124 /* Generate the code for the add */
2125 if (ED_IsLocAbs (Expr)) {
2126 /* Numeric constant */
2127 g_inc (flags, Expr->IVal);
2129 /* Constant address */
2130 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2134 Error ("Invalid operands for binary operator `+'");
2137 /* Result is a rvalue in primary register */
2138 ED_MakeRValExpr (Expr);
2143 /* Left hand side is not constant. Get the value onto the stack. */
2144 ExprLoad (CF_NONE, Expr); /* --> primary register */
2145 Mark = GetCodePos ();
2146 g_push (TypeOf (Expr->Type), 0); /* --> stack */
2148 /* Evaluate the rhs */
2149 if (evalexpr (CF_NONE, hie9, &Expr2) == 0) {
2151 /* Right hand side is a constant. Get the rhs type */
2154 /* Remove pushed value from stack */
2156 pop (TypeOf (Expr->Type));
2158 /* Check for pointer arithmetic */
2159 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2160 /* Left is pointer, right is int, must scale rhs */
2161 Expr2.IVal *= CheckedPSizeOf (lhst);
2162 /* Operate on pointers, result type is a pointer */
2164 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2165 /* Left is int, right is pointer, must scale lhs (ptr only) */
2166 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2167 /* Operate on pointers, result type is a pointer */
2169 Expr->Type = Expr2.Type;
2170 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2171 /* Integer addition */
2172 flags = typeadjust (Expr, &Expr2, 1);
2175 Error ("Invalid operands for binary operator `+'");
2178 /* Generate code for the add */
2179 g_inc (flags | CF_CONST, Expr2.IVal);
2183 /* lhs and rhs are not constant. Get the rhs type. */
2186 /* Check for pointer arithmetic */
2187 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2188 /* Left is pointer, right is int, must scale rhs */
2189 g_scale (CF_INT, CheckedPSizeOf (lhst));
2190 /* Operate on pointers, result type is a pointer */
2192 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2193 /* Left is int, right is pointer, must scale lhs */
2194 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2195 g_swap (CF_INT); /* Swap TOS and primary */
2196 g_scale (CF_INT, CheckedPSizeOf (rhst));
2197 /* Operate on pointers, result type is a pointer */
2199 Expr->Type = Expr2.Type;
2200 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2201 /* Integer addition. Note: Result is never constant.
2202 * Problem here is that typeadjust does not know if the
2203 * variable is an rvalue or lvalue, so if both operands
2204 * are dereferenced constant numeric addresses, typeadjust
2205 * thinks the operation works on constants. Removing
2206 * CF_CONST here means handling the symptoms, however, the
2207 * whole parser is such a mess that I fear to break anything
2208 * when trying to apply another solution.
2210 flags = typeadjust (Expr, &Expr2, 0) & ~CF_CONST;
2213 Error ("Invalid operands for binary operator `+'");
2216 /* Generate code for the add */
2221 /* Result is a rvalue in primary register */
2222 ED_MakeRValExpr (Expr);
2225 /* Condition codes not set */
2226 ED_MarkAsUntested (Expr);
2232 static void parsesub (ExprDesc* Expr)
2233 /* Parse an expression with the binary minus operator. Expr contains the
2234 * unprocessed left hand side of the expression and will contain the
2235 * result of the expression on return.
2239 unsigned flags; /* Operation flags */
2240 type* lhst; /* Type of left hand side */
2241 type* rhst; /* Type of right hand side */
2242 CodeMark Mark1; /* Save position of output queue */
2243 CodeMark Mark2; /* Another position in the queue */
2244 int rscale; /* Scale factor for the result */
2247 /* Skip the MINUS token */
2250 /* Get the left hand side type, initialize operation flags */
2253 rscale = 1; /* Scale by 1, that is, don't scale */
2255 /* Remember the output queue position, then bring the value onto the stack */
2256 Mark1 = GetCodePos ();
2257 ExprLoad (CF_NONE, Expr); /* --> primary register */
2258 Mark2 = GetCodePos ();
2259 g_push (TypeOf (lhst), 0); /* --> stack */
2261 /* Parse the right hand side */
2262 if (evalexpr (CF_NONE, hie9, &Expr2) == 0) {
2264 /* The right hand side is constant. Get the rhs type. */
2267 /* Check left hand side */
2268 if (ED_IsConstAbs (Expr)) {
2270 /* Both sides are constant, remove generated code */
2272 pop (TypeOf (lhst)); /* Clean up the stack */
2274 /* Check for pointer arithmetic */
2275 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2276 /* Left is pointer, right is int, must scale rhs */
2277 Expr->IVal -= Expr2.IVal * CheckedPSizeOf (lhst);
2278 /* Operate on pointers, result type is a pointer */
2279 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2280 /* Left is pointer, right is pointer, must scale result */
2281 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2282 Error ("Incompatible pointer types");
2284 Expr->IVal = (Expr->IVal - Expr2.IVal) /
2285 CheckedPSizeOf (lhst);
2287 /* Operate on pointers, result type is an integer */
2288 Expr->Type = type_int;
2289 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2290 /* Integer subtraction */
2291 typeadjust (Expr, &Expr2, 1);
2292 Expr->IVal -= Expr2.IVal;
2295 Error ("Invalid operands for binary operator `-'");
2298 /* Result is constant, condition codes not set */
2299 ED_MarkAsUntested (Expr);
2303 /* Left hand side is not constant, right hand side is.
2304 * Remove pushed value from stack.
2307 pop (TypeOf (lhst));
2309 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2310 /* Left is pointer, right is int, must scale rhs */
2311 Expr2.IVal *= CheckedPSizeOf (lhst);
2312 /* Operate on pointers, result type is a pointer */
2314 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2315 /* Left is pointer, right is pointer, must scale result */
2316 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2317 Error ("Incompatible pointer types");
2319 rscale = CheckedPSizeOf (lhst);
2321 /* Operate on pointers, result type is an integer */
2323 Expr->Type = type_int;
2324 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2325 /* Integer subtraction */
2326 flags = typeadjust (Expr, &Expr2, 1);
2329 Error ("Invalid operands for binary operator `-'");
2332 /* Do the subtraction */
2333 g_dec (flags | CF_CONST, Expr2.IVal);
2335 /* If this was a pointer subtraction, we must scale the result */
2337 g_scale (flags, -rscale);
2340 /* Result is a rvalue in the primary register */
2341 ED_MakeRValExpr (Expr);
2342 ED_MarkAsUntested (Expr);
2348 /* Right hand side is not constant. Get the rhs type. */
2351 /* Check for pointer arithmetic */
2352 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2353 /* Left is pointer, right is int, must scale rhs */
2354 g_scale (CF_INT, CheckedPSizeOf (lhst));
2355 /* Operate on pointers, result type is a pointer */
2357 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2358 /* Left is pointer, right is pointer, must scale result */
2359 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2360 Error ("Incompatible pointer types");
2362 rscale = CheckedPSizeOf (lhst);
2364 /* Operate on pointers, result type is an integer */
2366 Expr->Type = type_int;
2367 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2368 /* Integer subtraction. If the left hand side descriptor says that
2369 * the lhs is const, we have to remove this mark, since this is no
2370 * longer true, lhs is on stack instead.
2372 if (ED_IsLocAbs (Expr)) {
2373 ED_MakeRValExpr (Expr);
2375 /* Adjust operand types */
2376 flags = typeadjust (Expr, &Expr2, 0);
2379 Error ("Invalid operands for binary operator `-'");
2382 /* Generate code for the sub (the & is a hack here) */
2383 g_sub (flags & ~CF_CONST, 0);
2385 /* If this was a pointer subtraction, we must scale the result */
2387 g_scale (flags, -rscale);
2390 /* Result is a rvalue in the primary register */
2391 ED_MakeRValExpr (Expr);
2392 ED_MarkAsUntested (Expr);
2398 static void hie8 (ExprDesc* Expr)
2399 /* Process + and - binary operators. */
2402 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2403 if (CurTok.Tok == TOK_PLUS) {
2413 static void hie7 (ExprDesc* Expr)
2414 /* Parse << and >>. */
2416 static const GenDesc hie7_ops [] = {
2417 { TOK_SHL, GEN_NOPUSH, g_asl },
2418 { TOK_SHR, GEN_NOPUSH, g_asr },
2419 { TOK_INVALID, 0, 0 }
2423 hie_internal (hie7_ops, Expr, hie8, &UsedGen);
2428 static void hie6 (ExprDesc* Expr)
2429 /* Handle greater-than type comparators */
2431 static const GenDesc hie6_ops [] = {
2432 { TOK_LT, GEN_NOPUSH, g_lt },
2433 { TOK_LE, GEN_NOPUSH, g_le },
2434 { TOK_GE, GEN_NOPUSH, g_ge },
2435 { TOK_GT, GEN_NOPUSH, g_gt },
2436 { TOK_INVALID, 0, 0 }
2438 hie_compare (hie6_ops, Expr, hie7);
2443 static void hie5 (ExprDesc* Expr)
2444 /* Handle == and != */
2446 static const GenDesc hie5_ops[] = {
2447 { TOK_EQ, GEN_NOPUSH, g_eq },
2448 { TOK_NE, GEN_NOPUSH, g_ne },
2449 { TOK_INVALID, 0, 0 }
2451 hie_compare (hie5_ops, Expr, hie6);
2456 static void hie4 (ExprDesc* Expr)
2457 /* Handle & (bitwise and) */
2459 static const GenDesc hie4_ops[] = {
2460 { TOK_AND, GEN_NOPUSH, g_and },
2461 { TOK_INVALID, 0, 0 }
2465 hie_internal (hie4_ops, Expr, hie5, &UsedGen);
2470 static void hie3 (ExprDesc* Expr)
2471 /* Handle ^ (bitwise exclusive or) */
2473 static const GenDesc hie3_ops[] = {
2474 { TOK_XOR, GEN_NOPUSH, g_xor },
2475 { TOK_INVALID, 0, 0 }
2479 hie_internal (hie3_ops, Expr, hie4, &UsedGen);
2484 static void hie2 (ExprDesc* Expr)
2485 /* Handle | (bitwise or) */
2487 static const GenDesc hie2_ops[] = {
2488 { TOK_OR, GEN_NOPUSH, g_or },
2489 { TOK_INVALID, 0, 0 }
2493 hie_internal (hie2_ops, Expr, hie3, &UsedGen);
2498 static void hieAndPP (ExprDesc* Expr)
2499 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2500 * called recursively from the preprocessor.
2505 ConstAbsIntExpr (hie2, Expr);
2506 while (CurTok.Tok == TOK_BOOL_AND) {
2512 ConstAbsIntExpr (hie2, &Expr2);
2514 /* Combine the two */
2515 Expr->IVal = (Expr->IVal && Expr2.IVal);
2521 static void hieOrPP (ExprDesc *Expr)
2522 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2523 * called recursively from the preprocessor.
2528 ConstAbsIntExpr (hieAndPP, Expr);
2529 while (CurTok.Tok == TOK_BOOL_OR) {
2535 ConstAbsIntExpr (hieAndPP, &Expr2);
2537 /* Combine the two */
2538 Expr->IVal = (Expr->IVal || Expr2.IVal);
2544 static void hieAnd (ExprDesc* Expr, unsigned TrueLab, int* BoolOp)
2545 /* Process "exp && exp" */
2551 if (CurTok.Tok == TOK_BOOL_AND) {
2553 /* Tell our caller that we're evaluating a boolean */
2556 /* Get a label that we will use for false expressions */
2557 lab = GetLocalLabel ();
2559 /* If the expr hasn't set condition codes, set the force-test flag */
2560 if (!ED_IsTested (Expr)) {
2561 ED_MarkForTest (Expr);
2564 /* Load the value */
2565 ExprLoad (CF_FORCECHAR, Expr);
2567 /* Generate the jump */
2568 g_falsejump (CF_NONE, lab);
2570 /* Parse more boolean and's */
2571 while (CurTok.Tok == TOK_BOOL_AND) {
2578 if (!ED_IsTested (&Expr2)) {
2579 ED_MarkForTest (&Expr2);
2581 ExprLoad (CF_FORCECHAR, &Expr2);
2583 /* Do short circuit evaluation */
2584 if (CurTok.Tok == TOK_BOOL_AND) {
2585 g_falsejump (CF_NONE, lab);
2587 /* Last expression - will evaluate to true */
2588 g_truejump (CF_NONE, TrueLab);
2592 /* Define the false jump label here */
2593 g_defcodelabel (lab);
2595 /* The result is an rvalue in primary */
2596 ED_MakeRValExpr (Expr);
2597 ED_TestDone (Expr); /* Condition codes are set */
2603 static void hieOr (ExprDesc *Expr)
2604 /* Process "exp || exp". */
2607 int BoolOp = 0; /* Did we have a boolean op? */
2608 int AndOp; /* Did we have a && operation? */
2609 unsigned TrueLab; /* Jump to this label if true */
2613 TrueLab = GetLocalLabel ();
2615 /* Call the next level parser */
2616 hieAnd (Expr, TrueLab, &BoolOp);
2618 /* Any boolean or's? */
2619 if (CurTok.Tok == TOK_BOOL_OR) {
2621 /* If the expr hasn't set condition codes, set the force-test flag */
2622 if (!ED_IsTested (Expr)) {
2623 ED_MarkForTest (Expr);
2626 /* Get first expr */
2627 ExprLoad (CF_FORCECHAR, Expr);
2629 /* For each expression jump to TrueLab if true. Beware: If we
2630 * had && operators, the jump is already in place!
2633 g_truejump (CF_NONE, TrueLab);
2636 /* Remember that we had a boolean op */
2639 /* while there's more expr */
2640 while (CurTok.Tok == TOK_BOOL_OR) {
2647 hieAnd (&Expr2, TrueLab, &AndOp);
2648 if (!ED_IsTested (&Expr2)) {
2649 ED_MarkForTest (&Expr2);
2651 ExprLoad (CF_FORCECHAR, &Expr2);
2653 /* If there is more to come, add shortcut boolean eval. */
2654 g_truejump (CF_NONE, TrueLab);
2658 /* The result is an rvalue in primary */
2659 ED_MakeRValExpr (Expr);
2660 ED_TestDone (Expr); /* Condition codes are set */
2663 /* If we really had boolean ops, generate the end sequence */
2665 DoneLab = GetLocalLabel ();
2666 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2667 g_falsejump (CF_NONE, DoneLab);
2668 g_defcodelabel (TrueLab);
2669 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2670 g_defcodelabel (DoneLab);
2676 static void hieQuest (ExprDesc* Expr)
2677 /* Parse the ternary operator */
2681 ExprDesc Expr2; /* Expression 2 */
2682 ExprDesc Expr3; /* Expression 3 */
2683 int Expr2IsNULL; /* Expression 2 is a NULL pointer */
2684 int Expr3IsNULL; /* Expression 3 is a NULL pointer */
2685 type* ResultType; /* Type of result */
2688 /* Call the lower level eval routine */
2689 if (Preprocessing) {
2695 /* Check if it's a ternary expression */
2696 if (CurTok.Tok == TOK_QUEST) {
2698 if (!ED_IsTested (Expr)) {
2699 /* Condition codes not set, request a test */
2700 ED_MarkForTest (Expr);
2702 ExprLoad (CF_NONE, Expr);
2703 labf = GetLocalLabel ();
2704 g_falsejump (CF_NONE, labf);
2706 /* Parse second expression. Remember for later if it is a NULL pointer
2707 * expression, then load it into the primary.
2709 ExprWithCheck (hie1, &Expr2);
2710 Expr2IsNULL = ED_IsNullPtr (&Expr2);
2711 if (!IsTypeVoid (Expr2.Type)) {
2712 /* Load it into the primary */
2713 ExprLoad (CF_NONE, &Expr2);
2714 ED_MakeRValExpr (&Expr2);
2716 labt = GetLocalLabel ();
2720 /* Jump here if the first expression was false */
2721 g_defcodelabel (labf);
2723 /* Parse second expression. Remember for later if it is a NULL pointer
2724 * expression, then load it into the primary.
2726 ExprWithCheck (hie1, &Expr3);
2727 Expr3IsNULL = ED_IsNullPtr (&Expr3);
2728 if (!IsTypeVoid (Expr3.Type)) {
2729 /* Load it into the primary */
2730 ExprLoad (CF_NONE, &Expr3);
2731 ED_MakeRValExpr (&Expr3);
2734 /* Check if any conversions are needed, if so, do them.
2735 * Conversion rules for ?: expression are:
2736 * - if both expressions are int expressions, default promotion
2737 * rules for ints apply.
2738 * - if both expressions are pointers of the same type, the
2739 * result of the expression is of this type.
2740 * - if one of the expressions is a pointer and the other is
2741 * a zero constant, the resulting type is that of the pointer
2743 * - if both expressions are void expressions, the result is of
2745 * - all other cases are flagged by an error.
2747 if (IsClassInt (Expr2.Type) && IsClassInt (Expr3.Type)) {
2749 /* Get common type */
2750 ResultType = promoteint (Expr2.Type, Expr3.Type);
2752 /* Convert the third expression to this type if needed */
2753 TypeConversion (&Expr3, ResultType);
2755 /* Setup a new label so that the expr3 code will jump around
2756 * the type cast code for expr2.
2758 labf = GetLocalLabel (); /* Get new label */
2759 g_jump (labf); /* Jump around code */
2761 /* The jump for expr2 goes here */
2762 g_defcodelabel (labt);
2764 /* Create the typecast code for expr2 */
2765 TypeConversion (&Expr2, ResultType);
2767 /* Jump here around the typecase code. */
2768 g_defcodelabel (labf);
2769 labt = 0; /* Mark other label as invalid */
2771 } else if (IsClassPtr (Expr2.Type) && IsClassPtr (Expr3.Type)) {
2772 /* Must point to same type */
2773 if (TypeCmp (Indirect (Expr2.Type), Indirect (Expr3.Type)) < TC_EQUAL) {
2774 Error ("Incompatible pointer types");
2776 /* Result has the common type */
2777 ResultType = Expr2.Type;
2778 } else if (IsClassPtr (Expr2.Type) && Expr3IsNULL) {
2779 /* Result type is pointer, no cast needed */
2780 ResultType = Expr2.Type;
2781 } else if (Expr2IsNULL && IsClassPtr (Expr3.Type)) {
2782 /* Result type is pointer, no cast needed */
2783 ResultType = Expr3.Type;
2784 } else if (IsTypeVoid (Expr2.Type) && IsTypeVoid (Expr3.Type)) {
2785 /* Result type is void */
2786 ResultType = Expr3.Type;
2788 Error ("Incompatible types");
2789 ResultType = Expr2.Type; /* Doesn't matter here */
2792 /* If we don't have the label defined until now, do it */
2794 g_defcodelabel (labt);
2797 /* Setup the target expression */
2798 ED_MakeRValExpr (Expr);
2799 Expr->Type = ResultType;
2805 static void opeq (const GenDesc* Gen, ExprDesc* Expr)
2806 /* Process "op=" operators. */
2813 /* op= can only be used with lvalues */
2814 if (!ED_IsLVal (Expr)) {
2815 Error ("Invalid lvalue in assignment");
2819 /* There must be an integer or pointer on the left side */
2820 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
2821 Error ("Invalid left operand type");
2822 /* Continue. Wrong code will be generated, but the compiler won't
2823 * break, so this is the best error recovery.
2827 /* Skip the operator token */
2830 /* Determine the type of the lhs */
2831 flags = TypeOf (Expr->Type);
2832 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) && IsTypePtr (Expr->Type);
2834 /* Get the lhs address on stack (if needed) */
2837 /* Fetch the lhs into the primary register if needed */
2838 ExprLoad (CF_NONE, Expr);
2840 /* Bring the lhs on stack */
2841 Mark = GetCodePos ();
2844 /* Evaluate the rhs */
2845 if (evalexpr (CF_NONE, hie1, &Expr2) == 0) {
2846 /* The resulting value is a constant. If the generator has the NOPUSH
2847 * flag set, don't push the lhs.
2849 if (Gen->Flags & GEN_NOPUSH) {
2854 /* lhs is a pointer, scale rhs */
2855 Expr2.IVal *= CheckedSizeOf (Expr->Type+1);
2858 /* If the lhs is character sized, the operation may be later done
2861 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
2862 flags |= CF_FORCECHAR;
2865 /* Special handling for add and sub - some sort of a hack, but short code */
2866 if (Gen->Func == g_add) {
2867 g_inc (flags | CF_CONST, Expr2.IVal);
2868 } else if (Gen->Func == g_sub) {
2869 g_dec (flags | CF_CONST, Expr2.IVal);
2871 Gen->Func (flags | CF_CONST, Expr2.IVal);
2874 /* rhs is not constant and already in the primary register */
2876 /* lhs is a pointer, scale rhs */
2877 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Expr->Type+1));
2880 /* If the lhs is character sized, the operation may be later done
2883 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
2884 flags |= CF_FORCECHAR;
2887 /* Adjust the types of the operands if needed */
2888 Gen->Func (g_typeadjust (flags, TypeOf (Expr2.Type)), 0);
2891 ED_MakeRValExpr (Expr);
2896 static void addsubeq (const GenDesc* Gen, ExprDesc *Expr)
2897 /* Process the += and -= operators */
2905 /* We're currently only able to handle some adressing modes */
2906 if (ED_GetLoc (Expr) == E_LOC_EXPR || ED_GetLoc (Expr) == E_LOC_PRIMARY) {
2907 /* Use generic routine */
2912 /* We must have an lvalue */
2913 if (ED_IsRVal (Expr)) {
2914 Error ("Invalid lvalue in assignment");
2918 /* There must be an integer or pointer on the left side */
2919 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
2920 Error ("Invalid left operand type");
2921 /* Continue. Wrong code will be generated, but the compiler won't
2922 * break, so this is the best error recovery.
2926 /* Skip the operator */
2929 /* Check if we have a pointer expression and must scale rhs */
2930 MustScale = IsTypePtr (Expr->Type);
2932 /* Initialize the code generator flags */
2936 /* Evaluate the rhs */
2938 if (ED_IsConstAbs (&Expr2)) {
2939 /* The resulting value is a constant. Scale it. */
2941 Expr2.IVal *= CheckedSizeOf (Indirect (Expr->Type));
2946 /* Not constant, load into the primary */
2947 ExprLoad (CF_NONE, &Expr2);
2949 /* lhs is a pointer, scale rhs */
2950 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Indirect (Expr->Type)));
2954 /* Setup the code generator flags */
2955 lflags |= TypeOf (Expr->Type) | CF_FORCECHAR;
2956 rflags |= TypeOf (Expr2.Type);
2958 /* Convert the type of the lhs to that of the rhs */
2959 g_typecast (lflags, rflags);
2961 /* Output apropriate code depending on the location */
2962 switch (ED_GetLoc (Expr)) {
2965 /* Absolute: numeric address or const */
2966 lflags |= CF_ABSOLUTE;
2967 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2968 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2970 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2975 /* Global variable */
2976 lflags |= CF_EXTERNAL;
2977 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2978 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2980 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2986 /* Static variable or literal in the literal pool */
2987 lflags |= CF_STATIC;
2988 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2989 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2991 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2995 case E_LOC_REGISTER:
2996 /* Register variable */
2997 lflags |= CF_REGVAR;
2998 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2999 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3001 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
3006 /* Value on the stack */
3007 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3008 g_addeqlocal (lflags, Expr->IVal, Expr2.IVal);
3010 g_subeqlocal (lflags, Expr->IVal, Expr2.IVal);
3015 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
3018 /* Expression is a rvalue in the primary now */
3019 ED_MakeRValExpr (Expr);
3024 void hie1 (ExprDesc* Expr)
3025 /* Parse first level of expression hierarchy. */
3028 switch (CurTok.Tok) {
3034 case TOK_PLUS_ASSIGN:
3035 addsubeq (&GenPASGN, Expr);
3038 case TOK_MINUS_ASSIGN:
3039 addsubeq (&GenSASGN, Expr);
3042 case TOK_MUL_ASSIGN:
3043 opeq (&GenMASGN, Expr);
3046 case TOK_DIV_ASSIGN:
3047 opeq (&GenDASGN, Expr);
3050 case TOK_MOD_ASSIGN:
3051 opeq (&GenMOASGN, Expr);
3054 case TOK_SHL_ASSIGN:
3055 opeq (&GenSLASGN, Expr);
3058 case TOK_SHR_ASSIGN:
3059 opeq (&GenSRASGN, Expr);
3062 case TOK_AND_ASSIGN:
3063 opeq (&GenAASGN, Expr);
3066 case TOK_XOR_ASSIGN:
3067 opeq (&GenXOASGN, Expr);
3071 opeq (&GenOASGN, Expr);
3081 void hie0 (ExprDesc *Expr)
3082 /* Parse comma operator. */
3085 while (CurTok.Tok == TOK_COMMA) {
3093 int evalexpr (unsigned Flags, void (*Func) (ExprDesc*), ExprDesc* Expr)
3094 /* Will evaluate an expression via the given function. If the result is a
3095 * constant, 0 is returned and the value is put in the Expr struct. If the
3096 * result is not constant, ExprLoad is called to bring the value into the
3097 * primary register and 1 is returned.
3101 ExprWithCheck (Func, Expr);
3103 /* Check for a constant expression */
3104 if (ED_IsConstAbs (Expr)) {
3105 /* Constant expression */
3108 /* Not constant, load into the primary */
3109 ExprLoad (Flags, Expr);
3116 void Expression0 (ExprDesc* Expr)
3117 /* Evaluate an expression via hie0 and put the result into the primary register */
3119 ExprWithCheck (hie0, Expr);
3120 ExprLoad (CF_NONE, Expr);
3125 void ConstExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3126 /* Will evaluate an expression via the given function. If the result is not
3127 * a constant of some sort, a diagnostic will be printed, and the value is
3128 * replaced by a constant one to make sure there are no internal errors that
3129 * result from this input error.
3132 ExprWithCheck (Func, Expr);
3133 if (!ED_IsConst (Expr)) {
3134 Error ("Constant expression expected");
3135 /* To avoid any compiler errors, make the expression a valid const */
3136 ED_MakeConstAbsInt (Expr, 1);
3142 void BoolExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3143 /* Will evaluate an expression via the given function. If the result is not
3144 * something that may be evaluated in a boolean context, a diagnostic will be
3145 * printed, and the value is replaced by a constant one to make sure there
3146 * are no internal errors that result from this input error.
3149 ExprWithCheck (Func, Expr);
3150 if (!ED_IsBool (Expr)) {
3151 Error ("Boolean expression expected");
3152 /* To avoid any compiler errors, make the expression a valid int */
3153 ED_MakeConstAbsInt (Expr, 1);
3159 void ConstAbsIntExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3160 /* Will evaluate an expression via the given function. If the result is not
3161 * a constant numeric integer value, a diagnostic will be printed, and the
3162 * value is replaced by a constant one to make sure there are no internal
3163 * errors that result from this input error.
3166 ExprWithCheck (Func, Expr);
3167 if (!ED_IsConstAbsInt (Expr)) {
3168 Error ("Constant integer expression expected");
3169 /* To avoid any compiler errors, make the expression a valid const */
3170 ED_MakeConstAbsInt (Expr, 1);