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 /* If the left side is not const and the right is const, print
584 * an error. Note: This is an incomplete check, since other parts
585 * of the type string may have a const qualifier, but it catches
586 * some errors and is cheap here. We will redo it the right way
587 * as soon as the parser is rewritten. ####
589 if (!IsConst (Param->Type) && IsConst (lval2.e_tptr)) {
590 Error (ERR_CONST_PARAM, ParamCount);
593 /* Promote the argument if needed */
594 assignadjust (Param->Type, &lval2);
595 /* If we have a prototype, chars may be pushed as chars */
596 Flags |= CF_FORCECHAR;
599 /* Use the type of the argument for the push */
600 Flags |= TypeOf (lval2.e_tptr);
602 /* If this is a fastcall function, don't push the last argument */
603 if (ParamCount == Func->ParamCount && (Func->Flags & FD_FASTCALL) != 0) {
604 /* Just load the argument into the primary. This is only needed if
605 * we have a constant argument, otherwise the value is already in
608 if (Flags & CF_CONST) {
609 exprhs (CF_FORCECHAR, 0, &lval2);
612 /* Push the argument, count the argument size */
613 g_push (Flags, lval2.e_const);
614 ParamSize += sizeofarg (Flags);
617 /* Add an optimizer hint */
618 AddCodeHint ("param:end");
620 /* Check for end of argument list */
621 if (curtok != TOK_COMMA) {
627 /* We need the closing bracket here */
630 /* Check if we had enough parameters */
631 if (ParamCount < Func->ParamCount) {
632 Error (ERR_TOO_FEW_FUNC_ARGS);
636 if (lval->e_flags & E_MEXPR) {
637 /* Function called via pointer: Restore it and call function */
638 if (ParamSize != 0) {
641 /* We had no parameters - remove save code */
644 g_callind (TypeOf (lval->e_tptr), ParamSize);
646 g_call (TypeOf (lval->e_tptr), (char*) lval->e_name, ParamSize);
653 /* This function parses ASM statements. The syntax of the ASM directive
654 * looks like the one defined for C++ (C has no ASM directive), that is,
655 * a string literal in parenthesis.
661 /* Need left parenthesis */
665 if (curtok != TOK_SCONST) {
666 Error (ERR_STRLIT_EXPECTED);
668 /* Write the string directly into the output, followed by a newline */
669 AddCodeLine (GetLiteral (curval));
671 /* Reset the string pointer, effectivly clearing the string from the
672 * string table. Since we're working with one token lookahead, this
673 * will fail if the next token is also a string token, but that's a
674 * syntax error anyway, because we expect a right paren.
676 ResetLiteralOffs (curval);
679 /* Skip the string token */
682 /* Closing paren needed */
688 static int primary (struct expent* lval)
689 /* This is the lowest level of the expression parser. */
693 /* not a test at all, yet */
696 /* Character and integer constants. */
697 if (curtok == TOK_ICONST || curtok == TOK_CCONST) {
698 lval->e_flags = E_MCONST | E_TCONST;
699 lval->e_tptr = curtype;
700 lval->e_const = curval;
705 /* Process parenthesized subexpression by calling the whole parser
708 if (curtok == TOK_LPAREN) {
710 memset (lval, 0, sizeof (*lval)); /* Remove any attributes */
716 /* All others may only be used if the expression evaluation is not called
717 * recursively by the preprocessor.
720 /* Illegal expression in PP mode */
721 Error (ERR_CPP_EXPR_EXPECTED);
722 lval->e_flags = E_MCONST;
723 lval->e_tptr = type_int;
728 if (curtok == TOK_IDENT) {
733 /* Get a pointer to the symbol table entry */
734 Sym = FindSym (CurTok.Ident);
736 /* Is the symbol known? */
739 /* We found the symbol - skip the name token */
742 /* The expression type is the symbol type */
743 lval->e_tptr = Sym->Type;
745 /* Check for illegal symbol types */
746 if ((Sym->Flags & SC_LABEL) == SC_LABEL) {
747 /* Cannot use labels in expressions */
748 Error (ERR_SYMBOL_KIND);
750 } else if (Sym->Flags & SC_TYPE) {
751 /* Cannot use type symbols */
752 Error (ERR_VAR_IDENT_EXPECTED);
753 /* Assume an int type to make lval valid */
754 lval->e_flags = E_MLOCAL | E_TLOFFS;
755 lval->e_tptr = type_int;
760 /* Check for legal symbol types */
761 if ((Sym->Flags & SC_ENUM) == SC_ENUM) {
762 lval->e_flags = E_MCONST;
763 lval->e_const = Sym->V.EnumVal;
765 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
767 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
768 lval->e_name = (unsigned long) Sym->Name;
770 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
772 lval->e_flags = E_MLOCAL | E_TLOFFS;
773 lval->e_const = Sym->V.Offs;
774 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
775 /* Static variable */
776 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
777 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
778 lval->e_name = (unsigned long) Sym->Name;
780 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
781 lval->e_name = Sym->V.Label;
784 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
785 /* Register variable, zero page based */
786 lval->e_flags = E_MGLOBAL | E_MCONST | E_TREGISTER;
787 lval->e_name = Sym->V.Offs;
790 /* Local static variable */
791 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
792 lval->e_name = Sym->V.Offs;
796 /* The symbol is referenced now */
797 Sym->Flags |= SC_REF;
798 if (IsTypeFunc (lval->e_tptr) || IsTypeArray (lval->e_tptr)) {
804 /* We did not find the symbol. Remember the name, then skip it */
805 strcpy (Ident, CurTok.Ident);
808 /* IDENT is either an auto-declared function or an undefined variable. */
809 if (curtok == TOK_LPAREN) {
810 /* Declare a function returning int. For that purpose, prepare a
811 * function signature for a function having an empty param list
814 Warning (WARN_FUNC_WITHOUT_PROTO);
815 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
816 lval->e_tptr = Sym->Type;
817 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
818 lval->e_name = (unsigned long) Sym->Name;
824 /* Undeclared Variable */
825 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
826 lval->e_flags = E_MLOCAL | E_TLOFFS;
827 lval->e_tptr = type_int;
829 Error (ERR_UNDEFINED_SYMBOL, Ident);
835 /* String literal? */
836 if (curtok == TOK_SCONST) {
837 lval->e_flags = E_MCONST | E_TLIT;
838 lval->e_const = curval;
839 lval->e_tptr = GetCharArrayType (strlen (GetLiteral (curval)));
845 if (curtok == TOK_ASM) {
847 lval->e_tptr = type_void;
848 lval->e_flags = E_MEXPR;
853 /* __AX__ and __EAX__ pseudo values? */
854 if (curtok == TOK_AX || curtok == TOK_EAX) {
855 lval->e_tptr = (curtok == TOK_AX)? type_uint : type_ulong;
856 lval->e_flags = E_MREG;
857 lval->e_test &= ~E_CC;
860 return 1; /* May be used as lvalue */
863 /* Illegal primary. */
864 Error (ERR_EXPR_EXPECTED);
865 lval->e_flags = E_MCONST;
866 lval->e_tptr = type_int;
872 static int arrayref (int k, struct expent* lval)
873 /* Handle an array reference */
887 /* Skip the bracket */
890 /* Get the type of left side */
891 tptr1 = lval->e_tptr;
893 /* We can apply a special treatment for arrays that have a const base
894 * address. This is true for most arrays and will produce a lot better
895 * code. Check if this is a const base address.
897 lflags = lval->e_flags & ~E_MCTYPE;
898 ConstBaseAddr = (lflags == E_MCONST) || /* Constant numeric address */
899 (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
900 lflags == E_MLOCAL; /* Local array */
902 /* If we have a constant base, we delay the address fetch */
903 Mark1 = GetCodePos ();
904 Mark2 = 0; /* Silence gcc */
905 if (!ConstBaseAddr) {
906 /* Get a pointer to the array into the primary */
907 exprhs (CF_NONE, k, lval);
909 /* Get the array pointer on stack. Do not push more than 16
910 * bit, even if this value is greater, since we cannot handle
911 * other than 16bit stuff when doing indexing.
913 Mark2 = GetCodePos ();
917 /* TOS now contains ptr to array elements. Get the subscript. */
919 if (l == 0 && lval2.e_flags == E_MCONST) {
921 /* The array subscript is a constant - remove value from stack */
922 if (!ConstBaseAddr) {
926 /* Get an array pointer into the primary */
927 exprhs (CF_NONE, k, lval);
930 if (IsClassPtr (tptr1)) {
932 /* Scale the subscript value according to element size */
933 lval2.e_const *= PSizeOf (tptr1);
935 /* Remove code for lhs load */
938 /* Handle constant base array on stack. Be sure NOT to
939 * handle pointers the same way, this won't work.
941 if (IsTypeArray (tptr1) &&
942 ((lval->e_flags & ~E_MCTYPE) == E_MCONST ||
943 (lval->e_flags & ~E_MCTYPE) == E_MLOCAL ||
944 (lval->e_flags & E_MGLOBAL) != 0 ||
945 (lval->e_flags == E_MEOFFS))) {
946 lval->e_const += lval2.e_const;
949 /* Pointer - load into primary and remember offset */
950 if ((lval->e_flags & E_MEXPR) == 0 || k != 0) {
951 exprhs (CF_NONE, k, lval);
953 lval->e_const = lval2.e_const;
954 lval->e_flags = E_MEOFFS;
957 /* Result is of element type */
958 lval->e_tptr = Indirect (tptr1);
963 } else if (IsClassPtr (tptr2 = lval2.e_tptr)) {
964 /* Subscript is pointer, get element type */
965 lval2.e_tptr = Indirect (tptr2);
967 /* Scale the rhs value in the primary register */
968 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
970 lval->e_tptr = lval2.e_tptr;
972 Error (ERR_CANNOT_SUBSCRIPT);
975 /* Add the subscript. Since arrays are indexed by integers,
976 * we will ignore the true type of the subscript here and
979 g_inc (CF_INT | CF_CONST, lval2.e_const);
983 /* Array subscript is not constant. Load it into the primary */
984 Mark2 = GetCodePos ();
985 exprhs (CF_NONE, l, &lval2);
987 tptr2 = lval2.e_tptr;
988 if (IsClassPtr (tptr1)) {
990 /* Get the element type */
991 lval->e_tptr = Indirect (tptr1);
993 /* Indexing is based on int's, so we will just use the integer
994 * portion of the index (which is in (e)ax, so there's no further
997 g_scale (CF_INT, SizeOf (lval->e_tptr));
999 } else if (IsClassPtr (tptr2)) {
1001 /* Get the element type */
1002 lval2.e_tptr = Indirect (tptr2);
1004 /* Get the int value on top. If we go here, we're sure,
1005 * both values are 16 bit (the first one was truncated
1006 * if necessary and the second one is a pointer).
1007 * Note: If ConstBaseAddr is true, we don't have a value on
1008 * stack, so to "swap" both, just push the subscript.
1010 if (ConstBaseAddr) {
1012 exprhs (CF_NONE, k, lval);
1019 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
1020 lval->e_tptr = lval2.e_tptr;
1022 Error (ERR_CANNOT_SUBSCRIPT);
1025 /* The offset is now in the primary register. It didn't have a
1026 * constant base address for the lhs, the lhs address is already
1027 * on stack, and we must add the offset. If the base address was
1028 * constant, we call special functions to add the address to the
1031 if (!ConstBaseAddr) {
1032 /* Add the subscript. Both values are int sized. */
1036 /* If the subscript has itself a constant address, it is often
1037 * a better idea to reverse again the order of the evaluation.
1038 * This will generate better code if the subscript is a byte
1039 * sized variable. But beware: This is only possible if the
1040 * subscript was not scaled, that is, if this was a byte array
1043 rflags = lval2.e_flags & ~E_MCTYPE;
1044 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1045 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1046 rflags == E_MLOCAL; /* Local array */
1048 if (ConstSubAddr && SizeOf (lval->e_tptr) == 1) {
1052 /* Reverse the order of evaluation */
1053 unsigned flags = (SizeOf (lval2.e_tptr) == 1)? CF_CHAR : CF_INT;
1056 /* Get a pointer to the array into the primary. We have changed
1057 * e_tptr above but we need the original type to load the
1058 * address, so restore it temporarily.
1060 SavedType = lval->e_tptr;
1061 lval->e_tptr = tptr1;
1062 exprhs (CF_NONE, k, lval);
1063 lval->e_tptr = SavedType;
1065 /* Add the variable */
1066 if (rflags == E_MLOCAL) {
1067 g_addlocal (flags, lval2.e_const);
1069 flags |= GlobalModeFlags (lval2.e_flags);
1070 g_addstatic (flags, lval2.e_name, lval2.e_const);
1073 if (lflags == E_MCONST) {
1074 /* Constant numeric address. Just add it */
1075 g_inc (CF_INT | CF_UNSIGNED, lval->e_const);
1076 } else if (lflags == E_MLOCAL) {
1077 /* Base address is a local variable address */
1078 if (IsTypeArray (tptr1)) {
1079 g_addaddr_local (CF_INT, lval->e_const);
1081 g_addlocal (CF_PTR, lval->e_const);
1084 /* Base address is a static variable address */
1085 unsigned flags = CF_INT;
1086 flags |= GlobalModeFlags (lval->e_flags);
1087 if (IsTypeArray (tptr1)) {
1088 g_addaddr_static (flags, lval->e_name, lval->e_const);
1090 g_addstatic (flags, lval->e_name, lval->e_const);
1096 lval->e_flags = E_MEXPR;
1099 return !IsTypeArray (lval->e_tptr);
1105 static int structref (int k, struct expent* lval)
1106 /* Process struct field after . or ->. */
1112 /* Skip the token and check for an identifier */
1114 if (curtok != TOK_IDENT) {
1115 Error (ERR_IDENT_EXPECTED);
1116 lval->e_tptr = type_int;
1120 /* Get the symbol table entry and check for a struct field */
1121 strcpy (Ident, CurTok.Ident);
1123 Field = FindStructField (lval->e_tptr, Ident);
1125 Error (ERR_STRUCT_FIELD_MISMATCH, Ident);
1126 lval->e_tptr = type_int;
1130 /* If we have constant input data, the result is also constant */
1131 flags = lval->e_flags & ~E_MCTYPE;
1132 if (flags == E_MCONST ||
1133 (k == 0 && (flags == E_MLOCAL ||
1134 (flags & E_MGLOBAL) != 0 ||
1135 lval->e_flags == E_MEOFFS))) {
1136 lval->e_const += Field->V.Offs;
1138 if ((flags & E_MEXPR) == 0 || k != 0) {
1139 exprhs (CF_NONE, k, lval);
1141 lval->e_const = Field->V.Offs;
1142 lval->e_flags = E_MEOFFS;
1144 lval->e_tptr = Field->Type;
1145 return !IsTypeArray (Field->Type);
1150 static int hie11 (struct expent *lval)
1151 /* Handle compound types (structs and arrays) */
1158 if (curtok < TOK_LBRACK || curtok > TOK_PTR_REF) {
1165 if (curtok == TOK_LBRACK) {
1167 /* Array reference */
1168 k = arrayref (k, lval);
1170 } else if (curtok == TOK_LPAREN) {
1172 /* Function call. Skip the opening parenthesis */
1174 tptr = lval->e_tptr;
1175 if (IsTypeFunc (tptr) || IsTypeFuncPtr (tptr)) {
1176 if (IsTypeFuncPtr (tptr)) {
1177 /* Pointer to function. Handle transparently */
1178 exprhs (CF_NONE, k, lval); /* Function pointer to A/X */
1179 ++lval->e_tptr; /* Skip T_PTR */
1180 lval->e_flags |= E_MEXPR;
1182 callfunction (lval);
1183 lval->e_flags = E_MEXPR;
1184 lval->e_tptr += DECODE_SIZE + 1; /* Set to result */
1186 Error (ERR_ILLEGAL_FUNC_CALL);
1190 } else if (curtok == TOK_DOT) {
1192 if (!IsClassStruct (lval->e_tptr)) {
1193 Error (ERR_STRUCT_EXPECTED);
1195 k = structref (0, lval);
1197 } else if (curtok == TOK_PTR_REF) {
1199 tptr = lval->e_tptr;
1200 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1201 Error (ERR_STRUCT_PTR_EXPECTED);
1203 k = structref (k, lval);
1213 static void store (struct expent* lval)
1214 /* Store primary reg into this reference */
1220 flags = TypeOf (lval->e_tptr);
1221 if (f & E_MGLOBAL) {
1222 flags |= GlobalModeFlags (f);
1229 g_putstatic (flags, lval->e_name, lval->e_const);
1231 } else if (f & E_MLOCAL) {
1232 g_putlocal (flags, lval->e_const);
1233 } else if (f == E_MEOFFS) {
1234 g_putind (flags, lval->e_const);
1235 } else if (f != E_MREG) {
1237 g_putind (flags, 0);
1239 /* Store into absolute address */
1240 g_putstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
1244 /* Assume that each one of the stores will invalidate CC */
1245 lval->e_test &= ~E_CC;
1250 static void pre_incdec (struct expent* lval, void (*inc) (unsigned, unsigned long))
1251 /* Handle --i and ++i */
1258 if ((k = hie10 (lval)) == 0) {
1259 Error (ERR_LVALUE_EXPECTED);
1263 /* Get the data type */
1264 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR | CF_CONST;
1266 /* Get the increment value in bytes */
1267 val = (lval->e_tptr [0] == T_PTR)? PSizeOf (lval->e_tptr) : 1;
1269 /* We're currently only able to handle some adressing modes */
1270 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
1271 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
1272 (lval->e_flags & E_MCONST) == 0 && /* Constant address? */
1273 (lval->e_flags & E_MEXPR) == 0) { /* Address in a/x? */
1275 /* Use generic code. Push the address if needed */
1278 /* Fetch the value */
1279 exprhs (CF_NONE, k, lval);
1281 /* Increment value in primary */
1284 /* Store the result back */
1289 /* Special code for some addressing modes - use the special += ops */
1290 if (lval->e_flags & E_MGLOBAL) {
1291 flags |= GlobalModeFlags (lval->e_flags);
1293 g_addeqstatic (flags, lval->e_name, lval->e_const, val);
1295 g_subeqstatic (flags, lval->e_name, lval->e_const, val);
1297 } else if (lval->e_flags & E_MLOCAL) {
1298 /* ref to localvar */
1300 g_addeqlocal (flags, lval->e_const, val);
1302 g_subeqlocal (flags, lval->e_const, val);
1304 } else if (lval->e_flags & E_MCONST) {
1305 /* ref to absolute address */
1306 flags |= CF_ABSOLUTE;
1308 g_addeqstatic (flags, lval->e_const, 0, val);
1310 g_subeqstatic (flags, lval->e_const, 0, val);
1312 } else if (lval->e_flags & E_MEXPR) {
1313 /* Address in a/x. */
1315 g_addeqind (flags, lval->e_const, val);
1317 g_subeqind (flags, lval->e_const, val);
1320 Internal ("Invalid addressing mode");
1325 /* Result is an expression */
1326 lval->e_flags = E_MEXPR;
1331 static void post_incdec (struct expent *lval, int k, void (*inc) (unsigned, unsigned long))
1332 /* Handle i-- and i++ */
1338 Error (ERR_LVALUE_EXPECTED);
1342 /* Get the data type */
1343 flags = TypeOf (lval->e_tptr);
1345 /* Push the address if needed */
1348 /* Fetch the value and save it (since it's the result of the expression) */
1349 exprhs (CF_NONE, 1, lval);
1350 g_save (flags | CF_FORCECHAR);
1352 /* If we have a pointer expression, increment by the size of the type */
1353 if (lval->e_tptr[0] == T_PTR) {
1354 inc (flags | CF_CONST | CF_FORCECHAR, SizeOf (lval->e_tptr + 1));
1356 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1359 /* Store the result back */
1362 /* Restore the original value */
1363 g_restore (flags | CF_FORCECHAR);
1364 lval->e_flags = E_MEXPR;
1369 static void unaryop (int tok, struct expent* lval)
1370 /* Handle unary -/+ and ~ */
1377 if (k == 0 && lval->e_flags & E_MCONST) {
1378 /* Value is constant */
1380 case TOK_MINUS: lval->e_const = -lval->e_const; break;
1381 case TOK_PLUS: break;
1382 case TOK_COMP: lval->e_const = ~lval->e_const; break;
1383 default: Internal ("Unexpected token: %d", tok);
1386 /* Value is not constant */
1387 exprhs (CF_NONE, k, lval);
1389 /* Get the type of the expression */
1390 flags = TypeOf (lval->e_tptr);
1392 /* Handle the operation */
1394 case TOK_MINUS: g_neg (flags); break;
1395 case TOK_PLUS: break;
1396 case TOK_COMP: g_com (flags); break;
1397 default: Internal ("Unexpected token: %d", tok);
1399 lval->e_flags = E_MEXPR;
1405 static int typecast (struct expent* lval)
1406 /* Handle an explicit cast */
1409 type Type[MAXTYPELEN];
1412 /* Skip the left paren */
1421 /* Read the expression we have to cast */
1424 /* Get the type of the expression and honor constant values */
1425 rflags = TypeOf (lval->e_tptr);
1426 if (lval->e_flags & E_MCONST) {
1430 /* Do the actual cast. Special handling for void casts */
1431 if (!IsTypeVoid (Type)) {
1432 /* Mark the lhs as const to avoid a manipulation of TOS */
1433 g_typecast (TypeOf (Type) | CF_CONST, rflags);
1436 /* Use the new type */
1437 lval->e_tptr = TypeDup (Type);
1445 static int hie10 (struct expent* lval)
1446 /* Handle ++, --, !, unary - etc. */
1454 pre_incdec (lval, g_inc);
1458 pre_incdec (lval, g_dec);
1464 unaryop (curtok, lval);
1469 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1470 /* Constant expression */
1471 lval->e_const = !lval->e_const;
1473 g_bneg (TypeOf (lval->e_tptr));
1474 lval->e_test |= E_CC; /* bneg will set cc */
1475 lval->e_flags = E_MEXPR; /* say it's an expr */
1477 return 0; /* expr not storable */
1481 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1482 /* Expression is not const, indirect value loaded into primary */
1483 lval->e_flags = E_MEXPR;
1484 lval->e_const = 0; /* Offset is zero now */
1487 if (IsClassPtr (t)) {
1488 lval->e_tptr = Indirect (t);
1490 Error (ERR_ILLEGAL_INDIRECT);
1498 /* Allow the & operator with an array */
1499 if (!IsTypeArray (lval->e_tptr)) {
1500 Error (ERR_ILLEGAL_ADDRESS);
1503 t = TypeAlloc (TypeLen (lval->e_tptr) + 2);
1505 TypeCpy (t + 1, lval->e_tptr);
1512 if (istypeexpr ()) {
1513 type Type[MAXTYPELEN];
1515 lval->e_const = SizeOf (ParseType (Type));
1518 /* Remember the output queue pointer */
1519 CodeMark Mark = GetCodePos ();
1521 lval->e_const = SizeOf (lval->e_tptr);
1522 /* Remove any generated code */
1525 lval->e_flags = E_MCONST | E_TCONST;
1526 lval->e_tptr = type_uint;
1527 lval->e_test &= ~E_CC;
1531 if (istypeexpr ()) {
1533 return typecast (lval);
1540 post_incdec (lval, k, g_inc);
1544 post_incdec (lval, k, g_dec);
1554 static int hie_internal (GenDesc** ops, /* List of generators */
1555 struct expent* lval, /* parent expr's lval */
1556 int (*hienext) (struct expent*),
1557 int* UsedGen) /* next higher level */
1558 /* Helper function */
1561 struct expent lval2;
1565 token_t tok; /* The operator token */
1566 unsigned ltype, type;
1567 int rconst; /* Operand is a constant */
1573 while ((Gen = FindGen (curtok, ops)) != 0) {
1575 /* Tell the caller that we handled it's ops */
1578 /* All operators that call this function expect an int on the lhs */
1579 if (!IsClassInt (lval->e_tptr)) {
1580 Error (ERR_INT_EXPR_EXPECTED);
1583 /* Remember the operator token, then skip it */
1587 /* Get the lhs on stack */
1588 Mark1 = GetCodePos ();
1589 ltype = TypeOf (lval->e_tptr);
1590 if (k == 0 && lval->e_flags == E_MCONST) {
1591 /* Constant value */
1592 Mark2 = GetCodePos ();
1593 g_push (ltype | CF_CONST, lval->e_const);
1595 /* Value not constant */
1596 exprhs (CF_NONE, k, lval);
1597 Mark2 = GetCodePos ();
1601 /* Get the right hand side */
1602 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1604 /* Check the type of the rhs */
1605 if (!IsClassInt (lval2.e_tptr)) {
1606 Error (ERR_INT_EXPR_EXPECTED);
1609 /* Check for const operands */
1610 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1612 /* Both operands are constant, remove the generated code */
1616 /* Evaluate the result */
1617 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1619 /* Get the type of the result */
1620 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1624 /* If the right hand side is constant, and the generator function
1625 * expects the lhs in the primary, remove the push of the primary
1628 unsigned rtype = TypeOf (lval2.e_tptr);
1631 /* Second value is constant - check for div */
1634 if (tok == TOK_DIV && lval2.e_const == 0) {
1635 Error (ERR_DIV_BY_ZERO);
1636 } else if (tok == TOK_MOD && lval2.e_const == 0) {
1637 Error (ERR_MOD_BY_ZERO);
1639 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1642 ltype |= CF_REG; /* Value is in register */
1646 /* Determine the type of the operation result. */
1647 type |= g_typeadjust (ltype, rtype);
1648 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1651 Gen->Func (type, lval2.e_const);
1652 lval->e_flags = E_MEXPR;
1655 /* We have a rvalue now */
1664 static int hie_compare (GenDesc** ops, /* List of generators */
1665 struct expent* lval, /* parent expr's lval */
1666 int (*hienext) (struct expent*))
1667 /* Helper function for the compare operators */
1670 struct expent lval2;
1674 token_t tok; /* The operator token */
1676 int rconst; /* Operand is a constant */
1681 while ((Gen = FindGen (curtok, ops)) != 0) {
1683 /* Remember the operator token, then skip it */
1687 /* Get the lhs on stack */
1688 Mark1 = GetCodePos ();
1689 ltype = TypeOf (lval->e_tptr);
1690 if (k == 0 && lval->e_flags == E_MCONST) {
1691 /* Constant value */
1692 Mark2 = GetCodePos ();
1693 g_push (ltype | CF_CONST, lval->e_const);
1695 /* Value not constant */
1696 exprhs (CF_NONE, k, lval);
1697 Mark2 = GetCodePos ();
1701 /* Get the right hand side */
1702 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1704 /* Make sure, the types are compatible */
1705 if (IsClassInt (lval->e_tptr)) {
1706 if (!IsClassInt (lval2.e_tptr) && !(IsClassPtr(lval2.e_tptr) && IsNullPtr(lval))) {
1707 Error (ERR_INCOMPATIBLE_TYPES);
1709 } else if (IsClassPtr (lval->e_tptr)) {
1710 if (IsClassPtr (lval2.e_tptr)) {
1711 /* Both pointers are allowed in comparison if they point to
1712 * the same type, or if one of them is a void pointer.
1714 type* left = Indirect (lval->e_tptr);
1715 type* right = Indirect (lval2.e_tptr);
1716 if (!EqualTypes (left, right) && *left != T_VOID && *right != T_VOID) {
1717 /* Incomatible pointers */
1718 Error (ERR_INCOMPATIBLE_TYPES);
1720 } else if (!IsNullPtr (&lval2)) {
1721 Error (ERR_INCOMPATIBLE_TYPES);
1725 /* Check for const operands */
1726 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1728 /* Both operands are constant, remove the generated code */
1732 /* Evaluate the result */
1733 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1737 /* If the right hand side is constant, and the generator function
1738 * expects the lhs in the primary, remove the push of the primary
1744 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1747 ltype |= CF_REG; /* Value is in register */
1751 /* Determine the type of the operation result. If the left
1752 * operand is of type char and the right is a constant, or
1753 * if both operands are of type char, we will encode the
1754 * operation as char operation. Otherwise the default
1755 * promotions are used.
1757 if (IsTypeChar (lval->e_tptr) && (IsTypeChar (lval2.e_tptr) || rconst)) {
1759 if (IsUnsigned (lval->e_tptr) || IsUnsigned (lval2.e_tptr)) {
1760 flags |= CF_UNSIGNED;
1763 flags |= CF_FORCECHAR;
1766 unsigned rtype = TypeOf (lval2.e_tptr) | (flags & CF_CONST);
1767 flags |= g_typeadjust (ltype, rtype);
1771 Gen->Func (flags, lval2.e_const);
1772 lval->e_flags = E_MEXPR;
1775 /* Result type is always int */
1776 lval->e_tptr = type_int;
1778 /* We have a rvalue now, condition codes are set */
1780 lval->e_test |= E_CC;
1788 static int hie9 (struct expent *lval)
1789 /* Process * and / operators. */
1791 static GenDesc* hie9_ops [] = {
1792 &GenMUL, &GenDIV, &GenMOD, 0
1796 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1801 static void parseadd (int k, struct expent* lval)
1802 /* Parse an expression with the binary plus operator. lval contains the
1803 * unprocessed left hand side of the expression and will contain the
1804 * result of the expression on return.
1807 struct expent lval2;
1808 unsigned flags; /* Operation flags */
1809 CodeMark Mark; /* Remember code position */
1810 type* lhst; /* Type of left hand side */
1811 type* rhst; /* Type of right hand side */
1814 /* Skip the PLUS token */
1817 /* Get the left hand side type, initialize operation flags */
1818 lhst = lval->e_tptr;
1821 /* Check for constness on both sides */
1822 if (k == 0 && lval->e_flags == E_MCONST) {
1824 /* The left hand side is a constant. Good. Get rhs */
1825 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1827 /* Right hand side is also constant. Get the rhs type */
1828 rhst = lval2.e_tptr;
1830 /* Both expressions are constants. Check for pointer arithmetic */
1831 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1832 /* Left is pointer, right is int, must scale rhs */
1833 lval->e_const = lval->e_const + lval2.e_const * PSizeOf (lhst);
1834 /* Result type is a pointer */
1835 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1836 /* Left is int, right is pointer, must scale lhs */
1837 lval->e_const = lval->e_const * PSizeOf (rhst) + lval2.e_const;
1838 /* Result type is a pointer */
1839 lval->e_tptr = lval2.e_tptr;
1840 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1841 /* Integer addition */
1842 lval->e_const += lval2.e_const;
1843 typeadjust (lval, &lval2, 1);
1846 Error (ERR_OP_NOT_ALLOWED);
1849 /* Result is constant, condition codes not set */
1850 lval->e_test = E_MCONST;
1854 /* lhs is constant, rhs is not. Get the rhs type. */
1855 rhst = lval2.e_tptr;
1857 /* Check for pointer arithmetic */
1858 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1859 /* Left is pointer, right is int, must scale rhs */
1860 g_scale (CF_INT, PSizeOf (lhst));
1861 /* Operate on pointers, result type is a pointer */
1863 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1864 /* Left is int, right is pointer, must scale lhs */
1865 lval->e_const *= PSizeOf (rhst);
1866 /* Operate on pointers, result type is a pointer */
1868 lval->e_tptr = lval2.e_tptr;
1869 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1870 /* Integer addition */
1871 flags = typeadjust (lval, &lval2, 1);
1874 Error (ERR_OP_NOT_ALLOWED);
1877 /* Generate code for the add */
1878 g_inc (flags | CF_CONST, lval->e_const);
1880 /* Result is in primary register */
1881 lval->e_flags = E_MEXPR;
1882 lval->e_test &= ~E_CC;
1888 /* Left hand side is not constant. Get the value onto the stack. */
1889 exprhs (CF_NONE, k, lval); /* --> primary register */
1890 Mark = GetCodePos ();
1891 g_push (TypeOf (lval->e_tptr), 0); /* --> stack */
1893 /* Evaluate the rhs */
1894 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1896 /* Right hand side is a constant. Get the rhs type */
1897 rhst = lval2.e_tptr;
1899 /* Remove pushed value from stack */
1901 pop (TypeOf (lval->e_tptr));
1903 /* Check for pointer arithmetic */
1904 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1905 /* Left is pointer, right is int, must scale rhs */
1906 lval2.e_const *= PSizeOf (lhst);
1907 /* Operate on pointers, result type is a pointer */
1909 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1910 /* Left is int, right is pointer, must scale lhs (ptr only) */
1911 g_scale (CF_INT | CF_CONST, PSizeOf (rhst));
1912 /* Operate on pointers, result type is a pointer */
1914 lval->e_tptr = lval2.e_tptr;
1915 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1916 /* Integer addition */
1917 flags = typeadjust (lval, &lval2, 1);
1920 Error (ERR_OP_NOT_ALLOWED);
1923 /* Generate code for the add */
1924 g_inc (flags | CF_CONST, lval2.e_const);
1926 /* Result is in primary register */
1927 lval->e_flags = E_MEXPR;
1928 lval->e_test &= ~E_CC;
1932 /* lhs and rhs are not constant. Get the rhs type. */
1933 rhst = lval2.e_tptr;
1935 /* Check for pointer arithmetic */
1936 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1937 /* Left is pointer, right is int, must scale rhs */
1938 g_scale (CF_INT, PSizeOf (lhst));
1939 /* Operate on pointers, result type is a pointer */
1941 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1942 /* Left is int, right is pointer, must scale lhs */
1943 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
1944 g_swap (CF_INT); /* Swap TOS and primary */
1945 g_scale (CF_INT, PSizeOf (rhst));
1946 /* Operate on pointers, result type is a pointer */
1948 lval->e_tptr = lval2.e_tptr;
1949 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1950 /* Integer addition */
1951 flags = typeadjust (lval, &lval2, 0);
1954 Error (ERR_OP_NOT_ALLOWED);
1957 /* Generate code for the add */
1960 /* Result is in primary register */
1961 lval->e_flags = E_MEXPR;
1962 lval->e_test &= ~E_CC;
1971 static void parsesub (int k, struct expent* lval)
1972 /* Parse an expression with the binary minus operator. lval contains the
1973 * unprocessed left hand side of the expression and will contain the
1974 * result of the expression on return.
1977 struct expent lval2;
1978 unsigned flags; /* Operation flags */
1979 type* lhst; /* Type of left hand side */
1980 type* rhst; /* Type of right hand side */
1981 CodeMark Mark1; /* Save position of output queue */
1982 CodeMark Mark2; /* Another position in the queue */
1983 int rscale; /* Scale factor for the result */
1986 /* Skip the MINUS token */
1989 /* Get the left hand side type, initialize operation flags */
1990 lhst = lval->e_tptr;
1992 rscale = 1; /* Scale by 1, that is, don't scale */
1994 /* Remember the output queue position, then bring the value onto the stack */
1995 Mark1 = GetCodePos ();
1996 exprhs (CF_NONE, k, lval); /* --> primary register */
1997 Mark2 = GetCodePos ();
1998 g_push (TypeOf (lhst), 0); /* --> stack */
2000 /* Parse the right hand side */
2001 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2003 /* The right hand side is constant. Get the rhs type. */
2004 rhst = lval2.e_tptr;
2006 /* Check left hand side */
2007 if (k == 0 && lval->e_flags & E_MCONST) {
2009 /* Both sides are constant, remove generated code */
2011 pop (TypeOf (lhst)); /* Clean up the stack */
2013 /* Check for pointer arithmetic */
2014 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2015 /* Left is pointer, right is int, must scale rhs */
2016 lval->e_const -= lval2.e_const * PSizeOf (lhst);
2017 /* Operate on pointers, result type is a pointer */
2018 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2019 /* Left is pointer, right is pointer, must scale result */
2020 if (TypeCmp (Indirect (lhst), Indirect (rhst)) != 0) {
2021 Error (ERR_INCOMPATIBLE_POINTERS);
2023 lval->e_const = (lval->e_const - lval2.e_const) / PSizeOf (lhst);
2025 /* Operate on pointers, result type is an integer */
2026 lval->e_tptr = type_int;
2027 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2028 /* Integer subtraction */
2029 typeadjust (lval, &lval2, 1);
2030 lval->e_const -= lval2.e_const;
2033 Error (ERR_OP_NOT_ALLOWED);
2036 /* Result is constant, condition codes not set */
2037 lval->e_flags = E_MCONST;
2038 lval->e_test &= ~E_CC;
2042 /* Left hand side is not constant, right hand side is.
2043 * Remove pushed value from stack.
2046 pop (TypeOf (lhst));
2048 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2049 /* Left is pointer, right is int, must scale rhs */
2050 lval2.e_const *= PSizeOf (lhst);
2051 /* Operate on pointers, result type is a pointer */
2053 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2054 /* Left is pointer, right is pointer, must scale result */
2055 if (TypeCmp (Indirect (lhst), Indirect (rhst)) != 0) {
2056 Error (ERR_INCOMPATIBLE_POINTERS);
2058 rscale = PSizeOf (lhst);
2060 /* Operate on pointers, result type is an integer */
2062 lval->e_tptr = type_int;
2063 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2064 /* Integer subtraction */
2065 flags = typeadjust (lval, &lval2, 1);
2068 Error (ERR_OP_NOT_ALLOWED);
2071 /* Do the subtraction */
2072 g_dec (flags | CF_CONST, lval2.e_const);
2074 /* If this was a pointer subtraction, we must scale the result */
2076 g_scale (flags, -rscale);
2079 /* Result is in primary register */
2080 lval->e_flags = E_MEXPR;
2081 lval->e_test &= ~E_CC;
2087 /* Right hand side is not constant. Get the rhs type. */
2088 rhst = lval2.e_tptr;
2090 /* Check for pointer arithmetic */
2091 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2092 /* Left is pointer, right is int, must scale rhs */
2093 g_scale (CF_INT, PSizeOf (lhst));
2094 /* Operate on pointers, result type is a pointer */
2096 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2097 /* Left is pointer, right is pointer, must scale result */
2098 if (TypeCmp (Indirect (lhst), Indirect (rhst)) != 0) {
2099 Error (ERR_INCOMPATIBLE_POINTERS);
2101 rscale = PSizeOf (lhst);
2103 /* Operate on pointers, result type is an integer */
2105 lval->e_tptr = type_int;
2106 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2107 /* Integer subtraction. If the left hand side descriptor says that
2108 * the lhs is const, we have to remove this mark, since this is no
2109 * longer true, lhs is on stack instead.
2111 if (lval->e_flags == E_MCONST) {
2112 lval->e_flags = E_MEXPR;
2114 /* Adjust operand types */
2115 flags = typeadjust (lval, &lval2, 0);
2118 Error (ERR_OP_NOT_ALLOWED);
2121 /* Generate code for the sub (the & is a hack here) */
2122 g_sub (flags & ~CF_CONST, 0);
2124 /* If this was a pointer subtraction, we must scale the result */
2126 g_scale (flags, -rscale);
2129 /* Result is in primary register */
2130 lval->e_flags = E_MEXPR;
2131 lval->e_test &= ~E_CC;
2137 static int hie8 (struct expent* lval)
2138 /* Process + and - binary operators. */
2140 int k = hie9 (lval);
2141 while (curtok == TOK_PLUS || curtok == TOK_MINUS) {
2143 if (curtok == TOK_PLUS) {
2156 static int hie7 (struct expent *lval)
2157 /* Parse << and >>. */
2159 static GenDesc* hie7_ops [] = {
2164 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2169 static int hie6 (struct expent *lval)
2170 /* process greater-than type comparators */
2172 static GenDesc* hie6_ops [] = {
2173 &GenLT, &GenLE, &GenGE, &GenGT, 0
2175 return hie_compare (hie6_ops, lval, hie7);
2180 static int hie5 (struct expent *lval)
2182 static GenDesc* hie5_ops[] = {
2185 return hie_compare (hie5_ops, lval, hie6);
2190 static int hie4 (struct expent* lval)
2191 /* Handle & (bitwise and) */
2193 static GenDesc* hie4_ops [] = {
2198 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2203 static int hie3 (struct expent *lval)
2204 /* Handle ^ (bitwise exclusive or) */
2206 static GenDesc* hie3_ops [] = {
2211 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2216 static int hie2 (struct expent *lval)
2217 /* Handle | (bitwise or) */
2219 static GenDesc* hie2_ops [] = {
2224 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2229 static int hieAnd (struct expent* lval, unsigned TrueLab, int* BoolOp)
2230 /* Process "exp && exp" */
2234 struct expent lval2;
2237 if (curtok == TOK_BOOL_AND) {
2239 /* Tell our caller that we're evaluating a boolean */
2242 /* Get a label that we will use for false expressions */
2245 /* If the expr hasn't set condition codes, set the force-test flag */
2246 if ((lval->e_test & E_CC) == 0) {
2247 lval->e_test |= E_FORCETEST;
2250 /* Load the value */
2251 exprhs (CF_FORCECHAR, k, lval);
2253 /* Generate the jump */
2254 g_falsejump (CF_NONE, lab);
2256 /* Parse more boolean and's */
2257 while (curtok == TOK_BOOL_AND) {
2264 if ((lval2.e_test & E_CC) == 0) {
2265 lval2.e_test |= E_FORCETEST;
2267 exprhs (CF_FORCECHAR, k, &lval2);
2269 /* Do short circuit evaluation */
2270 if (curtok == TOK_BOOL_AND) {
2271 g_falsejump (CF_NONE, lab);
2273 /* Last expression - will evaluate to true */
2274 g_truejump (CF_NONE, TrueLab);
2278 /* Define the false jump label here */
2279 g_defloclabel (lab);
2281 /* Define the label */
2282 lval->e_flags = E_MEXPR;
2283 lval->e_test |= E_CC; /* Condition codes are set */
2291 static int hieOr (struct expent *lval)
2292 /* Process "exp || exp". */
2295 struct expent lval2;
2296 int BoolOp = 0; /* Did we have a boolean op? */
2297 int AndOp; /* Did we have a && operation? */
2298 unsigned TrueLab; /* Jump to this label if true */
2302 TrueLab = GetLabel ();
2304 /* Call the next level parser */
2305 k = hieAnd (lval, TrueLab, &BoolOp);
2307 /* Any boolean or's? */
2308 if (curtok == TOK_BOOL_OR) {
2310 /* If the expr hasn't set condition codes, set the force-test flag */
2311 if ((lval->e_test & E_CC) == 0) {
2312 lval->e_test |= E_FORCETEST;
2315 /* Get first expr */
2316 exprhs (CF_FORCECHAR, k, lval);
2318 /* For each expression jump to TrueLab if true. Beware: If we
2319 * had && operators, the jump is already in place!
2322 g_truejump (CF_NONE, TrueLab);
2325 /* Remember that we had a boolean op */
2328 /* while there's more expr */
2329 while (curtok == TOK_BOOL_OR) {
2336 k = hieAnd (&lval2, TrueLab, &AndOp);
2337 if ((lval2.e_test & E_CC) == 0) {
2338 lval2.e_test |= E_FORCETEST;
2340 exprhs (CF_FORCECHAR, k, &lval2);
2342 /* If there is more to come, add shortcut boolean eval.
2343 * Beware: If we had && operators, the jump is already
2347 /* Seems this sometimes generates wrong code */
2348 if (curtok == TOK_BOOL_OR && !AndOp) {
2349 g_truejump (CF_NONE, TrueLab);
2352 g_truejump (CF_NONE, TrueLab);
2355 lval->e_flags = E_MEXPR;
2356 lval->e_test |= E_CC; /* Condition codes are set */
2360 /* If we really had boolean ops, generate the end sequence */
2362 DoneLab = GetLabel ();
2363 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2364 g_falsejump (CF_NONE, DoneLab);
2365 g_defloclabel (TrueLab);
2366 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2367 g_defloclabel (DoneLab);
2374 static int hieQuest (struct expent *lval)
2375 /* Parse "lvalue ? exp : exp" */
2380 struct expent lval2; /* Expression 2 */
2381 struct expent lval3; /* Expression 3 */
2382 type* type2; /* Type of expression 2 */
2383 type* type3; /* Type of expression 3 */
2384 type* rtype; /* Type of result */
2385 CodeMark Mark1; /* Save position in output code */
2386 CodeMark Mark2; /* Save position in output code */
2391 if (curtok == TOK_QUEST) {
2393 if ((lval->e_test & E_CC) == 0) {
2394 /* Condition codes not set, force a test */
2395 lval->e_test |= E_FORCETEST;
2397 exprhs (CF_NONE, k, lval);
2399 g_falsejump (CF_NONE, labf);
2401 /* Parse second and third expression */
2402 expression1 (&lval2);
2406 g_defloclabel (labf);
2407 expression1 (&lval3);
2409 /* Check if any conversions are needed, if so, do them.
2410 * Conversion rules for ?: expression are:
2411 * - if both expressions are int expressions, default promotion
2412 * rules for ints apply.
2413 * - if both expressions are pointers of the same type, the
2414 * result of the expression is of this type.
2415 * - if one of the expressions is a pointer and the other is
2416 * a zero constant, the resulting type is that of the pointer
2418 * - all other cases are flagged by an error.
2420 type2 = lval2.e_tptr;
2421 type3 = lval3.e_tptr;
2422 if (IsClassInt (type2) && IsClassInt (type3)) {
2424 /* Get common type */
2425 rtype = promoteint (type2, type3);
2427 /* Convert the third expression to this type if needed */
2428 g_typecast (TypeOf (rtype), TypeOf (type3));
2430 /* Setup a new label so that the expr3 code will jump around
2431 * the type cast code for expr2.
2433 labf = GetLabel (); /* Get new label */
2434 Mark1 = GetCodePos (); /* Remember current position */
2435 g_jump (labf); /* Jump around code */
2437 /* The jump for expr2 goes here */
2438 g_defloclabel (labt);
2440 /* Create the typecast code for expr2 */
2441 Mark2 = GetCodePos (); /* Remember position */
2442 g_typecast (TypeOf (rtype), TypeOf (type2));
2444 /* If the typecast did not produce code, remove the jump,
2445 * otherwise output the label.
2447 if (GetCodePos() == Mark2) {
2448 RemoveCode (Mark1); /* Remove code */
2450 /* We have typecast code, output label */
2451 g_defloclabel (labf);
2452 labt = 0; /* Mark other label as invalid */
2455 } else if (IsClassPtr (type2) && IsClassPtr (type3)) {
2456 /* Must point to same type */
2457 if (TypeCmp (Indirect (type2), Indirect (type3)) != 0) {
2458 Error (ERR_INCOMPATIBLE_TYPES);
2460 /* Result has the common type */
2461 rtype = lval2.e_tptr;
2462 } else if (IsClassPtr (type2) && IsNullPtr (&lval3)) {
2463 /* Result type is pointer, no cast needed */
2464 rtype = lval2.e_tptr;
2465 } else if (IsNullPtr (&lval2) && IsClassPtr (type3)) {
2466 /* Result type is pointer, no cast needed */
2467 rtype = lval3.e_tptr;
2469 Error (ERR_INCOMPATIBLE_TYPES);
2470 rtype = lval2.e_tptr; /* Doesn't matter here */
2473 /* If we don't have the label defined until now, do it */
2475 g_defloclabel (labt);
2478 /* Setup the target expression */
2479 lval->e_flags = E_MEXPR;
2480 lval->e_tptr = rtype;
2488 static void opeq (GenDesc* Gen, struct expent *lval, int k)
2489 /* Process "op=" operators. */
2491 struct expent lval2;
2498 Error (ERR_LVALUE_EXPECTED);
2502 /* Determine the type of the lhs */
2503 flags = TypeOf (lval->e_tptr);
2504 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2505 lval->e_tptr [0] == T_PTR;
2507 /* Get the lhs address on stack (if needed) */
2510 /* Fetch the lhs into the primary register if needed */
2511 exprhs (CF_NONE, k, lval);
2513 /* Bring the lhs on stack */
2514 Mark = GetCodePos ();
2517 /* Evaluate the rhs */
2518 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2519 /* The resulting value is a constant. If the generator has the NOPUSH
2520 * flag set, don't push the lhs.
2522 if (Gen->Flags & GEN_NOPUSH) {
2527 /* lhs is a pointer, scale rhs */
2528 lval2.e_const *= SizeOf (lval->e_tptr+1);
2531 /* If the lhs is character sized, the operation may be later done
2534 if (SizeOf (lval->e_tptr) == 1) {
2535 flags |= CF_FORCECHAR;
2538 /* Special handling for add and sub - some sort of a hack, but short code */
2539 if (Gen->Func == g_add) {
2540 g_inc (flags | CF_CONST, lval2.e_const);
2541 } else if (Gen->Func == g_sub) {
2542 g_dec (flags | CF_CONST, lval2.e_const);
2544 Gen->Func (flags | CF_CONST, lval2.e_const);
2547 /* rhs is not constant and already in the primary register */
2549 /* lhs is a pointer, scale rhs */
2550 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2553 /* If the lhs is character sized, the operation may be later done
2556 if (SizeOf (lval->e_tptr) == 1) {
2557 flags |= CF_FORCECHAR;
2560 /* Adjust the types of the operands if needed */
2561 Gen->Func (g_typeadjust (flags, TypeOf (lval2.e_tptr)), 0);
2564 lval->e_flags = E_MEXPR;
2569 static void addsubeq (GenDesc* Gen, struct expent *lval, int k)
2570 /* Process the += and -= operators */
2572 struct expent lval2;
2578 Error (ERR_LVALUE_EXPECTED);
2583 /* We're currently only able to handle some adressing modes */
2584 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
2585 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
2586 (lval->e_flags & E_MCONST) == 0) { /* Constant address? */
2587 /* Use generic routine */
2588 opeq (Gen, lval, k);
2592 /* Skip the operator */
2595 /* Check if we have a pointer expression and must scale rhs */
2596 MustScale = (lval->e_tptr [0] == T_PTR);
2598 /* Determine the code generator flags */
2599 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR;
2601 /* Evaluate the rhs */
2602 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2603 /* The resulting value is a constant. */
2605 /* lhs is a pointer, scale rhs */
2606 lval2.e_const *= SizeOf (lval->e_tptr+1);
2610 /* rhs is not constant and already in the primary register */
2612 /* lhs is a pointer, scale rhs */
2613 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2617 /* Adjust the rhs to the lhs */
2618 g_typeadjust (flags, TypeOf (lval2.e_tptr));
2620 /* Output apropriate code */
2621 if (lval->e_flags & E_MGLOBAL) {
2622 /* Static variable */
2623 flags |= GlobalModeFlags (lval->e_flags);
2624 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2625 g_addeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2627 g_subeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2629 } else if (lval->e_flags & E_MLOCAL) {
2630 /* ref to localvar */
2631 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2632 g_addeqlocal (flags, lval->e_const, lval2.e_const);
2634 g_subeqlocal (flags, lval->e_const, lval2.e_const);
2636 } else if (lval->e_flags & E_MCONST) {
2637 /* ref to absolute address */
2638 flags |= CF_ABSOLUTE;
2639 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2640 g_addeqstatic (flags, lval->e_const, 0, lval2.e_const);
2642 g_subeqstatic (flags, lval->e_const, 0, lval2.e_const);
2644 } else if (lval->e_flags & E_MEXPR) {
2645 /* Address in a/x. */
2646 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2647 g_addeqind (flags, lval->e_const, lval2.e_const);
2649 g_subeqind (flags, lval->e_const, lval2.e_const);
2652 Internal ("Invalid addressing mode");
2655 /* Expression is in the primary now */
2656 lval->e_flags = E_MEXPR;
2661 static void Assignment (struct expent* lval)
2662 /* Parse an assignment */
2665 struct expent lval2;
2667 type* ltype = lval->e_tptr;
2669 /* Check for assignment to const */
2670 if (IsConst (ltype)) {
2671 Error (ERR_CONST_ASSIGN);
2674 /* cc65 does not have full support for handling structs by value. Since
2675 * assigning structs is one of the more useful operations from this
2676 * familiy, allow it here.
2678 if (IsClassStruct (ltype)) {
2680 /* Bring the address of the lhs into the primary and push it */
2681 exprhs (0, 0, lval);
2682 g_push (CF_PTR | CF_UNSIGNED, 0);
2684 /* Get the expression on the right of the '=' into the primary */
2687 /* Get the address */
2688 exprhs (0, 0, &lval2);
2690 /* We need an lvalue */
2691 Error (ERR_LVALUE_EXPECTED);
2694 /* Push the address (or whatever is in ax in case of errors) */
2695 g_push (CF_PTR | CF_UNSIGNED, 0);
2697 /* Check for equality of the structs */
2698 if (!EqualTypes (ltype, lval2.e_tptr)) {
2699 Error (ERR_INCOMPATIBLE_TYPES);
2702 /* Load the size of the struct into the primary */
2703 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, SizeOf (ltype), 0);
2705 /* Call the memcpy function */
2706 g_call (CF_FIXARGC, "memcpy", 4);
2710 /* Get the address on stack if needed */
2713 /* No struct, setup flags for the load */
2714 flags = SizeOf (ltype) == 1? CF_FORCECHAR : CF_NONE;
2716 /* Get the expression on the right of the '=' into the primary */
2717 if (evalexpr (flags, hie1, &lval2) == 0) {
2718 /* Constant expression. Adjust the types */
2719 assignadjust (ltype, &lval2);
2720 /* Put the value into the primary register */
2721 lconst (flags, &lval2);
2723 /* Expression is not constant and already in the primary */
2724 assignadjust (ltype, &lval2);
2727 /* Generate a store instruction */
2732 /* Value is still in primary */
2733 lval->e_flags = E_MEXPR;
2738 int hie1 (struct expent* lval)
2739 /* Parse first level of expression hierarchy. */
2743 k = hieQuest (lval);
2753 Error (ERR_LVALUE_EXPECTED);
2759 case TOK_PLUS_ASSIGN:
2760 addsubeq (&GenPASGN, lval, k);
2763 case TOK_MINUS_ASSIGN:
2764 addsubeq (&GenSASGN, lval, k);
2767 case TOK_MUL_ASSIGN:
2768 opeq (&GenMASGN, lval, k);
2771 case TOK_DIV_ASSIGN:
2772 opeq (&GenDASGN, lval, k);
2775 case TOK_MOD_ASSIGN:
2776 opeq (&GenMOASGN, lval, k);
2779 case TOK_SHL_ASSIGN:
2780 opeq (&GenSLASGN, lval, k);
2783 case TOK_SHR_ASSIGN:
2784 opeq (&GenSRASGN, lval, k);
2787 case TOK_AND_ASSIGN:
2788 opeq (&GenAASGN, lval, k);
2791 case TOK_XOR_ASSIGN:
2792 opeq (&GenXOASGN, lval, k);
2796 opeq (&GenOASGN, lval, k);
2807 int hie0 (struct expent *lval)
2808 /* Parse comma operator. */
2813 while (curtok == TOK_COMMA) {
2822 int evalexpr (unsigned flags, int (*f) (struct expent*), struct expent* lval)
2823 /* Will evaluate an expression via the given function. If the result is a
2824 * constant, 0 is returned and the value is put in the lval struct. If the
2825 * result is not constant, exprhs is called to bring the value into the
2826 * primary register and 1 is returned.
2833 if (k == 0 && lval->e_flags == E_MCONST) {
2834 /* Constant expression */
2837 /* Not constant, load into the primary */
2838 exprhs (flags, k, lval);
2845 int expr (int (*func) (), struct expent *lval)
2846 /* Expression parser; func is either hie0 or hie1. */
2855 /* Do some checks if code generation is still constistent */
2856 if (savsp != oursp) {
2858 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
2860 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
2868 void expression1 (struct expent* lval)
2869 /* Evaluate an expression on level 1 (no comma operator) and put it into
2870 * the primary register
2873 memset (lval, 0, sizeof (*lval));
2874 exprhs (CF_NONE, expr (hie1, lval), lval);
2879 void expression (struct expent* lval)
2880 /* Evaluate an expression and put it into the primary register */
2882 memset (lval, 0, sizeof (*lval));
2883 exprhs (CF_NONE, expr (hie0, lval), lval);
2888 void constexpr (struct expent* lval)
2889 /* Get a constant value */
2891 memset (lval, 0, sizeof (*lval));
2892 if (expr (hie1, lval) != 0 || (lval->e_flags & E_MCONST) == 0) {
2893 Error (ERR_CONST_EXPR_EXPECTED);
2894 /* To avoid any compiler errors, make the expression a valid const */
2895 lval->e_flags = E_MCONST;
2896 lval->e_tptr = type_int;
2903 void intexpr (struct expent* lval)
2904 /* Get an integer expression */
2907 if (!IsClassInt (lval->e_tptr)) {
2908 Error (ERR_INT_EXPR_EXPECTED);
2909 /* To avoid any compiler errors, make the expression a valid int */
2910 lval->e_flags = E_MCONST;
2911 lval->e_tptr = type_int;
2918 void boolexpr (struct expent* lval)
2919 /* Get a boolean expression */
2921 /* Read an expression */
2924 /* If it's an integer, it's ok. If it's not an integer, but a pointer,
2925 * the pointer used in a boolean context is also ok (Ootherwise check if it's a pointer
2928 if (!IsClassInt (lval->e_tptr) && !IsClassPtr (lval->e_tptr)) {
2929 Error (ERR_INT_EXPR_EXPECTED);
2930 /* To avoid any compiler errors, make the expression a valid int */
2931 lval->e_flags = E_MCONST;
2932 lval->e_tptr = type_int;
2939 void test (unsigned label, int cond)
2940 /* Generate code to perform test and jump if false. */
2945 /* Eat the parenthesis */
2948 /* Prepare the expression, setup labels */
2949 memset (&lval, 0, sizeof (lval));
2950 lval.e_test = E_TEST;
2952 /* Generate code to eval the expr */
2953 k = expr (hie0, &lval);
2954 if (k == 0 && lval.e_flags == E_MCONST) {
2955 /* Constant rvalue */
2956 if (cond == 0 && lval.e_const == 0) {
2958 Warning (WARN_UNREACHABLE_CODE);
2959 } else if (cond && lval.e_const) {
2966 /* If the expr hasn't set condition codes, set the force-test flag */
2967 if ((lval.e_test & E_CC) == 0) {
2968 lval.e_test |= E_FORCETEST;
2971 /* Load the value into the primary register */
2972 exprhs (CF_FORCECHAR, k, &lval);
2974 /* Check for the closing brace */
2977 /* Generate the jump */
2979 g_truejump (CF_NONE, label);
2981 /* Special case (putting this here is a small hack - but hey, the
2982 * compiler itself is one big hack...): If a semicolon follows, we
2983 * don't have a statement and may omit the jump.
2985 if (curtok != TOK_SEMI) {
2986 g_falsejump (CF_NONE, label);