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, LiteralPoolLabel, 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, LiteralPoolLabel, 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;
514 static unsigned FunctionParamList (FuncDesc* Func)
515 /* Parse a function parameter list and pass the parameters to the called
516 * function. Depending on several criteria this may be done by just pushing
517 * each parameter separately, or creating the parameter frame once and then
518 * storing into this frame.
519 * The function returns the size of the parameters pushed.
524 /* Initialize variables */
525 SymEntry* Param = 0; /* Keep gcc silent */
526 unsigned ParamSize = 0; /* Size of parameters pushed */
527 unsigned ParamCount = 0; /* Number of parameters pushed */
528 unsigned FrameSize = 0; /* Size of parameter frame */
529 unsigned FrameParams = 0; /* Number of params in frame */
530 int FrameOffs = 0; /* Offset into parameter frame */
531 int Ellipsis = 0; /* Function is variadic */
533 /* As an optimization, we may allocate the complete parameter frame at
534 * once instead of pushing each parameter as it comes. We may do that,
537 * - optimizations that increase code size are enabled (allocating the
538 * stack frame at once gives usually larger code).
539 * - we have more than one parameter to push (don't count the last param
540 * for __fastcall__ functions).
542 if (CodeSizeFactor >= 200) {
544 /* Calculate the number and size of the parameters */
545 FrameParams = Func->ParamCount;
546 FrameSize = Func->ParamSize;
547 if (FrameParams > 0 && (Func->Flags & FD_FASTCALL) != 0) {
548 /* Last parameter is not pushed */
549 const SymEntry* LastParam = Func->SymTab->SymTail;
550 FrameSize -= SizeOf (LastParam->Type);
554 /* Do we have more than one parameter in the frame? */
555 if (FrameParams > 1) {
556 /* Okeydokey, setup the frame */
561 /* Don't use a preallocated frame */
566 /* Parse the actual parameter list */
567 while (curtok != TOK_RPAREN) {
572 /* Add a hint for the optimizer */
573 AddCodeHint ("param:start");
575 /* Count arguments */
578 /* Fetch the pointer to the next argument, check for too many args */
579 if (ParamCount <= Func->ParamCount) {
580 /* Beware: If there are parameters with identical names, they
581 * cannot go into the same symbol table, which means that in this
582 * case of errorneous input, the number of nodes in the symbol
583 * table and ParamCount are NOT equal. We have to handle this case
584 * below to avoid segmentation violations. Since we know that this
585 * problem can only occur if there is more than one parameter,
586 * we will just use the last one.
588 if (ParamCount == 1) {
590 Param = Func->SymTab->SymHead;
591 } else if (Param->NextSym != 0) {
593 Param = Param->NextSym;
594 CHECK ((Param->Flags & SC_PARAM) != 0);
596 } else if (!Ellipsis) {
597 /* Too many arguments. Do we have an open param list? */
598 if ((Func->Flags & FD_VARIADIC) == 0) {
599 /* End of param list reached, no ellipsis */
600 Error ("Too many arguments in function call");
602 /* Assume an ellipsis even in case of errors to avoid an error
603 * message for each other argument.
608 /* Do some optimization: If we have a constant value to push,
609 * use a special function that may optimize.
612 if (!Ellipsis && SizeOf (Param->Type) == 1) {
613 CFlags = CF_FORCECHAR;
616 if (evalexpr (CFlags, hie1, &lval) == 0) {
617 /* A constant value */
621 /* If we don't have an argument spec, accept anything, otherwise
622 * convert the actual argument to the type needed.
625 /* Promote the argument if needed */
626 assignadjust (Param->Type, &lval);
628 /* If we have a prototype, chars may be pushed as chars */
629 Flags |= CF_FORCECHAR;
632 /* Use the type of the argument for the push */
633 Flags |= TypeOf (lval.e_tptr);
635 /* If this is a fastcall function, don't push the last argument */
636 if (ParamCount == Func->ParamCount && (Func->Flags & FD_FASTCALL) != 0) {
637 /* Just load the argument into the primary. This is only needed if
638 * we have a constant argument, otherwise the value is already in
641 if (Flags & CF_CONST) {
642 exprhs (CF_FORCECHAR, 0, &lval);
645 unsigned ArgSize = sizeofarg (Flags);
647 /* We have the space already allocated, store in the frame */
648 CHECK (FrameSize >= ArgSize);
649 FrameSize -= ArgSize;
650 FrameOffs -= ArgSize;
652 g_putlocal (Flags | CF_NOKEEP, FrameOffs, lval.e_const);
654 /* Push the argument */
655 g_push (Flags, lval.e_const);
658 /* Calculate total parameter size */
659 ParamSize += ArgSize;
662 /* Add an optimizer hint */
663 AddCodeHint ("param:end");
665 /* Check for end of argument list */
666 if (curtok != TOK_COMMA) {
672 /* Check if we had enough parameters */
673 if (ParamCount < Func->ParamCount) {
674 Error ("Too few arguments in function call");
677 /* The function returns the size of all parameters pushed onto the stack.
678 * However, if there are parameters missing (which is an error and was
679 * flagged by the compiler) AND a stack frame was preallocated above,
680 * we would loose track of the stackpointer and generate an internal error
681 * later. So we correct the value by the parameters that should have been
682 * pushed to avoid an internal compiler error. Since an error was
683 * generated before, no code will be output anyway.
685 return ParamSize + FrameSize;
690 static void CallFunction (struct expent* lval)
691 /* Perform a function call. Called from hie11, this routine will
692 * either call the named function, or the function pointer in a/x.
695 FuncDesc* Func; /* Function descriptor */
696 unsigned ParamSize; /* Number of parameter bytes */
700 /* Get a pointer to the function descriptor from the type string */
701 Func = GetFuncDesc (lval->e_tptr);
703 /* Initialize vars to keep gcc silent */
706 /* Check if this is a function pointer. If so, save it. If not, check for
707 * special known library functions that may be inlined.
709 if (lval->e_flags & E_MEXPR) {
710 /* Function pointer is in primary register, save it */
711 Mark = GetCodePos ();
713 } else if (InlineStdFuncs && IsStdFunc ((const char*) lval->e_name)) {
714 /* Inline this function */
715 HandleStdFunc (lval);
719 /* Parse the parameter list */
720 ParamSize = FunctionParamList (Func);
722 /* We need the closing bracket here */
726 if (lval->e_flags & E_MEXPR) {
727 /* Function called via pointer: Restore it and call function */
728 if (ParamSize != 0) {
731 /* We had no parameters - remove save code */
734 g_callind (TypeOf (lval->e_tptr), ParamSize);
736 g_call (TypeOf (lval->e_tptr), (const char*) lval->e_name, ParamSize);
743 /* This function parses ASM statements. The syntax of the ASM directive
744 * looks like the one defined for C++ (C has no ASM directive), that is,
745 * a string literal in parenthesis.
751 /* Need left parenthesis */
755 if (curtok != TOK_SCONST) {
756 Error ("String literal expected");
759 /* The string literal may consist of more than one line of assembler
760 * code. Separate the single lines and output the code.
762 const char* S = GetLiteral (curval);
765 /* Allow lines up to 256 bytes */
766 const char* E = strchr (S, '\n');
768 /* Found a newline */
772 int Len = strlen (S);
778 /* Reset the string pointer, effectivly clearing the string from the
779 * string table. Since we're working with one token lookahead, this
780 * will fail if the next token is also a string token, but that's a
781 * syntax error anyway, because we expect a right paren.
783 ResetLiteralPoolOffs (curval);
786 /* Skip the string token */
789 /* Closing paren needed */
795 static int primary (struct expent* lval)
796 /* This is the lowest level of the expression parser. */
800 /* not a test at all, yet */
803 /* Character and integer constants. */
804 if (curtok == TOK_ICONST || curtok == TOK_CCONST) {
805 lval->e_flags = E_MCONST | E_TCONST;
806 lval->e_tptr = curtype;
807 lval->e_const = curval;
812 /* Process parenthesized subexpression by calling the whole parser
815 if (curtok == TOK_LPAREN) {
817 memset (lval, 0, sizeof (*lval)); /* Remove any attributes */
823 /* All others may only be used if the expression evaluation is not called
824 * recursively by the preprocessor.
827 /* Illegal expression in PP mode */
828 Error ("Preprocessor expression expected");
829 lval->e_flags = E_MCONST;
830 lval->e_tptr = type_int;
835 if (curtok == TOK_IDENT) {
840 /* Get a pointer to the symbol table entry */
841 Sym = FindSym (CurTok.Ident);
843 /* Is the symbol known? */
846 /* We found the symbol - skip the name token */
849 /* The expression type is the symbol type */
850 lval->e_tptr = Sym->Type;
852 /* Check for illegal symbol types */
853 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
854 if (Sym->Flags & SC_TYPE) {
855 /* Cannot use type symbols */
856 Error ("Variable identifier expected");
857 /* Assume an int type to make lval valid */
858 lval->e_flags = E_MLOCAL | E_TLOFFS;
859 lval->e_tptr = type_int;
864 /* Check for legal symbol types */
865 if ((Sym->Flags & SC_CONST) == SC_CONST) {
866 /* Enum or some other numeric constant */
867 lval->e_flags = E_MCONST;
868 lval->e_const = Sym->V.ConstVal;
870 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
872 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
873 lval->e_name = (unsigned long) Sym->Name;
875 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
876 /* Local variable. If this is a parameter for a variadic
877 * function, we have to add some address calculations, and the
878 * address is not const.
880 if ((Sym->Flags & SC_PARAM) == SC_PARAM && IsVariadic (CurrentFunc)) {
881 /* Variadic parameter */
882 g_leavariadic (Sym->V.Offs - GetParamSize (CurrentFunc));
883 lval->e_flags = E_MEXPR;
886 /* Normal parameter */
887 lval->e_flags = E_MLOCAL | E_TLOFFS;
888 lval->e_const = Sym->V.Offs;
890 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
891 /* Static variable */
892 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
893 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
894 lval->e_name = (unsigned long) Sym->Name;
896 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
897 lval->e_name = Sym->V.Label;
900 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
901 /* Register variable, zero page based */
902 lval->e_flags = E_MGLOBAL | E_MCONST | E_TREGISTER;
903 lval->e_name = Sym->V.Offs;
906 /* Local static variable */
907 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
908 lval->e_name = Sym->V.Offs;
912 /* The symbol is referenced now */
913 Sym->Flags |= SC_REF;
914 if (IsTypeFunc (lval->e_tptr) || IsTypeArray (lval->e_tptr)) {
920 /* We did not find the symbol. Remember the name, then skip it */
921 strcpy (Ident, CurTok.Ident);
924 /* IDENT is either an auto-declared function or an undefined variable. */
925 if (curtok == TOK_LPAREN) {
926 /* Declare a function returning int. For that purpose, prepare a
927 * function signature for a function having an empty param list
930 Warning ("Function call without a prototype");
931 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
932 lval->e_tptr = Sym->Type;
933 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
934 lval->e_name = (unsigned long) Sym->Name;
940 /* Undeclared Variable */
941 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
942 lval->e_flags = E_MLOCAL | E_TLOFFS;
943 lval->e_tptr = type_int;
945 Error ("Undefined symbol: `%s'", Ident);
951 /* String literal? */
952 if (curtok == TOK_SCONST) {
953 lval->e_flags = E_MCONST | E_TLIT;
954 lval->e_const = curval;
955 lval->e_tptr = GetCharArrayType (strlen (GetLiteral (curval)));
961 if (curtok == TOK_ASM) {
963 lval->e_tptr = type_void;
964 lval->e_flags = E_MEXPR;
969 /* __AX__ and __EAX__ pseudo values? */
970 if (curtok == TOK_AX || curtok == TOK_EAX) {
971 lval->e_tptr = (curtok == TOK_AX)? type_uint : type_ulong;
972 lval->e_flags = E_MREG;
973 lval->e_test &= ~E_CC;
976 return 1; /* May be used as lvalue */
979 /* Illegal primary. */
980 Error ("Expression expected");
981 lval->e_flags = E_MCONST;
982 lval->e_tptr = type_int;
988 static int arrayref (int k, struct expent* lval)
989 /* Handle an array reference */
1003 /* Skip the bracket */
1006 /* Get the type of left side */
1007 tptr1 = lval->e_tptr;
1009 /* We can apply a special treatment for arrays that have a const base
1010 * address. This is true for most arrays and will produce a lot better
1011 * code. Check if this is a const base address.
1013 lflags = lval->e_flags & ~E_MCTYPE;
1014 ConstBaseAddr = (lflags == E_MCONST) || /* Constant numeric address */
1015 (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1016 lflags == E_MLOCAL; /* Local array */
1018 /* If we have a constant base, we delay the address fetch */
1019 Mark1 = GetCodePos ();
1020 Mark2 = 0; /* Silence gcc */
1021 if (!ConstBaseAddr) {
1022 /* Get a pointer to the array into the primary */
1023 exprhs (CF_NONE, k, lval);
1025 /* Get the array pointer on stack. Do not push more than 16
1026 * bit, even if this value is greater, since we cannot handle
1027 * other than 16bit stuff when doing indexing.
1029 Mark2 = GetCodePos ();
1033 /* TOS now contains ptr to array elements. Get the subscript. */
1035 if (l == 0 && lval2.e_flags == E_MCONST) {
1037 /* The array subscript is a constant - remove value from stack */
1038 if (!ConstBaseAddr) {
1042 /* Get an array pointer into the primary */
1043 exprhs (CF_NONE, k, lval);
1046 if (IsClassPtr (tptr1)) {
1048 /* Scale the subscript value according to element size */
1049 lval2.e_const *= PSizeOf (tptr1);
1051 /* Remove code for lhs load */
1054 /* Handle constant base array on stack. Be sure NOT to
1055 * handle pointers the same way, this won't work.
1057 if (IsTypeArray (tptr1) &&
1058 ((lval->e_flags & ~E_MCTYPE) == E_MCONST ||
1059 (lval->e_flags & ~E_MCTYPE) == E_MLOCAL ||
1060 (lval->e_flags & E_MGLOBAL) != 0 ||
1061 (lval->e_flags == E_MEOFFS))) {
1062 lval->e_const += lval2.e_const;
1065 /* Pointer - load into primary and remember offset */
1066 if ((lval->e_flags & E_MEXPR) == 0 || k != 0) {
1067 exprhs (CF_NONE, k, lval);
1069 lval->e_const = lval2.e_const;
1070 lval->e_flags = E_MEOFFS;
1073 /* Result is of element type */
1074 lval->e_tptr = Indirect (tptr1);
1079 } else if (IsClassPtr (tptr2 = lval2.e_tptr)) {
1080 /* Subscript is pointer, get element type */
1081 lval2.e_tptr = Indirect (tptr2);
1083 /* Scale the rhs value in the primary register */
1084 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
1086 lval->e_tptr = lval2.e_tptr;
1088 Error ("Cannot subscript");
1091 /* Add the subscript. Since arrays are indexed by integers,
1092 * we will ignore the true type of the subscript here and
1093 * use always an int.
1095 g_inc (CF_INT | CF_CONST, lval2.e_const);
1099 /* Array subscript is not constant. Load it into the primary */
1100 Mark2 = GetCodePos ();
1101 exprhs (CF_NONE, l, &lval2);
1103 tptr2 = lval2.e_tptr;
1104 if (IsClassPtr (tptr1)) {
1106 /* Get the element type */
1107 lval->e_tptr = Indirect (tptr1);
1109 /* Indexing is based on int's, so we will just use the integer
1110 * portion of the index (which is in (e)ax, so there's no further
1113 g_scale (CF_INT, SizeOf (lval->e_tptr));
1115 } else if (IsClassPtr (tptr2)) {
1117 /* Get the element type */
1118 lval2.e_tptr = Indirect (tptr2);
1120 /* Get the int value on top. If we go here, we're sure,
1121 * both values are 16 bit (the first one was truncated
1122 * if necessary and the second one is a pointer).
1123 * Note: If ConstBaseAddr is true, we don't have a value on
1124 * stack, so to "swap" both, just push the subscript.
1126 if (ConstBaseAddr) {
1128 exprhs (CF_NONE, k, lval);
1135 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
1136 lval->e_tptr = lval2.e_tptr;
1138 Error ("Cannot subscript");
1141 /* The offset is now in the primary register. It didn't have a
1142 * constant base address for the lhs, the lhs address is already
1143 * on stack, and we must add the offset. If the base address was
1144 * constant, we call special functions to add the address to the
1147 if (!ConstBaseAddr) {
1148 /* Add the subscript. Both values are int sized. */
1152 /* If the subscript has itself a constant address, it is often
1153 * a better idea to reverse again the order of the evaluation.
1154 * This will generate better code if the subscript is a byte
1155 * sized variable. But beware: This is only possible if the
1156 * subscript was not scaled, that is, if this was a byte array
1159 rflags = lval2.e_flags & ~E_MCTYPE;
1160 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1161 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1162 rflags == E_MLOCAL; /* Local array */
1164 if (ConstSubAddr && SizeOf (lval->e_tptr) == 1) {
1168 /* Reverse the order of evaluation */
1169 unsigned flags = (SizeOf (lval2.e_tptr) == 1)? CF_CHAR : CF_INT;
1172 /* Get a pointer to the array into the primary. We have changed
1173 * e_tptr above but we need the original type to load the
1174 * address, so restore it temporarily.
1176 SavedType = lval->e_tptr;
1177 lval->e_tptr = tptr1;
1178 exprhs (CF_NONE, k, lval);
1179 lval->e_tptr = SavedType;
1181 /* Add the variable */
1182 if (rflags == E_MLOCAL) {
1183 g_addlocal (flags, lval2.e_const);
1185 flags |= GlobalModeFlags (lval2.e_flags);
1186 g_addstatic (flags, lval2.e_name, lval2.e_const);
1189 if (lflags == E_MCONST) {
1190 /* Constant numeric address. Just add it */
1191 g_inc (CF_INT | CF_UNSIGNED, lval->e_const);
1192 } else if (lflags == E_MLOCAL) {
1193 /* Base address is a local variable address */
1194 if (IsTypeArray (tptr1)) {
1195 g_addaddr_local (CF_INT, lval->e_const);
1197 g_addlocal (CF_PTR, lval->e_const);
1200 /* Base address is a static variable address */
1201 unsigned flags = CF_INT;
1202 flags |= GlobalModeFlags (lval->e_flags);
1203 if (IsTypeArray (tptr1)) {
1204 g_addaddr_static (flags, lval->e_name, lval->e_const);
1206 g_addstatic (flags, lval->e_name, lval->e_const);
1212 lval->e_flags = E_MEXPR;
1215 return !IsTypeArray (lval->e_tptr);
1221 static int structref (int k, struct expent* lval)
1222 /* Process struct field after . or ->. */
1228 /* Skip the token and check for an identifier */
1230 if (curtok != TOK_IDENT) {
1231 Error ("Identifier expected");
1232 lval->e_tptr = type_int;
1236 /* Get the symbol table entry and check for a struct field */
1237 strcpy (Ident, CurTok.Ident);
1239 Field = FindStructField (lval->e_tptr, Ident);
1241 Error ("Struct/union has no field named `%s'", Ident);
1242 lval->e_tptr = type_int;
1246 /* If we have constant input data, the result is also constant */
1247 flags = lval->e_flags & ~E_MCTYPE;
1248 if (flags == E_MCONST ||
1249 (k == 0 && (flags == E_MLOCAL ||
1250 (flags & E_MGLOBAL) != 0 ||
1251 lval->e_flags == E_MEOFFS))) {
1252 lval->e_const += Field->V.Offs;
1254 if ((flags & E_MEXPR) == 0 || k != 0) {
1255 exprhs (CF_NONE, k, lval);
1257 lval->e_const = Field->V.Offs;
1258 lval->e_flags = E_MEOFFS;
1260 lval->e_tptr = Field->Type;
1261 return !IsTypeArray (Field->Type);
1266 static int hie11 (struct expent *lval)
1267 /* Handle compound types (structs and arrays) */
1274 if (curtok < TOK_LBRACK || curtok > TOK_PTR_REF) {
1281 if (curtok == TOK_LBRACK) {
1283 /* Array reference */
1284 k = arrayref (k, lval);
1286 } else if (curtok == TOK_LPAREN) {
1288 /* Function call. Skip the opening parenthesis */
1290 tptr = lval->e_tptr;
1291 if (IsTypeFunc (tptr) || IsTypeFuncPtr (tptr)) {
1292 if (IsTypeFuncPtr (tptr)) {
1293 /* Pointer to function. Handle transparently */
1294 exprhs (CF_NONE, k, lval); /* Function pointer to A/X */
1295 ++lval->e_tptr; /* Skip T_PTR */
1296 lval->e_flags |= E_MEXPR;
1298 CallFunction (lval);
1299 lval->e_flags = E_MEXPR;
1300 lval->e_tptr += DECODE_SIZE + 1; /* Set to result */
1302 Error ("Illegal function call");
1306 } else if (curtok == TOK_DOT) {
1308 if (!IsClassStruct (lval->e_tptr)) {
1309 Error ("Struct expected");
1311 k = structref (0, lval);
1313 } else if (curtok == TOK_PTR_REF) {
1315 tptr = lval->e_tptr;
1316 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1317 Error ("Struct pointer expected");
1319 k = structref (k, lval);
1329 static void store (struct expent* lval)
1330 /* Store primary reg into this reference */
1336 flags = TypeOf (lval->e_tptr);
1337 if (f & E_MGLOBAL) {
1338 flags |= GlobalModeFlags (f);
1345 g_putstatic (flags, lval->e_name, lval->e_const);
1347 } else if (f & E_MLOCAL) {
1348 g_putlocal (flags, lval->e_const, 0);
1349 } else if (f == E_MEOFFS) {
1350 g_putind (flags, lval->e_const);
1351 } else if (f != E_MREG) {
1353 g_putind (flags, 0);
1355 /* Store into absolute address */
1356 g_putstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
1360 /* Assume that each one of the stores will invalidate CC */
1361 lval->e_test &= ~E_CC;
1366 static void pre_incdec (struct expent* lval, void (*inc) (unsigned, unsigned long))
1367 /* Handle --i and ++i */
1374 if ((k = hie10 (lval)) == 0) {
1375 Error ("Invalid lvalue");
1379 /* Get the data type */
1380 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR | CF_CONST;
1382 /* Get the increment value in bytes */
1383 val = (lval->e_tptr [0] == T_PTR)? PSizeOf (lval->e_tptr) : 1;
1385 /* We're currently only able to handle some adressing modes */
1386 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
1387 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
1388 (lval->e_flags & E_MCONST) == 0 && /* Constant address? */
1389 (lval->e_flags & E_MEXPR) == 0) { /* Address in a/x? */
1391 /* Use generic code. Push the address if needed */
1394 /* Fetch the value */
1395 exprhs (CF_NONE, k, lval);
1397 /* Increment value in primary */
1400 /* Store the result back */
1405 /* Special code for some addressing modes - use the special += ops */
1406 if (lval->e_flags & E_MGLOBAL) {
1407 flags |= GlobalModeFlags (lval->e_flags);
1409 g_addeqstatic (flags, lval->e_name, lval->e_const, val);
1411 g_subeqstatic (flags, lval->e_name, lval->e_const, val);
1413 } else if (lval->e_flags & E_MLOCAL) {
1414 /* ref to localvar */
1416 g_addeqlocal (flags, lval->e_const, val);
1418 g_subeqlocal (flags, lval->e_const, val);
1420 } else if (lval->e_flags & E_MCONST) {
1421 /* ref to absolute address */
1422 flags |= CF_ABSOLUTE;
1424 g_addeqstatic (flags, lval->e_const, 0, val);
1426 g_subeqstatic (flags, lval->e_const, 0, val);
1428 } else if (lval->e_flags & E_MEXPR) {
1429 /* Address in a/x, check if we have an offset */
1430 unsigned Offs = (lval->e_flags == E_MEOFFS)? lval->e_const : 0;
1432 g_addeqind (flags, Offs, val);
1434 g_subeqind (flags, Offs, val);
1437 Internal ("Invalid addressing mode");
1442 /* Result is an expression */
1443 lval->e_flags = E_MEXPR;
1448 static void post_incdec (struct expent *lval, int k, void (*inc) (unsigned, unsigned long))
1449 /* Handle i-- and i++ */
1455 Error ("Invalid lvalue");
1459 /* Get the data type */
1460 flags = TypeOf (lval->e_tptr);
1462 /* Push the address if needed */
1465 /* Fetch the value and save it (since it's the result of the expression) */
1466 exprhs (CF_NONE, 1, lval);
1467 g_save (flags | CF_FORCECHAR);
1469 /* If we have a pointer expression, increment by the size of the type */
1470 if (lval->e_tptr[0] == T_PTR) {
1471 inc (flags | CF_CONST | CF_FORCECHAR, SizeOf (lval->e_tptr + 1));
1473 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1476 /* Store the result back */
1479 /* Restore the original value */
1480 g_restore (flags | CF_FORCECHAR);
1481 lval->e_flags = E_MEXPR;
1486 static void unaryop (int tok, struct expent* lval)
1487 /* Handle unary -/+ and ~ */
1494 if (k == 0 && lval->e_flags & E_MCONST) {
1495 /* Value is constant */
1497 case TOK_MINUS: lval->e_const = -lval->e_const; break;
1498 case TOK_PLUS: break;
1499 case TOK_COMP: lval->e_const = ~lval->e_const; break;
1500 default: Internal ("Unexpected token: %d", tok);
1503 /* Value is not constant */
1504 exprhs (CF_NONE, k, lval);
1506 /* Get the type of the expression */
1507 flags = TypeOf (lval->e_tptr);
1509 /* Handle the operation */
1511 case TOK_MINUS: g_neg (flags); break;
1512 case TOK_PLUS: break;
1513 case TOK_COMP: g_com (flags); break;
1514 default: Internal ("Unexpected token: %d", tok);
1516 lval->e_flags = E_MEXPR;
1522 static int typecast (struct expent* lval)
1523 /* Handle an explicit cast */
1526 type Type[MAXTYPELEN];
1528 /* Skip the left paren */
1537 /* Read the expression we have to cast */
1540 /* If the expression is a function, treat it as pointer-to-function */
1541 if (IsTypeFunc (lval->e_tptr)) {
1542 lval->e_tptr = PointerTo (lval->e_tptr);
1545 /* Check for a constant on the right side */
1546 if (k == 0 && lval->e_flags == E_MCONST) {
1548 /* A cast of a constant to something else. If the new type is an int,
1549 * be sure to handle the size extension correctly. If the new type is
1550 * not an int, the cast is implementation specific anyway, so leave
1553 if (IsClassInt (Type)) {
1555 /* Get the current and new size of the value */
1556 unsigned OldSize = SizeOf (lval->e_tptr);
1557 unsigned NewSize = SizeOf (Type);
1558 unsigned OldBits = OldSize * 8;
1559 unsigned NewBits = NewSize * 8;
1561 /* Check if the new datatype will have a smaller range */
1562 if (NewSize < OldSize) {
1564 /* Cut the value to the new size */
1565 lval->e_const &= (0xFFFFFFFFUL >> (32 - NewBits));
1567 /* If the new value is signed, sign extend the value */
1568 if (!IsSignUnsigned (Type)) {
1569 lval->e_const |= ((~0L) << NewBits);
1572 } else if (NewSize > OldSize) {
1574 /* Sign extend the value if needed */
1575 if (!IsSignUnsigned (Type) && !IsSignUnsigned (lval->e_tptr)) {
1576 if (lval->e_const & (0x01UL << (OldBits-1))) {
1577 lval->e_const |= ((~0L) << OldBits);
1585 /* Not a constant. Be sure to ignore casts to void */
1586 if (!IsTypeVoid (Type)) {
1588 /* If the size does not change, leave the value alone. Otherwise,
1589 * we have to load the value into the primary and generate code to
1590 * cast the value in the primary register.
1592 if (SizeOf (Type) != SizeOf (lval->e_tptr)) {
1594 /* Load the value into the primary */
1595 exprhs (CF_NONE, k, lval);
1597 /* Mark the lhs as const to avoid a manipulation of TOS */
1598 g_typecast (TypeOf (Type) | CF_CONST, TypeOf (lval->e_tptr));
1600 /* Value is now in primary */
1601 lval->e_flags = E_MEXPR;
1607 /* In any case, use the new type */
1608 lval->e_tptr = TypeDup (Type);
1616 static int hie10 (struct expent* lval)
1617 /* Handle ++, --, !, unary - etc. */
1625 pre_incdec (lval, g_inc);
1629 pre_incdec (lval, g_dec);
1635 unaryop (curtok, lval);
1640 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1641 /* Constant expression */
1642 lval->e_const = !lval->e_const;
1644 g_bneg (TypeOf (lval->e_tptr));
1645 lval->e_test |= E_CC; /* bneg will set cc */
1646 lval->e_flags = E_MEXPR; /* say it's an expr */
1648 return 0; /* expr not storable */
1652 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1653 /* Expression is not const, indirect value loaded into primary */
1654 lval->e_flags = E_MEXPR;
1655 lval->e_const = 0; /* Offset is zero now */
1658 if (IsClassPtr (t)) {
1659 lval->e_tptr = Indirect (t);
1661 Error ("Illegal indirection");
1668 /* The & operator may be applied to any lvalue, and it may be
1669 * applied to functions, even if they're no lvalues.
1671 if (k == 0 && !IsTypeFunc (lval->e_tptr)) {
1672 /* Allow the & operator with an array */
1673 if (!IsTypeArray (lval->e_tptr)) {
1674 Error ("Illegal address");
1677 t = TypeAlloc (TypeLen (lval->e_tptr) + 2);
1679 TypeCpy (t + 1, lval->e_tptr);
1686 if (istypeexpr ()) {
1687 type Type[MAXTYPELEN];
1689 lval->e_const = SizeOf (ParseType (Type));
1692 /* Remember the output queue pointer */
1693 CodeMark Mark = GetCodePos ();
1695 lval->e_const = SizeOf (lval->e_tptr);
1696 /* Remove any generated code */
1699 lval->e_flags = E_MCONST | E_TCONST;
1700 lval->e_tptr = type_uint;
1701 lval->e_test &= ~E_CC;
1705 if (istypeexpr ()) {
1707 return typecast (lval);
1714 post_incdec (lval, k, g_inc);
1718 post_incdec (lval, k, g_dec);
1728 static int hie_internal (GenDesc** ops, /* List of generators */
1729 struct expent* lval, /* parent expr's lval */
1730 int (*hienext) (struct expent*),
1731 int* UsedGen) /* next higher level */
1732 /* Helper function */
1735 struct expent lval2;
1739 token_t tok; /* The operator token */
1740 unsigned ltype, type;
1741 int rconst; /* Operand is a constant */
1747 while ((Gen = FindGen (curtok, ops)) != 0) {
1749 /* Tell the caller that we handled it's ops */
1752 /* All operators that call this function expect an int on the lhs */
1753 if (!IsClassInt (lval->e_tptr)) {
1754 Error ("Integer expression expected");
1757 /* Remember the operator token, then skip it */
1761 /* Get the lhs on stack */
1762 Mark1 = GetCodePos ();
1763 ltype = TypeOf (lval->e_tptr);
1764 if (k == 0 && lval->e_flags == E_MCONST) {
1765 /* Constant value */
1766 Mark2 = GetCodePos ();
1767 g_push (ltype | CF_CONST, lval->e_const);
1769 /* Value not constant */
1770 exprhs (CF_NONE, k, lval);
1771 Mark2 = GetCodePos ();
1775 /* Get the right hand side */
1776 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1778 /* Check the type of the rhs */
1779 if (!IsClassInt (lval2.e_tptr)) {
1780 Error ("Integer expression expected");
1783 /* Check for const operands */
1784 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1786 /* Both operands are constant, remove the generated code */
1790 /* Evaluate the result */
1791 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1793 /* Get the type of the result */
1794 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1798 /* If the right hand side is constant, and the generator function
1799 * expects the lhs in the primary, remove the push of the primary
1802 unsigned rtype = TypeOf (lval2.e_tptr);
1805 /* Second value is constant - check for div */
1808 if (tok == TOK_DIV && lval2.e_const == 0) {
1809 Error ("Division by zero");
1810 } else if (tok == TOK_MOD && lval2.e_const == 0) {
1811 Error ("Modulo operation with zero");
1813 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1816 ltype |= CF_REG; /* Value is in register */
1820 /* Determine the type of the operation result. */
1821 type |= g_typeadjust (ltype, rtype);
1822 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1825 Gen->Func (type, lval2.e_const);
1826 lval->e_flags = E_MEXPR;
1829 /* We have a rvalue now */
1838 static int hie_compare (GenDesc** ops, /* List of generators */
1839 struct expent* lval, /* parent expr's lval */
1840 int (*hienext) (struct expent*))
1841 /* Helper function for the compare operators */
1844 struct expent lval2;
1848 token_t tok; /* The operator token */
1850 int rconst; /* Operand is a constant */
1855 while ((Gen = FindGen (curtok, ops)) != 0) {
1857 /* Remember the operator token, then skip it */
1861 /* Get the lhs on stack */
1862 Mark1 = GetCodePos ();
1863 ltype = TypeOf (lval->e_tptr);
1864 if (k == 0 && lval->e_flags == E_MCONST) {
1865 /* Constant value */
1866 Mark2 = GetCodePos ();
1867 g_push (ltype | CF_CONST, lval->e_const);
1869 /* Value not constant */
1870 exprhs (CF_NONE, k, lval);
1871 Mark2 = GetCodePos ();
1875 /* Get the right hand side */
1876 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1878 /* Make sure, the types are compatible */
1879 if (IsClassInt (lval->e_tptr)) {
1880 if (!IsClassInt (lval2.e_tptr) && !(IsClassPtr(lval2.e_tptr) && IsNullPtr(lval))) {
1881 Error ("Incompatible types");
1883 } else if (IsClassPtr (lval->e_tptr)) {
1884 if (IsClassPtr (lval2.e_tptr)) {
1885 /* Both pointers are allowed in comparison if they point to
1886 * the same type, or if one of them is a void pointer.
1888 type* left = Indirect (lval->e_tptr);
1889 type* right = Indirect (lval2.e_tptr);
1890 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1891 /* Incomatible pointers */
1892 Error ("Incompatible types");
1894 } else if (!IsNullPtr (&lval2)) {
1895 Error ("Incompatible types");
1899 /* Check for const operands */
1900 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1902 /* Both operands are constant, remove the generated code */
1906 /* Evaluate the result */
1907 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1911 /* If the right hand side is constant, and the generator function
1912 * expects the lhs in the primary, remove the push of the primary
1918 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1921 ltype |= CF_REG; /* Value is in register */
1925 /* Determine the type of the operation result. If the left
1926 * operand is of type char and the right is a constant, or
1927 * if both operands are of type char, we will encode the
1928 * operation as char operation. Otherwise the default
1929 * promotions are used.
1931 if (IsTypeChar (lval->e_tptr) && (IsTypeChar (lval2.e_tptr) || rconst)) {
1933 if (IsSignUnsigned (lval->e_tptr) || IsSignUnsigned (lval2.e_tptr)) {
1934 flags |= CF_UNSIGNED;
1937 flags |= CF_FORCECHAR;
1940 unsigned rtype = TypeOf (lval2.e_tptr) | (flags & CF_CONST);
1941 flags |= g_typeadjust (ltype, rtype);
1945 Gen->Func (flags, lval2.e_const);
1946 lval->e_flags = E_MEXPR;
1949 /* Result type is always int */
1950 lval->e_tptr = type_int;
1952 /* We have a rvalue now, condition codes are set */
1954 lval->e_test |= E_CC;
1962 static int hie9 (struct expent *lval)
1963 /* Process * and / operators. */
1965 static GenDesc* hie9_ops [] = {
1966 &GenMUL, &GenDIV, &GenMOD, 0
1970 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1975 static void parseadd (int k, struct expent* lval)
1976 /* Parse an expression with the binary plus operator. lval contains the
1977 * unprocessed left hand side of the expression and will contain the
1978 * result of the expression on return.
1981 struct expent lval2;
1982 unsigned flags; /* Operation flags */
1983 CodeMark Mark; /* Remember code position */
1984 type* lhst; /* Type of left hand side */
1985 type* rhst; /* Type of right hand side */
1988 /* Skip the PLUS token */
1991 /* Get the left hand side type, initialize operation flags */
1992 lhst = lval->e_tptr;
1995 /* Check for constness on both sides */
1996 if (k == 0 && lval->e_flags == E_MCONST) {
1998 /* The left hand side is a constant. Good. Get rhs */
1999 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2001 /* Right hand side is also constant. Get the rhs type */
2002 rhst = lval2.e_tptr;
2004 /* Both expressions are constants. Check for pointer arithmetic */
2005 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2006 /* Left is pointer, right is int, must scale rhs */
2007 lval->e_const = lval->e_const + lval2.e_const * PSizeOf (lhst);
2008 /* Result type is a pointer */
2009 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2010 /* Left is int, right is pointer, must scale lhs */
2011 lval->e_const = lval->e_const * PSizeOf (rhst) + lval2.e_const;
2012 /* Result type is a pointer */
2013 lval->e_tptr = lval2.e_tptr;
2014 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2015 /* Integer addition */
2016 lval->e_const += lval2.e_const;
2017 typeadjust (lval, &lval2, 1);
2020 Error ("Invalid operands for binary operator `+'");
2023 /* Result is constant, condition codes not set */
2024 lval->e_test = E_MCONST;
2028 /* lhs is constant, rhs is not. Get the rhs type. */
2029 rhst = lval2.e_tptr;
2031 /* Check for pointer arithmetic */
2032 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2033 /* Left is pointer, right is int, must scale rhs */
2034 g_scale (CF_INT, PSizeOf (lhst));
2035 /* Operate on pointers, result type is a pointer */
2037 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2038 /* Left is int, right is pointer, must scale lhs */
2039 lval->e_const *= PSizeOf (rhst);
2040 /* Operate on pointers, result type is a pointer */
2042 lval->e_tptr = lval2.e_tptr;
2043 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2044 /* Integer addition */
2045 flags = typeadjust (lval, &lval2, 1);
2048 Error ("Invalid operands for binary operator `+'");
2051 /* Generate code for the add */
2052 g_inc (flags | CF_CONST, lval->e_const);
2054 /* Result is in primary register */
2055 lval->e_flags = E_MEXPR;
2056 lval->e_test &= ~E_CC;
2062 /* Left hand side is not constant. Get the value onto the stack. */
2063 exprhs (CF_NONE, k, lval); /* --> primary register */
2064 Mark = GetCodePos ();
2065 g_push (TypeOf (lval->e_tptr), 0); /* --> stack */
2067 /* Evaluate the rhs */
2068 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2070 /* Right hand side is a constant. Get the rhs type */
2071 rhst = lval2.e_tptr;
2073 /* Remove pushed value from stack */
2075 pop (TypeOf (lval->e_tptr));
2077 /* Check for pointer arithmetic */
2078 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2079 /* Left is pointer, right is int, must scale rhs */
2080 lval2.e_const *= PSizeOf (lhst);
2081 /* Operate on pointers, result type is a pointer */
2083 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2084 /* Left is int, right is pointer, must scale lhs (ptr only) */
2085 g_scale (CF_INT | CF_CONST, PSizeOf (rhst));
2086 /* Operate on pointers, result type is a pointer */
2088 lval->e_tptr = lval2.e_tptr;
2089 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2090 /* Integer addition */
2091 flags = typeadjust (lval, &lval2, 1);
2094 Error ("Invalid operands for binary operator `+'");
2097 /* Generate code for the add */
2098 g_inc (flags | CF_CONST, lval2.e_const);
2100 /* Result is in primary register */
2101 lval->e_flags = E_MEXPR;
2102 lval->e_test &= ~E_CC;
2106 /* lhs and rhs are not constant. Get the rhs type. */
2107 rhst = lval2.e_tptr;
2109 /* Check for pointer arithmetic */
2110 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2111 /* Left is pointer, right is int, must scale rhs */
2112 g_scale (CF_INT, PSizeOf (lhst));
2113 /* Operate on pointers, result type is a pointer */
2115 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2116 /* Left is int, right is pointer, must scale lhs */
2117 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2118 g_swap (CF_INT); /* Swap TOS and primary */
2119 g_scale (CF_INT, PSizeOf (rhst));
2120 /* Operate on pointers, result type is a pointer */
2122 lval->e_tptr = lval2.e_tptr;
2123 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2124 /* Integer addition */
2125 flags = typeadjust (lval, &lval2, 0);
2128 Error ("Invalid operands for binary operator `+'");
2131 /* Generate code for the add */
2134 /* Result is in primary register */
2135 lval->e_flags = E_MEXPR;
2136 lval->e_test &= ~E_CC;
2145 static void parsesub (int k, struct expent* lval)
2146 /* Parse an expression with the binary minus operator. lval contains the
2147 * unprocessed left hand side of the expression and will contain the
2148 * result of the expression on return.
2151 struct expent lval2;
2152 unsigned flags; /* Operation flags */
2153 type* lhst; /* Type of left hand side */
2154 type* rhst; /* Type of right hand side */
2155 CodeMark Mark1; /* Save position of output queue */
2156 CodeMark Mark2; /* Another position in the queue */
2157 int rscale; /* Scale factor for the result */
2160 /* Skip the MINUS token */
2163 /* Get the left hand side type, initialize operation flags */
2164 lhst = lval->e_tptr;
2166 rscale = 1; /* Scale by 1, that is, don't scale */
2168 /* Remember the output queue position, then bring the value onto the stack */
2169 Mark1 = GetCodePos ();
2170 exprhs (CF_NONE, k, lval); /* --> primary register */
2171 Mark2 = GetCodePos ();
2172 g_push (TypeOf (lhst), 0); /* --> stack */
2174 /* Parse the right hand side */
2175 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2177 /* The right hand side is constant. Get the rhs type. */
2178 rhst = lval2.e_tptr;
2180 /* Check left hand side */
2181 if (k == 0 && lval->e_flags & E_MCONST) {
2183 /* Both sides are constant, remove generated code */
2185 pop (TypeOf (lhst)); /* Clean up the stack */
2187 /* Check for pointer arithmetic */
2188 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2189 /* Left is pointer, right is int, must scale rhs */
2190 lval->e_const -= lval2.e_const * PSizeOf (lhst);
2191 /* Operate on pointers, result type is a pointer */
2192 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2193 /* Left is pointer, right is pointer, must scale result */
2194 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2195 Error ("Incompatible pointer types");
2197 lval->e_const = (lval->e_const - lval2.e_const) / PSizeOf (lhst);
2199 /* Operate on pointers, result type is an integer */
2200 lval->e_tptr = type_int;
2201 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2202 /* Integer subtraction */
2203 typeadjust (lval, &lval2, 1);
2204 lval->e_const -= lval2.e_const;
2207 Error ("Invalid operands for binary operator `-'");
2210 /* Result is constant, condition codes not set */
2211 lval->e_flags = E_MCONST;
2212 lval->e_test &= ~E_CC;
2216 /* Left hand side is not constant, right hand side is.
2217 * Remove pushed value from stack.
2220 pop (TypeOf (lhst));
2222 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2223 /* Left is pointer, right is int, must scale rhs */
2224 lval2.e_const *= PSizeOf (lhst);
2225 /* Operate on pointers, result type is a pointer */
2227 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2228 /* Left is pointer, right is pointer, must scale result */
2229 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2230 Error ("Incompatible pointer types");
2232 rscale = PSizeOf (lhst);
2234 /* Operate on pointers, result type is an integer */
2236 lval->e_tptr = type_int;
2237 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2238 /* Integer subtraction */
2239 flags = typeadjust (lval, &lval2, 1);
2242 Error ("Invalid operands for binary operator `-'");
2245 /* Do the subtraction */
2246 g_dec (flags | CF_CONST, lval2.e_const);
2248 /* If this was a pointer subtraction, we must scale the result */
2250 g_scale (flags, -rscale);
2253 /* Result is in primary register */
2254 lval->e_flags = E_MEXPR;
2255 lval->e_test &= ~E_CC;
2261 /* Right hand side is not constant. Get the rhs type. */
2262 rhst = lval2.e_tptr;
2264 /* Check for pointer arithmetic */
2265 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2266 /* Left is pointer, right is int, must scale rhs */
2267 g_scale (CF_INT, PSizeOf (lhst));
2268 /* Operate on pointers, result type is a pointer */
2270 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2271 /* Left is pointer, right is pointer, must scale result */
2272 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2273 Error ("Incompatible pointer types");
2275 rscale = PSizeOf (lhst);
2277 /* Operate on pointers, result type is an integer */
2279 lval->e_tptr = type_int;
2280 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2281 /* Integer subtraction. If the left hand side descriptor says that
2282 * the lhs is const, we have to remove this mark, since this is no
2283 * longer true, lhs is on stack instead.
2285 if (lval->e_flags == E_MCONST) {
2286 lval->e_flags = E_MEXPR;
2288 /* Adjust operand types */
2289 flags = typeadjust (lval, &lval2, 0);
2292 Error ("Invalid operands for binary operator `-'");
2295 /* Generate code for the sub (the & is a hack here) */
2296 g_sub (flags & ~CF_CONST, 0);
2298 /* If this was a pointer subtraction, we must scale the result */
2300 g_scale (flags, -rscale);
2303 /* Result is in primary register */
2304 lval->e_flags = E_MEXPR;
2305 lval->e_test &= ~E_CC;
2311 static int hie8 (struct expent* lval)
2312 /* Process + and - binary operators. */
2314 int k = hie9 (lval);
2315 while (curtok == TOK_PLUS || curtok == TOK_MINUS) {
2317 if (curtok == TOK_PLUS) {
2330 static int hie7 (struct expent *lval)
2331 /* Parse << and >>. */
2333 static GenDesc* hie7_ops [] = {
2338 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2343 static int hie6 (struct expent *lval)
2344 /* process greater-than type comparators */
2346 static GenDesc* hie6_ops [] = {
2347 &GenLT, &GenLE, &GenGE, &GenGT, 0
2349 return hie_compare (hie6_ops, lval, hie7);
2354 static int hie5 (struct expent *lval)
2356 static GenDesc* hie5_ops[] = {
2359 return hie_compare (hie5_ops, lval, hie6);
2364 static int hie4 (struct expent* lval)
2365 /* Handle & (bitwise and) */
2367 static GenDesc* hie4_ops [] = {
2372 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2377 static int hie3 (struct expent *lval)
2378 /* Handle ^ (bitwise exclusive or) */
2380 static GenDesc* hie3_ops [] = {
2385 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2390 static int hie2 (struct expent *lval)
2391 /* Handle | (bitwise or) */
2393 static GenDesc* hie2_ops [] = {
2398 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2403 static int hieAnd (struct expent* lval, unsigned TrueLab, int* BoolOp)
2404 /* Process "exp && exp" */
2408 struct expent lval2;
2411 if (curtok == TOK_BOOL_AND) {
2413 /* Tell our caller that we're evaluating a boolean */
2416 /* Get a label that we will use for false expressions */
2417 lab = GetLocalLabel ();
2419 /* If the expr hasn't set condition codes, set the force-test flag */
2420 if ((lval->e_test & E_CC) == 0) {
2421 lval->e_test |= E_FORCETEST;
2424 /* Load the value */
2425 exprhs (CF_FORCECHAR, k, lval);
2427 /* Generate the jump */
2428 g_falsejump (CF_NONE, lab);
2430 /* Parse more boolean and's */
2431 while (curtok == TOK_BOOL_AND) {
2438 if ((lval2.e_test & E_CC) == 0) {
2439 lval2.e_test |= E_FORCETEST;
2441 exprhs (CF_FORCECHAR, k, &lval2);
2443 /* Do short circuit evaluation */
2444 if (curtok == TOK_BOOL_AND) {
2445 g_falsejump (CF_NONE, lab);
2447 /* Last expression - will evaluate to true */
2448 g_truejump (CF_NONE, TrueLab);
2452 /* Define the false jump label here */
2453 g_defcodelabel (lab);
2455 /* Define the label */
2456 lval->e_flags = E_MEXPR;
2457 lval->e_test |= E_CC; /* Condition codes are set */
2465 static int hieOr (struct expent *lval)
2466 /* Process "exp || exp". */
2469 struct expent lval2;
2470 int BoolOp = 0; /* Did we have a boolean op? */
2471 int AndOp; /* Did we have a && operation? */
2472 unsigned TrueLab; /* Jump to this label if true */
2476 TrueLab = GetLocalLabel ();
2478 /* Call the next level parser */
2479 k = hieAnd (lval, TrueLab, &BoolOp);
2481 /* Any boolean or's? */
2482 if (curtok == TOK_BOOL_OR) {
2484 /* If the expr hasn't set condition codes, set the force-test flag */
2485 if ((lval->e_test & E_CC) == 0) {
2486 lval->e_test |= E_FORCETEST;
2489 /* Get first expr */
2490 exprhs (CF_FORCECHAR, k, lval);
2492 /* For each expression jump to TrueLab if true. Beware: If we
2493 * had && operators, the jump is already in place!
2496 g_truejump (CF_NONE, TrueLab);
2499 /* Remember that we had a boolean op */
2502 /* while there's more expr */
2503 while (curtok == TOK_BOOL_OR) {
2510 k = hieAnd (&lval2, TrueLab, &AndOp);
2511 if ((lval2.e_test & E_CC) == 0) {
2512 lval2.e_test |= E_FORCETEST;
2514 exprhs (CF_FORCECHAR, k, &lval2);
2516 /* If there is more to come, add shortcut boolean eval.
2517 * Beware: If we had && operators, the jump is already
2521 /* Seems this sometimes generates wrong code */
2522 if (curtok == TOK_BOOL_OR && !AndOp) {
2523 g_truejump (CF_NONE, TrueLab);
2526 g_truejump (CF_NONE, TrueLab);
2529 lval->e_flags = E_MEXPR;
2530 lval->e_test |= E_CC; /* Condition codes are set */
2534 /* If we really had boolean ops, generate the end sequence */
2536 DoneLab = GetLocalLabel ();
2537 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2538 g_falsejump (CF_NONE, DoneLab);
2539 g_defcodelabel (TrueLab);
2540 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2541 g_defcodelabel (DoneLab);
2548 static int hieQuest (struct expent *lval)
2549 /* Parse "lvalue ? exp : exp" */
2554 struct expent lval2; /* Expression 2 */
2555 struct expent lval3; /* Expression 3 */
2556 type* type2; /* Type of expression 2 */
2557 type* type3; /* Type of expression 3 */
2558 type* rtype; /* Type of result */
2559 CodeMark Mark1; /* Save position in output code */
2560 CodeMark Mark2; /* Save position in output code */
2565 if (curtok == TOK_QUEST) {
2567 if ((lval->e_test & E_CC) == 0) {
2568 /* Condition codes not set, force a test */
2569 lval->e_test |= E_FORCETEST;
2571 exprhs (CF_NONE, k, lval);
2572 labf = GetLocalLabel ();
2573 g_falsejump (CF_NONE, labf);
2575 /* Parse second and third expression */
2576 expression1 (&lval2);
2577 labt = GetLocalLabel ();
2580 g_defcodelabel (labf);
2581 expression1 (&lval3);
2583 /* Check if any conversions are needed, if so, do them.
2584 * Conversion rules for ?: expression are:
2585 * - if both expressions are int expressions, default promotion
2586 * rules for ints apply.
2587 * - if both expressions are pointers of the same type, the
2588 * result of the expression is of this type.
2589 * - if one of the expressions is a pointer and the other is
2590 * a zero constant, the resulting type is that of the pointer
2592 * - all other cases are flagged by an error.
2594 type2 = lval2.e_tptr;
2595 type3 = lval3.e_tptr;
2596 if (IsClassInt (type2) && IsClassInt (type3)) {
2598 /* Get common type */
2599 rtype = promoteint (type2, type3);
2601 /* Convert the third expression to this type if needed */
2602 g_typecast (TypeOf (rtype), TypeOf (type3));
2604 /* Setup a new label so that the expr3 code will jump around
2605 * the type cast code for expr2.
2607 labf = GetLocalLabel (); /* Get new label */
2608 Mark1 = GetCodePos (); /* Remember current position */
2609 g_jump (labf); /* Jump around code */
2611 /* The jump for expr2 goes here */
2612 g_defcodelabel (labt);
2614 /* Create the typecast code for expr2 */
2615 Mark2 = GetCodePos (); /* Remember position */
2616 g_typecast (TypeOf (rtype), TypeOf (type2));
2618 /* Jump here around the typecase code. */
2619 g_defcodelabel (labf);
2620 labt = 0; /* Mark other label as invalid */
2622 } else if (IsClassPtr (type2) && IsClassPtr (type3)) {
2623 /* Must point to same type */
2624 if (TypeCmp (Indirect (type2), Indirect (type3)) < TC_EQUAL) {
2625 Error ("Incompatible pointer types");
2627 /* Result has the common type */
2628 rtype = lval2.e_tptr;
2629 } else if (IsClassPtr (type2) && IsNullPtr (&lval3)) {
2630 /* Result type is pointer, no cast needed */
2631 rtype = lval2.e_tptr;
2632 } else if (IsNullPtr (&lval2) && IsClassPtr (type3)) {
2633 /* Result type is pointer, no cast needed */
2634 rtype = lval3.e_tptr;
2636 Error ("Incompatible types");
2637 rtype = lval2.e_tptr; /* Doesn't matter here */
2640 /* If we don't have the label defined until now, do it */
2642 g_defcodelabel (labt);
2645 /* Setup the target expression */
2646 lval->e_flags = E_MEXPR;
2647 lval->e_tptr = rtype;
2655 static void opeq (GenDesc* Gen, struct expent *lval, int k)
2656 /* Process "op=" operators. */
2658 struct expent lval2;
2665 Error ("Invalid lvalue in assignment");
2669 /* Determine the type of the lhs */
2670 flags = TypeOf (lval->e_tptr);
2671 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2672 lval->e_tptr [0] == T_PTR;
2674 /* Get the lhs address on stack (if needed) */
2677 /* Fetch the lhs into the primary register if needed */
2678 exprhs (CF_NONE, k, lval);
2680 /* Bring the lhs on stack */
2681 Mark = GetCodePos ();
2684 /* Evaluate the rhs */
2685 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2686 /* The resulting value is a constant. If the generator has the NOPUSH
2687 * flag set, don't push the lhs.
2689 if (Gen->Flags & GEN_NOPUSH) {
2694 /* lhs is a pointer, scale rhs */
2695 lval2.e_const *= SizeOf (lval->e_tptr+1);
2698 /* If the lhs is character sized, the operation may be later done
2701 if (SizeOf (lval->e_tptr) == 1) {
2702 flags |= CF_FORCECHAR;
2705 /* Special handling for add and sub - some sort of a hack, but short code */
2706 if (Gen->Func == g_add) {
2707 g_inc (flags | CF_CONST, lval2.e_const);
2708 } else if (Gen->Func == g_sub) {
2709 g_dec (flags | CF_CONST, lval2.e_const);
2711 Gen->Func (flags | CF_CONST, lval2.e_const);
2714 /* rhs is not constant and already in the primary register */
2716 /* lhs is a pointer, scale rhs */
2717 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2720 /* If the lhs is character sized, the operation may be later done
2723 if (SizeOf (lval->e_tptr) == 1) {
2724 flags |= CF_FORCECHAR;
2727 /* Adjust the types of the operands if needed */
2728 Gen->Func (g_typeadjust (flags, TypeOf (lval2.e_tptr)), 0);
2731 lval->e_flags = E_MEXPR;
2736 static void addsubeq (GenDesc* Gen, struct expent *lval, int k)
2737 /* Process the += and -= operators */
2739 struct expent lval2;
2745 Error ("Invalid lvalue in assignment");
2750 /* We're currently only able to handle some adressing modes */
2751 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
2752 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
2753 (lval->e_flags & E_MCONST) == 0) { /* Constant address? */
2754 /* Use generic routine */
2755 opeq (Gen, lval, k);
2759 /* Skip the operator */
2762 /* Check if we have a pointer expression and must scale rhs */
2763 MustScale = (lval->e_tptr [0] == T_PTR);
2765 /* Determine the code generator flags */
2766 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR;
2768 /* Evaluate the rhs */
2769 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2770 /* The resulting value is a constant. */
2772 /* lhs is a pointer, scale rhs */
2773 lval2.e_const *= SizeOf (lval->e_tptr+1);
2777 /* rhs is not constant and already in the primary register */
2779 /* lhs is a pointer, scale rhs */
2780 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2784 /* Adjust the rhs to the lhs */
2785 g_typeadjust (flags, TypeOf (lval2.e_tptr));
2787 /* Output apropriate code */
2788 if (lval->e_flags & E_MGLOBAL) {
2789 /* Static variable */
2790 flags |= GlobalModeFlags (lval->e_flags);
2791 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2792 g_addeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2794 g_subeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2796 } else if (lval->e_flags & E_MLOCAL) {
2797 /* ref to localvar */
2798 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2799 g_addeqlocal (flags, lval->e_const, lval2.e_const);
2801 g_subeqlocal (flags, lval->e_const, lval2.e_const);
2803 } else if (lval->e_flags & E_MCONST) {
2804 /* ref to absolute address */
2805 flags |= CF_ABSOLUTE;
2806 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2807 g_addeqstatic (flags, lval->e_const, 0, lval2.e_const);
2809 g_subeqstatic (flags, lval->e_const, 0, lval2.e_const);
2811 } else if (lval->e_flags & E_MEXPR) {
2812 /* Address in a/x. */
2813 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2814 g_addeqind (flags, lval->e_const, lval2.e_const);
2816 g_subeqind (flags, lval->e_const, lval2.e_const);
2819 Internal ("Invalid addressing mode");
2822 /* Expression is in the primary now */
2823 lval->e_flags = E_MEXPR;
2828 static void Assignment (struct expent* lval)
2829 /* Parse an assignment */
2832 struct expent lval2;
2834 type* ltype = lval->e_tptr;
2836 /* Check for assignment to const */
2837 if (IsQualConst (ltype)) {
2838 Error ("Assignment to const");
2841 /* cc65 does not have full support for handling structs by value. Since
2842 * assigning structs is one of the more useful operations from this
2843 * familiy, allow it here.
2845 if (IsClassStruct (ltype)) {
2847 /* Bring the address of the lhs into the primary and push it */
2848 exprhs (0, 0, lval);
2849 g_push (CF_PTR | CF_UNSIGNED, 0);
2851 /* Get the expression on the right of the '=' into the primary */
2854 /* Get the address */
2855 exprhs (0, 0, &lval2);
2857 /* We need an lvalue */
2858 Error ("Invalid lvalue in assignment");
2861 /* Push the address (or whatever is in ax in case of errors) */
2862 g_push (CF_PTR | CF_UNSIGNED, 0);
2864 /* Check for equality of the structs */
2865 if (TypeCmp (ltype, lval2.e_tptr) < TC_EQUAL) {
2866 Error ("Incompatible types");
2869 /* Load the size of the struct into the primary */
2870 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, SizeOf (ltype), 0);
2872 /* Call the memcpy function */
2873 g_call (CF_FIXARGC, "memcpy", 4);
2877 /* Get the address on stack if needed */
2880 /* No struct, setup flags for the load */
2881 flags = SizeOf (ltype) == 1? CF_FORCECHAR : CF_NONE;
2883 /* Get the expression on the right of the '=' into the primary */
2884 if (evalexpr (flags, hie1, &lval2) == 0) {
2885 /* Constant expression. Adjust the types */
2886 assignadjust (ltype, &lval2);
2887 /* Put the value into the primary register */
2888 lconst (flags, &lval2);
2890 /* Expression is not constant and already in the primary */
2891 assignadjust (ltype, &lval2);
2894 /* Generate a store instruction */
2899 /* Value is still in primary */
2900 lval->e_flags = E_MEXPR;
2905 int hie1 (struct expent* lval)
2906 /* Parse first level of expression hierarchy. */
2910 k = hieQuest (lval);
2920 Error ("Invalid lvalue in assignment");
2926 case TOK_PLUS_ASSIGN:
2927 addsubeq (&GenPASGN, lval, k);
2930 case TOK_MINUS_ASSIGN:
2931 addsubeq (&GenSASGN, lval, k);
2934 case TOK_MUL_ASSIGN:
2935 opeq (&GenMASGN, lval, k);
2938 case TOK_DIV_ASSIGN:
2939 opeq (&GenDASGN, lval, k);
2942 case TOK_MOD_ASSIGN:
2943 opeq (&GenMOASGN, lval, k);
2946 case TOK_SHL_ASSIGN:
2947 opeq (&GenSLASGN, lval, k);
2950 case TOK_SHR_ASSIGN:
2951 opeq (&GenSRASGN, lval, k);
2954 case TOK_AND_ASSIGN:
2955 opeq (&GenAASGN, lval, k);
2958 case TOK_XOR_ASSIGN:
2959 opeq (&GenXOASGN, lval, k);
2963 opeq (&GenOASGN, lval, k);
2974 int hie0 (struct expent *lval)
2975 /* Parse comma operator. */
2980 while (curtok == TOK_COMMA) {
2989 int evalexpr (unsigned flags, int (*f) (struct expent*), struct expent* lval)
2990 /* Will evaluate an expression via the given function. If the result is a
2991 * constant, 0 is returned and the value is put in the lval struct. If the
2992 * result is not constant, exprhs is called to bring the value into the
2993 * primary register and 1 is returned.
3000 if (k == 0 && lval->e_flags == E_MCONST) {
3001 /* Constant expression */
3004 /* Not constant, load into the primary */
3005 exprhs (flags, k, lval);
3012 int expr (int (*func) (struct expent*), struct expent *lval)
3013 /* Expression parser; func is either hie0 or hie1. */
3022 /* Do some checks if code generation is still constistent */
3023 if (savsp != oursp) {
3025 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
3027 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
3035 void expression1 (struct expent* lval)
3036 /* Evaluate an expression on level 1 (no comma operator) and put it into
3037 * the primary register
3040 memset (lval, 0, sizeof (*lval));
3041 exprhs (CF_NONE, expr (hie1, lval), lval);
3046 void expression (struct expent* lval)
3047 /* Evaluate an expression and put it into the primary register */
3049 memset (lval, 0, sizeof (*lval));
3050 exprhs (CF_NONE, expr (hie0, lval), lval);
3055 void constexpr (struct expent* lval)
3056 /* Get a constant value */
3058 memset (lval, 0, sizeof (*lval));
3059 if (expr (hie1, lval) != 0 || (lval->e_flags & E_MCONST) == 0) {
3060 Error ("Constant expression expected");
3061 /* To avoid any compiler errors, make the expression a valid const */
3062 lval->e_flags = E_MCONST;
3063 lval->e_tptr = type_int;
3070 void intexpr (struct expent* lval)
3071 /* Get an integer expression */
3074 if (!IsClassInt (lval->e_tptr)) {
3075 Error ("Integer expression expected");
3076 /* To avoid any compiler errors, make the expression a valid int */
3077 lval->e_flags = E_MCONST;
3078 lval->e_tptr = type_int;
3085 void boolexpr (struct expent* lval)
3086 /* Get a boolean expression */
3088 /* Read an expression */
3091 /* If it's an integer, it's ok. If it's not an integer, but a pointer,
3092 * the pointer used in a boolean context is also ok
3094 if (!IsClassInt (lval->e_tptr) && !IsClassPtr (lval->e_tptr)) {
3095 Error ("Boolean expression expected");
3096 /* To avoid any compiler errors, make the expression a valid int */
3097 lval->e_flags = E_MCONST;
3098 lval->e_tptr = type_int;
3105 void test (unsigned label, int cond)
3106 /* Generate code to perform test and jump if false. */
3111 /* Eat the parenthesis */
3114 /* Prepare the expression, setup labels */
3115 memset (&lval, 0, sizeof (lval));
3117 /* Generate code to eval the expr */
3118 k = expr (hie0, &lval);
3119 if (k == 0 && lval.e_flags == E_MCONST) {
3120 /* Constant rvalue */
3121 if (cond == 0 && lval.e_const == 0) {
3123 Warning ("Unreachable code");
3124 } else if (cond && lval.e_const) {
3131 /* If the expr hasn't set condition codes, set the force-test flag */
3132 if ((lval.e_test & E_CC) == 0) {
3133 lval.e_test |= E_FORCETEST;
3136 /* Load the value into the primary register */
3137 exprhs (CF_FORCECHAR, k, &lval);
3139 /* Check for the closing brace */
3142 /* Generate the jump */
3144 g_truejump (CF_NONE, label);
3146 /* Special case (putting this here is a small hack - but hey, the
3147 * compiler itself is one big hack...): If a semicolon follows, we
3148 * don't have a statement and may omit the jump.
3150 if (curtok != TOK_SEMI) {
3151 g_falsejump (CF_NONE, label);