4 * Ullrich von Bassewitz, 21.06.1998
38 /*****************************************************************************/
40 /*****************************************************************************/
44 /* Generator attributes */
45 #define GEN_NOPUSH 0x01 /* Don't push lhs */
47 /* Map a generator function and its attributes to a token */
49 unsigned char Tok; /* Token to map to */
50 unsigned char Flags; /* Flags for generator function */
51 void (*Func) (unsigned, unsigned long); /* Generator func */
54 /* Descriptors for the operations */
55 static GenDesc GenMUL = { TOK_STAR, GEN_NOPUSH, g_mul };
56 static GenDesc GenDIV = { TOK_DIV, GEN_NOPUSH, g_div };
57 static GenDesc GenMOD = { TOK_MOD, GEN_NOPUSH, g_mod };
58 static GenDesc GenASL = { TOK_SHL, GEN_NOPUSH, g_asl };
59 static GenDesc GenASR = { TOK_SHR, GEN_NOPUSH, g_asr };
60 static GenDesc GenLT = { TOK_LT, GEN_NOPUSH, g_lt };
61 static GenDesc GenLE = { TOK_LE, GEN_NOPUSH, g_le };
62 static GenDesc GenGE = { TOK_GE, GEN_NOPUSH, g_ge };
63 static GenDesc GenGT = { TOK_GT, GEN_NOPUSH, g_gt };
64 static GenDesc GenEQ = { TOK_EQ, GEN_NOPUSH, g_eq };
65 static GenDesc GenNE = { TOK_NE, GEN_NOPUSH, g_ne };
66 static GenDesc GenAND = { TOK_AND, GEN_NOPUSH, g_and };
67 static GenDesc GenXOR = { TOK_XOR, GEN_NOPUSH, g_xor };
68 static GenDesc GenOR = { TOK_OR, GEN_NOPUSH, g_or };
69 static GenDesc GenPASGN = { TOK_PLUS_ASSIGN, GEN_NOPUSH, g_add };
70 static GenDesc GenSASGN = { TOK_MINUS_ASSIGN, GEN_NOPUSH, g_sub };
71 static GenDesc GenMASGN = { TOK_MUL_ASSIGN, GEN_NOPUSH, g_mul };
72 static GenDesc GenDASGN = { TOK_DIV_ASSIGN, GEN_NOPUSH, g_div };
73 static GenDesc GenMOASGN = { TOK_MOD_ASSIGN, GEN_NOPUSH, g_mod };
74 static GenDesc GenSLASGN = { TOK_SHL_ASSIGN, GEN_NOPUSH, g_asl };
75 static GenDesc GenSRASGN = { TOK_SHR_ASSIGN, GEN_NOPUSH, g_asr };
76 static GenDesc GenAASGN = { TOK_AND_ASSIGN, GEN_NOPUSH, g_and };
77 static GenDesc GenXOASGN = { TOK_XOR_ASSIGN, GEN_NOPUSH, g_xor };
78 static GenDesc GenOASGN = { TOK_OR_ASSIGN, GEN_NOPUSH, g_or };
82 /*****************************************************************************/
83 /* Function forwards */
84 /*****************************************************************************/
88 static int hie10 (struct expent* lval);
89 /* Handle ++, --, !, unary - etc. */
93 /*****************************************************************************/
94 /* Helper functions */
95 /*****************************************************************************/
99 static unsigned GlobalModeFlags (unsigned flags)
100 /* Return the addressing mode flags for the variable with the given flags */
103 if (flags == E_TGLAB) {
104 /* External linkage */
106 } else if (flags == E_TREGISTER) {
107 /* Register variable */
117 static int IsNullPtr (struct expent* lval)
118 /* Return true if this is the NULL pointer constant */
120 return (IsClassInt (lval->e_tptr) && /* Is it an int? */
121 lval->e_flags == E_MCONST && /* Is it constant? */
122 lval->e_const == 0); /* And is it's value zero? */
127 static type* promoteint (type* lhst, type* rhst)
128 /* In an expression with two ints, return the type of the result */
130 /* Rules for integer types:
131 * - If one of the values is a long, the result is long.
132 * - If one of the values is unsigned, the result is also unsigned.
133 * - Otherwise the result is an int.
135 if (IsTypeLong (lhst) || IsTypeLong (rhst)) {
136 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
142 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
152 static unsigned typeadjust (struct expent* lhs, struct expent* rhs, int NoPush)
153 /* Adjust the two values for a binary operation. lhs is expected on stack or
154 * to be constant, rhs is expected to be in the primary register or constant.
155 * The function will put the type of the result into lhs and return the
156 * code generator flags for the operation.
157 * If NoPush is given, it is assumed that the operation does not expect the lhs
158 * to be on stack, and that lhs is in a register instead.
159 * Beware: The function does only accept int types.
162 unsigned ltype, rtype;
165 /* Get the type strings */
166 type* lhst = lhs->e_tptr;
167 type* rhst = rhs->e_tptr;
169 /* Generate type adjustment code if needed */
170 ltype = TypeOf (lhst);
171 if (lhs->e_flags == E_MCONST) {
175 /* Value is in primary register*/
178 rtype = TypeOf (rhst);
179 if (rhs->e_flags == E_MCONST) {
182 flags = g_typeadjust (ltype, rtype);
184 /* Set the type of the result */
185 lhs->e_tptr = promoteint (lhst, rhst);
187 /* Return the code generator flags */
193 unsigned assignadjust (type* lhst, struct expent* rhs)
194 /* Adjust the type of the right hand expression so that it can be assigned to
195 * the type on the left hand side. This function is used for assignment and
196 * for converting parameters in a function call. It returns the code generator
197 * flags for the operation. The type string of the right hand side will be
198 * set to the type of the left hand side.
201 /* Get the type of the right hand side */
202 type* rhst = rhs->e_tptr;
204 /* After calling this function, rhs will have the type of the lhs */
207 /* First, do some type checking */
208 if (IsTypeVoid (lhst) || IsTypeVoid (rhst)) {
209 /* If one of the sides are of type void, output a more apropriate
212 Error (ERR_ILLEGAL_TYPE);
213 } else if (IsClassInt (lhst)) {
214 if (IsClassPtr (rhst)) {
215 /* Pointer -> int conversion */
216 Warning (WARN_PTR_TO_INT_CONV);
217 } else if (!IsClassInt (rhst)) {
218 Error (ERR_INCOMPATIBLE_TYPES);
220 /* Adjust the int types. To avoid manipulation of TOS mark lhs
223 unsigned flags = TypeOf (rhst);
224 if (rhs->e_flags & E_MCONST) {
227 return g_typeadjust (TypeOf (lhst) | CF_CONST, flags);
229 } else if (IsClassPtr (lhst)) {
230 if (IsClassPtr (rhst)) {
231 /* Pointer to pointer assignment is valid, if:
232 * - both point to the same types, or
233 * - the rhs pointer is a void pointer, or
234 * - the lhs pointer is a void pointer.
236 if (!IsTypeVoid (Indirect (lhst)) && !IsTypeVoid (Indirect (rhst))) {
237 /* Compare the types */
238 switch (TypeCmp (lhst, rhst)) {
240 case TC_INCOMPATIBLE:
241 Error (ERR_INCOMPATIBLE_POINTERS);
245 Error (ERR_QUAL_DIFF);
253 } else if (IsClassInt (rhst)) {
254 /* Int to pointer assignment is valid only for constant zero */
255 if ((rhs->e_flags & E_MCONST) == 0 || rhs->e_const != 0) {
256 Warning (WARN_INT_TO_PTR_CONV);
258 } else if (IsTypeFuncPtr (lhst) && IsTypeFunc(rhst)) {
259 /* Assignment of function to function pointer is allowed, provided
260 * that both functions have the same parameter list.
262 if (TypeCmp (Indirect (lhst), rhst) < TC_EQUAL) {
263 Error (ERR_INCOMPATIBLE_TYPES);
266 Error (ERR_INCOMPATIBLE_TYPES);
269 Error (ERR_INCOMPATIBLE_TYPES);
272 /* Return an int value in all cases where the operands are not both ints */
278 void DefineData (struct expent* lval)
279 /* Output a data definition for the given expression */
281 unsigned flags = lval->e_flags;
283 switch (flags & E_MCTYPE) {
287 g_defdata (TypeOf (lval->e_tptr) | CF_CONST, lval->e_const, 0);
291 /* Register variable. Taking the address is usually not
294 if (!AllowRegVarAddr) {
295 Error (ERR_CANNOT_TAKE_ADDR_OF_REG);
301 /* Local or global symbol */
302 g_defdata (GlobalModeFlags (flags), lval->e_name, lval->e_const);
306 /* a literal of some kind */
307 g_defdata (CF_STATIC, LiteralLabel, lval->e_const);
311 Internal ("Unknown constant type: %04X", flags);
317 static void lconst (unsigned flags, struct expent* lval)
318 /* Load primary reg with some constant value. */
320 switch (lval->e_flags & E_MCTYPE) {
323 g_leasp (lval->e_const);
327 /* Number constant */
328 g_getimmed (flags | TypeOf (lval->e_tptr) | CF_CONST, lval->e_const, 0);
332 /* Register variable. Taking the address is usually not
335 if (!AllowRegVarAddr) {
336 Error (ERR_CANNOT_TAKE_ADDR_OF_REG);
342 /* Local or global symbol, load address */
343 flags |= GlobalModeFlags (lval->e_flags);
345 g_getimmed (flags, lval->e_name, lval->e_const);
350 g_getimmed (CF_STATIC, LiteralLabel, lval->e_const);
354 Internal ("Unknown constant type: %04X", lval->e_flags);
360 static int kcalc (int tok, long val1, long val2)
361 /* Calculate an operation with left and right operand constant. */
365 return (val1 == val2);
367 return (val1 != val2);
369 return (val1 < val2);
371 return (val1 <= val2);
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);
390 Error (ERR_DIV_BY_ZERO);
393 return (val1 / val2);
396 Error (ERR_MOD_BY_ZERO);
399 return (val1 % val2);
401 Internal ("kcalc: got token 0x%X\n", tok);
408 static GenDesc* FindGen (int Tok, GenDesc** Table)
411 while ((G = *Table) != 0) {
422 static int istypeexpr (void)
423 /* Return true if some sort of variable or type is waiting (helper for cast
424 * and sizeof() in hie10).
429 return curtok == TOK_LPAREN && (
430 (nxttok >= TOK_FIRSTTYPE && nxttok <= TOK_LASTTYPE) ||
431 (nxttok == TOK_CONST) ||
432 (nxttok == TOK_IDENT &&
433 (Entry = FindSym (NextTok.Ident)) != 0 &&
440 static void PushAddr (struct expent* lval)
441 /* If the expression contains an address that was somehow evaluated,
442 * push this address on the stack. This is a helper function for all
443 * sorts of implicit or explicit assignment functions where the lvalue
444 * must be saved if it's not constant, before evaluating the rhs.
447 /* Get the address on stack if needed */
448 if (lval->e_flags != E_MREG && (lval->e_flags & E_MEXPR)) {
449 /* Push the address (always a pointer) */
456 /*****************************************************************************/
458 /*****************************************************************************/
462 void exprhs (unsigned flags, int k, struct expent *lval)
463 /* Put the result of an expression into the primary register */
469 /* Dereferenced lvalue */
470 flags |= TypeOf (lval->e_tptr);
471 if (lval->e_test & E_FORCETEST) {
473 lval->e_test &= ~E_FORCETEST;
475 if (f & E_MGLOBAL) { /* ref to globalvar */
477 flags |= GlobalModeFlags (f);
478 g_getstatic (flags, lval->e_name, lval->e_const);
479 } else if (f & E_MLOCAL) {
480 /* ref to localvar */
481 g_getlocal (flags, lval->e_const);
482 } else if (f & E_MCONST) {
483 /* ref to absolute address */
484 g_getstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
485 } else if (f == E_MEOFFS) {
486 g_getind (flags, lval->e_const);
487 } else if (f != E_MREG) {
490 } else if (f == E_MEOFFS) {
491 /* reference not storable */
492 flags |= TypeOf (lval->e_tptr);
493 g_inc (flags | CF_CONST, lval->e_const);
494 } else if ((f & E_MEXPR) == 0) {
495 /* Constant of some sort, load it into the primary */
496 lconst (flags, lval);
498 if (lval->e_test & E_FORCETEST) { /* we testing this value? */
500 AddCodeHint ("forcetest");
501 flags |= TypeOf (lval->e_tptr);
502 g_test (flags); /* yes, force a test */
503 lval->e_test &= ~E_FORCETEST;
508 static void callfunction (struct expent* lval)
509 /* Perform a function call. Called from hie11, this routine will
510 * either call the named function, or if the supplied ptr is zero,
511 * will call the contents of P.
515 FuncDesc* Func; /* Function descriptor */
516 int Ellipsis; /* True if we have an open param list */
517 SymEntry* Param; /* Current formal parameter */
518 unsigned ParamCount; /* Actual parameter count */
519 unsigned ParamSize; /* Number of parameter bytes */
525 /* Get a pointer to the function descriptor from the type string */
526 Func = GetFuncDesc (lval->e_tptr);
528 /* Initialize vars to keep gcc silent */
532 /* Check if this is a function pointer. If so, save it. If not, check for
533 * special known library functions that may be inlined.
535 if (lval->e_flags & E_MEXPR) {
536 /* Function pointer is in primary register, save it */
537 Mark = GetCodePos ();
539 } else if (InlineStdFuncs && IsStdFunc ((const char*) lval->e_name)) {
540 /* Inline this function */
541 HandleStdFunc (lval);
545 /* Parse the actual parameter list */
549 while (curtok != TOK_RPAREN) {
551 /* Add a hint for the optimizer */
552 AddCodeHint ("param:start");
554 /* Count arguments */
557 /* Fetch the pointer to the next argument, check for too many args */
558 if (ParamCount <= Func->ParamCount) {
559 if (ParamCount == 1) {
561 Param = Func->SymTab->SymHead;
564 Param = Param->NextSym;
565 CHECK ((Param->Flags & SC_PARAM) != 0);
567 } else if (!Ellipsis) {
568 /* Too many arguments. Do we have an open param list? */
569 if ((Func->Flags & FD_ELLIPSIS) == 0) {
570 /* End of param list reached, no ellipsis */
571 Error (ERR_TOO_MANY_FUNC_ARGS);
573 /* Assume an ellipsis even in case of errors to avoid an error
574 * message for each other argument.
579 /* Do some optimization: If we have a constant value to push,
580 * use a special function that may optimize.
583 if (!Ellipsis && SizeOf (Param->Type) == 1) {
584 CFlags = CF_FORCECHAR;
587 if (evalexpr (CFlags, hie1, &lval2) == 0) {
588 /* A constant value */
592 /* If we don't have an argument spec, accept anything, otherwise
593 * convert the actual argument to the type needed.
596 /* Promote the argument if needed */
597 assignadjust (Param->Type, &lval2);
599 /* If we have a prototype, chars may be pushed as chars */
600 Flags |= CF_FORCECHAR;
603 /* Use the type of the argument for the push */
604 Flags |= TypeOf (lval2.e_tptr);
606 /* If this is a fastcall function, don't push the last argument */
607 if (ParamCount == Func->ParamCount && (Func->Flags & FD_FASTCALL) != 0) {
608 /* Just load the argument into the primary. This is only needed if
609 * we have a constant argument, otherwise the value is already in
612 if (Flags & CF_CONST) {
613 exprhs (CF_FORCECHAR, 0, &lval2);
616 /* Push the argument, count the argument size */
617 g_push (Flags, lval2.e_const);
618 ParamSize += sizeofarg (Flags);
621 /* Add an optimizer hint */
622 AddCodeHint ("param:end");
624 /* Check for end of argument list */
625 if (curtok != TOK_COMMA) {
631 /* We need the closing bracket here */
634 /* Check if we had enough parameters */
635 if (ParamCount < Func->ParamCount) {
636 Error (ERR_TOO_FEW_FUNC_ARGS);
640 if (lval->e_flags & E_MEXPR) {
641 /* Function called via pointer: Restore it and call function */
642 if (ParamSize != 0) {
645 /* We had no parameters - remove save code */
648 g_callind (TypeOf (lval->e_tptr), ParamSize);
650 g_call (TypeOf (lval->e_tptr), (char*) lval->e_name, ParamSize);
657 /* This function parses ASM statements. The syntax of the ASM directive
658 * looks like the one defined for C++ (C has no ASM directive), that is,
659 * a string literal in parenthesis.
665 /* Need left parenthesis */
669 if (curtok != TOK_SCONST) {
670 Error (ERR_STRLIT_EXPECTED);
672 /* Write the string directly into the output, followed by a newline */
673 AddCodeLine (GetLiteral (curval));
675 /* Reset the string pointer, effectivly clearing the string from the
676 * string table. Since we're working with one token lookahead, this
677 * will fail if the next token is also a string token, but that's a
678 * syntax error anyway, because we expect a right paren.
680 ResetLiteralOffs (curval);
683 /* Skip the string token */
686 /* Closing paren needed */
692 static int primary (struct expent* lval)
693 /* This is the lowest level of the expression parser. */
697 /* not a test at all, yet */
700 /* Character and integer constants. */
701 if (curtok == TOK_ICONST || curtok == TOK_CCONST) {
702 lval->e_flags = E_MCONST | E_TCONST;
703 lval->e_tptr = curtype;
704 lval->e_const = curval;
709 /* Process parenthesized subexpression by calling the whole parser
712 if (curtok == TOK_LPAREN) {
714 memset (lval, 0, sizeof (*lval)); /* Remove any attributes */
720 /* All others may only be used if the expression evaluation is not called
721 * recursively by the preprocessor.
724 /* Illegal expression in PP mode */
725 Error (ERR_CPP_EXPR_EXPECTED);
726 lval->e_flags = E_MCONST;
727 lval->e_tptr = type_int;
732 if (curtok == TOK_IDENT) {
737 /* Get a pointer to the symbol table entry */
738 Sym = FindSym (CurTok.Ident);
740 /* Is the symbol known? */
743 /* We found the symbol - skip the name token */
746 /* The expression type is the symbol type */
747 lval->e_tptr = Sym->Type;
749 /* Check for illegal symbol types */
750 if ((Sym->Flags & SC_LABEL) == SC_LABEL) {
751 /* Cannot use labels in expressions */
752 Error (ERR_SYMBOL_KIND);
754 } else if (Sym->Flags & SC_TYPE) {
755 /* Cannot use type symbols */
756 Error (ERR_VAR_IDENT_EXPECTED);
757 /* Assume an int type to make lval valid */
758 lval->e_flags = E_MLOCAL | E_TLOFFS;
759 lval->e_tptr = type_int;
764 /* Check for legal symbol types */
765 if ((Sym->Flags & SC_ENUM) == SC_ENUM) {
766 lval->e_flags = E_MCONST;
767 lval->e_const = Sym->V.EnumVal;
769 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
771 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
772 lval->e_name = (unsigned long) Sym->Name;
774 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
776 lval->e_flags = E_MLOCAL | E_TLOFFS;
777 lval->e_const = Sym->V.Offs;
778 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
779 /* Static variable */
780 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
781 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
782 lval->e_name = (unsigned long) Sym->Name;
784 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
785 lval->e_name = Sym->V.Label;
788 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
789 /* Register variable, zero page based */
790 lval->e_flags = E_MGLOBAL | E_MCONST | E_TREGISTER;
791 lval->e_name = Sym->V.Offs;
794 /* Local static variable */
795 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
796 lval->e_name = Sym->V.Offs;
800 /* The symbol is referenced now */
801 Sym->Flags |= SC_REF;
802 if (IsTypeFunc (lval->e_tptr) || IsTypeArray (lval->e_tptr)) {
808 /* We did not find the symbol. Remember the name, then skip it */
809 strcpy (Ident, CurTok.Ident);
812 /* IDENT is either an auto-declared function or an undefined variable. */
813 if (curtok == TOK_LPAREN) {
814 /* Declare a function returning int. For that purpose, prepare a
815 * function signature for a function having an empty param list
818 Warning (WARN_FUNC_WITHOUT_PROTO);
819 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
820 lval->e_tptr = Sym->Type;
821 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
822 lval->e_name = (unsigned long) Sym->Name;
828 /* Undeclared Variable */
829 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
830 lval->e_flags = E_MLOCAL | E_TLOFFS;
831 lval->e_tptr = type_int;
833 Error (ERR_UNDEFINED_SYMBOL, Ident);
839 /* String literal? */
840 if (curtok == TOK_SCONST) {
841 lval->e_flags = E_MCONST | E_TLIT;
842 lval->e_const = curval;
843 lval->e_tptr = GetCharArrayType (strlen (GetLiteral (curval)));
849 if (curtok == TOK_ASM) {
851 lval->e_tptr = type_void;
852 lval->e_flags = E_MEXPR;
857 /* __AX__ and __EAX__ pseudo values? */
858 if (curtok == TOK_AX || curtok == TOK_EAX) {
859 lval->e_tptr = (curtok == TOK_AX)? type_uint : type_ulong;
860 lval->e_flags = E_MREG;
861 lval->e_test &= ~E_CC;
864 return 1; /* May be used as lvalue */
867 /* Illegal primary. */
868 Error (ERR_EXPR_EXPECTED);
869 lval->e_flags = E_MCONST;
870 lval->e_tptr = type_int;
876 static int arrayref (int k, struct expent* lval)
877 /* Handle an array reference */
891 /* Skip the bracket */
894 /* Get the type of left side */
895 tptr1 = lval->e_tptr;
897 /* We can apply a special treatment for arrays that have a const base
898 * address. This is true for most arrays and will produce a lot better
899 * code. Check if this is a const base address.
901 lflags = lval->e_flags & ~E_MCTYPE;
902 ConstBaseAddr = (lflags == E_MCONST) || /* Constant numeric address */
903 (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
904 lflags == E_MLOCAL; /* Local array */
906 /* If we have a constant base, we delay the address fetch */
907 Mark1 = GetCodePos ();
908 Mark2 = 0; /* Silence gcc */
909 if (!ConstBaseAddr) {
910 /* Get a pointer to the array into the primary */
911 exprhs (CF_NONE, k, lval);
913 /* Get the array pointer on stack. Do not push more than 16
914 * bit, even if this value is greater, since we cannot handle
915 * other than 16bit stuff when doing indexing.
917 Mark2 = GetCodePos ();
921 /* TOS now contains ptr to array elements. Get the subscript. */
923 if (l == 0 && lval2.e_flags == E_MCONST) {
925 /* The array subscript is a constant - remove value from stack */
926 if (!ConstBaseAddr) {
930 /* Get an array pointer into the primary */
931 exprhs (CF_NONE, k, lval);
934 if (IsClassPtr (tptr1)) {
936 /* Scale the subscript value according to element size */
937 lval2.e_const *= PSizeOf (tptr1);
939 /* Remove code for lhs load */
942 /* Handle constant base array on stack. Be sure NOT to
943 * handle pointers the same way, this won't work.
945 if (IsTypeArray (tptr1) &&
946 ((lval->e_flags & ~E_MCTYPE) == E_MCONST ||
947 (lval->e_flags & ~E_MCTYPE) == E_MLOCAL ||
948 (lval->e_flags & E_MGLOBAL) != 0 ||
949 (lval->e_flags == E_MEOFFS))) {
950 lval->e_const += lval2.e_const;
953 /* Pointer - load into primary and remember offset */
954 if ((lval->e_flags & E_MEXPR) == 0 || k != 0) {
955 exprhs (CF_NONE, k, lval);
957 lval->e_const = lval2.e_const;
958 lval->e_flags = E_MEOFFS;
961 /* Result is of element type */
962 lval->e_tptr = Indirect (tptr1);
967 } else if (IsClassPtr (tptr2 = lval2.e_tptr)) {
968 /* Subscript is pointer, get element type */
969 lval2.e_tptr = Indirect (tptr2);
971 /* Scale the rhs value in the primary register */
972 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
974 lval->e_tptr = lval2.e_tptr;
976 Error (ERR_CANNOT_SUBSCRIPT);
979 /* Add the subscript. Since arrays are indexed by integers,
980 * we will ignore the true type of the subscript here and
983 g_inc (CF_INT | CF_CONST, lval2.e_const);
987 /* Array subscript is not constant. Load it into the primary */
988 Mark2 = GetCodePos ();
989 exprhs (CF_NONE, l, &lval2);
991 tptr2 = lval2.e_tptr;
992 if (IsClassPtr (tptr1)) {
994 /* Get the element type */
995 lval->e_tptr = Indirect (tptr1);
997 /* Indexing is based on int's, so we will just use the integer
998 * portion of the index (which is in (e)ax, so there's no further
1001 g_scale (CF_INT, SizeOf (lval->e_tptr));
1003 } else if (IsClassPtr (tptr2)) {
1005 /* Get the element type */
1006 lval2.e_tptr = Indirect (tptr2);
1008 /* Get the int value on top. If we go here, we're sure,
1009 * both values are 16 bit (the first one was truncated
1010 * if necessary and the second one is a pointer).
1011 * Note: If ConstBaseAddr is true, we don't have a value on
1012 * stack, so to "swap" both, just push the subscript.
1014 if (ConstBaseAddr) {
1016 exprhs (CF_NONE, k, lval);
1023 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
1024 lval->e_tptr = lval2.e_tptr;
1026 Error (ERR_CANNOT_SUBSCRIPT);
1029 /* The offset is now in the primary register. It didn't have a
1030 * constant base address for the lhs, the lhs address is already
1031 * on stack, and we must add the offset. If the base address was
1032 * constant, we call special functions to add the address to the
1035 if (!ConstBaseAddr) {
1036 /* Add the subscript. Both values are int sized. */
1040 /* If the subscript has itself a constant address, it is often
1041 * a better idea to reverse again the order of the evaluation.
1042 * This will generate better code if the subscript is a byte
1043 * sized variable. But beware: This is only possible if the
1044 * subscript was not scaled, that is, if this was a byte array
1047 rflags = lval2.e_flags & ~E_MCTYPE;
1048 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1049 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1050 rflags == E_MLOCAL; /* Local array */
1052 if (ConstSubAddr && SizeOf (lval->e_tptr) == 1) {
1056 /* Reverse the order of evaluation */
1057 unsigned flags = (SizeOf (lval2.e_tptr) == 1)? CF_CHAR : CF_INT;
1060 /* Get a pointer to the array into the primary. We have changed
1061 * e_tptr above but we need the original type to load the
1062 * address, so restore it temporarily.
1064 SavedType = lval->e_tptr;
1065 lval->e_tptr = tptr1;
1066 exprhs (CF_NONE, k, lval);
1067 lval->e_tptr = SavedType;
1069 /* Add the variable */
1070 if (rflags == E_MLOCAL) {
1071 g_addlocal (flags, lval2.e_const);
1073 flags |= GlobalModeFlags (lval2.e_flags);
1074 g_addstatic (flags, lval2.e_name, lval2.e_const);
1077 if (lflags == E_MCONST) {
1078 /* Constant numeric address. Just add it */
1079 g_inc (CF_INT | CF_UNSIGNED, lval->e_const);
1080 } else if (lflags == E_MLOCAL) {
1081 /* Base address is a local variable address */
1082 if (IsTypeArray (tptr1)) {
1083 g_addaddr_local (CF_INT, lval->e_const);
1085 g_addlocal (CF_PTR, lval->e_const);
1088 /* Base address is a static variable address */
1089 unsigned flags = CF_INT;
1090 flags |= GlobalModeFlags (lval->e_flags);
1091 if (IsTypeArray (tptr1)) {
1092 g_addaddr_static (flags, lval->e_name, lval->e_const);
1094 g_addstatic (flags, lval->e_name, lval->e_const);
1100 lval->e_flags = E_MEXPR;
1103 return !IsTypeArray (lval->e_tptr);
1109 static int structref (int k, struct expent* lval)
1110 /* Process struct field after . or ->. */
1116 /* Skip the token and check for an identifier */
1118 if (curtok != TOK_IDENT) {
1119 Error (ERR_IDENT_EXPECTED);
1120 lval->e_tptr = type_int;
1124 /* Get the symbol table entry and check for a struct field */
1125 strcpy (Ident, CurTok.Ident);
1127 Field = FindStructField (lval->e_tptr, Ident);
1129 Error (ERR_STRUCT_FIELD_MISMATCH, Ident);
1130 lval->e_tptr = type_int;
1134 /* If we have constant input data, the result is also constant */
1135 flags = lval->e_flags & ~E_MCTYPE;
1136 if (flags == E_MCONST ||
1137 (k == 0 && (flags == E_MLOCAL ||
1138 (flags & E_MGLOBAL) != 0 ||
1139 lval->e_flags == E_MEOFFS))) {
1140 lval->e_const += Field->V.Offs;
1142 if ((flags & E_MEXPR) == 0 || k != 0) {
1143 exprhs (CF_NONE, k, lval);
1145 lval->e_const = Field->V.Offs;
1146 lval->e_flags = E_MEOFFS;
1148 lval->e_tptr = Field->Type;
1149 return !IsTypeArray (Field->Type);
1154 static int hie11 (struct expent *lval)
1155 /* Handle compound types (structs and arrays) */
1162 if (curtok < TOK_LBRACK || curtok > TOK_PTR_REF) {
1169 if (curtok == TOK_LBRACK) {
1171 /* Array reference */
1172 k = arrayref (k, lval);
1174 } else if (curtok == TOK_LPAREN) {
1176 /* Function call. Skip the opening parenthesis */
1178 tptr = lval->e_tptr;
1179 if (IsTypeFunc (tptr) || IsTypeFuncPtr (tptr)) {
1180 if (IsTypeFuncPtr (tptr)) {
1181 /* Pointer to function. Handle transparently */
1182 exprhs (CF_NONE, k, lval); /* Function pointer to A/X */
1183 ++lval->e_tptr; /* Skip T_PTR */
1184 lval->e_flags |= E_MEXPR;
1186 callfunction (lval);
1187 lval->e_flags = E_MEXPR;
1188 lval->e_tptr += DECODE_SIZE + 1; /* Set to result */
1190 Error (ERR_ILLEGAL_FUNC_CALL);
1194 } else if (curtok == TOK_DOT) {
1196 if (!IsClassStruct (lval->e_tptr)) {
1197 Error (ERR_STRUCT_EXPECTED);
1199 k = structref (0, lval);
1201 } else if (curtok == TOK_PTR_REF) {
1203 tptr = lval->e_tptr;
1204 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1205 Error (ERR_STRUCT_PTR_EXPECTED);
1207 k = structref (k, lval);
1217 static void store (struct expent* lval)
1218 /* Store primary reg into this reference */
1224 flags = TypeOf (lval->e_tptr);
1225 if (f & E_MGLOBAL) {
1226 flags |= GlobalModeFlags (f);
1233 g_putstatic (flags, lval->e_name, lval->e_const);
1235 } else if (f & E_MLOCAL) {
1236 g_putlocal (flags, lval->e_const);
1237 } else if (f == E_MEOFFS) {
1238 g_putind (flags, lval->e_const);
1239 } else if (f != E_MREG) {
1241 g_putind (flags, 0);
1243 /* Store into absolute address */
1244 g_putstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
1248 /* Assume that each one of the stores will invalidate CC */
1249 lval->e_test &= ~E_CC;
1254 static void pre_incdec (struct expent* lval, void (*inc) (unsigned, unsigned long))
1255 /* Handle --i and ++i */
1262 if ((k = hie10 (lval)) == 0) {
1263 Error (ERR_LVALUE_EXPECTED);
1267 /* Get the data type */
1268 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR | CF_CONST;
1270 /* Get the increment value in bytes */
1271 val = (lval->e_tptr [0] == T_PTR)? PSizeOf (lval->e_tptr) : 1;
1273 /* We're currently only able to handle some adressing modes */
1274 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
1275 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
1276 (lval->e_flags & E_MCONST) == 0 && /* Constant address? */
1277 (lval->e_flags & E_MEXPR) == 0) { /* Address in a/x? */
1279 /* Use generic code. Push the address if needed */
1282 /* Fetch the value */
1283 exprhs (CF_NONE, k, lval);
1285 /* Increment value in primary */
1288 /* Store the result back */
1293 /* Special code for some addressing modes - use the special += ops */
1294 if (lval->e_flags & E_MGLOBAL) {
1295 flags |= GlobalModeFlags (lval->e_flags);
1297 g_addeqstatic (flags, lval->e_name, lval->e_const, val);
1299 g_subeqstatic (flags, lval->e_name, lval->e_const, val);
1301 } else if (lval->e_flags & E_MLOCAL) {
1302 /* ref to localvar */
1304 g_addeqlocal (flags, lval->e_const, val);
1306 g_subeqlocal (flags, lval->e_const, val);
1308 } else if (lval->e_flags & E_MCONST) {
1309 /* ref to absolute address */
1310 flags |= CF_ABSOLUTE;
1312 g_addeqstatic (flags, lval->e_const, 0, val);
1314 g_subeqstatic (flags, lval->e_const, 0, val);
1316 } else if (lval->e_flags & E_MEXPR) {
1317 /* Address in a/x. */
1319 g_addeqind (flags, lval->e_const, val);
1321 g_subeqind (flags, lval->e_const, val);
1324 Internal ("Invalid addressing mode");
1329 /* Result is an expression */
1330 lval->e_flags = E_MEXPR;
1335 static void post_incdec (struct expent *lval, int k, void (*inc) (unsigned, unsigned long))
1336 /* Handle i-- and i++ */
1342 Error (ERR_LVALUE_EXPECTED);
1346 /* Get the data type */
1347 flags = TypeOf (lval->e_tptr);
1349 /* Push the address if needed */
1352 /* Fetch the value and save it (since it's the result of the expression) */
1353 exprhs (CF_NONE, 1, lval);
1354 g_save (flags | CF_FORCECHAR);
1356 /* If we have a pointer expression, increment by the size of the type */
1357 if (lval->e_tptr[0] == T_PTR) {
1358 inc (flags | CF_CONST | CF_FORCECHAR, SizeOf (lval->e_tptr + 1));
1360 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1363 /* Store the result back */
1366 /* Restore the original value */
1367 g_restore (flags | CF_FORCECHAR);
1368 lval->e_flags = E_MEXPR;
1373 static void unaryop (int tok, struct expent* lval)
1374 /* Handle unary -/+ and ~ */
1381 if (k == 0 && lval->e_flags & E_MCONST) {
1382 /* Value is constant */
1384 case TOK_MINUS: lval->e_const = -lval->e_const; break;
1385 case TOK_PLUS: break;
1386 case TOK_COMP: lval->e_const = ~lval->e_const; break;
1387 default: Internal ("Unexpected token: %d", tok);
1390 /* Value is not constant */
1391 exprhs (CF_NONE, k, lval);
1393 /* Get the type of the expression */
1394 flags = TypeOf (lval->e_tptr);
1396 /* Handle the operation */
1398 case TOK_MINUS: g_neg (flags); break;
1399 case TOK_PLUS: break;
1400 case TOK_COMP: g_com (flags); break;
1401 default: Internal ("Unexpected token: %d", tok);
1403 lval->e_flags = E_MEXPR;
1409 static int typecast (struct expent* lval)
1410 /* Handle an explicit cast */
1413 type Type[MAXTYPELEN];
1416 /* Skip the left paren */
1425 /* Read the expression we have to cast */
1428 /* Get the type of the expression and honor constant values */
1429 rflags = TypeOf (lval->e_tptr);
1430 if (lval->e_flags & E_MCONST) {
1434 /* Do the actual cast. Special handling for void casts */
1435 if (!IsTypeVoid (Type)) {
1436 /* Mark the lhs as const to avoid a manipulation of TOS */
1437 g_typecast (TypeOf (Type) | CF_CONST, rflags);
1440 /* Use the new type */
1441 lval->e_tptr = TypeDup (Type);
1449 static int hie10 (struct expent* lval)
1450 /* Handle ++, --, !, unary - etc. */
1458 pre_incdec (lval, g_inc);
1462 pre_incdec (lval, g_dec);
1468 unaryop (curtok, lval);
1473 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1474 /* Constant expression */
1475 lval->e_const = !lval->e_const;
1477 g_bneg (TypeOf (lval->e_tptr));
1478 lval->e_test |= E_CC; /* bneg will set cc */
1479 lval->e_flags = E_MEXPR; /* say it's an expr */
1481 return 0; /* expr not storable */
1485 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1486 /* Expression is not const, indirect value loaded into primary */
1487 lval->e_flags = E_MEXPR;
1488 lval->e_const = 0; /* Offset is zero now */
1491 if (IsClassPtr (t)) {
1492 lval->e_tptr = Indirect (t);
1494 Error (ERR_ILLEGAL_INDIRECT);
1502 /* Allow the & operator with an array */
1503 if (!IsTypeArray (lval->e_tptr)) {
1504 Error (ERR_ILLEGAL_ADDRESS);
1507 t = TypeAlloc (TypeLen (lval->e_tptr) + 2);
1509 TypeCpy (t + 1, lval->e_tptr);
1516 if (istypeexpr ()) {
1517 type Type[MAXTYPELEN];
1519 lval->e_const = SizeOf (ParseType (Type));
1522 /* Remember the output queue pointer */
1523 CodeMark Mark = GetCodePos ();
1525 lval->e_const = SizeOf (lval->e_tptr);
1526 /* Remove any generated code */
1529 lval->e_flags = E_MCONST | E_TCONST;
1530 lval->e_tptr = type_uint;
1531 lval->e_test &= ~E_CC;
1535 if (istypeexpr ()) {
1537 return typecast (lval);
1544 post_incdec (lval, k, g_inc);
1548 post_incdec (lval, k, g_dec);
1558 static int hie_internal (GenDesc** ops, /* List of generators */
1559 struct expent* lval, /* parent expr's lval */
1560 int (*hienext) (struct expent*),
1561 int* UsedGen) /* next higher level */
1562 /* Helper function */
1565 struct expent lval2;
1569 token_t tok; /* The operator token */
1570 unsigned ltype, type;
1571 int rconst; /* Operand is a constant */
1577 while ((Gen = FindGen (curtok, ops)) != 0) {
1579 /* Tell the caller that we handled it's ops */
1582 /* All operators that call this function expect an int on the lhs */
1583 if (!IsClassInt (lval->e_tptr)) {
1584 Error (ERR_INT_EXPR_EXPECTED);
1587 /* Remember the operator token, then skip it */
1591 /* Get the lhs on stack */
1592 Mark1 = GetCodePos ();
1593 ltype = TypeOf (lval->e_tptr);
1594 if (k == 0 && lval->e_flags == E_MCONST) {
1595 /* Constant value */
1596 Mark2 = GetCodePos ();
1597 g_push (ltype | CF_CONST, lval->e_const);
1599 /* Value not constant */
1600 exprhs (CF_NONE, k, lval);
1601 Mark2 = GetCodePos ();
1605 /* Get the right hand side */
1606 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1608 /* Check the type of the rhs */
1609 if (!IsClassInt (lval2.e_tptr)) {
1610 Error (ERR_INT_EXPR_EXPECTED);
1613 /* Check for const operands */
1614 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1616 /* Both operands are constant, remove the generated code */
1620 /* Evaluate the result */
1621 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1623 /* Get the type of the result */
1624 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1628 /* If the right hand side is constant, and the generator function
1629 * expects the lhs in the primary, remove the push of the primary
1632 unsigned rtype = TypeOf (lval2.e_tptr);
1635 /* Second value is constant - check for div */
1638 if (tok == TOK_DIV && lval2.e_const == 0) {
1639 Error (ERR_DIV_BY_ZERO);
1640 } else if (tok == TOK_MOD && lval2.e_const == 0) {
1641 Error (ERR_MOD_BY_ZERO);
1643 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1646 ltype |= CF_REG; /* Value is in register */
1650 /* Determine the type of the operation result. */
1651 type |= g_typeadjust (ltype, rtype);
1652 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1655 Gen->Func (type, lval2.e_const);
1656 lval->e_flags = E_MEXPR;
1659 /* We have a rvalue now */
1668 static int hie_compare (GenDesc** ops, /* List of generators */
1669 struct expent* lval, /* parent expr's lval */
1670 int (*hienext) (struct expent*))
1671 /* Helper function for the compare operators */
1674 struct expent lval2;
1678 token_t tok; /* The operator token */
1680 int rconst; /* Operand is a constant */
1685 while ((Gen = FindGen (curtok, ops)) != 0) {
1687 /* Remember the operator token, then skip it */
1691 /* Get the lhs on stack */
1692 Mark1 = GetCodePos ();
1693 ltype = TypeOf (lval->e_tptr);
1694 if (k == 0 && lval->e_flags == E_MCONST) {
1695 /* Constant value */
1696 Mark2 = GetCodePos ();
1697 g_push (ltype | CF_CONST, lval->e_const);
1699 /* Value not constant */
1700 exprhs (CF_NONE, k, lval);
1701 Mark2 = GetCodePos ();
1705 /* Get the right hand side */
1706 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1708 /* Make sure, the types are compatible */
1709 if (IsClassInt (lval->e_tptr)) {
1710 if (!IsClassInt (lval2.e_tptr) && !(IsClassPtr(lval2.e_tptr) && IsNullPtr(lval))) {
1711 Error (ERR_INCOMPATIBLE_TYPES);
1713 } else if (IsClassPtr (lval->e_tptr)) {
1714 if (IsClassPtr (lval2.e_tptr)) {
1715 /* Both pointers are allowed in comparison if they point to
1716 * the same type, or if one of them is a void pointer.
1718 type* left = Indirect (lval->e_tptr);
1719 type* right = Indirect (lval2.e_tptr);
1720 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1721 /* Incomatible pointers */
1722 Error (ERR_INCOMPATIBLE_TYPES);
1724 } else if (!IsNullPtr (&lval2)) {
1725 Error (ERR_INCOMPATIBLE_TYPES);
1729 /* Check for const operands */
1730 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1732 /* Both operands are constant, remove the generated code */
1736 /* Evaluate the result */
1737 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1741 /* If the right hand side is constant, and the generator function
1742 * expects the lhs in the primary, remove the push of the primary
1748 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1751 ltype |= CF_REG; /* Value is in register */
1755 /* Determine the type of the operation result. If the left
1756 * operand is of type char and the right is a constant, or
1757 * if both operands are of type char, we will encode the
1758 * operation as char operation. Otherwise the default
1759 * promotions are used.
1761 if (IsTypeChar (lval->e_tptr) && (IsTypeChar (lval2.e_tptr) || rconst)) {
1763 if (IsSignUnsigned (lval->e_tptr) || IsSignUnsigned (lval2.e_tptr)) {
1764 flags |= CF_UNSIGNED;
1767 flags |= CF_FORCECHAR;
1770 unsigned rtype = TypeOf (lval2.e_tptr) | (flags & CF_CONST);
1771 flags |= g_typeadjust (ltype, rtype);
1775 Gen->Func (flags, lval2.e_const);
1776 lval->e_flags = E_MEXPR;
1779 /* Result type is always int */
1780 lval->e_tptr = type_int;
1782 /* We have a rvalue now, condition codes are set */
1784 lval->e_test |= E_CC;
1792 static int hie9 (struct expent *lval)
1793 /* Process * and / operators. */
1795 static GenDesc* hie9_ops [] = {
1796 &GenMUL, &GenDIV, &GenMOD, 0
1800 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1805 static void parseadd (int k, struct expent* lval)
1806 /* Parse an expression with the binary plus operator. lval contains the
1807 * unprocessed left hand side of the expression and will contain the
1808 * result of the expression on return.
1811 struct expent lval2;
1812 unsigned flags; /* Operation flags */
1813 CodeMark Mark; /* Remember code position */
1814 type* lhst; /* Type of left hand side */
1815 type* rhst; /* Type of right hand side */
1818 /* Skip the PLUS token */
1821 /* Get the left hand side type, initialize operation flags */
1822 lhst = lval->e_tptr;
1825 /* Check for constness on both sides */
1826 if (k == 0 && lval->e_flags == E_MCONST) {
1828 /* The left hand side is a constant. Good. Get rhs */
1829 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1831 /* Right hand side is also constant. Get the rhs type */
1832 rhst = lval2.e_tptr;
1834 /* Both expressions are constants. Check for pointer arithmetic */
1835 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1836 /* Left is pointer, right is int, must scale rhs */
1837 lval->e_const = lval->e_const + lval2.e_const * PSizeOf (lhst);
1838 /* Result type is a pointer */
1839 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1840 /* Left is int, right is pointer, must scale lhs */
1841 lval->e_const = lval->e_const * PSizeOf (rhst) + lval2.e_const;
1842 /* Result type is a pointer */
1843 lval->e_tptr = lval2.e_tptr;
1844 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1845 /* Integer addition */
1846 lval->e_const += lval2.e_const;
1847 typeadjust (lval, &lval2, 1);
1850 Error (ERR_OP_NOT_ALLOWED);
1853 /* Result is constant, condition codes not set */
1854 lval->e_test = E_MCONST;
1858 /* lhs is constant, rhs is not. Get the rhs type. */
1859 rhst = lval2.e_tptr;
1861 /* Check for pointer arithmetic */
1862 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1863 /* Left is pointer, right is int, must scale rhs */
1864 g_scale (CF_INT, PSizeOf (lhst));
1865 /* Operate on pointers, result type is a pointer */
1867 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1868 /* Left is int, right is pointer, must scale lhs */
1869 lval->e_const *= PSizeOf (rhst);
1870 /* Operate on pointers, result type is a pointer */
1872 lval->e_tptr = lval2.e_tptr;
1873 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1874 /* Integer addition */
1875 flags = typeadjust (lval, &lval2, 1);
1878 Error (ERR_OP_NOT_ALLOWED);
1881 /* Generate code for the add */
1882 g_inc (flags | CF_CONST, lval->e_const);
1884 /* Result is in primary register */
1885 lval->e_flags = E_MEXPR;
1886 lval->e_test &= ~E_CC;
1892 /* Left hand side is not constant. Get the value onto the stack. */
1893 exprhs (CF_NONE, k, lval); /* --> primary register */
1894 Mark = GetCodePos ();
1895 g_push (TypeOf (lval->e_tptr), 0); /* --> stack */
1897 /* Evaluate the rhs */
1898 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1900 /* Right hand side is a constant. Get the rhs type */
1901 rhst = lval2.e_tptr;
1903 /* Remove pushed value from stack */
1905 pop (TypeOf (lval->e_tptr));
1907 /* Check for pointer arithmetic */
1908 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1909 /* Left is pointer, right is int, must scale rhs */
1910 lval2.e_const *= PSizeOf (lhst);
1911 /* Operate on pointers, result type is a pointer */
1913 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1914 /* Left is int, right is pointer, must scale lhs (ptr only) */
1915 g_scale (CF_INT | CF_CONST, PSizeOf (rhst));
1916 /* Operate on pointers, result type is a pointer */
1918 lval->e_tptr = lval2.e_tptr;
1919 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1920 /* Integer addition */
1921 flags = typeadjust (lval, &lval2, 1);
1924 Error (ERR_OP_NOT_ALLOWED);
1927 /* Generate code for the add */
1928 g_inc (flags | CF_CONST, lval2.e_const);
1930 /* Result is in primary register */
1931 lval->e_flags = E_MEXPR;
1932 lval->e_test &= ~E_CC;
1936 /* lhs and rhs are not constant. Get the rhs type. */
1937 rhst = lval2.e_tptr;
1939 /* Check for pointer arithmetic */
1940 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1941 /* Left is pointer, right is int, must scale rhs */
1942 g_scale (CF_INT, PSizeOf (lhst));
1943 /* Operate on pointers, result type is a pointer */
1945 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1946 /* Left is int, right is pointer, must scale lhs */
1947 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
1948 g_swap (CF_INT); /* Swap TOS and primary */
1949 g_scale (CF_INT, PSizeOf (rhst));
1950 /* Operate on pointers, result type is a pointer */
1952 lval->e_tptr = lval2.e_tptr;
1953 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1954 /* Integer addition */
1955 flags = typeadjust (lval, &lval2, 0);
1958 Error (ERR_OP_NOT_ALLOWED);
1961 /* Generate code for the add */
1964 /* Result is in primary register */
1965 lval->e_flags = E_MEXPR;
1966 lval->e_test &= ~E_CC;
1975 static void parsesub (int k, struct expent* lval)
1976 /* Parse an expression with the binary minus operator. lval contains the
1977 * unprocessed left hand side of the expression and will contain the
1978 * result of the expression on return.
1981 struct expent lval2;
1982 unsigned flags; /* Operation flags */
1983 type* lhst; /* Type of left hand side */
1984 type* rhst; /* Type of right hand side */
1985 CodeMark Mark1; /* Save position of output queue */
1986 CodeMark Mark2; /* Another position in the queue */
1987 int rscale; /* Scale factor for the result */
1990 /* Skip the MINUS token */
1993 /* Get the left hand side type, initialize operation flags */
1994 lhst = lval->e_tptr;
1996 rscale = 1; /* Scale by 1, that is, don't scale */
1998 /* Remember the output queue position, then bring the value onto the stack */
1999 Mark1 = GetCodePos ();
2000 exprhs (CF_NONE, k, lval); /* --> primary register */
2001 Mark2 = GetCodePos ();
2002 g_push (TypeOf (lhst), 0); /* --> stack */
2004 /* Parse the right hand side */
2005 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2007 /* The right hand side is constant. Get the rhs type. */
2008 rhst = lval2.e_tptr;
2010 /* Check left hand side */
2011 if (k == 0 && lval->e_flags & E_MCONST) {
2013 /* Both sides are constant, remove generated code */
2015 pop (TypeOf (lhst)); /* Clean up the stack */
2017 /* Check for pointer arithmetic */
2018 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2019 /* Left is pointer, right is int, must scale rhs */
2020 lval->e_const -= lval2.e_const * PSizeOf (lhst);
2021 /* Operate on pointers, result type is a pointer */
2022 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2023 /* Left is pointer, right is pointer, must scale result */
2024 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2025 Error (ERR_INCOMPATIBLE_POINTERS);
2027 lval->e_const = (lval->e_const - lval2.e_const) / PSizeOf (lhst);
2029 /* Operate on pointers, result type is an integer */
2030 lval->e_tptr = type_int;
2031 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2032 /* Integer subtraction */
2033 typeadjust (lval, &lval2, 1);
2034 lval->e_const -= lval2.e_const;
2037 Error (ERR_OP_NOT_ALLOWED);
2040 /* Result is constant, condition codes not set */
2041 lval->e_flags = E_MCONST;
2042 lval->e_test &= ~E_CC;
2046 /* Left hand side is not constant, right hand side is.
2047 * Remove pushed value from stack.
2050 pop (TypeOf (lhst));
2052 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2053 /* Left is pointer, right is int, must scale rhs */
2054 lval2.e_const *= PSizeOf (lhst);
2055 /* Operate on pointers, result type is a pointer */
2057 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2058 /* Left is pointer, right is pointer, must scale result */
2059 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2060 Error (ERR_INCOMPATIBLE_POINTERS);
2062 rscale = PSizeOf (lhst);
2064 /* Operate on pointers, result type is an integer */
2066 lval->e_tptr = type_int;
2067 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2068 /* Integer subtraction */
2069 flags = typeadjust (lval, &lval2, 1);
2072 Error (ERR_OP_NOT_ALLOWED);
2075 /* Do the subtraction */
2076 g_dec (flags | CF_CONST, lval2.e_const);
2078 /* If this was a pointer subtraction, we must scale the result */
2080 g_scale (flags, -rscale);
2083 /* Result is in primary register */
2084 lval->e_flags = E_MEXPR;
2085 lval->e_test &= ~E_CC;
2091 /* Right hand side is not constant. Get the rhs type. */
2092 rhst = lval2.e_tptr;
2094 /* Check for pointer arithmetic */
2095 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2096 /* Left is pointer, right is int, must scale rhs */
2097 g_scale (CF_INT, PSizeOf (lhst));
2098 /* Operate on pointers, result type is a pointer */
2100 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2101 /* Left is pointer, right is pointer, must scale result */
2102 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2103 Error (ERR_INCOMPATIBLE_POINTERS);
2105 rscale = PSizeOf (lhst);
2107 /* Operate on pointers, result type is an integer */
2109 lval->e_tptr = type_int;
2110 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2111 /* Integer subtraction. If the left hand side descriptor says that
2112 * the lhs is const, we have to remove this mark, since this is no
2113 * longer true, lhs is on stack instead.
2115 if (lval->e_flags == E_MCONST) {
2116 lval->e_flags = E_MEXPR;
2118 /* Adjust operand types */
2119 flags = typeadjust (lval, &lval2, 0);
2122 Error (ERR_OP_NOT_ALLOWED);
2125 /* Generate code for the sub (the & is a hack here) */
2126 g_sub (flags & ~CF_CONST, 0);
2128 /* If this was a pointer subtraction, we must scale the result */
2130 g_scale (flags, -rscale);
2133 /* Result is in primary register */
2134 lval->e_flags = E_MEXPR;
2135 lval->e_test &= ~E_CC;
2141 static int hie8 (struct expent* lval)
2142 /* Process + and - binary operators. */
2144 int k = hie9 (lval);
2145 while (curtok == TOK_PLUS || curtok == TOK_MINUS) {
2147 if (curtok == TOK_PLUS) {
2160 static int hie7 (struct expent *lval)
2161 /* Parse << and >>. */
2163 static GenDesc* hie7_ops [] = {
2168 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2173 static int hie6 (struct expent *lval)
2174 /* process greater-than type comparators */
2176 static GenDesc* hie6_ops [] = {
2177 &GenLT, &GenLE, &GenGE, &GenGT, 0
2179 return hie_compare (hie6_ops, lval, hie7);
2184 static int hie5 (struct expent *lval)
2186 static GenDesc* hie5_ops[] = {
2189 return hie_compare (hie5_ops, lval, hie6);
2194 static int hie4 (struct expent* lval)
2195 /* Handle & (bitwise and) */
2197 static GenDesc* hie4_ops [] = {
2202 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2207 static int hie3 (struct expent *lval)
2208 /* Handle ^ (bitwise exclusive or) */
2210 static GenDesc* hie3_ops [] = {
2215 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2220 static int hie2 (struct expent *lval)
2221 /* Handle | (bitwise or) */
2223 static GenDesc* hie2_ops [] = {
2228 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2233 static int hieAnd (struct expent* lval, unsigned TrueLab, int* BoolOp)
2234 /* Process "exp && exp" */
2238 struct expent lval2;
2241 if (curtok == TOK_BOOL_AND) {
2243 /* Tell our caller that we're evaluating a boolean */
2246 /* Get a label that we will use for false expressions */
2249 /* If the expr hasn't set condition codes, set the force-test flag */
2250 if ((lval->e_test & E_CC) == 0) {
2251 lval->e_test |= E_FORCETEST;
2254 /* Load the value */
2255 exprhs (CF_FORCECHAR, k, lval);
2257 /* Generate the jump */
2258 g_falsejump (CF_NONE, lab);
2260 /* Parse more boolean and's */
2261 while (curtok == TOK_BOOL_AND) {
2268 if ((lval2.e_test & E_CC) == 0) {
2269 lval2.e_test |= E_FORCETEST;
2271 exprhs (CF_FORCECHAR, k, &lval2);
2273 /* Do short circuit evaluation */
2274 if (curtok == TOK_BOOL_AND) {
2275 g_falsejump (CF_NONE, lab);
2277 /* Last expression - will evaluate to true */
2278 g_truejump (CF_NONE, TrueLab);
2282 /* Define the false jump label here */
2283 g_defloclabel (lab);
2285 /* Define the label */
2286 lval->e_flags = E_MEXPR;
2287 lval->e_test |= E_CC; /* Condition codes are set */
2295 static int hieOr (struct expent *lval)
2296 /* Process "exp || exp". */
2299 struct expent lval2;
2300 int BoolOp = 0; /* Did we have a boolean op? */
2301 int AndOp; /* Did we have a && operation? */
2302 unsigned TrueLab; /* Jump to this label if true */
2306 TrueLab = GetLabel ();
2308 /* Call the next level parser */
2309 k = hieAnd (lval, TrueLab, &BoolOp);
2311 /* Any boolean or's? */
2312 if (curtok == TOK_BOOL_OR) {
2314 /* If the expr hasn't set condition codes, set the force-test flag */
2315 if ((lval->e_test & E_CC) == 0) {
2316 lval->e_test |= E_FORCETEST;
2319 /* Get first expr */
2320 exprhs (CF_FORCECHAR, k, lval);
2322 /* For each expression jump to TrueLab if true. Beware: If we
2323 * had && operators, the jump is already in place!
2326 g_truejump (CF_NONE, TrueLab);
2329 /* Remember that we had a boolean op */
2332 /* while there's more expr */
2333 while (curtok == TOK_BOOL_OR) {
2340 k = hieAnd (&lval2, TrueLab, &AndOp);
2341 if ((lval2.e_test & E_CC) == 0) {
2342 lval2.e_test |= E_FORCETEST;
2344 exprhs (CF_FORCECHAR, k, &lval2);
2346 /* If there is more to come, add shortcut boolean eval.
2347 * Beware: If we had && operators, the jump is already
2351 /* Seems this sometimes generates wrong code */
2352 if (curtok == TOK_BOOL_OR && !AndOp) {
2353 g_truejump (CF_NONE, TrueLab);
2356 g_truejump (CF_NONE, TrueLab);
2359 lval->e_flags = E_MEXPR;
2360 lval->e_test |= E_CC; /* Condition codes are set */
2364 /* If we really had boolean ops, generate the end sequence */
2366 DoneLab = GetLabel ();
2367 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2368 g_falsejump (CF_NONE, DoneLab);
2369 g_defloclabel (TrueLab);
2370 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2371 g_defloclabel (DoneLab);
2378 static int hieQuest (struct expent *lval)
2379 /* Parse "lvalue ? exp : exp" */
2384 struct expent lval2; /* Expression 2 */
2385 struct expent lval3; /* Expression 3 */
2386 type* type2; /* Type of expression 2 */
2387 type* type3; /* Type of expression 3 */
2388 type* rtype; /* Type of result */
2389 CodeMark Mark1; /* Save position in output code */
2390 CodeMark Mark2; /* Save position in output code */
2395 if (curtok == TOK_QUEST) {
2397 if ((lval->e_test & E_CC) == 0) {
2398 /* Condition codes not set, force a test */
2399 lval->e_test |= E_FORCETEST;
2401 exprhs (CF_NONE, k, lval);
2403 g_falsejump (CF_NONE, labf);
2405 /* Parse second and third expression */
2406 expression1 (&lval2);
2410 g_defloclabel (labf);
2411 expression1 (&lval3);
2413 /* Check if any conversions are needed, if so, do them.
2414 * Conversion rules for ?: expression are:
2415 * - if both expressions are int expressions, default promotion
2416 * rules for ints apply.
2417 * - if both expressions are pointers of the same type, the
2418 * result of the expression is of this type.
2419 * - if one of the expressions is a pointer and the other is
2420 * a zero constant, the resulting type is that of the pointer
2422 * - all other cases are flagged by an error.
2424 type2 = lval2.e_tptr;
2425 type3 = lval3.e_tptr;
2426 if (IsClassInt (type2) && IsClassInt (type3)) {
2428 /* Get common type */
2429 rtype = promoteint (type2, type3);
2431 /* Convert the third expression to this type if needed */
2432 g_typecast (TypeOf (rtype), TypeOf (type3));
2434 /* Setup a new label so that the expr3 code will jump around
2435 * the type cast code for expr2.
2437 labf = GetLabel (); /* Get new label */
2438 Mark1 = GetCodePos (); /* Remember current position */
2439 g_jump (labf); /* Jump around code */
2441 /* The jump for expr2 goes here */
2442 g_defloclabel (labt);
2444 /* Create the typecast code for expr2 */
2445 Mark2 = GetCodePos (); /* Remember position */
2446 g_typecast (TypeOf (rtype), TypeOf (type2));
2448 /* If the typecast did not produce code, remove the jump,
2449 * otherwise output the label.
2451 if (GetCodePos() == Mark2) {
2452 RemoveCode (Mark1); /* Remove code */
2454 /* We have typecast code, output label */
2455 g_defloclabel (labf);
2456 labt = 0; /* Mark other label as invalid */
2459 } else if (IsClassPtr (type2) && IsClassPtr (type3)) {
2460 /* Must point to same type */
2461 if (TypeCmp (Indirect (type2), Indirect (type3)) < TC_EQUAL) {
2462 Error (ERR_INCOMPATIBLE_TYPES);
2464 /* Result has the common type */
2465 rtype = lval2.e_tptr;
2466 } else if (IsClassPtr (type2) && IsNullPtr (&lval3)) {
2467 /* Result type is pointer, no cast needed */
2468 rtype = lval2.e_tptr;
2469 } else if (IsNullPtr (&lval2) && IsClassPtr (type3)) {
2470 /* Result type is pointer, no cast needed */
2471 rtype = lval3.e_tptr;
2473 Error (ERR_INCOMPATIBLE_TYPES);
2474 rtype = lval2.e_tptr; /* Doesn't matter here */
2477 /* If we don't have the label defined until now, do it */
2479 g_defloclabel (labt);
2482 /* Setup the target expression */
2483 lval->e_flags = E_MEXPR;
2484 lval->e_tptr = rtype;
2492 static void opeq (GenDesc* Gen, struct expent *lval, int k)
2493 /* Process "op=" operators. */
2495 struct expent lval2;
2502 Error (ERR_LVALUE_EXPECTED);
2506 /* Determine the type of the lhs */
2507 flags = TypeOf (lval->e_tptr);
2508 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2509 lval->e_tptr [0] == T_PTR;
2511 /* Get the lhs address on stack (if needed) */
2514 /* Fetch the lhs into the primary register if needed */
2515 exprhs (CF_NONE, k, lval);
2517 /* Bring the lhs on stack */
2518 Mark = GetCodePos ();
2521 /* Evaluate the rhs */
2522 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2523 /* The resulting value is a constant. If the generator has the NOPUSH
2524 * flag set, don't push the lhs.
2526 if (Gen->Flags & GEN_NOPUSH) {
2531 /* lhs is a pointer, scale rhs */
2532 lval2.e_const *= SizeOf (lval->e_tptr+1);
2535 /* If the lhs is character sized, the operation may be later done
2538 if (SizeOf (lval->e_tptr) == 1) {
2539 flags |= CF_FORCECHAR;
2542 /* Special handling for add and sub - some sort of a hack, but short code */
2543 if (Gen->Func == g_add) {
2544 g_inc (flags | CF_CONST, lval2.e_const);
2545 } else if (Gen->Func == g_sub) {
2546 g_dec (flags | CF_CONST, lval2.e_const);
2548 Gen->Func (flags | CF_CONST, lval2.e_const);
2551 /* rhs is not constant and already in the primary register */
2553 /* lhs is a pointer, scale rhs */
2554 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2557 /* If the lhs is character sized, the operation may be later done
2560 if (SizeOf (lval->e_tptr) == 1) {
2561 flags |= CF_FORCECHAR;
2564 /* Adjust the types of the operands if needed */
2565 Gen->Func (g_typeadjust (flags, TypeOf (lval2.e_tptr)), 0);
2568 lval->e_flags = E_MEXPR;
2573 static void addsubeq (GenDesc* Gen, struct expent *lval, int k)
2574 /* Process the += and -= operators */
2576 struct expent lval2;
2582 Error (ERR_LVALUE_EXPECTED);
2587 /* We're currently only able to handle some adressing modes */
2588 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
2589 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
2590 (lval->e_flags & E_MCONST) == 0) { /* Constant address? */
2591 /* Use generic routine */
2592 opeq (Gen, lval, k);
2596 /* Skip the operator */
2599 /* Check if we have a pointer expression and must scale rhs */
2600 MustScale = (lval->e_tptr [0] == T_PTR);
2602 /* Determine the code generator flags */
2603 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR;
2605 /* Evaluate the rhs */
2606 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2607 /* The resulting value is a constant. */
2609 /* lhs is a pointer, scale rhs */
2610 lval2.e_const *= SizeOf (lval->e_tptr+1);
2614 /* rhs is not constant and already in the primary register */
2616 /* lhs is a pointer, scale rhs */
2617 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2621 /* Adjust the rhs to the lhs */
2622 g_typeadjust (flags, TypeOf (lval2.e_tptr));
2624 /* Output apropriate code */
2625 if (lval->e_flags & E_MGLOBAL) {
2626 /* Static variable */
2627 flags |= GlobalModeFlags (lval->e_flags);
2628 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2629 g_addeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2631 g_subeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2633 } else if (lval->e_flags & E_MLOCAL) {
2634 /* ref to localvar */
2635 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2636 g_addeqlocal (flags, lval->e_const, lval2.e_const);
2638 g_subeqlocal (flags, lval->e_const, lval2.e_const);
2640 } else if (lval->e_flags & E_MCONST) {
2641 /* ref to absolute address */
2642 flags |= CF_ABSOLUTE;
2643 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2644 g_addeqstatic (flags, lval->e_const, 0, lval2.e_const);
2646 g_subeqstatic (flags, lval->e_const, 0, lval2.e_const);
2648 } else if (lval->e_flags & E_MEXPR) {
2649 /* Address in a/x. */
2650 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2651 g_addeqind (flags, lval->e_const, lval2.e_const);
2653 g_subeqind (flags, lval->e_const, lval2.e_const);
2656 Internal ("Invalid addressing mode");
2659 /* Expression is in the primary now */
2660 lval->e_flags = E_MEXPR;
2665 static void Assignment (struct expent* lval)
2666 /* Parse an assignment */
2669 struct expent lval2;
2671 type* ltype = lval->e_tptr;
2673 /* Check for assignment to const */
2674 if (IsQualConst (ltype)) {
2675 Error (ERR_CONST_ASSIGN);
2678 /* cc65 does not have full support for handling structs by value. Since
2679 * assigning structs is one of the more useful operations from this
2680 * familiy, allow it here.
2682 if (IsClassStruct (ltype)) {
2684 /* Bring the address of the lhs into the primary and push it */
2685 exprhs (0, 0, lval);
2686 g_push (CF_PTR | CF_UNSIGNED, 0);
2688 /* Get the expression on the right of the '=' into the primary */
2691 /* Get the address */
2692 exprhs (0, 0, &lval2);
2694 /* We need an lvalue */
2695 Error (ERR_LVALUE_EXPECTED);
2698 /* Push the address (or whatever is in ax in case of errors) */
2699 g_push (CF_PTR | CF_UNSIGNED, 0);
2701 /* Check for equality of the structs */
2702 if (TypeCmp (ltype, lval2.e_tptr) < TC_EQUAL) {
2703 Error (ERR_INCOMPATIBLE_TYPES);
2706 /* Load the size of the struct into the primary */
2707 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, SizeOf (ltype), 0);
2709 /* Call the memcpy function */
2710 g_call (CF_FIXARGC, "memcpy", 4);
2714 /* Get the address on stack if needed */
2717 /* No struct, setup flags for the load */
2718 flags = SizeOf (ltype) == 1? CF_FORCECHAR : CF_NONE;
2720 /* Get the expression on the right of the '=' into the primary */
2721 if (evalexpr (flags, hie1, &lval2) == 0) {
2722 /* Constant expression. Adjust the types */
2723 assignadjust (ltype, &lval2);
2724 /* Put the value into the primary register */
2725 lconst (flags, &lval2);
2727 /* Expression is not constant and already in the primary */
2728 assignadjust (ltype, &lval2);
2731 /* Generate a store instruction */
2736 /* Value is still in primary */
2737 lval->e_flags = E_MEXPR;
2742 int hie1 (struct expent* lval)
2743 /* Parse first level of expression hierarchy. */
2747 k = hieQuest (lval);
2757 Error (ERR_LVALUE_EXPECTED);
2763 case TOK_PLUS_ASSIGN:
2764 addsubeq (&GenPASGN, lval, k);
2767 case TOK_MINUS_ASSIGN:
2768 addsubeq (&GenSASGN, lval, k);
2771 case TOK_MUL_ASSIGN:
2772 opeq (&GenMASGN, lval, k);
2775 case TOK_DIV_ASSIGN:
2776 opeq (&GenDASGN, lval, k);
2779 case TOK_MOD_ASSIGN:
2780 opeq (&GenMOASGN, lval, k);
2783 case TOK_SHL_ASSIGN:
2784 opeq (&GenSLASGN, lval, k);
2787 case TOK_SHR_ASSIGN:
2788 opeq (&GenSRASGN, lval, k);
2791 case TOK_AND_ASSIGN:
2792 opeq (&GenAASGN, lval, k);
2795 case TOK_XOR_ASSIGN:
2796 opeq (&GenXOASGN, lval, k);
2800 opeq (&GenOASGN, lval, k);
2811 int hie0 (struct expent *lval)
2812 /* Parse comma operator. */
2817 while (curtok == TOK_COMMA) {
2826 int evalexpr (unsigned flags, int (*f) (struct expent*), struct expent* lval)
2827 /* Will evaluate an expression via the given function. If the result is a
2828 * constant, 0 is returned and the value is put in the lval struct. If the
2829 * result is not constant, exprhs is called to bring the value into the
2830 * primary register and 1 is returned.
2837 if (k == 0 && lval->e_flags == E_MCONST) {
2838 /* Constant expression */
2841 /* Not constant, load into the primary */
2842 exprhs (flags, k, lval);
2849 int expr (int (*func) (), struct expent *lval)
2850 /* Expression parser; func is either hie0 or hie1. */
2859 /* Do some checks if code generation is still constistent */
2860 if (savsp != oursp) {
2862 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
2864 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
2872 void expression1 (struct expent* lval)
2873 /* Evaluate an expression on level 1 (no comma operator) and put it into
2874 * the primary register
2877 memset (lval, 0, sizeof (*lval));
2878 exprhs (CF_NONE, expr (hie1, lval), lval);
2883 void expression (struct expent* lval)
2884 /* Evaluate an expression and put it into the primary register */
2886 memset (lval, 0, sizeof (*lval));
2887 exprhs (CF_NONE, expr (hie0, lval), lval);
2892 void constexpr (struct expent* lval)
2893 /* Get a constant value */
2895 memset (lval, 0, sizeof (*lval));
2896 if (expr (hie1, lval) != 0 || (lval->e_flags & E_MCONST) == 0) {
2897 Error (ERR_CONST_EXPR_EXPECTED);
2898 /* To avoid any compiler errors, make the expression a valid const */
2899 lval->e_flags = E_MCONST;
2900 lval->e_tptr = type_int;
2907 void intexpr (struct expent* lval)
2908 /* Get an integer expression */
2911 if (!IsClassInt (lval->e_tptr)) {
2912 Error (ERR_INT_EXPR_EXPECTED);
2913 /* To avoid any compiler errors, make the expression a valid int */
2914 lval->e_flags = E_MCONST;
2915 lval->e_tptr = type_int;
2922 void boolexpr (struct expent* lval)
2923 /* Get a boolean expression */
2925 /* Read an expression */
2928 /* If it's an integer, it's ok. If it's not an integer, but a pointer,
2929 * the pointer used in a boolean context is also ok (Ootherwise check if it's a pointer
2932 if (!IsClassInt (lval->e_tptr) && !IsClassPtr (lval->e_tptr)) {
2933 Error (ERR_INT_EXPR_EXPECTED);
2934 /* To avoid any compiler errors, make the expression a valid int */
2935 lval->e_flags = E_MCONST;
2936 lval->e_tptr = type_int;
2943 void test (unsigned label, int cond)
2944 /* Generate code to perform test and jump if false. */
2949 /* Eat the parenthesis */
2952 /* Prepare the expression, setup labels */
2953 memset (&lval, 0, sizeof (lval));
2954 lval.e_test = E_TEST;
2956 /* Generate code to eval the expr */
2957 k = expr (hie0, &lval);
2958 if (k == 0 && lval.e_flags == E_MCONST) {
2959 /* Constant rvalue */
2960 if (cond == 0 && lval.e_const == 0) {
2962 Warning (WARN_UNREACHABLE_CODE);
2963 } else if (cond && lval.e_const) {
2970 /* If the expr hasn't set condition codes, set the force-test flag */
2971 if ((lval.e_test & E_CC) == 0) {
2972 lval.e_test |= E_FORCETEST;
2975 /* Load the value into the primary register */
2976 exprhs (CF_FORCECHAR, k, &lval);
2978 /* Check for the closing brace */
2981 /* Generate the jump */
2983 g_truejump (CF_NONE, label);
2985 /* Special case (putting this here is a small hack - but hey, the
2986 * compiler itself is one big hack...): If a semicolon follows, we
2987 * don't have a statement and may omit the jump.
2989 if (curtok != TOK_SEMI) {
2990 g_falsejump (CF_NONE, label);