4 * Ullrich von Bassewitz, 21.06.1998
35 /*****************************************************************************/
37 /*****************************************************************************/
41 /* Generator attributes */
42 #define GEN_NOPUSH 0x01 /* Don't push lhs */
44 /* Map a generator function and its attributes to a token */
46 unsigned char Tok; /* Token to map to */
47 unsigned char Flags; /* Flags for generator function */
48 void (*Func) (unsigned, unsigned long); /* Generator func */
51 /* Descriptors for the operations */
52 static GenDesc GenMUL = { STAR, GEN_NOPUSH, g_mul };
53 static GenDesc GenDIV = { DIV, GEN_NOPUSH, g_div };
54 static GenDesc GenMOD = { MOD, GEN_NOPUSH, g_mod };
55 static GenDesc GenASL = { ASL, GEN_NOPUSH, g_asl };
56 static GenDesc GenASR = { ASR, GEN_NOPUSH, g_asr };
57 static GenDesc GenLT = { LT, GEN_NOPUSH, g_lt };
58 static GenDesc GenLE = { LE, GEN_NOPUSH, g_le };
59 static GenDesc GenGE = { GE, GEN_NOPUSH, g_ge };
60 static GenDesc GenGT = { GT, GEN_NOPUSH, g_gt };
61 static GenDesc GenEQ = { EQ, GEN_NOPUSH, g_eq };
62 static GenDesc GenNE = { NE, GEN_NOPUSH, g_ne };
63 static GenDesc GenAND = { AMP, GEN_NOPUSH, g_and };
64 static GenDesc GenXOR = { XOR, GEN_NOPUSH, g_xor };
65 static GenDesc GenOR = { BAR, GEN_NOPUSH, g_or };
66 static GenDesc GenPASGN = { PASGN, GEN_NOPUSH, g_add };
67 static GenDesc GenSASGN = { SASGN, GEN_NOPUSH, g_sub };
68 static GenDesc GenMASGN = { MASGN, GEN_NOPUSH, g_mul };
69 static GenDesc GenDASGN = { DASGN, GEN_NOPUSH, g_div };
70 static GenDesc GenMOASGN = { MOASGN, GEN_NOPUSH, g_mod };
71 static GenDesc GenSLASGN = { SLASGN, GEN_NOPUSH, g_asl };
72 static GenDesc GenSRASGN = { SRASGN, GEN_NOPUSH, g_asr };
73 static GenDesc GenAASGN = { AASGN, GEN_NOPUSH, g_and };
74 static GenDesc GenXOASGN = { XOASGN, GEN_NOPUSH, g_xor };
75 static GenDesc GenOASGN = { OASGN, GEN_NOPUSH, g_or };
79 /*****************************************************************************/
80 /* Function forwards */
81 /*****************************************************************************/
85 static int hie10 (struct expent* lval);
86 /* Handle ++, --, !, unary - etc. */
90 /*****************************************************************************/
91 /* Helper functions */
92 /*****************************************************************************/
96 static unsigned GlobalModeFlags (unsigned flags)
97 /* Return the addressing mode flags for the variable with the given flags */
100 if (flags == E_TGLAB) {
101 /* External linkage */
103 } else if (flags == E_TREGISTER) {
104 /* Register variable */
114 static int IsNullPtr (struct expent* lval)
115 /* Return true if this is the NULL pointer constant */
117 return (IsInt (lval->e_tptr) && /* Is it an int? */
118 lval->e_flags == E_MCONST && /* Is it constant? */
119 lval->e_const == 0); /* And is it's value zero? */
124 static type* promoteint (type* lhst, type* rhst)
125 /* In an expression with two ints, return the type of the result */
127 /* Rules for integer types:
128 * - If one of the values is a long, the result is long.
129 * - If one of the values is unsigned, the result is also unsigned.
130 * - Otherwise the result is an int.
132 if (IsLong (lhst) || IsLong (rhst)) {
133 if (IsUnsigned (lhst) || IsUnsigned (rhst)) {
139 if (IsUnsigned (lhst) || IsUnsigned (rhst)) {
149 static unsigned typeadjust (struct expent* lhs, struct expent* rhs, int NoPush)
150 /* Adjust the two values for a binary operation. lhs is expected on stack or
151 * to be constant, rhs is expected to be in the primary register or constant.
152 * The function will put the type of the result into lhs and return the
153 * code generator flags for the operation.
154 * If NoPush is given, it is assumed that the operation does not expect the lhs
155 * to be on stack, and that lhs is in a register instead.
156 * Beware: The function does only accept int types.
159 unsigned ltype, rtype;
162 /* Get the type strings */
163 type* lhst = lhs->e_tptr;
164 type* rhst = rhs->e_tptr;
166 /* Generate type adjustment code if needed */
167 ltype = TypeOf (lhst);
168 if (lhs->e_flags == E_MCONST) {
172 /* Value is in primary register*/
175 rtype = TypeOf (rhst);
176 if (rhs->e_flags == E_MCONST) {
179 flags = g_typeadjust (ltype, rtype);
181 /* Set the type of the result */
182 lhs->e_tptr = promoteint (lhst, rhst);
184 /* Return the code generator flags */
190 unsigned assignadjust (type* lhst, struct expent* rhs)
191 /* Adjust the type of the right hand expression so that it can be assigned to
192 * the type on the left hand side. This function is used for assignment and
193 * for converting parameters in a function call. It returns the code generator
194 * flags for the operation. The type string of the right hand side will be
195 * set to the type of the left hand side.
198 /* Get the type of the right hand side */
199 type* rhst = rhs->e_tptr;
201 /* After calling this function, rhs will have the type of the lhs */
204 /* First, do some type checking */
205 if (IsVoid (lhst) || IsVoid (rhst)) {
206 /* If one of the sides are of type void, output a more apropriate
209 Error (ERR_ILLEGAL_TYPE);
210 } else if (IsInt (lhst)) {
212 /* Pointer -> int conversion */
213 Warning (WARN_PTR_TO_INT_CONV);
214 } else if (!IsInt (rhst)) {
215 Error (ERR_INCOMPATIBLE_TYPES);
217 /* Adjust the int types. To avoid manipulation of TOS mark lhs
220 unsigned flags = TypeOf (rhst);
221 if (rhs->e_flags & E_MCONST) {
224 return g_typeadjust (TypeOf (lhst) | CF_CONST, flags);
226 } else if (IsPtr (lhst)) {
228 /* Pointer to pointer assignment is valid, if:
229 * - both point to the same types, or
230 * - the rhs pointer is a void pointer, or
231 * - the lhs pointer is a void pointer.
233 type* left = Indirect (lhst);
234 type* right = Indirect (rhst);
235 if (!EqualTypes (left, right) && *left != T_VOID && *right != T_VOID) {
236 Error (ERR_INCOMPATIBLE_POINTERS);
238 } else if (IsInt (rhst)) {
239 /* Int to pointer assignment is valid only for constant zero */
240 if ((rhs->e_flags & E_MCONST) == 0 || rhs->e_const != 0) {
241 Warning (WARN_INT_TO_PTR_CONV);
243 } else if (IsFuncPtr (lhst) && IsFunc(rhst)) {
244 /* Assignment of function to function pointer is allowed, provided
245 * that both functions have the same parameter list.
247 if (!EqualTypes(Indirect (lhst), rhst)) {
248 Error (ERR_INCOMPATIBLE_TYPES);
251 Error (ERR_INCOMPATIBLE_TYPES);
254 Error (ERR_INCOMPATIBLE_TYPES);
257 /* Return an int value in all cases where the operands are not both ints */
263 void DefineData (struct expent* lval)
264 /* Output a data definition for the given expression */
266 unsigned flags = lval->e_flags;
268 switch (flags & E_MCTYPE) {
272 g_defdata (TypeOf (lval->e_tptr) | CF_CONST, lval->e_const, 0);
276 /* Register variable. Taking the address is usually not
279 if (!AllowRegVarAddr) {
280 Error (ERR_CANNOT_TAKE_ADDR_OF_REG);
286 /* Local or global symbol */
287 g_defdata (GlobalModeFlags (flags), lval->e_name, lval->e_const);
291 /* a literal of some kind */
292 g_defdata (CF_STATIC, LiteralLabel, lval->e_const);
296 Internal ("Unknown constant type: %04X", flags);
302 static void lconst (unsigned flags, struct expent* lval)
303 /* Load primary reg with some constant value. */
305 switch (lval->e_flags & E_MCTYPE) {
308 g_leasp (lval->e_const);
312 /* Number constant */
313 g_getimmed (flags | TypeOf (lval->e_tptr) | CF_CONST, lval->e_const, 0);
317 /* Register variable. Taking the address is usually not
320 if (!AllowRegVarAddr) {
321 Error (ERR_CANNOT_TAKE_ADDR_OF_REG);
327 /* Local or global symbol, load address */
328 flags |= GlobalModeFlags (lval->e_flags);
330 g_getimmed (flags, lval->e_name, lval->e_const);
335 g_getimmed (CF_STATIC, LiteralLabel, lval->e_const);
339 Internal ("Unknown constant type: %04X", lval->e_flags);
345 static int kcalc (int tok, long val1, long val2)
346 /* Calculate an operation with left and right operand constant. */
350 return (val1 == val2);
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);
375 Error (ERR_DIV_BY_ZERO);
378 return (val1 / val2);
381 Error (ERR_MOD_BY_ZERO);
384 return (val1 % val2);
386 Internal ("kcalc: got token 0x%X\n", tok);
393 static GenDesc* FindGen (int Tok, GenDesc** Table)
396 while ((G = *Table) != 0) {
407 static int istypeexpr (void)
408 /* Return true if some sort of variable or type is waiting (helper for cast
409 * and sizeof() in hie10).
414 return curtok == LPAREN && (
415 (nxttok >= FIRSTTYPE && nxttok <= LASTTYPE) ||
418 (Entry = FindSym (NextTok.Ident)) != 0 &&
425 static void PushAddr (struct expent* lval)
426 /* If the expression contains an address that was somehow evaluated,
427 * push this address on the stack. This is a helper function for all
428 * sorts of implicit or explicit assignment functions where the lvalue
429 * must be saved if it's not constant, before evaluating the rhs.
432 /* Get the address on stack if needed */
433 if (lval->e_flags != E_MREG && (lval->e_flags & E_MEXPR)) {
434 /* Push the address (always a pointer) */
441 /*****************************************************************************/
443 /*****************************************************************************/
447 void exprhs (unsigned flags, int k, struct expent *lval)
448 /* Put the result of an expression into the primary register */
454 /* Dereferenced lvalue */
455 flags |= TypeOf (lval->e_tptr);
456 if (lval->e_test & E_FORCETEST) {
458 lval->e_test &= ~E_FORCETEST;
460 if (f & E_MGLOBAL) { /* ref to globalvar */
462 flags |= GlobalModeFlags (f);
463 g_getstatic (flags, lval->e_name, lval->e_const);
464 } else if (f & E_MLOCAL) {
465 /* ref to localvar */
466 g_getlocal (flags, lval->e_const);
467 } else if (f & E_MCONST) {
468 /* ref to absolute address */
469 g_getstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
470 } else if (f == E_MEOFFS) {
471 g_getind (flags, lval->e_const);
472 } else if (f != E_MREG) {
475 } else if (f == E_MEOFFS) {
476 /* reference not storable */
477 flags |= TypeOf (lval->e_tptr);
478 g_inc (flags | CF_CONST, lval->e_const);
479 } else if ((f & E_MEXPR) == 0) {
480 /* Constant of some sort, load it into the primary */
481 lconst (flags, lval);
483 if (lval->e_test & E_FORCETEST) { /* we testing this value? */
485 AddCodeHint ("forcetest");
486 flags |= TypeOf (lval->e_tptr);
487 g_test (flags); /* yes, force a test */
488 lval->e_test &= ~E_FORCETEST;
493 static void callfunction (struct expent* lval)
494 /* Perform a function call. Called from hie11, this routine will
495 * either call the named function, or if the supplied ptr is zero,
496 * will call the contents of P.
500 FuncDesc* Func; /* Function descriptor */
501 int Ellipsis; /* True if we have an open param list */
502 SymEntry* Param; /* Current formal parameter */
503 unsigned ParamCount; /* Actual parameter count */
504 unsigned ParamSize; /* Number of parameter bytes */
510 /* Get a pointer to the function descriptor from the type string */
511 Func = GetFuncDesc (lval->e_tptr);
513 /* Initialize vars to keep gcc silent */
517 /* Check if this is a function pointer. If so, save it. If not, check for
518 * special known library functions that may be inlined.
520 if (lval->e_flags & E_MEXPR) {
521 /* Function pointer is in primary register, save it */
522 Mark = GetCodePos ();
524 } else if (InlineStdFuncs && IsStdFunc ((const char*) lval->e_name)) {
525 /* Inline this function */
526 HandleStdFunc (lval);
530 /* Parse the actual parameter list */
534 while (curtok != RPAREN) {
536 /* Add a hint for the optimizer */
537 AddCodeHint ("param:start");
539 /* Count arguments */
542 /* Fetch the pointer to the next argument, check for too many args */
543 if (ParamCount <= Func->ParamCount) {
544 if (ParamCount == 1) {
546 Param = Func->SymTab->SymHead;
549 Param = Param->NextSym;
550 CHECK ((Param->Flags & SC_PARAM) != 0);
552 } else if (!Ellipsis) {
553 /* Too many arguments. Do we have an open param list? */
554 if ((Func->Flags & FD_ELLIPSIS) == 0) {
555 /* End of param list reached, no ellipsis */
556 Error (ERR_TOO_MANY_FUNC_ARGS);
558 /* Assume an ellipsis even in case of errors to avoid an error
559 * message for each other argument.
564 /* Do some optimization: If we have a constant value to push,
565 * use a special function that may optimize.
568 if (!Ellipsis && SizeOf (Param->Type) == 1) {
569 CFlags = CF_FORCECHAR;
572 if (evalexpr (CFlags, hie1, &lval2) == 0) {
573 /* A constant value */
577 /* If we don't have an argument spec, accept anything, otherwise
578 * convert the actual argument to the type needed.
581 /* Promote the argument if needed */
582 assignadjust (Param->Type, &lval2);
583 /* If we have a prototype, chars may be pushed as chars */
584 Flags |= CF_FORCECHAR;
587 /* Use the type of the argument for the push */
588 Flags |= TypeOf (lval2.e_tptr);
590 /* If this is a fastcall function, don't push the last argument */
591 if (ParamCount == Func->ParamCount && (Func->Flags & FD_FASTCALL) != 0) {
592 /* Just load the argument into the primary. This is only needed if
593 * we have a constant argument, otherwise the value is already in
596 if (Flags & CF_CONST) {
597 exprhs (CF_FORCECHAR, 0, &lval2);
600 /* Push the argument, count the argument size */
601 g_push (Flags, lval2.e_const);
602 ParamSize += sizeofarg (Flags);
605 /* Add an optimizer hint */
606 AddCodeHint ("param:end");
608 /* Check for end of argument list */
609 if (curtok != COMMA) {
615 /* We need the closing bracket here */
618 /* Check if we had enough parameters */
619 if (ParamCount < Func->ParamCount) {
620 Error (ERR_TOO_FEW_FUNC_ARGS);
624 if (lval->e_flags & E_MEXPR) {
625 /* Function called via pointer: Restore it and call function */
626 if (ParamSize != 0) {
629 /* We had no parameters - remove save code */
632 g_callind (TypeOf (lval->e_tptr), ParamSize);
634 g_call (TypeOf (lval->e_tptr), (char*) lval->e_name, ParamSize);
641 /* This function parses ASM statements. The syntax of the ASM directive
642 * looks like the one defined for C++ (C has no ASM directive), that is,
643 * a string literal in parenthesis.
649 /* Need left parenthesis */
653 if (curtok != SCONST) {
654 Error (ERR_STRLIT_EXPECTED);
656 /* Write the string directly into the output, followed by a newline */
657 AddCodeLine (GetLiteral (curval));
659 /* Reset the string pointer, effectivly clearing the string from the
660 * string table. Since we're working with one token lookahead, this
661 * will fail if the next token is also a string token, but that's a
662 * syntax error anyway, because we expect a right paren.
664 ResetLiteralOffs (curval);
667 /* Skip the string token */
670 /* Closing paren needed */
676 static int primary (struct expent* lval)
677 /* This is the lowest level of the expression parser. */
681 /* not a test at all, yet */
684 /* Character and integer constants. */
685 if (curtok == ICONST || curtok == CCONST) {
686 lval->e_flags = E_MCONST | E_TCONST;
687 lval->e_tptr = curtype;
688 lval->e_const = curval;
693 /* Process parenthesized subexpression by calling the whole parser
696 if (curtok == LPAREN) {
698 memset (lval, 0, sizeof (*lval)); /* Remove any attributes */
704 /* All others may only be used if the expression evaluation is not called
705 * recursively by the preprocessor.
708 /* Illegal expression in PP mode */
709 Error (ERR_CPP_EXPR_EXPECTED);
710 lval->e_flags = E_MCONST;
711 lval->e_tptr = type_int;
716 if (curtok == IDENT) {
721 /* Get a pointer to the symbol table entry */
722 Sym = FindSym (CurTok.Ident);
724 /* Is the symbol known? */
727 /* We found the symbol - skip the name token */
730 /* The expression type is the symbol type */
731 lval->e_tptr = Sym->Type;
733 /* Check for illegal symbol types */
734 if ((Sym->Flags & SC_LABEL) == SC_LABEL) {
735 /* Cannot use labels in expressions */
736 Error (ERR_SYMBOL_KIND);
738 } else if (Sym->Flags & SC_TYPE) {
739 /* Cannot use type symbols */
740 Error (ERR_VAR_IDENT_EXPECTED);
741 /* Assume an int type to make lval valid */
742 lval->e_flags = E_MLOCAL | E_TLOFFS;
743 lval->e_tptr = type_int;
748 /* Check for legal symbol types */
749 if ((Sym->Flags & SC_ENUM) == SC_ENUM) {
750 lval->e_flags = E_MCONST;
751 lval->e_const = Sym->V.EnumVal;
753 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
755 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
756 lval->e_name = (unsigned long) Sym->Name;
758 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
760 lval->e_flags = E_MLOCAL | E_TLOFFS;
761 lval->e_const = Sym->V.Offs;
762 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
763 /* Static variable */
764 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
765 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
766 lval->e_name = (unsigned long) Sym->Name;
768 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
769 lval->e_name = Sym->V.Label;
772 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
773 /* Register variable, zero page based */
774 lval->e_flags = E_MGLOBAL | E_MCONST | E_TREGISTER;
775 lval->e_name = Sym->V.Offs;
778 /* Local static variable */
779 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
780 lval->e_name = Sym->V.Offs;
784 /* The symbol is referenced now */
785 Sym->Flags |= SC_REF;
786 if (IsFunc (lval->e_tptr) || IsArray (lval->e_tptr)) {
792 /* We did not find the symbol. Remember the name, then skip it */
793 strcpy (Ident, CurTok.Ident);
796 /* IDENT is either an auto-declared function or an undefined variable. */
797 if (curtok == LPAREN) {
798 /* Declare a function returning int. For that purpose, prepare a
799 * function signature for a function having an empty param list
802 Warning (WARN_FUNC_WITHOUT_PROTO);
803 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF);
804 lval->e_tptr = Sym->Type;
805 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
806 lval->e_name = (unsigned long) Sym->Name;
812 /* Undeclared Variable */
813 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
814 lval->e_flags = E_MLOCAL | E_TLOFFS;
815 lval->e_tptr = type_int;
817 Error (ERR_UNDEFINED_SYMBOL, Ident);
823 /* String literal? */
824 if (curtok == SCONST) {
825 lval->e_flags = E_MCONST | E_TLIT;
826 lval->e_const = curval;
827 lval->e_tptr = GetCharArrayType (strlen (GetLiteral (curval)));
835 lval->e_tptr = type_void;
836 lval->e_flags = E_MEXPR;
841 /* __AX__ and __EAX__ pseudo values? */
842 if (curtok == AX || curtok == EAX) {
843 lval->e_tptr = (curtok == AX)? type_uint : type_ulong;
844 lval->e_flags = E_MREG;
845 lval->e_test &= ~E_CC;
848 return 1; /* May be used as lvalue */
851 /* Illegal primary. */
852 Error (ERR_EXPR_EXPECTED);
853 lval->e_flags = E_MCONST;
854 lval->e_tptr = type_int;
860 static int arrayref (int k, struct expent* lval)
861 /* Handle an array reference */
875 /* Skip the bracket */
878 /* Get the type of left side */
879 tptr1 = lval->e_tptr;
881 /* We can apply a special treatment for arrays that have a const base
882 * address. This is true for most arrays and will produce a lot better
883 * code. Check if this is a const base address.
885 lflags = lval->e_flags & ~E_MCTYPE;
886 ConstBaseAddr = (lflags == E_MCONST) || /* Constant numeric address */
887 (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
888 lflags == E_MLOCAL; /* Local array */
890 /* If we have a constant base, we delay the address fetch */
891 Mark1 = GetCodePos ();
892 Mark2 = 0; /* Silence gcc */
893 if (!ConstBaseAddr) {
894 /* Get a pointer to the array into the primary */
895 exprhs (CF_NONE, k, lval);
897 /* Get the array pointer on stack. Do not push more than 16
898 * bit, even if this value is greater, since we cannot handle
899 * other than 16bit stuff when doing indexing.
901 Mark2 = GetCodePos ();
905 /* TOS now contains ptr to array elements. Get the subscript. */
907 if (l == 0 && lval2.e_flags == E_MCONST) {
909 /* The array subscript is a constant - remove value from stack */
910 if (!ConstBaseAddr) {
914 /* Get an array pointer into the primary */
915 exprhs (CF_NONE, k, lval);
920 /* Scale the subscript value according to element size */
921 lval2.e_const *= PSizeOf (tptr1);
923 /* Remove code for lhs load */
926 /* Handle constant base array on stack. Be sure NOT to
927 * handle pointers the same way, this won't work.
929 if (IsArray (tptr1) &&
930 ((lval->e_flags & ~E_MCTYPE) == E_MCONST ||
931 (lval->e_flags & ~E_MCTYPE) == E_MLOCAL ||
932 (lval->e_flags & E_MGLOBAL) != 0 ||
933 (lval->e_flags == E_MEOFFS))) {
934 lval->e_const += lval2.e_const;
937 /* Pointer - load into primary and remember offset */
938 if ((lval->e_flags & E_MEXPR) == 0 || k != 0) {
939 exprhs (CF_NONE, k, lval);
941 lval->e_const = lval2.e_const;
942 lval->e_flags = E_MEOFFS;
945 /* Result is of element type */
946 lval->e_tptr = Indirect (tptr1);
951 } else if ((tptr2 = lval2.e_tptr) [0] & T_POINTER) {
952 /* Subscript is pointer, get element type */
953 lval2.e_tptr = Indirect (tptr2);
955 /* Scale the rhs value in the primary register */
956 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
958 lval->e_tptr = lval2.e_tptr;
960 Error (ERR_CANNOT_SUBSCRIPT);
963 /* Add the subscript. Since arrays are indexed by integers,
964 * we will ignore the true type of the subscript here and
967 g_inc (CF_INT | CF_CONST, lval2.e_const);
971 /* Array subscript is not constant. Load it into the primary */
972 Mark2 = GetCodePos ();
973 exprhs (CF_NONE, l, &lval2);
975 tptr2 = lval2.e_tptr;
978 /* Get the element type */
979 lval->e_tptr = Indirect (tptr1);
981 /* Indexing is based on int's, so we will just use the integer
982 * portion of the index (which is in (e)ax, so there's no further
985 g_scale (CF_INT, SizeOf (lval->e_tptr));
987 } else if (IsPtr (tptr2)) {
989 /* Get the element type */
990 lval2.e_tptr = Indirect (tptr2);
992 /* Get the int value on top. If we go here, we're sure,
993 * both values are 16 bit (the first one was truncated
994 * if necessary and the second one is a pointer).
995 * Note: If ConstBaseAddr is true, we don't have a value on
996 * stack, so to "swap" both, just push the subscript.
1000 exprhs (CF_NONE, k, lval);
1007 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
1008 lval->e_tptr = lval2.e_tptr;
1010 Error (ERR_CANNOT_SUBSCRIPT);
1013 /* The offset is now in the primary register. It didn't have a
1014 * constant base address for the lhs, the lhs address is already
1015 * on stack, and we must add the offset. If the base address was
1016 * constant, we call special functions to add the address to the
1019 if (!ConstBaseAddr) {
1020 /* Add the subscript. Both values are int sized. */
1024 /* If the subscript has itself a constant address, it is often
1025 * a better idea to reverse again the order of the evaluation.
1026 * This will generate better code if the subscript is a byte
1027 * sized variable. But beware: This is only possible if the
1028 * subscript was not scaled, that is, if this was a byte array
1031 rflags = lval2.e_flags & ~E_MCTYPE;
1032 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1033 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1034 rflags == E_MLOCAL; /* Local array */
1036 if (ConstSubAddr && SizeOf (lval->e_tptr) == 1) {
1040 /* Reverse the order of evaluation */
1041 unsigned flags = (SizeOf (lval2.e_tptr) == 1)? CF_CHAR : CF_INT;
1044 /* Get a pointer to the array into the primary. We have changed
1045 * e_tptr above but we need the original type to load the
1046 * address, so restore it temporarily.
1048 SavedType = lval->e_tptr;
1049 lval->e_tptr = tptr1;
1050 exprhs (CF_NONE, k, lval);
1051 lval->e_tptr = SavedType;
1053 /* Add the variable */
1054 if (rflags == E_MLOCAL) {
1055 g_addlocal (flags, lval2.e_const);
1057 flags |= GlobalModeFlags (lval2.e_flags);
1058 g_addstatic (flags, lval2.e_name, lval2.e_const);
1061 if (lflags == E_MCONST) {
1062 /* Constant numeric address. Just add it */
1063 g_inc (CF_INT | CF_UNSIGNED, lval->e_const);
1064 } else if (lflags == E_MLOCAL) {
1065 /* Base address is a local variable address */
1066 if (IsArray (tptr1)) {
1067 g_addaddr_local (CF_INT, lval->e_const);
1069 g_addlocal (CF_PTR, lval->e_const);
1072 /* Base address is a static variable address */
1073 unsigned flags = CF_INT;
1074 flags |= GlobalModeFlags (lval->e_flags);
1075 if (IsArray (tptr1)) {
1076 g_addaddr_static (flags, lval->e_name, lval->e_const);
1078 g_addstatic (flags, lval->e_name, lval->e_const);
1084 lval->e_flags = E_MEXPR;
1087 return !IsArray (lval->e_tptr);
1093 static int structref (int k, struct expent* lval)
1094 /* Process struct field after . or ->. */
1100 /* Skip the token and check for an identifier */
1102 if (curtok != IDENT) {
1103 Error (ERR_IDENT_EXPECTED);
1104 lval->e_tptr = type_int;
1108 /* Get the symbol table entry and check for a struct field */
1109 strcpy (Ident, CurTok.Ident);
1111 Field = FindStructField (lval->e_tptr, Ident);
1113 Error (ERR_STRUCT_FIELD_MISMATCH, Ident);
1114 lval->e_tptr = type_int;
1118 /* If we have constant input data, the result is also constant */
1119 flags = lval->e_flags & ~E_MCTYPE;
1120 if (flags == E_MCONST ||
1121 (k == 0 && (flags == E_MLOCAL ||
1122 (flags & E_MGLOBAL) != 0 ||
1123 lval->e_flags == E_MEOFFS))) {
1124 lval->e_const += Field->V.Offs;
1126 if ((flags & E_MEXPR) == 0 || k != 0) {
1127 exprhs (CF_NONE, k, lval);
1129 lval->e_const = Field->V.Offs;
1130 lval->e_flags = E_MEOFFS;
1132 lval->e_tptr = Field->Type;
1133 return !IsArray (Field->Type);
1138 static int hie11 (struct expent *lval)
1139 /* Handle compound types (structs and arrays) */
1146 if (curtok < LBRACK || curtok > PREF) {
1153 if (curtok == LBRACK) {
1155 /* Array reference */
1156 k = arrayref (k, lval);
1158 } else if (curtok == LPAREN) {
1160 /* Function call. Skip the opening parenthesis */
1162 tptr = lval->e_tptr;
1163 if (IsFunc (tptr) || IsFuncPtr (tptr)) {
1164 if (IsFuncPtr (tptr)) {
1165 /* Pointer to function. Handle transparently */
1166 exprhs (CF_NONE, k, lval); /* Function pointer to A/X */
1167 ++lval->e_tptr; /* Skip T_PTR */
1168 lval->e_flags |= E_MEXPR;
1170 callfunction (lval);
1171 lval->e_flags = E_MEXPR;
1172 lval->e_tptr += DECODE_SIZE + 1; /* Set to result */
1174 Error (ERR_ILLEGAL_FUNC_CALL);
1178 } else if (curtok == DOT) {
1180 if (!IsStruct (lval->e_tptr)) {
1181 Error (ERR_STRUCT_EXPECTED);
1183 k = structref (0, lval);
1185 } else if (curtok == PREF) {
1187 tptr = lval->e_tptr;
1188 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1189 Error (ERR_STRUCT_PTR_EXPECTED);
1191 k = structref (k, lval);
1201 static void store (struct expent* lval)
1202 /* Store primary reg into this reference */
1208 flags = TypeOf (lval->e_tptr);
1209 if (f & E_MGLOBAL) {
1210 flags |= GlobalModeFlags (f);
1217 g_putstatic (flags, lval->e_name, lval->e_const);
1219 } else if (f & E_MLOCAL) {
1220 g_putlocal (flags, lval->e_const);
1221 } else if (f == E_MEOFFS) {
1222 g_putind (flags, lval->e_const);
1223 } else if (f != E_MREG) {
1225 g_putind (flags, 0);
1227 /* Store into absolute address */
1228 g_putstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
1232 /* Assume that each one of the stores will invalidate CC */
1233 lval->e_test &= ~E_CC;
1238 static void pre_incdec (struct expent* lval, void (*inc) (unsigned, unsigned long))
1239 /* Handle --i and ++i */
1246 if ((k = hie10 (lval)) == 0) {
1247 Error (ERR_LVALUE_EXPECTED);
1251 /* Get the data type */
1252 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR | CF_CONST;
1254 /* Get the increment value in bytes */
1255 val = (lval->e_tptr [0] == T_PTR)? PSizeOf (lval->e_tptr) : 1;
1257 /* We're currently only able to handle some adressing modes */
1258 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
1259 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
1260 (lval->e_flags & E_MCONST) == 0 && /* Constant address? */
1261 (lval->e_flags & E_MEXPR) == 0) { /* Address in a/x? */
1263 /* Use generic code. Push the address if needed */
1266 /* Fetch the value */
1267 exprhs (CF_NONE, k, lval);
1269 /* Increment value in primary */
1272 /* Store the result back */
1277 /* Special code for some addressing modes - use the special += ops */
1278 if (lval->e_flags & E_MGLOBAL) {
1279 flags |= GlobalModeFlags (lval->e_flags);
1281 g_addeqstatic (flags, lval->e_name, lval->e_const, val);
1283 g_subeqstatic (flags, lval->e_name, lval->e_const, val);
1285 } else if (lval->e_flags & E_MLOCAL) {
1286 /* ref to localvar */
1288 g_addeqlocal (flags, lval->e_const, val);
1290 g_subeqlocal (flags, lval->e_const, val);
1292 } else if (lval->e_flags & E_MCONST) {
1293 /* ref to absolute address */
1294 flags |= CF_ABSOLUTE;
1296 g_addeqstatic (flags, lval->e_const, 0, val);
1298 g_subeqstatic (flags, lval->e_const, 0, val);
1300 } else if (lval->e_flags & E_MEXPR) {
1301 /* Address in a/x. */
1303 g_addeqind (flags, lval->e_const, val);
1305 g_subeqind (flags, lval->e_const, val);
1308 Internal ("Invalid addressing mode");
1313 /* Result is an expression */
1314 lval->e_flags = E_MEXPR;
1319 static void post_incdec (struct expent *lval, int k, void (*inc) (unsigned, unsigned long))
1320 /* Handle i-- and i++ */
1326 Error (ERR_LVALUE_EXPECTED);
1330 /* Get the data type */
1331 flags = TypeOf (lval->e_tptr);
1333 /* Push the address if needed */
1336 /* Fetch the value and save it (since it's the result of the expression) */
1337 exprhs (CF_NONE, 1, lval);
1338 g_save (flags | CF_FORCECHAR);
1340 /* If we have a pointer expression, increment by the size of the type */
1341 if (lval->e_tptr[0] == T_PTR) {
1342 inc (flags | CF_CONST | CF_FORCECHAR, SizeOf (lval->e_tptr + 1));
1344 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1347 /* Store the result back */
1350 /* Restore the original value */
1351 g_restore (flags | CF_FORCECHAR);
1352 lval->e_flags = E_MEXPR;
1357 static void unaryop (int tok, struct expent* lval)
1358 /* Handle unary -/+ and ~ */
1365 if (k == 0 && lval->e_flags & E_MCONST) {
1366 /* Value is constant */
1368 case MINUS: lval->e_const = -lval->e_const; break;
1370 case COMP: lval->e_const = ~lval->e_const; break;
1371 default: Internal ("Unexpected token: %d", tok);
1374 /* Value is not constant */
1375 exprhs (CF_NONE, k, lval);
1377 /* Get the type of the expression */
1378 flags = TypeOf (lval->e_tptr);
1380 /* Handle the operation */
1382 case MINUS: g_neg (flags); break;
1384 case COMP: g_com (flags); break;
1385 default: Internal ("Unexpected token: %d", tok);
1387 lval->e_flags = E_MEXPR;
1393 static int typecast (struct expent* lval)
1394 /* Handle an explicit cast */
1397 type Type[MAXTYPELEN];
1400 /* Skip the left paren */
1409 /* Read the expression we have to cast */
1412 /* Get the type of the expression and honor constant values */
1413 rflags = TypeOf (lval->e_tptr);
1414 if (lval->e_flags & E_MCONST) {
1418 /* Do the actual cast. Special handling for void casts */
1419 if (!IsVoid (Type)) {
1420 /* Mark the lhs as const to avoid a manipulation of TOS */
1421 g_typecast (TypeOf (Type) | CF_CONST, rflags);
1424 /* Use the new type */
1425 lval->e_tptr = TypeDup (Type);
1433 static int hie10 (struct expent* lval)
1434 /* Handle ++, --, !, unary - etc. */
1442 pre_incdec (lval, g_inc);
1446 pre_incdec (lval, g_dec);
1452 unaryop (curtok, lval);
1457 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1458 /* Constant expression */
1459 lval->e_const = !lval->e_const;
1461 g_bneg (TypeOf (lval->e_tptr));
1462 lval->e_test |= E_CC; /* bneg will set cc */
1463 lval->e_flags = E_MEXPR; /* say it's an expr */
1465 return 0; /* expr not storable */
1469 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1470 /* Expression is not const, indirect value loaded into primary */
1471 lval->e_flags = E_MEXPR;
1472 lval->e_const = 0; /* Offset is zero now */
1476 lval->e_tptr = Indirect (t);
1478 Error (ERR_ILLEGAL_INDIRECT);
1486 /* Allow the & operator with an array */
1487 if (!IsArray (lval->e_tptr)) {
1488 Error (ERR_ILLEGAL_ADDRESS);
1491 t = TypeAlloc (TypeLen (lval->e_tptr) + 2);
1493 TypeCpy (t + 1, lval->e_tptr);
1500 if (istypeexpr ()) {
1501 type Type[MAXTYPELEN];
1503 lval->e_const = SizeOf (ParseType (Type));
1506 /* Remember the output queue pointer */
1507 CodeMark Mark = GetCodePos ();
1509 lval->e_const = SizeOf (lval->e_tptr);
1510 /* Remove any generated code */
1513 lval->e_flags = E_MCONST | E_TCONST;
1514 lval->e_tptr = type_uint;
1515 lval->e_test &= ~E_CC;
1519 if (istypeexpr ()) {
1521 return typecast (lval);
1528 post_incdec (lval, k, g_inc);
1532 post_incdec (lval, k, g_dec);
1542 static int hie_internal (GenDesc** ops, /* List of generators */
1543 struct expent* lval, /* parent expr's lval */
1544 int (*hienext) (struct expent*),
1545 int* UsedGen) /* next higher level */
1546 /* Helper function */
1549 struct expent lval2;
1553 int tok; /* The operator token */
1554 unsigned ltype, type;
1555 int rconst; /* Operand is a constant */
1561 while ((Gen = FindGen (curtok, ops)) != 0) {
1563 /* Tell the caller that we handled it's ops */
1566 /* All operators that call this function expect an int on the lhs */
1567 if (!IsInt (lval->e_tptr)) {
1568 Error (ERR_INT_EXPR_EXPECTED);
1571 /* Remember the operator token, then skip it */
1575 /* Get the lhs on stack */
1576 Mark1 = GetCodePos ();
1577 ltype = TypeOf (lval->e_tptr);
1578 if (k == 0 && lval->e_flags == E_MCONST) {
1579 /* Constant value */
1580 Mark2 = GetCodePos ();
1581 g_push (ltype | CF_CONST, lval->e_const);
1583 /* Value not constant */
1584 exprhs (CF_NONE, k, lval);
1585 Mark2 = GetCodePos ();
1589 /* Get the right hand side */
1590 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1592 /* Check the type of the rhs */
1593 if (!IsInt (lval2.e_tptr)) {
1594 Error (ERR_INT_EXPR_EXPECTED);
1597 /* Check for const operands */
1598 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1600 /* Both operands are constant, remove the generated code */
1604 /* Evaluate the result */
1605 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1607 /* Get the type of the result */
1608 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1612 /* If the right hand side is constant, and the generator function
1613 * expects the lhs in the primary, remove the push of the primary
1616 unsigned rtype = TypeOf (lval2.e_tptr);
1619 /* Second value is constant - check for div */
1622 if (tok == DIV && lval2.e_const == 0) {
1623 Error (ERR_DIV_BY_ZERO);
1624 } else if (tok == MOD && lval2.e_const == 0) {
1625 Error (ERR_MOD_BY_ZERO);
1627 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1630 ltype |= CF_REG; /* Value is in register */
1634 /* Determine the type of the operation result. */
1635 type |= g_typeadjust (ltype, rtype);
1636 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1639 Gen->Func (type, lval2.e_const);
1640 lval->e_flags = E_MEXPR;
1643 /* We have a rvalue now */
1652 static int hie_compare (GenDesc** ops, /* List of generators */
1653 struct expent* lval, /* parent expr's lval */
1654 int (*hienext) (struct expent*))
1655 /* Helper function for the compare operators */
1658 struct expent lval2;
1662 int tok; /* The operator token */
1664 int rconst; /* Operand is a constant */
1669 while ((Gen = FindGen (curtok, ops)) != 0) {
1671 /* Remember the operator token, then skip it */
1675 /* Get the lhs on stack */
1676 Mark1 = GetCodePos ();
1677 ltype = TypeOf (lval->e_tptr);
1678 if (k == 0 && lval->e_flags == E_MCONST) {
1679 /* Constant value */
1680 Mark2 = GetCodePos ();
1681 g_push (ltype | CF_CONST, lval->e_const);
1683 /* Value not constant */
1684 exprhs (CF_NONE, k, lval);
1685 Mark2 = GetCodePos ();
1689 /* Get the right hand side */
1690 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1692 /* Make sure, the types are compatible */
1693 if (IsInt (lval->e_tptr)) {
1694 if (!IsInt (lval2.e_tptr) && !(IsPtr(lval2.e_tptr) && IsNullPtr(lval))) {
1695 Error (ERR_INCOMPATIBLE_TYPES);
1697 } else if (IsPtr (lval->e_tptr)) {
1698 if (IsPtr (lval2.e_tptr)) {
1699 /* Both pointers are allowed in comparison if they point to
1700 * the same type, or if one of them is a void pointer.
1702 type* left = Indirect (lval->e_tptr);
1703 type* right = Indirect (lval2.e_tptr);
1704 if (!EqualTypes (left, right) && *left != T_VOID && *right != T_VOID) {
1705 /* Incomatible pointers */
1706 Error (ERR_INCOMPATIBLE_TYPES);
1708 } else if (!IsNullPtr (&lval2)) {
1709 Error (ERR_INCOMPATIBLE_TYPES);
1713 /* Check for const operands */
1714 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1716 /* Both operands are constant, remove the generated code */
1720 /* Evaluate the result */
1721 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1725 /* If the right hand side is constant, and the generator function
1726 * expects the lhs in the primary, remove the push of the primary
1732 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1735 ltype |= CF_REG; /* Value is in register */
1739 /* Determine the type of the operation result. If the left
1740 * operand is of type char and the right is a constant, or
1741 * if both operands are of type char, we will encode the
1742 * operation as char operation. Otherwise the default
1743 * promotions are used.
1745 if (IsChar (lval->e_tptr) && (IsChar (lval2.e_tptr) || rconst)) {
1747 if (IsUnsigned (lval->e_tptr) || IsUnsigned (lval2.e_tptr)) {
1748 flags |= CF_UNSIGNED;
1751 flags |= CF_FORCECHAR;
1754 unsigned rtype = TypeOf (lval2.e_tptr) | (flags & CF_CONST);
1755 flags |= g_typeadjust (ltype, rtype);
1759 Gen->Func (flags, lval2.e_const);
1760 lval->e_flags = E_MEXPR;
1763 /* Result type is always int */
1764 lval->e_tptr = type_int;
1766 /* We have a rvalue now, condition codes are set */
1768 lval->e_test |= E_CC;
1776 static int hie9 (struct expent *lval)
1777 /* Process * and / operators. */
1779 static GenDesc* hie9_ops [] = {
1780 &GenMUL, &GenDIV, &GenMOD, 0
1784 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1789 static void parseadd (int k, struct expent* lval)
1790 /* Parse an expression with the binary plus operator. lval contains the
1791 * unprocessed left hand side of the expression and will contain the
1792 * result of the expression on return.
1795 struct expent lval2;
1796 unsigned flags; /* Operation flags */
1797 CodeMark Mark; /* Remember code position */
1798 type* lhst; /* Type of left hand side */
1799 type* rhst; /* Type of right hand side */
1802 /* Skip the PLUS token */
1805 /* Get the left hand side type, initialize operation flags */
1806 lhst = lval->e_tptr;
1809 /* Check for constness on both sides */
1810 if (k == 0 && lval->e_flags == E_MCONST) {
1812 /* The left hand side is a constant. Good. Get rhs */
1813 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1815 /* Right hand side is also constant. Get the rhs type */
1816 rhst = lval2.e_tptr;
1818 /* Both expressions are constants. Check for pointer arithmetic */
1819 if (IsPtr (lhst) && IsInt (rhst)) {
1820 /* Left is pointer, right is int, must scale rhs */
1821 lval->e_const = lval->e_const + lval2.e_const * PSizeOf (lhst);
1822 /* Result type is a pointer */
1823 } else if (IsInt (lhst) && IsPtr (rhst)) {
1824 /* Left is int, right is pointer, must scale lhs */
1825 lval->e_const = lval->e_const * PSizeOf (rhst) + lval2.e_const;
1826 /* Result type is a pointer */
1827 lval->e_tptr = lval2.e_tptr;
1828 } else if (IsInt (lhst) && IsInt (rhst)) {
1829 /* Integer addition */
1830 lval->e_const += lval2.e_const;
1831 typeadjust (lval, &lval2, 1);
1834 Error (ERR_OP_NOT_ALLOWED);
1837 /* Result is constant, condition codes not set */
1838 lval->e_test = E_MCONST;
1842 /* lhs is constant, rhs is not. Get the rhs type. */
1843 rhst = lval2.e_tptr;
1845 /* Check for pointer arithmetic */
1846 if (IsPtr (lhst) && IsInt (rhst)) {
1847 /* Left is pointer, right is int, must scale rhs */
1848 g_scale (CF_INT, PSizeOf (lhst));
1849 /* Operate on pointers, result type is a pointer */
1851 } else if (IsInt (lhst) && IsPtr (rhst)) {
1852 /* Left is int, right is pointer, must scale lhs */
1853 lval->e_const *= PSizeOf (rhst);
1854 /* Operate on pointers, result type is a pointer */
1856 lval->e_tptr = lval2.e_tptr;
1857 } else if (IsInt (lhst) && IsInt (rhst)) {
1858 /* Integer addition */
1859 flags = typeadjust (lval, &lval2, 1);
1862 Error (ERR_OP_NOT_ALLOWED);
1865 /* Generate code for the add */
1866 g_inc (flags | CF_CONST, lval->e_const);
1868 /* Result is in primary register */
1869 lval->e_flags = E_MEXPR;
1870 lval->e_test &= ~E_CC;
1876 /* Left hand side is not constant. Get the value onto the stack. */
1877 exprhs (CF_NONE, k, lval); /* --> primary register */
1878 Mark = GetCodePos ();
1879 g_push (TypeOf (lval->e_tptr), 0); /* --> stack */
1881 /* Evaluate the rhs */
1882 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1884 /* Right hand side is a constant. Get the rhs type */
1885 rhst = lval2.e_tptr;
1887 /* Remove pushed value from stack */
1889 pop (TypeOf (lval->e_tptr));
1891 /* Check for pointer arithmetic */
1892 if (IsPtr (lhst) && IsInt (rhst)) {
1893 /* Left is pointer, right is int, must scale rhs */
1894 lval2.e_const *= PSizeOf (lhst);
1895 /* Operate on pointers, result type is a pointer */
1897 } else if (IsInt (lhst) && IsPtr (rhst)) {
1898 /* Left is int, right is pointer, must scale lhs (ptr only) */
1899 g_scale (CF_INT | CF_CONST, PSizeOf (rhst));
1900 /* Operate on pointers, result type is a pointer */
1902 lval->e_tptr = lval2.e_tptr;
1903 } else if (IsInt (lhst) && IsInt (rhst)) {
1904 /* Integer addition */
1905 flags = typeadjust (lval, &lval2, 1);
1908 Error (ERR_OP_NOT_ALLOWED);
1911 /* Generate code for the add */
1912 g_inc (flags | CF_CONST, lval2.e_const);
1914 /* Result is in primary register */
1915 lval->e_flags = E_MEXPR;
1916 lval->e_test &= ~E_CC;
1920 /* lhs and rhs are not constant. Get the rhs type. */
1921 rhst = lval2.e_tptr;
1923 /* Check for pointer arithmetic */
1924 if (IsPtr (lhst) && IsInt (rhst)) {
1925 /* Left is pointer, right is int, must scale rhs */
1926 g_scale (CF_INT, PSizeOf (lhst));
1927 /* Operate on pointers, result type is a pointer */
1929 } else if (IsInt (lhst) && IsPtr (rhst)) {
1930 /* Left is int, right is pointer, must scale lhs */
1931 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
1932 g_swap (CF_INT); /* Swap TOS and primary */
1933 g_scale (CF_INT, PSizeOf (rhst));
1934 /* Operate on pointers, result type is a pointer */
1936 lval->e_tptr = lval2.e_tptr;
1937 } else if (IsInt (lhst) && IsInt (rhst)) {
1938 /* Integer addition */
1939 flags = typeadjust (lval, &lval2, 0);
1942 Error (ERR_OP_NOT_ALLOWED);
1945 /* Generate code for the add */
1948 /* Result is in primary register */
1949 lval->e_flags = E_MEXPR;
1950 lval->e_test &= ~E_CC;
1959 static void parsesub (int k, struct expent* lval)
1960 /* Parse an expression with the binary minus operator. lval contains the
1961 * unprocessed left hand side of the expression and will contain the
1962 * result of the expression on return.
1965 struct expent lval2;
1966 unsigned flags; /* Operation flags */
1967 type* lhst; /* Type of left hand side */
1968 type* rhst; /* Type of right hand side */
1969 CodeMark Mark1; /* Save position of output queue */
1970 CodeMark Mark2; /* Another position in the queue */
1971 int rscale; /* Scale factor for the result */
1974 /* Skip the MINUS token */
1977 /* Get the left hand side type, initialize operation flags */
1978 lhst = lval->e_tptr;
1980 rscale = 1; /* Scale by 1, that is, don't scale */
1982 /* Remember the output queue position, then bring the value onto the stack */
1983 Mark1 = GetCodePos ();
1984 exprhs (CF_NONE, k, lval); /* --> primary register */
1985 Mark2 = GetCodePos ();
1986 g_push (TypeOf (lhst), 0); /* --> stack */
1988 /* Parse the right hand side */
1989 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1991 /* The right hand side is constant. Get the rhs type. */
1992 rhst = lval2.e_tptr;
1994 /* Check left hand side */
1995 if (k == 0 && lval->e_flags & E_MCONST) {
1997 /* Both sides are constant, remove generated code */
1999 pop (TypeOf (lhst)); /* Clean up the stack */
2001 /* Check for pointer arithmetic */
2002 if (IsPtr (lhst) && IsInt (rhst)) {
2003 /* Left is pointer, right is int, must scale rhs */
2004 lval->e_const -= lval2.e_const * PSizeOf (lhst);
2005 /* Operate on pointers, result type is a pointer */
2006 } else if (IsPtr (lhst) && IsPtr (rhst)) {
2007 /* Left is pointer, right is pointer, must scale result */
2008 if (TypeCmp (Indirect (lhst), Indirect (rhst)) != 0) {
2009 Error (ERR_INCOMPATIBLE_POINTERS);
2011 lval->e_const = (lval->e_const - lval2.e_const) / PSizeOf (lhst);
2013 /* Operate on pointers, result type is an integer */
2014 lval->e_tptr = type_int;
2015 } else if (IsInt (lhst) && IsInt (rhst)) {
2016 /* Integer subtraction */
2017 typeadjust (lval, &lval2, 1);
2018 lval->e_const -= lval2.e_const;
2021 Error (ERR_OP_NOT_ALLOWED);
2024 /* Result is constant, condition codes not set */
2025 lval->e_flags = E_MCONST;
2026 lval->e_test &= ~E_CC;
2030 /* Left hand side is not constant, right hand side is.
2031 * Remove pushed value from stack.
2034 pop (TypeOf (lhst));
2036 if (IsPtr (lhst) && IsInt (rhst)) {
2037 /* Left is pointer, right is int, must scale rhs */
2038 lval2.e_const *= PSizeOf (lhst);
2039 /* Operate on pointers, result type is a pointer */
2041 } else if (IsPtr (lhst) && IsPtr (rhst)) {
2042 /* Left is pointer, right is pointer, must scale result */
2043 if (TypeCmp (Indirect (lhst), Indirect (rhst)) != 0) {
2044 Error (ERR_INCOMPATIBLE_POINTERS);
2046 rscale = PSizeOf (lhst);
2048 /* Operate on pointers, result type is an integer */
2050 lval->e_tptr = type_int;
2051 } else if (IsInt (lhst) && IsInt (rhst)) {
2052 /* Integer subtraction */
2053 flags = typeadjust (lval, &lval2, 1);
2056 Error (ERR_OP_NOT_ALLOWED);
2059 /* Do the subtraction */
2060 g_dec (flags | CF_CONST, lval2.e_const);
2062 /* If this was a pointer subtraction, we must scale the result */
2064 g_scale (flags, -rscale);
2067 /* Result is in primary register */
2068 lval->e_flags = E_MEXPR;
2069 lval->e_test &= ~E_CC;
2075 /* Right hand side is not constant. Get the rhs type. */
2076 rhst = lval2.e_tptr;
2078 /* Check for pointer arithmetic */
2079 if (IsPtr (lhst) && IsInt (rhst)) {
2080 /* Left is pointer, right is int, must scale rhs */
2081 g_scale (CF_INT, PSizeOf (lhst));
2082 /* Operate on pointers, result type is a pointer */
2084 } else if (IsPtr (lhst) && IsPtr (rhst)) {
2085 /* Left is pointer, right is pointer, must scale result */
2086 if (TypeCmp (Indirect (lhst), Indirect (rhst)) != 0) {
2087 Error (ERR_INCOMPATIBLE_POINTERS);
2089 rscale = PSizeOf (lhst);
2091 /* Operate on pointers, result type is an integer */
2093 lval->e_tptr = type_int;
2094 } else if (IsInt (lhst) && IsInt (rhst)) {
2095 /* Integer subtraction. If the left hand side descriptor says that
2096 * the lhs is const, we have to remove this mark, since this is no
2097 * longer true, lhs is on stack instead.
2099 if (lval->e_flags == E_MCONST) {
2100 lval->e_flags = E_MEXPR;
2102 /* Adjust operand types */
2103 flags = typeadjust (lval, &lval2, 0);
2106 Error (ERR_OP_NOT_ALLOWED);
2109 /* Generate code for the sub (the & is a hack here) */
2110 g_sub (flags & ~CF_CONST, 0);
2112 /* If this was a pointer subtraction, we must scale the result */
2114 g_scale (flags, -rscale);
2117 /* Result is in primary register */
2118 lval->e_flags = E_MEXPR;
2119 lval->e_test &= ~E_CC;
2125 static int hie8 (struct expent* lval)
2126 /* Process + and - binary operators. */
2128 int k = hie9 (lval);
2129 while (curtok == PLUS || curtok == MINUS) {
2131 if (curtok == PLUS) {
2144 static int hie7 (struct expent *lval)
2145 /* Parse << and >>. */
2147 static GenDesc* hie7_ops [] = {
2152 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2157 static int hie6 (struct expent *lval)
2158 /* process greater-than type comparators */
2160 static GenDesc* hie6_ops [] = {
2161 &GenLT, &GenLE, &GenGE, &GenGT, 0
2163 return hie_compare (hie6_ops, lval, hie7);
2168 static int hie5 (struct expent *lval)
2170 static GenDesc* hie5_ops[] = {
2173 return hie_compare (hie5_ops, lval, hie6);
2178 static int hie4 (struct expent* lval)
2179 /* Handle & (bitwise and) */
2181 static GenDesc* hie4_ops [] = {
2186 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2191 static int hie3 (struct expent *lval)
2192 /* Handle ^ (bitwise exclusive or) */
2194 static GenDesc* hie3_ops [] = {
2199 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2204 static int hie2 (struct expent *lval)
2205 /* Handle | (bitwise or) */
2207 static GenDesc* hie2_ops [] = {
2212 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2217 static int hieAnd (struct expent* lval, unsigned TrueLab, int* BoolOp)
2218 /* Process "exp && exp" */
2222 struct expent lval2;
2225 if (curtok == DAMP) {
2227 /* Tell our caller that we're evaluating a boolean */
2230 /* Get a label that we will use for false expressions */
2233 /* If the expr hasn't set condition codes, set the force-test flag */
2234 if ((lval->e_test & E_CC) == 0) {
2235 lval->e_test |= E_FORCETEST;
2238 /* Load the value */
2239 exprhs (CF_FORCECHAR, k, lval);
2241 /* Generate the jump */
2242 g_falsejump (CF_NONE, lab);
2244 /* Parse more boolean and's */
2245 while (curtok == DAMP) {
2252 if ((lval2.e_test & E_CC) == 0) {
2253 lval2.e_test |= E_FORCETEST;
2255 exprhs (CF_FORCECHAR, k, &lval2);
2257 /* Do short circuit evaluation */
2258 if (curtok == DAMP) {
2259 g_falsejump (CF_NONE, lab);
2261 /* Last expression - will evaluate to true */
2262 g_truejump (CF_NONE, TrueLab);
2266 /* Define the false jump label here */
2267 g_defloclabel (lab);
2269 /* Define the label */
2270 lval->e_flags = E_MEXPR;
2271 lval->e_test |= E_CC; /* Condition codes are set */
2279 static int hieOr (struct expent *lval)
2280 /* Process "exp || exp". */
2283 struct expent lval2;
2284 int BoolOp = 0; /* Did we have a boolean op? */
2285 int AndOp; /* Did we have a && operation? */
2286 unsigned TrueLab; /* Jump to this label if true */
2290 TrueLab = GetLabel ();
2292 /* Call the next level parser */
2293 k = hieAnd (lval, TrueLab, &BoolOp);
2295 /* Any boolean or's? */
2296 if (curtok == DBAR) {
2298 /* If the expr hasn't set condition codes, set the force-test flag */
2299 if ((lval->e_test & E_CC) == 0) {
2300 lval->e_test |= E_FORCETEST;
2303 /* Get first expr */
2304 exprhs (CF_FORCECHAR, k, lval);
2306 /* For each expression jump to TrueLab if true. Beware: If we
2307 * had && operators, the jump is already in place!
2310 g_truejump (CF_NONE, TrueLab);
2313 /* Remember that we had a boolean op */
2316 /* while there's more expr */
2317 while (curtok == DBAR) {
2324 k = hieAnd (&lval2, TrueLab, &AndOp);
2325 if ((lval2.e_test & E_CC) == 0) {
2326 lval2.e_test |= E_FORCETEST;
2328 exprhs (CF_FORCECHAR, k, &lval2);
2330 /* If there is more to come, add shortcut boolean eval.
2331 * Beware: If we had && operators, the jump is already
2335 /* Seems this sometimes generates wrong code */
2336 if (curtok == DBAR && !AndOp) {
2337 g_truejump (CF_NONE, TrueLab);
2340 g_truejump (CF_NONE, TrueLab);
2343 lval->e_flags = E_MEXPR;
2344 lval->e_test |= E_CC; /* Condition codes are set */
2348 /* If we really had boolean ops, generate the end sequence */
2350 DoneLab = GetLabel ();
2351 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2352 g_falsejump (CF_NONE, DoneLab);
2353 g_defloclabel (TrueLab);
2354 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2355 g_defloclabel (DoneLab);
2362 static int hieQuest (struct expent *lval)
2363 /* Parse "lvalue ? exp : exp" */
2368 struct expent lval2; /* Expression 2 */
2369 struct expent lval3; /* Expression 3 */
2370 type* type2; /* Type of expression 2 */
2371 type* type3; /* Type of expression 3 */
2372 type* rtype; /* Type of result */
2373 CodeMark Mark1; /* Save position in output code */
2374 CodeMark Mark2; /* Save position in output code */
2379 if (curtok == QUEST) {
2381 if ((lval->e_test & E_CC) == 0) {
2382 /* Condition codes not set, force a test */
2383 lval->e_test |= E_FORCETEST;
2385 exprhs (CF_NONE, k, lval);
2387 g_falsejump (CF_NONE, labf);
2389 /* Parse second and third expression */
2390 expression1 (&lval2);
2394 g_defloclabel (labf);
2395 expression1 (&lval3);
2397 /* Check if any conversions are needed, if so, do them.
2398 * Conversion rules for ?: expression are:
2399 * - if both expressions are int expressions, default promotion
2400 * rules for ints apply.
2401 * - if both expressions are pointers of the same type, the
2402 * result of the expression is of this type.
2403 * - if one of the expressions is a pointer and the other is
2404 * a zero constant, the resulting type is that of the pointer
2406 * - all other cases are flagged by an error.
2408 type2 = lval2.e_tptr;
2409 type3 = lval3.e_tptr;
2410 if (IsInt (type2) && IsInt (type3)) {
2412 /* Get common type */
2413 rtype = promoteint (type2, type3);
2415 /* Convert the third expression to this type if needed */
2416 g_typecast (TypeOf (rtype), TypeOf (type3));
2418 /* Setup a new label so that the expr3 code will jump around
2419 * the type cast code for expr2.
2421 labf = GetLabel (); /* Get new label */
2422 Mark1 = GetCodePos (); /* Remember current position */
2423 g_jump (labf); /* Jump around code */
2425 /* The jump for expr2 goes here */
2426 g_defloclabel (labt);
2428 /* Create the typecast code for expr2 */
2429 Mark2 = GetCodePos (); /* Remember position */
2430 g_typecast (TypeOf (rtype), TypeOf (type2));
2432 /* If the typecast did not produce code, remove the jump,
2433 * otherwise output the label.
2435 if (GetCodePos() == Mark2) {
2436 RemoveCode (Mark1); /* Remove code */
2438 /* We have typecast code, output label */
2439 g_defloclabel (labf);
2440 labt = 0; /* Mark other label as invalid */
2443 } else if (IsPtr (type2) && IsPtr (type3)) {
2444 /* Must point to same type */
2445 if (TypeCmp (Indirect (type2), Indirect (type3)) != 0) {
2446 Error (ERR_INCOMPATIBLE_TYPES);
2448 /* Result has the common type */
2449 rtype = lval2.e_tptr;
2450 } else if (IsPtr (type2) && IsNullPtr (&lval3)) {
2451 /* Result type is pointer, no cast needed */
2452 rtype = lval2.e_tptr;
2453 } else if (IsNullPtr (&lval2) && IsPtr (type3)) {
2454 /* Result type is pointer, no cast needed */
2455 rtype = lval3.e_tptr;
2457 Error (ERR_INCOMPATIBLE_TYPES);
2458 rtype = lval2.e_tptr; /* Doesn't matter here */
2461 /* If we don't have the label defined until now, do it */
2463 g_defloclabel (labt);
2466 /* Setup the target expression */
2467 lval->e_flags = E_MEXPR;
2468 lval->e_tptr = rtype;
2476 static void opeq (GenDesc* Gen, struct expent *lval, int k)
2477 /* Process "op=" operators. */
2479 struct expent lval2;
2486 Error (ERR_LVALUE_EXPECTED);
2490 /* Determine the type of the lhs */
2491 flags = TypeOf (lval->e_tptr);
2492 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2493 lval->e_tptr [0] == T_PTR;
2495 /* Get the lhs address on stack (if needed) */
2498 /* Fetch the lhs into the primary register if needed */
2499 exprhs (CF_NONE, k, lval);
2501 /* Bring the lhs on stack */
2502 Mark = GetCodePos ();
2505 /* Evaluate the rhs */
2506 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2507 /* The resulting value is a constant. If the generator has the NOPUSH
2508 * flag set, don't push the lhs.
2510 if (Gen->Flags & GEN_NOPUSH) {
2515 /* lhs is a pointer, scale rhs */
2516 lval2.e_const *= SizeOf (lval->e_tptr+1);
2519 /* If the lhs is character sized, the operation may be later done
2522 if (SizeOf (lval->e_tptr) == 1) {
2523 flags |= CF_FORCECHAR;
2526 /* Special handling for add and sub - some sort of a hack, but short code */
2527 if (Gen->Func == g_add) {
2528 g_inc (flags | CF_CONST, lval2.e_const);
2529 } else if (Gen->Func == g_sub) {
2530 g_dec (flags | CF_CONST, lval2.e_const);
2532 Gen->Func (flags | CF_CONST, lval2.e_const);
2535 /* rhs is not constant and already in the primary register */
2537 /* lhs is a pointer, scale rhs */
2538 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2541 /* If the lhs is character sized, the operation may be later done
2544 if (SizeOf (lval->e_tptr) == 1) {
2545 flags |= CF_FORCECHAR;
2548 /* Adjust the types of the operands if needed */
2549 Gen->Func (g_typeadjust (flags, TypeOf (lval2.e_tptr)), 0);
2552 lval->e_flags = E_MEXPR;
2557 static void addsubeq (GenDesc* Gen, struct expent *lval, int k)
2558 /* Process the += and -= operators */
2560 struct expent lval2;
2566 Error (ERR_LVALUE_EXPECTED);
2571 /* We're currently only able to handle some adressing modes */
2572 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
2573 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
2574 (lval->e_flags & E_MCONST) == 0) { /* Constant address? */
2575 /* Use generic routine */
2576 opeq (Gen, lval, k);
2580 /* Skip the operator */
2583 /* Check if we have a pointer expression and must scale rhs */
2584 MustScale = (lval->e_tptr [0] == T_PTR);
2586 /* Determine the code generator flags */
2587 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR;
2589 /* Evaluate the rhs */
2590 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2591 /* The resulting value is a constant. */
2593 /* lhs is a pointer, scale rhs */
2594 lval2.e_const *= SizeOf (lval->e_tptr+1);
2598 /* rhs is not constant and already in the primary register */
2600 /* lhs is a pointer, scale rhs */
2601 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2605 /* Adjust the rhs to the lhs */
2606 g_typeadjust (flags, TypeOf (lval2.e_tptr));
2608 /* Output apropriate code */
2609 if (lval->e_flags & E_MGLOBAL) {
2610 /* Static variable */
2611 flags |= GlobalModeFlags (lval->e_flags);
2612 if (Gen->Tok == PASGN) {
2613 g_addeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2615 g_subeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2617 } else if (lval->e_flags & E_MLOCAL) {
2618 /* ref to localvar */
2619 if (Gen->Tok == PASGN) {
2620 g_addeqlocal (flags, lval->e_const, lval2.e_const);
2622 g_subeqlocal (flags, lval->e_const, lval2.e_const);
2624 } else if (lval->e_flags & E_MCONST) {
2625 /* ref to absolute address */
2626 flags |= CF_ABSOLUTE;
2627 if (Gen->Tok == PASGN) {
2628 g_addeqstatic (flags, lval->e_const, 0, lval2.e_const);
2630 g_subeqstatic (flags, lval->e_const, 0, lval2.e_const);
2632 } else if (lval->e_flags & E_MEXPR) {
2633 /* Address in a/x. */
2634 if (Gen->Tok == PASGN) {
2635 g_addeqind (flags, lval->e_const, lval2.e_const);
2637 g_subeqind (flags, lval->e_const, lval2.e_const);
2640 Internal ("Invalid addressing mode");
2643 /* Expression is in the primary now */
2644 lval->e_flags = E_MEXPR;
2649 static void Assignment (struct expent* lval)
2650 /* Parse an assignment */
2653 struct expent lval2;
2655 type* ltype = lval->e_tptr;
2657 /* cc65 does not have full support for handling structs by value. Since
2658 * assigning structs is one of the more useful operations from this
2659 * familiy, allow it here.
2661 if (IsStruct (ltype)) {
2663 /* Bring the address of the lhs into the primary and push it */
2664 exprhs (0, 0, lval);
2665 g_push (CF_PTR | CF_UNSIGNED, 0);
2667 /* Get the expression on the right of the '=' into the primary */
2670 /* Get the address */
2671 exprhs (0, 0, &lval2);
2673 /* We need an lvalue */
2674 Error (ERR_LVALUE_EXPECTED);
2677 /* Push the address (or whatever is in ax in case of errors) */
2678 g_push (CF_PTR | CF_UNSIGNED, 0);
2680 /* Check for equality of the structs */
2681 if (!EqualTypes (ltype, lval2.e_tptr)) {
2682 Error (ERR_INCOMPATIBLE_TYPES);
2685 /* Load the size of the struct into the primary */
2686 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, SizeOf (ltype), 0);
2688 /* Call the memcpy function */
2689 g_call (CF_FIXARGC, "memcpy", 4);
2693 /* Get the address on stack if needed */
2696 /* No struct, setup flags for the load */
2697 flags = SizeOf (ltype) == 1? CF_FORCECHAR : CF_NONE;
2699 /* Get the expression on the right of the '=' into the primary */
2700 if (evalexpr (flags, hie1, &lval2) == 0) {
2701 /* Constant expression. Adjust the types */
2702 assignadjust (ltype, &lval2);
2703 /* Put the value into the primary register */
2704 lconst (flags, &lval2);
2706 /* Expression is not constant and already in the primary */
2707 assignadjust (ltype, &lval2);
2710 /* Generate a store instruction */
2715 /* Value is still in primary */
2716 lval->e_flags = E_MEXPR;
2721 int hie1 (struct expent* lval)
2722 /* Parse first level of expression hierarchy. */
2726 k = hieQuest (lval);
2736 Error (ERR_LVALUE_EXPECTED);
2743 addsubeq (&GenPASGN, lval, k);
2747 addsubeq (&GenSASGN, lval, k);
2751 opeq (&GenMASGN, lval, k);
2755 opeq (&GenDASGN, lval, k);
2759 opeq (&GenMOASGN, lval, k);
2763 opeq (&GenSLASGN, lval, k);
2767 opeq (&GenSRASGN, lval, k);
2771 opeq (&GenAASGN, lval, k);
2775 opeq (&GenXOASGN, lval, k);
2779 opeq (&GenOASGN, lval, k);
2790 int hie0 (struct expent *lval)
2791 /* Parse comma operator. */
2796 while (curtok == COMMA) {
2805 int evalexpr (unsigned flags, int (*f) (struct expent*), struct expent* lval)
2806 /* Will evaluate an expression via the given function. If the result is a
2807 * constant, 0 is returned and the value is put in the lval struct. If the
2808 * result is not constant, exprhs is called to bring the value into the
2809 * primary register and 1 is returned.
2816 if (k == 0 && lval->e_flags == E_MCONST) {
2817 /* Constant expression */
2820 /* Not constant, load into the primary */
2821 exprhs (flags, k, lval);
2828 int expr (int (*func) (), struct expent *lval)
2829 /* Expression parser; func is either hie0 or hie1. */
2838 /* Do some checks if code generation is still constistent */
2839 if (savsp != oursp) {
2841 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
2843 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
2851 void expression1 (struct expent* lval)
2852 /* Evaluate an expression on level 1 (no comma operator) and put it into
2853 * the primary register
2856 memset (lval, 0, sizeof (*lval));
2857 exprhs (CF_NONE, expr (hie1, lval), lval);
2862 void expression (struct expent* lval)
2863 /* Evaluate an expression and put it into the primary register */
2865 memset (lval, 0, sizeof (*lval));
2866 exprhs (CF_NONE, expr (hie0, lval), lval);
2871 void constexpr (struct expent* lval)
2872 /* Get a constant value */
2874 memset (lval, 0, sizeof (*lval));
2875 if (expr (hie1, lval) != 0 || (lval->e_flags & E_MCONST) == 0) {
2876 Error (ERR_CONST_EXPR_EXPECTED);
2877 /* To avoid any compiler errors, make the expression a valid const */
2878 lval->e_flags = E_MCONST;
2879 lval->e_tptr = type_int;
2886 void intexpr (struct expent* lval)
2887 /* Get an integer expression */
2890 if (!IsInt (lval->e_tptr)) {
2891 Error (ERR_INT_EXPR_EXPECTED);
2892 /* To avoid any compiler errors, make the expression a valid int */
2893 lval->e_flags = E_MCONST;
2894 lval->e_tptr = type_int;
2901 void boolexpr (struct expent* lval)
2902 /* Get a boolean expression */
2904 /* Read an expression */
2907 /* If it's an integer, it's ok. If it's not an integer, but a pointer,
2908 * the pointer used in a boolean context is also ok (Ootherwise check if it's a pointer
2911 if (!IsInt (lval->e_tptr) && !IsPtr (lval->e_tptr)) {
2912 Error (ERR_INT_EXPR_EXPECTED);
2913 /* To avoid any compiler errors, make the expression a valid int */
2914 lval->e_flags = E_MCONST;
2915 lval->e_tptr = type_int;
2922 void test (unsigned label, int cond)
2923 /* Generate code to perform test and jump if false. */
2928 /* Eat the parenthesis */
2931 /* Prepare the expression, setup labels */
2932 memset (&lval, 0, sizeof (lval));
2933 lval.e_test = E_TEST;
2935 /* Generate code to eval the expr */
2936 k = expr (hie0, &lval);
2937 if (k == 0 && lval.e_flags == E_MCONST) {
2938 /* Constant rvalue */
2939 if (cond == 0 && lval.e_const == 0) {
2941 Warning (WARN_UNREACHABLE_CODE);
2942 } else if (cond && lval.e_const) {
2949 /* If the expr hasn't set condition codes, set the force-test flag */
2950 if ((lval.e_test & E_CC) == 0) {
2951 lval.e_test |= E_FORCETEST;
2954 /* Load the value into the primary register */
2955 exprhs (CF_FORCECHAR, k, &lval);
2957 /* Check for the closing brace */
2960 /* Generate the jump */
2962 g_truejump (CF_NONE, label);
2964 /* Special case (putting this here is a small hack - but hey, the
2965 * compiler itself is one big hack...): If a semicolon follows, we
2966 * don't have a statement and may omit the jump.
2968 if (curtok != SEMI) {
2969 g_falsejump (CF_NONE, label);