4 * Ullrich von Bassewitz, 21.06.1998
13 #include "../common/xmalloc.h"
36 /*****************************************************************************/
38 /*****************************************************************************/
42 /* Generator attributes */
43 #define GEN_NOPUSH 0x01 /* Don't push lhs */
45 /* Map a generator function and its attributes to a token */
47 unsigned char Tok; /* Token to map to */
48 unsigned char Flags; /* Flags for generator function */
49 void (*Func) (unsigned, unsigned long); /* Generator func */
52 /* Descriptors for the operations */
53 static GenDesc GenMUL = { TOK_STAR, GEN_NOPUSH, g_mul };
54 static GenDesc GenDIV = { TOK_DIV, GEN_NOPUSH, g_div };
55 static GenDesc GenMOD = { TOK_MOD, GEN_NOPUSH, g_mod };
56 static GenDesc GenASL = { TOK_SHL, GEN_NOPUSH, g_asl };
57 static GenDesc GenASR = { TOK_SHR, GEN_NOPUSH, g_asr };
58 static GenDesc GenLT = { TOK_LT, GEN_NOPUSH, g_lt };
59 static GenDesc GenLE = { TOK_LE, GEN_NOPUSH, g_le };
60 static GenDesc GenGE = { TOK_GE, GEN_NOPUSH, g_ge };
61 static GenDesc GenGT = { TOK_GT, GEN_NOPUSH, g_gt };
62 static GenDesc GenEQ = { TOK_EQ, GEN_NOPUSH, g_eq };
63 static GenDesc GenNE = { TOK_NE, GEN_NOPUSH, g_ne };
64 static GenDesc GenAND = { TOK_AND, GEN_NOPUSH, g_and };
65 static GenDesc GenXOR = { TOK_XOR, GEN_NOPUSH, g_xor };
66 static GenDesc GenOR = { TOK_OR, GEN_NOPUSH, g_or };
67 static GenDesc GenPASGN = { TOK_PLUS_ASSIGN, GEN_NOPUSH, g_add };
68 static GenDesc GenSASGN = { TOK_MINUS_ASSIGN, GEN_NOPUSH, g_sub };
69 static GenDesc GenMASGN = { TOK_MUL_ASSIGN, GEN_NOPUSH, g_mul };
70 static GenDesc GenDASGN = { TOK_DIV_ASSIGN, GEN_NOPUSH, g_div };
71 static GenDesc GenMOASGN = { TOK_MOD_ASSIGN, GEN_NOPUSH, g_mod };
72 static GenDesc GenSLASGN = { TOK_SHL_ASSIGN, GEN_NOPUSH, g_asl };
73 static GenDesc GenSRASGN = { TOK_SHR_ASSIGN, GEN_NOPUSH, g_asr };
74 static GenDesc GenAASGN = { TOK_AND_ASSIGN, GEN_NOPUSH, g_and };
75 static GenDesc GenXOASGN = { TOK_XOR_ASSIGN, GEN_NOPUSH, g_xor };
76 static GenDesc GenOASGN = { TOK_OR_ASSIGN, GEN_NOPUSH, g_or };
80 /*****************************************************************************/
81 /* Function forwards */
82 /*****************************************************************************/
86 static int hie10 (struct expent* lval);
87 /* Handle ++, --, !, unary - etc. */
91 /*****************************************************************************/
92 /* Helper functions */
93 /*****************************************************************************/
97 static unsigned GlobalModeFlags (unsigned flags)
98 /* Return the addressing mode flags for the variable with the given flags */
101 if (flags == E_TGLAB) {
102 /* External linkage */
104 } else if (flags == E_TREGISTER) {
105 /* Register variable */
115 static int IsNullPtr (struct expent* lval)
116 /* Return true if this is the NULL pointer constant */
118 return (IsInt (lval->e_tptr) && /* Is it an int? */
119 lval->e_flags == E_MCONST && /* Is it constant? */
120 lval->e_const == 0); /* And is it's value zero? */
125 static type* promoteint (type* lhst, type* rhst)
126 /* In an expression with two ints, return the type of the result */
128 /* Rules for integer types:
129 * - If one of the values is a long, the result is long.
130 * - If one of the values is unsigned, the result is also unsigned.
131 * - Otherwise the result is an int.
133 if (IsLong (lhst) || IsLong (rhst)) {
134 if (IsUnsigned (lhst) || IsUnsigned (rhst)) {
140 if (IsUnsigned (lhst) || IsUnsigned (rhst)) {
150 static unsigned typeadjust (struct expent* lhs, struct expent* rhs, int NoPush)
151 /* Adjust the two values for a binary operation. lhs is expected on stack or
152 * to be constant, rhs is expected to be in the primary register or constant.
153 * The function will put the type of the result into lhs and return the
154 * code generator flags for the operation.
155 * If NoPush is given, it is assumed that the operation does not expect the lhs
156 * to be on stack, and that lhs is in a register instead.
157 * Beware: The function does only accept int types.
160 unsigned ltype, rtype;
163 /* Get the type strings */
164 type* lhst = lhs->e_tptr;
165 type* rhst = rhs->e_tptr;
167 /* Generate type adjustment code if needed */
168 ltype = TypeOf (lhst);
169 if (lhs->e_flags == E_MCONST) {
173 /* Value is in primary register*/
176 rtype = TypeOf (rhst);
177 if (rhs->e_flags == E_MCONST) {
180 flags = g_typeadjust (ltype, rtype);
182 /* Set the type of the result */
183 lhs->e_tptr = promoteint (lhst, rhst);
185 /* Return the code generator flags */
191 unsigned assignadjust (type* lhst, struct expent* rhs)
192 /* Adjust the type of the right hand expression so that it can be assigned to
193 * the type on the left hand side. This function is used for assignment and
194 * for converting parameters in a function call. It returns the code generator
195 * flags for the operation. The type string of the right hand side will be
196 * set to the type of the left hand side.
199 /* Get the type of the right hand side */
200 type* rhst = rhs->e_tptr;
202 /* After calling this function, rhs will have the type of the lhs */
205 /* First, do some type checking */
206 if (IsVoid (lhst) || IsVoid (rhst)) {
207 /* If one of the sides are of type void, output a more apropriate
210 Error (ERR_ILLEGAL_TYPE);
211 } else if (IsInt (lhst)) {
213 /* Pointer -> int conversion */
214 Warning (WARN_PTR_TO_INT_CONV);
215 } else if (!IsInt (rhst)) {
216 Error (ERR_INCOMPATIBLE_TYPES);
218 /* Adjust the int types. To avoid manipulation of TOS mark lhs
221 unsigned flags = TypeOf (rhst);
222 if (rhs->e_flags & E_MCONST) {
225 return g_typeadjust (TypeOf (lhst) | CF_CONST, flags);
227 } else if (IsPtr (lhst)) {
229 /* Pointer to pointer assignment is valid, if:
230 * - both point to the same types, or
231 * - the rhs pointer is a void pointer, or
232 * - the lhs pointer is a void pointer.
234 type* left = Indirect (lhst);
235 type* right = Indirect (rhst);
236 if (!EqualTypes (left, right) && *left != T_VOID && *right != T_VOID) {
237 Error (ERR_INCOMPATIBLE_POINTERS);
239 } else if (IsInt (rhst)) {
240 /* Int to pointer assignment is valid only for constant zero */
241 if ((rhs->e_flags & E_MCONST) == 0 || rhs->e_const != 0) {
242 Warning (WARN_INT_TO_PTR_CONV);
244 } else if (IsFuncPtr (lhst) && IsFunc(rhst)) {
245 /* Assignment of function to function pointer is allowed, provided
246 * that both functions have the same parameter list.
248 if (!EqualTypes(Indirect (lhst), rhst)) {
249 Error (ERR_INCOMPATIBLE_TYPES);
252 Error (ERR_INCOMPATIBLE_TYPES);
255 Error (ERR_INCOMPATIBLE_TYPES);
258 /* Return an int value in all cases where the operands are not both ints */
264 void DefineData (struct expent* lval)
265 /* Output a data definition for the given expression */
267 unsigned flags = lval->e_flags;
269 switch (flags & E_MCTYPE) {
273 g_defdata (TypeOf (lval->e_tptr) | CF_CONST, lval->e_const, 0);
277 /* Register variable. Taking the address is usually not
280 if (!AllowRegVarAddr) {
281 Error (ERR_CANNOT_TAKE_ADDR_OF_REG);
287 /* Local or global symbol */
288 g_defdata (GlobalModeFlags (flags), lval->e_name, lval->e_const);
292 /* a literal of some kind */
293 g_defdata (CF_STATIC, LiteralLabel, lval->e_const);
297 Internal ("Unknown constant type: %04X", flags);
303 static void lconst (unsigned flags, struct expent* lval)
304 /* Load primary reg with some constant value. */
306 switch (lval->e_flags & E_MCTYPE) {
309 g_leasp (lval->e_const);
313 /* Number constant */
314 g_getimmed (flags | TypeOf (lval->e_tptr) | CF_CONST, lval->e_const, 0);
318 /* Register variable. Taking the address is usually not
321 if (!AllowRegVarAddr) {
322 Error (ERR_CANNOT_TAKE_ADDR_OF_REG);
328 /* Local or global symbol, load address */
329 flags |= GlobalModeFlags (lval->e_flags);
331 g_getimmed (flags, lval->e_name, lval->e_const);
336 g_getimmed (CF_STATIC, LiteralLabel, lval->e_const);
340 Internal ("Unknown constant type: %04X", lval->e_flags);
346 static int kcalc (int tok, long val1, long val2)
347 /* Calculate an operation with left and right operand constant. */
351 return (val1 == val2);
353 return (val1 != val2);
355 return (val1 < val2);
357 return (val1 <= val2);
359 return (val1 >= val2);
361 return (val1 > val2);
363 return (val1 | val2);
365 return (val1 ^ val2);
367 return (val1 & val2);
369 return (val1 >> val2);
371 return (val1 << val2);
373 return (val1 * val2);
376 Error (ERR_DIV_BY_ZERO);
379 return (val1 / val2);
382 Error (ERR_MOD_BY_ZERO);
385 return (val1 % val2);
387 Internal ("kcalc: got token 0x%X\n", tok);
394 static GenDesc* FindGen (int Tok, GenDesc** Table)
397 while ((G = *Table) != 0) {
408 static int istypeexpr (void)
409 /* Return true if some sort of variable or type is waiting (helper for cast
410 * and sizeof() in hie10).
415 return curtok == TOK_LPAREN && (
416 (nxttok >= TOK_FIRSTTYPE && nxttok <= TOK_LASTTYPE) ||
417 (nxttok == TOK_CONST) ||
418 (nxttok == TOK_IDENT &&
419 (Entry = FindSym (NextTok.Ident)) != 0 &&
426 static void PushAddr (struct expent* lval)
427 /* If the expression contains an address that was somehow evaluated,
428 * push this address on the stack. This is a helper function for all
429 * sorts of implicit or explicit assignment functions where the lvalue
430 * must be saved if it's not constant, before evaluating the rhs.
433 /* Get the address on stack if needed */
434 if (lval->e_flags != E_MREG && (lval->e_flags & E_MEXPR)) {
435 /* Push the address (always a pointer) */
442 /*****************************************************************************/
444 /*****************************************************************************/
448 void exprhs (unsigned flags, int k, struct expent *lval)
449 /* Put the result of an expression into the primary register */
455 /* Dereferenced lvalue */
456 flags |= TypeOf (lval->e_tptr);
457 if (lval->e_test & E_FORCETEST) {
459 lval->e_test &= ~E_FORCETEST;
461 if (f & E_MGLOBAL) { /* ref to globalvar */
463 flags |= GlobalModeFlags (f);
464 g_getstatic (flags, lval->e_name, lval->e_const);
465 } else if (f & E_MLOCAL) {
466 /* ref to localvar */
467 g_getlocal (flags, lval->e_const);
468 } else if (f & E_MCONST) {
469 /* ref to absolute address */
470 g_getstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
471 } else if (f == E_MEOFFS) {
472 g_getind (flags, lval->e_const);
473 } else if (f != E_MREG) {
476 } else if (f == E_MEOFFS) {
477 /* reference not storable */
478 flags |= TypeOf (lval->e_tptr);
479 g_inc (flags | CF_CONST, lval->e_const);
480 } else if ((f & E_MEXPR) == 0) {
481 /* Constant of some sort, load it into the primary */
482 lconst (flags, lval);
484 if (lval->e_test & E_FORCETEST) { /* we testing this value? */
486 AddCodeHint ("forcetest");
487 flags |= TypeOf (lval->e_tptr);
488 g_test (flags); /* yes, force a test */
489 lval->e_test &= ~E_FORCETEST;
494 static void callfunction (struct expent* lval)
495 /* Perform a function call. Called from hie11, this routine will
496 * either call the named function, or if the supplied ptr is zero,
497 * will call the contents of P.
501 FuncDesc* Func; /* Function descriptor */
502 int Ellipsis; /* True if we have an open param list */
503 SymEntry* Param; /* Current formal parameter */
504 unsigned ParamCount; /* Actual parameter count */
505 unsigned ParamSize; /* Number of parameter bytes */
511 /* Get a pointer to the function descriptor from the type string */
512 Func = GetFuncDesc (lval->e_tptr);
514 /* Initialize vars to keep gcc silent */
518 /* Check if this is a function pointer. If so, save it. If not, check for
519 * special known library functions that may be inlined.
521 if (lval->e_flags & E_MEXPR) {
522 /* Function pointer is in primary register, save it */
523 Mark = GetCodePos ();
525 } else if (InlineStdFuncs && IsStdFunc ((const char*) lval->e_name)) {
526 /* Inline this function */
527 HandleStdFunc (lval);
531 /* Parse the actual parameter list */
535 while (curtok != TOK_RPAREN) {
537 /* Add a hint for the optimizer */
538 AddCodeHint ("param:start");
540 /* Count arguments */
543 /* Fetch the pointer to the next argument, check for too many args */
544 if (ParamCount <= Func->ParamCount) {
545 if (ParamCount == 1) {
547 Param = Func->SymTab->SymHead;
550 Param = Param->NextSym;
551 CHECK ((Param->Flags & SC_PARAM) != 0);
553 } else if (!Ellipsis) {
554 /* Too many arguments. Do we have an open param list? */
555 if ((Func->Flags & FD_ELLIPSIS) == 0) {
556 /* End of param list reached, no ellipsis */
557 Error (ERR_TOO_MANY_FUNC_ARGS);
559 /* Assume an ellipsis even in case of errors to avoid an error
560 * message for each other argument.
565 /* Do some optimization: If we have a constant value to push,
566 * use a special function that may optimize.
569 if (!Ellipsis && SizeOf (Param->Type) == 1) {
570 CFlags = CF_FORCECHAR;
573 if (evalexpr (CFlags, hie1, &lval2) == 0) {
574 /* A constant value */
578 /* If we don't have an argument spec, accept anything, otherwise
579 * convert the actual argument to the type needed.
582 /* Promote the argument if needed */
583 assignadjust (Param->Type, &lval2);
584 /* If we have a prototype, chars may be pushed as chars */
585 Flags |= CF_FORCECHAR;
588 /* Use the type of the argument for the push */
589 Flags |= TypeOf (lval2.e_tptr);
591 /* If this is a fastcall function, don't push the last argument */
592 if (ParamCount == Func->ParamCount && (Func->Flags & FD_FASTCALL) != 0) {
593 /* Just load the argument into the primary. This is only needed if
594 * we have a constant argument, otherwise the value is already in
597 if (Flags & CF_CONST) {
598 exprhs (CF_FORCECHAR, 0, &lval2);
601 /* Push the argument, count the argument size */
602 g_push (Flags, lval2.e_const);
603 ParamSize += sizeofarg (Flags);
606 /* Add an optimizer hint */
607 AddCodeHint ("param:end");
609 /* Check for end of argument list */
610 if (curtok != TOK_COMMA) {
616 /* We need the closing bracket here */
619 /* Check if we had enough parameters */
620 if (ParamCount < Func->ParamCount) {
621 Error (ERR_TOO_FEW_FUNC_ARGS);
625 if (lval->e_flags & E_MEXPR) {
626 /* Function called via pointer: Restore it and call function */
627 if (ParamSize != 0) {
630 /* We had no parameters - remove save code */
633 g_callind (TypeOf (lval->e_tptr), ParamSize);
635 g_call (TypeOf (lval->e_tptr), (char*) lval->e_name, ParamSize);
642 /* This function parses ASM statements. The syntax of the ASM directive
643 * looks like the one defined for C++ (C has no ASM directive), that is,
644 * a string literal in parenthesis.
650 /* Need left parenthesis */
654 if (curtok != TOK_SCONST) {
655 Error (ERR_STRLIT_EXPECTED);
657 /* Write the string directly into the output, followed by a newline */
658 AddCodeLine (GetLiteral (curval));
660 /* Reset the string pointer, effectivly clearing the string from the
661 * string table. Since we're working with one token lookahead, this
662 * will fail if the next token is also a string token, but that's a
663 * syntax error anyway, because we expect a right paren.
665 ResetLiteralOffs (curval);
668 /* Skip the string token */
671 /* Closing paren needed */
677 static int primary (struct expent* lval)
678 /* This is the lowest level of the expression parser. */
682 /* not a test at all, yet */
685 /* Character and integer constants. */
686 if (curtok == TOK_ICONST || curtok == TOK_CCONST) {
687 lval->e_flags = E_MCONST | E_TCONST;
688 lval->e_tptr = curtype;
689 lval->e_const = curval;
694 /* Process parenthesized subexpression by calling the whole parser
697 if (curtok == TOK_LPAREN) {
699 memset (lval, 0, sizeof (*lval)); /* Remove any attributes */
705 /* All others may only be used if the expression evaluation is not called
706 * recursively by the preprocessor.
709 /* Illegal expression in PP mode */
710 Error (ERR_CPP_EXPR_EXPECTED);
711 lval->e_flags = E_MCONST;
712 lval->e_tptr = type_int;
717 if (curtok == TOK_IDENT) {
722 /* Get a pointer to the symbol table entry */
723 Sym = FindSym (CurTok.Ident);
725 /* Is the symbol known? */
728 /* We found the symbol - skip the name token */
731 /* The expression type is the symbol type */
732 lval->e_tptr = Sym->Type;
734 /* Check for illegal symbol types */
735 if ((Sym->Flags & SC_LABEL) == SC_LABEL) {
736 /* Cannot use labels in expressions */
737 Error (ERR_SYMBOL_KIND);
739 } else if (Sym->Flags & SC_TYPE) {
740 /* Cannot use type symbols */
741 Error (ERR_VAR_IDENT_EXPECTED);
742 /* Assume an int type to make lval valid */
743 lval->e_flags = E_MLOCAL | E_TLOFFS;
744 lval->e_tptr = type_int;
749 /* Check for legal symbol types */
750 if ((Sym->Flags & SC_ENUM) == SC_ENUM) {
751 lval->e_flags = E_MCONST;
752 lval->e_const = Sym->V.EnumVal;
754 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
756 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
757 lval->e_name = (unsigned long) Sym->Name;
759 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
761 lval->e_flags = E_MLOCAL | E_TLOFFS;
762 lval->e_const = Sym->V.Offs;
763 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
764 /* Static variable */
765 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
766 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
767 lval->e_name = (unsigned long) Sym->Name;
769 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
770 lval->e_name = Sym->V.Label;
773 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
774 /* Register variable, zero page based */
775 lval->e_flags = E_MGLOBAL | E_MCONST | E_TREGISTER;
776 lval->e_name = Sym->V.Offs;
779 /* Local static variable */
780 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
781 lval->e_name = Sym->V.Offs;
785 /* The symbol is referenced now */
786 Sym->Flags |= SC_REF;
787 if (IsFunc (lval->e_tptr) || IsArray (lval->e_tptr)) {
793 /* We did not find the symbol. Remember the name, then skip it */
794 strcpy (Ident, CurTok.Ident);
797 /* IDENT is either an auto-declared function or an undefined variable. */
798 if (curtok == TOK_LPAREN) {
799 /* Declare a function returning int. For that purpose, prepare a
800 * function signature for a function having an empty param list
803 Warning (WARN_FUNC_WITHOUT_PROTO);
804 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF);
805 lval->e_tptr = Sym->Type;
806 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
807 lval->e_name = (unsigned long) Sym->Name;
813 /* Undeclared Variable */
814 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
815 lval->e_flags = E_MLOCAL | E_TLOFFS;
816 lval->e_tptr = type_int;
818 Error (ERR_UNDEFINED_SYMBOL, Ident);
824 /* String literal? */
825 if (curtok == TOK_SCONST) {
826 lval->e_flags = E_MCONST | E_TLIT;
827 lval->e_const = curval;
828 lval->e_tptr = GetCharArrayType (strlen (GetLiteral (curval)));
834 if (curtok == TOK_ASM) {
836 lval->e_tptr = type_void;
837 lval->e_flags = E_MEXPR;
842 /* __AX__ and __EAX__ pseudo values? */
843 if (curtok == TOK_AX || curtok == TOK_EAX) {
844 lval->e_tptr = (curtok == TOK_AX)? type_uint : type_ulong;
845 lval->e_flags = E_MREG;
846 lval->e_test &= ~E_CC;
849 return 1; /* May be used as lvalue */
852 /* Illegal primary. */
853 Error (ERR_EXPR_EXPECTED);
854 lval->e_flags = E_MCONST;
855 lval->e_tptr = type_int;
861 static int arrayref (int k, struct expent* lval)
862 /* Handle an array reference */
876 /* Skip the bracket */
879 /* Get the type of left side */
880 tptr1 = lval->e_tptr;
882 /* We can apply a special treatment for arrays that have a const base
883 * address. This is true for most arrays and will produce a lot better
884 * code. Check if this is a const base address.
886 lflags = lval->e_flags & ~E_MCTYPE;
887 ConstBaseAddr = (lflags == E_MCONST) || /* Constant numeric address */
888 (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
889 lflags == E_MLOCAL; /* Local array */
891 /* If we have a constant base, we delay the address fetch */
892 Mark1 = GetCodePos ();
893 Mark2 = 0; /* Silence gcc */
894 if (!ConstBaseAddr) {
895 /* Get a pointer to the array into the primary */
896 exprhs (CF_NONE, k, lval);
898 /* Get the array pointer on stack. Do not push more than 16
899 * bit, even if this value is greater, since we cannot handle
900 * other than 16bit stuff when doing indexing.
902 Mark2 = GetCodePos ();
906 /* TOS now contains ptr to array elements. Get the subscript. */
908 if (l == 0 && lval2.e_flags == E_MCONST) {
910 /* The array subscript is a constant - remove value from stack */
911 if (!ConstBaseAddr) {
915 /* Get an array pointer into the primary */
916 exprhs (CF_NONE, k, lval);
921 /* Scale the subscript value according to element size */
922 lval2.e_const *= PSizeOf (tptr1);
924 /* Remove code for lhs load */
927 /* Handle constant base array on stack. Be sure NOT to
928 * handle pointers the same way, this won't work.
930 if (IsArray (tptr1) &&
931 ((lval->e_flags & ~E_MCTYPE) == E_MCONST ||
932 (lval->e_flags & ~E_MCTYPE) == E_MLOCAL ||
933 (lval->e_flags & E_MGLOBAL) != 0 ||
934 (lval->e_flags == E_MEOFFS))) {
935 lval->e_const += lval2.e_const;
938 /* Pointer - load into primary and remember offset */
939 if ((lval->e_flags & E_MEXPR) == 0 || k != 0) {
940 exprhs (CF_NONE, k, lval);
942 lval->e_const = lval2.e_const;
943 lval->e_flags = E_MEOFFS;
946 /* Result is of element type */
947 lval->e_tptr = Indirect (tptr1);
952 } else if ((tptr2 = lval2.e_tptr) [0] & T_POINTER) {
953 /* Subscript is pointer, get element type */
954 lval2.e_tptr = Indirect (tptr2);
956 /* Scale the rhs value in the primary register */
957 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
959 lval->e_tptr = lval2.e_tptr;
961 Error (ERR_CANNOT_SUBSCRIPT);
964 /* Add the subscript. Since arrays are indexed by integers,
965 * we will ignore the true type of the subscript here and
968 g_inc (CF_INT | CF_CONST, lval2.e_const);
972 /* Array subscript is not constant. Load it into the primary */
973 Mark2 = GetCodePos ();
974 exprhs (CF_NONE, l, &lval2);
976 tptr2 = lval2.e_tptr;
979 /* Get the element type */
980 lval->e_tptr = Indirect (tptr1);
982 /* Indexing is based on int's, so we will just use the integer
983 * portion of the index (which is in (e)ax, so there's no further
986 g_scale (CF_INT, SizeOf (lval->e_tptr));
988 } else if (IsPtr (tptr2)) {
990 /* Get the element type */
991 lval2.e_tptr = Indirect (tptr2);
993 /* Get the int value on top. If we go here, we're sure,
994 * both values are 16 bit (the first one was truncated
995 * if necessary and the second one is a pointer).
996 * Note: If ConstBaseAddr is true, we don't have a value on
997 * stack, so to "swap" both, just push the subscript.
1001 exprhs (CF_NONE, k, lval);
1008 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
1009 lval->e_tptr = lval2.e_tptr;
1011 Error (ERR_CANNOT_SUBSCRIPT);
1014 /* The offset is now in the primary register. It didn't have a
1015 * constant base address for the lhs, the lhs address is already
1016 * on stack, and we must add the offset. If the base address was
1017 * constant, we call special functions to add the address to the
1020 if (!ConstBaseAddr) {
1021 /* Add the subscript. Both values are int sized. */
1025 /* If the subscript has itself a constant address, it is often
1026 * a better idea to reverse again the order of the evaluation.
1027 * This will generate better code if the subscript is a byte
1028 * sized variable. But beware: This is only possible if the
1029 * subscript was not scaled, that is, if this was a byte array
1032 rflags = lval2.e_flags & ~E_MCTYPE;
1033 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1034 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1035 rflags == E_MLOCAL; /* Local array */
1037 if (ConstSubAddr && SizeOf (lval->e_tptr) == 1) {
1041 /* Reverse the order of evaluation */
1042 unsigned flags = (SizeOf (lval2.e_tptr) == 1)? CF_CHAR : CF_INT;
1045 /* Get a pointer to the array into the primary. We have changed
1046 * e_tptr above but we need the original type to load the
1047 * address, so restore it temporarily.
1049 SavedType = lval->e_tptr;
1050 lval->e_tptr = tptr1;
1051 exprhs (CF_NONE, k, lval);
1052 lval->e_tptr = SavedType;
1054 /* Add the variable */
1055 if (rflags == E_MLOCAL) {
1056 g_addlocal (flags, lval2.e_const);
1058 flags |= GlobalModeFlags (lval2.e_flags);
1059 g_addstatic (flags, lval2.e_name, lval2.e_const);
1062 if (lflags == E_MCONST) {
1063 /* Constant numeric address. Just add it */
1064 g_inc (CF_INT | CF_UNSIGNED, lval->e_const);
1065 } else if (lflags == E_MLOCAL) {
1066 /* Base address is a local variable address */
1067 if (IsArray (tptr1)) {
1068 g_addaddr_local (CF_INT, lval->e_const);
1070 g_addlocal (CF_PTR, lval->e_const);
1073 /* Base address is a static variable address */
1074 unsigned flags = CF_INT;
1075 flags |= GlobalModeFlags (lval->e_flags);
1076 if (IsArray (tptr1)) {
1077 g_addaddr_static (flags, lval->e_name, lval->e_const);
1079 g_addstatic (flags, lval->e_name, lval->e_const);
1085 lval->e_flags = E_MEXPR;
1088 return !IsArray (lval->e_tptr);
1094 static int structref (int k, struct expent* lval)
1095 /* Process struct field after . or ->. */
1101 /* Skip the token and check for an identifier */
1103 if (curtok != TOK_IDENT) {
1104 Error (ERR_IDENT_EXPECTED);
1105 lval->e_tptr = type_int;
1109 /* Get the symbol table entry and check for a struct field */
1110 strcpy (Ident, CurTok.Ident);
1112 Field = FindStructField (lval->e_tptr, Ident);
1114 Error (ERR_STRUCT_FIELD_MISMATCH, Ident);
1115 lval->e_tptr = type_int;
1119 /* If we have constant input data, the result is also constant */
1120 flags = lval->e_flags & ~E_MCTYPE;
1121 if (flags == E_MCONST ||
1122 (k == 0 && (flags == E_MLOCAL ||
1123 (flags & E_MGLOBAL) != 0 ||
1124 lval->e_flags == E_MEOFFS))) {
1125 lval->e_const += Field->V.Offs;
1127 if ((flags & E_MEXPR) == 0 || k != 0) {
1128 exprhs (CF_NONE, k, lval);
1130 lval->e_const = Field->V.Offs;
1131 lval->e_flags = E_MEOFFS;
1133 lval->e_tptr = Field->Type;
1134 return !IsArray (Field->Type);
1139 static int hie11 (struct expent *lval)
1140 /* Handle compound types (structs and arrays) */
1147 if (curtok < TOK_LBRACK || curtok > TOK_PTR_REF) {
1154 if (curtok == TOK_LBRACK) {
1156 /* Array reference */
1157 k = arrayref (k, lval);
1159 } else if (curtok == TOK_LPAREN) {
1161 /* Function call. Skip the opening parenthesis */
1163 tptr = lval->e_tptr;
1164 if (IsFunc (tptr) || IsFuncPtr (tptr)) {
1165 if (IsFuncPtr (tptr)) {
1166 /* Pointer to function. Handle transparently */
1167 exprhs (CF_NONE, k, lval); /* Function pointer to A/X */
1168 ++lval->e_tptr; /* Skip T_PTR */
1169 lval->e_flags |= E_MEXPR;
1171 callfunction (lval);
1172 lval->e_flags = E_MEXPR;
1173 lval->e_tptr += DECODE_SIZE + 1; /* Set to result */
1175 Error (ERR_ILLEGAL_FUNC_CALL);
1179 } else if (curtok == TOK_DOT) {
1181 if (!IsStruct (lval->e_tptr)) {
1182 Error (ERR_STRUCT_EXPECTED);
1184 k = structref (0, lval);
1186 } else if (curtok == TOK_PTR_REF) {
1188 tptr = lval->e_tptr;
1189 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1190 Error (ERR_STRUCT_PTR_EXPECTED);
1192 k = structref (k, lval);
1202 static void store (struct expent* lval)
1203 /* Store primary reg into this reference */
1209 flags = TypeOf (lval->e_tptr);
1210 if (f & E_MGLOBAL) {
1211 flags |= GlobalModeFlags (f);
1218 g_putstatic (flags, lval->e_name, lval->e_const);
1220 } else if (f & E_MLOCAL) {
1221 g_putlocal (flags, lval->e_const);
1222 } else if (f == E_MEOFFS) {
1223 g_putind (flags, lval->e_const);
1224 } else if (f != E_MREG) {
1226 g_putind (flags, 0);
1228 /* Store into absolute address */
1229 g_putstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
1233 /* Assume that each one of the stores will invalidate CC */
1234 lval->e_test &= ~E_CC;
1239 static void pre_incdec (struct expent* lval, void (*inc) (unsigned, unsigned long))
1240 /* Handle --i and ++i */
1247 if ((k = hie10 (lval)) == 0) {
1248 Error (ERR_LVALUE_EXPECTED);
1252 /* Get the data type */
1253 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR | CF_CONST;
1255 /* Get the increment value in bytes */
1256 val = (lval->e_tptr [0] == T_PTR)? PSizeOf (lval->e_tptr) : 1;
1258 /* We're currently only able to handle some adressing modes */
1259 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
1260 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
1261 (lval->e_flags & E_MCONST) == 0 && /* Constant address? */
1262 (lval->e_flags & E_MEXPR) == 0) { /* Address in a/x? */
1264 /* Use generic code. Push the address if needed */
1267 /* Fetch the value */
1268 exprhs (CF_NONE, k, lval);
1270 /* Increment value in primary */
1273 /* Store the result back */
1278 /* Special code for some addressing modes - use the special += ops */
1279 if (lval->e_flags & E_MGLOBAL) {
1280 flags |= GlobalModeFlags (lval->e_flags);
1282 g_addeqstatic (flags, lval->e_name, lval->e_const, val);
1284 g_subeqstatic (flags, lval->e_name, lval->e_const, val);
1286 } else if (lval->e_flags & E_MLOCAL) {
1287 /* ref to localvar */
1289 g_addeqlocal (flags, lval->e_const, val);
1291 g_subeqlocal (flags, lval->e_const, val);
1293 } else if (lval->e_flags & E_MCONST) {
1294 /* ref to absolute address */
1295 flags |= CF_ABSOLUTE;
1297 g_addeqstatic (flags, lval->e_const, 0, val);
1299 g_subeqstatic (flags, lval->e_const, 0, val);
1301 } else if (lval->e_flags & E_MEXPR) {
1302 /* Address in a/x. */
1304 g_addeqind (flags, lval->e_const, val);
1306 g_subeqind (flags, lval->e_const, val);
1309 Internal ("Invalid addressing mode");
1314 /* Result is an expression */
1315 lval->e_flags = E_MEXPR;
1320 static void post_incdec (struct expent *lval, int k, void (*inc) (unsigned, unsigned long))
1321 /* Handle i-- and i++ */
1327 Error (ERR_LVALUE_EXPECTED);
1331 /* Get the data type */
1332 flags = TypeOf (lval->e_tptr);
1334 /* Push the address if needed */
1337 /* Fetch the value and save it (since it's the result of the expression) */
1338 exprhs (CF_NONE, 1, lval);
1339 g_save (flags | CF_FORCECHAR);
1341 /* If we have a pointer expression, increment by the size of the type */
1342 if (lval->e_tptr[0] == T_PTR) {
1343 inc (flags | CF_CONST | CF_FORCECHAR, SizeOf (lval->e_tptr + 1));
1345 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1348 /* Store the result back */
1351 /* Restore the original value */
1352 g_restore (flags | CF_FORCECHAR);
1353 lval->e_flags = E_MEXPR;
1358 static void unaryop (int tok, struct expent* lval)
1359 /* Handle unary -/+ and ~ */
1366 if (k == 0 && lval->e_flags & E_MCONST) {
1367 /* Value is constant */
1369 case TOK_MINUS: lval->e_const = -lval->e_const; break;
1370 case TOK_PLUS: break;
1371 case TOK_COMP: lval->e_const = ~lval->e_const; break;
1372 default: Internal ("Unexpected token: %d", tok);
1375 /* Value is not constant */
1376 exprhs (CF_NONE, k, lval);
1378 /* Get the type of the expression */
1379 flags = TypeOf (lval->e_tptr);
1381 /* Handle the operation */
1383 case TOK_MINUS: g_neg (flags); break;
1384 case TOK_PLUS: break;
1385 case TOK_COMP: g_com (flags); break;
1386 default: Internal ("Unexpected token: %d", tok);
1388 lval->e_flags = E_MEXPR;
1394 static int typecast (struct expent* lval)
1395 /* Handle an explicit cast */
1398 type Type[MAXTYPELEN];
1401 /* Skip the left paren */
1410 /* Read the expression we have to cast */
1413 /* Get the type of the expression and honor constant values */
1414 rflags = TypeOf (lval->e_tptr);
1415 if (lval->e_flags & E_MCONST) {
1419 /* Do the actual cast. Special handling for void casts */
1420 if (!IsVoid (Type)) {
1421 /* Mark the lhs as const to avoid a manipulation of TOS */
1422 g_typecast (TypeOf (Type) | CF_CONST, rflags);
1425 /* Use the new type */
1426 lval->e_tptr = TypeDup (Type);
1434 static int hie10 (struct expent* lval)
1435 /* Handle ++, --, !, unary - etc. */
1443 pre_incdec (lval, g_inc);
1447 pre_incdec (lval, g_dec);
1453 unaryop (curtok, lval);
1458 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1459 /* Constant expression */
1460 lval->e_const = !lval->e_const;
1462 g_bneg (TypeOf (lval->e_tptr));
1463 lval->e_test |= E_CC; /* bneg will set cc */
1464 lval->e_flags = E_MEXPR; /* say it's an expr */
1466 return 0; /* expr not storable */
1470 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1471 /* Expression is not const, indirect value loaded into primary */
1472 lval->e_flags = E_MEXPR;
1473 lval->e_const = 0; /* Offset is zero now */
1477 lval->e_tptr = Indirect (t);
1479 Error (ERR_ILLEGAL_INDIRECT);
1487 /* Allow the & operator with an array */
1488 if (!IsArray (lval->e_tptr)) {
1489 Error (ERR_ILLEGAL_ADDRESS);
1492 t = TypeAlloc (TypeLen (lval->e_tptr) + 2);
1494 TypeCpy (t + 1, lval->e_tptr);
1501 if (istypeexpr ()) {
1502 type Type[MAXTYPELEN];
1504 lval->e_const = SizeOf (ParseType (Type));
1507 /* Remember the output queue pointer */
1508 CodeMark Mark = GetCodePos ();
1510 lval->e_const = SizeOf (lval->e_tptr);
1511 /* Remove any generated code */
1514 lval->e_flags = E_MCONST | E_TCONST;
1515 lval->e_tptr = type_uint;
1516 lval->e_test &= ~E_CC;
1520 if (istypeexpr ()) {
1522 return typecast (lval);
1529 post_incdec (lval, k, g_inc);
1533 post_incdec (lval, k, g_dec);
1543 static int hie_internal (GenDesc** ops, /* List of generators */
1544 struct expent* lval, /* parent expr's lval */
1545 int (*hienext) (struct expent*),
1546 int* UsedGen) /* next higher level */
1547 /* Helper function */
1550 struct expent lval2;
1554 token_t tok; /* The operator token */
1555 unsigned ltype, type;
1556 int rconst; /* Operand is a constant */
1562 while ((Gen = FindGen (curtok, ops)) != 0) {
1564 /* Tell the caller that we handled it's ops */
1567 /* All operators that call this function expect an int on the lhs */
1568 if (!IsInt (lval->e_tptr)) {
1569 Error (ERR_INT_EXPR_EXPECTED);
1572 /* Remember the operator token, then skip it */
1576 /* Get the lhs on stack */
1577 Mark1 = GetCodePos ();
1578 ltype = TypeOf (lval->e_tptr);
1579 if (k == 0 && lval->e_flags == E_MCONST) {
1580 /* Constant value */
1581 Mark2 = GetCodePos ();
1582 g_push (ltype | CF_CONST, lval->e_const);
1584 /* Value not constant */
1585 exprhs (CF_NONE, k, lval);
1586 Mark2 = GetCodePos ();
1590 /* Get the right hand side */
1591 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1593 /* Check the type of the rhs */
1594 if (!IsInt (lval2.e_tptr)) {
1595 Error (ERR_INT_EXPR_EXPECTED);
1598 /* Check for const operands */
1599 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1601 /* Both operands are constant, remove the generated code */
1605 /* Evaluate the result */
1606 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1608 /* Get the type of the result */
1609 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1613 /* If the right hand side is constant, and the generator function
1614 * expects the lhs in the primary, remove the push of the primary
1617 unsigned rtype = TypeOf (lval2.e_tptr);
1620 /* Second value is constant - check for div */
1623 if (tok == TOK_DIV && lval2.e_const == 0) {
1624 Error (ERR_DIV_BY_ZERO);
1625 } else if (tok == TOK_MOD && lval2.e_const == 0) {
1626 Error (ERR_MOD_BY_ZERO);
1628 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1631 ltype |= CF_REG; /* Value is in register */
1635 /* Determine the type of the operation result. */
1636 type |= g_typeadjust (ltype, rtype);
1637 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1640 Gen->Func (type, lval2.e_const);
1641 lval->e_flags = E_MEXPR;
1644 /* We have a rvalue now */
1653 static int hie_compare (GenDesc** ops, /* List of generators */
1654 struct expent* lval, /* parent expr's lval */
1655 int (*hienext) (struct expent*))
1656 /* Helper function for the compare operators */
1659 struct expent lval2;
1663 token_t tok; /* The operator token */
1665 int rconst; /* Operand is a constant */
1670 while ((Gen = FindGen (curtok, ops)) != 0) {
1672 /* Remember the operator token, then skip it */
1676 /* Get the lhs on stack */
1677 Mark1 = GetCodePos ();
1678 ltype = TypeOf (lval->e_tptr);
1679 if (k == 0 && lval->e_flags == E_MCONST) {
1680 /* Constant value */
1681 Mark2 = GetCodePos ();
1682 g_push (ltype | CF_CONST, lval->e_const);
1684 /* Value not constant */
1685 exprhs (CF_NONE, k, lval);
1686 Mark2 = GetCodePos ();
1690 /* Get the right hand side */
1691 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1693 /* Make sure, the types are compatible */
1694 if (IsInt (lval->e_tptr)) {
1695 if (!IsInt (lval2.e_tptr) && !(IsPtr(lval2.e_tptr) && IsNullPtr(lval))) {
1696 Error (ERR_INCOMPATIBLE_TYPES);
1698 } else if (IsPtr (lval->e_tptr)) {
1699 if (IsPtr (lval2.e_tptr)) {
1700 /* Both pointers are allowed in comparison if they point to
1701 * the same type, or if one of them is a void pointer.
1703 type* left = Indirect (lval->e_tptr);
1704 type* right = Indirect (lval2.e_tptr);
1705 if (!EqualTypes (left, right) && *left != T_VOID && *right != T_VOID) {
1706 /* Incomatible pointers */
1707 Error (ERR_INCOMPATIBLE_TYPES);
1709 } else if (!IsNullPtr (&lval2)) {
1710 Error (ERR_INCOMPATIBLE_TYPES);
1714 /* Check for const operands */
1715 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1717 /* Both operands are constant, remove the generated code */
1721 /* Evaluate the result */
1722 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1726 /* If the right hand side is constant, and the generator function
1727 * expects the lhs in the primary, remove the push of the primary
1733 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1736 ltype |= CF_REG; /* Value is in register */
1740 /* Determine the type of the operation result. If the left
1741 * operand is of type char and the right is a constant, or
1742 * if both operands are of type char, we will encode the
1743 * operation as char operation. Otherwise the default
1744 * promotions are used.
1746 if (IsChar (lval->e_tptr) && (IsChar (lval2.e_tptr) || rconst)) {
1748 if (IsUnsigned (lval->e_tptr) || IsUnsigned (lval2.e_tptr)) {
1749 flags |= CF_UNSIGNED;
1752 flags |= CF_FORCECHAR;
1755 unsigned rtype = TypeOf (lval2.e_tptr) | (flags & CF_CONST);
1756 flags |= g_typeadjust (ltype, rtype);
1760 Gen->Func (flags, lval2.e_const);
1761 lval->e_flags = E_MEXPR;
1764 /* Result type is always int */
1765 lval->e_tptr = type_int;
1767 /* We have a rvalue now, condition codes are set */
1769 lval->e_test |= E_CC;
1777 static int hie9 (struct expent *lval)
1778 /* Process * and / operators. */
1780 static GenDesc* hie9_ops [] = {
1781 &GenMUL, &GenDIV, &GenMOD, 0
1785 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1790 static void parseadd (int k, struct expent* lval)
1791 /* Parse an expression with the binary plus operator. lval contains the
1792 * unprocessed left hand side of the expression and will contain the
1793 * result of the expression on return.
1796 struct expent lval2;
1797 unsigned flags; /* Operation flags */
1798 CodeMark Mark; /* Remember code position */
1799 type* lhst; /* Type of left hand side */
1800 type* rhst; /* Type of right hand side */
1803 /* Skip the PLUS token */
1806 /* Get the left hand side type, initialize operation flags */
1807 lhst = lval->e_tptr;
1810 /* Check for constness on both sides */
1811 if (k == 0 && lval->e_flags == E_MCONST) {
1813 /* The left hand side is a constant. Good. Get rhs */
1814 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1816 /* Right hand side is also constant. Get the rhs type */
1817 rhst = lval2.e_tptr;
1819 /* Both expressions are constants. Check for pointer arithmetic */
1820 if (IsPtr (lhst) && IsInt (rhst)) {
1821 /* Left is pointer, right is int, must scale rhs */
1822 lval->e_const = lval->e_const + lval2.e_const * PSizeOf (lhst);
1823 /* Result type is a pointer */
1824 } else if (IsInt (lhst) && IsPtr (rhst)) {
1825 /* Left is int, right is pointer, must scale lhs */
1826 lval->e_const = lval->e_const * PSizeOf (rhst) + lval2.e_const;
1827 /* Result type is a pointer */
1828 lval->e_tptr = lval2.e_tptr;
1829 } else if (IsInt (lhst) && IsInt (rhst)) {
1830 /* Integer addition */
1831 lval->e_const += lval2.e_const;
1832 typeadjust (lval, &lval2, 1);
1835 Error (ERR_OP_NOT_ALLOWED);
1838 /* Result is constant, condition codes not set */
1839 lval->e_test = E_MCONST;
1843 /* lhs is constant, rhs is not. Get the rhs type. */
1844 rhst = lval2.e_tptr;
1846 /* Check for pointer arithmetic */
1847 if (IsPtr (lhst) && IsInt (rhst)) {
1848 /* Left is pointer, right is int, must scale rhs */
1849 g_scale (CF_INT, PSizeOf (lhst));
1850 /* Operate on pointers, result type is a pointer */
1852 } else if (IsInt (lhst) && IsPtr (rhst)) {
1853 /* Left is int, right is pointer, must scale lhs */
1854 lval->e_const *= PSizeOf (rhst);
1855 /* Operate on pointers, result type is a pointer */
1857 lval->e_tptr = lval2.e_tptr;
1858 } else if (IsInt (lhst) && IsInt (rhst)) {
1859 /* Integer addition */
1860 flags = typeadjust (lval, &lval2, 1);
1863 Error (ERR_OP_NOT_ALLOWED);
1866 /* Generate code for the add */
1867 g_inc (flags | CF_CONST, lval->e_const);
1869 /* Result is in primary register */
1870 lval->e_flags = E_MEXPR;
1871 lval->e_test &= ~E_CC;
1877 /* Left hand side is not constant. Get the value onto the stack. */
1878 exprhs (CF_NONE, k, lval); /* --> primary register */
1879 Mark = GetCodePos ();
1880 g_push (TypeOf (lval->e_tptr), 0); /* --> stack */
1882 /* Evaluate the rhs */
1883 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1885 /* Right hand side is a constant. Get the rhs type */
1886 rhst = lval2.e_tptr;
1888 /* Remove pushed value from stack */
1890 pop (TypeOf (lval->e_tptr));
1892 /* Check for pointer arithmetic */
1893 if (IsPtr (lhst) && IsInt (rhst)) {
1894 /* Left is pointer, right is int, must scale rhs */
1895 lval2.e_const *= PSizeOf (lhst);
1896 /* Operate on pointers, result type is a pointer */
1898 } else if (IsInt (lhst) && IsPtr (rhst)) {
1899 /* Left is int, right is pointer, must scale lhs (ptr only) */
1900 g_scale (CF_INT | CF_CONST, PSizeOf (rhst));
1901 /* Operate on pointers, result type is a pointer */
1903 lval->e_tptr = lval2.e_tptr;
1904 } else if (IsInt (lhst) && IsInt (rhst)) {
1905 /* Integer addition */
1906 flags = typeadjust (lval, &lval2, 1);
1909 Error (ERR_OP_NOT_ALLOWED);
1912 /* Generate code for the add */
1913 g_inc (flags | CF_CONST, lval2.e_const);
1915 /* Result is in primary register */
1916 lval->e_flags = E_MEXPR;
1917 lval->e_test &= ~E_CC;
1921 /* lhs and rhs are not constant. Get the rhs type. */
1922 rhst = lval2.e_tptr;
1924 /* Check for pointer arithmetic */
1925 if (IsPtr (lhst) && IsInt (rhst)) {
1926 /* Left is pointer, right is int, must scale rhs */
1927 g_scale (CF_INT, PSizeOf (lhst));
1928 /* Operate on pointers, result type is a pointer */
1930 } else if (IsInt (lhst) && IsPtr (rhst)) {
1931 /* Left is int, right is pointer, must scale lhs */
1932 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
1933 g_swap (CF_INT); /* Swap TOS and primary */
1934 g_scale (CF_INT, PSizeOf (rhst));
1935 /* Operate on pointers, result type is a pointer */
1937 lval->e_tptr = lval2.e_tptr;
1938 } else if (IsInt (lhst) && IsInt (rhst)) {
1939 /* Integer addition */
1940 flags = typeadjust (lval, &lval2, 0);
1943 Error (ERR_OP_NOT_ALLOWED);
1946 /* Generate code for the add */
1949 /* Result is in primary register */
1950 lval->e_flags = E_MEXPR;
1951 lval->e_test &= ~E_CC;
1960 static void parsesub (int k, struct expent* lval)
1961 /* Parse an expression with the binary minus operator. lval contains the
1962 * unprocessed left hand side of the expression and will contain the
1963 * result of the expression on return.
1966 struct expent lval2;
1967 unsigned flags; /* Operation flags */
1968 type* lhst; /* Type of left hand side */
1969 type* rhst; /* Type of right hand side */
1970 CodeMark Mark1; /* Save position of output queue */
1971 CodeMark Mark2; /* Another position in the queue */
1972 int rscale; /* Scale factor for the result */
1975 /* Skip the MINUS token */
1978 /* Get the left hand side type, initialize operation flags */
1979 lhst = lval->e_tptr;
1981 rscale = 1; /* Scale by 1, that is, don't scale */
1983 /* Remember the output queue position, then bring the value onto the stack */
1984 Mark1 = GetCodePos ();
1985 exprhs (CF_NONE, k, lval); /* --> primary register */
1986 Mark2 = GetCodePos ();
1987 g_push (TypeOf (lhst), 0); /* --> stack */
1989 /* Parse the right hand side */
1990 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1992 /* The right hand side is constant. Get the rhs type. */
1993 rhst = lval2.e_tptr;
1995 /* Check left hand side */
1996 if (k == 0 && lval->e_flags & E_MCONST) {
1998 /* Both sides are constant, remove generated code */
2000 pop (TypeOf (lhst)); /* Clean up the stack */
2002 /* Check for pointer arithmetic */
2003 if (IsPtr (lhst) && IsInt (rhst)) {
2004 /* Left is pointer, right is int, must scale rhs */
2005 lval->e_const -= lval2.e_const * PSizeOf (lhst);
2006 /* Operate on pointers, result type is a pointer */
2007 } else if (IsPtr (lhst) && IsPtr (rhst)) {
2008 /* Left is pointer, right is pointer, must scale result */
2009 if (TypeCmp (Indirect (lhst), Indirect (rhst)) != 0) {
2010 Error (ERR_INCOMPATIBLE_POINTERS);
2012 lval->e_const = (lval->e_const - lval2.e_const) / PSizeOf (lhst);
2014 /* Operate on pointers, result type is an integer */
2015 lval->e_tptr = type_int;
2016 } else if (IsInt (lhst) && IsInt (rhst)) {
2017 /* Integer subtraction */
2018 typeadjust (lval, &lval2, 1);
2019 lval->e_const -= lval2.e_const;
2022 Error (ERR_OP_NOT_ALLOWED);
2025 /* Result is constant, condition codes not set */
2026 lval->e_flags = E_MCONST;
2027 lval->e_test &= ~E_CC;
2031 /* Left hand side is not constant, right hand side is.
2032 * Remove pushed value from stack.
2035 pop (TypeOf (lhst));
2037 if (IsPtr (lhst) && IsInt (rhst)) {
2038 /* Left is pointer, right is int, must scale rhs */
2039 lval2.e_const *= PSizeOf (lhst);
2040 /* Operate on pointers, result type is a pointer */
2042 } else if (IsPtr (lhst) && IsPtr (rhst)) {
2043 /* Left is pointer, right is pointer, must scale result */
2044 if (TypeCmp (Indirect (lhst), Indirect (rhst)) != 0) {
2045 Error (ERR_INCOMPATIBLE_POINTERS);
2047 rscale = PSizeOf (lhst);
2049 /* Operate on pointers, result type is an integer */
2051 lval->e_tptr = type_int;
2052 } else if (IsInt (lhst) && IsInt (rhst)) {
2053 /* Integer subtraction */
2054 flags = typeadjust (lval, &lval2, 1);
2057 Error (ERR_OP_NOT_ALLOWED);
2060 /* Do the subtraction */
2061 g_dec (flags | CF_CONST, lval2.e_const);
2063 /* If this was a pointer subtraction, we must scale the result */
2065 g_scale (flags, -rscale);
2068 /* Result is in primary register */
2069 lval->e_flags = E_MEXPR;
2070 lval->e_test &= ~E_CC;
2076 /* Right hand side is not constant. Get the rhs type. */
2077 rhst = lval2.e_tptr;
2079 /* Check for pointer arithmetic */
2080 if (IsPtr (lhst) && IsInt (rhst)) {
2081 /* Left is pointer, right is int, must scale rhs */
2082 g_scale (CF_INT, PSizeOf (lhst));
2083 /* Operate on pointers, result type is a pointer */
2085 } else if (IsPtr (lhst) && IsPtr (rhst)) {
2086 /* Left is pointer, right is pointer, must scale result */
2087 if (TypeCmp (Indirect (lhst), Indirect (rhst)) != 0) {
2088 Error (ERR_INCOMPATIBLE_POINTERS);
2090 rscale = PSizeOf (lhst);
2092 /* Operate on pointers, result type is an integer */
2094 lval->e_tptr = type_int;
2095 } else if (IsInt (lhst) && IsInt (rhst)) {
2096 /* Integer subtraction. If the left hand side descriptor says that
2097 * the lhs is const, we have to remove this mark, since this is no
2098 * longer true, lhs is on stack instead.
2100 if (lval->e_flags == E_MCONST) {
2101 lval->e_flags = E_MEXPR;
2103 /* Adjust operand types */
2104 flags = typeadjust (lval, &lval2, 0);
2107 Error (ERR_OP_NOT_ALLOWED);
2110 /* Generate code for the sub (the & is a hack here) */
2111 g_sub (flags & ~CF_CONST, 0);
2113 /* If this was a pointer subtraction, we must scale the result */
2115 g_scale (flags, -rscale);
2118 /* Result is in primary register */
2119 lval->e_flags = E_MEXPR;
2120 lval->e_test &= ~E_CC;
2126 static int hie8 (struct expent* lval)
2127 /* Process + and - binary operators. */
2129 int k = hie9 (lval);
2130 while (curtok == TOK_PLUS || curtok == TOK_MINUS) {
2132 if (curtok == TOK_PLUS) {
2145 static int hie7 (struct expent *lval)
2146 /* Parse << and >>. */
2148 static GenDesc* hie7_ops [] = {
2153 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2158 static int hie6 (struct expent *lval)
2159 /* process greater-than type comparators */
2161 static GenDesc* hie6_ops [] = {
2162 &GenLT, &GenLE, &GenGE, &GenGT, 0
2164 return hie_compare (hie6_ops, lval, hie7);
2169 static int hie5 (struct expent *lval)
2171 static GenDesc* hie5_ops[] = {
2174 return hie_compare (hie5_ops, lval, hie6);
2179 static int hie4 (struct expent* lval)
2180 /* Handle & (bitwise and) */
2182 static GenDesc* hie4_ops [] = {
2187 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2192 static int hie3 (struct expent *lval)
2193 /* Handle ^ (bitwise exclusive or) */
2195 static GenDesc* hie3_ops [] = {
2200 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2205 static int hie2 (struct expent *lval)
2206 /* Handle | (bitwise or) */
2208 static GenDesc* hie2_ops [] = {
2213 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2218 static int hieAnd (struct expent* lval, unsigned TrueLab, int* BoolOp)
2219 /* Process "exp && exp" */
2223 struct expent lval2;
2226 if (curtok == TOK_BOOL_AND) {
2228 /* Tell our caller that we're evaluating a boolean */
2231 /* Get a label that we will use for false expressions */
2234 /* If the expr hasn't set condition codes, set the force-test flag */
2235 if ((lval->e_test & E_CC) == 0) {
2236 lval->e_test |= E_FORCETEST;
2239 /* Load the value */
2240 exprhs (CF_FORCECHAR, k, lval);
2242 /* Generate the jump */
2243 g_falsejump (CF_NONE, lab);
2245 /* Parse more boolean and's */
2246 while (curtok == TOK_BOOL_AND) {
2253 if ((lval2.e_test & E_CC) == 0) {
2254 lval2.e_test |= E_FORCETEST;
2256 exprhs (CF_FORCECHAR, k, &lval2);
2258 /* Do short circuit evaluation */
2259 if (curtok == TOK_BOOL_AND) {
2260 g_falsejump (CF_NONE, lab);
2262 /* Last expression - will evaluate to true */
2263 g_truejump (CF_NONE, TrueLab);
2267 /* Define the false jump label here */
2268 g_defloclabel (lab);
2270 /* Define the label */
2271 lval->e_flags = E_MEXPR;
2272 lval->e_test |= E_CC; /* Condition codes are set */
2280 static int hieOr (struct expent *lval)
2281 /* Process "exp || exp". */
2284 struct expent lval2;
2285 int BoolOp = 0; /* Did we have a boolean op? */
2286 int AndOp; /* Did we have a && operation? */
2287 unsigned TrueLab; /* Jump to this label if true */
2291 TrueLab = GetLabel ();
2293 /* Call the next level parser */
2294 k = hieAnd (lval, TrueLab, &BoolOp);
2296 /* Any boolean or's? */
2297 if (curtok == TOK_BOOL_OR) {
2299 /* If the expr hasn't set condition codes, set the force-test flag */
2300 if ((lval->e_test & E_CC) == 0) {
2301 lval->e_test |= E_FORCETEST;
2304 /* Get first expr */
2305 exprhs (CF_FORCECHAR, k, lval);
2307 /* For each expression jump to TrueLab if true. Beware: If we
2308 * had && operators, the jump is already in place!
2311 g_truejump (CF_NONE, TrueLab);
2314 /* Remember that we had a boolean op */
2317 /* while there's more expr */
2318 while (curtok == TOK_BOOL_OR) {
2325 k = hieAnd (&lval2, TrueLab, &AndOp);
2326 if ((lval2.e_test & E_CC) == 0) {
2327 lval2.e_test |= E_FORCETEST;
2329 exprhs (CF_FORCECHAR, k, &lval2);
2331 /* If there is more to come, add shortcut boolean eval.
2332 * Beware: If we had && operators, the jump is already
2336 /* Seems this sometimes generates wrong code */
2337 if (curtok == TOK_BOOL_OR && !AndOp) {
2338 g_truejump (CF_NONE, TrueLab);
2341 g_truejump (CF_NONE, TrueLab);
2344 lval->e_flags = E_MEXPR;
2345 lval->e_test |= E_CC; /* Condition codes are set */
2349 /* If we really had boolean ops, generate the end sequence */
2351 DoneLab = GetLabel ();
2352 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2353 g_falsejump (CF_NONE, DoneLab);
2354 g_defloclabel (TrueLab);
2355 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2356 g_defloclabel (DoneLab);
2363 static int hieQuest (struct expent *lval)
2364 /* Parse "lvalue ? exp : exp" */
2369 struct expent lval2; /* Expression 2 */
2370 struct expent lval3; /* Expression 3 */
2371 type* type2; /* Type of expression 2 */
2372 type* type3; /* Type of expression 3 */
2373 type* rtype; /* Type of result */
2374 CodeMark Mark1; /* Save position in output code */
2375 CodeMark Mark2; /* Save position in output code */
2380 if (curtok == TOK_QUEST) {
2382 if ((lval->e_test & E_CC) == 0) {
2383 /* Condition codes not set, force a test */
2384 lval->e_test |= E_FORCETEST;
2386 exprhs (CF_NONE, k, lval);
2388 g_falsejump (CF_NONE, labf);
2390 /* Parse second and third expression */
2391 expression1 (&lval2);
2395 g_defloclabel (labf);
2396 expression1 (&lval3);
2398 /* Check if any conversions are needed, if so, do them.
2399 * Conversion rules for ?: expression are:
2400 * - if both expressions are int expressions, default promotion
2401 * rules for ints apply.
2402 * - if both expressions are pointers of the same type, the
2403 * result of the expression is of this type.
2404 * - if one of the expressions is a pointer and the other is
2405 * a zero constant, the resulting type is that of the pointer
2407 * - all other cases are flagged by an error.
2409 type2 = lval2.e_tptr;
2410 type3 = lval3.e_tptr;
2411 if (IsInt (type2) && IsInt (type3)) {
2413 /* Get common type */
2414 rtype = promoteint (type2, type3);
2416 /* Convert the third expression to this type if needed */
2417 g_typecast (TypeOf (rtype), TypeOf (type3));
2419 /* Setup a new label so that the expr3 code will jump around
2420 * the type cast code for expr2.
2422 labf = GetLabel (); /* Get new label */
2423 Mark1 = GetCodePos (); /* Remember current position */
2424 g_jump (labf); /* Jump around code */
2426 /* The jump for expr2 goes here */
2427 g_defloclabel (labt);
2429 /* Create the typecast code for expr2 */
2430 Mark2 = GetCodePos (); /* Remember position */
2431 g_typecast (TypeOf (rtype), TypeOf (type2));
2433 /* If the typecast did not produce code, remove the jump,
2434 * otherwise output the label.
2436 if (GetCodePos() == Mark2) {
2437 RemoveCode (Mark1); /* Remove code */
2439 /* We have typecast code, output label */
2440 g_defloclabel (labf);
2441 labt = 0; /* Mark other label as invalid */
2444 } else if (IsPtr (type2) && IsPtr (type3)) {
2445 /* Must point to same type */
2446 if (TypeCmp (Indirect (type2), Indirect (type3)) != 0) {
2447 Error (ERR_INCOMPATIBLE_TYPES);
2449 /* Result has the common type */
2450 rtype = lval2.e_tptr;
2451 } else if (IsPtr (type2) && IsNullPtr (&lval3)) {
2452 /* Result type is pointer, no cast needed */
2453 rtype = lval2.e_tptr;
2454 } else if (IsNullPtr (&lval2) && IsPtr (type3)) {
2455 /* Result type is pointer, no cast needed */
2456 rtype = lval3.e_tptr;
2458 Error (ERR_INCOMPATIBLE_TYPES);
2459 rtype = lval2.e_tptr; /* Doesn't matter here */
2462 /* If we don't have the label defined until now, do it */
2464 g_defloclabel (labt);
2467 /* Setup the target expression */
2468 lval->e_flags = E_MEXPR;
2469 lval->e_tptr = rtype;
2477 static void opeq (GenDesc* Gen, struct expent *lval, int k)
2478 /* Process "op=" operators. */
2480 struct expent lval2;
2487 Error (ERR_LVALUE_EXPECTED);
2491 /* Determine the type of the lhs */
2492 flags = TypeOf (lval->e_tptr);
2493 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2494 lval->e_tptr [0] == T_PTR;
2496 /* Get the lhs address on stack (if needed) */
2499 /* Fetch the lhs into the primary register if needed */
2500 exprhs (CF_NONE, k, lval);
2502 /* Bring the lhs on stack */
2503 Mark = GetCodePos ();
2506 /* Evaluate the rhs */
2507 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2508 /* The resulting value is a constant. If the generator has the NOPUSH
2509 * flag set, don't push the lhs.
2511 if (Gen->Flags & GEN_NOPUSH) {
2516 /* lhs is a pointer, scale rhs */
2517 lval2.e_const *= SizeOf (lval->e_tptr+1);
2520 /* If the lhs is character sized, the operation may be later done
2523 if (SizeOf (lval->e_tptr) == 1) {
2524 flags |= CF_FORCECHAR;
2527 /* Special handling for add and sub - some sort of a hack, but short code */
2528 if (Gen->Func == g_add) {
2529 g_inc (flags | CF_CONST, lval2.e_const);
2530 } else if (Gen->Func == g_sub) {
2531 g_dec (flags | CF_CONST, lval2.e_const);
2533 Gen->Func (flags | CF_CONST, lval2.e_const);
2536 /* rhs is not constant and already in the primary register */
2538 /* lhs is a pointer, scale rhs */
2539 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2542 /* If the lhs is character sized, the operation may be later done
2545 if (SizeOf (lval->e_tptr) == 1) {
2546 flags |= CF_FORCECHAR;
2549 /* Adjust the types of the operands if needed */
2550 Gen->Func (g_typeadjust (flags, TypeOf (lval2.e_tptr)), 0);
2553 lval->e_flags = E_MEXPR;
2558 static void addsubeq (GenDesc* Gen, struct expent *lval, int k)
2559 /* Process the += and -= operators */
2561 struct expent lval2;
2567 Error (ERR_LVALUE_EXPECTED);
2572 /* We're currently only able to handle some adressing modes */
2573 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
2574 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
2575 (lval->e_flags & E_MCONST) == 0) { /* Constant address? */
2576 /* Use generic routine */
2577 opeq (Gen, lval, k);
2581 /* Skip the operator */
2584 /* Check if we have a pointer expression and must scale rhs */
2585 MustScale = (lval->e_tptr [0] == T_PTR);
2587 /* Determine the code generator flags */
2588 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR;
2590 /* Evaluate the rhs */
2591 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2592 /* The resulting value is a constant. */
2594 /* lhs is a pointer, scale rhs */
2595 lval2.e_const *= SizeOf (lval->e_tptr+1);
2599 /* rhs is not constant and already in the primary register */
2601 /* lhs is a pointer, scale rhs */
2602 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2606 /* Adjust the rhs to the lhs */
2607 g_typeadjust (flags, TypeOf (lval2.e_tptr));
2609 /* Output apropriate code */
2610 if (lval->e_flags & E_MGLOBAL) {
2611 /* Static variable */
2612 flags |= GlobalModeFlags (lval->e_flags);
2613 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2614 g_addeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2616 g_subeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2618 } else if (lval->e_flags & E_MLOCAL) {
2619 /* ref to localvar */
2620 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2621 g_addeqlocal (flags, lval->e_const, lval2.e_const);
2623 g_subeqlocal (flags, lval->e_const, lval2.e_const);
2625 } else if (lval->e_flags & E_MCONST) {
2626 /* ref to absolute address */
2627 flags |= CF_ABSOLUTE;
2628 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2629 g_addeqstatic (flags, lval->e_const, 0, lval2.e_const);
2631 g_subeqstatic (flags, lval->e_const, 0, lval2.e_const);
2633 } else if (lval->e_flags & E_MEXPR) {
2634 /* Address in a/x. */
2635 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2636 g_addeqind (flags, lval->e_const, lval2.e_const);
2638 g_subeqind (flags, lval->e_const, lval2.e_const);
2641 Internal ("Invalid addressing mode");
2644 /* Expression is in the primary now */
2645 lval->e_flags = E_MEXPR;
2650 static void Assignment (struct expent* lval)
2651 /* Parse an assignment */
2654 struct expent lval2;
2656 type* ltype = lval->e_tptr;
2658 /* cc65 does not have full support for handling structs by value. Since
2659 * assigning structs is one of the more useful operations from this
2660 * familiy, allow it here.
2662 if (IsStruct (ltype)) {
2664 /* Bring the address of the lhs into the primary and push it */
2665 exprhs (0, 0, lval);
2666 g_push (CF_PTR | CF_UNSIGNED, 0);
2668 /* Get the expression on the right of the '=' into the primary */
2671 /* Get the address */
2672 exprhs (0, 0, &lval2);
2674 /* We need an lvalue */
2675 Error (ERR_LVALUE_EXPECTED);
2678 /* Push the address (or whatever is in ax in case of errors) */
2679 g_push (CF_PTR | CF_UNSIGNED, 0);
2681 /* Check for equality of the structs */
2682 if (!EqualTypes (ltype, lval2.e_tptr)) {
2683 Error (ERR_INCOMPATIBLE_TYPES);
2686 /* Load the size of the struct into the primary */
2687 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, SizeOf (ltype), 0);
2689 /* Call the memcpy function */
2690 g_call (CF_FIXARGC, "memcpy", 4);
2694 /* Get the address on stack if needed */
2697 /* No struct, setup flags for the load */
2698 flags = SizeOf (ltype) == 1? CF_FORCECHAR : CF_NONE;
2700 /* Get the expression on the right of the '=' into the primary */
2701 if (evalexpr (flags, hie1, &lval2) == 0) {
2702 /* Constant expression. Adjust the types */
2703 assignadjust (ltype, &lval2);
2704 /* Put the value into the primary register */
2705 lconst (flags, &lval2);
2707 /* Expression is not constant and already in the primary */
2708 assignadjust (ltype, &lval2);
2711 /* Generate a store instruction */
2716 /* Value is still in primary */
2717 lval->e_flags = E_MEXPR;
2722 int hie1 (struct expent* lval)
2723 /* Parse first level of expression hierarchy. */
2727 k = hieQuest (lval);
2737 Error (ERR_LVALUE_EXPECTED);
2743 case TOK_PLUS_ASSIGN:
2744 addsubeq (&GenPASGN, lval, k);
2747 case TOK_MINUS_ASSIGN:
2748 addsubeq (&GenSASGN, lval, k);
2751 case TOK_MUL_ASSIGN:
2752 opeq (&GenMASGN, lval, k);
2755 case TOK_DIV_ASSIGN:
2756 opeq (&GenDASGN, lval, k);
2759 case TOK_MOD_ASSIGN:
2760 opeq (&GenMOASGN, lval, k);
2763 case TOK_SHL_ASSIGN:
2764 opeq (&GenSLASGN, lval, k);
2767 case TOK_SHR_ASSIGN:
2768 opeq (&GenSRASGN, lval, k);
2771 case TOK_AND_ASSIGN:
2772 opeq (&GenAASGN, lval, k);
2775 case TOK_XOR_ASSIGN:
2776 opeq (&GenXOASGN, lval, k);
2780 opeq (&GenOASGN, lval, k);
2791 int hie0 (struct expent *lval)
2792 /* Parse comma operator. */
2797 while (curtok == TOK_COMMA) {
2806 int evalexpr (unsigned flags, int (*f) (struct expent*), struct expent* lval)
2807 /* Will evaluate an expression via the given function. If the result is a
2808 * constant, 0 is returned and the value is put in the lval struct. If the
2809 * result is not constant, exprhs is called to bring the value into the
2810 * primary register and 1 is returned.
2817 if (k == 0 && lval->e_flags == E_MCONST) {
2818 /* Constant expression */
2821 /* Not constant, load into the primary */
2822 exprhs (flags, k, lval);
2829 int expr (int (*func) (), struct expent *lval)
2830 /* Expression parser; func is either hie0 or hie1. */
2839 /* Do some checks if code generation is still constistent */
2840 if (savsp != oursp) {
2842 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
2844 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
2852 void expression1 (struct expent* lval)
2853 /* Evaluate an expression on level 1 (no comma operator) and put it into
2854 * the primary register
2857 memset (lval, 0, sizeof (*lval));
2858 exprhs (CF_NONE, expr (hie1, lval), lval);
2863 void expression (struct expent* lval)
2864 /* Evaluate an expression and put it into the primary register */
2866 memset (lval, 0, sizeof (*lval));
2867 exprhs (CF_NONE, expr (hie0, lval), lval);
2872 void constexpr (struct expent* lval)
2873 /* Get a constant value */
2875 memset (lval, 0, sizeof (*lval));
2876 if (expr (hie1, lval) != 0 || (lval->e_flags & E_MCONST) == 0) {
2877 Error (ERR_CONST_EXPR_EXPECTED);
2878 /* To avoid any compiler errors, make the expression a valid const */
2879 lval->e_flags = E_MCONST;
2880 lval->e_tptr = type_int;
2887 void intexpr (struct expent* lval)
2888 /* Get an integer expression */
2891 if (!IsInt (lval->e_tptr)) {
2892 Error (ERR_INT_EXPR_EXPECTED);
2893 /* To avoid any compiler errors, make the expression a valid int */
2894 lval->e_flags = E_MCONST;
2895 lval->e_tptr = type_int;
2902 void boolexpr (struct expent* lval)
2903 /* Get a boolean expression */
2905 /* Read an expression */
2908 /* If it's an integer, it's ok. If it's not an integer, but a pointer,
2909 * the pointer used in a boolean context is also ok (Ootherwise check if it's a pointer
2912 if (!IsInt (lval->e_tptr) && !IsPtr (lval->e_tptr)) {
2913 Error (ERR_INT_EXPR_EXPECTED);
2914 /* To avoid any compiler errors, make the expression a valid int */
2915 lval->e_flags = E_MCONST;
2916 lval->e_tptr = type_int;
2923 void test (unsigned label, int cond)
2924 /* Generate code to perform test and jump if false. */
2929 /* Eat the parenthesis */
2932 /* Prepare the expression, setup labels */
2933 memset (&lval, 0, sizeof (lval));
2934 lval.e_test = E_TEST;
2936 /* Generate code to eval the expr */
2937 k = expr (hie0, &lval);
2938 if (k == 0 && lval.e_flags == E_MCONST) {
2939 /* Constant rvalue */
2940 if (cond == 0 && lval.e_const == 0) {
2942 Warning (WARN_UNREACHABLE_CODE);
2943 } else if (cond && lval.e_const) {
2950 /* If the expr hasn't set condition codes, set the force-test flag */
2951 if ((lval.e_test & E_CC) == 0) {
2952 lval.e_test |= E_FORCETEST;
2955 /* Load the value into the primary register */
2956 exprhs (CF_FORCECHAR, k, &lval);
2958 /* Check for the closing brace */
2961 /* Generate the jump */
2963 g_truejump (CF_NONE, label);
2965 /* Special case (putting this here is a small hack - but hey, the
2966 * compiler itself is one big hack...): If a semicolon follows, we
2967 * don't have a statement and may omit the jump.
2969 if (curtok != TOK_SEMI) {
2970 g_falsejump (CF_NONE, label);