4 * Ullrich von Bassewitz, 21.06.1998
37 /*****************************************************************************/
39 /*****************************************************************************/
43 /* Generator attributes */
44 #define GEN_NOPUSH 0x01 /* Don't push lhs */
46 /* Map a generator function and its attributes to a token */
48 unsigned char Tok; /* Token to map to */
49 unsigned char Flags; /* Flags for generator function */
50 void (*Func) (unsigned, unsigned long); /* Generator func */
53 /* Descriptors for the operations */
54 static GenDesc GenMUL = { TOK_STAR, GEN_NOPUSH, g_mul };
55 static GenDesc GenDIV = { TOK_DIV, GEN_NOPUSH, g_div };
56 static GenDesc GenMOD = { TOK_MOD, GEN_NOPUSH, g_mod };
57 static GenDesc GenASL = { TOK_SHL, GEN_NOPUSH, g_asl };
58 static GenDesc GenASR = { TOK_SHR, GEN_NOPUSH, g_asr };
59 static GenDesc GenLT = { TOK_LT, GEN_NOPUSH, g_lt };
60 static GenDesc GenLE = { TOK_LE, GEN_NOPUSH, g_le };
61 static GenDesc GenGE = { TOK_GE, GEN_NOPUSH, g_ge };
62 static GenDesc GenGT = { TOK_GT, GEN_NOPUSH, g_gt };
63 static GenDesc GenEQ = { TOK_EQ, GEN_NOPUSH, g_eq };
64 static GenDesc GenNE = { TOK_NE, GEN_NOPUSH, g_ne };
65 static GenDesc GenAND = { TOK_AND, GEN_NOPUSH, g_and };
66 static GenDesc GenXOR = { TOK_XOR, GEN_NOPUSH, g_xor };
67 static GenDesc GenOR = { TOK_OR, GEN_NOPUSH, g_or };
68 static GenDesc GenPASGN = { TOK_PLUS_ASSIGN, GEN_NOPUSH, g_add };
69 static GenDesc GenSASGN = { TOK_MINUS_ASSIGN, GEN_NOPUSH, g_sub };
70 static GenDesc GenMASGN = { TOK_MUL_ASSIGN, GEN_NOPUSH, g_mul };
71 static GenDesc GenDASGN = { TOK_DIV_ASSIGN, GEN_NOPUSH, g_div };
72 static GenDesc GenMOASGN = { TOK_MOD_ASSIGN, GEN_NOPUSH, g_mod };
73 static GenDesc GenSLASGN = { TOK_SHL_ASSIGN, GEN_NOPUSH, g_asl };
74 static GenDesc GenSRASGN = { TOK_SHR_ASSIGN, GEN_NOPUSH, g_asr };
75 static GenDesc GenAASGN = { TOK_AND_ASSIGN, GEN_NOPUSH, g_and };
76 static GenDesc GenXOASGN = { TOK_XOR_ASSIGN, GEN_NOPUSH, g_xor };
77 static GenDesc GenOASGN = { TOK_OR_ASSIGN, GEN_NOPUSH, g_or };
81 /*****************************************************************************/
82 /* Function forwards */
83 /*****************************************************************************/
87 static int hie10 (struct expent* lval);
88 /* Handle ++, --, !, unary - etc. */
92 /*****************************************************************************/
93 /* Helper functions */
94 /*****************************************************************************/
98 static unsigned GlobalModeFlags (unsigned flags)
99 /* Return the addressing mode flags for the variable with the given flags */
102 if (flags == E_TGLAB) {
103 /* External linkage */
105 } else if (flags == E_TREGISTER) {
106 /* Register variable */
116 static int IsNullPtr (struct expent* lval)
117 /* Return true if this is the NULL pointer constant */
119 return (IsClassInt (lval->e_tptr) && /* Is it an int? */
120 lval->e_flags == E_MCONST && /* Is it constant? */
121 lval->e_const == 0); /* And is it's value zero? */
126 static type* promoteint (type* lhst, type* rhst)
127 /* In an expression with two ints, return the type of the result */
129 /* Rules for integer types:
130 * - If one of the values is a long, the result is long.
131 * - If one of the values is unsigned, the result is also unsigned.
132 * - Otherwise the result is an int.
134 if (IsTypeLong (lhst) || IsTypeLong (rhst)) {
135 if (IsUnsigned (lhst) || IsUnsigned (rhst)) {
141 if (IsUnsigned (lhst) || IsUnsigned (rhst)) {
151 static unsigned typeadjust (struct expent* lhs, struct expent* rhs, int NoPush)
152 /* Adjust the two values for a binary operation. lhs is expected on stack or
153 * to be constant, rhs is expected to be in the primary register or constant.
154 * The function will put the type of the result into lhs and return the
155 * code generator flags for the operation.
156 * If NoPush is given, it is assumed that the operation does not expect the lhs
157 * to be on stack, and that lhs is in a register instead.
158 * Beware: The function does only accept int types.
161 unsigned ltype, rtype;
164 /* Get the type strings */
165 type* lhst = lhs->e_tptr;
166 type* rhst = rhs->e_tptr;
168 /* Generate type adjustment code if needed */
169 ltype = TypeOf (lhst);
170 if (lhs->e_flags == E_MCONST) {
174 /* Value is in primary register*/
177 rtype = TypeOf (rhst);
178 if (rhs->e_flags == E_MCONST) {
181 flags = g_typeadjust (ltype, rtype);
183 /* Set the type of the result */
184 lhs->e_tptr = promoteint (lhst, rhst);
186 /* Return the code generator flags */
192 unsigned assignadjust (type* lhst, struct expent* rhs)
193 /* Adjust the type of the right hand expression so that it can be assigned to
194 * the type on the left hand side. This function is used for assignment and
195 * for converting parameters in a function call. It returns the code generator
196 * flags for the operation. The type string of the right hand side will be
197 * set to the type of the left hand side.
200 /* Get the type of the right hand side */
201 type* rhst = rhs->e_tptr;
203 /* After calling this function, rhs will have the type of the lhs */
206 /* First, do some type checking */
207 if (IsTypeVoid (lhst) || IsTypeVoid (rhst)) {
208 /* If one of the sides are of type void, output a more apropriate
211 Error (ERR_ILLEGAL_TYPE);
212 } else if (IsClassInt (lhst)) {
213 if (IsClassPtr (rhst)) {
214 /* Pointer -> int conversion */
215 Warning (WARN_PTR_TO_INT_CONV);
216 } else if (!IsClassInt (rhst)) {
217 Error (ERR_INCOMPATIBLE_TYPES);
219 /* Adjust the int types. To avoid manipulation of TOS mark lhs
222 unsigned flags = TypeOf (rhst);
223 if (rhs->e_flags & E_MCONST) {
226 return g_typeadjust (TypeOf (lhst) | CF_CONST, flags);
228 } else if (IsClassPtr (lhst)) {
229 if (IsClassPtr (rhst)) {
230 /* Pointer to pointer assignment is valid, if:
231 * - both point to the same types, or
232 * - the rhs pointer is a void pointer, or
233 * - the lhs pointer is a void pointer.
235 type* left = Indirect (lhst);
236 type* right = Indirect (rhst);
237 if (!EqualTypes (left, right) && *left != T_VOID && *right != T_VOID) {
238 Error (ERR_INCOMPATIBLE_POINTERS);
240 } else if (IsClassInt (rhst)) {
241 /* Int to pointer assignment is valid only for constant zero */
242 if ((rhs->e_flags & E_MCONST) == 0 || rhs->e_const != 0) {
243 Warning (WARN_INT_TO_PTR_CONV);
245 } else if (IsTypeFuncPtr (lhst) && IsTypeFunc(rhst)) {
246 /* Assignment of function to function pointer is allowed, provided
247 * that both functions have the same parameter list.
249 if (!EqualTypes(Indirect (lhst), rhst)) {
250 Error (ERR_INCOMPATIBLE_TYPES);
253 Error (ERR_INCOMPATIBLE_TYPES);
256 Error (ERR_INCOMPATIBLE_TYPES);
259 /* Return an int value in all cases where the operands are not both ints */
265 void DefineData (struct expent* lval)
266 /* Output a data definition for the given expression */
268 unsigned flags = lval->e_flags;
270 switch (flags & E_MCTYPE) {
274 g_defdata (TypeOf (lval->e_tptr) | CF_CONST, lval->e_const, 0);
278 /* Register variable. Taking the address is usually not
281 if (!AllowRegVarAddr) {
282 Error (ERR_CANNOT_TAKE_ADDR_OF_REG);
288 /* Local or global symbol */
289 g_defdata (GlobalModeFlags (flags), lval->e_name, lval->e_const);
293 /* a literal of some kind */
294 g_defdata (CF_STATIC, LiteralLabel, lval->e_const);
298 Internal ("Unknown constant type: %04X", flags);
304 static void lconst (unsigned flags, struct expent* lval)
305 /* Load primary reg with some constant value. */
307 switch (lval->e_flags & E_MCTYPE) {
310 g_leasp (lval->e_const);
314 /* Number constant */
315 g_getimmed (flags | TypeOf (lval->e_tptr) | CF_CONST, lval->e_const, 0);
319 /* Register variable. Taking the address is usually not
322 if (!AllowRegVarAddr) {
323 Error (ERR_CANNOT_TAKE_ADDR_OF_REG);
329 /* Local or global symbol, load address */
330 flags |= GlobalModeFlags (lval->e_flags);
332 g_getimmed (flags, lval->e_name, lval->e_const);
337 g_getimmed (CF_STATIC, LiteralLabel, lval->e_const);
341 Internal ("Unknown constant type: %04X", lval->e_flags);
347 static int kcalc (int tok, long val1, long val2)
348 /* Calculate an operation with left and right operand constant. */
352 return (val1 == val2);
354 return (val1 != val2);
356 return (val1 < val2);
358 return (val1 <= val2);
360 return (val1 >= val2);
362 return (val1 > val2);
364 return (val1 | val2);
366 return (val1 ^ val2);
368 return (val1 & val2);
370 return (val1 >> val2);
372 return (val1 << val2);
374 return (val1 * val2);
377 Error (ERR_DIV_BY_ZERO);
380 return (val1 / val2);
383 Error (ERR_MOD_BY_ZERO);
386 return (val1 % val2);
388 Internal ("kcalc: got token 0x%X\n", tok);
395 static GenDesc* FindGen (int Tok, GenDesc** Table)
398 while ((G = *Table) != 0) {
409 static int istypeexpr (void)
410 /* Return true if some sort of variable or type is waiting (helper for cast
411 * and sizeof() in hie10).
416 return curtok == TOK_LPAREN && (
417 (nxttok >= TOK_FIRSTTYPE && nxttok <= TOK_LASTTYPE) ||
418 (nxttok == TOK_CONST) ||
419 (nxttok == TOK_IDENT &&
420 (Entry = FindSym (NextTok.Ident)) != 0 &&
427 static void PushAddr (struct expent* lval)
428 /* If the expression contains an address that was somehow evaluated,
429 * push this address on the stack. This is a helper function for all
430 * sorts of implicit or explicit assignment functions where the lvalue
431 * must be saved if it's not constant, before evaluating the rhs.
434 /* Get the address on stack if needed */
435 if (lval->e_flags != E_MREG && (lval->e_flags & E_MEXPR)) {
436 /* Push the address (always a pointer) */
443 /*****************************************************************************/
445 /*****************************************************************************/
449 void exprhs (unsigned flags, int k, struct expent *lval)
450 /* Put the result of an expression into the primary register */
456 /* Dereferenced lvalue */
457 flags |= TypeOf (lval->e_tptr);
458 if (lval->e_test & E_FORCETEST) {
460 lval->e_test &= ~E_FORCETEST;
462 if (f & E_MGLOBAL) { /* ref to globalvar */
464 flags |= GlobalModeFlags (f);
465 g_getstatic (flags, lval->e_name, lval->e_const);
466 } else if (f & E_MLOCAL) {
467 /* ref to localvar */
468 g_getlocal (flags, lval->e_const);
469 } else if (f & E_MCONST) {
470 /* ref to absolute address */
471 g_getstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
472 } else if (f == E_MEOFFS) {
473 g_getind (flags, lval->e_const);
474 } else if (f != E_MREG) {
477 } else if (f == E_MEOFFS) {
478 /* reference not storable */
479 flags |= TypeOf (lval->e_tptr);
480 g_inc (flags | CF_CONST, lval->e_const);
481 } else if ((f & E_MEXPR) == 0) {
482 /* Constant of some sort, load it into the primary */
483 lconst (flags, lval);
485 if (lval->e_test & E_FORCETEST) { /* we testing this value? */
487 AddCodeHint ("forcetest");
488 flags |= TypeOf (lval->e_tptr);
489 g_test (flags); /* yes, force a test */
490 lval->e_test &= ~E_FORCETEST;
495 static void callfunction (struct expent* lval)
496 /* Perform a function call. Called from hie11, this routine will
497 * either call the named function, or if the supplied ptr is zero,
498 * will call the contents of P.
502 FuncDesc* Func; /* Function descriptor */
503 int Ellipsis; /* True if we have an open param list */
504 SymEntry* Param; /* Current formal parameter */
505 unsigned ParamCount; /* Actual parameter count */
506 unsigned ParamSize; /* Number of parameter bytes */
512 /* Get a pointer to the function descriptor from the type string */
513 Func = GetFuncDesc (lval->e_tptr);
515 /* Initialize vars to keep gcc silent */
519 /* Check if this is a function pointer. If so, save it. If not, check for
520 * special known library functions that may be inlined.
522 if (lval->e_flags & E_MEXPR) {
523 /* Function pointer is in primary register, save it */
524 Mark = GetCodePos ();
526 } else if (InlineStdFuncs && IsStdFunc ((const char*) lval->e_name)) {
527 /* Inline this function */
528 HandleStdFunc (lval);
532 /* Parse the actual parameter list */
536 while (curtok != TOK_RPAREN) {
538 /* Add a hint for the optimizer */
539 AddCodeHint ("param:start");
541 /* Count arguments */
544 /* Fetch the pointer to the next argument, check for too many args */
545 if (ParamCount <= Func->ParamCount) {
546 if (ParamCount == 1) {
548 Param = Func->SymTab->SymHead;
551 Param = Param->NextSym;
552 CHECK ((Param->Flags & SC_PARAM) != 0);
554 } else if (!Ellipsis) {
555 /* Too many arguments. Do we have an open param list? */
556 if ((Func->Flags & FD_ELLIPSIS) == 0) {
557 /* End of param list reached, no ellipsis */
558 Error (ERR_TOO_MANY_FUNC_ARGS);
560 /* Assume an ellipsis even in case of errors to avoid an error
561 * message for each other argument.
566 /* Do some optimization: If we have a constant value to push,
567 * use a special function that may optimize.
570 if (!Ellipsis && SizeOf (Param->Type) == 1) {
571 CFlags = CF_FORCECHAR;
574 if (evalexpr (CFlags, hie1, &lval2) == 0) {
575 /* A constant value */
579 /* If we don't have an argument spec, accept anything, otherwise
580 * convert the actual argument to the type needed.
583 /* Promote the argument if needed */
584 assignadjust (Param->Type, &lval2);
585 /* If we have a prototype, chars may be pushed as chars */
586 Flags |= CF_FORCECHAR;
589 /* Use the type of the argument for the push */
590 Flags |= TypeOf (lval2.e_tptr);
592 /* If this is a fastcall function, don't push the last argument */
593 if (ParamCount == Func->ParamCount && (Func->Flags & FD_FASTCALL) != 0) {
594 /* Just load the argument into the primary. This is only needed if
595 * we have a constant argument, otherwise the value is already in
598 if (Flags & CF_CONST) {
599 exprhs (CF_FORCECHAR, 0, &lval2);
602 /* Push the argument, count the argument size */
603 g_push (Flags, lval2.e_const);
604 ParamSize += sizeofarg (Flags);
607 /* Add an optimizer hint */
608 AddCodeHint ("param:end");
610 /* Check for end of argument list */
611 if (curtok != TOK_COMMA) {
617 /* We need the closing bracket here */
620 /* Check if we had enough parameters */
621 if (ParamCount < Func->ParamCount) {
622 Error (ERR_TOO_FEW_FUNC_ARGS);
626 if (lval->e_flags & E_MEXPR) {
627 /* Function called via pointer: Restore it and call function */
628 if (ParamSize != 0) {
631 /* We had no parameters - remove save code */
634 g_callind (TypeOf (lval->e_tptr), ParamSize);
636 g_call (TypeOf (lval->e_tptr), (char*) lval->e_name, ParamSize);
643 /* This function parses ASM statements. The syntax of the ASM directive
644 * looks like the one defined for C++ (C has no ASM directive), that is,
645 * a string literal in parenthesis.
651 /* Need left parenthesis */
655 if (curtok != TOK_SCONST) {
656 Error (ERR_STRLIT_EXPECTED);
658 /* Write the string directly into the output, followed by a newline */
659 AddCodeLine (GetLiteral (curval));
661 /* Reset the string pointer, effectivly clearing the string from the
662 * string table. Since we're working with one token lookahead, this
663 * will fail if the next token is also a string token, but that's a
664 * syntax error anyway, because we expect a right paren.
666 ResetLiteralOffs (curval);
669 /* Skip the string token */
672 /* Closing paren needed */
678 static int primary (struct expent* lval)
679 /* This is the lowest level of the expression parser. */
683 /* not a test at all, yet */
686 /* Character and integer constants. */
687 if (curtok == TOK_ICONST || curtok == TOK_CCONST) {
688 lval->e_flags = E_MCONST | E_TCONST;
689 lval->e_tptr = curtype;
690 lval->e_const = curval;
695 /* Process parenthesized subexpression by calling the whole parser
698 if (curtok == TOK_LPAREN) {
700 memset (lval, 0, sizeof (*lval)); /* Remove any attributes */
706 /* All others may only be used if the expression evaluation is not called
707 * recursively by the preprocessor.
710 /* Illegal expression in PP mode */
711 Error (ERR_CPP_EXPR_EXPECTED);
712 lval->e_flags = E_MCONST;
713 lval->e_tptr = type_int;
718 if (curtok == TOK_IDENT) {
723 /* Get a pointer to the symbol table entry */
724 Sym = FindSym (CurTok.Ident);
726 /* Is the symbol known? */
729 /* We found the symbol - skip the name token */
732 /* The expression type is the symbol type */
733 lval->e_tptr = Sym->Type;
735 /* Check for illegal symbol types */
736 if ((Sym->Flags & SC_LABEL) == SC_LABEL) {
737 /* Cannot use labels in expressions */
738 Error (ERR_SYMBOL_KIND);
740 } else if (Sym->Flags & SC_TYPE) {
741 /* Cannot use type symbols */
742 Error (ERR_VAR_IDENT_EXPECTED);
743 /* Assume an int type to make lval valid */
744 lval->e_flags = E_MLOCAL | E_TLOFFS;
745 lval->e_tptr = type_int;
750 /* Check for legal symbol types */
751 if ((Sym->Flags & SC_ENUM) == SC_ENUM) {
752 lval->e_flags = E_MCONST;
753 lval->e_const = Sym->V.EnumVal;
755 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
757 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
758 lval->e_name = (unsigned long) Sym->Name;
760 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
762 lval->e_flags = E_MLOCAL | E_TLOFFS;
763 lval->e_const = Sym->V.Offs;
764 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
765 /* Static variable */
766 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
767 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
768 lval->e_name = (unsigned long) Sym->Name;
770 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
771 lval->e_name = Sym->V.Label;
774 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
775 /* Register variable, zero page based */
776 lval->e_flags = E_MGLOBAL | E_MCONST | E_TREGISTER;
777 lval->e_name = Sym->V.Offs;
780 /* Local static variable */
781 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
782 lval->e_name = Sym->V.Offs;
786 /* The symbol is referenced now */
787 Sym->Flags |= SC_REF;
788 if (IsTypeFunc (lval->e_tptr) || IsTypeArray (lval->e_tptr)) {
794 /* We did not find the symbol. Remember the name, then skip it */
795 strcpy (Ident, CurTok.Ident);
798 /* IDENT is either an auto-declared function or an undefined variable. */
799 if (curtok == TOK_LPAREN) {
800 /* Declare a function returning int. For that purpose, prepare a
801 * function signature for a function having an empty param list
804 Warning (WARN_FUNC_WITHOUT_PROTO);
805 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
806 lval->e_tptr = Sym->Type;
807 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
808 lval->e_name = (unsigned long) Sym->Name;
814 /* Undeclared Variable */
815 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
816 lval->e_flags = E_MLOCAL | E_TLOFFS;
817 lval->e_tptr = type_int;
819 Error (ERR_UNDEFINED_SYMBOL, Ident);
825 /* String literal? */
826 if (curtok == TOK_SCONST) {
827 lval->e_flags = E_MCONST | E_TLIT;
828 lval->e_const = curval;
829 lval->e_tptr = GetCharArrayType (strlen (GetLiteral (curval)));
835 if (curtok == TOK_ASM) {
837 lval->e_tptr = type_void;
838 lval->e_flags = E_MEXPR;
843 /* __AX__ and __EAX__ pseudo values? */
844 if (curtok == TOK_AX || curtok == TOK_EAX) {
845 lval->e_tptr = (curtok == TOK_AX)? type_uint : type_ulong;
846 lval->e_flags = E_MREG;
847 lval->e_test &= ~E_CC;
850 return 1; /* May be used as lvalue */
853 /* Illegal primary. */
854 Error (ERR_EXPR_EXPECTED);
855 lval->e_flags = E_MCONST;
856 lval->e_tptr = type_int;
862 static int arrayref (int k, struct expent* lval)
863 /* Handle an array reference */
877 /* Skip the bracket */
880 /* Get the type of left side */
881 tptr1 = lval->e_tptr;
883 /* We can apply a special treatment for arrays that have a const base
884 * address. This is true for most arrays and will produce a lot better
885 * code. Check if this is a const base address.
887 lflags = lval->e_flags & ~E_MCTYPE;
888 ConstBaseAddr = (lflags == E_MCONST) || /* Constant numeric address */
889 (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
890 lflags == E_MLOCAL; /* Local array */
892 /* If we have a constant base, we delay the address fetch */
893 Mark1 = GetCodePos ();
894 Mark2 = 0; /* Silence gcc */
895 if (!ConstBaseAddr) {
896 /* Get a pointer to the array into the primary */
897 exprhs (CF_NONE, k, lval);
899 /* Get the array pointer on stack. Do not push more than 16
900 * bit, even if this value is greater, since we cannot handle
901 * other than 16bit stuff when doing indexing.
903 Mark2 = GetCodePos ();
907 /* TOS now contains ptr to array elements. Get the subscript. */
909 if (l == 0 && lval2.e_flags == E_MCONST) {
911 /* The array subscript is a constant - remove value from stack */
912 if (!ConstBaseAddr) {
916 /* Get an array pointer into the primary */
917 exprhs (CF_NONE, k, lval);
920 if (IsClassPtr (tptr1)) {
922 /* Scale the subscript value according to element size */
923 lval2.e_const *= PSizeOf (tptr1);
925 /* Remove code for lhs load */
928 /* Handle constant base array on stack. Be sure NOT to
929 * handle pointers the same way, this won't work.
931 if (IsTypeArray (tptr1) &&
932 ((lval->e_flags & ~E_MCTYPE) == E_MCONST ||
933 (lval->e_flags & ~E_MCTYPE) == E_MLOCAL ||
934 (lval->e_flags & E_MGLOBAL) != 0 ||
935 (lval->e_flags == E_MEOFFS))) {
936 lval->e_const += lval2.e_const;
939 /* Pointer - load into primary and remember offset */
940 if ((lval->e_flags & E_MEXPR) == 0 || k != 0) {
941 exprhs (CF_NONE, k, lval);
943 lval->e_const = lval2.e_const;
944 lval->e_flags = E_MEOFFS;
947 /* Result is of element type */
948 lval->e_tptr = Indirect (tptr1);
953 } else if (IsClassPtr (tptr2 = lval2.e_tptr)) {
954 /* Subscript is pointer, get element type */
955 lval2.e_tptr = Indirect (tptr2);
957 /* Scale the rhs value in the primary register */
958 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
960 lval->e_tptr = lval2.e_tptr;
962 Error (ERR_CANNOT_SUBSCRIPT);
965 /* Add the subscript. Since arrays are indexed by integers,
966 * we will ignore the true type of the subscript here and
969 g_inc (CF_INT | CF_CONST, lval2.e_const);
973 /* Array subscript is not constant. Load it into the primary */
974 Mark2 = GetCodePos ();
975 exprhs (CF_NONE, l, &lval2);
977 tptr2 = lval2.e_tptr;
978 if (IsClassPtr (tptr1)) {
980 /* Get the element type */
981 lval->e_tptr = Indirect (tptr1);
983 /* Indexing is based on int's, so we will just use the integer
984 * portion of the index (which is in (e)ax, so there's no further
987 g_scale (CF_INT, SizeOf (lval->e_tptr));
989 } else if (IsClassPtr (tptr2)) {
991 /* Get the element type */
992 lval2.e_tptr = Indirect (tptr2);
994 /* Get the int value on top. If we go here, we're sure,
995 * both values are 16 bit (the first one was truncated
996 * if necessary and the second one is a pointer).
997 * Note: If ConstBaseAddr is true, we don't have a value on
998 * stack, so to "swap" both, just push the subscript.
1000 if (ConstBaseAddr) {
1002 exprhs (CF_NONE, k, lval);
1009 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
1010 lval->e_tptr = lval2.e_tptr;
1012 Error (ERR_CANNOT_SUBSCRIPT);
1015 /* The offset is now in the primary register. It didn't have a
1016 * constant base address for the lhs, the lhs address is already
1017 * on stack, and we must add the offset. If the base address was
1018 * constant, we call special functions to add the address to the
1021 if (!ConstBaseAddr) {
1022 /* Add the subscript. Both values are int sized. */
1026 /* If the subscript has itself a constant address, it is often
1027 * a better idea to reverse again the order of the evaluation.
1028 * This will generate better code if the subscript is a byte
1029 * sized variable. But beware: This is only possible if the
1030 * subscript was not scaled, that is, if this was a byte array
1033 rflags = lval2.e_flags & ~E_MCTYPE;
1034 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1035 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1036 rflags == E_MLOCAL; /* Local array */
1038 if (ConstSubAddr && SizeOf (lval->e_tptr) == 1) {
1042 /* Reverse the order of evaluation */
1043 unsigned flags = (SizeOf (lval2.e_tptr) == 1)? CF_CHAR : CF_INT;
1046 /* Get a pointer to the array into the primary. We have changed
1047 * e_tptr above but we need the original type to load the
1048 * address, so restore it temporarily.
1050 SavedType = lval->e_tptr;
1051 lval->e_tptr = tptr1;
1052 exprhs (CF_NONE, k, lval);
1053 lval->e_tptr = SavedType;
1055 /* Add the variable */
1056 if (rflags == E_MLOCAL) {
1057 g_addlocal (flags, lval2.e_const);
1059 flags |= GlobalModeFlags (lval2.e_flags);
1060 g_addstatic (flags, lval2.e_name, lval2.e_const);
1063 if (lflags == E_MCONST) {
1064 /* Constant numeric address. Just add it */
1065 g_inc (CF_INT | CF_UNSIGNED, lval->e_const);
1066 } else if (lflags == E_MLOCAL) {
1067 /* Base address is a local variable address */
1068 if (IsTypeArray (tptr1)) {
1069 g_addaddr_local (CF_INT, lval->e_const);
1071 g_addlocal (CF_PTR, lval->e_const);
1074 /* Base address is a static variable address */
1075 unsigned flags = CF_INT;
1076 flags |= GlobalModeFlags (lval->e_flags);
1077 if (IsTypeArray (tptr1)) {
1078 g_addaddr_static (flags, lval->e_name, lval->e_const);
1080 g_addstatic (flags, lval->e_name, lval->e_const);
1086 lval->e_flags = E_MEXPR;
1089 return !IsTypeArray (lval->e_tptr);
1095 static int structref (int k, struct expent* lval)
1096 /* Process struct field after . or ->. */
1102 /* Skip the token and check for an identifier */
1104 if (curtok != TOK_IDENT) {
1105 Error (ERR_IDENT_EXPECTED);
1106 lval->e_tptr = type_int;
1110 /* Get the symbol table entry and check for a struct field */
1111 strcpy (Ident, CurTok.Ident);
1113 Field = FindStructField (lval->e_tptr, Ident);
1115 Error (ERR_STRUCT_FIELD_MISMATCH, Ident);
1116 lval->e_tptr = type_int;
1120 /* If we have constant input data, the result is also constant */
1121 flags = lval->e_flags & ~E_MCTYPE;
1122 if (flags == E_MCONST ||
1123 (k == 0 && (flags == E_MLOCAL ||
1124 (flags & E_MGLOBAL) != 0 ||
1125 lval->e_flags == E_MEOFFS))) {
1126 lval->e_const += Field->V.Offs;
1128 if ((flags & E_MEXPR) == 0 || k != 0) {
1129 exprhs (CF_NONE, k, lval);
1131 lval->e_const = Field->V.Offs;
1132 lval->e_flags = E_MEOFFS;
1134 lval->e_tptr = Field->Type;
1135 return !IsTypeArray (Field->Type);
1140 static int hie11 (struct expent *lval)
1141 /* Handle compound types (structs and arrays) */
1148 if (curtok < TOK_LBRACK || curtok > TOK_PTR_REF) {
1155 if (curtok == TOK_LBRACK) {
1157 /* Array reference */
1158 k = arrayref (k, lval);
1160 } else if (curtok == TOK_LPAREN) {
1162 /* Function call. Skip the opening parenthesis */
1164 tptr = lval->e_tptr;
1165 if (IsTypeFunc (tptr) || IsTypeFuncPtr (tptr)) {
1166 if (IsTypeFuncPtr (tptr)) {
1167 /* Pointer to function. Handle transparently */
1168 exprhs (CF_NONE, k, lval); /* Function pointer to A/X */
1169 ++lval->e_tptr; /* Skip T_PTR */
1170 lval->e_flags |= E_MEXPR;
1172 callfunction (lval);
1173 lval->e_flags = E_MEXPR;
1174 lval->e_tptr += DECODE_SIZE + 1; /* Set to result */
1176 Error (ERR_ILLEGAL_FUNC_CALL);
1180 } else if (curtok == TOK_DOT) {
1182 if (!IsClassStruct (lval->e_tptr)) {
1183 Error (ERR_STRUCT_EXPECTED);
1185 k = structref (0, lval);
1187 } else if (curtok == TOK_PTR_REF) {
1189 tptr = lval->e_tptr;
1190 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1191 Error (ERR_STRUCT_PTR_EXPECTED);
1193 k = structref (k, lval);
1203 static void store (struct expent* lval)
1204 /* Store primary reg into this reference */
1210 flags = TypeOf (lval->e_tptr);
1211 if (f & E_MGLOBAL) {
1212 flags |= GlobalModeFlags (f);
1219 g_putstatic (flags, lval->e_name, lval->e_const);
1221 } else if (f & E_MLOCAL) {
1222 g_putlocal (flags, lval->e_const);
1223 } else if (f == E_MEOFFS) {
1224 g_putind (flags, lval->e_const);
1225 } else if (f != E_MREG) {
1227 g_putind (flags, 0);
1229 /* Store into absolute address */
1230 g_putstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
1234 /* Assume that each one of the stores will invalidate CC */
1235 lval->e_test &= ~E_CC;
1240 static void pre_incdec (struct expent* lval, void (*inc) (unsigned, unsigned long))
1241 /* Handle --i and ++i */
1248 if ((k = hie10 (lval)) == 0) {
1249 Error (ERR_LVALUE_EXPECTED);
1253 /* Get the data type */
1254 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR | CF_CONST;
1256 /* Get the increment value in bytes */
1257 val = (lval->e_tptr [0] == T_PTR)? PSizeOf (lval->e_tptr) : 1;
1259 /* We're currently only able to handle some adressing modes */
1260 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
1261 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
1262 (lval->e_flags & E_MCONST) == 0 && /* Constant address? */
1263 (lval->e_flags & E_MEXPR) == 0) { /* Address in a/x? */
1265 /* Use generic code. Push the address if needed */
1268 /* Fetch the value */
1269 exprhs (CF_NONE, k, lval);
1271 /* Increment value in primary */
1274 /* Store the result back */
1279 /* Special code for some addressing modes - use the special += ops */
1280 if (lval->e_flags & E_MGLOBAL) {
1281 flags |= GlobalModeFlags (lval->e_flags);
1283 g_addeqstatic (flags, lval->e_name, lval->e_const, val);
1285 g_subeqstatic (flags, lval->e_name, lval->e_const, val);
1287 } else if (lval->e_flags & E_MLOCAL) {
1288 /* ref to localvar */
1290 g_addeqlocal (flags, lval->e_const, val);
1292 g_subeqlocal (flags, lval->e_const, val);
1294 } else if (lval->e_flags & E_MCONST) {
1295 /* ref to absolute address */
1296 flags |= CF_ABSOLUTE;
1298 g_addeqstatic (flags, lval->e_const, 0, val);
1300 g_subeqstatic (flags, lval->e_const, 0, val);
1302 } else if (lval->e_flags & E_MEXPR) {
1303 /* Address in a/x. */
1305 g_addeqind (flags, lval->e_const, val);
1307 g_subeqind (flags, lval->e_const, val);
1310 Internal ("Invalid addressing mode");
1315 /* Result is an expression */
1316 lval->e_flags = E_MEXPR;
1321 static void post_incdec (struct expent *lval, int k, void (*inc) (unsigned, unsigned long))
1322 /* Handle i-- and i++ */
1328 Error (ERR_LVALUE_EXPECTED);
1332 /* Get the data type */
1333 flags = TypeOf (lval->e_tptr);
1335 /* Push the address if needed */
1338 /* Fetch the value and save it (since it's the result of the expression) */
1339 exprhs (CF_NONE, 1, lval);
1340 g_save (flags | CF_FORCECHAR);
1342 /* If we have a pointer expression, increment by the size of the type */
1343 if (lval->e_tptr[0] == T_PTR) {
1344 inc (flags | CF_CONST | CF_FORCECHAR, SizeOf (lval->e_tptr + 1));
1346 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1349 /* Store the result back */
1352 /* Restore the original value */
1353 g_restore (flags | CF_FORCECHAR);
1354 lval->e_flags = E_MEXPR;
1359 static void unaryop (int tok, struct expent* lval)
1360 /* Handle unary -/+ and ~ */
1367 if (k == 0 && lval->e_flags & E_MCONST) {
1368 /* Value is constant */
1370 case TOK_MINUS: lval->e_const = -lval->e_const; break;
1371 case TOK_PLUS: break;
1372 case TOK_COMP: lval->e_const = ~lval->e_const; break;
1373 default: Internal ("Unexpected token: %d", tok);
1376 /* Value is not constant */
1377 exprhs (CF_NONE, k, lval);
1379 /* Get the type of the expression */
1380 flags = TypeOf (lval->e_tptr);
1382 /* Handle the operation */
1384 case TOK_MINUS: g_neg (flags); break;
1385 case TOK_PLUS: break;
1386 case TOK_COMP: g_com (flags); break;
1387 default: Internal ("Unexpected token: %d", tok);
1389 lval->e_flags = E_MEXPR;
1395 static int typecast (struct expent* lval)
1396 /* Handle an explicit cast */
1399 type Type[MAXTYPELEN];
1402 /* Skip the left paren */
1411 /* Read the expression we have to cast */
1414 /* Get the type of the expression and honor constant values */
1415 rflags = TypeOf (lval->e_tptr);
1416 if (lval->e_flags & E_MCONST) {
1420 /* Do the actual cast. Special handling for void casts */
1421 if (!IsTypeVoid (Type)) {
1422 /* Mark the lhs as const to avoid a manipulation of TOS */
1423 g_typecast (TypeOf (Type) | CF_CONST, rflags);
1426 /* Use the new type */
1427 lval->e_tptr = TypeDup (Type);
1435 static int hie10 (struct expent* lval)
1436 /* Handle ++, --, !, unary - etc. */
1444 pre_incdec (lval, g_inc);
1448 pre_incdec (lval, g_dec);
1454 unaryop (curtok, lval);
1459 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1460 /* Constant expression */
1461 lval->e_const = !lval->e_const;
1463 g_bneg (TypeOf (lval->e_tptr));
1464 lval->e_test |= E_CC; /* bneg will set cc */
1465 lval->e_flags = E_MEXPR; /* say it's an expr */
1467 return 0; /* expr not storable */
1471 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1472 /* Expression is not const, indirect value loaded into primary */
1473 lval->e_flags = E_MEXPR;
1474 lval->e_const = 0; /* Offset is zero now */
1477 if (IsClassPtr (t)) {
1478 lval->e_tptr = Indirect (t);
1480 Error (ERR_ILLEGAL_INDIRECT);
1488 /* Allow the & operator with an array */
1489 if (!IsTypeArray (lval->e_tptr)) {
1490 Error (ERR_ILLEGAL_ADDRESS);
1493 t = TypeAlloc (TypeLen (lval->e_tptr) + 2);
1495 TypeCpy (t + 1, lval->e_tptr);
1502 if (istypeexpr ()) {
1503 type Type[MAXTYPELEN];
1505 lval->e_const = SizeOf (ParseType (Type));
1508 /* Remember the output queue pointer */
1509 CodeMark Mark = GetCodePos ();
1511 lval->e_const = SizeOf (lval->e_tptr);
1512 /* Remove any generated code */
1515 lval->e_flags = E_MCONST | E_TCONST;
1516 lval->e_tptr = type_uint;
1517 lval->e_test &= ~E_CC;
1521 if (istypeexpr ()) {
1523 return typecast (lval);
1530 post_incdec (lval, k, g_inc);
1534 post_incdec (lval, k, g_dec);
1544 static int hie_internal (GenDesc** ops, /* List of generators */
1545 struct expent* lval, /* parent expr's lval */
1546 int (*hienext) (struct expent*),
1547 int* UsedGen) /* next higher level */
1548 /* Helper function */
1551 struct expent lval2;
1555 token_t tok; /* The operator token */
1556 unsigned ltype, type;
1557 int rconst; /* Operand is a constant */
1563 while ((Gen = FindGen (curtok, ops)) != 0) {
1565 /* Tell the caller that we handled it's ops */
1568 /* All operators that call this function expect an int on the lhs */
1569 if (!IsClassInt (lval->e_tptr)) {
1570 Error (ERR_INT_EXPR_EXPECTED);
1573 /* Remember the operator token, then skip it */
1577 /* Get the lhs on stack */
1578 Mark1 = GetCodePos ();
1579 ltype = TypeOf (lval->e_tptr);
1580 if (k == 0 && lval->e_flags == E_MCONST) {
1581 /* Constant value */
1582 Mark2 = GetCodePos ();
1583 g_push (ltype | CF_CONST, lval->e_const);
1585 /* Value not constant */
1586 exprhs (CF_NONE, k, lval);
1587 Mark2 = GetCodePos ();
1591 /* Get the right hand side */
1592 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1594 /* Check the type of the rhs */
1595 if (!IsClassInt (lval2.e_tptr)) {
1596 Error (ERR_INT_EXPR_EXPECTED);
1599 /* Check for const operands */
1600 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1602 /* Both operands are constant, remove the generated code */
1606 /* Evaluate the result */
1607 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1609 /* Get the type of the result */
1610 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1614 /* If the right hand side is constant, and the generator function
1615 * expects the lhs in the primary, remove the push of the primary
1618 unsigned rtype = TypeOf (lval2.e_tptr);
1621 /* Second value is constant - check for div */
1624 if (tok == TOK_DIV && lval2.e_const == 0) {
1625 Error (ERR_DIV_BY_ZERO);
1626 } else if (tok == TOK_MOD && lval2.e_const == 0) {
1627 Error (ERR_MOD_BY_ZERO);
1629 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1632 ltype |= CF_REG; /* Value is in register */
1636 /* Determine the type of the operation result. */
1637 type |= g_typeadjust (ltype, rtype);
1638 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1641 Gen->Func (type, lval2.e_const);
1642 lval->e_flags = E_MEXPR;
1645 /* We have a rvalue now */
1654 static int hie_compare (GenDesc** ops, /* List of generators */
1655 struct expent* lval, /* parent expr's lval */
1656 int (*hienext) (struct expent*))
1657 /* Helper function for the compare operators */
1660 struct expent lval2;
1664 token_t tok; /* The operator token */
1666 int rconst; /* Operand is a constant */
1671 while ((Gen = FindGen (curtok, ops)) != 0) {
1673 /* Remember the operator token, then skip it */
1677 /* Get the lhs on stack */
1678 Mark1 = GetCodePos ();
1679 ltype = TypeOf (lval->e_tptr);
1680 if (k == 0 && lval->e_flags == E_MCONST) {
1681 /* Constant value */
1682 Mark2 = GetCodePos ();
1683 g_push (ltype | CF_CONST, lval->e_const);
1685 /* Value not constant */
1686 exprhs (CF_NONE, k, lval);
1687 Mark2 = GetCodePos ();
1691 /* Get the right hand side */
1692 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1694 /* Make sure, the types are compatible */
1695 if (IsClassInt (lval->e_tptr)) {
1696 if (!IsClassInt (lval2.e_tptr) && !(IsClassPtr(lval2.e_tptr) && IsNullPtr(lval))) {
1697 Error (ERR_INCOMPATIBLE_TYPES);
1699 } else if (IsClassPtr (lval->e_tptr)) {
1700 if (IsClassPtr (lval2.e_tptr)) {
1701 /* Both pointers are allowed in comparison if they point to
1702 * the same type, or if one of them is a void pointer.
1704 type* left = Indirect (lval->e_tptr);
1705 type* right = Indirect (lval2.e_tptr);
1706 if (!EqualTypes (left, right) && *left != T_VOID && *right != T_VOID) {
1707 /* Incomatible pointers */
1708 Error (ERR_INCOMPATIBLE_TYPES);
1710 } else if (!IsNullPtr (&lval2)) {
1711 Error (ERR_INCOMPATIBLE_TYPES);
1715 /* Check for const operands */
1716 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1718 /* Both operands are constant, remove the generated code */
1722 /* Evaluate the result */
1723 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1727 /* If the right hand side is constant, and the generator function
1728 * expects the lhs in the primary, remove the push of the primary
1734 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1737 ltype |= CF_REG; /* Value is in register */
1741 /* Determine the type of the operation result. If the left
1742 * operand is of type char and the right is a constant, or
1743 * if both operands are of type char, we will encode the
1744 * operation as char operation. Otherwise the default
1745 * promotions are used.
1747 if (IsTypeChar (lval->e_tptr) && (IsTypeChar (lval2.e_tptr) || rconst)) {
1749 if (IsUnsigned (lval->e_tptr) || IsUnsigned (lval2.e_tptr)) {
1750 flags |= CF_UNSIGNED;
1753 flags |= CF_FORCECHAR;
1756 unsigned rtype = TypeOf (lval2.e_tptr) | (flags & CF_CONST);
1757 flags |= g_typeadjust (ltype, rtype);
1761 Gen->Func (flags, lval2.e_const);
1762 lval->e_flags = E_MEXPR;
1765 /* Result type is always int */
1766 lval->e_tptr = type_int;
1768 /* We have a rvalue now, condition codes are set */
1770 lval->e_test |= E_CC;
1778 static int hie9 (struct expent *lval)
1779 /* Process * and / operators. */
1781 static GenDesc* hie9_ops [] = {
1782 &GenMUL, &GenDIV, &GenMOD, 0
1786 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1791 static void parseadd (int k, struct expent* lval)
1792 /* Parse an expression with the binary plus operator. lval contains the
1793 * unprocessed left hand side of the expression and will contain the
1794 * result of the expression on return.
1797 struct expent lval2;
1798 unsigned flags; /* Operation flags */
1799 CodeMark Mark; /* Remember code position */
1800 type* lhst; /* Type of left hand side */
1801 type* rhst; /* Type of right hand side */
1804 /* Skip the PLUS token */
1807 /* Get the left hand side type, initialize operation flags */
1808 lhst = lval->e_tptr;
1811 /* Check for constness on both sides */
1812 if (k == 0 && lval->e_flags == E_MCONST) {
1814 /* The left hand side is a constant. Good. Get rhs */
1815 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1817 /* Right hand side is also constant. Get the rhs type */
1818 rhst = lval2.e_tptr;
1820 /* Both expressions are constants. Check for pointer arithmetic */
1821 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1822 /* Left is pointer, right is int, must scale rhs */
1823 lval->e_const = lval->e_const + lval2.e_const * PSizeOf (lhst);
1824 /* Result type is a pointer */
1825 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1826 /* Left is int, right is pointer, must scale lhs */
1827 lval->e_const = lval->e_const * PSizeOf (rhst) + lval2.e_const;
1828 /* Result type is a pointer */
1829 lval->e_tptr = lval2.e_tptr;
1830 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1831 /* Integer addition */
1832 lval->e_const += lval2.e_const;
1833 typeadjust (lval, &lval2, 1);
1836 Error (ERR_OP_NOT_ALLOWED);
1839 /* Result is constant, condition codes not set */
1840 lval->e_test = E_MCONST;
1844 /* lhs is constant, rhs is not. Get the rhs type. */
1845 rhst = lval2.e_tptr;
1847 /* Check for pointer arithmetic */
1848 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1849 /* Left is pointer, right is int, must scale rhs */
1850 g_scale (CF_INT, PSizeOf (lhst));
1851 /* Operate on pointers, result type is a pointer */
1853 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1854 /* Left is int, right is pointer, must scale lhs */
1855 lval->e_const *= PSizeOf (rhst);
1856 /* Operate on pointers, result type is a pointer */
1858 lval->e_tptr = lval2.e_tptr;
1859 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1860 /* Integer addition */
1861 flags = typeadjust (lval, &lval2, 1);
1864 Error (ERR_OP_NOT_ALLOWED);
1867 /* Generate code for the add */
1868 g_inc (flags | CF_CONST, lval->e_const);
1870 /* Result is in primary register */
1871 lval->e_flags = E_MEXPR;
1872 lval->e_test &= ~E_CC;
1878 /* Left hand side is not constant. Get the value onto the stack. */
1879 exprhs (CF_NONE, k, lval); /* --> primary register */
1880 Mark = GetCodePos ();
1881 g_push (TypeOf (lval->e_tptr), 0); /* --> stack */
1883 /* Evaluate the rhs */
1884 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1886 /* Right hand side is a constant. Get the rhs type */
1887 rhst = lval2.e_tptr;
1889 /* Remove pushed value from stack */
1891 pop (TypeOf (lval->e_tptr));
1893 /* Check for pointer arithmetic */
1894 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1895 /* Left is pointer, right is int, must scale rhs */
1896 lval2.e_const *= PSizeOf (lhst);
1897 /* Operate on pointers, result type is a pointer */
1899 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1900 /* Left is int, right is pointer, must scale lhs (ptr only) */
1901 g_scale (CF_INT | CF_CONST, PSizeOf (rhst));
1902 /* Operate on pointers, result type is a pointer */
1904 lval->e_tptr = lval2.e_tptr;
1905 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1906 /* Integer addition */
1907 flags = typeadjust (lval, &lval2, 1);
1910 Error (ERR_OP_NOT_ALLOWED);
1913 /* Generate code for the add */
1914 g_inc (flags | CF_CONST, lval2.e_const);
1916 /* Result is in primary register */
1917 lval->e_flags = E_MEXPR;
1918 lval->e_test &= ~E_CC;
1922 /* lhs and rhs are not constant. Get the rhs type. */
1923 rhst = lval2.e_tptr;
1925 /* Check for pointer arithmetic */
1926 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1927 /* Left is pointer, right is int, must scale rhs */
1928 g_scale (CF_INT, PSizeOf (lhst));
1929 /* Operate on pointers, result type is a pointer */
1931 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1932 /* Left is int, right is pointer, must scale lhs */
1933 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
1934 g_swap (CF_INT); /* Swap TOS and primary */
1935 g_scale (CF_INT, PSizeOf (rhst));
1936 /* Operate on pointers, result type is a pointer */
1938 lval->e_tptr = lval2.e_tptr;
1939 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1940 /* Integer addition */
1941 flags = typeadjust (lval, &lval2, 0);
1944 Error (ERR_OP_NOT_ALLOWED);
1947 /* Generate code for the add */
1950 /* Result is in primary register */
1951 lval->e_flags = E_MEXPR;
1952 lval->e_test &= ~E_CC;
1961 static void parsesub (int k, struct expent* lval)
1962 /* Parse an expression with the binary minus operator. lval contains the
1963 * unprocessed left hand side of the expression and will contain the
1964 * result of the expression on return.
1967 struct expent lval2;
1968 unsigned flags; /* Operation flags */
1969 type* lhst; /* Type of left hand side */
1970 type* rhst; /* Type of right hand side */
1971 CodeMark Mark1; /* Save position of output queue */
1972 CodeMark Mark2; /* Another position in the queue */
1973 int rscale; /* Scale factor for the result */
1976 /* Skip the MINUS token */
1979 /* Get the left hand side type, initialize operation flags */
1980 lhst = lval->e_tptr;
1982 rscale = 1; /* Scale by 1, that is, don't scale */
1984 /* Remember the output queue position, then bring the value onto the stack */
1985 Mark1 = GetCodePos ();
1986 exprhs (CF_NONE, k, lval); /* --> primary register */
1987 Mark2 = GetCodePos ();
1988 g_push (TypeOf (lhst), 0); /* --> stack */
1990 /* Parse the right hand side */
1991 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1993 /* The right hand side is constant. Get the rhs type. */
1994 rhst = lval2.e_tptr;
1996 /* Check left hand side */
1997 if (k == 0 && lval->e_flags & E_MCONST) {
1999 /* Both sides are constant, remove generated code */
2001 pop (TypeOf (lhst)); /* Clean up the stack */
2003 /* Check for pointer arithmetic */
2004 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2005 /* Left is pointer, right is int, must scale rhs */
2006 lval->e_const -= lval2.e_const * PSizeOf (lhst);
2007 /* Operate on pointers, result type is a pointer */
2008 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2009 /* Left is pointer, right is pointer, must scale result */
2010 if (TypeCmp (Indirect (lhst), Indirect (rhst)) != 0) {
2011 Error (ERR_INCOMPATIBLE_POINTERS);
2013 lval->e_const = (lval->e_const - lval2.e_const) / PSizeOf (lhst);
2015 /* Operate on pointers, result type is an integer */
2016 lval->e_tptr = type_int;
2017 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2018 /* Integer subtraction */
2019 typeadjust (lval, &lval2, 1);
2020 lval->e_const -= lval2.e_const;
2023 Error (ERR_OP_NOT_ALLOWED);
2026 /* Result is constant, condition codes not set */
2027 lval->e_flags = E_MCONST;
2028 lval->e_test &= ~E_CC;
2032 /* Left hand side is not constant, right hand side is.
2033 * Remove pushed value from stack.
2036 pop (TypeOf (lhst));
2038 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2039 /* Left is pointer, right is int, must scale rhs */
2040 lval2.e_const *= PSizeOf (lhst);
2041 /* Operate on pointers, result type is a pointer */
2043 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2044 /* Left is pointer, right is pointer, must scale result */
2045 if (TypeCmp (Indirect (lhst), Indirect (rhst)) != 0) {
2046 Error (ERR_INCOMPATIBLE_POINTERS);
2048 rscale = PSizeOf (lhst);
2050 /* Operate on pointers, result type is an integer */
2052 lval->e_tptr = type_int;
2053 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2054 /* Integer subtraction */
2055 flags = typeadjust (lval, &lval2, 1);
2058 Error (ERR_OP_NOT_ALLOWED);
2061 /* Do the subtraction */
2062 g_dec (flags | CF_CONST, lval2.e_const);
2064 /* If this was a pointer subtraction, we must scale the result */
2066 g_scale (flags, -rscale);
2069 /* Result is in primary register */
2070 lval->e_flags = E_MEXPR;
2071 lval->e_test &= ~E_CC;
2077 /* Right hand side is not constant. Get the rhs type. */
2078 rhst = lval2.e_tptr;
2080 /* Check for pointer arithmetic */
2081 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2082 /* Left is pointer, right is int, must scale rhs */
2083 g_scale (CF_INT, PSizeOf (lhst));
2084 /* Operate on pointers, result type is a pointer */
2086 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2087 /* Left is pointer, right is pointer, must scale result */
2088 if (TypeCmp (Indirect (lhst), Indirect (rhst)) != 0) {
2089 Error (ERR_INCOMPATIBLE_POINTERS);
2091 rscale = PSizeOf (lhst);
2093 /* Operate on pointers, result type is an integer */
2095 lval->e_tptr = type_int;
2096 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2097 /* Integer subtraction. If the left hand side descriptor says that
2098 * the lhs is const, we have to remove this mark, since this is no
2099 * longer true, lhs is on stack instead.
2101 if (lval->e_flags == E_MCONST) {
2102 lval->e_flags = E_MEXPR;
2104 /* Adjust operand types */
2105 flags = typeadjust (lval, &lval2, 0);
2108 Error (ERR_OP_NOT_ALLOWED);
2111 /* Generate code for the sub (the & is a hack here) */
2112 g_sub (flags & ~CF_CONST, 0);
2114 /* If this was a pointer subtraction, we must scale the result */
2116 g_scale (flags, -rscale);
2119 /* Result is in primary register */
2120 lval->e_flags = E_MEXPR;
2121 lval->e_test &= ~E_CC;
2127 static int hie8 (struct expent* lval)
2128 /* Process + and - binary operators. */
2130 int k = hie9 (lval);
2131 while (curtok == TOK_PLUS || curtok == TOK_MINUS) {
2133 if (curtok == TOK_PLUS) {
2146 static int hie7 (struct expent *lval)
2147 /* Parse << and >>. */
2149 static GenDesc* hie7_ops [] = {
2154 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2159 static int hie6 (struct expent *lval)
2160 /* process greater-than type comparators */
2162 static GenDesc* hie6_ops [] = {
2163 &GenLT, &GenLE, &GenGE, &GenGT, 0
2165 return hie_compare (hie6_ops, lval, hie7);
2170 static int hie5 (struct expent *lval)
2172 static GenDesc* hie5_ops[] = {
2175 return hie_compare (hie5_ops, lval, hie6);
2180 static int hie4 (struct expent* lval)
2181 /* Handle & (bitwise and) */
2183 static GenDesc* hie4_ops [] = {
2188 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2193 static int hie3 (struct expent *lval)
2194 /* Handle ^ (bitwise exclusive or) */
2196 static GenDesc* hie3_ops [] = {
2201 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2206 static int hie2 (struct expent *lval)
2207 /* Handle | (bitwise or) */
2209 static GenDesc* hie2_ops [] = {
2214 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2219 static int hieAnd (struct expent* lval, unsigned TrueLab, int* BoolOp)
2220 /* Process "exp && exp" */
2224 struct expent lval2;
2227 if (curtok == TOK_BOOL_AND) {
2229 /* Tell our caller that we're evaluating a boolean */
2232 /* Get a label that we will use for false expressions */
2235 /* If the expr hasn't set condition codes, set the force-test flag */
2236 if ((lval->e_test & E_CC) == 0) {
2237 lval->e_test |= E_FORCETEST;
2240 /* Load the value */
2241 exprhs (CF_FORCECHAR, k, lval);
2243 /* Generate the jump */
2244 g_falsejump (CF_NONE, lab);
2246 /* Parse more boolean and's */
2247 while (curtok == TOK_BOOL_AND) {
2254 if ((lval2.e_test & E_CC) == 0) {
2255 lval2.e_test |= E_FORCETEST;
2257 exprhs (CF_FORCECHAR, k, &lval2);
2259 /* Do short circuit evaluation */
2260 if (curtok == TOK_BOOL_AND) {
2261 g_falsejump (CF_NONE, lab);
2263 /* Last expression - will evaluate to true */
2264 g_truejump (CF_NONE, TrueLab);
2268 /* Define the false jump label here */
2269 g_defloclabel (lab);
2271 /* Define the label */
2272 lval->e_flags = E_MEXPR;
2273 lval->e_test |= E_CC; /* Condition codes are set */
2281 static int hieOr (struct expent *lval)
2282 /* Process "exp || exp". */
2285 struct expent lval2;
2286 int BoolOp = 0; /* Did we have a boolean op? */
2287 int AndOp; /* Did we have a && operation? */
2288 unsigned TrueLab; /* Jump to this label if true */
2292 TrueLab = GetLabel ();
2294 /* Call the next level parser */
2295 k = hieAnd (lval, TrueLab, &BoolOp);
2297 /* Any boolean or's? */
2298 if (curtok == TOK_BOOL_OR) {
2300 /* If the expr hasn't set condition codes, set the force-test flag */
2301 if ((lval->e_test & E_CC) == 0) {
2302 lval->e_test |= E_FORCETEST;
2305 /* Get first expr */
2306 exprhs (CF_FORCECHAR, k, lval);
2308 /* For each expression jump to TrueLab if true. Beware: If we
2309 * had && operators, the jump is already in place!
2312 g_truejump (CF_NONE, TrueLab);
2315 /* Remember that we had a boolean op */
2318 /* while there's more expr */
2319 while (curtok == TOK_BOOL_OR) {
2326 k = hieAnd (&lval2, TrueLab, &AndOp);
2327 if ((lval2.e_test & E_CC) == 0) {
2328 lval2.e_test |= E_FORCETEST;
2330 exprhs (CF_FORCECHAR, k, &lval2);
2332 /* If there is more to come, add shortcut boolean eval.
2333 * Beware: If we had && operators, the jump is already
2337 /* Seems this sometimes generates wrong code */
2338 if (curtok == TOK_BOOL_OR && !AndOp) {
2339 g_truejump (CF_NONE, TrueLab);
2342 g_truejump (CF_NONE, TrueLab);
2345 lval->e_flags = E_MEXPR;
2346 lval->e_test |= E_CC; /* Condition codes are set */
2350 /* If we really had boolean ops, generate the end sequence */
2352 DoneLab = GetLabel ();
2353 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2354 g_falsejump (CF_NONE, DoneLab);
2355 g_defloclabel (TrueLab);
2356 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2357 g_defloclabel (DoneLab);
2364 static int hieQuest (struct expent *lval)
2365 /* Parse "lvalue ? exp : exp" */
2370 struct expent lval2; /* Expression 2 */
2371 struct expent lval3; /* Expression 3 */
2372 type* type2; /* Type of expression 2 */
2373 type* type3; /* Type of expression 3 */
2374 type* rtype; /* Type of result */
2375 CodeMark Mark1; /* Save position in output code */
2376 CodeMark Mark2; /* Save position in output code */
2381 if (curtok == TOK_QUEST) {
2383 if ((lval->e_test & E_CC) == 0) {
2384 /* Condition codes not set, force a test */
2385 lval->e_test |= E_FORCETEST;
2387 exprhs (CF_NONE, k, lval);
2389 g_falsejump (CF_NONE, labf);
2391 /* Parse second and third expression */
2392 expression1 (&lval2);
2396 g_defloclabel (labf);
2397 expression1 (&lval3);
2399 /* Check if any conversions are needed, if so, do them.
2400 * Conversion rules for ?: expression are:
2401 * - if both expressions are int expressions, default promotion
2402 * rules for ints apply.
2403 * - if both expressions are pointers of the same type, the
2404 * result of the expression is of this type.
2405 * - if one of the expressions is a pointer and the other is
2406 * a zero constant, the resulting type is that of the pointer
2408 * - all other cases are flagged by an error.
2410 type2 = lval2.e_tptr;
2411 type3 = lval3.e_tptr;
2412 if (IsClassInt (type2) && IsClassInt (type3)) {
2414 /* Get common type */
2415 rtype = promoteint (type2, type3);
2417 /* Convert the third expression to this type if needed */
2418 g_typecast (TypeOf (rtype), TypeOf (type3));
2420 /* Setup a new label so that the expr3 code will jump around
2421 * the type cast code for expr2.
2423 labf = GetLabel (); /* Get new label */
2424 Mark1 = GetCodePos (); /* Remember current position */
2425 g_jump (labf); /* Jump around code */
2427 /* The jump for expr2 goes here */
2428 g_defloclabel (labt);
2430 /* Create the typecast code for expr2 */
2431 Mark2 = GetCodePos (); /* Remember position */
2432 g_typecast (TypeOf (rtype), TypeOf (type2));
2434 /* If the typecast did not produce code, remove the jump,
2435 * otherwise output the label.
2437 if (GetCodePos() == Mark2) {
2438 RemoveCode (Mark1); /* Remove code */
2440 /* We have typecast code, output label */
2441 g_defloclabel (labf);
2442 labt = 0; /* Mark other label as invalid */
2445 } else if (IsClassPtr (type2) && IsClassPtr (type3)) {
2446 /* Must point to same type */
2447 if (TypeCmp (Indirect (type2), Indirect (type3)) != 0) {
2448 Error (ERR_INCOMPATIBLE_TYPES);
2450 /* Result has the common type */
2451 rtype = lval2.e_tptr;
2452 } else if (IsClassPtr (type2) && IsNullPtr (&lval3)) {
2453 /* Result type is pointer, no cast needed */
2454 rtype = lval2.e_tptr;
2455 } else if (IsNullPtr (&lval2) && IsClassPtr (type3)) {
2456 /* Result type is pointer, no cast needed */
2457 rtype = lval3.e_tptr;
2459 Error (ERR_INCOMPATIBLE_TYPES);
2460 rtype = lval2.e_tptr; /* Doesn't matter here */
2463 /* If we don't have the label defined until now, do it */
2465 g_defloclabel (labt);
2468 /* Setup the target expression */
2469 lval->e_flags = E_MEXPR;
2470 lval->e_tptr = rtype;
2478 static void opeq (GenDesc* Gen, struct expent *lval, int k)
2479 /* Process "op=" operators. */
2481 struct expent lval2;
2488 Error (ERR_LVALUE_EXPECTED);
2492 /* Determine the type of the lhs */
2493 flags = TypeOf (lval->e_tptr);
2494 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2495 lval->e_tptr [0] == T_PTR;
2497 /* Get the lhs address on stack (if needed) */
2500 /* Fetch the lhs into the primary register if needed */
2501 exprhs (CF_NONE, k, lval);
2503 /* Bring the lhs on stack */
2504 Mark = GetCodePos ();
2507 /* Evaluate the rhs */
2508 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2509 /* The resulting value is a constant. If the generator has the NOPUSH
2510 * flag set, don't push the lhs.
2512 if (Gen->Flags & GEN_NOPUSH) {
2517 /* lhs is a pointer, scale rhs */
2518 lval2.e_const *= SizeOf (lval->e_tptr+1);
2521 /* If the lhs is character sized, the operation may be later done
2524 if (SizeOf (lval->e_tptr) == 1) {
2525 flags |= CF_FORCECHAR;
2528 /* Special handling for add and sub - some sort of a hack, but short code */
2529 if (Gen->Func == g_add) {
2530 g_inc (flags | CF_CONST, lval2.e_const);
2531 } else if (Gen->Func == g_sub) {
2532 g_dec (flags | CF_CONST, lval2.e_const);
2534 Gen->Func (flags | CF_CONST, lval2.e_const);
2537 /* rhs is not constant and already in the primary register */
2539 /* lhs is a pointer, scale rhs */
2540 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2543 /* If the lhs is character sized, the operation may be later done
2546 if (SizeOf (lval->e_tptr) == 1) {
2547 flags |= CF_FORCECHAR;
2550 /* Adjust the types of the operands if needed */
2551 Gen->Func (g_typeadjust (flags, TypeOf (lval2.e_tptr)), 0);
2554 lval->e_flags = E_MEXPR;
2559 static void addsubeq (GenDesc* Gen, struct expent *lval, int k)
2560 /* Process the += and -= operators */
2562 struct expent lval2;
2568 Error (ERR_LVALUE_EXPECTED);
2573 /* We're currently only able to handle some adressing modes */
2574 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
2575 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
2576 (lval->e_flags & E_MCONST) == 0) { /* Constant address? */
2577 /* Use generic routine */
2578 opeq (Gen, lval, k);
2582 /* Skip the operator */
2585 /* Check if we have a pointer expression and must scale rhs */
2586 MustScale = (lval->e_tptr [0] == T_PTR);
2588 /* Determine the code generator flags */
2589 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR;
2591 /* Evaluate the rhs */
2592 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2593 /* The resulting value is a constant. */
2595 /* lhs is a pointer, scale rhs */
2596 lval2.e_const *= SizeOf (lval->e_tptr+1);
2600 /* rhs is not constant and already in the primary register */
2602 /* lhs is a pointer, scale rhs */
2603 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2607 /* Adjust the rhs to the lhs */
2608 g_typeadjust (flags, TypeOf (lval2.e_tptr));
2610 /* Output apropriate code */
2611 if (lval->e_flags & E_MGLOBAL) {
2612 /* Static variable */
2613 flags |= GlobalModeFlags (lval->e_flags);
2614 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2615 g_addeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2617 g_subeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2619 } else if (lval->e_flags & E_MLOCAL) {
2620 /* ref to localvar */
2621 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2622 g_addeqlocal (flags, lval->e_const, lval2.e_const);
2624 g_subeqlocal (flags, lval->e_const, lval2.e_const);
2626 } else if (lval->e_flags & E_MCONST) {
2627 /* ref to absolute address */
2628 flags |= CF_ABSOLUTE;
2629 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2630 g_addeqstatic (flags, lval->e_const, 0, lval2.e_const);
2632 g_subeqstatic (flags, lval->e_const, 0, lval2.e_const);
2634 } else if (lval->e_flags & E_MEXPR) {
2635 /* Address in a/x. */
2636 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2637 g_addeqind (flags, lval->e_const, lval2.e_const);
2639 g_subeqind (flags, lval->e_const, lval2.e_const);
2642 Internal ("Invalid addressing mode");
2645 /* Expression is in the primary now */
2646 lval->e_flags = E_MEXPR;
2651 static void Assignment (struct expent* lval)
2652 /* Parse an assignment */
2655 struct expent lval2;
2657 type* ltype = lval->e_tptr;
2659 /* Check for assignment to const */
2660 if (IsConst (ltype)) {
2661 Error (ERR_CONST_ASSIGN);
2664 /* cc65 does not have full support for handling structs by value. Since
2665 * assigning structs is one of the more useful operations from this
2666 * familiy, allow it here.
2668 if (IsClassStruct (ltype)) {
2670 /* Bring the address of the lhs into the primary and push it */
2671 exprhs (0, 0, lval);
2672 g_push (CF_PTR | CF_UNSIGNED, 0);
2674 /* Get the expression on the right of the '=' into the primary */
2677 /* Get the address */
2678 exprhs (0, 0, &lval2);
2680 /* We need an lvalue */
2681 Error (ERR_LVALUE_EXPECTED);
2684 /* Push the address (or whatever is in ax in case of errors) */
2685 g_push (CF_PTR | CF_UNSIGNED, 0);
2687 /* Check for equality of the structs */
2688 if (!EqualTypes (ltype, lval2.e_tptr)) {
2689 Error (ERR_INCOMPATIBLE_TYPES);
2692 /* Load the size of the struct into the primary */
2693 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, SizeOf (ltype), 0);
2695 /* Call the memcpy function */
2696 g_call (CF_FIXARGC, "memcpy", 4);
2700 /* Get the address on stack if needed */
2703 /* No struct, setup flags for the load */
2704 flags = SizeOf (ltype) == 1? CF_FORCECHAR : CF_NONE;
2706 /* Get the expression on the right of the '=' into the primary */
2707 if (evalexpr (flags, hie1, &lval2) == 0) {
2708 /* Constant expression. Adjust the types */
2709 assignadjust (ltype, &lval2);
2710 /* Put the value into the primary register */
2711 lconst (flags, &lval2);
2713 /* Expression is not constant and already in the primary */
2714 assignadjust (ltype, &lval2);
2717 /* Generate a store instruction */
2722 /* Value is still in primary */
2723 lval->e_flags = E_MEXPR;
2728 int hie1 (struct expent* lval)
2729 /* Parse first level of expression hierarchy. */
2733 k = hieQuest (lval);
2743 Error (ERR_LVALUE_EXPECTED);
2749 case TOK_PLUS_ASSIGN:
2750 addsubeq (&GenPASGN, lval, k);
2753 case TOK_MINUS_ASSIGN:
2754 addsubeq (&GenSASGN, lval, k);
2757 case TOK_MUL_ASSIGN:
2758 opeq (&GenMASGN, lval, k);
2761 case TOK_DIV_ASSIGN:
2762 opeq (&GenDASGN, lval, k);
2765 case TOK_MOD_ASSIGN:
2766 opeq (&GenMOASGN, lval, k);
2769 case TOK_SHL_ASSIGN:
2770 opeq (&GenSLASGN, lval, k);
2773 case TOK_SHR_ASSIGN:
2774 opeq (&GenSRASGN, lval, k);
2777 case TOK_AND_ASSIGN:
2778 opeq (&GenAASGN, lval, k);
2781 case TOK_XOR_ASSIGN:
2782 opeq (&GenXOASGN, lval, k);
2786 opeq (&GenOASGN, lval, k);
2797 int hie0 (struct expent *lval)
2798 /* Parse comma operator. */
2803 while (curtok == TOK_COMMA) {
2812 int evalexpr (unsigned flags, int (*f) (struct expent*), struct expent* lval)
2813 /* Will evaluate an expression via the given function. If the result is a
2814 * constant, 0 is returned and the value is put in the lval struct. If the
2815 * result is not constant, exprhs is called to bring the value into the
2816 * primary register and 1 is returned.
2823 if (k == 0 && lval->e_flags == E_MCONST) {
2824 /* Constant expression */
2827 /* Not constant, load into the primary */
2828 exprhs (flags, k, lval);
2835 int expr (int (*func) (), struct expent *lval)
2836 /* Expression parser; func is either hie0 or hie1. */
2845 /* Do some checks if code generation is still constistent */
2846 if (savsp != oursp) {
2848 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
2850 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
2858 void expression1 (struct expent* lval)
2859 /* Evaluate an expression on level 1 (no comma operator) and put it into
2860 * the primary register
2863 memset (lval, 0, sizeof (*lval));
2864 exprhs (CF_NONE, expr (hie1, lval), lval);
2869 void expression (struct expent* lval)
2870 /* Evaluate an expression and put it into the primary register */
2872 memset (lval, 0, sizeof (*lval));
2873 exprhs (CF_NONE, expr (hie0, lval), lval);
2878 void constexpr (struct expent* lval)
2879 /* Get a constant value */
2881 memset (lval, 0, sizeof (*lval));
2882 if (expr (hie1, lval) != 0 || (lval->e_flags & E_MCONST) == 0) {
2883 Error (ERR_CONST_EXPR_EXPECTED);
2884 /* To avoid any compiler errors, make the expression a valid const */
2885 lval->e_flags = E_MCONST;
2886 lval->e_tptr = type_int;
2893 void intexpr (struct expent* lval)
2894 /* Get an integer expression */
2897 if (!IsClassInt (lval->e_tptr)) {
2898 Error (ERR_INT_EXPR_EXPECTED);
2899 /* To avoid any compiler errors, make the expression a valid int */
2900 lval->e_flags = E_MCONST;
2901 lval->e_tptr = type_int;
2908 void boolexpr (struct expent* lval)
2909 /* Get a boolean expression */
2911 /* Read an expression */
2914 /* If it's an integer, it's ok. If it's not an integer, but a pointer,
2915 * the pointer used in a boolean context is also ok (Ootherwise check if it's a pointer
2918 if (!IsClassInt (lval->e_tptr) && !IsClassPtr (lval->e_tptr)) {
2919 Error (ERR_INT_EXPR_EXPECTED);
2920 /* To avoid any compiler errors, make the expression a valid int */
2921 lval->e_flags = E_MCONST;
2922 lval->e_tptr = type_int;
2929 void test (unsigned label, int cond)
2930 /* Generate code to perform test and jump if false. */
2935 /* Eat the parenthesis */
2938 /* Prepare the expression, setup labels */
2939 memset (&lval, 0, sizeof (lval));
2940 lval.e_test = E_TEST;
2942 /* Generate code to eval the expr */
2943 k = expr (hie0, &lval);
2944 if (k == 0 && lval.e_flags == E_MCONST) {
2945 /* Constant rvalue */
2946 if (cond == 0 && lval.e_const == 0) {
2948 Warning (WARN_UNREACHABLE_CODE);
2949 } else if (cond && lval.e_const) {
2956 /* If the expr hasn't set condition codes, set the force-test flag */
2957 if ((lval.e_test & E_CC) == 0) {
2958 lval.e_test |= E_FORCETEST;
2961 /* Load the value into the primary register */
2962 exprhs (CF_FORCECHAR, k, &lval);
2964 /* Check for the closing brace */
2967 /* Generate the jump */
2969 g_truejump (CF_NONE, label);
2971 /* Special case (putting this here is a small hack - but hey, the
2972 * compiler itself is one big hack...): If a semicolon follows, we
2973 * don't have a statement and may omit the jump.
2975 if (curtok != TOK_SEMI) {
2976 g_falsejump (CF_NONE, label);