4 * Ullrich von Bassewitz, 21.06.1998
20 #include "assignment.h"
39 /*****************************************************************************/
41 /*****************************************************************************/
45 /* Generator attributes */
46 #define GEN_NOPUSH 0x01 /* Don't push lhs */
48 /* Map a generator function and its attributes to a token */
50 token_t Tok; /* Token to map to */
51 unsigned Flags; /* Flags for generator function */
52 void (*Func) (unsigned, unsigned long); /* Generator func */
55 /* Descriptors for the operations */
56 static GenDesc GenMUL = { TOK_STAR, GEN_NOPUSH, g_mul };
57 static GenDesc GenDIV = { TOK_DIV, GEN_NOPUSH, g_div };
58 static GenDesc GenMOD = { TOK_MOD, GEN_NOPUSH, g_mod };
59 static GenDesc GenASL = { TOK_SHL, GEN_NOPUSH, g_asl };
60 static GenDesc GenASR = { TOK_SHR, GEN_NOPUSH, g_asr };
61 static GenDesc GenLT = { TOK_LT, GEN_NOPUSH, g_lt };
62 static GenDesc GenLE = { TOK_LE, GEN_NOPUSH, g_le };
63 static GenDesc GenGE = { TOK_GE, GEN_NOPUSH, g_ge };
64 static GenDesc GenGT = { TOK_GT, GEN_NOPUSH, g_gt };
65 static GenDesc GenEQ = { TOK_EQ, GEN_NOPUSH, g_eq };
66 static GenDesc GenNE = { TOK_NE, GEN_NOPUSH, g_ne };
67 static GenDesc GenAND = { TOK_AND, GEN_NOPUSH, g_and };
68 static GenDesc GenXOR = { TOK_XOR, GEN_NOPUSH, g_xor };
69 static GenDesc GenOR = { TOK_OR, GEN_NOPUSH, g_or };
70 static GenDesc GenPASGN = { TOK_PLUS_ASSIGN, GEN_NOPUSH, g_add };
71 static GenDesc GenSASGN = { TOK_MINUS_ASSIGN, GEN_NOPUSH, g_sub };
72 static GenDesc GenMASGN = { TOK_MUL_ASSIGN, GEN_NOPUSH, g_mul };
73 static GenDesc GenDASGN = { TOK_DIV_ASSIGN, GEN_NOPUSH, g_div };
74 static GenDesc GenMOASGN = { TOK_MOD_ASSIGN, GEN_NOPUSH, g_mod };
75 static GenDesc GenSLASGN = { TOK_SHL_ASSIGN, GEN_NOPUSH, g_asl };
76 static GenDesc GenSRASGN = { TOK_SHR_ASSIGN, GEN_NOPUSH, g_asr };
77 static GenDesc GenAASGN = { TOK_AND_ASSIGN, GEN_NOPUSH, g_and };
78 static GenDesc GenXOASGN = { TOK_XOR_ASSIGN, GEN_NOPUSH, g_xor };
79 static GenDesc GenOASGN = { TOK_OR_ASSIGN, GEN_NOPUSH, g_or };
83 /*****************************************************************************/
84 /* Function forwards */
85 /*****************************************************************************/
89 static int hie0 (ExprDesc *lval);
90 /* Parse comma operator. */
92 static int expr (int (*func) (ExprDesc*), ExprDesc *lval);
93 /* Expression parser; func is either hie0 or hie1. */
97 /*****************************************************************************/
98 /* Helper functions */
99 /*****************************************************************************/
103 static unsigned GlobalModeFlags (unsigned flags)
104 /* Return the addressing mode flags for the variable with the given flags */
107 if (flags == E_TGLAB) {
108 /* External linkage */
110 } else if (flags == E_TREGISTER) {
111 /* Register variable */
121 static int IsNullPtr (ExprDesc* lval)
122 /* Return true if this is the NULL pointer constant */
124 return (IsClassInt (lval->Type) && /* Is it an int? */
125 lval->Flags == E_MCONST && /* Is it constant? */
126 lval->ConstVal == 0); /* And is it's value zero? */
131 static type* promoteint (type* lhst, type* rhst)
132 /* In an expression with two ints, return the type of the result */
134 /* Rules for integer types:
135 * - If one of the values is a long, the result is long.
136 * - If one of the values is unsigned, the result is also unsigned.
137 * - Otherwise the result is an int.
139 if (IsTypeLong (lhst) || IsTypeLong (rhst)) {
140 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
146 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
156 static unsigned typeadjust (ExprDesc* lhs, ExprDesc* rhs, int NoPush)
157 /* Adjust the two values for a binary operation. lhs is expected on stack or
158 * to be constant, rhs is expected to be in the primary register or constant.
159 * The function will put the type of the result into lhs and return the
160 * code generator flags for the operation.
161 * If NoPush is given, it is assumed that the operation does not expect the lhs
162 * to be on stack, and that lhs is in a register instead.
163 * Beware: The function does only accept int types.
166 unsigned ltype, rtype;
169 /* Get the type strings */
170 type* lhst = lhs->Type;
171 type* rhst = rhs->Type;
173 /* Generate type adjustment code if needed */
174 ltype = TypeOf (lhst);
175 if (lhs->Flags == E_MCONST) {
179 /* Value is in primary register*/
182 rtype = TypeOf (rhst);
183 if (rhs->Flags == E_MCONST) {
186 flags = g_typeadjust (ltype, rtype);
188 /* Set the type of the result */
189 lhs->Type = promoteint (lhst, rhst);
191 /* Return the code generator flags */
197 unsigned assignadjust (type* lhst, ExprDesc* rhs)
198 /* Adjust the type of the right hand expression so that it can be assigned to
199 * the type on the left hand side. This function is used for assignment and
200 * for converting parameters in a function call. It returns the code generator
201 * flags for the operation. The type string of the right hand side will be
202 * set to the type of the left hand side.
205 /* Get the type of the right hand side. Treat function types as
206 * pointer-to-function
208 type* rhst = rhs->Type;
209 if (IsTypeFunc (rhst)) {
210 rhst = PointerTo (rhst);
213 /* After calling this function, rhs will have the type of the lhs */
216 /* First, do some type checking */
217 if (IsTypeVoid (lhst) || IsTypeVoid (rhst)) {
218 /* If one of the sides are of type void, output a more apropriate
221 Error ("Illegal type");
222 } else if (IsClassInt (lhst)) {
223 if (IsClassPtr (rhst)) {
224 /* Pointer -> int conversion */
225 Warning ("Converting pointer to integer without a cast");
226 } else if (!IsClassInt (rhst)) {
227 Error ("Incompatible types");
229 /* Convert the rhs to the type of the lhs. */
230 unsigned flags = TypeOf (rhst);
231 if (rhs->Flags == E_MCONST) {
234 return g_typecast (TypeOf (lhst), flags);
236 } else if (IsClassPtr (lhst)) {
237 if (IsClassPtr (rhst)) {
238 /* Pointer to pointer assignment is valid, if:
239 * - both point to the same types, or
240 * - the rhs pointer is a void pointer, or
241 * - the lhs pointer is a void pointer.
243 if (!IsTypeVoid (Indirect (lhst)) && !IsTypeVoid (Indirect (rhst))) {
244 /* Compare the types */
245 switch (TypeCmp (lhst, rhst)) {
247 case TC_INCOMPATIBLE:
248 Error ("Incompatible pointer types");
252 Error ("Pointer types differ in type qualifiers");
260 } else if (IsClassInt (rhst)) {
261 /* Int to pointer assignment is valid only for constant zero */
262 if (rhs->Flags != E_MCONST || rhs->ConstVal != 0) {
263 Warning ("Converting integer to pointer without a cast");
265 } else if (IsTypeFuncPtr (lhst) && IsTypeFunc(rhst)) {
266 /* Assignment of function to function pointer is allowed, provided
267 * that both functions have the same parameter list.
269 if (TypeCmp (Indirect (lhst), rhst) < TC_EQUAL) {
270 Error ("Incompatible types");
273 Error ("Incompatible types");
276 Error ("Incompatible types");
279 /* Return an int value in all cases where the operands are not both ints */
285 void DefineData (ExprDesc* Expr)
286 /* Output a data definition for the given expression */
288 unsigned Flags = Expr->Flags;
290 switch (Flags & E_MCTYPE) {
294 g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->ConstVal, 0);
298 /* Register variable. Taking the address is usually not
301 if (!AllowRegVarAddr) {
302 Error ("Cannot take the address of a register variable");
308 /* Local or global symbol */
309 g_defdata (GlobalModeFlags (Flags), Expr->Name, Expr->ConstVal);
313 /* a literal of some kind */
314 g_defdata (CF_STATIC, LiteralPoolLabel, Expr->ConstVal);
318 Internal ("Unknown constant type: %04X", Flags);
324 static void LoadConstant (unsigned Flags, ExprDesc* Expr)
325 /* Load the primary register with some constant value. */
327 switch (Expr->Flags & E_MCTYPE) {
330 g_leasp (Expr->ConstVal);
334 /* Number constant */
335 g_getimmed (Flags | TypeOf (Expr->Type) | CF_CONST, Expr->ConstVal, 0);
339 /* Register variable. Taking the address is usually not
342 if (!AllowRegVarAddr) {
343 Error ("Cannot take the address of a register variable");
349 /* Local or global symbol, load address */
350 Flags |= GlobalModeFlags (Expr->Flags);
352 g_getimmed (Flags, Expr->Name, Expr->ConstVal);
357 g_getimmed (CF_STATIC, LiteralPoolLabel, Expr->ConstVal);
361 Internal ("Unknown constant type: %04X", Expr->Flags);
367 static int kcalc (int tok, long val1, long val2)
368 /* Calculate an operation with left and right operand constant. */
372 return (val1 == val2);
374 return (val1 != val2);
376 return (val1 < val2);
378 return (val1 <= val2);
380 return (val1 >= val2);
382 return (val1 > val2);
384 return (val1 | val2);
386 return (val1 ^ val2);
388 return (val1 & val2);
390 return (val1 >> val2);
392 return (val1 << val2);
394 return (val1 * val2);
397 Error ("Division by zero");
400 return (val1 / val2);
403 Error ("Modulo operation with zero");
406 return (val1 % val2);
408 Internal ("kcalc: got token 0x%X\n", tok);
415 static const GenDesc* FindGen (token_t Tok, const GenDesc** Table)
416 /* Find a token in a generator table */
419 while ((G = *Table) != 0) {
430 static int istypeexpr (void)
431 /* Return true if some sort of variable or type is waiting (helper for cast
432 * and sizeof() in hie10).
437 return CurTok.Tok == TOK_LPAREN && (
438 (NextTok.Tok >= TOK_FIRSTTYPE && NextTok.Tok <= TOK_LASTTYPE) ||
439 (NextTok.Tok == TOK_CONST) ||
440 (NextTok.Tok == TOK_IDENT &&
441 (Entry = FindSym (NextTok.Ident)) != 0 &&
442 SymIsTypeDef (Entry)));
447 void PushAddr (ExprDesc* lval)
448 /* If the expression contains an address that was somehow evaluated,
449 * push this address on the stack. This is a helper function for all
450 * sorts of implicit or explicit assignment functions where the lvalue
451 * must be saved if it's not constant, before evaluating the rhs.
454 /* Get the address on stack if needed */
455 if (lval->Flags != E_MREG && (lval->Flags & E_MEXPR)) {
456 /* Push the address (always a pointer) */
463 void ConstSubExpr (int (*F) (ExprDesc*), ExprDesc* Expr)
464 /* Will evaluate an expression via the given function. If the result is not
465 * a constant, a diagnostic will be printed, and the value is replaced by
466 * a constant one to make sure there are no internal errors that result
467 * from this input error.
471 if (F (Expr) != 0 || Expr->Flags != E_MCONST) {
472 Error ("Constant expression expected");
473 /* To avoid any compiler errors, make the expression a valid const */
474 MakeConstIntExpr (Expr, 1);
480 void CheckBoolExpr (ExprDesc* lval)
481 /* Check if the given expression is a boolean expression, output a diagnostic
485 /* If it's an integer, it's ok. If it's not an integer, but a pointer,
486 * the pointer used in a boolean context is also ok
488 if (!IsClassInt (lval->Type) && !IsClassPtr (lval->Type)) {
489 Error ("Boolean expression expected");
490 /* To avoid any compiler errors, make the expression a valid int */
491 MakeConstIntExpr (lval, 1);
497 /*****************************************************************************/
499 /*****************************************************************************/
503 void exprhs (unsigned flags, int k, ExprDesc *lval)
504 /* Put the result of an expression into the primary register */
510 /* Dereferenced lvalue */
511 flags |= TypeOf (lval->Type);
512 if (lval->Test & E_FORCETEST) {
514 lval->Test &= ~E_FORCETEST;
516 if (f & E_MGLOBAL) { /* ref to globalvar */
518 flags |= GlobalModeFlags (f);
519 g_getstatic (flags, lval->Name, lval->ConstVal);
520 } else if (f & E_MLOCAL) {
521 /* ref to localvar */
522 g_getlocal (flags, lval->ConstVal);
523 } else if (f & E_MCONST) {
524 /* ref to absolute address */
525 g_getstatic (flags | CF_ABSOLUTE, lval->ConstVal, 0);
526 } else if (f == E_MEOFFS) {
527 g_getind (flags, lval->ConstVal);
528 } else if (f != E_MREG) {
531 } else if (f == E_MEOFFS) {
532 /* reference not storable */
533 flags |= TypeOf (lval->Type);
534 g_inc (flags | CF_CONST, lval->ConstVal);
535 } else if ((f & E_MEXPR) == 0) {
536 /* Constant of some sort, load it into the primary */
537 LoadConstant (flags, lval);
539 /* Are we testing this value? */
540 if (lval->Test & E_FORCETEST) {
541 /* Yes, force a test */
542 flags |= TypeOf (lval->Type);
544 lval->Test &= ~E_FORCETEST;
550 static unsigned FunctionParamList (FuncDesc* Func)
551 /* Parse a function parameter list and pass the parameters to the called
552 * function. Depending on several criteria this may be done by just pushing
553 * each parameter separately, or creating the parameter frame once and then
554 * storing into this frame.
555 * The function returns the size of the parameters pushed.
560 /* Initialize variables */
561 SymEntry* Param = 0; /* Keep gcc silent */
562 unsigned ParamSize = 0; /* Size of parameters pushed */
563 unsigned ParamCount = 0; /* Number of parameters pushed */
564 unsigned FrameSize = 0; /* Size of parameter frame */
565 unsigned FrameParams = 0; /* Number of params in frame */
566 int FrameOffs = 0; /* Offset into parameter frame */
567 int Ellipsis = 0; /* Function is variadic */
569 /* As an optimization, we may allocate the complete parameter frame at
570 * once instead of pushing each parameter as it comes. We may do that,
573 * - optimizations that increase code size are enabled (allocating the
574 * stack frame at once gives usually larger code).
575 * - we have more than one parameter to push (don't count the last param
576 * for __fastcall__ functions).
578 if (CodeSizeFactor >= 200) {
580 /* Calculate the number and size of the parameters */
581 FrameParams = Func->ParamCount;
582 FrameSize = Func->ParamSize;
583 if (FrameParams > 0 && (Func->Flags & FD_FASTCALL) != 0) {
584 /* Last parameter is not pushed */
585 const SymEntry* LastParam = Func->SymTab->SymTail;
586 FrameSize -= CheckedSizeOf (LastParam->Type);
590 /* Do we have more than one parameter in the frame? */
591 if (FrameParams > 1) {
592 /* Okeydokey, setup the frame */
597 /* Don't use a preallocated frame */
602 /* Parse the actual parameter list */
603 while (CurTok.Tok != TOK_RPAREN) {
608 /* Count arguments */
611 /* Fetch the pointer to the next argument, check for too many args */
612 if (ParamCount <= Func->ParamCount) {
613 /* Beware: If there are parameters with identical names, they
614 * cannot go into the same symbol table, which means that in this
615 * case of errorneous input, the number of nodes in the symbol
616 * table and ParamCount are NOT equal. We have to handle this case
617 * below to avoid segmentation violations. Since we know that this
618 * problem can only occur if there is more than one parameter,
619 * we will just use the last one.
621 if (ParamCount == 1) {
623 Param = Func->SymTab->SymHead;
624 } else if (Param->NextSym != 0) {
626 Param = Param->NextSym;
627 CHECK ((Param->Flags & SC_PARAM) != 0);
629 } else if (!Ellipsis) {
630 /* Too many arguments. Do we have an open param list? */
631 if ((Func->Flags & FD_VARIADIC) == 0) {
632 /* End of param list reached, no ellipsis */
633 Error ("Too many arguments in function call");
635 /* Assume an ellipsis even in case of errors to avoid an error
636 * message for each other argument.
641 /* Do some optimization: If we have a constant value to push,
642 * use a special function that may optimize.
645 if (!Ellipsis && CheckedSizeOf (Param->Type) == 1) {
646 CFlags = CF_FORCECHAR;
649 if (evalexpr (CFlags, hie1, &lval) == 0) {
650 /* A constant value */
654 /* If we don't have an argument spec, accept anything, otherwise
655 * convert the actual argument to the type needed.
658 /* Promote the argument if needed */
659 assignadjust (Param->Type, &lval);
661 /* If we have a prototype, chars may be pushed as chars */
662 Flags |= CF_FORCECHAR;
665 /* Use the type of the argument for the push */
666 Flags |= TypeOf (lval.Type);
668 /* If this is a fastcall function, don't push the last argument */
669 if (ParamCount == Func->ParamCount && (Func->Flags & FD_FASTCALL) != 0) {
670 /* Just load the argument into the primary. This is only needed if
671 * we have a constant argument, otherwise the value is already in
674 if (Flags & CF_CONST) {
675 exprhs (CF_FORCECHAR, 0, &lval);
678 unsigned ArgSize = sizeofarg (Flags);
680 /* We have the space already allocated, store in the frame */
681 CHECK (FrameSize >= ArgSize);
682 FrameSize -= ArgSize;
683 FrameOffs -= ArgSize;
685 g_putlocal (Flags | CF_NOKEEP, FrameOffs, lval.ConstVal);
687 /* Push the argument */
688 g_push (Flags, lval.ConstVal);
691 /* Calculate total parameter size */
692 ParamSize += ArgSize;
695 /* Check for end of argument list */
696 if (CurTok.Tok != TOK_COMMA) {
702 /* Check if we had enough parameters */
703 if (ParamCount < Func->ParamCount) {
704 Error ("Too few arguments in function call");
707 /* The function returns the size of all parameters pushed onto the stack.
708 * However, if there are parameters missing (which is an error and was
709 * flagged by the compiler) AND a stack frame was preallocated above,
710 * we would loose track of the stackpointer and generate an internal error
711 * later. So we correct the value by the parameters that should have been
712 * pushed to avoid an internal compiler error. Since an error was
713 * generated before, no code will be output anyway.
715 return ParamSize + FrameSize;
720 static void FunctionCall (int k, ExprDesc* lval)
721 /* Perform a function call. */
723 FuncDesc* Func; /* Function descriptor */
724 int IsFuncPtr; /* Flag */
725 unsigned ParamSize; /* Number of parameter bytes */
726 CodeMark Mark = 0; /* Initialize to keep gcc silent */
727 int PtrOffs = 0; /* Offset of function pointer on stack */
728 int IsFastCall = 0; /* True if it's a fast call function */
729 int PtrOnStack = 0; /* True if a pointer copy is on stack */
731 /* Get a pointer to the function descriptor from the type string */
732 Func = GetFuncDesc (lval->Type);
734 /* Handle function pointers transparently */
735 IsFuncPtr = IsTypeFuncPtr (lval->Type);
738 /* Check wether it's a fastcall function that has parameters */
739 IsFastCall = IsFastCallFunc (lval->Type + 1) && (Func->ParamCount > 0);
741 /* Things may be difficult, depending on where the function pointer
742 * resides. If the function pointer is an expression of some sort
743 * (not a local or global variable), we have to evaluate this
744 * expression now and save the result for later. Since calls to
745 * function pointers may be nested, we must save it onto the stack.
746 * For fastcall functions we do also need to place a copy of the
747 * pointer on stack, since we cannot use a/x.
749 PtrOnStack = IsFastCall || ((lval->Flags & (E_MGLOBAL | E_MLOCAL)) == 0);
752 /* Not a global or local variable, or a fastcall function. Load
753 * the pointer into the primary and mark it as an expression.
755 exprhs (CF_NONE, k, lval);
756 lval->Flags |= E_MEXPR;
758 /* Remember the code position */
759 Mark = GetCodePos ();
761 /* Push the pointer onto the stack and remember the offset */
766 /* Check for known standard functions and inline them if requested */
767 } else if (InlineStdFuncs && IsStdFunc ((const char*) lval->Name)) {
769 /* Inline this function */
770 HandleStdFunc (Func, lval);
775 /* Parse the parameter list */
776 ParamSize = FunctionParamList (Func);
778 /* We need the closing paren here */
781 /* Special handling for function pointers */
784 /* If the function is not a fastcall function, load the pointer to
785 * the function into the primary.
789 /* Not a fastcall function - we may use the primary */
791 /* If we have no parameters, the pointer is still in the
792 * primary. Remove the code to push it and correct the
795 if (ParamSize == 0) {
800 /* Load from the saved copy */
801 g_getlocal (CF_PTR, PtrOffs);
804 /* Load from original location */
805 exprhs (CF_NONE, k, lval);
808 /* Call the function */
809 g_callind (TypeOf (lval->Type+1), ParamSize, PtrOffs);
813 /* Fastcall function. We cannot use the primary for the function
814 * pointer and must therefore use an offset to the stack location.
815 * Since fastcall functions may never be variadic, we can use the
816 * index register for this purpose.
818 g_callind (CF_LOCAL, ParamSize, PtrOffs);
821 /* If we have a pointer on stack, remove it */
823 g_space (- (int) sizeofarg (CF_PTR));
832 /* Normal function */
833 g_call (TypeOf (lval->Type), (const char*) lval->Name, ParamSize);
840 static int primary (ExprDesc* lval)
841 /* This is the lowest level of the expression parser. */
845 /* Initialize fields in the expression stucture */
846 lval->Test = 0; /* No test */
847 lval->Sym = 0; /* Symbol unknown */
849 /* Character and integer constants. */
850 if (CurTok.Tok == TOK_ICONST || CurTok.Tok == TOK_CCONST) {
851 lval->Flags = E_MCONST | E_TCONST;
852 lval->Type = CurTok.Type;
853 lval->ConstVal = CurTok.IVal;
858 /* Process parenthesized subexpression by calling the whole parser
861 if (CurTok.Tok == TOK_LPAREN) {
863 InitExprDesc (lval); /* Remove any attributes */
869 /* If we run into an identifier in preprocessing mode, we assume that this
870 * is an undefined macro and replace it by a constant value of zero.
872 if (Preprocessing && CurTok.Tok == TOK_IDENT) {
873 MakeConstIntExpr (lval, 0);
877 /* All others may only be used if the expression evaluation is not called
878 * recursively by the preprocessor.
881 /* Illegal expression in PP mode */
882 Error ("Preprocessor expression expected");
883 MakeConstIntExpr (lval, 1);
888 if (CurTok.Tok == TOK_IDENT) {
893 /* Get a pointer to the symbol table entry */
894 Sym = lval->Sym = FindSym (CurTok.Ident);
896 /* Is the symbol known? */
899 /* We found the symbol - skip the name token */
902 /* The expression type is the symbol type */
903 lval->Type = Sym->Type;
905 /* Check for illegal symbol types */
906 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
907 if (Sym->Flags & SC_TYPE) {
908 /* Cannot use type symbols */
909 Error ("Variable identifier expected");
910 /* Assume an int type to make lval valid */
911 lval->Flags = E_MLOCAL | E_TLOFFS;
912 lval->Type = type_int;
917 /* Check for legal symbol types */
918 if ((Sym->Flags & SC_CONST) == SC_CONST) {
919 /* Enum or some other numeric constant */
920 lval->Flags = E_MCONST | E_TCONST;
921 lval->ConstVal = Sym->V.ConstVal;
923 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
925 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
926 lval->Name = (unsigned long) Sym->Name;
928 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
929 /* Local variable. If this is a parameter for a variadic
930 * function, we have to add some address calculations, and the
931 * address is not const.
933 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
934 /* Variadic parameter */
935 g_leavariadic (Sym->V.Offs - F_GetParamSize (CurrentFunc));
936 lval->Flags = E_MEXPR;
939 /* Normal parameter */
940 lval->Flags = E_MLOCAL | E_TLOFFS;
941 lval->ConstVal = Sym->V.Offs;
943 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
944 /* Register variable, zero page based */
945 lval->Flags = E_MGLOBAL | E_MCONST | E_TREGISTER;
946 lval->Name = Sym->V.R.RegOffs;
948 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
949 /* Static variable */
950 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
951 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
952 lval->Name = (unsigned long) Sym->Name;
954 lval->Flags = E_MGLOBAL | E_MCONST | E_TLLAB;
955 lval->Name = Sym->V.Label;
959 /* Local static variable */
960 lval->Flags = E_MGLOBAL | E_MCONST | E_TLLAB;
961 lval->Name = Sym->V.Offs;
965 /* The symbol is referenced now */
966 Sym->Flags |= SC_REF;
967 if (IsTypeFunc (lval->Type) || IsTypeArray (lval->Type)) {
973 /* We did not find the symbol. Remember the name, then skip it */
974 strcpy (Ident, CurTok.Ident);
977 /* IDENT is either an auto-declared function or an undefined variable. */
978 if (CurTok.Tok == TOK_LPAREN) {
979 /* Declare a function returning int. For that purpose, prepare a
980 * function signature for a function having an empty param list
983 Warning ("Function call without a prototype");
984 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
985 lval->Type = Sym->Type;
986 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
987 lval->Name = (unsigned long) Sym->Name;
993 /* Undeclared Variable */
994 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
995 lval->Flags = E_MLOCAL | E_TLOFFS;
996 lval->Type = type_int;
998 Error ("Undefined symbol: `%s'", Ident);
1004 /* String literal? */
1005 if (CurTok.Tok == TOK_SCONST) {
1006 lval->Flags = E_MCONST | E_TLIT;
1007 lval->ConstVal = CurTok.IVal;
1008 lval->Type = GetCharArrayType (GetLiteralPoolOffs () - CurTok.IVal);
1013 /* ASM statement? */
1014 if (CurTok.Tok == TOK_ASM) {
1016 lval->Type = type_void;
1017 lval->Flags = E_MEXPR;
1022 /* __AX__ and __EAX__ pseudo values? */
1023 if (CurTok.Tok == TOK_AX || CurTok.Tok == TOK_EAX) {
1024 lval->Type = (CurTok.Tok == TOK_AX)? type_uint : type_ulong;
1025 lval->Flags = E_MREG;
1026 lval->Test &= ~E_CC;
1029 return 1; /* May be used as lvalue */
1032 /* Illegal primary. */
1033 Error ("Expression expected");
1034 MakeConstIntExpr (lval, 1);
1040 static int arrayref (int k, ExprDesc* lval)
1041 /* Handle an array reference */
1055 /* Skip the bracket */
1058 /* Get the type of left side */
1061 /* We can apply a special treatment for arrays that have a const base
1062 * address. This is true for most arrays and will produce a lot better
1063 * code. Check if this is a const base address.
1065 lflags = lval->Flags & ~E_MCTYPE;
1066 ConstBaseAddr = (lflags == E_MCONST) || /* Constant numeric address */
1067 (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1068 lflags == E_MLOCAL; /* Local array */
1070 /* If we have a constant base, we delay the address fetch */
1071 Mark1 = GetCodePos ();
1072 Mark2 = 0; /* Silence gcc */
1073 if (!ConstBaseAddr) {
1074 /* Get a pointer to the array into the primary */
1075 exprhs (CF_NONE, k, lval);
1077 /* Get the array pointer on stack. Do not push more than 16
1078 * bit, even if this value is greater, since we cannot handle
1079 * other than 16bit stuff when doing indexing.
1081 Mark2 = GetCodePos ();
1085 /* TOS now contains ptr to array elements. Get the subscript. */
1087 if (l == 0 && lval2.Flags == E_MCONST) {
1089 /* The array subscript is a constant - remove value from stack */
1090 if (!ConstBaseAddr) {
1094 /* Get an array pointer into the primary */
1095 exprhs (CF_NONE, k, lval);
1098 if (IsClassPtr (tptr1)) {
1100 /* Scale the subscript value according to element size */
1101 lval2.ConstVal *= CheckedPSizeOf (tptr1);
1103 /* Remove code for lhs load */
1106 /* Handle constant base array on stack. Be sure NOT to
1107 * handle pointers the same way, this won't work.
1109 if (IsTypeArray (tptr1) &&
1110 ((lval->Flags & ~E_MCTYPE) == E_MCONST ||
1111 (lval->Flags & ~E_MCTYPE) == E_MLOCAL ||
1112 (lval->Flags & E_MGLOBAL) != 0 ||
1113 (lval->Flags == E_MEOFFS))) {
1114 lval->ConstVal += lval2.ConstVal;
1117 /* Pointer - load into primary and remember offset */
1118 if ((lval->Flags & E_MEXPR) == 0 || k != 0) {
1119 exprhs (CF_NONE, k, lval);
1121 lval->ConstVal = lval2.ConstVal;
1122 lval->Flags = E_MEOFFS;
1125 /* Result is of element type */
1126 lval->Type = Indirect (tptr1);
1131 } else if (IsClassPtr (tptr2 = lval2.Type)) {
1132 /* Subscript is pointer, get element type */
1133 lval2.Type = Indirect (tptr2);
1135 /* Scale the rhs value in the primary register */
1136 g_scale (TypeOf (tptr1), CheckedSizeOf (lval2.Type));
1138 lval->Type = lval2.Type;
1140 Error ("Cannot subscript");
1143 /* Add the subscript. Since arrays are indexed by integers,
1144 * we will ignore the true type of the subscript here and
1145 * use always an int.
1147 g_inc (CF_INT | CF_CONST, lval2.ConstVal);
1151 /* Array subscript is not constant. Load it into the primary */
1152 Mark2 = GetCodePos ();
1153 exprhs (CF_NONE, l, &lval2);
1156 if (IsClassPtr (tptr1)) {
1158 /* Get the element type */
1159 lval->Type = Indirect (tptr1);
1161 /* Indexing is based on int's, so we will just use the integer
1162 * portion of the index (which is in (e)ax, so there's no further
1165 g_scale (CF_INT, CheckedSizeOf (lval->Type));
1167 } else if (IsClassPtr (tptr2)) {
1169 /* Get the element type */
1170 lval2.Type = Indirect (tptr2);
1172 /* Get the int value on top. If we go here, we're sure,
1173 * both values are 16 bit (the first one was truncated
1174 * if necessary and the second one is a pointer).
1175 * Note: If ConstBaseAddr is true, we don't have a value on
1176 * stack, so to "swap" both, just push the subscript.
1178 if (ConstBaseAddr) {
1180 exprhs (CF_NONE, k, lval);
1187 g_scale (TypeOf (tptr1), CheckedSizeOf (lval2.Type));
1188 lval->Type = lval2.Type;
1190 Error ("Cannot subscript");
1193 /* The offset is now in the primary register. It didn't have a
1194 * constant base address for the lhs, the lhs address is already
1195 * on stack, and we must add the offset. If the base address was
1196 * constant, we call special functions to add the address to the
1199 if (!ConstBaseAddr) {
1200 /* Add the subscript. Both values are int sized. */
1204 /* If the subscript has itself a constant address, it is often
1205 * a better idea to reverse again the order of the evaluation.
1206 * This will generate better code if the subscript is a byte
1207 * sized variable. But beware: This is only possible if the
1208 * subscript was not scaled, that is, if this was a byte array
1211 rflags = lval2.Flags & ~E_MCTYPE;
1212 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1213 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1214 rflags == E_MLOCAL; /* Local array */
1216 if (ConstSubAddr && CheckedSizeOf (lval->Type) == SIZEOF_CHAR) {
1220 /* Reverse the order of evaluation */
1221 unsigned flags = (CheckedSizeOf (lval2.Type) == SIZEOF_CHAR)? CF_CHAR : CF_INT;
1224 /* Get a pointer to the array into the primary. We have changed
1225 * Type above but we need the original type to load the
1226 * address, so restore it temporarily.
1228 SavedType = lval->Type;
1230 exprhs (CF_NONE, k, lval);
1231 lval->Type = SavedType;
1233 /* Add the variable */
1234 if (rflags == E_MLOCAL) {
1235 g_addlocal (flags, lval2.ConstVal);
1237 flags |= GlobalModeFlags (lval2.Flags);
1238 g_addstatic (flags, lval2.Name, lval2.ConstVal);
1241 if (lflags == E_MCONST) {
1242 /* Constant numeric address. Just add it */
1243 g_inc (CF_INT | CF_UNSIGNED, lval->ConstVal);
1244 } else if (lflags == E_MLOCAL) {
1245 /* Base address is a local variable address */
1246 if (IsTypeArray (tptr1)) {
1247 g_addaddr_local (CF_INT, lval->ConstVal);
1249 g_addlocal (CF_PTR, lval->ConstVal);
1252 /* Base address is a static variable address */
1253 unsigned flags = CF_INT;
1254 flags |= GlobalModeFlags (lval->Flags);
1255 if (IsTypeArray (tptr1)) {
1256 g_addaddr_static (flags, lval->Name, lval->ConstVal);
1258 g_addstatic (flags, lval->Name, lval->ConstVal);
1264 lval->Flags = E_MEXPR;
1267 return !IsTypeArray (lval->Type);
1273 static int structref (int k, ExprDesc* lval)
1274 /* Process struct field after . or ->. */
1280 /* Skip the token and check for an identifier */
1282 if (CurTok.Tok != TOK_IDENT) {
1283 Error ("Identifier expected");
1284 lval->Type = type_int;
1288 /* Get the symbol table entry and check for a struct field */
1289 strcpy (Ident, CurTok.Ident);
1291 Field = FindStructField (lval->Type, Ident);
1293 Error ("Struct/union has no field named `%s'", Ident);
1294 lval->Type = type_int;
1298 /* If we have constant input data, the result is also constant */
1299 flags = lval->Flags & ~E_MCTYPE;
1300 if (flags == E_MCONST ||
1301 (k == 0 && (flags == E_MLOCAL ||
1302 (flags & E_MGLOBAL) != 0 ||
1303 lval->Flags == E_MEOFFS))) {
1304 lval->ConstVal += Field->V.Offs;
1306 if ((flags & E_MEXPR) == 0 || k != 0) {
1307 exprhs (CF_NONE, k, lval);
1309 lval->ConstVal = Field->V.Offs;
1310 lval->Flags = E_MEOFFS;
1312 lval->Type = Field->Type;
1313 return !IsTypeArray (Field->Type);
1318 static int hie11 (ExprDesc *lval)
1319 /* Handle compound types (structs and arrays) */
1326 if (CurTok.Tok < TOK_LBRACK || CurTok.Tok > TOK_PTR_REF) {
1333 if (CurTok.Tok == TOK_LBRACK) {
1335 /* Array reference */
1336 k = arrayref (k, lval);
1338 } else if (CurTok.Tok == TOK_LPAREN) {
1340 /* Function call. Skip the opening parenthesis */
1343 if (IsTypeFunc (lval->Type) || IsTypeFuncPtr (lval->Type)) {
1345 /* Call the function */
1346 FunctionCall (k, lval);
1348 /* Result is in the primary register */
1349 lval->Flags = E_MEXPR;
1352 lval->Type = GetFuncReturn (lval->Type);
1355 Error ("Illegal function call");
1359 } else if (CurTok.Tok == TOK_DOT) {
1361 if (!IsClassStruct (lval->Type)) {
1362 Error ("Struct expected");
1364 k = structref (0, lval);
1366 } else if (CurTok.Tok == TOK_PTR_REF) {
1369 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1370 Error ("Struct pointer expected");
1372 k = structref (k, lval);
1382 void Store (ExprDesc* lval, const type* StoreType)
1383 /* Store the primary register into the location denoted by lval. If StoreType
1384 * is given, use this type when storing instead of lval->Type. If StoreType
1385 * is NULL, use lval->Type instead.
1390 unsigned f = lval->Flags;
1392 /* If StoreType was not given, use lval->Type instead */
1393 if (StoreType == 0) {
1394 StoreType = lval->Type;
1397 /* Get the code generator flags */
1398 Flags = TypeOf (StoreType);
1399 if (f & E_MGLOBAL) {
1400 Flags |= GlobalModeFlags (f);
1407 g_putstatic (Flags, lval->Name, lval->ConstVal);
1409 } else if (f & E_MLOCAL) {
1410 /* Store an auto variable */
1411 g_putlocal (Flags, lval->ConstVal, 0);
1412 } else if (f == E_MEOFFS) {
1413 /* Store indirect with offset */
1414 g_putind (Flags, lval->ConstVal);
1415 } else if (f != E_MREG) {
1417 /* Indirect without offset */
1418 g_putind (Flags, 0);
1420 /* Store into absolute address */
1421 g_putstatic (Flags | CF_ABSOLUTE, lval->ConstVal, 0);
1425 /* Assume that each one of the stores will invalidate CC */
1426 lval->Test &= ~E_CC;
1431 static void pre_incdec (ExprDesc* lval, void (*inc) (unsigned, unsigned long))
1432 /* Handle --i and ++i */
1439 if ((k = hie10 (lval)) == 0) {
1440 Error ("Invalid lvalue");
1444 /* Get the data type */
1445 flags = TypeOf (lval->Type) | CF_FORCECHAR | CF_CONST;
1447 /* Get the increment value in bytes */
1448 val = (lval->Type [0] == T_PTR)? CheckedPSizeOf (lval->Type) : 1;
1450 /* We're currently only able to handle some adressing modes */
1451 if ((lval->Flags & E_MGLOBAL) == 0 && /* Global address? */
1452 (lval->Flags & E_MLOCAL) == 0 && /* Local address? */
1453 (lval->Flags & E_MCONST) == 0 && /* Constant address? */
1454 (lval->Flags & E_MEXPR) == 0) { /* Address in a/x? */
1456 /* Use generic code. Push the address if needed */
1459 /* Fetch the value */
1460 exprhs (CF_NONE, k, lval);
1462 /* Increment value in primary */
1465 /* Store the result back */
1470 /* Special code for some addressing modes - use the special += ops */
1471 if (lval->Flags & E_MGLOBAL) {
1472 flags |= GlobalModeFlags (lval->Flags);
1474 g_addeqstatic (flags, lval->Name, lval->ConstVal, val);
1476 g_subeqstatic (flags, lval->Name, lval->ConstVal, val);
1478 } else if (lval->Flags & E_MLOCAL) {
1479 /* ref to localvar */
1481 g_addeqlocal (flags, lval->ConstVal, val);
1483 g_subeqlocal (flags, lval->ConstVal, val);
1485 } else if (lval->Flags & E_MCONST) {
1486 /* ref to absolute address */
1487 flags |= CF_ABSOLUTE;
1489 g_addeqstatic (flags, lval->ConstVal, 0, val);
1491 g_subeqstatic (flags, lval->ConstVal, 0, val);
1493 } else if (lval->Flags & E_MEXPR) {
1494 /* Address in a/x, check if we have an offset */
1495 unsigned Offs = (lval->Flags == E_MEOFFS)? lval->ConstVal : 0;
1497 g_addeqind (flags, Offs, val);
1499 g_subeqind (flags, Offs, val);
1502 Internal ("Invalid addressing mode");
1507 /* Result is an expression */
1508 lval->Flags = E_MEXPR;
1513 static void post_incdec (ExprDesc* lval, int k, void (*inc) (unsigned, unsigned long))
1514 /* Handle i-- and i++ */
1520 Error ("Invalid lvalue");
1524 /* Get the data type */
1525 flags = TypeOf (lval->Type);
1527 /* Push the address if needed */
1530 /* Fetch the value and save it (since it's the result of the expression) */
1531 exprhs (CF_NONE, 1, lval);
1532 g_save (flags | CF_FORCECHAR);
1534 /* If we have a pointer expression, increment by the size of the type */
1535 if (lval->Type[0] == T_PTR) {
1536 inc (flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (lval->Type + 1));
1538 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1541 /* Store the result back */
1544 /* Restore the original value */
1545 g_restore (flags | CF_FORCECHAR);
1546 lval->Flags = E_MEXPR;
1551 static void unaryop (int tok, ExprDesc* lval)
1552 /* Handle unary -/+ and ~ */
1559 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
1560 /* Value is constant */
1562 case TOK_MINUS: lval->ConstVal = -lval->ConstVal; break;
1563 case TOK_PLUS: break;
1564 case TOK_COMP: lval->ConstVal = ~lval->ConstVal; break;
1565 default: Internal ("Unexpected token: %d", tok);
1568 /* Value is not constant */
1569 exprhs (CF_NONE, k, lval);
1571 /* Get the type of the expression */
1572 flags = TypeOf (lval->Type);
1574 /* Handle the operation */
1576 case TOK_MINUS: g_neg (flags); break;
1577 case TOK_PLUS: break;
1578 case TOK_COMP: g_com (flags); break;
1579 default: Internal ("Unexpected token: %d", tok);
1581 lval->Flags = E_MEXPR;
1587 int hie10 (ExprDesc* lval)
1588 /* Handle ++, --, !, unary - etc. */
1593 switch (CurTok.Tok) {
1596 pre_incdec (lval, g_inc);
1600 pre_incdec (lval, g_dec);
1606 unaryop (CurTok.Tok, lval);
1611 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1612 /* Constant expression */
1613 lval->ConstVal = !lval->ConstVal;
1615 g_bneg (TypeOf (lval->Type));
1616 lval->Test |= E_CC; /* bneg will set cc */
1617 lval->Flags = E_MEXPR; /* say it's an expr */
1619 return 0; /* expr not storable */
1623 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1624 /* Expression is not const, indirect value loaded into primary */
1625 lval->Flags = E_MEXPR;
1626 lval->ConstVal = 0; /* Offset is zero now */
1629 if (IsClassPtr (t)) {
1630 lval->Type = Indirect (t);
1632 Error ("Illegal indirection");
1639 /* The & operator may be applied to any lvalue, and it may be
1640 * applied to functions, even if they're no lvalues.
1642 if (k == 0 && !IsTypeFunc (lval->Type)) {
1643 /* Allow the & operator with an array */
1644 if (!IsTypeArray (lval->Type)) {
1645 Error ("Illegal address");
1648 t = TypeAlloc (TypeLen (lval->Type) + 2);
1650 TypeCpy (t + 1, lval->Type);
1657 if (istypeexpr ()) {
1658 type Type[MAXTYPELEN];
1660 lval->ConstVal = CheckedSizeOf (ParseType (Type));
1663 /* Remember the output queue pointer */
1664 CodeMark Mark = GetCodePos ();
1666 lval->ConstVal = CheckedSizeOf (lval->Type);
1667 /* Remove any generated code */
1670 lval->Flags = E_MCONST | E_TCONST;
1671 lval->Type = type_uint;
1672 lval->Test &= ~E_CC;
1676 if (istypeexpr ()) {
1678 return TypeCast (lval);
1683 switch (CurTok.Tok) {
1685 post_incdec (lval, k, g_inc);
1689 post_incdec (lval, k, g_dec);
1699 static int hie_internal (const GenDesc** ops, /* List of generators */
1700 ExprDesc* lval, /* parent expr's lval */
1701 int (*hienext) (ExprDesc*),
1702 int* UsedGen) /* next higher level */
1703 /* Helper function */
1710 token_t tok; /* The operator token */
1711 unsigned ltype, type;
1712 int rconst; /* Operand is a constant */
1718 while ((Gen = FindGen (CurTok.Tok, ops)) != 0) {
1720 /* Tell the caller that we handled it's ops */
1723 /* All operators that call this function expect an int on the lhs */
1724 if (!IsClassInt (lval->Type)) {
1725 Error ("Integer expression expected");
1728 /* Remember the operator token, then skip it */
1732 /* Get the lhs on stack */
1733 Mark1 = GetCodePos ();
1734 ltype = TypeOf (lval->Type);
1735 if (k == 0 && lval->Flags == E_MCONST) {
1736 /* Constant value */
1737 Mark2 = GetCodePos ();
1738 g_push (ltype | CF_CONST, lval->ConstVal);
1740 /* Value not constant */
1741 exprhs (CF_NONE, k, lval);
1742 Mark2 = GetCodePos ();
1746 /* Get the right hand side */
1747 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1749 /* Check the type of the rhs */
1750 if (!IsClassInt (lval2.Type)) {
1751 Error ("Integer expression expected");
1754 /* Check for const operands */
1755 if (k == 0 && lval->Flags == E_MCONST && rconst) {
1757 /* Both operands are constant, remove the generated code */
1761 /* Evaluate the result */
1762 lval->ConstVal = kcalc (tok, lval->ConstVal, lval2.ConstVal);
1764 /* Get the type of the result */
1765 lval->Type = promoteint (lval->Type, lval2.Type);
1769 /* If the right hand side is constant, and the generator function
1770 * expects the lhs in the primary, remove the push of the primary
1773 unsigned rtype = TypeOf (lval2.Type);
1776 /* Second value is constant - check for div */
1779 if (tok == TOK_DIV && lval2.ConstVal == 0) {
1780 Error ("Division by zero");
1781 } else if (tok == TOK_MOD && lval2.ConstVal == 0) {
1782 Error ("Modulo operation with zero");
1784 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1787 ltype |= CF_REG; /* Value is in register */
1791 /* Determine the type of the operation result. */
1792 type |= g_typeadjust (ltype, rtype);
1793 lval->Type = promoteint (lval->Type, lval2.Type);
1796 Gen->Func (type, lval2.ConstVal);
1797 lval->Flags = E_MEXPR;
1800 /* We have a rvalue now */
1809 static int hie_compare (const GenDesc** ops, /* List of generators */
1810 ExprDesc* lval, /* parent expr's lval */
1811 int (*hienext) (ExprDesc*))
1812 /* Helper function for the compare operators */
1819 token_t tok; /* The operator token */
1821 int rconst; /* Operand is a constant */
1826 while ((Gen = FindGen (CurTok.Tok, ops)) != 0) {
1828 /* Remember the operator token, then skip it */
1832 /* Get the lhs on stack */
1833 Mark1 = GetCodePos ();
1834 ltype = TypeOf (lval->Type);
1835 if (k == 0 && lval->Flags == E_MCONST) {
1836 /* Constant value */
1837 Mark2 = GetCodePos ();
1838 g_push (ltype | CF_CONST, lval->ConstVal);
1840 /* Value not constant */
1841 exprhs (CF_NONE, k, lval);
1842 Mark2 = GetCodePos ();
1846 /* Get the right hand side */
1847 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1849 /* Make sure, the types are compatible */
1850 if (IsClassInt (lval->Type)) {
1851 if (!IsClassInt (lval2.Type) && !(IsClassPtr(lval2.Type) && IsNullPtr(lval))) {
1852 Error ("Incompatible types");
1854 } else if (IsClassPtr (lval->Type)) {
1855 if (IsClassPtr (lval2.Type)) {
1856 /* Both pointers are allowed in comparison if they point to
1857 * the same type, or if one of them is a void pointer.
1859 type* left = Indirect (lval->Type);
1860 type* right = Indirect (lval2.Type);
1861 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1862 /* Incomatible pointers */
1863 Error ("Incompatible types");
1865 } else if (!IsNullPtr (&lval2)) {
1866 Error ("Incompatible types");
1870 /* Check for const operands */
1871 if (k == 0 && lval->Flags == E_MCONST && rconst) {
1873 /* Both operands are constant, remove the generated code */
1877 /* Evaluate the result */
1878 lval->ConstVal = kcalc (tok, lval->ConstVal, lval2.ConstVal);
1882 /* If the right hand side is constant, and the generator function
1883 * expects the lhs in the primary, remove the push of the primary
1889 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1892 ltype |= CF_REG; /* Value is in register */
1896 /* Determine the type of the operation result. If the left
1897 * operand is of type char and the right is a constant, or
1898 * if both operands are of type char, we will encode the
1899 * operation as char operation. Otherwise the default
1900 * promotions are used.
1902 if (IsTypeChar (lval->Type) && (IsTypeChar (lval2.Type) || rconst)) {
1904 if (IsSignUnsigned (lval->Type) || IsSignUnsigned (lval2.Type)) {
1905 flags |= CF_UNSIGNED;
1908 flags |= CF_FORCECHAR;
1911 unsigned rtype = TypeOf (lval2.Type) | (flags & CF_CONST);
1912 flags |= g_typeadjust (ltype, rtype);
1916 Gen->Func (flags, lval2.ConstVal);
1917 lval->Flags = E_MEXPR;
1920 /* Result type is always int */
1921 lval->Type = type_int;
1923 /* We have a rvalue now, condition codes are set */
1933 static int hie9 (ExprDesc *lval)
1934 /* Process * and / operators. */
1936 static const GenDesc* hie9_ops [] = {
1937 &GenMUL, &GenDIV, &GenMOD, 0
1941 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1946 static void parseadd (int k, ExprDesc* lval)
1947 /* Parse an expression with the binary plus operator. lval contains the
1948 * unprocessed left hand side of the expression and will contain the
1949 * result of the expression on return.
1953 unsigned flags; /* Operation flags */
1954 CodeMark Mark; /* Remember code position */
1955 type* lhst; /* Type of left hand side */
1956 type* rhst; /* Type of right hand side */
1959 /* Skip the PLUS token */
1962 /* Get the left hand side type, initialize operation flags */
1966 /* Check for constness on both sides */
1967 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
1969 /* The left hand side is a constant. Good. Get rhs */
1971 if (k == 0 && lval2.Flags == E_MCONST) {
1973 /* Right hand side is also constant. Get the rhs type */
1976 /* Both expressions are constants. Check for pointer arithmetic */
1977 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1978 /* Left is pointer, right is int, must scale rhs */
1979 lval->ConstVal += lval2.ConstVal * CheckedPSizeOf (lhst);
1980 /* Result type is a pointer */
1981 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1982 /* Left is int, right is pointer, must scale lhs */
1983 lval->ConstVal = lval->ConstVal * CheckedPSizeOf (rhst) + lval2.ConstVal;
1984 /* Result type is a pointer */
1985 lval->Type = lval2.Type;
1986 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1987 /* Integer addition */
1988 lval->ConstVal += lval2.ConstVal;
1989 typeadjust (lval, &lval2, 1);
1992 Error ("Invalid operands for binary operator `+'");
1995 /* Result is constant, condition codes not set */
1996 lval->Test &= ~E_CC;
2000 /* lhs is a constant and rhs is not constant. Load rhs into
2003 exprhs (CF_NONE, k, &lval2);
2005 /* Beware: The check above (for lhs) lets not only pass numeric
2006 * constants, but also constant addresses (labels), maybe even
2007 * with an offset. We have to check for that here.
2010 /* First, get the rhs type. */
2014 if (lval->Flags == E_MCONST) {
2015 /* A numerical constant */
2018 /* Constant address label */
2019 flags |= GlobalModeFlags (lval->Flags) | CF_CONSTADDR;
2022 /* Check for pointer arithmetic */
2023 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2024 /* Left is pointer, right is int, must scale rhs */
2025 g_scale (CF_INT, CheckedPSizeOf (lhst));
2026 /* Operate on pointers, result type is a pointer */
2028 /* Generate the code for the add */
2029 if (lval->Flags == E_MCONST) {
2030 /* Numeric constant */
2031 g_inc (flags, lval->ConstVal);
2033 /* Constant address */
2034 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2036 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2038 /* Left is int, right is pointer, must scale lhs. */
2039 unsigned ScaleFactor = CheckedPSizeOf (rhst);
2041 /* Operate on pointers, result type is a pointer */
2043 lval->Type = lval2.Type;
2045 /* Since we do already have rhs in the primary, if lhs is
2046 * not a numeric constant, and the scale factor is not one
2047 * (no scaling), we must take the long way over the stack.
2049 if (lval->Flags == E_MCONST) {
2050 /* Numeric constant, scale lhs */
2051 lval->ConstVal *= ScaleFactor;
2052 /* Generate the code for the add */
2053 g_inc (flags, lval->ConstVal);
2054 } else if (ScaleFactor == 1) {
2055 /* Constant address but no need to scale */
2056 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2058 /* Constant address that must be scaled */
2059 g_push (TypeOf (lval2.Type), 0); /* rhs --> stack */
2060 g_getimmed (flags, lval->Name, lval->ConstVal);
2061 g_scale (CF_PTR, ScaleFactor);
2064 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2065 /* Integer addition */
2066 flags |= typeadjust (lval, &lval2, 1);
2067 /* Generate the code for the add */
2068 if (lval->Flags == E_MCONST) {
2069 /* Numeric constant */
2070 g_inc (flags, lval->ConstVal);
2072 /* Constant address */
2073 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2077 Error ("Invalid operands for binary operator `+'");
2080 /* Result is in primary register */
2081 lval->Flags = E_MEXPR;
2082 lval->Test &= ~E_CC;
2088 /* Left hand side is not constant. Get the value onto the stack. */
2089 exprhs (CF_NONE, k, lval); /* --> primary register */
2090 Mark = GetCodePos ();
2091 g_push (TypeOf (lval->Type), 0); /* --> stack */
2093 /* Evaluate the rhs */
2094 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2096 /* Right hand side is a constant. Get the rhs type */
2099 /* Remove pushed value from stack */
2101 pop (TypeOf (lval->Type));
2103 /* Check for pointer arithmetic */
2104 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2105 /* Left is pointer, right is int, must scale rhs */
2106 lval2.ConstVal *= CheckedPSizeOf (lhst);
2107 /* Operate on pointers, result type is a pointer */
2109 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2110 /* Left is int, right is pointer, must scale lhs (ptr only) */
2111 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2112 /* Operate on pointers, result type is a pointer */
2114 lval->Type = lval2.Type;
2115 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2116 /* Integer addition */
2117 flags = typeadjust (lval, &lval2, 1);
2120 Error ("Invalid operands for binary operator `+'");
2123 /* Generate code for the add */
2124 g_inc (flags | CF_CONST, lval2.ConstVal);
2126 /* Result is in primary register */
2127 lval->Flags = E_MEXPR;
2128 lval->Test &= ~E_CC;
2132 /* lhs and rhs are not constant. Get the rhs type. */
2135 /* Check for pointer arithmetic */
2136 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2137 /* Left is pointer, right is int, must scale rhs */
2138 g_scale (CF_INT, CheckedPSizeOf (lhst));
2139 /* Operate on pointers, result type is a pointer */
2141 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2142 /* Left is int, right is pointer, must scale lhs */
2143 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2144 g_swap (CF_INT); /* Swap TOS and primary */
2145 g_scale (CF_INT, CheckedPSizeOf (rhst));
2146 /* Operate on pointers, result type is a pointer */
2148 lval->Type = lval2.Type;
2149 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2150 /* Integer addition */
2151 flags = typeadjust (lval, &lval2, 0);
2154 Error ("Invalid operands for binary operator `+'");
2157 /* Generate code for the add */
2160 /* Result is in primary register */
2161 lval->Flags = E_MEXPR;
2162 lval->Test &= ~E_CC;
2171 static void parsesub (int k, ExprDesc* lval)
2172 /* Parse an expression with the binary minus operator. lval contains the
2173 * unprocessed left hand side of the expression and will contain the
2174 * result of the expression on return.
2178 unsigned flags; /* Operation flags */
2179 type* lhst; /* Type of left hand side */
2180 type* rhst; /* Type of right hand side */
2181 CodeMark Mark1; /* Save position of output queue */
2182 CodeMark Mark2; /* Another position in the queue */
2183 int rscale; /* Scale factor for the result */
2186 /* Skip the MINUS token */
2189 /* Get the left hand side type, initialize operation flags */
2192 rscale = 1; /* Scale by 1, that is, don't scale */
2194 /* Remember the output queue position, then bring the value onto the stack */
2195 Mark1 = GetCodePos ();
2196 exprhs (CF_NONE, k, lval); /* --> primary register */
2197 Mark2 = GetCodePos ();
2198 g_push (TypeOf (lhst), 0); /* --> stack */
2200 /* Parse the right hand side */
2201 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2203 /* The right hand side is constant. Get the rhs type. */
2206 /* Check left hand side */
2207 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
2209 /* Both sides are constant, remove generated code */
2211 pop (TypeOf (lhst)); /* Clean up the stack */
2213 /* Check for pointer arithmetic */
2214 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2215 /* Left is pointer, right is int, must scale rhs */
2216 lval->ConstVal -= lval2.ConstVal * CheckedPSizeOf (lhst);
2217 /* Operate on pointers, result type is a pointer */
2218 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2219 /* Left is pointer, right is pointer, must scale result */
2220 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2221 Error ("Incompatible pointer types");
2223 lval->ConstVal = (lval->ConstVal - lval2.ConstVal) /
2224 CheckedPSizeOf (lhst);
2226 /* Operate on pointers, result type is an integer */
2227 lval->Type = type_int;
2228 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2229 /* Integer subtraction */
2230 typeadjust (lval, &lval2, 1);
2231 lval->ConstVal -= lval2.ConstVal;
2234 Error ("Invalid operands for binary operator `-'");
2237 /* Result is constant, condition codes not set */
2238 /* lval->Flags = E_MCONST; ### */
2239 lval->Test &= ~E_CC;
2243 /* Left hand side is not constant, right hand side is.
2244 * Remove pushed value from stack.
2247 pop (TypeOf (lhst));
2249 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2250 /* Left is pointer, right is int, must scale rhs */
2251 lval2.ConstVal *= CheckedPSizeOf (lhst);
2252 /* Operate on pointers, result type is a pointer */
2254 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2255 /* Left is pointer, right is pointer, must scale result */
2256 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2257 Error ("Incompatible pointer types");
2259 rscale = CheckedPSizeOf (lhst);
2261 /* Operate on pointers, result type is an integer */
2263 lval->Type = type_int;
2264 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2265 /* Integer subtraction */
2266 flags = typeadjust (lval, &lval2, 1);
2269 Error ("Invalid operands for binary operator `-'");
2272 /* Do the subtraction */
2273 g_dec (flags | CF_CONST, lval2.ConstVal);
2275 /* If this was a pointer subtraction, we must scale the result */
2277 g_scale (flags, -rscale);
2280 /* Result is in primary register */
2281 lval->Flags = E_MEXPR;
2282 lval->Test &= ~E_CC;
2288 /* Right hand side is not constant. Get the rhs type. */
2291 /* Check for pointer arithmetic */
2292 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2293 /* Left is pointer, right is int, must scale rhs */
2294 g_scale (CF_INT, CheckedPSizeOf (lhst));
2295 /* Operate on pointers, result type is a pointer */
2297 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2298 /* Left is pointer, right is pointer, must scale result */
2299 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2300 Error ("Incompatible pointer types");
2302 rscale = CheckedPSizeOf (lhst);
2304 /* Operate on pointers, result type is an integer */
2306 lval->Type = type_int;
2307 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2308 /* Integer subtraction. If the left hand side descriptor says that
2309 * the lhs is const, we have to remove this mark, since this is no
2310 * longer true, lhs is on stack instead.
2312 if (lval->Flags == E_MCONST) {
2313 lval->Flags = E_MEXPR;
2315 /* Adjust operand types */
2316 flags = typeadjust (lval, &lval2, 0);
2319 Error ("Invalid operands for binary operator `-'");
2322 /* Generate code for the sub (the & is a hack here) */
2323 g_sub (flags & ~CF_CONST, 0);
2325 /* If this was a pointer subtraction, we must scale the result */
2327 g_scale (flags, -rscale);
2330 /* Result is in primary register */
2331 lval->Flags = E_MEXPR;
2332 lval->Test &= ~E_CC;
2338 static int hie8 (ExprDesc* lval)
2339 /* Process + and - binary operators. */
2341 int k = hie9 (lval);
2342 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2344 if (CurTok.Tok == TOK_PLUS) {
2357 static int hie7 (ExprDesc *lval)
2358 /* Parse << and >>. */
2360 static const GenDesc* hie7_ops [] = {
2365 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2370 static int hie6 (ExprDesc *lval)
2371 /* process greater-than type comparators */
2373 static const GenDesc* hie6_ops [] = {
2374 &GenLT, &GenLE, &GenGE, &GenGT, 0
2376 return hie_compare (hie6_ops, lval, hie7);
2381 static int hie5 (ExprDesc *lval)
2383 static const GenDesc* hie5_ops[] = {
2386 return hie_compare (hie5_ops, lval, hie6);
2391 static int hie4 (ExprDesc* lval)
2392 /* Handle & (bitwise and) */
2394 static const GenDesc* hie4_ops [] = {
2399 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2404 static int hie3 (ExprDesc *lval)
2405 /* Handle ^ (bitwise exclusive or) */
2407 static const GenDesc* hie3_ops [] = {
2412 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2417 static int hie2 (ExprDesc *lval)
2418 /* Handle | (bitwise or) */
2420 static const GenDesc* hie2_ops [] = {
2425 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2430 static int hieAndPP (ExprDesc* lval)
2431 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2432 * called recursively from the preprocessor.
2437 ConstSubExpr (hie2, lval);
2438 while (CurTok.Tok == TOK_BOOL_AND) {
2440 /* Left hand side must be an int */
2441 if (!IsClassInt (lval->Type)) {
2442 Error ("Left hand side must be of integer type");
2443 MakeConstIntExpr (lval, 1);
2450 ConstSubExpr (hie2, &lval2);
2452 /* Since we are in PP mode, all we know about is integers */
2453 if (!IsClassInt (lval2.Type)) {
2454 Error ("Right hand side must be of integer type");
2455 MakeConstIntExpr (&lval2, 1);
2458 /* Combine the two */
2459 lval->ConstVal = (lval->ConstVal && lval2.ConstVal);
2462 /* Always a rvalue */
2468 static int hieOrPP (ExprDesc *lval)
2469 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2470 * called recursively from the preprocessor.
2475 ConstSubExpr (hieAndPP, lval);
2476 while (CurTok.Tok == TOK_BOOL_OR) {
2478 /* Left hand side must be an int */
2479 if (!IsClassInt (lval->Type)) {
2480 Error ("Left hand side must be of integer type");
2481 MakeConstIntExpr (lval, 1);
2488 ConstSubExpr (hieAndPP, &lval2);
2490 /* Since we are in PP mode, all we know about is integers */
2491 if (!IsClassInt (lval2.Type)) {
2492 Error ("Right hand side must be of integer type");
2493 MakeConstIntExpr (&lval2, 1);
2496 /* Combine the two */
2497 lval->ConstVal = (lval->ConstVal || lval2.ConstVal);
2500 /* Always a rvalue */
2506 static int hieAnd (ExprDesc* lval, unsigned TrueLab, int* BoolOp)
2507 /* Process "exp && exp" */
2514 if (CurTok.Tok == TOK_BOOL_AND) {
2516 /* Tell our caller that we're evaluating a boolean */
2519 /* Get a label that we will use for false expressions */
2520 lab = GetLocalLabel ();
2522 /* If the expr hasn't set condition codes, set the force-test flag */
2523 if ((lval->Test & E_CC) == 0) {
2524 lval->Test |= E_FORCETEST;
2527 /* Load the value */
2528 exprhs (CF_FORCECHAR, k, lval);
2530 /* Generate the jump */
2531 g_falsejump (CF_NONE, lab);
2533 /* Parse more boolean and's */
2534 while (CurTok.Tok == TOK_BOOL_AND) {
2541 if ((lval2.Test & E_CC) == 0) {
2542 lval2.Test |= E_FORCETEST;
2544 exprhs (CF_FORCECHAR, k, &lval2);
2546 /* Do short circuit evaluation */
2547 if (CurTok.Tok == TOK_BOOL_AND) {
2548 g_falsejump (CF_NONE, lab);
2550 /* Last expression - will evaluate to true */
2551 g_truejump (CF_NONE, TrueLab);
2555 /* Define the false jump label here */
2556 g_defcodelabel (lab);
2558 /* Define the label */
2559 lval->Flags = E_MEXPR;
2560 lval->Test |= E_CC; /* Condition codes are set */
2568 static int hieOr (ExprDesc *lval)
2569 /* Process "exp || exp". */
2573 int BoolOp = 0; /* Did we have a boolean op? */
2574 int AndOp; /* Did we have a && operation? */
2575 unsigned TrueLab; /* Jump to this label if true */
2579 TrueLab = GetLocalLabel ();
2581 /* Call the next level parser */
2582 k = hieAnd (lval, TrueLab, &BoolOp);
2584 /* Any boolean or's? */
2585 if (CurTok.Tok == TOK_BOOL_OR) {
2587 /* If the expr hasn't set condition codes, set the force-test flag */
2588 if ((lval->Test & E_CC) == 0) {
2589 lval->Test |= E_FORCETEST;
2592 /* Get first expr */
2593 exprhs (CF_FORCECHAR, k, lval);
2595 /* For each expression jump to TrueLab if true. Beware: If we
2596 * had && operators, the jump is already in place!
2599 g_truejump (CF_NONE, TrueLab);
2602 /* Remember that we had a boolean op */
2605 /* while there's more expr */
2606 while (CurTok.Tok == TOK_BOOL_OR) {
2613 k = hieAnd (&lval2, TrueLab, &AndOp);
2614 if ((lval2.Test & E_CC) == 0) {
2615 lval2.Test |= E_FORCETEST;
2617 exprhs (CF_FORCECHAR, k, &lval2);
2619 /* If there is more to come, add shortcut boolean eval. */
2620 g_truejump (CF_NONE, TrueLab);
2623 lval->Flags = E_MEXPR;
2624 lval->Test |= E_CC; /* Condition codes are set */
2628 /* If we really had boolean ops, generate the end sequence */
2630 DoneLab = GetLocalLabel ();
2631 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2632 g_falsejump (CF_NONE, DoneLab);
2633 g_defcodelabel (TrueLab);
2634 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2635 g_defcodelabel (DoneLab);
2642 static int hieQuest (ExprDesc *lval)
2643 /* Parse "lvalue ? exp : exp" */
2648 ExprDesc lval2; /* Expression 2 */
2649 ExprDesc lval3; /* Expression 3 */
2650 type* type2; /* Type of expression 2 */
2651 type* type3; /* Type of expression 3 */
2652 type* rtype; /* Type of result */
2655 k = Preprocessing? hieOrPP (lval) : hieOr (lval);
2656 if (CurTok.Tok == TOK_QUEST) {
2658 if ((lval->Test & E_CC) == 0) {
2659 /* Condition codes not set, force a test */
2660 lval->Test |= E_FORCETEST;
2662 exprhs (CF_NONE, k, lval);
2663 labf = GetLocalLabel ();
2664 g_falsejump (CF_NONE, labf);
2666 /* Parse second expression */
2667 k = expr (hie1, &lval2);
2669 if (!IsTypeVoid (lval2.Type)) {
2670 /* Load it into the primary */
2671 exprhs (CF_NONE, k, &lval2);
2673 labt = GetLocalLabel ();
2677 /* Parse the third expression */
2678 g_defcodelabel (labf);
2679 k = expr (hie1, &lval3);
2681 if (!IsTypeVoid (lval3.Type)) {
2682 /* Load it into the primary */
2683 exprhs (CF_NONE, k, &lval3);
2686 /* Check if any conversions are needed, if so, do them.
2687 * Conversion rules for ?: expression are:
2688 * - if both expressions are int expressions, default promotion
2689 * rules for ints apply.
2690 * - if both expressions are pointers of the same type, the
2691 * result of the expression is of this type.
2692 * - if one of the expressions is a pointer and the other is
2693 * a zero constant, the resulting type is that of the pointer
2695 * - if both expressions are void expressions, the result is of
2697 * - all other cases are flagged by an error.
2699 if (IsClassInt (type2) && IsClassInt (type3)) {
2701 /* Get common type */
2702 rtype = promoteint (type2, type3);
2704 /* Convert the third expression to this type if needed */
2705 g_typecast (TypeOf (rtype), TypeOf (type3));
2707 /* Setup a new label so that the expr3 code will jump around
2708 * the type cast code for expr2.
2710 labf = GetLocalLabel (); /* Get new label */
2711 g_jump (labf); /* Jump around code */
2713 /* The jump for expr2 goes here */
2714 g_defcodelabel (labt);
2716 /* Create the typecast code for expr2 */
2717 g_typecast (TypeOf (rtype), TypeOf (type2));
2719 /* Jump here around the typecase code. */
2720 g_defcodelabel (labf);
2721 labt = 0; /* Mark other label as invalid */
2723 } else if (IsClassPtr (type2) && IsClassPtr (type3)) {
2724 /* Must point to same type */
2725 if (TypeCmp (Indirect (type2), Indirect (type3)) < TC_EQUAL) {
2726 Error ("Incompatible pointer types");
2728 /* Result has the common type */
2730 } else if (IsClassPtr (type2) && IsNullPtr (&lval3)) {
2731 /* Result type is pointer, no cast needed */
2733 } else if (IsNullPtr (&lval2) && IsClassPtr (type3)) {
2734 /* Result type is pointer, no cast needed */
2736 } else if (IsTypeVoid (type2) && IsTypeVoid (type3)) {
2737 /* Result type is void */
2740 Error ("Incompatible types");
2741 rtype = lval2.Type; /* Doesn't matter here */
2744 /* If we don't have the label defined until now, do it */
2746 g_defcodelabel (labt);
2749 /* Setup the target expression */
2750 lval->Flags = E_MEXPR;
2759 static void opeq (const GenDesc* Gen, ExprDesc *lval, int k)
2760 /* Process "op=" operators. */
2769 Error ("Invalid lvalue in assignment");
2773 /* Determine the type of the lhs */
2774 flags = TypeOf (lval->Type);
2775 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2776 lval->Type [0] == T_PTR;
2778 /* Get the lhs address on stack (if needed) */
2781 /* Fetch the lhs into the primary register if needed */
2782 exprhs (CF_NONE, k, lval);
2784 /* Bring the lhs on stack */
2785 Mark = GetCodePos ();
2788 /* Evaluate the rhs */
2789 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2790 /* The resulting value is a constant. If the generator has the NOPUSH
2791 * flag set, don't push the lhs.
2793 if (Gen->Flags & GEN_NOPUSH) {
2798 /* lhs is a pointer, scale rhs */
2799 lval2.ConstVal *= CheckedSizeOf (lval->Type+1);
2802 /* If the lhs is character sized, the operation may be later done
2805 if (CheckedSizeOf (lval->Type) == SIZEOF_CHAR) {
2806 flags |= CF_FORCECHAR;
2809 /* Special handling for add and sub - some sort of a hack, but short code */
2810 if (Gen->Func == g_add) {
2811 g_inc (flags | CF_CONST, lval2.ConstVal);
2812 } else if (Gen->Func == g_sub) {
2813 g_dec (flags | CF_CONST, lval2.ConstVal);
2815 Gen->Func (flags | CF_CONST, lval2.ConstVal);
2818 /* rhs is not constant and already in the primary register */
2820 /* lhs is a pointer, scale rhs */
2821 g_scale (TypeOf (lval2.Type), CheckedSizeOf (lval->Type+1));
2824 /* If the lhs is character sized, the operation may be later done
2827 if (CheckedSizeOf (lval->Type) == SIZEOF_CHAR) {
2828 flags |= CF_FORCECHAR;
2831 /* Adjust the types of the operands if needed */
2832 Gen->Func (g_typeadjust (flags, TypeOf (lval2.Type)), 0);
2835 lval->Flags = E_MEXPR;
2840 static void addsubeq (const GenDesc* Gen, ExprDesc *lval, int k)
2841 /* Process the += and -= operators */
2849 /* We must have an lvalue */
2851 Error ("Invalid lvalue in assignment");
2855 /* We're currently only able to handle some adressing modes */
2856 if ((lval->Flags & E_MGLOBAL) == 0 && /* Global address? */
2857 (lval->Flags & E_MLOCAL) == 0 && /* Local address? */
2858 (lval->Flags & E_MCONST) == 0) { /* Constant address? */
2859 /* Use generic routine */
2860 opeq (Gen, lval, k);
2864 /* Skip the operator */
2867 /* Check if we have a pointer expression and must scale rhs */
2868 MustScale = (lval->Type [0] == T_PTR);
2870 /* Initialize the code generator flags */
2874 /* Evaluate the rhs */
2875 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2876 /* The resulting value is a constant. */
2878 /* lhs is a pointer, scale rhs */
2879 lval2.ConstVal *= CheckedSizeOf (lval->Type+1);
2884 /* rhs is not constant and already in the primary register */
2886 /* lhs is a pointer, scale rhs */
2887 g_scale (TypeOf (lval2.Type), CheckedSizeOf (lval->Type+1));
2891 /* Setup the code generator flags */
2892 lflags |= TypeOf (lval->Type) | CF_FORCECHAR;
2893 rflags |= TypeOf (lval2.Type);
2895 /* Cast the rhs to the type of the lhs */
2896 g_typecast (lflags, rflags);
2898 /* Output apropriate code */
2899 if (lval->Flags & E_MGLOBAL) {
2900 /* Static variable */
2901 lflags |= GlobalModeFlags (lval->Flags);
2902 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2903 g_addeqstatic (lflags, lval->Name, lval->ConstVal, lval2.ConstVal);
2905 g_subeqstatic (lflags, lval->Name, lval->ConstVal, lval2.ConstVal);
2907 } else if (lval->Flags & E_MLOCAL) {
2908 /* ref to localvar */
2909 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2910 g_addeqlocal (lflags, lval->ConstVal, lval2.ConstVal);
2912 g_subeqlocal (lflags, lval->ConstVal, lval2.ConstVal);
2914 } else if (lval->Flags & E_MCONST) {
2915 /* ref to absolute address */
2916 lflags |= CF_ABSOLUTE;
2917 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2918 g_addeqstatic (lflags, lval->ConstVal, 0, lval2.ConstVal);
2920 g_subeqstatic (lflags, lval->ConstVal, 0, lval2.ConstVal);
2922 } else if (lval->Flags & E_MEXPR) {
2923 /* Address in a/x. */
2924 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2925 g_addeqind (lflags, lval->ConstVal, lval2.ConstVal);
2927 g_subeqind (lflags, lval->ConstVal, lval2.ConstVal);
2930 Internal ("Invalid addressing mode");
2933 /* Expression is in the primary now */
2934 lval->Flags = E_MEXPR;
2939 int hie1 (ExprDesc* lval)
2940 /* Parse first level of expression hierarchy. */
2944 k = hieQuest (lval);
2945 switch (CurTok.Tok) {
2954 Error ("Invalid lvalue in assignment");
2960 case TOK_PLUS_ASSIGN:
2961 addsubeq (&GenPASGN, lval, k);
2964 case TOK_MINUS_ASSIGN:
2965 addsubeq (&GenSASGN, lval, k);
2968 case TOK_MUL_ASSIGN:
2969 opeq (&GenMASGN, lval, k);
2972 case TOK_DIV_ASSIGN:
2973 opeq (&GenDASGN, lval, k);
2976 case TOK_MOD_ASSIGN:
2977 opeq (&GenMOASGN, lval, k);
2980 case TOK_SHL_ASSIGN:
2981 opeq (&GenSLASGN, lval, k);
2984 case TOK_SHR_ASSIGN:
2985 opeq (&GenSRASGN, lval, k);
2988 case TOK_AND_ASSIGN:
2989 opeq (&GenAASGN, lval, k);
2992 case TOK_XOR_ASSIGN:
2993 opeq (&GenXOASGN, lval, k);
2997 opeq (&GenOASGN, lval, k);
3008 static int hie0 (ExprDesc *lval)
3009 /* Parse comma operator. */
3014 while (CurTok.Tok == TOK_COMMA) {
3023 int evalexpr (unsigned flags, int (*f) (ExprDesc*), ExprDesc* lval)
3024 /* Will evaluate an expression via the given function. If the result is a
3025 * constant, 0 is returned and the value is put in the lval struct. If the
3026 * result is not constant, exprhs is called to bring the value into the
3027 * primary register and 1 is returned.
3034 if (k == 0 && lval->Flags == E_MCONST) {
3035 /* Constant expression */
3038 /* Not constant, load into the primary */
3039 exprhs (flags, k, lval);
3046 static int expr (int (*func) (ExprDesc*), ExprDesc *lval)
3047 /* Expression parser; func is either hie0 or hie1. */
3056 /* Do some checks if code generation is still constistent */
3057 if (savsp != oursp) {
3059 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
3061 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
3069 void expression1 (ExprDesc* lval)
3070 /* Evaluate an expression on level 1 (no comma operator) and put it into
3071 * the primary register
3074 InitExprDesc (lval);
3075 exprhs (CF_NONE, expr (hie1, lval), lval);
3080 void expression (ExprDesc* lval)
3081 /* Evaluate an expression and put it into the primary register */
3083 InitExprDesc (lval);
3084 exprhs (CF_NONE, expr (hie0, lval), lval);
3089 void ConstExpr (ExprDesc* lval)
3090 /* Get a constant value */
3092 InitExprDesc (lval);
3093 if (expr (hie1, lval) != 0 || (lval->Flags & E_MCONST) == 0) {
3094 Error ("Constant expression expected");
3095 /* To avoid any compiler errors, make the expression a valid const */
3096 MakeConstIntExpr (lval, 1);
3102 void ConstIntExpr (ExprDesc* Val)
3103 /* Get a constant int value */
3106 if (expr (hie1, Val) != 0 ||
3107 (Val->Flags & E_MCONST) == 0 ||
3108 !IsClassInt (Val->Type)) {
3109 Error ("Constant integer expression expected");
3110 /* To avoid any compiler errors, make the expression a valid const */
3111 MakeConstIntExpr (Val, 1);
3117 void intexpr (ExprDesc* lval)
3118 /* Get an integer expression */
3121 if (!IsClassInt (lval->Type)) {
3122 Error ("Integer expression expected");
3123 /* To avoid any compiler errors, make the expression a valid int */
3124 MakeConstIntExpr (lval, 1);
3130 void Test (unsigned Label, int Invert)
3131 /* Evaluate a boolean test expression and jump depending on the result of
3132 * the test and on Invert.
3138 /* Evaluate the expression */
3139 k = expr (hie0, InitExprDesc (&lval));
3141 /* Check for a boolean expression */
3142 CheckBoolExpr (&lval);
3144 /* Check for a constant expression */
3145 if (k == 0 && lval.Flags == E_MCONST) {
3147 /* Constant rvalue */
3148 if (!Invert && lval.ConstVal == 0) {
3150 Warning ("Unreachable code");
3151 } else if (Invert && lval.ConstVal != 0) {
3157 /* If the expr hasn't set condition codes, set the force-test flag */
3158 if ((lval.Test & E_CC) == 0) {
3159 lval.Test |= E_FORCETEST;
3162 /* Load the value into the primary register */
3163 exprhs (CF_FORCECHAR, k, &lval);
3165 /* Generate the jump */
3167 g_truejump (CF_NONE, Label);
3169 g_falsejump (CF_NONE, Label);
3176 void TestInParens (unsigned Label, int Invert)
3177 /* Evaluate a boolean test expression in parenthesis and jump depending on
3178 * the result of the test * and on Invert.
3181 /* Eat the parenthesis */
3185 Test (Label, Invert);
3187 /* Check for the closing brace */