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.EnumVal;
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) {
787 lval->e_flags = E_MLOCAL | E_TLOFFS;
788 lval->e_const = Sym->V.Offs;
789 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
790 /* Static variable */
791 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
792 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
793 lval->e_name = (unsigned long) Sym->Name;
795 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
796 lval->e_name = Sym->V.Label;
799 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
800 /* Register variable, zero page based */
801 lval->e_flags = E_MGLOBAL | E_MCONST | E_TREGISTER;
802 lval->e_name = Sym->V.Offs;
805 /* Local static variable */
806 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
807 lval->e_name = Sym->V.Offs;
811 /* The symbol is referenced now */
812 Sym->Flags |= SC_REF;
813 if (IsTypeFunc (lval->e_tptr) || IsTypeArray (lval->e_tptr)) {
819 /* We did not find the symbol. Remember the name, then skip it */
820 strcpy (Ident, CurTok.Ident);
823 /* IDENT is either an auto-declared function or an undefined variable. */
824 if (curtok == TOK_LPAREN) {
825 /* Declare a function returning int. For that purpose, prepare a
826 * function signature for a function having an empty param list
829 Warning ("Function call without a prototype");
830 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
831 lval->e_tptr = Sym->Type;
832 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
833 lval->e_name = (unsigned long) Sym->Name;
839 /* Undeclared Variable */
840 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
841 lval->e_flags = E_MLOCAL | E_TLOFFS;
842 lval->e_tptr = type_int;
844 Error ("Undefined symbol: `%s'", Ident);
850 /* String literal? */
851 if (curtok == TOK_SCONST) {
852 lval->e_flags = E_MCONST | E_TLIT;
853 lval->e_const = curval;
854 lval->e_tptr = GetCharArrayType (strlen (GetLiteral (curval)));
860 if (curtok == TOK_ASM) {
862 lval->e_tptr = type_void;
863 lval->e_flags = E_MEXPR;
868 /* __AX__ and __EAX__ pseudo values? */
869 if (curtok == TOK_AX || curtok == TOK_EAX) {
870 lval->e_tptr = (curtok == TOK_AX)? type_uint : type_ulong;
871 lval->e_flags = E_MREG;
872 lval->e_test &= ~E_CC;
875 return 1; /* May be used as lvalue */
878 /* Illegal primary. */
879 Error ("Expression expected");
880 lval->e_flags = E_MCONST;
881 lval->e_tptr = type_int;
887 static int arrayref (int k, struct expent* lval)
888 /* Handle an array reference */
902 /* Skip the bracket */
905 /* Get the type of left side */
906 tptr1 = lval->e_tptr;
908 /* We can apply a special treatment for arrays that have a const base
909 * address. This is true for most arrays and will produce a lot better
910 * code. Check if this is a const base address.
912 lflags = lval->e_flags & ~E_MCTYPE;
913 ConstBaseAddr = (lflags == E_MCONST) || /* Constant numeric address */
914 (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
915 lflags == E_MLOCAL; /* Local array */
917 /* If we have a constant base, we delay the address fetch */
918 Mark1 = GetCodePos ();
919 Mark2 = 0; /* Silence gcc */
920 if (!ConstBaseAddr) {
921 /* Get a pointer to the array into the primary */
922 exprhs (CF_NONE, k, lval);
924 /* Get the array pointer on stack. Do not push more than 16
925 * bit, even if this value is greater, since we cannot handle
926 * other than 16bit stuff when doing indexing.
928 Mark2 = GetCodePos ();
932 /* TOS now contains ptr to array elements. Get the subscript. */
934 if (l == 0 && lval2.e_flags == E_MCONST) {
936 /* The array subscript is a constant - remove value from stack */
937 if (!ConstBaseAddr) {
941 /* Get an array pointer into the primary */
942 exprhs (CF_NONE, k, lval);
945 if (IsClassPtr (tptr1)) {
947 /* Scale the subscript value according to element size */
948 lval2.e_const *= PSizeOf (tptr1);
950 /* Remove code for lhs load */
953 /* Handle constant base array on stack. Be sure NOT to
954 * handle pointers the same way, this won't work.
956 if (IsTypeArray (tptr1) &&
957 ((lval->e_flags & ~E_MCTYPE) == E_MCONST ||
958 (lval->e_flags & ~E_MCTYPE) == E_MLOCAL ||
959 (lval->e_flags & E_MGLOBAL) != 0 ||
960 (lval->e_flags == E_MEOFFS))) {
961 lval->e_const += lval2.e_const;
964 /* Pointer - load into primary and remember offset */
965 if ((lval->e_flags & E_MEXPR) == 0 || k != 0) {
966 exprhs (CF_NONE, k, lval);
968 lval->e_const = lval2.e_const;
969 lval->e_flags = E_MEOFFS;
972 /* Result is of element type */
973 lval->e_tptr = Indirect (tptr1);
978 } else if (IsClassPtr (tptr2 = lval2.e_tptr)) {
979 /* Subscript is pointer, get element type */
980 lval2.e_tptr = Indirect (tptr2);
982 /* Scale the rhs value in the primary register */
983 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
985 lval->e_tptr = lval2.e_tptr;
987 Error ("Cannot subscript");
990 /* Add the subscript. Since arrays are indexed by integers,
991 * we will ignore the true type of the subscript here and
994 g_inc (CF_INT | CF_CONST, lval2.e_const);
998 /* Array subscript is not constant. Load it into the primary */
999 Mark2 = GetCodePos ();
1000 exprhs (CF_NONE, l, &lval2);
1002 tptr2 = lval2.e_tptr;
1003 if (IsClassPtr (tptr1)) {
1005 /* Get the element type */
1006 lval->e_tptr = Indirect (tptr1);
1008 /* Indexing is based on int's, so we will just use the integer
1009 * portion of the index (which is in (e)ax, so there's no further
1012 g_scale (CF_INT, SizeOf (lval->e_tptr));
1014 } else if (IsClassPtr (tptr2)) {
1016 /* Get the element type */
1017 lval2.e_tptr = Indirect (tptr2);
1019 /* Get the int value on top. If we go here, we're sure,
1020 * both values are 16 bit (the first one was truncated
1021 * if necessary and the second one is a pointer).
1022 * Note: If ConstBaseAddr is true, we don't have a value on
1023 * stack, so to "swap" both, just push the subscript.
1025 if (ConstBaseAddr) {
1027 exprhs (CF_NONE, k, lval);
1034 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
1035 lval->e_tptr = lval2.e_tptr;
1037 Error ("Cannot subscript");
1040 /* The offset is now in the primary register. It didn't have a
1041 * constant base address for the lhs, the lhs address is already
1042 * on stack, and we must add the offset. If the base address was
1043 * constant, we call special functions to add the address to the
1046 if (!ConstBaseAddr) {
1047 /* Add the subscript. Both values are int sized. */
1051 /* If the subscript has itself a constant address, it is often
1052 * a better idea to reverse again the order of the evaluation.
1053 * This will generate better code if the subscript is a byte
1054 * sized variable. But beware: This is only possible if the
1055 * subscript was not scaled, that is, if this was a byte array
1058 rflags = lval2.e_flags & ~E_MCTYPE;
1059 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1060 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1061 rflags == E_MLOCAL; /* Local array */
1063 if (ConstSubAddr && SizeOf (lval->e_tptr) == 1) {
1067 /* Reverse the order of evaluation */
1068 unsigned flags = (SizeOf (lval2.e_tptr) == 1)? CF_CHAR : CF_INT;
1071 /* Get a pointer to the array into the primary. We have changed
1072 * e_tptr above but we need the original type to load the
1073 * address, so restore it temporarily.
1075 SavedType = lval->e_tptr;
1076 lval->e_tptr = tptr1;
1077 exprhs (CF_NONE, k, lval);
1078 lval->e_tptr = SavedType;
1080 /* Add the variable */
1081 if (rflags == E_MLOCAL) {
1082 g_addlocal (flags, lval2.e_const);
1084 flags |= GlobalModeFlags (lval2.e_flags);
1085 g_addstatic (flags, lval2.e_name, lval2.e_const);
1088 if (lflags == E_MCONST) {
1089 /* Constant numeric address. Just add it */
1090 g_inc (CF_INT | CF_UNSIGNED, lval->e_const);
1091 } else if (lflags == E_MLOCAL) {
1092 /* Base address is a local variable address */
1093 if (IsTypeArray (tptr1)) {
1094 g_addaddr_local (CF_INT, lval->e_const);
1096 g_addlocal (CF_PTR, lval->e_const);
1099 /* Base address is a static variable address */
1100 unsigned flags = CF_INT;
1101 flags |= GlobalModeFlags (lval->e_flags);
1102 if (IsTypeArray (tptr1)) {
1103 g_addaddr_static (flags, lval->e_name, lval->e_const);
1105 g_addstatic (flags, lval->e_name, lval->e_const);
1111 lval->e_flags = E_MEXPR;
1114 return !IsTypeArray (lval->e_tptr);
1120 static int structref (int k, struct expent* lval)
1121 /* Process struct field after . or ->. */
1127 /* Skip the token and check for an identifier */
1129 if (curtok != TOK_IDENT) {
1130 Error ("Identifier expected");
1131 lval->e_tptr = type_int;
1135 /* Get the symbol table entry and check for a struct field */
1136 strcpy (Ident, CurTok.Ident);
1138 Field = FindStructField (lval->e_tptr, Ident);
1140 Error ("Struct/union has no field named `%s'", Ident);
1141 lval->e_tptr = type_int;
1145 /* If we have constant input data, the result is also constant */
1146 flags = lval->e_flags & ~E_MCTYPE;
1147 if (flags == E_MCONST ||
1148 (k == 0 && (flags == E_MLOCAL ||
1149 (flags & E_MGLOBAL) != 0 ||
1150 lval->e_flags == E_MEOFFS))) {
1151 lval->e_const += Field->V.Offs;
1153 if ((flags & E_MEXPR) == 0 || k != 0) {
1154 exprhs (CF_NONE, k, lval);
1156 lval->e_const = Field->V.Offs;
1157 lval->e_flags = E_MEOFFS;
1159 lval->e_tptr = Field->Type;
1160 return !IsTypeArray (Field->Type);
1165 static int hie11 (struct expent *lval)
1166 /* Handle compound types (structs and arrays) */
1173 if (curtok < TOK_LBRACK || curtok > TOK_PTR_REF) {
1180 if (curtok == TOK_LBRACK) {
1182 /* Array reference */
1183 k = arrayref (k, lval);
1185 } else if (curtok == TOK_LPAREN) {
1187 /* Function call. Skip the opening parenthesis */
1189 tptr = lval->e_tptr;
1190 if (IsTypeFunc (tptr) || IsTypeFuncPtr (tptr)) {
1191 if (IsTypeFuncPtr (tptr)) {
1192 /* Pointer to function. Handle transparently */
1193 exprhs (CF_NONE, k, lval); /* Function pointer to A/X */
1194 ++lval->e_tptr; /* Skip T_PTR */
1195 lval->e_flags |= E_MEXPR;
1197 callfunction (lval);
1198 lval->e_flags = E_MEXPR;
1199 lval->e_tptr += DECODE_SIZE + 1; /* Set to result */
1201 Error ("Illegal function call");
1205 } else if (curtok == TOK_DOT) {
1207 if (!IsClassStruct (lval->e_tptr)) {
1208 Error ("Struct expected");
1210 k = structref (0, lval);
1212 } else if (curtok == TOK_PTR_REF) {
1214 tptr = lval->e_tptr;
1215 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1216 Error ("Struct pointer expected");
1218 k = structref (k, lval);
1228 static void store (struct expent* lval)
1229 /* Store primary reg into this reference */
1235 flags = TypeOf (lval->e_tptr);
1236 if (f & E_MGLOBAL) {
1237 flags |= GlobalModeFlags (f);
1244 g_putstatic (flags, lval->e_name, lval->e_const);
1246 } else if (f & E_MLOCAL) {
1247 g_putlocal (flags, lval->e_const);
1248 } else if (f == E_MEOFFS) {
1249 g_putind (flags, lval->e_const);
1250 } else if (f != E_MREG) {
1252 g_putind (flags, 0);
1254 /* Store into absolute address */
1255 g_putstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
1259 /* Assume that each one of the stores will invalidate CC */
1260 lval->e_test &= ~E_CC;
1265 static void pre_incdec (struct expent* lval, void (*inc) (unsigned, unsigned long))
1266 /* Handle --i and ++i */
1273 if ((k = hie10 (lval)) == 0) {
1274 Error ("Invalid lvalue");
1278 /* Get the data type */
1279 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR | CF_CONST;
1281 /* Get the increment value in bytes */
1282 val = (lval->e_tptr [0] == T_PTR)? PSizeOf (lval->e_tptr) : 1;
1284 /* We're currently only able to handle some adressing modes */
1285 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
1286 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
1287 (lval->e_flags & E_MCONST) == 0 && /* Constant address? */
1288 (lval->e_flags & E_MEXPR) == 0) { /* Address in a/x? */
1290 /* Use generic code. Push the address if needed */
1293 /* Fetch the value */
1294 exprhs (CF_NONE, k, lval);
1296 /* Increment value in primary */
1299 /* Store the result back */
1304 /* Special code for some addressing modes - use the special += ops */
1305 if (lval->e_flags & E_MGLOBAL) {
1306 flags |= GlobalModeFlags (lval->e_flags);
1308 g_addeqstatic (flags, lval->e_name, lval->e_const, val);
1310 g_subeqstatic (flags, lval->e_name, lval->e_const, val);
1312 } else if (lval->e_flags & E_MLOCAL) {
1313 /* ref to localvar */
1315 g_addeqlocal (flags, lval->e_const, val);
1317 g_subeqlocal (flags, lval->e_const, val);
1319 } else if (lval->e_flags & E_MCONST) {
1320 /* ref to absolute address */
1321 flags |= CF_ABSOLUTE;
1323 g_addeqstatic (flags, lval->e_const, 0, val);
1325 g_subeqstatic (flags, lval->e_const, 0, val);
1327 } else if (lval->e_flags & E_MEXPR) {
1328 /* Address in a/x, check if we have an offset */
1329 unsigned Offs = (lval->e_flags == E_MEOFFS)? lval->e_const : 0;
1331 g_addeqind (flags, Offs, val);
1333 g_subeqind (flags, Offs, val);
1336 Internal ("Invalid addressing mode");
1341 /* Result is an expression */
1342 lval->e_flags = E_MEXPR;
1347 static void post_incdec (struct expent *lval, int k, void (*inc) (unsigned, unsigned long))
1348 /* Handle i-- and i++ */
1354 Error ("Invalid lvalue");
1358 /* Get the data type */
1359 flags = TypeOf (lval->e_tptr);
1361 /* Push the address if needed */
1364 /* Fetch the value and save it (since it's the result of the expression) */
1365 exprhs (CF_NONE, 1, lval);
1366 g_save (flags | CF_FORCECHAR);
1368 /* If we have a pointer expression, increment by the size of the type */
1369 if (lval->e_tptr[0] == T_PTR) {
1370 inc (flags | CF_CONST | CF_FORCECHAR, SizeOf (lval->e_tptr + 1));
1372 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1375 /* Store the result back */
1378 /* Restore the original value */
1379 g_restore (flags | CF_FORCECHAR);
1380 lval->e_flags = E_MEXPR;
1385 static void unaryop (int tok, struct expent* lval)
1386 /* Handle unary -/+ and ~ */
1393 if (k == 0 && lval->e_flags & E_MCONST) {
1394 /* Value is constant */
1396 case TOK_MINUS: lval->e_const = -lval->e_const; break;
1397 case TOK_PLUS: break;
1398 case TOK_COMP: lval->e_const = ~lval->e_const; break;
1399 default: Internal ("Unexpected token: %d", tok);
1402 /* Value is not constant */
1403 exprhs (CF_NONE, k, lval);
1405 /* Get the type of the expression */
1406 flags = TypeOf (lval->e_tptr);
1408 /* Handle the operation */
1410 case TOK_MINUS: g_neg (flags); break;
1411 case TOK_PLUS: break;
1412 case TOK_COMP: g_com (flags); break;
1413 default: Internal ("Unexpected token: %d", tok);
1415 lval->e_flags = E_MEXPR;
1421 static int typecast (struct expent* lval)
1422 /* Handle an explicit cast */
1425 type Type[MAXTYPELEN];
1427 /* Skip the left paren */
1436 /* Read the expression we have to cast */
1439 /* If the expression is a function, treat it as pointer-to-function */
1440 if (IsTypeFunc (lval->e_tptr)) {
1441 lval->e_tptr = PointerTo (lval->e_tptr);
1444 /* Check for a constant on the right side */
1445 if (k == 0 && lval->e_flags == E_MCONST) {
1447 /* A cast of a constant to something else. If the new type is an int,
1448 * be sure to handle the size extension correctly. If the new type is
1449 * not an int, the cast is implementation specific anyway, so leave
1452 if (IsClassInt (Type)) {
1454 /* Get the current and new size of the value */
1455 unsigned OldSize = SizeOf (lval->e_tptr);
1456 unsigned NewSize = SizeOf (Type);
1457 unsigned OldBits = OldSize * 8;
1458 unsigned NewBits = NewSize * 8;
1460 /* Check if the new datatype will have a smaller range */
1461 if (NewSize < OldSize) {
1463 /* Cut the value to the new size */
1464 lval->e_const &= (0xFFFFFFFFUL >> (32 - NewBits));
1466 /* If the new value is signed, sign extend the value */
1467 if (!IsSignUnsigned (Type)) {
1468 lval->e_const |= ((~0L) << NewBits);
1471 } else if (NewSize > OldSize) {
1473 /* Sign extend the value if needed */
1474 if (!IsSignUnsigned (Type) && !IsSignUnsigned (lval->e_tptr)) {
1475 if (lval->e_const & (0x01UL << (OldBits-1))) {
1476 lval->e_const |= ((~0L) << OldBits);
1484 /* Not a constant. Be sure to ignore casts to void */
1485 if (!IsTypeVoid (Type)) {
1487 /* If the size does not change, leave the value alone. Otherwise,
1488 * we have to load the value into the primary and generate code to
1489 * cast the value in the primary register.
1491 if (SizeOf (Type) != SizeOf (lval->e_tptr)) {
1493 /* Load the value into the primary */
1494 exprhs (CF_NONE, k, lval);
1496 /* Mark the lhs as const to avoid a manipulation of TOS */
1497 g_typecast (TypeOf (Type) | CF_CONST, TypeOf (lval->e_tptr));
1499 /* Value is now in primary */
1500 lval->e_flags = E_MEXPR;
1506 /* In any case, use the new type */
1507 lval->e_tptr = TypeDup (Type);
1515 static int hie10 (struct expent* lval)
1516 /* Handle ++, --, !, unary - etc. */
1524 pre_incdec (lval, g_inc);
1528 pre_incdec (lval, g_dec);
1534 unaryop (curtok, lval);
1539 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1540 /* Constant expression */
1541 lval->e_const = !lval->e_const;
1543 g_bneg (TypeOf (lval->e_tptr));
1544 lval->e_test |= E_CC; /* bneg will set cc */
1545 lval->e_flags = E_MEXPR; /* say it's an expr */
1547 return 0; /* expr not storable */
1551 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1552 /* Expression is not const, indirect value loaded into primary */
1553 lval->e_flags = E_MEXPR;
1554 lval->e_const = 0; /* Offset is zero now */
1557 if (IsClassPtr (t)) {
1558 lval->e_tptr = Indirect (t);
1560 Error ("Illegal indirection");
1567 /* The & operator may be applied to any lvalue, and it may be
1568 * applied to functions, even if they're no lvalues.
1570 if (k == 0 && !IsTypeFunc (lval->e_tptr)) {
1571 /* Allow the & operator with an array */
1572 if (!IsTypeArray (lval->e_tptr)) {
1573 Error ("Illegal address");
1576 t = TypeAlloc (TypeLen (lval->e_tptr) + 2);
1578 TypeCpy (t + 1, lval->e_tptr);
1585 if (istypeexpr ()) {
1586 type Type[MAXTYPELEN];
1588 lval->e_const = SizeOf (ParseType (Type));
1591 /* Remember the output queue pointer */
1592 CodeMark Mark = GetCodePos ();
1594 lval->e_const = SizeOf (lval->e_tptr);
1595 /* Remove any generated code */
1598 lval->e_flags = E_MCONST | E_TCONST;
1599 lval->e_tptr = type_uint;
1600 lval->e_test &= ~E_CC;
1604 if (istypeexpr ()) {
1606 return typecast (lval);
1613 post_incdec (lval, k, g_inc);
1617 post_incdec (lval, k, g_dec);
1627 static int hie_internal (GenDesc** ops, /* List of generators */
1628 struct expent* lval, /* parent expr's lval */
1629 int (*hienext) (struct expent*),
1630 int* UsedGen) /* next higher level */
1631 /* Helper function */
1634 struct expent lval2;
1638 token_t tok; /* The operator token */
1639 unsigned ltype, type;
1640 int rconst; /* Operand is a constant */
1646 while ((Gen = FindGen (curtok, ops)) != 0) {
1648 /* Tell the caller that we handled it's ops */
1651 /* All operators that call this function expect an int on the lhs */
1652 if (!IsClassInt (lval->e_tptr)) {
1653 Error ("Integer expression expected");
1656 /* Remember the operator token, then skip it */
1660 /* Get the lhs on stack */
1661 Mark1 = GetCodePos ();
1662 ltype = TypeOf (lval->e_tptr);
1663 if (k == 0 && lval->e_flags == E_MCONST) {
1664 /* Constant value */
1665 Mark2 = GetCodePos ();
1666 g_push (ltype | CF_CONST, lval->e_const);
1668 /* Value not constant */
1669 exprhs (CF_NONE, k, lval);
1670 Mark2 = GetCodePos ();
1674 /* Get the right hand side */
1675 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1677 /* Check the type of the rhs */
1678 if (!IsClassInt (lval2.e_tptr)) {
1679 Error ("Integer expression expected");
1682 /* Check for const operands */
1683 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1685 /* Both operands are constant, remove the generated code */
1689 /* Evaluate the result */
1690 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1692 /* Get the type of the result */
1693 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1697 /* If the right hand side is constant, and the generator function
1698 * expects the lhs in the primary, remove the push of the primary
1701 unsigned rtype = TypeOf (lval2.e_tptr);
1704 /* Second value is constant - check for div */
1707 if (tok == TOK_DIV && lval2.e_const == 0) {
1708 Error ("Division by zero");
1709 } else if (tok == TOK_MOD && lval2.e_const == 0) {
1710 Error ("Modulo operation with zero");
1712 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1715 ltype |= CF_REG; /* Value is in register */
1719 /* Determine the type of the operation result. */
1720 type |= g_typeadjust (ltype, rtype);
1721 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1724 Gen->Func (type, lval2.e_const);
1725 lval->e_flags = E_MEXPR;
1728 /* We have a rvalue now */
1737 static int hie_compare (GenDesc** ops, /* List of generators */
1738 struct expent* lval, /* parent expr's lval */
1739 int (*hienext) (struct expent*))
1740 /* Helper function for the compare operators */
1743 struct expent lval2;
1747 token_t tok; /* The operator token */
1749 int rconst; /* Operand is a constant */
1754 while ((Gen = FindGen (curtok, ops)) != 0) {
1756 /* Remember the operator token, then skip it */
1760 /* Get the lhs on stack */
1761 Mark1 = GetCodePos ();
1762 ltype = TypeOf (lval->e_tptr);
1763 if (k == 0 && lval->e_flags == E_MCONST) {
1764 /* Constant value */
1765 Mark2 = GetCodePos ();
1766 g_push (ltype | CF_CONST, lval->e_const);
1768 /* Value not constant */
1769 exprhs (CF_NONE, k, lval);
1770 Mark2 = GetCodePos ();
1774 /* Get the right hand side */
1775 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1777 /* Make sure, the types are compatible */
1778 if (IsClassInt (lval->e_tptr)) {
1779 if (!IsClassInt (lval2.e_tptr) && !(IsClassPtr(lval2.e_tptr) && IsNullPtr(lval))) {
1780 Error ("Incompatible types");
1782 } else if (IsClassPtr (lval->e_tptr)) {
1783 if (IsClassPtr (lval2.e_tptr)) {
1784 /* Both pointers are allowed in comparison if they point to
1785 * the same type, or if one of them is a void pointer.
1787 type* left = Indirect (lval->e_tptr);
1788 type* right = Indirect (lval2.e_tptr);
1789 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1790 /* Incomatible pointers */
1791 Error ("Incompatible types");
1793 } else if (!IsNullPtr (&lval2)) {
1794 Error ("Incompatible types");
1798 /* Check for const operands */
1799 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1801 /* Both operands are constant, remove the generated code */
1805 /* Evaluate the result */
1806 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1810 /* If the right hand side is constant, and the generator function
1811 * expects the lhs in the primary, remove the push of the primary
1817 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1820 ltype |= CF_REG; /* Value is in register */
1824 /* Determine the type of the operation result. If the left
1825 * operand is of type char and the right is a constant, or
1826 * if both operands are of type char, we will encode the
1827 * operation as char operation. Otherwise the default
1828 * promotions are used.
1830 if (IsTypeChar (lval->e_tptr) && (IsTypeChar (lval2.e_tptr) || rconst)) {
1832 if (IsSignUnsigned (lval->e_tptr) || IsSignUnsigned (lval2.e_tptr)) {
1833 flags |= CF_UNSIGNED;
1836 flags |= CF_FORCECHAR;
1839 unsigned rtype = TypeOf (lval2.e_tptr) | (flags & CF_CONST);
1840 flags |= g_typeadjust (ltype, rtype);
1844 Gen->Func (flags, lval2.e_const);
1845 lval->e_flags = E_MEXPR;
1848 /* Result type is always int */
1849 lval->e_tptr = type_int;
1851 /* We have a rvalue now, condition codes are set */
1853 lval->e_test |= E_CC;
1861 static int hie9 (struct expent *lval)
1862 /* Process * and / operators. */
1864 static GenDesc* hie9_ops [] = {
1865 &GenMUL, &GenDIV, &GenMOD, 0
1869 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1874 static void parseadd (int k, struct expent* lval)
1875 /* Parse an expression with the binary plus operator. lval contains the
1876 * unprocessed left hand side of the expression and will contain the
1877 * result of the expression on return.
1880 struct expent lval2;
1881 unsigned flags; /* Operation flags */
1882 CodeMark Mark; /* Remember code position */
1883 type* lhst; /* Type of left hand side */
1884 type* rhst; /* Type of right hand side */
1887 /* Skip the PLUS token */
1890 /* Get the left hand side type, initialize operation flags */
1891 lhst = lval->e_tptr;
1894 /* Check for constness on both sides */
1895 if (k == 0 && lval->e_flags == E_MCONST) {
1897 /* The left hand side is a constant. Good. Get rhs */
1898 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1900 /* Right hand side is also constant. Get the rhs type */
1901 rhst = lval2.e_tptr;
1903 /* Both expressions are constants. Check for pointer arithmetic */
1904 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1905 /* Left is pointer, right is int, must scale rhs */
1906 lval->e_const = lval->e_const + lval2.e_const * PSizeOf (lhst);
1907 /* Result type is a pointer */
1908 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1909 /* Left is int, right is pointer, must scale lhs */
1910 lval->e_const = lval->e_const * PSizeOf (rhst) + lval2.e_const;
1911 /* Result type is a pointer */
1912 lval->e_tptr = lval2.e_tptr;
1913 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1914 /* Integer addition */
1915 lval->e_const += lval2.e_const;
1916 typeadjust (lval, &lval2, 1);
1919 Error ("Invalid operands for binary operator `+'");
1922 /* Result is constant, condition codes not set */
1923 lval->e_test = E_MCONST;
1927 /* lhs is constant, rhs is not. Get the rhs type. */
1928 rhst = lval2.e_tptr;
1930 /* Check for pointer arithmetic */
1931 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1932 /* Left is pointer, right is int, must scale rhs */
1933 g_scale (CF_INT, PSizeOf (lhst));
1934 /* Operate on pointers, result type is a pointer */
1936 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1937 /* Left is int, right is pointer, must scale lhs */
1938 lval->e_const *= PSizeOf (rhst);
1939 /* Operate on pointers, result type is a pointer */
1941 lval->e_tptr = lval2.e_tptr;
1942 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1943 /* Integer addition */
1944 flags = typeadjust (lval, &lval2, 1);
1947 Error ("Invalid operands for binary operator `+'");
1950 /* Generate code for the add */
1951 g_inc (flags | CF_CONST, lval->e_const);
1953 /* Result is in primary register */
1954 lval->e_flags = E_MEXPR;
1955 lval->e_test &= ~E_CC;
1961 /* Left hand side is not constant. Get the value onto the stack. */
1962 exprhs (CF_NONE, k, lval); /* --> primary register */
1963 Mark = GetCodePos ();
1964 g_push (TypeOf (lval->e_tptr), 0); /* --> stack */
1966 /* Evaluate the rhs */
1967 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1969 /* Right hand side is a constant. Get the rhs type */
1970 rhst = lval2.e_tptr;
1972 /* Remove pushed value from stack */
1974 pop (TypeOf (lval->e_tptr));
1976 /* Check for pointer arithmetic */
1977 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1978 /* Left is pointer, right is int, must scale rhs */
1979 lval2.e_const *= PSizeOf (lhst);
1980 /* Operate on pointers, result type is a pointer */
1982 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1983 /* Left is int, right is pointer, must scale lhs (ptr only) */
1984 g_scale (CF_INT | CF_CONST, PSizeOf (rhst));
1985 /* Operate on pointers, result type is a pointer */
1987 lval->e_tptr = lval2.e_tptr;
1988 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1989 /* Integer addition */
1990 flags = typeadjust (lval, &lval2, 1);
1993 Error ("Invalid operands for binary operator `+'");
1996 /* Generate code for the add */
1997 g_inc (flags | CF_CONST, lval2.e_const);
1999 /* Result is in primary register */
2000 lval->e_flags = E_MEXPR;
2001 lval->e_test &= ~E_CC;
2005 /* lhs and rhs are not constant. Get the rhs type. */
2006 rhst = lval2.e_tptr;
2008 /* Check for pointer arithmetic */
2009 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2010 /* Left is pointer, right is int, must scale rhs */
2011 g_scale (CF_INT, PSizeOf (lhst));
2012 /* Operate on pointers, result type is a pointer */
2014 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2015 /* Left is int, right is pointer, must scale lhs */
2016 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2017 g_swap (CF_INT); /* Swap TOS and primary */
2018 g_scale (CF_INT, PSizeOf (rhst));
2019 /* Operate on pointers, result type is a pointer */
2021 lval->e_tptr = lval2.e_tptr;
2022 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2023 /* Integer addition */
2024 flags = typeadjust (lval, &lval2, 0);
2027 Error ("Invalid operands for binary operator `+'");
2030 /* Generate code for the add */
2033 /* Result is in primary register */
2034 lval->e_flags = E_MEXPR;
2035 lval->e_test &= ~E_CC;
2044 static void parsesub (int k, struct expent* lval)
2045 /* Parse an expression with the binary minus operator. lval contains the
2046 * unprocessed left hand side of the expression and will contain the
2047 * result of the expression on return.
2050 struct expent lval2;
2051 unsigned flags; /* Operation flags */
2052 type* lhst; /* Type of left hand side */
2053 type* rhst; /* Type of right hand side */
2054 CodeMark Mark1; /* Save position of output queue */
2055 CodeMark Mark2; /* Another position in the queue */
2056 int rscale; /* Scale factor for the result */
2059 /* Skip the MINUS token */
2062 /* Get the left hand side type, initialize operation flags */
2063 lhst = lval->e_tptr;
2065 rscale = 1; /* Scale by 1, that is, don't scale */
2067 /* Remember the output queue position, then bring the value onto the stack */
2068 Mark1 = GetCodePos ();
2069 exprhs (CF_NONE, k, lval); /* --> primary register */
2070 Mark2 = GetCodePos ();
2071 g_push (TypeOf (lhst), 0); /* --> stack */
2073 /* Parse the right hand side */
2074 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2076 /* The right hand side is constant. Get the rhs type. */
2077 rhst = lval2.e_tptr;
2079 /* Check left hand side */
2080 if (k == 0 && lval->e_flags & E_MCONST) {
2082 /* Both sides are constant, remove generated code */
2084 pop (TypeOf (lhst)); /* Clean up the stack */
2086 /* Check for pointer arithmetic */
2087 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2088 /* Left is pointer, right is int, must scale rhs */
2089 lval->e_const -= lval2.e_const * PSizeOf (lhst);
2090 /* Operate on pointers, result type is a pointer */
2091 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2092 /* Left is pointer, right is pointer, must scale result */
2093 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2094 Error ("Incompatible pointer types");
2096 lval->e_const = (lval->e_const - lval2.e_const) / PSizeOf (lhst);
2098 /* Operate on pointers, result type is an integer */
2099 lval->e_tptr = type_int;
2100 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2101 /* Integer subtraction */
2102 typeadjust (lval, &lval2, 1);
2103 lval->e_const -= lval2.e_const;
2106 Error ("Invalid operands for binary operator `-'");
2109 /* Result is constant, condition codes not set */
2110 lval->e_flags = E_MCONST;
2111 lval->e_test &= ~E_CC;
2115 /* Left hand side is not constant, right hand side is.
2116 * Remove pushed value from stack.
2119 pop (TypeOf (lhst));
2121 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2122 /* Left is pointer, right is int, must scale rhs */
2123 lval2.e_const *= PSizeOf (lhst);
2124 /* Operate on pointers, result type is a pointer */
2126 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2127 /* Left is pointer, right is pointer, must scale result */
2128 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2129 Error ("Incompatible pointer types");
2131 rscale = PSizeOf (lhst);
2133 /* Operate on pointers, result type is an integer */
2135 lval->e_tptr = type_int;
2136 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2137 /* Integer subtraction */
2138 flags = typeadjust (lval, &lval2, 1);
2141 Error ("Invalid operands for binary operator `-'");
2144 /* Do the subtraction */
2145 g_dec (flags | CF_CONST, lval2.e_const);
2147 /* If this was a pointer subtraction, we must scale the result */
2149 g_scale (flags, -rscale);
2152 /* Result is in primary register */
2153 lval->e_flags = E_MEXPR;
2154 lval->e_test &= ~E_CC;
2160 /* Right hand side is not constant. Get the rhs type. */
2161 rhst = lval2.e_tptr;
2163 /* Check for pointer arithmetic */
2164 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2165 /* Left is pointer, right is int, must scale rhs */
2166 g_scale (CF_INT, PSizeOf (lhst));
2167 /* Operate on pointers, result type is a pointer */
2169 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2170 /* Left is pointer, right is pointer, must scale result */
2171 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2172 Error ("Incompatible pointer types");
2174 rscale = PSizeOf (lhst);
2176 /* Operate on pointers, result type is an integer */
2178 lval->e_tptr = type_int;
2179 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2180 /* Integer subtraction. If the left hand side descriptor says that
2181 * the lhs is const, we have to remove this mark, since this is no
2182 * longer true, lhs is on stack instead.
2184 if (lval->e_flags == E_MCONST) {
2185 lval->e_flags = E_MEXPR;
2187 /* Adjust operand types */
2188 flags = typeadjust (lval, &lval2, 0);
2191 Error ("Invalid operands for binary operator `-'");
2194 /* Generate code for the sub (the & is a hack here) */
2195 g_sub (flags & ~CF_CONST, 0);
2197 /* If this was a pointer subtraction, we must scale the result */
2199 g_scale (flags, -rscale);
2202 /* Result is in primary register */
2203 lval->e_flags = E_MEXPR;
2204 lval->e_test &= ~E_CC;
2210 static int hie8 (struct expent* lval)
2211 /* Process + and - binary operators. */
2213 int k = hie9 (lval);
2214 while (curtok == TOK_PLUS || curtok == TOK_MINUS) {
2216 if (curtok == TOK_PLUS) {
2229 static int hie7 (struct expent *lval)
2230 /* Parse << and >>. */
2232 static GenDesc* hie7_ops [] = {
2237 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2242 static int hie6 (struct expent *lval)
2243 /* process greater-than type comparators */
2245 static GenDesc* hie6_ops [] = {
2246 &GenLT, &GenLE, &GenGE, &GenGT, 0
2248 return hie_compare (hie6_ops, lval, hie7);
2253 static int hie5 (struct expent *lval)
2255 static GenDesc* hie5_ops[] = {
2258 return hie_compare (hie5_ops, lval, hie6);
2263 static int hie4 (struct expent* lval)
2264 /* Handle & (bitwise and) */
2266 static GenDesc* hie4_ops [] = {
2271 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2276 static int hie3 (struct expent *lval)
2277 /* Handle ^ (bitwise exclusive or) */
2279 static GenDesc* hie3_ops [] = {
2284 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2289 static int hie2 (struct expent *lval)
2290 /* Handle | (bitwise or) */
2292 static GenDesc* hie2_ops [] = {
2297 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2302 static int hieAnd (struct expent* lval, unsigned TrueLab, int* BoolOp)
2303 /* Process "exp && exp" */
2307 struct expent lval2;
2310 if (curtok == TOK_BOOL_AND) {
2312 /* Tell our caller that we're evaluating a boolean */
2315 /* Get a label that we will use for false expressions */
2318 /* If the expr hasn't set condition codes, set the force-test flag */
2319 if ((lval->e_test & E_CC) == 0) {
2320 lval->e_test |= E_FORCETEST;
2323 /* Load the value */
2324 exprhs (CF_FORCECHAR, k, lval);
2326 /* Generate the jump */
2327 g_falsejump (CF_NONE, lab);
2329 /* Parse more boolean and's */
2330 while (curtok == TOK_BOOL_AND) {
2337 if ((lval2.e_test & E_CC) == 0) {
2338 lval2.e_test |= E_FORCETEST;
2340 exprhs (CF_FORCECHAR, k, &lval2);
2342 /* Do short circuit evaluation */
2343 if (curtok == TOK_BOOL_AND) {
2344 g_falsejump (CF_NONE, lab);
2346 /* Last expression - will evaluate to true */
2347 g_truejump (CF_NONE, TrueLab);
2351 /* Define the false jump label here */
2352 g_defloclabel (lab);
2354 /* Define the label */
2355 lval->e_flags = E_MEXPR;
2356 lval->e_test |= E_CC; /* Condition codes are set */
2364 static int hieOr (struct expent *lval)
2365 /* Process "exp || exp". */
2368 struct expent lval2;
2369 int BoolOp = 0; /* Did we have a boolean op? */
2370 int AndOp; /* Did we have a && operation? */
2371 unsigned TrueLab; /* Jump to this label if true */
2375 TrueLab = GetLabel ();
2377 /* Call the next level parser */
2378 k = hieAnd (lval, TrueLab, &BoolOp);
2380 /* Any boolean or's? */
2381 if (curtok == TOK_BOOL_OR) {
2383 /* If the expr hasn't set condition codes, set the force-test flag */
2384 if ((lval->e_test & E_CC) == 0) {
2385 lval->e_test |= E_FORCETEST;
2388 /* Get first expr */
2389 exprhs (CF_FORCECHAR, k, lval);
2391 /* For each expression jump to TrueLab if true. Beware: If we
2392 * had && operators, the jump is already in place!
2395 g_truejump (CF_NONE, TrueLab);
2398 /* Remember that we had a boolean op */
2401 /* while there's more expr */
2402 while (curtok == TOK_BOOL_OR) {
2409 k = hieAnd (&lval2, TrueLab, &AndOp);
2410 if ((lval2.e_test & E_CC) == 0) {
2411 lval2.e_test |= E_FORCETEST;
2413 exprhs (CF_FORCECHAR, k, &lval2);
2415 /* If there is more to come, add shortcut boolean eval.
2416 * Beware: If we had && operators, the jump is already
2420 /* Seems this sometimes generates wrong code */
2421 if (curtok == TOK_BOOL_OR && !AndOp) {
2422 g_truejump (CF_NONE, TrueLab);
2425 g_truejump (CF_NONE, TrueLab);
2428 lval->e_flags = E_MEXPR;
2429 lval->e_test |= E_CC; /* Condition codes are set */
2433 /* If we really had boolean ops, generate the end sequence */
2435 DoneLab = GetLabel ();
2436 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2437 g_falsejump (CF_NONE, DoneLab);
2438 g_defloclabel (TrueLab);
2439 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2440 g_defloclabel (DoneLab);
2447 static int hieQuest (struct expent *lval)
2448 /* Parse "lvalue ? exp : exp" */
2453 struct expent lval2; /* Expression 2 */
2454 struct expent lval3; /* Expression 3 */
2455 type* type2; /* Type of expression 2 */
2456 type* type3; /* Type of expression 3 */
2457 type* rtype; /* Type of result */
2458 CodeMark Mark1; /* Save position in output code */
2459 CodeMark Mark2; /* Save position in output code */
2464 if (curtok == TOK_QUEST) {
2466 if ((lval->e_test & E_CC) == 0) {
2467 /* Condition codes not set, force a test */
2468 lval->e_test |= E_FORCETEST;
2470 exprhs (CF_NONE, k, lval);
2472 g_falsejump (CF_NONE, labf);
2474 /* Parse second and third expression */
2475 expression1 (&lval2);
2479 g_defloclabel (labf);
2480 expression1 (&lval3);
2482 /* Check if any conversions are needed, if so, do them.
2483 * Conversion rules for ?: expression are:
2484 * - if both expressions are int expressions, default promotion
2485 * rules for ints apply.
2486 * - if both expressions are pointers of the same type, the
2487 * result of the expression is of this type.
2488 * - if one of the expressions is a pointer and the other is
2489 * a zero constant, the resulting type is that of the pointer
2491 * - all other cases are flagged by an error.
2493 type2 = lval2.e_tptr;
2494 type3 = lval3.e_tptr;
2495 if (IsClassInt (type2) && IsClassInt (type3)) {
2497 /* Get common type */
2498 rtype = promoteint (type2, type3);
2500 /* Convert the third expression to this type if needed */
2501 g_typecast (TypeOf (rtype), TypeOf (type3));
2503 /* Setup a new label so that the expr3 code will jump around
2504 * the type cast code for expr2.
2506 labf = GetLabel (); /* Get new label */
2507 Mark1 = GetCodePos (); /* Remember current position */
2508 g_jump (labf); /* Jump around code */
2510 /* The jump for expr2 goes here */
2511 g_defloclabel (labt);
2513 /* Create the typecast code for expr2 */
2514 Mark2 = GetCodePos (); /* Remember position */
2515 g_typecast (TypeOf (rtype), TypeOf (type2));
2517 /* If the typecast did not produce code, remove the jump,
2518 * otherwise output the label.
2520 if (GetCodePos() == Mark2) {
2521 RemoveCode (Mark1); /* Remove code */
2523 /* We have typecast code, output label */
2524 g_defloclabel (labf);
2525 labt = 0; /* Mark other label as invalid */
2528 } else if (IsClassPtr (type2) && IsClassPtr (type3)) {
2529 /* Must point to same type */
2530 if (TypeCmp (Indirect (type2), Indirect (type3)) < TC_EQUAL) {
2531 Error ("Incompatible pointer types");
2533 /* Result has the common type */
2534 rtype = lval2.e_tptr;
2535 } else if (IsClassPtr (type2) && IsNullPtr (&lval3)) {
2536 /* Result type is pointer, no cast needed */
2537 rtype = lval2.e_tptr;
2538 } else if (IsNullPtr (&lval2) && IsClassPtr (type3)) {
2539 /* Result type is pointer, no cast needed */
2540 rtype = lval3.e_tptr;
2542 Error ("Incompatible types");
2543 rtype = lval2.e_tptr; /* Doesn't matter here */
2546 /* If we don't have the label defined until now, do it */
2548 g_defloclabel (labt);
2551 /* Setup the target expression */
2552 lval->e_flags = E_MEXPR;
2553 lval->e_tptr = rtype;
2561 static void opeq (GenDesc* Gen, struct expent *lval, int k)
2562 /* Process "op=" operators. */
2564 struct expent lval2;
2571 Error ("Invalid lvalue in assignment");
2575 /* Determine the type of the lhs */
2576 flags = TypeOf (lval->e_tptr);
2577 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2578 lval->e_tptr [0] == T_PTR;
2580 /* Get the lhs address on stack (if needed) */
2583 /* Fetch the lhs into the primary register if needed */
2584 exprhs (CF_NONE, k, lval);
2586 /* Bring the lhs on stack */
2587 Mark = GetCodePos ();
2590 /* Evaluate the rhs */
2591 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2592 /* The resulting value is a constant. If the generator has the NOPUSH
2593 * flag set, don't push the lhs.
2595 if (Gen->Flags & GEN_NOPUSH) {
2600 /* lhs is a pointer, scale rhs */
2601 lval2.e_const *= SizeOf (lval->e_tptr+1);
2604 /* If the lhs is character sized, the operation may be later done
2607 if (SizeOf (lval->e_tptr) == 1) {
2608 flags |= CF_FORCECHAR;
2611 /* Special handling for add and sub - some sort of a hack, but short code */
2612 if (Gen->Func == g_add) {
2613 g_inc (flags | CF_CONST, lval2.e_const);
2614 } else if (Gen->Func == g_sub) {
2615 g_dec (flags | CF_CONST, lval2.e_const);
2617 Gen->Func (flags | CF_CONST, lval2.e_const);
2620 /* rhs is not constant and already in the primary register */
2622 /* lhs is a pointer, scale rhs */
2623 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2626 /* If the lhs is character sized, the operation may be later done
2629 if (SizeOf (lval->e_tptr) == 1) {
2630 flags |= CF_FORCECHAR;
2633 /* Adjust the types of the operands if needed */
2634 Gen->Func (g_typeadjust (flags, TypeOf (lval2.e_tptr)), 0);
2637 lval->e_flags = E_MEXPR;
2642 static void addsubeq (GenDesc* Gen, struct expent *lval, int k)
2643 /* Process the += and -= operators */
2645 struct expent lval2;
2651 Error ("Invalid lvalue in assignment");
2656 /* We're currently only able to handle some adressing modes */
2657 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
2658 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
2659 (lval->e_flags & E_MCONST) == 0) { /* Constant address? */
2660 /* Use generic routine */
2661 opeq (Gen, lval, k);
2665 /* Skip the operator */
2668 /* Check if we have a pointer expression and must scale rhs */
2669 MustScale = (lval->e_tptr [0] == T_PTR);
2671 /* Determine the code generator flags */
2672 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR;
2674 /* Evaluate the rhs */
2675 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2676 /* The resulting value is a constant. */
2678 /* lhs is a pointer, scale rhs */
2679 lval2.e_const *= SizeOf (lval->e_tptr+1);
2683 /* rhs is not constant and already in the primary register */
2685 /* lhs is a pointer, scale rhs */
2686 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2690 /* Adjust the rhs to the lhs */
2691 g_typeadjust (flags, TypeOf (lval2.e_tptr));
2693 /* Output apropriate code */
2694 if (lval->e_flags & E_MGLOBAL) {
2695 /* Static variable */
2696 flags |= GlobalModeFlags (lval->e_flags);
2697 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2698 g_addeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2700 g_subeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2702 } else if (lval->e_flags & E_MLOCAL) {
2703 /* ref to localvar */
2704 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2705 g_addeqlocal (flags, lval->e_const, lval2.e_const);
2707 g_subeqlocal (flags, lval->e_const, lval2.e_const);
2709 } else if (lval->e_flags & E_MCONST) {
2710 /* ref to absolute address */
2711 flags |= CF_ABSOLUTE;
2712 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2713 g_addeqstatic (flags, lval->e_const, 0, lval2.e_const);
2715 g_subeqstatic (flags, lval->e_const, 0, lval2.e_const);
2717 } else if (lval->e_flags & E_MEXPR) {
2718 /* Address in a/x. */
2719 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2720 g_addeqind (flags, lval->e_const, lval2.e_const);
2722 g_subeqind (flags, lval->e_const, lval2.e_const);
2725 Internal ("Invalid addressing mode");
2728 /* Expression is in the primary now */
2729 lval->e_flags = E_MEXPR;
2734 static void Assignment (struct expent* lval)
2735 /* Parse an assignment */
2738 struct expent lval2;
2740 type* ltype = lval->e_tptr;
2742 /* Check for assignment to const */
2743 if (IsQualConst (ltype)) {
2744 Error ("Assignment to const");
2747 /* cc65 does not have full support for handling structs by value. Since
2748 * assigning structs is one of the more useful operations from this
2749 * familiy, allow it here.
2751 if (IsClassStruct (ltype)) {
2753 /* Bring the address of the lhs into the primary and push it */
2754 exprhs (0, 0, lval);
2755 g_push (CF_PTR | CF_UNSIGNED, 0);
2757 /* Get the expression on the right of the '=' into the primary */
2760 /* Get the address */
2761 exprhs (0, 0, &lval2);
2763 /* We need an lvalue */
2764 Error ("Invalid lvalue in assignment");
2767 /* Push the address (or whatever is in ax in case of errors) */
2768 g_push (CF_PTR | CF_UNSIGNED, 0);
2770 /* Check for equality of the structs */
2771 if (TypeCmp (ltype, lval2.e_tptr) < TC_EQUAL) {
2772 Error ("Incompatible types");
2775 /* Load the size of the struct into the primary */
2776 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, SizeOf (ltype), 0);
2778 /* Call the memcpy function */
2779 g_call (CF_FIXARGC, "memcpy", 4);
2783 /* Get the address on stack if needed */
2786 /* No struct, setup flags for the load */
2787 flags = SizeOf (ltype) == 1? CF_FORCECHAR : CF_NONE;
2789 /* Get the expression on the right of the '=' into the primary */
2790 if (evalexpr (flags, hie1, &lval2) == 0) {
2791 /* Constant expression. Adjust the types */
2792 assignadjust (ltype, &lval2);
2793 /* Put the value into the primary register */
2794 lconst (flags, &lval2);
2796 /* Expression is not constant and already in the primary */
2797 assignadjust (ltype, &lval2);
2800 /* Generate a store instruction */
2805 /* Value is still in primary */
2806 lval->e_flags = E_MEXPR;
2811 int hie1 (struct expent* lval)
2812 /* Parse first level of expression hierarchy. */
2816 k = hieQuest (lval);
2826 Error ("Invalid lvalue in assignment");
2832 case TOK_PLUS_ASSIGN:
2833 addsubeq (&GenPASGN, lval, k);
2836 case TOK_MINUS_ASSIGN:
2837 addsubeq (&GenSASGN, lval, k);
2840 case TOK_MUL_ASSIGN:
2841 opeq (&GenMASGN, lval, k);
2844 case TOK_DIV_ASSIGN:
2845 opeq (&GenDASGN, lval, k);
2848 case TOK_MOD_ASSIGN:
2849 opeq (&GenMOASGN, lval, k);
2852 case TOK_SHL_ASSIGN:
2853 opeq (&GenSLASGN, lval, k);
2856 case TOK_SHR_ASSIGN:
2857 opeq (&GenSRASGN, lval, k);
2860 case TOK_AND_ASSIGN:
2861 opeq (&GenAASGN, lval, k);
2864 case TOK_XOR_ASSIGN:
2865 opeq (&GenXOASGN, lval, k);
2869 opeq (&GenOASGN, lval, k);
2880 int hie0 (struct expent *lval)
2881 /* Parse comma operator. */
2886 while (curtok == TOK_COMMA) {
2895 int evalexpr (unsigned flags, int (*f) (struct expent*), struct expent* lval)
2896 /* Will evaluate an expression via the given function. If the result is a
2897 * constant, 0 is returned and the value is put in the lval struct. If the
2898 * result is not constant, exprhs is called to bring the value into the
2899 * primary register and 1 is returned.
2906 if (k == 0 && lval->e_flags == E_MCONST) {
2907 /* Constant expression */
2910 /* Not constant, load into the primary */
2911 exprhs (flags, k, lval);
2918 int expr (int (*func) (), struct expent *lval)
2919 /* Expression parser; func is either hie0 or hie1. */
2928 /* Do some checks if code generation is still constistent */
2929 if (savsp != oursp) {
2931 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
2933 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
2941 void expression1 (struct expent* lval)
2942 /* Evaluate an expression on level 1 (no comma operator) and put it into
2943 * the primary register
2946 memset (lval, 0, sizeof (*lval));
2947 exprhs (CF_NONE, expr (hie1, lval), lval);
2952 void expression (struct expent* lval)
2953 /* Evaluate an expression and put it into the primary register */
2955 memset (lval, 0, sizeof (*lval));
2956 exprhs (CF_NONE, expr (hie0, lval), lval);
2961 void constexpr (struct expent* lval)
2962 /* Get a constant value */
2964 memset (lval, 0, sizeof (*lval));
2965 if (expr (hie1, lval) != 0 || (lval->e_flags & E_MCONST) == 0) {
2966 Error ("Constant expression expected");
2967 /* To avoid any compiler errors, make the expression a valid const */
2968 lval->e_flags = E_MCONST;
2969 lval->e_tptr = type_int;
2976 void intexpr (struct expent* lval)
2977 /* Get an integer expression */
2980 if (!IsClassInt (lval->e_tptr)) {
2981 Error ("Integer expression expected");
2982 /* To avoid any compiler errors, make the expression a valid int */
2983 lval->e_flags = E_MCONST;
2984 lval->e_tptr = type_int;
2991 void boolexpr (struct expent* lval)
2992 /* Get a boolean expression */
2994 /* Read an expression */
2997 /* If it's an integer, it's ok. If it's not an integer, but a pointer,
2998 * the pointer used in a boolean context is also ok
3000 if (!IsClassInt (lval->e_tptr) && !IsClassPtr (lval->e_tptr)) {
3001 Error ("Boolean expression expected");
3002 /* To avoid any compiler errors, make the expression a valid int */
3003 lval->e_flags = E_MCONST;
3004 lval->e_tptr = type_int;
3011 void test (unsigned label, int cond)
3012 /* Generate code to perform test and jump if false. */
3017 /* Eat the parenthesis */
3020 /* Prepare the expression, setup labels */
3021 memset (&lval, 0, sizeof (lval));
3023 /* Generate code to eval the expr */
3024 k = expr (hie0, &lval);
3025 if (k == 0 && lval.e_flags == E_MCONST) {
3026 /* Constant rvalue */
3027 if (cond == 0 && lval.e_const == 0) {
3029 Warning ("Unreachable code");
3030 } else if (cond && lval.e_const) {
3037 /* If the expr hasn't set condition codes, set the force-test flag */
3038 if ((lval.e_test & E_CC) == 0) {
3039 lval.e_test |= E_FORCETEST;
3042 /* Load the value into the primary register */
3043 exprhs (CF_FORCECHAR, k, &lval);
3045 /* Check for the closing brace */
3048 /* Generate the jump */
3050 g_truejump (CF_NONE, label);
3052 /* Special case (putting this here is a small hack - but hey, the
3053 * compiler itself is one big hack...): If a semicolon follows, we
3054 * don't have a statement and may omit the jump.
3056 if (curtok != TOK_SEMI) {
3057 g_falsejump (CF_NONE, label);