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.
749 Error ("Currently unavailable");
755 /* Need left parenthesis */
759 if (curtok != TOK_SCONST) {
760 Error ("String literal expected");
762 /* Write the string directly into the output, followed by a newline */
763 AddCodeLine (GetLiteral (curval));
765 /* Reset the string pointer, effectivly clearing the string from the
766 * string table. Since we're working with one token lookahead, this
767 * will fail if the next token is also a string token, but that's a
768 * syntax error anyway, because we expect a right paren.
770 ResetLiteralPoolOffs (curval);
773 /* Skip the string token */
776 /* Closing paren needed */
783 static int primary (struct expent* lval)
784 /* This is the lowest level of the expression parser. */
788 /* not a test at all, yet */
791 /* Character and integer constants. */
792 if (curtok == TOK_ICONST || curtok == TOK_CCONST) {
793 lval->e_flags = E_MCONST | E_TCONST;
794 lval->e_tptr = curtype;
795 lval->e_const = curval;
800 /* Process parenthesized subexpression by calling the whole parser
803 if (curtok == TOK_LPAREN) {
805 memset (lval, 0, sizeof (*lval)); /* Remove any attributes */
811 /* All others may only be used if the expression evaluation is not called
812 * recursively by the preprocessor.
815 /* Illegal expression in PP mode */
816 Error ("Preprocessor expression expected");
817 lval->e_flags = E_MCONST;
818 lval->e_tptr = type_int;
823 if (curtok == TOK_IDENT) {
828 /* Get a pointer to the symbol table entry */
829 Sym = FindSym (CurTok.Ident);
831 /* Is the symbol known? */
834 /* We found the symbol - skip the name token */
837 /* The expression type is the symbol type */
838 lval->e_tptr = Sym->Type;
840 /* Check for illegal symbol types */
841 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
842 if (Sym->Flags & SC_TYPE) {
843 /* Cannot use type symbols */
844 Error ("Variable identifier expected");
845 /* Assume an int type to make lval valid */
846 lval->e_flags = E_MLOCAL | E_TLOFFS;
847 lval->e_tptr = type_int;
852 /* Check for legal symbol types */
853 if ((Sym->Flags & SC_CONST) == SC_CONST) {
854 /* Enum or some other numeric constant */
855 lval->e_flags = E_MCONST;
856 lval->e_const = Sym->V.ConstVal;
858 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
860 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
861 lval->e_name = (unsigned long) Sym->Name;
863 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
864 /* Local variable. If this is a parameter for a variadic
865 * function, we have to add some address calculations, and the
866 * address is not const.
868 if ((Sym->Flags & SC_PARAM) == SC_PARAM && IsVariadic (CurrentFunc)) {
869 /* Variadic parameter */
870 g_leavariadic (Sym->V.Offs - GetParamSize (CurrentFunc));
871 lval->e_flags = E_MEXPR;
874 /* Normal parameter */
875 lval->e_flags = E_MLOCAL | E_TLOFFS;
876 lval->e_const = Sym->V.Offs;
878 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
879 /* Static variable */
880 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
881 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
882 lval->e_name = (unsigned long) Sym->Name;
884 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
885 lval->e_name = Sym->V.Label;
888 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
889 /* Register variable, zero page based */
890 lval->e_flags = E_MGLOBAL | E_MCONST | E_TREGISTER;
891 lval->e_name = Sym->V.Offs;
894 /* Local static variable */
895 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
896 lval->e_name = Sym->V.Offs;
900 /* The symbol is referenced now */
901 Sym->Flags |= SC_REF;
902 if (IsTypeFunc (lval->e_tptr) || IsTypeArray (lval->e_tptr)) {
908 /* We did not find the symbol. Remember the name, then skip it */
909 strcpy (Ident, CurTok.Ident);
912 /* IDENT is either an auto-declared function or an undefined variable. */
913 if (curtok == TOK_LPAREN) {
914 /* Declare a function returning int. For that purpose, prepare a
915 * function signature for a function having an empty param list
918 Warning ("Function call without a prototype");
919 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
920 lval->e_tptr = Sym->Type;
921 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
922 lval->e_name = (unsigned long) Sym->Name;
928 /* Undeclared Variable */
929 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
930 lval->e_flags = E_MLOCAL | E_TLOFFS;
931 lval->e_tptr = type_int;
933 Error ("Undefined symbol: `%s'", Ident);
939 /* String literal? */
940 if (curtok == TOK_SCONST) {
941 lval->e_flags = E_MCONST | E_TLIT;
942 lval->e_const = curval;
943 lval->e_tptr = GetCharArrayType (strlen (GetLiteral (curval)));
949 if (curtok == TOK_ASM) {
951 lval->e_tptr = type_void;
952 lval->e_flags = E_MEXPR;
957 /* __AX__ and __EAX__ pseudo values? */
958 if (curtok == TOK_AX || curtok == TOK_EAX) {
959 lval->e_tptr = (curtok == TOK_AX)? type_uint : type_ulong;
960 lval->e_flags = E_MREG;
961 lval->e_test &= ~E_CC;
964 return 1; /* May be used as lvalue */
967 /* Illegal primary. */
968 Error ("Expression expected");
969 lval->e_flags = E_MCONST;
970 lval->e_tptr = type_int;
976 static int arrayref (int k, struct expent* lval)
977 /* Handle an array reference */
991 /* Skip the bracket */
994 /* Get the type of left side */
995 tptr1 = lval->e_tptr;
997 /* We can apply a special treatment for arrays that have a const base
998 * address. This is true for most arrays and will produce a lot better
999 * code. Check if this is a const base address.
1001 lflags = lval->e_flags & ~E_MCTYPE;
1002 ConstBaseAddr = (lflags == E_MCONST) || /* Constant numeric address */
1003 (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1004 lflags == E_MLOCAL; /* Local array */
1006 /* If we have a constant base, we delay the address fetch */
1007 Mark1 = GetCodePos ();
1008 Mark2 = 0; /* Silence gcc */
1009 if (!ConstBaseAddr) {
1010 /* Get a pointer to the array into the primary */
1011 exprhs (CF_NONE, k, lval);
1013 /* Get the array pointer on stack. Do not push more than 16
1014 * bit, even if this value is greater, since we cannot handle
1015 * other than 16bit stuff when doing indexing.
1017 Mark2 = GetCodePos ();
1021 /* TOS now contains ptr to array elements. Get the subscript. */
1023 if (l == 0 && lval2.e_flags == E_MCONST) {
1025 /* The array subscript is a constant - remove value from stack */
1026 if (!ConstBaseAddr) {
1030 /* Get an array pointer into the primary */
1031 exprhs (CF_NONE, k, lval);
1034 if (IsClassPtr (tptr1)) {
1036 /* Scale the subscript value according to element size */
1037 lval2.e_const *= PSizeOf (tptr1);
1039 /* Remove code for lhs load */
1042 /* Handle constant base array on stack. Be sure NOT to
1043 * handle pointers the same way, this won't work.
1045 if (IsTypeArray (tptr1) &&
1046 ((lval->e_flags & ~E_MCTYPE) == E_MCONST ||
1047 (lval->e_flags & ~E_MCTYPE) == E_MLOCAL ||
1048 (lval->e_flags & E_MGLOBAL) != 0 ||
1049 (lval->e_flags == E_MEOFFS))) {
1050 lval->e_const += lval2.e_const;
1053 /* Pointer - load into primary and remember offset */
1054 if ((lval->e_flags & E_MEXPR) == 0 || k != 0) {
1055 exprhs (CF_NONE, k, lval);
1057 lval->e_const = lval2.e_const;
1058 lval->e_flags = E_MEOFFS;
1061 /* Result is of element type */
1062 lval->e_tptr = Indirect (tptr1);
1067 } else if (IsClassPtr (tptr2 = lval2.e_tptr)) {
1068 /* Subscript is pointer, get element type */
1069 lval2.e_tptr = Indirect (tptr2);
1071 /* Scale the rhs value in the primary register */
1072 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
1074 lval->e_tptr = lval2.e_tptr;
1076 Error ("Cannot subscript");
1079 /* Add the subscript. Since arrays are indexed by integers,
1080 * we will ignore the true type of the subscript here and
1081 * use always an int.
1083 g_inc (CF_INT | CF_CONST, lval2.e_const);
1087 /* Array subscript is not constant. Load it into the primary */
1088 Mark2 = GetCodePos ();
1089 exprhs (CF_NONE, l, &lval2);
1091 tptr2 = lval2.e_tptr;
1092 if (IsClassPtr (tptr1)) {
1094 /* Get the element type */
1095 lval->e_tptr = Indirect (tptr1);
1097 /* Indexing is based on int's, so we will just use the integer
1098 * portion of the index (which is in (e)ax, so there's no further
1101 g_scale (CF_INT, SizeOf (lval->e_tptr));
1103 } else if (IsClassPtr (tptr2)) {
1105 /* Get the element type */
1106 lval2.e_tptr = Indirect (tptr2);
1108 /* Get the int value on top. If we go here, we're sure,
1109 * both values are 16 bit (the first one was truncated
1110 * if necessary and the second one is a pointer).
1111 * Note: If ConstBaseAddr is true, we don't have a value on
1112 * stack, so to "swap" both, just push the subscript.
1114 if (ConstBaseAddr) {
1116 exprhs (CF_NONE, k, lval);
1123 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
1124 lval->e_tptr = lval2.e_tptr;
1126 Error ("Cannot subscript");
1129 /* The offset is now in the primary register. It didn't have a
1130 * constant base address for the lhs, the lhs address is already
1131 * on stack, and we must add the offset. If the base address was
1132 * constant, we call special functions to add the address to the
1135 if (!ConstBaseAddr) {
1136 /* Add the subscript. Both values are int sized. */
1140 /* If the subscript has itself a constant address, it is often
1141 * a better idea to reverse again the order of the evaluation.
1142 * This will generate better code if the subscript is a byte
1143 * sized variable. But beware: This is only possible if the
1144 * subscript was not scaled, that is, if this was a byte array
1147 rflags = lval2.e_flags & ~E_MCTYPE;
1148 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1149 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1150 rflags == E_MLOCAL; /* Local array */
1152 if (ConstSubAddr && SizeOf (lval->e_tptr) == 1) {
1156 /* Reverse the order of evaluation */
1157 unsigned flags = (SizeOf (lval2.e_tptr) == 1)? CF_CHAR : CF_INT;
1160 /* Get a pointer to the array into the primary. We have changed
1161 * e_tptr above but we need the original type to load the
1162 * address, so restore it temporarily.
1164 SavedType = lval->e_tptr;
1165 lval->e_tptr = tptr1;
1166 exprhs (CF_NONE, k, lval);
1167 lval->e_tptr = SavedType;
1169 /* Add the variable */
1170 if (rflags == E_MLOCAL) {
1171 g_addlocal (flags, lval2.e_const);
1173 flags |= GlobalModeFlags (lval2.e_flags);
1174 g_addstatic (flags, lval2.e_name, lval2.e_const);
1177 if (lflags == E_MCONST) {
1178 /* Constant numeric address. Just add it */
1179 g_inc (CF_INT | CF_UNSIGNED, lval->e_const);
1180 } else if (lflags == E_MLOCAL) {
1181 /* Base address is a local variable address */
1182 if (IsTypeArray (tptr1)) {
1183 g_addaddr_local (CF_INT, lval->e_const);
1185 g_addlocal (CF_PTR, lval->e_const);
1188 /* Base address is a static variable address */
1189 unsigned flags = CF_INT;
1190 flags |= GlobalModeFlags (lval->e_flags);
1191 if (IsTypeArray (tptr1)) {
1192 g_addaddr_static (flags, lval->e_name, lval->e_const);
1194 g_addstatic (flags, lval->e_name, lval->e_const);
1200 lval->e_flags = E_MEXPR;
1203 return !IsTypeArray (lval->e_tptr);
1209 static int structref (int k, struct expent* lval)
1210 /* Process struct field after . or ->. */
1216 /* Skip the token and check for an identifier */
1218 if (curtok != TOK_IDENT) {
1219 Error ("Identifier expected");
1220 lval->e_tptr = type_int;
1224 /* Get the symbol table entry and check for a struct field */
1225 strcpy (Ident, CurTok.Ident);
1227 Field = FindStructField (lval->e_tptr, Ident);
1229 Error ("Struct/union has no field named `%s'", Ident);
1230 lval->e_tptr = type_int;
1234 /* If we have constant input data, the result is also constant */
1235 flags = lval->e_flags & ~E_MCTYPE;
1236 if (flags == E_MCONST ||
1237 (k == 0 && (flags == E_MLOCAL ||
1238 (flags & E_MGLOBAL) != 0 ||
1239 lval->e_flags == E_MEOFFS))) {
1240 lval->e_const += Field->V.Offs;
1242 if ((flags & E_MEXPR) == 0 || k != 0) {
1243 exprhs (CF_NONE, k, lval);
1245 lval->e_const = Field->V.Offs;
1246 lval->e_flags = E_MEOFFS;
1248 lval->e_tptr = Field->Type;
1249 return !IsTypeArray (Field->Type);
1254 static int hie11 (struct expent *lval)
1255 /* Handle compound types (structs and arrays) */
1262 if (curtok < TOK_LBRACK || curtok > TOK_PTR_REF) {
1269 if (curtok == TOK_LBRACK) {
1271 /* Array reference */
1272 k = arrayref (k, lval);
1274 } else if (curtok == TOK_LPAREN) {
1276 /* Function call. Skip the opening parenthesis */
1278 tptr = lval->e_tptr;
1279 if (IsTypeFunc (tptr) || IsTypeFuncPtr (tptr)) {
1280 if (IsTypeFuncPtr (tptr)) {
1281 /* Pointer to function. Handle transparently */
1282 exprhs (CF_NONE, k, lval); /* Function pointer to A/X */
1283 ++lval->e_tptr; /* Skip T_PTR */
1284 lval->e_flags |= E_MEXPR;
1286 CallFunction (lval);
1287 lval->e_flags = E_MEXPR;
1288 lval->e_tptr += DECODE_SIZE + 1; /* Set to result */
1290 Error ("Illegal function call");
1294 } else if (curtok == TOK_DOT) {
1296 if (!IsClassStruct (lval->e_tptr)) {
1297 Error ("Struct expected");
1299 k = structref (0, lval);
1301 } else if (curtok == TOK_PTR_REF) {
1303 tptr = lval->e_tptr;
1304 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1305 Error ("Struct pointer expected");
1307 k = structref (k, lval);
1317 static void store (struct expent* lval)
1318 /* Store primary reg into this reference */
1324 flags = TypeOf (lval->e_tptr);
1325 if (f & E_MGLOBAL) {
1326 flags |= GlobalModeFlags (f);
1333 g_putstatic (flags, lval->e_name, lval->e_const);
1335 } else if (f & E_MLOCAL) {
1336 g_putlocal (flags, lval->e_const, 0);
1337 } else if (f == E_MEOFFS) {
1338 g_putind (flags, lval->e_const);
1339 } else if (f != E_MREG) {
1341 g_putind (flags, 0);
1343 /* Store into absolute address */
1344 g_putstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
1348 /* Assume that each one of the stores will invalidate CC */
1349 lval->e_test &= ~E_CC;
1354 static void pre_incdec (struct expent* lval, void (*inc) (unsigned, unsigned long))
1355 /* Handle --i and ++i */
1362 if ((k = hie10 (lval)) == 0) {
1363 Error ("Invalid lvalue");
1367 /* Get the data type */
1368 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR | CF_CONST;
1370 /* Get the increment value in bytes */
1371 val = (lval->e_tptr [0] == T_PTR)? PSizeOf (lval->e_tptr) : 1;
1373 /* We're currently only able to handle some adressing modes */
1374 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
1375 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
1376 (lval->e_flags & E_MCONST) == 0 && /* Constant address? */
1377 (lval->e_flags & E_MEXPR) == 0) { /* Address in a/x? */
1379 /* Use generic code. Push the address if needed */
1382 /* Fetch the value */
1383 exprhs (CF_NONE, k, lval);
1385 /* Increment value in primary */
1388 /* Store the result back */
1393 /* Special code for some addressing modes - use the special += ops */
1394 if (lval->e_flags & E_MGLOBAL) {
1395 flags |= GlobalModeFlags (lval->e_flags);
1397 g_addeqstatic (flags, lval->e_name, lval->e_const, val);
1399 g_subeqstatic (flags, lval->e_name, lval->e_const, val);
1401 } else if (lval->e_flags & E_MLOCAL) {
1402 /* ref to localvar */
1404 g_addeqlocal (flags, lval->e_const, val);
1406 g_subeqlocal (flags, lval->e_const, val);
1408 } else if (lval->e_flags & E_MCONST) {
1409 /* ref to absolute address */
1410 flags |= CF_ABSOLUTE;
1412 g_addeqstatic (flags, lval->e_const, 0, val);
1414 g_subeqstatic (flags, lval->e_const, 0, val);
1416 } else if (lval->e_flags & E_MEXPR) {
1417 /* Address in a/x, check if we have an offset */
1418 unsigned Offs = (lval->e_flags == E_MEOFFS)? lval->e_const : 0;
1420 g_addeqind (flags, Offs, val);
1422 g_subeqind (flags, Offs, val);
1425 Internal ("Invalid addressing mode");
1430 /* Result is an expression */
1431 lval->e_flags = E_MEXPR;
1436 static void post_incdec (struct expent *lval, int k, void (*inc) (unsigned, unsigned long))
1437 /* Handle i-- and i++ */
1443 Error ("Invalid lvalue");
1447 /* Get the data type */
1448 flags = TypeOf (lval->e_tptr);
1450 /* Push the address if needed */
1453 /* Fetch the value and save it (since it's the result of the expression) */
1454 exprhs (CF_NONE, 1, lval);
1455 g_save (flags | CF_FORCECHAR);
1457 /* If we have a pointer expression, increment by the size of the type */
1458 if (lval->e_tptr[0] == T_PTR) {
1459 inc (flags | CF_CONST | CF_FORCECHAR, SizeOf (lval->e_tptr + 1));
1461 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1464 /* Store the result back */
1467 /* Restore the original value */
1468 g_restore (flags | CF_FORCECHAR);
1469 lval->e_flags = E_MEXPR;
1474 static void unaryop (int tok, struct expent* lval)
1475 /* Handle unary -/+ and ~ */
1482 if (k == 0 && lval->e_flags & E_MCONST) {
1483 /* Value is constant */
1485 case TOK_MINUS: lval->e_const = -lval->e_const; break;
1486 case TOK_PLUS: break;
1487 case TOK_COMP: lval->e_const = ~lval->e_const; break;
1488 default: Internal ("Unexpected token: %d", tok);
1491 /* Value is not constant */
1492 exprhs (CF_NONE, k, lval);
1494 /* Get the type of the expression */
1495 flags = TypeOf (lval->e_tptr);
1497 /* Handle the operation */
1499 case TOK_MINUS: g_neg (flags); break;
1500 case TOK_PLUS: break;
1501 case TOK_COMP: g_com (flags); break;
1502 default: Internal ("Unexpected token: %d", tok);
1504 lval->e_flags = E_MEXPR;
1510 static int typecast (struct expent* lval)
1511 /* Handle an explicit cast */
1514 type Type[MAXTYPELEN];
1516 /* Skip the left paren */
1525 /* Read the expression we have to cast */
1528 /* If the expression is a function, treat it as pointer-to-function */
1529 if (IsTypeFunc (lval->e_tptr)) {
1530 lval->e_tptr = PointerTo (lval->e_tptr);
1533 /* Check for a constant on the right side */
1534 if (k == 0 && lval->e_flags == E_MCONST) {
1536 /* A cast of a constant to something else. If the new type is an int,
1537 * be sure to handle the size extension correctly. If the new type is
1538 * not an int, the cast is implementation specific anyway, so leave
1541 if (IsClassInt (Type)) {
1543 /* Get the current and new size of the value */
1544 unsigned OldSize = SizeOf (lval->e_tptr);
1545 unsigned NewSize = SizeOf (Type);
1546 unsigned OldBits = OldSize * 8;
1547 unsigned NewBits = NewSize * 8;
1549 /* Check if the new datatype will have a smaller range */
1550 if (NewSize < OldSize) {
1552 /* Cut the value to the new size */
1553 lval->e_const &= (0xFFFFFFFFUL >> (32 - NewBits));
1555 /* If the new value is signed, sign extend the value */
1556 if (!IsSignUnsigned (Type)) {
1557 lval->e_const |= ((~0L) << NewBits);
1560 } else if (NewSize > OldSize) {
1562 /* Sign extend the value if needed */
1563 if (!IsSignUnsigned (Type) && !IsSignUnsigned (lval->e_tptr)) {
1564 if (lval->e_const & (0x01UL << (OldBits-1))) {
1565 lval->e_const |= ((~0L) << OldBits);
1573 /* Not a constant. Be sure to ignore casts to void */
1574 if (!IsTypeVoid (Type)) {
1576 /* If the size does not change, leave the value alone. Otherwise,
1577 * we have to load the value into the primary and generate code to
1578 * cast the value in the primary register.
1580 if (SizeOf (Type) != SizeOf (lval->e_tptr)) {
1582 /* Load the value into the primary */
1583 exprhs (CF_NONE, k, lval);
1585 /* Mark the lhs as const to avoid a manipulation of TOS */
1586 g_typecast (TypeOf (Type) | CF_CONST, TypeOf (lval->e_tptr));
1588 /* Value is now in primary */
1589 lval->e_flags = E_MEXPR;
1595 /* In any case, use the new type */
1596 lval->e_tptr = TypeDup (Type);
1604 static int hie10 (struct expent* lval)
1605 /* Handle ++, --, !, unary - etc. */
1613 pre_incdec (lval, g_inc);
1617 pre_incdec (lval, g_dec);
1623 unaryop (curtok, lval);
1628 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1629 /* Constant expression */
1630 lval->e_const = !lval->e_const;
1632 g_bneg (TypeOf (lval->e_tptr));
1633 lval->e_test |= E_CC; /* bneg will set cc */
1634 lval->e_flags = E_MEXPR; /* say it's an expr */
1636 return 0; /* expr not storable */
1640 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1641 /* Expression is not const, indirect value loaded into primary */
1642 lval->e_flags = E_MEXPR;
1643 lval->e_const = 0; /* Offset is zero now */
1646 if (IsClassPtr (t)) {
1647 lval->e_tptr = Indirect (t);
1649 Error ("Illegal indirection");
1656 /* The & operator may be applied to any lvalue, and it may be
1657 * applied to functions, even if they're no lvalues.
1659 if (k == 0 && !IsTypeFunc (lval->e_tptr)) {
1660 /* Allow the & operator with an array */
1661 if (!IsTypeArray (lval->e_tptr)) {
1662 Error ("Illegal address");
1665 t = TypeAlloc (TypeLen (lval->e_tptr) + 2);
1667 TypeCpy (t + 1, lval->e_tptr);
1674 if (istypeexpr ()) {
1675 type Type[MAXTYPELEN];
1677 lval->e_const = SizeOf (ParseType (Type));
1680 /* Remember the output queue pointer */
1681 CodeMark Mark = GetCodePos ();
1683 lval->e_const = SizeOf (lval->e_tptr);
1684 /* Remove any generated code */
1687 lval->e_flags = E_MCONST | E_TCONST;
1688 lval->e_tptr = type_uint;
1689 lval->e_test &= ~E_CC;
1693 if (istypeexpr ()) {
1695 return typecast (lval);
1702 post_incdec (lval, k, g_inc);
1706 post_incdec (lval, k, g_dec);
1716 static int hie_internal (GenDesc** ops, /* List of generators */
1717 struct expent* lval, /* parent expr's lval */
1718 int (*hienext) (struct expent*),
1719 int* UsedGen) /* next higher level */
1720 /* Helper function */
1723 struct expent lval2;
1727 token_t tok; /* The operator token */
1728 unsigned ltype, type;
1729 int rconst; /* Operand is a constant */
1735 while ((Gen = FindGen (curtok, ops)) != 0) {
1737 /* Tell the caller that we handled it's ops */
1740 /* All operators that call this function expect an int on the lhs */
1741 if (!IsClassInt (lval->e_tptr)) {
1742 Error ("Integer expression expected");
1745 /* Remember the operator token, then skip it */
1749 /* Get the lhs on stack */
1750 Mark1 = GetCodePos ();
1751 ltype = TypeOf (lval->e_tptr);
1752 if (k == 0 && lval->e_flags == E_MCONST) {
1753 /* Constant value */
1754 Mark2 = GetCodePos ();
1755 g_push (ltype | CF_CONST, lval->e_const);
1757 /* Value not constant */
1758 exprhs (CF_NONE, k, lval);
1759 Mark2 = GetCodePos ();
1763 /* Get the right hand side */
1764 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1766 /* Check the type of the rhs */
1767 if (!IsClassInt (lval2.e_tptr)) {
1768 Error ("Integer expression expected");
1771 /* Check for const operands */
1772 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1774 /* Both operands are constant, remove the generated code */
1778 /* Evaluate the result */
1779 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1781 /* Get the type of the result */
1782 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1786 /* If the right hand side is constant, and the generator function
1787 * expects the lhs in the primary, remove the push of the primary
1790 unsigned rtype = TypeOf (lval2.e_tptr);
1793 /* Second value is constant - check for div */
1796 if (tok == TOK_DIV && lval2.e_const == 0) {
1797 Error ("Division by zero");
1798 } else if (tok == TOK_MOD && lval2.e_const == 0) {
1799 Error ("Modulo operation with zero");
1801 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1804 ltype |= CF_REG; /* Value is in register */
1808 /* Determine the type of the operation result. */
1809 type |= g_typeadjust (ltype, rtype);
1810 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1813 Gen->Func (type, lval2.e_const);
1814 lval->e_flags = E_MEXPR;
1817 /* We have a rvalue now */
1826 static int hie_compare (GenDesc** ops, /* List of generators */
1827 struct expent* lval, /* parent expr's lval */
1828 int (*hienext) (struct expent*))
1829 /* Helper function for the compare operators */
1832 struct expent lval2;
1836 token_t tok; /* The operator token */
1838 int rconst; /* Operand is a constant */
1843 while ((Gen = FindGen (curtok, ops)) != 0) {
1845 /* Remember the operator token, then skip it */
1849 /* Get the lhs on stack */
1850 Mark1 = GetCodePos ();
1851 ltype = TypeOf (lval->e_tptr);
1852 if (k == 0 && lval->e_flags == E_MCONST) {
1853 /* Constant value */
1854 Mark2 = GetCodePos ();
1855 g_push (ltype | CF_CONST, lval->e_const);
1857 /* Value not constant */
1858 exprhs (CF_NONE, k, lval);
1859 Mark2 = GetCodePos ();
1863 /* Get the right hand side */
1864 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1866 /* Make sure, the types are compatible */
1867 if (IsClassInt (lval->e_tptr)) {
1868 if (!IsClassInt (lval2.e_tptr) && !(IsClassPtr(lval2.e_tptr) && IsNullPtr(lval))) {
1869 Error ("Incompatible types");
1871 } else if (IsClassPtr (lval->e_tptr)) {
1872 if (IsClassPtr (lval2.e_tptr)) {
1873 /* Both pointers are allowed in comparison if they point to
1874 * the same type, or if one of them is a void pointer.
1876 type* left = Indirect (lval->e_tptr);
1877 type* right = Indirect (lval2.e_tptr);
1878 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1879 /* Incomatible pointers */
1880 Error ("Incompatible types");
1882 } else if (!IsNullPtr (&lval2)) {
1883 Error ("Incompatible types");
1887 /* Check for const operands */
1888 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1890 /* Both operands are constant, remove the generated code */
1894 /* Evaluate the result */
1895 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1899 /* If the right hand side is constant, and the generator function
1900 * expects the lhs in the primary, remove the push of the primary
1906 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1909 ltype |= CF_REG; /* Value is in register */
1913 /* Determine the type of the operation result. If the left
1914 * operand is of type char and the right is a constant, or
1915 * if both operands are of type char, we will encode the
1916 * operation as char operation. Otherwise the default
1917 * promotions are used.
1919 if (IsTypeChar (lval->e_tptr) && (IsTypeChar (lval2.e_tptr) || rconst)) {
1921 if (IsSignUnsigned (lval->e_tptr) || IsSignUnsigned (lval2.e_tptr)) {
1922 flags |= CF_UNSIGNED;
1925 flags |= CF_FORCECHAR;
1928 unsigned rtype = TypeOf (lval2.e_tptr) | (flags & CF_CONST);
1929 flags |= g_typeadjust (ltype, rtype);
1933 Gen->Func (flags, lval2.e_const);
1934 lval->e_flags = E_MEXPR;
1937 /* Result type is always int */
1938 lval->e_tptr = type_int;
1940 /* We have a rvalue now, condition codes are set */
1942 lval->e_test |= E_CC;
1950 static int hie9 (struct expent *lval)
1951 /* Process * and / operators. */
1953 static GenDesc* hie9_ops [] = {
1954 &GenMUL, &GenDIV, &GenMOD, 0
1958 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1963 static void parseadd (int k, struct expent* lval)
1964 /* Parse an expression with the binary plus operator. lval contains the
1965 * unprocessed left hand side of the expression and will contain the
1966 * result of the expression on return.
1969 struct expent lval2;
1970 unsigned flags; /* Operation flags */
1971 CodeMark Mark; /* Remember code position */
1972 type* lhst; /* Type of left hand side */
1973 type* rhst; /* Type of right hand side */
1976 /* Skip the PLUS token */
1979 /* Get the left hand side type, initialize operation flags */
1980 lhst = lval->e_tptr;
1983 /* Check for constness on both sides */
1984 if (k == 0 && lval->e_flags == E_MCONST) {
1986 /* The left hand side is a constant. Good. Get rhs */
1987 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1989 /* Right hand side is also constant. Get the rhs type */
1990 rhst = lval2.e_tptr;
1992 /* Both expressions are constants. Check for pointer arithmetic */
1993 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1994 /* Left is pointer, right is int, must scale rhs */
1995 lval->e_const = lval->e_const + lval2.e_const * PSizeOf (lhst);
1996 /* Result type is a pointer */
1997 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1998 /* Left is int, right is pointer, must scale lhs */
1999 lval->e_const = lval->e_const * PSizeOf (rhst) + lval2.e_const;
2000 /* Result type is a pointer */
2001 lval->e_tptr = lval2.e_tptr;
2002 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2003 /* Integer addition */
2004 lval->e_const += lval2.e_const;
2005 typeadjust (lval, &lval2, 1);
2008 Error ("Invalid operands for binary operator `+'");
2011 /* Result is constant, condition codes not set */
2012 lval->e_test = E_MCONST;
2016 /* lhs is constant, rhs is not. Get the rhs type. */
2017 rhst = lval2.e_tptr;
2019 /* Check for pointer arithmetic */
2020 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2021 /* Left is pointer, right is int, must scale rhs */
2022 g_scale (CF_INT, PSizeOf (lhst));
2023 /* Operate on pointers, result type is a pointer */
2025 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2026 /* Left is int, right is pointer, must scale lhs */
2027 lval->e_const *= PSizeOf (rhst);
2028 /* Operate on pointers, result type is a pointer */
2030 lval->e_tptr = lval2.e_tptr;
2031 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2032 /* Integer addition */
2033 flags = typeadjust (lval, &lval2, 1);
2036 Error ("Invalid operands for binary operator `+'");
2039 /* Generate code for the add */
2040 g_inc (flags | CF_CONST, lval->e_const);
2042 /* Result is in primary register */
2043 lval->e_flags = E_MEXPR;
2044 lval->e_test &= ~E_CC;
2050 /* Left hand side is not constant. Get the value onto the stack. */
2051 exprhs (CF_NONE, k, lval); /* --> primary register */
2052 Mark = GetCodePos ();
2053 g_push (TypeOf (lval->e_tptr), 0); /* --> stack */
2055 /* Evaluate the rhs */
2056 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2058 /* Right hand side is a constant. Get the rhs type */
2059 rhst = lval2.e_tptr;
2061 /* Remove pushed value from stack */
2063 pop (TypeOf (lval->e_tptr));
2065 /* Check for pointer arithmetic */
2066 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2067 /* Left is pointer, right is int, must scale rhs */
2068 lval2.e_const *= PSizeOf (lhst);
2069 /* Operate on pointers, result type is a pointer */
2071 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2072 /* Left is int, right is pointer, must scale lhs (ptr only) */
2073 g_scale (CF_INT | CF_CONST, PSizeOf (rhst));
2074 /* Operate on pointers, result type is a pointer */
2076 lval->e_tptr = lval2.e_tptr;
2077 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2078 /* Integer addition */
2079 flags = typeadjust (lval, &lval2, 1);
2082 Error ("Invalid operands for binary operator `+'");
2085 /* Generate code for the add */
2086 g_inc (flags | CF_CONST, lval2.e_const);
2088 /* Result is in primary register */
2089 lval->e_flags = E_MEXPR;
2090 lval->e_test &= ~E_CC;
2094 /* lhs and rhs are not constant. Get the rhs type. */
2095 rhst = lval2.e_tptr;
2097 /* Check for pointer arithmetic */
2098 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2099 /* Left is pointer, right is int, must scale rhs */
2100 g_scale (CF_INT, PSizeOf (lhst));
2101 /* Operate on pointers, result type is a pointer */
2103 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2104 /* Left is int, right is pointer, must scale lhs */
2105 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2106 g_swap (CF_INT); /* Swap TOS and primary */
2107 g_scale (CF_INT, PSizeOf (rhst));
2108 /* Operate on pointers, result type is a pointer */
2110 lval->e_tptr = lval2.e_tptr;
2111 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2112 /* Integer addition */
2113 flags = typeadjust (lval, &lval2, 0);
2116 Error ("Invalid operands for binary operator `+'");
2119 /* Generate code for the add */
2122 /* Result is in primary register */
2123 lval->e_flags = E_MEXPR;
2124 lval->e_test &= ~E_CC;
2133 static void parsesub (int k, struct expent* lval)
2134 /* Parse an expression with the binary minus operator. lval contains the
2135 * unprocessed left hand side of the expression and will contain the
2136 * result of the expression on return.
2139 struct expent lval2;
2140 unsigned flags; /* Operation flags */
2141 type* lhst; /* Type of left hand side */
2142 type* rhst; /* Type of right hand side */
2143 CodeMark Mark1; /* Save position of output queue */
2144 CodeMark Mark2; /* Another position in the queue */
2145 int rscale; /* Scale factor for the result */
2148 /* Skip the MINUS token */
2151 /* Get the left hand side type, initialize operation flags */
2152 lhst = lval->e_tptr;
2154 rscale = 1; /* Scale by 1, that is, don't scale */
2156 /* Remember the output queue position, then bring the value onto the stack */
2157 Mark1 = GetCodePos ();
2158 exprhs (CF_NONE, k, lval); /* --> primary register */
2159 Mark2 = GetCodePos ();
2160 g_push (TypeOf (lhst), 0); /* --> stack */
2162 /* Parse the right hand side */
2163 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2165 /* The right hand side is constant. Get the rhs type. */
2166 rhst = lval2.e_tptr;
2168 /* Check left hand side */
2169 if (k == 0 && lval->e_flags & E_MCONST) {
2171 /* Both sides are constant, remove generated code */
2173 pop (TypeOf (lhst)); /* Clean up the stack */
2175 /* Check for pointer arithmetic */
2176 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2177 /* Left is pointer, right is int, must scale rhs */
2178 lval->e_const -= lval2.e_const * PSizeOf (lhst);
2179 /* Operate on pointers, result type is a pointer */
2180 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2181 /* Left is pointer, right is pointer, must scale result */
2182 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2183 Error ("Incompatible pointer types");
2185 lval->e_const = (lval->e_const - lval2.e_const) / PSizeOf (lhst);
2187 /* Operate on pointers, result type is an integer */
2188 lval->e_tptr = type_int;
2189 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2190 /* Integer subtraction */
2191 typeadjust (lval, &lval2, 1);
2192 lval->e_const -= lval2.e_const;
2195 Error ("Invalid operands for binary operator `-'");
2198 /* Result is constant, condition codes not set */
2199 lval->e_flags = E_MCONST;
2200 lval->e_test &= ~E_CC;
2204 /* Left hand side is not constant, right hand side is.
2205 * Remove pushed value from stack.
2208 pop (TypeOf (lhst));
2210 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2211 /* Left is pointer, right is int, must scale rhs */
2212 lval2.e_const *= PSizeOf (lhst);
2213 /* Operate on pointers, result type is a pointer */
2215 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2216 /* Left is pointer, right is pointer, must scale result */
2217 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2218 Error ("Incompatible pointer types");
2220 rscale = PSizeOf (lhst);
2222 /* Operate on pointers, result type is an integer */
2224 lval->e_tptr = type_int;
2225 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2226 /* Integer subtraction */
2227 flags = typeadjust (lval, &lval2, 1);
2230 Error ("Invalid operands for binary operator `-'");
2233 /* Do the subtraction */
2234 g_dec (flags | CF_CONST, lval2.e_const);
2236 /* If this was a pointer subtraction, we must scale the result */
2238 g_scale (flags, -rscale);
2241 /* Result is in primary register */
2242 lval->e_flags = E_MEXPR;
2243 lval->e_test &= ~E_CC;
2249 /* Right hand side is not constant. Get the rhs type. */
2250 rhst = lval2.e_tptr;
2252 /* Check for pointer arithmetic */
2253 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2254 /* Left is pointer, right is int, must scale rhs */
2255 g_scale (CF_INT, PSizeOf (lhst));
2256 /* Operate on pointers, result type is a pointer */
2258 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2259 /* Left is pointer, right is pointer, must scale result */
2260 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2261 Error ("Incompatible pointer types");
2263 rscale = PSizeOf (lhst);
2265 /* Operate on pointers, result type is an integer */
2267 lval->e_tptr = type_int;
2268 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2269 /* Integer subtraction. If the left hand side descriptor says that
2270 * the lhs is const, we have to remove this mark, since this is no
2271 * longer true, lhs is on stack instead.
2273 if (lval->e_flags == E_MCONST) {
2274 lval->e_flags = E_MEXPR;
2276 /* Adjust operand types */
2277 flags = typeadjust (lval, &lval2, 0);
2280 Error ("Invalid operands for binary operator `-'");
2283 /* Generate code for the sub (the & is a hack here) */
2284 g_sub (flags & ~CF_CONST, 0);
2286 /* If this was a pointer subtraction, we must scale the result */
2288 g_scale (flags, -rscale);
2291 /* Result is in primary register */
2292 lval->e_flags = E_MEXPR;
2293 lval->e_test &= ~E_CC;
2299 static int hie8 (struct expent* lval)
2300 /* Process + and - binary operators. */
2302 int k = hie9 (lval);
2303 while (curtok == TOK_PLUS || curtok == TOK_MINUS) {
2305 if (curtok == TOK_PLUS) {
2318 static int hie7 (struct expent *lval)
2319 /* Parse << and >>. */
2321 static GenDesc* hie7_ops [] = {
2326 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2331 static int hie6 (struct expent *lval)
2332 /* process greater-than type comparators */
2334 static GenDesc* hie6_ops [] = {
2335 &GenLT, &GenLE, &GenGE, &GenGT, 0
2337 return hie_compare (hie6_ops, lval, hie7);
2342 static int hie5 (struct expent *lval)
2344 static GenDesc* hie5_ops[] = {
2347 return hie_compare (hie5_ops, lval, hie6);
2352 static int hie4 (struct expent* lval)
2353 /* Handle & (bitwise and) */
2355 static GenDesc* hie4_ops [] = {
2360 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2365 static int hie3 (struct expent *lval)
2366 /* Handle ^ (bitwise exclusive or) */
2368 static GenDesc* hie3_ops [] = {
2373 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2378 static int hie2 (struct expent *lval)
2379 /* Handle | (bitwise or) */
2381 static GenDesc* hie2_ops [] = {
2386 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2391 static int hieAnd (struct expent* lval, unsigned TrueLab, int* BoolOp)
2392 /* Process "exp && exp" */
2396 struct expent lval2;
2399 if (curtok == TOK_BOOL_AND) {
2401 /* Tell our caller that we're evaluating a boolean */
2404 /* Get a label that we will use for false expressions */
2405 lab = GetLocalLabel ();
2407 /* If the expr hasn't set condition codes, set the force-test flag */
2408 if ((lval->e_test & E_CC) == 0) {
2409 lval->e_test |= E_FORCETEST;
2412 /* Load the value */
2413 exprhs (CF_FORCECHAR, k, lval);
2415 /* Generate the jump */
2416 g_falsejump (CF_NONE, lab);
2418 /* Parse more boolean and's */
2419 while (curtok == TOK_BOOL_AND) {
2426 if ((lval2.e_test & E_CC) == 0) {
2427 lval2.e_test |= E_FORCETEST;
2429 exprhs (CF_FORCECHAR, k, &lval2);
2431 /* Do short circuit evaluation */
2432 if (curtok == TOK_BOOL_AND) {
2433 g_falsejump (CF_NONE, lab);
2435 /* Last expression - will evaluate to true */
2436 g_truejump (CF_NONE, TrueLab);
2440 /* Define the false jump label here */
2441 g_defloclabel (lab);
2443 /* Define the label */
2444 lval->e_flags = E_MEXPR;
2445 lval->e_test |= E_CC; /* Condition codes are set */
2453 static int hieOr (struct expent *lval)
2454 /* Process "exp || exp". */
2457 struct expent lval2;
2458 int BoolOp = 0; /* Did we have a boolean op? */
2459 int AndOp; /* Did we have a && operation? */
2460 unsigned TrueLab; /* Jump to this label if true */
2464 TrueLab = GetLocalLabel ();
2466 /* Call the next level parser */
2467 k = hieAnd (lval, TrueLab, &BoolOp);
2469 /* Any boolean or's? */
2470 if (curtok == TOK_BOOL_OR) {
2472 /* If the expr hasn't set condition codes, set the force-test flag */
2473 if ((lval->e_test & E_CC) == 0) {
2474 lval->e_test |= E_FORCETEST;
2477 /* Get first expr */
2478 exprhs (CF_FORCECHAR, k, lval);
2480 /* For each expression jump to TrueLab if true. Beware: If we
2481 * had && operators, the jump is already in place!
2484 g_truejump (CF_NONE, TrueLab);
2487 /* Remember that we had a boolean op */
2490 /* while there's more expr */
2491 while (curtok == TOK_BOOL_OR) {
2498 k = hieAnd (&lval2, TrueLab, &AndOp);
2499 if ((lval2.e_test & E_CC) == 0) {
2500 lval2.e_test |= E_FORCETEST;
2502 exprhs (CF_FORCECHAR, k, &lval2);
2504 /* If there is more to come, add shortcut boolean eval.
2505 * Beware: If we had && operators, the jump is already
2509 /* Seems this sometimes generates wrong code */
2510 if (curtok == TOK_BOOL_OR && !AndOp) {
2511 g_truejump (CF_NONE, TrueLab);
2514 g_truejump (CF_NONE, TrueLab);
2517 lval->e_flags = E_MEXPR;
2518 lval->e_test |= E_CC; /* Condition codes are set */
2522 /* If we really had boolean ops, generate the end sequence */
2524 DoneLab = GetLocalLabel ();
2525 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2526 g_falsejump (CF_NONE, DoneLab);
2527 g_defloclabel (TrueLab);
2528 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2529 g_defloclabel (DoneLab);
2536 static int hieQuest (struct expent *lval)
2537 /* Parse "lvalue ? exp : exp" */
2542 struct expent lval2; /* Expression 2 */
2543 struct expent lval3; /* Expression 3 */
2544 type* type2; /* Type of expression 2 */
2545 type* type3; /* Type of expression 3 */
2546 type* rtype; /* Type of result */
2547 CodeMark Mark1; /* Save position in output code */
2548 CodeMark Mark2; /* Save position in output code */
2553 if (curtok == TOK_QUEST) {
2555 if ((lval->e_test & E_CC) == 0) {
2556 /* Condition codes not set, force a test */
2557 lval->e_test |= E_FORCETEST;
2559 exprhs (CF_NONE, k, lval);
2560 labf = GetLocalLabel ();
2561 g_falsejump (CF_NONE, labf);
2563 /* Parse second and third expression */
2564 expression1 (&lval2);
2565 labt = GetLocalLabel ();
2568 g_defloclabel (labf);
2569 expression1 (&lval3);
2571 /* Check if any conversions are needed, if so, do them.
2572 * Conversion rules for ?: expression are:
2573 * - if both expressions are int expressions, default promotion
2574 * rules for ints apply.
2575 * - if both expressions are pointers of the same type, the
2576 * result of the expression is of this type.
2577 * - if one of the expressions is a pointer and the other is
2578 * a zero constant, the resulting type is that of the pointer
2580 * - all other cases are flagged by an error.
2582 type2 = lval2.e_tptr;
2583 type3 = lval3.e_tptr;
2584 if (IsClassInt (type2) && IsClassInt (type3)) {
2586 /* Get common type */
2587 rtype = promoteint (type2, type3);
2589 /* Convert the third expression to this type if needed */
2590 g_typecast (TypeOf (rtype), TypeOf (type3));
2592 /* Setup a new label so that the expr3 code will jump around
2593 * the type cast code for expr2.
2595 labf = GetLocalLabel (); /* Get new label */
2596 Mark1 = GetCodePos (); /* Remember current position */
2597 g_jump (labf); /* Jump around code */
2599 /* The jump for expr2 goes here */
2600 g_defloclabel (labt);
2602 /* Create the typecast code for expr2 */
2603 Mark2 = GetCodePos (); /* Remember position */
2604 g_typecast (TypeOf (rtype), TypeOf (type2));
2606 /* If the typecast did not produce code, remove the jump,
2607 * otherwise output the label.
2609 if (GetCodePos() == Mark2) {
2610 RemoveCode (Mark1); /* Remove code */
2612 /* We have typecast code, output label */
2613 g_defloclabel (labf);
2614 labt = 0; /* Mark other label as invalid */
2617 } else if (IsClassPtr (type2) && IsClassPtr (type3)) {
2618 /* Must point to same type */
2619 if (TypeCmp (Indirect (type2), Indirect (type3)) < TC_EQUAL) {
2620 Error ("Incompatible pointer types");
2622 /* Result has the common type */
2623 rtype = lval2.e_tptr;
2624 } else if (IsClassPtr (type2) && IsNullPtr (&lval3)) {
2625 /* Result type is pointer, no cast needed */
2626 rtype = lval2.e_tptr;
2627 } else if (IsNullPtr (&lval2) && IsClassPtr (type3)) {
2628 /* Result type is pointer, no cast needed */
2629 rtype = lval3.e_tptr;
2631 Error ("Incompatible types");
2632 rtype = lval2.e_tptr; /* Doesn't matter here */
2635 /* If we don't have the label defined until now, do it */
2637 g_defloclabel (labt);
2640 /* Setup the target expression */
2641 lval->e_flags = E_MEXPR;
2642 lval->e_tptr = rtype;
2650 static void opeq (GenDesc* Gen, struct expent *lval, int k)
2651 /* Process "op=" operators. */
2653 struct expent lval2;
2660 Error ("Invalid lvalue in assignment");
2664 /* Determine the type of the lhs */
2665 flags = TypeOf (lval->e_tptr);
2666 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2667 lval->e_tptr [0] == T_PTR;
2669 /* Get the lhs address on stack (if needed) */
2672 /* Fetch the lhs into the primary register if needed */
2673 exprhs (CF_NONE, k, lval);
2675 /* Bring the lhs on stack */
2676 Mark = GetCodePos ();
2679 /* Evaluate the rhs */
2680 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2681 /* The resulting value is a constant. If the generator has the NOPUSH
2682 * flag set, don't push the lhs.
2684 if (Gen->Flags & GEN_NOPUSH) {
2689 /* lhs is a pointer, scale rhs */
2690 lval2.e_const *= SizeOf (lval->e_tptr+1);
2693 /* If the lhs is character sized, the operation may be later done
2696 if (SizeOf (lval->e_tptr) == 1) {
2697 flags |= CF_FORCECHAR;
2700 /* Special handling for add and sub - some sort of a hack, but short code */
2701 if (Gen->Func == g_add) {
2702 g_inc (flags | CF_CONST, lval2.e_const);
2703 } else if (Gen->Func == g_sub) {
2704 g_dec (flags | CF_CONST, lval2.e_const);
2706 Gen->Func (flags | CF_CONST, lval2.e_const);
2709 /* rhs is not constant and already in the primary register */
2711 /* lhs is a pointer, scale rhs */
2712 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2715 /* If the lhs is character sized, the operation may be later done
2718 if (SizeOf (lval->e_tptr) == 1) {
2719 flags |= CF_FORCECHAR;
2722 /* Adjust the types of the operands if needed */
2723 Gen->Func (g_typeadjust (flags, TypeOf (lval2.e_tptr)), 0);
2726 lval->e_flags = E_MEXPR;
2731 static void addsubeq (GenDesc* Gen, struct expent *lval, int k)
2732 /* Process the += and -= operators */
2734 struct expent lval2;
2740 Error ("Invalid lvalue in assignment");
2745 /* We're currently only able to handle some adressing modes */
2746 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
2747 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
2748 (lval->e_flags & E_MCONST) == 0) { /* Constant address? */
2749 /* Use generic routine */
2750 opeq (Gen, lval, k);
2754 /* Skip the operator */
2757 /* Check if we have a pointer expression and must scale rhs */
2758 MustScale = (lval->e_tptr [0] == T_PTR);
2760 /* Determine the code generator flags */
2761 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR;
2763 /* Evaluate the rhs */
2764 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2765 /* The resulting value is a constant. */
2767 /* lhs is a pointer, scale rhs */
2768 lval2.e_const *= SizeOf (lval->e_tptr+1);
2772 /* rhs is not constant and already in the primary register */
2774 /* lhs is a pointer, scale rhs */
2775 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2779 /* Adjust the rhs to the lhs */
2780 g_typeadjust (flags, TypeOf (lval2.e_tptr));
2782 /* Output apropriate code */
2783 if (lval->e_flags & E_MGLOBAL) {
2784 /* Static variable */
2785 flags |= GlobalModeFlags (lval->e_flags);
2786 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2787 g_addeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2789 g_subeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2791 } else if (lval->e_flags & E_MLOCAL) {
2792 /* ref to localvar */
2793 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2794 g_addeqlocal (flags, lval->e_const, lval2.e_const);
2796 g_subeqlocal (flags, lval->e_const, lval2.e_const);
2798 } else if (lval->e_flags & E_MCONST) {
2799 /* ref to absolute address */
2800 flags |= CF_ABSOLUTE;
2801 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2802 g_addeqstatic (flags, lval->e_const, 0, lval2.e_const);
2804 g_subeqstatic (flags, lval->e_const, 0, lval2.e_const);
2806 } else if (lval->e_flags & E_MEXPR) {
2807 /* Address in a/x. */
2808 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2809 g_addeqind (flags, lval->e_const, lval2.e_const);
2811 g_subeqind (flags, lval->e_const, lval2.e_const);
2814 Internal ("Invalid addressing mode");
2817 /* Expression is in the primary now */
2818 lval->e_flags = E_MEXPR;
2823 static void Assignment (struct expent* lval)
2824 /* Parse an assignment */
2827 struct expent lval2;
2829 type* ltype = lval->e_tptr;
2831 /* Check for assignment to const */
2832 if (IsQualConst (ltype)) {
2833 Error ("Assignment to const");
2836 /* cc65 does not have full support for handling structs by value. Since
2837 * assigning structs is one of the more useful operations from this
2838 * familiy, allow it here.
2840 if (IsClassStruct (ltype)) {
2842 /* Bring the address of the lhs into the primary and push it */
2843 exprhs (0, 0, lval);
2844 g_push (CF_PTR | CF_UNSIGNED, 0);
2846 /* Get the expression on the right of the '=' into the primary */
2849 /* Get the address */
2850 exprhs (0, 0, &lval2);
2852 /* We need an lvalue */
2853 Error ("Invalid lvalue in assignment");
2856 /* Push the address (or whatever is in ax in case of errors) */
2857 g_push (CF_PTR | CF_UNSIGNED, 0);
2859 /* Check for equality of the structs */
2860 if (TypeCmp (ltype, lval2.e_tptr) < TC_EQUAL) {
2861 Error ("Incompatible types");
2864 /* Load the size of the struct into the primary */
2865 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, SizeOf (ltype), 0);
2867 /* Call the memcpy function */
2868 g_call (CF_FIXARGC, "memcpy", 4);
2872 /* Get the address on stack if needed */
2875 /* No struct, setup flags for the load */
2876 flags = SizeOf (ltype) == 1? CF_FORCECHAR : CF_NONE;
2878 /* Get the expression on the right of the '=' into the primary */
2879 if (evalexpr (flags, hie1, &lval2) == 0) {
2880 /* Constant expression. Adjust the types */
2881 assignadjust (ltype, &lval2);
2882 /* Put the value into the primary register */
2883 lconst (flags, &lval2);
2885 /* Expression is not constant and already in the primary */
2886 assignadjust (ltype, &lval2);
2889 /* Generate a store instruction */
2894 /* Value is still in primary */
2895 lval->e_flags = E_MEXPR;
2900 int hie1 (struct expent* lval)
2901 /* Parse first level of expression hierarchy. */
2905 k = hieQuest (lval);
2915 Error ("Invalid lvalue in assignment");
2921 case TOK_PLUS_ASSIGN:
2922 addsubeq (&GenPASGN, lval, k);
2925 case TOK_MINUS_ASSIGN:
2926 addsubeq (&GenSASGN, lval, k);
2929 case TOK_MUL_ASSIGN:
2930 opeq (&GenMASGN, lval, k);
2933 case TOK_DIV_ASSIGN:
2934 opeq (&GenDASGN, lval, k);
2937 case TOK_MOD_ASSIGN:
2938 opeq (&GenMOASGN, lval, k);
2941 case TOK_SHL_ASSIGN:
2942 opeq (&GenSLASGN, lval, k);
2945 case TOK_SHR_ASSIGN:
2946 opeq (&GenSRASGN, lval, k);
2949 case TOK_AND_ASSIGN:
2950 opeq (&GenAASGN, lval, k);
2953 case TOK_XOR_ASSIGN:
2954 opeq (&GenXOASGN, lval, k);
2958 opeq (&GenOASGN, lval, k);
2969 int hie0 (struct expent *lval)
2970 /* Parse comma operator. */
2975 while (curtok == TOK_COMMA) {
2984 int evalexpr (unsigned flags, int (*f) (struct expent*), struct expent* lval)
2985 /* Will evaluate an expression via the given function. If the result is a
2986 * constant, 0 is returned and the value is put in the lval struct. If the
2987 * result is not constant, exprhs is called to bring the value into the
2988 * primary register and 1 is returned.
2995 if (k == 0 && lval->e_flags == E_MCONST) {
2996 /* Constant expression */
2999 /* Not constant, load into the primary */
3000 exprhs (flags, k, lval);
3007 int expr (int (*func) (struct expent*), struct expent *lval)
3008 /* Expression parser; func is either hie0 or hie1. */
3017 /* Do some checks if code generation is still constistent */
3018 if (savsp != oursp) {
3020 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
3022 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
3030 void expression1 (struct expent* lval)
3031 /* Evaluate an expression on level 1 (no comma operator) and put it into
3032 * the primary register
3035 memset (lval, 0, sizeof (*lval));
3036 exprhs (CF_NONE, expr (hie1, lval), lval);
3041 void expression (struct expent* lval)
3042 /* Evaluate an expression and put it into the primary register */
3044 memset (lval, 0, sizeof (*lval));
3045 exprhs (CF_NONE, expr (hie0, lval), lval);
3050 void constexpr (struct expent* lval)
3051 /* Get a constant value */
3053 memset (lval, 0, sizeof (*lval));
3054 if (expr (hie1, lval) != 0 || (lval->e_flags & E_MCONST) == 0) {
3055 Error ("Constant expression expected");
3056 /* To avoid any compiler errors, make the expression a valid const */
3057 lval->e_flags = E_MCONST;
3058 lval->e_tptr = type_int;
3065 void intexpr (struct expent* lval)
3066 /* Get an integer expression */
3069 if (!IsClassInt (lval->e_tptr)) {
3070 Error ("Integer expression expected");
3071 /* To avoid any compiler errors, make the expression a valid int */
3072 lval->e_flags = E_MCONST;
3073 lval->e_tptr = type_int;
3080 void boolexpr (struct expent* lval)
3081 /* Get a boolean expression */
3083 /* Read an expression */
3086 /* If it's an integer, it's ok. If it's not an integer, but a pointer,
3087 * the pointer used in a boolean context is also ok
3089 if (!IsClassInt (lval->e_tptr) && !IsClassPtr (lval->e_tptr)) {
3090 Error ("Boolean expression expected");
3091 /* To avoid any compiler errors, make the expression a valid int */
3092 lval->e_flags = E_MCONST;
3093 lval->e_tptr = type_int;
3100 void test (unsigned label, int cond)
3101 /* Generate code to perform test and jump if false. */
3106 /* Eat the parenthesis */
3109 /* Prepare the expression, setup labels */
3110 memset (&lval, 0, sizeof (lval));
3112 /* Generate code to eval the expr */
3113 k = expr (hie0, &lval);
3114 if (k == 0 && lval.e_flags == E_MCONST) {
3115 /* Constant rvalue */
3116 if (cond == 0 && lval.e_const == 0) {
3118 Warning ("Unreachable code");
3119 } else if (cond && lval.e_const) {
3126 /* If the expr hasn't set condition codes, set the force-test flag */
3127 if ((lval.e_test & E_CC) == 0) {
3128 lval.e_test |= E_FORCETEST;
3131 /* Load the value into the primary register */
3132 exprhs (CF_FORCECHAR, k, &lval);
3134 /* Check for the closing brace */
3137 /* Generate the jump */
3139 g_truejump (CF_NONE, label);
3141 /* Special case (putting this here is a small hack - but hey, the
3142 * compiler itself is one big hack...): If a semicolon follows, we
3143 * don't have a statement and may omit the jump.
3145 if (curtok != TOK_SEMI) {
3146 g_falsejump (CF_NONE, label);