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 /* Beware: If there are parameters with identical names, they
565 * cannot go into the same symbol table, which means that in this
566 * case of errorneous input, the number of nodes in the symbol
567 * table and ParamCount are NOT equal. We have to handle this case
568 * below to avoid segmentation violations. Since we know that this
569 * problem can only occur if there is more than one parameter,
570 * we will just use the last one.
572 if (ParamCount == 1) {
574 Param = Func->SymTab->SymHead;
575 } else if (Param->NextSym != 0) {
577 Param = Param->NextSym;
578 CHECK ((Param->Flags & SC_PARAM) != 0);
580 } else if (!Ellipsis) {
581 /* Too many arguments. Do we have an open param list? */
582 if ((Func->Flags & FD_ELLIPSIS) == 0) {
583 /* End of param list reached, no ellipsis */
584 Error ("Too many arguments in function call");
586 /* Assume an ellipsis even in case of errors to avoid an error
587 * message for each other argument.
592 /* Do some optimization: If we have a constant value to push,
593 * use a special function that may optimize.
596 if (!Ellipsis && SizeOf (Param->Type) == 1) {
597 CFlags = CF_FORCECHAR;
600 if (evalexpr (CFlags, hie1, &lval2) == 0) {
601 /* A constant value */
605 /* If we don't have an argument spec, accept anything, otherwise
606 * convert the actual argument to the type needed.
609 /* Promote the argument if needed */
610 assignadjust (Param->Type, &lval2);
612 /* If we have a prototype, chars may be pushed as chars */
613 Flags |= CF_FORCECHAR;
616 /* Use the type of the argument for the push */
617 Flags |= TypeOf (lval2.e_tptr);
619 /* If this is a fastcall function, don't push the last argument */
620 if (ParamCount == Func->ParamCount && (Func->Flags & FD_FASTCALL) != 0) {
621 /* Just load the argument into the primary. This is only needed if
622 * we have a constant argument, otherwise the value is already in
625 if (Flags & CF_CONST) {
626 exprhs (CF_FORCECHAR, 0, &lval2);
629 /* Push the argument, count the argument size */
630 g_push (Flags, lval2.e_const);
631 ParamSize += sizeofarg (Flags);
634 /* Add an optimizer hint */
635 AddCodeHint ("param:end");
637 /* Check for end of argument list */
638 if (curtok != TOK_COMMA) {
644 /* We need the closing bracket here */
647 /* Check if we had enough parameters */
648 if (ParamCount < Func->ParamCount) {
649 Error ("Too few arguments in function call");
653 if (lval->e_flags & E_MEXPR) {
654 /* Function called via pointer: Restore it and call function */
655 if (ParamSize != 0) {
658 /* We had no parameters - remove save code */
661 g_callind (TypeOf (lval->e_tptr), ParamSize);
663 g_call (TypeOf (lval->e_tptr), (char*) lval->e_name, ParamSize);
670 /* This function parses ASM statements. The syntax of the ASM directive
671 * looks like the one defined for C++ (C has no ASM directive), that is,
672 * a string literal in parenthesis.
678 /* Need left parenthesis */
682 if (curtok != TOK_SCONST) {
683 Error ("String literal expected");
685 /* Write the string directly into the output, followed by a newline */
686 AddCodeLine (GetLiteral (curval));
688 /* Reset the string pointer, effectivly clearing the string from the
689 * string table. Since we're working with one token lookahead, this
690 * will fail if the next token is also a string token, but that's a
691 * syntax error anyway, because we expect a right paren.
693 ResetLiteralOffs (curval);
696 /* Skip the string token */
699 /* Closing paren needed */
705 static int primary (struct expent* lval)
706 /* This is the lowest level of the expression parser. */
710 /* not a test at all, yet */
713 /* Character and integer constants. */
714 if (curtok == TOK_ICONST || curtok == TOK_CCONST) {
715 lval->e_flags = E_MCONST | E_TCONST;
716 lval->e_tptr = curtype;
717 lval->e_const = curval;
722 /* Process parenthesized subexpression by calling the whole parser
725 if (curtok == TOK_LPAREN) {
727 memset (lval, 0, sizeof (*lval)); /* Remove any attributes */
733 /* All others may only be used if the expression evaluation is not called
734 * recursively by the preprocessor.
737 /* Illegal expression in PP mode */
738 Error ("Preprocessor expression expected");
739 lval->e_flags = E_MCONST;
740 lval->e_tptr = type_int;
745 if (curtok == TOK_IDENT) {
750 /* Get a pointer to the symbol table entry */
751 Sym = FindSym (CurTok.Ident);
753 /* Is the symbol known? */
756 /* We found the symbol - skip the name token */
759 /* The expression type is the symbol type */
760 lval->e_tptr = Sym->Type;
762 /* Check for illegal symbol types */
763 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
764 if (Sym->Flags & SC_TYPE) {
765 /* Cannot use type symbols */
766 Error ("Variable identifier expected");
767 /* Assume an int type to make lval valid */
768 lval->e_flags = E_MLOCAL | E_TLOFFS;
769 lval->e_tptr = type_int;
774 /* Check for legal symbol types */
775 if ((Sym->Flags & SC_CONST) == SC_CONST) {
776 /* Enum or some other numeric constant */
777 lval->e_flags = E_MCONST;
778 lval->e_const = Sym->V.ConstVal;
780 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
782 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
783 lval->e_name = (unsigned long) Sym->Name;
785 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
786 /* Local variable. If this is a parameter for a variadic
787 * function, we have to add some address calculations, and the
788 * address is not const.
790 if ((Sym->Flags & SC_PARAM) == SC_PARAM && IsVariadic (CurrentFunc)) {
791 /* Variadic parameter */
792 g_leavariadic (Sym->V.Offs - GetParamSize (CurrentFunc));
793 lval->e_flags = E_MEXPR;
796 /* Normal parameter */
797 lval->e_flags = E_MLOCAL | E_TLOFFS;
798 lval->e_const = Sym->V.Offs;
800 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
801 /* Static variable */
802 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
803 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
804 lval->e_name = (unsigned long) Sym->Name;
806 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
807 lval->e_name = Sym->V.Label;
810 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
811 /* Register variable, zero page based */
812 lval->e_flags = E_MGLOBAL | E_MCONST | E_TREGISTER;
813 lval->e_name = Sym->V.Offs;
816 /* Local static variable */
817 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
818 lval->e_name = Sym->V.Offs;
822 /* The symbol is referenced now */
823 Sym->Flags |= SC_REF;
824 if (IsTypeFunc (lval->e_tptr) || IsTypeArray (lval->e_tptr)) {
830 /* We did not find the symbol. Remember the name, then skip it */
831 strcpy (Ident, CurTok.Ident);
834 /* IDENT is either an auto-declared function or an undefined variable. */
835 if (curtok == TOK_LPAREN) {
836 /* Declare a function returning int. For that purpose, prepare a
837 * function signature for a function having an empty param list
840 Warning ("Function call without a prototype");
841 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
842 lval->e_tptr = Sym->Type;
843 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
844 lval->e_name = (unsigned long) Sym->Name;
850 /* Undeclared Variable */
851 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
852 lval->e_flags = E_MLOCAL | E_TLOFFS;
853 lval->e_tptr = type_int;
855 Error ("Undefined symbol: `%s'", Ident);
861 /* String literal? */
862 if (curtok == TOK_SCONST) {
863 lval->e_flags = E_MCONST | E_TLIT;
864 lval->e_const = curval;
865 lval->e_tptr = GetCharArrayType (strlen (GetLiteral (curval)));
871 if (curtok == TOK_ASM) {
873 lval->e_tptr = type_void;
874 lval->e_flags = E_MEXPR;
879 /* __AX__ and __EAX__ pseudo values? */
880 if (curtok == TOK_AX || curtok == TOK_EAX) {
881 lval->e_tptr = (curtok == TOK_AX)? type_uint : type_ulong;
882 lval->e_flags = E_MREG;
883 lval->e_test &= ~E_CC;
886 return 1; /* May be used as lvalue */
889 /* Illegal primary. */
890 Error ("Expression expected");
891 lval->e_flags = E_MCONST;
892 lval->e_tptr = type_int;
898 static int arrayref (int k, struct expent* lval)
899 /* Handle an array reference */
913 /* Skip the bracket */
916 /* Get the type of left side */
917 tptr1 = lval->e_tptr;
919 /* We can apply a special treatment for arrays that have a const base
920 * address. This is true for most arrays and will produce a lot better
921 * code. Check if this is a const base address.
923 lflags = lval->e_flags & ~E_MCTYPE;
924 ConstBaseAddr = (lflags == E_MCONST) || /* Constant numeric address */
925 (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
926 lflags == E_MLOCAL; /* Local array */
928 /* If we have a constant base, we delay the address fetch */
929 Mark1 = GetCodePos ();
930 Mark2 = 0; /* Silence gcc */
931 if (!ConstBaseAddr) {
932 /* Get a pointer to the array into the primary */
933 exprhs (CF_NONE, k, lval);
935 /* Get the array pointer on stack. Do not push more than 16
936 * bit, even if this value is greater, since we cannot handle
937 * other than 16bit stuff when doing indexing.
939 Mark2 = GetCodePos ();
943 /* TOS now contains ptr to array elements. Get the subscript. */
945 if (l == 0 && lval2.e_flags == E_MCONST) {
947 /* The array subscript is a constant - remove value from stack */
948 if (!ConstBaseAddr) {
952 /* Get an array pointer into the primary */
953 exprhs (CF_NONE, k, lval);
956 if (IsClassPtr (tptr1)) {
958 /* Scale the subscript value according to element size */
959 lval2.e_const *= PSizeOf (tptr1);
961 /* Remove code for lhs load */
964 /* Handle constant base array on stack. Be sure NOT to
965 * handle pointers the same way, this won't work.
967 if (IsTypeArray (tptr1) &&
968 ((lval->e_flags & ~E_MCTYPE) == E_MCONST ||
969 (lval->e_flags & ~E_MCTYPE) == E_MLOCAL ||
970 (lval->e_flags & E_MGLOBAL) != 0 ||
971 (lval->e_flags == E_MEOFFS))) {
972 lval->e_const += lval2.e_const;
975 /* Pointer - load into primary and remember offset */
976 if ((lval->e_flags & E_MEXPR) == 0 || k != 0) {
977 exprhs (CF_NONE, k, lval);
979 lval->e_const = lval2.e_const;
980 lval->e_flags = E_MEOFFS;
983 /* Result is of element type */
984 lval->e_tptr = Indirect (tptr1);
989 } else if (IsClassPtr (tptr2 = lval2.e_tptr)) {
990 /* Subscript is pointer, get element type */
991 lval2.e_tptr = Indirect (tptr2);
993 /* Scale the rhs value in the primary register */
994 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
996 lval->e_tptr = lval2.e_tptr;
998 Error ("Cannot subscript");
1001 /* Add the subscript. Since arrays are indexed by integers,
1002 * we will ignore the true type of the subscript here and
1003 * use always an int.
1005 g_inc (CF_INT | CF_CONST, lval2.e_const);
1009 /* Array subscript is not constant. Load it into the primary */
1010 Mark2 = GetCodePos ();
1011 exprhs (CF_NONE, l, &lval2);
1013 tptr2 = lval2.e_tptr;
1014 if (IsClassPtr (tptr1)) {
1016 /* Get the element type */
1017 lval->e_tptr = Indirect (tptr1);
1019 /* Indexing is based on int's, so we will just use the integer
1020 * portion of the index (which is in (e)ax, so there's no further
1023 g_scale (CF_INT, SizeOf (lval->e_tptr));
1025 } else if (IsClassPtr (tptr2)) {
1027 /* Get the element type */
1028 lval2.e_tptr = Indirect (tptr2);
1030 /* Get the int value on top. If we go here, we're sure,
1031 * both values are 16 bit (the first one was truncated
1032 * if necessary and the second one is a pointer).
1033 * Note: If ConstBaseAddr is true, we don't have a value on
1034 * stack, so to "swap" both, just push the subscript.
1036 if (ConstBaseAddr) {
1038 exprhs (CF_NONE, k, lval);
1045 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
1046 lval->e_tptr = lval2.e_tptr;
1048 Error ("Cannot subscript");
1051 /* The offset is now in the primary register. It didn't have a
1052 * constant base address for the lhs, the lhs address is already
1053 * on stack, and we must add the offset. If the base address was
1054 * constant, we call special functions to add the address to the
1057 if (!ConstBaseAddr) {
1058 /* Add the subscript. Both values are int sized. */
1062 /* If the subscript has itself a constant address, it is often
1063 * a better idea to reverse again the order of the evaluation.
1064 * This will generate better code if the subscript is a byte
1065 * sized variable. But beware: This is only possible if the
1066 * subscript was not scaled, that is, if this was a byte array
1069 rflags = lval2.e_flags & ~E_MCTYPE;
1070 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1071 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1072 rflags == E_MLOCAL; /* Local array */
1074 if (ConstSubAddr && SizeOf (lval->e_tptr) == 1) {
1078 /* Reverse the order of evaluation */
1079 unsigned flags = (SizeOf (lval2.e_tptr) == 1)? CF_CHAR : CF_INT;
1082 /* Get a pointer to the array into the primary. We have changed
1083 * e_tptr above but we need the original type to load the
1084 * address, so restore it temporarily.
1086 SavedType = lval->e_tptr;
1087 lval->e_tptr = tptr1;
1088 exprhs (CF_NONE, k, lval);
1089 lval->e_tptr = SavedType;
1091 /* Add the variable */
1092 if (rflags == E_MLOCAL) {
1093 g_addlocal (flags, lval2.e_const);
1095 flags |= GlobalModeFlags (lval2.e_flags);
1096 g_addstatic (flags, lval2.e_name, lval2.e_const);
1099 if (lflags == E_MCONST) {
1100 /* Constant numeric address. Just add it */
1101 g_inc (CF_INT | CF_UNSIGNED, lval->e_const);
1102 } else if (lflags == E_MLOCAL) {
1103 /* Base address is a local variable address */
1104 if (IsTypeArray (tptr1)) {
1105 g_addaddr_local (CF_INT, lval->e_const);
1107 g_addlocal (CF_PTR, lval->e_const);
1110 /* Base address is a static variable address */
1111 unsigned flags = CF_INT;
1112 flags |= GlobalModeFlags (lval->e_flags);
1113 if (IsTypeArray (tptr1)) {
1114 g_addaddr_static (flags, lval->e_name, lval->e_const);
1116 g_addstatic (flags, lval->e_name, lval->e_const);
1122 lval->e_flags = E_MEXPR;
1125 return !IsTypeArray (lval->e_tptr);
1131 static int structref (int k, struct expent* lval)
1132 /* Process struct field after . or ->. */
1138 /* Skip the token and check for an identifier */
1140 if (curtok != TOK_IDENT) {
1141 Error ("Identifier expected");
1142 lval->e_tptr = type_int;
1146 /* Get the symbol table entry and check for a struct field */
1147 strcpy (Ident, CurTok.Ident);
1149 Field = FindStructField (lval->e_tptr, Ident);
1151 Error ("Struct/union has no field named `%s'", Ident);
1152 lval->e_tptr = type_int;
1156 /* If we have constant input data, the result is also constant */
1157 flags = lval->e_flags & ~E_MCTYPE;
1158 if (flags == E_MCONST ||
1159 (k == 0 && (flags == E_MLOCAL ||
1160 (flags & E_MGLOBAL) != 0 ||
1161 lval->e_flags == E_MEOFFS))) {
1162 lval->e_const += Field->V.Offs;
1164 if ((flags & E_MEXPR) == 0 || k != 0) {
1165 exprhs (CF_NONE, k, lval);
1167 lval->e_const = Field->V.Offs;
1168 lval->e_flags = E_MEOFFS;
1170 lval->e_tptr = Field->Type;
1171 return !IsTypeArray (Field->Type);
1176 static int hie11 (struct expent *lval)
1177 /* Handle compound types (structs and arrays) */
1184 if (curtok < TOK_LBRACK || curtok > TOK_PTR_REF) {
1191 if (curtok == TOK_LBRACK) {
1193 /* Array reference */
1194 k = arrayref (k, lval);
1196 } else if (curtok == TOK_LPAREN) {
1198 /* Function call. Skip the opening parenthesis */
1200 tptr = lval->e_tptr;
1201 if (IsTypeFunc (tptr) || IsTypeFuncPtr (tptr)) {
1202 if (IsTypeFuncPtr (tptr)) {
1203 /* Pointer to function. Handle transparently */
1204 exprhs (CF_NONE, k, lval); /* Function pointer to A/X */
1205 ++lval->e_tptr; /* Skip T_PTR */
1206 lval->e_flags |= E_MEXPR;
1208 callfunction (lval);
1209 lval->e_flags = E_MEXPR;
1210 lval->e_tptr += DECODE_SIZE + 1; /* Set to result */
1212 Error ("Illegal function call");
1216 } else if (curtok == TOK_DOT) {
1218 if (!IsClassStruct (lval->e_tptr)) {
1219 Error ("Struct expected");
1221 k = structref (0, lval);
1223 } else if (curtok == TOK_PTR_REF) {
1225 tptr = lval->e_tptr;
1226 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1227 Error ("Struct pointer expected");
1229 k = structref (k, lval);
1239 static void store (struct expent* lval)
1240 /* Store primary reg into this reference */
1246 flags = TypeOf (lval->e_tptr);
1247 if (f & E_MGLOBAL) {
1248 flags |= GlobalModeFlags (f);
1255 g_putstatic (flags, lval->e_name, lval->e_const);
1257 } else if (f & E_MLOCAL) {
1258 g_putlocal (flags, lval->e_const);
1259 } else if (f == E_MEOFFS) {
1260 g_putind (flags, lval->e_const);
1261 } else if (f != E_MREG) {
1263 g_putind (flags, 0);
1265 /* Store into absolute address */
1266 g_putstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
1270 /* Assume that each one of the stores will invalidate CC */
1271 lval->e_test &= ~E_CC;
1276 static void pre_incdec (struct expent* lval, void (*inc) (unsigned, unsigned long))
1277 /* Handle --i and ++i */
1284 if ((k = hie10 (lval)) == 0) {
1285 Error ("Invalid lvalue");
1289 /* Get the data type */
1290 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR | CF_CONST;
1292 /* Get the increment value in bytes */
1293 val = (lval->e_tptr [0] == T_PTR)? PSizeOf (lval->e_tptr) : 1;
1295 /* We're currently only able to handle some adressing modes */
1296 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
1297 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
1298 (lval->e_flags & E_MCONST) == 0 && /* Constant address? */
1299 (lval->e_flags & E_MEXPR) == 0) { /* Address in a/x? */
1301 /* Use generic code. Push the address if needed */
1304 /* Fetch the value */
1305 exprhs (CF_NONE, k, lval);
1307 /* Increment value in primary */
1310 /* Store the result back */
1315 /* Special code for some addressing modes - use the special += ops */
1316 if (lval->e_flags & E_MGLOBAL) {
1317 flags |= GlobalModeFlags (lval->e_flags);
1319 g_addeqstatic (flags, lval->e_name, lval->e_const, val);
1321 g_subeqstatic (flags, lval->e_name, lval->e_const, val);
1323 } else if (lval->e_flags & E_MLOCAL) {
1324 /* ref to localvar */
1326 g_addeqlocal (flags, lval->e_const, val);
1328 g_subeqlocal (flags, lval->e_const, val);
1330 } else if (lval->e_flags & E_MCONST) {
1331 /* ref to absolute address */
1332 flags |= CF_ABSOLUTE;
1334 g_addeqstatic (flags, lval->e_const, 0, val);
1336 g_subeqstatic (flags, lval->e_const, 0, val);
1338 } else if (lval->e_flags & E_MEXPR) {
1339 /* Address in a/x, check if we have an offset */
1340 unsigned Offs = (lval->e_flags == E_MEOFFS)? lval->e_const : 0;
1342 g_addeqind (flags, Offs, val);
1344 g_subeqind (flags, Offs, val);
1347 Internal ("Invalid addressing mode");
1352 /* Result is an expression */
1353 lval->e_flags = E_MEXPR;
1358 static void post_incdec (struct expent *lval, int k, void (*inc) (unsigned, unsigned long))
1359 /* Handle i-- and i++ */
1365 Error ("Invalid lvalue");
1369 /* Get the data type */
1370 flags = TypeOf (lval->e_tptr);
1372 /* Push the address if needed */
1375 /* Fetch the value and save it (since it's the result of the expression) */
1376 exprhs (CF_NONE, 1, lval);
1377 g_save (flags | CF_FORCECHAR);
1379 /* If we have a pointer expression, increment by the size of the type */
1380 if (lval->e_tptr[0] == T_PTR) {
1381 inc (flags | CF_CONST | CF_FORCECHAR, SizeOf (lval->e_tptr + 1));
1383 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1386 /* Store the result back */
1389 /* Restore the original value */
1390 g_restore (flags | CF_FORCECHAR);
1391 lval->e_flags = E_MEXPR;
1396 static void unaryop (int tok, struct expent* lval)
1397 /* Handle unary -/+ and ~ */
1404 if (k == 0 && lval->e_flags & E_MCONST) {
1405 /* Value is constant */
1407 case TOK_MINUS: lval->e_const = -lval->e_const; break;
1408 case TOK_PLUS: break;
1409 case TOK_COMP: lval->e_const = ~lval->e_const; break;
1410 default: Internal ("Unexpected token: %d", tok);
1413 /* Value is not constant */
1414 exprhs (CF_NONE, k, lval);
1416 /* Get the type of the expression */
1417 flags = TypeOf (lval->e_tptr);
1419 /* Handle the operation */
1421 case TOK_MINUS: g_neg (flags); break;
1422 case TOK_PLUS: break;
1423 case TOK_COMP: g_com (flags); break;
1424 default: Internal ("Unexpected token: %d", tok);
1426 lval->e_flags = E_MEXPR;
1432 static int typecast (struct expent* lval)
1433 /* Handle an explicit cast */
1436 type Type[MAXTYPELEN];
1438 /* Skip the left paren */
1447 /* Read the expression we have to cast */
1450 /* If the expression is a function, treat it as pointer-to-function */
1451 if (IsTypeFunc (lval->e_tptr)) {
1452 lval->e_tptr = PointerTo (lval->e_tptr);
1455 /* Check for a constant on the right side */
1456 if (k == 0 && lval->e_flags == E_MCONST) {
1458 /* A cast of a constant to something else. If the new type is an int,
1459 * be sure to handle the size extension correctly. If the new type is
1460 * not an int, the cast is implementation specific anyway, so leave
1463 if (IsClassInt (Type)) {
1465 /* Get the current and new size of the value */
1466 unsigned OldSize = SizeOf (lval->e_tptr);
1467 unsigned NewSize = SizeOf (Type);
1468 unsigned OldBits = OldSize * 8;
1469 unsigned NewBits = NewSize * 8;
1471 /* Check if the new datatype will have a smaller range */
1472 if (NewSize < OldSize) {
1474 /* Cut the value to the new size */
1475 lval->e_const &= (0xFFFFFFFFUL >> (32 - NewBits));
1477 /* If the new value is signed, sign extend the value */
1478 if (!IsSignUnsigned (Type)) {
1479 lval->e_const |= ((~0L) << NewBits);
1482 } else if (NewSize > OldSize) {
1484 /* Sign extend the value if needed */
1485 if (!IsSignUnsigned (Type) && !IsSignUnsigned (lval->e_tptr)) {
1486 if (lval->e_const & (0x01UL << (OldBits-1))) {
1487 lval->e_const |= ((~0L) << OldBits);
1495 /* Not a constant. Be sure to ignore casts to void */
1496 if (!IsTypeVoid (Type)) {
1498 /* If the size does not change, leave the value alone. Otherwise,
1499 * we have to load the value into the primary and generate code to
1500 * cast the value in the primary register.
1502 if (SizeOf (Type) != SizeOf (lval->e_tptr)) {
1504 /* Load the value into the primary */
1505 exprhs (CF_NONE, k, lval);
1507 /* Mark the lhs as const to avoid a manipulation of TOS */
1508 g_typecast (TypeOf (Type) | CF_CONST, TypeOf (lval->e_tptr));
1510 /* Value is now in primary */
1511 lval->e_flags = E_MEXPR;
1517 /* In any case, use the new type */
1518 lval->e_tptr = TypeDup (Type);
1526 static int hie10 (struct expent* lval)
1527 /* Handle ++, --, !, unary - etc. */
1535 pre_incdec (lval, g_inc);
1539 pre_incdec (lval, g_dec);
1545 unaryop (curtok, lval);
1550 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1551 /* Constant expression */
1552 lval->e_const = !lval->e_const;
1554 g_bneg (TypeOf (lval->e_tptr));
1555 lval->e_test |= E_CC; /* bneg will set cc */
1556 lval->e_flags = E_MEXPR; /* say it's an expr */
1558 return 0; /* expr not storable */
1562 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1563 /* Expression is not const, indirect value loaded into primary */
1564 lval->e_flags = E_MEXPR;
1565 lval->e_const = 0; /* Offset is zero now */
1568 if (IsClassPtr (t)) {
1569 lval->e_tptr = Indirect (t);
1571 Error ("Illegal indirection");
1578 /* The & operator may be applied to any lvalue, and it may be
1579 * applied to functions, even if they're no lvalues.
1581 if (k == 0 && !IsTypeFunc (lval->e_tptr)) {
1582 /* Allow the & operator with an array */
1583 if (!IsTypeArray (lval->e_tptr)) {
1584 Error ("Illegal address");
1587 t = TypeAlloc (TypeLen (lval->e_tptr) + 2);
1589 TypeCpy (t + 1, lval->e_tptr);
1596 if (istypeexpr ()) {
1597 type Type[MAXTYPELEN];
1599 lval->e_const = SizeOf (ParseType (Type));
1602 /* Remember the output queue pointer */
1603 CodeMark Mark = GetCodePos ();
1605 lval->e_const = SizeOf (lval->e_tptr);
1606 /* Remove any generated code */
1609 lval->e_flags = E_MCONST | E_TCONST;
1610 lval->e_tptr = type_uint;
1611 lval->e_test &= ~E_CC;
1615 if (istypeexpr ()) {
1617 return typecast (lval);
1624 post_incdec (lval, k, g_inc);
1628 post_incdec (lval, k, g_dec);
1638 static int hie_internal (GenDesc** ops, /* List of generators */
1639 struct expent* lval, /* parent expr's lval */
1640 int (*hienext) (struct expent*),
1641 int* UsedGen) /* next higher level */
1642 /* Helper function */
1645 struct expent lval2;
1649 token_t tok; /* The operator token */
1650 unsigned ltype, type;
1651 int rconst; /* Operand is a constant */
1657 while ((Gen = FindGen (curtok, ops)) != 0) {
1659 /* Tell the caller that we handled it's ops */
1662 /* All operators that call this function expect an int on the lhs */
1663 if (!IsClassInt (lval->e_tptr)) {
1664 Error ("Integer expression expected");
1667 /* Remember the operator token, then skip it */
1671 /* Get the lhs on stack */
1672 Mark1 = GetCodePos ();
1673 ltype = TypeOf (lval->e_tptr);
1674 if (k == 0 && lval->e_flags == E_MCONST) {
1675 /* Constant value */
1676 Mark2 = GetCodePos ();
1677 g_push (ltype | CF_CONST, lval->e_const);
1679 /* Value not constant */
1680 exprhs (CF_NONE, k, lval);
1681 Mark2 = GetCodePos ();
1685 /* Get the right hand side */
1686 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1688 /* Check the type of the rhs */
1689 if (!IsClassInt (lval2.e_tptr)) {
1690 Error ("Integer expression expected");
1693 /* Check for const operands */
1694 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1696 /* Both operands are constant, remove the generated code */
1700 /* Evaluate the result */
1701 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1703 /* Get the type of the result */
1704 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1708 /* If the right hand side is constant, and the generator function
1709 * expects the lhs in the primary, remove the push of the primary
1712 unsigned rtype = TypeOf (lval2.e_tptr);
1715 /* Second value is constant - check for div */
1718 if (tok == TOK_DIV && lval2.e_const == 0) {
1719 Error ("Division by zero");
1720 } else if (tok == TOK_MOD && lval2.e_const == 0) {
1721 Error ("Modulo operation with zero");
1723 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1726 ltype |= CF_REG; /* Value is in register */
1730 /* Determine the type of the operation result. */
1731 type |= g_typeadjust (ltype, rtype);
1732 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1735 Gen->Func (type, lval2.e_const);
1736 lval->e_flags = E_MEXPR;
1739 /* We have a rvalue now */
1748 static int hie_compare (GenDesc** ops, /* List of generators */
1749 struct expent* lval, /* parent expr's lval */
1750 int (*hienext) (struct expent*))
1751 /* Helper function for the compare operators */
1754 struct expent lval2;
1758 token_t tok; /* The operator token */
1760 int rconst; /* Operand is a constant */
1765 while ((Gen = FindGen (curtok, ops)) != 0) {
1767 /* Remember the operator token, then skip it */
1771 /* Get the lhs on stack */
1772 Mark1 = GetCodePos ();
1773 ltype = TypeOf (lval->e_tptr);
1774 if (k == 0 && lval->e_flags == E_MCONST) {
1775 /* Constant value */
1776 Mark2 = GetCodePos ();
1777 g_push (ltype | CF_CONST, lval->e_const);
1779 /* Value not constant */
1780 exprhs (CF_NONE, k, lval);
1781 Mark2 = GetCodePos ();
1785 /* Get the right hand side */
1786 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1788 /* Make sure, the types are compatible */
1789 if (IsClassInt (lval->e_tptr)) {
1790 if (!IsClassInt (lval2.e_tptr) && !(IsClassPtr(lval2.e_tptr) && IsNullPtr(lval))) {
1791 Error ("Incompatible types");
1793 } else if (IsClassPtr (lval->e_tptr)) {
1794 if (IsClassPtr (lval2.e_tptr)) {
1795 /* Both pointers are allowed in comparison if they point to
1796 * the same type, or if one of them is a void pointer.
1798 type* left = Indirect (lval->e_tptr);
1799 type* right = Indirect (lval2.e_tptr);
1800 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1801 /* Incomatible pointers */
1802 Error ("Incompatible types");
1804 } else if (!IsNullPtr (&lval2)) {
1805 Error ("Incompatible types");
1809 /* Check for const operands */
1810 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1812 /* Both operands are constant, remove the generated code */
1816 /* Evaluate the result */
1817 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1821 /* If the right hand side is constant, and the generator function
1822 * expects the lhs in the primary, remove the push of the primary
1828 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1831 ltype |= CF_REG; /* Value is in register */
1835 /* Determine the type of the operation result. If the left
1836 * operand is of type char and the right is a constant, or
1837 * if both operands are of type char, we will encode the
1838 * operation as char operation. Otherwise the default
1839 * promotions are used.
1841 if (IsTypeChar (lval->e_tptr) && (IsTypeChar (lval2.e_tptr) || rconst)) {
1843 if (IsSignUnsigned (lval->e_tptr) || IsSignUnsigned (lval2.e_tptr)) {
1844 flags |= CF_UNSIGNED;
1847 flags |= CF_FORCECHAR;
1850 unsigned rtype = TypeOf (lval2.e_tptr) | (flags & CF_CONST);
1851 flags |= g_typeadjust (ltype, rtype);
1855 Gen->Func (flags, lval2.e_const);
1856 lval->e_flags = E_MEXPR;
1859 /* Result type is always int */
1860 lval->e_tptr = type_int;
1862 /* We have a rvalue now, condition codes are set */
1864 lval->e_test |= E_CC;
1872 static int hie9 (struct expent *lval)
1873 /* Process * and / operators. */
1875 static GenDesc* hie9_ops [] = {
1876 &GenMUL, &GenDIV, &GenMOD, 0
1880 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1885 static void parseadd (int k, struct expent* lval)
1886 /* Parse an expression with the binary plus operator. lval contains the
1887 * unprocessed left hand side of the expression and will contain the
1888 * result of the expression on return.
1891 struct expent lval2;
1892 unsigned flags; /* Operation flags */
1893 CodeMark Mark; /* Remember code position */
1894 type* lhst; /* Type of left hand side */
1895 type* rhst; /* Type of right hand side */
1898 /* Skip the PLUS token */
1901 /* Get the left hand side type, initialize operation flags */
1902 lhst = lval->e_tptr;
1905 /* Check for constness on both sides */
1906 if (k == 0 && lval->e_flags == E_MCONST) {
1908 /* The left hand side is a constant. Good. Get rhs */
1909 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1911 /* Right hand side is also constant. Get the rhs type */
1912 rhst = lval2.e_tptr;
1914 /* Both expressions are constants. Check for pointer arithmetic */
1915 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1916 /* Left is pointer, right is int, must scale rhs */
1917 lval->e_const = lval->e_const + lval2.e_const * PSizeOf (lhst);
1918 /* Result type is a pointer */
1919 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1920 /* Left is int, right is pointer, must scale lhs */
1921 lval->e_const = lval->e_const * PSizeOf (rhst) + lval2.e_const;
1922 /* Result type is a pointer */
1923 lval->e_tptr = lval2.e_tptr;
1924 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1925 /* Integer addition */
1926 lval->e_const += lval2.e_const;
1927 typeadjust (lval, &lval2, 1);
1930 Error ("Invalid operands for binary operator `+'");
1933 /* Result is constant, condition codes not set */
1934 lval->e_test = E_MCONST;
1938 /* lhs is constant, rhs is not. Get the rhs type. */
1939 rhst = lval2.e_tptr;
1941 /* Check for pointer arithmetic */
1942 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1943 /* Left is pointer, right is int, must scale rhs */
1944 g_scale (CF_INT, PSizeOf (lhst));
1945 /* Operate on pointers, result type is a pointer */
1947 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1948 /* Left is int, right is pointer, must scale lhs */
1949 lval->e_const *= PSizeOf (rhst);
1950 /* Operate on pointers, result type is a pointer */
1952 lval->e_tptr = lval2.e_tptr;
1953 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1954 /* Integer addition */
1955 flags = typeadjust (lval, &lval2, 1);
1958 Error ("Invalid operands for binary operator `+'");
1961 /* Generate code for the add */
1962 g_inc (flags | CF_CONST, lval->e_const);
1964 /* Result is in primary register */
1965 lval->e_flags = E_MEXPR;
1966 lval->e_test &= ~E_CC;
1972 /* Left hand side is not constant. Get the value onto the stack. */
1973 exprhs (CF_NONE, k, lval); /* --> primary register */
1974 Mark = GetCodePos ();
1975 g_push (TypeOf (lval->e_tptr), 0); /* --> stack */
1977 /* Evaluate the rhs */
1978 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1980 /* Right hand side is a constant. Get the rhs type */
1981 rhst = lval2.e_tptr;
1983 /* Remove pushed value from stack */
1985 pop (TypeOf (lval->e_tptr));
1987 /* Check for pointer arithmetic */
1988 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1989 /* Left is pointer, right is int, must scale rhs */
1990 lval2.e_const *= PSizeOf (lhst);
1991 /* Operate on pointers, result type is a pointer */
1993 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1994 /* Left is int, right is pointer, must scale lhs (ptr only) */
1995 g_scale (CF_INT | CF_CONST, PSizeOf (rhst));
1996 /* Operate on pointers, result type is a pointer */
1998 lval->e_tptr = lval2.e_tptr;
1999 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2000 /* Integer addition */
2001 flags = typeadjust (lval, &lval2, 1);
2004 Error ("Invalid operands for binary operator `+'");
2007 /* Generate code for the add */
2008 g_inc (flags | CF_CONST, lval2.e_const);
2010 /* Result is in primary register */
2011 lval->e_flags = E_MEXPR;
2012 lval->e_test &= ~E_CC;
2016 /* lhs and rhs are not constant. Get the rhs type. */
2017 rhst = lval2.e_tptr;
2019 /* Check for pointer arithmetic */
2020 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2021 /* Left is pointer, right is int, must scale rhs */
2022 g_scale (CF_INT, PSizeOf (lhst));
2023 /* Operate on pointers, result type is a pointer */
2025 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2026 /* Left is int, right is pointer, must scale lhs */
2027 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2028 g_swap (CF_INT); /* Swap TOS and primary */
2029 g_scale (CF_INT, PSizeOf (rhst));
2030 /* Operate on pointers, result type is a pointer */
2032 lval->e_tptr = lval2.e_tptr;
2033 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2034 /* Integer addition */
2035 flags = typeadjust (lval, &lval2, 0);
2038 Error ("Invalid operands for binary operator `+'");
2041 /* Generate code for the add */
2044 /* Result is in primary register */
2045 lval->e_flags = E_MEXPR;
2046 lval->e_test &= ~E_CC;
2055 static void parsesub (int k, struct expent* lval)
2056 /* Parse an expression with the binary minus operator. lval contains the
2057 * unprocessed left hand side of the expression and will contain the
2058 * result of the expression on return.
2061 struct expent lval2;
2062 unsigned flags; /* Operation flags */
2063 type* lhst; /* Type of left hand side */
2064 type* rhst; /* Type of right hand side */
2065 CodeMark Mark1; /* Save position of output queue */
2066 CodeMark Mark2; /* Another position in the queue */
2067 int rscale; /* Scale factor for the result */
2070 /* Skip the MINUS token */
2073 /* Get the left hand side type, initialize operation flags */
2074 lhst = lval->e_tptr;
2076 rscale = 1; /* Scale by 1, that is, don't scale */
2078 /* Remember the output queue position, then bring the value onto the stack */
2079 Mark1 = GetCodePos ();
2080 exprhs (CF_NONE, k, lval); /* --> primary register */
2081 Mark2 = GetCodePos ();
2082 g_push (TypeOf (lhst), 0); /* --> stack */
2084 /* Parse the right hand side */
2085 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2087 /* The right hand side is constant. Get the rhs type. */
2088 rhst = lval2.e_tptr;
2090 /* Check left hand side */
2091 if (k == 0 && lval->e_flags & E_MCONST) {
2093 /* Both sides are constant, remove generated code */
2095 pop (TypeOf (lhst)); /* Clean up the stack */
2097 /* Check for pointer arithmetic */
2098 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2099 /* Left is pointer, right is int, must scale rhs */
2100 lval->e_const -= lval2.e_const * PSizeOf (lhst);
2101 /* Operate on pointers, result type is a pointer */
2102 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2103 /* Left is pointer, right is pointer, must scale result */
2104 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2105 Error ("Incompatible pointer types");
2107 lval->e_const = (lval->e_const - lval2.e_const) / PSizeOf (lhst);
2109 /* Operate on pointers, result type is an integer */
2110 lval->e_tptr = type_int;
2111 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2112 /* Integer subtraction */
2113 typeadjust (lval, &lval2, 1);
2114 lval->e_const -= lval2.e_const;
2117 Error ("Invalid operands for binary operator `-'");
2120 /* Result is constant, condition codes not set */
2121 lval->e_flags = E_MCONST;
2122 lval->e_test &= ~E_CC;
2126 /* Left hand side is not constant, right hand side is.
2127 * Remove pushed value from stack.
2130 pop (TypeOf (lhst));
2132 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2133 /* Left is pointer, right is int, must scale rhs */
2134 lval2.e_const *= PSizeOf (lhst);
2135 /* Operate on pointers, result type is a pointer */
2137 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2138 /* Left is pointer, right is pointer, must scale result */
2139 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2140 Error ("Incompatible pointer types");
2142 rscale = PSizeOf (lhst);
2144 /* Operate on pointers, result type is an integer */
2146 lval->e_tptr = type_int;
2147 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2148 /* Integer subtraction */
2149 flags = typeadjust (lval, &lval2, 1);
2152 Error ("Invalid operands for binary operator `-'");
2155 /* Do the subtraction */
2156 g_dec (flags | CF_CONST, lval2.e_const);
2158 /* If this was a pointer subtraction, we must scale the result */
2160 g_scale (flags, -rscale);
2163 /* Result is in primary register */
2164 lval->e_flags = E_MEXPR;
2165 lval->e_test &= ~E_CC;
2171 /* Right hand side is not constant. Get the rhs type. */
2172 rhst = lval2.e_tptr;
2174 /* Check for pointer arithmetic */
2175 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2176 /* Left is pointer, right is int, must scale rhs */
2177 g_scale (CF_INT, PSizeOf (lhst));
2178 /* Operate on pointers, result type is a pointer */
2180 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2181 /* Left is pointer, right is pointer, must scale result */
2182 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2183 Error ("Incompatible pointer types");
2185 rscale = PSizeOf (lhst);
2187 /* Operate on pointers, result type is an integer */
2189 lval->e_tptr = type_int;
2190 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2191 /* Integer subtraction. If the left hand side descriptor says that
2192 * the lhs is const, we have to remove this mark, since this is no
2193 * longer true, lhs is on stack instead.
2195 if (lval->e_flags == E_MCONST) {
2196 lval->e_flags = E_MEXPR;
2198 /* Adjust operand types */
2199 flags = typeadjust (lval, &lval2, 0);
2202 Error ("Invalid operands for binary operator `-'");
2205 /* Generate code for the sub (the & is a hack here) */
2206 g_sub (flags & ~CF_CONST, 0);
2208 /* If this was a pointer subtraction, we must scale the result */
2210 g_scale (flags, -rscale);
2213 /* Result is in primary register */
2214 lval->e_flags = E_MEXPR;
2215 lval->e_test &= ~E_CC;
2221 static int hie8 (struct expent* lval)
2222 /* Process + and - binary operators. */
2224 int k = hie9 (lval);
2225 while (curtok == TOK_PLUS || curtok == TOK_MINUS) {
2227 if (curtok == TOK_PLUS) {
2240 static int hie7 (struct expent *lval)
2241 /* Parse << and >>. */
2243 static GenDesc* hie7_ops [] = {
2248 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2253 static int hie6 (struct expent *lval)
2254 /* process greater-than type comparators */
2256 static GenDesc* hie6_ops [] = {
2257 &GenLT, &GenLE, &GenGE, &GenGT, 0
2259 return hie_compare (hie6_ops, lval, hie7);
2264 static int hie5 (struct expent *lval)
2266 static GenDesc* hie5_ops[] = {
2269 return hie_compare (hie5_ops, lval, hie6);
2274 static int hie4 (struct expent* lval)
2275 /* Handle & (bitwise and) */
2277 static GenDesc* hie4_ops [] = {
2282 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2287 static int hie3 (struct expent *lval)
2288 /* Handle ^ (bitwise exclusive or) */
2290 static GenDesc* hie3_ops [] = {
2295 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2300 static int hie2 (struct expent *lval)
2301 /* Handle | (bitwise or) */
2303 static GenDesc* hie2_ops [] = {
2308 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2313 static int hieAnd (struct expent* lval, unsigned TrueLab, int* BoolOp)
2314 /* Process "exp && exp" */
2318 struct expent lval2;
2321 if (curtok == TOK_BOOL_AND) {
2323 /* Tell our caller that we're evaluating a boolean */
2326 /* Get a label that we will use for false expressions */
2329 /* If the expr hasn't set condition codes, set the force-test flag */
2330 if ((lval->e_test & E_CC) == 0) {
2331 lval->e_test |= E_FORCETEST;
2334 /* Load the value */
2335 exprhs (CF_FORCECHAR, k, lval);
2337 /* Generate the jump */
2338 g_falsejump (CF_NONE, lab);
2340 /* Parse more boolean and's */
2341 while (curtok == TOK_BOOL_AND) {
2348 if ((lval2.e_test & E_CC) == 0) {
2349 lval2.e_test |= E_FORCETEST;
2351 exprhs (CF_FORCECHAR, k, &lval2);
2353 /* Do short circuit evaluation */
2354 if (curtok == TOK_BOOL_AND) {
2355 g_falsejump (CF_NONE, lab);
2357 /* Last expression - will evaluate to true */
2358 g_truejump (CF_NONE, TrueLab);
2362 /* Define the false jump label here */
2363 g_defloclabel (lab);
2365 /* Define the label */
2366 lval->e_flags = E_MEXPR;
2367 lval->e_test |= E_CC; /* Condition codes are set */
2375 static int hieOr (struct expent *lval)
2376 /* Process "exp || exp". */
2379 struct expent lval2;
2380 int BoolOp = 0; /* Did we have a boolean op? */
2381 int AndOp; /* Did we have a && operation? */
2382 unsigned TrueLab; /* Jump to this label if true */
2386 TrueLab = GetLabel ();
2388 /* Call the next level parser */
2389 k = hieAnd (lval, TrueLab, &BoolOp);
2391 /* Any boolean or's? */
2392 if (curtok == TOK_BOOL_OR) {
2394 /* If the expr hasn't set condition codes, set the force-test flag */
2395 if ((lval->e_test & E_CC) == 0) {
2396 lval->e_test |= E_FORCETEST;
2399 /* Get first expr */
2400 exprhs (CF_FORCECHAR, k, lval);
2402 /* For each expression jump to TrueLab if true. Beware: If we
2403 * had && operators, the jump is already in place!
2406 g_truejump (CF_NONE, TrueLab);
2409 /* Remember that we had a boolean op */
2412 /* while there's more expr */
2413 while (curtok == TOK_BOOL_OR) {
2420 k = hieAnd (&lval2, TrueLab, &AndOp);
2421 if ((lval2.e_test & E_CC) == 0) {
2422 lval2.e_test |= E_FORCETEST;
2424 exprhs (CF_FORCECHAR, k, &lval2);
2426 /* If there is more to come, add shortcut boolean eval.
2427 * Beware: If we had && operators, the jump is already
2431 /* Seems this sometimes generates wrong code */
2432 if (curtok == TOK_BOOL_OR && !AndOp) {
2433 g_truejump (CF_NONE, TrueLab);
2436 g_truejump (CF_NONE, TrueLab);
2439 lval->e_flags = E_MEXPR;
2440 lval->e_test |= E_CC; /* Condition codes are set */
2444 /* If we really had boolean ops, generate the end sequence */
2446 DoneLab = GetLabel ();
2447 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2448 g_falsejump (CF_NONE, DoneLab);
2449 g_defloclabel (TrueLab);
2450 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2451 g_defloclabel (DoneLab);
2458 static int hieQuest (struct expent *lval)
2459 /* Parse "lvalue ? exp : exp" */
2464 struct expent lval2; /* Expression 2 */
2465 struct expent lval3; /* Expression 3 */
2466 type* type2; /* Type of expression 2 */
2467 type* type3; /* Type of expression 3 */
2468 type* rtype; /* Type of result */
2469 CodeMark Mark1; /* Save position in output code */
2470 CodeMark Mark2; /* Save position in output code */
2475 if (curtok == TOK_QUEST) {
2477 if ((lval->e_test & E_CC) == 0) {
2478 /* Condition codes not set, force a test */
2479 lval->e_test |= E_FORCETEST;
2481 exprhs (CF_NONE, k, lval);
2483 g_falsejump (CF_NONE, labf);
2485 /* Parse second and third expression */
2486 expression1 (&lval2);
2490 g_defloclabel (labf);
2491 expression1 (&lval3);
2493 /* Check if any conversions are needed, if so, do them.
2494 * Conversion rules for ?: expression are:
2495 * - if both expressions are int expressions, default promotion
2496 * rules for ints apply.
2497 * - if both expressions are pointers of the same type, the
2498 * result of the expression is of this type.
2499 * - if one of the expressions is a pointer and the other is
2500 * a zero constant, the resulting type is that of the pointer
2502 * - all other cases are flagged by an error.
2504 type2 = lval2.e_tptr;
2505 type3 = lval3.e_tptr;
2506 if (IsClassInt (type2) && IsClassInt (type3)) {
2508 /* Get common type */
2509 rtype = promoteint (type2, type3);
2511 /* Convert the third expression to this type if needed */
2512 g_typecast (TypeOf (rtype), TypeOf (type3));
2514 /* Setup a new label so that the expr3 code will jump around
2515 * the type cast code for expr2.
2517 labf = GetLabel (); /* Get new label */
2518 Mark1 = GetCodePos (); /* Remember current position */
2519 g_jump (labf); /* Jump around code */
2521 /* The jump for expr2 goes here */
2522 g_defloclabel (labt);
2524 /* Create the typecast code for expr2 */
2525 Mark2 = GetCodePos (); /* Remember position */
2526 g_typecast (TypeOf (rtype), TypeOf (type2));
2528 /* If the typecast did not produce code, remove the jump,
2529 * otherwise output the label.
2531 if (GetCodePos() == Mark2) {
2532 RemoveCode (Mark1); /* Remove code */
2534 /* We have typecast code, output label */
2535 g_defloclabel (labf);
2536 labt = 0; /* Mark other label as invalid */
2539 } else if (IsClassPtr (type2) && IsClassPtr (type3)) {
2540 /* Must point to same type */
2541 if (TypeCmp (Indirect (type2), Indirect (type3)) < TC_EQUAL) {
2542 Error ("Incompatible pointer types");
2544 /* Result has the common type */
2545 rtype = lval2.e_tptr;
2546 } else if (IsClassPtr (type2) && IsNullPtr (&lval3)) {
2547 /* Result type is pointer, no cast needed */
2548 rtype = lval2.e_tptr;
2549 } else if (IsNullPtr (&lval2) && IsClassPtr (type3)) {
2550 /* Result type is pointer, no cast needed */
2551 rtype = lval3.e_tptr;
2553 Error ("Incompatible types");
2554 rtype = lval2.e_tptr; /* Doesn't matter here */
2557 /* If we don't have the label defined until now, do it */
2559 g_defloclabel (labt);
2562 /* Setup the target expression */
2563 lval->e_flags = E_MEXPR;
2564 lval->e_tptr = rtype;
2572 static void opeq (GenDesc* Gen, struct expent *lval, int k)
2573 /* Process "op=" operators. */
2575 struct expent lval2;
2582 Error ("Invalid lvalue in assignment");
2586 /* Determine the type of the lhs */
2587 flags = TypeOf (lval->e_tptr);
2588 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2589 lval->e_tptr [0] == T_PTR;
2591 /* Get the lhs address on stack (if needed) */
2594 /* Fetch the lhs into the primary register if needed */
2595 exprhs (CF_NONE, k, lval);
2597 /* Bring the lhs on stack */
2598 Mark = GetCodePos ();
2601 /* Evaluate the rhs */
2602 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2603 /* The resulting value is a constant. If the generator has the NOPUSH
2604 * flag set, don't push the lhs.
2606 if (Gen->Flags & GEN_NOPUSH) {
2611 /* lhs is a pointer, scale rhs */
2612 lval2.e_const *= SizeOf (lval->e_tptr+1);
2615 /* If the lhs is character sized, the operation may be later done
2618 if (SizeOf (lval->e_tptr) == 1) {
2619 flags |= CF_FORCECHAR;
2622 /* Special handling for add and sub - some sort of a hack, but short code */
2623 if (Gen->Func == g_add) {
2624 g_inc (flags | CF_CONST, lval2.e_const);
2625 } else if (Gen->Func == g_sub) {
2626 g_dec (flags | CF_CONST, lval2.e_const);
2628 Gen->Func (flags | CF_CONST, lval2.e_const);
2631 /* rhs is not constant and already in the primary register */
2633 /* lhs is a pointer, scale rhs */
2634 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2637 /* If the lhs is character sized, the operation may be later done
2640 if (SizeOf (lval->e_tptr) == 1) {
2641 flags |= CF_FORCECHAR;
2644 /* Adjust the types of the operands if needed */
2645 Gen->Func (g_typeadjust (flags, TypeOf (lval2.e_tptr)), 0);
2648 lval->e_flags = E_MEXPR;
2653 static void addsubeq (GenDesc* Gen, struct expent *lval, int k)
2654 /* Process the += and -= operators */
2656 struct expent lval2;
2662 Error ("Invalid lvalue in assignment");
2667 /* We're currently only able to handle some adressing modes */
2668 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
2669 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
2670 (lval->e_flags & E_MCONST) == 0) { /* Constant address? */
2671 /* Use generic routine */
2672 opeq (Gen, lval, k);
2676 /* Skip the operator */
2679 /* Check if we have a pointer expression and must scale rhs */
2680 MustScale = (lval->e_tptr [0] == T_PTR);
2682 /* Determine the code generator flags */
2683 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR;
2685 /* Evaluate the rhs */
2686 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2687 /* The resulting value is a constant. */
2689 /* lhs is a pointer, scale rhs */
2690 lval2.e_const *= SizeOf (lval->e_tptr+1);
2694 /* rhs is not constant and already in the primary register */
2696 /* lhs is a pointer, scale rhs */
2697 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2701 /* Adjust the rhs to the lhs */
2702 g_typeadjust (flags, TypeOf (lval2.e_tptr));
2704 /* Output apropriate code */
2705 if (lval->e_flags & E_MGLOBAL) {
2706 /* Static variable */
2707 flags |= GlobalModeFlags (lval->e_flags);
2708 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2709 g_addeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2711 g_subeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2713 } else if (lval->e_flags & E_MLOCAL) {
2714 /* ref to localvar */
2715 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2716 g_addeqlocal (flags, lval->e_const, lval2.e_const);
2718 g_subeqlocal (flags, lval->e_const, lval2.e_const);
2720 } else if (lval->e_flags & E_MCONST) {
2721 /* ref to absolute address */
2722 flags |= CF_ABSOLUTE;
2723 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2724 g_addeqstatic (flags, lval->e_const, 0, lval2.e_const);
2726 g_subeqstatic (flags, lval->e_const, 0, lval2.e_const);
2728 } else if (lval->e_flags & E_MEXPR) {
2729 /* Address in a/x. */
2730 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2731 g_addeqind (flags, lval->e_const, lval2.e_const);
2733 g_subeqind (flags, lval->e_const, lval2.e_const);
2736 Internal ("Invalid addressing mode");
2739 /* Expression is in the primary now */
2740 lval->e_flags = E_MEXPR;
2745 static void Assignment (struct expent* lval)
2746 /* Parse an assignment */
2749 struct expent lval2;
2751 type* ltype = lval->e_tptr;
2753 /* Check for assignment to const */
2754 if (IsQualConst (ltype)) {
2755 Error ("Assignment to const");
2758 /* cc65 does not have full support for handling structs by value. Since
2759 * assigning structs is one of the more useful operations from this
2760 * familiy, allow it here.
2762 if (IsClassStruct (ltype)) {
2764 /* Bring the address of the lhs into the primary and push it */
2765 exprhs (0, 0, lval);
2766 g_push (CF_PTR | CF_UNSIGNED, 0);
2768 /* Get the expression on the right of the '=' into the primary */
2771 /* Get the address */
2772 exprhs (0, 0, &lval2);
2774 /* We need an lvalue */
2775 Error ("Invalid lvalue in assignment");
2778 /* Push the address (or whatever is in ax in case of errors) */
2779 g_push (CF_PTR | CF_UNSIGNED, 0);
2781 /* Check for equality of the structs */
2782 if (TypeCmp (ltype, lval2.e_tptr) < TC_EQUAL) {
2783 Error ("Incompatible types");
2786 /* Load the size of the struct into the primary */
2787 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, SizeOf (ltype), 0);
2789 /* Call the memcpy function */
2790 g_call (CF_FIXARGC, "memcpy", 4);
2794 /* Get the address on stack if needed */
2797 /* No struct, setup flags for the load */
2798 flags = SizeOf (ltype) == 1? CF_FORCECHAR : CF_NONE;
2800 /* Get the expression on the right of the '=' into the primary */
2801 if (evalexpr (flags, hie1, &lval2) == 0) {
2802 /* Constant expression. Adjust the types */
2803 assignadjust (ltype, &lval2);
2804 /* Put the value into the primary register */
2805 lconst (flags, &lval2);
2807 /* Expression is not constant and already in the primary */
2808 assignadjust (ltype, &lval2);
2811 /* Generate a store instruction */
2816 /* Value is still in primary */
2817 lval->e_flags = E_MEXPR;
2822 int hie1 (struct expent* lval)
2823 /* Parse first level of expression hierarchy. */
2827 k = hieQuest (lval);
2837 Error ("Invalid lvalue in assignment");
2843 case TOK_PLUS_ASSIGN:
2844 addsubeq (&GenPASGN, lval, k);
2847 case TOK_MINUS_ASSIGN:
2848 addsubeq (&GenSASGN, lval, k);
2851 case TOK_MUL_ASSIGN:
2852 opeq (&GenMASGN, lval, k);
2855 case TOK_DIV_ASSIGN:
2856 opeq (&GenDASGN, lval, k);
2859 case TOK_MOD_ASSIGN:
2860 opeq (&GenMOASGN, lval, k);
2863 case TOK_SHL_ASSIGN:
2864 opeq (&GenSLASGN, lval, k);
2867 case TOK_SHR_ASSIGN:
2868 opeq (&GenSRASGN, lval, k);
2871 case TOK_AND_ASSIGN:
2872 opeq (&GenAASGN, lval, k);
2875 case TOK_XOR_ASSIGN:
2876 opeq (&GenXOASGN, lval, k);
2880 opeq (&GenOASGN, lval, k);
2891 int hie0 (struct expent *lval)
2892 /* Parse comma operator. */
2897 while (curtok == TOK_COMMA) {
2906 int evalexpr (unsigned flags, int (*f) (struct expent*), struct expent* lval)
2907 /* Will evaluate an expression via the given function. If the result is a
2908 * constant, 0 is returned and the value is put in the lval struct. If the
2909 * result is not constant, exprhs is called to bring the value into the
2910 * primary register and 1 is returned.
2917 if (k == 0 && lval->e_flags == E_MCONST) {
2918 /* Constant expression */
2921 /* Not constant, load into the primary */
2922 exprhs (flags, k, lval);
2929 int expr (int (*func) (struct expent*), struct expent *lval)
2930 /* Expression parser; func is either hie0 or hie1. */
2939 /* Do some checks if code generation is still constistent */
2940 if (savsp != oursp) {
2942 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
2944 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
2952 void expression1 (struct expent* lval)
2953 /* Evaluate an expression on level 1 (no comma operator) and put it into
2954 * the primary register
2957 memset (lval, 0, sizeof (*lval));
2958 exprhs (CF_NONE, expr (hie1, lval), lval);
2963 void expression (struct expent* lval)
2964 /* Evaluate an expression and put it into the primary register */
2966 memset (lval, 0, sizeof (*lval));
2967 exprhs (CF_NONE, expr (hie0, lval), lval);
2972 void constexpr (struct expent* lval)
2973 /* Get a constant value */
2975 memset (lval, 0, sizeof (*lval));
2976 if (expr (hie1, lval) != 0 || (lval->e_flags & E_MCONST) == 0) {
2977 Error ("Constant expression expected");
2978 /* To avoid any compiler errors, make the expression a valid const */
2979 lval->e_flags = E_MCONST;
2980 lval->e_tptr = type_int;
2987 void intexpr (struct expent* lval)
2988 /* Get an integer expression */
2991 if (!IsClassInt (lval->e_tptr)) {
2992 Error ("Integer expression expected");
2993 /* To avoid any compiler errors, make the expression a valid int */
2994 lval->e_flags = E_MCONST;
2995 lval->e_tptr = type_int;
3002 void boolexpr (struct expent* lval)
3003 /* Get a boolean expression */
3005 /* Read an expression */
3008 /* If it's an integer, it's ok. If it's not an integer, but a pointer,
3009 * the pointer used in a boolean context is also ok
3011 if (!IsClassInt (lval->e_tptr) && !IsClassPtr (lval->e_tptr)) {
3012 Error ("Boolean expression expected");
3013 /* To avoid any compiler errors, make the expression a valid int */
3014 lval->e_flags = E_MCONST;
3015 lval->e_tptr = type_int;
3022 void test (unsigned label, int cond)
3023 /* Generate code to perform test and jump if false. */
3028 /* Eat the parenthesis */
3031 /* Prepare the expression, setup labels */
3032 memset (&lval, 0, sizeof (lval));
3034 /* Generate code to eval the expr */
3035 k = expr (hie0, &lval);
3036 if (k == 0 && lval.e_flags == E_MCONST) {
3037 /* Constant rvalue */
3038 if (cond == 0 && lval.e_const == 0) {
3040 Warning ("Unreachable code");
3041 } else if (cond && lval.e_const) {
3048 /* If the expr hasn't set condition codes, set the force-test flag */
3049 if ((lval.e_test & E_CC) == 0) {
3050 lval.e_test |= E_FORCETEST;
3053 /* Load the value into the primary register */
3054 exprhs (CF_FORCECHAR, k, &lval);
3056 /* Check for the closing brace */
3059 /* Generate the jump */
3061 g_truejump (CF_NONE, label);
3063 /* Special case (putting this here is a small hack - but hey, the
3064 * compiler itself is one big hack...): If a semicolon follows, we
3065 * don't have a statement and may omit the jump.
3067 if (curtok != TOK_SEMI) {
3068 g_falsejump (CF_NONE, label);