4 * Ullrich von Bassewitz, 21.06.1998
38 /*****************************************************************************/
40 /*****************************************************************************/
44 /* Generator attributes */
45 #define GEN_NOPUSH 0x01 /* Don't push lhs */
47 /* Map a generator function and its attributes to a token */
49 unsigned char Tok; /* Token to map to */
50 unsigned char Flags; /* Flags for generator function */
51 void (*Func) (unsigned, unsigned long); /* Generator func */
54 /* Descriptors for the operations */
55 static GenDesc GenMUL = { TOK_STAR, GEN_NOPUSH, g_mul };
56 static GenDesc GenDIV = { TOK_DIV, GEN_NOPUSH, g_div };
57 static GenDesc GenMOD = { TOK_MOD, GEN_NOPUSH, g_mod };
58 static GenDesc GenASL = { TOK_SHL, GEN_NOPUSH, g_asl };
59 static GenDesc GenASR = { TOK_SHR, GEN_NOPUSH, g_asr };
60 static GenDesc GenLT = { TOK_LT, GEN_NOPUSH, g_lt };
61 static GenDesc GenLE = { TOK_LE, GEN_NOPUSH, g_le };
62 static GenDesc GenGE = { TOK_GE, GEN_NOPUSH, g_ge };
63 static GenDesc GenGT = { TOK_GT, GEN_NOPUSH, g_gt };
64 static GenDesc GenEQ = { TOK_EQ, GEN_NOPUSH, g_eq };
65 static GenDesc GenNE = { TOK_NE, GEN_NOPUSH, g_ne };
66 static GenDesc GenAND = { TOK_AND, GEN_NOPUSH, g_and };
67 static GenDesc GenXOR = { TOK_XOR, GEN_NOPUSH, g_xor };
68 static GenDesc GenOR = { TOK_OR, GEN_NOPUSH, g_or };
69 static GenDesc GenPASGN = { TOK_PLUS_ASSIGN, GEN_NOPUSH, g_add };
70 static GenDesc GenSASGN = { TOK_MINUS_ASSIGN, GEN_NOPUSH, g_sub };
71 static GenDesc GenMASGN = { TOK_MUL_ASSIGN, GEN_NOPUSH, g_mul };
72 static GenDesc GenDASGN = { TOK_DIV_ASSIGN, GEN_NOPUSH, g_div };
73 static GenDesc GenMOASGN = { TOK_MOD_ASSIGN, GEN_NOPUSH, g_mod };
74 static GenDesc GenSLASGN = { TOK_SHL_ASSIGN, GEN_NOPUSH, g_asl };
75 static GenDesc GenSRASGN = { TOK_SHR_ASSIGN, GEN_NOPUSH, g_asr };
76 static GenDesc GenAASGN = { TOK_AND_ASSIGN, GEN_NOPUSH, g_and };
77 static GenDesc GenXOASGN = { TOK_XOR_ASSIGN, GEN_NOPUSH, g_xor };
78 static GenDesc GenOASGN = { TOK_OR_ASSIGN, GEN_NOPUSH, g_or };
82 /*****************************************************************************/
83 /* Function forwards */
84 /*****************************************************************************/
88 static int hie10 (struct expent* lval);
89 /* Handle ++, --, !, unary - etc. */
93 /*****************************************************************************/
94 /* Helper functions */
95 /*****************************************************************************/
99 static unsigned GlobalModeFlags (unsigned flags)
100 /* Return the addressing mode flags for the variable with the given flags */
103 if (flags == E_TGLAB) {
104 /* External linkage */
106 } else if (flags == E_TREGISTER) {
107 /* Register variable */
117 static int IsNullPtr (struct expent* lval)
118 /* Return true if this is the NULL pointer constant */
120 return (IsClassInt (lval->e_tptr) && /* Is it an int? */
121 lval->e_flags == E_MCONST && /* Is it constant? */
122 lval->e_const == 0); /* And is it's value zero? */
127 static type* promoteint (type* lhst, type* rhst)
128 /* In an expression with two ints, return the type of the result */
130 /* Rules for integer types:
131 * - If one of the values is a long, the result is long.
132 * - If one of the values is unsigned, the result is also unsigned.
133 * - Otherwise the result is an int.
135 if (IsTypeLong (lhst) || IsTypeLong (rhst)) {
136 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
142 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
152 static unsigned typeadjust (struct expent* lhs, struct expent* rhs, int NoPush)
153 /* Adjust the two values for a binary operation. lhs is expected on stack or
154 * to be constant, rhs is expected to be in the primary register or constant.
155 * The function will put the type of the result into lhs and return the
156 * code generator flags for the operation.
157 * If NoPush is given, it is assumed that the operation does not expect the lhs
158 * to be on stack, and that lhs is in a register instead.
159 * Beware: The function does only accept int types.
162 unsigned ltype, rtype;
165 /* Get the type strings */
166 type* lhst = lhs->e_tptr;
167 type* rhst = rhs->e_tptr;
169 /* Generate type adjustment code if needed */
170 ltype = TypeOf (lhst);
171 if (lhs->e_flags == E_MCONST) {
175 /* Value is in primary register*/
178 rtype = TypeOf (rhst);
179 if (rhs->e_flags == E_MCONST) {
182 flags = g_typeadjust (ltype, rtype);
184 /* Set the type of the result */
185 lhs->e_tptr = promoteint (lhst, rhst);
187 /* Return the code generator flags */
193 unsigned assignadjust (type* lhst, struct expent* rhs)
194 /* Adjust the type of the right hand expression so that it can be assigned to
195 * the type on the left hand side. This function is used for assignment and
196 * for converting parameters in a function call. It returns the code generator
197 * flags for the operation. The type string of the right hand side will be
198 * set to the type of the left hand side.
201 /* Get the type of the right hand side. Treat function types as
202 * pointer-to-function
204 type* rhst = rhs->e_tptr;
205 if (IsTypeFunc (rhst)) {
206 rhst = PointerTo (rhst);
209 /* After calling this function, rhs will have the type of the lhs */
212 /* First, do some type checking */
213 if (IsTypeVoid (lhst) || IsTypeVoid (rhst)) {
214 /* If one of the sides are of type void, output a more apropriate
217 Error ("Illegal type");
218 } else if (IsClassInt (lhst)) {
219 if (IsClassPtr (rhst)) {
220 /* Pointer -> int conversion */
221 Warning ("Converting pointer to integer without a cast");
222 } else if (!IsClassInt (rhst)) {
223 Error ("Incompatible types");
225 /* Adjust the int types. To avoid manipulation of TOS mark lhs
228 unsigned flags = TypeOf (rhst);
229 if (rhs->e_flags & E_MCONST) {
232 return g_typeadjust (TypeOf (lhst) | CF_CONST, flags);
234 } else if (IsClassPtr (lhst)) {
235 if (IsClassPtr (rhst)) {
236 /* Pointer to pointer assignment is valid, if:
237 * - both point to the same types, or
238 * - the rhs pointer is a void pointer, or
239 * - the lhs pointer is a void pointer.
241 if (!IsTypeVoid (Indirect (lhst)) && !IsTypeVoid (Indirect (rhst))) {
242 /* Compare the types */
243 switch (TypeCmp (lhst, rhst)) {
245 case TC_INCOMPATIBLE:
246 Error ("Incompatible pointer types");
250 Error ("Pointer types differ in type qualifiers");
258 } else if (IsClassInt (rhst)) {
259 /* Int to pointer assignment is valid only for constant zero */
260 if ((rhs->e_flags & E_MCONST) == 0 || rhs->e_const != 0) {
261 Warning ("Converting integer to pointer without a cast");
263 } else if (IsTypeFuncPtr (lhst) && IsTypeFunc(rhst)) {
264 /* Assignment of function to function pointer is allowed, provided
265 * that both functions have the same parameter list.
267 if (TypeCmp (Indirect (lhst), rhst) < TC_EQUAL) {
268 Error ("Incompatible types");
271 Error ("Incompatible types");
274 Error ("Incompatible types");
277 /* Return an int value in all cases where the operands are not both ints */
283 void DefineData (struct expent* lval)
284 /* Output a data definition for the given expression */
286 unsigned flags = lval->e_flags;
288 switch (flags & E_MCTYPE) {
292 g_defdata (TypeOf (lval->e_tptr) | CF_CONST, lval->e_const, 0);
296 /* Register variable. Taking the address is usually not
299 if (!AllowRegVarAddr) {
300 Error ("Cannot take the address of a register variable");
306 /* Local or global symbol */
307 g_defdata (GlobalModeFlags (flags), lval->e_name, lval->e_const);
311 /* a literal of some kind */
312 g_defdata (CF_STATIC, LiteralLabel, lval->e_const);
316 Internal ("Unknown constant type: %04X", flags);
322 static void lconst (unsigned flags, struct expent* lval)
323 /* Load primary reg with some constant value. */
325 switch (lval->e_flags & E_MCTYPE) {
328 g_leasp (lval->e_const);
332 /* Number constant */
333 g_getimmed (flags | TypeOf (lval->e_tptr) | CF_CONST, lval->e_const, 0);
337 /* Register variable. Taking the address is usually not
340 if (!AllowRegVarAddr) {
341 Error ("Cannot take the address of a register variable");
347 /* Local or global symbol, load address */
348 flags |= GlobalModeFlags (lval->e_flags);
350 g_getimmed (flags, lval->e_name, lval->e_const);
355 g_getimmed (CF_STATIC, LiteralLabel, lval->e_const);
359 Internal ("Unknown constant type: %04X", lval->e_flags);
365 static int kcalc (int tok, long val1, long val2)
366 /* Calculate an operation with left and right operand constant. */
370 return (val1 == val2);
372 return (val1 != val2);
374 return (val1 < val2);
376 return (val1 <= val2);
378 return (val1 >= val2);
380 return (val1 > val2);
382 return (val1 | val2);
384 return (val1 ^ val2);
386 return (val1 & val2);
388 return (val1 >> val2);
390 return (val1 << val2);
392 return (val1 * val2);
395 Error ("Division by zero");
398 return (val1 / val2);
401 Error ("Modulo operation with zero");
404 return (val1 % val2);
406 Internal ("kcalc: got token 0x%X\n", tok);
413 static GenDesc* FindGen (int Tok, GenDesc** Table)
416 while ((G = *Table) != 0) {
427 static int istypeexpr (void)
428 /* Return true if some sort of variable or type is waiting (helper for cast
429 * and sizeof() in hie10).
434 return curtok == TOK_LPAREN && (
435 (nxttok >= TOK_FIRSTTYPE && nxttok <= TOK_LASTTYPE) ||
436 (nxttok == TOK_CONST) ||
437 (nxttok == TOK_IDENT &&
438 (Entry = FindSym (NextTok.Ident)) != 0 &&
445 static void PushAddr (struct expent* lval)
446 /* If the expression contains an address that was somehow evaluated,
447 * push this address on the stack. This is a helper function for all
448 * sorts of implicit or explicit assignment functions where the lvalue
449 * must be saved if it's not constant, before evaluating the rhs.
452 /* Get the address on stack if needed */
453 if (lval->e_flags != E_MREG && (lval->e_flags & E_MEXPR)) {
454 /* Push the address (always a pointer) */
461 /*****************************************************************************/
463 /*****************************************************************************/
467 void exprhs (unsigned flags, int k, struct expent *lval)
468 /* Put the result of an expression into the primary register */
474 /* Dereferenced lvalue */
475 flags |= TypeOf (lval->e_tptr);
476 if (lval->e_test & E_FORCETEST) {
478 lval->e_test &= ~E_FORCETEST;
480 if (f & E_MGLOBAL) { /* ref to globalvar */
482 flags |= GlobalModeFlags (f);
483 g_getstatic (flags, lval->e_name, lval->e_const);
484 } else if (f & E_MLOCAL) {
485 /* ref to localvar */
486 g_getlocal (flags, lval->e_const);
487 } else if (f & E_MCONST) {
488 /* ref to absolute address */
489 g_getstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
490 } else if (f == E_MEOFFS) {
491 g_getind (flags, lval->e_const);
492 } else if (f != E_MREG) {
495 } else if (f == E_MEOFFS) {
496 /* reference not storable */
497 flags |= TypeOf (lval->e_tptr);
498 g_inc (flags | CF_CONST, lval->e_const);
499 } else if ((f & E_MEXPR) == 0) {
500 /* Constant of some sort, load it into the primary */
501 lconst (flags, lval);
503 if (lval->e_test & E_FORCETEST) { /* we testing this value? */
505 AddCodeHint ("forcetest");
506 flags |= TypeOf (lval->e_tptr);
507 g_test (flags); /* yes, force a test */
508 lval->e_test &= ~E_FORCETEST;
513 static void callfunction (struct expent* lval)
514 /* Perform a function call. Called from hie11, this routine will
515 * either call the named function, or if the supplied ptr is zero,
516 * will call the contents of P.
520 FuncDesc* Func; /* Function descriptor */
521 int Ellipsis; /* True if we have an open param list */
522 SymEntry* Param; /* Current formal parameter */
523 unsigned ParamCount; /* Actual parameter count */
524 unsigned ParamSize; /* Number of parameter bytes */
530 /* Get a pointer to the function descriptor from the type string */
531 Func = GetFuncDesc (lval->e_tptr);
533 /* Initialize vars to keep gcc silent */
537 /* Check if this is a function pointer. If so, save it. If not, check for
538 * special known library functions that may be inlined.
540 if (lval->e_flags & E_MEXPR) {
541 /* Function pointer is in primary register, save it */
542 Mark = GetCodePos ();
544 } else if (InlineStdFuncs && IsStdFunc ((const char*) lval->e_name)) {
545 /* Inline this function */
546 HandleStdFunc (lval);
550 /* Parse the actual parameter list */
554 while (curtok != TOK_RPAREN) {
556 /* Add a hint for the optimizer */
557 AddCodeHint ("param:start");
559 /* Count arguments */
562 /* Fetch the pointer to the next argument, check for too many args */
563 if (ParamCount <= Func->ParamCount) {
564 if (ParamCount == 1) {
566 Param = Func->SymTab->SymHead;
569 Param = Param->NextSym;
570 CHECK ((Param->Flags & SC_PARAM) != 0);
572 } else if (!Ellipsis) {
573 /* Too many arguments. Do we have an open param list? */
574 if ((Func->Flags & FD_ELLIPSIS) == 0) {
575 /* End of param list reached, no ellipsis */
576 Error ("Too many arguments in function call");
578 /* Assume an ellipsis even in case of errors to avoid an error
579 * message for each other argument.
584 /* Do some optimization: If we have a constant value to push,
585 * use a special function that may optimize.
588 if (!Ellipsis && SizeOf (Param->Type) == 1) {
589 CFlags = CF_FORCECHAR;
592 if (evalexpr (CFlags, hie1, &lval2) == 0) {
593 /* A constant value */
597 /* If we don't have an argument spec, accept anything, otherwise
598 * convert the actual argument to the type needed.
601 /* Promote the argument if needed */
602 assignadjust (Param->Type, &lval2);
604 /* If we have a prototype, chars may be pushed as chars */
605 Flags |= CF_FORCECHAR;
608 /* Use the type of the argument for the push */
609 Flags |= TypeOf (lval2.e_tptr);
611 /* If this is a fastcall function, don't push the last argument */
612 if (ParamCount == Func->ParamCount && (Func->Flags & FD_FASTCALL) != 0) {
613 /* Just load the argument into the primary. This is only needed if
614 * we have a constant argument, otherwise the value is already in
617 if (Flags & CF_CONST) {
618 exprhs (CF_FORCECHAR, 0, &lval2);
621 /* Push the argument, count the argument size */
622 g_push (Flags, lval2.e_const);
623 ParamSize += sizeofarg (Flags);
626 /* Add an optimizer hint */
627 AddCodeHint ("param:end");
629 /* Check for end of argument list */
630 if (curtok != TOK_COMMA) {
636 /* We need the closing bracket here */
639 /* Check if we had enough parameters */
640 if (ParamCount < Func->ParamCount) {
641 Error ("Too few arguments in function call");
645 if (lval->e_flags & E_MEXPR) {
646 /* Function called via pointer: Restore it and call function */
647 if (ParamSize != 0) {
650 /* We had no parameters - remove save code */
653 g_callind (TypeOf (lval->e_tptr), ParamSize);
655 g_call (TypeOf (lval->e_tptr), (char*) lval->e_name, ParamSize);
662 /* This function parses ASM statements. The syntax of the ASM directive
663 * looks like the one defined for C++ (C has no ASM directive), that is,
664 * a string literal in parenthesis.
670 /* Need left parenthesis */
674 if (curtok != TOK_SCONST) {
675 Error ("String literal expected");
677 /* Write the string directly into the output, followed by a newline */
678 AddCodeLine (GetLiteral (curval));
680 /* Reset the string pointer, effectivly clearing the string from the
681 * string table. Since we're working with one token lookahead, this
682 * will fail if the next token is also a string token, but that's a
683 * syntax error anyway, because we expect a right paren.
685 ResetLiteralOffs (curval);
688 /* Skip the string token */
691 /* Closing paren needed */
697 static int primary (struct expent* lval)
698 /* This is the lowest level of the expression parser. */
702 /* not a test at all, yet */
705 /* Character and integer constants. */
706 if (curtok == TOK_ICONST || curtok == TOK_CCONST) {
707 lval->e_flags = E_MCONST | E_TCONST;
708 lval->e_tptr = curtype;
709 lval->e_const = curval;
714 /* Process parenthesized subexpression by calling the whole parser
717 if (curtok == TOK_LPAREN) {
719 memset (lval, 0, sizeof (*lval)); /* Remove any attributes */
725 /* All others may only be used if the expression evaluation is not called
726 * recursively by the preprocessor.
729 /* Illegal expression in PP mode */
730 Error ("Preprocessor expression expected");
731 lval->e_flags = E_MCONST;
732 lval->e_tptr = type_int;
737 if (curtok == TOK_IDENT) {
742 /* Get a pointer to the symbol table entry */
743 Sym = FindSym (CurTok.Ident);
745 /* Is the symbol known? */
748 /* We found the symbol - skip the name token */
751 /* The expression type is the symbol type */
752 lval->e_tptr = Sym->Type;
754 /* Check for illegal symbol types */
755 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
756 if (Sym->Flags & SC_TYPE) {
757 /* Cannot use type symbols */
758 Error ("Variable identifier expected");
759 /* Assume an int type to make lval valid */
760 lval->e_flags = E_MLOCAL | E_TLOFFS;
761 lval->e_tptr = type_int;
766 /* Check for legal symbol types */
767 if ((Sym->Flags & SC_ENUM) == SC_ENUM) {
768 lval->e_flags = E_MCONST;
769 lval->e_const = Sym->V.EnumVal;
771 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
773 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
774 lval->e_name = (unsigned long) Sym->Name;
776 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
778 lval->e_flags = E_MLOCAL | E_TLOFFS;
779 lval->e_const = Sym->V.Offs;
780 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
781 /* Static variable */
782 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
783 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
784 lval->e_name = (unsigned long) Sym->Name;
786 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
787 lval->e_name = Sym->V.Label;
790 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
791 /* Register variable, zero page based */
792 lval->e_flags = E_MGLOBAL | E_MCONST | E_TREGISTER;
793 lval->e_name = Sym->V.Offs;
796 /* Local static variable */
797 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
798 lval->e_name = Sym->V.Offs;
802 /* The symbol is referenced now */
803 Sym->Flags |= SC_REF;
804 if (IsTypeFunc (lval->e_tptr) || IsTypeArray (lval->e_tptr)) {
810 /* We did not find the symbol. Remember the name, then skip it */
811 strcpy (Ident, CurTok.Ident);
814 /* IDENT is either an auto-declared function or an undefined variable. */
815 if (curtok == TOK_LPAREN) {
816 /* Declare a function returning int. For that purpose, prepare a
817 * function signature for a function having an empty param list
820 Warning ("Function call without a prototype");
821 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
822 lval->e_tptr = Sym->Type;
823 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
824 lval->e_name = (unsigned long) Sym->Name;
830 /* Undeclared Variable */
831 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
832 lval->e_flags = E_MLOCAL | E_TLOFFS;
833 lval->e_tptr = type_int;
835 Error ("Undefined symbol: `%s'", Ident);
841 /* String literal? */
842 if (curtok == TOK_SCONST) {
843 lval->e_flags = E_MCONST | E_TLIT;
844 lval->e_const = curval;
845 lval->e_tptr = GetCharArrayType (strlen (GetLiteral (curval)));
851 if (curtok == TOK_ASM) {
853 lval->e_tptr = type_void;
854 lval->e_flags = E_MEXPR;
859 /* __AX__ and __EAX__ pseudo values? */
860 if (curtok == TOK_AX || curtok == TOK_EAX) {
861 lval->e_tptr = (curtok == TOK_AX)? type_uint : type_ulong;
862 lval->e_flags = E_MREG;
863 lval->e_test &= ~E_CC;
866 return 1; /* May be used as lvalue */
869 /* Illegal primary. */
870 Error ("Expression expected");
871 lval->e_flags = E_MCONST;
872 lval->e_tptr = type_int;
878 static int arrayref (int k, struct expent* lval)
879 /* Handle an array reference */
893 /* Skip the bracket */
896 /* Get the type of left side */
897 tptr1 = lval->e_tptr;
899 /* We can apply a special treatment for arrays that have a const base
900 * address. This is true for most arrays and will produce a lot better
901 * code. Check if this is a const base address.
903 lflags = lval->e_flags & ~E_MCTYPE;
904 ConstBaseAddr = (lflags == E_MCONST) || /* Constant numeric address */
905 (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
906 lflags == E_MLOCAL; /* Local array */
908 /* If we have a constant base, we delay the address fetch */
909 Mark1 = GetCodePos ();
910 Mark2 = 0; /* Silence gcc */
911 if (!ConstBaseAddr) {
912 /* Get a pointer to the array into the primary */
913 exprhs (CF_NONE, k, lval);
915 /* Get the array pointer on stack. Do not push more than 16
916 * bit, even if this value is greater, since we cannot handle
917 * other than 16bit stuff when doing indexing.
919 Mark2 = GetCodePos ();
923 /* TOS now contains ptr to array elements. Get the subscript. */
925 if (l == 0 && lval2.e_flags == E_MCONST) {
927 /* The array subscript is a constant - remove value from stack */
928 if (!ConstBaseAddr) {
932 /* Get an array pointer into the primary */
933 exprhs (CF_NONE, k, lval);
936 if (IsClassPtr (tptr1)) {
938 /* Scale the subscript value according to element size */
939 lval2.e_const *= PSizeOf (tptr1);
941 /* Remove code for lhs load */
944 /* Handle constant base array on stack. Be sure NOT to
945 * handle pointers the same way, this won't work.
947 if (IsTypeArray (tptr1) &&
948 ((lval->e_flags & ~E_MCTYPE) == E_MCONST ||
949 (lval->e_flags & ~E_MCTYPE) == E_MLOCAL ||
950 (lval->e_flags & E_MGLOBAL) != 0 ||
951 (lval->e_flags == E_MEOFFS))) {
952 lval->e_const += lval2.e_const;
955 /* Pointer - load into primary and remember offset */
956 if ((lval->e_flags & E_MEXPR) == 0 || k != 0) {
957 exprhs (CF_NONE, k, lval);
959 lval->e_const = lval2.e_const;
960 lval->e_flags = E_MEOFFS;
963 /* Result is of element type */
964 lval->e_tptr = Indirect (tptr1);
969 } else if (IsClassPtr (tptr2 = lval2.e_tptr)) {
970 /* Subscript is pointer, get element type */
971 lval2.e_tptr = Indirect (tptr2);
973 /* Scale the rhs value in the primary register */
974 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
976 lval->e_tptr = lval2.e_tptr;
978 Error ("Cannot subscript");
981 /* Add the subscript. Since arrays are indexed by integers,
982 * we will ignore the true type of the subscript here and
985 g_inc (CF_INT | CF_CONST, lval2.e_const);
989 /* Array subscript is not constant. Load it into the primary */
990 Mark2 = GetCodePos ();
991 exprhs (CF_NONE, l, &lval2);
993 tptr2 = lval2.e_tptr;
994 if (IsClassPtr (tptr1)) {
996 /* Get the element type */
997 lval->e_tptr = Indirect (tptr1);
999 /* Indexing is based on int's, so we will just use the integer
1000 * portion of the index (which is in (e)ax, so there's no further
1003 g_scale (CF_INT, SizeOf (lval->e_tptr));
1005 } else if (IsClassPtr (tptr2)) {
1007 /* Get the element type */
1008 lval2.e_tptr = Indirect (tptr2);
1010 /* Get the int value on top. If we go here, we're sure,
1011 * both values are 16 bit (the first one was truncated
1012 * if necessary and the second one is a pointer).
1013 * Note: If ConstBaseAddr is true, we don't have a value on
1014 * stack, so to "swap" both, just push the subscript.
1016 if (ConstBaseAddr) {
1018 exprhs (CF_NONE, k, lval);
1025 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
1026 lval->e_tptr = lval2.e_tptr;
1028 Error ("Cannot subscript");
1031 /* The offset is now in the primary register. It didn't have a
1032 * constant base address for the lhs, the lhs address is already
1033 * on stack, and we must add the offset. If the base address was
1034 * constant, we call special functions to add the address to the
1037 if (!ConstBaseAddr) {
1038 /* Add the subscript. Both values are int sized. */
1042 /* If the subscript has itself a constant address, it is often
1043 * a better idea to reverse again the order of the evaluation.
1044 * This will generate better code if the subscript is a byte
1045 * sized variable. But beware: This is only possible if the
1046 * subscript was not scaled, that is, if this was a byte array
1049 rflags = lval2.e_flags & ~E_MCTYPE;
1050 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1051 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1052 rflags == E_MLOCAL; /* Local array */
1054 if (ConstSubAddr && SizeOf (lval->e_tptr) == 1) {
1058 /* Reverse the order of evaluation */
1059 unsigned flags = (SizeOf (lval2.e_tptr) == 1)? CF_CHAR : CF_INT;
1062 /* Get a pointer to the array into the primary. We have changed
1063 * e_tptr above but we need the original type to load the
1064 * address, so restore it temporarily.
1066 SavedType = lval->e_tptr;
1067 lval->e_tptr = tptr1;
1068 exprhs (CF_NONE, k, lval);
1069 lval->e_tptr = SavedType;
1071 /* Add the variable */
1072 if (rflags == E_MLOCAL) {
1073 g_addlocal (flags, lval2.e_const);
1075 flags |= GlobalModeFlags (lval2.e_flags);
1076 g_addstatic (flags, lval2.e_name, lval2.e_const);
1079 if (lflags == E_MCONST) {
1080 /* Constant numeric address. Just add it */
1081 g_inc (CF_INT | CF_UNSIGNED, lval->e_const);
1082 } else if (lflags == E_MLOCAL) {
1083 /* Base address is a local variable address */
1084 if (IsTypeArray (tptr1)) {
1085 g_addaddr_local (CF_INT, lval->e_const);
1087 g_addlocal (CF_PTR, lval->e_const);
1090 /* Base address is a static variable address */
1091 unsigned flags = CF_INT;
1092 flags |= GlobalModeFlags (lval->e_flags);
1093 if (IsTypeArray (tptr1)) {
1094 g_addaddr_static (flags, lval->e_name, lval->e_const);
1096 g_addstatic (flags, lval->e_name, lval->e_const);
1102 lval->e_flags = E_MEXPR;
1105 return !IsTypeArray (lval->e_tptr);
1111 static int structref (int k, struct expent* lval)
1112 /* Process struct field after . or ->. */
1118 /* Skip the token and check for an identifier */
1120 if (curtok != TOK_IDENT) {
1121 Error ("Identifier expected");
1122 lval->e_tptr = type_int;
1126 /* Get the symbol table entry and check for a struct field */
1127 strcpy (Ident, CurTok.Ident);
1129 Field = FindStructField (lval->e_tptr, Ident);
1131 Error ("Struct/union has no field named `%s'", Ident);
1132 lval->e_tptr = type_int;
1136 /* If we have constant input data, the result is also constant */
1137 flags = lval->e_flags & ~E_MCTYPE;
1138 if (flags == E_MCONST ||
1139 (k == 0 && (flags == E_MLOCAL ||
1140 (flags & E_MGLOBAL) != 0 ||
1141 lval->e_flags == E_MEOFFS))) {
1142 lval->e_const += Field->V.Offs;
1144 if ((flags & E_MEXPR) == 0 || k != 0) {
1145 exprhs (CF_NONE, k, lval);
1147 lval->e_const = Field->V.Offs;
1148 lval->e_flags = E_MEOFFS;
1150 lval->e_tptr = Field->Type;
1151 return !IsTypeArray (Field->Type);
1156 static int hie11 (struct expent *lval)
1157 /* Handle compound types (structs and arrays) */
1164 if (curtok < TOK_LBRACK || curtok > TOK_PTR_REF) {
1171 if (curtok == TOK_LBRACK) {
1173 /* Array reference */
1174 k = arrayref (k, lval);
1176 } else if (curtok == TOK_LPAREN) {
1178 /* Function call. Skip the opening parenthesis */
1180 tptr = lval->e_tptr;
1181 if (IsTypeFunc (tptr) || IsTypeFuncPtr (tptr)) {
1182 if (IsTypeFuncPtr (tptr)) {
1183 /* Pointer to function. Handle transparently */
1184 exprhs (CF_NONE, k, lval); /* Function pointer to A/X */
1185 ++lval->e_tptr; /* Skip T_PTR */
1186 lval->e_flags |= E_MEXPR;
1188 callfunction (lval);
1189 lval->e_flags = E_MEXPR;
1190 lval->e_tptr += DECODE_SIZE + 1; /* Set to result */
1192 Error ("Illegal function call");
1196 } else if (curtok == TOK_DOT) {
1198 if (!IsClassStruct (lval->e_tptr)) {
1199 Error ("Struct expected");
1201 k = structref (0, lval);
1203 } else if (curtok == TOK_PTR_REF) {
1205 tptr = lval->e_tptr;
1206 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1207 Error ("Struct pointer expected");
1209 k = structref (k, lval);
1219 static void store (struct expent* lval)
1220 /* Store primary reg into this reference */
1226 flags = TypeOf (lval->e_tptr);
1227 if (f & E_MGLOBAL) {
1228 flags |= GlobalModeFlags (f);
1235 g_putstatic (flags, lval->e_name, lval->e_const);
1237 } else if (f & E_MLOCAL) {
1238 g_putlocal (flags, lval->e_const);
1239 } else if (f == E_MEOFFS) {
1240 g_putind (flags, lval->e_const);
1241 } else if (f != E_MREG) {
1243 g_putind (flags, 0);
1245 /* Store into absolute address */
1246 g_putstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
1250 /* Assume that each one of the stores will invalidate CC */
1251 lval->e_test &= ~E_CC;
1256 static void pre_incdec (struct expent* lval, void (*inc) (unsigned, unsigned long))
1257 /* Handle --i and ++i */
1264 if ((k = hie10 (lval)) == 0) {
1265 Error ("Invalid lvalue");
1269 /* Get the data type */
1270 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR | CF_CONST;
1272 /* Get the increment value in bytes */
1273 val = (lval->e_tptr [0] == T_PTR)? PSizeOf (lval->e_tptr) : 1;
1275 /* We're currently only able to handle some adressing modes */
1276 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
1277 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
1278 (lval->e_flags & E_MCONST) == 0 && /* Constant address? */
1279 (lval->e_flags & E_MEXPR) == 0) { /* Address in a/x? */
1281 /* Use generic code. Push the address if needed */
1284 /* Fetch the value */
1285 exprhs (CF_NONE, k, lval);
1287 /* Increment value in primary */
1290 /* Store the result back */
1295 /* Special code for some addressing modes - use the special += ops */
1296 if (lval->e_flags & E_MGLOBAL) {
1297 flags |= GlobalModeFlags (lval->e_flags);
1299 g_addeqstatic (flags, lval->e_name, lval->e_const, val);
1301 g_subeqstatic (flags, lval->e_name, lval->e_const, val);
1303 } else if (lval->e_flags & E_MLOCAL) {
1304 /* ref to localvar */
1306 g_addeqlocal (flags, lval->e_const, val);
1308 g_subeqlocal (flags, lval->e_const, val);
1310 } else if (lval->e_flags & E_MCONST) {
1311 /* ref to absolute address */
1312 flags |= CF_ABSOLUTE;
1314 g_addeqstatic (flags, lval->e_const, 0, val);
1316 g_subeqstatic (flags, lval->e_const, 0, val);
1318 } else if (lval->e_flags & E_MEXPR) {
1319 /* Address in a/x. */
1321 g_addeqind (flags, lval->e_const, val);
1323 g_subeqind (flags, lval->e_const, val);
1326 Internal ("Invalid addressing mode");
1331 /* Result is an expression */
1332 lval->e_flags = E_MEXPR;
1337 static void post_incdec (struct expent *lval, int k, void (*inc) (unsigned, unsigned long))
1338 /* Handle i-- and i++ */
1344 Error ("Invalid lvalue");
1348 /* Get the data type */
1349 flags = TypeOf (lval->e_tptr);
1351 /* Push the address if needed */
1354 /* Fetch the value and save it (since it's the result of the expression) */
1355 exprhs (CF_NONE, 1, lval);
1356 g_save (flags | CF_FORCECHAR);
1358 /* If we have a pointer expression, increment by the size of the type */
1359 if (lval->e_tptr[0] == T_PTR) {
1360 inc (flags | CF_CONST | CF_FORCECHAR, SizeOf (lval->e_tptr + 1));
1362 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1365 /* Store the result back */
1368 /* Restore the original value */
1369 g_restore (flags | CF_FORCECHAR);
1370 lval->e_flags = E_MEXPR;
1375 static void unaryop (int tok, struct expent* lval)
1376 /* Handle unary -/+ and ~ */
1383 if (k == 0 && lval->e_flags & E_MCONST) {
1384 /* Value is constant */
1386 case TOK_MINUS: lval->e_const = -lval->e_const; break;
1387 case TOK_PLUS: break;
1388 case TOK_COMP: lval->e_const = ~lval->e_const; break;
1389 default: Internal ("Unexpected token: %d", tok);
1392 /* Value is not constant */
1393 exprhs (CF_NONE, k, lval);
1395 /* Get the type of the expression */
1396 flags = TypeOf (lval->e_tptr);
1398 /* Handle the operation */
1400 case TOK_MINUS: g_neg (flags); break;
1401 case TOK_PLUS: break;
1402 case TOK_COMP: g_com (flags); break;
1403 default: Internal ("Unexpected token: %d", tok);
1405 lval->e_flags = E_MEXPR;
1411 static int typecast (struct expent* lval)
1412 /* Handle an explicit cast */
1415 type Type[MAXTYPELEN];
1417 /* Skip the left paren */
1426 /* Read the expression we have to cast */
1429 /* If the expression is a function, treat it as pointer-to-function */
1430 if (IsTypeFunc (lval->e_tptr)) {
1431 lval->e_tptr = PointerTo (lval->e_tptr);
1434 /* Check for a constant on the right side */
1435 if (k == 0 && lval->e_flags == E_MCONST) {
1437 /* A cast of a constant to something else. If the new type is an int,
1438 * be sure to handle the size extension correctly. If the new type is
1439 * not an int, the cast is implementation specific anyway, so leave
1442 if (IsClassInt (Type)) {
1444 /* Get the current and new size of the value */
1445 unsigned OldSize = SizeOf (lval->e_tptr);
1446 unsigned NewSize = SizeOf (Type);
1447 unsigned OldBits = OldSize * 8;
1448 unsigned NewBits = NewSize * 8;
1450 /* Check if the new datatype will have a smaller range */
1451 if (NewSize < OldSize) {
1453 /* Cut the value to the new size */
1454 lval->e_const &= (0xFFFFFFFFUL >> (32 - NewBits));
1456 /* If the new value is signed, sign extend the value */
1457 if (!IsSignUnsigned (Type)) {
1458 lval->e_const |= ((~0L) << NewBits);
1461 } else if (NewSize > OldSize) {
1463 /* Sign extend the value if needed */
1464 if (!IsSignUnsigned (Type) && !IsSignUnsigned (lval->e_tptr)) {
1465 if (lval->e_const & (0x01UL << (OldBits-1))) {
1466 lval->e_const |= ((~0L) << OldBits);
1474 /* Not a constant. Be sure to ignore casts to void */
1475 if (!IsTypeVoid (Type)) {
1477 /* If the size does not change, leave the value alone. Otherwise,
1478 * we have to load the value into the primary and generate code to
1479 * cast the value in the primary register.
1481 if (SizeOf (Type) != SizeOf (lval->e_tptr)) {
1483 /* Load the value into the primary */
1484 exprhs (CF_NONE, k, lval);
1486 /* Mark the lhs as const to avoid a manipulation of TOS */
1487 g_typecast (TypeOf (Type) | CF_CONST, TypeOf (lval->e_tptr));
1489 /* Value is now in primary */
1490 lval->e_flags = E_MEXPR;
1496 /* In any case, use the new type */
1497 lval->e_tptr = TypeDup (Type);
1505 static int hie10 (struct expent* lval)
1506 /* Handle ++, --, !, unary - etc. */
1514 pre_incdec (lval, g_inc);
1518 pre_incdec (lval, g_dec);
1524 unaryop (curtok, lval);
1529 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1530 /* Constant expression */
1531 lval->e_const = !lval->e_const;
1533 g_bneg (TypeOf (lval->e_tptr));
1534 lval->e_test |= E_CC; /* bneg will set cc */
1535 lval->e_flags = E_MEXPR; /* say it's an expr */
1537 return 0; /* expr not storable */
1541 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1542 /* Expression is not const, indirect value loaded into primary */
1543 lval->e_flags = E_MEXPR;
1544 lval->e_const = 0; /* Offset is zero now */
1547 if (IsClassPtr (t)) {
1548 lval->e_tptr = Indirect (t);
1550 Error ("Illegal indirection");
1557 /* The & operator may be applied to any lvalue, and it may be
1558 * applied to functions, even if they're no lvalues.
1560 if (k == 0 && !IsTypeFunc (lval->e_tptr)) {
1561 /* Allow the & operator with an array */
1562 if (!IsTypeArray (lval->e_tptr)) {
1563 Error ("Illegal address");
1566 t = TypeAlloc (TypeLen (lval->e_tptr) + 2);
1568 TypeCpy (t + 1, lval->e_tptr);
1575 if (istypeexpr ()) {
1576 type Type[MAXTYPELEN];
1578 lval->e_const = SizeOf (ParseType (Type));
1581 /* Remember the output queue pointer */
1582 CodeMark Mark = GetCodePos ();
1584 lval->e_const = SizeOf (lval->e_tptr);
1585 /* Remove any generated code */
1588 lval->e_flags = E_MCONST | E_TCONST;
1589 lval->e_tptr = type_uint;
1590 lval->e_test &= ~E_CC;
1594 if (istypeexpr ()) {
1596 return typecast (lval);
1603 post_incdec (lval, k, g_inc);
1607 post_incdec (lval, k, g_dec);
1617 static int hie_internal (GenDesc** ops, /* List of generators */
1618 struct expent* lval, /* parent expr's lval */
1619 int (*hienext) (struct expent*),
1620 int* UsedGen) /* next higher level */
1621 /* Helper function */
1624 struct expent lval2;
1628 token_t tok; /* The operator token */
1629 unsigned ltype, type;
1630 int rconst; /* Operand is a constant */
1636 while ((Gen = FindGen (curtok, ops)) != 0) {
1638 /* Tell the caller that we handled it's ops */
1641 /* All operators that call this function expect an int on the lhs */
1642 if (!IsClassInt (lval->e_tptr)) {
1643 Error ("Integer expression expected");
1646 /* Remember the operator token, then skip it */
1650 /* Get the lhs on stack */
1651 Mark1 = GetCodePos ();
1652 ltype = TypeOf (lval->e_tptr);
1653 if (k == 0 && lval->e_flags == E_MCONST) {
1654 /* Constant value */
1655 Mark2 = GetCodePos ();
1656 g_push (ltype | CF_CONST, lval->e_const);
1658 /* Value not constant */
1659 exprhs (CF_NONE, k, lval);
1660 Mark2 = GetCodePos ();
1664 /* Get the right hand side */
1665 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1667 /* Check the type of the rhs */
1668 if (!IsClassInt (lval2.e_tptr)) {
1669 Error ("Integer expression expected");
1672 /* Check for const operands */
1673 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1675 /* Both operands are constant, remove the generated code */
1679 /* Evaluate the result */
1680 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1682 /* Get the type of the result */
1683 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1687 /* If the right hand side is constant, and the generator function
1688 * expects the lhs in the primary, remove the push of the primary
1691 unsigned rtype = TypeOf (lval2.e_tptr);
1694 /* Second value is constant - check for div */
1697 if (tok == TOK_DIV && lval2.e_const == 0) {
1698 Error ("Division by zero");
1699 } else if (tok == TOK_MOD && lval2.e_const == 0) {
1700 Error ("Modulo operation with zero");
1702 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1705 ltype |= CF_REG; /* Value is in register */
1709 /* Determine the type of the operation result. */
1710 type |= g_typeadjust (ltype, rtype);
1711 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1714 Gen->Func (type, lval2.e_const);
1715 lval->e_flags = E_MEXPR;
1718 /* We have a rvalue now */
1727 static int hie_compare (GenDesc** ops, /* List of generators */
1728 struct expent* lval, /* parent expr's lval */
1729 int (*hienext) (struct expent*))
1730 /* Helper function for the compare operators */
1733 struct expent lval2;
1737 token_t tok; /* The operator token */
1739 int rconst; /* Operand is a constant */
1744 while ((Gen = FindGen (curtok, ops)) != 0) {
1746 /* Remember the operator token, then skip it */
1750 /* Get the lhs on stack */
1751 Mark1 = GetCodePos ();
1752 ltype = TypeOf (lval->e_tptr);
1753 if (k == 0 && lval->e_flags == E_MCONST) {
1754 /* Constant value */
1755 Mark2 = GetCodePos ();
1756 g_push (ltype | CF_CONST, lval->e_const);
1758 /* Value not constant */
1759 exprhs (CF_NONE, k, lval);
1760 Mark2 = GetCodePos ();
1764 /* Get the right hand side */
1765 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1767 /* Make sure, the types are compatible */
1768 if (IsClassInt (lval->e_tptr)) {
1769 if (!IsClassInt (lval2.e_tptr) && !(IsClassPtr(lval2.e_tptr) && IsNullPtr(lval))) {
1770 Error ("Incompatible types");
1772 } else if (IsClassPtr (lval->e_tptr)) {
1773 if (IsClassPtr (lval2.e_tptr)) {
1774 /* Both pointers are allowed in comparison if they point to
1775 * the same type, or if one of them is a void pointer.
1777 type* left = Indirect (lval->e_tptr);
1778 type* right = Indirect (lval2.e_tptr);
1779 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1780 /* Incomatible pointers */
1781 Error ("Incompatible types");
1783 } else if (!IsNullPtr (&lval2)) {
1784 Error ("Incompatible types");
1788 /* Check for const operands */
1789 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1791 /* Both operands are constant, remove the generated code */
1795 /* Evaluate the result */
1796 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1800 /* If the right hand side is constant, and the generator function
1801 * expects the lhs in the primary, remove the push of the primary
1807 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1810 ltype |= CF_REG; /* Value is in register */
1814 /* Determine the type of the operation result. If the left
1815 * operand is of type char and the right is a constant, or
1816 * if both operands are of type char, we will encode the
1817 * operation as char operation. Otherwise the default
1818 * promotions are used.
1820 if (IsTypeChar (lval->e_tptr) && (IsTypeChar (lval2.e_tptr) || rconst)) {
1822 if (IsSignUnsigned (lval->e_tptr) || IsSignUnsigned (lval2.e_tptr)) {
1823 flags |= CF_UNSIGNED;
1826 flags |= CF_FORCECHAR;
1829 unsigned rtype = TypeOf (lval2.e_tptr) | (flags & CF_CONST);
1830 flags |= g_typeadjust (ltype, rtype);
1834 Gen->Func (flags, lval2.e_const);
1835 lval->e_flags = E_MEXPR;
1838 /* Result type is always int */
1839 lval->e_tptr = type_int;
1841 /* We have a rvalue now, condition codes are set */
1843 lval->e_test |= E_CC;
1851 static int hie9 (struct expent *lval)
1852 /* Process * and / operators. */
1854 static GenDesc* hie9_ops [] = {
1855 &GenMUL, &GenDIV, &GenMOD, 0
1859 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1864 static void parseadd (int k, struct expent* lval)
1865 /* Parse an expression with the binary plus operator. lval contains the
1866 * unprocessed left hand side of the expression and will contain the
1867 * result of the expression on return.
1870 struct expent lval2;
1871 unsigned flags; /* Operation flags */
1872 CodeMark Mark; /* Remember code position */
1873 type* lhst; /* Type of left hand side */
1874 type* rhst; /* Type of right hand side */
1877 /* Skip the PLUS token */
1880 /* Get the left hand side type, initialize operation flags */
1881 lhst = lval->e_tptr;
1884 /* Check for constness on both sides */
1885 if (k == 0 && lval->e_flags == E_MCONST) {
1887 /* The left hand side is a constant. Good. Get rhs */
1888 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1890 /* Right hand side is also constant. Get the rhs type */
1891 rhst = lval2.e_tptr;
1893 /* Both expressions are constants. Check for pointer arithmetic */
1894 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1895 /* Left is pointer, right is int, must scale rhs */
1896 lval->e_const = lval->e_const + lval2.e_const * PSizeOf (lhst);
1897 /* Result type is a pointer */
1898 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1899 /* Left is int, right is pointer, must scale lhs */
1900 lval->e_const = lval->e_const * PSizeOf (rhst) + lval2.e_const;
1901 /* Result type is a pointer */
1902 lval->e_tptr = lval2.e_tptr;
1903 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1904 /* Integer addition */
1905 lval->e_const += lval2.e_const;
1906 typeadjust (lval, &lval2, 1);
1909 Error ("Invalid operands for binary operator `+'");
1912 /* Result is constant, condition codes not set */
1913 lval->e_test = E_MCONST;
1917 /* lhs is constant, rhs is not. Get the rhs type. */
1918 rhst = lval2.e_tptr;
1920 /* Check for pointer arithmetic */
1921 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1922 /* Left is pointer, right is int, must scale rhs */
1923 g_scale (CF_INT, PSizeOf (lhst));
1924 /* Operate on pointers, result type is a pointer */
1926 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1927 /* Left is int, right is pointer, must scale lhs */
1928 lval->e_const *= PSizeOf (rhst);
1929 /* Operate on pointers, result type is a pointer */
1931 lval->e_tptr = lval2.e_tptr;
1932 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1933 /* Integer addition */
1934 flags = typeadjust (lval, &lval2, 1);
1937 Error ("Invalid operands for binary operator `+'");
1940 /* Generate code for the add */
1941 g_inc (flags | CF_CONST, lval->e_const);
1943 /* Result is in primary register */
1944 lval->e_flags = E_MEXPR;
1945 lval->e_test &= ~E_CC;
1951 /* Left hand side is not constant. Get the value onto the stack. */
1952 exprhs (CF_NONE, k, lval); /* --> primary register */
1953 Mark = GetCodePos ();
1954 g_push (TypeOf (lval->e_tptr), 0); /* --> stack */
1956 /* Evaluate the rhs */
1957 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1959 /* Right hand side is a constant. Get the rhs type */
1960 rhst = lval2.e_tptr;
1962 /* Remove pushed value from stack */
1964 pop (TypeOf (lval->e_tptr));
1966 /* Check for pointer arithmetic */
1967 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1968 /* Left is pointer, right is int, must scale rhs */
1969 lval2.e_const *= PSizeOf (lhst);
1970 /* Operate on pointers, result type is a pointer */
1972 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1973 /* Left is int, right is pointer, must scale lhs (ptr only) */
1974 g_scale (CF_INT | CF_CONST, PSizeOf (rhst));
1975 /* Operate on pointers, result type is a pointer */
1977 lval->e_tptr = lval2.e_tptr;
1978 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1979 /* Integer addition */
1980 flags = typeadjust (lval, &lval2, 1);
1983 Error ("Invalid operands for binary operator `+'");
1986 /* Generate code for the add */
1987 g_inc (flags | CF_CONST, lval2.e_const);
1989 /* Result is in primary register */
1990 lval->e_flags = E_MEXPR;
1991 lval->e_test &= ~E_CC;
1995 /* lhs and rhs are not constant. Get the rhs type. */
1996 rhst = lval2.e_tptr;
1998 /* Check for pointer arithmetic */
1999 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2000 /* Left is pointer, right is int, must scale rhs */
2001 g_scale (CF_INT, PSizeOf (lhst));
2002 /* Operate on pointers, result type is a pointer */
2004 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2005 /* Left is int, right is pointer, must scale lhs */
2006 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2007 g_swap (CF_INT); /* Swap TOS and primary */
2008 g_scale (CF_INT, PSizeOf (rhst));
2009 /* Operate on pointers, result type is a pointer */
2011 lval->e_tptr = lval2.e_tptr;
2012 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2013 /* Integer addition */
2014 flags = typeadjust (lval, &lval2, 0);
2017 Error ("Invalid operands for binary operator `+'");
2020 /* Generate code for the add */
2023 /* Result is in primary register */
2024 lval->e_flags = E_MEXPR;
2025 lval->e_test &= ~E_CC;
2034 static void parsesub (int k, struct expent* lval)
2035 /* Parse an expression with the binary minus operator. lval contains the
2036 * unprocessed left hand side of the expression and will contain the
2037 * result of the expression on return.
2040 struct expent lval2;
2041 unsigned flags; /* Operation flags */
2042 type* lhst; /* Type of left hand side */
2043 type* rhst; /* Type of right hand side */
2044 CodeMark Mark1; /* Save position of output queue */
2045 CodeMark Mark2; /* Another position in the queue */
2046 int rscale; /* Scale factor for the result */
2049 /* Skip the MINUS token */
2052 /* Get the left hand side type, initialize operation flags */
2053 lhst = lval->e_tptr;
2055 rscale = 1; /* Scale by 1, that is, don't scale */
2057 /* Remember the output queue position, then bring the value onto the stack */
2058 Mark1 = GetCodePos ();
2059 exprhs (CF_NONE, k, lval); /* --> primary register */
2060 Mark2 = GetCodePos ();
2061 g_push (TypeOf (lhst), 0); /* --> stack */
2063 /* Parse the right hand side */
2064 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2066 /* The right hand side is constant. Get the rhs type. */
2067 rhst = lval2.e_tptr;
2069 /* Check left hand side */
2070 if (k == 0 && lval->e_flags & E_MCONST) {
2072 /* Both sides are constant, remove generated code */
2074 pop (TypeOf (lhst)); /* Clean up the stack */
2076 /* Check for pointer arithmetic */
2077 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2078 /* Left is pointer, right is int, must scale rhs */
2079 lval->e_const -= lval2.e_const * PSizeOf (lhst);
2080 /* Operate on pointers, result type is a pointer */
2081 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2082 /* Left is pointer, right is pointer, must scale result */
2083 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2084 Error ("Incompatible pointer types");
2086 lval->e_const = (lval->e_const - lval2.e_const) / PSizeOf (lhst);
2088 /* Operate on pointers, result type is an integer */
2089 lval->e_tptr = type_int;
2090 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2091 /* Integer subtraction */
2092 typeadjust (lval, &lval2, 1);
2093 lval->e_const -= lval2.e_const;
2096 Error ("Invalid operands for binary operator `-'");
2099 /* Result is constant, condition codes not set */
2100 lval->e_flags = E_MCONST;
2101 lval->e_test &= ~E_CC;
2105 /* Left hand side is not constant, right hand side is.
2106 * Remove pushed value from stack.
2109 pop (TypeOf (lhst));
2111 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2112 /* Left is pointer, right is int, must scale rhs */
2113 lval2.e_const *= PSizeOf (lhst);
2114 /* Operate on pointers, result type is a pointer */
2116 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2117 /* Left is pointer, right is pointer, must scale result */
2118 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2119 Error ("Incompatible pointer types");
2121 rscale = PSizeOf (lhst);
2123 /* Operate on pointers, result type is an integer */
2125 lval->e_tptr = type_int;
2126 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2127 /* Integer subtraction */
2128 flags = typeadjust (lval, &lval2, 1);
2131 Error ("Invalid operands for binary operator `-'");
2134 /* Do the subtraction */
2135 g_dec (flags | CF_CONST, lval2.e_const);
2137 /* If this was a pointer subtraction, we must scale the result */
2139 g_scale (flags, -rscale);
2142 /* Result is in primary register */
2143 lval->e_flags = E_MEXPR;
2144 lval->e_test &= ~E_CC;
2150 /* Right hand side is not constant. Get the rhs type. */
2151 rhst = lval2.e_tptr;
2153 /* Check for pointer arithmetic */
2154 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2155 /* Left is pointer, right is int, must scale rhs */
2156 g_scale (CF_INT, PSizeOf (lhst));
2157 /* Operate on pointers, result type is a pointer */
2159 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2160 /* Left is pointer, right is pointer, must scale result */
2161 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2162 Error ("Incompatible pointer types");
2164 rscale = PSizeOf (lhst);
2166 /* Operate on pointers, result type is an integer */
2168 lval->e_tptr = type_int;
2169 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2170 /* Integer subtraction. If the left hand side descriptor says that
2171 * the lhs is const, we have to remove this mark, since this is no
2172 * longer true, lhs is on stack instead.
2174 if (lval->e_flags == E_MCONST) {
2175 lval->e_flags = E_MEXPR;
2177 /* Adjust operand types */
2178 flags = typeadjust (lval, &lval2, 0);
2181 Error ("Invalid operands for binary operator `-'");
2184 /* Generate code for the sub (the & is a hack here) */
2185 g_sub (flags & ~CF_CONST, 0);
2187 /* If this was a pointer subtraction, we must scale the result */
2189 g_scale (flags, -rscale);
2192 /* Result is in primary register */
2193 lval->e_flags = E_MEXPR;
2194 lval->e_test &= ~E_CC;
2200 static int hie8 (struct expent* lval)
2201 /* Process + and - binary operators. */
2203 int k = hie9 (lval);
2204 while (curtok == TOK_PLUS || curtok == TOK_MINUS) {
2206 if (curtok == TOK_PLUS) {
2219 static int hie7 (struct expent *lval)
2220 /* Parse << and >>. */
2222 static GenDesc* hie7_ops [] = {
2227 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2232 static int hie6 (struct expent *lval)
2233 /* process greater-than type comparators */
2235 static GenDesc* hie6_ops [] = {
2236 &GenLT, &GenLE, &GenGE, &GenGT, 0
2238 return hie_compare (hie6_ops, lval, hie7);
2243 static int hie5 (struct expent *lval)
2245 static GenDesc* hie5_ops[] = {
2248 return hie_compare (hie5_ops, lval, hie6);
2253 static int hie4 (struct expent* lval)
2254 /* Handle & (bitwise and) */
2256 static GenDesc* hie4_ops [] = {
2261 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2266 static int hie3 (struct expent *lval)
2267 /* Handle ^ (bitwise exclusive or) */
2269 static GenDesc* hie3_ops [] = {
2274 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2279 static int hie2 (struct expent *lval)
2280 /* Handle | (bitwise or) */
2282 static GenDesc* hie2_ops [] = {
2287 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2292 static int hieAnd (struct expent* lval, unsigned TrueLab, int* BoolOp)
2293 /* Process "exp && exp" */
2297 struct expent lval2;
2300 if (curtok == TOK_BOOL_AND) {
2302 /* Tell our caller that we're evaluating a boolean */
2305 /* Get a label that we will use for false expressions */
2308 /* If the expr hasn't set condition codes, set the force-test flag */
2309 if ((lval->e_test & E_CC) == 0) {
2310 lval->e_test |= E_FORCETEST;
2313 /* Load the value */
2314 exprhs (CF_FORCECHAR, k, lval);
2316 /* Generate the jump */
2317 g_falsejump (CF_NONE, lab);
2319 /* Parse more boolean and's */
2320 while (curtok == TOK_BOOL_AND) {
2327 if ((lval2.e_test & E_CC) == 0) {
2328 lval2.e_test |= E_FORCETEST;
2330 exprhs (CF_FORCECHAR, k, &lval2);
2332 /* Do short circuit evaluation */
2333 if (curtok == TOK_BOOL_AND) {
2334 g_falsejump (CF_NONE, lab);
2336 /* Last expression - will evaluate to true */
2337 g_truejump (CF_NONE, TrueLab);
2341 /* Define the false jump label here */
2342 g_defloclabel (lab);
2344 /* Define the label */
2345 lval->e_flags = E_MEXPR;
2346 lval->e_test |= E_CC; /* Condition codes are set */
2354 static int hieOr (struct expent *lval)
2355 /* Process "exp || exp". */
2358 struct expent lval2;
2359 int BoolOp = 0; /* Did we have a boolean op? */
2360 int AndOp; /* Did we have a && operation? */
2361 unsigned TrueLab; /* Jump to this label if true */
2365 TrueLab = GetLabel ();
2367 /* Call the next level parser */
2368 k = hieAnd (lval, TrueLab, &BoolOp);
2370 /* Any boolean or's? */
2371 if (curtok == TOK_BOOL_OR) {
2373 /* If the expr hasn't set condition codes, set the force-test flag */
2374 if ((lval->e_test & E_CC) == 0) {
2375 lval->e_test |= E_FORCETEST;
2378 /* Get first expr */
2379 exprhs (CF_FORCECHAR, k, lval);
2381 /* For each expression jump to TrueLab if true. Beware: If we
2382 * had && operators, the jump is already in place!
2385 g_truejump (CF_NONE, TrueLab);
2388 /* Remember that we had a boolean op */
2391 /* while there's more expr */
2392 while (curtok == TOK_BOOL_OR) {
2399 k = hieAnd (&lval2, TrueLab, &AndOp);
2400 if ((lval2.e_test & E_CC) == 0) {
2401 lval2.e_test |= E_FORCETEST;
2403 exprhs (CF_FORCECHAR, k, &lval2);
2405 /* If there is more to come, add shortcut boolean eval.
2406 * Beware: If we had && operators, the jump is already
2410 /* Seems this sometimes generates wrong code */
2411 if (curtok == TOK_BOOL_OR && !AndOp) {
2412 g_truejump (CF_NONE, TrueLab);
2415 g_truejump (CF_NONE, TrueLab);
2418 lval->e_flags = E_MEXPR;
2419 lval->e_test |= E_CC; /* Condition codes are set */
2423 /* If we really had boolean ops, generate the end sequence */
2425 DoneLab = GetLabel ();
2426 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2427 g_falsejump (CF_NONE, DoneLab);
2428 g_defloclabel (TrueLab);
2429 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2430 g_defloclabel (DoneLab);
2437 static int hieQuest (struct expent *lval)
2438 /* Parse "lvalue ? exp : exp" */
2443 struct expent lval2; /* Expression 2 */
2444 struct expent lval3; /* Expression 3 */
2445 type* type2; /* Type of expression 2 */
2446 type* type3; /* Type of expression 3 */
2447 type* rtype; /* Type of result */
2448 CodeMark Mark1; /* Save position in output code */
2449 CodeMark Mark2; /* Save position in output code */
2454 if (curtok == TOK_QUEST) {
2456 if ((lval->e_test & E_CC) == 0) {
2457 /* Condition codes not set, force a test */
2458 lval->e_test |= E_FORCETEST;
2460 exprhs (CF_NONE, k, lval);
2462 g_falsejump (CF_NONE, labf);
2464 /* Parse second and third expression */
2465 expression1 (&lval2);
2469 g_defloclabel (labf);
2470 expression1 (&lval3);
2472 /* Check if any conversions are needed, if so, do them.
2473 * Conversion rules for ?: expression are:
2474 * - if both expressions are int expressions, default promotion
2475 * rules for ints apply.
2476 * - if both expressions are pointers of the same type, the
2477 * result of the expression is of this type.
2478 * - if one of the expressions is a pointer and the other is
2479 * a zero constant, the resulting type is that of the pointer
2481 * - all other cases are flagged by an error.
2483 type2 = lval2.e_tptr;
2484 type3 = lval3.e_tptr;
2485 if (IsClassInt (type2) && IsClassInt (type3)) {
2487 /* Get common type */
2488 rtype = promoteint (type2, type3);
2490 /* Convert the third expression to this type if needed */
2491 g_typecast (TypeOf (rtype), TypeOf (type3));
2493 /* Setup a new label so that the expr3 code will jump around
2494 * the type cast code for expr2.
2496 labf = GetLabel (); /* Get new label */
2497 Mark1 = GetCodePos (); /* Remember current position */
2498 g_jump (labf); /* Jump around code */
2500 /* The jump for expr2 goes here */
2501 g_defloclabel (labt);
2503 /* Create the typecast code for expr2 */
2504 Mark2 = GetCodePos (); /* Remember position */
2505 g_typecast (TypeOf (rtype), TypeOf (type2));
2507 /* If the typecast did not produce code, remove the jump,
2508 * otherwise output the label.
2510 if (GetCodePos() == Mark2) {
2511 RemoveCode (Mark1); /* Remove code */
2513 /* We have typecast code, output label */
2514 g_defloclabel (labf);
2515 labt = 0; /* Mark other label as invalid */
2518 } else if (IsClassPtr (type2) && IsClassPtr (type3)) {
2519 /* Must point to same type */
2520 if (TypeCmp (Indirect (type2), Indirect (type3)) < TC_EQUAL) {
2521 Error ("Incompatible pointer types");
2523 /* Result has the common type */
2524 rtype = lval2.e_tptr;
2525 } else if (IsClassPtr (type2) && IsNullPtr (&lval3)) {
2526 /* Result type is pointer, no cast needed */
2527 rtype = lval2.e_tptr;
2528 } else if (IsNullPtr (&lval2) && IsClassPtr (type3)) {
2529 /* Result type is pointer, no cast needed */
2530 rtype = lval3.e_tptr;
2532 Error ("Incompatible types");
2533 rtype = lval2.e_tptr; /* Doesn't matter here */
2536 /* If we don't have the label defined until now, do it */
2538 g_defloclabel (labt);
2541 /* Setup the target expression */
2542 lval->e_flags = E_MEXPR;
2543 lval->e_tptr = rtype;
2551 static void opeq (GenDesc* Gen, struct expent *lval, int k)
2552 /* Process "op=" operators. */
2554 struct expent lval2;
2561 Error ("Invalid lvalue in assignment");
2565 /* Determine the type of the lhs */
2566 flags = TypeOf (lval->e_tptr);
2567 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2568 lval->e_tptr [0] == T_PTR;
2570 /* Get the lhs address on stack (if needed) */
2573 /* Fetch the lhs into the primary register if needed */
2574 exprhs (CF_NONE, k, lval);
2576 /* Bring the lhs on stack */
2577 Mark = GetCodePos ();
2580 /* Evaluate the rhs */
2581 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2582 /* The resulting value is a constant. If the generator has the NOPUSH
2583 * flag set, don't push the lhs.
2585 if (Gen->Flags & GEN_NOPUSH) {
2590 /* lhs is a pointer, scale rhs */
2591 lval2.e_const *= SizeOf (lval->e_tptr+1);
2594 /* If the lhs is character sized, the operation may be later done
2597 if (SizeOf (lval->e_tptr) == 1) {
2598 flags |= CF_FORCECHAR;
2601 /* Special handling for add and sub - some sort of a hack, but short code */
2602 if (Gen->Func == g_add) {
2603 g_inc (flags | CF_CONST, lval2.e_const);
2604 } else if (Gen->Func == g_sub) {
2605 g_dec (flags | CF_CONST, lval2.e_const);
2607 Gen->Func (flags | CF_CONST, lval2.e_const);
2610 /* rhs is not constant and already in the primary register */
2612 /* lhs is a pointer, scale rhs */
2613 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2616 /* If the lhs is character sized, the operation may be later done
2619 if (SizeOf (lval->e_tptr) == 1) {
2620 flags |= CF_FORCECHAR;
2623 /* Adjust the types of the operands if needed */
2624 Gen->Func (g_typeadjust (flags, TypeOf (lval2.e_tptr)), 0);
2627 lval->e_flags = E_MEXPR;
2632 static void addsubeq (GenDesc* Gen, struct expent *lval, int k)
2633 /* Process the += and -= operators */
2635 struct expent lval2;
2641 Error ("Invalid lvalue in assignment");
2646 /* We're currently only able to handle some adressing modes */
2647 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
2648 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
2649 (lval->e_flags & E_MCONST) == 0) { /* Constant address? */
2650 /* Use generic routine */
2651 opeq (Gen, lval, k);
2655 /* Skip the operator */
2658 /* Check if we have a pointer expression and must scale rhs */
2659 MustScale = (lval->e_tptr [0] == T_PTR);
2661 /* Determine the code generator flags */
2662 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR;
2664 /* Evaluate the rhs */
2665 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2666 /* The resulting value is a constant. */
2668 /* lhs is a pointer, scale rhs */
2669 lval2.e_const *= SizeOf (lval->e_tptr+1);
2673 /* rhs is not constant and already in the primary register */
2675 /* lhs is a pointer, scale rhs */
2676 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2680 /* Adjust the rhs to the lhs */
2681 g_typeadjust (flags, TypeOf (lval2.e_tptr));
2683 /* Output apropriate code */
2684 if (lval->e_flags & E_MGLOBAL) {
2685 /* Static variable */
2686 flags |= GlobalModeFlags (lval->e_flags);
2687 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2688 g_addeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2690 g_subeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2692 } else if (lval->e_flags & E_MLOCAL) {
2693 /* ref to localvar */
2694 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2695 g_addeqlocal (flags, lval->e_const, lval2.e_const);
2697 g_subeqlocal (flags, lval->e_const, lval2.e_const);
2699 } else if (lval->e_flags & E_MCONST) {
2700 /* ref to absolute address */
2701 flags |= CF_ABSOLUTE;
2702 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2703 g_addeqstatic (flags, lval->e_const, 0, lval2.e_const);
2705 g_subeqstatic (flags, lval->e_const, 0, lval2.e_const);
2707 } else if (lval->e_flags & E_MEXPR) {
2708 /* Address in a/x. */
2709 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2710 g_addeqind (flags, lval->e_const, lval2.e_const);
2712 g_subeqind (flags, lval->e_const, lval2.e_const);
2715 Internal ("Invalid addressing mode");
2718 /* Expression is in the primary now */
2719 lval->e_flags = E_MEXPR;
2724 static void Assignment (struct expent* lval)
2725 /* Parse an assignment */
2728 struct expent lval2;
2730 type* ltype = lval->e_tptr;
2732 /* Check for assignment to const */
2733 if (IsQualConst (ltype)) {
2734 Error ("Assignment to const");
2737 /* cc65 does not have full support for handling structs by value. Since
2738 * assigning structs is one of the more useful operations from this
2739 * familiy, allow it here.
2741 if (IsClassStruct (ltype)) {
2743 /* Bring the address of the lhs into the primary and push it */
2744 exprhs (0, 0, lval);
2745 g_push (CF_PTR | CF_UNSIGNED, 0);
2747 /* Get the expression on the right of the '=' into the primary */
2750 /* Get the address */
2751 exprhs (0, 0, &lval2);
2753 /* We need an lvalue */
2754 Error ("Invalid lvalue in assignment");
2757 /* Push the address (or whatever is in ax in case of errors) */
2758 g_push (CF_PTR | CF_UNSIGNED, 0);
2760 /* Check for equality of the structs */
2761 if (TypeCmp (ltype, lval2.e_tptr) < TC_EQUAL) {
2762 Error ("Incompatible types");
2765 /* Load the size of the struct into the primary */
2766 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, SizeOf (ltype), 0);
2768 /* Call the memcpy function */
2769 g_call (CF_FIXARGC, "memcpy", 4);
2773 /* Get the address on stack if needed */
2776 /* No struct, setup flags for the load */
2777 flags = SizeOf (ltype) == 1? CF_FORCECHAR : CF_NONE;
2779 /* Get the expression on the right of the '=' into the primary */
2780 if (evalexpr (flags, hie1, &lval2) == 0) {
2781 /* Constant expression. Adjust the types */
2782 assignadjust (ltype, &lval2);
2783 /* Put the value into the primary register */
2784 lconst (flags, &lval2);
2786 /* Expression is not constant and already in the primary */
2787 assignadjust (ltype, &lval2);
2790 /* Generate a store instruction */
2795 /* Value is still in primary */
2796 lval->e_flags = E_MEXPR;
2801 int hie1 (struct expent* lval)
2802 /* Parse first level of expression hierarchy. */
2806 k = hieQuest (lval);
2816 Error ("Invalid lvalue in assignment");
2822 case TOK_PLUS_ASSIGN:
2823 addsubeq (&GenPASGN, lval, k);
2826 case TOK_MINUS_ASSIGN:
2827 addsubeq (&GenSASGN, lval, k);
2830 case TOK_MUL_ASSIGN:
2831 opeq (&GenMASGN, lval, k);
2834 case TOK_DIV_ASSIGN:
2835 opeq (&GenDASGN, lval, k);
2838 case TOK_MOD_ASSIGN:
2839 opeq (&GenMOASGN, lval, k);
2842 case TOK_SHL_ASSIGN:
2843 opeq (&GenSLASGN, lval, k);
2846 case TOK_SHR_ASSIGN:
2847 opeq (&GenSRASGN, lval, k);
2850 case TOK_AND_ASSIGN:
2851 opeq (&GenAASGN, lval, k);
2854 case TOK_XOR_ASSIGN:
2855 opeq (&GenXOASGN, lval, k);
2859 opeq (&GenOASGN, lval, k);
2870 int hie0 (struct expent *lval)
2871 /* Parse comma operator. */
2876 while (curtok == TOK_COMMA) {
2885 int evalexpr (unsigned flags, int (*f) (struct expent*), struct expent* lval)
2886 /* Will evaluate an expression via the given function. If the result is a
2887 * constant, 0 is returned and the value is put in the lval struct. If the
2888 * result is not constant, exprhs is called to bring the value into the
2889 * primary register and 1 is returned.
2896 if (k == 0 && lval->e_flags == E_MCONST) {
2897 /* Constant expression */
2900 /* Not constant, load into the primary */
2901 exprhs (flags, k, lval);
2908 int expr (int (*func) (), struct expent *lval)
2909 /* Expression parser; func is either hie0 or hie1. */
2918 /* Do some checks if code generation is still constistent */
2919 if (savsp != oursp) {
2921 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
2923 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
2931 void expression1 (struct expent* lval)
2932 /* Evaluate an expression on level 1 (no comma operator) and put it into
2933 * the primary register
2936 memset (lval, 0, sizeof (*lval));
2937 exprhs (CF_NONE, expr (hie1, lval), lval);
2942 void expression (struct expent* lval)
2943 /* Evaluate an expression and put it into the primary register */
2945 memset (lval, 0, sizeof (*lval));
2946 exprhs (CF_NONE, expr (hie0, lval), lval);
2951 void constexpr (struct expent* lval)
2952 /* Get a constant value */
2954 memset (lval, 0, sizeof (*lval));
2955 if (expr (hie1, lval) != 0 || (lval->e_flags & E_MCONST) == 0) {
2956 Error ("Constant expression expected");
2957 /* To avoid any compiler errors, make the expression a valid const */
2958 lval->e_flags = E_MCONST;
2959 lval->e_tptr = type_int;
2966 void intexpr (struct expent* lval)
2967 /* Get an integer expression */
2970 if (!IsClassInt (lval->e_tptr)) {
2971 Error ("Integer expression expected");
2972 /* To avoid any compiler errors, make the expression a valid int */
2973 lval->e_flags = E_MCONST;
2974 lval->e_tptr = type_int;
2981 void boolexpr (struct expent* lval)
2982 /* Get a boolean expression */
2984 /* Read an expression */
2987 /* If it's an integer, it's ok. If it's not an integer, but a pointer,
2988 * the pointer used in a boolean context is also ok
2990 if (!IsClassInt (lval->e_tptr) && !IsClassPtr (lval->e_tptr)) {
2991 Error ("Boolean expression expected");
2992 /* To avoid any compiler errors, make the expression a valid int */
2993 lval->e_flags = E_MCONST;
2994 lval->e_tptr = type_int;
3001 void test (unsigned label, int cond)
3002 /* Generate code to perform test and jump if false. */
3007 /* Eat the parenthesis */
3010 /* Prepare the expression, setup labels */
3011 memset (&lval, 0, sizeof (lval));
3012 lval.e_test = E_TEST;
3014 /* Generate code to eval the expr */
3015 k = expr (hie0, &lval);
3016 if (k == 0 && lval.e_flags == E_MCONST) {
3017 /* Constant rvalue */
3018 if (cond == 0 && lval.e_const == 0) {
3020 Warning ("Unreachable code");
3021 } else if (cond && lval.e_const) {
3028 /* If the expr hasn't set condition codes, set the force-test flag */
3029 if ((lval.e_test & E_CC) == 0) {
3030 lval.e_test |= E_FORCETEST;
3033 /* Load the value into the primary register */
3034 exprhs (CF_FORCECHAR, k, &lval);
3036 /* Check for the closing brace */
3039 /* Generate the jump */
3041 g_truejump (CF_NONE, label);
3043 /* Special case (putting this here is a small hack - but hey, the
3044 * compiler itself is one big hack...): If a semicolon follows, we
3045 * don't have a statement and may omit the jump.
3047 if (curtok != TOK_SEMI) {
3048 g_falsejump (CF_NONE, label);