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->Val, 0);
202 /* Global variable */
203 g_defdata (CF_EXTERNAL, Expr->Name, Expr->Val);
208 /* Static variable or literal in the literal pool */
209 g_defdata (CF_STATIC, Expr->Name, Expr->Val);
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->Val);
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->Val, 0);
240 /* Global symbol, load address */
241 g_getimmed ((Flags | CF_EXTERNAL) & ~CF_CONST, Expr->Name, Expr->Val);
246 /* Static symbol or literal, load address */
247 g_getimmed ((Flags | CF_STATIC) & ~CF_CONST, Expr->Name, Expr->Val);
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->Val);
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 (Expr->Test & E_FORCETEST) {
389 Expr->Test &= ~E_FORCETEST;
392 switch (ED_GetLoc (Expr)) {
395 /* Absolute: numeric address or const */
396 g_getstatic (Flags | CF_ABSOLUTE, Expr->Val, 0);
400 /* Global variable */
401 g_getstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->Val);
406 /* Static variable or literal in the literal pool */
407 g_getstatic (Flags | CF_STATIC, Expr->Name, Expr->Val);
411 /* Register variable */
412 g_getstatic (Flags | CF_REGVAR, Expr->Name, Expr->Val);
416 /* Value on the stack */
417 g_getlocal (Flags, Expr->Val);
421 /* The primary register - just test if necessary */
422 if (Flags & CF_TEST) {
428 /* Reference to address in primary with offset in Expr */
429 g_getind (Flags, Expr->Val);
433 Internal ("Invalid location in ExprLoad: 0x%04X", ED_GetLoc (Expr));
438 if (ED_IsLocExpr (Expr)) {
439 if (Expr->Val != 0) {
440 /* We have an expression in the primary plus a constant
441 * offset. Adjust the value in the primary accordingly.
443 Flags |= TypeOf (Expr->Type);
444 g_inc (Flags | CF_CONST, Expr->Val);
447 /* Constant of some sort, load it into the primary */
448 LoadConstant (Flags, Expr);
451 /* Are we testing this value? */
452 if (Expr->Test & E_FORCETEST) {
453 /* Yes, force a test */
454 Flags |= TypeOf (Expr->Type);
456 Expr->Test &= ~E_FORCETEST;
463 static unsigned FunctionParamList (FuncDesc* Func)
464 /* Parse a function parameter list and pass the parameters to the called
465 * function. Depending on several criteria this may be done by just pushing
466 * each parameter separately, or creating the parameter frame once and then
467 * storing into this frame.
468 * The function returns the size of the parameters pushed.
473 /* Initialize variables */
474 SymEntry* Param = 0; /* Keep gcc silent */
475 unsigned ParamSize = 0; /* Size of parameters pushed */
476 unsigned ParamCount = 0; /* Number of parameters pushed */
477 unsigned FrameSize = 0; /* Size of parameter frame */
478 unsigned FrameParams = 0; /* Number of params in frame */
479 int FrameOffs = 0; /* Offset into parameter frame */
480 int Ellipsis = 0; /* Function is variadic */
482 /* As an optimization, we may allocate the complete parameter frame at
483 * once instead of pushing each parameter as it comes. We may do that,
486 * - optimizations that increase code size are enabled (allocating the
487 * stack frame at once gives usually larger code).
488 * - we have more than one parameter to push (don't count the last param
489 * for __fastcall__ functions).
491 * The FrameSize variable will contain a value > 0 if storing into a frame
492 * (instead of pushing) is enabled.
495 if (CodeSizeFactor >= 200) {
497 /* Calculate the number and size of the parameters */
498 FrameParams = Func->ParamCount;
499 FrameSize = Func->ParamSize;
500 if (FrameParams > 0 && (Func->Flags & FD_FASTCALL) != 0) {
501 /* Last parameter is not pushed */
502 FrameSize -= CheckedSizeOf (Func->LastParam->Type);
506 /* Do we have more than one parameter in the frame? */
507 if (FrameParams > 1) {
508 /* Okeydokey, setup the frame */
509 FrameOffs = StackPtr;
511 StackPtr -= FrameSize;
513 /* Don't use a preallocated frame */
518 /* Parse the actual parameter list */
519 while (CurTok.Tok != TOK_RPAREN) {
523 /* Count arguments */
526 /* Fetch the pointer to the next argument, check for too many args */
527 if (ParamCount <= Func->ParamCount) {
528 /* Beware: If there are parameters with identical names, they
529 * cannot go into the same symbol table, which means that in this
530 * case of errorneous input, the number of nodes in the symbol
531 * table and ParamCount are NOT equal. We have to handle this case
532 * below to avoid segmentation violations. Since we know that this
533 * problem can only occur if there is more than one parameter,
534 * we will just use the last one.
536 if (ParamCount == 1) {
538 Param = Func->SymTab->SymHead;
539 } else if (Param->NextSym != 0) {
541 Param = Param->NextSym;
542 CHECK ((Param->Flags & SC_PARAM) != 0);
544 } else if (!Ellipsis) {
545 /* Too many arguments. Do we have an open param list? */
546 if ((Func->Flags & FD_VARIADIC) == 0) {
547 /* End of param list reached, no ellipsis */
548 Error ("Too many arguments in function call");
550 /* Assume an ellipsis even in case of errors to avoid an error
551 * message for each other argument.
556 /* Evaluate the parameter expression */
559 /* If we don't have an argument spec, accept anything, otherwise
560 * convert the actual argument to the type needed.
564 /* Convert the argument to the parameter type if needed */
565 TypeConversion (&Expr, Param->Type);
567 /* If we have a prototype, chars may be pushed as chars */
568 Flags |= CF_FORCECHAR;
571 /* Load the value into the primary if it is not already there */
572 ExprLoad (Flags, &Expr);
574 /* Use the type of the argument for the push */
575 Flags |= TypeOf (Expr.Type);
577 /* If this is a fastcall function, don't push the last argument */
578 if (ParamCount != Func->ParamCount || (Func->Flags & FD_FASTCALL) == 0) {
579 unsigned ArgSize = sizeofarg (Flags);
581 /* We have the space already allocated, store in the frame.
582 * Because of invalid type conversions (that have produced an
583 * error before), we can end up here with a non aligned stack
584 * frame. Since no output will be generated anyway, handle
585 * these cases gracefully instead of doing a CHECK.
587 if (FrameSize >= ArgSize) {
588 FrameSize -= ArgSize;
592 FrameOffs -= ArgSize;
594 g_putlocal (Flags | CF_NOKEEP, FrameOffs, Expr.Val);
596 /* Push the argument */
597 g_push (Flags, Expr.Val);
600 /* Calculate total parameter size */
601 ParamSize += ArgSize;
604 /* Check for end of argument list */
605 if (CurTok.Tok != TOK_COMMA) {
611 /* Check if we had enough parameters */
612 if (ParamCount < Func->ParamCount) {
613 Error ("Too few arguments in function call");
616 /* The function returns the size of all parameters pushed onto the stack.
617 * However, if there are parameters missing (which is an error and was
618 * flagged by the compiler) AND a stack frame was preallocated above,
619 * we would loose track of the stackpointer and generate an internal error
620 * later. So we correct the value by the parameters that should have been
621 * pushed to avoid an internal compiler error. Since an error was
622 * generated before, no code will be output anyway.
624 return ParamSize + FrameSize;
629 static void FunctionCall (ExprDesc* Expr)
630 /* Perform a function call. */
632 FuncDesc* Func; /* Function descriptor */
633 int IsFuncPtr; /* Flag */
634 int StdFunc; /* Standard function index */
635 unsigned ParamSize; /* Number of parameter bytes */
636 CodeMark Mark = 0; /* Initialize to keep gcc silent */
637 int PtrOffs = 0; /* Offset of function pointer on stack */
638 int IsFastCall = 0; /* True if it's a fast call function */
639 int PtrOnStack = 0; /* True if a pointer copy is on stack */
641 /* Skip the left paren */
644 /* Get a pointer to the function descriptor from the type string */
645 Func = GetFuncDesc (Expr->Type);
647 /* Handle function pointers transparently */
648 IsFuncPtr = IsTypeFuncPtr (Expr->Type);
651 /* Check wether it's a fastcall function that has parameters */
652 IsFastCall = IsFastCallFunc (Expr->Type + 1) && (Func->ParamCount > 0);
654 /* Things may be difficult, depending on where the function pointer
655 * resides. If the function pointer is an expression of some sort
656 * (not a local or global variable), we have to evaluate this
657 * expression now and save the result for later. Since calls to
658 * function pointers may be nested, we must save it onto the stack.
659 * For fastcall functions we do also need to place a copy of the
660 * pointer on stack, since we cannot use a/x.
662 PtrOnStack = IsFastCall || !ED_IsConst (Expr);
665 /* Not a global or local variable, or a fastcall function. Load
666 * the pointer into the primary and mark it as an expression.
668 ExprLoad (CF_NONE, Expr);
669 ED_MakeRValExpr (Expr);
671 /* Remember the code position */
672 Mark = GetCodePos ();
674 /* Push the pointer onto the stack and remember the offset */
679 /* Check for known standard functions and inline them */
680 } else if ((StdFunc = FindStdFunc ((const char*) Expr->Name)) >= 0) {
682 /* Inline this function */
683 HandleStdFunc (StdFunc, Func, Expr);
688 /* Parse the parameter list */
689 ParamSize = FunctionParamList (Func);
691 /* We need the closing paren here */
694 /* Special handling for function pointers */
697 /* If the function is not a fastcall function, load the pointer to
698 * the function into the primary.
702 /* Not a fastcall function - we may use the primary */
704 /* If we have no parameters, the pointer is still in the
705 * primary. Remove the code to push it and correct the
708 if (ParamSize == 0) {
713 /* Load from the saved copy */
714 g_getlocal (CF_PTR, PtrOffs);
717 /* Load from original location */
718 ExprLoad (CF_NONE, Expr);
721 /* Call the function */
722 g_callind (TypeOf (Expr->Type+1), ParamSize, PtrOffs);
726 /* Fastcall function. We cannot use the primary for the function
727 * pointer and must therefore use an offset to the stack location.
728 * Since fastcall functions may never be variadic, we can use the
729 * index register for this purpose.
731 g_callind (CF_LOCAL, ParamSize, PtrOffs);
734 /* If we have a pointer on stack, remove it */
736 g_space (- (int) sizeofarg (CF_PTR));
745 /* Normal function */
746 g_call (TypeOf (Expr->Type), (const char*) Expr->Name, ParamSize);
750 /* The function result is an rvalue in the primary register */
751 ED_MakeRValExpr (Expr);
752 Expr->Type = GetFuncReturn (Expr->Type);
757 static void Primary (ExprDesc* E)
758 /* This is the lowest level of the expression parser. */
762 /* Initialize fields in the expression stucture */
765 /* Character and integer constants. */
766 if (CurTok.Tok == TOK_ICONST || CurTok.Tok == TOK_CCONST) {
767 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
768 E->Type = CurTok.Type;
769 E->Val = CurTok.IVal;
774 /* Process parenthesized subexpression by calling the whole parser
777 if (CurTok.Tok == TOK_LPAREN) {
784 /* If we run into an identifier in preprocessing mode, we assume that this
785 * is an undefined macro and replace it by a constant value of zero.
787 if (Preprocessing && CurTok.Tok == TOK_IDENT) {
788 ED_MakeConstAbsInt (E, 0);
792 /* All others may only be used if the expression evaluation is not called
793 * recursively by the preprocessor.
796 /* Illegal expression in PP mode */
797 Error ("Preprocessor expression expected");
798 ED_MakeConstAbsInt (E, 1);
802 switch (CurTok.Tok) {
805 /* Identifier. Get a pointer to the symbol table entry */
806 Sym = E->Sym = FindSym (CurTok.Ident);
808 /* Is the symbol known? */
811 /* We found the symbol - skip the name token */
814 /* Check for illegal symbol types */
815 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
816 if (Sym->Flags & SC_TYPE) {
817 /* Cannot use type symbols */
818 Error ("Variable identifier expected");
819 /* Assume an int type to make E valid */
820 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
825 /* Mark the symbol as referenced */
826 Sym->Flags |= SC_REF;
828 /* The expression type is the symbol type */
831 /* Check for legal symbol types */
832 if ((Sym->Flags & SC_CONST) == SC_CONST) {
833 /* Enum or some other numeric constant */
834 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
835 E->Val = Sym->V.ConstVal;
836 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
838 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
839 E->Name = (unsigned long) Sym->Name;
840 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
841 /* Local variable. If this is a parameter for a variadic
842 * function, we have to add some address calculations, and the
843 * address is not const.
845 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
846 /* Variadic parameter */
847 g_leavariadic (Sym->V.Offs - F_GetParamSize (CurrentFunc));
848 E->Flags = E_LOC_EXPR | E_RTYPE_LVAL;
850 /* Normal parameter */
851 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
852 E->Val = Sym->V.Offs;
854 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
855 /* Register variable, zero page based */
856 E->Flags = E_LOC_REGISTER | E_RTYPE_LVAL;
857 E->Name = Sym->V.R.RegOffs;
858 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
859 /* Static variable */
860 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
861 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
862 E->Name = (unsigned long) Sym->Name;
864 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
865 E->Name = Sym->V.Label;
868 /* Local static variable */
869 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
870 E->Name = Sym->V.Offs;
873 /* We've made all variables lvalues above. However, this is
874 * not always correct: An array is actually the address of its
875 * first element, which is a rvalue, and a function is a
876 * rvalue, too, because we cannot store anything in a function.
877 * So fix the flags depending on the type.
879 if (IsTypeArray (E->Type) || IsTypeFunc (E->Type)) {
885 /* We did not find the symbol. Remember the name, then skip it */
887 strcpy (Ident, CurTok.Ident);
890 /* IDENT is either an auto-declared function or an undefined variable. */
891 if (CurTok.Tok == TOK_LPAREN) {
892 /* Declare a function returning int. For that purpose, prepare a
893 * function signature for a function having an empty param list
896 Warning ("Function call without a prototype");
897 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
899 E->Flags = E_LOC_GLOBAL | E_RTYPE_RVAL;
900 E->Name = (unsigned long) Sym->Name;
902 /* Undeclared Variable */
903 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
904 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
906 Error ("Undefined symbol: `%s'", Ident);
914 E->Type = GetCharArrayType (GetLiteralPoolOffs () - CurTok.IVal);
915 E->Flags = E_LOC_LITERAL | E_RTYPE_RVAL;
916 E->Val = CurTok.IVal;
917 E->Name = LiteralPoolLabel;
924 E->Flags = E_LOC_EXPR | E_RTYPE_RVAL;
929 /* Register pseudo variable */
930 E->Type = type_uchar;
931 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
936 /* Register pseudo variable */
938 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
943 /* Register pseudo variable */
944 E->Type = type_ulong;
945 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
950 /* Illegal primary. */
951 Error ("Expression expected");
952 ED_MakeConstAbsInt (E, 1);
959 static void ArrayRef (ExprDesc* Expr)
960 /* Handle an array reference */
970 /* Skip the bracket */
973 /* Get the type of left side */
976 /* We can apply a special treatment for arrays that have a const base
977 * address. This is true for most arrays and will produce a lot better
978 * code. Check if this is a const base address.
980 ConstBaseAddr = (ED_IsLocConst (Expr) || ED_IsLocStack (Expr));
982 /* If we have a constant base, we delay the address fetch */
983 Mark1 = GetCodePos ();
984 Mark2 = 0; /* Silence gcc */
985 if (!ConstBaseAddr) {
986 /* Get a pointer to the array into the primary */
987 ExprLoad (CF_NONE, Expr);
989 /* Get the array pointer on stack. Do not push more than 16
990 * bit, even if this value is greater, since we cannot handle
991 * other than 16bit stuff when doing indexing.
993 Mark2 = GetCodePos ();
997 /* TOS now contains ptr to array elements. Get the subscript. */
998 ExprWithCheck (hie0, &SubScript);
1000 /* Check the types of array and subscript. We can either have a
1001 * pointer/array to the left, in which case the subscript must be of an
1002 * integer type, or we have an integer to the left, in which case the
1003 * subscript must be a pointer/array.
1004 * Since we do the necessary checking here, we can rely later on the
1007 if (IsClassPtr (Expr->Type)) {
1008 if (!IsClassInt (SubScript.Type)) {
1009 Error ("Array subscript is not an integer");
1010 /* To avoid any compiler errors, make the expression a valid int */
1011 ED_MakeConstAbsInt (&SubScript, 0);
1013 ElementType = Indirect (Expr->Type);
1014 } else if (IsClassInt (Expr->Type)) {
1015 if (!IsClassPtr (SubScript.Type)) {
1016 Error ("Subscripted value is neither array nor pointer");
1017 /* To avoid compiler errors, make the subscript a char[] at
1020 ED_MakeConstAbs (&SubScript, 0, GetCharArrayType (1));
1022 ElementType = Indirect (SubScript.Type);
1024 Error ("Cannot subscript");
1025 /* To avoid compiler errors, fake both the array and the subscript, so
1026 * we can just proceed.
1028 ED_MakeConstAbs (Expr, 0, GetCharArrayType (1));
1029 ED_MakeConstAbsInt (&SubScript, 0);
1030 ElementType = Indirect (Expr->Type);
1033 /* Check if the subscript is constant absolute value */
1034 if (ED_IsConstAbs (&SubScript)) {
1036 /* The array subscript is a numeric constant. If we had pushed the
1037 * array base address onto the stack before, we can remove this value,
1038 * since we can generate expression+offset.
1040 if (!ConstBaseAddr) {
1044 /* Get an array pointer into the primary */
1045 ExprLoad (CF_NONE, Expr);
1048 if (IsClassPtr (Expr->Type)) {
1050 /* Lhs is pointer/array. Scale the subscript value according to
1053 SubScript.Val *= CheckedSizeOf (ElementType);
1055 /* Remove the address load code */
1058 /* In case of an array, we can adjust the offset of the expression
1059 * already in Expr. If the base address was a constant, we can even
1060 * remove the code that loaded the address into the primary.
1062 if (IsTypeArray (Expr->Type)) {
1064 /* Adjust the offset */
1065 Expr->Val += SubScript.Val;
1069 /* It's a pointer, so we do have to load it into the primary
1070 * first (if it's not already there).
1072 if (ConstBaseAddr) {
1073 ExprLoad (CF_NONE, Expr);
1074 ED_MakeRValExpr (Expr);
1077 /* Use the offset */
1078 Expr->Val = SubScript.Val;
1083 /* Scale the rhs value according to the element type */
1084 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
1086 /* Add the subscript. Since arrays are indexed by integers,
1087 * we will ignore the true type of the subscript here and
1088 * use always an int. #### Use offset but beware of ExprLoad!
1090 g_inc (CF_INT | CF_CONST, SubScript.Val);
1096 /* Array subscript is not constant. Load it into the primary */
1097 Mark2 = GetCodePos ();
1098 ExprLoad (CF_NONE, &SubScript);
1101 if (IsClassPtr (Expr->Type)) {
1103 /* Indexing is based on unsigneds, so we will just use the integer
1104 * portion of the index (which is in (e)ax, so there's no further
1107 g_scale (CF_INT, CheckedSizeOf (ElementType));
1111 /* Get the int value on top. If we come here, we're sure, both
1112 * values are 16 bit (the first one was truncated if necessary
1113 * and the second one is a pointer). Note: If ConstBaseAddr is
1114 * true, we don't have a value on stack, so to "swap" both, just
1115 * push the subscript.
1117 if (ConstBaseAddr) {
1119 ExprLoad (CF_NONE, Expr);
1126 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
1130 /* The offset is now in the primary register. It we didn't have a
1131 * constant base address for the lhs, the lhs address is already
1132 * on stack, and we must add the offset. If the base address was
1133 * constant, we call special functions to add the address to the
1136 if (!ConstBaseAddr) {
1138 /* The array base address is on stack and the subscript is in the
1139 * primary. Add both.
1145 /* The subscript is in the primary, and the array base address is
1146 * in Expr. If the subscript has itself a constant address, it is
1147 * often a better idea to reverse again the order of the
1148 * evaluation. This will generate better code if the subscript is
1149 * a byte sized variable. But beware: This is only possible if the
1150 * subscript was not scaled, that is, if this was a byte array
1153 if ((ED_IsLocConst (&SubScript) || ED_IsLocStack (&SubScript)) &&
1154 CheckedSizeOf (ElementType) == SIZEOF_CHAR) {
1158 /* Reverse the order of evaluation */
1159 if (CheckedSizeOf (SubScript.Type) == SIZEOF_CHAR) {
1166 /* Get a pointer to the array into the primary. */
1167 ExprLoad (CF_NONE, Expr);
1169 /* Add the variable */
1170 if (ED_IsLocStack (&SubScript)) {
1171 g_addlocal (Flags, SubScript.Val);
1173 Flags |= GlobalModeFlags (SubScript.Flags);
1174 g_addstatic (Flags, SubScript.Name, SubScript.Val);
1177 if (ED_IsLocAbs (Expr)) {
1178 /* Constant numeric address. Just add it */
1179 g_inc (CF_INT, Expr->Val);
1180 } else if (ED_IsLocStack (Expr)) {
1181 /* Base address is a local variable address */
1182 if (IsTypeArray (Expr->Type)) {
1183 g_addaddr_local (CF_INT, Expr->Val);
1185 g_addlocal (CF_PTR, Expr->Val);
1188 /* Base address is a static variable address */
1189 unsigned Flags = CF_INT | GlobalModeFlags (Expr->Flags);
1190 if (IsTypeArray (Expr->Type)) {
1191 g_addaddr_static (Flags, Expr->Name, Expr->Val);
1193 g_addstatic (Flags, Expr->Name, Expr->Val);
1201 /* The result is an expression in the primary */
1202 ED_MakeRValExpr (Expr);
1206 /* Result is of element type */
1207 Expr->Type = ElementType;
1209 /* An array element is actually a variable. So the rules for variables
1210 * with respect to the reference type apply: If it's an array, it is
1211 * a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1212 * but an array cannot contain functions).
1214 if (IsTypeArray (Expr->Type)) {
1220 /* Consume the closing bracket */
1226 static void StructRef (ExprDesc* Expr)
1227 /* Process struct field after . or ->. */
1232 /* Skip the token and check for an identifier */
1234 if (CurTok.Tok != TOK_IDENT) {
1235 Error ("Identifier expected");
1236 Expr->Type = type_int;
1240 /* Get the symbol table entry and check for a struct field */
1241 strcpy (Ident, CurTok.Ident);
1243 Field = FindStructField (Expr->Type, Ident);
1245 Error ("Struct/union has no field named `%s'", Ident);
1246 Expr->Type = type_int;
1250 /* If we have a struct pointer that is an lvalue and not already in the
1251 * primary, load it now.
1253 if (ED_IsLVal (Expr) && IsTypePtr (Expr->Type)) {
1255 /* Load into the primary */
1256 ExprLoad (CF_NONE, Expr);
1258 /* Make it an lvalue expression */
1259 ED_MakeLValExpr (Expr);
1262 /* Set the struct field offset */
1263 Expr->Val += Field->V.Offs;
1265 /* The type is now the type of the field */
1266 Expr->Type = Field->Type;
1268 /* An struct member is actually a variable. So the rules for variables
1269 * with respect to the reference type apply: If it's an array, it is
1270 * a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1271 * but a struct field cannot be a function).
1273 if (IsTypeArray (Expr->Type)) {
1282 static void hie11 (ExprDesc *Expr)
1283 /* Handle compound types (structs and arrays) */
1285 /* Evaluate the lhs */
1288 /* Check for a rhs */
1289 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN ||
1290 CurTok.Tok == TOK_DOT || CurTok.Tok == TOK_PTR_REF) {
1292 switch (CurTok.Tok) {
1295 /* Array reference */
1300 /* Function call. */
1301 if (!IsTypeFunc (Expr->Type) && !IsTypeFuncPtr (Expr->Type)) {
1302 /* Not a function */
1303 Error ("Illegal function call");
1304 /* Force the type to be a implicitly defined function, one
1305 * returning an int and taking any number of arguments.
1306 * Since we don't have a name, place it at absolute address
1309 ED_MakeConstAbs (Expr, 0, GetImplicitFuncType ());
1311 /* Call the function */
1312 FunctionCall (Expr);
1316 if (!IsClassStruct (Expr->Type)) {
1317 Error ("Struct expected");
1323 /* If we have an array, convert it to pointer to first element */
1324 if (IsTypeArray (Expr->Type)) {
1325 Expr->Type = ArrayToPtr (Expr->Type);
1327 if (!IsClassPtr (Expr->Type) || !IsClassStruct (Indirect (Expr->Type))) {
1328 Error ("Struct pointer expected");
1334 Internal ("Invalid token in hie11: %d", CurTok.Tok);
1342 void Store (ExprDesc* Expr, const type* StoreType)
1343 /* Store the primary register into the location denoted by Expr. If StoreType
1344 * is given, use this type when storing instead of Expr->Type. If StoreType
1345 * is NULL, use Expr->Type instead.
1350 /* If StoreType was not given, use Expr->Type instead */
1351 if (StoreType == 0) {
1352 StoreType = Expr->Type;
1355 /* Prepare the code generator flags */
1356 Flags = TypeOf (StoreType);
1358 /* Testing the value */
1362 /* Do the store depending on the location */
1363 switch (ED_GetLoc (Expr)) {
1366 /* Absolute: numeric address or const */
1367 g_putstatic (Flags | CF_ABSOLUTE, Expr->Val, 0);
1371 /* Global variable */
1372 g_putstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->Val);
1377 /* Static variable or literal in the literal pool */
1378 g_putstatic (Flags | CF_STATIC, Expr->Name, Expr->Val);
1381 case E_LOC_REGISTER:
1382 /* Register variable */
1383 g_putstatic (Flags | CF_REGVAR, Expr->Name, Expr->Val);
1387 /* Value on the stack */
1388 g_putlocal (Flags, Expr->Val, 0);
1392 /* The primary register (value is already there) */
1393 /* ### Do we need a test here if the flag is set? */
1397 /* An expression in the primary register */
1398 g_putind (Flags, Expr->Val);
1402 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
1405 /* Assume that each one of the stores will invalidate CC */
1406 Expr->Test &= ~E_CC;
1411 static void PreInc (ExprDesc* Expr)
1412 /* Handle the preincrement operators */
1417 /* Skip the operator token */
1420 /* Evaluate the expression and check that it is an lvalue */
1422 if (!ED_IsLVal (Expr)) {
1423 Error ("Invalid lvalue");
1427 /* Get the data type */
1428 Flags = TypeOf (Expr->Type) | CF_FORCECHAR | CF_CONST;
1430 /* Get the increment value in bytes */
1431 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1433 /* Check the location of the data */
1434 switch (ED_GetLoc (Expr)) {
1437 /* Absolute: numeric address or const */
1438 g_addeqstatic (Flags | CF_ABSOLUTE, Expr->Val, 0, Val);
1442 /* Global variable */
1443 g_addeqstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->Val, Val);
1448 /* Static variable or literal in the literal pool */
1449 g_addeqstatic (Flags | CF_STATIC, Expr->Name, Expr->Val, Val);
1452 case E_LOC_REGISTER:
1453 /* Register variable */
1454 g_addeqstatic (Flags | CF_REGVAR, Expr->Name, Expr->Val, Val);
1458 /* Value on the stack */
1459 g_addeqlocal (Flags, Expr->Val, Val);
1463 /* The primary register */
1468 /* An expression in the primary register */
1469 g_addeqind (Flags, Expr->Val, Val);
1473 Internal ("Invalid location in PreInc(): 0x%04X", ED_GetLoc (Expr));
1476 /* Result is an expression, no reference */
1477 ED_MakeRValExpr (Expr);
1482 static void PreDec (ExprDesc* Expr)
1483 /* Handle the predecrement operators */
1488 /* Skip the operator token */
1491 /* Evaluate the expression and check that it is an lvalue */
1493 if (!ED_IsLVal (Expr)) {
1494 Error ("Invalid lvalue");
1498 /* Get the data type */
1499 Flags = TypeOf (Expr->Type) | CF_FORCECHAR | CF_CONST;
1501 /* Get the increment value in bytes */
1502 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1504 /* Check the location of the data */
1505 switch (ED_GetLoc (Expr)) {
1508 /* Absolute: numeric address or const */
1509 g_subeqstatic (Flags | CF_ABSOLUTE, Expr->Val, 0, Val);
1513 /* Global variable */
1514 g_subeqstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->Val, Val);
1519 /* Static variable or literal in the literal pool */
1520 g_subeqstatic (Flags | CF_STATIC, Expr->Name, Expr->Val, Val);
1523 case E_LOC_REGISTER:
1524 /* Register variable */
1525 g_subeqstatic (Flags | CF_REGVAR, Expr->Name, Expr->Val, Val);
1529 /* Value on the stack */
1530 g_subeqlocal (Flags, Expr->Val, Val);
1534 /* The primary register */
1539 /* An expression in the primary register */
1540 g_subeqind (Flags, Expr->Val, Val);
1544 Internal ("Invalid location in PreDec(): 0x%04X", ED_GetLoc (Expr));
1547 /* Result is an expression, no reference */
1548 ED_MakeRValExpr (Expr);
1553 static void PostIncDec (ExprDesc* Expr, void (*inc) (unsigned, unsigned long))
1554 /* Handle i-- and i++ */
1560 /* The expression to increment must be an lvalue */
1561 if (!ED_IsLVal (Expr)) {
1562 Error ("Invalid lvalue");
1566 /* Get the data type */
1567 Flags = TypeOf (Expr->Type);
1569 /* Push the address if needed */
1572 /* Fetch the value and save it (since it's the result of the expression) */
1573 ExprLoad (CF_NONE, Expr);
1574 g_save (Flags | CF_FORCECHAR);
1576 /* If we have a pointer expression, increment by the size of the type */
1577 if (IsTypePtr (Expr->Type)) {
1578 inc (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1580 inc (Flags | CF_CONST | CF_FORCECHAR, 1);
1583 /* Store the result back */
1586 /* Restore the original value in the primary register */
1587 g_restore (Flags | CF_FORCECHAR);
1589 /* The result is always an expression, no reference */
1590 ED_MakeRValExpr (Expr);
1595 static void UnaryOp (ExprDesc* Expr)
1596 /* Handle unary -/+ and ~ */
1600 /* Remember the operator token and skip it */
1601 token_t Tok = CurTok.Tok;
1604 /* Get the expression */
1607 /* We can only handle integer types */
1608 if (!IsClassInt (Expr->Type)) {
1609 Error ("Argument must have integer type");
1610 ED_MakeConstAbsInt (Expr, 1);
1613 /* Check for a constant expression */
1614 if (ED_IsConstAbs (Expr)) {
1615 /* Value is constant */
1617 case TOK_MINUS: Expr->Val = -Expr->Val; break;
1618 case TOK_PLUS: break;
1619 case TOK_COMP: Expr->Val = ~Expr->Val; break;
1620 default: Internal ("Unexpected token: %d", Tok);
1623 /* Value is not constant */
1624 ExprLoad (CF_NONE, Expr);
1626 /* Get the type of the expression */
1627 Flags = TypeOf (Expr->Type);
1629 /* Handle the operation */
1631 case TOK_MINUS: g_neg (Flags); break;
1632 case TOK_PLUS: break;
1633 case TOK_COMP: g_com (Flags); break;
1634 default: Internal ("Unexpected token: %d", Tok);
1637 /* The result is a rvalue in the primary */
1638 ED_MakeRValExpr (Expr);
1644 void hie10 (ExprDesc* Expr)
1645 /* Handle ++, --, !, unary - etc. */
1649 switch (CurTok.Tok) {
1667 if (evalexpr (CF_NONE, hie10, Expr) == 0) {
1668 /* Constant expression */
1669 Expr->Val = !Expr->Val;
1671 g_bneg (TypeOf (Expr->Type));
1672 ED_MakeRValExpr (Expr);
1673 Expr->Test |= E_CC; /* bneg will set cc */
1679 ExprWithCheck (hie10, Expr);
1680 if (ED_IsLVal (Expr) || !(ED_IsLocConst (Expr) || ED_IsLocStack (Expr))) {
1681 /* Not a const, load it into the primary and make it a
1684 ExprLoad (CF_NONE, Expr);
1685 ED_MakeRValExpr (Expr);
1687 /* If the expression is already a pointer to function, the
1688 * additional dereferencing operator must be ignored.
1690 if (IsTypeFuncPtr (Expr->Type)) {
1691 /* Expression not storable */
1694 if (IsClassPtr (Expr->Type)) {
1695 Expr->Type = Indirect (Expr->Type);
1697 Error ("Illegal indirection");
1705 ExprWithCheck (hie10, Expr);
1706 /* The & operator may be applied to any lvalue, and it may be
1707 * applied to functions, even if they're no lvalues.
1709 if (ED_IsRVal (Expr) && !IsTypeFunc (Expr->Type)) {
1710 /* Allow the & operator with an array */
1711 if (!IsTypeArray (Expr->Type)) {
1712 Error ("Illegal address");
1715 Expr->Type = PointerTo (Expr->Type);
1722 if (TypeSpecAhead ()) {
1723 type Type[MAXTYPELEN];
1725 Size = CheckedSizeOf (ParseType (Type));
1728 /* Remember the output queue pointer */
1729 CodeMark Mark = GetCodePos ();
1731 Size = CheckedSizeOf (Expr->Type);
1732 /* Remove any generated code */
1735 ED_MakeConstAbs (Expr, Size, type_size_t);
1736 Expr->Test &= ~E_CC;
1740 if (TypeSpecAhead ()) {
1750 /* Handle post increment */
1751 if (CurTok.Tok == TOK_INC) {
1752 PostIncDec (Expr, g_inc);
1753 } else if (CurTok.Tok == TOK_DEC) {
1754 PostIncDec (Expr, g_dec);
1764 static void hie_internal (const GenDesc* Ops, /* List of generators */
1766 void (*hienext) (ExprDesc*),
1768 /* Helper function */
1774 token_t Tok; /* The operator token */
1775 unsigned ltype, type;
1776 int rconst; /* Operand is a constant */
1782 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1784 /* Tell the caller that we handled it's ops */
1787 /* All operators that call this function expect an int on the lhs */
1788 if (!IsClassInt (Expr->Type)) {
1789 Error ("Integer expression expected");
1792 /* Remember the operator token, then skip it */
1796 /* Get the lhs on stack */
1797 Mark1 = GetCodePos ();
1798 ltype = TypeOf (Expr->Type);
1799 if (ED_IsConstAbs (Expr)) {
1800 /* Constant value */
1801 Mark2 = GetCodePos ();
1802 g_push (ltype | CF_CONST, Expr->Val);
1804 /* Value not constant */
1805 ExprLoad (CF_NONE, Expr);
1806 Mark2 = GetCodePos ();
1810 /* Get the right hand side */
1811 rconst = (evalexpr (CF_NONE, hienext, &Expr2) == 0);
1813 /* Check the type of the rhs */
1814 if (!IsClassInt (Expr2.Type)) {
1815 Error ("Integer expression expected");
1818 /* Check for const operands */
1819 if (ED_IsConstAbs (Expr) && rconst) {
1821 /* Both operands are constant, remove the generated code */
1825 /* Evaluate the result */
1826 Expr->Val = kcalc (Tok, Expr->Val, Expr2.Val);
1828 /* Get the type of the result */
1829 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1833 /* If the right hand side is constant, and the generator function
1834 * expects the lhs in the primary, remove the push of the primary
1837 unsigned rtype = TypeOf (Expr2.Type);
1840 /* Second value is constant - check for div */
1843 if (Tok == TOK_DIV && Expr2.Val == 0) {
1844 Error ("Division by zero");
1845 } else if (Tok == TOK_MOD && Expr2.Val == 0) {
1846 Error ("Modulo operation with zero");
1848 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1851 ltype |= CF_REG; /* Value is in register */
1855 /* Determine the type of the operation result. */
1856 type |= g_typeadjust (ltype, rtype);
1857 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1860 Gen->Func (type, Expr2.Val);
1862 /* We have a rvalue in the primary now */
1863 ED_MakeRValExpr (Expr);
1870 static void hie_compare (const GenDesc* Ops, /* List of generators */
1872 void (*hienext) (ExprDesc*))
1873 /* Helper function for the compare operators */
1879 token_t tok; /* The operator token */
1881 int rconst; /* Operand is a constant */
1886 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1888 /* Remember the operator token, then skip it */
1892 /* Get the lhs on stack */
1893 Mark1 = GetCodePos ();
1894 ltype = TypeOf (Expr->Type);
1895 if (ED_IsConstAbs (Expr)) {
1896 /* Constant value */
1897 Mark2 = GetCodePos ();
1898 g_push (ltype | CF_CONST, Expr->Val);
1900 /* Value not constant */
1901 ExprLoad (CF_NONE, Expr);
1902 Mark2 = GetCodePos ();
1906 /* Get the right hand side */
1907 rconst = (evalexpr (CF_NONE, hienext, &Expr2) == 0);
1909 /* Make sure, the types are compatible */
1910 if (IsClassInt (Expr->Type)) {
1911 if (!IsClassInt (Expr2.Type) && !(IsClassPtr(Expr2.Type) && ED_IsNullPtr(Expr))) {
1912 Error ("Incompatible types");
1914 } else if (IsClassPtr (Expr->Type)) {
1915 if (IsClassPtr (Expr2.Type)) {
1916 /* Both pointers are allowed in comparison if they point to
1917 * the same type, or if one of them is a void pointer.
1919 type* left = Indirect (Expr->Type);
1920 type* right = Indirect (Expr2.Type);
1921 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1922 /* Incomatible pointers */
1923 Error ("Incompatible types");
1925 } else if (!ED_IsNullPtr (&Expr2)) {
1926 Error ("Incompatible types");
1930 /* Check for const operands */
1931 if (ED_IsConstAbs (Expr) && rconst) {
1933 /* Both operands are constant, remove the generated code */
1937 /* Evaluate the result */
1938 Expr->Val = kcalc (tok, Expr->Val, Expr2.Val);
1942 /* If the right hand side is constant, and the generator function
1943 * expects the lhs in the primary, remove the push of the primary
1949 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1952 ltype |= CF_REG; /* Value is in register */
1956 /* Determine the type of the operation result. If the left
1957 * operand is of type char and the right is a constant, or
1958 * if both operands are of type char, we will encode the
1959 * operation as char operation. Otherwise the default
1960 * promotions are used.
1962 if (IsTypeChar (Expr->Type) && (IsTypeChar (Expr2.Type) || rconst)) {
1964 if (IsSignUnsigned (Expr->Type) || IsSignUnsigned (Expr2.Type)) {
1965 flags |= CF_UNSIGNED;
1968 flags |= CF_FORCECHAR;
1971 unsigned rtype = TypeOf (Expr2.Type) | (flags & CF_CONST);
1972 flags |= g_typeadjust (ltype, rtype);
1976 Gen->Func (flags, Expr2.Val);
1978 /* The result is an rvalue in the primary */
1979 ED_MakeRValExpr (Expr);
1982 /* Result type is always int */
1983 Expr->Type = type_int;
1985 /* Condition codes are set */
1992 static void hie9 (ExprDesc *Expr)
1993 /* Process * and / operators. */
1995 static const GenDesc hie9_ops[] = {
1996 { TOK_STAR, GEN_NOPUSH, g_mul },
1997 { TOK_DIV, GEN_NOPUSH, g_div },
1998 { TOK_MOD, GEN_NOPUSH, g_mod },
1999 { TOK_INVALID, 0, 0 }
2003 hie_internal (hie9_ops, Expr, hie10, &UsedGen);
2008 static void parseadd (ExprDesc* Expr)
2009 /* Parse an expression with the binary plus operator. Expr contains the
2010 * unprocessed left hand side of the expression and will contain the
2011 * result of the expression on return.
2015 unsigned flags; /* Operation flags */
2016 CodeMark Mark; /* Remember code position */
2017 type* lhst; /* Type of left hand side */
2018 type* rhst; /* Type of right hand side */
2021 /* Skip the PLUS token */
2024 /* Get the left hand side type, initialize operation flags */
2028 /* Check for constness on both sides */
2029 if (ED_IsConst (Expr)) {
2031 /* The left hand side is a constant of some sort. Good. Get rhs */
2033 if (ED_IsConstAbs (&Expr2)) {
2035 /* Right hand side is a constant numeric value. Get the rhs type */
2038 /* Both expressions are constants. Check for pointer arithmetic */
2039 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2040 /* Left is pointer, right is int, must scale rhs */
2041 Expr->Val += Expr2.Val * CheckedPSizeOf (lhst);
2042 /* Result type is a pointer */
2043 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2044 /* Left is int, right is pointer, must scale lhs */
2045 Expr->Val = Expr->Val * CheckedPSizeOf (rhst) + Expr2.Val;
2046 /* Result type is a pointer */
2047 Expr->Type = Expr2.Type;
2048 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2049 /* Integer addition */
2050 Expr->Val += Expr2.Val;
2051 typeadjust (Expr, &Expr2, 1);
2054 Error ("Invalid operands for binary operator `+'");
2059 /* lhs is a constant and rhs is not constant. Load rhs into
2062 ExprLoad (CF_NONE, &Expr2);
2064 /* Beware: The check above (for lhs) lets not only pass numeric
2065 * constants, but also constant addresses (labels), maybe even
2066 * with an offset. We have to check for that here.
2069 /* First, get the rhs type. */
2073 if (ED_IsLocAbs (Expr)) {
2074 /* A numerical constant */
2077 /* Constant address label */
2078 flags |= GlobalModeFlags (Expr->Flags) | CF_CONSTADDR;
2081 /* Check for pointer arithmetic */
2082 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2083 /* Left is pointer, right is int, must scale rhs */
2084 g_scale (CF_INT, CheckedPSizeOf (lhst));
2085 /* Operate on pointers, result type is a pointer */
2087 /* Generate the code for the add */
2088 if (ED_GetLoc (Expr) == E_LOC_ABS) {
2089 /* Numeric constant */
2090 g_inc (flags, Expr->Val);
2092 /* Constant address */
2093 g_addaddr_static (flags, Expr->Name, Expr->Val);
2095 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2097 /* Left is int, right is pointer, must scale lhs. */
2098 unsigned ScaleFactor = CheckedPSizeOf (rhst);
2100 /* Operate on pointers, result type is a pointer */
2102 Expr->Type = Expr2.Type;
2104 /* Since we do already have rhs in the primary, if lhs is
2105 * not a numeric constant, and the scale factor is not one
2106 * (no scaling), we must take the long way over the stack.
2108 if (ED_IsLocAbs (Expr)) {
2109 /* Numeric constant, scale lhs */
2110 Expr->Val *= ScaleFactor;
2111 /* Generate the code for the add */
2112 g_inc (flags, Expr->Val);
2113 } else if (ScaleFactor == 1) {
2114 /* Constant address but no need to scale */
2115 g_addaddr_static (flags, Expr->Name, Expr->Val);
2117 /* Constant address that must be scaled */
2118 g_push (TypeOf (Expr2.Type), 0); /* rhs --> stack */
2119 g_getimmed (flags, Expr->Name, Expr->Val);
2120 g_scale (CF_PTR, ScaleFactor);
2123 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2124 /* Integer addition */
2125 flags |= typeadjust (Expr, &Expr2, 1);
2126 /* Generate the code for the add */
2127 if (ED_IsLocAbs (Expr)) {
2128 /* Numeric constant */
2129 g_inc (flags, Expr->Val);
2131 /* Constant address */
2132 g_addaddr_static (flags, Expr->Name, Expr->Val);
2136 Error ("Invalid operands for binary operator `+'");
2139 /* Result is a rvalue in primary register */
2140 ED_MakeRValExpr (Expr);
2145 /* Left hand side is not constant. Get the value onto the stack. */
2146 ExprLoad (CF_NONE, Expr); /* --> primary register */
2147 Mark = GetCodePos ();
2148 g_push (TypeOf (Expr->Type), 0); /* --> stack */
2150 /* Evaluate the rhs */
2151 if (evalexpr (CF_NONE, hie9, &Expr2) == 0) {
2153 /* Right hand side is a constant. Get the rhs type */
2156 /* Remove pushed value from stack */
2158 pop (TypeOf (Expr->Type));
2160 /* Check for pointer arithmetic */
2161 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2162 /* Left is pointer, right is int, must scale rhs */
2163 Expr2.Val *= CheckedPSizeOf (lhst);
2164 /* Operate on pointers, result type is a pointer */
2166 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2167 /* Left is int, right is pointer, must scale lhs (ptr only) */
2168 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2169 /* Operate on pointers, result type is a pointer */
2171 Expr->Type = Expr2.Type;
2172 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2173 /* Integer addition */
2174 flags = typeadjust (Expr, &Expr2, 1);
2177 Error ("Invalid operands for binary operator `+'");
2180 /* Generate code for the add */
2181 g_inc (flags | CF_CONST, Expr2.Val);
2185 /* lhs and rhs are not constant. Get the rhs type. */
2188 /* Check for pointer arithmetic */
2189 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2190 /* Left is pointer, right is int, must scale rhs */
2191 g_scale (CF_INT, CheckedPSizeOf (lhst));
2192 /* Operate on pointers, result type is a pointer */
2194 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2195 /* Left is int, right is pointer, must scale lhs */
2196 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2197 g_swap (CF_INT); /* Swap TOS and primary */
2198 g_scale (CF_INT, CheckedPSizeOf (rhst));
2199 /* Operate on pointers, result type is a pointer */
2201 Expr->Type = Expr2.Type;
2202 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2203 /* Integer addition. Note: Result is never constant.
2204 * Problem here is that typeadjust does not know if the
2205 * variable is an rvalue or lvalue, so if both operands
2206 * are dereferenced constant numeric addresses, typeadjust
2207 * thinks the operation works on constants. Removing
2208 * CF_CONST here means handling the symptoms, however, the
2209 * whole parser is such a mess that I fear to break anything
2210 * when trying to apply another solution.
2212 flags = typeadjust (Expr, &Expr2, 0) & ~CF_CONST;
2215 Error ("Invalid operands for binary operator `+'");
2218 /* Generate code for the add */
2223 /* Result is a rvalue in primary register */
2224 ED_MakeRValExpr (Expr);
2227 /* Condition codes not set */
2228 Expr->Test &= ~E_CC;
2234 static void parsesub (ExprDesc* Expr)
2235 /* Parse an expression with the binary minus operator. Expr contains the
2236 * unprocessed left hand side of the expression and will contain the
2237 * result of the expression on return.
2241 unsigned flags; /* Operation flags */
2242 type* lhst; /* Type of left hand side */
2243 type* rhst; /* Type of right hand side */
2244 CodeMark Mark1; /* Save position of output queue */
2245 CodeMark Mark2; /* Another position in the queue */
2246 int rscale; /* Scale factor for the result */
2249 /* Skip the MINUS token */
2252 /* Get the left hand side type, initialize operation flags */
2255 rscale = 1; /* Scale by 1, that is, don't scale */
2257 /* Remember the output queue position, then bring the value onto the stack */
2258 Mark1 = GetCodePos ();
2259 ExprLoad (CF_NONE, Expr); /* --> primary register */
2260 Mark2 = GetCodePos ();
2261 g_push (TypeOf (lhst), 0); /* --> stack */
2263 /* Parse the right hand side */
2264 if (evalexpr (CF_NONE, hie9, &Expr2) == 0) {
2266 /* The right hand side is constant. Get the rhs type. */
2269 /* Check left hand side */
2270 if (ED_IsConstAbs (Expr)) {
2272 /* Both sides are constant, remove generated code */
2274 pop (TypeOf (lhst)); /* Clean up the stack */
2276 /* Check for pointer arithmetic */
2277 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2278 /* Left is pointer, right is int, must scale rhs */
2279 Expr->Val -= Expr2.Val * CheckedPSizeOf (lhst);
2280 /* Operate on pointers, result type is a pointer */
2281 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2282 /* Left is pointer, right is pointer, must scale result */
2283 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2284 Error ("Incompatible pointer types");
2286 Expr->Val = (Expr->Val - Expr2.Val) /
2287 CheckedPSizeOf (lhst);
2289 /* Operate on pointers, result type is an integer */
2290 Expr->Type = type_int;
2291 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2292 /* Integer subtraction */
2293 typeadjust (Expr, &Expr2, 1);
2294 Expr->Val -= Expr2.Val;
2297 Error ("Invalid operands for binary operator `-'");
2300 /* Result is constant, condition codes not set */
2301 Expr->Test &= ~E_CC;
2305 /* Left hand side is not constant, right hand side is.
2306 * Remove pushed value from stack.
2309 pop (TypeOf (lhst));
2311 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2312 /* Left is pointer, right is int, must scale rhs */
2313 Expr2.Val *= CheckedPSizeOf (lhst);
2314 /* Operate on pointers, result type is a pointer */
2316 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2317 /* Left is pointer, right is pointer, must scale result */
2318 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2319 Error ("Incompatible pointer types");
2321 rscale = CheckedPSizeOf (lhst);
2323 /* Operate on pointers, result type is an integer */
2325 Expr->Type = type_int;
2326 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2327 /* Integer subtraction */
2328 flags = typeadjust (Expr, &Expr2, 1);
2331 Error ("Invalid operands for binary operator `-'");
2334 /* Do the subtraction */
2335 g_dec (flags | CF_CONST, Expr2.Val);
2337 /* If this was a pointer subtraction, we must scale the result */
2339 g_scale (flags, -rscale);
2342 /* Result is a rvalue in the primary register */
2343 ED_MakeRValExpr (Expr);
2344 Expr->Test &= ~E_CC;
2350 /* Right hand side is not constant. Get the rhs type. */
2353 /* Check for pointer arithmetic */
2354 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2355 /* Left is pointer, right is int, must scale rhs */
2356 g_scale (CF_INT, CheckedPSizeOf (lhst));
2357 /* Operate on pointers, result type is a pointer */
2359 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2360 /* Left is pointer, right is pointer, must scale result */
2361 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2362 Error ("Incompatible pointer types");
2364 rscale = CheckedPSizeOf (lhst);
2366 /* Operate on pointers, result type is an integer */
2368 Expr->Type = type_int;
2369 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2370 /* Integer subtraction. If the left hand side descriptor says that
2371 * the lhs is const, we have to remove this mark, since this is no
2372 * longer true, lhs is on stack instead.
2374 if (ED_IsLocAbs (Expr)) {
2375 ED_MakeRValExpr (Expr);
2377 /* Adjust operand types */
2378 flags = typeadjust (Expr, &Expr2, 0);
2381 Error ("Invalid operands for binary operator `-'");
2384 /* Generate code for the sub (the & is a hack here) */
2385 g_sub (flags & ~CF_CONST, 0);
2387 /* If this was a pointer subtraction, we must scale the result */
2389 g_scale (flags, -rscale);
2392 /* Result is a rvalue in the primary register */
2393 ED_MakeRValExpr (Expr);
2394 Expr->Test &= ~E_CC;
2400 static void hie8 (ExprDesc* Expr)
2401 /* Process + and - binary operators. */
2404 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2405 if (CurTok.Tok == TOK_PLUS) {
2415 static void hie7 (ExprDesc* Expr)
2416 /* Parse << and >>. */
2418 static const GenDesc hie7_ops [] = {
2419 { TOK_SHL, GEN_NOPUSH, g_asl },
2420 { TOK_SHR, GEN_NOPUSH, g_asr },
2421 { TOK_INVALID, 0, 0 }
2425 hie_internal (hie7_ops, Expr, hie8, &UsedGen);
2430 static void hie6 (ExprDesc* Expr)
2431 /* Handle greater-than type comparators */
2433 static const GenDesc hie6_ops [] = {
2434 { TOK_LT, GEN_NOPUSH, g_lt },
2435 { TOK_LE, GEN_NOPUSH, g_le },
2436 { TOK_GE, GEN_NOPUSH, g_ge },
2437 { TOK_GT, GEN_NOPUSH, g_gt },
2438 { TOK_INVALID, 0, 0 }
2440 hie_compare (hie6_ops, Expr, hie7);
2445 static void hie5 (ExprDesc* Expr)
2446 /* Handle == and != */
2448 static const GenDesc hie5_ops[] = {
2449 { TOK_EQ, GEN_NOPUSH, g_eq },
2450 { TOK_NE, GEN_NOPUSH, g_ne },
2451 { TOK_INVALID, 0, 0 }
2453 hie_compare (hie5_ops, Expr, hie6);
2458 static void hie4 (ExprDesc* Expr)
2459 /* Handle & (bitwise and) */
2461 static const GenDesc hie4_ops[] = {
2462 { TOK_AND, GEN_NOPUSH, g_and },
2463 { TOK_INVALID, 0, 0 }
2467 hie_internal (hie4_ops, Expr, hie5, &UsedGen);
2472 static void hie3 (ExprDesc* Expr)
2473 /* Handle ^ (bitwise exclusive or) */
2475 static const GenDesc hie3_ops[] = {
2476 { TOK_XOR, GEN_NOPUSH, g_xor },
2477 { TOK_INVALID, 0, 0 }
2481 hie_internal (hie3_ops, Expr, hie4, &UsedGen);
2486 static void hie2 (ExprDesc* Expr)
2487 /* Handle | (bitwise or) */
2489 static const GenDesc hie2_ops[] = {
2490 { TOK_OR, GEN_NOPUSH, g_or },
2491 { TOK_INVALID, 0, 0 }
2495 hie_internal (hie2_ops, Expr, hie3, &UsedGen);
2500 static void hieAndPP (ExprDesc* Expr)
2501 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2502 * called recursively from the preprocessor.
2507 ConstAbsIntExpr (hie2, Expr);
2508 while (CurTok.Tok == TOK_BOOL_AND) {
2514 ConstAbsIntExpr (hie2, &Expr2);
2516 /* Combine the two */
2517 Expr->Val = (Expr->Val && Expr2.Val);
2523 static void hieOrPP (ExprDesc *Expr)
2524 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2525 * called recursively from the preprocessor.
2530 ConstAbsIntExpr (hieAndPP, Expr);
2531 while (CurTok.Tok == TOK_BOOL_OR) {
2537 ConstAbsIntExpr (hieAndPP, &Expr2);
2539 /* Combine the two */
2540 Expr->Val = (Expr->Val || Expr2.Val);
2546 static void hieAnd (ExprDesc* Expr, unsigned TrueLab, int* BoolOp)
2547 /* Process "exp && exp" */
2553 if (CurTok.Tok == TOK_BOOL_AND) {
2555 /* Tell our caller that we're evaluating a boolean */
2558 /* Get a label that we will use for false expressions */
2559 lab = GetLocalLabel ();
2561 /* If the expr hasn't set condition codes, set the force-test flag */
2562 if ((Expr->Test & E_CC) == 0) {
2563 Expr->Test |= E_FORCETEST;
2566 /* Load the value */
2567 ExprLoad (CF_FORCECHAR, Expr);
2569 /* Generate the jump */
2570 g_falsejump (CF_NONE, lab);
2572 /* Parse more boolean and's */
2573 while (CurTok.Tok == TOK_BOOL_AND) {
2580 if ((Expr2.Test & E_CC) == 0) {
2581 Expr2.Test |= E_FORCETEST;
2583 ExprLoad (CF_FORCECHAR, &Expr2);
2585 /* Do short circuit evaluation */
2586 if (CurTok.Tok == TOK_BOOL_AND) {
2587 g_falsejump (CF_NONE, lab);
2589 /* Last expression - will evaluate to true */
2590 g_truejump (CF_NONE, TrueLab);
2594 /* Define the false jump label here */
2595 g_defcodelabel (lab);
2597 /* The result is an rvalue in primary */
2598 ED_MakeRValExpr (Expr);
2599 Expr->Test |= E_CC; /* Condition codes are set */
2605 static void hieOr (ExprDesc *Expr)
2606 /* Process "exp || exp". */
2609 int BoolOp = 0; /* Did we have a boolean op? */
2610 int AndOp; /* Did we have a && operation? */
2611 unsigned TrueLab; /* Jump to this label if true */
2615 TrueLab = GetLocalLabel ();
2617 /* Call the next level parser */
2618 hieAnd (Expr, TrueLab, &BoolOp);
2620 /* Any boolean or's? */
2621 if (CurTok.Tok == TOK_BOOL_OR) {
2623 /* If the expr hasn't set condition codes, set the force-test flag */
2624 if ((Expr->Test & E_CC) == 0) {
2625 Expr->Test |= E_FORCETEST;
2628 /* Get first expr */
2629 ExprLoad (CF_FORCECHAR, Expr);
2631 /* For each expression jump to TrueLab if true. Beware: If we
2632 * had && operators, the jump is already in place!
2635 g_truejump (CF_NONE, TrueLab);
2638 /* Remember that we had a boolean op */
2641 /* while there's more expr */
2642 while (CurTok.Tok == TOK_BOOL_OR) {
2649 hieAnd (&Expr2, TrueLab, &AndOp);
2650 if ((Expr2.Test & E_CC) == 0) {
2651 Expr2.Test |= E_FORCETEST;
2653 ExprLoad (CF_FORCECHAR, &Expr2);
2655 /* If there is more to come, add shortcut boolean eval. */
2656 g_truejump (CF_NONE, TrueLab);
2660 /* The result is an rvalue in primary */
2661 ED_MakeRValExpr (Expr);
2662 Expr->Test |= E_CC; /* Condition codes are set */
2665 /* If we really had boolean ops, generate the end sequence */
2667 DoneLab = GetLocalLabel ();
2668 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2669 g_falsejump (CF_NONE, DoneLab);
2670 g_defcodelabel (TrueLab);
2671 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2672 g_defcodelabel (DoneLab);
2678 static void hieQuest (ExprDesc* Expr)
2679 /* Parse the ternary operator */
2683 ExprDesc Expr2; /* Expression 2 */
2684 ExprDesc Expr3; /* Expression 3 */
2685 int Expr2IsNULL; /* Expression 2 is a NULL pointer */
2686 int Expr3IsNULL; /* Expression 3 is a NULL pointer */
2687 type* ResultType; /* Type of result */
2690 /* Call the lower level eval routine */
2691 if (Preprocessing) {
2697 /* Check if it's a ternary expression */
2698 if (CurTok.Tok == TOK_QUEST) {
2700 if ((Expr->Test & E_CC) == 0) {
2701 /* Condition codes not set, force a test */
2702 Expr->Test |= E_FORCETEST;
2704 ExprLoad (CF_NONE, Expr);
2705 labf = GetLocalLabel ();
2706 g_falsejump (CF_NONE, labf);
2708 /* Parse second expression. Remember for later if it is a NULL pointer
2709 * expression, then load it into the primary.
2711 ExprWithCheck (hie1, &Expr2);
2712 Expr2IsNULL = ED_IsNullPtr (&Expr2);
2713 if (!IsTypeVoid (Expr2.Type)) {
2714 /* Load it into the primary */
2715 ExprLoad (CF_NONE, &Expr2);
2716 ED_MakeRValExpr (&Expr2);
2718 labt = GetLocalLabel ();
2722 /* Jump here if the first expression was false */
2723 g_defcodelabel (labf);
2725 /* Parse second expression. Remember for later if it is a NULL pointer
2726 * expression, then load it into the primary.
2728 ExprWithCheck (hie1, &Expr3);
2729 Expr3IsNULL = ED_IsNullPtr (&Expr3);
2730 if (!IsTypeVoid (Expr3.Type)) {
2731 /* Load it into the primary */
2732 ExprLoad (CF_NONE, &Expr3);
2733 ED_MakeRValExpr (&Expr3);
2736 /* Check if any conversions are needed, if so, do them.
2737 * Conversion rules for ?: expression are:
2738 * - if both expressions are int expressions, default promotion
2739 * rules for ints apply.
2740 * - if both expressions are pointers of the same type, the
2741 * result of the expression is of this type.
2742 * - if one of the expressions is a pointer and the other is
2743 * a zero constant, the resulting type is that of the pointer
2745 * - if both expressions are void expressions, the result is of
2747 * - all other cases are flagged by an error.
2749 if (IsClassInt (Expr2.Type) && IsClassInt (Expr3.Type)) {
2751 /* Get common type */
2752 ResultType = promoteint (Expr2.Type, Expr3.Type);
2754 /* Convert the third expression to this type if needed */
2755 TypeConversion (&Expr3, ResultType);
2757 /* Setup a new label so that the expr3 code will jump around
2758 * the type cast code for expr2.
2760 labf = GetLocalLabel (); /* Get new label */
2761 g_jump (labf); /* Jump around code */
2763 /* The jump for expr2 goes here */
2764 g_defcodelabel (labt);
2766 /* Create the typecast code for expr2 */
2767 TypeConversion (&Expr2, ResultType);
2769 /* Jump here around the typecase code. */
2770 g_defcodelabel (labf);
2771 labt = 0; /* Mark other label as invalid */
2773 } else if (IsClassPtr (Expr2.Type) && IsClassPtr (Expr3.Type)) {
2774 /* Must point to same type */
2775 if (TypeCmp (Indirect (Expr2.Type), Indirect (Expr3.Type)) < TC_EQUAL) {
2776 Error ("Incompatible pointer types");
2778 /* Result has the common type */
2779 ResultType = Expr2.Type;
2780 } else if (IsClassPtr (Expr2.Type) && Expr3IsNULL) {
2781 /* Result type is pointer, no cast needed */
2782 ResultType = Expr2.Type;
2783 } else if (Expr2IsNULL && IsClassPtr (Expr3.Type)) {
2784 /* Result type is pointer, no cast needed */
2785 ResultType = Expr3.Type;
2786 } else if (IsTypeVoid (Expr2.Type) && IsTypeVoid (Expr3.Type)) {
2787 /* Result type is void */
2788 ResultType = Expr3.Type;
2790 Error ("Incompatible types");
2791 ResultType = Expr2.Type; /* Doesn't matter here */
2794 /* If we don't have the label defined until now, do it */
2796 g_defcodelabel (labt);
2799 /* Setup the target expression */
2800 ED_MakeRValExpr (Expr);
2801 Expr->Type = ResultType;
2807 static void opeq (const GenDesc* Gen, ExprDesc* Expr)
2808 /* Process "op=" operators. */
2815 /* op= can only be used with lvalues */
2816 if (!ED_IsLVal (Expr)) {
2817 Error ("Invalid lvalue in assignment");
2821 /* There must be an integer or pointer on the left side */
2822 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
2823 Error ("Invalid left operand type");
2824 /* Continue. Wrong code will be generated, but the compiler won't
2825 * break, so this is the best error recovery.
2829 /* Skip the operator token */
2832 /* Determine the type of the lhs */
2833 flags = TypeOf (Expr->Type);
2834 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) && IsTypePtr (Expr->Type);
2836 /* Get the lhs address on stack (if needed) */
2839 /* Fetch the lhs into the primary register if needed */
2840 ExprLoad (CF_NONE, Expr);
2842 /* Bring the lhs on stack */
2843 Mark = GetCodePos ();
2846 /* Evaluate the rhs */
2847 if (evalexpr (CF_NONE, hie1, &Expr2) == 0) {
2848 /* The resulting value is a constant. If the generator has the NOPUSH
2849 * flag set, don't push the lhs.
2851 if (Gen->Flags & GEN_NOPUSH) {
2856 /* lhs is a pointer, scale rhs */
2857 Expr2.Val *= CheckedSizeOf (Expr->Type+1);
2860 /* If the lhs is character sized, the operation may be later done
2863 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
2864 flags |= CF_FORCECHAR;
2867 /* Special handling for add and sub - some sort of a hack, but short code */
2868 if (Gen->Func == g_add) {
2869 g_inc (flags | CF_CONST, Expr2.Val);
2870 } else if (Gen->Func == g_sub) {
2871 g_dec (flags | CF_CONST, Expr2.Val);
2873 Gen->Func (flags | CF_CONST, Expr2.Val);
2876 /* rhs is not constant and already in the primary register */
2878 /* lhs is a pointer, scale rhs */
2879 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Expr->Type+1));
2882 /* If the lhs is character sized, the operation may be later done
2885 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
2886 flags |= CF_FORCECHAR;
2889 /* Adjust the types of the operands if needed */
2890 Gen->Func (g_typeadjust (flags, TypeOf (Expr2.Type)), 0);
2893 ED_MakeRValExpr (Expr);
2898 static void addsubeq (const GenDesc* Gen, ExprDesc *Expr)
2899 /* Process the += and -= operators */
2907 /* We're currently only able to handle some adressing modes */
2908 if (ED_GetLoc (Expr) == E_LOC_EXPR || ED_GetLoc (Expr) == E_LOC_PRIMARY) {
2909 /* Use generic routine */
2914 /* We must have an lvalue */
2915 if (ED_IsRVal (Expr)) {
2916 Error ("Invalid lvalue in assignment");
2920 /* There must be an integer or pointer on the left side */
2921 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
2922 Error ("Invalid left operand type");
2923 /* Continue. Wrong code will be generated, but the compiler won't
2924 * break, so this is the best error recovery.
2928 /* Skip the operator */
2931 /* Check if we have a pointer expression and must scale rhs */
2932 MustScale = IsTypePtr (Expr->Type);
2934 /* Initialize the code generator flags */
2938 /* Evaluate the rhs */
2940 if (ED_IsConstAbs (&Expr2)) {
2941 /* The resulting value is a constant. Scale it. */
2943 Expr2.Val *= CheckedSizeOf (Indirect (Expr->Type));
2948 /* Not constant, load into the primary */
2949 ExprLoad (CF_NONE, &Expr2);
2951 /* lhs is a pointer, scale rhs */
2952 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Indirect (Expr->Type)));
2956 /* Setup the code generator flags */
2957 lflags |= TypeOf (Expr->Type) | CF_FORCECHAR;
2958 rflags |= TypeOf (Expr2.Type);
2960 /* Convert the type of the lhs to that of the rhs */
2961 g_typecast (lflags, rflags);
2963 /* Output apropriate code depending on the location */
2964 switch (ED_GetLoc (Expr)) {
2967 /* Absolute: numeric address or const */
2968 lflags |= CF_ABSOLUTE;
2969 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2970 g_addeqstatic (lflags, Expr->Name, Expr->Val, Expr2.Val);
2972 g_subeqstatic (lflags, Expr->Name, Expr->Val, Expr2.Val);
2977 /* Global variable */
2978 lflags |= CF_EXTERNAL;
2979 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2980 g_addeqstatic (lflags, Expr->Name, Expr->Val, Expr2.Val);
2982 g_subeqstatic (lflags, Expr->Name, Expr->Val, Expr2.Val);
2988 /* Static variable or literal in the literal pool */
2989 lflags |= CF_STATIC;
2990 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2991 g_addeqstatic (lflags, Expr->Name, Expr->Val, Expr2.Val);
2993 g_subeqstatic (lflags, Expr->Name, Expr->Val, Expr2.Val);
2997 case E_LOC_REGISTER:
2998 /* Register variable */
2999 lflags |= CF_REGVAR;
3000 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3001 g_addeqstatic (lflags, Expr->Name, Expr->Val, Expr2.Val);
3003 g_subeqstatic (lflags, Expr->Name, Expr->Val, Expr2.Val);
3008 /* Value on the stack */
3009 if (Gen->Tok == TOK_PLUS_ASSIGN) {
3010 g_addeqlocal (lflags, Expr->Val, Expr2.Val);
3012 g_subeqlocal (lflags, Expr->Val, Expr2.Val);
3017 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
3020 /* Expression is a rvalue in the primary now */
3021 ED_MakeRValExpr (Expr);
3026 void hie1 (ExprDesc* Expr)
3027 /* Parse first level of expression hierarchy. */
3030 switch (CurTok.Tok) {
3036 case TOK_PLUS_ASSIGN:
3037 addsubeq (&GenPASGN, Expr);
3040 case TOK_MINUS_ASSIGN:
3041 addsubeq (&GenSASGN, Expr);
3044 case TOK_MUL_ASSIGN:
3045 opeq (&GenMASGN, Expr);
3048 case TOK_DIV_ASSIGN:
3049 opeq (&GenDASGN, Expr);
3052 case TOK_MOD_ASSIGN:
3053 opeq (&GenMOASGN, Expr);
3056 case TOK_SHL_ASSIGN:
3057 opeq (&GenSLASGN, Expr);
3060 case TOK_SHR_ASSIGN:
3061 opeq (&GenSRASGN, Expr);
3064 case TOK_AND_ASSIGN:
3065 opeq (&GenAASGN, Expr);
3068 case TOK_XOR_ASSIGN:
3069 opeq (&GenXOASGN, Expr);
3073 opeq (&GenOASGN, Expr);
3083 void hie0 (ExprDesc *Expr)
3084 /* Parse comma operator. */
3087 while (CurTok.Tok == TOK_COMMA) {
3095 int evalexpr (unsigned Flags, void (*Func) (ExprDesc*), ExprDesc* Expr)
3096 /* Will evaluate an expression via the given function. If the result is a
3097 * constant, 0 is returned and the value is put in the Expr struct. If the
3098 * result is not constant, ExprLoad is called to bring the value into the
3099 * primary register and 1 is returned.
3103 ExprWithCheck (Func, Expr);
3105 /* Check for a constant expression */
3106 if (ED_IsConstAbs (Expr)) {
3107 /* Constant expression */
3110 /* Not constant, load into the primary */
3111 ExprLoad (Flags, Expr);
3118 void Expression0 (ExprDesc* Expr)
3119 /* Evaluate an expression via hie0 and put the result into the primary register */
3121 ExprWithCheck (hie0, Expr);
3122 ExprLoad (CF_NONE, Expr);
3127 void ConstExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3128 /* Will evaluate an expression via the given function. If the result is not
3129 * a constant of some sort, a diagnostic will be printed, and the value is
3130 * replaced by a constant one to make sure there are no internal errors that
3131 * result from this input error.
3134 ExprWithCheck (Func, Expr);
3135 if (!ED_IsConst (Expr)) {
3136 Error ("Constant expression expected");
3137 /* To avoid any compiler errors, make the expression a valid const */
3138 ED_MakeConstAbsInt (Expr, 1);
3144 void BoolExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3145 /* Will evaluate an expression via the given function. If the result is not
3146 * something that may be evaluated in a boolean context, a diagnostic will be
3147 * printed, and the value is replaced by a constant one to make sure there
3148 * are no internal errors that result from this input error.
3151 ExprWithCheck (Func, Expr);
3152 if (!ED_IsBool (Expr)) {
3153 Error ("Boolean expression expected");
3154 /* To avoid any compiler errors, make the expression a valid int */
3155 ED_MakeConstAbsInt (Expr, 1);
3161 void ConstAbsIntExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3162 /* Will evaluate an expression via the given function. If the result is not
3163 * a constant numeric integer value, a diagnostic will be printed, and the
3164 * value is replaced by a constant one to make sure there are no internal
3165 * errors that result from this input error.
3168 ExprWithCheck (Func, Expr);
3169 if (!ED_IsConstAbsInt (Expr)) {
3170 Error ("Constant integer expression expected");
3171 /* To avoid any compiler errors, make the expression a valid const */
3172 ED_MakeConstAbsInt (Expr, 1);