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 /* Convert the rhs to the type of the lhs. */
229 unsigned flags = TypeOf (rhst);
230 if (rhs->Flags == E_MCONST) {
233 return g_typecast (TypeOf (lhst), flags);
235 Error ("Incompatible types");
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, and check for character literals
1109 * (both won't work).
1111 if (IsTypeArray (tptr1) && lval->Flags != (E_MCONST | E_TLIT) &&
1112 ((lval->Flags & ~E_MCTYPE) == E_MCONST ||
1113 (lval->Flags & ~E_MCTYPE) == E_MLOCAL ||
1114 (lval->Flags & E_MGLOBAL) != 0 ||
1115 (lval->Flags == E_MEOFFS))) {
1116 lval->ConstVal += lval2.ConstVal;
1119 /* Pointer - load into primary and remember offset */
1120 if ((lval->Flags & E_MEXPR) == 0 || k != 0) {
1121 exprhs (CF_NONE, k, lval);
1123 lval->ConstVal = lval2.ConstVal;
1124 lval->Flags = E_MEOFFS;
1127 /* Result is of element type */
1128 lval->Type = Indirect (tptr1);
1133 } else if (IsClassPtr (tptr2 = lval2.Type)) {
1134 /* Subscript is pointer, get element type */
1135 lval2.Type = Indirect (tptr2);
1137 /* Scale the rhs value in the primary register */
1138 g_scale (TypeOf (tptr1), CheckedSizeOf (lval2.Type));
1140 lval->Type = lval2.Type;
1142 Error ("Cannot subscript");
1145 /* Add the subscript. Since arrays are indexed by integers,
1146 * we will ignore the true type of the subscript here and
1147 * use always an int.
1149 g_inc (CF_INT | CF_CONST, lval2.ConstVal);
1153 /* Array subscript is not constant. Load it into the primary */
1154 Mark2 = GetCodePos ();
1155 exprhs (CF_NONE, l, &lval2);
1158 if (IsClassPtr (tptr1)) {
1160 /* Get the element type */
1161 lval->Type = Indirect (tptr1);
1163 /* Indexing is based on int's, so we will just use the integer
1164 * portion of the index (which is in (e)ax, so there's no further
1167 g_scale (CF_INT, CheckedSizeOf (lval->Type));
1169 } else if (IsClassPtr (tptr2)) {
1171 /* Get the element type */
1172 lval2.Type = Indirect (tptr2);
1174 /* Get the int value on top. If we go here, we're sure,
1175 * both values are 16 bit (the first one was truncated
1176 * if necessary and the second one is a pointer).
1177 * Note: If ConstBaseAddr is true, we don't have a value on
1178 * stack, so to "swap" both, just push the subscript.
1180 if (ConstBaseAddr) {
1182 exprhs (CF_NONE, k, lval);
1189 g_scale (TypeOf (tptr1), CheckedSizeOf (lval2.Type));
1190 lval->Type = lval2.Type;
1192 Error ("Cannot subscript");
1195 /* The offset is now in the primary register. It didn't have a
1196 * constant base address for the lhs, the lhs address is already
1197 * on stack, and we must add the offset. If the base address was
1198 * constant, we call special functions to add the address to the
1201 if (!ConstBaseAddr) {
1202 /* Add the subscript. Both values are int sized. */
1206 /* If the subscript has itself a constant address, it is often
1207 * a better idea to reverse again the order of the evaluation.
1208 * This will generate better code if the subscript is a byte
1209 * sized variable. But beware: This is only possible if the
1210 * subscript was not scaled, that is, if this was a byte array
1213 rflags = lval2.Flags & ~E_MCTYPE;
1214 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1215 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1216 rflags == E_MLOCAL; /* Local array */
1218 if (ConstSubAddr && CheckedSizeOf (lval->Type) == SIZEOF_CHAR) {
1222 /* Reverse the order of evaluation */
1223 unsigned flags = (CheckedSizeOf (lval2.Type) == SIZEOF_CHAR)? CF_CHAR : CF_INT;
1226 /* Get a pointer to the array into the primary. We have changed
1227 * Type above but we need the original type to load the
1228 * address, so restore it temporarily.
1230 SavedType = lval->Type;
1232 exprhs (CF_NONE, k, lval);
1233 lval->Type = SavedType;
1235 /* Add the variable */
1236 if (rflags == E_MLOCAL) {
1237 g_addlocal (flags, lval2.ConstVal);
1239 flags |= GlobalModeFlags (lval2.Flags);
1240 g_addstatic (flags, lval2.Name, lval2.ConstVal);
1243 if (lflags == E_MCONST) {
1244 /* Constant numeric address. Just add it */
1245 g_inc (CF_INT | CF_UNSIGNED, lval->ConstVal);
1246 } else if (lflags == E_MLOCAL) {
1247 /* Base address is a local variable address */
1248 if (IsTypeArray (tptr1)) {
1249 g_addaddr_local (CF_INT, lval->ConstVal);
1251 g_addlocal (CF_PTR, lval->ConstVal);
1254 /* Base address is a static variable address */
1255 unsigned flags = CF_INT;
1256 flags |= GlobalModeFlags (lval->Flags);
1257 if (IsTypeArray (tptr1)) {
1258 g_addaddr_static (flags, lval->Name, lval->ConstVal);
1260 g_addstatic (flags, lval->Name, lval->ConstVal);
1266 lval->Flags = E_MEXPR;
1269 return !IsTypeArray (lval->Type);
1275 static int structref (int k, ExprDesc* lval)
1276 /* Process struct field after . or ->. */
1282 /* Skip the token and check for an identifier */
1284 if (CurTok.Tok != TOK_IDENT) {
1285 Error ("Identifier expected");
1286 lval->Type = type_int;
1290 /* Get the symbol table entry and check for a struct field */
1291 strcpy (Ident, CurTok.Ident);
1293 Field = FindStructField (lval->Type, Ident);
1295 Error ("Struct/union has no field named `%s'", Ident);
1296 lval->Type = type_int;
1300 /* If we have constant input data, the result is also constant */
1301 flags = lval->Flags & ~E_MCTYPE;
1302 if (flags == E_MCONST ||
1303 (k == 0 && (flags == E_MLOCAL ||
1304 (flags & E_MGLOBAL) != 0 ||
1305 lval->Flags == E_MEOFFS))) {
1306 lval->ConstVal += Field->V.Offs;
1308 if ((flags & E_MEXPR) == 0 || k != 0) {
1309 exprhs (CF_NONE, k, lval);
1311 lval->ConstVal = Field->V.Offs;
1312 lval->Flags = E_MEOFFS;
1314 lval->Type = Field->Type;
1315 return !IsTypeArray (Field->Type);
1320 static int hie11 (ExprDesc *lval)
1321 /* Handle compound types (structs and arrays) */
1328 if (CurTok.Tok < TOK_LBRACK || CurTok.Tok > TOK_PTR_REF) {
1335 if (CurTok.Tok == TOK_LBRACK) {
1337 /* Array reference */
1338 k = arrayref (k, lval);
1340 } else if (CurTok.Tok == TOK_LPAREN) {
1342 /* Function call. Skip the opening parenthesis */
1345 if (IsTypeFunc (lval->Type) || IsTypeFuncPtr (lval->Type)) {
1347 /* Call the function */
1348 FunctionCall (k, lval);
1350 /* Result is in the primary register */
1351 lval->Flags = E_MEXPR;
1354 lval->Type = GetFuncReturn (lval->Type);
1357 Error ("Illegal function call");
1361 } else if (CurTok.Tok == TOK_DOT) {
1363 if (!IsClassStruct (lval->Type)) {
1364 Error ("Struct expected");
1366 k = structref (0, lval);
1368 } else if (CurTok.Tok == TOK_PTR_REF) {
1371 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1372 Error ("Struct pointer expected");
1374 k = structref (k, lval);
1384 void Store (ExprDesc* lval, const type* StoreType)
1385 /* Store the primary register into the location denoted by lval. If StoreType
1386 * is given, use this type when storing instead of lval->Type. If StoreType
1387 * is NULL, use lval->Type instead.
1392 unsigned f = lval->Flags;
1394 /* If StoreType was not given, use lval->Type instead */
1395 if (StoreType == 0) {
1396 StoreType = lval->Type;
1399 /* Get the code generator flags */
1400 Flags = TypeOf (StoreType);
1401 if (f & E_MGLOBAL) {
1402 Flags |= GlobalModeFlags (f);
1409 g_putstatic (Flags, lval->Name, lval->ConstVal);
1411 } else if (f & E_MLOCAL) {
1412 /* Store an auto variable */
1413 g_putlocal (Flags, lval->ConstVal, 0);
1414 } else if (f == E_MEOFFS) {
1415 /* Store indirect with offset */
1416 g_putind (Flags, lval->ConstVal);
1417 } else if (f != E_MREG) {
1419 /* Indirect without offset */
1420 g_putind (Flags, 0);
1422 /* Store into absolute address */
1423 g_putstatic (Flags | CF_ABSOLUTE, lval->ConstVal, 0);
1427 /* Assume that each one of the stores will invalidate CC */
1428 lval->Test &= ~E_CC;
1433 static void pre_incdec (ExprDesc* lval, void (*inc) (unsigned, unsigned long))
1434 /* Handle --i and ++i */
1441 if ((k = hie10 (lval)) == 0) {
1442 Error ("Invalid lvalue");
1446 /* Get the data type */
1447 flags = TypeOf (lval->Type) | CF_FORCECHAR | CF_CONST;
1449 /* Get the increment value in bytes */
1450 val = (lval->Type [0] == T_PTR)? CheckedPSizeOf (lval->Type) : 1;
1452 /* We're currently only able to handle some adressing modes */
1453 if ((lval->Flags & E_MGLOBAL) == 0 && /* Global address? */
1454 (lval->Flags & E_MLOCAL) == 0 && /* Local address? */
1455 (lval->Flags & E_MCONST) == 0 && /* Constant address? */
1456 (lval->Flags & E_MEXPR) == 0) { /* Address in a/x? */
1458 /* Use generic code. Push the address if needed */
1461 /* Fetch the value */
1462 exprhs (CF_NONE, k, lval);
1464 /* Increment value in primary */
1467 /* Store the result back */
1472 /* Special code for some addressing modes - use the special += ops */
1473 if (lval->Flags & E_MGLOBAL) {
1474 flags |= GlobalModeFlags (lval->Flags);
1476 g_addeqstatic (flags, lval->Name, lval->ConstVal, val);
1478 g_subeqstatic (flags, lval->Name, lval->ConstVal, val);
1480 } else if (lval->Flags & E_MLOCAL) {
1481 /* ref to localvar */
1483 g_addeqlocal (flags, lval->ConstVal, val);
1485 g_subeqlocal (flags, lval->ConstVal, val);
1487 } else if (lval->Flags & E_MCONST) {
1488 /* ref to absolute address */
1489 flags |= CF_ABSOLUTE;
1491 g_addeqstatic (flags, lval->ConstVal, 0, val);
1493 g_subeqstatic (flags, lval->ConstVal, 0, val);
1495 } else if (lval->Flags & E_MEXPR) {
1496 /* Address in a/x, check if we have an offset */
1497 unsigned Offs = (lval->Flags == E_MEOFFS)? lval->ConstVal : 0;
1499 g_addeqind (flags, Offs, val);
1501 g_subeqind (flags, Offs, val);
1504 Internal ("Invalid addressing mode");
1509 /* Result is an expression */
1510 lval->Flags = E_MEXPR;
1515 static void post_incdec (ExprDesc* lval, int k, void (*inc) (unsigned, unsigned long))
1516 /* Handle i-- and i++ */
1522 Error ("Invalid lvalue");
1526 /* Get the data type */
1527 flags = TypeOf (lval->Type);
1529 /* Push the address if needed */
1532 /* Fetch the value and save it (since it's the result of the expression) */
1533 exprhs (CF_NONE, 1, lval);
1534 g_save (flags | CF_FORCECHAR);
1536 /* If we have a pointer expression, increment by the size of the type */
1537 if (lval->Type[0] == T_PTR) {
1538 inc (flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (lval->Type + 1));
1540 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1543 /* Store the result back */
1546 /* Restore the original value */
1547 g_restore (flags | CF_FORCECHAR);
1548 lval->Flags = E_MEXPR;
1553 static void unaryop (int tok, ExprDesc* lval)
1554 /* Handle unary -/+ and ~ */
1561 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
1562 /* Value is constant */
1564 case TOK_MINUS: lval->ConstVal = -lval->ConstVal; break;
1565 case TOK_PLUS: break;
1566 case TOK_COMP: lval->ConstVal = ~lval->ConstVal; break;
1567 default: Internal ("Unexpected token: %d", tok);
1570 /* Value is not constant */
1571 exprhs (CF_NONE, k, lval);
1573 /* Get the type of the expression */
1574 flags = TypeOf (lval->Type);
1576 /* Handle the operation */
1578 case TOK_MINUS: g_neg (flags); break;
1579 case TOK_PLUS: break;
1580 case TOK_COMP: g_com (flags); break;
1581 default: Internal ("Unexpected token: %d", tok);
1583 lval->Flags = E_MEXPR;
1589 int hie10 (ExprDesc* lval)
1590 /* Handle ++, --, !, unary - etc. */
1595 switch (CurTok.Tok) {
1598 pre_incdec (lval, g_inc);
1602 pre_incdec (lval, g_dec);
1608 unaryop (CurTok.Tok, lval);
1613 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1614 /* Constant expression */
1615 lval->ConstVal = !lval->ConstVal;
1617 g_bneg (TypeOf (lval->Type));
1618 lval->Test |= E_CC; /* bneg will set cc */
1619 lval->Flags = E_MEXPR; /* say it's an expr */
1621 return 0; /* expr not storable */
1625 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1626 /* Expression is not const, indirect value loaded into primary */
1627 lval->Flags = E_MEXPR;
1628 lval->ConstVal = 0; /* Offset is zero now */
1630 /* If the expression is already a pointer to function, the
1631 * additional dereferencing operator must be ignored.
1633 if (IsTypeFuncPtr (lval->Type)) {
1634 /* Expression not storable */
1637 if (IsClassPtr (lval->Type)) {
1638 lval->Type = Indirect (lval->Type);
1640 Error ("Illegal indirection");
1649 /* The & operator may be applied to any lvalue, and it may be
1650 * applied to functions, even if they're no lvalues.
1652 if (k == 0 && !IsTypeFunc (lval->Type)) {
1653 /* Allow the & operator with an array */
1654 if (!IsTypeArray (lval->Type)) {
1655 Error ("Illegal address");
1658 t = TypeAlloc (TypeLen (lval->Type) + 2);
1660 TypeCpy (t + 1, lval->Type);
1667 if (istypeexpr ()) {
1668 type Type[MAXTYPELEN];
1670 lval->ConstVal = CheckedSizeOf (ParseType (Type));
1673 /* Remember the output queue pointer */
1674 CodeMark Mark = GetCodePos ();
1676 lval->ConstVal = CheckedSizeOf (lval->Type);
1677 /* Remove any generated code */
1680 lval->Flags = E_MCONST | E_TCONST;
1681 lval->Type = type_uint;
1682 lval->Test &= ~E_CC;
1686 if (istypeexpr ()) {
1688 return TypeCast (lval);
1693 switch (CurTok.Tok) {
1695 post_incdec (lval, k, g_inc);
1699 post_incdec (lval, k, g_dec);
1709 static int hie_internal (const GenDesc** ops, /* List of generators */
1710 ExprDesc* lval, /* parent expr's lval */
1711 int (*hienext) (ExprDesc*),
1712 int* UsedGen) /* next higher level */
1713 /* Helper function */
1720 token_t tok; /* The operator token */
1721 unsigned ltype, type;
1722 int rconst; /* Operand is a constant */
1728 while ((Gen = FindGen (CurTok.Tok, ops)) != 0) {
1730 /* Tell the caller that we handled it's ops */
1733 /* All operators that call this function expect an int on the lhs */
1734 if (!IsClassInt (lval->Type)) {
1735 Error ("Integer expression expected");
1738 /* Remember the operator token, then skip it */
1742 /* Get the lhs on stack */
1743 Mark1 = GetCodePos ();
1744 ltype = TypeOf (lval->Type);
1745 if (k == 0 && lval->Flags == E_MCONST) {
1746 /* Constant value */
1747 Mark2 = GetCodePos ();
1748 g_push (ltype | CF_CONST, lval->ConstVal);
1750 /* Value not constant */
1751 exprhs (CF_NONE, k, lval);
1752 Mark2 = GetCodePos ();
1756 /* Get the right hand side */
1757 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1759 /* Check the type of the rhs */
1760 if (!IsClassInt (lval2.Type)) {
1761 Error ("Integer expression expected");
1764 /* Check for const operands */
1765 if (k == 0 && lval->Flags == E_MCONST && rconst) {
1767 /* Both operands are constant, remove the generated code */
1771 /* Evaluate the result */
1772 lval->ConstVal = kcalc (tok, lval->ConstVal, lval2.ConstVal);
1774 /* Get the type of the result */
1775 lval->Type = promoteint (lval->Type, lval2.Type);
1779 /* If the right hand side is constant, and the generator function
1780 * expects the lhs in the primary, remove the push of the primary
1783 unsigned rtype = TypeOf (lval2.Type);
1786 /* Second value is constant - check for div */
1789 if (tok == TOK_DIV && lval2.ConstVal == 0) {
1790 Error ("Division by zero");
1791 } else if (tok == TOK_MOD && lval2.ConstVal == 0) {
1792 Error ("Modulo operation with zero");
1794 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1797 ltype |= CF_REG; /* Value is in register */
1801 /* Determine the type of the operation result. */
1802 type |= g_typeadjust (ltype, rtype);
1803 lval->Type = promoteint (lval->Type, lval2.Type);
1806 Gen->Func (type, lval2.ConstVal);
1807 lval->Flags = E_MEXPR;
1810 /* We have a rvalue now */
1819 static int hie_compare (const GenDesc** ops, /* List of generators */
1820 ExprDesc* lval, /* parent expr's lval */
1821 int (*hienext) (ExprDesc*))
1822 /* Helper function for the compare operators */
1829 token_t tok; /* The operator token */
1831 int rconst; /* Operand is a constant */
1836 while ((Gen = FindGen (CurTok.Tok, ops)) != 0) {
1838 /* Remember the operator token, then skip it */
1842 /* Get the lhs on stack */
1843 Mark1 = GetCodePos ();
1844 ltype = TypeOf (lval->Type);
1845 if (k == 0 && lval->Flags == E_MCONST) {
1846 /* Constant value */
1847 Mark2 = GetCodePos ();
1848 g_push (ltype | CF_CONST, lval->ConstVal);
1850 /* Value not constant */
1851 exprhs (CF_NONE, k, lval);
1852 Mark2 = GetCodePos ();
1856 /* Get the right hand side */
1857 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1859 /* Make sure, the types are compatible */
1860 if (IsClassInt (lval->Type)) {
1861 if (!IsClassInt (lval2.Type) && !(IsClassPtr(lval2.Type) && IsNullPtr(lval))) {
1862 Error ("Incompatible types");
1864 } else if (IsClassPtr (lval->Type)) {
1865 if (IsClassPtr (lval2.Type)) {
1866 /* Both pointers are allowed in comparison if they point to
1867 * the same type, or if one of them is a void pointer.
1869 type* left = Indirect (lval->Type);
1870 type* right = Indirect (lval2.Type);
1871 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1872 /* Incomatible pointers */
1873 Error ("Incompatible types");
1875 } else if (!IsNullPtr (&lval2)) {
1876 Error ("Incompatible types");
1880 /* Check for const operands */
1881 if (k == 0 && lval->Flags == E_MCONST && rconst) {
1883 /* Both operands are constant, remove the generated code */
1887 /* Evaluate the result */
1888 lval->ConstVal = kcalc (tok, lval->ConstVal, lval2.ConstVal);
1892 /* If the right hand side is constant, and the generator function
1893 * expects the lhs in the primary, remove the push of the primary
1899 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1902 ltype |= CF_REG; /* Value is in register */
1906 /* Determine the type of the operation result. If the left
1907 * operand is of type char and the right is a constant, or
1908 * if both operands are of type char, we will encode the
1909 * operation as char operation. Otherwise the default
1910 * promotions are used.
1912 if (IsTypeChar (lval->Type) && (IsTypeChar (lval2.Type) || rconst)) {
1914 if (IsSignUnsigned (lval->Type) || IsSignUnsigned (lval2.Type)) {
1915 flags |= CF_UNSIGNED;
1918 flags |= CF_FORCECHAR;
1921 unsigned rtype = TypeOf (lval2.Type) | (flags & CF_CONST);
1922 flags |= g_typeadjust (ltype, rtype);
1926 Gen->Func (flags, lval2.ConstVal);
1927 lval->Flags = E_MEXPR;
1930 /* Result type is always int */
1931 lval->Type = type_int;
1933 /* We have a rvalue now, condition codes are set */
1943 static int hie9 (ExprDesc *lval)
1944 /* Process * and / operators. */
1946 static const GenDesc* hie9_ops [] = {
1947 &GenMUL, &GenDIV, &GenMOD, 0
1951 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1956 static void parseadd (int k, ExprDesc* lval)
1957 /* Parse an expression with the binary plus operator. lval contains the
1958 * unprocessed left hand side of the expression and will contain the
1959 * result of the expression on return.
1963 unsigned flags; /* Operation flags */
1964 CodeMark Mark; /* Remember code position */
1965 type* lhst; /* Type of left hand side */
1966 type* rhst; /* Type of right hand side */
1969 /* Skip the PLUS token */
1972 /* Get the left hand side type, initialize operation flags */
1976 /* Check for constness on both sides */
1977 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
1979 /* The left hand side is a constant. Good. Get rhs */
1981 if (k == 0 && lval2.Flags == E_MCONST) {
1983 /* Right hand side is also constant. Get the rhs type */
1986 /* Both expressions are constants. Check for pointer arithmetic */
1987 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1988 /* Left is pointer, right is int, must scale rhs */
1989 lval->ConstVal += lval2.ConstVal * CheckedPSizeOf (lhst);
1990 /* Result type is a pointer */
1991 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1992 /* Left is int, right is pointer, must scale lhs */
1993 lval->ConstVal = lval->ConstVal * CheckedPSizeOf (rhst) + lval2.ConstVal;
1994 /* Result type is a pointer */
1995 lval->Type = lval2.Type;
1996 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1997 /* Integer addition */
1998 lval->ConstVal += lval2.ConstVal;
1999 typeadjust (lval, &lval2, 1);
2002 Error ("Invalid operands for binary operator `+'");
2005 /* Result is constant, condition codes not set */
2006 lval->Test &= ~E_CC;
2010 /* lhs is a constant and rhs is not constant. Load rhs into
2013 exprhs (CF_NONE, k, &lval2);
2015 /* Beware: The check above (for lhs) lets not only pass numeric
2016 * constants, but also constant addresses (labels), maybe even
2017 * with an offset. We have to check for that here.
2020 /* First, get the rhs type. */
2024 if (lval->Flags == E_MCONST) {
2025 /* A numerical constant */
2028 /* Constant address label */
2029 flags |= GlobalModeFlags (lval->Flags) | CF_CONSTADDR;
2032 /* Check for pointer arithmetic */
2033 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2034 /* Left is pointer, right is int, must scale rhs */
2035 g_scale (CF_INT, CheckedPSizeOf (lhst));
2036 /* Operate on pointers, result type is a pointer */
2038 /* Generate the code for the add */
2039 if (lval->Flags == E_MCONST) {
2040 /* Numeric constant */
2041 g_inc (flags, lval->ConstVal);
2043 /* Constant address */
2044 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2046 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2048 /* Left is int, right is pointer, must scale lhs. */
2049 unsigned ScaleFactor = CheckedPSizeOf (rhst);
2051 /* Operate on pointers, result type is a pointer */
2053 lval->Type = lval2.Type;
2055 /* Since we do already have rhs in the primary, if lhs is
2056 * not a numeric constant, and the scale factor is not one
2057 * (no scaling), we must take the long way over the stack.
2059 if (lval->Flags == E_MCONST) {
2060 /* Numeric constant, scale lhs */
2061 lval->ConstVal *= ScaleFactor;
2062 /* Generate the code for the add */
2063 g_inc (flags, lval->ConstVal);
2064 } else if (ScaleFactor == 1) {
2065 /* Constant address but no need to scale */
2066 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2068 /* Constant address that must be scaled */
2069 g_push (TypeOf (lval2.Type), 0); /* rhs --> stack */
2070 g_getimmed (flags, lval->Name, lval->ConstVal);
2071 g_scale (CF_PTR, ScaleFactor);
2074 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2075 /* Integer addition */
2076 flags |= typeadjust (lval, &lval2, 1);
2077 /* Generate the code for the add */
2078 if (lval->Flags == E_MCONST) {
2079 /* Numeric constant */
2080 g_inc (flags, lval->ConstVal);
2082 /* Constant address */
2083 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2087 Error ("Invalid operands for binary operator `+'");
2090 /* Result is in primary register */
2091 lval->Flags = E_MEXPR;
2092 lval->Test &= ~E_CC;
2098 /* Left hand side is not constant. Get the value onto the stack. */
2099 exprhs (CF_NONE, k, lval); /* --> primary register */
2100 Mark = GetCodePos ();
2101 g_push (TypeOf (lval->Type), 0); /* --> stack */
2103 /* Evaluate the rhs */
2104 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2106 /* Right hand side is a constant. Get the rhs type */
2109 /* Remove pushed value from stack */
2111 pop (TypeOf (lval->Type));
2113 /* Check for pointer arithmetic */
2114 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2115 /* Left is pointer, right is int, must scale rhs */
2116 lval2.ConstVal *= CheckedPSizeOf (lhst);
2117 /* Operate on pointers, result type is a pointer */
2119 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2120 /* Left is int, right is pointer, must scale lhs (ptr only) */
2121 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2122 /* Operate on pointers, result type is a pointer */
2124 lval->Type = lval2.Type;
2125 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2126 /* Integer addition */
2127 flags = typeadjust (lval, &lval2, 1);
2130 Error ("Invalid operands for binary operator `+'");
2133 /* Generate code for the add */
2134 g_inc (flags | CF_CONST, lval2.ConstVal);
2136 /* Result is in primary register */
2137 lval->Flags = E_MEXPR;
2138 lval->Test &= ~E_CC;
2142 /* lhs and rhs are not constant. Get the rhs type. */
2145 /* Check for pointer arithmetic */
2146 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2147 /* Left is pointer, right is int, must scale rhs */
2148 g_scale (CF_INT, CheckedPSizeOf (lhst));
2149 /* Operate on pointers, result type is a pointer */
2151 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2152 /* Left is int, right is pointer, must scale lhs */
2153 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2154 g_swap (CF_INT); /* Swap TOS and primary */
2155 g_scale (CF_INT, CheckedPSizeOf (rhst));
2156 /* Operate on pointers, result type is a pointer */
2158 lval->Type = lval2.Type;
2159 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2160 /* Integer addition. Note: Result is never constant.
2161 * Problem here is that typeadjust does not know if the
2162 * variable is an rvalue or lvalue, so if both operands
2163 * are dereferenced constant numeric addresses, typeadjust
2164 * thinks the operation works on constants. Removing
2165 * CF_CONST here means handling the symptoms, however, the
2166 * whole parser is such a mess that I fear to break anything
2167 * when trying to apply another solution.
2169 flags = typeadjust (lval, &lval2, 0) & ~CF_CONST;
2172 Error ("Invalid operands for binary operator `+'");
2175 /* Generate code for the add */
2178 /* Result is in primary register */
2179 lval->Flags = E_MEXPR;
2180 lval->Test &= ~E_CC;
2189 static void parsesub (int k, ExprDesc* lval)
2190 /* Parse an expression with the binary minus operator. lval contains the
2191 * unprocessed left hand side of the expression and will contain the
2192 * result of the expression on return.
2196 unsigned flags; /* Operation flags */
2197 type* lhst; /* Type of left hand side */
2198 type* rhst; /* Type of right hand side */
2199 CodeMark Mark1; /* Save position of output queue */
2200 CodeMark Mark2; /* Another position in the queue */
2201 int rscale; /* Scale factor for the result */
2204 /* Skip the MINUS token */
2207 /* Get the left hand side type, initialize operation flags */
2210 rscale = 1; /* Scale by 1, that is, don't scale */
2212 /* Remember the output queue position, then bring the value onto the stack */
2213 Mark1 = GetCodePos ();
2214 exprhs (CF_NONE, k, lval); /* --> primary register */
2215 Mark2 = GetCodePos ();
2216 g_push (TypeOf (lhst), 0); /* --> stack */
2218 /* Parse the right hand side */
2219 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2221 /* The right hand side is constant. Get the rhs type. */
2224 /* Check left hand side */
2225 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
2227 /* Both sides are constant, remove generated code */
2229 pop (TypeOf (lhst)); /* Clean up the stack */
2231 /* Check for pointer arithmetic */
2232 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2233 /* Left is pointer, right is int, must scale rhs */
2234 lval->ConstVal -= lval2.ConstVal * CheckedPSizeOf (lhst);
2235 /* Operate on pointers, result type is a pointer */
2236 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2237 /* Left is pointer, right is pointer, must scale result */
2238 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2239 Error ("Incompatible pointer types");
2241 lval->ConstVal = (lval->ConstVal - lval2.ConstVal) /
2242 CheckedPSizeOf (lhst);
2244 /* Operate on pointers, result type is an integer */
2245 lval->Type = type_int;
2246 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2247 /* Integer subtraction */
2248 typeadjust (lval, &lval2, 1);
2249 lval->ConstVal -= lval2.ConstVal;
2252 Error ("Invalid operands for binary operator `-'");
2255 /* Result is constant, condition codes not set */
2256 /* lval->Flags = E_MCONST; ### */
2257 lval->Test &= ~E_CC;
2261 /* Left hand side is not constant, right hand side is.
2262 * Remove pushed value from stack.
2265 pop (TypeOf (lhst));
2267 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2268 /* Left is pointer, right is int, must scale rhs */
2269 lval2.ConstVal *= CheckedPSizeOf (lhst);
2270 /* Operate on pointers, result type is a pointer */
2272 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2273 /* Left is pointer, right is pointer, must scale result */
2274 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2275 Error ("Incompatible pointer types");
2277 rscale = CheckedPSizeOf (lhst);
2279 /* Operate on pointers, result type is an integer */
2281 lval->Type = type_int;
2282 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2283 /* Integer subtraction */
2284 flags = typeadjust (lval, &lval2, 1);
2287 Error ("Invalid operands for binary operator `-'");
2290 /* Do the subtraction */
2291 g_dec (flags | CF_CONST, lval2.ConstVal);
2293 /* If this was a pointer subtraction, we must scale the result */
2295 g_scale (flags, -rscale);
2298 /* Result is in primary register */
2299 lval->Flags = E_MEXPR;
2300 lval->Test &= ~E_CC;
2306 /* Right hand side is not constant. Get the rhs type. */
2309 /* Check for pointer arithmetic */
2310 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2311 /* Left is pointer, right is int, must scale rhs */
2312 g_scale (CF_INT, CheckedPSizeOf (lhst));
2313 /* Operate on pointers, result type is a pointer */
2315 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2316 /* Left is pointer, right is pointer, must scale result */
2317 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2318 Error ("Incompatible pointer types");
2320 rscale = CheckedPSizeOf (lhst);
2322 /* Operate on pointers, result type is an integer */
2324 lval->Type = type_int;
2325 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2326 /* Integer subtraction. If the left hand side descriptor says that
2327 * the lhs is const, we have to remove this mark, since this is no
2328 * longer true, lhs is on stack instead.
2330 if (lval->Flags == E_MCONST) {
2331 lval->Flags = E_MEXPR;
2333 /* Adjust operand types */
2334 flags = typeadjust (lval, &lval2, 0);
2337 Error ("Invalid operands for binary operator `-'");
2340 /* Generate code for the sub (the & is a hack here) */
2341 g_sub (flags & ~CF_CONST, 0);
2343 /* If this was a pointer subtraction, we must scale the result */
2345 g_scale (flags, -rscale);
2348 /* Result is in primary register */
2349 lval->Flags = E_MEXPR;
2350 lval->Test &= ~E_CC;
2356 static int hie8 (ExprDesc* lval)
2357 /* Process + and - binary operators. */
2359 int k = hie9 (lval);
2360 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2362 if (CurTok.Tok == TOK_PLUS) {
2375 static int hie7 (ExprDesc *lval)
2376 /* Parse << and >>. */
2378 static const GenDesc* hie7_ops [] = {
2383 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2388 static int hie6 (ExprDesc *lval)
2389 /* process greater-than type comparators */
2391 static const GenDesc* hie6_ops [] = {
2392 &GenLT, &GenLE, &GenGE, &GenGT, 0
2394 return hie_compare (hie6_ops, lval, hie7);
2399 static int hie5 (ExprDesc *lval)
2401 static const GenDesc* hie5_ops[] = {
2404 return hie_compare (hie5_ops, lval, hie6);
2409 static int hie4 (ExprDesc* lval)
2410 /* Handle & (bitwise and) */
2412 static const GenDesc* hie4_ops [] = {
2417 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2422 static int hie3 (ExprDesc *lval)
2423 /* Handle ^ (bitwise exclusive or) */
2425 static const GenDesc* hie3_ops [] = {
2430 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2435 static int hie2 (ExprDesc *lval)
2436 /* Handle | (bitwise or) */
2438 static const GenDesc* hie2_ops [] = {
2443 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2448 static int hieAndPP (ExprDesc* lval)
2449 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2450 * called recursively from the preprocessor.
2455 ConstSubExpr (hie2, lval);
2456 while (CurTok.Tok == TOK_BOOL_AND) {
2458 /* Left hand side must be an int */
2459 if (!IsClassInt (lval->Type)) {
2460 Error ("Left hand side must be of integer type");
2461 MakeConstIntExpr (lval, 1);
2468 ConstSubExpr (hie2, &lval2);
2470 /* Since we are in PP mode, all we know about is integers */
2471 if (!IsClassInt (lval2.Type)) {
2472 Error ("Right hand side must be of integer type");
2473 MakeConstIntExpr (&lval2, 1);
2476 /* Combine the two */
2477 lval->ConstVal = (lval->ConstVal && lval2.ConstVal);
2480 /* Always a rvalue */
2486 static int hieOrPP (ExprDesc *lval)
2487 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2488 * called recursively from the preprocessor.
2493 ConstSubExpr (hieAndPP, lval);
2494 while (CurTok.Tok == TOK_BOOL_OR) {
2496 /* Left hand side must be an int */
2497 if (!IsClassInt (lval->Type)) {
2498 Error ("Left hand side must be of integer type");
2499 MakeConstIntExpr (lval, 1);
2506 ConstSubExpr (hieAndPP, &lval2);
2508 /* Since we are in PP mode, all we know about is integers */
2509 if (!IsClassInt (lval2.Type)) {
2510 Error ("Right hand side must be of integer type");
2511 MakeConstIntExpr (&lval2, 1);
2514 /* Combine the two */
2515 lval->ConstVal = (lval->ConstVal || lval2.ConstVal);
2518 /* Always a rvalue */
2524 static int hieAnd (ExprDesc* lval, unsigned TrueLab, int* BoolOp)
2525 /* Process "exp && exp" */
2532 if (CurTok.Tok == TOK_BOOL_AND) {
2534 /* Tell our caller that we're evaluating a boolean */
2537 /* Get a label that we will use for false expressions */
2538 lab = GetLocalLabel ();
2540 /* If the expr hasn't set condition codes, set the force-test flag */
2541 if ((lval->Test & E_CC) == 0) {
2542 lval->Test |= E_FORCETEST;
2545 /* Load the value */
2546 exprhs (CF_FORCECHAR, k, lval);
2548 /* Generate the jump */
2549 g_falsejump (CF_NONE, lab);
2551 /* Parse more boolean and's */
2552 while (CurTok.Tok == TOK_BOOL_AND) {
2559 if ((lval2.Test & E_CC) == 0) {
2560 lval2.Test |= E_FORCETEST;
2562 exprhs (CF_FORCECHAR, k, &lval2);
2564 /* Do short circuit evaluation */
2565 if (CurTok.Tok == TOK_BOOL_AND) {
2566 g_falsejump (CF_NONE, lab);
2568 /* Last expression - will evaluate to true */
2569 g_truejump (CF_NONE, TrueLab);
2573 /* Define the false jump label here */
2574 g_defcodelabel (lab);
2576 /* Define the label */
2577 lval->Flags = E_MEXPR;
2578 lval->Test |= E_CC; /* Condition codes are set */
2586 static int hieOr (ExprDesc *lval)
2587 /* Process "exp || exp". */
2591 int BoolOp = 0; /* Did we have a boolean op? */
2592 int AndOp; /* Did we have a && operation? */
2593 unsigned TrueLab; /* Jump to this label if true */
2597 TrueLab = GetLocalLabel ();
2599 /* Call the next level parser */
2600 k = hieAnd (lval, TrueLab, &BoolOp);
2602 /* Any boolean or's? */
2603 if (CurTok.Tok == TOK_BOOL_OR) {
2605 /* If the expr hasn't set condition codes, set the force-test flag */
2606 if ((lval->Test & E_CC) == 0) {
2607 lval->Test |= E_FORCETEST;
2610 /* Get first expr */
2611 exprhs (CF_FORCECHAR, k, lval);
2613 /* For each expression jump to TrueLab if true. Beware: If we
2614 * had && operators, the jump is already in place!
2617 g_truejump (CF_NONE, TrueLab);
2620 /* Remember that we had a boolean op */
2623 /* while there's more expr */
2624 while (CurTok.Tok == TOK_BOOL_OR) {
2631 k = hieAnd (&lval2, TrueLab, &AndOp);
2632 if ((lval2.Test & E_CC) == 0) {
2633 lval2.Test |= E_FORCETEST;
2635 exprhs (CF_FORCECHAR, k, &lval2);
2637 /* If there is more to come, add shortcut boolean eval. */
2638 g_truejump (CF_NONE, TrueLab);
2641 lval->Flags = E_MEXPR;
2642 lval->Test |= E_CC; /* Condition codes are set */
2646 /* If we really had boolean ops, generate the end sequence */
2648 DoneLab = GetLocalLabel ();
2649 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2650 g_falsejump (CF_NONE, DoneLab);
2651 g_defcodelabel (TrueLab);
2652 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2653 g_defcodelabel (DoneLab);
2660 static int hieQuest (ExprDesc *lval)
2661 /* Parse "lvalue ? exp : exp" */
2666 ExprDesc lval2; /* Expression 2 */
2667 ExprDesc lval3; /* Expression 3 */
2668 type* type2; /* Type of expression 2 */
2669 type* type3; /* Type of expression 3 */
2670 type* rtype; /* Type of result */
2673 k = Preprocessing? hieOrPP (lval) : hieOr (lval);
2674 if (CurTok.Tok == TOK_QUEST) {
2676 if ((lval->Test & E_CC) == 0) {
2677 /* Condition codes not set, force a test */
2678 lval->Test |= E_FORCETEST;
2680 exprhs (CF_NONE, k, lval);
2681 labf = GetLocalLabel ();
2682 g_falsejump (CF_NONE, labf);
2684 /* Parse second expression */
2685 k = expr (hie1, &lval2);
2687 if (!IsTypeVoid (lval2.Type)) {
2688 /* Load it into the primary */
2689 exprhs (CF_NONE, k, &lval2);
2691 labt = GetLocalLabel ();
2695 /* Parse the third expression */
2696 g_defcodelabel (labf);
2697 k = expr (hie1, &lval3);
2699 if (!IsTypeVoid (lval3.Type)) {
2700 /* Load it into the primary */
2701 exprhs (CF_NONE, k, &lval3);
2704 /* Check if any conversions are needed, if so, do them.
2705 * Conversion rules for ?: expression are:
2706 * - if both expressions are int expressions, default promotion
2707 * rules for ints apply.
2708 * - if both expressions are pointers of the same type, the
2709 * result of the expression is of this type.
2710 * - if one of the expressions is a pointer and the other is
2711 * a zero constant, the resulting type is that of the pointer
2713 * - if both expressions are void expressions, the result is of
2715 * - all other cases are flagged by an error.
2717 if (IsClassInt (type2) && IsClassInt (type3)) {
2719 /* Get common type */
2720 rtype = promoteint (type2, type3);
2722 /* Convert the third expression to this type if needed */
2723 g_typecast (TypeOf (rtype), TypeOf (type3));
2725 /* Setup a new label so that the expr3 code will jump around
2726 * the type cast code for expr2.
2728 labf = GetLocalLabel (); /* Get new label */
2729 g_jump (labf); /* Jump around code */
2731 /* The jump for expr2 goes here */
2732 g_defcodelabel (labt);
2734 /* Create the typecast code for expr2 */
2735 g_typecast (TypeOf (rtype), TypeOf (type2));
2737 /* Jump here around the typecase code. */
2738 g_defcodelabel (labf);
2739 labt = 0; /* Mark other label as invalid */
2741 } else if (IsClassPtr (type2) && IsClassPtr (type3)) {
2742 /* Must point to same type */
2743 if (TypeCmp (Indirect (type2), Indirect (type3)) < TC_EQUAL) {
2744 Error ("Incompatible pointer types");
2746 /* Result has the common type */
2748 } else if (IsClassPtr (type2) && IsNullPtr (&lval3)) {
2749 /* Result type is pointer, no cast needed */
2751 } else if (IsNullPtr (&lval2) && IsClassPtr (type3)) {
2752 /* Result type is pointer, no cast needed */
2754 } else if (IsTypeVoid (type2) && IsTypeVoid (type3)) {
2755 /* Result type is void */
2758 Error ("Incompatible types");
2759 rtype = lval2.Type; /* Doesn't matter here */
2762 /* If we don't have the label defined until now, do it */
2764 g_defcodelabel (labt);
2767 /* Setup the target expression */
2768 lval->Flags = E_MEXPR;
2777 static void opeq (const GenDesc* Gen, ExprDesc *lval, int k)
2778 /* Process "op=" operators. */
2787 Error ("Invalid lvalue in assignment");
2791 /* Determine the type of the lhs */
2792 flags = TypeOf (lval->Type);
2793 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2794 lval->Type [0] == T_PTR;
2796 /* Get the lhs address on stack (if needed) */
2799 /* Fetch the lhs into the primary register if needed */
2800 exprhs (CF_NONE, k, lval);
2802 /* Bring the lhs on stack */
2803 Mark = GetCodePos ();
2806 /* Evaluate the rhs */
2807 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2808 /* The resulting value is a constant. If the generator has the NOPUSH
2809 * flag set, don't push the lhs.
2811 if (Gen->Flags & GEN_NOPUSH) {
2816 /* lhs is a pointer, scale rhs */
2817 lval2.ConstVal *= CheckedSizeOf (lval->Type+1);
2820 /* If the lhs is character sized, the operation may be later done
2823 if (CheckedSizeOf (lval->Type) == SIZEOF_CHAR) {
2824 flags |= CF_FORCECHAR;
2827 /* Special handling for add and sub - some sort of a hack, but short code */
2828 if (Gen->Func == g_add) {
2829 g_inc (flags | CF_CONST, lval2.ConstVal);
2830 } else if (Gen->Func == g_sub) {
2831 g_dec (flags | CF_CONST, lval2.ConstVal);
2833 Gen->Func (flags | CF_CONST, lval2.ConstVal);
2836 /* rhs is not constant and already in the primary register */
2838 /* lhs is a pointer, scale rhs */
2839 g_scale (TypeOf (lval2.Type), CheckedSizeOf (lval->Type+1));
2842 /* If the lhs is character sized, the operation may be later done
2845 if (CheckedSizeOf (lval->Type) == SIZEOF_CHAR) {
2846 flags |= CF_FORCECHAR;
2849 /* Adjust the types of the operands if needed */
2850 Gen->Func (g_typeadjust (flags, TypeOf (lval2.Type)), 0);
2853 lval->Flags = E_MEXPR;
2858 static void addsubeq (const GenDesc* Gen, ExprDesc *lval, int k)
2859 /* Process the += and -= operators */
2867 /* We must have an lvalue */
2869 Error ("Invalid lvalue in assignment");
2873 /* We're currently only able to handle some adressing modes */
2874 if ((lval->Flags & E_MGLOBAL) == 0 && /* Global address? */
2875 (lval->Flags & E_MLOCAL) == 0 && /* Local address? */
2876 (lval->Flags & E_MCONST) == 0) { /* Constant address? */
2877 /* Use generic routine */
2878 opeq (Gen, lval, k);
2882 /* Skip the operator */
2885 /* Check if we have a pointer expression and must scale rhs */
2886 MustScale = (lval->Type [0] == T_PTR);
2888 /* Initialize the code generator flags */
2892 /* Evaluate the rhs */
2893 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2894 /* The resulting value is a constant. */
2896 /* lhs is a pointer, scale rhs */
2897 lval2.ConstVal *= CheckedSizeOf (lval->Type+1);
2902 /* rhs is not constant and already in the primary register */
2904 /* lhs is a pointer, scale rhs */
2905 g_scale (TypeOf (lval2.Type), CheckedSizeOf (lval->Type+1));
2909 /* Setup the code generator flags */
2910 lflags |= TypeOf (lval->Type) | CF_FORCECHAR;
2911 rflags |= TypeOf (lval2.Type);
2913 /* Cast the rhs to the type of the lhs */
2914 g_typecast (lflags, rflags);
2916 /* Output apropriate code */
2917 if (lval->Flags & E_MGLOBAL) {
2918 /* Static variable */
2919 lflags |= GlobalModeFlags (lval->Flags);
2920 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2921 g_addeqstatic (lflags, lval->Name, lval->ConstVal, lval2.ConstVal);
2923 g_subeqstatic (lflags, lval->Name, lval->ConstVal, lval2.ConstVal);
2925 } else if (lval->Flags & E_MLOCAL) {
2926 /* ref to localvar */
2927 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2928 g_addeqlocal (lflags, lval->ConstVal, lval2.ConstVal);
2930 g_subeqlocal (lflags, lval->ConstVal, lval2.ConstVal);
2932 } else if (lval->Flags & E_MCONST) {
2933 /* ref to absolute address */
2934 lflags |= CF_ABSOLUTE;
2935 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2936 g_addeqstatic (lflags, lval->ConstVal, 0, lval2.ConstVal);
2938 g_subeqstatic (lflags, lval->ConstVal, 0, lval2.ConstVal);
2940 } else if (lval->Flags & E_MEXPR) {
2941 /* Address in a/x. */
2942 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2943 g_addeqind (lflags, lval->ConstVal, lval2.ConstVal);
2945 g_subeqind (lflags, lval->ConstVal, lval2.ConstVal);
2948 Internal ("Invalid addressing mode");
2951 /* Expression is in the primary now */
2952 lval->Flags = E_MEXPR;
2957 int hie1 (ExprDesc* lval)
2958 /* Parse first level of expression hierarchy. */
2962 k = hieQuest (lval);
2963 switch (CurTok.Tok) {
2972 Error ("Invalid lvalue in assignment");
2978 case TOK_PLUS_ASSIGN:
2979 addsubeq (&GenPASGN, lval, k);
2982 case TOK_MINUS_ASSIGN:
2983 addsubeq (&GenSASGN, lval, k);
2986 case TOK_MUL_ASSIGN:
2987 opeq (&GenMASGN, lval, k);
2990 case TOK_DIV_ASSIGN:
2991 opeq (&GenDASGN, lval, k);
2994 case TOK_MOD_ASSIGN:
2995 opeq (&GenMOASGN, lval, k);
2998 case TOK_SHL_ASSIGN:
2999 opeq (&GenSLASGN, lval, k);
3002 case TOK_SHR_ASSIGN:
3003 opeq (&GenSRASGN, lval, k);
3006 case TOK_AND_ASSIGN:
3007 opeq (&GenAASGN, lval, k);
3010 case TOK_XOR_ASSIGN:
3011 opeq (&GenXOASGN, lval, k);
3015 opeq (&GenOASGN, lval, k);
3026 static int hie0 (ExprDesc *lval)
3027 /* Parse comma operator. */
3032 while (CurTok.Tok == TOK_COMMA) {
3041 int evalexpr (unsigned flags, int (*f) (ExprDesc*), ExprDesc* lval)
3042 /* Will evaluate an expression via the given function. If the result is a
3043 * constant, 0 is returned and the value is put in the lval struct. If the
3044 * result is not constant, exprhs is called to bring the value into the
3045 * primary register and 1 is returned.
3052 if (k == 0 && lval->Flags == E_MCONST) {
3053 /* Constant expression */
3056 /* Not constant, load into the primary */
3057 exprhs (flags, k, lval);
3064 static int expr (int (*func) (ExprDesc*), ExprDesc *lval)
3065 /* Expression parser; func is either hie0 or hie1. */
3074 /* Do some checks if code generation is still constistent */
3075 if (savsp != oursp) {
3077 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
3079 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
3087 void expression1 (ExprDesc* lval)
3088 /* Evaluate an expression on level 1 (no comma operator) and put it into
3089 * the primary register
3092 InitExprDesc (lval);
3093 exprhs (CF_NONE, expr (hie1, lval), lval);
3098 void expression (ExprDesc* lval)
3099 /* Evaluate an expression and put it into the primary register */
3101 InitExprDesc (lval);
3102 exprhs (CF_NONE, expr (hie0, lval), lval);
3107 void ConstExpr (ExprDesc* lval)
3108 /* Get a constant value */
3110 InitExprDesc (lval);
3111 if (expr (hie1, lval) != 0 || (lval->Flags & E_MCONST) == 0) {
3112 Error ("Constant expression expected");
3113 /* To avoid any compiler errors, make the expression a valid const */
3114 MakeConstIntExpr (lval, 1);
3120 void ConstIntExpr (ExprDesc* Val)
3121 /* Get a constant int value */
3124 if (expr (hie1, Val) != 0 ||
3125 (Val->Flags & E_MCONST) == 0 ||
3126 !IsClassInt (Val->Type)) {
3127 Error ("Constant integer expression expected");
3128 /* To avoid any compiler errors, make the expression a valid const */
3129 MakeConstIntExpr (Val, 1);
3135 void intexpr (ExprDesc* lval)
3136 /* Get an integer expression */
3139 if (!IsClassInt (lval->Type)) {
3140 Error ("Integer expression expected");
3141 /* To avoid any compiler errors, make the expression a valid int */
3142 MakeConstIntExpr (lval, 1);
3148 void Test (unsigned Label, int Invert)
3149 /* Evaluate a boolean test expression and jump depending on the result of
3150 * the test and on Invert.
3156 /* Evaluate the expression */
3157 k = expr (hie0, InitExprDesc (&lval));
3159 /* Check for a boolean expression */
3160 CheckBoolExpr (&lval);
3162 /* Check for a constant expression */
3163 if (k == 0 && lval.Flags == E_MCONST) {
3165 /* Constant rvalue */
3166 if (!Invert && lval.ConstVal == 0) {
3168 Warning ("Unreachable code");
3169 } else if (Invert && lval.ConstVal != 0) {
3175 /* If the expr hasn't set condition codes, set the force-test flag */
3176 if ((lval.Test & E_CC) == 0) {
3177 lval.Test |= E_FORCETEST;
3180 /* Load the value into the primary register */
3181 exprhs (CF_FORCECHAR, k, &lval);
3183 /* Generate the jump */
3185 g_truejump (CF_NONE, Label);
3187 g_falsejump (CF_NONE, Label);
3194 void TestInParens (unsigned Label, int Invert)
3195 /* Evaluate a boolean test expression in parenthesis and jump depending on
3196 * the result of the test * and on Invert.
3199 /* Eat the parenthesis */
3203 Test (Label, Invert);
3205 /* Check for the closing brace */