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 token_t Tok; /* Token to map to */
50 unsigned 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 (ExprDesc* 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 (ExprDesc* lval)
118 /* Return true if this is the NULL pointer constant */
120 return (IsClassInt (lval->Type) && /* Is it an int? */
121 lval->Flags == E_MCONST && /* Is it constant? */
122 lval->ConstVal == 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 (ExprDesc* lhs, ExprDesc* 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->Type;
167 type* rhst = rhs->Type;
169 /* Generate type adjustment code if needed */
170 ltype = TypeOf (lhst);
171 if (lhs->Flags == E_MCONST) {
175 /* Value is in primary register*/
178 rtype = TypeOf (rhst);
179 if (rhs->Flags == E_MCONST) {
182 flags = g_typeadjust (ltype, rtype);
184 /* Set the type of the result */
185 lhs->Type = promoteint (lhst, rhst);
187 /* Return the code generator flags */
193 unsigned assignadjust (type* lhst, ExprDesc* 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->Type;
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->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->Flags != E_MCONST || rhs->ConstVal != 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 (ExprDesc* lval)
284 /* Output a data definition for the given expression */
286 unsigned flags = lval->Flags;
288 switch (flags & E_MCTYPE) {
292 g_defdata (TypeOf (lval->Type) | CF_CONST, lval->ConstVal, 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->Name, lval->ConstVal);
311 /* a literal of some kind */
312 g_defdata (CF_STATIC, LiteralPoolLabel, lval->ConstVal);
316 Internal ("Unknown constant type: %04X", flags);
322 static void lconst (unsigned flags, ExprDesc* lval)
323 /* Load primary reg with some constant value. */
325 switch (lval->Flags & E_MCTYPE) {
328 g_leasp (lval->ConstVal);
332 /* Number constant */
333 g_getimmed (flags | TypeOf (lval->Type) | CF_CONST, lval->ConstVal, 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->Flags);
350 g_getimmed (flags, lval->Name, lval->ConstVal);
355 g_getimmed (CF_STATIC, LiteralPoolLabel, lval->ConstVal);
359 Internal ("Unknown constant type: %04X", lval->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 == TOK_LPAREN && (
435 (NextTok.Tok >= TOK_FIRSTTYPE && NextTok.Tok <= TOK_LASTTYPE) ||
436 (NextTok.Tok == TOK_CONST) ||
437 (NextTok.Tok == TOK_IDENT &&
438 (Entry = FindSym (NextTok.Ident)) != 0 &&
444 static void PushAddr (ExprDesc* lval)
445 /* If the expression contains an address that was somehow evaluated,
446 * push this address on the stack. This is a helper function for all
447 * sorts of implicit or explicit assignment functions where the lvalue
448 * must be saved if it's not constant, before evaluating the rhs.
451 /* Get the address on stack if needed */
452 if (lval->Flags != E_MREG && (lval->Flags & E_MEXPR)) {
453 /* Push the address (always a pointer) */
460 /*****************************************************************************/
462 /*****************************************************************************/
466 void exprhs (unsigned flags, int k, ExprDesc *lval)
467 /* Put the result of an expression into the primary register */
473 /* Dereferenced lvalue */
474 flags |= TypeOf (lval->Type);
475 if (lval->Test & E_FORCETEST) {
477 lval->Test &= ~E_FORCETEST;
479 if (f & E_MGLOBAL) { /* ref to globalvar */
481 flags |= GlobalModeFlags (f);
482 g_getstatic (flags, lval->Name, lval->ConstVal);
483 } else if (f & E_MLOCAL) {
484 /* ref to localvar */
485 g_getlocal (flags, lval->ConstVal);
486 } else if (f & E_MCONST) {
487 /* ref to absolute address */
488 g_getstatic (flags | CF_ABSOLUTE, lval->ConstVal, 0);
489 } else if (f == E_MEOFFS) {
490 g_getind (flags, lval->ConstVal);
491 } else if (f != E_MREG) {
494 } else if (f == E_MEOFFS) {
495 /* reference not storable */
496 flags |= TypeOf (lval->Type);
497 g_inc (flags | CF_CONST, lval->ConstVal);
498 } else if ((f & E_MEXPR) == 0) {
499 /* Constant of some sort, load it into the primary */
500 lconst (flags, lval);
502 if (lval->Test & E_FORCETEST) { /* we testing this value? */
504 flags |= TypeOf (lval->Type);
505 g_test (flags); /* yes, force a test */
506 lval->Test &= ~E_FORCETEST;
512 static unsigned FunctionParamList (FuncDesc* Func)
513 /* Parse a function parameter list and pass the parameters to the called
514 * function. Depending on several criteria this may be done by just pushing
515 * each parameter separately, or creating the parameter frame once and then
516 * storing into this frame.
517 * The function returns the size of the parameters pushed.
522 /* Initialize variables */
523 SymEntry* Param = 0; /* Keep gcc silent */
524 unsigned ParamSize = 0; /* Size of parameters pushed */
525 unsigned ParamCount = 0; /* Number of parameters pushed */
526 unsigned FrameSize = 0; /* Size of parameter frame */
527 unsigned FrameParams = 0; /* Number of params in frame */
528 int FrameOffs = 0; /* Offset into parameter frame */
529 int Ellipsis = 0; /* Function is variadic */
531 /* As an optimization, we may allocate the complete parameter frame at
532 * once instead of pushing each parameter as it comes. We may do that,
535 * - optimizations that increase code size are enabled (allocating the
536 * stack frame at once gives usually larger code).
537 * - we have more than one parameter to push (don't count the last param
538 * for __fastcall__ functions).
540 if (CodeSizeFactor >= 200) {
542 /* Calculate the number and size of the parameters */
543 FrameParams = Func->ParamCount;
544 FrameSize = Func->ParamSize;
545 if (FrameParams > 0 && (Func->Flags & FD_FASTCALL) != 0) {
546 /* Last parameter is not pushed */
547 const SymEntry* LastParam = Func->SymTab->SymTail;
548 FrameSize -= SizeOf (LastParam->Type);
552 /* Do we have more than one parameter in the frame? */
553 if (FrameParams > 1) {
554 /* Okeydokey, setup the frame */
559 /* Don't use a preallocated frame */
564 /* Parse the actual parameter list */
565 while (CurTok.Tok != TOK_RPAREN) {
570 /* Count arguments */
573 /* Fetch the pointer to the next argument, check for too many args */
574 if (ParamCount <= Func->ParamCount) {
575 /* Beware: If there are parameters with identical names, they
576 * cannot go into the same symbol table, which means that in this
577 * case of errorneous input, the number of nodes in the symbol
578 * table and ParamCount are NOT equal. We have to handle this case
579 * below to avoid segmentation violations. Since we know that this
580 * problem can only occur if there is more than one parameter,
581 * we will just use the last one.
583 if (ParamCount == 1) {
585 Param = Func->SymTab->SymHead;
586 } else if (Param->NextSym != 0) {
588 Param = Param->NextSym;
589 CHECK ((Param->Flags & SC_PARAM) != 0);
591 } else if (!Ellipsis) {
592 /* Too many arguments. Do we have an open param list? */
593 if ((Func->Flags & FD_VARIADIC) == 0) {
594 /* End of param list reached, no ellipsis */
595 Error ("Too many arguments in function call");
597 /* Assume an ellipsis even in case of errors to avoid an error
598 * message for each other argument.
603 /* Do some optimization: If we have a constant value to push,
604 * use a special function that may optimize.
607 if (!Ellipsis && SizeOf (Param->Type) == 1) {
608 CFlags = CF_FORCECHAR;
611 if (evalexpr (CFlags, hie1, &lval) == 0) {
612 /* A constant value */
616 /* If we don't have an argument spec, accept anything, otherwise
617 * convert the actual argument to the type needed.
620 /* Promote the argument if needed */
621 assignadjust (Param->Type, &lval);
623 /* If we have a prototype, chars may be pushed as chars */
624 Flags |= CF_FORCECHAR;
627 /* Use the type of the argument for the push */
628 Flags |= TypeOf (lval.Type);
630 /* If this is a fastcall function, don't push the last argument */
631 if (ParamCount == Func->ParamCount && (Func->Flags & FD_FASTCALL) != 0) {
632 /* Just load the argument into the primary. This is only needed if
633 * we have a constant argument, otherwise the value is already in
636 if (Flags & CF_CONST) {
637 exprhs (CF_FORCECHAR, 0, &lval);
640 unsigned ArgSize = sizeofarg (Flags);
642 /* We have the space already allocated, store in the frame */
643 CHECK (FrameSize >= ArgSize);
644 FrameSize -= ArgSize;
645 FrameOffs -= ArgSize;
647 g_putlocal (Flags | CF_NOKEEP, FrameOffs, lval.ConstVal);
649 /* Push the argument */
650 g_push (Flags, lval.ConstVal);
653 /* Calculate total parameter size */
654 ParamSize += ArgSize;
657 /* Check for end of argument list */
658 if (CurTok.Tok != TOK_COMMA) {
664 /* Check if we had enough parameters */
665 if (ParamCount < Func->ParamCount) {
666 Error ("Too few arguments in function call");
669 /* The function returns the size of all parameters pushed onto the stack.
670 * However, if there are parameters missing (which is an error and was
671 * flagged by the compiler) AND a stack frame was preallocated above,
672 * we would loose track of the stackpointer and generate an internal error
673 * later. So we correct the value by the parameters that should have been
674 * pushed to avoid an internal compiler error. Since an error was
675 * generated before, no code will be output anyway.
677 return ParamSize + FrameSize;
682 static void CallFunction (ExprDesc* lval)
683 /* Perform a function call. Called from hie11, this routine will
684 * either call the named function, or the function pointer in a/x.
687 FuncDesc* Func; /* Function descriptor */
688 unsigned ParamSize; /* Number of parameter bytes */
692 /* Get a pointer to the function descriptor from the type string */
693 Func = GetFuncDesc (lval->Type);
695 /* Initialize vars to keep gcc silent */
698 /* Check if this is a function pointer. If so, save it. If not, check for
699 * special known library functions that may be inlined.
701 if (lval->Flags & E_MEXPR) {
702 /* Function pointer is in primary register, save it */
703 Mark = GetCodePos ();
705 } else if (InlineStdFuncs && IsStdFunc ((const char*) lval->Name)) {
706 /* Inline this function */
707 HandleStdFunc (lval);
711 /* Parse the parameter list */
712 ParamSize = FunctionParamList (Func);
714 /* We need the closing bracket here */
718 if (lval->Flags & E_MEXPR) {
719 /* Function called via pointer: Restore it and call function */
720 if (ParamSize != 0) {
723 /* We had no parameters - remove save code */
726 g_callind (TypeOf (lval->Type), ParamSize);
728 g_call (TypeOf (lval->Type), (const char*) lval->Name, ParamSize);
735 /* This function parses ASM statements. The syntax of the ASM directive
736 * looks like the one defined for C++ (C has no ASM directive), that is,
737 * a string literal in parenthesis.
743 /* Need left parenthesis */
747 if (CurTok.Tok != TOK_SCONST) {
748 Error ("String literal expected");
751 /* The string literal may consist of more than one line of assembler
752 * code. Separate the single lines and output the code.
754 const char* S = GetLiteral (CurTok.IVal);
757 /* Allow lines up to 256 bytes */
758 const char* E = strchr (S, '\n');
760 /* Found a newline */
764 int Len = strlen (S);
770 /* Reset the string pointer, effectivly clearing the string from the
771 * string table. Since we're working with one token lookahead, this
772 * will fail if the next token is also a string token, but that's a
773 * syntax error anyway, because we expect a right paren.
775 ResetLiteralPoolOffs (CurTok.IVal);
778 /* Skip the string token */
781 /* Closing paren needed */
787 static int primary (ExprDesc* lval)
788 /* This is the lowest level of the expression parser. */
792 /* not a test at all, yet */
795 /* Character and integer constants. */
796 if (CurTok.Tok == TOK_ICONST || CurTok.Tok == TOK_CCONST) {
797 lval->Flags = E_MCONST | E_TCONST;
798 lval->Type = CurTok.Type;
799 lval->ConstVal = CurTok.IVal;
804 /* Process parenthesized subexpression by calling the whole parser
807 if (CurTok.Tok == TOK_LPAREN) {
809 memset (lval, 0, sizeof (*lval)); /* Remove any attributes */
815 /* All others may only be used if the expression evaluation is not called
816 * recursively by the preprocessor.
819 /* Illegal expression in PP mode */
820 Error ("Preprocessor expression expected");
821 lval->Flags = E_MCONST;
822 lval->Type = type_int;
827 if (CurTok.Tok == TOK_IDENT) {
832 /* Get a pointer to the symbol table entry */
833 Sym = FindSym (CurTok.Ident);
835 /* Is the symbol known? */
838 /* We found the symbol - skip the name token */
841 /* The expression type is the symbol type */
842 lval->Type = Sym->Type;
844 /* Check for illegal symbol types */
845 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
846 if (Sym->Flags & SC_TYPE) {
847 /* Cannot use type symbols */
848 Error ("Variable identifier expected");
849 /* Assume an int type to make lval valid */
850 lval->Flags = E_MLOCAL | E_TLOFFS;
851 lval->Type = type_int;
856 /* Check for legal symbol types */
857 if ((Sym->Flags & SC_CONST) == SC_CONST) {
858 /* Enum or some other numeric constant */
859 lval->Flags = E_MCONST;
860 lval->ConstVal = Sym->V.ConstVal;
862 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
864 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
865 lval->Name = (unsigned long) Sym->Name;
867 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
868 /* Local variable. If this is a parameter for a variadic
869 * function, we have to add some address calculations, and the
870 * address is not const.
872 if ((Sym->Flags & SC_PARAM) == SC_PARAM && IsVariadic (CurrentFunc)) {
873 /* Variadic parameter */
874 g_leavariadic (Sym->V.Offs - GetParamSize (CurrentFunc));
875 lval->Flags = E_MEXPR;
878 /* Normal parameter */
879 lval->Flags = E_MLOCAL | E_TLOFFS;
880 lval->ConstVal = Sym->V.Offs;
882 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
883 /* Static variable */
884 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
885 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
886 lval->Name = (unsigned long) Sym->Name;
888 lval->Flags = E_MGLOBAL | E_MCONST | E_TLLAB;
889 lval->Name = Sym->V.Label;
892 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
893 /* Register variable, zero page based */
894 lval->Flags = E_MGLOBAL | E_MCONST | E_TREGISTER;
895 lval->Name = Sym->V.Offs;
898 /* Local static variable */
899 lval->Flags = E_MGLOBAL | E_MCONST | E_TLLAB;
900 lval->Name = Sym->V.Offs;
904 /* The symbol is referenced now */
905 Sym->Flags |= SC_REF;
906 if (IsTypeFunc (lval->Type) || IsTypeArray (lval->Type)) {
912 /* We did not find the symbol. Remember the name, then skip it */
913 strcpy (Ident, CurTok.Ident);
916 /* IDENT is either an auto-declared function or an undefined variable. */
917 if (CurTok.Tok == TOK_LPAREN) {
918 /* Declare a function returning int. For that purpose, prepare a
919 * function signature for a function having an empty param list
922 Warning ("Function call without a prototype");
923 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
924 lval->Type = Sym->Type;
925 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
926 lval->Name = (unsigned long) Sym->Name;
932 /* Undeclared Variable */
933 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
934 lval->Flags = E_MLOCAL | E_TLOFFS;
935 lval->Type = type_int;
937 Error ("Undefined symbol: `%s'", Ident);
943 /* String literal? */
944 if (CurTok.Tok == TOK_SCONST) {
945 lval->Flags = E_MCONST | E_TLIT;
946 lval->ConstVal = CurTok.IVal;
947 lval->Type = GetCharArrayType (strlen (GetLiteral (CurTok.IVal)));
953 if (CurTok.Tok == TOK_ASM) {
955 lval->Type = type_void;
956 lval->Flags = E_MEXPR;
961 /* __AX__ and __EAX__ pseudo values? */
962 if (CurTok.Tok == TOK_AX || CurTok.Tok == TOK_EAX) {
963 lval->Type = (CurTok.Tok == TOK_AX)? type_uint : type_ulong;
964 lval->Flags = E_MREG;
968 return 1; /* May be used as lvalue */
971 /* Illegal primary. */
972 Error ("Expression expected");
973 lval->Flags = E_MCONST;
974 lval->Type = type_int;
980 static int arrayref (int k, ExprDesc* lval)
981 /* Handle an array reference */
995 /* Skip the bracket */
998 /* Get the type of left side */
1001 /* We can apply a special treatment for arrays that have a const base
1002 * address. This is true for most arrays and will produce a lot better
1003 * code. Check if this is a const base address.
1005 lflags = lval->Flags & ~E_MCTYPE;
1006 ConstBaseAddr = (lflags == E_MCONST) || /* Constant numeric address */
1007 (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1008 lflags == E_MLOCAL; /* Local array */
1010 /* If we have a constant base, we delay the address fetch */
1011 Mark1 = GetCodePos ();
1012 Mark2 = 0; /* Silence gcc */
1013 if (!ConstBaseAddr) {
1014 /* Get a pointer to the array into the primary */
1015 exprhs (CF_NONE, k, lval);
1017 /* Get the array pointer on stack. Do not push more than 16
1018 * bit, even if this value is greater, since we cannot handle
1019 * other than 16bit stuff when doing indexing.
1021 Mark2 = GetCodePos ();
1025 /* TOS now contains ptr to array elements. Get the subscript. */
1027 if (l == 0 && lval2.Flags == E_MCONST) {
1029 /* The array subscript is a constant - remove value from stack */
1030 if (!ConstBaseAddr) {
1034 /* Get an array pointer into the primary */
1035 exprhs (CF_NONE, k, lval);
1038 if (IsClassPtr (tptr1)) {
1040 /* Scale the subscript value according to element size */
1041 lval2.ConstVal *= PSizeOf (tptr1);
1043 /* Remove code for lhs load */
1046 /* Handle constant base array on stack. Be sure NOT to
1047 * handle pointers the same way, this won't work.
1049 if (IsTypeArray (tptr1) &&
1050 ((lval->Flags & ~E_MCTYPE) == E_MCONST ||
1051 (lval->Flags & ~E_MCTYPE) == E_MLOCAL ||
1052 (lval->Flags & E_MGLOBAL) != 0 ||
1053 (lval->Flags == E_MEOFFS))) {
1054 lval->ConstVal += lval2.ConstVal;
1057 /* Pointer - load into primary and remember offset */
1058 if ((lval->Flags & E_MEXPR) == 0 || k != 0) {
1059 exprhs (CF_NONE, k, lval);
1061 lval->ConstVal = lval2.ConstVal;
1062 lval->Flags = E_MEOFFS;
1065 /* Result is of element type */
1066 lval->Type = Indirect (tptr1);
1071 } else if (IsClassPtr (tptr2 = lval2.Type)) {
1072 /* Subscript is pointer, get element type */
1073 lval2.Type = Indirect (tptr2);
1075 /* Scale the rhs value in the primary register */
1076 g_scale (TypeOf (tptr1), SizeOf (lval2.Type));
1078 lval->Type = lval2.Type;
1080 Error ("Cannot subscript");
1083 /* Add the subscript. Since arrays are indexed by integers,
1084 * we will ignore the true type of the subscript here and
1085 * use always an int.
1087 g_inc (CF_INT | CF_CONST, lval2.ConstVal);
1091 /* Array subscript is not constant. Load it into the primary */
1092 Mark2 = GetCodePos ();
1093 exprhs (CF_NONE, l, &lval2);
1096 if (IsClassPtr (tptr1)) {
1098 /* Get the element type */
1099 lval->Type = Indirect (tptr1);
1101 /* Indexing is based on int's, so we will just use the integer
1102 * portion of the index (which is in (e)ax, so there's no further
1105 g_scale (CF_INT, SizeOf (lval->Type));
1107 } else if (IsClassPtr (tptr2)) {
1109 /* Get the element type */
1110 lval2.Type = Indirect (tptr2);
1112 /* Get the int value on top. If we go here, we're sure,
1113 * both values are 16 bit (the first one was truncated
1114 * if necessary and the second one is a pointer).
1115 * Note: If ConstBaseAddr is true, we don't have a value on
1116 * stack, so to "swap" both, just push the subscript.
1118 if (ConstBaseAddr) {
1120 exprhs (CF_NONE, k, lval);
1127 g_scale (TypeOf (tptr1), SizeOf (lval2.Type));
1128 lval->Type = lval2.Type;
1130 Error ("Cannot subscript");
1133 /* The offset is now in the primary register. It didn't have a
1134 * constant base address for the lhs, the lhs address is already
1135 * on stack, and we must add the offset. If the base address was
1136 * constant, we call special functions to add the address to the
1139 if (!ConstBaseAddr) {
1140 /* Add the subscript. Both values are int sized. */
1144 /* If the subscript has itself a constant address, it is often
1145 * a better idea to reverse again the order of the evaluation.
1146 * This will generate better code if the subscript is a byte
1147 * sized variable. But beware: This is only possible if the
1148 * subscript was not scaled, that is, if this was a byte array
1151 rflags = lval2.Flags & ~E_MCTYPE;
1152 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1153 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1154 rflags == E_MLOCAL; /* Local array */
1156 if (ConstSubAddr && SizeOf (lval->Type) == 1) {
1160 /* Reverse the order of evaluation */
1161 unsigned flags = (SizeOf (lval2.Type) == 1)? CF_CHAR : CF_INT;
1164 /* Get a pointer to the array into the primary. We have changed
1165 * Type above but we need the original type to load the
1166 * address, so restore it temporarily.
1168 SavedType = lval->Type;
1170 exprhs (CF_NONE, k, lval);
1171 lval->Type = SavedType;
1173 /* Add the variable */
1174 if (rflags == E_MLOCAL) {
1175 g_addlocal (flags, lval2.ConstVal);
1177 flags |= GlobalModeFlags (lval2.Flags);
1178 g_addstatic (flags, lval2.Name, lval2.ConstVal);
1181 if (lflags == E_MCONST) {
1182 /* Constant numeric address. Just add it */
1183 g_inc (CF_INT | CF_UNSIGNED, lval->ConstVal);
1184 } else if (lflags == E_MLOCAL) {
1185 /* Base address is a local variable address */
1186 if (IsTypeArray (tptr1)) {
1187 g_addaddr_local (CF_INT, lval->ConstVal);
1189 g_addlocal (CF_PTR, lval->ConstVal);
1192 /* Base address is a static variable address */
1193 unsigned flags = CF_INT;
1194 flags |= GlobalModeFlags (lval->Flags);
1195 if (IsTypeArray (tptr1)) {
1196 g_addaddr_static (flags, lval->Name, lval->ConstVal);
1198 g_addstatic (flags, lval->Name, lval->ConstVal);
1204 lval->Flags = E_MEXPR;
1207 return !IsTypeArray (lval->Type);
1213 static int structref (int k, ExprDesc* lval)
1214 /* Process struct field after . or ->. */
1220 /* Skip the token and check for an identifier */
1222 if (CurTok.Tok != TOK_IDENT) {
1223 Error ("Identifier expected");
1224 lval->Type = type_int;
1228 /* Get the symbol table entry and check for a struct field */
1229 strcpy (Ident, CurTok.Ident);
1231 Field = FindStructField (lval->Type, Ident);
1233 Error ("Struct/union has no field named `%s'", Ident);
1234 lval->Type = type_int;
1238 /* If we have constant input data, the result is also constant */
1239 flags = lval->Flags & ~E_MCTYPE;
1240 if (flags == E_MCONST ||
1241 (k == 0 && (flags == E_MLOCAL ||
1242 (flags & E_MGLOBAL) != 0 ||
1243 lval->Flags == E_MEOFFS))) {
1244 lval->ConstVal += Field->V.Offs;
1246 if ((flags & E_MEXPR) == 0 || k != 0) {
1247 exprhs (CF_NONE, k, lval);
1249 lval->ConstVal = Field->V.Offs;
1250 lval->Flags = E_MEOFFS;
1252 lval->Type = Field->Type;
1253 return !IsTypeArray (Field->Type);
1258 static int hie11 (ExprDesc *lval)
1259 /* Handle compound types (structs and arrays) */
1266 if (CurTok.Tok < TOK_LBRACK || CurTok.Tok > TOK_PTR_REF) {
1273 if (CurTok.Tok == TOK_LBRACK) {
1275 /* Array reference */
1276 k = arrayref (k, lval);
1278 } else if (CurTok.Tok == TOK_LPAREN) {
1280 /* Function call. Skip the opening parenthesis */
1283 if (IsTypeFunc (tptr) || IsTypeFuncPtr (tptr)) {
1284 if (IsTypeFuncPtr (tptr)) {
1285 /* Pointer to function. Handle transparently */
1286 exprhs (CF_NONE, k, lval); /* Function pointer to A/X */
1287 ++lval->Type; /* Skip T_PTR */
1288 lval->Flags |= E_MEXPR;
1290 CallFunction (lval);
1291 lval->Flags = E_MEXPR;
1292 lval->Type += DECODE_SIZE + 1; /* Set to result */
1294 Error ("Illegal function call");
1298 } else if (CurTok.Tok == TOK_DOT) {
1300 if (!IsClassStruct (lval->Type)) {
1301 Error ("Struct expected");
1303 k = structref (0, lval);
1305 } else if (CurTok.Tok == TOK_PTR_REF) {
1308 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1309 Error ("Struct pointer expected");
1311 k = structref (k, lval);
1321 static void store (ExprDesc* lval)
1322 /* Store primary reg into this reference */
1328 flags = TypeOf (lval->Type);
1329 if (f & E_MGLOBAL) {
1330 flags |= GlobalModeFlags (f);
1337 g_putstatic (flags, lval->Name, lval->ConstVal);
1339 } else if (f & E_MLOCAL) {
1340 g_putlocal (flags, lval->ConstVal, 0);
1341 } else if (f == E_MEOFFS) {
1342 g_putind (flags, lval->ConstVal);
1343 } else if (f != E_MREG) {
1345 g_putind (flags, 0);
1347 /* Store into absolute address */
1348 g_putstatic (flags | CF_ABSOLUTE, lval->ConstVal, 0);
1352 /* Assume that each one of the stores will invalidate CC */
1353 lval->Test &= ~E_CC;
1358 static void pre_incdec (ExprDesc* lval, void (*inc) (unsigned, unsigned long))
1359 /* Handle --i and ++i */
1366 if ((k = hie10 (lval)) == 0) {
1367 Error ("Invalid lvalue");
1371 /* Get the data type */
1372 flags = TypeOf (lval->Type) | CF_FORCECHAR | CF_CONST;
1374 /* Get the increment value in bytes */
1375 val = (lval->Type [0] == T_PTR)? PSizeOf (lval->Type) : 1;
1377 /* We're currently only able to handle some adressing modes */
1378 if ((lval->Flags & E_MGLOBAL) == 0 && /* Global address? */
1379 (lval->Flags & E_MLOCAL) == 0 && /* Local address? */
1380 (lval->Flags & E_MCONST) == 0 && /* Constant address? */
1381 (lval->Flags & E_MEXPR) == 0) { /* Address in a/x? */
1383 /* Use generic code. Push the address if needed */
1386 /* Fetch the value */
1387 exprhs (CF_NONE, k, lval);
1389 /* Increment value in primary */
1392 /* Store the result back */
1397 /* Special code for some addressing modes - use the special += ops */
1398 if (lval->Flags & E_MGLOBAL) {
1399 flags |= GlobalModeFlags (lval->Flags);
1401 g_addeqstatic (flags, lval->Name, lval->ConstVal, val);
1403 g_subeqstatic (flags, lval->Name, lval->ConstVal, val);
1405 } else if (lval->Flags & E_MLOCAL) {
1406 /* ref to localvar */
1408 g_addeqlocal (flags, lval->ConstVal, val);
1410 g_subeqlocal (flags, lval->ConstVal, val);
1412 } else if (lval->Flags & E_MCONST) {
1413 /* ref to absolute address */
1414 flags |= CF_ABSOLUTE;
1416 g_addeqstatic (flags, lval->ConstVal, 0, val);
1418 g_subeqstatic (flags, lval->ConstVal, 0, val);
1420 } else if (lval->Flags & E_MEXPR) {
1421 /* Address in a/x, check if we have an offset */
1422 unsigned Offs = (lval->Flags == E_MEOFFS)? lval->ConstVal : 0;
1424 g_addeqind (flags, Offs, val);
1426 g_subeqind (flags, Offs, val);
1429 Internal ("Invalid addressing mode");
1434 /* Result is an expression */
1435 lval->Flags = E_MEXPR;
1440 static void post_incdec (ExprDesc *lval, int k, void (*inc) (unsigned, unsigned long))
1441 /* Handle i-- and i++ */
1447 Error ("Invalid lvalue");
1451 /* Get the data type */
1452 flags = TypeOf (lval->Type);
1454 /* Push the address if needed */
1457 /* Fetch the value and save it (since it's the result of the expression) */
1458 exprhs (CF_NONE, 1, lval);
1459 g_save (flags | CF_FORCECHAR);
1461 /* If we have a pointer expression, increment by the size of the type */
1462 if (lval->Type[0] == T_PTR) {
1463 inc (flags | CF_CONST | CF_FORCECHAR, SizeOf (lval->Type + 1));
1465 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1468 /* Store the result back */
1471 /* Restore the original value */
1472 g_restore (flags | CF_FORCECHAR);
1473 lval->Flags = E_MEXPR;
1478 static void unaryop (int tok, ExprDesc* lval)
1479 /* Handle unary -/+ and ~ */
1486 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
1487 /* Value is constant */
1489 case TOK_MINUS: lval->ConstVal = -lval->ConstVal; break;
1490 case TOK_PLUS: break;
1491 case TOK_COMP: lval->ConstVal = ~lval->ConstVal; break;
1492 default: Internal ("Unexpected token: %d", tok);
1495 /* Value is not constant */
1496 exprhs (CF_NONE, k, lval);
1498 /* Get the type of the expression */
1499 flags = TypeOf (lval->Type);
1501 /* Handle the operation */
1503 case TOK_MINUS: g_neg (flags); break;
1504 case TOK_PLUS: break;
1505 case TOK_COMP: g_com (flags); break;
1506 default: Internal ("Unexpected token: %d", tok);
1508 lval->Flags = E_MEXPR;
1514 static int typecast (ExprDesc* lval)
1515 /* Handle an explicit cast */
1518 type Type[MAXTYPELEN];
1520 /* Skip the left paren */
1529 /* Read the expression we have to cast */
1532 /* If the expression is a function, treat it as pointer-to-function */
1533 if (IsTypeFunc (lval->Type)) {
1534 lval->Type = PointerTo (lval->Type);
1537 /* Check for a constant on the right side */
1538 if (k == 0 && lval->Flags == E_MCONST) {
1540 /* A cast of a constant to something else. If the new type is an int,
1541 * be sure to handle the size extension correctly. If the new type is
1542 * not an int, the cast is implementation specific anyway, so leave
1545 if (IsClassInt (Type)) {
1547 /* Get the current and new size of the value */
1548 unsigned OldSize = SizeOf (lval->Type);
1549 unsigned NewSize = SizeOf (Type);
1550 unsigned OldBits = OldSize * 8;
1551 unsigned NewBits = NewSize * 8;
1553 /* Check if the new datatype will have a smaller range */
1554 if (NewSize < OldSize) {
1556 /* Cut the value to the new size */
1557 lval->ConstVal &= (0xFFFFFFFFUL >> (32 - NewBits));
1559 /* If the new value is signed, sign extend the value */
1560 if (!IsSignUnsigned (Type)) {
1561 lval->ConstVal |= ((~0L) << NewBits);
1564 } else if (NewSize > OldSize) {
1566 /* Sign extend the value if needed */
1567 if (!IsSignUnsigned (Type) && !IsSignUnsigned (lval->Type)) {
1568 if (lval->ConstVal & (0x01UL << (OldBits-1))) {
1569 lval->ConstVal |= ((~0L) << OldBits);
1577 /* Not a constant. Be sure to ignore casts to void */
1578 if (!IsTypeVoid (Type)) {
1580 /* If the size does not change, leave the value alone. Otherwise,
1581 * we have to load the value into the primary and generate code to
1582 * cast the value in the primary register.
1584 if (SizeOf (Type) != SizeOf (lval->Type)) {
1586 /* Load the value into the primary */
1587 exprhs (CF_NONE, k, lval);
1589 /* Mark the lhs as const to avoid a manipulation of TOS */
1590 g_typecast (TypeOf (Type) | CF_CONST, TypeOf (lval->Type));
1592 /* Value is now in primary */
1593 lval->Flags = E_MEXPR;
1599 /* In any case, use the new type */
1600 lval->Type = TypeDup (Type);
1608 static int hie10 (ExprDesc* lval)
1609 /* Handle ++, --, !, unary - etc. */
1614 switch (CurTok.Tok) {
1617 pre_incdec (lval, g_inc);
1621 pre_incdec (lval, g_dec);
1627 unaryop (CurTok.Tok, lval);
1632 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1633 /* Constant expression */
1634 lval->ConstVal = !lval->ConstVal;
1636 g_bneg (TypeOf (lval->Type));
1637 lval->Test |= E_CC; /* bneg will set cc */
1638 lval->Flags = E_MEXPR; /* say it's an expr */
1640 return 0; /* expr not storable */
1644 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1645 /* Expression is not const, indirect value loaded into primary */
1646 lval->Flags = E_MEXPR;
1647 lval->ConstVal = 0; /* Offset is zero now */
1650 if (IsClassPtr (t)) {
1651 lval->Type = Indirect (t);
1653 Error ("Illegal indirection");
1660 /* The & operator may be applied to any lvalue, and it may be
1661 * applied to functions, even if they're no lvalues.
1663 if (k == 0 && !IsTypeFunc (lval->Type)) {
1664 /* Allow the & operator with an array */
1665 if (!IsTypeArray (lval->Type)) {
1666 Error ("Illegal address");
1669 t = TypeAlloc (TypeLen (lval->Type) + 2);
1671 TypeCpy (t + 1, lval->Type);
1678 if (istypeexpr ()) {
1679 type Type[MAXTYPELEN];
1681 lval->ConstVal = SizeOf (ParseType (Type));
1684 /* Remember the output queue pointer */
1685 CodeMark Mark = GetCodePos ();
1687 lval->ConstVal = SizeOf (lval->Type);
1688 /* Remove any generated code */
1691 lval->Flags = E_MCONST | E_TCONST;
1692 lval->Type = type_uint;
1693 lval->Test &= ~E_CC;
1697 if (istypeexpr ()) {
1699 return typecast (lval);
1704 switch (CurTok.Tok) {
1706 post_incdec (lval, k, g_inc);
1710 post_incdec (lval, k, g_dec);
1720 static int hie_internal (GenDesc** ops, /* List of generators */
1721 ExprDesc* lval, /* parent expr's lval */
1722 int (*hienext) (ExprDesc*),
1723 int* UsedGen) /* next higher level */
1724 /* Helper function */
1731 token_t tok; /* The operator token */
1732 unsigned ltype, type;
1733 int rconst; /* Operand is a constant */
1739 while ((Gen = FindGen (CurTok.Tok, ops)) != 0) {
1741 /* Tell the caller that we handled it's ops */
1744 /* All operators that call this function expect an int on the lhs */
1745 if (!IsClassInt (lval->Type)) {
1746 Error ("Integer expression expected");
1749 /* Remember the operator token, then skip it */
1753 /* Get the lhs on stack */
1754 Mark1 = GetCodePos ();
1755 ltype = TypeOf (lval->Type);
1756 if (k == 0 && lval->Flags == E_MCONST) {
1757 /* Constant value */
1758 Mark2 = GetCodePos ();
1759 g_push (ltype | CF_CONST, lval->ConstVal);
1761 /* Value not constant */
1762 exprhs (CF_NONE, k, lval);
1763 Mark2 = GetCodePos ();
1767 /* Get the right hand side */
1768 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1770 /* Check the type of the rhs */
1771 if (!IsClassInt (lval2.Type)) {
1772 Error ("Integer expression expected");
1775 /* Check for const operands */
1776 if (k == 0 && lval->Flags == E_MCONST && rconst) {
1778 /* Both operands are constant, remove the generated code */
1782 /* Evaluate the result */
1783 lval->ConstVal = kcalc (tok, lval->ConstVal, lval2.ConstVal);
1785 /* Get the type of the result */
1786 lval->Type = promoteint (lval->Type, lval2.Type);
1790 /* If the right hand side is constant, and the generator function
1791 * expects the lhs in the primary, remove the push of the primary
1794 unsigned rtype = TypeOf (lval2.Type);
1797 /* Second value is constant - check for div */
1800 if (tok == TOK_DIV && lval2.ConstVal == 0) {
1801 Error ("Division by zero");
1802 } else if (tok == TOK_MOD && lval2.ConstVal == 0) {
1803 Error ("Modulo operation with zero");
1805 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1808 ltype |= CF_REG; /* Value is in register */
1812 /* Determine the type of the operation result. */
1813 type |= g_typeadjust (ltype, rtype);
1814 lval->Type = promoteint (lval->Type, lval2.Type);
1817 Gen->Func (type, lval2.ConstVal);
1818 lval->Flags = E_MEXPR;
1821 /* We have a rvalue now */
1830 static int hie_compare (GenDesc** ops, /* List of generators */
1831 ExprDesc* lval, /* parent expr's lval */
1832 int (*hienext) (ExprDesc*))
1833 /* Helper function for the compare operators */
1840 token_t tok; /* The operator token */
1842 int rconst; /* Operand is a constant */
1847 while ((Gen = FindGen (CurTok.Tok, ops)) != 0) {
1849 /* Remember the operator token, then skip it */
1853 /* Get the lhs on stack */
1854 Mark1 = GetCodePos ();
1855 ltype = TypeOf (lval->Type);
1856 if (k == 0 && lval->Flags == E_MCONST) {
1857 /* Constant value */
1858 Mark2 = GetCodePos ();
1859 g_push (ltype | CF_CONST, lval->ConstVal);
1861 /* Value not constant */
1862 exprhs (CF_NONE, k, lval);
1863 Mark2 = GetCodePos ();
1867 /* Get the right hand side */
1868 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1870 /* Make sure, the types are compatible */
1871 if (IsClassInt (lval->Type)) {
1872 if (!IsClassInt (lval2.Type) && !(IsClassPtr(lval2.Type) && IsNullPtr(lval))) {
1873 Error ("Incompatible types");
1875 } else if (IsClassPtr (lval->Type)) {
1876 if (IsClassPtr (lval2.Type)) {
1877 /* Both pointers are allowed in comparison if they point to
1878 * the same type, or if one of them is a void pointer.
1880 type* left = Indirect (lval->Type);
1881 type* right = Indirect (lval2.Type);
1882 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1883 /* Incomatible pointers */
1884 Error ("Incompatible types");
1886 } else if (!IsNullPtr (&lval2)) {
1887 Error ("Incompatible types");
1891 /* Check for const operands */
1892 if (k == 0 && lval->Flags == E_MCONST && rconst) {
1894 /* Both operands are constant, remove the generated code */
1898 /* Evaluate the result */
1899 lval->ConstVal = kcalc (tok, lval->ConstVal, lval2.ConstVal);
1903 /* If the right hand side is constant, and the generator function
1904 * expects the lhs in the primary, remove the push of the primary
1910 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1913 ltype |= CF_REG; /* Value is in register */
1917 /* Determine the type of the operation result. If the left
1918 * operand is of type char and the right is a constant, or
1919 * if both operands are of type char, we will encode the
1920 * operation as char operation. Otherwise the default
1921 * promotions are used.
1923 if (IsTypeChar (lval->Type) && (IsTypeChar (lval2.Type) || rconst)) {
1925 if (IsSignUnsigned (lval->Type) || IsSignUnsigned (lval2.Type)) {
1926 flags |= CF_UNSIGNED;
1929 flags |= CF_FORCECHAR;
1932 unsigned rtype = TypeOf (lval2.Type) | (flags & CF_CONST);
1933 flags |= g_typeadjust (ltype, rtype);
1937 Gen->Func (flags, lval2.ConstVal);
1938 lval->Flags = E_MEXPR;
1941 /* Result type is always int */
1942 lval->Type = type_int;
1944 /* We have a rvalue now, condition codes are set */
1954 static int hie9 (ExprDesc *lval)
1955 /* Process * and / operators. */
1957 static GenDesc* hie9_ops [] = {
1958 &GenMUL, &GenDIV, &GenMOD, 0
1962 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1967 static void parseadd (int k, ExprDesc* lval)
1968 /* Parse an expression with the binary plus operator. lval contains the
1969 * unprocessed left hand side of the expression and will contain the
1970 * result of the expression on return.
1974 unsigned flags; /* Operation flags */
1975 CodeMark Mark; /* Remember code position */
1976 type* lhst; /* Type of left hand side */
1977 type* rhst; /* Type of right hand side */
1980 /* Skip the PLUS token */
1983 /* Get the left hand side type, initialize operation flags */
1987 /* Check for constness on both sides */
1988 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
1990 /* The left hand side is a constant. Good. Get rhs */
1991 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
1993 /* Right hand side is also constant. Get the rhs type */
1996 /* Both expressions are constants. Check for pointer arithmetic */
1997 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1998 /* Left is pointer, right is int, must scale rhs */
1999 lval->ConstVal = lval->ConstVal + lval2.ConstVal * PSizeOf (lhst);
2000 /* Result type is a pointer */
2001 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2002 /* Left is int, right is pointer, must scale lhs */
2003 lval->ConstVal = lval->ConstVal * PSizeOf (rhst) + lval2.ConstVal;
2004 /* Result type is a pointer */
2005 lval->Type = lval2.Type;
2006 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2007 /* Integer addition */
2008 lval->ConstVal += lval2.ConstVal;
2009 typeadjust (lval, &lval2, 1);
2012 Error ("Invalid operands for binary operator `+'");
2015 /* Result is constant, condition codes not set */
2016 lval->Test &= ~E_CC;
2020 /* lhs is constant, rhs is not. Get the rhs type. */
2023 /* Check for pointer arithmetic */
2024 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2025 /* Left is pointer, right is int, must scale rhs */
2026 g_scale (CF_INT, PSizeOf (lhst));
2027 /* Operate on pointers, result type is a pointer */
2029 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2030 /* Left is int, right is pointer, must scale lhs */
2031 lval->ConstVal *= PSizeOf (rhst);
2032 /* Operate on pointers, result type is a pointer */
2034 lval->Type = lval2.Type;
2035 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2036 /* Integer addition */
2037 flags = typeadjust (lval, &lval2, 1);
2040 Error ("Invalid operands for binary operator `+'");
2043 /* Generate code for the add */
2044 g_inc (flags | CF_CONST, lval->ConstVal);
2046 /* Result is in primary register */
2047 lval->Flags = E_MEXPR;
2048 lval->Test &= ~E_CC;
2054 /* Left hand side is not constant. Get the value onto the stack. */
2055 exprhs (CF_NONE, k, lval); /* --> primary register */
2056 Mark = GetCodePos ();
2057 g_push (TypeOf (lval->Type), 0); /* --> stack */
2059 /* Evaluate the rhs */
2060 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2062 /* Right hand side is a constant. Get the rhs type */
2065 /* Remove pushed value from stack */
2067 pop (TypeOf (lval->Type));
2069 /* Check for pointer arithmetic */
2070 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2071 /* Left is pointer, right is int, must scale rhs */
2072 lval2.ConstVal *= PSizeOf (lhst);
2073 /* Operate on pointers, result type is a pointer */
2075 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2076 /* Left is int, right is pointer, must scale lhs (ptr only) */
2077 g_scale (CF_INT | CF_CONST, PSizeOf (rhst));
2078 /* Operate on pointers, result type is a pointer */
2080 lval->Type = lval2.Type;
2081 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2082 /* Integer addition */
2083 flags = typeadjust (lval, &lval2, 1);
2086 Error ("Invalid operands for binary operator `+'");
2089 /* Generate code for the add */
2090 g_inc (flags | CF_CONST, lval2.ConstVal);
2092 /* Result is in primary register */
2093 lval->Flags = E_MEXPR;
2094 lval->Test &= ~E_CC;
2098 /* lhs and rhs are not constant. Get the rhs type. */
2101 /* Check for pointer arithmetic */
2102 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2103 /* Left is pointer, right is int, must scale rhs */
2104 g_scale (CF_INT, PSizeOf (lhst));
2105 /* Operate on pointers, result type is a pointer */
2107 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2108 /* Left is int, right is pointer, must scale lhs */
2109 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2110 g_swap (CF_INT); /* Swap TOS and primary */
2111 g_scale (CF_INT, PSizeOf (rhst));
2112 /* Operate on pointers, result type is a pointer */
2114 lval->Type = lval2.Type;
2115 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2116 /* Integer addition */
2117 flags = typeadjust (lval, &lval2, 0);
2120 Error ("Invalid operands for binary operator `+'");
2123 /* Generate code for the add */
2126 /* Result is in primary register */
2127 lval->Flags = E_MEXPR;
2128 lval->Test &= ~E_CC;
2137 static void parsesub (int k, ExprDesc* lval)
2138 /* Parse an expression with the binary minus operator. lval contains the
2139 * unprocessed left hand side of the expression and will contain the
2140 * result of the expression on return.
2144 unsigned flags; /* Operation flags */
2145 type* lhst; /* Type of left hand side */
2146 type* rhst; /* Type of right hand side */
2147 CodeMark Mark1; /* Save position of output queue */
2148 CodeMark Mark2; /* Another position in the queue */
2149 int rscale; /* Scale factor for the result */
2152 /* Skip the MINUS token */
2155 /* Get the left hand side type, initialize operation flags */
2158 rscale = 1; /* Scale by 1, that is, don't scale */
2160 /* Remember the output queue position, then bring the value onto the stack */
2161 Mark1 = GetCodePos ();
2162 exprhs (CF_NONE, k, lval); /* --> primary register */
2163 Mark2 = GetCodePos ();
2164 g_push (TypeOf (lhst), 0); /* --> stack */
2166 /* Parse the right hand side */
2167 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2169 /* The right hand side is constant. Get the rhs type. */
2172 /* Check left hand side */
2173 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
2175 /* Both sides are constant, remove generated code */
2177 pop (TypeOf (lhst)); /* Clean up the stack */
2179 /* Check for pointer arithmetic */
2180 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2181 /* Left is pointer, right is int, must scale rhs */
2182 lval->ConstVal -= lval2.ConstVal * PSizeOf (lhst);
2183 /* Operate on pointers, result type is a pointer */
2184 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2185 /* Left is pointer, right is pointer, must scale result */
2186 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2187 Error ("Incompatible pointer types");
2189 lval->ConstVal = (lval->ConstVal - lval2.ConstVal) / PSizeOf (lhst);
2191 /* Operate on pointers, result type is an integer */
2192 lval->Type = type_int;
2193 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2194 /* Integer subtraction */
2195 typeadjust (lval, &lval2, 1);
2196 lval->ConstVal -= lval2.ConstVal;
2199 Error ("Invalid operands for binary operator `-'");
2202 /* Result is constant, condition codes not set */
2203 /* lval->Flags = E_MCONST; ### */
2204 lval->Test &= ~E_CC;
2208 /* Left hand side is not constant, right hand side is.
2209 * Remove pushed value from stack.
2212 pop (TypeOf (lhst));
2214 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2215 /* Left is pointer, right is int, must scale rhs */
2216 lval2.ConstVal *= PSizeOf (lhst);
2217 /* Operate on pointers, result type is a pointer */
2219 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2220 /* Left is pointer, right is pointer, must scale result */
2221 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2222 Error ("Incompatible pointer types");
2224 rscale = PSizeOf (lhst);
2226 /* Operate on pointers, result type is an integer */
2228 lval->Type = type_int;
2229 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2230 /* Integer subtraction */
2231 flags = typeadjust (lval, &lval2, 1);
2234 Error ("Invalid operands for binary operator `-'");
2237 /* Do the subtraction */
2238 g_dec (flags | CF_CONST, lval2.ConstVal);
2240 /* If this was a pointer subtraction, we must scale the result */
2242 g_scale (flags, -rscale);
2245 /* Result is in primary register */
2246 lval->Flags = E_MEXPR;
2247 lval->Test &= ~E_CC;
2253 /* Right hand side is not constant. Get the rhs type. */
2256 /* Check for pointer arithmetic */
2257 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2258 /* Left is pointer, right is int, must scale rhs */
2259 g_scale (CF_INT, PSizeOf (lhst));
2260 /* Operate on pointers, result type is a pointer */
2262 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2263 /* Left is pointer, right is pointer, must scale result */
2264 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_EQUAL) {
2265 Error ("Incompatible pointer types");
2267 rscale = PSizeOf (lhst);
2269 /* Operate on pointers, result type is an integer */
2271 lval->Type = type_int;
2272 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2273 /* Integer subtraction. If the left hand side descriptor says that
2274 * the lhs is const, we have to remove this mark, since this is no
2275 * longer true, lhs is on stack instead.
2277 if (lval->Flags == E_MCONST) {
2278 lval->Flags = E_MEXPR;
2280 /* Adjust operand types */
2281 flags = typeadjust (lval, &lval2, 0);
2284 Error ("Invalid operands for binary operator `-'");
2287 /* Generate code for the sub (the & is a hack here) */
2288 g_sub (flags & ~CF_CONST, 0);
2290 /* If this was a pointer subtraction, we must scale the result */
2292 g_scale (flags, -rscale);
2295 /* Result is in primary register */
2296 lval->Flags = E_MEXPR;
2297 lval->Test &= ~E_CC;
2303 static int hie8 (ExprDesc* lval)
2304 /* Process + and - binary operators. */
2306 int k = hie9 (lval);
2307 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2309 if (CurTok.Tok == TOK_PLUS) {
2322 static int hie7 (ExprDesc *lval)
2323 /* Parse << and >>. */
2325 static GenDesc* hie7_ops [] = {
2330 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2335 static int hie6 (ExprDesc *lval)
2336 /* process greater-than type comparators */
2338 static GenDesc* hie6_ops [] = {
2339 &GenLT, &GenLE, &GenGE, &GenGT, 0
2341 return hie_compare (hie6_ops, lval, hie7);
2346 static int hie5 (ExprDesc *lval)
2348 static GenDesc* hie5_ops[] = {
2351 return hie_compare (hie5_ops, lval, hie6);
2356 static int hie4 (ExprDesc* lval)
2357 /* Handle & (bitwise and) */
2359 static GenDesc* hie4_ops [] = {
2364 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2369 static int hie3 (ExprDesc *lval)
2370 /* Handle ^ (bitwise exclusive or) */
2372 static GenDesc* hie3_ops [] = {
2377 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2382 static int hie2 (ExprDesc *lval)
2383 /* Handle | (bitwise or) */
2385 static GenDesc* hie2_ops [] = {
2390 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2395 static int hieAnd (ExprDesc* lval, unsigned TrueLab, int* BoolOp)
2396 /* Process "exp && exp" */
2403 if (CurTok.Tok == TOK_BOOL_AND) {
2405 /* Tell our caller that we're evaluating a boolean */
2408 /* Get a label that we will use for false expressions */
2409 lab = GetLocalLabel ();
2411 /* If the expr hasn't set condition codes, set the force-test flag */
2412 if ((lval->Test & E_CC) == 0) {
2413 lval->Test |= E_FORCETEST;
2416 /* Load the value */
2417 exprhs (CF_FORCECHAR, k, lval);
2419 /* Generate the jump */
2420 g_falsejump (CF_NONE, lab);
2422 /* Parse more boolean and's */
2423 while (CurTok.Tok == TOK_BOOL_AND) {
2430 if ((lval2.Test & E_CC) == 0) {
2431 lval2.Test |= E_FORCETEST;
2433 exprhs (CF_FORCECHAR, k, &lval2);
2435 /* Do short circuit evaluation */
2436 if (CurTok.Tok == TOK_BOOL_AND) {
2437 g_falsejump (CF_NONE, lab);
2439 /* Last expression - will evaluate to true */
2440 g_truejump (CF_NONE, TrueLab);
2444 /* Define the false jump label here */
2445 g_defcodelabel (lab);
2447 /* Define the label */
2448 lval->Flags = E_MEXPR;
2449 lval->Test |= E_CC; /* Condition codes are set */
2457 static int hieOr (ExprDesc *lval)
2458 /* Process "exp || exp". */
2462 int BoolOp = 0; /* Did we have a boolean op? */
2463 int AndOp; /* Did we have a && operation? */
2464 unsigned TrueLab; /* Jump to this label if true */
2468 TrueLab = GetLocalLabel ();
2470 /* Call the next level parser */
2471 k = hieAnd (lval, TrueLab, &BoolOp);
2473 /* Any boolean or's? */
2474 if (CurTok.Tok == TOK_BOOL_OR) {
2476 /* If the expr hasn't set condition codes, set the force-test flag */
2477 if ((lval->Test & E_CC) == 0) {
2478 lval->Test |= E_FORCETEST;
2481 /* Get first expr */
2482 exprhs (CF_FORCECHAR, k, lval);
2484 /* For each expression jump to TrueLab if true. Beware: If we
2485 * had && operators, the jump is already in place!
2488 g_truejump (CF_NONE, TrueLab);
2491 /* Remember that we had a boolean op */
2494 /* while there's more expr */
2495 while (CurTok.Tok == TOK_BOOL_OR) {
2502 k = hieAnd (&lval2, TrueLab, &AndOp);
2503 if ((lval2.Test & E_CC) == 0) {
2504 lval2.Test |= E_FORCETEST;
2506 exprhs (CF_FORCECHAR, k, &lval2);
2508 /* If there is more to come, add shortcut boolean eval.
2509 * Beware: If we had && operators, the jump is already
2513 /* Seems this sometimes generates wrong code */
2514 if (CurTok.Tok == TOK_BOOL_OR && !AndOp) {
2515 g_truejump (CF_NONE, TrueLab);
2518 g_truejump (CF_NONE, TrueLab);
2521 lval->Flags = E_MEXPR;
2522 lval->Test |= E_CC; /* Condition codes are set */
2526 /* If we really had boolean ops, generate the end sequence */
2528 DoneLab = GetLocalLabel ();
2529 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2530 g_falsejump (CF_NONE, DoneLab);
2531 g_defcodelabel (TrueLab);
2532 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2533 g_defcodelabel (DoneLab);
2540 static int hieQuest (ExprDesc *lval)
2541 /* Parse "lvalue ? exp : exp" */
2546 ExprDesc lval2; /* Expression 2 */
2547 ExprDesc lval3; /* Expression 3 */
2548 type* type2; /* Type of expression 2 */
2549 type* type3; /* Type of expression 3 */
2550 type* rtype; /* Type of result */
2551 CodeMark Mark1; /* Save position in output code */
2552 CodeMark Mark2; /* Save position in output code */
2557 if (CurTok.Tok == TOK_QUEST) {
2559 if ((lval->Test & E_CC) == 0) {
2560 /* Condition codes not set, force a test */
2561 lval->Test |= E_FORCETEST;
2563 exprhs (CF_NONE, k, lval);
2564 labf = GetLocalLabel ();
2565 g_falsejump (CF_NONE, labf);
2567 /* Parse second and third expression */
2568 expression1 (&lval2);
2569 labt = GetLocalLabel ();
2572 g_defcodelabel (labf);
2573 expression1 (&lval3);
2575 /* Check if any conversions are needed, if so, do them.
2576 * Conversion rules for ?: expression are:
2577 * - if both expressions are int expressions, default promotion
2578 * rules for ints apply.
2579 * - if both expressions are pointers of the same type, the
2580 * result of the expression is of this type.
2581 * - if one of the expressions is a pointer and the other is
2582 * a zero constant, the resulting type is that of the pointer
2584 * - all other cases are flagged by an error.
2588 if (IsClassInt (type2) && IsClassInt (type3)) {
2590 /* Get common type */
2591 rtype = promoteint (type2, type3);
2593 /* Convert the third expression to this type if needed */
2594 g_typecast (TypeOf (rtype), TypeOf (type3));
2596 /* Setup a new label so that the expr3 code will jump around
2597 * the type cast code for expr2.
2599 labf = GetLocalLabel (); /* Get new label */
2600 Mark1 = GetCodePos (); /* Remember current position */
2601 g_jump (labf); /* Jump around code */
2603 /* The jump for expr2 goes here */
2604 g_defcodelabel (labt);
2606 /* Create the typecast code for expr2 */
2607 Mark2 = GetCodePos (); /* Remember position */
2608 g_typecast (TypeOf (rtype), TypeOf (type2));
2610 /* Jump here around the typecase code. */
2611 g_defcodelabel (labf);
2612 labt = 0; /* Mark other label as invalid */
2614 } else if (IsClassPtr (type2) && IsClassPtr (type3)) {
2615 /* Must point to same type */
2616 if (TypeCmp (Indirect (type2), Indirect (type3)) < TC_EQUAL) {
2617 Error ("Incompatible pointer types");
2619 /* Result has the common type */
2621 } else if (IsClassPtr (type2) && IsNullPtr (&lval3)) {
2622 /* Result type is pointer, no cast needed */
2624 } else if (IsNullPtr (&lval2) && IsClassPtr (type3)) {
2625 /* Result type is pointer, no cast needed */
2628 Error ("Incompatible types");
2629 rtype = lval2.Type; /* Doesn't matter here */
2632 /* If we don't have the label defined until now, do it */
2634 g_defcodelabel (labt);
2637 /* Setup the target expression */
2638 lval->Flags = E_MEXPR;
2647 static void opeq (GenDesc* Gen, ExprDesc *lval, int k)
2648 /* Process "op=" operators. */
2657 Error ("Invalid lvalue in assignment");
2661 /* Determine the type of the lhs */
2662 flags = TypeOf (lval->Type);
2663 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2664 lval->Type [0] == T_PTR;
2666 /* Get the lhs address on stack (if needed) */
2669 /* Fetch the lhs into the primary register if needed */
2670 exprhs (CF_NONE, k, lval);
2672 /* Bring the lhs on stack */
2673 Mark = GetCodePos ();
2676 /* Evaluate the rhs */
2677 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2678 /* The resulting value is a constant. If the generator has the NOPUSH
2679 * flag set, don't push the lhs.
2681 if (Gen->Flags & GEN_NOPUSH) {
2686 /* lhs is a pointer, scale rhs */
2687 lval2.ConstVal *= SizeOf (lval->Type+1);
2690 /* If the lhs is character sized, the operation may be later done
2693 if (SizeOf (lval->Type) == 1) {
2694 flags |= CF_FORCECHAR;
2697 /* Special handling for add and sub - some sort of a hack, but short code */
2698 if (Gen->Func == g_add) {
2699 g_inc (flags | CF_CONST, lval2.ConstVal);
2700 } else if (Gen->Func == g_sub) {
2701 g_dec (flags | CF_CONST, lval2.ConstVal);
2703 Gen->Func (flags | CF_CONST, lval2.ConstVal);
2706 /* rhs is not constant and already in the primary register */
2708 /* lhs is a pointer, scale rhs */
2709 g_scale (TypeOf (lval2.Type), SizeOf (lval->Type+1));
2712 /* If the lhs is character sized, the operation may be later done
2715 if (SizeOf (lval->Type) == 1) {
2716 flags |= CF_FORCECHAR;
2719 /* Adjust the types of the operands if needed */
2720 Gen->Func (g_typeadjust (flags, TypeOf (lval2.Type)), 0);
2723 lval->Flags = E_MEXPR;
2728 static void addsubeq (GenDesc* Gen, ExprDesc *lval, int k)
2729 /* Process the += and -= operators */
2737 Error ("Invalid lvalue in assignment");
2742 /* We're currently only able to handle some adressing modes */
2743 if ((lval->Flags & E_MGLOBAL) == 0 && /* Global address? */
2744 (lval->Flags & E_MLOCAL) == 0 && /* Local address? */
2745 (lval->Flags & E_MCONST) == 0) { /* Constant address? */
2746 /* Use generic routine */
2747 opeq (Gen, lval, k);
2751 /* Skip the operator */
2754 /* Check if we have a pointer expression and must scale rhs */
2755 MustScale = (lval->Type [0] == T_PTR);
2757 /* Determine the code generator flags */
2758 flags = TypeOf (lval->Type) | CF_FORCECHAR;
2760 /* Evaluate the rhs */
2761 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2762 /* The resulting value is a constant. */
2764 /* lhs is a pointer, scale rhs */
2765 lval2.ConstVal *= SizeOf (lval->Type+1);
2769 /* rhs is not constant and already in the primary register */
2771 /* lhs is a pointer, scale rhs */
2772 g_scale (TypeOf (lval2.Type), SizeOf (lval->Type+1));
2776 /* Adjust the rhs to the lhs */
2777 g_typeadjust (flags, TypeOf (lval2.Type));
2779 /* Output apropriate code */
2780 if (lval->Flags & E_MGLOBAL) {
2781 /* Static variable */
2782 flags |= GlobalModeFlags (lval->Flags);
2783 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2784 g_addeqstatic (flags, lval->Name, lval->ConstVal, lval2.ConstVal);
2786 g_subeqstatic (flags, lval->Name, lval->ConstVal, lval2.ConstVal);
2788 } else if (lval->Flags & E_MLOCAL) {
2789 /* ref to localvar */
2790 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2791 g_addeqlocal (flags, lval->ConstVal, lval2.ConstVal);
2793 g_subeqlocal (flags, lval->ConstVal, lval2.ConstVal);
2795 } else if (lval->Flags & E_MCONST) {
2796 /* ref to absolute address */
2797 flags |= CF_ABSOLUTE;
2798 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2799 g_addeqstatic (flags, lval->ConstVal, 0, lval2.ConstVal);
2801 g_subeqstatic (flags, lval->ConstVal, 0, lval2.ConstVal);
2803 } else if (lval->Flags & E_MEXPR) {
2804 /* Address in a/x. */
2805 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2806 g_addeqind (flags, lval->ConstVal, lval2.ConstVal);
2808 g_subeqind (flags, lval->ConstVal, lval2.ConstVal);
2811 Internal ("Invalid addressing mode");
2814 /* Expression is in the primary now */
2815 lval->Flags = E_MEXPR;
2820 static void Assignment (ExprDesc* lval)
2821 /* Parse an assignment */
2826 type* ltype = lval->Type;
2828 /* Check for assignment to const */
2829 if (IsQualConst (ltype)) {
2830 Error ("Assignment to const");
2833 /* cc65 does not have full support for handling structs by value. Since
2834 * assigning structs is one of the more useful operations from this
2835 * family, allow it here.
2837 if (IsClassStruct (ltype)) {
2839 /* Bring the address of the lhs into the primary and push it */
2840 exprhs (0, 0, lval);
2841 g_push (CF_PTR | CF_UNSIGNED, 0);
2843 /* Get the expression on the right of the '=' into the primary */
2846 /* Get the address */
2847 exprhs (0, 0, &lval2);
2849 /* We need an lvalue */
2850 Error ("Invalid lvalue in assignment");
2853 /* Push the address (or whatever is in ax in case of errors) */
2854 g_push (CF_PTR | CF_UNSIGNED, 0);
2856 /* Check for equality of the structs */
2857 if (TypeCmp (ltype, lval2.Type) < TC_EQUAL) {
2858 Error ("Incompatible types");
2861 /* Load the size of the struct into the primary */
2862 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, SizeOf (ltype), 0);
2864 /* Call the memcpy function */
2865 g_call (CF_FIXARGC, "memcpy", 4);
2869 /* Get the address on stack if needed */
2872 /* No struct, setup flags for the load */
2873 flags = SizeOf (ltype) == 1? CF_FORCECHAR : CF_NONE;
2875 /* Get the expression on the right of the '=' into the primary */
2876 if (evalexpr (flags, hie1, &lval2) == 0) {
2877 /* Constant expression. Adjust the types */
2878 assignadjust (ltype, &lval2);
2879 /* Put the value into the primary register */
2880 lconst (flags, &lval2);
2882 /* Expression is not constant and already in the primary */
2883 assignadjust (ltype, &lval2);
2886 /* Generate a store instruction */
2891 /* Value is still in primary */
2892 lval->Flags = E_MEXPR;
2897 int hie1 (ExprDesc* lval)
2898 /* Parse first level of expression hierarchy. */
2902 k = hieQuest (lval);
2903 switch (CurTok.Tok) {
2912 Error ("Invalid lvalue in assignment");
2918 case TOK_PLUS_ASSIGN:
2919 addsubeq (&GenPASGN, lval, k);
2922 case TOK_MINUS_ASSIGN:
2923 addsubeq (&GenSASGN, lval, k);
2926 case TOK_MUL_ASSIGN:
2927 opeq (&GenMASGN, lval, k);
2930 case TOK_DIV_ASSIGN:
2931 opeq (&GenDASGN, lval, k);
2934 case TOK_MOD_ASSIGN:
2935 opeq (&GenMOASGN, lval, k);
2938 case TOK_SHL_ASSIGN:
2939 opeq (&GenSLASGN, lval, k);
2942 case TOK_SHR_ASSIGN:
2943 opeq (&GenSRASGN, lval, k);
2946 case TOK_AND_ASSIGN:
2947 opeq (&GenAASGN, lval, k);
2950 case TOK_XOR_ASSIGN:
2951 opeq (&GenXOASGN, lval, k);
2955 opeq (&GenOASGN, lval, k);
2966 int hie0 (ExprDesc *lval)
2967 /* Parse comma operator. */
2972 while (CurTok.Tok == TOK_COMMA) {
2981 int evalexpr (unsigned flags, int (*f) (ExprDesc*), ExprDesc* lval)
2982 /* Will evaluate an expression via the given function. If the result is a
2983 * constant, 0 is returned and the value is put in the lval struct. If the
2984 * result is not constant, exprhs is called to bring the value into the
2985 * primary register and 1 is returned.
2992 if (k == 0 && lval->Flags == E_MCONST) {
2993 /* Constant expression */
2996 /* Not constant, load into the primary */
2997 exprhs (flags, k, lval);
3004 int expr (int (*func) (ExprDesc*), ExprDesc *lval)
3005 /* Expression parser; func is either hie0 or hie1. */
3014 /* Do some checks if code generation is still constistent */
3015 if (savsp != oursp) {
3017 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
3019 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
3027 void expression1 (ExprDesc* lval)
3028 /* Evaluate an expression on level 1 (no comma operator) and put it into
3029 * the primary register
3032 memset (lval, 0, sizeof (*lval));
3033 exprhs (CF_NONE, expr (hie1, lval), lval);
3038 void expression (ExprDesc* lval)
3039 /* Evaluate an expression and put it into the primary register */
3041 memset (lval, 0, sizeof (*lval));
3042 exprhs (CF_NONE, expr (hie0, lval), lval);
3047 void constexpr (ExprDesc* lval)
3048 /* Get a constant value */
3050 memset (lval, 0, sizeof (*lval));
3051 if (expr (hie1, lval) != 0 || (lval->Flags & E_MCONST) == 0) {
3052 Error ("Constant expression expected");
3053 /* To avoid any compiler errors, make the expression a valid const */
3054 lval->Flags = E_MCONST;
3055 lval->Type = type_int;
3062 void intexpr (ExprDesc* lval)
3063 /* Get an integer expression */
3066 if (!IsClassInt (lval->Type)) {
3067 Error ("Integer expression expected");
3068 /* To avoid any compiler errors, make the expression a valid int */
3069 lval->Flags = E_MCONST;
3070 lval->Type = type_int;
3077 void boolexpr (ExprDesc* lval)
3078 /* Get a boolean expression */
3080 /* Read an expression */
3083 /* If it's an integer, it's ok. If it's not an integer, but a pointer,
3084 * the pointer used in a boolean context is also ok
3086 if (!IsClassInt (lval->Type) && !IsClassPtr (lval->Type)) {
3087 Error ("Boolean expression expected");
3088 /* To avoid any compiler errors, make the expression a valid int */
3089 lval->Flags = E_MCONST;
3090 lval->Type = type_int;
3097 void test (unsigned label, int cond)
3098 /* Generate code to perform test and jump if false. */
3103 /* Eat the parenthesis */
3106 /* Prepare the expression, setup labels */
3107 memset (&lval, 0, sizeof (lval));
3109 /* Generate code to eval the expr */
3110 k = expr (hie0, &lval);
3111 if (k == 0 && lval.Flags == E_MCONST) {
3112 /* Constant rvalue */
3113 if (cond == 0 && lval.ConstVal == 0) {
3115 Warning ("Unreachable code");
3116 } else if (cond && lval.ConstVal) {
3123 /* If the expr hasn't set condition codes, set the force-test flag */
3124 if ((lval.Test & E_CC) == 0) {
3125 lval.Test |= E_FORCETEST;
3128 /* Load the value into the primary register */
3129 exprhs (CF_FORCECHAR, k, &lval);
3131 /* Generate the jump */
3133 g_truejump (CF_NONE, label);
3135 /* Special case (putting this here is a small hack - but hey, the
3136 * compiler itself is one big hack...): If a semicolon follows, we
3137 * don't have a statement and may omit the jump.
3139 if (CurTok.Tok != TOK_SEMI) {
3140 g_falsejump (CF_NONE, label);
3144 /* Check for the closing brace */