3 * Ullrich von Bassewitz, 21.06.1998
13 #include "debugflag.h"
20 #include "assignment.h"
32 #include "shiftexpr.h"
42 /*****************************************************************************/
44 /*****************************************************************************/
48 /* Generator attributes */
49 #define GEN_NOPUSH 0x01 /* Don't push lhs */
51 /* Map a generator function and its attributes to a token */
53 token_t Tok; /* Token to map to */
54 unsigned Flags; /* Flags for generator function */
55 void (*Func) (unsigned, unsigned long); /* Generator func */
58 /* Descriptors for the operations */
59 static GenDesc GenPASGN = { TOK_PLUS_ASSIGN, GEN_NOPUSH, g_add };
60 static GenDesc GenSASGN = { TOK_MINUS_ASSIGN, GEN_NOPUSH, g_sub };
61 static GenDesc GenMASGN = { TOK_MUL_ASSIGN, GEN_NOPUSH, g_mul };
62 static GenDesc GenDASGN = { TOK_DIV_ASSIGN, GEN_NOPUSH, g_div };
63 static GenDesc GenMOASGN = { TOK_MOD_ASSIGN, GEN_NOPUSH, g_mod };
64 static GenDesc GenSLASGN = { TOK_SHL_ASSIGN, GEN_NOPUSH, g_asl };
65 static GenDesc GenSRASGN = { TOK_SHR_ASSIGN, GEN_NOPUSH, g_asr };
66 static GenDesc GenAASGN = { TOK_AND_ASSIGN, GEN_NOPUSH, g_and };
67 static GenDesc GenXOASGN = { TOK_XOR_ASSIGN, GEN_NOPUSH, g_xor };
68 static GenDesc GenOASGN = { TOK_OR_ASSIGN, GEN_NOPUSH, g_or };
72 /*****************************************************************************/
73 /* Helper functions */
74 /*****************************************************************************/
78 static unsigned GlobalModeFlags (unsigned Flags)
79 /* Return the addressing mode flags for the variable with the given flags */
81 switch (Flags & E_MASK_LOC) {
82 case E_LOC_GLOBAL: return CF_EXTERNAL;
83 case E_LOC_STATIC: return CF_STATIC;
84 case E_LOC_REGISTER: return CF_REGVAR;
86 Internal ("GlobalModeFlags: Invalid flags value: %u", Flags);
92 void ExprWithCheck (void (*Func) (ExprDesc*), ExprDesc *Expr)
93 /* Call an expression function with checks. */
95 /* Remember the stack pointer */
98 /* Call the expression function */
101 /* Do some checks if code generation is still constistent */
102 if (StackPtr != OldSP) {
105 "Code generation messed up!\n"
106 "StackPtr is %d, should be %d",
109 Internal ("StackPtr is %d, should be %d\n", StackPtr, OldSP);
116 static type* promoteint (type* lhst, type* rhst)
117 /* In an expression with two ints, return the type of the result */
119 /* Rules for integer types:
120 * - If one of the values is a long, the result is long.
121 * - If one of the values is unsigned, the result is also unsigned.
122 * - Otherwise the result is an int.
124 if (IsTypeLong (lhst) || IsTypeLong (rhst)) {
125 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
131 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
141 static unsigned typeadjust (ExprDesc* lhs, ExprDesc* rhs, int NoPush)
142 /* Adjust the two values for a binary operation. lhs is expected on stack or
143 * to be constant, rhs is expected to be in the primary register or constant.
144 * The function will put the type of the result into lhs and return the
145 * code generator flags for the operation.
146 * If NoPush is given, it is assumed that the operation does not expect the lhs
147 * to be on stack, and that lhs is in a register instead.
148 * Beware: The function does only accept int types.
151 unsigned ltype, rtype;
154 /* Get the type strings */
155 type* lhst = lhs->Type;
156 type* rhst = rhs->Type;
158 /* Generate type adjustment code if needed */
159 ltype = TypeOf (lhst);
160 if (ED_IsLocAbs (lhs)) {
164 /* Value is in primary register*/
167 rtype = TypeOf (rhst);
168 if (ED_IsLocAbs (rhs)) {
171 flags = g_typeadjust (ltype, rtype);
173 /* Set the type of the result */
174 lhs->Type = promoteint (lhst, rhst);
176 /* Return the code generator flags */
182 void DefineData (ExprDesc* Expr)
183 /* Output a data definition for the given expression */
185 switch (ED_GetLoc (Expr)) {
188 /* Absolute: numeric address or const */
189 g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->IVal, 0);
193 /* Global variable */
194 g_defdata (CF_EXTERNAL, Expr->Name, Expr->IVal);
199 /* Static variable or literal in the literal pool */
200 g_defdata (CF_STATIC, Expr->Name, Expr->IVal);
204 /* Register variable. Taking the address is usually not
207 if (IS_Get (&AllowRegVarAddr) == 0) {
208 Error ("Cannot take the address of a register variable");
210 g_defdata (CF_REGVAR, Expr->Name, Expr->IVal);
214 Internal ("Unknown constant type: 0x%04X", ED_GetLoc (Expr));
220 static int kcalc (token_t tok, long val1, long val2)
221 /* Calculate an operation with left and right operand constant. */
225 return (val1 == val2);
227 return (val1 != val2);
229 return (val1 < val2);
231 return (val1 <= val2);
233 return (val1 >= val2);
235 return (val1 > val2);
237 return (val1 | val2);
239 return (val1 ^ val2);
241 return (val1 & val2);
243 return (val1 * val2);
246 Error ("Division by zero");
249 return (val1 / val2);
252 Error ("Modulo operation with zero");
255 return (val1 % val2);
257 Internal ("kcalc: got token 0x%X\n", tok);
264 static const GenDesc* FindGen (token_t Tok, const GenDesc* Table)
265 /* Find a token in a generator table */
267 while (Table->Tok != TOK_INVALID) {
268 if (Table->Tok == Tok) {
278 static int TypeSpecAhead (void)
279 /* Return true if some sort of type is waiting (helper for cast and sizeof()
285 /* There's a type waiting if:
287 * We have an opening paren, and
288 * a. the next token is a type, or
289 * b. the next token is a type qualifier, or
290 * c. the next token is a typedef'd type
292 return CurTok.Tok == TOK_LPAREN && (
293 TokIsType (&NextTok) ||
294 TokIsTypeQual (&NextTok) ||
295 (NextTok.Tok == TOK_IDENT &&
296 (Entry = FindSym (NextTok.Ident)) != 0 &&
297 SymIsTypeDef (Entry)));
302 void PushAddr (const ExprDesc* Expr)
303 /* If the expression contains an address that was somehow evaluated,
304 * push this address on the stack. This is a helper function for all
305 * sorts of implicit or explicit assignment functions where the lvalue
306 * must be saved if it's not constant, before evaluating the rhs.
309 /* Get the address on stack if needed */
310 if (ED_IsLocExpr (Expr)) {
311 /* Push the address (always a pointer) */
318 /*****************************************************************************/
320 /*****************************************************************************/
324 static unsigned FunctionParamList (FuncDesc* Func)
325 /* Parse a function parameter list and pass the parameters to the called
326 * function. Depending on several criteria this may be done by just pushing
327 * each parameter separately, or creating the parameter frame once and then
328 * storing into this frame.
329 * The function returns the size of the parameters pushed.
334 /* Initialize variables */
335 SymEntry* Param = 0; /* Keep gcc silent */
336 unsigned ParamSize = 0; /* Size of parameters pushed */
337 unsigned ParamCount = 0; /* Number of parameters pushed */
338 unsigned FrameSize = 0; /* Size of parameter frame */
339 unsigned FrameParams = 0; /* Number of params in frame */
340 int FrameOffs = 0; /* Offset into parameter frame */
341 int Ellipsis = 0; /* Function is variadic */
343 /* As an optimization, we may allocate the complete parameter frame at
344 * once instead of pushing each parameter as it comes. We may do that,
347 * - optimizations that increase code size are enabled (allocating the
348 * stack frame at once gives usually larger code).
349 * - we have more than one parameter to push (don't count the last param
350 * for __fastcall__ functions).
352 * The FrameSize variable will contain a value > 0 if storing into a frame
353 * (instead of pushing) is enabled.
356 if (IS_Get (&CodeSizeFactor) >= 200) {
358 /* Calculate the number and size of the parameters */
359 FrameParams = Func->ParamCount;
360 FrameSize = Func->ParamSize;
361 if (FrameParams > 0 && (Func->Flags & FD_FASTCALL) != 0) {
362 /* Last parameter is not pushed */
363 FrameSize -= CheckedSizeOf (Func->LastParam->Type);
367 /* Do we have more than one parameter in the frame? */
368 if (FrameParams > 1) {
369 /* Okeydokey, setup the frame */
370 FrameOffs = StackPtr;
372 StackPtr -= FrameSize;
374 /* Don't use a preallocated frame */
379 /* Parse the actual parameter list */
380 while (CurTok.Tok != TOK_RPAREN) {
384 /* Count arguments */
387 /* Fetch the pointer to the next argument, check for too many args */
388 if (ParamCount <= Func->ParamCount) {
389 /* Beware: If there are parameters with identical names, they
390 * cannot go into the same symbol table, which means that in this
391 * case of errorneous input, the number of nodes in the symbol
392 * table and ParamCount are NOT equal. We have to handle this case
393 * below to avoid segmentation violations. Since we know that this
394 * problem can only occur if there is more than one parameter,
395 * we will just use the last one.
397 if (ParamCount == 1) {
399 Param = Func->SymTab->SymHead;
400 } else if (Param->NextSym != 0) {
402 Param = Param->NextSym;
403 CHECK ((Param->Flags & SC_PARAM) != 0);
405 } else if (!Ellipsis) {
406 /* Too many arguments. Do we have an open param list? */
407 if ((Func->Flags & FD_VARIADIC) == 0) {
408 /* End of param list reached, no ellipsis */
409 Error ("Too many arguments in function call");
411 /* Assume an ellipsis even in case of errors to avoid an error
412 * message for each other argument.
417 /* Evaluate the parameter expression */
420 /* If we don't have an argument spec, accept anything, otherwise
421 * convert the actual argument to the type needed.
425 /* Convert the argument to the parameter type if needed */
426 TypeConversion (&Expr, Param->Type);
428 /* If we have a prototype, chars may be pushed as chars */
429 Flags |= CF_FORCECHAR;
432 /* Load the value into the primary if it is not already there */
433 LoadExpr (Flags, &Expr);
435 /* Use the type of the argument for the push */
436 Flags |= TypeOf (Expr.Type);
438 /* If this is a fastcall function, don't push the last argument */
439 if (ParamCount != Func->ParamCount || (Func->Flags & FD_FASTCALL) == 0) {
440 unsigned ArgSize = sizeofarg (Flags);
442 /* We have the space already allocated, store in the frame.
443 * Because of invalid type conversions (that have produced an
444 * error before), we can end up here with a non aligned stack
445 * frame. Since no output will be generated anyway, handle
446 * these cases gracefully instead of doing a CHECK.
448 if (FrameSize >= ArgSize) {
449 FrameSize -= ArgSize;
453 FrameOffs -= ArgSize;
455 g_putlocal (Flags | CF_NOKEEP, FrameOffs, Expr.IVal);
457 /* Push the argument */
458 g_push (Flags, Expr.IVal);
461 /* Calculate total parameter size */
462 ParamSize += ArgSize;
465 /* Check for end of argument list */
466 if (CurTok.Tok != TOK_COMMA) {
472 /* Check if we had enough parameters */
473 if (ParamCount < Func->ParamCount) {
474 Error ("Too few arguments in function call");
477 /* The function returns the size of all parameters pushed onto the stack.
478 * However, if there are parameters missing (which is an error and was
479 * flagged by the compiler) AND a stack frame was preallocated above,
480 * we would loose track of the stackpointer and generate an internal error
481 * later. So we correct the value by the parameters that should have been
482 * pushed to avoid an internal compiler error. Since an error was
483 * generated before, no code will be output anyway.
485 return ParamSize + FrameSize;
490 static void FunctionCall (ExprDesc* Expr)
491 /* Perform a function call. */
493 FuncDesc* Func; /* Function descriptor */
494 int IsFuncPtr; /* Flag */
495 unsigned ParamSize; /* Number of parameter bytes */
497 int PtrOffs = 0; /* Offset of function pointer on stack */
498 int IsFastCall = 0; /* True if it's a fast call function */
499 int PtrOnStack = 0; /* True if a pointer copy is on stack */
501 /* Skip the left paren */
504 /* Get a pointer to the function descriptor from the type string */
505 Func = GetFuncDesc (Expr->Type);
507 /* Handle function pointers transparently */
508 IsFuncPtr = IsTypeFuncPtr (Expr->Type);
511 /* Check wether it's a fastcall function that has parameters */
512 IsFastCall = IsFastCallFunc (Expr->Type + 1) && (Func->ParamCount > 0);
514 /* Things may be difficult, depending on where the function pointer
515 * resides. If the function pointer is an expression of some sort
516 * (not a local or global variable), we have to evaluate this
517 * expression now and save the result for later. Since calls to
518 * function pointers may be nested, we must save it onto the stack.
519 * For fastcall functions we do also need to place a copy of the
520 * pointer on stack, since we cannot use a/x.
522 PtrOnStack = IsFastCall || !ED_IsConst (Expr);
525 /* Not a global or local variable, or a fastcall function. Load
526 * the pointer into the primary and mark it as an expression.
528 LoadExpr (CF_NONE, Expr);
529 ED_MakeRValExpr (Expr);
531 /* Remember the code position */
534 /* Push the pointer onto the stack and remember the offset */
539 /* Check for known standard functions and inline them */
540 } else if (Expr->Name != 0) {
541 int StdFunc = FindStdFunc ((const char*) Expr->Name);
543 /* Inline this function */
544 HandleStdFunc (StdFunc, Func, Expr);
549 /* Parse the parameter list */
550 ParamSize = FunctionParamList (Func);
552 /* We need the closing paren here */
555 /* Special handling for function pointers */
558 /* If the function is not a fastcall function, load the pointer to
559 * the function into the primary.
563 /* Not a fastcall function - we may use the primary */
565 /* If we have no parameters, the pointer is still in the
566 * primary. Remove the code to push it and correct the
569 if (ParamSize == 0) {
573 /* Load from the saved copy */
574 g_getlocal (CF_PTR, PtrOffs);
577 /* Load from original location */
578 LoadExpr (CF_NONE, Expr);
581 /* Call the function */
582 g_callind (TypeOf (Expr->Type+1), ParamSize, PtrOffs);
586 /* Fastcall function. We cannot use the primary for the function
587 * pointer and must therefore use an offset to the stack location.
588 * Since fastcall functions may never be variadic, we can use the
589 * index register for this purpose.
591 g_callind (CF_LOCAL, ParamSize, PtrOffs);
594 /* If we have a pointer on stack, remove it */
596 g_space (- (int) sizeofarg (CF_PTR));
605 /* Normal function */
606 g_call (TypeOf (Expr->Type), (const char*) Expr->Name, ParamSize);
610 /* The function result is an rvalue in the primary register */
611 ED_MakeRValExpr (Expr);
612 Expr->Type = GetFuncReturn (Expr->Type);
617 static void Primary (ExprDesc* E)
618 /* This is the lowest level of the expression parser. */
622 /* Initialize fields in the expression stucture */
625 /* Character and integer constants. */
626 if (CurTok.Tok == TOK_ICONST || CurTok.Tok == TOK_CCONST) {
627 E->IVal = CurTok.IVal;
628 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
629 E->Type = CurTok.Type;
634 /* Floating point constant */
635 if (CurTok.Tok == TOK_FCONST) {
636 E->FVal = CurTok.FVal;
637 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
638 E->Type = CurTok.Type;
643 /* Process parenthesized subexpression by calling the whole parser
646 if (CurTok.Tok == TOK_LPAREN) {
653 /* If we run into an identifier in preprocessing mode, we assume that this
654 * is an undefined macro and replace it by a constant value of zero.
656 if (Preprocessing && CurTok.Tok == TOK_IDENT) {
657 ED_MakeConstAbsInt (E, 0);
661 /* All others may only be used if the expression evaluation is not called
662 * recursively by the preprocessor.
665 /* Illegal expression in PP mode */
666 Error ("Preprocessor expression expected");
667 ED_MakeConstAbsInt (E, 1);
671 switch (CurTok.Tok) {
674 /* Identifier. Get a pointer to the symbol table entry */
675 Sym = E->Sym = FindSym (CurTok.Ident);
677 /* Is the symbol known? */
680 /* We found the symbol - skip the name token */
683 /* Check for illegal symbol types */
684 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
685 if (Sym->Flags & SC_TYPE) {
686 /* Cannot use type symbols */
687 Error ("Variable identifier expected");
688 /* Assume an int type to make E valid */
689 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
694 /* Mark the symbol as referenced */
695 Sym->Flags |= SC_REF;
697 /* The expression type is the symbol type */
700 /* Check for legal symbol types */
701 if ((Sym->Flags & SC_CONST) == SC_CONST) {
702 /* Enum or some other numeric constant */
703 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
704 E->IVal = Sym->V.ConstVal;
705 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
707 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
708 E->Name = (unsigned long) Sym->Name;
709 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
710 /* Local variable. If this is a parameter for a variadic
711 * function, we have to add some address calculations, and the
712 * address is not const.
714 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
715 /* Variadic parameter */
716 g_leavariadic (Sym->V.Offs - F_GetParamSize (CurrentFunc));
717 E->Flags = E_LOC_EXPR | E_RTYPE_LVAL;
719 /* Normal parameter */
720 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
721 E->IVal = Sym->V.Offs;
723 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
724 /* Register variable, zero page based */
725 E->Flags = E_LOC_REGISTER | E_RTYPE_LVAL;
726 E->Name = Sym->V.R.RegOffs;
727 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
728 /* Static variable */
729 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
730 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
731 E->Name = (unsigned long) Sym->Name;
733 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
734 E->Name = Sym->V.Label;
737 /* Local static variable */
738 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
739 E->Name = Sym->V.Offs;
742 /* We've made all variables lvalues above. However, this is
743 * not always correct: An array is actually the address of its
744 * first element, which is a rvalue, and a function is a
745 * rvalue, too, because we cannot store anything in a function.
746 * So fix the flags depending on the type.
748 if (IsTypeArray (E->Type) || IsTypeFunc (E->Type)) {
754 /* We did not find the symbol. Remember the name, then skip it */
756 strcpy (Ident, CurTok.Ident);
759 /* IDENT is either an auto-declared function or an undefined variable. */
760 if (CurTok.Tok == TOK_LPAREN) {
761 /* Declare a function returning int. For that purpose, prepare a
762 * function signature for a function having an empty param list
765 Warning ("Function call without a prototype");
766 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
768 E->Flags = E_LOC_GLOBAL | E_RTYPE_RVAL;
769 E->Name = (unsigned long) Sym->Name;
771 /* Undeclared Variable */
772 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
773 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
775 Error ("Undefined symbol: `%s'", Ident);
783 E->Type = GetCharArrayType (GetLiteralPoolOffs () - CurTok.IVal);
784 E->Flags = E_LOC_LITERAL | E_RTYPE_RVAL;
785 E->IVal = CurTok.IVal;
786 E->Name = LiteralPoolLabel;
793 E->Flags = E_LOC_EXPR | E_RTYPE_RVAL;
798 /* Register pseudo variable */
799 E->Type = type_uchar;
800 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
805 /* Register pseudo variable */
807 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
812 /* Register pseudo variable */
813 E->Type = type_ulong;
814 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
819 /* Illegal primary. */
820 Error ("Expression expected");
821 ED_MakeConstAbsInt (E, 1);
828 static void ArrayRef (ExprDesc* Expr)
829 /* Handle an array reference */
839 /* Skip the bracket */
842 /* Get the type of left side */
845 /* We can apply a special treatment for arrays that have a const base
846 * address. This is true for most arrays and will produce a lot better
847 * code. Check if this is a const base address.
849 ConstBaseAddr = (ED_IsLocConst (Expr) || ED_IsLocStack (Expr));
851 /* If we have a constant base, we delay the address fetch */
853 if (!ConstBaseAddr) {
854 /* Get a pointer to the array into the primary */
855 LoadExpr (CF_NONE, Expr);
857 /* Get the array pointer on stack. Do not push more than 16
858 * bit, even if this value is greater, since we cannot handle
859 * other than 16bit stuff when doing indexing.
865 /* TOS now contains ptr to array elements. Get the subscript. */
866 ExprWithCheck (hie0, &SubScript);
868 /* Check the types of array and subscript. We can either have a
869 * pointer/array to the left, in which case the subscript must be of an
870 * integer type, or we have an integer to the left, in which case the
871 * subscript must be a pointer/array.
872 * Since we do the necessary checking here, we can rely later on the
875 if (IsClassPtr (Expr->Type)) {
876 if (!IsClassInt (SubScript.Type)) {
877 Error ("Array subscript is not an integer");
878 /* To avoid any compiler errors, make the expression a valid int */
879 ED_MakeConstAbsInt (&SubScript, 0);
881 ElementType = Indirect (Expr->Type);
882 } else if (IsClassInt (Expr->Type)) {
883 if (!IsClassPtr (SubScript.Type)) {
884 Error ("Subscripted value is neither array nor pointer");
885 /* To avoid compiler errors, make the subscript a char[] at
888 ED_MakeConstAbs (&SubScript, 0, GetCharArrayType (1));
890 ElementType = Indirect (SubScript.Type);
892 Error ("Cannot subscript");
893 /* To avoid compiler errors, fake both the array and the subscript, so
894 * we can just proceed.
896 ED_MakeConstAbs (Expr, 0, GetCharArrayType (1));
897 ED_MakeConstAbsInt (&SubScript, 0);
898 ElementType = Indirect (Expr->Type);
901 /* Check if the subscript is constant absolute value */
902 if (ED_IsConstAbs (&SubScript)) {
904 /* The array subscript is a numeric constant. If we had pushed the
905 * array base address onto the stack before, we can remove this value,
906 * since we can generate expression+offset.
908 if (!ConstBaseAddr) {
911 /* Get an array pointer into the primary */
912 LoadExpr (CF_NONE, Expr);
915 if (IsClassPtr (Expr->Type)) {
917 /* Lhs is pointer/array. Scale the subscript value according to
920 SubScript.IVal *= CheckedSizeOf (ElementType);
922 /* Remove the address load code */
925 /* In case of an array, we can adjust the offset of the expression
926 * already in Expr. If the base address was a constant, we can even
927 * remove the code that loaded the address into the primary.
929 if (IsTypeArray (Expr->Type)) {
931 /* Adjust the offset */
932 Expr->IVal += SubScript.IVal;
936 /* It's a pointer, so we do have to load it into the primary
937 * first (if it's not already there).
940 LoadExpr (CF_NONE, Expr);
941 ED_MakeRValExpr (Expr);
945 Expr->IVal = SubScript.IVal;
950 /* Scale the rhs value according to the element type */
951 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
953 /* Add the subscript. Since arrays are indexed by integers,
954 * we will ignore the true type of the subscript here and
955 * use always an int. #### Use offset but beware of LoadExpr!
957 g_inc (CF_INT | CF_CONST, SubScript.IVal);
963 /* Array subscript is not constant. Load it into the primary */
965 LoadExpr (CF_NONE, &SubScript);
968 if (IsClassPtr (Expr->Type)) {
970 /* Indexing is based on unsigneds, so we will just use the integer
971 * portion of the index (which is in (e)ax, so there's no further
974 g_scale (CF_INT, CheckedSizeOf (ElementType));
978 /* Get the int value on top. If we come here, we're sure, both
979 * values are 16 bit (the first one was truncated if necessary
980 * and the second one is a pointer). Note: If ConstBaseAddr is
981 * true, we don't have a value on stack, so to "swap" both, just
982 * push the subscript.
986 LoadExpr (CF_NONE, Expr);
993 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
997 /* The offset is now in the primary register. It we didn't have a
998 * constant base address for the lhs, the lhs address is already
999 * on stack, and we must add the offset. If the base address was
1000 * constant, we call special functions to add the address to the
1003 if (!ConstBaseAddr) {
1005 /* The array base address is on stack and the subscript is in the
1006 * primary. Add both.
1012 /* The subscript is in the primary, and the array base address is
1013 * in Expr. If the subscript has itself a constant address, it is
1014 * often a better idea to reverse again the order of the
1015 * evaluation. This will generate better code if the subscript is
1016 * a byte sized variable. But beware: This is only possible if the
1017 * subscript was not scaled, that is, if this was a byte array
1020 if ((ED_IsLocConst (&SubScript) || ED_IsLocStack (&SubScript)) &&
1021 CheckedSizeOf (ElementType) == SIZEOF_CHAR) {
1025 /* Reverse the order of evaluation */
1026 if (CheckedSizeOf (SubScript.Type) == SIZEOF_CHAR) {
1031 RemoveCode (&Mark2);
1033 /* Get a pointer to the array into the primary. */
1034 LoadExpr (CF_NONE, Expr);
1036 /* Add the variable */
1037 if (ED_IsLocStack (&SubScript)) {
1038 g_addlocal (Flags, SubScript.IVal);
1040 Flags |= GlobalModeFlags (SubScript.Flags);
1041 g_addstatic (Flags, SubScript.Name, SubScript.IVal);
1044 if (ED_IsLocAbs (Expr)) {
1045 /* Constant numeric address. Just add it */
1046 g_inc (CF_INT, Expr->IVal);
1047 } else if (ED_IsLocStack (Expr)) {
1048 /* Base address is a local variable address */
1049 if (IsTypeArray (Expr->Type)) {
1050 g_addaddr_local (CF_INT, Expr->IVal);
1052 g_addlocal (CF_PTR, Expr->IVal);
1055 /* Base address is a static variable address */
1056 unsigned Flags = CF_INT | GlobalModeFlags (Expr->Flags);
1057 if (IsTypeArray (Expr->Type)) {
1058 g_addaddr_static (Flags, Expr->Name, Expr->IVal);
1060 g_addstatic (Flags, Expr->Name, Expr->IVal);
1068 /* The result is an expression in the primary */
1069 ED_MakeRValExpr (Expr);
1073 /* Result is of element type */
1074 Expr->Type = ElementType;
1076 /* An array element is actually a variable. So the rules for variables
1077 * with respect to the reference type apply: If it's an array, it is
1078 * a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1079 * but an array cannot contain functions).
1081 if (IsTypeArray (Expr->Type)) {
1087 /* Consume the closing bracket */
1093 static void StructRef (ExprDesc* Expr)
1094 /* Process struct field after . or ->. */
1099 /* Skip the token and check for an identifier */
1101 if (CurTok.Tok != TOK_IDENT) {
1102 Error ("Identifier expected");
1103 Expr->Type = type_int;
1107 /* Get the symbol table entry and check for a struct field */
1108 strcpy (Ident, CurTok.Ident);
1110 Field = FindStructField (Expr->Type, Ident);
1112 Error ("Struct/union has no field named `%s'", Ident);
1113 Expr->Type = type_int;
1117 /* If we have a struct pointer that is an lvalue and not already in the
1118 * primary, load it now.
1120 if (ED_IsLVal (Expr) && IsTypePtr (Expr->Type)) {
1122 /* Load into the primary */
1123 LoadExpr (CF_NONE, Expr);
1125 /* Make it an lvalue expression */
1126 ED_MakeLValExpr (Expr);
1129 /* Set the struct field offset */
1130 Expr->IVal += Field->V.Offs;
1132 /* The type is now the type of the field */
1133 Expr->Type = Field->Type;
1135 /* An struct member is actually a variable. So the rules for variables
1136 * with respect to the reference type apply: If it's an array, it is
1137 * a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1138 * but a struct field cannot be a function).
1140 if (IsTypeArray (Expr->Type)) {
1149 static void hie11 (ExprDesc *Expr)
1150 /* Handle compound types (structs and arrays) */
1152 /* Evaluate the lhs */
1155 /* Check for a rhs */
1156 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN ||
1157 CurTok.Tok == TOK_DOT || CurTok.Tok == TOK_PTR_REF) {
1159 switch (CurTok.Tok) {
1162 /* Array reference */
1167 /* Function call. */
1168 if (!IsTypeFunc (Expr->Type) && !IsTypeFuncPtr (Expr->Type)) {
1169 /* Not a function */
1170 Error ("Illegal function call");
1171 /* Force the type to be a implicitly defined function, one
1172 * returning an int and taking any number of arguments.
1173 * Since we don't have a name, place it at absolute address
1176 ED_MakeConstAbs (Expr, 0, GetImplicitFuncType ());
1178 /* Call the function */
1179 FunctionCall (Expr);
1183 if (!IsClassStruct (Expr->Type)) {
1184 Error ("Struct expected");
1190 /* If we have an array, convert it to pointer to first element */
1191 if (IsTypeArray (Expr->Type)) {
1192 Expr->Type = ArrayToPtr (Expr->Type);
1194 if (!IsClassPtr (Expr->Type) || !IsClassStruct (Indirect (Expr->Type))) {
1195 Error ("Struct pointer expected");
1201 Internal ("Invalid token in hie11: %d", CurTok.Tok);
1209 void Store (ExprDesc* Expr, const type* StoreType)
1210 /* Store the primary register into the location denoted by Expr. If StoreType
1211 * is given, use this type when storing instead of Expr->Type. If StoreType
1212 * is NULL, use Expr->Type instead.
1217 /* If StoreType was not given, use Expr->Type instead */
1218 if (StoreType == 0) {
1219 StoreType = Expr->Type;
1222 /* Prepare the code generator flags */
1223 Flags = TypeOf (StoreType);
1225 /* Do the store depending on the location */
1226 switch (ED_GetLoc (Expr)) {
1229 /* Absolute: numeric address or const */
1230 g_putstatic (Flags | CF_ABSOLUTE, Expr->IVal, 0);
1234 /* Global variable */
1235 g_putstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->IVal);
1240 /* Static variable or literal in the literal pool */
1241 g_putstatic (Flags | CF_STATIC, Expr->Name, Expr->IVal);
1244 case E_LOC_REGISTER:
1245 /* Register variable */
1246 g_putstatic (Flags | CF_REGVAR, Expr->Name, Expr->IVal);
1250 /* Value on the stack */
1251 g_putlocal (Flags, Expr->IVal, 0);
1255 /* The primary register (value is already there) */
1256 /* ### Do we need a test here if the flag is set? */
1260 /* An expression in the primary register */
1261 g_putind (Flags, Expr->IVal);
1265 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
1268 /* Assume that each one of the stores will invalidate CC */
1269 ED_MarkAsUntested (Expr);
1274 static void PreInc (ExprDesc* Expr)
1275 /* Handle the preincrement operators */
1280 /* Skip the operator token */
1283 /* Evaluate the expression and check that it is an lvalue */
1285 if (!ED_IsLVal (Expr)) {
1286 Error ("Invalid lvalue");
1290 /* Get the data type */
1291 Flags = TypeOf (Expr->Type) | CF_FORCECHAR | CF_CONST;
1293 /* Get the increment value in bytes */
1294 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1296 /* Check the location of the data */
1297 switch (ED_GetLoc (Expr)) {
1300 /* Absolute: numeric address or const */
1301 g_addeqstatic (Flags | CF_ABSOLUTE, Expr->IVal, 0, Val);
1305 /* Global variable */
1306 g_addeqstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->IVal, Val);
1311 /* Static variable or literal in the literal pool */
1312 g_addeqstatic (Flags | CF_STATIC, Expr->Name, Expr->IVal, Val);
1315 case E_LOC_REGISTER:
1316 /* Register variable */
1317 g_addeqstatic (Flags | CF_REGVAR, Expr->Name, Expr->IVal, Val);
1321 /* Value on the stack */
1322 g_addeqlocal (Flags, Expr->IVal, Val);
1326 /* The primary register */
1331 /* An expression in the primary register */
1332 g_addeqind (Flags, Expr->IVal, Val);
1336 Internal ("Invalid location in PreInc(): 0x%04X", ED_GetLoc (Expr));
1339 /* Result is an expression, no reference */
1340 ED_MakeRValExpr (Expr);
1345 static void PreDec (ExprDesc* Expr)
1346 /* Handle the predecrement operators */
1351 /* Skip the operator token */
1354 /* Evaluate the expression and check that it is an lvalue */
1356 if (!ED_IsLVal (Expr)) {
1357 Error ("Invalid lvalue");
1361 /* Get the data type */
1362 Flags = TypeOf (Expr->Type) | CF_FORCECHAR | CF_CONST;
1364 /* Get the increment value in bytes */
1365 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1367 /* Check the location of the data */
1368 switch (ED_GetLoc (Expr)) {
1371 /* Absolute: numeric address or const */
1372 g_subeqstatic (Flags | CF_ABSOLUTE, Expr->IVal, 0, Val);
1376 /* Global variable */
1377 g_subeqstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->IVal, Val);
1382 /* Static variable or literal in the literal pool */
1383 g_subeqstatic (Flags | CF_STATIC, Expr->Name, Expr->IVal, Val);
1386 case E_LOC_REGISTER:
1387 /* Register variable */
1388 g_subeqstatic (Flags | CF_REGVAR, Expr->Name, Expr->IVal, Val);
1392 /* Value on the stack */
1393 g_subeqlocal (Flags, Expr->IVal, Val);
1397 /* The primary register */
1402 /* An expression in the primary register */
1403 g_subeqind (Flags, Expr->IVal, Val);
1407 Internal ("Invalid location in PreDec(): 0x%04X", ED_GetLoc (Expr));
1410 /* Result is an expression, no reference */
1411 ED_MakeRValExpr (Expr);
1416 static void PostIncDec (ExprDesc* Expr, void (*inc) (unsigned, unsigned long))
1417 /* Handle i-- and i++ */
1423 /* The expression to increment must be an lvalue */
1424 if (!ED_IsLVal (Expr)) {
1425 Error ("Invalid lvalue");
1429 /* Get the data type */
1430 Flags = TypeOf (Expr->Type);
1432 /* Push the address if needed */
1435 /* Fetch the value and save it (since it's the result of the expression) */
1436 LoadExpr (CF_NONE, Expr);
1437 g_save (Flags | CF_FORCECHAR);
1439 /* If we have a pointer expression, increment by the size of the type */
1440 if (IsTypePtr (Expr->Type)) {
1441 inc (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1443 inc (Flags | CF_CONST | CF_FORCECHAR, 1);
1446 /* Store the result back */
1449 /* Restore the original value in the primary register */
1450 g_restore (Flags | CF_FORCECHAR);
1452 /* The result is always an expression, no reference */
1453 ED_MakeRValExpr (Expr);
1458 static void UnaryOp (ExprDesc* Expr)
1459 /* Handle unary -/+ and ~ */
1463 /* Remember the operator token and skip it */
1464 token_t Tok = CurTok.Tok;
1467 /* Get the expression */
1470 /* We can only handle integer types */
1471 if (!IsClassInt (Expr->Type)) {
1472 Error ("Argument must have integer type");
1473 ED_MakeConstAbsInt (Expr, 1);
1476 /* Check for a constant expression */
1477 if (ED_IsConstAbs (Expr)) {
1478 /* Value is constant */
1480 case TOK_MINUS: Expr->IVal = -Expr->IVal; break;
1481 case TOK_PLUS: break;
1482 case TOK_COMP: Expr->IVal = ~Expr->IVal; break;
1483 default: Internal ("Unexpected token: %d", Tok);
1486 /* Value is not constant */
1487 LoadExpr (CF_NONE, Expr);
1489 /* Get the type of the expression */
1490 Flags = TypeOf (Expr->Type);
1492 /* Handle the operation */
1494 case TOK_MINUS: g_neg (Flags); break;
1495 case TOK_PLUS: break;
1496 case TOK_COMP: g_com (Flags); break;
1497 default: Internal ("Unexpected token: %d", Tok);
1500 /* The result is a rvalue in the primary */
1501 ED_MakeRValExpr (Expr);
1507 void hie10 (ExprDesc* Expr)
1508 /* Handle ++, --, !, unary - etc. */
1512 switch (CurTok.Tok) {
1530 if (evalexpr (CF_NONE, hie10, Expr) == 0) {
1531 /* Constant expression */
1532 Expr->IVal = !Expr->IVal;
1534 g_bneg (TypeOf (Expr->Type));
1535 ED_MakeRValExpr (Expr);
1536 ED_TestDone (Expr); /* bneg will set cc */
1542 ExprWithCheck (hie10, Expr);
1543 if (ED_IsLVal (Expr) || !(ED_IsLocConst (Expr) || ED_IsLocStack (Expr))) {
1544 /* Not a const, load it into the primary and make it a
1547 LoadExpr (CF_NONE, Expr);
1548 ED_MakeRValExpr (Expr);
1550 /* If the expression is already a pointer to function, the
1551 * additional dereferencing operator must be ignored.
1553 if (IsTypeFuncPtr (Expr->Type)) {
1554 /* Expression not storable */
1557 if (IsClassPtr (Expr->Type)) {
1558 Expr->Type = Indirect (Expr->Type);
1560 Error ("Illegal indirection");
1568 ExprWithCheck (hie10, Expr);
1569 /* The & operator may be applied to any lvalue, and it may be
1570 * applied to functions, even if they're no lvalues.
1572 if (ED_IsRVal (Expr) && !IsTypeFunc (Expr->Type)) {
1573 /* Allow the & operator with an array */
1574 if (!IsTypeArray (Expr->Type)) {
1575 Error ("Illegal address");
1578 Expr->Type = PointerTo (Expr->Type);
1585 if (TypeSpecAhead ()) {
1586 type Type[MAXTYPELEN];
1588 Size = CheckedSizeOf (ParseType (Type));
1591 /* Remember the output queue pointer */
1595 Size = CheckedSizeOf (Expr->Type);
1596 /* Remove any generated code */
1599 ED_MakeConstAbs (Expr, Size, type_size_t);
1600 ED_MarkAsUntested (Expr);
1604 if (TypeSpecAhead ()) {
1614 /* Handle post increment */
1615 if (CurTok.Tok == TOK_INC) {
1616 PostIncDec (Expr, g_inc);
1617 } else if (CurTok.Tok == TOK_DEC) {
1618 PostIncDec (Expr, g_dec);
1628 static void hie_internal (const GenDesc* Ops, /* List of generators */
1630 void (*hienext) (ExprDesc*),
1632 /* Helper function */
1638 token_t Tok; /* The operator token */
1639 unsigned ltype, type;
1640 int rconst; /* Operand is a constant */
1646 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1648 /* Tell the caller that we handled it's ops */
1651 /* All operators that call this function expect an int on the lhs */
1652 if (!IsClassInt (Expr->Type)) {
1653 Error ("Integer expression expected");
1656 /* Remember the operator token, then skip it */
1660 /* Get the lhs on stack */
1661 GetCodePos (&Mark1);
1662 ltype = TypeOf (Expr->Type);
1663 if (ED_IsConstAbs (Expr)) {
1664 /* Constant value */
1665 GetCodePos (&Mark2);
1666 g_push (ltype | CF_CONST, Expr->IVal);
1668 /* Value not constant */
1669 LoadExpr (CF_NONE, Expr);
1670 GetCodePos (&Mark2);
1674 /* Get the right hand side */
1675 rconst = (evalexpr (CF_NONE, hienext, &Expr2) == 0);
1677 /* Check the type of the rhs */
1678 if (!IsClassInt (Expr2.Type)) {
1679 Error ("Integer expression expected");
1682 /* Check for const operands */
1683 if (ED_IsConstAbs (Expr) && rconst) {
1685 /* Both operands are constant, remove the generated code */
1686 RemoveCode (&Mark1);
1688 /* Evaluate the result */
1689 Expr->IVal = kcalc (Tok, Expr->IVal, Expr2.IVal);
1691 /* Get the type of the result */
1692 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1696 /* If the right hand side is constant, and the generator function
1697 * expects the lhs in the primary, remove the push of the primary
1700 unsigned rtype = TypeOf (Expr2.Type);
1703 /* Second value is constant - check for div */
1706 if (Tok == TOK_DIV && Expr2.IVal == 0) {
1707 Error ("Division by zero");
1708 } else if (Tok == TOK_MOD && Expr2.IVal == 0) {
1709 Error ("Modulo operation with zero");
1711 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1712 RemoveCode (&Mark2);
1713 ltype |= CF_REG; /* Value is in register */
1717 /* Determine the type of the operation result. */
1718 type |= g_typeadjust (ltype, rtype);
1719 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1722 Gen->Func (type, Expr2.IVal);
1724 /* We have a rvalue in the primary now */
1725 ED_MakeRValExpr (Expr);
1732 static void hie_compare (const GenDesc* Ops, /* List of generators */
1734 void (*hienext) (ExprDesc*))
1735 /* Helper function for the compare operators */
1741 token_t tok; /* The operator token */
1743 int rconst; /* Operand is a constant */
1748 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1750 /* Remember the operator token, then skip it */
1754 /* Get the lhs on stack */
1755 GetCodePos (&Mark1);
1756 ltype = TypeOf (Expr->Type);
1757 if (ED_IsConstAbs (Expr)) {
1758 /* Constant value */
1759 GetCodePos (&Mark2);
1760 g_push (ltype | CF_CONST, Expr->IVal);
1762 /* Value not constant */
1763 LoadExpr (CF_NONE, Expr);
1764 GetCodePos (&Mark2);
1768 /* Get the right hand side */
1769 rconst = (evalexpr (CF_NONE, hienext, &Expr2) == 0);
1771 /* Make sure, the types are compatible */
1772 if (IsClassInt (Expr->Type)) {
1773 if (!IsClassInt (Expr2.Type) && !(IsClassPtr(Expr2.Type) && ED_IsNullPtr(Expr))) {
1774 Error ("Incompatible types");
1776 } else if (IsClassPtr (Expr->Type)) {
1777 if (IsClassPtr (Expr2.Type)) {
1778 /* Both pointers are allowed in comparison if they point to
1779 * the same type, or if one of them is a void pointer.
1781 type* left = Indirect (Expr->Type);
1782 type* right = Indirect (Expr2.Type);
1783 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1784 /* Incomatible pointers */
1785 Error ("Incompatible types");
1787 } else if (!ED_IsNullPtr (&Expr2)) {
1788 Error ("Incompatible types");
1792 /* Check for const operands */
1793 if (ED_IsConstAbs (Expr) && rconst) {
1795 /* Both operands are constant, remove the generated code */
1796 RemoveCode (&Mark1);
1798 /* Evaluate the result */
1799 Expr->IVal = kcalc (tok, Expr->IVal, Expr2.IVal);
1803 /* If the right hand side is constant, and the generator function
1804 * expects the lhs in the primary, remove the push of the primary
1810 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1811 RemoveCode (&Mark2);
1812 ltype |= CF_REG; /* Value is in register */
1816 /* Determine the type of the operation result. If the left
1817 * operand is of type char and the right is a constant, or
1818 * if both operands are of type char, we will encode the
1819 * operation as char operation. Otherwise the default
1820 * promotions are used.
1822 if (IsTypeChar (Expr->Type) && (IsTypeChar (Expr2.Type) || rconst)) {
1824 if (IsSignUnsigned (Expr->Type) || IsSignUnsigned (Expr2.Type)) {
1825 flags |= CF_UNSIGNED;
1828 flags |= CF_FORCECHAR;
1831 unsigned rtype = TypeOf (Expr2.Type) | (flags & CF_CONST);
1832 flags |= g_typeadjust (ltype, rtype);
1836 Gen->Func (flags, Expr2.IVal);
1838 /* The result is an rvalue in the primary */
1839 ED_MakeRValExpr (Expr);
1842 /* Result type is always int */
1843 Expr->Type = type_int;
1845 /* Condition codes are set */
1852 static void hie9 (ExprDesc *Expr)
1853 /* Process * and / operators. */
1855 static const GenDesc hie9_ops[] = {
1856 { TOK_STAR, GEN_NOPUSH, g_mul },
1857 { TOK_DIV, GEN_NOPUSH, g_div },
1858 { TOK_MOD, GEN_NOPUSH, g_mod },
1859 { TOK_INVALID, 0, 0 }
1863 hie_internal (hie9_ops, Expr, hie10, &UsedGen);
1868 static void parseadd (ExprDesc* Expr)
1869 /* Parse an expression with the binary plus operator. Expr contains the
1870 * unprocessed left hand side of the expression and will contain the
1871 * result of the expression on return.
1875 unsigned flags; /* Operation flags */
1876 CodeMark Mark; /* Remember code position */
1877 type* lhst; /* Type of left hand side */
1878 type* rhst; /* Type of right hand side */
1881 /* Skip the PLUS token */
1884 /* Get the left hand side type, initialize operation flags */
1888 /* Check for constness on both sides */
1889 if (ED_IsConst (Expr)) {
1891 /* The left hand side is a constant of some sort. Good. Get rhs */
1893 if (ED_IsConstAbs (&Expr2)) {
1895 /* Right hand side is a constant numeric value. Get the rhs type */
1898 /* Both expressions are constants. Check for pointer arithmetic */
1899 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1900 /* Left is pointer, right is int, must scale rhs */
1901 Expr->IVal += Expr2.IVal * CheckedPSizeOf (lhst);
1902 /* Result type is a pointer */
1903 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1904 /* Left is int, right is pointer, must scale lhs */
1905 Expr->IVal = Expr->IVal * CheckedPSizeOf (rhst) + Expr2.IVal;
1906 /* Result type is a pointer */
1907 Expr->Type = Expr2.Type;
1908 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1909 /* Integer addition */
1910 Expr->IVal += Expr2.IVal;
1911 typeadjust (Expr, &Expr2, 1);
1914 Error ("Invalid operands for binary operator `+'");
1919 /* lhs is a constant and rhs is not constant. Load rhs into
1922 LoadExpr (CF_NONE, &Expr2);
1924 /* Beware: The check above (for lhs) lets not only pass numeric
1925 * constants, but also constant addresses (labels), maybe even
1926 * with an offset. We have to check for that here.
1929 /* First, get the rhs type. */
1933 if (ED_IsLocAbs (Expr)) {
1934 /* A numerical constant */
1937 /* Constant address label */
1938 flags |= GlobalModeFlags (Expr->Flags) | CF_CONSTADDR;
1941 /* Check for pointer arithmetic */
1942 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1943 /* Left is pointer, right is int, must scale rhs */
1944 g_scale (CF_INT, CheckedPSizeOf (lhst));
1945 /* Operate on pointers, result type is a pointer */
1947 /* Generate the code for the add */
1948 if (ED_GetLoc (Expr) == E_LOC_ABS) {
1949 /* Numeric constant */
1950 g_inc (flags, Expr->IVal);
1952 /* Constant address */
1953 g_addaddr_static (flags, Expr->Name, Expr->IVal);
1955 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1957 /* Left is int, right is pointer, must scale lhs. */
1958 unsigned ScaleFactor = CheckedPSizeOf (rhst);
1960 /* Operate on pointers, result type is a pointer */
1962 Expr->Type = Expr2.Type;
1964 /* Since we do already have rhs in the primary, if lhs is
1965 * not a numeric constant, and the scale factor is not one
1966 * (no scaling), we must take the long way over the stack.
1968 if (ED_IsLocAbs (Expr)) {
1969 /* Numeric constant, scale lhs */
1970 Expr->IVal *= ScaleFactor;
1971 /* Generate the code for the add */
1972 g_inc (flags, Expr->IVal);
1973 } else if (ScaleFactor == 1) {
1974 /* Constant address but no need to scale */
1975 g_addaddr_static (flags, Expr->Name, Expr->IVal);
1977 /* Constant address that must be scaled */
1978 g_push (TypeOf (Expr2.Type), 0); /* rhs --> stack */
1979 g_getimmed (flags, Expr->Name, Expr->IVal);
1980 g_scale (CF_PTR, ScaleFactor);
1983 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1984 /* Integer addition */
1985 flags |= typeadjust (Expr, &Expr2, 1);
1986 /* Generate the code for the add */
1987 if (ED_IsLocAbs (Expr)) {
1988 /* Numeric constant */
1989 g_inc (flags, Expr->IVal);
1991 /* Constant address */
1992 g_addaddr_static (flags, Expr->Name, Expr->IVal);
1996 Error ("Invalid operands for binary operator `+'");
1999 /* Result is a rvalue in primary register */
2000 ED_MakeRValExpr (Expr);
2005 /* Left hand side is not constant. Get the value onto the stack. */
2006 LoadExpr (CF_NONE, Expr); /* --> primary register */
2008 g_push (TypeOf (Expr->Type), 0); /* --> stack */
2010 /* Evaluate the rhs */
2011 if (evalexpr (CF_NONE, hie9, &Expr2) == 0) {
2013 /* Right hand side is a constant. Get the rhs type */
2016 /* Remove pushed value from stack */
2019 /* Check for pointer arithmetic */
2020 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2021 /* Left is pointer, right is int, must scale rhs */
2022 Expr2.IVal *= CheckedPSizeOf (lhst);
2023 /* Operate on pointers, result type is a pointer */
2025 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2026 /* Left is int, right is pointer, must scale lhs (ptr only) */
2027 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2028 /* Operate on pointers, result type is a pointer */
2030 Expr->Type = Expr2.Type;
2031 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2032 /* Integer addition */
2033 flags = typeadjust (Expr, &Expr2, 1);
2036 Error ("Invalid operands for binary operator `+'");
2039 /* Generate code for the add */
2040 g_inc (flags | CF_CONST, Expr2.IVal);
2044 /* lhs and rhs are not constant. Get the rhs type. */
2047 /* Check for pointer arithmetic */
2048 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2049 /* Left is pointer, right is int, must scale rhs */
2050 g_scale (CF_INT, CheckedPSizeOf (lhst));
2051 /* Operate on pointers, result type is a pointer */
2053 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2054 /* Left is int, right is pointer, must scale lhs */
2055 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2056 g_swap (CF_INT); /* Swap TOS and primary */
2057 g_scale (CF_INT, CheckedPSizeOf (rhst));
2058 /* Operate on pointers, result type is a pointer */
2060 Expr->Type = Expr2.Type;
2061 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2062 /* Integer addition. Note: Result is never constant.
2063 * Problem here is that typeadjust does not know if the
2064 * variable is an rvalue or lvalue, so if both operands
2065 * are dereferenced constant numeric addresses, typeadjust
2066 * thinks the operation works on constants. Removing
2067 * CF_CONST here means handling the symptoms, however, the
2068 * whole parser is such a mess that I fear to break anything
2069 * when trying to apply another solution.
2071 flags = typeadjust (Expr, &Expr2, 0) & ~CF_CONST;
2074 Error ("Invalid operands for binary operator `+'");
2077 /* Generate code for the add */
2082 /* Result is a rvalue in primary register */
2083 ED_MakeRValExpr (Expr);
2086 /* Condition codes not set */
2087 ED_MarkAsUntested (Expr);
2093 static void parsesub (ExprDesc* Expr)
2094 /* Parse an expression with the binary minus operator. Expr contains the
2095 * unprocessed left hand side of the expression and will contain the
2096 * result of the expression on return.
2100 unsigned flags; /* Operation flags */
2101 type* lhst; /* Type of left hand side */
2102 type* rhst; /* Type of right hand side */
2103 CodeMark Mark1; /* Save position of output queue */
2104 CodeMark Mark2; /* Another position in the queue */
2105 int rscale; /* Scale factor for the result */
2108 /* Skip the MINUS token */
2111 /* Get the left hand side type, initialize operation flags */
2114 rscale = 1; /* Scale by 1, that is, don't scale */
2116 /* Remember the output queue position, then bring the value onto the stack */
2117 GetCodePos (&Mark1);
2118 LoadExpr (CF_NONE, Expr); /* --> primary register */
2119 GetCodePos (&Mark2);
2120 g_push (TypeOf (lhst), 0); /* --> stack */
2122 /* Parse the right hand side */
2123 if (evalexpr (CF_NONE, hie9, &Expr2) == 0) {
2125 /* The right hand side is constant. Get the rhs type. */
2128 /* Check left hand side */
2129 if (ED_IsConstAbs (Expr)) {
2131 /* Both sides are constant, remove generated code */
2132 RemoveCode (&Mark1);
2134 /* Check for pointer arithmetic */
2135 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2136 /* Left is pointer, right is int, must scale rhs */
2137 Expr->IVal -= Expr2.IVal * CheckedPSizeOf (lhst);
2138 /* Operate on pointers, result type is a pointer */
2139 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2140 /* Left is pointer, right is pointer, must scale result */
2141 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2142 Error ("Incompatible pointer types");
2144 Expr->IVal = (Expr->IVal - Expr2.IVal) /
2145 CheckedPSizeOf (lhst);
2147 /* Operate on pointers, result type is an integer */
2148 Expr->Type = type_int;
2149 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2150 /* Integer subtraction */
2151 typeadjust (Expr, &Expr2, 1);
2152 Expr->IVal -= Expr2.IVal;
2155 Error ("Invalid operands for binary operator `-'");
2158 /* Result is constant, condition codes not set */
2159 ED_MarkAsUntested (Expr);
2163 /* Left hand side is not constant, right hand side is.
2164 * Remove pushed value from stack.
2166 RemoveCode (&Mark2);
2168 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2169 /* Left is pointer, right is int, must scale rhs */
2170 Expr2.IVal *= CheckedPSizeOf (lhst);
2171 /* Operate on pointers, result type is a pointer */
2173 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2174 /* Left is pointer, right is pointer, must scale result */
2175 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2176 Error ("Incompatible pointer types");
2178 rscale = CheckedPSizeOf (lhst);
2180 /* Operate on pointers, result type is an integer */
2182 Expr->Type = type_int;
2183 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2184 /* Integer subtraction */
2185 flags = typeadjust (Expr, &Expr2, 1);
2188 Error ("Invalid operands for binary operator `-'");
2191 /* Do the subtraction */
2192 g_dec (flags | CF_CONST, Expr2.IVal);
2194 /* If this was a pointer subtraction, we must scale the result */
2196 g_scale (flags, -rscale);
2199 /* Result is a rvalue in the primary register */
2200 ED_MakeRValExpr (Expr);
2201 ED_MarkAsUntested (Expr);
2207 /* Right hand side is not constant. Get the rhs type. */
2210 /* Check for pointer arithmetic */
2211 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2212 /* Left is pointer, right is int, must scale rhs */
2213 g_scale (CF_INT, CheckedPSizeOf (lhst));
2214 /* Operate on pointers, result type is a pointer */
2216 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2217 /* Left is pointer, right is pointer, must scale result */
2218 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2219 Error ("Incompatible pointer types");
2221 rscale = CheckedPSizeOf (lhst);
2223 /* Operate on pointers, result type is an integer */
2225 Expr->Type = type_int;
2226 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2227 /* Integer subtraction. If the left hand side descriptor says that
2228 * the lhs is const, we have to remove this mark, since this is no
2229 * longer true, lhs is on stack instead.
2231 if (ED_IsLocAbs (Expr)) {
2232 ED_MakeRValExpr (Expr);
2234 /* Adjust operand types */
2235 flags = typeadjust (Expr, &Expr2, 0);
2238 Error ("Invalid operands for binary operator `-'");
2241 /* Generate code for the sub (the & is a hack here) */
2242 g_sub (flags & ~CF_CONST, 0);
2244 /* If this was a pointer subtraction, we must scale the result */
2246 g_scale (flags, -rscale);
2249 /* Result is a rvalue in the primary register */
2250 ED_MakeRValExpr (Expr);
2251 ED_MarkAsUntested (Expr);
2257 void hie8 (ExprDesc* Expr)
2258 /* Process + and - binary operators. */
2261 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2262 if (CurTok.Tok == TOK_PLUS) {
2272 static void hie6 (ExprDesc* Expr)
2273 /* Handle greater-than type comparators */
2275 static const GenDesc hie6_ops [] = {
2276 { TOK_LT, GEN_NOPUSH, g_lt },
2277 { TOK_LE, GEN_NOPUSH, g_le },
2278 { TOK_GE, GEN_NOPUSH, g_ge },
2279 { TOK_GT, GEN_NOPUSH, g_gt },
2280 { TOK_INVALID, 0, 0 }
2282 hie_compare (hie6_ops, Expr, ShiftExpr);
2287 static void hie5 (ExprDesc* Expr)
2288 /* Handle == and != */
2290 static const GenDesc hie5_ops[] = {
2291 { TOK_EQ, GEN_NOPUSH, g_eq },
2292 { TOK_NE, GEN_NOPUSH, g_ne },
2293 { TOK_INVALID, 0, 0 }
2295 hie_compare (hie5_ops, Expr, hie6);
2300 static void hie4 (ExprDesc* Expr)
2301 /* Handle & (bitwise and) */
2303 static const GenDesc hie4_ops[] = {
2304 { TOK_AND, GEN_NOPUSH, g_and },
2305 { TOK_INVALID, 0, 0 }
2309 hie_internal (hie4_ops, Expr, hie5, &UsedGen);
2314 static void hie3 (ExprDesc* Expr)
2315 /* Handle ^ (bitwise exclusive or) */
2317 static const GenDesc hie3_ops[] = {
2318 { TOK_XOR, GEN_NOPUSH, g_xor },
2319 { TOK_INVALID, 0, 0 }
2323 hie_internal (hie3_ops, Expr, hie4, &UsedGen);
2328 static void hie2 (ExprDesc* Expr)
2329 /* Handle | (bitwise or) */
2331 static const GenDesc hie2_ops[] = {
2332 { TOK_OR, GEN_NOPUSH, g_or },
2333 { TOK_INVALID, 0, 0 }
2337 hie_internal (hie2_ops, Expr, hie3, &UsedGen);
2342 static void hieAndPP (ExprDesc* Expr)
2343 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2344 * called recursively from the preprocessor.
2349 ConstAbsIntExpr (hie2, Expr);
2350 while (CurTok.Tok == TOK_BOOL_AND) {
2356 ConstAbsIntExpr (hie2, &Expr2);
2358 /* Combine the two */
2359 Expr->IVal = (Expr->IVal && Expr2.IVal);
2365 static void hieOrPP (ExprDesc *Expr)
2366 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2367 * called recursively from the preprocessor.
2372 ConstAbsIntExpr (hieAndPP, Expr);
2373 while (CurTok.Tok == TOK_BOOL_OR) {
2379 ConstAbsIntExpr (hieAndPP, &Expr2);
2381 /* Combine the two */
2382 Expr->IVal = (Expr->IVal || Expr2.IVal);
2388 static void hieAnd (ExprDesc* Expr, unsigned TrueLab, int* BoolOp)
2389 /* Process "exp && exp" */
2395 if (CurTok.Tok == TOK_BOOL_AND) {
2397 /* Tell our caller that we're evaluating a boolean */
2400 /* Get a label that we will use for false expressions */
2401 lab = GetLocalLabel ();
2403 /* If the expr hasn't set condition codes, set the force-test flag */
2404 if (!ED_IsTested (Expr)) {
2405 ED_MarkForTest (Expr);
2408 /* Load the value */
2409 LoadExpr (CF_FORCECHAR, Expr);
2411 /* Generate the jump */
2412 g_falsejump (CF_NONE, lab);
2414 /* Parse more boolean and's */
2415 while (CurTok.Tok == TOK_BOOL_AND) {
2422 if (!ED_IsTested (&Expr2)) {
2423 ED_MarkForTest (&Expr2);
2425 LoadExpr (CF_FORCECHAR, &Expr2);
2427 /* Do short circuit evaluation */
2428 if (CurTok.Tok == TOK_BOOL_AND) {
2429 g_falsejump (CF_NONE, lab);
2431 /* Last expression - will evaluate to true */
2432 g_truejump (CF_NONE, TrueLab);
2436 /* Define the false jump label here */
2437 g_defcodelabel (lab);
2439 /* The result is an rvalue in primary */
2440 ED_MakeRValExpr (Expr);
2441 ED_TestDone (Expr); /* Condition codes are set */
2447 static void hieOr (ExprDesc *Expr)
2448 /* Process "exp || exp". */
2451 int BoolOp = 0; /* Did we have a boolean op? */
2452 int AndOp; /* Did we have a && operation? */
2453 unsigned TrueLab; /* Jump to this label if true */
2457 TrueLab = GetLocalLabel ();
2459 /* Call the next level parser */
2460 hieAnd (Expr, TrueLab, &BoolOp);
2462 /* Any boolean or's? */
2463 if (CurTok.Tok == TOK_BOOL_OR) {
2465 /* If the expr hasn't set condition codes, set the force-test flag */
2466 if (!ED_IsTested (Expr)) {
2467 ED_MarkForTest (Expr);
2470 /* Get first expr */
2471 LoadExpr (CF_FORCECHAR, Expr);
2473 /* For each expression jump to TrueLab if true. Beware: If we
2474 * had && operators, the jump is already in place!
2477 g_truejump (CF_NONE, TrueLab);
2480 /* Remember that we had a boolean op */
2483 /* while there's more expr */
2484 while (CurTok.Tok == TOK_BOOL_OR) {
2491 hieAnd (&Expr2, TrueLab, &AndOp);
2492 if (!ED_IsTested (&Expr2)) {
2493 ED_MarkForTest (&Expr2);
2495 LoadExpr (CF_FORCECHAR, &Expr2);
2497 /* If there is more to come, add shortcut boolean eval. */
2498 g_truejump (CF_NONE, TrueLab);
2502 /* The result is an rvalue in primary */
2503 ED_MakeRValExpr (Expr);
2504 ED_TestDone (Expr); /* Condition codes are set */
2507 /* If we really had boolean ops, generate the end sequence */
2509 DoneLab = GetLocalLabel ();
2510 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2511 g_falsejump (CF_NONE, DoneLab);
2512 g_defcodelabel (TrueLab);
2513 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2514 g_defcodelabel (DoneLab);
2520 static void hieQuest (ExprDesc* Expr)
2521 /* Parse the ternary operator */
2525 ExprDesc Expr2; /* Expression 2 */
2526 ExprDesc Expr3; /* Expression 3 */
2527 int Expr2IsNULL; /* Expression 2 is a NULL pointer */
2528 int Expr3IsNULL; /* Expression 3 is a NULL pointer */
2529 type* ResultType; /* Type of result */
2532 /* Call the lower level eval routine */
2533 if (Preprocessing) {
2539 /* Check if it's a ternary expression */
2540 if (CurTok.Tok == TOK_QUEST) {
2542 if (!ED_IsTested (Expr)) {
2543 /* Condition codes not set, request a test */
2544 ED_MarkForTest (Expr);
2546 LoadExpr (CF_NONE, Expr);
2547 labf = GetLocalLabel ();
2548 g_falsejump (CF_NONE, labf);
2550 /* Parse second expression. Remember for later if it is a NULL pointer
2551 * expression, then load it into the primary.
2553 ExprWithCheck (hie1, &Expr2);
2554 Expr2IsNULL = ED_IsNullPtr (&Expr2);
2555 if (!IsTypeVoid (Expr2.Type)) {
2556 /* Load it into the primary */
2557 LoadExpr (CF_NONE, &Expr2);
2558 ED_MakeRValExpr (&Expr2);
2560 labt = GetLocalLabel ();
2564 /* Jump here if the first expression was false */
2565 g_defcodelabel (labf);
2567 /* Parse second expression. Remember for later if it is a NULL pointer
2568 * expression, then load it into the primary.
2570 ExprWithCheck (hie1, &Expr3);
2571 Expr3IsNULL = ED_IsNullPtr (&Expr3);
2572 if (!IsTypeVoid (Expr3.Type)) {
2573 /* Load it into the primary */
2574 LoadExpr (CF_NONE, &Expr3);
2575 ED_MakeRValExpr (&Expr3);
2578 /* Check if any conversions are needed, if so, do them.
2579 * Conversion rules for ?: expression are:
2580 * - if both expressions are int expressions, default promotion
2581 * rules for ints apply.
2582 * - if both expressions are pointers of the same type, the
2583 * result of the expression is of this type.
2584 * - if one of the expressions is a pointer and the other is
2585 * a zero constant, the resulting type is that of the pointer
2587 * - if both expressions are void expressions, the result is of
2589 * - all other cases are flagged by an error.
2591 if (IsClassInt (Expr2.Type) && IsClassInt (Expr3.Type)) {
2593 /* Get common type */
2594 ResultType = promoteint (Expr2.Type, Expr3.Type);
2596 /* Convert the third expression to this type if needed */
2597 TypeConversion (&Expr3, ResultType);
2599 /* Setup a new label so that the expr3 code will jump around
2600 * the type cast code for expr2.
2602 labf = GetLocalLabel (); /* Get new label */
2603 g_jump (labf); /* Jump around code */
2605 /* The jump for expr2 goes here */
2606 g_defcodelabel (labt);
2608 /* Create the typecast code for expr2 */
2609 TypeConversion (&Expr2, ResultType);
2611 /* Jump here around the typecase code. */
2612 g_defcodelabel (labf);
2613 labt = 0; /* Mark other label as invalid */
2615 } else if (IsClassPtr (Expr2.Type) && IsClassPtr (Expr3.Type)) {
2616 /* Must point to same type */
2617 if (TypeCmp (Indirect (Expr2.Type), Indirect (Expr3.Type)) < TC_EQUAL) {
2618 Error ("Incompatible pointer types");
2620 /* Result has the common type */
2621 ResultType = Expr2.Type;
2622 } else if (IsClassPtr (Expr2.Type) && Expr3IsNULL) {
2623 /* Result type is pointer, no cast needed */
2624 ResultType = Expr2.Type;
2625 } else if (Expr2IsNULL && IsClassPtr (Expr3.Type)) {
2626 /* Result type is pointer, no cast needed */
2627 ResultType = Expr3.Type;
2628 } else if (IsTypeVoid (Expr2.Type) && IsTypeVoid (Expr3.Type)) {
2629 /* Result type is void */
2630 ResultType = Expr3.Type;
2632 Error ("Incompatible types");
2633 ResultType = Expr2.Type; /* Doesn't matter here */
2636 /* If we don't have the label defined until now, do it */
2638 g_defcodelabel (labt);
2641 /* Setup the target expression */
2642 ED_MakeRValExpr (Expr);
2643 Expr->Type = ResultType;
2649 static void opeq (const GenDesc* Gen, ExprDesc* Expr)
2650 /* Process "op=" operators. */
2657 /* op= can only be used with lvalues */
2658 if (!ED_IsLVal (Expr)) {
2659 Error ("Invalid lvalue in assignment");
2663 /* There must be an integer or pointer on the left side */
2664 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
2665 Error ("Invalid left operand type");
2666 /* Continue. Wrong code will be generated, but the compiler won't
2667 * break, so this is the best error recovery.
2671 /* Skip the operator token */
2674 /* Determine the type of the lhs */
2675 flags = TypeOf (Expr->Type);
2676 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) && IsTypePtr (Expr->Type);
2678 /* Get the lhs address on stack (if needed) */
2681 /* Fetch the lhs into the primary register if needed */
2682 LoadExpr (CF_NONE, Expr);
2684 /* Bring the lhs on stack */
2688 /* Evaluate the rhs */
2689 if (evalexpr (CF_NONE, hie1, &Expr2) == 0) {
2690 /* The resulting value is a constant. If the generator has the NOPUSH
2691 * flag set, don't push the lhs.
2693 if (Gen->Flags & GEN_NOPUSH) {
2697 /* lhs is a pointer, scale rhs */
2698 Expr2.IVal *= CheckedSizeOf (Expr->Type+1);
2701 /* If the lhs is character sized, the operation may be later done
2704 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
2705 flags |= CF_FORCECHAR;
2708 /* Special handling for add and sub - some sort of a hack, but short code */
2709 if (Gen->Func == g_add) {
2710 g_inc (flags | CF_CONST, Expr2.IVal);
2711 } else if (Gen->Func == g_sub) {
2712 g_dec (flags | CF_CONST, Expr2.IVal);
2714 Gen->Func (flags | CF_CONST, Expr2.IVal);
2717 /* rhs is not constant and already in the primary register */
2719 /* lhs is a pointer, scale rhs */
2720 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Expr->Type+1));
2723 /* If the lhs is character sized, the operation may be later done
2726 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
2727 flags |= CF_FORCECHAR;
2730 /* Adjust the types of the operands if needed */
2731 Gen->Func (g_typeadjust (flags, TypeOf (Expr2.Type)), 0);
2734 ED_MakeRValExpr (Expr);
2739 static void addsubeq (const GenDesc* Gen, ExprDesc *Expr)
2740 /* Process the += and -= operators */
2748 /* We're currently only able to handle some adressing modes */
2749 if (ED_GetLoc (Expr) == E_LOC_EXPR || ED_GetLoc (Expr) == E_LOC_PRIMARY) {
2750 /* Use generic routine */
2755 /* We must have an lvalue */
2756 if (ED_IsRVal (Expr)) {
2757 Error ("Invalid lvalue in assignment");
2761 /* There must be an integer or pointer on the left side */
2762 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
2763 Error ("Invalid left operand type");
2764 /* Continue. Wrong code will be generated, but the compiler won't
2765 * break, so this is the best error recovery.
2769 /* Skip the operator */
2772 /* Check if we have a pointer expression and must scale rhs */
2773 MustScale = IsTypePtr (Expr->Type);
2775 /* Initialize the code generator flags */
2779 /* Evaluate the rhs */
2781 if (ED_IsConstAbs (&Expr2)) {
2782 /* The resulting value is a constant. Scale it. */
2784 Expr2.IVal *= CheckedSizeOf (Indirect (Expr->Type));
2789 /* Not constant, load into the primary */
2790 LoadExpr (CF_NONE, &Expr2);
2792 /* lhs is a pointer, scale rhs */
2793 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Indirect (Expr->Type)));
2797 /* Setup the code generator flags */
2798 lflags |= TypeOf (Expr->Type) | CF_FORCECHAR;
2799 rflags |= TypeOf (Expr2.Type);
2801 /* Convert the type of the lhs to that of the rhs */
2802 g_typecast (lflags, rflags);
2804 /* Output apropriate code depending on the location */
2805 switch (ED_GetLoc (Expr)) {
2808 /* Absolute: numeric address or const */
2809 lflags |= CF_ABSOLUTE;
2810 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2811 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2813 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2818 /* Global variable */
2819 lflags |= CF_EXTERNAL;
2820 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2821 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2823 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2829 /* Static variable or literal in the literal pool */
2830 lflags |= CF_STATIC;
2831 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2832 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2834 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2838 case E_LOC_REGISTER:
2839 /* Register variable */
2840 lflags |= CF_REGVAR;
2841 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2842 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2844 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2849 /* Value on the stack */
2850 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2851 g_addeqlocal (lflags, Expr->IVal, Expr2.IVal);
2853 g_subeqlocal (lflags, Expr->IVal, Expr2.IVal);
2858 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
2861 /* Expression is a rvalue in the primary now */
2862 ED_MakeRValExpr (Expr);
2867 void hie1 (ExprDesc* Expr)
2868 /* Parse first level of expression hierarchy. */
2871 switch (CurTok.Tok) {
2877 case TOK_PLUS_ASSIGN:
2878 addsubeq (&GenPASGN, Expr);
2881 case TOK_MINUS_ASSIGN:
2882 addsubeq (&GenSASGN, Expr);
2885 case TOK_MUL_ASSIGN:
2886 opeq (&GenMASGN, Expr);
2889 case TOK_DIV_ASSIGN:
2890 opeq (&GenDASGN, Expr);
2893 case TOK_MOD_ASSIGN:
2894 opeq (&GenMOASGN, Expr);
2897 case TOK_SHL_ASSIGN:
2898 opeq (&GenSLASGN, Expr);
2901 case TOK_SHR_ASSIGN:
2902 opeq (&GenSRASGN, Expr);
2905 case TOK_AND_ASSIGN:
2906 opeq (&GenAASGN, Expr);
2909 case TOK_XOR_ASSIGN:
2910 opeq (&GenXOASGN, Expr);
2914 opeq (&GenOASGN, Expr);
2924 void hie0 (ExprDesc *Expr)
2925 /* Parse comma operator. */
2928 while (CurTok.Tok == TOK_COMMA) {
2936 int evalexpr (unsigned Flags, void (*Func) (ExprDesc*), ExprDesc* Expr)
2937 /* Will evaluate an expression via the given function. If the result is a
2938 * constant, 0 is returned and the value is put in the Expr struct. If the
2939 * result is not constant, LoadExpr is called to bring the value into the
2940 * primary register and 1 is returned.
2944 ExprWithCheck (Func, Expr);
2946 /* Check for a constant expression */
2947 if (ED_IsConstAbs (Expr)) {
2948 /* Constant expression */
2951 /* Not constant, load into the primary */
2952 LoadExpr (Flags, Expr);
2959 void Expression0 (ExprDesc* Expr)
2960 /* Evaluate an expression via hie0 and put the result into the primary register */
2962 ExprWithCheck (hie0, Expr);
2963 LoadExpr (CF_NONE, Expr);
2968 void ConstExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
2969 /* Will evaluate an expression via the given function. If the result is not
2970 * a constant of some sort, a diagnostic will be printed, and the value is
2971 * replaced by a constant one to make sure there are no internal errors that
2972 * result from this input error.
2975 ExprWithCheck (Func, Expr);
2976 if (!ED_IsConst (Expr)) {
2977 Error ("Constant expression expected");
2978 /* To avoid any compiler errors, make the expression a valid const */
2979 ED_MakeConstAbsInt (Expr, 1);
2985 void BoolExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
2986 /* Will evaluate an expression via the given function. If the result is not
2987 * something that may be evaluated in a boolean context, a diagnostic will be
2988 * printed, and the value is replaced by a constant one to make sure there
2989 * are no internal errors that result from this input error.
2992 ExprWithCheck (Func, Expr);
2993 if (!ED_IsBool (Expr)) {
2994 Error ("Boolean expression expected");
2995 /* To avoid any compiler errors, make the expression a valid int */
2996 ED_MakeConstAbsInt (Expr, 1);
3002 void ConstAbsIntExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3003 /* Will evaluate an expression via the given function. If the result is not
3004 * a constant numeric integer value, a diagnostic will be printed, and the
3005 * value is replaced by a constant one to make sure there are no internal
3006 * errors that result from this input error.
3009 ExprWithCheck (Func, Expr);
3010 if (!ED_IsConstAbsInt (Expr)) {
3011 Error ("Constant integer expression expected");
3012 /* To avoid any compiler errors, make the expression a valid const */
3013 ED_MakeConstAbsInt (Expr, 1);