4 * Ullrich von Bassewitz, 21.06.1998
21 #include "assignment.h"
40 /*****************************************************************************/
42 /*****************************************************************************/
46 /* Generator attributes */
47 #define GEN_NOPUSH 0x01 /* Don't push lhs */
49 /* Map a generator function and its attributes to a token */
51 token_t Tok; /* Token to map to */
52 unsigned Flags; /* Flags for generator function */
53 void (*Func) (unsigned, unsigned long); /* Generator func */
56 /* Descriptors for the operations */
57 static GenDesc GenMUL = { TOK_STAR, GEN_NOPUSH, g_mul };
58 static GenDesc GenDIV = { TOK_DIV, GEN_NOPUSH, g_div };
59 static GenDesc GenMOD = { TOK_MOD, GEN_NOPUSH, g_mod };
60 static GenDesc GenASL = { TOK_SHL, GEN_NOPUSH, g_asl };
61 static GenDesc GenASR = { TOK_SHR, GEN_NOPUSH, g_asr };
62 static GenDesc GenLT = { TOK_LT, GEN_NOPUSH, g_lt };
63 static GenDesc GenLE = { TOK_LE, GEN_NOPUSH, g_le };
64 static GenDesc GenGE = { TOK_GE, GEN_NOPUSH, g_ge };
65 static GenDesc GenGT = { TOK_GT, GEN_NOPUSH, g_gt };
66 static GenDesc GenEQ = { TOK_EQ, GEN_NOPUSH, g_eq };
67 static GenDesc GenNE = { TOK_NE, GEN_NOPUSH, g_ne };
68 static GenDesc GenAND = { TOK_AND, GEN_NOPUSH, g_and };
69 static GenDesc GenXOR = { TOK_XOR, GEN_NOPUSH, g_xor };
70 static GenDesc GenOR = { TOK_OR, GEN_NOPUSH, g_or };
71 static GenDesc GenPASGN = { TOK_PLUS_ASSIGN, GEN_NOPUSH, g_add };
72 static GenDesc GenSASGN = { TOK_MINUS_ASSIGN, GEN_NOPUSH, g_sub };
73 static GenDesc GenMASGN = { TOK_MUL_ASSIGN, GEN_NOPUSH, g_mul };
74 static GenDesc GenDASGN = { TOK_DIV_ASSIGN, GEN_NOPUSH, g_div };
75 static GenDesc GenMOASGN = { TOK_MOD_ASSIGN, GEN_NOPUSH, g_mod };
76 static GenDesc GenSLASGN = { TOK_SHL_ASSIGN, GEN_NOPUSH, g_asl };
77 static GenDesc GenSRASGN = { TOK_SHR_ASSIGN, GEN_NOPUSH, g_asr };
78 static GenDesc GenAASGN = { TOK_AND_ASSIGN, GEN_NOPUSH, g_and };
79 static GenDesc GenXOASGN = { TOK_XOR_ASSIGN, GEN_NOPUSH, g_xor };
80 static GenDesc GenOASGN = { TOK_OR_ASSIGN, GEN_NOPUSH, g_or };
84 /*****************************************************************************/
85 /* Function forwards */
86 /*****************************************************************************/
90 static int expr (int (*func) (ExprDesc*), ExprDesc *lval);
91 /* Expression parser; func is either hie0 or hie1. */
95 /*****************************************************************************/
96 /* Helper functions */
97 /*****************************************************************************/
101 static unsigned GlobalModeFlags (unsigned flags)
102 /* Return the addressing mode flags for the variable with the given flags */
105 if (flags == E_TGLAB) {
106 /* External linkage */
108 } else if (flags == E_TREGISTER) {
109 /* Register variable */
119 static int IsNullPtr (ExprDesc* lval)
120 /* Return true if this is the NULL pointer constant */
122 return (IsClassInt (lval->Type) && /* Is it an int? */
123 lval->Flags == E_MCONST && /* Is it constant? */
124 lval->ConstVal == 0); /* And is it's value zero? */
129 static type* promoteint (type* lhst, type* rhst)
130 /* In an expression with two ints, return the type of the result */
132 /* Rules for integer types:
133 * - If one of the values is a long, the result is long.
134 * - If one of the values is unsigned, the result is also unsigned.
135 * - Otherwise the result is an int.
137 if (IsTypeLong (lhst) || IsTypeLong (rhst)) {
138 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
144 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
154 static unsigned typeadjust (ExprDesc* lhs, ExprDesc* rhs, int NoPush)
155 /* Adjust the two values for a binary operation. lhs is expected on stack or
156 * to be constant, rhs is expected to be in the primary register or constant.
157 * The function will put the type of the result into lhs and return the
158 * code generator flags for the operation.
159 * If NoPush is given, it is assumed that the operation does not expect the lhs
160 * to be on stack, and that lhs is in a register instead.
161 * Beware: The function does only accept int types.
164 unsigned ltype, rtype;
167 /* Get the type strings */
168 type* lhst = lhs->Type;
169 type* rhst = rhs->Type;
171 /* Generate type adjustment code if needed */
172 ltype = TypeOf (lhst);
173 if (lhs->Flags == E_MCONST) {
177 /* Value is in primary register*/
180 rtype = TypeOf (rhst);
181 if (rhs->Flags == E_MCONST) {
184 flags = g_typeadjust (ltype, rtype);
186 /* Set the type of the result */
187 lhs->Type = promoteint (lhst, rhst);
189 /* Return the code generator flags */
195 unsigned assignadjust (type* lhst, ExprDesc* rhs)
196 /* Adjust the type of the right hand expression so that it can be assigned to
197 * the type on the left hand side. This function is used for assignment and
198 * for converting parameters in a function call. It returns the code generator
199 * flags for the operation. The type string of the right hand side will be
200 * set to the type of the left hand side.
203 /* Get the type of the right hand side. Treat function types as
204 * pointer-to-function
206 type* rhst = rhs->Type;
207 if (IsTypeFunc (rhst)) {
208 rhst = PointerTo (rhst);
211 /* After calling this function, rhs will have the type of the lhs */
214 /* First, do some type checking */
215 if (IsTypeVoid (lhst) || IsTypeVoid (rhst)) {
216 /* If one of the sides are of type void, output a more apropriate
219 Error ("Illegal type");
220 } else if (IsClassInt (lhst)) {
221 if (IsClassPtr (rhst)) {
222 /* Pointer -> int conversion */
223 Warning ("Converting pointer to integer without a cast");
224 } else if (!IsClassInt (rhst)) {
225 Error ("Incompatible types");
227 /* Convert the rhs to the type of the lhs. */
228 unsigned flags = TypeOf (rhst);
229 if (rhs->Flags == E_MCONST) {
232 return g_typecast (TypeOf (lhst), 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* Expr)
284 /* Output a data definition for the given expression */
286 unsigned Flags = Expr->Flags;
288 switch (Flags & E_MCTYPE) {
292 g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->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), Expr->Name, Expr->ConstVal);
311 /* a literal of some kind */
312 g_defdata (CF_STATIC, LiteralPoolLabel, Expr->ConstVal);
316 Internal ("Unknown constant type: %04X", Flags);
322 static void LoadConstant (unsigned Flags, ExprDesc* Expr)
323 /* Load the primary register with some constant value. */
325 switch (Expr->Flags & E_MCTYPE) {
328 g_leasp (Expr->ConstVal);
332 /* Number constant */
333 g_getimmed (Flags | TypeOf (Expr->Type) | CF_CONST, Expr->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 (Expr->Flags);
350 g_getimmed (Flags, Expr->Name, Expr->ConstVal);
355 g_getimmed (CF_STATIC, LiteralPoolLabel, Expr->ConstVal);
359 Internal ("Unknown constant type: %04X", Expr->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 const GenDesc* FindGen (token_t Tok, const GenDesc** Table)
414 /* Find a token in a generator table */
417 while ((G = *Table) != 0) {
428 static int istypeexpr (void)
429 /* Return true if some sort of variable or type is waiting (helper for cast
430 * and sizeof() in hie10).
435 return CurTok.Tok == TOK_LPAREN && (
436 (NextTok.Tok >= TOK_FIRSTTYPE && NextTok.Tok <= TOK_LASTTYPE) ||
437 (NextTok.Tok == TOK_CONST) ||
438 (NextTok.Tok == TOK_IDENT &&
439 (Entry = FindSym (NextTok.Ident)) != 0 &&
445 void PushAddr (ExprDesc* 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->Flags != E_MREG && (lval->Flags & E_MEXPR)) {
454 /* Push the address (always a pointer) */
461 void ConstSubExpr (int (*F) (ExprDesc*), ExprDesc* Expr)
462 /* Will evaluate an expression via the given function. If the result is not
463 * a constant, a diagnostic will be printed, and the value is replaced by
464 * a constant one to make sure there are no internal errors that result
465 * from this input error.
468 memset (Expr, 0, sizeof (*Expr));
469 if (F (Expr) != 0 || Expr->Flags != E_MCONST) {
470 Error ("Constant expression expected");
471 /* To avoid any compiler errors, make the expression a valid const */
472 MakeConstIntExpr (Expr, 1);
478 /*****************************************************************************/
480 /*****************************************************************************/
484 void exprhs (unsigned flags, int k, ExprDesc *lval)
485 /* Put the result of an expression into the primary register */
491 /* Dereferenced lvalue */
492 flags |= TypeOf (lval->Type);
493 if (lval->Test & E_FORCETEST) {
495 lval->Test &= ~E_FORCETEST;
497 if (f & E_MGLOBAL) { /* ref to globalvar */
499 flags |= GlobalModeFlags (f);
500 g_getstatic (flags, lval->Name, lval->ConstVal);
501 } else if (f & E_MLOCAL) {
502 /* ref to localvar */
503 g_getlocal (flags, lval->ConstVal);
504 } else if (f & E_MCONST) {
505 /* ref to absolute address */
506 g_getstatic (flags | CF_ABSOLUTE, lval->ConstVal, 0);
507 } else if (f == E_MEOFFS) {
508 g_getind (flags, lval->ConstVal);
509 } else if (f != E_MREG) {
512 } else if (f == E_MEOFFS) {
513 /* reference not storable */
514 flags |= TypeOf (lval->Type);
515 g_inc (flags | CF_CONST, lval->ConstVal);
516 } else if ((f & E_MEXPR) == 0) {
517 /* Constant of some sort, load it into the primary */
518 LoadConstant (flags, lval);
520 /* Are we testing this value? */
521 if (lval->Test & E_FORCETEST) {
522 /* Yes, force a test */
523 flags |= TypeOf (lval->Type);
525 lval->Test &= ~E_FORCETEST;
531 static unsigned FunctionParamList (FuncDesc* Func)
532 /* Parse a function parameter list and pass the parameters to the called
533 * function. Depending on several criteria this may be done by just pushing
534 * each parameter separately, or creating the parameter frame once and then
535 * storing into this frame.
536 * The function returns the size of the parameters pushed.
541 /* Initialize variables */
542 SymEntry* Param = 0; /* Keep gcc silent */
543 unsigned ParamSize = 0; /* Size of parameters pushed */
544 unsigned ParamCount = 0; /* Number of parameters pushed */
545 unsigned FrameSize = 0; /* Size of parameter frame */
546 unsigned FrameParams = 0; /* Number of params in frame */
547 int FrameOffs = 0; /* Offset into parameter frame */
548 int Ellipsis = 0; /* Function is variadic */
550 /* As an optimization, we may allocate the complete parameter frame at
551 * once instead of pushing each parameter as it comes. We may do that,
554 * - optimizations that increase code size are enabled (allocating the
555 * stack frame at once gives usually larger code).
556 * - we have more than one parameter to push (don't count the last param
557 * for __fastcall__ functions).
559 if (CodeSizeFactor >= 200) {
561 /* Calculate the number and size of the parameters */
562 FrameParams = Func->ParamCount;
563 FrameSize = Func->ParamSize;
564 if (FrameParams > 0 && (Func->Flags & FD_FASTCALL) != 0) {
565 /* Last parameter is not pushed */
566 const SymEntry* LastParam = Func->SymTab->SymTail;
567 FrameSize -= CheckedSizeOf (LastParam->Type);
571 /* Do we have more than one parameter in the frame? */
572 if (FrameParams > 1) {
573 /* Okeydokey, setup the frame */
578 /* Don't use a preallocated frame */
583 /* Parse the actual parameter list */
584 while (CurTok.Tok != TOK_RPAREN) {
589 /* Count arguments */
592 /* Fetch the pointer to the next argument, check for too many args */
593 if (ParamCount <= Func->ParamCount) {
594 /* Beware: If there are parameters with identical names, they
595 * cannot go into the same symbol table, which means that in this
596 * case of errorneous input, the number of nodes in the symbol
597 * table and ParamCount are NOT equal. We have to handle this case
598 * below to avoid segmentation violations. Since we know that this
599 * problem can only occur if there is more than one parameter,
600 * we will just use the last one.
602 if (ParamCount == 1) {
604 Param = Func->SymTab->SymHead;
605 } else if (Param->NextSym != 0) {
607 Param = Param->NextSym;
608 CHECK ((Param->Flags & SC_PARAM) != 0);
610 } else if (!Ellipsis) {
611 /* Too many arguments. Do we have an open param list? */
612 if ((Func->Flags & FD_VARIADIC) == 0) {
613 /* End of param list reached, no ellipsis */
614 Error ("Too many arguments in function call");
616 /* Assume an ellipsis even in case of errors to avoid an error
617 * message for each other argument.
622 /* Do some optimization: If we have a constant value to push,
623 * use a special function that may optimize.
626 if (!Ellipsis && CheckedSizeOf (Param->Type) == 1) {
627 CFlags = CF_FORCECHAR;
630 if (evalexpr (CFlags, hie1, &lval) == 0) {
631 /* A constant value */
635 /* If we don't have an argument spec, accept anything, otherwise
636 * convert the actual argument to the type needed.
639 /* Promote the argument if needed */
640 assignadjust (Param->Type, &lval);
642 /* If we have a prototype, chars may be pushed as chars */
643 Flags |= CF_FORCECHAR;
646 /* Use the type of the argument for the push */
647 Flags |= TypeOf (lval.Type);
649 /* If this is a fastcall function, don't push the last argument */
650 if (ParamCount == Func->ParamCount && (Func->Flags & FD_FASTCALL) != 0) {
651 /* Just load the argument into the primary. This is only needed if
652 * we have a constant argument, otherwise the value is already in
655 if (Flags & CF_CONST) {
656 exprhs (CF_FORCECHAR, 0, &lval);
659 unsigned ArgSize = sizeofarg (Flags);
661 /* We have the space already allocated, store in the frame */
662 CHECK (FrameSize >= ArgSize);
663 FrameSize -= ArgSize;
664 FrameOffs -= ArgSize;
666 g_putlocal (Flags | CF_NOKEEP, FrameOffs, lval.ConstVal);
668 /* Push the argument */
669 g_push (Flags, lval.ConstVal);
672 /* Calculate total parameter size */
673 ParamSize += ArgSize;
676 /* Check for end of argument list */
677 if (CurTok.Tok != TOK_COMMA) {
683 /* Check if we had enough parameters */
684 if (ParamCount < Func->ParamCount) {
685 Error ("Too few arguments in function call");
688 /* The function returns the size of all parameters pushed onto the stack.
689 * However, if there are parameters missing (which is an error and was
690 * flagged by the compiler) AND a stack frame was preallocated above,
691 * we would loose track of the stackpointer and generate an internal error
692 * later. So we correct the value by the parameters that should have been
693 * pushed to avoid an internal compiler error. Since an error was
694 * generated before, no code will be output anyway.
696 return ParamSize + FrameSize;
701 static void FunctionCall (int k, ExprDesc* lval)
702 /* Perform a function call. */
704 FuncDesc* Func; /* Function descriptor */
705 int IsFuncPtr; /* Flag */
706 unsigned ParamSize; /* Number of parameter bytes */
707 CodeMark Mark = 0; /* Initialize to keep gcc silent */
708 int PtrOffs = 0; /* Offset of function pointer on stack */
709 int IsFastCall = 0; /* True if it's a fast call function */
710 int PtrOnStack = 0; /* True if a pointer copy is on stack */
712 /* Get a pointer to the function descriptor from the type string */
713 Func = GetFuncDesc (lval->Type);
715 /* Handle function pointers transparently */
716 IsFuncPtr = IsTypeFuncPtr (lval->Type);
719 /* Check wether it's a fastcall function that has parameters */
720 IsFastCall = IsFastCallFunc (lval->Type + 1) && (Func->ParamCount > 0);
722 /* Things may be difficult, depending on where the function pointer
723 * resides. If the function pointer is an expression of some sort
724 * (not a local or global variable), we have to evaluate this
725 * expression now and save the result for later. Since calls to
726 * function pointers may be nested, we must save it onto the stack.
727 * For fastcall functions we do also need to place a copy of the
728 * pointer on stack, since we cannot use a/x.
730 PtrOnStack = IsFastCall || ((lval->Flags & (E_MGLOBAL | E_MLOCAL)) == 0);
733 /* Not a global or local variable, or a fastcall function. Load
734 * the pointer into the primary and mark it as an expression.
736 exprhs (CF_NONE, k, lval);
737 lval->Flags |= E_MEXPR;
739 /* Remember the code position */
740 Mark = GetCodePos ();
742 /* Push the pointer onto the stack and remember the offset */
747 /* Check for known standard functions and inline them if requested */
748 } else if (InlineStdFuncs && IsStdFunc ((const char*) lval->Name)) {
750 /* Inline this function */
751 HandleStdFunc (Func, lval);
756 /* Parse the parameter list */
757 ParamSize = FunctionParamList (Func);
759 /* We need the closing paren here */
762 /* Special handling for function pointers */
765 /* If the function is not a fastcall function, load the pointer to
766 * the function into the primary.
770 /* Not a fastcall function - we may use the primary */
772 /* If we have no parameters, the pointer is still in the
773 * primary. Remove the code to push it and correct the
776 if (ParamSize == 0) {
781 /* Load from the saved copy */
782 g_getlocal (CF_PTR, PtrOffs);
785 /* Load from original location */
786 exprhs (CF_NONE, k, lval);
789 /* Call the function */
790 g_callind (TypeOf (lval->Type+1), ParamSize, PtrOffs);
794 /* Fastcall function. We cannot use the primary for the function
795 * pointer and must therefore use an offset to the stack location.
796 * Since fastcall functions may never be variadic, we can use the
797 * index register for this purpose.
799 g_callind (CF_LOCAL, ParamSize, PtrOffs);
802 /* If we have a pointer on stack, remove it */
804 g_space (- (int) sizeofarg (CF_PTR));
813 /* Normal function */
814 g_call (TypeOf (lval->Type), (const char*) lval->Name, ParamSize);
821 static int primary (ExprDesc* lval)
822 /* This is the lowest level of the expression parser. */
826 /* Initialize fields in the expression stucture */
827 lval->Test = 0; /* No test */
828 lval->Sym = 0; /* Symbol unknown */
830 /* Character and integer constants. */
831 if (CurTok.Tok == TOK_ICONST || CurTok.Tok == TOK_CCONST) {
832 lval->Flags = E_MCONST | E_TCONST;
833 lval->Type = CurTok.Type;
834 lval->ConstVal = CurTok.IVal;
839 /* Process parenthesized subexpression by calling the whole parser
842 if (CurTok.Tok == TOK_LPAREN) {
844 memset (lval, 0, sizeof (*lval)); /* Remove any attributes */
850 /* If we run into an identifier in preprocessing mode, we assume that this
851 * is an undefined macro and replace it by a constant value of zero.
853 if (Preprocessing && CurTok.Tok == TOK_IDENT) {
854 MakeConstIntExpr (lval, 0);
858 /* All others may only be used if the expression evaluation is not called
859 * recursively by the preprocessor.
862 /* Illegal expression in PP mode */
863 Error ("Preprocessor expression expected");
864 MakeConstIntExpr (lval, 1);
869 if (CurTok.Tok == TOK_IDENT) {
874 /* Get a pointer to the symbol table entry */
875 Sym = lval->Sym = FindSym (CurTok.Ident);
877 /* Is the symbol known? */
880 /* We found the symbol - skip the name token */
883 /* The expression type is the symbol type */
884 lval->Type = Sym->Type;
886 /* Check for illegal symbol types */
887 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
888 if (Sym->Flags & SC_TYPE) {
889 /* Cannot use type symbols */
890 Error ("Variable identifier expected");
891 /* Assume an int type to make lval valid */
892 lval->Flags = E_MLOCAL | E_TLOFFS;
893 lval->Type = type_int;
898 /* Check for legal symbol types */
899 if ((Sym->Flags & SC_CONST) == SC_CONST) {
900 /* Enum or some other numeric constant */
901 lval->Flags = E_MCONST;
902 lval->ConstVal = Sym->V.ConstVal;
904 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
906 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
907 lval->Name = (unsigned long) Sym->Name;
909 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
910 /* Local variable. If this is a parameter for a variadic
911 * function, we have to add some address calculations, and the
912 * address is not const.
914 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
915 /* Variadic parameter */
916 g_leavariadic (Sym->V.Offs - F_GetParamSize (CurrentFunc));
917 lval->Flags = E_MEXPR;
920 /* Normal parameter */
921 lval->Flags = E_MLOCAL | E_TLOFFS;
922 lval->ConstVal = Sym->V.Offs;
924 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
925 /* Static variable */
926 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
927 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
928 lval->Name = (unsigned long) Sym->Name;
930 lval->Flags = E_MGLOBAL | E_MCONST | E_TLLAB;
931 lval->Name = Sym->V.Label;
934 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
935 /* Register variable, zero page based */
936 lval->Flags = E_MGLOBAL | E_MCONST | E_TREGISTER;
937 lval->Name = Sym->V.Offs;
940 /* Local static variable */
941 lval->Flags = E_MGLOBAL | E_MCONST | E_TLLAB;
942 lval->Name = Sym->V.Offs;
946 /* The symbol is referenced now */
947 Sym->Flags |= SC_REF;
948 if (IsTypeFunc (lval->Type) || IsTypeArray (lval->Type)) {
954 /* We did not find the symbol. Remember the name, then skip it */
955 strcpy (Ident, CurTok.Ident);
958 /* IDENT is either an auto-declared function or an undefined variable. */
959 if (CurTok.Tok == TOK_LPAREN) {
960 /* Declare a function returning int. For that purpose, prepare a
961 * function signature for a function having an empty param list
964 Warning ("Function call without a prototype");
965 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
966 lval->Type = Sym->Type;
967 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
968 lval->Name = (unsigned long) Sym->Name;
974 /* Undeclared Variable */
975 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
976 lval->Flags = E_MLOCAL | E_TLOFFS;
977 lval->Type = type_int;
979 Error ("Undefined symbol: `%s'", Ident);
985 /* String literal? */
986 if (CurTok.Tok == TOK_SCONST) {
987 lval->Flags = E_MCONST | E_TLIT;
988 lval->ConstVal = CurTok.IVal;
989 lval->Type = GetCharArrayType (GetLiteralPoolOffs () - CurTok.IVal);
995 if (CurTok.Tok == TOK_ASM) {
997 lval->Type = type_void;
998 lval->Flags = E_MEXPR;
1003 /* __AX__ and __EAX__ pseudo values? */
1004 if (CurTok.Tok == TOK_AX || CurTok.Tok == TOK_EAX) {
1005 lval->Type = (CurTok.Tok == TOK_AX)? type_uint : type_ulong;
1006 lval->Flags = E_MREG;
1007 lval->Test &= ~E_CC;
1010 return 1; /* May be used as lvalue */
1013 /* Illegal primary. */
1014 Error ("Expression expected");
1015 MakeConstIntExpr (lval, 1);
1021 static int arrayref (int k, ExprDesc* lval)
1022 /* Handle an array reference */
1036 /* Skip the bracket */
1039 /* Get the type of left side */
1042 /* We can apply a special treatment for arrays that have a const base
1043 * address. This is true for most arrays and will produce a lot better
1044 * code. Check if this is a const base address.
1046 lflags = lval->Flags & ~E_MCTYPE;
1047 ConstBaseAddr = (lflags == E_MCONST) || /* Constant numeric address */
1048 (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1049 lflags == E_MLOCAL; /* Local array */
1051 /* If we have a constant base, we delay the address fetch */
1052 Mark1 = GetCodePos ();
1053 Mark2 = 0; /* Silence gcc */
1054 if (!ConstBaseAddr) {
1055 /* Get a pointer to the array into the primary */
1056 exprhs (CF_NONE, k, lval);
1058 /* Get the array pointer on stack. Do not push more than 16
1059 * bit, even if this value is greater, since we cannot handle
1060 * other than 16bit stuff when doing indexing.
1062 Mark2 = GetCodePos ();
1066 /* TOS now contains ptr to array elements. Get the subscript. */
1068 if (l == 0 && lval2.Flags == E_MCONST) {
1070 /* The array subscript is a constant - remove value from stack */
1071 if (!ConstBaseAddr) {
1075 /* Get an array pointer into the primary */
1076 exprhs (CF_NONE, k, lval);
1079 if (IsClassPtr (tptr1)) {
1081 /* Scale the subscript value according to element size */
1082 lval2.ConstVal *= CheckedPSizeOf (tptr1);
1084 /* Remove code for lhs load */
1087 /* Handle constant base array on stack. Be sure NOT to
1088 * handle pointers the same way, this won't work.
1090 if (IsTypeArray (tptr1) &&
1091 ((lval->Flags & ~E_MCTYPE) == E_MCONST ||
1092 (lval->Flags & ~E_MCTYPE) == E_MLOCAL ||
1093 (lval->Flags & E_MGLOBAL) != 0 ||
1094 (lval->Flags == E_MEOFFS))) {
1095 lval->ConstVal += lval2.ConstVal;
1098 /* Pointer - load into primary and remember offset */
1099 if ((lval->Flags & E_MEXPR) == 0 || k != 0) {
1100 exprhs (CF_NONE, k, lval);
1102 lval->ConstVal = lval2.ConstVal;
1103 lval->Flags = E_MEOFFS;
1106 /* Result is of element type */
1107 lval->Type = Indirect (tptr1);
1112 } else if (IsClassPtr (tptr2 = lval2.Type)) {
1113 /* Subscript is pointer, get element type */
1114 lval2.Type = Indirect (tptr2);
1116 /* Scale the rhs value in the primary register */
1117 g_scale (TypeOf (tptr1), CheckedSizeOf (lval2.Type));
1119 lval->Type = lval2.Type;
1121 Error ("Cannot subscript");
1124 /* Add the subscript. Since arrays are indexed by integers,
1125 * we will ignore the true type of the subscript here and
1126 * use always an int.
1128 g_inc (CF_INT | CF_CONST, lval2.ConstVal);
1132 /* Array subscript is not constant. Load it into the primary */
1133 Mark2 = GetCodePos ();
1134 exprhs (CF_NONE, l, &lval2);
1137 if (IsClassPtr (tptr1)) {
1139 /* Get the element type */
1140 lval->Type = Indirect (tptr1);
1142 /* Indexing is based on int's, so we will just use the integer
1143 * portion of the index (which is in (e)ax, so there's no further
1146 g_scale (CF_INT, CheckedSizeOf (lval->Type));
1148 } else if (IsClassPtr (tptr2)) {
1150 /* Get the element type */
1151 lval2.Type = Indirect (tptr2);
1153 /* Get the int value on top. If we go here, we're sure,
1154 * both values are 16 bit (the first one was truncated
1155 * if necessary and the second one is a pointer).
1156 * Note: If ConstBaseAddr is true, we don't have a value on
1157 * stack, so to "swap" both, just push the subscript.
1159 if (ConstBaseAddr) {
1161 exprhs (CF_NONE, k, lval);
1168 g_scale (TypeOf (tptr1), CheckedSizeOf (lval2.Type));
1169 lval->Type = lval2.Type;
1171 Error ("Cannot subscript");
1174 /* The offset is now in the primary register. It didn't have a
1175 * constant base address for the lhs, the lhs address is already
1176 * on stack, and we must add the offset. If the base address was
1177 * constant, we call special functions to add the address to the
1180 if (!ConstBaseAddr) {
1181 /* Add the subscript. Both values are int sized. */
1185 /* If the subscript has itself a constant address, it is often
1186 * a better idea to reverse again the order of the evaluation.
1187 * This will generate better code if the subscript is a byte
1188 * sized variable. But beware: This is only possible if the
1189 * subscript was not scaled, that is, if this was a byte array
1192 rflags = lval2.Flags & ~E_MCTYPE;
1193 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1194 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1195 rflags == E_MLOCAL; /* Local array */
1197 if (ConstSubAddr && CheckedSizeOf (lval->Type) == 1) {
1201 /* Reverse the order of evaluation */
1202 unsigned flags = (CheckedSizeOf (lval2.Type) == 1)? CF_CHAR : CF_INT;
1205 /* Get a pointer to the array into the primary. We have changed
1206 * Type above but we need the original type to load the
1207 * address, so restore it temporarily.
1209 SavedType = lval->Type;
1211 exprhs (CF_NONE, k, lval);
1212 lval->Type = SavedType;
1214 /* Add the variable */
1215 if (rflags == E_MLOCAL) {
1216 g_addlocal (flags, lval2.ConstVal);
1218 flags |= GlobalModeFlags (lval2.Flags);
1219 g_addstatic (flags, lval2.Name, lval2.ConstVal);
1222 if (lflags == E_MCONST) {
1223 /* Constant numeric address. Just add it */
1224 g_inc (CF_INT | CF_UNSIGNED, lval->ConstVal);
1225 } else if (lflags == E_MLOCAL) {
1226 /* Base address is a local variable address */
1227 if (IsTypeArray (tptr1)) {
1228 g_addaddr_local (CF_INT, lval->ConstVal);
1230 g_addlocal (CF_PTR, lval->ConstVal);
1233 /* Base address is a static variable address */
1234 unsigned flags = CF_INT;
1235 flags |= GlobalModeFlags (lval->Flags);
1236 if (IsTypeArray (tptr1)) {
1237 g_addaddr_static (flags, lval->Name, lval->ConstVal);
1239 g_addstatic (flags, lval->Name, lval->ConstVal);
1245 lval->Flags = E_MEXPR;
1248 return !IsTypeArray (lval->Type);
1254 static int structref (int k, ExprDesc* lval)
1255 /* Process struct field after . or ->. */
1261 /* Skip the token and check for an identifier */
1263 if (CurTok.Tok != TOK_IDENT) {
1264 Error ("Identifier expected");
1265 lval->Type = type_int;
1269 /* Get the symbol table entry and check for a struct field */
1270 strcpy (Ident, CurTok.Ident);
1272 Field = FindStructField (lval->Type, Ident);
1274 Error ("Struct/union has no field named `%s'", Ident);
1275 lval->Type = type_int;
1279 /* If we have constant input data, the result is also constant */
1280 flags = lval->Flags & ~E_MCTYPE;
1281 if (flags == E_MCONST ||
1282 (k == 0 && (flags == E_MLOCAL ||
1283 (flags & E_MGLOBAL) != 0 ||
1284 lval->Flags == E_MEOFFS))) {
1285 lval->ConstVal += Field->V.Offs;
1287 if ((flags & E_MEXPR) == 0 || k != 0) {
1288 exprhs (CF_NONE, k, lval);
1290 lval->ConstVal = Field->V.Offs;
1291 lval->Flags = E_MEOFFS;
1293 lval->Type = Field->Type;
1294 return !IsTypeArray (Field->Type);
1299 static int hie11 (ExprDesc *lval)
1300 /* Handle compound types (structs and arrays) */
1307 if (CurTok.Tok < TOK_LBRACK || CurTok.Tok > TOK_PTR_REF) {
1314 if (CurTok.Tok == TOK_LBRACK) {
1316 /* Array reference */
1317 k = arrayref (k, lval);
1319 } else if (CurTok.Tok == TOK_LPAREN) {
1321 /* Function call. Skip the opening parenthesis */
1324 if (IsTypeFunc (lval->Type) || IsTypeFuncPtr (lval->Type)) {
1326 /* Call the function */
1327 FunctionCall (k, lval);
1329 /* Result is in the primary register */
1330 lval->Flags = E_MEXPR;
1333 lval->Type = GetFuncReturn (lval->Type);
1336 Error ("Illegal function call");
1340 } else if (CurTok.Tok == TOK_DOT) {
1342 if (!IsClassStruct (lval->Type)) {
1343 Error ("Struct expected");
1345 k = structref (0, lval);
1347 } else if (CurTok.Tok == TOK_PTR_REF) {
1350 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1351 Error ("Struct pointer expected");
1353 k = structref (k, lval);
1363 void Store (ExprDesc* lval, const type* StoreType)
1364 /* Store the primary register into the location denoted by lval. If StoreType
1365 * is given, use this type when storing instead of lval->Type. If StoreType
1366 * is NULL, use lval->Type instead.
1371 unsigned f = lval->Flags;
1373 /* If StoreType was not given, use lval->Type instead */
1374 if (StoreType == 0) {
1375 StoreType = lval->Type;
1378 /* Get the code generator flags */
1379 Flags = TypeOf (StoreType);
1380 if (f & E_MGLOBAL) {
1381 Flags |= GlobalModeFlags (f);
1388 g_putstatic (Flags, lval->Name, lval->ConstVal);
1390 } else if (f & E_MLOCAL) {
1391 /* Store an auto variable */
1392 g_putlocal (Flags, lval->ConstVal, 0);
1393 } else if (f == E_MEOFFS) {
1394 /* Store indirect with offset */
1395 g_putind (Flags, lval->ConstVal);
1396 } else if (f != E_MREG) {
1398 /* Indirect without offset */
1399 g_putind (Flags, 0);
1401 /* Store into absolute address */
1402 g_putstatic (Flags | CF_ABSOLUTE, lval->ConstVal, 0);
1406 /* Assume that each one of the stores will invalidate CC */
1407 lval->Test &= ~E_CC;
1412 static void pre_incdec (ExprDesc* lval, void (*inc) (unsigned, unsigned long))
1413 /* Handle --i and ++i */
1420 if ((k = hie10 (lval)) == 0) {
1421 Error ("Invalid lvalue");
1425 /* Get the data type */
1426 flags = TypeOf (lval->Type) | CF_FORCECHAR | CF_CONST;
1428 /* Get the increment value in bytes */
1429 val = (lval->Type [0] == T_PTR)? CheckedPSizeOf (lval->Type) : 1;
1431 /* We're currently only able to handle some adressing modes */
1432 if ((lval->Flags & E_MGLOBAL) == 0 && /* Global address? */
1433 (lval->Flags & E_MLOCAL) == 0 && /* Local address? */
1434 (lval->Flags & E_MCONST) == 0 && /* Constant address? */
1435 (lval->Flags & E_MEXPR) == 0) { /* Address in a/x? */
1437 /* Use generic code. Push the address if needed */
1440 /* Fetch the value */
1441 exprhs (CF_NONE, k, lval);
1443 /* Increment value in primary */
1446 /* Store the result back */
1451 /* Special code for some addressing modes - use the special += ops */
1452 if (lval->Flags & E_MGLOBAL) {
1453 flags |= GlobalModeFlags (lval->Flags);
1455 g_addeqstatic (flags, lval->Name, lval->ConstVal, val);
1457 g_subeqstatic (flags, lval->Name, lval->ConstVal, val);
1459 } else if (lval->Flags & E_MLOCAL) {
1460 /* ref to localvar */
1462 g_addeqlocal (flags, lval->ConstVal, val);
1464 g_subeqlocal (flags, lval->ConstVal, val);
1466 } else if (lval->Flags & E_MCONST) {
1467 /* ref to absolute address */
1468 flags |= CF_ABSOLUTE;
1470 g_addeqstatic (flags, lval->ConstVal, 0, val);
1472 g_subeqstatic (flags, lval->ConstVal, 0, val);
1474 } else if (lval->Flags & E_MEXPR) {
1475 /* Address in a/x, check if we have an offset */
1476 unsigned Offs = (lval->Flags == E_MEOFFS)? lval->ConstVal : 0;
1478 g_addeqind (flags, Offs, val);
1480 g_subeqind (flags, Offs, val);
1483 Internal ("Invalid addressing mode");
1488 /* Result is an expression */
1489 lval->Flags = E_MEXPR;
1494 static void post_incdec (ExprDesc* lval, int k, void (*inc) (unsigned, unsigned long))
1495 /* Handle i-- and i++ */
1501 Error ("Invalid lvalue");
1505 /* Get the data type */
1506 flags = TypeOf (lval->Type);
1508 /* Push the address if needed */
1511 /* Fetch the value and save it (since it's the result of the expression) */
1512 exprhs (CF_NONE, 1, lval);
1513 g_save (flags | CF_FORCECHAR);
1515 /* If we have a pointer expression, increment by the size of the type */
1516 if (lval->Type[0] == T_PTR) {
1517 inc (flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (lval->Type + 1));
1519 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1522 /* Store the result back */
1525 /* Restore the original value */
1526 g_restore (flags | CF_FORCECHAR);
1527 lval->Flags = E_MEXPR;
1532 static void unaryop (int tok, ExprDesc* lval)
1533 /* Handle unary -/+ and ~ */
1540 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
1541 /* Value is constant */
1543 case TOK_MINUS: lval->ConstVal = -lval->ConstVal; break;
1544 case TOK_PLUS: break;
1545 case TOK_COMP: lval->ConstVal = ~lval->ConstVal; break;
1546 default: Internal ("Unexpected token: %d", tok);
1549 /* Value is not constant */
1550 exprhs (CF_NONE, k, lval);
1552 /* Get the type of the expression */
1553 flags = TypeOf (lval->Type);
1555 /* Handle the operation */
1557 case TOK_MINUS: g_neg (flags); break;
1558 case TOK_PLUS: break;
1559 case TOK_COMP: g_com (flags); break;
1560 default: Internal ("Unexpected token: %d", tok);
1562 lval->Flags = E_MEXPR;
1568 int hie10 (ExprDesc* lval)
1569 /* Handle ++, --, !, unary - etc. */
1574 switch (CurTok.Tok) {
1577 pre_incdec (lval, g_inc);
1581 pre_incdec (lval, g_dec);
1587 unaryop (CurTok.Tok, lval);
1592 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1593 /* Constant expression */
1594 lval->ConstVal = !lval->ConstVal;
1596 g_bneg (TypeOf (lval->Type));
1597 lval->Test |= E_CC; /* bneg will set cc */
1598 lval->Flags = E_MEXPR; /* say it's an expr */
1600 return 0; /* expr not storable */
1604 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1605 /* Expression is not const, indirect value loaded into primary */
1606 lval->Flags = E_MEXPR;
1607 lval->ConstVal = 0; /* Offset is zero now */
1610 if (IsClassPtr (t)) {
1611 lval->Type = Indirect (t);
1613 Error ("Illegal indirection");
1620 /* The & operator may be applied to any lvalue, and it may be
1621 * applied to functions, even if they're no lvalues.
1623 if (k == 0 && !IsTypeFunc (lval->Type)) {
1624 /* Allow the & operator with an array */
1625 if (!IsTypeArray (lval->Type)) {
1626 Error ("Illegal address");
1629 t = TypeAlloc (TypeLen (lval->Type) + 2);
1631 TypeCpy (t + 1, lval->Type);
1638 if (istypeexpr ()) {
1639 type Type[MAXTYPELEN];
1641 lval->ConstVal = CheckedSizeOf (ParseType (Type));
1644 /* Remember the output queue pointer */
1645 CodeMark Mark = GetCodePos ();
1647 lval->ConstVal = CheckedSizeOf (lval->Type);
1648 /* Remove any generated code */
1651 lval->Flags = E_MCONST | E_TCONST;
1652 lval->Type = type_uint;
1653 lval->Test &= ~E_CC;
1657 if (istypeexpr ()) {
1659 return TypeCast (lval);
1664 switch (CurTok.Tok) {
1666 post_incdec (lval, k, g_inc);
1670 post_incdec (lval, k, g_dec);
1680 static int hie_internal (const GenDesc** ops, /* List of generators */
1681 ExprDesc* lval, /* parent expr's lval */
1682 int (*hienext) (ExprDesc*),
1683 int* UsedGen) /* next higher level */
1684 /* Helper function */
1691 token_t tok; /* The operator token */
1692 unsigned ltype, type;
1693 int rconst; /* Operand is a constant */
1699 while ((Gen = FindGen (CurTok.Tok, ops)) != 0) {
1701 /* Tell the caller that we handled it's ops */
1704 /* All operators that call this function expect an int on the lhs */
1705 if (!IsClassInt (lval->Type)) {
1706 Error ("Integer expression expected");
1709 /* Remember the operator token, then skip it */
1713 /* Get the lhs on stack */
1714 Mark1 = GetCodePos ();
1715 ltype = TypeOf (lval->Type);
1716 if (k == 0 && lval->Flags == E_MCONST) {
1717 /* Constant value */
1718 Mark2 = GetCodePos ();
1719 g_push (ltype | CF_CONST, lval->ConstVal);
1721 /* Value not constant */
1722 exprhs (CF_NONE, k, lval);
1723 Mark2 = GetCodePos ();
1727 /* Get the right hand side */
1728 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1730 /* Check the type of the rhs */
1731 if (!IsClassInt (lval2.Type)) {
1732 Error ("Integer expression expected");
1735 /* Check for const operands */
1736 if (k == 0 && lval->Flags == E_MCONST && rconst) {
1738 /* Both operands are constant, remove the generated code */
1742 /* Evaluate the result */
1743 lval->ConstVal = kcalc (tok, lval->ConstVal, lval2.ConstVal);
1745 /* Get the type of the result */
1746 lval->Type = promoteint (lval->Type, lval2.Type);
1750 /* If the right hand side is constant, and the generator function
1751 * expects the lhs in the primary, remove the push of the primary
1754 unsigned rtype = TypeOf (lval2.Type);
1757 /* Second value is constant - check for div */
1760 if (tok == TOK_DIV && lval2.ConstVal == 0) {
1761 Error ("Division by zero");
1762 } else if (tok == TOK_MOD && lval2.ConstVal == 0) {
1763 Error ("Modulo operation with zero");
1765 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1768 ltype |= CF_REG; /* Value is in register */
1772 /* Determine the type of the operation result. */
1773 type |= g_typeadjust (ltype, rtype);
1774 lval->Type = promoteint (lval->Type, lval2.Type);
1777 Gen->Func (type, lval2.ConstVal);
1778 lval->Flags = E_MEXPR;
1781 /* We have a rvalue now */
1790 static int hie_compare (const GenDesc** ops, /* List of generators */
1791 ExprDesc* lval, /* parent expr's lval */
1792 int (*hienext) (ExprDesc*))
1793 /* Helper function for the compare operators */
1800 token_t tok; /* The operator token */
1802 int rconst; /* Operand is a constant */
1807 while ((Gen = FindGen (CurTok.Tok, ops)) != 0) {
1809 /* Remember the operator token, then skip it */
1813 /* Get the lhs on stack */
1814 Mark1 = GetCodePos ();
1815 ltype = TypeOf (lval->Type);
1816 if (k == 0 && lval->Flags == E_MCONST) {
1817 /* Constant value */
1818 Mark2 = GetCodePos ();
1819 g_push (ltype | CF_CONST, lval->ConstVal);
1821 /* Value not constant */
1822 exprhs (CF_NONE, k, lval);
1823 Mark2 = GetCodePos ();
1827 /* Get the right hand side */
1828 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1830 /* Make sure, the types are compatible */
1831 if (IsClassInt (lval->Type)) {
1832 if (!IsClassInt (lval2.Type) && !(IsClassPtr(lval2.Type) && IsNullPtr(lval))) {
1833 Error ("Incompatible types");
1835 } else if (IsClassPtr (lval->Type)) {
1836 if (IsClassPtr (lval2.Type)) {
1837 /* Both pointers are allowed in comparison if they point to
1838 * the same type, or if one of them is a void pointer.
1840 type* left = Indirect (lval->Type);
1841 type* right = Indirect (lval2.Type);
1842 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1843 /* Incomatible pointers */
1844 Error ("Incompatible types");
1846 } else if (!IsNullPtr (&lval2)) {
1847 Error ("Incompatible types");
1851 /* Check for const operands */
1852 if (k == 0 && lval->Flags == E_MCONST && rconst) {
1854 /* Both operands are constant, remove the generated code */
1858 /* Evaluate the result */
1859 lval->ConstVal = kcalc (tok, lval->ConstVal, lval2.ConstVal);
1863 /* If the right hand side is constant, and the generator function
1864 * expects the lhs in the primary, remove the push of the primary
1870 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1873 ltype |= CF_REG; /* Value is in register */
1877 /* Determine the type of the operation result. If the left
1878 * operand is of type char and the right is a constant, or
1879 * if both operands are of type char, we will encode the
1880 * operation as char operation. Otherwise the default
1881 * promotions are used.
1883 if (IsTypeChar (lval->Type) && (IsTypeChar (lval2.Type) || rconst)) {
1885 if (IsSignUnsigned (lval->Type) || IsSignUnsigned (lval2.Type)) {
1886 flags |= CF_UNSIGNED;
1889 flags |= CF_FORCECHAR;
1892 unsigned rtype = TypeOf (lval2.Type) | (flags & CF_CONST);
1893 flags |= g_typeadjust (ltype, rtype);
1897 Gen->Func (flags, lval2.ConstVal);
1898 lval->Flags = E_MEXPR;
1901 /* Result type is always int */
1902 lval->Type = type_int;
1904 /* We have a rvalue now, condition codes are set */
1914 static int hie9 (ExprDesc *lval)
1915 /* Process * and / operators. */
1917 static const GenDesc* hie9_ops [] = {
1918 &GenMUL, &GenDIV, &GenMOD, 0
1922 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
1927 static void parseadd (int k, ExprDesc* lval)
1928 /* Parse an expression with the binary plus operator. lval contains the
1929 * unprocessed left hand side of the expression and will contain the
1930 * result of the expression on return.
1934 unsigned flags; /* Operation flags */
1935 CodeMark Mark; /* Remember code position */
1936 type* lhst; /* Type of left hand side */
1937 type* rhst; /* Type of right hand side */
1940 /* Skip the PLUS token */
1943 /* Get the left hand side type, initialize operation flags */
1947 /* Check for constness on both sides */
1948 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
1950 /* The left hand side is a constant. Good. Get rhs */
1952 if (k == 0 && lval2.Flags == E_MCONST) {
1954 /* Right hand side is also constant. Get the rhs type */
1957 /* Both expressions are constants. Check for pointer arithmetic */
1958 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1959 /* Left is pointer, right is int, must scale rhs */
1960 lval->ConstVal += lval2.ConstVal * CheckedPSizeOf (lhst);
1961 /* Result type is a pointer */
1962 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1963 /* Left is int, right is pointer, must scale lhs */
1964 lval->ConstVal = lval->ConstVal * CheckedPSizeOf (rhst) + lval2.ConstVal;
1965 /* Result type is a pointer */
1966 lval->Type = lval2.Type;
1967 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1968 /* Integer addition */
1969 lval->ConstVal += lval2.ConstVal;
1970 typeadjust (lval, &lval2, 1);
1973 Error ("Invalid operands for binary operator `+'");
1976 /* Result is constant, condition codes not set */
1977 lval->Test &= ~E_CC;
1981 /* lhs is a constant and rhs is not constant. Load rhs into
1984 exprhs (CF_NONE, k, &lval2);
1986 /* Beware: The check above (for lhs) lets not only pass numeric
1987 * constants, but also constant addresses (labels), maybe even
1988 * with an offset. We have to check for that here.
1991 /* First, get the rhs type. */
1995 if (lval->Flags == E_MCONST) {
1996 /* A numerical constant */
1999 /* Constant address label */
2000 flags |= GlobalModeFlags (lval->Flags) | CF_CONSTADDR;
2003 /* Check for pointer arithmetic */
2004 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2005 /* Left is pointer, right is int, must scale rhs */
2006 g_scale (CF_INT, CheckedPSizeOf (lhst));
2007 /* Operate on pointers, result type is a pointer */
2009 /* Generate the code for the add */
2010 if (lval->Flags == E_MCONST) {
2011 /* Numeric constant */
2012 g_inc (flags, lval->ConstVal);
2014 /* Constant address */
2015 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2017 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2019 /* Left is int, right is pointer, must scale lhs. */
2020 unsigned ScaleFactor = CheckedPSizeOf (rhst);
2022 /* Operate on pointers, result type is a pointer */
2024 lval->Type = lval2.Type;
2026 /* Since we do already have rhs in the primary, if lhs is
2027 * not a numeric constant, and the scale factor is not one
2028 * (no scaling), we must take the long way over the stack.
2030 if (lval->Flags == E_MCONST) {
2031 /* Numeric constant, scale lhs */
2032 lval->ConstVal *= ScaleFactor;
2033 /* Generate the code for the add */
2034 g_inc (flags, lval->ConstVal);
2035 } else if (ScaleFactor == 1) {
2036 /* Constant address but no need to scale */
2037 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2039 /* Constant address that must be scaled */
2040 g_push (TypeOf (lval2.Type), 0); /* rhs --> stack */
2041 g_getimmed (flags, lval->Name, lval->ConstVal);
2042 g_scale (CF_PTR, ScaleFactor);
2045 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2046 /* Integer addition */
2047 flags |= typeadjust (lval, &lval2, 1);
2048 /* Generate the code for the add */
2049 if (lval->Flags == E_MCONST) {
2050 /* Numeric constant */
2051 g_inc (flags, lval->ConstVal);
2053 /* Constant address */
2054 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2058 Error ("Invalid operands for binary operator `+'");
2061 /* Result is in primary register */
2062 lval->Flags = E_MEXPR;
2063 lval->Test &= ~E_CC;
2069 /* Left hand side is not constant. Get the value onto the stack. */
2070 exprhs (CF_NONE, k, lval); /* --> primary register */
2071 Mark = GetCodePos ();
2072 g_push (TypeOf (lval->Type), 0); /* --> stack */
2074 /* Evaluate the rhs */
2075 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2077 /* Right hand side is a constant. Get the rhs type */
2080 /* Remove pushed value from stack */
2082 pop (TypeOf (lval->Type));
2084 /* Check for pointer arithmetic */
2085 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2086 /* Left is pointer, right is int, must scale rhs */
2087 lval2.ConstVal *= CheckedPSizeOf (lhst);
2088 /* Operate on pointers, result type is a pointer */
2090 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2091 /* Left is int, right is pointer, must scale lhs (ptr only) */
2092 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2093 /* Operate on pointers, result type is a pointer */
2095 lval->Type = lval2.Type;
2096 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2097 /* Integer addition */
2098 flags = typeadjust (lval, &lval2, 1);
2101 Error ("Invalid operands for binary operator `+'");
2104 /* Generate code for the add */
2105 g_inc (flags | CF_CONST, lval2.ConstVal);
2107 /* Result is in primary register */
2108 lval->Flags = E_MEXPR;
2109 lval->Test &= ~E_CC;
2113 /* lhs and rhs are not constant. Get the rhs type. */
2116 /* Check for pointer arithmetic */
2117 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2118 /* Left is pointer, right is int, must scale rhs */
2119 g_scale (CF_INT, CheckedPSizeOf (lhst));
2120 /* Operate on pointers, result type is a pointer */
2122 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2123 /* Left is int, right is pointer, must scale lhs */
2124 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2125 g_swap (CF_INT); /* Swap TOS and primary */
2126 g_scale (CF_INT, CheckedPSizeOf (rhst));
2127 /* Operate on pointers, result type is a pointer */
2129 lval->Type = lval2.Type;
2130 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2131 /* Integer addition */
2132 flags = typeadjust (lval, &lval2, 0);
2135 Error ("Invalid operands for binary operator `+'");
2138 /* Generate code for the add */
2141 /* Result is in primary register */
2142 lval->Flags = E_MEXPR;
2143 lval->Test &= ~E_CC;
2152 static void parsesub (int k, ExprDesc* lval)
2153 /* Parse an expression with the binary minus operator. lval contains the
2154 * unprocessed left hand side of the expression and will contain the
2155 * result of the expression on return.
2159 unsigned flags; /* Operation flags */
2160 type* lhst; /* Type of left hand side */
2161 type* rhst; /* Type of right hand side */
2162 CodeMark Mark1; /* Save position of output queue */
2163 CodeMark Mark2; /* Another position in the queue */
2164 int rscale; /* Scale factor for the result */
2167 /* Skip the MINUS token */
2170 /* Get the left hand side type, initialize operation flags */
2173 rscale = 1; /* Scale by 1, that is, don't scale */
2175 /* Remember the output queue position, then bring the value onto the stack */
2176 Mark1 = GetCodePos ();
2177 exprhs (CF_NONE, k, lval); /* --> primary register */
2178 Mark2 = GetCodePos ();
2179 g_push (TypeOf (lhst), 0); /* --> stack */
2181 /* Parse the right hand side */
2182 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2184 /* The right hand side is constant. Get the rhs type. */
2187 /* Check left hand side */
2188 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
2190 /* Both sides are constant, remove generated code */
2192 pop (TypeOf (lhst)); /* Clean up the stack */
2194 /* Check for pointer arithmetic */
2195 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2196 /* Left is pointer, right is int, must scale rhs */
2197 lval->ConstVal -= lval2.ConstVal * CheckedPSizeOf (lhst);
2198 /* Operate on pointers, result type is a pointer */
2199 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2200 /* Left is pointer, right is pointer, must scale result */
2201 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2202 Error ("Incompatible pointer types");
2204 lval->ConstVal = (lval->ConstVal - lval2.ConstVal) /
2205 CheckedPSizeOf (lhst);
2207 /* Operate on pointers, result type is an integer */
2208 lval->Type = type_int;
2209 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2210 /* Integer subtraction */
2211 typeadjust (lval, &lval2, 1);
2212 lval->ConstVal -= lval2.ConstVal;
2215 Error ("Invalid operands for binary operator `-'");
2218 /* Result is constant, condition codes not set */
2219 /* lval->Flags = E_MCONST; ### */
2220 lval->Test &= ~E_CC;
2224 /* Left hand side is not constant, right hand side is.
2225 * Remove pushed value from stack.
2228 pop (TypeOf (lhst));
2230 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2231 /* Left is pointer, right is int, must scale rhs */
2232 lval2.ConstVal *= CheckedPSizeOf (lhst);
2233 /* Operate on pointers, result type is a pointer */
2235 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2236 /* Left is pointer, right is pointer, must scale result */
2237 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2238 Error ("Incompatible pointer types");
2240 rscale = CheckedPSizeOf (lhst);
2242 /* Operate on pointers, result type is an integer */
2244 lval->Type = type_int;
2245 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2246 /* Integer subtraction */
2247 flags = typeadjust (lval, &lval2, 1);
2250 Error ("Invalid operands for binary operator `-'");
2253 /* Do the subtraction */
2254 g_dec (flags | CF_CONST, lval2.ConstVal);
2256 /* If this was a pointer subtraction, we must scale the result */
2258 g_scale (flags, -rscale);
2261 /* Result is in primary register */
2262 lval->Flags = E_MEXPR;
2263 lval->Test &= ~E_CC;
2269 /* Right hand side is not constant. Get the rhs type. */
2272 /* Check for pointer arithmetic */
2273 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2274 /* Left is pointer, right is int, must scale rhs */
2275 g_scale (CF_INT, CheckedPSizeOf (lhst));
2276 /* Operate on pointers, result type is a pointer */
2278 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2279 /* Left is pointer, right is pointer, must scale result */
2280 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2281 Error ("Incompatible pointer types");
2283 rscale = CheckedPSizeOf (lhst);
2285 /* Operate on pointers, result type is an integer */
2287 lval->Type = type_int;
2288 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2289 /* Integer subtraction. If the left hand side descriptor says that
2290 * the lhs is const, we have to remove this mark, since this is no
2291 * longer true, lhs is on stack instead.
2293 if (lval->Flags == E_MCONST) {
2294 lval->Flags = E_MEXPR;
2296 /* Adjust operand types */
2297 flags = typeadjust (lval, &lval2, 0);
2300 Error ("Invalid operands for binary operator `-'");
2303 /* Generate code for the sub (the & is a hack here) */
2304 g_sub (flags & ~CF_CONST, 0);
2306 /* If this was a pointer subtraction, we must scale the result */
2308 g_scale (flags, -rscale);
2311 /* Result is in primary register */
2312 lval->Flags = E_MEXPR;
2313 lval->Test &= ~E_CC;
2319 static int hie8 (ExprDesc* lval)
2320 /* Process + and - binary operators. */
2322 int k = hie9 (lval);
2323 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2325 if (CurTok.Tok == TOK_PLUS) {
2338 static int hie7 (ExprDesc *lval)
2339 /* Parse << and >>. */
2341 static const GenDesc* hie7_ops [] = {
2346 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2351 static int hie6 (ExprDesc *lval)
2352 /* process greater-than type comparators */
2354 static const GenDesc* hie6_ops [] = {
2355 &GenLT, &GenLE, &GenGE, &GenGT, 0
2357 return hie_compare (hie6_ops, lval, hie7);
2362 static int hie5 (ExprDesc *lval)
2364 static const GenDesc* hie5_ops[] = {
2367 return hie_compare (hie5_ops, lval, hie6);
2372 static int hie4 (ExprDesc* lval)
2373 /* Handle & (bitwise and) */
2375 static const GenDesc* hie4_ops [] = {
2380 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2385 static int hie3 (ExprDesc *lval)
2386 /* Handle ^ (bitwise exclusive or) */
2388 static const GenDesc* hie3_ops [] = {
2393 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2398 static int hie2 (ExprDesc *lval)
2399 /* Handle | (bitwise or) */
2401 static const GenDesc* hie2_ops [] = {
2406 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2411 static int hieAndPP (ExprDesc* lval)
2412 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2413 * called recursively from the preprocessor.
2418 ConstSubExpr (hie2, lval);
2419 while (CurTok.Tok == TOK_BOOL_AND) {
2421 /* Left hand side must be an int */
2422 if (!IsClassInt (lval->Type)) {
2423 Error ("Left hand side must be of integer type");
2424 MakeConstIntExpr (lval, 1);
2431 ConstSubExpr (hie2, &lval2);
2433 /* Since we are in PP mode, all we know about is integers */
2434 if (!IsClassInt (lval2.Type)) {
2435 Error ("Right hand side must be of integer type");
2436 MakeConstIntExpr (&lval2, 1);
2439 /* Combine the two */
2440 lval->ConstVal = (lval->ConstVal && lval2.ConstVal);
2443 /* Always a rvalue */
2449 static int hieOrPP (ExprDesc *lval)
2450 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2451 * called recursively from the preprocessor.
2456 ConstSubExpr (hieAndPP, lval);
2457 while (CurTok.Tok == TOK_BOOL_OR) {
2459 /* Left hand side must be an int */
2460 if (!IsClassInt (lval->Type)) {
2461 Error ("Left hand side must be of integer type");
2462 MakeConstIntExpr (lval, 1);
2469 ConstSubExpr (hieAndPP, &lval2);
2471 /* Since we are in PP mode, all we know about is integers */
2472 if (!IsClassInt (lval2.Type)) {
2473 Error ("Right hand side must be of integer type");
2474 MakeConstIntExpr (&lval2, 1);
2477 /* Combine the two */
2478 lval->ConstVal = (lval->ConstVal || lval2.ConstVal);
2481 /* Always a rvalue */
2487 static int hieAnd (ExprDesc* lval, unsigned TrueLab, int* BoolOp)
2488 /* Process "exp && exp" */
2495 if (CurTok.Tok == TOK_BOOL_AND) {
2497 /* Tell our caller that we're evaluating a boolean */
2500 /* Get a label that we will use for false expressions */
2501 lab = GetLocalLabel ();
2503 /* If the expr hasn't set condition codes, set the force-test flag */
2504 if ((lval->Test & E_CC) == 0) {
2505 lval->Test |= E_FORCETEST;
2508 /* Load the value */
2509 exprhs (CF_FORCECHAR, k, lval);
2511 /* Generate the jump */
2512 g_falsejump (CF_NONE, lab);
2514 /* Parse more boolean and's */
2515 while (CurTok.Tok == TOK_BOOL_AND) {
2522 if ((lval2.Test & E_CC) == 0) {
2523 lval2.Test |= E_FORCETEST;
2525 exprhs (CF_FORCECHAR, k, &lval2);
2527 /* Do short circuit evaluation */
2528 if (CurTok.Tok == TOK_BOOL_AND) {
2529 g_falsejump (CF_NONE, lab);
2531 /* Last expression - will evaluate to true */
2532 g_truejump (CF_NONE, TrueLab);
2536 /* Define the false jump label here */
2537 g_defcodelabel (lab);
2539 /* Define the label */
2540 lval->Flags = E_MEXPR;
2541 lval->Test |= E_CC; /* Condition codes are set */
2549 static int hieOr (ExprDesc *lval)
2550 /* Process "exp || exp". */
2554 int BoolOp = 0; /* Did we have a boolean op? */
2555 int AndOp; /* Did we have a && operation? */
2556 unsigned TrueLab; /* Jump to this label if true */
2560 TrueLab = GetLocalLabel ();
2562 /* Call the next level parser */
2563 k = hieAnd (lval, TrueLab, &BoolOp);
2565 /* Any boolean or's? */
2566 if (CurTok.Tok == TOK_BOOL_OR) {
2568 /* If the expr hasn't set condition codes, set the force-test flag */
2569 if ((lval->Test & E_CC) == 0) {
2570 lval->Test |= E_FORCETEST;
2573 /* Get first expr */
2574 exprhs (CF_FORCECHAR, k, lval);
2576 /* For each expression jump to TrueLab if true. Beware: If we
2577 * had && operators, the jump is already in place!
2580 g_truejump (CF_NONE, TrueLab);
2583 /* Remember that we had a boolean op */
2586 /* while there's more expr */
2587 while (CurTok.Tok == TOK_BOOL_OR) {
2594 k = hieAnd (&lval2, TrueLab, &AndOp);
2595 if ((lval2.Test & E_CC) == 0) {
2596 lval2.Test |= E_FORCETEST;
2598 exprhs (CF_FORCECHAR, k, &lval2);
2600 /* If there is more to come, add shortcut boolean eval. */
2601 g_truejump (CF_NONE, TrueLab);
2604 lval->Flags = E_MEXPR;
2605 lval->Test |= E_CC; /* Condition codes are set */
2609 /* If we really had boolean ops, generate the end sequence */
2611 DoneLab = GetLocalLabel ();
2612 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2613 g_falsejump (CF_NONE, DoneLab);
2614 g_defcodelabel (TrueLab);
2615 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2616 g_defcodelabel (DoneLab);
2623 static int hieQuest (ExprDesc *lval)
2624 /* Parse "lvalue ? exp : exp" */
2629 ExprDesc lval2; /* Expression 2 */
2630 ExprDesc lval3; /* Expression 3 */
2631 type* type2; /* Type of expression 2 */
2632 type* type3; /* Type of expression 3 */
2633 type* rtype; /* Type of result */
2634 CodeMark Mark1; /* Save position in output code */
2635 CodeMark Mark2; /* Save position in output code */
2639 k = Preprocessing? hieOrPP (lval) : hieOr (lval);
2640 if (CurTok.Tok == TOK_QUEST) {
2642 if ((lval->Test & E_CC) == 0) {
2643 /* Condition codes not set, force a test */
2644 lval->Test |= E_FORCETEST;
2646 exprhs (CF_NONE, k, lval);
2647 labf = GetLocalLabel ();
2648 g_falsejump (CF_NONE, labf);
2650 /* Parse second expression */
2651 k = expr (hie1, &lval2);
2653 if (!IsTypeVoid (lval2.Type)) {
2654 /* Load it into the primary */
2655 exprhs (CF_NONE, k, &lval2);
2657 labt = GetLocalLabel ();
2661 /* Parse the third expression */
2662 g_defcodelabel (labf);
2663 k = expr (hie1, &lval3);
2665 if (!IsTypeVoid (lval3.Type)) {
2666 /* Load it into the primary */
2667 exprhs (CF_NONE, k, &lval3);
2670 /* Check if any conversions are needed, if so, do them.
2671 * Conversion rules for ?: expression are:
2672 * - if both expressions are int expressions, default promotion
2673 * rules for ints apply.
2674 * - if both expressions are pointers of the same type, the
2675 * result of the expression is of this type.
2676 * - if one of the expressions is a pointer and the other is
2677 * a zero constant, the resulting type is that of the pointer
2679 * - if both expressions are void expressions, the result is of
2681 * - all other cases are flagged by an error.
2683 if (IsClassInt (type2) && IsClassInt (type3)) {
2685 /* Get common type */
2686 rtype = promoteint (type2, type3);
2688 /* Convert the third expression to this type if needed */
2689 g_typecast (TypeOf (rtype), TypeOf (type3));
2691 /* Setup a new label so that the expr3 code will jump around
2692 * the type cast code for expr2.
2694 labf = GetLocalLabel (); /* Get new label */
2695 Mark1 = GetCodePos (); /* Remember current position */
2696 g_jump (labf); /* Jump around code */
2698 /* The jump for expr2 goes here */
2699 g_defcodelabel (labt);
2701 /* Create the typecast code for expr2 */
2702 Mark2 = GetCodePos (); /* Remember position */
2703 g_typecast (TypeOf (rtype), TypeOf (type2));
2705 /* Jump here around the typecase code. */
2706 g_defcodelabel (labf);
2707 labt = 0; /* Mark other label as invalid */
2709 } else if (IsClassPtr (type2) && IsClassPtr (type3)) {
2710 /* Must point to same type */
2711 if (TypeCmp (Indirect (type2), Indirect (type3)) < TC_EQUAL) {
2712 Error ("Incompatible pointer types");
2714 /* Result has the common type */
2716 } else if (IsClassPtr (type2) && IsNullPtr (&lval3)) {
2717 /* Result type is pointer, no cast needed */
2719 } else if (IsNullPtr (&lval2) && IsClassPtr (type3)) {
2720 /* Result type is pointer, no cast needed */
2722 } else if (IsTypeVoid (type2) && IsTypeVoid (type3)) {
2723 /* Result type is void */
2726 Error ("Incompatible types");
2727 rtype = lval2.Type; /* Doesn't matter here */
2730 /* If we don't have the label defined until now, do it */
2732 g_defcodelabel (labt);
2735 /* Setup the target expression */
2736 lval->Flags = E_MEXPR;
2745 static void opeq (const GenDesc* Gen, ExprDesc *lval, int k)
2746 /* Process "op=" operators. */
2755 Error ("Invalid lvalue in assignment");
2759 /* Determine the type of the lhs */
2760 flags = TypeOf (lval->Type);
2761 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2762 lval->Type [0] == T_PTR;
2764 /* Get the lhs address on stack (if needed) */
2767 /* Fetch the lhs into the primary register if needed */
2768 exprhs (CF_NONE, k, lval);
2770 /* Bring the lhs on stack */
2771 Mark = GetCodePos ();
2774 /* Evaluate the rhs */
2775 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2776 /* The resulting value is a constant. If the generator has the NOPUSH
2777 * flag set, don't push the lhs.
2779 if (Gen->Flags & GEN_NOPUSH) {
2784 /* lhs is a pointer, scale rhs */
2785 lval2.ConstVal *= CheckedSizeOf (lval->Type+1);
2788 /* If the lhs is character sized, the operation may be later done
2791 if (CheckedSizeOf (lval->Type) == 1) {
2792 flags |= CF_FORCECHAR;
2795 /* Special handling for add and sub - some sort of a hack, but short code */
2796 if (Gen->Func == g_add) {
2797 g_inc (flags | CF_CONST, lval2.ConstVal);
2798 } else if (Gen->Func == g_sub) {
2799 g_dec (flags | CF_CONST, lval2.ConstVal);
2801 Gen->Func (flags | CF_CONST, lval2.ConstVal);
2804 /* rhs is not constant and already in the primary register */
2806 /* lhs is a pointer, scale rhs */
2807 g_scale (TypeOf (lval2.Type), CheckedSizeOf (lval->Type+1));
2810 /* If the lhs is character sized, the operation may be later done
2813 if (CheckedSizeOf (lval->Type) == 1) {
2814 flags |= CF_FORCECHAR;
2817 /* Adjust the types of the operands if needed */
2818 Gen->Func (g_typeadjust (flags, TypeOf (lval2.Type)), 0);
2821 lval->Flags = E_MEXPR;
2826 static void addsubeq (const GenDesc* Gen, ExprDesc *lval, int k)
2827 /* Process the += and -= operators */
2835 /* We must have an lvalue */
2837 Error ("Invalid lvalue in assignment");
2841 /* We're currently only able to handle some adressing modes */
2842 if ((lval->Flags & E_MGLOBAL) == 0 && /* Global address? */
2843 (lval->Flags & E_MLOCAL) == 0 && /* Local address? */
2844 (lval->Flags & E_MCONST) == 0) { /* Constant address? */
2845 /* Use generic routine */
2846 opeq (Gen, lval, k);
2850 /* Skip the operator */
2853 /* Check if we have a pointer expression and must scale rhs */
2854 MustScale = (lval->Type [0] == T_PTR);
2856 /* Initialize the code generator flags */
2860 /* Evaluate the rhs */
2861 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2862 /* The resulting value is a constant. */
2864 /* lhs is a pointer, scale rhs */
2865 lval2.ConstVal *= CheckedSizeOf (lval->Type+1);
2870 /* rhs is not constant and already in the primary register */
2872 /* lhs is a pointer, scale rhs */
2873 g_scale (TypeOf (lval2.Type), CheckedSizeOf (lval->Type+1));
2877 /* Setup the code generator flags */
2878 lflags |= TypeOf (lval->Type) | CF_FORCECHAR;
2879 rflags |= TypeOf (lval2.Type);
2881 /* Cast the rhs to the type of the lhs */
2882 g_typecast (lflags, rflags);
2884 /* Output apropriate code */
2885 if (lval->Flags & E_MGLOBAL) {
2886 /* Static variable */
2887 lflags |= GlobalModeFlags (lval->Flags);
2888 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2889 g_addeqstatic (lflags, lval->Name, lval->ConstVal, lval2.ConstVal);
2891 g_subeqstatic (lflags, lval->Name, lval->ConstVal, lval2.ConstVal);
2893 } else if (lval->Flags & E_MLOCAL) {
2894 /* ref to localvar */
2895 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2896 g_addeqlocal (lflags, lval->ConstVal, lval2.ConstVal);
2898 g_subeqlocal (lflags, lval->ConstVal, lval2.ConstVal);
2900 } else if (lval->Flags & E_MCONST) {
2901 /* ref to absolute address */
2902 lflags |= CF_ABSOLUTE;
2903 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2904 g_addeqstatic (lflags, lval->ConstVal, 0, lval2.ConstVal);
2906 g_subeqstatic (lflags, lval->ConstVal, 0, lval2.ConstVal);
2908 } else if (lval->Flags & E_MEXPR) {
2909 /* Address in a/x. */
2910 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2911 g_addeqind (lflags, lval->ConstVal, lval2.ConstVal);
2913 g_subeqind (lflags, lval->ConstVal, lval2.ConstVal);
2916 Internal ("Invalid addressing mode");
2919 /* Expression is in the primary now */
2920 lval->Flags = E_MEXPR;
2925 int hie1 (ExprDesc* lval)
2926 /* Parse first level of expression hierarchy. */
2930 k = hieQuest (lval);
2931 switch (CurTok.Tok) {
2940 Error ("Invalid lvalue in assignment");
2946 case TOK_PLUS_ASSIGN:
2947 addsubeq (&GenPASGN, lval, k);
2950 case TOK_MINUS_ASSIGN:
2951 addsubeq (&GenSASGN, lval, k);
2954 case TOK_MUL_ASSIGN:
2955 opeq (&GenMASGN, lval, k);
2958 case TOK_DIV_ASSIGN:
2959 opeq (&GenDASGN, lval, k);
2962 case TOK_MOD_ASSIGN:
2963 opeq (&GenMOASGN, lval, k);
2966 case TOK_SHL_ASSIGN:
2967 opeq (&GenSLASGN, lval, k);
2970 case TOK_SHR_ASSIGN:
2971 opeq (&GenSRASGN, lval, k);
2974 case TOK_AND_ASSIGN:
2975 opeq (&GenAASGN, lval, k);
2978 case TOK_XOR_ASSIGN:
2979 opeq (&GenXOASGN, lval, k);
2983 opeq (&GenOASGN, lval, k);
2994 int hie0 (ExprDesc *lval)
2995 /* Parse comma operator. */
3000 while (CurTok.Tok == TOK_COMMA) {
3009 int evalexpr (unsigned flags, int (*f) (ExprDesc*), ExprDesc* lval)
3010 /* Will evaluate an expression via the given function. If the result is a
3011 * constant, 0 is returned and the value is put in the lval struct. If the
3012 * result is not constant, exprhs is called to bring the value into the
3013 * primary register and 1 is returned.
3020 if (k == 0 && lval->Flags == E_MCONST) {
3021 /* Constant expression */
3024 /* Not constant, load into the primary */
3025 exprhs (flags, k, lval);
3032 static int expr (int (*func) (ExprDesc*), ExprDesc *lval)
3033 /* Expression parser; func is either hie0 or hie1. */
3042 /* Do some checks if code generation is still constistent */
3043 if (savsp != oursp) {
3045 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
3047 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
3055 void expression1 (ExprDesc* lval)
3056 /* Evaluate an expression on level 1 (no comma operator) and put it into
3057 * the primary register
3060 memset (lval, 0, sizeof (*lval));
3061 exprhs (CF_NONE, expr (hie1, lval), lval);
3066 void expression (ExprDesc* lval)
3067 /* Evaluate an expression and put it into the primary register */
3069 memset (lval, 0, sizeof (*lval));
3070 exprhs (CF_NONE, expr (hie0, lval), lval);
3075 void ConstExpr (ExprDesc* lval)
3076 /* Get a constant value */
3078 memset (lval, 0, sizeof (*lval));
3079 if (expr (hie1, lval) != 0 || (lval->Flags & E_MCONST) == 0) {
3080 Error ("Constant expression expected");
3081 /* To avoid any compiler errors, make the expression a valid const */
3082 MakeConstIntExpr (lval, 1);
3088 void ConstIntExpr (ExprDesc* Val)
3089 /* Get a constant int value */
3091 memset (Val, 0, sizeof (*Val));
3092 if (expr (hie1, Val) != 0 ||
3093 (Val->Flags & E_MCONST) == 0 ||
3094 !IsClassInt (Val->Type)) {
3095 Error ("Constant integer expression expected");
3096 /* To avoid any compiler errors, make the expression a valid const */
3097 MakeConstIntExpr (Val, 1);
3103 void intexpr (ExprDesc* lval)
3104 /* Get an integer expression */
3107 if (!IsClassInt (lval->Type)) {
3108 Error ("Integer expression expected");
3109 /* To avoid any compiler errors, make the expression a valid int */
3110 MakeConstIntExpr (lval, 1);
3116 void boolexpr (ExprDesc* lval)
3117 /* Get a boolean expression */
3119 /* Read an expression */
3122 /* If it's an integer, it's ok. If it's not an integer, but a pointer,
3123 * the pointer used in a boolean context is also ok
3125 if (!IsClassInt (lval->Type) && !IsClassPtr (lval->Type)) {
3126 Error ("Boolean expression expected");
3127 /* To avoid any compiler errors, make the expression a valid int */
3128 MakeConstIntExpr (lval, 1);
3134 void test (unsigned label, int cond)
3135 /* Generate code to perform test and jump if false. */
3140 /* Eat the parenthesis */
3143 /* Prepare the expression, setup labels */
3144 memset (&lval, 0, sizeof (lval));
3146 /* Generate code to eval the expr */
3147 k = expr (hie0, &lval);
3148 if (k == 0 && lval.Flags == E_MCONST) {
3149 /* Constant rvalue */
3150 if (cond == 0 && lval.ConstVal == 0) {
3152 Warning ("Unreachable code");
3153 } else if (cond && lval.ConstVal) {
3160 /* If the expr hasn't set condition codes, set the force-test flag */
3161 if ((lval.Test & E_CC) == 0) {
3162 lval.Test |= E_FORCETEST;
3165 /* Load the value into the primary register */
3166 exprhs (CF_FORCECHAR, k, &lval);
3168 /* Generate the jump */
3170 g_truejump (CF_NONE, label);
3172 /* Special case (putting this here is a small hack - but hey, the
3173 * compiler itself is one big hack...): If a semicolon follows, we
3174 * don't have a statement and may omit the jump.
3176 if (CurTok.Tok != TOK_SEMI) {
3177 g_falsejump (CF_NONE, label);
3181 /* Check for the closing brace */