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");
758 /* Write the string directly into the output, followed by a newline */
759 AddCodeLine (GetLiteral (curval));
761 /* Reset the string pointer, effectivly clearing the string from the
762 * string table. Since we're working with one token lookahead, this
763 * will fail if the next token is also a string token, but that's a
764 * syntax error anyway, because we expect a right paren.
766 ResetLiteralPoolOffs (curval);
769 /* Skip the string token */
772 /* Closing paren needed */
778 static int primary (struct expent* lval)
779 /* This is the lowest level of the expression parser. */
783 /* not a test at all, yet */
786 /* Character and integer constants. */
787 if (curtok == TOK_ICONST || curtok == TOK_CCONST) {
788 lval->e_flags = E_MCONST | E_TCONST;
789 lval->e_tptr = curtype;
790 lval->e_const = curval;
795 /* Process parenthesized subexpression by calling the whole parser
798 if (curtok == TOK_LPAREN) {
800 memset (lval, 0, sizeof (*lval)); /* Remove any attributes */
806 /* All others may only be used if the expression evaluation is not called
807 * recursively by the preprocessor.
810 /* Illegal expression in PP mode */
811 Error ("Preprocessor expression expected");
812 lval->e_flags = E_MCONST;
813 lval->e_tptr = type_int;
818 if (curtok == TOK_IDENT) {
823 /* Get a pointer to the symbol table entry */
824 Sym = FindSym (CurTok.Ident);
826 /* Is the symbol known? */
829 /* We found the symbol - skip the name token */
832 /* The expression type is the symbol type */
833 lval->e_tptr = Sym->Type;
835 /* Check for illegal symbol types */
836 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
837 if (Sym->Flags & SC_TYPE) {
838 /* Cannot use type symbols */
839 Error ("Variable identifier expected");
840 /* Assume an int type to make lval valid */
841 lval->e_flags = E_MLOCAL | E_TLOFFS;
842 lval->e_tptr = type_int;
847 /* Check for legal symbol types */
848 if ((Sym->Flags & SC_CONST) == SC_CONST) {
849 /* Enum or some other numeric constant */
850 lval->e_flags = E_MCONST;
851 lval->e_const = Sym->V.ConstVal;
853 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
855 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
856 lval->e_name = (unsigned long) Sym->Name;
858 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
859 /* Local variable. If this is a parameter for a variadic
860 * function, we have to add some address calculations, and the
861 * address is not const.
863 if ((Sym->Flags & SC_PARAM) == SC_PARAM && IsVariadic (CurrentFunc)) {
864 /* Variadic parameter */
865 g_leavariadic (Sym->V.Offs - GetParamSize (CurrentFunc));
866 lval->e_flags = E_MEXPR;
869 /* Normal parameter */
870 lval->e_flags = E_MLOCAL | E_TLOFFS;
871 lval->e_const = Sym->V.Offs;
873 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
874 /* Static variable */
875 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
876 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
877 lval->e_name = (unsigned long) Sym->Name;
879 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
880 lval->e_name = Sym->V.Label;
883 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
884 /* Register variable, zero page based */
885 lval->e_flags = E_MGLOBAL | E_MCONST | E_TREGISTER;
886 lval->e_name = Sym->V.Offs;
889 /* Local static variable */
890 lval->e_flags = E_MGLOBAL | E_MCONST | E_TLLAB;
891 lval->e_name = Sym->V.Offs;
895 /* The symbol is referenced now */
896 Sym->Flags |= SC_REF;
897 if (IsTypeFunc (lval->e_tptr) || IsTypeArray (lval->e_tptr)) {
903 /* We did not find the symbol. Remember the name, then skip it */
904 strcpy (Ident, CurTok.Ident);
907 /* IDENT is either an auto-declared function or an undefined variable. */
908 if (curtok == TOK_LPAREN) {
909 /* Declare a function returning int. For that purpose, prepare a
910 * function signature for a function having an empty param list
913 Warning ("Function call without a prototype");
914 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
915 lval->e_tptr = Sym->Type;
916 lval->e_flags = E_MGLOBAL | E_MCONST | E_TGLAB;
917 lval->e_name = (unsigned long) Sym->Name;
923 /* Undeclared Variable */
924 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
925 lval->e_flags = E_MLOCAL | E_TLOFFS;
926 lval->e_tptr = type_int;
928 Error ("Undefined symbol: `%s'", Ident);
934 /* String literal? */
935 if (curtok == TOK_SCONST) {
936 lval->e_flags = E_MCONST | E_TLIT;
937 lval->e_const = curval;
938 lval->e_tptr = GetCharArrayType (strlen (GetLiteral (curval)));
944 if (curtok == TOK_ASM) {
946 lval->e_tptr = type_void;
947 lval->e_flags = E_MEXPR;
952 /* __AX__ and __EAX__ pseudo values? */
953 if (curtok == TOK_AX || curtok == TOK_EAX) {
954 lval->e_tptr = (curtok == TOK_AX)? type_uint : type_ulong;
955 lval->e_flags = E_MREG;
956 lval->e_test &= ~E_CC;
959 return 1; /* May be used as lvalue */
962 /* Illegal primary. */
963 Error ("Expression expected");
964 lval->e_flags = E_MCONST;
965 lval->e_tptr = type_int;
971 static int arrayref (int k, struct expent* lval)
972 /* Handle an array reference */
986 /* Skip the bracket */
989 /* Get the type of left side */
990 tptr1 = lval->e_tptr;
992 /* We can apply a special treatment for arrays that have a const base
993 * address. This is true for most arrays and will produce a lot better
994 * code. Check if this is a const base address.
996 lflags = lval->e_flags & ~E_MCTYPE;
997 ConstBaseAddr = (lflags == E_MCONST) || /* Constant numeric address */
998 (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
999 lflags == E_MLOCAL; /* Local array */
1001 /* If we have a constant base, we delay the address fetch */
1002 Mark1 = GetCodePos ();
1003 Mark2 = 0; /* Silence gcc */
1004 if (!ConstBaseAddr) {
1005 /* Get a pointer to the array into the primary */
1006 exprhs (CF_NONE, k, lval);
1008 /* Get the array pointer on stack. Do not push more than 16
1009 * bit, even if this value is greater, since we cannot handle
1010 * other than 16bit stuff when doing indexing.
1012 Mark2 = GetCodePos ();
1016 /* TOS now contains ptr to array elements. Get the subscript. */
1018 if (l == 0 && lval2.e_flags == E_MCONST) {
1020 /* The array subscript is a constant - remove value from stack */
1021 if (!ConstBaseAddr) {
1025 /* Get an array pointer into the primary */
1026 exprhs (CF_NONE, k, lval);
1029 if (IsClassPtr (tptr1)) {
1031 /* Scale the subscript value according to element size */
1032 lval2.e_const *= PSizeOf (tptr1);
1034 /* Remove code for lhs load */
1037 /* Handle constant base array on stack. Be sure NOT to
1038 * handle pointers the same way, this won't work.
1040 if (IsTypeArray (tptr1) &&
1041 ((lval->e_flags & ~E_MCTYPE) == E_MCONST ||
1042 (lval->e_flags & ~E_MCTYPE) == E_MLOCAL ||
1043 (lval->e_flags & E_MGLOBAL) != 0 ||
1044 (lval->e_flags == E_MEOFFS))) {
1045 lval->e_const += lval2.e_const;
1048 /* Pointer - load into primary and remember offset */
1049 if ((lval->e_flags & E_MEXPR) == 0 || k != 0) {
1050 exprhs (CF_NONE, k, lval);
1052 lval->e_const = lval2.e_const;
1053 lval->e_flags = E_MEOFFS;
1056 /* Result is of element type */
1057 lval->e_tptr = Indirect (tptr1);
1062 } else if (IsClassPtr (tptr2 = lval2.e_tptr)) {
1063 /* Subscript is pointer, get element type */
1064 lval2.e_tptr = Indirect (tptr2);
1066 /* Scale the rhs value in the primary register */
1067 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
1069 lval->e_tptr = lval2.e_tptr;
1071 Error ("Cannot subscript");
1074 /* Add the subscript. Since arrays are indexed by integers,
1075 * we will ignore the true type of the subscript here and
1076 * use always an int.
1078 g_inc (CF_INT | CF_CONST, lval2.e_const);
1082 /* Array subscript is not constant. Load it into the primary */
1083 Mark2 = GetCodePos ();
1084 exprhs (CF_NONE, l, &lval2);
1086 tptr2 = lval2.e_tptr;
1087 if (IsClassPtr (tptr1)) {
1089 /* Get the element type */
1090 lval->e_tptr = Indirect (tptr1);
1092 /* Indexing is based on int's, so we will just use the integer
1093 * portion of the index (which is in (e)ax, so there's no further
1096 g_scale (CF_INT, SizeOf (lval->e_tptr));
1098 } else if (IsClassPtr (tptr2)) {
1100 /* Get the element type */
1101 lval2.e_tptr = Indirect (tptr2);
1103 /* Get the int value on top. If we go here, we're sure,
1104 * both values are 16 bit (the first one was truncated
1105 * if necessary and the second one is a pointer).
1106 * Note: If ConstBaseAddr is true, we don't have a value on
1107 * stack, so to "swap" both, just push the subscript.
1109 if (ConstBaseAddr) {
1111 exprhs (CF_NONE, k, lval);
1118 g_scale (TypeOf (tptr1), SizeOf (lval2.e_tptr));
1119 lval->e_tptr = lval2.e_tptr;
1121 Error ("Cannot subscript");
1124 /* The offset is now in the primary register. It didn't have a
1125 * constant base address for the lhs, the lhs address is already
1126 * on stack, and we must add the offset. If the base address was
1127 * constant, we call special functions to add the address to the
1130 if (!ConstBaseAddr) {
1131 /* Add the subscript. Both values are int sized. */
1135 /* If the subscript has itself a constant address, it is often
1136 * a better idea to reverse again the order of the evaluation.
1137 * This will generate better code if the subscript is a byte
1138 * sized variable. But beware: This is only possible if the
1139 * subscript was not scaled, that is, if this was a byte array
1142 rflags = lval2.e_flags & ~E_MCTYPE;
1143 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1144 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1145 rflags == E_MLOCAL; /* Local array */
1147 if (ConstSubAddr && SizeOf (lval->e_tptr) == 1) {
1151 /* Reverse the order of evaluation */
1152 unsigned flags = (SizeOf (lval2.e_tptr) == 1)? CF_CHAR : CF_INT;
1155 /* Get a pointer to the array into the primary. We have changed
1156 * e_tptr above but we need the original type to load the
1157 * address, so restore it temporarily.
1159 SavedType = lval->e_tptr;
1160 lval->e_tptr = tptr1;
1161 exprhs (CF_NONE, k, lval);
1162 lval->e_tptr = SavedType;
1164 /* Add the variable */
1165 if (rflags == E_MLOCAL) {
1166 g_addlocal (flags, lval2.e_const);
1168 flags |= GlobalModeFlags (lval2.e_flags);
1169 g_addstatic (flags, lval2.e_name, lval2.e_const);
1172 if (lflags == E_MCONST) {
1173 /* Constant numeric address. Just add it */
1174 g_inc (CF_INT | CF_UNSIGNED, lval->e_const);
1175 } else if (lflags == E_MLOCAL) {
1176 /* Base address is a local variable address */
1177 if (IsTypeArray (tptr1)) {
1178 g_addaddr_local (CF_INT, lval->e_const);
1180 g_addlocal (CF_PTR, lval->e_const);
1183 /* Base address is a static variable address */
1184 unsigned flags = CF_INT;
1185 flags |= GlobalModeFlags (lval->e_flags);
1186 if (IsTypeArray (tptr1)) {
1187 g_addaddr_static (flags, lval->e_name, lval->e_const);
1189 g_addstatic (flags, lval->e_name, lval->e_const);
1195 lval->e_flags = E_MEXPR;
1198 return !IsTypeArray (lval->e_tptr);
1204 static int structref (int k, struct expent* lval)
1205 /* Process struct field after . or ->. */
1211 /* Skip the token and check for an identifier */
1213 if (curtok != TOK_IDENT) {
1214 Error ("Identifier expected");
1215 lval->e_tptr = type_int;
1219 /* Get the symbol table entry and check for a struct field */
1220 strcpy (Ident, CurTok.Ident);
1222 Field = FindStructField (lval->e_tptr, Ident);
1224 Error ("Struct/union has no field named `%s'", Ident);
1225 lval->e_tptr = type_int;
1229 /* If we have constant input data, the result is also constant */
1230 flags = lval->e_flags & ~E_MCTYPE;
1231 if (flags == E_MCONST ||
1232 (k == 0 && (flags == E_MLOCAL ||
1233 (flags & E_MGLOBAL) != 0 ||
1234 lval->e_flags == E_MEOFFS))) {
1235 lval->e_const += Field->V.Offs;
1237 if ((flags & E_MEXPR) == 0 || k != 0) {
1238 exprhs (CF_NONE, k, lval);
1240 lval->e_const = Field->V.Offs;
1241 lval->e_flags = E_MEOFFS;
1243 lval->e_tptr = Field->Type;
1244 return !IsTypeArray (Field->Type);
1249 static int hie11 (struct expent *lval)
1250 /* Handle compound types (structs and arrays) */
1257 if (curtok < TOK_LBRACK || curtok > TOK_PTR_REF) {
1264 if (curtok == TOK_LBRACK) {
1266 /* Array reference */
1267 k = arrayref (k, lval);
1269 } else if (curtok == TOK_LPAREN) {
1271 /* Function call. Skip the opening parenthesis */
1273 tptr = lval->e_tptr;
1274 if (IsTypeFunc (tptr) || IsTypeFuncPtr (tptr)) {
1275 if (IsTypeFuncPtr (tptr)) {
1276 /* Pointer to function. Handle transparently */
1277 exprhs (CF_NONE, k, lval); /* Function pointer to A/X */
1278 ++lval->e_tptr; /* Skip T_PTR */
1279 lval->e_flags |= E_MEXPR;
1281 CallFunction (lval);
1282 lval->e_flags = E_MEXPR;
1283 lval->e_tptr += DECODE_SIZE + 1; /* Set to result */
1285 Error ("Illegal function call");
1289 } else if (curtok == TOK_DOT) {
1291 if (!IsClassStruct (lval->e_tptr)) {
1292 Error ("Struct expected");
1294 k = structref (0, lval);
1296 } else if (curtok == TOK_PTR_REF) {
1298 tptr = lval->e_tptr;
1299 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1300 Error ("Struct pointer expected");
1302 k = structref (k, lval);
1312 static void store (struct expent* lval)
1313 /* Store primary reg into this reference */
1319 flags = TypeOf (lval->e_tptr);
1320 if (f & E_MGLOBAL) {
1321 flags |= GlobalModeFlags (f);
1328 g_putstatic (flags, lval->e_name, lval->e_const);
1330 } else if (f & E_MLOCAL) {
1331 g_putlocal (flags, lval->e_const, 0);
1332 } else if (f == E_MEOFFS) {
1333 g_putind (flags, lval->e_const);
1334 } else if (f != E_MREG) {
1336 g_putind (flags, 0);
1338 /* Store into absolute address */
1339 g_putstatic (flags | CF_ABSOLUTE, lval->e_const, 0);
1343 /* Assume that each one of the stores will invalidate CC */
1344 lval->e_test &= ~E_CC;
1349 static void pre_incdec (struct expent* lval, void (*inc) (unsigned, unsigned long))
1350 /* Handle --i and ++i */
1357 if ((k = hie10 (lval)) == 0) {
1358 Error ("Invalid lvalue");
1362 /* Get the data type */
1363 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR | CF_CONST;
1365 /* Get the increment value in bytes */
1366 val = (lval->e_tptr [0] == T_PTR)? PSizeOf (lval->e_tptr) : 1;
1368 /* We're currently only able to handle some adressing modes */
1369 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
1370 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
1371 (lval->e_flags & E_MCONST) == 0 && /* Constant address? */
1372 (lval->e_flags & E_MEXPR) == 0) { /* Address in a/x? */
1374 /* Use generic code. Push the address if needed */
1377 /* Fetch the value */
1378 exprhs (CF_NONE, k, lval);
1380 /* Increment value in primary */
1383 /* Store the result back */
1388 /* Special code for some addressing modes - use the special += ops */
1389 if (lval->e_flags & E_MGLOBAL) {
1390 flags |= GlobalModeFlags (lval->e_flags);
1392 g_addeqstatic (flags, lval->e_name, lval->e_const, val);
1394 g_subeqstatic (flags, lval->e_name, lval->e_const, val);
1396 } else if (lval->e_flags & E_MLOCAL) {
1397 /* ref to localvar */
1399 g_addeqlocal (flags, lval->e_const, val);
1401 g_subeqlocal (flags, lval->e_const, val);
1403 } else if (lval->e_flags & E_MCONST) {
1404 /* ref to absolute address */
1405 flags |= CF_ABSOLUTE;
1407 g_addeqstatic (flags, lval->e_const, 0, val);
1409 g_subeqstatic (flags, lval->e_const, 0, val);
1411 } else if (lval->e_flags & E_MEXPR) {
1412 /* Address in a/x, check if we have an offset */
1413 unsigned Offs = (lval->e_flags == E_MEOFFS)? lval->e_const : 0;
1415 g_addeqind (flags, Offs, val);
1417 g_subeqind (flags, Offs, val);
1420 Internal ("Invalid addressing mode");
1425 /* Result is an expression */
1426 lval->e_flags = E_MEXPR;
1431 static void post_incdec (struct expent *lval, int k, void (*inc) (unsigned, unsigned long))
1432 /* Handle i-- and i++ */
1438 Error ("Invalid lvalue");
1442 /* Get the data type */
1443 flags = TypeOf (lval->e_tptr);
1445 /* Push the address if needed */
1448 /* Fetch the value and save it (since it's the result of the expression) */
1449 exprhs (CF_NONE, 1, lval);
1450 g_save (flags | CF_FORCECHAR);
1452 /* If we have a pointer expression, increment by the size of the type */
1453 if (lval->e_tptr[0] == T_PTR) {
1454 inc (flags | CF_CONST | CF_FORCECHAR, SizeOf (lval->e_tptr + 1));
1456 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1459 /* Store the result back */
1462 /* Restore the original value */
1463 g_restore (flags | CF_FORCECHAR);
1464 lval->e_flags = E_MEXPR;
1469 static void unaryop (int tok, struct expent* lval)
1470 /* Handle unary -/+ and ~ */
1477 if (k == 0 && lval->e_flags & E_MCONST) {
1478 /* Value is constant */
1480 case TOK_MINUS: lval->e_const = -lval->e_const; break;
1481 case TOK_PLUS: break;
1482 case TOK_COMP: lval->e_const = ~lval->e_const; break;
1483 default: Internal ("Unexpected token: %d", tok);
1486 /* Value is not constant */
1487 exprhs (CF_NONE, k, lval);
1489 /* Get the type of the expression */
1490 flags = TypeOf (lval->e_tptr);
1492 /* Handle the operation */
1494 case TOK_MINUS: g_neg (flags); break;
1495 case TOK_PLUS: break;
1496 case TOK_COMP: g_com (flags); break;
1497 default: Internal ("Unexpected token: %d", tok);
1499 lval->e_flags = E_MEXPR;
1505 static int typecast (struct expent* lval)
1506 /* Handle an explicit cast */
1509 type Type[MAXTYPELEN];
1511 /* Skip the left paren */
1520 /* Read the expression we have to cast */
1523 /* If the expression is a function, treat it as pointer-to-function */
1524 if (IsTypeFunc (lval->e_tptr)) {
1525 lval->e_tptr = PointerTo (lval->e_tptr);
1528 /* Check for a constant on the right side */
1529 if (k == 0 && lval->e_flags == E_MCONST) {
1531 /* A cast of a constant to something else. If the new type is an int,
1532 * be sure to handle the size extension correctly. If the new type is
1533 * not an int, the cast is implementation specific anyway, so leave
1536 if (IsClassInt (Type)) {
1538 /* Get the current and new size of the value */
1539 unsigned OldSize = SizeOf (lval->e_tptr);
1540 unsigned NewSize = SizeOf (Type);
1541 unsigned OldBits = OldSize * 8;
1542 unsigned NewBits = NewSize * 8;
1544 /* Check if the new datatype will have a smaller range */
1545 if (NewSize < OldSize) {
1547 /* Cut the value to the new size */
1548 lval->e_const &= (0xFFFFFFFFUL >> (32 - NewBits));
1550 /* If the new value is signed, sign extend the value */
1551 if (!IsSignUnsigned (Type)) {
1552 lval->e_const |= ((~0L) << NewBits);
1555 } else if (NewSize > OldSize) {
1557 /* Sign extend the value if needed */
1558 if (!IsSignUnsigned (Type) && !IsSignUnsigned (lval->e_tptr)) {
1559 if (lval->e_const & (0x01UL << (OldBits-1))) {
1560 lval->e_const |= ((~0L) << OldBits);
1568 /* Not a constant. Be sure to ignore casts to void */
1569 if (!IsTypeVoid (Type)) {
1571 /* If the size does not change, leave the value alone. Otherwise,
1572 * we have to load the value into the primary and generate code to
1573 * cast the value in the primary register.
1575 if (SizeOf (Type) != SizeOf (lval->e_tptr)) {
1577 /* Load the value into the primary */
1578 exprhs (CF_NONE, k, lval);
1580 /* Mark the lhs as const to avoid a manipulation of TOS */
1581 g_typecast (TypeOf (Type) | CF_CONST, TypeOf (lval->e_tptr));
1583 /* Value is now in primary */
1584 lval->e_flags = E_MEXPR;
1590 /* In any case, use the new type */
1591 lval->e_tptr = TypeDup (Type);
1599 static int hie10 (struct expent* lval)
1600 /* Handle ++, --, !, unary - etc. */
1608 pre_incdec (lval, g_inc);
1612 pre_incdec (lval, g_dec);
1618 unaryop (curtok, lval);
1623 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1624 /* Constant expression */
1625 lval->e_const = !lval->e_const;
1627 g_bneg (TypeOf (lval->e_tptr));
1628 lval->e_test |= E_CC; /* bneg will set cc */
1629 lval->e_flags = E_MEXPR; /* say it's an expr */
1631 return 0; /* expr not storable */
1635 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1636 /* Expression is not const, indirect value loaded into primary */
1637 lval->e_flags = E_MEXPR;
1638 lval->e_const = 0; /* Offset is zero now */
1641 if (IsClassPtr (t)) {
1642 lval->e_tptr = Indirect (t);
1644 Error ("Illegal indirection");
1651 /* The & operator may be applied to any lvalue, and it may be
1652 * applied to functions, even if they're no lvalues.
1654 if (k == 0 && !IsTypeFunc (lval->e_tptr)) {
1655 /* Allow the & operator with an array */
1656 if (!IsTypeArray (lval->e_tptr)) {
1657 Error ("Illegal address");
1660 t = TypeAlloc (TypeLen (lval->e_tptr) + 2);
1662 TypeCpy (t + 1, lval->e_tptr);
1669 if (istypeexpr ()) {
1670 type Type[MAXTYPELEN];
1672 lval->e_const = SizeOf (ParseType (Type));
1675 /* Remember the output queue pointer */
1676 CodeMark Mark = GetCodePos ();
1678 lval->e_const = SizeOf (lval->e_tptr);
1679 /* Remove any generated code */
1682 lval->e_flags = E_MCONST | E_TCONST;
1683 lval->e_tptr = type_uint;
1684 lval->e_test &= ~E_CC;
1688 if (istypeexpr ()) {
1690 return typecast (lval);
1697 post_incdec (lval, k, g_inc);
1701 post_incdec (lval, k, g_dec);
1711 static int hie_internal (GenDesc** ops, /* List of generators */
1712 struct expent* lval, /* parent expr's lval */
1713 int (*hienext) (struct expent*),
1714 int* UsedGen) /* next higher level */
1715 /* Helper function */
1718 struct expent lval2;
1722 token_t tok; /* The operator token */
1723 unsigned ltype, type;
1724 int rconst; /* Operand is a constant */
1730 while ((Gen = FindGen (curtok, ops)) != 0) {
1732 /* Tell the caller that we handled it's ops */
1735 /* All operators that call this function expect an int on the lhs */
1736 if (!IsClassInt (lval->e_tptr)) {
1737 Error ("Integer expression expected");
1740 /* Remember the operator token, then skip it */
1744 /* Get the lhs on stack */
1745 Mark1 = GetCodePos ();
1746 ltype = TypeOf (lval->e_tptr);
1747 if (k == 0 && lval->e_flags == E_MCONST) {
1748 /* Constant value */
1749 Mark2 = GetCodePos ();
1750 g_push (ltype | CF_CONST, lval->e_const);
1752 /* Value not constant */
1753 exprhs (CF_NONE, k, lval);
1754 Mark2 = GetCodePos ();
1758 /* Get the right hand side */
1759 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1761 /* Check the type of the rhs */
1762 if (!IsClassInt (lval2.e_tptr)) {
1763 Error ("Integer expression expected");
1766 /* Check for const operands */
1767 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1769 /* Both operands are constant, remove the generated code */
1773 /* Evaluate the result */
1774 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1776 /* Get the type of the result */
1777 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1781 /* If the right hand side is constant, and the generator function
1782 * expects the lhs in the primary, remove the push of the primary
1785 unsigned rtype = TypeOf (lval2.e_tptr);
1788 /* Second value is constant - check for div */
1791 if (tok == TOK_DIV && lval2.e_const == 0) {
1792 Error ("Division by zero");
1793 } else if (tok == TOK_MOD && lval2.e_const == 0) {
1794 Error ("Modulo operation with zero");
1796 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1799 ltype |= CF_REG; /* Value is in register */
1803 /* Determine the type of the operation result. */
1804 type |= g_typeadjust (ltype, rtype);
1805 lval->e_tptr = promoteint (lval->e_tptr, lval2.e_tptr);
1808 Gen->Func (type, lval2.e_const);
1809 lval->e_flags = E_MEXPR;
1812 /* We have a rvalue now */
1821 static int hie_compare (GenDesc** ops, /* List of generators */
1822 struct expent* lval, /* parent expr's lval */
1823 int (*hienext) (struct expent*))
1824 /* Helper function for the compare operators */
1827 struct expent lval2;
1831 token_t tok; /* The operator token */
1833 int rconst; /* Operand is a constant */
1838 while ((Gen = FindGen (curtok, ops)) != 0) {
1840 /* Remember the operator token, then skip it */
1844 /* Get the lhs on stack */
1845 Mark1 = GetCodePos ();
1846 ltype = TypeOf (lval->e_tptr);
1847 if (k == 0 && lval->e_flags == E_MCONST) {
1848 /* Constant value */
1849 Mark2 = GetCodePos ();
1850 g_push (ltype | CF_CONST, lval->e_const);
1852 /* Value not constant */
1853 exprhs (CF_NONE, k, lval);
1854 Mark2 = GetCodePos ();
1858 /* Get the right hand side */
1859 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1861 /* Make sure, the types are compatible */
1862 if (IsClassInt (lval->e_tptr)) {
1863 if (!IsClassInt (lval2.e_tptr) && !(IsClassPtr(lval2.e_tptr) && IsNullPtr(lval))) {
1864 Error ("Incompatible types");
1866 } else if (IsClassPtr (lval->e_tptr)) {
1867 if (IsClassPtr (lval2.e_tptr)) {
1868 /* Both pointers are allowed in comparison if they point to
1869 * the same type, or if one of them is a void pointer.
1871 type* left = Indirect (lval->e_tptr);
1872 type* right = Indirect (lval2.e_tptr);
1873 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1874 /* Incomatible pointers */
1875 Error ("Incompatible types");
1877 } else if (!IsNullPtr (&lval2)) {
1878 Error ("Incompatible types");
1882 /* Check for const operands */
1883 if (k == 0 && lval->e_flags == E_MCONST && rconst) {
1885 /* Both operands are constant, remove the generated code */
1889 /* Evaluate the result */
1890 lval->e_const = kcalc (tok, lval->e_const, lval2.e_const);
1894 /* If the right hand side is constant, and the generator function
1895 * expects the lhs in the primary, remove the push of the primary
1901 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1904 ltype |= CF_REG; /* Value is in register */
1908 /* Determine the type of the operation result. If the left
1909 * operand is of type char and the right is a constant, or
1910 * if both operands are of type char, we will encode the
1911 * operation as char operation. Otherwise the default
1912 * promotions are used.
1914 if (IsTypeChar (lval->e_tptr) && (IsTypeChar (lval2.e_tptr) || rconst)) {
1916 if (IsSignUnsigned (lval->e_tptr) || IsSignUnsigned (lval2.e_tptr)) {
1917 flags |= CF_UNSIGNED;
1920 flags |= CF_FORCECHAR;
1923 unsigned rtype = TypeOf (lval2.e_tptr) | (flags & CF_CONST);
1924 flags |= g_typeadjust (ltype, rtype);
1928 Gen->Func (flags, lval2.e_const);
1929 lval->e_flags = E_MEXPR;
1932 /* Result type is always int */
1933 lval->e_tptr = type_int;
1935 /* We have a rvalue now, condition codes are set */
1937 lval->e_test |= E_CC;
1945 static int hie9 (struct expent *lval)
1946 /* Process * and / operators. */
1948 static GenDesc* hie9_ops [] = {
1949 &GenMUL, &GenDIV, &GenMOD, 0
1953 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1958 static void parseadd (int k, struct expent* lval)
1959 /* Parse an expression with the binary plus operator. lval contains the
1960 * unprocessed left hand side of the expression and will contain the
1961 * result of the expression on return.
1964 struct expent lval2;
1965 unsigned flags; /* Operation flags */
1966 CodeMark Mark; /* Remember code position */
1967 type* lhst; /* Type of left hand side */
1968 type* rhst; /* Type of right hand side */
1971 /* Skip the PLUS token */
1974 /* Get the left hand side type, initialize operation flags */
1975 lhst = lval->e_tptr;
1978 /* Check for constness on both sides */
1979 if (k == 0 && lval->e_flags == E_MCONST) {
1981 /* The left hand side is a constant. Good. Get rhs */
1982 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1984 /* Right hand side is also constant. Get the rhs type */
1985 rhst = lval2.e_tptr;
1987 /* Both expressions are constants. Check for pointer arithmetic */
1988 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1989 /* Left is pointer, right is int, must scale rhs */
1990 lval->e_const = lval->e_const + lval2.e_const * PSizeOf (lhst);
1991 /* Result type is a pointer */
1992 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1993 /* Left is int, right is pointer, must scale lhs */
1994 lval->e_const = lval->e_const * PSizeOf (rhst) + lval2.e_const;
1995 /* Result type is a pointer */
1996 lval->e_tptr = lval2.e_tptr;
1997 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1998 /* Integer addition */
1999 lval->e_const += lval2.e_const;
2000 typeadjust (lval, &lval2, 1);
2003 Error ("Invalid operands for binary operator `+'");
2006 /* Result is constant, condition codes not set */
2007 lval->e_test = E_MCONST;
2011 /* lhs is constant, rhs is not. Get the rhs type. */
2012 rhst = lval2.e_tptr;
2014 /* Check for pointer arithmetic */
2015 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2016 /* Left is pointer, right is int, must scale rhs */
2017 g_scale (CF_INT, PSizeOf (lhst));
2018 /* Operate on pointers, result type is a pointer */
2020 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2021 /* Left is int, right is pointer, must scale lhs */
2022 lval->e_const *= PSizeOf (rhst);
2023 /* Operate on pointers, result type is a pointer */
2025 lval->e_tptr = lval2.e_tptr;
2026 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2027 /* Integer addition */
2028 flags = typeadjust (lval, &lval2, 1);
2031 Error ("Invalid operands for binary operator `+'");
2034 /* Generate code for the add */
2035 g_inc (flags | CF_CONST, lval->e_const);
2037 /* Result is in primary register */
2038 lval->e_flags = E_MEXPR;
2039 lval->e_test &= ~E_CC;
2045 /* Left hand side is not constant. Get the value onto the stack. */
2046 exprhs (CF_NONE, k, lval); /* --> primary register */
2047 Mark = GetCodePos ();
2048 g_push (TypeOf (lval->e_tptr), 0); /* --> stack */
2050 /* Evaluate the rhs */
2051 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2053 /* Right hand side is a constant. Get the rhs type */
2054 rhst = lval2.e_tptr;
2056 /* Remove pushed value from stack */
2058 pop (TypeOf (lval->e_tptr));
2060 /* Check for pointer arithmetic */
2061 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2062 /* Left is pointer, right is int, must scale rhs */
2063 lval2.e_const *= PSizeOf (lhst);
2064 /* Operate on pointers, result type is a pointer */
2066 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2067 /* Left is int, right is pointer, must scale lhs (ptr only) */
2068 g_scale (CF_INT | CF_CONST, PSizeOf (rhst));
2069 /* Operate on pointers, result type is a pointer */
2071 lval->e_tptr = lval2.e_tptr;
2072 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2073 /* Integer addition */
2074 flags = typeadjust (lval, &lval2, 1);
2077 Error ("Invalid operands for binary operator `+'");
2080 /* Generate code for the add */
2081 g_inc (flags | CF_CONST, lval2.e_const);
2083 /* Result is in primary register */
2084 lval->e_flags = E_MEXPR;
2085 lval->e_test &= ~E_CC;
2089 /* lhs and rhs are not constant. Get the rhs type. */
2090 rhst = lval2.e_tptr;
2092 /* Check for pointer arithmetic */
2093 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2094 /* Left is pointer, right is int, must scale rhs */
2095 g_scale (CF_INT, PSizeOf (lhst));
2096 /* Operate on pointers, result type is a pointer */
2098 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2099 /* Left is int, right is pointer, must scale lhs */
2100 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2101 g_swap (CF_INT); /* Swap TOS and primary */
2102 g_scale (CF_INT, PSizeOf (rhst));
2103 /* Operate on pointers, result type is a pointer */
2105 lval->e_tptr = lval2.e_tptr;
2106 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2107 /* Integer addition */
2108 flags = typeadjust (lval, &lval2, 0);
2111 Error ("Invalid operands for binary operator `+'");
2114 /* Generate code for the add */
2117 /* Result is in primary register */
2118 lval->e_flags = E_MEXPR;
2119 lval->e_test &= ~E_CC;
2128 static void parsesub (int k, struct expent* lval)
2129 /* Parse an expression with the binary minus operator. lval contains the
2130 * unprocessed left hand side of the expression and will contain the
2131 * result of the expression on return.
2134 struct expent lval2;
2135 unsigned flags; /* Operation flags */
2136 type* lhst; /* Type of left hand side */
2137 type* rhst; /* Type of right hand side */
2138 CodeMark Mark1; /* Save position of output queue */
2139 CodeMark Mark2; /* Another position in the queue */
2140 int rscale; /* Scale factor for the result */
2143 /* Skip the MINUS token */
2146 /* Get the left hand side type, initialize operation flags */
2147 lhst = lval->e_tptr;
2149 rscale = 1; /* Scale by 1, that is, don't scale */
2151 /* Remember the output queue position, then bring the value onto the stack */
2152 Mark1 = GetCodePos ();
2153 exprhs (CF_NONE, k, lval); /* --> primary register */
2154 Mark2 = GetCodePos ();
2155 g_push (TypeOf (lhst), 0); /* --> stack */
2157 /* Parse the right hand side */
2158 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2160 /* The right hand side is constant. Get the rhs type. */
2161 rhst = lval2.e_tptr;
2163 /* Check left hand side */
2164 if (k == 0 && lval->e_flags & E_MCONST) {
2166 /* Both sides are constant, remove generated code */
2168 pop (TypeOf (lhst)); /* Clean up the stack */
2170 /* Check for pointer arithmetic */
2171 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2172 /* Left is pointer, right is int, must scale rhs */
2173 lval->e_const -= lval2.e_const * PSizeOf (lhst);
2174 /* Operate on pointers, result type is a pointer */
2175 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2176 /* Left is pointer, right is pointer, must scale result */
2177 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2178 Error ("Incompatible pointer types");
2180 lval->e_const = (lval->e_const - lval2.e_const) / PSizeOf (lhst);
2182 /* Operate on pointers, result type is an integer */
2183 lval->e_tptr = type_int;
2184 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2185 /* Integer subtraction */
2186 typeadjust (lval, &lval2, 1);
2187 lval->e_const -= lval2.e_const;
2190 Error ("Invalid operands for binary operator `-'");
2193 /* Result is constant, condition codes not set */
2194 lval->e_flags = E_MCONST;
2195 lval->e_test &= ~E_CC;
2199 /* Left hand side is not constant, right hand side is.
2200 * Remove pushed value from stack.
2203 pop (TypeOf (lhst));
2205 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2206 /* Left is pointer, right is int, must scale rhs */
2207 lval2.e_const *= PSizeOf (lhst);
2208 /* Operate on pointers, result type is a pointer */
2210 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2211 /* Left is pointer, right is pointer, must scale result */
2212 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2213 Error ("Incompatible pointer types");
2215 rscale = PSizeOf (lhst);
2217 /* Operate on pointers, result type is an integer */
2219 lval->e_tptr = type_int;
2220 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2221 /* Integer subtraction */
2222 flags = typeadjust (lval, &lval2, 1);
2225 Error ("Invalid operands for binary operator `-'");
2228 /* Do the subtraction */
2229 g_dec (flags | CF_CONST, lval2.e_const);
2231 /* If this was a pointer subtraction, we must scale the result */
2233 g_scale (flags, -rscale);
2236 /* Result is in primary register */
2237 lval->e_flags = E_MEXPR;
2238 lval->e_test &= ~E_CC;
2244 /* Right hand side is not constant. Get the rhs type. */
2245 rhst = lval2.e_tptr;
2247 /* Check for pointer arithmetic */
2248 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2249 /* Left is pointer, right is int, must scale rhs */
2250 g_scale (CF_INT, PSizeOf (lhst));
2251 /* Operate on pointers, result type is a pointer */
2253 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2254 /* Left is pointer, right is pointer, must scale result */
2255 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2256 Error ("Incompatible pointer types");
2258 rscale = PSizeOf (lhst);
2260 /* Operate on pointers, result type is an integer */
2262 lval->e_tptr = type_int;
2263 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2264 /* Integer subtraction. If the left hand side descriptor says that
2265 * the lhs is const, we have to remove this mark, since this is no
2266 * longer true, lhs is on stack instead.
2268 if (lval->e_flags == E_MCONST) {
2269 lval->e_flags = E_MEXPR;
2271 /* Adjust operand types */
2272 flags = typeadjust (lval, &lval2, 0);
2275 Error ("Invalid operands for binary operator `-'");
2278 /* Generate code for the sub (the & is a hack here) */
2279 g_sub (flags & ~CF_CONST, 0);
2281 /* If this was a pointer subtraction, we must scale the result */
2283 g_scale (flags, -rscale);
2286 /* Result is in primary register */
2287 lval->e_flags = E_MEXPR;
2288 lval->e_test &= ~E_CC;
2294 static int hie8 (struct expent* lval)
2295 /* Process + and - binary operators. */
2297 int k = hie9 (lval);
2298 while (curtok == TOK_PLUS || curtok == TOK_MINUS) {
2300 if (curtok == TOK_PLUS) {
2313 static int hie7 (struct expent *lval)
2314 /* Parse << and >>. */
2316 static GenDesc* hie7_ops [] = {
2321 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2326 static int hie6 (struct expent *lval)
2327 /* process greater-than type comparators */
2329 static GenDesc* hie6_ops [] = {
2330 &GenLT, &GenLE, &GenGE, &GenGT, 0
2332 return hie_compare (hie6_ops, lval, hie7);
2337 static int hie5 (struct expent *lval)
2339 static GenDesc* hie5_ops[] = {
2342 return hie_compare (hie5_ops, lval, hie6);
2347 static int hie4 (struct expent* lval)
2348 /* Handle & (bitwise and) */
2350 static GenDesc* hie4_ops [] = {
2355 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2360 static int hie3 (struct expent *lval)
2361 /* Handle ^ (bitwise exclusive or) */
2363 static GenDesc* hie3_ops [] = {
2368 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2373 static int hie2 (struct expent *lval)
2374 /* Handle | (bitwise or) */
2376 static GenDesc* hie2_ops [] = {
2381 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2386 static int hieAnd (struct expent* lval, unsigned TrueLab, int* BoolOp)
2387 /* Process "exp && exp" */
2391 struct expent lval2;
2394 if (curtok == TOK_BOOL_AND) {
2396 /* Tell our caller that we're evaluating a boolean */
2399 /* Get a label that we will use for false expressions */
2402 /* If the expr hasn't set condition codes, set the force-test flag */
2403 if ((lval->e_test & E_CC) == 0) {
2404 lval->e_test |= E_FORCETEST;
2407 /* Load the value */
2408 exprhs (CF_FORCECHAR, k, lval);
2410 /* Generate the jump */
2411 g_falsejump (CF_NONE, lab);
2413 /* Parse more boolean and's */
2414 while (curtok == TOK_BOOL_AND) {
2421 if ((lval2.e_test & E_CC) == 0) {
2422 lval2.e_test |= E_FORCETEST;
2424 exprhs (CF_FORCECHAR, k, &lval2);
2426 /* Do short circuit evaluation */
2427 if (curtok == TOK_BOOL_AND) {
2428 g_falsejump (CF_NONE, lab);
2430 /* Last expression - will evaluate to true */
2431 g_truejump (CF_NONE, TrueLab);
2435 /* Define the false jump label here */
2436 g_defloclabel (lab);
2438 /* Define the label */
2439 lval->e_flags = E_MEXPR;
2440 lval->e_test |= E_CC; /* Condition codes are set */
2448 static int hieOr (struct expent *lval)
2449 /* Process "exp || exp". */
2452 struct expent lval2;
2453 int BoolOp = 0; /* Did we have a boolean op? */
2454 int AndOp; /* Did we have a && operation? */
2455 unsigned TrueLab; /* Jump to this label if true */
2459 TrueLab = GetLabel ();
2461 /* Call the next level parser */
2462 k = hieAnd (lval, TrueLab, &BoolOp);
2464 /* Any boolean or's? */
2465 if (curtok == TOK_BOOL_OR) {
2467 /* If the expr hasn't set condition codes, set the force-test flag */
2468 if ((lval->e_test & E_CC) == 0) {
2469 lval->e_test |= E_FORCETEST;
2472 /* Get first expr */
2473 exprhs (CF_FORCECHAR, k, lval);
2475 /* For each expression jump to TrueLab if true. Beware: If we
2476 * had && operators, the jump is already in place!
2479 g_truejump (CF_NONE, TrueLab);
2482 /* Remember that we had a boolean op */
2485 /* while there's more expr */
2486 while (curtok == TOK_BOOL_OR) {
2493 k = hieAnd (&lval2, TrueLab, &AndOp);
2494 if ((lval2.e_test & E_CC) == 0) {
2495 lval2.e_test |= E_FORCETEST;
2497 exprhs (CF_FORCECHAR, k, &lval2);
2499 /* If there is more to come, add shortcut boolean eval.
2500 * Beware: If we had && operators, the jump is already
2504 /* Seems this sometimes generates wrong code */
2505 if (curtok == TOK_BOOL_OR && !AndOp) {
2506 g_truejump (CF_NONE, TrueLab);
2509 g_truejump (CF_NONE, TrueLab);
2512 lval->e_flags = E_MEXPR;
2513 lval->e_test |= E_CC; /* Condition codes are set */
2517 /* If we really had boolean ops, generate the end sequence */
2519 DoneLab = GetLabel ();
2520 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2521 g_falsejump (CF_NONE, DoneLab);
2522 g_defloclabel (TrueLab);
2523 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2524 g_defloclabel (DoneLab);
2531 static int hieQuest (struct expent *lval)
2532 /* Parse "lvalue ? exp : exp" */
2537 struct expent lval2; /* Expression 2 */
2538 struct expent lval3; /* Expression 3 */
2539 type* type2; /* Type of expression 2 */
2540 type* type3; /* Type of expression 3 */
2541 type* rtype; /* Type of result */
2542 CodeMark Mark1; /* Save position in output code */
2543 CodeMark Mark2; /* Save position in output code */
2548 if (curtok == TOK_QUEST) {
2550 if ((lval->e_test & E_CC) == 0) {
2551 /* Condition codes not set, force a test */
2552 lval->e_test |= E_FORCETEST;
2554 exprhs (CF_NONE, k, lval);
2556 g_falsejump (CF_NONE, labf);
2558 /* Parse second and third expression */
2559 expression1 (&lval2);
2563 g_defloclabel (labf);
2564 expression1 (&lval3);
2566 /* Check if any conversions are needed, if so, do them.
2567 * Conversion rules for ?: expression are:
2568 * - if both expressions are int expressions, default promotion
2569 * rules for ints apply.
2570 * - if both expressions are pointers of the same type, the
2571 * result of the expression is of this type.
2572 * - if one of the expressions is a pointer and the other is
2573 * a zero constant, the resulting type is that of the pointer
2575 * - all other cases are flagged by an error.
2577 type2 = lval2.e_tptr;
2578 type3 = lval3.e_tptr;
2579 if (IsClassInt (type2) && IsClassInt (type3)) {
2581 /* Get common type */
2582 rtype = promoteint (type2, type3);
2584 /* Convert the third expression to this type if needed */
2585 g_typecast (TypeOf (rtype), TypeOf (type3));
2587 /* Setup a new label so that the expr3 code will jump around
2588 * the type cast code for expr2.
2590 labf = GetLabel (); /* Get new label */
2591 Mark1 = GetCodePos (); /* Remember current position */
2592 g_jump (labf); /* Jump around code */
2594 /* The jump for expr2 goes here */
2595 g_defloclabel (labt);
2597 /* Create the typecast code for expr2 */
2598 Mark2 = GetCodePos (); /* Remember position */
2599 g_typecast (TypeOf (rtype), TypeOf (type2));
2601 /* If the typecast did not produce code, remove the jump,
2602 * otherwise output the label.
2604 if (GetCodePos() == Mark2) {
2605 RemoveCode (Mark1); /* Remove code */
2607 /* We have typecast code, output label */
2608 g_defloclabel (labf);
2609 labt = 0; /* Mark other label as invalid */
2612 } else if (IsClassPtr (type2) && IsClassPtr (type3)) {
2613 /* Must point to same type */
2614 if (TypeCmp (Indirect (type2), Indirect (type3)) < TC_EQUAL) {
2615 Error ("Incompatible pointer types");
2617 /* Result has the common type */
2618 rtype = lval2.e_tptr;
2619 } else if (IsClassPtr (type2) && IsNullPtr (&lval3)) {
2620 /* Result type is pointer, no cast needed */
2621 rtype = lval2.e_tptr;
2622 } else if (IsNullPtr (&lval2) && IsClassPtr (type3)) {
2623 /* Result type is pointer, no cast needed */
2624 rtype = lval3.e_tptr;
2626 Error ("Incompatible types");
2627 rtype = lval2.e_tptr; /* Doesn't matter here */
2630 /* If we don't have the label defined until now, do it */
2632 g_defloclabel (labt);
2635 /* Setup the target expression */
2636 lval->e_flags = E_MEXPR;
2637 lval->e_tptr = rtype;
2645 static void opeq (GenDesc* Gen, struct expent *lval, int k)
2646 /* Process "op=" operators. */
2648 struct expent lval2;
2655 Error ("Invalid lvalue in assignment");
2659 /* Determine the type of the lhs */
2660 flags = TypeOf (lval->e_tptr);
2661 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2662 lval->e_tptr [0] == T_PTR;
2664 /* Get the lhs address on stack (if needed) */
2667 /* Fetch the lhs into the primary register if needed */
2668 exprhs (CF_NONE, k, lval);
2670 /* Bring the lhs on stack */
2671 Mark = GetCodePos ();
2674 /* Evaluate the rhs */
2675 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2676 /* The resulting value is a constant. If the generator has the NOPUSH
2677 * flag set, don't push the lhs.
2679 if (Gen->Flags & GEN_NOPUSH) {
2684 /* lhs is a pointer, scale rhs */
2685 lval2.e_const *= SizeOf (lval->e_tptr+1);
2688 /* If the lhs is character sized, the operation may be later done
2691 if (SizeOf (lval->e_tptr) == 1) {
2692 flags |= CF_FORCECHAR;
2695 /* Special handling for add and sub - some sort of a hack, but short code */
2696 if (Gen->Func == g_add) {
2697 g_inc (flags | CF_CONST, lval2.e_const);
2698 } else if (Gen->Func == g_sub) {
2699 g_dec (flags | CF_CONST, lval2.e_const);
2701 Gen->Func (flags | CF_CONST, lval2.e_const);
2704 /* rhs is not constant and already in the primary register */
2706 /* lhs is a pointer, scale rhs */
2707 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2710 /* If the lhs is character sized, the operation may be later done
2713 if (SizeOf (lval->e_tptr) == 1) {
2714 flags |= CF_FORCECHAR;
2717 /* Adjust the types of the operands if needed */
2718 Gen->Func (g_typeadjust (flags, TypeOf (lval2.e_tptr)), 0);
2721 lval->e_flags = E_MEXPR;
2726 static void addsubeq (GenDesc* Gen, struct expent *lval, int k)
2727 /* Process the += and -= operators */
2729 struct expent lval2;
2735 Error ("Invalid lvalue in assignment");
2740 /* We're currently only able to handle some adressing modes */
2741 if ((lval->e_flags & E_MGLOBAL) == 0 && /* Global address? */
2742 (lval->e_flags & E_MLOCAL) == 0 && /* Local address? */
2743 (lval->e_flags & E_MCONST) == 0) { /* Constant address? */
2744 /* Use generic routine */
2745 opeq (Gen, lval, k);
2749 /* Skip the operator */
2752 /* Check if we have a pointer expression and must scale rhs */
2753 MustScale = (lval->e_tptr [0] == T_PTR);
2755 /* Determine the code generator flags */
2756 flags = TypeOf (lval->e_tptr) | CF_FORCECHAR;
2758 /* Evaluate the rhs */
2759 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2760 /* The resulting value is a constant. */
2762 /* lhs is a pointer, scale rhs */
2763 lval2.e_const *= SizeOf (lval->e_tptr+1);
2767 /* rhs is not constant and already in the primary register */
2769 /* lhs is a pointer, scale rhs */
2770 g_scale (TypeOf (lval2.e_tptr), SizeOf (lval->e_tptr+1));
2774 /* Adjust the rhs to the lhs */
2775 g_typeadjust (flags, TypeOf (lval2.e_tptr));
2777 /* Output apropriate code */
2778 if (lval->e_flags & E_MGLOBAL) {
2779 /* Static variable */
2780 flags |= GlobalModeFlags (lval->e_flags);
2781 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2782 g_addeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2784 g_subeqstatic (flags, lval->e_name, lval->e_const, lval2.e_const);
2786 } else if (lval->e_flags & E_MLOCAL) {
2787 /* ref to localvar */
2788 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2789 g_addeqlocal (flags, lval->e_const, lval2.e_const);
2791 g_subeqlocal (flags, lval->e_const, lval2.e_const);
2793 } else if (lval->e_flags & E_MCONST) {
2794 /* ref to absolute address */
2795 flags |= CF_ABSOLUTE;
2796 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2797 g_addeqstatic (flags, lval->e_const, 0, lval2.e_const);
2799 g_subeqstatic (flags, lval->e_const, 0, lval2.e_const);
2801 } else if (lval->e_flags & E_MEXPR) {
2802 /* Address in a/x. */
2803 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2804 g_addeqind (flags, lval->e_const, lval2.e_const);
2806 g_subeqind (flags, lval->e_const, lval2.e_const);
2809 Internal ("Invalid addressing mode");
2812 /* Expression is in the primary now */
2813 lval->e_flags = E_MEXPR;
2818 static void Assignment (struct expent* lval)
2819 /* Parse an assignment */
2822 struct expent lval2;
2824 type* ltype = lval->e_tptr;
2826 /* Check for assignment to const */
2827 if (IsQualConst (ltype)) {
2828 Error ("Assignment to const");
2831 /* cc65 does not have full support for handling structs by value. Since
2832 * assigning structs is one of the more useful operations from this
2833 * familiy, allow it here.
2835 if (IsClassStruct (ltype)) {
2837 /* Bring the address of the lhs into the primary and push it */
2838 exprhs (0, 0, lval);
2839 g_push (CF_PTR | CF_UNSIGNED, 0);
2841 /* Get the expression on the right of the '=' into the primary */
2844 /* Get the address */
2845 exprhs (0, 0, &lval2);
2847 /* We need an lvalue */
2848 Error ("Invalid lvalue in assignment");
2851 /* Push the address (or whatever is in ax in case of errors) */
2852 g_push (CF_PTR | CF_UNSIGNED, 0);
2854 /* Check for equality of the structs */
2855 if (TypeCmp (ltype, lval2.e_tptr) < TC_EQUAL) {
2856 Error ("Incompatible types");
2859 /* Load the size of the struct into the primary */
2860 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, SizeOf (ltype), 0);
2862 /* Call the memcpy function */
2863 g_call (CF_FIXARGC, "memcpy", 4);
2867 /* Get the address on stack if needed */
2870 /* No struct, setup flags for the load */
2871 flags = SizeOf (ltype) == 1? CF_FORCECHAR : CF_NONE;
2873 /* Get the expression on the right of the '=' into the primary */
2874 if (evalexpr (flags, hie1, &lval2) == 0) {
2875 /* Constant expression. Adjust the types */
2876 assignadjust (ltype, &lval2);
2877 /* Put the value into the primary register */
2878 lconst (flags, &lval2);
2880 /* Expression is not constant and already in the primary */
2881 assignadjust (ltype, &lval2);
2884 /* Generate a store instruction */
2889 /* Value is still in primary */
2890 lval->e_flags = E_MEXPR;
2895 int hie1 (struct expent* lval)
2896 /* Parse first level of expression hierarchy. */
2900 k = hieQuest (lval);
2910 Error ("Invalid lvalue in assignment");
2916 case TOK_PLUS_ASSIGN:
2917 addsubeq (&GenPASGN, lval, k);
2920 case TOK_MINUS_ASSIGN:
2921 addsubeq (&GenSASGN, lval, k);
2924 case TOK_MUL_ASSIGN:
2925 opeq (&GenMASGN, lval, k);
2928 case TOK_DIV_ASSIGN:
2929 opeq (&GenDASGN, lval, k);
2932 case TOK_MOD_ASSIGN:
2933 opeq (&GenMOASGN, lval, k);
2936 case TOK_SHL_ASSIGN:
2937 opeq (&GenSLASGN, lval, k);
2940 case TOK_SHR_ASSIGN:
2941 opeq (&GenSRASGN, lval, k);
2944 case TOK_AND_ASSIGN:
2945 opeq (&GenAASGN, lval, k);
2948 case TOK_XOR_ASSIGN:
2949 opeq (&GenXOASGN, lval, k);
2953 opeq (&GenOASGN, lval, k);
2964 int hie0 (struct expent *lval)
2965 /* Parse comma operator. */
2970 while (curtok == TOK_COMMA) {
2979 int evalexpr (unsigned flags, int (*f) (struct expent*), struct expent* lval)
2980 /* Will evaluate an expression via the given function. If the result is a
2981 * constant, 0 is returned and the value is put in the lval struct. If the
2982 * result is not constant, exprhs is called to bring the value into the
2983 * primary register and 1 is returned.
2990 if (k == 0 && lval->e_flags == E_MCONST) {
2991 /* Constant expression */
2994 /* Not constant, load into the primary */
2995 exprhs (flags, k, lval);
3002 int expr (int (*func) (struct expent*), struct expent *lval)
3003 /* Expression parser; func is either hie0 or hie1. */
3012 /* Do some checks if code generation is still constistent */
3013 if (savsp != oursp) {
3015 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
3017 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
3025 void expression1 (struct expent* lval)
3026 /* Evaluate an expression on level 1 (no comma operator) and put it into
3027 * the primary register
3030 memset (lval, 0, sizeof (*lval));
3031 exprhs (CF_NONE, expr (hie1, lval), lval);
3036 void expression (struct expent* lval)
3037 /* Evaluate an expression and put it into the primary register */
3039 memset (lval, 0, sizeof (*lval));
3040 exprhs (CF_NONE, expr (hie0, lval), lval);
3045 void constexpr (struct expent* lval)
3046 /* Get a constant value */
3048 memset (lval, 0, sizeof (*lval));
3049 if (expr (hie1, lval) != 0 || (lval->e_flags & E_MCONST) == 0) {
3050 Error ("Constant expression expected");
3051 /* To avoid any compiler errors, make the expression a valid const */
3052 lval->e_flags = E_MCONST;
3053 lval->e_tptr = type_int;
3060 void intexpr (struct expent* lval)
3061 /* Get an integer expression */
3064 if (!IsClassInt (lval->e_tptr)) {
3065 Error ("Integer expression expected");
3066 /* To avoid any compiler errors, make the expression a valid int */
3067 lval->e_flags = E_MCONST;
3068 lval->e_tptr = type_int;
3075 void boolexpr (struct expent* lval)
3076 /* Get a boolean expression */
3078 /* Read an expression */
3081 /* If it's an integer, it's ok. If it's not an integer, but a pointer,
3082 * the pointer used in a boolean context is also ok
3084 if (!IsClassInt (lval->e_tptr) && !IsClassPtr (lval->e_tptr)) {
3085 Error ("Boolean expression expected");
3086 /* To avoid any compiler errors, make the expression a valid int */
3087 lval->e_flags = E_MCONST;
3088 lval->e_tptr = type_int;
3095 void test (unsigned label, int cond)
3096 /* Generate code to perform test and jump if false. */
3101 /* Eat the parenthesis */
3104 /* Prepare the expression, setup labels */
3105 memset (&lval, 0, sizeof (lval));
3107 /* Generate code to eval the expr */
3108 k = expr (hie0, &lval);
3109 if (k == 0 && lval.e_flags == E_MCONST) {
3110 /* Constant rvalue */
3111 if (cond == 0 && lval.e_const == 0) {
3113 Warning ("Unreachable code");
3114 } else if (cond && lval.e_const) {
3121 /* If the expr hasn't set condition codes, set the force-test flag */
3122 if ((lval.e_test & E_CC) == 0) {
3123 lval.e_test |= E_FORCETEST;
3126 /* Load the value into the primary register */
3127 exprhs (CF_FORCECHAR, k, &lval);
3129 /* Check for the closing brace */
3132 /* Generate the jump */
3134 g_truejump (CF_NONE, label);
3136 /* Special case (putting this here is a small hack - but hey, the
3137 * compiler itself is one big hack...): If a semicolon follows, we
3138 * don't have a statement and may omit the jump.
3140 if (curtok != TOK_SEMI) {
3141 g_falsejump (CF_NONE, label);