4 * Ullrich von Bassewitz, 21.06.1998
14 #include "debugflag.h"
21 #include "assignment.h"
40 /*****************************************************************************/
42 /*****************************************************************************/
46 /* Generator attributes */
47 #define GEN_NOPUSH 0x01 /* Don't push lhs */
49 /* Map a generator function and its attributes to a token */
51 token_t Tok; /* Token to map to */
52 unsigned Flags; /* Flags for generator function */
53 void (*Func) (unsigned, unsigned long); /* Generator func */
56 /* Descriptors for the operations */
57 static GenDesc 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 hie0 (ExprDesc *lval);
91 /* Parse comma operator. */
93 static int expr (int (*func) (ExprDesc*), ExprDesc *lval);
94 /* Expression parser; func is either hie0 or hie1. */
98 /*****************************************************************************/
99 /* Helper functions */
100 /*****************************************************************************/
104 static unsigned GlobalModeFlags (unsigned flags)
105 /* Return the addressing mode flags for the variable with the given flags */
108 if (flags == E_TGLAB) {
109 /* External linkage */
111 } else if (flags == E_TREGISTER) {
112 /* Register variable */
122 static int IsNullPtr (ExprDesc* lval)
123 /* Return true if this is the NULL pointer constant */
125 return (IsClassInt (lval->Type) && /* Is it an int? */
126 lval->Flags == E_MCONST && /* Is it constant? */
127 lval->ConstVal == 0); /* And is it's value zero? */
132 static type* promoteint (type* lhst, type* rhst)
133 /* In an expression with two ints, return the type of the result */
135 /* Rules for integer types:
136 * - If one of the values is a long, the result is long.
137 * - If one of the values is unsigned, the result is also unsigned.
138 * - Otherwise the result is an int.
140 if (IsTypeLong (lhst) || IsTypeLong (rhst)) {
141 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
147 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
157 static unsigned typeadjust (ExprDesc* lhs, ExprDesc* rhs, int NoPush)
158 /* Adjust the two values for a binary operation. lhs is expected on stack or
159 * to be constant, rhs is expected to be in the primary register or constant.
160 * The function will put the type of the result into lhs and return the
161 * code generator flags for the operation.
162 * If NoPush is given, it is assumed that the operation does not expect the lhs
163 * to be on stack, and that lhs is in a register instead.
164 * Beware: The function does only accept int types.
167 unsigned ltype, rtype;
170 /* Get the type strings */
171 type* lhst = lhs->Type;
172 type* rhst = rhs->Type;
174 /* Generate type adjustment code if needed */
175 ltype = TypeOf (lhst);
176 if (lhs->Flags == E_MCONST) {
180 /* Value is in primary register*/
183 rtype = TypeOf (rhst);
184 if (rhs->Flags == E_MCONST) {
187 flags = g_typeadjust (ltype, rtype);
189 /* Set the type of the result */
190 lhs->Type = promoteint (lhst, rhst);
192 /* Return the code generator flags */
198 unsigned assignadjust (type* lhst, ExprDesc* rhs)
199 /* Adjust the type of the right hand expression so that it can be assigned to
200 * the type on the left hand side. This function is used for assignment and
201 * for converting parameters in a function call. It returns the code generator
202 * flags for the operation. The type string of the right hand side will be
203 * set to the type of the left hand side.
206 /* Get the type of the right hand side. Treat function types as
207 * pointer-to-function
209 type* rhst = rhs->Type;
210 if (IsTypeFunc (rhst)) {
211 rhst = PointerTo (rhst);
214 /* After calling this function, rhs will have the type of the lhs */
217 /* First, do some type checking */
218 if (IsTypeVoid (lhst) || IsTypeVoid (rhst)) {
219 /* If one of the sides are of type void, output a more apropriate
222 Error ("Illegal type");
223 } else if (IsClassInt (lhst)) {
224 if (IsClassPtr (rhst)) {
225 /* Pointer -> int conversion */
226 Warning ("Converting pointer to integer without a cast");
227 } else if (!IsClassInt (rhst)) {
228 Error ("Incompatible types");
230 /* Convert the rhs to the type of the lhs. */
231 unsigned flags = TypeOf (rhst);
232 if (rhs->Flags == E_MCONST) {
235 return g_typecast (TypeOf (lhst), flags);
237 } else if (IsClassPtr (lhst)) {
238 if (IsClassPtr (rhst)) {
239 /* Pointer to pointer assignment is valid, if:
240 * - both point to the same types, or
241 * - the rhs pointer is a void pointer, or
242 * - the lhs pointer is a void pointer.
244 if (!IsTypeVoid (Indirect (lhst)) && !IsTypeVoid (Indirect (rhst))) {
245 /* Compare the types */
246 switch (TypeCmp (lhst, rhst)) {
248 case TC_INCOMPATIBLE:
249 Error ("Incompatible pointer types");
253 Error ("Pointer types differ in type qualifiers");
261 } else if (IsClassInt (rhst)) {
262 /* Int to pointer assignment is valid only for constant zero */
263 if (rhs->Flags != E_MCONST || rhs->ConstVal != 0) {
264 Warning ("Converting integer to pointer without a cast");
266 } else if (IsTypeFuncPtr (lhst) && IsTypeFunc(rhst)) {
267 /* Assignment of function to function pointer is allowed, provided
268 * that both functions have the same parameter list.
270 if (TypeCmp (Indirect (lhst), rhst) < TC_EQUAL) {
271 Error ("Incompatible types");
274 Error ("Incompatible types");
277 Error ("Incompatible types");
280 /* Return an int value in all cases where the operands are not both ints */
286 void DefineData (ExprDesc* Expr)
287 /* Output a data definition for the given expression */
289 unsigned Flags = Expr->Flags;
291 switch (Flags & E_MCTYPE) {
295 g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->ConstVal, 0);
299 /* Register variable. Taking the address is usually not
302 if (!AllowRegVarAddr) {
303 Error ("Cannot take the address of a register variable");
309 /* Local or global symbol */
310 g_defdata (GlobalModeFlags (Flags), Expr->Name, Expr->ConstVal);
314 /* a literal of some kind */
315 g_defdata (CF_STATIC, LiteralPoolLabel, Expr->ConstVal);
319 Internal ("Unknown constant type: %04X", Flags);
325 static void LoadConstant (unsigned Flags, ExprDesc* Expr)
326 /* Load the primary register with some constant value. */
328 switch (Expr->Flags & E_MCTYPE) {
331 g_leasp (Expr->ConstVal);
335 /* Number constant */
336 g_getimmed (Flags | TypeOf (Expr->Type) | CF_CONST, Expr->ConstVal, 0);
340 /* Register variable. Taking the address is usually not
343 if (!AllowRegVarAddr) {
344 Error ("Cannot take the address of a register variable");
350 /* Local or global symbol, load address */
351 Flags |= GlobalModeFlags (Expr->Flags);
353 g_getimmed (Flags, Expr->Name, Expr->ConstVal);
358 g_getimmed (CF_STATIC, LiteralPoolLabel, Expr->ConstVal);
362 Internal ("Unknown constant type: %04X", Expr->Flags);
368 static int kcalc (token_t tok, long val1, long val2)
369 /* Calculate an operation with left and right operand constant. */
373 return (val1 == val2);
375 return (val1 != val2);
377 return (val1 < val2);
379 return (val1 <= val2);
381 return (val1 >= val2);
383 return (val1 > val2);
385 return (val1 | val2);
387 return (val1 ^ val2);
389 return (val1 & val2);
391 return (val1 >> val2);
393 return (val1 << val2);
395 return (val1 * val2);
398 Error ("Division by zero");
401 return (val1 / val2);
404 Error ("Modulo operation with zero");
407 return (val1 % val2);
409 Internal ("kcalc: got token 0x%X\n", tok);
416 static const GenDesc* FindGen (token_t Tok, const GenDesc** Table)
417 /* Find a token in a generator table */
420 while ((G = *Table) != 0) {
431 static int istypeexpr (void)
432 /* Return true if some sort of variable or type is waiting (helper for cast
433 * and sizeof() in hie10).
438 return CurTok.Tok == TOK_LPAREN && (
439 (NextTok.Tok >= TOK_FIRSTTYPE && NextTok.Tok <= TOK_LASTTYPE) ||
440 (NextTok.Tok == TOK_CONST) ||
441 (NextTok.Tok == TOK_IDENT &&
442 (Entry = FindSym (NextTok.Ident)) != 0 &&
443 SymIsTypeDef (Entry)));
448 void PushAddr (ExprDesc* lval)
449 /* If the expression contains an address that was somehow evaluated,
450 * push this address on the stack. This is a helper function for all
451 * sorts of implicit or explicit assignment functions where the lvalue
452 * must be saved if it's not constant, before evaluating the rhs.
455 /* Get the address on stack if needed */
456 if (lval->Flags != E_MREG && (lval->Flags & E_MEXPR)) {
457 /* Push the address (always a pointer) */
464 void ConstSubExpr (int (*F) (ExprDesc*), ExprDesc* Expr)
465 /* Will evaluate an expression via the given function. If the result is not
466 * a constant, a diagnostic will be printed, and the value is replaced by
467 * a constant one to make sure there are no internal errors that result
468 * from this input error.
472 if (F (Expr) != 0 || Expr->Flags != E_MCONST) {
473 Error ("Constant expression expected");
474 /* To avoid any compiler errors, make the expression a valid const */
475 MakeConstIntExpr (Expr, 1);
481 void CheckBoolExpr (ExprDesc* lval)
482 /* Check if the given expression is a boolean expression, output a diagnostic
486 /* If it's an integer, it's ok. If it's not an integer, but a pointer,
487 * the pointer used in a boolean context is also ok
489 if (!IsClassInt (lval->Type) && !IsClassPtr (lval->Type)) {
490 Error ("Boolean expression expected");
491 /* To avoid any compiler errors, make the expression a valid int */
492 MakeConstIntExpr (lval, 1);
498 /*****************************************************************************/
500 /*****************************************************************************/
504 void exprhs (unsigned flags, int k, ExprDesc* lval)
505 /* Put the result of an expression into the primary register */
511 /* Dereferenced lvalue */
512 flags |= TypeOf (lval->Type);
513 if (lval->Test & E_FORCETEST) {
515 lval->Test &= ~E_FORCETEST;
517 if (f & E_MGLOBAL) { /* ref to globalvar */
519 flags |= GlobalModeFlags (f);
520 g_getstatic (flags, lval->Name, lval->ConstVal);
521 } else if (f & E_MLOCAL) {
522 /* ref to localvar */
523 g_getlocal (flags, lval->ConstVal);
524 } else if (f & E_MCONST) {
525 /* ref to absolute address */
526 g_getstatic (flags | CF_ABSOLUTE, lval->ConstVal, 0);
527 } else if (f == E_MEOFFS) {
528 g_getind (flags, lval->ConstVal);
529 } else if (f != E_MREG) {
532 } else if (f == E_MEOFFS) {
533 /* reference not storable */
534 flags |= TypeOf (lval->Type);
535 g_inc (flags | CF_CONST, lval->ConstVal);
536 } else if ((f & E_MEXPR) == 0) {
537 /* Constant of some sort, load it into the primary */
538 LoadConstant (flags, lval);
540 /* Are we testing this value? */
541 if (lval->Test & E_FORCETEST) {
542 /* Yes, force a test */
543 flags |= TypeOf (lval->Type);
545 lval->Test &= ~E_FORCETEST;
551 static unsigned FunctionParamList (FuncDesc* Func)
552 /* Parse a function parameter list and pass the parameters to the called
553 * function. Depending on several criteria this may be done by just pushing
554 * each parameter separately, or creating the parameter frame once and then
555 * storing into this frame.
556 * The function returns the size of the parameters pushed.
561 /* Initialize variables */
562 SymEntry* Param = 0; /* Keep gcc silent */
563 unsigned ParamSize = 0; /* Size of parameters pushed */
564 unsigned ParamCount = 0; /* Number of parameters pushed */
565 unsigned FrameSize = 0; /* Size of parameter frame */
566 unsigned FrameParams = 0; /* Number of params in frame */
567 int FrameOffs = 0; /* Offset into parameter frame */
568 int Ellipsis = 0; /* Function is variadic */
570 /* As an optimization, we may allocate the complete parameter frame at
571 * once instead of pushing each parameter as it comes. We may do that,
574 * - optimizations that increase code size are enabled (allocating the
575 * stack frame at once gives usually larger code).
576 * - we have more than one parameter to push (don't count the last param
577 * for __fastcall__ functions).
579 if (CodeSizeFactor >= 200) {
581 /* Calculate the number and size of the parameters */
582 FrameParams = Func->ParamCount;
583 FrameSize = Func->ParamSize;
584 if (FrameParams > 0 && (Func->Flags & FD_FASTCALL) != 0) {
585 /* Last parameter is not pushed */
586 const SymEntry* LastParam = Func->SymTab->SymTail;
587 FrameSize -= CheckedSizeOf (LastParam->Type);
591 /* Do we have more than one parameter in the frame? */
592 if (FrameParams > 1) {
593 /* Okeydokey, setup the frame */
598 /* Don't use a preallocated frame */
603 /* Parse the actual parameter list */
604 while (CurTok.Tok != TOK_RPAREN) {
609 /* Count arguments */
612 /* Fetch the pointer to the next argument, check for too many args */
613 if (ParamCount <= Func->ParamCount) {
614 /* Beware: If there are parameters with identical names, they
615 * cannot go into the same symbol table, which means that in this
616 * case of errorneous input, the number of nodes in the symbol
617 * table and ParamCount are NOT equal. We have to handle this case
618 * below to avoid segmentation violations. Since we know that this
619 * problem can only occur if there is more than one parameter,
620 * we will just use the last one.
622 if (ParamCount == 1) {
624 Param = Func->SymTab->SymHead;
625 } else if (Param->NextSym != 0) {
627 Param = Param->NextSym;
628 CHECK ((Param->Flags & SC_PARAM) != 0);
630 } else if (!Ellipsis) {
631 /* Too many arguments. Do we have an open param list? */
632 if ((Func->Flags & FD_VARIADIC) == 0) {
633 /* End of param list reached, no ellipsis */
634 Error ("Too many arguments in function call");
636 /* Assume an ellipsis even in case of errors to avoid an error
637 * message for each other argument.
642 /* Do some optimization: If we have a constant value to push,
643 * use a special function that may optimize.
646 if (!Ellipsis && CheckedSizeOf (Param->Type) == 1) {
647 CFlags = CF_FORCECHAR;
650 if (evalexpr (CFlags, hie1, &lval) == 0) {
651 /* A constant value */
655 /* If we don't have an argument spec, accept anything, otherwise
656 * convert the actual argument to the type needed.
659 /* Promote the argument if needed */
660 assignadjust (Param->Type, &lval);
662 /* If we have a prototype, chars may be pushed as chars */
663 Flags |= CF_FORCECHAR;
666 /* Use the type of the argument for the push */
667 Flags |= TypeOf (lval.Type);
669 /* If this is a fastcall function, don't push the last argument */
670 if (ParamCount == Func->ParamCount && (Func->Flags & FD_FASTCALL) != 0) {
671 /* Just load the argument into the primary. This is only needed if
672 * we have a constant argument, otherwise the value is already in
675 if (Flags & CF_CONST) {
676 exprhs (CF_FORCECHAR, 0, &lval);
679 unsigned ArgSize = sizeofarg (Flags);
681 /* We have the space already allocated, store in the frame */
682 CHECK (FrameSize >= ArgSize);
683 FrameSize -= ArgSize;
684 FrameOffs -= ArgSize;
686 g_putlocal (Flags | CF_NOKEEP, FrameOffs, lval.ConstVal);
688 /* Push the argument */
689 g_push (Flags, lval.ConstVal);
692 /* Calculate total parameter size */
693 ParamSize += ArgSize;
696 /* Check for end of argument list */
697 if (CurTok.Tok != TOK_COMMA) {
703 /* Check if we had enough parameters */
704 if (ParamCount < Func->ParamCount) {
705 Error ("Too few arguments in function call");
708 /* The function returns the size of all parameters pushed onto the stack.
709 * However, if there are parameters missing (which is an error and was
710 * flagged by the compiler) AND a stack frame was preallocated above,
711 * we would loose track of the stackpointer and generate an internal error
712 * later. So we correct the value by the parameters that should have been
713 * pushed to avoid an internal compiler error. Since an error was
714 * generated before, no code will be output anyway.
716 return ParamSize + FrameSize;
721 static void FunctionCall (int k, ExprDesc* lval)
722 /* Perform a function call. */
724 FuncDesc* Func; /* Function descriptor */
725 int IsFuncPtr; /* Flag */
726 unsigned ParamSize; /* Number of parameter bytes */
727 CodeMark Mark = 0; /* Initialize to keep gcc silent */
728 int PtrOffs = 0; /* Offset of function pointer on stack */
729 int IsFastCall = 0; /* True if it's a fast call function */
730 int PtrOnStack = 0; /* True if a pointer copy is on stack */
732 /* Get a pointer to the function descriptor from the type string */
733 Func = GetFuncDesc (lval->Type);
735 /* Handle function pointers transparently */
736 IsFuncPtr = IsTypeFuncPtr (lval->Type);
739 /* Check wether it's a fastcall function that has parameters */
740 IsFastCall = IsFastCallFunc (lval->Type + 1) && (Func->ParamCount > 0);
742 /* Things may be difficult, depending on where the function pointer
743 * resides. If the function pointer is an expression of some sort
744 * (not a local or global variable), we have to evaluate this
745 * expression now and save the result for later. Since calls to
746 * function pointers may be nested, we must save it onto the stack.
747 * For fastcall functions we do also need to place a copy of the
748 * pointer on stack, since we cannot use a/x.
750 PtrOnStack = IsFastCall || ((lval->Flags & (E_MGLOBAL | E_MLOCAL)) == 0);
753 /* Not a global or local variable, or a fastcall function. Load
754 * the pointer into the primary and mark it as an expression.
756 exprhs (CF_NONE, k, lval);
757 lval->Flags |= E_MEXPR;
759 /* Remember the code position */
760 Mark = GetCodePos ();
762 /* Push the pointer onto the stack and remember the offset */
767 /* Check for known standard functions and inline them if requested */
768 } else if (InlineStdFuncs && IsStdFunc ((const char*) lval->Name)) {
770 /* Inline this function */
771 HandleStdFunc (Func, lval);
776 /* Parse the parameter list */
777 ParamSize = FunctionParamList (Func);
779 /* We need the closing paren here */
782 /* Special handling for function pointers */
785 /* If the function is not a fastcall function, load the pointer to
786 * the function into the primary.
790 /* Not a fastcall function - we may use the primary */
792 /* If we have no parameters, the pointer is still in the
793 * primary. Remove the code to push it and correct the
796 if (ParamSize == 0) {
801 /* Load from the saved copy */
802 g_getlocal (CF_PTR, PtrOffs);
805 /* Load from original location */
806 exprhs (CF_NONE, k, lval);
809 /* Call the function */
810 g_callind (TypeOf (lval->Type+1), ParamSize, PtrOffs);
814 /* Fastcall function. We cannot use the primary for the function
815 * pointer and must therefore use an offset to the stack location.
816 * Since fastcall functions may never be variadic, we can use the
817 * index register for this purpose.
819 g_callind (CF_LOCAL, ParamSize, PtrOffs);
822 /* If we have a pointer on stack, remove it */
824 g_space (- (int) sizeofarg (CF_PTR));
833 /* Normal function */
834 g_call (TypeOf (lval->Type), (const char*) lval->Name, ParamSize);
841 static int primary (ExprDesc* lval)
842 /* This is the lowest level of the expression parser. */
846 /* Initialize fields in the expression stucture */
847 lval->Test = 0; /* No test */
848 lval->Sym = 0; /* Symbol unknown */
850 /* Character and integer constants. */
851 if (CurTok.Tok == TOK_ICONST || CurTok.Tok == TOK_CCONST) {
852 lval->Flags = E_MCONST | E_TCONST;
853 lval->Type = CurTok.Type;
854 lval->ConstVal = CurTok.IVal;
859 /* Process parenthesized subexpression by calling the whole parser
862 if (CurTok.Tok == TOK_LPAREN) {
864 InitExprDesc (lval); /* Remove any attributes */
870 /* If we run into an identifier in preprocessing mode, we assume that this
871 * is an undefined macro and replace it by a constant value of zero.
873 if (Preprocessing && CurTok.Tok == TOK_IDENT) {
874 MakeConstIntExpr (lval, 0);
878 /* All others may only be used if the expression evaluation is not called
879 * recursively by the preprocessor.
882 /* Illegal expression in PP mode */
883 Error ("Preprocessor expression expected");
884 MakeConstIntExpr (lval, 1);
889 if (CurTok.Tok == TOK_IDENT) {
894 /* Get a pointer to the symbol table entry */
895 Sym = lval->Sym = FindSym (CurTok.Ident);
897 /* Is the symbol known? */
900 /* We found the symbol - skip the name token */
903 /* The expression type is the symbol type */
904 lval->Type = Sym->Type;
906 /* Check for illegal symbol types */
907 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
908 if (Sym->Flags & SC_TYPE) {
909 /* Cannot use type symbols */
910 Error ("Variable identifier expected");
911 /* Assume an int type to make lval valid */
912 lval->Flags = E_MLOCAL | E_TLOFFS;
913 lval->Type = type_int;
918 /* Check for legal symbol types */
919 if ((Sym->Flags & SC_CONST) == SC_CONST) {
920 /* Enum or some other numeric constant */
921 lval->Flags = E_MCONST | E_TCONST;
922 lval->ConstVal = Sym->V.ConstVal;
924 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
926 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
927 lval->Name = (unsigned long) Sym->Name;
929 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
930 /* Local variable. If this is a parameter for a variadic
931 * function, we have to add some address calculations, and the
932 * address is not const.
934 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
935 /* Variadic parameter */
936 g_leavariadic (Sym->V.Offs - F_GetParamSize (CurrentFunc));
937 lval->Flags = E_MEXPR;
940 /* Normal parameter */
941 lval->Flags = E_MLOCAL | E_TLOFFS;
942 lval->ConstVal = Sym->V.Offs;
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.R.RegOffs;
949 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
950 /* Static variable */
951 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
952 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
953 lval->Name = (unsigned long) Sym->Name;
955 lval->Flags = E_MGLOBAL | E_MCONST | E_TLLAB;
956 lval->Name = Sym->V.Label;
960 /* Local static variable */
961 lval->Flags = E_MGLOBAL | E_MCONST | E_TLLAB;
962 lval->Name = Sym->V.Offs;
966 /* The symbol is referenced now */
967 Sym->Flags |= SC_REF;
968 if (IsTypeFunc (lval->Type) || IsTypeArray (lval->Type)) {
974 /* We did not find the symbol. Remember the name, then skip it */
975 strcpy (Ident, CurTok.Ident);
978 /* IDENT is either an auto-declared function or an undefined variable. */
979 if (CurTok.Tok == TOK_LPAREN) {
980 /* Declare a function returning int. For that purpose, prepare a
981 * function signature for a function having an empty param list
984 Warning ("Function call without a prototype");
985 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
986 lval->Type = Sym->Type;
987 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
988 lval->Name = (unsigned long) Sym->Name;
994 /* Undeclared Variable */
995 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
996 lval->Flags = E_MLOCAL | E_TLOFFS;
997 lval->Type = type_int;
999 Error ("Undefined symbol: `%s'", Ident);
1005 /* String literal? */
1006 if (CurTok.Tok == TOK_SCONST) {
1007 lval->Flags = E_MCONST | E_TLIT;
1008 lval->ConstVal = CurTok.IVal;
1009 lval->Type = GetCharArrayType (GetLiteralPoolOffs () - CurTok.IVal);
1014 /* ASM statement? */
1015 if (CurTok.Tok == TOK_ASM) {
1017 lval->Type = type_void;
1018 lval->Flags = E_MEXPR;
1023 /* __AX__ and __EAX__ pseudo values? */
1024 if (CurTok.Tok == TOK_AX || CurTok.Tok == TOK_EAX) {
1025 lval->Type = (CurTok.Tok == TOK_AX)? type_uint : type_ulong;
1026 lval->Flags = E_MREG;
1027 lval->Test &= ~E_CC;
1030 return 1; /* May be used as lvalue */
1033 /* Illegal primary. */
1034 Error ("Expression expected");
1035 MakeConstIntExpr (lval, 1);
1041 static int arrayref (int k, ExprDesc* lval)
1042 /* Handle an array reference */
1056 /* Skip the bracket */
1059 /* Get the type of left side */
1062 /* We can apply a special treatment for arrays that have a const base
1063 * address. This is true for most arrays and will produce a lot better
1064 * code. Check if this is a const base address.
1066 lflags = lval->Flags & ~E_MCTYPE;
1067 ConstBaseAddr = (lflags == E_MCONST) || /* Constant numeric address */
1068 (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1069 lflags == E_MLOCAL; /* Local array */
1071 /* If we have a constant base, we delay the address fetch */
1072 Mark1 = GetCodePos ();
1073 Mark2 = 0; /* Silence gcc */
1074 if (!ConstBaseAddr) {
1075 /* Get a pointer to the array into the primary */
1076 exprhs (CF_NONE, k, lval);
1078 /* Get the array pointer on stack. Do not push more than 16
1079 * bit, even if this value is greater, since we cannot handle
1080 * other than 16bit stuff when doing indexing.
1082 Mark2 = GetCodePos ();
1086 /* TOS now contains ptr to array elements. Get the subscript. */
1088 if (l == 0 && lval2.Flags == E_MCONST) {
1090 /* The array subscript is a constant - remove value from stack */
1091 if (!ConstBaseAddr) {
1095 /* Get an array pointer into the primary */
1096 exprhs (CF_NONE, k, lval);
1099 if (IsClassPtr (tptr1)) {
1101 /* Scale the subscript value according to element size */
1102 lval2.ConstVal *= CheckedPSizeOf (tptr1);
1104 /* Remove code for lhs load */
1107 /* Handle constant base array on stack. Be sure NOT to
1108 * handle pointers the same way, this won't work.
1110 if (IsTypeArray (tptr1) &&
1111 ((lval->Flags & ~E_MCTYPE) == E_MCONST ||
1112 (lval->Flags & ~E_MCTYPE) == E_MLOCAL ||
1113 (lval->Flags & E_MGLOBAL) != 0 ||
1114 (lval->Flags == E_MEOFFS))) {
1115 lval->ConstVal += lval2.ConstVal;
1118 /* Pointer - load into primary and remember offset */
1119 if ((lval->Flags & E_MEXPR) == 0 || k != 0) {
1120 exprhs (CF_NONE, k, lval);
1122 lval->ConstVal = lval2.ConstVal;
1123 lval->Flags = E_MEOFFS;
1126 /* Result is of element type */
1127 lval->Type = Indirect (tptr1);
1132 } else if (IsClassPtr (tptr2 = lval2.Type)) {
1133 /* Subscript is pointer, get element type */
1134 lval2.Type = Indirect (tptr2);
1136 /* Scale the rhs value in the primary register */
1137 g_scale (TypeOf (tptr1), CheckedSizeOf (lval2.Type));
1139 lval->Type = lval2.Type;
1141 Error ("Cannot subscript");
1144 /* Add the subscript. Since arrays are indexed by integers,
1145 * we will ignore the true type of the subscript here and
1146 * use always an int.
1148 g_inc (CF_INT | CF_CONST, lval2.ConstVal);
1152 /* Array subscript is not constant. Load it into the primary */
1153 Mark2 = GetCodePos ();
1154 exprhs (CF_NONE, l, &lval2);
1157 if (IsClassPtr (tptr1)) {
1159 /* Get the element type */
1160 lval->Type = Indirect (tptr1);
1162 /* Indexing is based on int's, so we will just use the integer
1163 * portion of the index (which is in (e)ax, so there's no further
1166 g_scale (CF_INT, CheckedSizeOf (lval->Type));
1168 } else if (IsClassPtr (tptr2)) {
1170 /* Get the element type */
1171 lval2.Type = Indirect (tptr2);
1173 /* Get the int value on top. If we go here, we're sure,
1174 * both values are 16 bit (the first one was truncated
1175 * if necessary and the second one is a pointer).
1176 * Note: If ConstBaseAddr is true, we don't have a value on
1177 * stack, so to "swap" both, just push the subscript.
1179 if (ConstBaseAddr) {
1181 exprhs (CF_NONE, k, lval);
1188 g_scale (TypeOf (tptr1), CheckedSizeOf (lval2.Type));
1189 lval->Type = lval2.Type;
1191 Error ("Cannot subscript");
1194 /* The offset is now in the primary register. It didn't have a
1195 * constant base address for the lhs, the lhs address is already
1196 * on stack, and we must add the offset. If the base address was
1197 * constant, we call special functions to add the address to the
1200 if (!ConstBaseAddr) {
1201 /* Add the subscript. Both values are int sized. */
1205 /* If the subscript has itself a constant address, it is often
1206 * a better idea to reverse again the order of the evaluation.
1207 * This will generate better code if the subscript is a byte
1208 * sized variable. But beware: This is only possible if the
1209 * subscript was not scaled, that is, if this was a byte array
1212 rflags = lval2.Flags & ~E_MCTYPE;
1213 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1214 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1215 rflags == E_MLOCAL; /* Local array */
1217 if (ConstSubAddr && CheckedSizeOf (lval->Type) == SIZEOF_CHAR) {
1221 /* Reverse the order of evaluation */
1222 unsigned flags = (CheckedSizeOf (lval2.Type) == SIZEOF_CHAR)? CF_CHAR : CF_INT;
1225 /* Get a pointer to the array into the primary. We have changed
1226 * Type above but we need the original type to load the
1227 * address, so restore it temporarily.
1229 SavedType = lval->Type;
1231 exprhs (CF_NONE, k, lval);
1232 lval->Type = SavedType;
1234 /* Add the variable */
1235 if (rflags == E_MLOCAL) {
1236 g_addlocal (flags, lval2.ConstVal);
1238 flags |= GlobalModeFlags (lval2.Flags);
1239 g_addstatic (flags, lval2.Name, lval2.ConstVal);
1242 if (lflags == E_MCONST) {
1243 /* Constant numeric address. Just add it */
1244 g_inc (CF_INT | CF_UNSIGNED, lval->ConstVal);
1245 } else if (lflags == E_MLOCAL) {
1246 /* Base address is a local variable address */
1247 if (IsTypeArray (tptr1)) {
1248 g_addaddr_local (CF_INT, lval->ConstVal);
1250 g_addlocal (CF_PTR, lval->ConstVal);
1253 /* Base address is a static variable address */
1254 unsigned flags = CF_INT;
1255 flags |= GlobalModeFlags (lval->Flags);
1256 if (IsTypeArray (tptr1)) {
1257 g_addaddr_static (flags, lval->Name, lval->ConstVal);
1259 g_addstatic (flags, lval->Name, lval->ConstVal);
1265 lval->Flags = E_MEXPR;
1268 return !IsTypeArray (lval->Type);
1274 static int structref (int k, ExprDesc* lval)
1275 /* Process struct field after . or ->. */
1281 /* Skip the token and check for an identifier */
1283 if (CurTok.Tok != TOK_IDENT) {
1284 Error ("Identifier expected");
1285 lval->Type = type_int;
1289 /* Get the symbol table entry and check for a struct field */
1290 strcpy (Ident, CurTok.Ident);
1292 Field = FindStructField (lval->Type, Ident);
1294 Error ("Struct/union has no field named `%s'", Ident);
1295 lval->Type = type_int;
1299 /* If we have constant input data, the result is also constant */
1300 flags = lval->Flags & ~E_MCTYPE;
1301 if (flags == E_MCONST ||
1302 (k == 0 && (flags == E_MLOCAL ||
1303 (flags & E_MGLOBAL) != 0 ||
1304 lval->Flags == E_MEOFFS))) {
1305 lval->ConstVal += Field->V.Offs;
1307 if ((flags & E_MEXPR) == 0 || k != 0) {
1308 exprhs (CF_NONE, k, lval);
1310 lval->ConstVal = Field->V.Offs;
1311 lval->Flags = E_MEOFFS;
1313 lval->Type = Field->Type;
1314 return !IsTypeArray (Field->Type);
1319 static int hie11 (ExprDesc *lval)
1320 /* Handle compound types (structs and arrays) */
1327 if (CurTok.Tok < TOK_LBRACK || CurTok.Tok > TOK_PTR_REF) {
1334 if (CurTok.Tok == TOK_LBRACK) {
1336 /* Array reference */
1337 k = arrayref (k, lval);
1339 } else if (CurTok.Tok == TOK_LPAREN) {
1341 /* Function call. Skip the opening parenthesis */
1344 if (IsTypeFunc (lval->Type) || IsTypeFuncPtr (lval->Type)) {
1346 /* Call the function */
1347 FunctionCall (k, lval);
1349 /* Result is in the primary register */
1350 lval->Flags = E_MEXPR;
1353 lval->Type = GetFuncReturn (lval->Type);
1356 Error ("Illegal function call");
1360 } else if (CurTok.Tok == TOK_DOT) {
1362 if (!IsClassStruct (lval->Type)) {
1363 Error ("Struct expected");
1365 k = structref (0, lval);
1367 } else if (CurTok.Tok == TOK_PTR_REF) {
1370 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1371 Error ("Struct pointer expected");
1373 k = structref (k, lval);
1383 void Store (ExprDesc* lval, const type* StoreType)
1384 /* Store the primary register into the location denoted by lval. If StoreType
1385 * is given, use this type when storing instead of lval->Type. If StoreType
1386 * is NULL, use lval->Type instead.
1391 unsigned f = lval->Flags;
1393 /* If StoreType was not given, use lval->Type instead */
1394 if (StoreType == 0) {
1395 StoreType = lval->Type;
1398 /* Get the code generator flags */
1399 Flags = TypeOf (StoreType);
1400 if (f & E_MGLOBAL) {
1401 Flags |= GlobalModeFlags (f);
1408 g_putstatic (Flags, lval->Name, lval->ConstVal);
1410 } else if (f & E_MLOCAL) {
1411 /* Store an auto variable */
1412 g_putlocal (Flags, lval->ConstVal, 0);
1413 } else if (f == E_MEOFFS) {
1414 /* Store indirect with offset */
1415 g_putind (Flags, lval->ConstVal);
1416 } else if (f != E_MREG) {
1418 /* Indirect without offset */
1419 g_putind (Flags, 0);
1421 /* Store into absolute address */
1422 g_putstatic (Flags | CF_ABSOLUTE, lval->ConstVal, 0);
1426 /* Assume that each one of the stores will invalidate CC */
1427 lval->Test &= ~E_CC;
1432 static void pre_incdec (ExprDesc* lval, void (*inc) (unsigned, unsigned long))
1433 /* Handle --i and ++i */
1440 if ((k = hie10 (lval)) == 0) {
1441 Error ("Invalid lvalue");
1445 /* Get the data type */
1446 flags = TypeOf (lval->Type) | CF_FORCECHAR | CF_CONST;
1448 /* Get the increment value in bytes */
1449 val = (lval->Type [0] == T_PTR)? CheckedPSizeOf (lval->Type) : 1;
1451 /* We're currently only able to handle some adressing modes */
1452 if ((lval->Flags & E_MGLOBAL) == 0 && /* Global address? */
1453 (lval->Flags & E_MLOCAL) == 0 && /* Local address? */
1454 (lval->Flags & E_MCONST) == 0 && /* Constant address? */
1455 (lval->Flags & E_MEXPR) == 0) { /* Address in a/x? */
1457 /* Use generic code. Push the address if needed */
1460 /* Fetch the value */
1461 exprhs (CF_NONE, k, lval);
1463 /* Increment value in primary */
1466 /* Store the result back */
1471 /* Special code for some addressing modes - use the special += ops */
1472 if (lval->Flags & E_MGLOBAL) {
1473 flags |= GlobalModeFlags (lval->Flags);
1475 g_addeqstatic (flags, lval->Name, lval->ConstVal, val);
1477 g_subeqstatic (flags, lval->Name, lval->ConstVal, val);
1479 } else if (lval->Flags & E_MLOCAL) {
1480 /* ref to localvar */
1482 g_addeqlocal (flags, lval->ConstVal, val);
1484 g_subeqlocal (flags, lval->ConstVal, val);
1486 } else if (lval->Flags & E_MCONST) {
1487 /* ref to absolute address */
1488 flags |= CF_ABSOLUTE;
1490 g_addeqstatic (flags, lval->ConstVal, 0, val);
1492 g_subeqstatic (flags, lval->ConstVal, 0, val);
1494 } else if (lval->Flags & E_MEXPR) {
1495 /* Address in a/x, check if we have an offset */
1496 unsigned Offs = (lval->Flags == E_MEOFFS)? lval->ConstVal : 0;
1498 g_addeqind (flags, Offs, val);
1500 g_subeqind (flags, Offs, val);
1503 Internal ("Invalid addressing mode");
1508 /* Result is an expression */
1509 lval->Flags = E_MEXPR;
1514 static void post_incdec (ExprDesc* lval, int k, void (*inc) (unsigned, unsigned long))
1515 /* Handle i-- and i++ */
1521 Error ("Invalid lvalue");
1525 /* Get the data type */
1526 flags = TypeOf (lval->Type);
1528 /* Push the address if needed */
1531 /* Fetch the value and save it (since it's the result of the expression) */
1532 exprhs (CF_NONE, 1, lval);
1533 g_save (flags | CF_FORCECHAR);
1535 /* If we have a pointer expression, increment by the size of the type */
1536 if (lval->Type[0] == T_PTR) {
1537 inc (flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (lval->Type + 1));
1539 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1542 /* Store the result back */
1545 /* Restore the original value */
1546 g_restore (flags | CF_FORCECHAR);
1547 lval->Flags = E_MEXPR;
1552 static void unaryop (int tok, ExprDesc* lval)
1553 /* Handle unary -/+ and ~ */
1560 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
1561 /* Value is constant */
1563 case TOK_MINUS: lval->ConstVal = -lval->ConstVal; break;
1564 case TOK_PLUS: break;
1565 case TOK_COMP: lval->ConstVal = ~lval->ConstVal; break;
1566 default: Internal ("Unexpected token: %d", tok);
1569 /* Value is not constant */
1570 exprhs (CF_NONE, k, lval);
1572 /* Get the type of the expression */
1573 flags = TypeOf (lval->Type);
1575 /* Handle the operation */
1577 case TOK_MINUS: g_neg (flags); break;
1578 case TOK_PLUS: break;
1579 case TOK_COMP: g_com (flags); break;
1580 default: Internal ("Unexpected token: %d", tok);
1582 lval->Flags = E_MEXPR;
1588 int hie10 (ExprDesc* lval)
1589 /* Handle ++, --, !, unary - etc. */
1594 switch (CurTok.Tok) {
1597 pre_incdec (lval, g_inc);
1601 pre_incdec (lval, g_dec);
1607 unaryop (CurTok.Tok, lval);
1612 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1613 /* Constant expression */
1614 lval->ConstVal = !lval->ConstVal;
1616 g_bneg (TypeOf (lval->Type));
1617 lval->Test |= E_CC; /* bneg will set cc */
1618 lval->Flags = E_MEXPR; /* say it's an expr */
1620 return 0; /* expr not storable */
1624 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1625 /* Expression is not const, indirect value loaded into primary */
1626 lval->Flags = E_MEXPR;
1627 lval->ConstVal = 0; /* Offset is zero now */
1629 /* If the expression is already a pointer to function, the
1630 * additional dereferencing operator must be ignored.
1632 if (IsTypeFuncPtr (lval->Type)) {
1633 /* Expression not storable */
1636 if (IsClassPtr (lval->Type)) {
1637 lval->Type = Indirect (lval->Type);
1639 Error ("Illegal indirection");
1648 /* The & operator may be applied to any lvalue, and it may be
1649 * applied to functions, even if they're no lvalues.
1651 if (k == 0 && !IsTypeFunc (lval->Type)) {
1652 /* Allow the & operator with an array */
1653 if (!IsTypeArray (lval->Type)) {
1654 Error ("Illegal address");
1657 t = TypeAlloc (TypeLen (lval->Type) + 2);
1659 TypeCpy (t + 1, lval->Type);
1666 if (istypeexpr ()) {
1667 type Type[MAXTYPELEN];
1669 lval->ConstVal = CheckedSizeOf (ParseType (Type));
1672 /* Remember the output queue pointer */
1673 CodeMark Mark = GetCodePos ();
1675 lval->ConstVal = CheckedSizeOf (lval->Type);
1676 /* Remove any generated code */
1679 lval->Flags = E_MCONST | E_TCONST;
1680 lval->Type = type_uint;
1681 lval->Test &= ~E_CC;
1685 if (istypeexpr ()) {
1687 return TypeCast (lval);
1692 switch (CurTok.Tok) {
1694 post_incdec (lval, k, g_inc);
1698 post_incdec (lval, k, g_dec);
1708 static int hie_internal (const GenDesc** ops, /* List of generators */
1709 ExprDesc* lval, /* parent expr's lval */
1710 int (*hienext) (ExprDesc*),
1711 int* UsedGen) /* next higher level */
1712 /* Helper function */
1719 token_t tok; /* The operator token */
1720 unsigned ltype, type;
1721 int rconst; /* Operand is a constant */
1727 while ((Gen = FindGen (CurTok.Tok, ops)) != 0) {
1729 /* Tell the caller that we handled it's ops */
1732 /* All operators that call this function expect an int on the lhs */
1733 if (!IsClassInt (lval->Type)) {
1734 Error ("Integer expression expected");
1737 /* Remember the operator token, then skip it */
1741 /* Get the lhs on stack */
1742 Mark1 = GetCodePos ();
1743 ltype = TypeOf (lval->Type);
1744 if (k == 0 && lval->Flags == E_MCONST) {
1745 /* Constant value */
1746 Mark2 = GetCodePos ();
1747 g_push (ltype | CF_CONST, lval->ConstVal);
1749 /* Value not constant */
1750 exprhs (CF_NONE, k, lval);
1751 Mark2 = GetCodePos ();
1755 /* Get the right hand side */
1756 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1758 /* Check the type of the rhs */
1759 if (!IsClassInt (lval2.Type)) {
1760 Error ("Integer expression expected");
1763 /* Check for const operands */
1764 if (k == 0 && lval->Flags == E_MCONST && rconst) {
1766 /* Both operands are constant, remove the generated code */
1770 /* Evaluate the result */
1771 lval->ConstVal = kcalc (tok, lval->ConstVal, lval2.ConstVal);
1773 /* Get the type of the result */
1774 lval->Type = promoteint (lval->Type, lval2.Type);
1778 /* If the right hand side is constant, and the generator function
1779 * expects the lhs in the primary, remove the push of the primary
1782 unsigned rtype = TypeOf (lval2.Type);
1785 /* Second value is constant - check for div */
1788 if (tok == TOK_DIV && lval2.ConstVal == 0) {
1789 Error ("Division by zero");
1790 } else if (tok == TOK_MOD && lval2.ConstVal == 0) {
1791 Error ("Modulo operation with zero");
1793 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1796 ltype |= CF_REG; /* Value is in register */
1800 /* Determine the type of the operation result. */
1801 type |= g_typeadjust (ltype, rtype);
1802 lval->Type = promoteint (lval->Type, lval2.Type);
1805 Gen->Func (type, lval2.ConstVal);
1806 lval->Flags = E_MEXPR;
1809 /* We have a rvalue now */
1818 static int hie_compare (const GenDesc** ops, /* List of generators */
1819 ExprDesc* lval, /* parent expr's lval */
1820 int (*hienext) (ExprDesc*))
1821 /* Helper function for the compare operators */
1828 token_t tok; /* The operator token */
1830 int rconst; /* Operand is a constant */
1835 while ((Gen = FindGen (CurTok.Tok, ops)) != 0) {
1837 /* Remember the operator token, then skip it */
1841 /* Get the lhs on stack */
1842 Mark1 = GetCodePos ();
1843 ltype = TypeOf (lval->Type);
1844 if (k == 0 && lval->Flags == E_MCONST) {
1845 /* Constant value */
1846 Mark2 = GetCodePos ();
1847 g_push (ltype | CF_CONST, lval->ConstVal);
1849 /* Value not constant */
1850 exprhs (CF_NONE, k, lval);
1851 Mark2 = GetCodePos ();
1855 /* Get the right hand side */
1856 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1858 /* Make sure, the types are compatible */
1859 if (IsClassInt (lval->Type)) {
1860 if (!IsClassInt (lval2.Type) && !(IsClassPtr(lval2.Type) && IsNullPtr(lval))) {
1861 Error ("Incompatible types");
1863 } else if (IsClassPtr (lval->Type)) {
1864 if (IsClassPtr (lval2.Type)) {
1865 /* Both pointers are allowed in comparison if they point to
1866 * the same type, or if one of them is a void pointer.
1868 type* left = Indirect (lval->Type);
1869 type* right = Indirect (lval2.Type);
1870 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1871 /* Incomatible pointers */
1872 Error ("Incompatible types");
1874 } else if (!IsNullPtr (&lval2)) {
1875 Error ("Incompatible types");
1879 /* Check for const operands */
1880 if (k == 0 && lval->Flags == E_MCONST && rconst) {
1882 /* Both operands are constant, remove the generated code */
1886 /* Evaluate the result */
1887 lval->ConstVal = kcalc (tok, lval->ConstVal, lval2.ConstVal);
1891 /* If the right hand side is constant, and the generator function
1892 * expects the lhs in the primary, remove the push of the primary
1898 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1901 ltype |= CF_REG; /* Value is in register */
1905 /* Determine the type of the operation result. If the left
1906 * operand is of type char and the right is a constant, or
1907 * if both operands are of type char, we will encode the
1908 * operation as char operation. Otherwise the default
1909 * promotions are used.
1911 if (IsTypeChar (lval->Type) && (IsTypeChar (lval2.Type) || rconst)) {
1913 if (IsSignUnsigned (lval->Type) || IsSignUnsigned (lval2.Type)) {
1914 flags |= CF_UNSIGNED;
1917 flags |= CF_FORCECHAR;
1920 unsigned rtype = TypeOf (lval2.Type) | (flags & CF_CONST);
1921 flags |= g_typeadjust (ltype, rtype);
1925 Gen->Func (flags, lval2.ConstVal);
1926 lval->Flags = E_MEXPR;
1929 /* Result type is always int */
1930 lval->Type = type_int;
1932 /* We have a rvalue now, condition codes are set */
1942 static int hie9 (ExprDesc *lval)
1943 /* Process * and / operators. */
1945 static const GenDesc* hie9_ops [] = {
1946 &GenMUL, &GenDIV, &GenMOD, 0
1950 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1955 static void parseadd (int k, ExprDesc* lval)
1956 /* Parse an expression with the binary plus operator. lval contains the
1957 * unprocessed left hand side of the expression and will contain the
1958 * result of the expression on return.
1962 unsigned flags; /* Operation flags */
1963 CodeMark Mark; /* Remember code position */
1964 type* lhst; /* Type of left hand side */
1965 type* rhst; /* Type of right hand side */
1968 /* Skip the PLUS token */
1971 /* Get the left hand side type, initialize operation flags */
1975 /* Check for constness on both sides */
1976 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
1978 /* The left hand side is a constant. Good. Get rhs */
1980 if (k == 0 && lval2.Flags == E_MCONST) {
1982 /* Right hand side is also constant. Get the rhs type */
1985 /* Both expressions are constants. Check for pointer arithmetic */
1986 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1987 /* Left is pointer, right is int, must scale rhs */
1988 lval->ConstVal += lval2.ConstVal * CheckedPSizeOf (lhst);
1989 /* Result type is a pointer */
1990 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1991 /* Left is int, right is pointer, must scale lhs */
1992 lval->ConstVal = lval->ConstVal * CheckedPSizeOf (rhst) + lval2.ConstVal;
1993 /* Result type is a pointer */
1994 lval->Type = lval2.Type;
1995 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1996 /* Integer addition */
1997 lval->ConstVal += lval2.ConstVal;
1998 typeadjust (lval, &lval2, 1);
2001 Error ("Invalid operands for binary operator `+'");
2004 /* Result is constant, condition codes not set */
2005 lval->Test &= ~E_CC;
2009 /* lhs is a constant and rhs is not constant. Load rhs into
2012 exprhs (CF_NONE, k, &lval2);
2014 /* Beware: The check above (for lhs) lets not only pass numeric
2015 * constants, but also constant addresses (labels), maybe even
2016 * with an offset. We have to check for that here.
2019 /* First, get the rhs type. */
2023 if (lval->Flags == E_MCONST) {
2024 /* A numerical constant */
2027 /* Constant address label */
2028 flags |= GlobalModeFlags (lval->Flags) | CF_CONSTADDR;
2031 /* Check for pointer arithmetic */
2032 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2033 /* Left is pointer, right is int, must scale rhs */
2034 g_scale (CF_INT, CheckedPSizeOf (lhst));
2035 /* Operate on pointers, result type is a pointer */
2037 /* Generate the code for the add */
2038 if (lval->Flags == E_MCONST) {
2039 /* Numeric constant */
2040 g_inc (flags, lval->ConstVal);
2042 /* Constant address */
2043 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2045 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2047 /* Left is int, right is pointer, must scale lhs. */
2048 unsigned ScaleFactor = CheckedPSizeOf (rhst);
2050 /* Operate on pointers, result type is a pointer */
2052 lval->Type = lval2.Type;
2054 /* Since we do already have rhs in the primary, if lhs is
2055 * not a numeric constant, and the scale factor is not one
2056 * (no scaling), we must take the long way over the stack.
2058 if (lval->Flags == E_MCONST) {
2059 /* Numeric constant, scale lhs */
2060 lval->ConstVal *= ScaleFactor;
2061 /* Generate the code for the add */
2062 g_inc (flags, lval->ConstVal);
2063 } else if (ScaleFactor == 1) {
2064 /* Constant address but no need to scale */
2065 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2067 /* Constant address that must be scaled */
2068 g_push (TypeOf (lval2.Type), 0); /* rhs --> stack */
2069 g_getimmed (flags, lval->Name, lval->ConstVal);
2070 g_scale (CF_PTR, ScaleFactor);
2073 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2074 /* Integer addition */
2075 flags |= typeadjust (lval, &lval2, 1);
2076 /* Generate the code for the add */
2077 if (lval->Flags == E_MCONST) {
2078 /* Numeric constant */
2079 g_inc (flags, lval->ConstVal);
2081 /* Constant address */
2082 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2086 Error ("Invalid operands for binary operator `+'");
2089 /* Result is in primary register */
2090 lval->Flags = E_MEXPR;
2091 lval->Test &= ~E_CC;
2097 /* Left hand side is not constant. Get the value onto the stack. */
2098 exprhs (CF_NONE, k, lval); /* --> primary register */
2099 Mark = GetCodePos ();
2100 g_push (TypeOf (lval->Type), 0); /* --> stack */
2102 /* Evaluate the rhs */
2103 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2105 /* Right hand side is a constant. Get the rhs type */
2108 /* Remove pushed value from stack */
2110 pop (TypeOf (lval->Type));
2112 /* Check for pointer arithmetic */
2113 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2114 /* Left is pointer, right is int, must scale rhs */
2115 lval2.ConstVal *= CheckedPSizeOf (lhst);
2116 /* Operate on pointers, result type is a pointer */
2118 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2119 /* Left is int, right is pointer, must scale lhs (ptr only) */
2120 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2121 /* Operate on pointers, result type is a pointer */
2123 lval->Type = lval2.Type;
2124 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2125 /* Integer addition */
2126 flags = typeadjust (lval, &lval2, 1);
2129 Error ("Invalid operands for binary operator `+'");
2132 /* Generate code for the add */
2133 g_inc (flags | CF_CONST, lval2.ConstVal);
2135 /* Result is in primary register */
2136 lval->Flags = E_MEXPR;
2137 lval->Test &= ~E_CC;
2141 /* lhs and rhs are not constant. Get the rhs type. */
2144 /* Check for pointer arithmetic */
2145 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2146 /* Left is pointer, right is int, must scale rhs */
2147 g_scale (CF_INT, CheckedPSizeOf (lhst));
2148 /* Operate on pointers, result type is a pointer */
2150 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2151 /* Left is int, right is pointer, must scale lhs */
2152 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2153 g_swap (CF_INT); /* Swap TOS and primary */
2154 g_scale (CF_INT, CheckedPSizeOf (rhst));
2155 /* Operate on pointers, result type is a pointer */
2157 lval->Type = lval2.Type;
2158 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2159 /* Integer addition. Note: Result is never constant.
2160 * Problem here is that typeadjust does not know if the
2161 * variable is an rvalue or lvalue, so if both operands
2162 * are dereferenced constant numeric addresses, typeadjust
2163 * thinks the operation works on constants. Removing
2164 * CF_CONST here means handling the symptoms, however, the
2165 * whole parser is such a mess that I fear to break anything
2166 * when trying to apply another solution.
2168 flags = typeadjust (lval, &lval2, 0) & ~CF_CONST;
2171 Error ("Invalid operands for binary operator `+'");
2174 /* Generate code for the add */
2177 /* Result is in primary register */
2178 lval->Flags = E_MEXPR;
2179 lval->Test &= ~E_CC;
2188 static void parsesub (int k, ExprDesc* lval)
2189 /* Parse an expression with the binary minus operator. lval contains the
2190 * unprocessed left hand side of the expression and will contain the
2191 * result of the expression on return.
2195 unsigned flags; /* Operation flags */
2196 type* lhst; /* Type of left hand side */
2197 type* rhst; /* Type of right hand side */
2198 CodeMark Mark1; /* Save position of output queue */
2199 CodeMark Mark2; /* Another position in the queue */
2200 int rscale; /* Scale factor for the result */
2203 /* Skip the MINUS token */
2206 /* Get the left hand side type, initialize operation flags */
2209 rscale = 1; /* Scale by 1, that is, don't scale */
2211 /* Remember the output queue position, then bring the value onto the stack */
2212 Mark1 = GetCodePos ();
2213 exprhs (CF_NONE, k, lval); /* --> primary register */
2214 Mark2 = GetCodePos ();
2215 g_push (TypeOf (lhst), 0); /* --> stack */
2217 /* Parse the right hand side */
2218 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2220 /* The right hand side is constant. Get the rhs type. */
2223 /* Check left hand side */
2224 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
2226 /* Both sides are constant, remove generated code */
2228 pop (TypeOf (lhst)); /* Clean up the stack */
2230 /* Check for pointer arithmetic */
2231 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2232 /* Left is pointer, right is int, must scale rhs */
2233 lval->ConstVal -= lval2.ConstVal * CheckedPSizeOf (lhst);
2234 /* Operate on pointers, result type is a pointer */
2235 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2236 /* Left is pointer, right is pointer, must scale result */
2237 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2238 Error ("Incompatible pointer types");
2240 lval->ConstVal = (lval->ConstVal - lval2.ConstVal) /
2241 CheckedPSizeOf (lhst);
2243 /* Operate on pointers, result type is an integer */
2244 lval->Type = type_int;
2245 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2246 /* Integer subtraction */
2247 typeadjust (lval, &lval2, 1);
2248 lval->ConstVal -= lval2.ConstVal;
2251 Error ("Invalid operands for binary operator `-'");
2254 /* Result is constant, condition codes not set */
2255 /* lval->Flags = E_MCONST; ### */
2256 lval->Test &= ~E_CC;
2260 /* Left hand side is not constant, right hand side is.
2261 * Remove pushed value from stack.
2264 pop (TypeOf (lhst));
2266 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2267 /* Left is pointer, right is int, must scale rhs */
2268 lval2.ConstVal *= CheckedPSizeOf (lhst);
2269 /* Operate on pointers, result type is a pointer */
2271 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2272 /* Left is pointer, right is pointer, must scale result */
2273 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2274 Error ("Incompatible pointer types");
2276 rscale = CheckedPSizeOf (lhst);
2278 /* Operate on pointers, result type is an integer */
2280 lval->Type = type_int;
2281 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2282 /* Integer subtraction */
2283 flags = typeadjust (lval, &lval2, 1);
2286 Error ("Invalid operands for binary operator `-'");
2289 /* Do the subtraction */
2290 g_dec (flags | CF_CONST, lval2.ConstVal);
2292 /* If this was a pointer subtraction, we must scale the result */
2294 g_scale (flags, -rscale);
2297 /* Result is in primary register */
2298 lval->Flags = E_MEXPR;
2299 lval->Test &= ~E_CC;
2305 /* Right hand side is not constant. Get the rhs type. */
2308 /* Check for pointer arithmetic */
2309 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2310 /* Left is pointer, right is int, must scale rhs */
2311 g_scale (CF_INT, CheckedPSizeOf (lhst));
2312 /* Operate on pointers, result type is a pointer */
2314 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2315 /* Left is pointer, right is pointer, must scale result */
2316 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2317 Error ("Incompatible pointer types");
2319 rscale = CheckedPSizeOf (lhst);
2321 /* Operate on pointers, result type is an integer */
2323 lval->Type = type_int;
2324 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2325 /* Integer subtraction. If the left hand side descriptor says that
2326 * the lhs is const, we have to remove this mark, since this is no
2327 * longer true, lhs is on stack instead.
2329 if (lval->Flags == E_MCONST) {
2330 lval->Flags = E_MEXPR;
2332 /* Adjust operand types */
2333 flags = typeadjust (lval, &lval2, 0);
2336 Error ("Invalid operands for binary operator `-'");
2339 /* Generate code for the sub (the & is a hack here) */
2340 g_sub (flags & ~CF_CONST, 0);
2342 /* If this was a pointer subtraction, we must scale the result */
2344 g_scale (flags, -rscale);
2347 /* Result is in primary register */
2348 lval->Flags = E_MEXPR;
2349 lval->Test &= ~E_CC;
2355 static int hie8 (ExprDesc* lval)
2356 /* Process + and - binary operators. */
2358 int k = hie9 (lval);
2359 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2361 if (CurTok.Tok == TOK_PLUS) {
2374 static int hie7 (ExprDesc *lval)
2375 /* Parse << and >>. */
2377 static const GenDesc* hie7_ops [] = {
2382 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2387 static int hie6 (ExprDesc *lval)
2388 /* process greater-than type comparators */
2390 static const GenDesc* hie6_ops [] = {
2391 &GenLT, &GenLE, &GenGE, &GenGT, 0
2393 return hie_compare (hie6_ops, lval, hie7);
2398 static int hie5 (ExprDesc *lval)
2400 static const GenDesc* hie5_ops[] = {
2403 return hie_compare (hie5_ops, lval, hie6);
2408 static int hie4 (ExprDesc* lval)
2409 /* Handle & (bitwise and) */
2411 static const GenDesc* hie4_ops [] = {
2416 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2421 static int hie3 (ExprDesc *lval)
2422 /* Handle ^ (bitwise exclusive or) */
2424 static const GenDesc* hie3_ops [] = {
2429 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2434 static int hie2 (ExprDesc *lval)
2435 /* Handle | (bitwise or) */
2437 static const GenDesc* hie2_ops [] = {
2442 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2447 static int hieAndPP (ExprDesc* lval)
2448 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2449 * called recursively from the preprocessor.
2454 ConstSubExpr (hie2, lval);
2455 while (CurTok.Tok == TOK_BOOL_AND) {
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 (hie2, &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 hieOrPP (ExprDesc *lval)
2486 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2487 * called recursively from the preprocessor.
2492 ConstSubExpr (hieAndPP, lval);
2493 while (CurTok.Tok == TOK_BOOL_OR) {
2495 /* Left hand side must be an int */
2496 if (!IsClassInt (lval->Type)) {
2497 Error ("Left hand side must be of integer type");
2498 MakeConstIntExpr (lval, 1);
2505 ConstSubExpr (hieAndPP, &lval2);
2507 /* Since we are in PP mode, all we know about is integers */
2508 if (!IsClassInt (lval2.Type)) {
2509 Error ("Right hand side must be of integer type");
2510 MakeConstIntExpr (&lval2, 1);
2513 /* Combine the two */
2514 lval->ConstVal = (lval->ConstVal || lval2.ConstVal);
2517 /* Always a rvalue */
2523 static int hieAnd (ExprDesc* lval, unsigned TrueLab, int* BoolOp)
2524 /* Process "exp && exp" */
2531 if (CurTok.Tok == TOK_BOOL_AND) {
2533 /* Tell our caller that we're evaluating a boolean */
2536 /* Get a label that we will use for false expressions */
2537 lab = GetLocalLabel ();
2539 /* If the expr hasn't set condition codes, set the force-test flag */
2540 if ((lval->Test & E_CC) == 0) {
2541 lval->Test |= E_FORCETEST;
2544 /* Load the value */
2545 exprhs (CF_FORCECHAR, k, lval);
2547 /* Generate the jump */
2548 g_falsejump (CF_NONE, lab);
2550 /* Parse more boolean and's */
2551 while (CurTok.Tok == TOK_BOOL_AND) {
2558 if ((lval2.Test & E_CC) == 0) {
2559 lval2.Test |= E_FORCETEST;
2561 exprhs (CF_FORCECHAR, k, &lval2);
2563 /* Do short circuit evaluation */
2564 if (CurTok.Tok == TOK_BOOL_AND) {
2565 g_falsejump (CF_NONE, lab);
2567 /* Last expression - will evaluate to true */
2568 g_truejump (CF_NONE, TrueLab);
2572 /* Define the false jump label here */
2573 g_defcodelabel (lab);
2575 /* Define the label */
2576 lval->Flags = E_MEXPR;
2577 lval->Test |= E_CC; /* Condition codes are set */
2585 static int hieOr (ExprDesc *lval)
2586 /* Process "exp || exp". */
2590 int BoolOp = 0; /* Did we have a boolean op? */
2591 int AndOp; /* Did we have a && operation? */
2592 unsigned TrueLab; /* Jump to this label if true */
2596 TrueLab = GetLocalLabel ();
2598 /* Call the next level parser */
2599 k = hieAnd (lval, TrueLab, &BoolOp);
2601 /* Any boolean or's? */
2602 if (CurTok.Tok == TOK_BOOL_OR) {
2604 /* If the expr hasn't set condition codes, set the force-test flag */
2605 if ((lval->Test & E_CC) == 0) {
2606 lval->Test |= E_FORCETEST;
2609 /* Get first expr */
2610 exprhs (CF_FORCECHAR, k, lval);
2612 /* For each expression jump to TrueLab if true. Beware: If we
2613 * had && operators, the jump is already in place!
2616 g_truejump (CF_NONE, TrueLab);
2619 /* Remember that we had a boolean op */
2622 /* while there's more expr */
2623 while (CurTok.Tok == TOK_BOOL_OR) {
2630 k = hieAnd (&lval2, TrueLab, &AndOp);
2631 if ((lval2.Test & E_CC) == 0) {
2632 lval2.Test |= E_FORCETEST;
2634 exprhs (CF_FORCECHAR, k, &lval2);
2636 /* If there is more to come, add shortcut boolean eval. */
2637 g_truejump (CF_NONE, TrueLab);
2640 lval->Flags = E_MEXPR;
2641 lval->Test |= E_CC; /* Condition codes are set */
2645 /* If we really had boolean ops, generate the end sequence */
2647 DoneLab = GetLocalLabel ();
2648 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2649 g_falsejump (CF_NONE, DoneLab);
2650 g_defcodelabel (TrueLab);
2651 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2652 g_defcodelabel (DoneLab);
2659 static int hieQuest (ExprDesc *lval)
2660 /* Parse "lvalue ? exp : exp" */
2665 ExprDesc lval2; /* Expression 2 */
2666 ExprDesc lval3; /* Expression 3 */
2667 type* type2; /* Type of expression 2 */
2668 type* type3; /* Type of expression 3 */
2669 type* rtype; /* Type of result */
2672 k = Preprocessing? hieOrPP (lval) : hieOr (lval);
2673 if (CurTok.Tok == TOK_QUEST) {
2675 if ((lval->Test & E_CC) == 0) {
2676 /* Condition codes not set, force a test */
2677 lval->Test |= E_FORCETEST;
2679 exprhs (CF_NONE, k, lval);
2680 labf = GetLocalLabel ();
2681 g_falsejump (CF_NONE, labf);
2683 /* Parse second expression */
2684 k = expr (hie1, &lval2);
2686 if (!IsTypeVoid (lval2.Type)) {
2687 /* Load it into the primary */
2688 exprhs (CF_NONE, k, &lval2);
2690 labt = GetLocalLabel ();
2694 /* Parse the third expression */
2695 g_defcodelabel (labf);
2696 k = expr (hie1, &lval3);
2698 if (!IsTypeVoid (lval3.Type)) {
2699 /* Load it into the primary */
2700 exprhs (CF_NONE, k, &lval3);
2703 /* Check if any conversions are needed, if so, do them.
2704 * Conversion rules for ?: expression are:
2705 * - if both expressions are int expressions, default promotion
2706 * rules for ints apply.
2707 * - if both expressions are pointers of the same type, the
2708 * result of the expression is of this type.
2709 * - if one of the expressions is a pointer and the other is
2710 * a zero constant, the resulting type is that of the pointer
2712 * - if both expressions are void expressions, the result is of
2714 * - all other cases are flagged by an error.
2716 if (IsClassInt (type2) && IsClassInt (type3)) {
2718 /* Get common type */
2719 rtype = promoteint (type2, type3);
2721 /* Convert the third expression to this type if needed */
2722 g_typecast (TypeOf (rtype), TypeOf (type3));
2724 /* Setup a new label so that the expr3 code will jump around
2725 * the type cast code for expr2.
2727 labf = GetLocalLabel (); /* Get new label */
2728 g_jump (labf); /* Jump around code */
2730 /* The jump for expr2 goes here */
2731 g_defcodelabel (labt);
2733 /* Create the typecast code for expr2 */
2734 g_typecast (TypeOf (rtype), TypeOf (type2));
2736 /* Jump here around the typecase code. */
2737 g_defcodelabel (labf);
2738 labt = 0; /* Mark other label as invalid */
2740 } else if (IsClassPtr (type2) && IsClassPtr (type3)) {
2741 /* Must point to same type */
2742 if (TypeCmp (Indirect (type2), Indirect (type3)) < TC_EQUAL) {
2743 Error ("Incompatible pointer types");
2745 /* Result has the common type */
2747 } else if (IsClassPtr (type2) && IsNullPtr (&lval3)) {
2748 /* Result type is pointer, no cast needed */
2750 } else if (IsNullPtr (&lval2) && IsClassPtr (type3)) {
2751 /* Result type is pointer, no cast needed */
2753 } else if (IsTypeVoid (type2) && IsTypeVoid (type3)) {
2754 /* Result type is void */
2757 Error ("Incompatible types");
2758 rtype = lval2.Type; /* Doesn't matter here */
2761 /* If we don't have the label defined until now, do it */
2763 g_defcodelabel (labt);
2766 /* Setup the target expression */
2767 lval->Flags = E_MEXPR;
2776 static void opeq (const GenDesc* Gen, ExprDesc *lval, int k)
2777 /* Process "op=" operators. */
2786 Error ("Invalid lvalue in assignment");
2790 /* Determine the type of the lhs */
2791 flags = TypeOf (lval->Type);
2792 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2793 lval->Type [0] == T_PTR;
2795 /* Get the lhs address on stack (if needed) */
2798 /* Fetch the lhs into the primary register if needed */
2799 exprhs (CF_NONE, k, lval);
2801 /* Bring the lhs on stack */
2802 Mark = GetCodePos ();
2805 /* Evaluate the rhs */
2806 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2807 /* The resulting value is a constant. If the generator has the NOPUSH
2808 * flag set, don't push the lhs.
2810 if (Gen->Flags & GEN_NOPUSH) {
2815 /* lhs is a pointer, scale rhs */
2816 lval2.ConstVal *= CheckedSizeOf (lval->Type+1);
2819 /* If the lhs is character sized, the operation may be later done
2822 if (CheckedSizeOf (lval->Type) == SIZEOF_CHAR) {
2823 flags |= CF_FORCECHAR;
2826 /* Special handling for add and sub - some sort of a hack, but short code */
2827 if (Gen->Func == g_add) {
2828 g_inc (flags | CF_CONST, lval2.ConstVal);
2829 } else if (Gen->Func == g_sub) {
2830 g_dec (flags | CF_CONST, lval2.ConstVal);
2832 Gen->Func (flags | CF_CONST, lval2.ConstVal);
2835 /* rhs is not constant and already in the primary register */
2837 /* lhs is a pointer, scale rhs */
2838 g_scale (TypeOf (lval2.Type), CheckedSizeOf (lval->Type+1));
2841 /* If the lhs is character sized, the operation may be later done
2844 if (CheckedSizeOf (lval->Type) == SIZEOF_CHAR) {
2845 flags |= CF_FORCECHAR;
2848 /* Adjust the types of the operands if needed */
2849 Gen->Func (g_typeadjust (flags, TypeOf (lval2.Type)), 0);
2852 lval->Flags = E_MEXPR;
2857 static void addsubeq (const GenDesc* Gen, ExprDesc *lval, int k)
2858 /* Process the += and -= operators */
2866 /* We must have an lvalue */
2868 Error ("Invalid lvalue in assignment");
2872 /* We're currently only able to handle some adressing modes */
2873 if ((lval->Flags & E_MGLOBAL) == 0 && /* Global address? */
2874 (lval->Flags & E_MLOCAL) == 0 && /* Local address? */
2875 (lval->Flags & E_MCONST) == 0) { /* Constant address? */
2876 /* Use generic routine */
2877 opeq (Gen, lval, k);
2881 /* Skip the operator */
2884 /* Check if we have a pointer expression and must scale rhs */
2885 MustScale = (lval->Type [0] == T_PTR);
2887 /* Initialize the code generator flags */
2891 /* Evaluate the rhs */
2892 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2893 /* The resulting value is a constant. */
2895 /* lhs is a pointer, scale rhs */
2896 lval2.ConstVal *= CheckedSizeOf (lval->Type+1);
2901 /* rhs is not constant and already in the primary register */
2903 /* lhs is a pointer, scale rhs */
2904 g_scale (TypeOf (lval2.Type), CheckedSizeOf (lval->Type+1));
2908 /* Setup the code generator flags */
2909 lflags |= TypeOf (lval->Type) | CF_FORCECHAR;
2910 rflags |= TypeOf (lval2.Type);
2912 /* Cast the rhs to the type of the lhs */
2913 g_typecast (lflags, rflags);
2915 /* Output apropriate code */
2916 if (lval->Flags & E_MGLOBAL) {
2917 /* Static variable */
2918 lflags |= GlobalModeFlags (lval->Flags);
2919 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2920 g_addeqstatic (lflags, lval->Name, lval->ConstVal, lval2.ConstVal);
2922 g_subeqstatic (lflags, lval->Name, lval->ConstVal, lval2.ConstVal);
2924 } else if (lval->Flags & E_MLOCAL) {
2925 /* ref to localvar */
2926 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2927 g_addeqlocal (lflags, lval->ConstVal, lval2.ConstVal);
2929 g_subeqlocal (lflags, lval->ConstVal, lval2.ConstVal);
2931 } else if (lval->Flags & E_MCONST) {
2932 /* ref to absolute address */
2933 lflags |= CF_ABSOLUTE;
2934 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2935 g_addeqstatic (lflags, lval->ConstVal, 0, lval2.ConstVal);
2937 g_subeqstatic (lflags, lval->ConstVal, 0, lval2.ConstVal);
2939 } else if (lval->Flags & E_MEXPR) {
2940 /* Address in a/x. */
2941 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2942 g_addeqind (lflags, lval->ConstVal, lval2.ConstVal);
2944 g_subeqind (lflags, lval->ConstVal, lval2.ConstVal);
2947 Internal ("Invalid addressing mode");
2950 /* Expression is in the primary now */
2951 lval->Flags = E_MEXPR;
2956 int hie1 (ExprDesc* lval)
2957 /* Parse first level of expression hierarchy. */
2961 k = hieQuest (lval);
2962 switch (CurTok.Tok) {
2971 Error ("Invalid lvalue in assignment");
2977 case TOK_PLUS_ASSIGN:
2978 addsubeq (&GenPASGN, lval, k);
2981 case TOK_MINUS_ASSIGN:
2982 addsubeq (&GenSASGN, lval, k);
2985 case TOK_MUL_ASSIGN:
2986 opeq (&GenMASGN, lval, k);
2989 case TOK_DIV_ASSIGN:
2990 opeq (&GenDASGN, lval, k);
2993 case TOK_MOD_ASSIGN:
2994 opeq (&GenMOASGN, lval, k);
2997 case TOK_SHL_ASSIGN:
2998 opeq (&GenSLASGN, lval, k);
3001 case TOK_SHR_ASSIGN:
3002 opeq (&GenSRASGN, lval, k);
3005 case TOK_AND_ASSIGN:
3006 opeq (&GenAASGN, lval, k);
3009 case TOK_XOR_ASSIGN:
3010 opeq (&GenXOASGN, lval, k);
3014 opeq (&GenOASGN, lval, k);
3025 static int hie0 (ExprDesc *lval)
3026 /* Parse comma operator. */
3031 while (CurTok.Tok == TOK_COMMA) {
3040 int evalexpr (unsigned flags, int (*f) (ExprDesc*), ExprDesc* lval)
3041 /* Will evaluate an expression via the given function. If the result is a
3042 * constant, 0 is returned and the value is put in the lval struct. If the
3043 * result is not constant, exprhs is called to bring the value into the
3044 * primary register and 1 is returned.
3051 if (k == 0 && lval->Flags == E_MCONST) {
3052 /* Constant expression */
3055 /* Not constant, load into the primary */
3056 exprhs (flags, k, lval);
3063 static int expr (int (*func) (ExprDesc*), ExprDesc *lval)
3064 /* Expression parser; func is either hie0 or hie1. */
3073 /* Do some checks if code generation is still constistent */
3074 if (savsp != oursp) {
3076 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
3078 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
3086 void expression1 (ExprDesc* lval)
3087 /* Evaluate an expression on level 1 (no comma operator) and put it into
3088 * the primary register
3091 InitExprDesc (lval);
3092 exprhs (CF_NONE, expr (hie1, lval), lval);
3097 void expression (ExprDesc* lval)
3098 /* Evaluate an expression and put it into the primary register */
3100 InitExprDesc (lval);
3101 exprhs (CF_NONE, expr (hie0, lval), lval);
3106 void ConstExpr (ExprDesc* lval)
3107 /* Get a constant value */
3109 InitExprDesc (lval);
3110 if (expr (hie1, lval) != 0 || (lval->Flags & E_MCONST) == 0) {
3111 Error ("Constant expression expected");
3112 /* To avoid any compiler errors, make the expression a valid const */
3113 MakeConstIntExpr (lval, 1);
3119 void ConstIntExpr (ExprDesc* Val)
3120 /* Get a constant int value */
3123 if (expr (hie1, Val) != 0 ||
3124 (Val->Flags & E_MCONST) == 0 ||
3125 !IsClassInt (Val->Type)) {
3126 Error ("Constant integer expression expected");
3127 /* To avoid any compiler errors, make the expression a valid const */
3128 MakeConstIntExpr (Val, 1);
3134 void intexpr (ExprDesc* lval)
3135 /* Get an integer expression */
3138 if (!IsClassInt (lval->Type)) {
3139 Error ("Integer expression expected");
3140 /* To avoid any compiler errors, make the expression a valid int */
3141 MakeConstIntExpr (lval, 1);
3147 void Test (unsigned Label, int Invert)
3148 /* Evaluate a boolean test expression and jump depending on the result of
3149 * the test and on Invert.
3155 /* Evaluate the expression */
3156 k = expr (hie0, InitExprDesc (&lval));
3158 /* Check for a boolean expression */
3159 CheckBoolExpr (&lval);
3161 /* Check for a constant expression */
3162 if (k == 0 && lval.Flags == E_MCONST) {
3164 /* Constant rvalue */
3165 if (!Invert && lval.ConstVal == 0) {
3167 Warning ("Unreachable code");
3168 } else if (Invert && lval.ConstVal != 0) {
3174 /* If the expr hasn't set condition codes, set the force-test flag */
3175 if ((lval.Test & E_CC) == 0) {
3176 lval.Test |= E_FORCETEST;
3179 /* Load the value into the primary register */
3180 exprhs (CF_FORCECHAR, k, &lval);
3182 /* Generate the jump */
3184 g_truejump (CF_NONE, Label);
3186 g_falsejump (CF_NONE, Label);
3193 void TestInParens (unsigned Label, int Invert)
3194 /* Evaluate a boolean test expression in parenthesis and jump depending on
3195 * the result of the test * and on Invert.
3198 /* Eat the parenthesis */
3202 Test (Label, Invert);
3204 /* Check for the closing brace */