4 * Ullrich von Bassewitz, 21.06.1998
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 GenMUL = { TOK_STAR, GEN_NOPUSH, g_mul };
58 static GenDesc GenDIV = { TOK_DIV, GEN_NOPUSH, g_div };
59 static GenDesc GenMOD = { TOK_MOD, GEN_NOPUSH, g_mod };
60 static GenDesc GenASL = { TOK_SHL, GEN_NOPUSH, g_asl };
61 static GenDesc GenASR = { TOK_SHR, GEN_NOPUSH, g_asr };
62 static GenDesc GenLT = { TOK_LT, GEN_NOPUSH, g_lt };
63 static GenDesc GenLE = { TOK_LE, GEN_NOPUSH, g_le };
64 static GenDesc GenGE = { TOK_GE, GEN_NOPUSH, g_ge };
65 static GenDesc GenGT = { TOK_GT, GEN_NOPUSH, g_gt };
66 static GenDesc GenEQ = { TOK_EQ, GEN_NOPUSH, g_eq };
67 static GenDesc GenNE = { TOK_NE, GEN_NOPUSH, g_ne };
68 static GenDesc GenAND = { TOK_AND, GEN_NOPUSH, g_and };
69 static GenDesc GenXOR = { TOK_XOR, GEN_NOPUSH, g_xor };
70 static GenDesc GenOR = { TOK_OR, GEN_NOPUSH, g_or };
71 static GenDesc GenPASGN = { TOK_PLUS_ASSIGN, GEN_NOPUSH, g_add };
72 static GenDesc GenSASGN = { TOK_MINUS_ASSIGN, GEN_NOPUSH, g_sub };
73 static GenDesc GenMASGN = { TOK_MUL_ASSIGN, GEN_NOPUSH, g_mul };
74 static GenDesc GenDASGN = { TOK_DIV_ASSIGN, GEN_NOPUSH, g_div };
75 static GenDesc GenMOASGN = { TOK_MOD_ASSIGN, GEN_NOPUSH, g_mod };
76 static GenDesc GenSLASGN = { TOK_SHL_ASSIGN, GEN_NOPUSH, g_asl };
77 static GenDesc GenSRASGN = { TOK_SHR_ASSIGN, GEN_NOPUSH, g_asr };
78 static GenDesc GenAASGN = { TOK_AND_ASSIGN, GEN_NOPUSH, g_and };
79 static GenDesc GenXOASGN = { TOK_XOR_ASSIGN, GEN_NOPUSH, g_xor };
80 static GenDesc GenOASGN = { TOK_OR_ASSIGN, GEN_NOPUSH, g_or };
84 /*****************************************************************************/
85 /* Function forwards */
86 /*****************************************************************************/
90 static int expr (int (*func) (ExprDesc*), ExprDesc *lval);
91 /* Expression parser; func is either hie0 or hie1. */
95 /*****************************************************************************/
96 /* Helper functions */
97 /*****************************************************************************/
101 static unsigned GlobalModeFlags (unsigned flags)
102 /* Return the addressing mode flags for the variable with the given flags */
105 if (flags == E_TGLAB) {
106 /* External linkage */
108 } else if (flags == E_TREGISTER) {
109 /* Register variable */
119 static int IsNullPtr (ExprDesc* lval)
120 /* Return true if this is the NULL pointer constant */
122 return (IsClassInt (lval->Type) && /* Is it an int? */
123 lval->Flags == E_MCONST && /* Is it constant? */
124 lval->ConstVal == 0); /* And is it's value zero? */
129 static type* promoteint (type* lhst, type* rhst)
130 /* In an expression with two ints, return the type of the result */
132 /* Rules for integer types:
133 * - If one of the values is a long, the result is long.
134 * - If one of the values is unsigned, the result is also unsigned.
135 * - Otherwise the result is an int.
137 if (IsTypeLong (lhst) || IsTypeLong (rhst)) {
138 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
144 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
154 static unsigned typeadjust (ExprDesc* lhs, ExprDesc* rhs, int NoPush)
155 /* Adjust the two values for a binary operation. lhs is expected on stack or
156 * to be constant, rhs is expected to be in the primary register or constant.
157 * The function will put the type of the result into lhs and return the
158 * code generator flags for the operation.
159 * If NoPush is given, it is assumed that the operation does not expect the lhs
160 * to be on stack, and that lhs is in a register instead.
161 * Beware: The function does only accept int types.
164 unsigned ltype, rtype;
167 /* Get the type strings */
168 type* lhst = lhs->Type;
169 type* rhst = rhs->Type;
171 /* Generate type adjustment code if needed */
172 ltype = TypeOf (lhst);
173 if (lhs->Flags == E_MCONST) {
177 /* Value is in primary register*/
180 rtype = TypeOf (rhst);
181 if (rhs->Flags == E_MCONST) {
184 flags = g_typeadjust (ltype, rtype);
186 /* Set the type of the result */
187 lhs->Type = promoteint (lhst, rhst);
189 /* Return the code generator flags */
195 unsigned assignadjust (type* lhst, ExprDesc* rhs)
196 /* Adjust the type of the right hand expression so that it can be assigned to
197 * the type on the left hand side. This function is used for assignment and
198 * for converting parameters in a function call. It returns the code generator
199 * flags for the operation. The type string of the right hand side will be
200 * set to the type of the left hand side.
203 /* Get the type of the right hand side. Treat function types as
204 * pointer-to-function
206 type* rhst = rhs->Type;
207 if (IsTypeFunc (rhst)) {
208 rhst = PointerTo (rhst);
211 /* After calling this function, rhs will have the type of the lhs */
214 /* First, do some type checking */
215 if (IsTypeVoid (lhst) || IsTypeVoid (rhst)) {
216 /* If one of the sides are of type void, output a more apropriate
219 Error ("Illegal type");
220 } else if (IsClassInt (lhst)) {
221 if (IsClassPtr (rhst)) {
222 /* Pointer -> int conversion */
223 Warning ("Converting pointer to integer without a cast");
224 } else if (!IsClassInt (rhst)) {
225 Error ("Incompatible types");
227 /* Convert the rhs to the type of the lhs. */
228 unsigned flags = TypeOf (rhst);
229 if (rhs->Flags == E_MCONST) {
232 return g_typecast (TypeOf (lhst), flags);
234 } else if (IsClassPtr (lhst)) {
235 if (IsClassPtr (rhst)) {
236 /* Pointer to pointer assignment is valid, if:
237 * - both point to the same types, or
238 * - the rhs pointer is a void pointer, or
239 * - the lhs pointer is a void pointer.
241 if (!IsTypeVoid (Indirect (lhst)) && !IsTypeVoid (Indirect (rhst))) {
242 /* Compare the types */
243 switch (TypeCmp (lhst, rhst)) {
245 case TC_INCOMPATIBLE:
246 Error ("Incompatible pointer types");
250 Error ("Pointer types differ in type qualifiers");
258 } else if (IsClassInt (rhst)) {
259 /* Int to pointer assignment is valid only for constant zero */
260 if (rhs->Flags != E_MCONST || rhs->ConstVal != 0) {
261 Warning ("Converting integer to pointer without a cast");
263 } else if (IsTypeFuncPtr (lhst) && IsTypeFunc(rhst)) {
264 /* Assignment of function to function pointer is allowed, provided
265 * that both functions have the same parameter list.
267 if (TypeCmp (Indirect (lhst), rhst) < TC_EQUAL) {
268 Error ("Incompatible types");
271 Error ("Incompatible types");
274 Error ("Incompatible types");
277 /* Return an int value in all cases where the operands are not both ints */
283 void DefineData (ExprDesc* Expr)
284 /* Output a data definition for the given expression */
286 unsigned Flags = Expr->Flags;
288 switch (Flags & E_MCTYPE) {
292 g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->ConstVal, 0);
296 /* Register variable. Taking the address is usually not
299 if (!AllowRegVarAddr) {
300 Error ("Cannot take the address of a register variable");
306 /* Local or global symbol */
307 g_defdata (GlobalModeFlags (Flags), Expr->Name, Expr->ConstVal);
311 /* a literal of some kind */
312 g_defdata (CF_STATIC, LiteralPoolLabel, Expr->ConstVal);
316 Internal ("Unknown constant type: %04X", Flags);
322 static void lconst (unsigned Flags, ExprDesc* Expr)
323 /* Load the primary register with some constant value. */
325 switch (Expr->Flags & E_MCTYPE) {
328 g_leasp (Expr->ConstVal);
332 /* Number constant */
333 g_getimmed (Flags | TypeOf (Expr->Type) | CF_CONST, Expr->ConstVal, 0);
337 /* Register variable. Taking the address is usually not
340 if (!AllowRegVarAddr) {
341 Error ("Cannot take the address of a register variable");
347 /* Local or global symbol, load address */
348 Flags |= GlobalModeFlags (Expr->Flags);
350 g_getimmed (Flags, Expr->Name, Expr->ConstVal);
355 g_getimmed (CF_STATIC, LiteralPoolLabel, Expr->ConstVal);
359 Internal ("Unknown constant type: %04X", Expr->Flags);
365 static int kcalc (int tok, long val1, long val2)
366 /* Calculate an operation with left and right operand constant. */
370 return (val1 == val2);
372 return (val1 != val2);
374 return (val1 < val2);
376 return (val1 <= val2);
378 return (val1 >= val2);
380 return (val1 > val2);
382 return (val1 | val2);
384 return (val1 ^ val2);
386 return (val1 & val2);
388 return (val1 >> val2);
390 return (val1 << val2);
392 return (val1 * val2);
395 Error ("Division by zero");
398 return (val1 / val2);
401 Error ("Modulo operation with zero");
404 return (val1 % val2);
406 Internal ("kcalc: got token 0x%X\n", tok);
413 static const GenDesc* FindGen (token_t Tok, const GenDesc** Table)
414 /* Find a token in a generator table */
417 while ((G = *Table) != 0) {
428 static int istypeexpr (void)
429 /* Return true if some sort of variable or type is waiting (helper for cast
430 * and sizeof() in hie10).
435 return CurTok.Tok == TOK_LPAREN && (
436 (NextTok.Tok >= TOK_FIRSTTYPE && NextTok.Tok <= TOK_LASTTYPE) ||
437 (NextTok.Tok == TOK_CONST) ||
438 (NextTok.Tok == TOK_IDENT &&
439 (Entry = FindSym (NextTok.Ident)) != 0 &&
445 static void PushAddr (ExprDesc* lval)
446 /* If the expression contains an address that was somehow evaluated,
447 * push this address on the stack. This is a helper function for all
448 * sorts of implicit or explicit assignment functions where the lvalue
449 * must be saved if it's not constant, before evaluating the rhs.
452 /* Get the address on stack if needed */
453 if (lval->Flags != E_MREG && (lval->Flags & E_MEXPR)) {
454 /* Push the address (always a pointer) */
461 static void MakeConstIntExpr (ExprDesc* Expr, long Value)
462 /* Make Expr a constant integer expression with the given value */
464 Expr->Flags = E_MCONST;
465 Expr->Type = type_int;
466 Expr->ConstVal = Value;
471 void ConstSubExpr (int (*F) (ExprDesc*), ExprDesc* Expr)
472 /* Will evaluate an expression via the given function. If the result is not
473 * a constant, a diagnostic will be printed, and the value is replaced by
474 * a constant one to make sure there are no internal errors that result
475 * from this input error.
478 memset (Expr, 0, sizeof (*Expr));
479 if (F (Expr) != 0 || Expr->Flags != E_MCONST) {
480 Error ("Constant expression expected");
481 /* To avoid any compiler errors, make the expression a valid const */
482 MakeConstIntExpr (Expr, 1);
488 /*****************************************************************************/
490 /*****************************************************************************/
494 void exprhs (unsigned flags, int k, ExprDesc *lval)
495 /* Put the result of an expression into the primary register */
501 /* Dereferenced lvalue */
502 flags |= TypeOf (lval->Type);
503 if (lval->Test & E_FORCETEST) {
505 lval->Test &= ~E_FORCETEST;
507 if (f & E_MGLOBAL) { /* ref to globalvar */
509 flags |= GlobalModeFlags (f);
510 g_getstatic (flags, lval->Name, lval->ConstVal);
511 } else if (f & E_MLOCAL) {
512 /* ref to localvar */
513 g_getlocal (flags, lval->ConstVal);
514 } else if (f & E_MCONST) {
515 /* ref to absolute address */
516 g_getstatic (flags | CF_ABSOLUTE, lval->ConstVal, 0);
517 } else if (f == E_MEOFFS) {
518 g_getind (flags, lval->ConstVal);
519 } else if (f != E_MREG) {
522 } else if (f == E_MEOFFS) {
523 /* reference not storable */
524 flags |= TypeOf (lval->Type);
525 g_inc (flags | CF_CONST, lval->ConstVal);
526 } else if ((f & E_MEXPR) == 0) {
527 /* Constant of some sort, load it into the primary */
528 lconst (flags, lval);
530 /* Are we testing this value? */
531 if (lval->Test & E_FORCETEST) {
532 /* Yes, force a test */
533 flags |= TypeOf (lval->Type);
535 lval->Test &= ~E_FORCETEST;
541 static unsigned FunctionParamList (FuncDesc* Func)
542 /* Parse a function parameter list and pass the parameters to the called
543 * function. Depending on several criteria this may be done by just pushing
544 * each parameter separately, or creating the parameter frame once and then
545 * storing into this frame.
546 * The function returns the size of the parameters pushed.
551 /* Initialize variables */
552 SymEntry* Param = 0; /* Keep gcc silent */
553 unsigned ParamSize = 0; /* Size of parameters pushed */
554 unsigned ParamCount = 0; /* Number of parameters pushed */
555 unsigned FrameSize = 0; /* Size of parameter frame */
556 unsigned FrameParams = 0; /* Number of params in frame */
557 int FrameOffs = 0; /* Offset into parameter frame */
558 int Ellipsis = 0; /* Function is variadic */
560 /* As an optimization, we may allocate the complete parameter frame at
561 * once instead of pushing each parameter as it comes. We may do that,
564 * - optimizations that increase code size are enabled (allocating the
565 * stack frame at once gives usually larger code).
566 * - we have more than one parameter to push (don't count the last param
567 * for __fastcall__ functions).
569 if (CodeSizeFactor >= 200) {
571 /* Calculate the number and size of the parameters */
572 FrameParams = Func->ParamCount;
573 FrameSize = Func->ParamSize;
574 if (FrameParams > 0 && (Func->Flags & FD_FASTCALL) != 0) {
575 /* Last parameter is not pushed */
576 const SymEntry* LastParam = Func->SymTab->SymTail;
577 FrameSize -= CheckedSizeOf (LastParam->Type);
581 /* Do we have more than one parameter in the frame? */
582 if (FrameParams > 1) {
583 /* Okeydokey, setup the frame */
588 /* Don't use a preallocated frame */
593 /* Parse the actual parameter list */
594 while (CurTok.Tok != TOK_RPAREN) {
599 /* Count arguments */
602 /* Fetch the pointer to the next argument, check for too many args */
603 if (ParamCount <= Func->ParamCount) {
604 /* Beware: If there are parameters with identical names, they
605 * cannot go into the same symbol table, which means that in this
606 * case of errorneous input, the number of nodes in the symbol
607 * table and ParamCount are NOT equal. We have to handle this case
608 * below to avoid segmentation violations. Since we know that this
609 * problem can only occur if there is more than one parameter,
610 * we will just use the last one.
612 if (ParamCount == 1) {
614 Param = Func->SymTab->SymHead;
615 } else if (Param->NextSym != 0) {
617 Param = Param->NextSym;
618 CHECK ((Param->Flags & SC_PARAM) != 0);
620 } else if (!Ellipsis) {
621 /* Too many arguments. Do we have an open param list? */
622 if ((Func->Flags & FD_VARIADIC) == 0) {
623 /* End of param list reached, no ellipsis */
624 Error ("Too many arguments in function call");
626 /* Assume an ellipsis even in case of errors to avoid an error
627 * message for each other argument.
632 /* Do some optimization: If we have a constant value to push,
633 * use a special function that may optimize.
636 if (!Ellipsis && CheckedSizeOf (Param->Type) == 1) {
637 CFlags = CF_FORCECHAR;
640 if (evalexpr (CFlags, hie1, &lval) == 0) {
641 /* A constant value */
645 /* If we don't have an argument spec, accept anything, otherwise
646 * convert the actual argument to the type needed.
649 /* Promote the argument if needed */
650 assignadjust (Param->Type, &lval);
652 /* If we have a prototype, chars may be pushed as chars */
653 Flags |= CF_FORCECHAR;
656 /* Use the type of the argument for the push */
657 Flags |= TypeOf (lval.Type);
659 /* If this is a fastcall function, don't push the last argument */
660 if (ParamCount == Func->ParamCount && (Func->Flags & FD_FASTCALL) != 0) {
661 /* Just load the argument into the primary. This is only needed if
662 * we have a constant argument, otherwise the value is already in
665 if (Flags & CF_CONST) {
666 exprhs (CF_FORCECHAR, 0, &lval);
669 unsigned ArgSize = sizeofarg (Flags);
671 /* We have the space already allocated, store in the frame */
672 CHECK (FrameSize >= ArgSize);
673 FrameSize -= ArgSize;
674 FrameOffs -= ArgSize;
676 g_putlocal (Flags | CF_NOKEEP, FrameOffs, lval.ConstVal);
678 /* Push the argument */
679 g_push (Flags, lval.ConstVal);
682 /* Calculate total parameter size */
683 ParamSize += ArgSize;
686 /* Check for end of argument list */
687 if (CurTok.Tok != TOK_COMMA) {
693 /* Check if we had enough parameters */
694 if (ParamCount < Func->ParamCount) {
695 Error ("Too few arguments in function call");
698 /* The function returns the size of all parameters pushed onto the stack.
699 * However, if there are parameters missing (which is an error and was
700 * flagged by the compiler) AND a stack frame was preallocated above,
701 * we would loose track of the stackpointer and generate an internal error
702 * later. So we correct the value by the parameters that should have been
703 * pushed to avoid an internal compiler error. Since an error was
704 * generated before, no code will be output anyway.
706 return ParamSize + FrameSize;
711 static void FunctionCall (int k, ExprDesc* lval)
712 /* Perform a function call. */
714 FuncDesc* Func; /* Function descriptor */
715 int IsFuncPtr; /* Flag */
716 unsigned ParamSize; /* Number of parameter bytes */
717 CodeMark Mark = 0; /* Initialize to keep gcc silent */
718 int PtrOffs = 0; /* Offset of function pointer on stack */
719 int IsFastCall = 0; /* True if it's a fast call function */
720 int PtrOnStack = 0; /* True if a pointer copy is on stack */
722 /* Get a pointer to the function descriptor from the type string */
723 Func = GetFuncDesc (lval->Type);
725 /* Handle function pointers transparently */
726 IsFuncPtr = IsTypeFuncPtr (lval->Type);
729 /* Check wether it's a fastcall function that has parameters */
730 IsFastCall = IsFastCallFunc (lval->Type + 1) && (Func->ParamCount > 0);
732 /* Things may be difficult, depending on where the function pointer
733 * resides. If the function pointer is an expression of some sort
734 * (not a local or global variable), we have to evaluate this
735 * expression now and save the result for later. Since calls to
736 * function pointers may be nested, we must save it onto the stack.
737 * For fastcall functions we do also need to place a copy of the
738 * pointer on stack, since we cannot use a/x.
740 PtrOnStack = IsFastCall || ((lval->Flags & (E_MGLOBAL | E_MLOCAL)) == 0);
743 /* Not a global or local variable, or a fastcall function. Load
744 * the pointer into the primary and mark it as an expression.
746 exprhs (CF_NONE, k, lval);
747 lval->Flags |= E_MEXPR;
749 /* Remember the code position */
750 Mark = GetCodePos ();
752 /* Push the pointer onto the stack and remember the offset */
757 /* Check for known standard functions and inline them if requested */
758 } else if (InlineStdFuncs && IsStdFunc ((const char*) lval->Name)) {
760 /* Inline this function */
761 HandleStdFunc (Func, lval);
766 /* Parse the parameter list */
767 ParamSize = FunctionParamList (Func);
769 /* We need the closing paren here */
772 /* Special handling for function pointers */
775 /* If the function is not a fastcall function, load the pointer to
776 * the function into the primary.
780 /* Not a fastcall function - we may use the primary */
782 /* If we have no parameters, the pointer is still in the
783 * primary. Remove the code to push it and correct the
786 if (ParamSize == 0) {
791 /* Load from the saved copy */
792 g_getlocal (CF_PTR, PtrOffs);
795 /* Load from original location */
796 exprhs (CF_NONE, k, lval);
799 /* Call the function */
800 g_callind (TypeOf (lval->Type+1), ParamSize, PtrOffs);
804 /* Fastcall function. We cannot use the primary for the function
805 * pointer and must therefore use an offset to the stack location.
806 * Since fastcall functions may never be variadic, we can use the
807 * index register for this purpose.
809 g_callind (CF_LOCAL, ParamSize, PtrOffs);
812 /* If we have a pointer on stack, remove it */
814 g_space (- (int) sizeofarg (CF_PTR));
823 /* Normal function */
824 g_call (TypeOf (lval->Type), (const char*) lval->Name, ParamSize);
831 static int primary (ExprDesc* lval)
832 /* This is the lowest level of the expression parser. */
836 /* Initialize fields in the expression stucture */
837 lval->Test = 0; /* No test */
838 lval->Sym = 0; /* Symbol unknown */
840 /* Character and integer constants. */
841 if (CurTok.Tok == TOK_ICONST || CurTok.Tok == TOK_CCONST) {
842 lval->Flags = E_MCONST | E_TCONST;
843 lval->Type = CurTok.Type;
844 lval->ConstVal = CurTok.IVal;
849 /* Process parenthesized subexpression by calling the whole parser
852 if (CurTok.Tok == TOK_LPAREN) {
854 memset (lval, 0, sizeof (*lval)); /* Remove any attributes */
860 /* If we run into an identifier in preprocessing mode, we assume that this
861 * is an undefined macro and replace it by a constant value of zero.
863 if (Preprocessing && CurTok.Tok == TOK_IDENT) {
864 MakeConstIntExpr (lval, 0);
868 /* All others may only be used if the expression evaluation is not called
869 * recursively by the preprocessor.
872 /* Illegal expression in PP mode */
873 Error ("Preprocessor expression expected");
874 MakeConstIntExpr (lval, 1);
879 if (CurTok.Tok == TOK_IDENT) {
884 /* Get a pointer to the symbol table entry */
885 Sym = lval->Sym = FindSym (CurTok.Ident);
887 /* Is the symbol known? */
890 /* We found the symbol - skip the name token */
893 /* The expression type is the symbol type */
894 lval->Type = Sym->Type;
896 /* Check for illegal symbol types */
897 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
898 if (Sym->Flags & SC_TYPE) {
899 /* Cannot use type symbols */
900 Error ("Variable identifier expected");
901 /* Assume an int type to make lval valid */
902 lval->Flags = E_MLOCAL | E_TLOFFS;
903 lval->Type = type_int;
908 /* Check for legal symbol types */
909 if ((Sym->Flags & SC_CONST) == SC_CONST) {
910 /* Enum or some other numeric constant */
911 lval->Flags = E_MCONST;
912 lval->ConstVal = Sym->V.ConstVal;
914 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
916 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
917 lval->Name = (unsigned long) Sym->Name;
919 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
920 /* Local variable. If this is a parameter for a variadic
921 * function, we have to add some address calculations, and the
922 * address is not const.
924 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
925 /* Variadic parameter */
926 g_leavariadic (Sym->V.Offs - F_GetParamSize (CurrentFunc));
927 lval->Flags = E_MEXPR;
930 /* Normal parameter */
931 lval->Flags = E_MLOCAL | E_TLOFFS;
932 lval->ConstVal = Sym->V.Offs;
934 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
935 /* Static variable */
936 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
937 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
938 lval->Name = (unsigned long) Sym->Name;
940 lval->Flags = E_MGLOBAL | E_MCONST | E_TLLAB;
941 lval->Name = Sym->V.Label;
944 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
945 /* Register variable, zero page based */
946 lval->Flags = E_MGLOBAL | E_MCONST | E_TREGISTER;
947 lval->Name = Sym->V.Offs;
950 /* Local static variable */
951 lval->Flags = E_MGLOBAL | E_MCONST | E_TLLAB;
952 lval->Name = Sym->V.Offs;
956 /* The symbol is referenced now */
957 Sym->Flags |= SC_REF;
958 if (IsTypeFunc (lval->Type) || IsTypeArray (lval->Type)) {
964 /* We did not find the symbol. Remember the name, then skip it */
965 strcpy (Ident, CurTok.Ident);
968 /* IDENT is either an auto-declared function or an undefined variable. */
969 if (CurTok.Tok == TOK_LPAREN) {
970 /* Declare a function returning int. For that purpose, prepare a
971 * function signature for a function having an empty param list
974 Warning ("Function call without a prototype");
975 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
976 lval->Type = Sym->Type;
977 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
978 lval->Name = (unsigned long) Sym->Name;
984 /* Undeclared Variable */
985 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
986 lval->Flags = E_MLOCAL | E_TLOFFS;
987 lval->Type = type_int;
989 Error ("Undefined symbol: `%s'", Ident);
995 /* String literal? */
996 if (CurTok.Tok == TOK_SCONST) {
997 lval->Flags = E_MCONST | E_TLIT;
998 lval->ConstVal = CurTok.IVal;
999 lval->Type = GetCharArrayType (GetLiteralPoolOffs () - CurTok.IVal);
1004 /* ASM statement? */
1005 if (CurTok.Tok == TOK_ASM) {
1007 lval->Type = type_void;
1008 lval->Flags = E_MEXPR;
1013 /* __AX__ and __EAX__ pseudo values? */
1014 if (CurTok.Tok == TOK_AX || CurTok.Tok == TOK_EAX) {
1015 lval->Type = (CurTok.Tok == TOK_AX)? type_uint : type_ulong;
1016 lval->Flags = E_MREG;
1017 lval->Test &= ~E_CC;
1020 return 1; /* May be used as lvalue */
1023 /* Illegal primary. */
1024 Error ("Expression expected");
1025 MakeConstIntExpr (lval, 1);
1031 static int arrayref (int k, ExprDesc* lval)
1032 /* Handle an array reference */
1046 /* Skip the bracket */
1049 /* Get the type of left side */
1052 /* We can apply a special treatment for arrays that have a const base
1053 * address. This is true for most arrays and will produce a lot better
1054 * code. Check if this is a const base address.
1056 lflags = lval->Flags & ~E_MCTYPE;
1057 ConstBaseAddr = (lflags == E_MCONST) || /* Constant numeric address */
1058 (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1059 lflags == E_MLOCAL; /* Local array */
1061 /* If we have a constant base, we delay the address fetch */
1062 Mark1 = GetCodePos ();
1063 Mark2 = 0; /* Silence gcc */
1064 if (!ConstBaseAddr) {
1065 /* Get a pointer to the array into the primary */
1066 exprhs (CF_NONE, k, lval);
1068 /* Get the array pointer on stack. Do not push more than 16
1069 * bit, even if this value is greater, since we cannot handle
1070 * other than 16bit stuff when doing indexing.
1072 Mark2 = GetCodePos ();
1076 /* TOS now contains ptr to array elements. Get the subscript. */
1078 if (l == 0 && lval2.Flags == E_MCONST) {
1080 /* The array subscript is a constant - remove value from stack */
1081 if (!ConstBaseAddr) {
1085 /* Get an array pointer into the primary */
1086 exprhs (CF_NONE, k, lval);
1089 if (IsClassPtr (tptr1)) {
1091 /* Scale the subscript value according to element size */
1092 lval2.ConstVal *= CheckedPSizeOf (tptr1);
1094 /* Remove code for lhs load */
1097 /* Handle constant base array on stack. Be sure NOT to
1098 * handle pointers the same way, this won't work.
1100 if (IsTypeArray (tptr1) &&
1101 ((lval->Flags & ~E_MCTYPE) == E_MCONST ||
1102 (lval->Flags & ~E_MCTYPE) == E_MLOCAL ||
1103 (lval->Flags & E_MGLOBAL) != 0 ||
1104 (lval->Flags == E_MEOFFS))) {
1105 lval->ConstVal += lval2.ConstVal;
1108 /* Pointer - load into primary and remember offset */
1109 if ((lval->Flags & E_MEXPR) == 0 || k != 0) {
1110 exprhs (CF_NONE, k, lval);
1112 lval->ConstVal = lval2.ConstVal;
1113 lval->Flags = E_MEOFFS;
1116 /* Result is of element type */
1117 lval->Type = Indirect (tptr1);
1122 } else if (IsClassPtr (tptr2 = lval2.Type)) {
1123 /* Subscript is pointer, get element type */
1124 lval2.Type = Indirect (tptr2);
1126 /* Scale the rhs value in the primary register */
1127 g_scale (TypeOf (tptr1), CheckedSizeOf (lval2.Type));
1129 lval->Type = lval2.Type;
1131 Error ("Cannot subscript");
1134 /* Add the subscript. Since arrays are indexed by integers,
1135 * we will ignore the true type of the subscript here and
1136 * use always an int.
1138 g_inc (CF_INT | CF_CONST, lval2.ConstVal);
1142 /* Array subscript is not constant. Load it into the primary */
1143 Mark2 = GetCodePos ();
1144 exprhs (CF_NONE, l, &lval2);
1147 if (IsClassPtr (tptr1)) {
1149 /* Get the element type */
1150 lval->Type = Indirect (tptr1);
1152 /* Indexing is based on int's, so we will just use the integer
1153 * portion of the index (which is in (e)ax, so there's no further
1156 g_scale (CF_INT, CheckedSizeOf (lval->Type));
1158 } else if (IsClassPtr (tptr2)) {
1160 /* Get the element type */
1161 lval2.Type = Indirect (tptr2);
1163 /* Get the int value on top. If we go here, we're sure,
1164 * both values are 16 bit (the first one was truncated
1165 * if necessary and the second one is a pointer).
1166 * Note: If ConstBaseAddr is true, we don't have a value on
1167 * stack, so to "swap" both, just push the subscript.
1169 if (ConstBaseAddr) {
1171 exprhs (CF_NONE, k, lval);
1178 g_scale (TypeOf (tptr1), CheckedSizeOf (lval2.Type));
1179 lval->Type = lval2.Type;
1181 Error ("Cannot subscript");
1184 /* The offset is now in the primary register. It didn't have a
1185 * constant base address for the lhs, the lhs address is already
1186 * on stack, and we must add the offset. If the base address was
1187 * constant, we call special functions to add the address to the
1190 if (!ConstBaseAddr) {
1191 /* Add the subscript. Both values are int sized. */
1195 /* If the subscript has itself a constant address, it is often
1196 * a better idea to reverse again the order of the evaluation.
1197 * This will generate better code if the subscript is a byte
1198 * sized variable. But beware: This is only possible if the
1199 * subscript was not scaled, that is, if this was a byte array
1202 rflags = lval2.Flags & ~E_MCTYPE;
1203 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1204 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1205 rflags == E_MLOCAL; /* Local array */
1207 if (ConstSubAddr && CheckedSizeOf (lval->Type) == 1) {
1211 /* Reverse the order of evaluation */
1212 unsigned flags = (CheckedSizeOf (lval2.Type) == 1)? CF_CHAR : CF_INT;
1215 /* Get a pointer to the array into the primary. We have changed
1216 * Type above but we need the original type to load the
1217 * address, so restore it temporarily.
1219 SavedType = lval->Type;
1221 exprhs (CF_NONE, k, lval);
1222 lval->Type = SavedType;
1224 /* Add the variable */
1225 if (rflags == E_MLOCAL) {
1226 g_addlocal (flags, lval2.ConstVal);
1228 flags |= GlobalModeFlags (lval2.Flags);
1229 g_addstatic (flags, lval2.Name, lval2.ConstVal);
1232 if (lflags == E_MCONST) {
1233 /* Constant numeric address. Just add it */
1234 g_inc (CF_INT | CF_UNSIGNED, lval->ConstVal);
1235 } else if (lflags == E_MLOCAL) {
1236 /* Base address is a local variable address */
1237 if (IsTypeArray (tptr1)) {
1238 g_addaddr_local (CF_INT, lval->ConstVal);
1240 g_addlocal (CF_PTR, lval->ConstVal);
1243 /* Base address is a static variable address */
1244 unsigned flags = CF_INT;
1245 flags |= GlobalModeFlags (lval->Flags);
1246 if (IsTypeArray (tptr1)) {
1247 g_addaddr_static (flags, lval->Name, lval->ConstVal);
1249 g_addstatic (flags, lval->Name, lval->ConstVal);
1255 lval->Flags = E_MEXPR;
1258 return !IsTypeArray (lval->Type);
1264 static int structref (int k, ExprDesc* lval)
1265 /* Process struct field after . or ->. */
1271 /* Skip the token and check for an identifier */
1273 if (CurTok.Tok != TOK_IDENT) {
1274 Error ("Identifier expected");
1275 lval->Type = type_int;
1279 /* Get the symbol table entry and check for a struct field */
1280 strcpy (Ident, CurTok.Ident);
1282 Field = FindStructField (lval->Type, Ident);
1284 Error ("Struct/union has no field named `%s'", Ident);
1285 lval->Type = type_int;
1289 /* If we have constant input data, the result is also constant */
1290 flags = lval->Flags & ~E_MCTYPE;
1291 if (flags == E_MCONST ||
1292 (k == 0 && (flags == E_MLOCAL ||
1293 (flags & E_MGLOBAL) != 0 ||
1294 lval->Flags == E_MEOFFS))) {
1295 lval->ConstVal += Field->V.Offs;
1297 if ((flags & E_MEXPR) == 0 || k != 0) {
1298 exprhs (CF_NONE, k, lval);
1300 lval->ConstVal = Field->V.Offs;
1301 lval->Flags = E_MEOFFS;
1303 lval->Type = Field->Type;
1304 return !IsTypeArray (Field->Type);
1309 static int hie11 (ExprDesc *lval)
1310 /* Handle compound types (structs and arrays) */
1317 if (CurTok.Tok < TOK_LBRACK || CurTok.Tok > TOK_PTR_REF) {
1324 if (CurTok.Tok == TOK_LBRACK) {
1326 /* Array reference */
1327 k = arrayref (k, lval);
1329 } else if (CurTok.Tok == TOK_LPAREN) {
1331 /* Function call. Skip the opening parenthesis */
1334 if (IsTypeFunc (lval->Type) || IsTypeFuncPtr (lval->Type)) {
1336 /* Call the function */
1337 FunctionCall (k, lval);
1339 /* Result is in the primary register */
1340 lval->Flags = E_MEXPR;
1343 lval->Type = GetFuncReturn (lval->Type);
1346 Error ("Illegal function call");
1350 } else if (CurTok.Tok == TOK_DOT) {
1352 if (!IsClassStruct (lval->Type)) {
1353 Error ("Struct expected");
1355 k = structref (0, lval);
1357 } else if (CurTok.Tok == TOK_PTR_REF) {
1360 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1361 Error ("Struct pointer expected");
1363 k = structref (k, lval);
1373 static void store (ExprDesc* lval)
1374 /* Store primary reg into this reference */
1380 flags = TypeOf (lval->Type);
1381 if (f & E_MGLOBAL) {
1382 flags |= GlobalModeFlags (f);
1389 g_putstatic (flags, lval->Name, lval->ConstVal);
1391 } else if (f & E_MLOCAL) {
1392 g_putlocal (flags, lval->ConstVal, 0);
1393 } else if (f == E_MEOFFS) {
1394 g_putind (flags, lval->ConstVal);
1395 } else if (f != E_MREG) {
1397 g_putind (flags, 0);
1399 /* Store into absolute address */
1400 g_putstatic (flags | CF_ABSOLUTE, lval->ConstVal, 0);
1404 /* Assume that each one of the stores will invalidate CC */
1405 lval->Test &= ~E_CC;
1410 static void pre_incdec (ExprDesc* lval, void (*inc) (unsigned, unsigned long))
1411 /* Handle --i and ++i */
1418 if ((k = hie10 (lval)) == 0) {
1419 Error ("Invalid lvalue");
1423 /* Get the data type */
1424 flags = TypeOf (lval->Type) | CF_FORCECHAR | CF_CONST;
1426 /* Get the increment value in bytes */
1427 val = (lval->Type [0] == T_PTR)? CheckedPSizeOf (lval->Type) : 1;
1429 /* We're currently only able to handle some adressing modes */
1430 if ((lval->Flags & E_MGLOBAL) == 0 && /* Global address? */
1431 (lval->Flags & E_MLOCAL) == 0 && /* Local address? */
1432 (lval->Flags & E_MCONST) == 0 && /* Constant address? */
1433 (lval->Flags & E_MEXPR) == 0) { /* Address in a/x? */
1435 /* Use generic code. Push the address if needed */
1438 /* Fetch the value */
1439 exprhs (CF_NONE, k, lval);
1441 /* Increment value in primary */
1444 /* Store the result back */
1449 /* Special code for some addressing modes - use the special += ops */
1450 if (lval->Flags & E_MGLOBAL) {
1451 flags |= GlobalModeFlags (lval->Flags);
1453 g_addeqstatic (flags, lval->Name, lval->ConstVal, val);
1455 g_subeqstatic (flags, lval->Name, lval->ConstVal, val);
1457 } else if (lval->Flags & E_MLOCAL) {
1458 /* ref to localvar */
1460 g_addeqlocal (flags, lval->ConstVal, val);
1462 g_subeqlocal (flags, lval->ConstVal, val);
1464 } else if (lval->Flags & E_MCONST) {
1465 /* ref to absolute address */
1466 flags |= CF_ABSOLUTE;
1468 g_addeqstatic (flags, lval->ConstVal, 0, val);
1470 g_subeqstatic (flags, lval->ConstVal, 0, val);
1472 } else if (lval->Flags & E_MEXPR) {
1473 /* Address in a/x, check if we have an offset */
1474 unsigned Offs = (lval->Flags == E_MEOFFS)? lval->ConstVal : 0;
1476 g_addeqind (flags, Offs, val);
1478 g_subeqind (flags, Offs, val);
1481 Internal ("Invalid addressing mode");
1486 /* Result is an expression */
1487 lval->Flags = E_MEXPR;
1492 static void post_incdec (ExprDesc* lval, int k, void (*inc) (unsigned, unsigned long))
1493 /* Handle i-- and i++ */
1499 Error ("Invalid lvalue");
1503 /* Get the data type */
1504 flags = TypeOf (lval->Type);
1506 /* Push the address if needed */
1509 /* Fetch the value and save it (since it's the result of the expression) */
1510 exprhs (CF_NONE, 1, lval);
1511 g_save (flags | CF_FORCECHAR);
1513 /* If we have a pointer expression, increment by the size of the type */
1514 if (lval->Type[0] == T_PTR) {
1515 inc (flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (lval->Type + 1));
1517 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1520 /* Store the result back */
1523 /* Restore the original value */
1524 g_restore (flags | CF_FORCECHAR);
1525 lval->Flags = E_MEXPR;
1530 static void unaryop (int tok, ExprDesc* lval)
1531 /* Handle unary -/+ and ~ */
1538 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
1539 /* Value is constant */
1541 case TOK_MINUS: lval->ConstVal = -lval->ConstVal; break;
1542 case TOK_PLUS: break;
1543 case TOK_COMP: lval->ConstVal = ~lval->ConstVal; break;
1544 default: Internal ("Unexpected token: %d", tok);
1547 /* Value is not constant */
1548 exprhs (CF_NONE, k, lval);
1550 /* Get the type of the expression */
1551 flags = TypeOf (lval->Type);
1553 /* Handle the operation */
1555 case TOK_MINUS: g_neg (flags); break;
1556 case TOK_PLUS: break;
1557 case TOK_COMP: g_com (flags); break;
1558 default: Internal ("Unexpected token: %d", tok);
1560 lval->Flags = E_MEXPR;
1566 int hie10 (ExprDesc* lval)
1567 /* Handle ++, --, !, unary - etc. */
1572 switch (CurTok.Tok) {
1575 pre_incdec (lval, g_inc);
1579 pre_incdec (lval, g_dec);
1585 unaryop (CurTok.Tok, lval);
1590 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1591 /* Constant expression */
1592 lval->ConstVal = !lval->ConstVal;
1594 g_bneg (TypeOf (lval->Type));
1595 lval->Test |= E_CC; /* bneg will set cc */
1596 lval->Flags = E_MEXPR; /* say it's an expr */
1598 return 0; /* expr not storable */
1602 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1603 /* Expression is not const, indirect value loaded into primary */
1604 lval->Flags = E_MEXPR;
1605 lval->ConstVal = 0; /* Offset is zero now */
1608 if (IsClassPtr (t)) {
1609 lval->Type = Indirect (t);
1611 Error ("Illegal indirection");
1618 /* The & operator may be applied to any lvalue, and it may be
1619 * applied to functions, even if they're no lvalues.
1621 if (k == 0 && !IsTypeFunc (lval->Type)) {
1622 /* Allow the & operator with an array */
1623 if (!IsTypeArray (lval->Type)) {
1624 Error ("Illegal address");
1627 t = TypeAlloc (TypeLen (lval->Type) + 2);
1629 TypeCpy (t + 1, lval->Type);
1636 if (istypeexpr ()) {
1637 type Type[MAXTYPELEN];
1639 lval->ConstVal = CheckedSizeOf (ParseType (Type));
1642 /* Remember the output queue pointer */
1643 CodeMark Mark = GetCodePos ();
1645 lval->ConstVal = CheckedSizeOf (lval->Type);
1646 /* Remove any generated code */
1649 lval->Flags = E_MCONST | E_TCONST;
1650 lval->Type = type_uint;
1651 lval->Test &= ~E_CC;
1655 if (istypeexpr ()) {
1657 return TypeCast (lval);
1662 switch (CurTok.Tok) {
1664 post_incdec (lval, k, g_inc);
1668 post_incdec (lval, k, g_dec);
1678 static int hie_internal (const GenDesc** ops, /* List of generators */
1679 ExprDesc* lval, /* parent expr's lval */
1680 int (*hienext) (ExprDesc*),
1681 int* UsedGen) /* next higher level */
1682 /* Helper function */
1689 token_t tok; /* The operator token */
1690 unsigned ltype, type;
1691 int rconst; /* Operand is a constant */
1697 while ((Gen = FindGen (CurTok.Tok, ops)) != 0) {
1699 /* Tell the caller that we handled it's ops */
1702 /* All operators that call this function expect an int on the lhs */
1703 if (!IsClassInt (lval->Type)) {
1704 Error ("Integer expression expected");
1707 /* Remember the operator token, then skip it */
1711 /* Get the lhs on stack */
1712 Mark1 = GetCodePos ();
1713 ltype = TypeOf (lval->Type);
1714 if (k == 0 && lval->Flags == E_MCONST) {
1715 /* Constant value */
1716 Mark2 = GetCodePos ();
1717 g_push (ltype | CF_CONST, lval->ConstVal);
1719 /* Value not constant */
1720 exprhs (CF_NONE, k, lval);
1721 Mark2 = GetCodePos ();
1725 /* Get the right hand side */
1726 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1728 /* Check the type of the rhs */
1729 if (!IsClassInt (lval2.Type)) {
1730 Error ("Integer expression expected");
1733 /* Check for const operands */
1734 if (k == 0 && lval->Flags == E_MCONST && rconst) {
1736 /* Both operands are constant, remove the generated code */
1740 /* Evaluate the result */
1741 lval->ConstVal = kcalc (tok, lval->ConstVal, lval2.ConstVal);
1743 /* Get the type of the result */
1744 lval->Type = promoteint (lval->Type, lval2.Type);
1748 /* If the right hand side is constant, and the generator function
1749 * expects the lhs in the primary, remove the push of the primary
1752 unsigned rtype = TypeOf (lval2.Type);
1755 /* Second value is constant - check for div */
1758 if (tok == TOK_DIV && lval2.ConstVal == 0) {
1759 Error ("Division by zero");
1760 } else if (tok == TOK_MOD && lval2.ConstVal == 0) {
1761 Error ("Modulo operation with zero");
1763 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1766 ltype |= CF_REG; /* Value is in register */
1770 /* Determine the type of the operation result. */
1771 type |= g_typeadjust (ltype, rtype);
1772 lval->Type = promoteint (lval->Type, lval2.Type);
1775 Gen->Func (type, lval2.ConstVal);
1776 lval->Flags = E_MEXPR;
1779 /* We have a rvalue now */
1788 static int hie_compare (const GenDesc** ops, /* List of generators */
1789 ExprDesc* lval, /* parent expr's lval */
1790 int (*hienext) (ExprDesc*))
1791 /* Helper function for the compare operators */
1798 token_t tok; /* The operator token */
1800 int rconst; /* Operand is a constant */
1805 while ((Gen = FindGen (CurTok.Tok, ops)) != 0) {
1807 /* Remember the operator token, then skip it */
1811 /* Get the lhs on stack */
1812 Mark1 = GetCodePos ();
1813 ltype = TypeOf (lval->Type);
1814 if (k == 0 && lval->Flags == E_MCONST) {
1815 /* Constant value */
1816 Mark2 = GetCodePos ();
1817 g_push (ltype | CF_CONST, lval->ConstVal);
1819 /* Value not constant */
1820 exprhs (CF_NONE, k, lval);
1821 Mark2 = GetCodePos ();
1825 /* Get the right hand side */
1826 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1828 /* Make sure, the types are compatible */
1829 if (IsClassInt (lval->Type)) {
1830 if (!IsClassInt (lval2.Type) && !(IsClassPtr(lval2.Type) && IsNullPtr(lval))) {
1831 Error ("Incompatible types");
1833 } else if (IsClassPtr (lval->Type)) {
1834 if (IsClassPtr (lval2.Type)) {
1835 /* Both pointers are allowed in comparison if they point to
1836 * the same type, or if one of them is a void pointer.
1838 type* left = Indirect (lval->Type);
1839 type* right = Indirect (lval2.Type);
1840 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1841 /* Incomatible pointers */
1842 Error ("Incompatible types");
1844 } else if (!IsNullPtr (&lval2)) {
1845 Error ("Incompatible types");
1849 /* Check for const operands */
1850 if (k == 0 && lval->Flags == E_MCONST && rconst) {
1852 /* Both operands are constant, remove the generated code */
1856 /* Evaluate the result */
1857 lval->ConstVal = kcalc (tok, lval->ConstVal, lval2.ConstVal);
1861 /* If the right hand side is constant, and the generator function
1862 * expects the lhs in the primary, remove the push of the primary
1868 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1871 ltype |= CF_REG; /* Value is in register */
1875 /* Determine the type of the operation result. If the left
1876 * operand is of type char and the right is a constant, or
1877 * if both operands are of type char, we will encode the
1878 * operation as char operation. Otherwise the default
1879 * promotions are used.
1881 if (IsTypeChar (lval->Type) && (IsTypeChar (lval2.Type) || rconst)) {
1883 if (IsSignUnsigned (lval->Type) || IsSignUnsigned (lval2.Type)) {
1884 flags |= CF_UNSIGNED;
1887 flags |= CF_FORCECHAR;
1890 unsigned rtype = TypeOf (lval2.Type) | (flags & CF_CONST);
1891 flags |= g_typeadjust (ltype, rtype);
1895 Gen->Func (flags, lval2.ConstVal);
1896 lval->Flags = E_MEXPR;
1899 /* Result type is always int */
1900 lval->Type = type_int;
1902 /* We have a rvalue now, condition codes are set */
1912 static int hie9 (ExprDesc *lval)
1913 /* Process * and / operators. */
1915 static const GenDesc* hie9_ops [] = {
1916 &GenMUL, &GenDIV, &GenMOD, 0
1920 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1925 static void parseadd (int k, ExprDesc* lval)
1926 /* Parse an expression with the binary plus operator. lval contains the
1927 * unprocessed left hand side of the expression and will contain the
1928 * result of the expression on return.
1932 unsigned flags; /* Operation flags */
1933 CodeMark Mark; /* Remember code position */
1934 type* lhst; /* Type of left hand side */
1935 type* rhst; /* Type of right hand side */
1938 /* Skip the PLUS token */
1941 /* Get the left hand side type, initialize operation flags */
1945 /* Check for constness on both sides */
1946 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
1948 /* The left hand side is a constant. Good. Get rhs */
1950 if (k == 0 && lval2.Flags == E_MCONST) {
1952 /* Right hand side is also constant. Get the rhs type */
1955 /* Both expressions are constants. Check for pointer arithmetic */
1956 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1957 /* Left is pointer, right is int, must scale rhs */
1958 lval->ConstVal += lval2.ConstVal * CheckedPSizeOf (lhst);
1959 /* Result type is a pointer */
1960 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1961 /* Left is int, right is pointer, must scale lhs */
1962 lval->ConstVal = lval->ConstVal * CheckedPSizeOf (rhst) + lval2.ConstVal;
1963 /* Result type is a pointer */
1964 lval->Type = lval2.Type;
1965 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1966 /* Integer addition */
1967 lval->ConstVal += lval2.ConstVal;
1968 typeadjust (lval, &lval2, 1);
1971 Error ("Invalid operands for binary operator `+'");
1974 /* Result is constant, condition codes not set */
1975 lval->Test &= ~E_CC;
1979 /* lhs is a constant and rhs is not constant. Load rhs into
1982 exprhs (CF_NONE, k, &lval2);
1984 /* Beware: The check above (for lhs) lets not only pass numeric
1985 * constants, but also constant addresses (labels), maybe even
1986 * with an offset. We have to check for that here.
1989 /* First, get the rhs type. */
1993 if (lval->Flags == E_MCONST) {
1994 /* A numerical constant */
1997 /* Constant address label */
1998 flags |= GlobalModeFlags (lval->Flags) | CF_CONSTADDR;
2001 /* Check for pointer arithmetic */
2002 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2003 /* Left is pointer, right is int, must scale rhs */
2004 g_scale (CF_INT, CheckedPSizeOf (lhst));
2005 /* Operate on pointers, result type is a pointer */
2007 /* Generate the code for the add */
2008 if (lval->Flags == E_MCONST) {
2009 /* Numeric constant */
2010 g_inc (flags, lval->ConstVal);
2012 /* Constant address */
2013 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2015 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2017 /* Left is int, right is pointer, must scale lhs. */
2018 unsigned ScaleFactor = CheckedPSizeOf (rhst);
2020 /* Operate on pointers, result type is a pointer */
2022 lval->Type = lval2.Type;
2024 /* Since we do already have rhs in the primary, if lhs is
2025 * not a numeric constant, and the scale factor is not one
2026 * (no scaling), we must take the long way over the stack.
2028 if (lval->Flags == E_MCONST) {
2029 /* Numeric constant, scale lhs */
2030 lval->ConstVal *= ScaleFactor;
2031 /* Generate the code for the add */
2032 g_inc (flags, lval->ConstVal);
2033 } else if (ScaleFactor == 1) {
2034 /* Constant address but no need to scale */
2035 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2037 /* Constant address that must be scaled */
2038 g_push (TypeOf (lval2.Type), 0); /* rhs --> stack */
2039 g_getimmed (flags, lval->Name, lval->ConstVal);
2040 g_scale (CF_PTR, ScaleFactor);
2043 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2044 /* Integer addition */
2045 flags |= typeadjust (lval, &lval2, 1);
2046 /* Generate the code for the add */
2047 if (lval->Flags == E_MCONST) {
2048 /* Numeric constant */
2049 g_inc (flags, lval->ConstVal);
2051 /* Constant address */
2052 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2056 Error ("Invalid operands for binary operator `+'");
2059 /* Result is in primary register */
2060 lval->Flags = E_MEXPR;
2061 lval->Test &= ~E_CC;
2067 /* Left hand side is not constant. Get the value onto the stack. */
2068 exprhs (CF_NONE, k, lval); /* --> primary register */
2069 Mark = GetCodePos ();
2070 g_push (TypeOf (lval->Type), 0); /* --> stack */
2072 /* Evaluate the rhs */
2073 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2075 /* Right hand side is a constant. Get the rhs type */
2078 /* Remove pushed value from stack */
2080 pop (TypeOf (lval->Type));
2082 /* Check for pointer arithmetic */
2083 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2084 /* Left is pointer, right is int, must scale rhs */
2085 lval2.ConstVal *= CheckedPSizeOf (lhst);
2086 /* Operate on pointers, result type is a pointer */
2088 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2089 /* Left is int, right is pointer, must scale lhs (ptr only) */
2090 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2091 /* Operate on pointers, result type is a pointer */
2093 lval->Type = lval2.Type;
2094 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2095 /* Integer addition */
2096 flags = typeadjust (lval, &lval2, 1);
2099 Error ("Invalid operands for binary operator `+'");
2102 /* Generate code for the add */
2103 g_inc (flags | CF_CONST, lval2.ConstVal);
2105 /* Result is in primary register */
2106 lval->Flags = E_MEXPR;
2107 lval->Test &= ~E_CC;
2111 /* lhs and rhs are not constant. Get the rhs type. */
2114 /* Check for pointer arithmetic */
2115 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2116 /* Left is pointer, right is int, must scale rhs */
2117 g_scale (CF_INT, CheckedPSizeOf (lhst));
2118 /* Operate on pointers, result type is a pointer */
2120 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2121 /* Left is int, right is pointer, must scale lhs */
2122 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2123 g_swap (CF_INT); /* Swap TOS and primary */
2124 g_scale (CF_INT, CheckedPSizeOf (rhst));
2125 /* Operate on pointers, result type is a pointer */
2127 lval->Type = lval2.Type;
2128 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2129 /* Integer addition */
2130 flags = typeadjust (lval, &lval2, 0);
2133 Error ("Invalid operands for binary operator `+'");
2136 /* Generate code for the add */
2139 /* Result is in primary register */
2140 lval->Flags = E_MEXPR;
2141 lval->Test &= ~E_CC;
2150 static void parsesub (int k, ExprDesc* lval)
2151 /* Parse an expression with the binary minus operator. lval contains the
2152 * unprocessed left hand side of the expression and will contain the
2153 * result of the expression on return.
2157 unsigned flags; /* Operation flags */
2158 type* lhst; /* Type of left hand side */
2159 type* rhst; /* Type of right hand side */
2160 CodeMark Mark1; /* Save position of output queue */
2161 CodeMark Mark2; /* Another position in the queue */
2162 int rscale; /* Scale factor for the result */
2165 /* Skip the MINUS token */
2168 /* Get the left hand side type, initialize operation flags */
2171 rscale = 1; /* Scale by 1, that is, don't scale */
2173 /* Remember the output queue position, then bring the value onto the stack */
2174 Mark1 = GetCodePos ();
2175 exprhs (CF_NONE, k, lval); /* --> primary register */
2176 Mark2 = GetCodePos ();
2177 g_push (TypeOf (lhst), 0); /* --> stack */
2179 /* Parse the right hand side */
2180 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2182 /* The right hand side is constant. Get the rhs type. */
2185 /* Check left hand side */
2186 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
2188 /* Both sides are constant, remove generated code */
2190 pop (TypeOf (lhst)); /* Clean up the stack */
2192 /* Check for pointer arithmetic */
2193 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2194 /* Left is pointer, right is int, must scale rhs */
2195 lval->ConstVal -= lval2.ConstVal * CheckedPSizeOf (lhst);
2196 /* Operate on pointers, result type is a pointer */
2197 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2198 /* Left is pointer, right is pointer, must scale result */
2199 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2200 Error ("Incompatible pointer types");
2202 lval->ConstVal = (lval->ConstVal - lval2.ConstVal) /
2203 CheckedPSizeOf (lhst);
2205 /* Operate on pointers, result type is an integer */
2206 lval->Type = type_int;
2207 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2208 /* Integer subtraction */
2209 typeadjust (lval, &lval2, 1);
2210 lval->ConstVal -= lval2.ConstVal;
2213 Error ("Invalid operands for binary operator `-'");
2216 /* Result is constant, condition codes not set */
2217 /* lval->Flags = E_MCONST; ### */
2218 lval->Test &= ~E_CC;
2222 /* Left hand side is not constant, right hand side is.
2223 * Remove pushed value from stack.
2226 pop (TypeOf (lhst));
2228 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2229 /* Left is pointer, right is int, must scale rhs */
2230 lval2.ConstVal *= CheckedPSizeOf (lhst);
2231 /* Operate on pointers, result type is a pointer */
2233 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2234 /* Left is pointer, right is pointer, must scale result */
2235 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2236 Error ("Incompatible pointer types");
2238 rscale = CheckedPSizeOf (lhst);
2240 /* Operate on pointers, result type is an integer */
2242 lval->Type = type_int;
2243 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2244 /* Integer subtraction */
2245 flags = typeadjust (lval, &lval2, 1);
2248 Error ("Invalid operands for binary operator `-'");
2251 /* Do the subtraction */
2252 g_dec (flags | CF_CONST, lval2.ConstVal);
2254 /* If this was a pointer subtraction, we must scale the result */
2256 g_scale (flags, -rscale);
2259 /* Result is in primary register */
2260 lval->Flags = E_MEXPR;
2261 lval->Test &= ~E_CC;
2267 /* Right hand side is not constant. Get the rhs type. */
2270 /* Check for pointer arithmetic */
2271 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2272 /* Left is pointer, right is int, must scale rhs */
2273 g_scale (CF_INT, CheckedPSizeOf (lhst));
2274 /* Operate on pointers, result type is a pointer */
2276 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2277 /* Left is pointer, right is pointer, must scale result */
2278 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2279 Error ("Incompatible pointer types");
2281 rscale = CheckedPSizeOf (lhst);
2283 /* Operate on pointers, result type is an integer */
2285 lval->Type = type_int;
2286 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2287 /* Integer subtraction. If the left hand side descriptor says that
2288 * the lhs is const, we have to remove this mark, since this is no
2289 * longer true, lhs is on stack instead.
2291 if (lval->Flags == E_MCONST) {
2292 lval->Flags = E_MEXPR;
2294 /* Adjust operand types */
2295 flags = typeadjust (lval, &lval2, 0);
2298 Error ("Invalid operands for binary operator `-'");
2301 /* Generate code for the sub (the & is a hack here) */
2302 g_sub (flags & ~CF_CONST, 0);
2304 /* If this was a pointer subtraction, we must scale the result */
2306 g_scale (flags, -rscale);
2309 /* Result is in primary register */
2310 lval->Flags = E_MEXPR;
2311 lval->Test &= ~E_CC;
2317 static int hie8 (ExprDesc* lval)
2318 /* Process + and - binary operators. */
2320 int k = hie9 (lval);
2321 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2323 if (CurTok.Tok == TOK_PLUS) {
2336 static int hie7 (ExprDesc *lval)
2337 /* Parse << and >>. */
2339 static const GenDesc* hie7_ops [] = {
2344 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2349 static int hie6 (ExprDesc *lval)
2350 /* process greater-than type comparators */
2352 static const GenDesc* hie6_ops [] = {
2353 &GenLT, &GenLE, &GenGE, &GenGT, 0
2355 return hie_compare (hie6_ops, lval, hie7);
2360 static int hie5 (ExprDesc *lval)
2362 static const GenDesc* hie5_ops[] = {
2365 return hie_compare (hie5_ops, lval, hie6);
2370 static int hie4 (ExprDesc* lval)
2371 /* Handle & (bitwise and) */
2373 static const GenDesc* hie4_ops [] = {
2378 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2383 static int hie3 (ExprDesc *lval)
2384 /* Handle ^ (bitwise exclusive or) */
2386 static const GenDesc* hie3_ops [] = {
2391 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2396 static int hie2 (ExprDesc *lval)
2397 /* Handle | (bitwise or) */
2399 static const GenDesc* hie2_ops [] = {
2404 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2409 static int hieAndPP (ExprDesc* lval)
2410 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2411 * called recursively from the preprocessor.
2416 ConstSubExpr (hie2, lval);
2417 while (CurTok.Tok == TOK_BOOL_AND) {
2419 /* Left hand side must be an int */
2420 if (!IsClassInt (lval->Type)) {
2421 Error ("Left hand side must be of integer type");
2422 MakeConstIntExpr (lval, 1);
2429 ConstSubExpr (hie2, &lval2);
2431 /* Since we are in PP mode, all we know about is integers */
2432 if (!IsClassInt (lval2.Type)) {
2433 Error ("Right hand side must be of integer type");
2434 MakeConstIntExpr (&lval2, 1);
2437 /* Combine the two */
2438 lval->ConstVal = (lval->ConstVal && lval2.ConstVal);
2441 /* Always a rvalue */
2447 static int hieOrPP (ExprDesc *lval)
2448 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2449 * called recursively from the preprocessor.
2454 ConstSubExpr (hieAndPP, lval);
2455 while (CurTok.Tok == TOK_BOOL_OR) {
2457 /* Left hand side must be an int */
2458 if (!IsClassInt (lval->Type)) {
2459 Error ("Left hand side must be of integer type");
2460 MakeConstIntExpr (lval, 1);
2467 ConstSubExpr (hieAndPP, &lval2);
2469 /* Since we are in PP mode, all we know about is integers */
2470 if (!IsClassInt (lval2.Type)) {
2471 Error ("Right hand side must be of integer type");
2472 MakeConstIntExpr (&lval2, 1);
2475 /* Combine the two */
2476 lval->ConstVal = (lval->ConstVal || lval2.ConstVal);
2479 /* Always a rvalue */
2485 static int hieAnd (ExprDesc* lval, unsigned TrueLab, int* BoolOp)
2486 /* Process "exp && exp" */
2493 if (CurTok.Tok == TOK_BOOL_AND) {
2495 /* Tell our caller that we're evaluating a boolean */
2498 /* Get a label that we will use for false expressions */
2499 lab = GetLocalLabel ();
2501 /* If the expr hasn't set condition codes, set the force-test flag */
2502 if ((lval->Test & E_CC) == 0) {
2503 lval->Test |= E_FORCETEST;
2506 /* Load the value */
2507 exprhs (CF_FORCECHAR, k, lval);
2509 /* Generate the jump */
2510 g_falsejump (CF_NONE, lab);
2512 /* Parse more boolean and's */
2513 while (CurTok.Tok == TOK_BOOL_AND) {
2520 if ((lval2.Test & E_CC) == 0) {
2521 lval2.Test |= E_FORCETEST;
2523 exprhs (CF_FORCECHAR, k, &lval2);
2525 /* Do short circuit evaluation */
2526 if (CurTok.Tok == TOK_BOOL_AND) {
2527 g_falsejump (CF_NONE, lab);
2529 /* Last expression - will evaluate to true */
2530 g_truejump (CF_NONE, TrueLab);
2534 /* Define the false jump label here */
2535 g_defcodelabel (lab);
2537 /* Define the label */
2538 lval->Flags = E_MEXPR;
2539 lval->Test |= E_CC; /* Condition codes are set */
2547 static int hieOr (ExprDesc *lval)
2548 /* Process "exp || exp". */
2552 int BoolOp = 0; /* Did we have a boolean op? */
2553 int AndOp; /* Did we have a && operation? */
2554 unsigned TrueLab; /* Jump to this label if true */
2558 TrueLab = GetLocalLabel ();
2560 /* Call the next level parser */
2561 k = hieAnd (lval, TrueLab, &BoolOp);
2563 /* Any boolean or's? */
2564 if (CurTok.Tok == TOK_BOOL_OR) {
2566 /* If the expr hasn't set condition codes, set the force-test flag */
2567 if ((lval->Test & E_CC) == 0) {
2568 lval->Test |= E_FORCETEST;
2571 /* Get first expr */
2572 exprhs (CF_FORCECHAR, k, lval);
2574 /* For each expression jump to TrueLab if true. Beware: If we
2575 * had && operators, the jump is already in place!
2578 g_truejump (CF_NONE, TrueLab);
2581 /* Remember that we had a boolean op */
2584 /* while there's more expr */
2585 while (CurTok.Tok == TOK_BOOL_OR) {
2592 k = hieAnd (&lval2, TrueLab, &AndOp);
2593 if ((lval2.Test & E_CC) == 0) {
2594 lval2.Test |= E_FORCETEST;
2596 exprhs (CF_FORCECHAR, k, &lval2);
2598 /* If there is more to come, add shortcut boolean eval. */
2599 g_truejump (CF_NONE, TrueLab);
2602 lval->Flags = E_MEXPR;
2603 lval->Test |= E_CC; /* Condition codes are set */
2607 /* If we really had boolean ops, generate the end sequence */
2609 DoneLab = GetLocalLabel ();
2610 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2611 g_falsejump (CF_NONE, DoneLab);
2612 g_defcodelabel (TrueLab);
2613 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2614 g_defcodelabel (DoneLab);
2621 static int hieQuest (ExprDesc *lval)
2622 /* Parse "lvalue ? exp : exp" */
2627 ExprDesc lval2; /* Expression 2 */
2628 ExprDesc lval3; /* Expression 3 */
2629 type* type2; /* Type of expression 2 */
2630 type* type3; /* Type of expression 3 */
2631 type* rtype; /* Type of result */
2632 CodeMark Mark1; /* Save position in output code */
2633 CodeMark Mark2; /* Save position in output code */
2637 k = Preprocessing? hieOrPP (lval) : hieOr (lval);
2638 if (CurTok.Tok == TOK_QUEST) {
2640 if ((lval->Test & E_CC) == 0) {
2641 /* Condition codes not set, force a test */
2642 lval->Test |= E_FORCETEST;
2644 exprhs (CF_NONE, k, lval);
2645 labf = GetLocalLabel ();
2646 g_falsejump (CF_NONE, labf);
2648 /* Parse second expression */
2649 k = expr (hie1, &lval2);
2651 if (!IsTypeVoid (lval2.Type)) {
2652 /* Load it into the primary */
2653 exprhs (CF_NONE, k, &lval2);
2655 labt = GetLocalLabel ();
2659 /* Parse the third expression */
2660 g_defcodelabel (labf);
2661 k = expr (hie1, &lval3);
2663 if (!IsTypeVoid (lval3.Type)) {
2664 /* Load it into the primary */
2665 exprhs (CF_NONE, k, &lval3);
2668 /* Check if any conversions are needed, if so, do them.
2669 * Conversion rules for ?: expression are:
2670 * - if both expressions are int expressions, default promotion
2671 * rules for ints apply.
2672 * - if both expressions are pointers of the same type, the
2673 * result of the expression is of this type.
2674 * - if one of the expressions is a pointer and the other is
2675 * a zero constant, the resulting type is that of the pointer
2677 * - if both expressions are void expressions, the result is of
2679 * - all other cases are flagged by an error.
2681 if (IsClassInt (type2) && IsClassInt (type3)) {
2683 /* Get common type */
2684 rtype = promoteint (type2, type3);
2686 /* Convert the third expression to this type if needed */
2687 g_typecast (TypeOf (rtype), TypeOf (type3));
2689 /* Setup a new label so that the expr3 code will jump around
2690 * the type cast code for expr2.
2692 labf = GetLocalLabel (); /* Get new label */
2693 Mark1 = GetCodePos (); /* Remember current position */
2694 g_jump (labf); /* Jump around code */
2696 /* The jump for expr2 goes here */
2697 g_defcodelabel (labt);
2699 /* Create the typecast code for expr2 */
2700 Mark2 = GetCodePos (); /* Remember position */
2701 g_typecast (TypeOf (rtype), TypeOf (type2));
2703 /* Jump here around the typecase code. */
2704 g_defcodelabel (labf);
2705 labt = 0; /* Mark other label as invalid */
2707 } else if (IsClassPtr (type2) && IsClassPtr (type3)) {
2708 /* Must point to same type */
2709 if (TypeCmp (Indirect (type2), Indirect (type3)) < TC_EQUAL) {
2710 Error ("Incompatible pointer types");
2712 /* Result has the common type */
2714 } else if (IsClassPtr (type2) && IsNullPtr (&lval3)) {
2715 /* Result type is pointer, no cast needed */
2717 } else if (IsNullPtr (&lval2) && IsClassPtr (type3)) {
2718 /* Result type is pointer, no cast needed */
2720 } else if (IsTypeVoid (type2) && IsTypeVoid (type3)) {
2721 /* Result type is void */
2724 Error ("Incompatible types");
2725 rtype = lval2.Type; /* Doesn't matter here */
2728 /* If we don't have the label defined until now, do it */
2730 g_defcodelabel (labt);
2733 /* Setup the target expression */
2734 lval->Flags = E_MEXPR;
2743 static void opeq (const GenDesc* Gen, ExprDesc *lval, int k)
2744 /* Process "op=" operators. */
2753 Error ("Invalid lvalue in assignment");
2757 /* Determine the type of the lhs */
2758 flags = TypeOf (lval->Type);
2759 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2760 lval->Type [0] == T_PTR;
2762 /* Get the lhs address on stack (if needed) */
2765 /* Fetch the lhs into the primary register if needed */
2766 exprhs (CF_NONE, k, lval);
2768 /* Bring the lhs on stack */
2769 Mark = GetCodePos ();
2772 /* Evaluate the rhs */
2773 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2774 /* The resulting value is a constant. If the generator has the NOPUSH
2775 * flag set, don't push the lhs.
2777 if (Gen->Flags & GEN_NOPUSH) {
2782 /* lhs is a pointer, scale rhs */
2783 lval2.ConstVal *= CheckedSizeOf (lval->Type+1);
2786 /* If the lhs is character sized, the operation may be later done
2789 if (CheckedSizeOf (lval->Type) == 1) {
2790 flags |= CF_FORCECHAR;
2793 /* Special handling for add and sub - some sort of a hack, but short code */
2794 if (Gen->Func == g_add) {
2795 g_inc (flags | CF_CONST, lval2.ConstVal);
2796 } else if (Gen->Func == g_sub) {
2797 g_dec (flags | CF_CONST, lval2.ConstVal);
2799 Gen->Func (flags | CF_CONST, lval2.ConstVal);
2802 /* rhs is not constant and already in the primary register */
2804 /* lhs is a pointer, scale rhs */
2805 g_scale (TypeOf (lval2.Type), CheckedSizeOf (lval->Type+1));
2808 /* If the lhs is character sized, the operation may be later done
2811 if (CheckedSizeOf (lval->Type) == 1) {
2812 flags |= CF_FORCECHAR;
2815 /* Adjust the types of the operands if needed */
2816 Gen->Func (g_typeadjust (flags, TypeOf (lval2.Type)), 0);
2819 lval->Flags = E_MEXPR;
2824 static void addsubeq (const GenDesc* Gen, ExprDesc *lval, int k)
2825 /* Process the += and -= operators */
2833 /* We must have an lvalue */
2835 Error ("Invalid lvalue in assignment");
2839 /* We're currently only able to handle some adressing modes */
2840 if ((lval->Flags & E_MGLOBAL) == 0 && /* Global address? */
2841 (lval->Flags & E_MLOCAL) == 0 && /* Local address? */
2842 (lval->Flags & E_MCONST) == 0) { /* Constant address? */
2843 /* Use generic routine */
2844 opeq (Gen, lval, k);
2848 /* Skip the operator */
2851 /* Check if we have a pointer expression and must scale rhs */
2852 MustScale = (lval->Type [0] == T_PTR);
2854 /* Initialize the code generator flags */
2858 /* Evaluate the rhs */
2859 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2860 /* The resulting value is a constant. */
2862 /* lhs is a pointer, scale rhs */
2863 lval2.ConstVal *= CheckedSizeOf (lval->Type+1);
2868 /* rhs is not constant and already in the primary register */
2870 /* lhs is a pointer, scale rhs */
2871 g_scale (TypeOf (lval2.Type), CheckedSizeOf (lval->Type+1));
2875 /* Setup the code generator flags */
2876 lflags |= TypeOf (lval->Type) | CF_FORCECHAR;
2877 rflags |= TypeOf (lval2.Type);
2879 /* Cast the rhs to the type of the lhs */
2880 g_typecast (lflags, rflags);
2882 /* Output apropriate code */
2883 if (lval->Flags & E_MGLOBAL) {
2884 /* Static variable */
2885 lflags |= GlobalModeFlags (lval->Flags);
2886 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2887 g_addeqstatic (lflags, lval->Name, lval->ConstVal, lval2.ConstVal);
2889 g_subeqstatic (lflags, lval->Name, lval->ConstVal, lval2.ConstVal);
2891 } else if (lval->Flags & E_MLOCAL) {
2892 /* ref to localvar */
2893 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2894 g_addeqlocal (lflags, lval->ConstVal, lval2.ConstVal);
2896 g_subeqlocal (lflags, lval->ConstVal, lval2.ConstVal);
2898 } else if (lval->Flags & E_MCONST) {
2899 /* ref to absolute address */
2900 lflags |= CF_ABSOLUTE;
2901 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2902 g_addeqstatic (lflags, lval->ConstVal, 0, lval2.ConstVal);
2904 g_subeqstatic (lflags, lval->ConstVal, 0, lval2.ConstVal);
2906 } else if (lval->Flags & E_MEXPR) {
2907 /* Address in a/x. */
2908 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2909 g_addeqind (lflags, lval->ConstVal, lval2.ConstVal);
2911 g_subeqind (lflags, lval->ConstVal, lval2.ConstVal);
2914 Internal ("Invalid addressing mode");
2917 /* Expression is in the primary now */
2918 lval->Flags = E_MEXPR;
2923 static void Assignment (ExprDesc* lval)
2924 /* Parse an assignment */
2929 type* ltype = lval->Type;
2931 /* Check for assignment to const */
2932 if (IsQualConst (ltype)) {
2933 Error ("Assignment to const");
2936 /* cc65 does not have full support for handling structs by value. Since
2937 * assigning structs is one of the more useful operations from this
2938 * family, allow it here.
2940 if (IsClassStruct (ltype)) {
2942 /* Bring the address of the lhs into the primary and push it */
2943 exprhs (0, 0, lval);
2944 g_push (CF_PTR | CF_UNSIGNED, 0);
2946 /* Get the expression on the right of the '=' into the primary */
2949 /* Get the address */
2950 exprhs (0, 0, &lval2);
2953 /* Push the address (or whatever is in ax in case of errors) */
2954 g_push (CF_PTR | CF_UNSIGNED, 0);
2956 /* Check for equality of the structs */
2957 if (TypeCmp (ltype, lval2.Type) < TC_STRICT_COMPATIBLE) {
2958 Error ("Incompatible types");
2961 /* Load the size of the struct into the primary */
2962 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, CheckedSizeOf (ltype), 0);
2964 /* Call the memcpy function */
2965 g_call (CF_FIXARGC, "memcpy", 4);
2969 /* Get the address on stack if needed */
2972 /* No struct, setup flags for the load */
2974 /* Generates wrong code!!! ### */
2975 flags = CheckedSizeOf (ltype) == 1? CF_FORCECHAR : CF_NONE;
2980 /* Get the expression on the right of the '=' into the primary */
2981 if (evalexpr (flags, hie1, &lval2) == 0) {
2982 /* Constant expression. Adjust the types */
2983 assignadjust (ltype, &lval2);
2984 /* Put the value into the primary register */
2985 lconst (flags, &lval2);
2987 /* Expression is not constant and already in the primary */
2988 assignadjust (ltype, &lval2);
2991 /* Generate a store instruction */
2996 /* Value is still in primary */
2997 lval->Flags = E_MEXPR;
3002 int hie1 (ExprDesc* lval)
3003 /* Parse first level of expression hierarchy. */
3007 k = hieQuest (lval);
3008 switch (CurTok.Tok) {
3017 Error ("Invalid lvalue in assignment");
3023 case TOK_PLUS_ASSIGN:
3024 addsubeq (&GenPASGN, lval, k);
3027 case TOK_MINUS_ASSIGN:
3028 addsubeq (&GenSASGN, lval, k);
3031 case TOK_MUL_ASSIGN:
3032 opeq (&GenMASGN, lval, k);
3035 case TOK_DIV_ASSIGN:
3036 opeq (&GenDASGN, lval, k);
3039 case TOK_MOD_ASSIGN:
3040 opeq (&GenMOASGN, lval, k);
3043 case TOK_SHL_ASSIGN:
3044 opeq (&GenSLASGN, lval, k);
3047 case TOK_SHR_ASSIGN:
3048 opeq (&GenSRASGN, lval, k);
3051 case TOK_AND_ASSIGN:
3052 opeq (&GenAASGN, lval, k);
3055 case TOK_XOR_ASSIGN:
3056 opeq (&GenXOASGN, lval, k);
3060 opeq (&GenOASGN, lval, k);
3071 int hie0 (ExprDesc *lval)
3072 /* Parse comma operator. */
3077 while (CurTok.Tok == TOK_COMMA) {
3086 int evalexpr (unsigned flags, int (*f) (ExprDesc*), ExprDesc* lval)
3087 /* Will evaluate an expression via the given function. If the result is a
3088 * constant, 0 is returned and the value is put in the lval struct. If the
3089 * result is not constant, exprhs is called to bring the value into the
3090 * primary register and 1 is returned.
3097 if (k == 0 && lval->Flags == E_MCONST) {
3098 /* Constant expression */
3101 /* Not constant, load into the primary */
3102 exprhs (flags, k, lval);
3109 static int expr (int (*func) (ExprDesc*), ExprDesc *lval)
3110 /* Expression parser; func is either hie0 or hie1. */
3119 /* Do some checks if code generation is still constistent */
3120 if (savsp != oursp) {
3122 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
3124 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
3132 void expression1 (ExprDesc* lval)
3133 /* Evaluate an expression on level 1 (no comma operator) and put it into
3134 * the primary register
3137 memset (lval, 0, sizeof (*lval));
3138 exprhs (CF_NONE, expr (hie1, lval), lval);
3143 void expression (ExprDesc* lval)
3144 /* Evaluate an expression and put it into the primary register */
3146 memset (lval, 0, sizeof (*lval));
3147 exprhs (CF_NONE, expr (hie0, lval), lval);
3152 void ConstExpr (ExprDesc* lval)
3153 /* Get a constant value */
3155 memset (lval, 0, sizeof (*lval));
3156 if (expr (hie1, lval) != 0 || (lval->Flags & E_MCONST) == 0) {
3157 Error ("Constant expression expected");
3158 /* To avoid any compiler errors, make the expression a valid const */
3159 MakeConstIntExpr (lval, 1);
3165 void ConstIntExpr (ExprDesc* Val)
3166 /* Get a constant int value */
3168 memset (Val, 0, sizeof (*Val));
3169 if (expr (hie1, Val) != 0 ||
3170 (Val->Flags & E_MCONST) == 0 ||
3171 !IsClassInt (Val->Type)) {
3172 Error ("Constant integer expression expected");
3173 /* To avoid any compiler errors, make the expression a valid const */
3174 MakeConstIntExpr (Val, 1);
3180 void intexpr (ExprDesc* lval)
3181 /* Get an integer expression */
3184 if (!IsClassInt (lval->Type)) {
3185 Error ("Integer expression expected");
3186 /* To avoid any compiler errors, make the expression a valid int */
3187 MakeConstIntExpr (lval, 1);
3193 void boolexpr (ExprDesc* lval)
3194 /* Get a boolean expression */
3196 /* Read an expression */
3199 /* If it's an integer, it's ok. If it's not an integer, but a pointer,
3200 * the pointer used in a boolean context is also ok
3202 if (!IsClassInt (lval->Type) && !IsClassPtr (lval->Type)) {
3203 Error ("Boolean expression expected");
3204 /* To avoid any compiler errors, make the expression a valid int */
3205 MakeConstIntExpr (lval, 1);
3211 void test (unsigned label, int cond)
3212 /* Generate code to perform test and jump if false. */
3217 /* Eat the parenthesis */
3220 /* Prepare the expression, setup labels */
3221 memset (&lval, 0, sizeof (lval));
3223 /* Generate code to eval the expr */
3224 k = expr (hie0, &lval);
3225 if (k == 0 && lval.Flags == E_MCONST) {
3226 /* Constant rvalue */
3227 if (cond == 0 && lval.ConstVal == 0) {
3229 Warning ("Unreachable code");
3230 } else if (cond && lval.ConstVal) {
3237 /* If the expr hasn't set condition codes, set the force-test flag */
3238 if ((lval.Test & E_CC) == 0) {
3239 lval.Test |= E_FORCETEST;
3242 /* Load the value into the primary register */
3243 exprhs (CF_FORCECHAR, k, &lval);
3245 /* Generate the jump */
3247 g_truejump (CF_NONE, label);
3249 /* Special case (putting this here is a small hack - but hey, the
3250 * compiler itself is one big hack...): If a semicolon follows, we
3251 * don't have a statement and may omit the jump.
3253 if (CurTok.Tok != TOK_SEMI) {
3254 g_falsejump (CF_NONE, label);
3258 /* Check for the closing brace */