4 * Ullrich von Bassewitz, 21.06.1998
39 /*****************************************************************************/
41 /*****************************************************************************/
45 /* Generator attributes */
46 #define GEN_NOPUSH 0x01 /* Don't push lhs */
48 /* Map a generator function and its attributes to a token */
50 token_t Tok; /* Token to map to */
51 unsigned Flags; /* Flags for generator function */
52 void (*Func) (unsigned, unsigned long); /* Generator func */
55 /* Descriptors for the operations */
56 static GenDesc GenMUL = { TOK_STAR, GEN_NOPUSH, g_mul };
57 static GenDesc GenDIV = { TOK_DIV, GEN_NOPUSH, g_div };
58 static GenDesc GenMOD = { TOK_MOD, GEN_NOPUSH, g_mod };
59 static GenDesc GenASL = { TOK_SHL, GEN_NOPUSH, g_asl };
60 static GenDesc GenASR = { TOK_SHR, GEN_NOPUSH, g_asr };
61 static GenDesc GenLT = { TOK_LT, GEN_NOPUSH, g_lt };
62 static GenDesc GenLE = { TOK_LE, GEN_NOPUSH, g_le };
63 static GenDesc GenGE = { TOK_GE, GEN_NOPUSH, g_ge };
64 static GenDesc GenGT = { TOK_GT, GEN_NOPUSH, g_gt };
65 static GenDesc GenEQ = { TOK_EQ, GEN_NOPUSH, g_eq };
66 static GenDesc GenNE = { TOK_NE, GEN_NOPUSH, g_ne };
67 static GenDesc GenAND = { TOK_AND, GEN_NOPUSH, g_and };
68 static GenDesc GenXOR = { TOK_XOR, GEN_NOPUSH, g_xor };
69 static GenDesc GenOR = { TOK_OR, GEN_NOPUSH, g_or };
70 static GenDesc GenPASGN = { TOK_PLUS_ASSIGN, GEN_NOPUSH, g_add };
71 static GenDesc GenSASGN = { TOK_MINUS_ASSIGN, GEN_NOPUSH, g_sub };
72 static GenDesc GenMASGN = { TOK_MUL_ASSIGN, GEN_NOPUSH, g_mul };
73 static GenDesc GenDASGN = { TOK_DIV_ASSIGN, GEN_NOPUSH, g_div };
74 static GenDesc GenMOASGN = { TOK_MOD_ASSIGN, GEN_NOPUSH, g_mod };
75 static GenDesc GenSLASGN = { TOK_SHL_ASSIGN, GEN_NOPUSH, g_asl };
76 static GenDesc GenSRASGN = { TOK_SHR_ASSIGN, GEN_NOPUSH, g_asr };
77 static GenDesc GenAASGN = { TOK_AND_ASSIGN, GEN_NOPUSH, g_and };
78 static GenDesc GenXOASGN = { TOK_XOR_ASSIGN, GEN_NOPUSH, g_xor };
79 static GenDesc GenOASGN = { TOK_OR_ASSIGN, GEN_NOPUSH, g_or };
83 /*****************************************************************************/
84 /* Function forwards */
85 /*****************************************************************************/
89 static int hie10 (ExprDesc* lval);
90 /* Handle ++, --, !, unary - etc. */
94 /*****************************************************************************/
95 /* Helper functions */
96 /*****************************************************************************/
100 static unsigned GlobalModeFlags (unsigned flags)
101 /* Return the addressing mode flags for the variable with the given flags */
104 if (flags == E_TGLAB) {
105 /* External linkage */
107 } else if (flags == E_TREGISTER) {
108 /* Register variable */
118 static int IsNullPtr (ExprDesc* lval)
119 /* Return true if this is the NULL pointer constant */
121 return (IsClassInt (lval->Type) && /* Is it an int? */
122 lval->Flags == E_MCONST && /* Is it constant? */
123 lval->ConstVal == 0); /* And is it's value zero? */
128 static type* promoteint (type* lhst, type* rhst)
129 /* In an expression with two ints, return the type of the result */
131 /* Rules for integer types:
132 * - If one of the values is a long, the result is long.
133 * - If one of the values is unsigned, the result is also unsigned.
134 * - Otherwise the result is an int.
136 if (IsTypeLong (lhst) || IsTypeLong (rhst)) {
137 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
143 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
153 static unsigned typeadjust (ExprDesc* lhs, ExprDesc* rhs, int NoPush)
154 /* Adjust the two values for a binary operation. lhs is expected on stack or
155 * to be constant, rhs is expected to be in the primary register or constant.
156 * The function will put the type of the result into lhs and return the
157 * code generator flags for the operation.
158 * If NoPush is given, it is assumed that the operation does not expect the lhs
159 * to be on stack, and that lhs is in a register instead.
160 * Beware: The function does only accept int types.
163 unsigned ltype, rtype;
166 /* Get the type strings */
167 type* lhst = lhs->Type;
168 type* rhst = rhs->Type;
170 /* Generate type adjustment code if needed */
171 ltype = TypeOf (lhst);
172 if (lhs->Flags == E_MCONST) {
176 /* Value is in primary register*/
179 rtype = TypeOf (rhst);
180 if (rhs->Flags == E_MCONST) {
183 flags = g_typeadjust (ltype, rtype);
185 /* Set the type of the result */
186 lhs->Type = promoteint (lhst, rhst);
188 /* Return the code generator flags */
194 unsigned assignadjust (type* lhst, ExprDesc* rhs)
195 /* Adjust the type of the right hand expression so that it can be assigned to
196 * the type on the left hand side. This function is used for assignment and
197 * for converting parameters in a function call. It returns the code generator
198 * flags for the operation. The type string of the right hand side will be
199 * set to the type of the left hand side.
202 /* Get the type of the right hand side. Treat function types as
203 * pointer-to-function
205 type* rhst = rhs->Type;
206 if (IsTypeFunc (rhst)) {
207 rhst = PointerTo (rhst);
210 /* After calling this function, rhs will have the type of the lhs */
213 /* First, do some type checking */
214 if (IsTypeVoid (lhst) || IsTypeVoid (rhst)) {
215 /* If one of the sides are of type void, output a more apropriate
218 Error ("Illegal type");
219 } else if (IsClassInt (lhst)) {
220 if (IsClassPtr (rhst)) {
221 /* Pointer -> int conversion */
222 Warning ("Converting pointer to integer without a cast");
223 } else if (!IsClassInt (rhst)) {
224 Error ("Incompatible types");
226 /* Convert the rhs to the type of the lhs. */
227 unsigned flags = TypeOf (rhst);
228 if (rhs->Flags == E_MCONST) {
231 return g_typecast (TypeOf (lhst), flags);
233 } else if (IsClassPtr (lhst)) {
234 if (IsClassPtr (rhst)) {
235 /* Pointer to pointer assignment is valid, if:
236 * - both point to the same types, or
237 * - the rhs pointer is a void pointer, or
238 * - the lhs pointer is a void pointer.
240 if (!IsTypeVoid (Indirect (lhst)) && !IsTypeVoid (Indirect (rhst))) {
241 /* Compare the types */
242 switch (TypeCmp (lhst, rhst)) {
244 case TC_INCOMPATIBLE:
245 Error ("Incompatible pointer types");
249 Error ("Pointer types differ in type qualifiers");
257 } else if (IsClassInt (rhst)) {
258 /* Int to pointer assignment is valid only for constant zero */
259 if (rhs->Flags != E_MCONST || rhs->ConstVal != 0) {
260 Warning ("Converting integer to pointer without a cast");
262 } else if (IsTypeFuncPtr (lhst) && IsTypeFunc(rhst)) {
263 /* Assignment of function to function pointer is allowed, provided
264 * that both functions have the same parameter list.
266 if (TypeCmp (Indirect (lhst), rhst) < TC_EQUAL) {
267 Error ("Incompatible types");
270 Error ("Incompatible types");
273 Error ("Incompatible types");
276 /* Return an int value in all cases where the operands are not both ints */
282 void DefineData (ExprDesc* Expr)
283 /* Output a data definition for the given expression */
285 unsigned Flags = Expr->Flags;
287 switch (Flags & E_MCTYPE) {
291 g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->ConstVal, 0);
295 /* Register variable. Taking the address is usually not
298 if (!AllowRegVarAddr) {
299 Error ("Cannot take the address of a register variable");
305 /* Local or global symbol */
306 g_defdata (GlobalModeFlags (Flags), Expr->Name, Expr->ConstVal);
310 /* a literal of some kind */
311 g_defdata (CF_STATIC, LiteralPoolLabel, Expr->ConstVal);
315 Internal ("Unknown constant type: %04X", Flags);
321 static void lconst (unsigned Flags, ExprDesc* Expr)
322 /* Load the primary register with some constant value. */
324 switch (Expr->Flags & E_MCTYPE) {
327 g_leasp (Expr->ConstVal);
331 /* Number constant */
332 g_getimmed (Flags | TypeOf (Expr->Type) | CF_CONST, Expr->ConstVal, 0);
336 /* Register variable. Taking the address is usually not
339 if (!AllowRegVarAddr) {
340 Error ("Cannot take the address of a register variable");
346 /* Local or global symbol, load address */
347 Flags |= GlobalModeFlags (Expr->Flags);
349 g_getimmed (Flags, Expr->Name, Expr->ConstVal);
354 g_getimmed (CF_STATIC, LiteralPoolLabel, Expr->ConstVal);
358 Internal ("Unknown constant type: %04X", Expr->Flags);
364 static int kcalc (int tok, long val1, long val2)
365 /* Calculate an operation with left and right operand constant. */
369 return (val1 == val2);
371 return (val1 != val2);
373 return (val1 < val2);
375 return (val1 <= val2);
377 return (val1 >= val2);
379 return (val1 > val2);
381 return (val1 | val2);
383 return (val1 ^ val2);
385 return (val1 & val2);
387 return (val1 >> val2);
389 return (val1 << val2);
391 return (val1 * val2);
394 Error ("Division by zero");
397 return (val1 / val2);
400 Error ("Modulo operation with zero");
403 return (val1 % val2);
405 Internal ("kcalc: got token 0x%X\n", tok);
412 static const GenDesc* FindGen (token_t Tok, const GenDesc** Table)
413 /* Find a token in a generator 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 static void MakeConstIntExpr (ExprDesc* Expr, long Value)
461 /* Make Expr a constant integer expression with the given value */
463 Expr->Flags = E_MCONST;
464 Expr->Type = type_int;
465 Expr->ConstVal = Value;
470 void ConstSubExpr (int (*F) (ExprDesc*), ExprDesc* Expr)
471 /* Will evaluate an expression via the given function. If the result is not
472 * a constant, a diagnostic will be printed, and the value is replaced by
473 * a constant one to make sure there are no internal errors that result
474 * from this input error.
477 memset (Expr, 0, sizeof (*Expr));
478 if (F (Expr) != 0 || Expr->Flags != E_MCONST) {
479 Error ("Constant expression expected");
480 /* To avoid any compiler errors, make the expression a valid const */
481 MakeConstIntExpr (Expr, 1);
487 /*****************************************************************************/
489 /*****************************************************************************/
493 void exprhs (unsigned flags, int k, ExprDesc *lval)
494 /* Put the result of an expression into the primary register */
500 /* Dereferenced lvalue */
501 flags |= TypeOf (lval->Type);
502 if (lval->Test & E_FORCETEST) {
504 lval->Test &= ~E_FORCETEST;
506 if (f & E_MGLOBAL) { /* ref to globalvar */
508 flags |= GlobalModeFlags (f);
509 g_getstatic (flags, lval->Name, lval->ConstVal);
510 } else if (f & E_MLOCAL) {
511 /* ref to localvar */
512 g_getlocal (flags, lval->ConstVal);
513 } else if (f & E_MCONST) {
514 /* ref to absolute address */
515 g_getstatic (flags | CF_ABSOLUTE, lval->ConstVal, 0);
516 } else if (f == E_MEOFFS) {
517 g_getind (flags, lval->ConstVal);
518 } else if (f != E_MREG) {
521 } else if (f == E_MEOFFS) {
522 /* reference not storable */
523 flags |= TypeOf (lval->Type);
524 g_inc (flags | CF_CONST, lval->ConstVal);
525 } else if ((f & E_MEXPR) == 0) {
526 /* Constant of some sort, load it into the primary */
527 lconst (flags, lval);
529 if (lval->Test & E_FORCETEST) { /* we testing this value? */
531 flags |= TypeOf (lval->Type);
532 g_test (flags); /* yes, force a test */
533 lval->Test &= ~E_FORCETEST;
539 static unsigned FunctionParamList (FuncDesc* Func)
540 /* Parse a function parameter list and pass the parameters to the called
541 * function. Depending on several criteria this may be done by just pushing
542 * each parameter separately, or creating the parameter frame once and then
543 * storing into this frame.
544 * The function returns the size of the parameters pushed.
549 /* Initialize variables */
550 SymEntry* Param = 0; /* Keep gcc silent */
551 unsigned ParamSize = 0; /* Size of parameters pushed */
552 unsigned ParamCount = 0; /* Number of parameters pushed */
553 unsigned FrameSize = 0; /* Size of parameter frame */
554 unsigned FrameParams = 0; /* Number of params in frame */
555 int FrameOffs = 0; /* Offset into parameter frame */
556 int Ellipsis = 0; /* Function is variadic */
558 /* As an optimization, we may allocate the complete parameter frame at
559 * once instead of pushing each parameter as it comes. We may do that,
562 * - optimizations that increase code size are enabled (allocating the
563 * stack frame at once gives usually larger code).
564 * - we have more than one parameter to push (don't count the last param
565 * for __fastcall__ functions).
567 if (CodeSizeFactor >= 200) {
569 /* Calculate the number and size of the parameters */
570 FrameParams = Func->ParamCount;
571 FrameSize = Func->ParamSize;
572 if (FrameParams > 0 && (Func->Flags & FD_FASTCALL) != 0) {
573 /* Last parameter is not pushed */
574 const SymEntry* LastParam = Func->SymTab->SymTail;
575 FrameSize -= CheckedSizeOf (LastParam->Type);
579 /* Do we have more than one parameter in the frame? */
580 if (FrameParams > 1) {
581 /* Okeydokey, setup the frame */
586 /* Don't use a preallocated frame */
591 /* Parse the actual parameter list */
592 while (CurTok.Tok != TOK_RPAREN) {
597 /* Count arguments */
600 /* Fetch the pointer to the next argument, check for too many args */
601 if (ParamCount <= Func->ParamCount) {
602 /* Beware: If there are parameters with identical names, they
603 * cannot go into the same symbol table, which means that in this
604 * case of errorneous input, the number of nodes in the symbol
605 * table and ParamCount are NOT equal. We have to handle this case
606 * below to avoid segmentation violations. Since we know that this
607 * problem can only occur if there is more than one parameter,
608 * we will just use the last one.
610 if (ParamCount == 1) {
612 Param = Func->SymTab->SymHead;
613 } else if (Param->NextSym != 0) {
615 Param = Param->NextSym;
616 CHECK ((Param->Flags & SC_PARAM) != 0);
618 } else if (!Ellipsis) {
619 /* Too many arguments. Do we have an open param list? */
620 if ((Func->Flags & FD_VARIADIC) == 0) {
621 /* End of param list reached, no ellipsis */
622 Error ("Too many arguments in function call");
624 /* Assume an ellipsis even in case of errors to avoid an error
625 * message for each other argument.
630 /* Do some optimization: If we have a constant value to push,
631 * use a special function that may optimize.
634 if (!Ellipsis && CheckedSizeOf (Param->Type) == 1) {
635 CFlags = CF_FORCECHAR;
638 if (evalexpr (CFlags, hie1, &lval) == 0) {
639 /* A constant value */
643 /* If we don't have an argument spec, accept anything, otherwise
644 * convert the actual argument to the type needed.
647 /* Promote the argument if needed */
648 assignadjust (Param->Type, &lval);
650 /* If we have a prototype, chars may be pushed as chars */
651 Flags |= CF_FORCECHAR;
654 /* Use the type of the argument for the push */
655 Flags |= TypeOf (lval.Type);
657 /* If this is a fastcall function, don't push the last argument */
658 if (ParamCount == Func->ParamCount && (Func->Flags & FD_FASTCALL) != 0) {
659 /* Just load the argument into the primary. This is only needed if
660 * we have a constant argument, otherwise the value is already in
663 if (Flags & CF_CONST) {
664 exprhs (CF_FORCECHAR, 0, &lval);
667 unsigned ArgSize = sizeofarg (Flags);
669 /* We have the space already allocated, store in the frame */
670 CHECK (FrameSize >= ArgSize);
671 FrameSize -= ArgSize;
672 FrameOffs -= ArgSize;
674 g_putlocal (Flags | CF_NOKEEP, FrameOffs, lval.ConstVal);
676 /* Push the argument */
677 g_push (Flags, lval.ConstVal);
680 /* Calculate total parameter size */
681 ParamSize += ArgSize;
684 /* Check for end of argument list */
685 if (CurTok.Tok != TOK_COMMA) {
691 /* Check if we had enough parameters */
692 if (ParamCount < Func->ParamCount) {
693 Error ("Too few arguments in function call");
696 /* The function returns the size of all parameters pushed onto the stack.
697 * However, if there are parameters missing (which is an error and was
698 * flagged by the compiler) AND a stack frame was preallocated above,
699 * we would loose track of the stackpointer and generate an internal error
700 * later. So we correct the value by the parameters that should have been
701 * pushed to avoid an internal compiler error. Since an error was
702 * generated before, no code will be output anyway.
704 return ParamSize + FrameSize;
709 static void FunctionCall (int k, ExprDesc* lval)
710 /* Perform a function call. */
712 FuncDesc* Func; /* Function descriptor */
713 int IsFuncPtr; /* Flag */
714 unsigned ParamSize; /* Number of parameter bytes */
715 CodeMark Mark = 0; /* Initialize to keep gcc silent */
716 int PtrOffs = 0; /* Offset of function pointer on stack */
717 int IsFastCall = 0; /* True if it's a fast call function */
718 int PtrOnStack = 0; /* True if a pointer copy is on stack */
720 /* Get a pointer to the function descriptor from the type string */
721 Func = GetFuncDesc (lval->Type);
723 /* Handle function pointers transparently */
724 IsFuncPtr = IsTypeFuncPtr (lval->Type);
727 /* Check wether it's a fastcall function that has parameters */
728 IsFastCall = IsFastCallFunc (lval->Type + 1) && (Func->ParamCount > 0);
730 /* Things may be difficult, depending on where the function pointer
731 * resides. If the function pointer is an expression of some sort
732 * (not a local or global variable), we have to evaluate this
733 * expression now and save the result for later. Since calls to
734 * function pointers may be nested, we must save it onto the stack.
735 * For fastcall functions we do also need to place a copy of the
736 * pointer on stack, since we cannot use a/x.
738 PtrOnStack = IsFastCall || ((lval->Flags & (E_MGLOBAL | E_MLOCAL)) == 0);
741 /* Not a global or local variable, or a fastcall function. Load
742 * the pointer into the primary and mark it as an expression.
744 exprhs (CF_NONE, k, lval);
745 lval->Flags |= E_MEXPR;
747 /* Remember the code position */
748 Mark = GetCodePos ();
750 /* Push the pointer onto the stack and remember the offset */
755 /* Check for known standard functions and inline them if requested */
756 } else if (InlineStdFuncs && IsStdFunc ((const char*) lval->Name)) {
758 /* Inline this function */
759 HandleStdFunc (Func, lval);
764 /* Parse the parameter list */
765 ParamSize = FunctionParamList (Func);
767 /* We need the closing paren here */
770 /* Special handling for function pointers */
773 /* If the function is not a fastcall function, load the pointer to
774 * the function into the primary.
778 /* Not a fastcall function - we may use the primary */
780 /* If we have no parameters, the pointer is still in the
781 * primary. Remove the code to push it and correct the
784 if (ParamSize == 0) {
789 /* Load from the saved copy */
790 g_getlocal (CF_PTR, PtrOffs);
793 /* Load from original location */
794 exprhs (CF_NONE, k, lval);
797 /* Call the function */
798 g_callind (TypeOf (lval->Type+1), ParamSize, PtrOffs);
802 /* Fastcall function. We cannot use the primary for the function
803 * pointer and must therefore use an offset to the stack location.
804 * Since fastcall functions may never be variadic, we can use the
805 * index register for this purpose.
807 g_callind (CF_LOCAL, ParamSize, PtrOffs);
810 /* If we have a pointer on stack, remove it */
812 g_space (- (int) sizeofarg (CF_PTR));
821 /* Normal function */
822 g_call (TypeOf (lval->Type), (const char*) lval->Name, ParamSize);
829 static int primary (ExprDesc* lval)
830 /* This is the lowest level of the expression parser. */
834 /* Initialize fields in the expression stucture */
835 lval->Test = 0; /* No test */
836 lval->Sym = 0; /* Symbol unknown */
838 /* Character and integer constants. */
839 if (CurTok.Tok == TOK_ICONST || CurTok.Tok == TOK_CCONST) {
840 lval->Flags = E_MCONST | E_TCONST;
841 lval->Type = CurTok.Type;
842 lval->ConstVal = CurTok.IVal;
847 /* Process parenthesized subexpression by calling the whole parser
850 if (CurTok.Tok == TOK_LPAREN) {
852 memset (lval, 0, sizeof (*lval)); /* Remove any attributes */
858 /* If we run into an identifier in preprocessing mode, we assume that this
859 * is an undefined macro and replace it by a constant value of zero.
861 if (Preprocessing && CurTok.Tok == TOK_IDENT) {
862 MakeConstIntExpr (lval, 0);
866 /* All others may only be used if the expression evaluation is not called
867 * recursively by the preprocessor.
870 /* Illegal expression in PP mode */
871 Error ("Preprocessor expression expected");
872 MakeConstIntExpr (lval, 1);
877 if (CurTok.Tok == TOK_IDENT) {
882 /* Get a pointer to the symbol table entry */
883 Sym = lval->Sym = FindSym (CurTok.Ident);
885 /* Is the symbol known? */
888 /* We found the symbol - skip the name token */
891 /* The expression type is the symbol type */
892 lval->Type = Sym->Type;
894 /* Check for illegal symbol types */
895 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
896 if (Sym->Flags & SC_TYPE) {
897 /* Cannot use type symbols */
898 Error ("Variable identifier expected");
899 /* Assume an int type to make lval valid */
900 lval->Flags = E_MLOCAL | E_TLOFFS;
901 lval->Type = type_int;
906 /* Check for legal symbol types */
907 if ((Sym->Flags & SC_CONST) == SC_CONST) {
908 /* Enum or some other numeric constant */
909 lval->Flags = E_MCONST;
910 lval->ConstVal = Sym->V.ConstVal;
912 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
914 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
915 lval->Name = (unsigned long) Sym->Name;
917 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
918 /* Local variable. If this is a parameter for a variadic
919 * function, we have to add some address calculations, and the
920 * address is not const.
922 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
923 /* Variadic parameter */
924 g_leavariadic (Sym->V.Offs - F_GetParamSize (CurrentFunc));
925 lval->Flags = E_MEXPR;
928 /* Normal parameter */
929 lval->Flags = E_MLOCAL | E_TLOFFS;
930 lval->ConstVal = Sym->V.Offs;
932 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
933 /* Static variable */
934 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
935 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
936 lval->Name = (unsigned long) Sym->Name;
938 lval->Flags = E_MGLOBAL | E_MCONST | E_TLLAB;
939 lval->Name = Sym->V.Label;
942 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
943 /* Register variable, zero page based */
944 lval->Flags = E_MGLOBAL | E_MCONST | E_TREGISTER;
945 lval->Name = Sym->V.Offs;
948 /* Local static variable */
949 lval->Flags = E_MGLOBAL | E_MCONST | E_TLLAB;
950 lval->Name = Sym->V.Offs;
954 /* The symbol is referenced now */
955 Sym->Flags |= SC_REF;
956 if (IsTypeFunc (lval->Type) || IsTypeArray (lval->Type)) {
962 /* We did not find the symbol. Remember the name, then skip it */
963 strcpy (Ident, CurTok.Ident);
966 /* IDENT is either an auto-declared function or an undefined variable. */
967 if (CurTok.Tok == TOK_LPAREN) {
968 /* Declare a function returning int. For that purpose, prepare a
969 * function signature for a function having an empty param list
972 Warning ("Function call without a prototype");
973 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
974 lval->Type = Sym->Type;
975 lval->Flags = E_MGLOBAL | E_MCONST | E_TGLAB;
976 lval->Name = (unsigned long) Sym->Name;
982 /* Undeclared Variable */
983 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
984 lval->Flags = E_MLOCAL | E_TLOFFS;
985 lval->Type = type_int;
987 Error ("Undefined symbol: `%s'", Ident);
993 /* String literal? */
994 if (CurTok.Tok == TOK_SCONST) {
995 lval->Flags = E_MCONST | E_TLIT;
996 lval->ConstVal = CurTok.IVal;
997 lval->Type = GetCharArrayType (GetLiteralPoolOffs () - CurTok.IVal);
1002 /* ASM statement? */
1003 if (CurTok.Tok == TOK_ASM) {
1005 lval->Type = type_void;
1006 lval->Flags = E_MEXPR;
1011 /* __AX__ and __EAX__ pseudo values? */
1012 if (CurTok.Tok == TOK_AX || CurTok.Tok == TOK_EAX) {
1013 lval->Type = (CurTok.Tok == TOK_AX)? type_uint : type_ulong;
1014 lval->Flags = E_MREG;
1015 lval->Test &= ~E_CC;
1018 return 1; /* May be used as lvalue */
1021 /* Illegal primary. */
1022 Error ("Expression expected");
1023 MakeConstIntExpr (lval, 1);
1029 static int arrayref (int k, ExprDesc* lval)
1030 /* Handle an array reference */
1044 /* Skip the bracket */
1047 /* Get the type of left side */
1050 /* We can apply a special treatment for arrays that have a const base
1051 * address. This is true for most arrays and will produce a lot better
1052 * code. Check if this is a const base address.
1054 lflags = lval->Flags & ~E_MCTYPE;
1055 ConstBaseAddr = (lflags == E_MCONST) || /* Constant numeric address */
1056 (lflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1057 lflags == E_MLOCAL; /* Local array */
1059 /* If we have a constant base, we delay the address fetch */
1060 Mark1 = GetCodePos ();
1061 Mark2 = 0; /* Silence gcc */
1062 if (!ConstBaseAddr) {
1063 /* Get a pointer to the array into the primary */
1064 exprhs (CF_NONE, k, lval);
1066 /* Get the array pointer on stack. Do not push more than 16
1067 * bit, even if this value is greater, since we cannot handle
1068 * other than 16bit stuff when doing indexing.
1070 Mark2 = GetCodePos ();
1074 /* TOS now contains ptr to array elements. Get the subscript. */
1076 if (l == 0 && lval2.Flags == E_MCONST) {
1078 /* The array subscript is a constant - remove value from stack */
1079 if (!ConstBaseAddr) {
1083 /* Get an array pointer into the primary */
1084 exprhs (CF_NONE, k, lval);
1087 if (IsClassPtr (tptr1)) {
1089 /* Scale the subscript value according to element size */
1090 lval2.ConstVal *= CheckedPSizeOf (tptr1);
1092 /* Remove code for lhs load */
1095 /* Handle constant base array on stack. Be sure NOT to
1096 * handle pointers the same way, this won't work.
1098 if (IsTypeArray (tptr1) &&
1099 ((lval->Flags & ~E_MCTYPE) == E_MCONST ||
1100 (lval->Flags & ~E_MCTYPE) == E_MLOCAL ||
1101 (lval->Flags & E_MGLOBAL) != 0 ||
1102 (lval->Flags == E_MEOFFS))) {
1103 lval->ConstVal += lval2.ConstVal;
1106 /* Pointer - load into primary and remember offset */
1107 if ((lval->Flags & E_MEXPR) == 0 || k != 0) {
1108 exprhs (CF_NONE, k, lval);
1110 lval->ConstVal = lval2.ConstVal;
1111 lval->Flags = E_MEOFFS;
1114 /* Result is of element type */
1115 lval->Type = Indirect (tptr1);
1120 } else if (IsClassPtr (tptr2 = lval2.Type)) {
1121 /* Subscript is pointer, get element type */
1122 lval2.Type = Indirect (tptr2);
1124 /* Scale the rhs value in the primary register */
1125 g_scale (TypeOf (tptr1), CheckedSizeOf (lval2.Type));
1127 lval->Type = lval2.Type;
1129 Error ("Cannot subscript");
1132 /* Add the subscript. Since arrays are indexed by integers,
1133 * we will ignore the true type of the subscript here and
1134 * use always an int.
1136 g_inc (CF_INT | CF_CONST, lval2.ConstVal);
1140 /* Array subscript is not constant. Load it into the primary */
1141 Mark2 = GetCodePos ();
1142 exprhs (CF_NONE, l, &lval2);
1145 if (IsClassPtr (tptr1)) {
1147 /* Get the element type */
1148 lval->Type = Indirect (tptr1);
1150 /* Indexing is based on int's, so we will just use the integer
1151 * portion of the index (which is in (e)ax, so there's no further
1154 g_scale (CF_INT, CheckedSizeOf (lval->Type));
1156 } else if (IsClassPtr (tptr2)) {
1158 /* Get the element type */
1159 lval2.Type = Indirect (tptr2);
1161 /* Get the int value on top. If we go here, we're sure,
1162 * both values are 16 bit (the first one was truncated
1163 * if necessary and the second one is a pointer).
1164 * Note: If ConstBaseAddr is true, we don't have a value on
1165 * stack, so to "swap" both, just push the subscript.
1167 if (ConstBaseAddr) {
1169 exprhs (CF_NONE, k, lval);
1176 g_scale (TypeOf (tptr1), CheckedSizeOf (lval2.Type));
1177 lval->Type = lval2.Type;
1179 Error ("Cannot subscript");
1182 /* The offset is now in the primary register. It didn't have a
1183 * constant base address for the lhs, the lhs address is already
1184 * on stack, and we must add the offset. If the base address was
1185 * constant, we call special functions to add the address to the
1188 if (!ConstBaseAddr) {
1189 /* Add the subscript. Both values are int sized. */
1193 /* If the subscript has itself a constant address, it is often
1194 * a better idea to reverse again the order of the evaluation.
1195 * This will generate better code if the subscript is a byte
1196 * sized variable. But beware: This is only possible if the
1197 * subscript was not scaled, that is, if this was a byte array
1200 rflags = lval2.Flags & ~E_MCTYPE;
1201 ConstSubAddr = (rflags == E_MCONST) || /* Constant numeric address */
1202 (rflags & E_MGLOBAL) != 0 || /* Static array, or ... */
1203 rflags == E_MLOCAL; /* Local array */
1205 if (ConstSubAddr && CheckedSizeOf (lval->Type) == 1) {
1209 /* Reverse the order of evaluation */
1210 unsigned flags = (CheckedSizeOf (lval2.Type) == 1)? CF_CHAR : CF_INT;
1213 /* Get a pointer to the array into the primary. We have changed
1214 * Type above but we need the original type to load the
1215 * address, so restore it temporarily.
1217 SavedType = lval->Type;
1219 exprhs (CF_NONE, k, lval);
1220 lval->Type = SavedType;
1222 /* Add the variable */
1223 if (rflags == E_MLOCAL) {
1224 g_addlocal (flags, lval2.ConstVal);
1226 flags |= GlobalModeFlags (lval2.Flags);
1227 g_addstatic (flags, lval2.Name, lval2.ConstVal);
1230 if (lflags == E_MCONST) {
1231 /* Constant numeric address. Just add it */
1232 g_inc (CF_INT | CF_UNSIGNED, lval->ConstVal);
1233 } else if (lflags == E_MLOCAL) {
1234 /* Base address is a local variable address */
1235 if (IsTypeArray (tptr1)) {
1236 g_addaddr_local (CF_INT, lval->ConstVal);
1238 g_addlocal (CF_PTR, lval->ConstVal);
1241 /* Base address is a static variable address */
1242 unsigned flags = CF_INT;
1243 flags |= GlobalModeFlags (lval->Flags);
1244 if (IsTypeArray (tptr1)) {
1245 g_addaddr_static (flags, lval->Name, lval->ConstVal);
1247 g_addstatic (flags, lval->Name, lval->ConstVal);
1253 lval->Flags = E_MEXPR;
1256 return !IsTypeArray (lval->Type);
1262 static int structref (int k, ExprDesc* lval)
1263 /* Process struct field after . or ->. */
1269 /* Skip the token and check for an identifier */
1271 if (CurTok.Tok != TOK_IDENT) {
1272 Error ("Identifier expected");
1273 lval->Type = type_int;
1277 /* Get the symbol table entry and check for a struct field */
1278 strcpy (Ident, CurTok.Ident);
1280 Field = FindStructField (lval->Type, Ident);
1282 Error ("Struct/union has no field named `%s'", Ident);
1283 lval->Type = type_int;
1287 /* If we have constant input data, the result is also constant */
1288 flags = lval->Flags & ~E_MCTYPE;
1289 if (flags == E_MCONST ||
1290 (k == 0 && (flags == E_MLOCAL ||
1291 (flags & E_MGLOBAL) != 0 ||
1292 lval->Flags == E_MEOFFS))) {
1293 lval->ConstVal += Field->V.Offs;
1295 if ((flags & E_MEXPR) == 0 || k != 0) {
1296 exprhs (CF_NONE, k, lval);
1298 lval->ConstVal = Field->V.Offs;
1299 lval->Flags = E_MEOFFS;
1301 lval->Type = Field->Type;
1302 return !IsTypeArray (Field->Type);
1307 static int hie11 (ExprDesc *lval)
1308 /* Handle compound types (structs and arrays) */
1315 if (CurTok.Tok < TOK_LBRACK || CurTok.Tok > TOK_PTR_REF) {
1322 if (CurTok.Tok == TOK_LBRACK) {
1324 /* Array reference */
1325 k = arrayref (k, lval);
1327 } else if (CurTok.Tok == TOK_LPAREN) {
1329 /* Function call. Skip the opening parenthesis */
1332 if (IsTypeFunc (lval->Type) || IsTypeFuncPtr (lval->Type)) {
1334 /* Call the function */
1335 FunctionCall (k, lval);
1337 /* Result is in the primary register */
1338 lval->Flags = E_MEXPR;
1341 lval->Type = GetFuncReturn (lval->Type);
1344 Error ("Illegal function call");
1348 } else if (CurTok.Tok == TOK_DOT) {
1350 if (!IsClassStruct (lval->Type)) {
1351 Error ("Struct expected");
1353 k = structref (0, lval);
1355 } else if (CurTok.Tok == TOK_PTR_REF) {
1358 if (tptr[0] != T_PTR || (tptr[1] & T_STRUCT) == 0) {
1359 Error ("Struct pointer expected");
1361 k = structref (k, lval);
1371 static void store (ExprDesc* lval)
1372 /* Store primary reg into this reference */
1378 flags = TypeOf (lval->Type);
1379 if (f & E_MGLOBAL) {
1380 flags |= GlobalModeFlags (f);
1387 g_putstatic (flags, lval->Name, lval->ConstVal);
1389 } else if (f & E_MLOCAL) {
1390 g_putlocal (flags, lval->ConstVal, 0);
1391 } else if (f == E_MEOFFS) {
1392 g_putind (flags, lval->ConstVal);
1393 } else if (f != E_MREG) {
1395 g_putind (flags, 0);
1397 /* Store into absolute address */
1398 g_putstatic (flags | CF_ABSOLUTE, lval->ConstVal, 0);
1402 /* Assume that each one of the stores will invalidate CC */
1403 lval->Test &= ~E_CC;
1408 static void pre_incdec (ExprDesc* lval, void (*inc) (unsigned, unsigned long))
1409 /* Handle --i and ++i */
1416 if ((k = hie10 (lval)) == 0) {
1417 Error ("Invalid lvalue");
1421 /* Get the data type */
1422 flags = TypeOf (lval->Type) | CF_FORCECHAR | CF_CONST;
1424 /* Get the increment value in bytes */
1425 val = (lval->Type [0] == T_PTR)? CheckedPSizeOf (lval->Type) : 1;
1427 /* We're currently only able to handle some adressing modes */
1428 if ((lval->Flags & E_MGLOBAL) == 0 && /* Global address? */
1429 (lval->Flags & E_MLOCAL) == 0 && /* Local address? */
1430 (lval->Flags & E_MCONST) == 0 && /* Constant address? */
1431 (lval->Flags & E_MEXPR) == 0) { /* Address in a/x? */
1433 /* Use generic code. Push the address if needed */
1436 /* Fetch the value */
1437 exprhs (CF_NONE, k, lval);
1439 /* Increment value in primary */
1442 /* Store the result back */
1447 /* Special code for some addressing modes - use the special += ops */
1448 if (lval->Flags & E_MGLOBAL) {
1449 flags |= GlobalModeFlags (lval->Flags);
1451 g_addeqstatic (flags, lval->Name, lval->ConstVal, val);
1453 g_subeqstatic (flags, lval->Name, lval->ConstVal, val);
1455 } else if (lval->Flags & E_MLOCAL) {
1456 /* ref to localvar */
1458 g_addeqlocal (flags, lval->ConstVal, val);
1460 g_subeqlocal (flags, lval->ConstVal, val);
1462 } else if (lval->Flags & E_MCONST) {
1463 /* ref to absolute address */
1464 flags |= CF_ABSOLUTE;
1466 g_addeqstatic (flags, lval->ConstVal, 0, val);
1468 g_subeqstatic (flags, lval->ConstVal, 0, val);
1470 } else if (lval->Flags & E_MEXPR) {
1471 /* Address in a/x, check if we have an offset */
1472 unsigned Offs = (lval->Flags == E_MEOFFS)? lval->ConstVal : 0;
1474 g_addeqind (flags, Offs, val);
1476 g_subeqind (flags, Offs, val);
1479 Internal ("Invalid addressing mode");
1484 /* Result is an expression */
1485 lval->Flags = E_MEXPR;
1490 static void post_incdec (ExprDesc *lval, int k, void (*inc) (unsigned, unsigned long))
1491 /* Handle i-- and i++ */
1497 Error ("Invalid lvalue");
1501 /* Get the data type */
1502 flags = TypeOf (lval->Type);
1504 /* Push the address if needed */
1507 /* Fetch the value and save it (since it's the result of the expression) */
1508 exprhs (CF_NONE, 1, lval);
1509 g_save (flags | CF_FORCECHAR);
1511 /* If we have a pointer expression, increment by the size of the type */
1512 if (lval->Type[0] == T_PTR) {
1513 inc (flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (lval->Type + 1));
1515 inc (flags | CF_CONST | CF_FORCECHAR, 1);
1518 /* Store the result back */
1521 /* Restore the original value */
1522 g_restore (flags | CF_FORCECHAR);
1523 lval->Flags = E_MEXPR;
1528 static void unaryop (int tok, ExprDesc* lval)
1529 /* Handle unary -/+ and ~ */
1536 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
1537 /* Value is constant */
1539 case TOK_MINUS: lval->ConstVal = -lval->ConstVal; break;
1540 case TOK_PLUS: break;
1541 case TOK_COMP: lval->ConstVal = ~lval->ConstVal; break;
1542 default: Internal ("Unexpected token: %d", tok);
1545 /* Value is not constant */
1546 exprhs (CF_NONE, k, lval);
1548 /* Get the type of the expression */
1549 flags = TypeOf (lval->Type);
1551 /* Handle the operation */
1553 case TOK_MINUS: g_neg (flags); break;
1554 case TOK_PLUS: break;
1555 case TOK_COMP: g_com (flags); break;
1556 default: Internal ("Unexpected token: %d", tok);
1558 lval->Flags = E_MEXPR;
1564 static int typecast (ExprDesc* lval)
1565 /* Handle an explicit cast */
1568 type Type[MAXTYPELEN];
1570 /* Skip the left paren */
1579 /* Read the expression we have to cast */
1582 /* If the expression is a function, treat it as pointer-to-function */
1583 if (IsTypeFunc (lval->Type)) {
1584 lval->Type = PointerTo (lval->Type);
1587 /* Check for a constant on the right side */
1588 if (k == 0 && lval->Flags == E_MCONST) {
1590 /* A cast of a constant to something else. If the new type is an int,
1591 * be sure to handle the size extension correctly. If the new type is
1592 * not an int, the cast is implementation specific anyway, so leave
1595 if (IsClassInt (Type)) {
1597 /* Get the current and new size of the value */
1598 unsigned OldSize = CheckedSizeOf (lval->Type);
1599 unsigned NewSize = CheckedSizeOf (Type);
1600 unsigned OldBits = OldSize * 8;
1601 unsigned NewBits = NewSize * 8;
1603 /* Check if the new datatype will have a smaller range */
1604 if (NewSize < OldSize) {
1606 /* Cut the value to the new size */
1607 lval->ConstVal &= (0xFFFFFFFFUL >> (32 - NewBits));
1609 /* If the new value is signed, sign extend the value */
1610 if (!IsSignUnsigned (Type)) {
1611 lval->ConstVal |= ((~0L) << NewBits);
1614 } else if (NewSize > OldSize) {
1616 /* Sign extend the value if needed */
1617 if (!IsSignUnsigned (Type) && !IsSignUnsigned (lval->Type)) {
1618 if (lval->ConstVal & (0x01UL << (OldBits-1))) {
1619 lval->ConstVal |= ((~0L) << OldBits);
1627 /* Not a constant. Be sure to ignore casts to void */
1628 if (!IsTypeVoid (Type)) {
1630 /* If the size does not change, leave the value alone. Otherwise,
1631 * we have to load the value into the primary and generate code to
1632 * cast the value in the primary register.
1634 if (SizeOf (Type) != SizeOf (lval->Type)) {
1636 /* Load the value into the primary */
1637 exprhs (CF_NONE, k, lval);
1639 /* Mark the lhs as const to avoid a manipulation of TOS */
1640 g_typecast (TypeOf (Type) | CF_CONST, TypeOf (lval->Type));
1642 /* Value is now in primary */
1643 lval->Flags = E_MEXPR;
1649 /* In any case, use the new type */
1650 lval->Type = TypeDup (Type);
1658 static int hie10 (ExprDesc* lval)
1659 /* Handle ++, --, !, unary - etc. */
1664 switch (CurTok.Tok) {
1667 pre_incdec (lval, g_inc);
1671 pre_incdec (lval, g_dec);
1677 unaryop (CurTok.Tok, lval);
1682 if (evalexpr (CF_NONE, hie10, lval) == 0) {
1683 /* Constant expression */
1684 lval->ConstVal = !lval->ConstVal;
1686 g_bneg (TypeOf (lval->Type));
1687 lval->Test |= E_CC; /* bneg will set cc */
1688 lval->Flags = E_MEXPR; /* say it's an expr */
1690 return 0; /* expr not storable */
1694 if (evalexpr (CF_NONE, hie10, lval) != 0) {
1695 /* Expression is not const, indirect value loaded into primary */
1696 lval->Flags = E_MEXPR;
1697 lval->ConstVal = 0; /* Offset is zero now */
1700 if (IsClassPtr (t)) {
1701 lval->Type = Indirect (t);
1703 Error ("Illegal indirection");
1710 /* The & operator may be applied to any lvalue, and it may be
1711 * applied to functions, even if they're no lvalues.
1713 if (k == 0 && !IsTypeFunc (lval->Type)) {
1714 /* Allow the & operator with an array */
1715 if (!IsTypeArray (lval->Type)) {
1716 Error ("Illegal address");
1719 t = TypeAlloc (TypeLen (lval->Type) + 2);
1721 TypeCpy (t + 1, lval->Type);
1728 if (istypeexpr ()) {
1729 type Type[MAXTYPELEN];
1731 lval->ConstVal = CheckedSizeOf (ParseType (Type));
1734 /* Remember the output queue pointer */
1735 CodeMark Mark = GetCodePos ();
1737 lval->ConstVal = CheckedSizeOf (lval->Type);
1738 /* Remove any generated code */
1741 lval->Flags = E_MCONST | E_TCONST;
1742 lval->Type = type_uint;
1743 lval->Test &= ~E_CC;
1747 if (istypeexpr ()) {
1749 return typecast (lval);
1754 switch (CurTok.Tok) {
1756 post_incdec (lval, k, g_inc);
1760 post_incdec (lval, k, g_dec);
1770 static int hie_internal (const GenDesc** ops, /* List of generators */
1771 ExprDesc* lval, /* parent expr's lval */
1772 int (*hienext) (ExprDesc*),
1773 int* UsedGen) /* next higher level */
1774 /* Helper function */
1781 token_t tok; /* The operator token */
1782 unsigned ltype, type;
1783 int rconst; /* Operand is a constant */
1789 while ((Gen = FindGen (CurTok.Tok, ops)) != 0) {
1791 /* Tell the caller that we handled it's ops */
1794 /* All operators that call this function expect an int on the lhs */
1795 if (!IsClassInt (lval->Type)) {
1796 Error ("Integer expression expected");
1799 /* Remember the operator token, then skip it */
1803 /* Get the lhs on stack */
1804 Mark1 = GetCodePos ();
1805 ltype = TypeOf (lval->Type);
1806 if (k == 0 && lval->Flags == E_MCONST) {
1807 /* Constant value */
1808 Mark2 = GetCodePos ();
1809 g_push (ltype | CF_CONST, lval->ConstVal);
1811 /* Value not constant */
1812 exprhs (CF_NONE, k, lval);
1813 Mark2 = GetCodePos ();
1817 /* Get the right hand side */
1818 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1820 /* Check the type of the rhs */
1821 if (!IsClassInt (lval2.Type)) {
1822 Error ("Integer expression expected");
1825 /* Check for const operands */
1826 if (k == 0 && lval->Flags == E_MCONST && rconst) {
1828 /* Both operands are constant, remove the generated code */
1832 /* Evaluate the result */
1833 lval->ConstVal = kcalc (tok, lval->ConstVal, lval2.ConstVal);
1835 /* Get the type of the result */
1836 lval->Type = promoteint (lval->Type, lval2.Type);
1840 /* If the right hand side is constant, and the generator function
1841 * expects the lhs in the primary, remove the push of the primary
1844 unsigned rtype = TypeOf (lval2.Type);
1847 /* Second value is constant - check for div */
1850 if (tok == TOK_DIV && lval2.ConstVal == 0) {
1851 Error ("Division by zero");
1852 } else if (tok == TOK_MOD && lval2.ConstVal == 0) {
1853 Error ("Modulo operation with zero");
1855 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1858 ltype |= CF_REG; /* Value is in register */
1862 /* Determine the type of the operation result. */
1863 type |= g_typeadjust (ltype, rtype);
1864 lval->Type = promoteint (lval->Type, lval2.Type);
1867 Gen->Func (type, lval2.ConstVal);
1868 lval->Flags = E_MEXPR;
1871 /* We have a rvalue now */
1880 static int hie_compare (const GenDesc** ops, /* List of generators */
1881 ExprDesc* lval, /* parent expr's lval */
1882 int (*hienext) (ExprDesc*))
1883 /* Helper function for the compare operators */
1890 token_t tok; /* The operator token */
1892 int rconst; /* Operand is a constant */
1897 while ((Gen = FindGen (CurTok.Tok, ops)) != 0) {
1899 /* Remember the operator token, then skip it */
1903 /* Get the lhs on stack */
1904 Mark1 = GetCodePos ();
1905 ltype = TypeOf (lval->Type);
1906 if (k == 0 && lval->Flags == E_MCONST) {
1907 /* Constant value */
1908 Mark2 = GetCodePos ();
1909 g_push (ltype | CF_CONST, lval->ConstVal);
1911 /* Value not constant */
1912 exprhs (CF_NONE, k, lval);
1913 Mark2 = GetCodePos ();
1917 /* Get the right hand side */
1918 rconst = (evalexpr (CF_NONE, hienext, &lval2) == 0);
1920 /* Make sure, the types are compatible */
1921 if (IsClassInt (lval->Type)) {
1922 if (!IsClassInt (lval2.Type) && !(IsClassPtr(lval2.Type) && IsNullPtr(lval))) {
1923 Error ("Incompatible types");
1925 } else if (IsClassPtr (lval->Type)) {
1926 if (IsClassPtr (lval2.Type)) {
1927 /* Both pointers are allowed in comparison if they point to
1928 * the same type, or if one of them is a void pointer.
1930 type* left = Indirect (lval->Type);
1931 type* right = Indirect (lval2.Type);
1932 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1933 /* Incomatible pointers */
1934 Error ("Incompatible types");
1936 } else if (!IsNullPtr (&lval2)) {
1937 Error ("Incompatible types");
1941 /* Check for const operands */
1942 if (k == 0 && lval->Flags == E_MCONST && rconst) {
1944 /* Both operands are constant, remove the generated code */
1948 /* Evaluate the result */
1949 lval->ConstVal = kcalc (tok, lval->ConstVal, lval2.ConstVal);
1953 /* If the right hand side is constant, and the generator function
1954 * expects the lhs in the primary, remove the push of the primary
1960 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1963 ltype |= CF_REG; /* Value is in register */
1967 /* Determine the type of the operation result. If the left
1968 * operand is of type char and the right is a constant, or
1969 * if both operands are of type char, we will encode the
1970 * operation as char operation. Otherwise the default
1971 * promotions are used.
1973 if (IsTypeChar (lval->Type) && (IsTypeChar (lval2.Type) || rconst)) {
1975 if (IsSignUnsigned (lval->Type) || IsSignUnsigned (lval2.Type)) {
1976 flags |= CF_UNSIGNED;
1979 flags |= CF_FORCECHAR;
1982 unsigned rtype = TypeOf (lval2.Type) | (flags & CF_CONST);
1983 flags |= g_typeadjust (ltype, rtype);
1987 Gen->Func (flags, lval2.ConstVal);
1988 lval->Flags = E_MEXPR;
1991 /* Result type is always int */
1992 lval->Type = type_int;
1994 /* We have a rvalue now, condition codes are set */
2004 static int hie9 (ExprDesc *lval)
2005 /* Process * and / operators. */
2007 static const GenDesc* hie9_ops [] = {
2008 &GenMUL, &GenDIV, &GenMOD, 0
2012 return hie_internal (hie9_ops, lval, hie10, &UsedGen);
2017 static void parseadd (int k, ExprDesc* lval)
2018 /* Parse an expression with the binary plus operator. lval contains the
2019 * unprocessed left hand side of the expression and will contain the
2020 * result of the expression on return.
2024 unsigned flags; /* Operation flags */
2025 CodeMark Mark; /* Remember code position */
2026 type* lhst; /* Type of left hand side */
2027 type* rhst; /* Type of right hand side */
2030 /* Skip the PLUS token */
2033 /* Get the left hand side type, initialize operation flags */
2037 /* Check for constness on both sides */
2038 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
2040 /* The left hand side is a constant. Good. Get rhs */
2042 if (k == 0 && lval2.Flags == E_MCONST) {
2044 /* Right hand side is also constant. Get the rhs type */
2047 /* Both expressions are constants. Check for pointer arithmetic */
2048 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2049 /* Left is pointer, right is int, must scale rhs */
2050 lval->ConstVal += lval2.ConstVal * CheckedPSizeOf (lhst);
2051 /* Result type is a pointer */
2052 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2053 /* Left is int, right is pointer, must scale lhs */
2054 lval->ConstVal = lval->ConstVal * CheckedPSizeOf (rhst) + lval2.ConstVal;
2055 /* Result type is a pointer */
2056 lval->Type = lval2.Type;
2057 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2058 /* Integer addition */
2059 lval->ConstVal += lval2.ConstVal;
2060 typeadjust (lval, &lval2, 1);
2063 Error ("Invalid operands for binary operator `+'");
2066 /* Result is constant, condition codes not set */
2067 lval->Test &= ~E_CC;
2071 /* lhs is a constant and rhs is not constant. Load rhs into
2074 exprhs (CF_NONE, k, &lval2);
2076 /* Beware: The check above (for lhs) lets not only pass numeric
2077 * constants, but also constant addresses (labels), maybe even
2078 * with an offset. We have to check for that here.
2081 /* First, get the rhs type. */
2085 if (lval->Flags == E_MCONST) {
2086 /* A numerical constant */
2089 /* Constant address label */
2090 flags |= GlobalModeFlags (lval->Flags) | CF_CONSTADDR;
2093 /* Check for pointer arithmetic */
2094 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2095 /* Left is pointer, right is int, must scale rhs */
2096 g_scale (CF_INT, CheckedPSizeOf (lhst));
2097 /* Operate on pointers, result type is a pointer */
2099 /* Generate the code for the add */
2100 if (lval->Flags == E_MCONST) {
2101 /* Numeric constant */
2102 g_inc (flags, lval->ConstVal);
2104 /* Constant address */
2105 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2107 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2109 /* Left is int, right is pointer, must scale lhs. */
2110 unsigned ScaleFactor = CheckedPSizeOf (rhst);
2112 /* Operate on pointers, result type is a pointer */
2114 lval->Type = lval2.Type;
2116 /* Since we do already have rhs in the primary, if lhs is
2117 * not a numeric constant, and the scale factor is not one
2118 * (no scaling), we must take the long way over the stack.
2120 if (lval->Flags == E_MCONST) {
2121 /* Numeric constant, scale lhs */
2122 lval->ConstVal *= ScaleFactor;
2123 /* Generate the code for the add */
2124 g_inc (flags, lval->ConstVal);
2125 } else if (ScaleFactor == 1) {
2126 /* Constant address but no need to scale */
2127 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2129 /* Constant address that must be scaled */
2130 g_push (TypeOf (lval2.Type), 0); /* rhs --> stack */
2131 g_getimmed (flags, lval->Name, lval->ConstVal);
2132 g_scale (CF_PTR, ScaleFactor);
2135 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2136 /* Integer addition */
2137 flags |= typeadjust (lval, &lval2, 1);
2138 /* Generate the code for the add */
2139 if (lval->Flags == E_MCONST) {
2140 /* Numeric constant */
2141 g_inc (flags, lval->ConstVal);
2143 /* Constant address */
2144 g_addaddr_static (flags, lval->Name, lval->ConstVal);
2148 Error ("Invalid operands for binary operator `+'");
2151 /* Result is in primary register */
2152 lval->Flags = E_MEXPR;
2153 lval->Test &= ~E_CC;
2159 /* Left hand side is not constant. Get the value onto the stack. */
2160 exprhs (CF_NONE, k, lval); /* --> primary register */
2161 Mark = GetCodePos ();
2162 g_push (TypeOf (lval->Type), 0); /* --> stack */
2164 /* Evaluate the rhs */
2165 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2167 /* Right hand side is a constant. Get the rhs type */
2170 /* Remove pushed value from stack */
2172 pop (TypeOf (lval->Type));
2174 /* Check for pointer arithmetic */
2175 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2176 /* Left is pointer, right is int, must scale rhs */
2177 lval2.ConstVal *= CheckedPSizeOf (lhst);
2178 /* Operate on pointers, result type is a pointer */
2180 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2181 /* Left is int, right is pointer, must scale lhs (ptr only) */
2182 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2183 /* Operate on pointers, result type is a pointer */
2185 lval->Type = lval2.Type;
2186 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2187 /* Integer addition */
2188 flags = typeadjust (lval, &lval2, 1);
2191 Error ("Invalid operands for binary operator `+'");
2194 /* Generate code for the add */
2195 g_inc (flags | CF_CONST, lval2.ConstVal);
2197 /* Result is in primary register */
2198 lval->Flags = E_MEXPR;
2199 lval->Test &= ~E_CC;
2203 /* lhs and rhs are not constant. Get the rhs type. */
2206 /* Check for pointer arithmetic */
2207 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2208 /* Left is pointer, right is int, must scale rhs */
2209 g_scale (CF_INT, CheckedPSizeOf (lhst));
2210 /* Operate on pointers, result type is a pointer */
2212 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2213 /* Left is int, right is pointer, must scale lhs */
2214 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2215 g_swap (CF_INT); /* Swap TOS and primary */
2216 g_scale (CF_INT, CheckedPSizeOf (rhst));
2217 /* Operate on pointers, result type is a pointer */
2219 lval->Type = lval2.Type;
2220 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2221 /* Integer addition */
2222 flags = typeadjust (lval, &lval2, 0);
2225 Error ("Invalid operands for binary operator `+'");
2228 /* Generate code for the add */
2231 /* Result is in primary register */
2232 lval->Flags = E_MEXPR;
2233 lval->Test &= ~E_CC;
2242 static void parsesub (int k, ExprDesc* lval)
2243 /* Parse an expression with the binary minus operator. lval contains the
2244 * unprocessed left hand side of the expression and will contain the
2245 * result of the expression on return.
2249 unsigned flags; /* Operation flags */
2250 type* lhst; /* Type of left hand side */
2251 type* rhst; /* Type of right hand side */
2252 CodeMark Mark1; /* Save position of output queue */
2253 CodeMark Mark2; /* Another position in the queue */
2254 int rscale; /* Scale factor for the result */
2257 /* Skip the MINUS token */
2260 /* Get the left hand side type, initialize operation flags */
2263 rscale = 1; /* Scale by 1, that is, don't scale */
2265 /* Remember the output queue position, then bring the value onto the stack */
2266 Mark1 = GetCodePos ();
2267 exprhs (CF_NONE, k, lval); /* --> primary register */
2268 Mark2 = GetCodePos ();
2269 g_push (TypeOf (lhst), 0); /* --> stack */
2271 /* Parse the right hand side */
2272 if (evalexpr (CF_NONE, hie9, &lval2) == 0) {
2274 /* The right hand side is constant. Get the rhs type. */
2277 /* Check left hand side */
2278 if (k == 0 && (lval->Flags & E_MCONST) != 0) {
2280 /* Both sides are constant, remove generated code */
2282 pop (TypeOf (lhst)); /* Clean up the stack */
2284 /* Check for pointer arithmetic */
2285 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2286 /* Left is pointer, right is int, must scale rhs */
2287 lval->ConstVal -= lval2.ConstVal * CheckedPSizeOf (lhst);
2288 /* Operate on pointers, result type is a pointer */
2289 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2290 /* Left is pointer, right is pointer, must scale result */
2291 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2292 Error ("Incompatible pointer types");
2294 lval->ConstVal = (lval->ConstVal - lval2.ConstVal) /
2295 CheckedPSizeOf (lhst);
2297 /* Operate on pointers, result type is an integer */
2298 lval->Type = type_int;
2299 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2300 /* Integer subtraction */
2301 typeadjust (lval, &lval2, 1);
2302 lval->ConstVal -= lval2.ConstVal;
2305 Error ("Invalid operands for binary operator `-'");
2308 /* Result is constant, condition codes not set */
2309 /* lval->Flags = E_MCONST; ### */
2310 lval->Test &= ~E_CC;
2314 /* Left hand side is not constant, right hand side is.
2315 * Remove pushed value from stack.
2318 pop (TypeOf (lhst));
2320 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2321 /* Left is pointer, right is int, must scale rhs */
2322 lval2.ConstVal *= CheckedPSizeOf (lhst);
2323 /* Operate on pointers, result type is a pointer */
2325 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2326 /* Left is pointer, right is pointer, must scale result */
2327 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2328 Error ("Incompatible pointer types");
2330 rscale = CheckedPSizeOf (lhst);
2332 /* Operate on pointers, result type is an integer */
2334 lval->Type = type_int;
2335 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2336 /* Integer subtraction */
2337 flags = typeadjust (lval, &lval2, 1);
2340 Error ("Invalid operands for binary operator `-'");
2343 /* Do the subtraction */
2344 g_dec (flags | CF_CONST, lval2.ConstVal);
2346 /* If this was a pointer subtraction, we must scale the result */
2348 g_scale (flags, -rscale);
2351 /* Result is in primary register */
2352 lval->Flags = E_MEXPR;
2353 lval->Test &= ~E_CC;
2359 /* Right hand side is not constant. Get the rhs type. */
2362 /* Check for pointer arithmetic */
2363 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2364 /* Left is pointer, right is int, must scale rhs */
2365 g_scale (CF_INT, CheckedPSizeOf (lhst));
2366 /* Operate on pointers, result type is a pointer */
2368 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2369 /* Left is pointer, right is pointer, must scale result */
2370 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2371 Error ("Incompatible pointer types");
2373 rscale = CheckedPSizeOf (lhst);
2375 /* Operate on pointers, result type is an integer */
2377 lval->Type = type_int;
2378 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2379 /* Integer subtraction. If the left hand side descriptor says that
2380 * the lhs is const, we have to remove this mark, since this is no
2381 * longer true, lhs is on stack instead.
2383 if (lval->Flags == E_MCONST) {
2384 lval->Flags = E_MEXPR;
2386 /* Adjust operand types */
2387 flags = typeadjust (lval, &lval2, 0);
2390 Error ("Invalid operands for binary operator `-'");
2393 /* Generate code for the sub (the & is a hack here) */
2394 g_sub (flags & ~CF_CONST, 0);
2396 /* If this was a pointer subtraction, we must scale the result */
2398 g_scale (flags, -rscale);
2401 /* Result is in primary register */
2402 lval->Flags = E_MEXPR;
2403 lval->Test &= ~E_CC;
2409 static int hie8 (ExprDesc* lval)
2410 /* Process + and - binary operators. */
2412 int k = hie9 (lval);
2413 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2415 if (CurTok.Tok == TOK_PLUS) {
2428 static int hie7 (ExprDesc *lval)
2429 /* Parse << and >>. */
2431 static const GenDesc* hie7_ops [] = {
2436 return hie_internal (hie7_ops, lval, hie8, &UsedGen);
2441 static int hie6 (ExprDesc *lval)
2442 /* process greater-than type comparators */
2444 static const GenDesc* hie6_ops [] = {
2445 &GenLT, &GenLE, &GenGE, &GenGT, 0
2447 return hie_compare (hie6_ops, lval, hie7);
2452 static int hie5 (ExprDesc *lval)
2454 static const GenDesc* hie5_ops[] = {
2457 return hie_compare (hie5_ops, lval, hie6);
2462 static int hie4 (ExprDesc* lval)
2463 /* Handle & (bitwise and) */
2465 static const GenDesc* hie4_ops [] = {
2470 return hie_internal (hie4_ops, lval, hie5, &UsedGen);
2475 static int hie3 (ExprDesc *lval)
2476 /* Handle ^ (bitwise exclusive or) */
2478 static const GenDesc* hie3_ops [] = {
2483 return hie_internal (hie3_ops, lval, hie4, &UsedGen);
2488 static int hie2 (ExprDesc *lval)
2489 /* Handle | (bitwise or) */
2491 static const GenDesc* hie2_ops [] = {
2496 return hie_internal (hie2_ops, lval, hie3, &UsedGen);
2501 static int hieAndPP (ExprDesc* lval)
2502 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2503 * called recursively from the preprocessor.
2508 ConstSubExpr (hie2, lval);
2509 while (CurTok.Tok == TOK_BOOL_AND) {
2511 /* Left hand side must be an int */
2512 if (!IsClassInt (lval->Type)) {
2513 Error ("Left hand side must be of integer type");
2514 MakeConstIntExpr (lval, 1);
2521 ConstSubExpr (hie2, &lval2);
2523 /* Since we are in PP mode, all we know about is integers */
2524 if (!IsClassInt (lval2.Type)) {
2525 Error ("Right hand side must be of integer type");
2526 MakeConstIntExpr (&lval2, 1);
2529 /* Combine the two */
2530 lval->ConstVal = (lval->ConstVal && lval2.ConstVal);
2533 /* Always a rvalue */
2539 static int hieOrPP (ExprDesc *lval)
2540 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2541 * called recursively from the preprocessor.
2546 ConstSubExpr (hieAndPP, lval);
2547 while (CurTok.Tok == TOK_BOOL_OR) {
2549 /* Left hand side must be an int */
2550 if (!IsClassInt (lval->Type)) {
2551 Error ("Left hand side must be of integer type");
2552 MakeConstIntExpr (lval, 1);
2559 ConstSubExpr (hieAndPP, &lval2);
2561 /* Since we are in PP mode, all we know about is integers */
2562 if (!IsClassInt (lval2.Type)) {
2563 Error ("Right hand side must be of integer type");
2564 MakeConstIntExpr (&lval2, 1);
2567 /* Combine the two */
2568 lval->ConstVal = (lval->ConstVal || lval2.ConstVal);
2571 /* Always a rvalue */
2577 static int hieAnd (ExprDesc* lval, unsigned TrueLab, int* BoolOp)
2578 /* Process "exp && exp" */
2585 if (CurTok.Tok == TOK_BOOL_AND) {
2587 /* Tell our caller that we're evaluating a boolean */
2590 /* Get a label that we will use for false expressions */
2591 lab = GetLocalLabel ();
2593 /* If the expr hasn't set condition codes, set the force-test flag */
2594 if ((lval->Test & E_CC) == 0) {
2595 lval->Test |= E_FORCETEST;
2598 /* Load the value */
2599 exprhs (CF_FORCECHAR, k, lval);
2601 /* Generate the jump */
2602 g_falsejump (CF_NONE, lab);
2604 /* Parse more boolean and's */
2605 while (CurTok.Tok == TOK_BOOL_AND) {
2612 if ((lval2.Test & E_CC) == 0) {
2613 lval2.Test |= E_FORCETEST;
2615 exprhs (CF_FORCECHAR, k, &lval2);
2617 /* Do short circuit evaluation */
2618 if (CurTok.Tok == TOK_BOOL_AND) {
2619 g_falsejump (CF_NONE, lab);
2621 /* Last expression - will evaluate to true */
2622 g_truejump (CF_NONE, TrueLab);
2626 /* Define the false jump label here */
2627 g_defcodelabel (lab);
2629 /* Define the label */
2630 lval->Flags = E_MEXPR;
2631 lval->Test |= E_CC; /* Condition codes are set */
2639 static int hieOr (ExprDesc *lval)
2640 /* Process "exp || exp". */
2644 int BoolOp = 0; /* Did we have a boolean op? */
2645 int AndOp; /* Did we have a && operation? */
2646 unsigned TrueLab; /* Jump to this label if true */
2650 TrueLab = GetLocalLabel ();
2652 /* Call the next level parser */
2653 k = hieAnd (lval, TrueLab, &BoolOp);
2655 /* Any boolean or's? */
2656 if (CurTok.Tok == TOK_BOOL_OR) {
2658 /* If the expr hasn't set condition codes, set the force-test flag */
2659 if ((lval->Test & E_CC) == 0) {
2660 lval->Test |= E_FORCETEST;
2663 /* Get first expr */
2664 exprhs (CF_FORCECHAR, k, lval);
2666 /* For each expression jump to TrueLab if true. Beware: If we
2667 * had && operators, the jump is already in place!
2670 g_truejump (CF_NONE, TrueLab);
2673 /* Remember that we had a boolean op */
2676 /* while there's more expr */
2677 while (CurTok.Tok == TOK_BOOL_OR) {
2684 k = hieAnd (&lval2, TrueLab, &AndOp);
2685 if ((lval2.Test & E_CC) == 0) {
2686 lval2.Test |= E_FORCETEST;
2688 exprhs (CF_FORCECHAR, k, &lval2);
2690 /* If there is more to come, add shortcut boolean eval. */
2691 g_truejump (CF_NONE, TrueLab);
2694 lval->Flags = E_MEXPR;
2695 lval->Test |= E_CC; /* Condition codes are set */
2699 /* If we really had boolean ops, generate the end sequence */
2701 DoneLab = GetLocalLabel ();
2702 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2703 g_falsejump (CF_NONE, DoneLab);
2704 g_defcodelabel (TrueLab);
2705 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2706 g_defcodelabel (DoneLab);
2713 static int hieQuest (ExprDesc *lval)
2714 /* Parse "lvalue ? exp : exp" */
2719 ExprDesc lval2; /* Expression 2 */
2720 ExprDesc lval3; /* Expression 3 */
2721 type* type2; /* Type of expression 2 */
2722 type* type3; /* Type of expression 3 */
2723 type* rtype; /* Type of result */
2724 CodeMark Mark1; /* Save position in output code */
2725 CodeMark Mark2; /* Save position in output code */
2729 k = Preprocessing? hieOrPP (lval) : hieOr (lval);
2730 if (CurTok.Tok == TOK_QUEST) {
2732 if ((lval->Test & E_CC) == 0) {
2733 /* Condition codes not set, force a test */
2734 lval->Test |= E_FORCETEST;
2736 exprhs (CF_NONE, k, lval);
2737 labf = GetLocalLabel ();
2738 g_falsejump (CF_NONE, labf);
2740 /* Parse second and third expression */
2741 expression1 (&lval2);
2742 labt = GetLocalLabel ();
2745 g_defcodelabel (labf);
2746 expression1 (&lval3);
2748 /* Check if any conversions are needed, if so, do them.
2749 * Conversion rules for ?: expression are:
2750 * - if both expressions are int expressions, default promotion
2751 * rules for ints apply.
2752 * - if both expressions are pointers of the same type, the
2753 * result of the expression is of this type.
2754 * - if one of the expressions is a pointer and the other is
2755 * a zero constant, the resulting type is that of the pointer
2757 * - all other cases are flagged by an error.
2761 if (IsClassInt (type2) && IsClassInt (type3)) {
2763 /* Get common type */
2764 rtype = promoteint (type2, type3);
2766 /* Convert the third expression to this type if needed */
2767 g_typecast (TypeOf (rtype), TypeOf (type3));
2769 /* Setup a new label so that the expr3 code will jump around
2770 * the type cast code for expr2.
2772 labf = GetLocalLabel (); /* Get new label */
2773 Mark1 = GetCodePos (); /* Remember current position */
2774 g_jump (labf); /* Jump around code */
2776 /* The jump for expr2 goes here */
2777 g_defcodelabel (labt);
2779 /* Create the typecast code for expr2 */
2780 Mark2 = GetCodePos (); /* Remember position */
2781 g_typecast (TypeOf (rtype), TypeOf (type2));
2783 /* Jump here around the typecase code. */
2784 g_defcodelabel (labf);
2785 labt = 0; /* Mark other label as invalid */
2787 } else if (IsClassPtr (type2) && IsClassPtr (type3)) {
2788 /* Must point to same type */
2789 if (TypeCmp (Indirect (type2), Indirect (type3)) < TC_EQUAL) {
2790 Error ("Incompatible pointer types");
2792 /* Result has the common type */
2794 } else if (IsClassPtr (type2) && IsNullPtr (&lval3)) {
2795 /* Result type is pointer, no cast needed */
2797 } else if (IsNullPtr (&lval2) && IsClassPtr (type3)) {
2798 /* Result type is pointer, no cast needed */
2801 Error ("Incompatible types");
2802 rtype = lval2.Type; /* Doesn't matter here */
2805 /* If we don't have the label defined until now, do it */
2807 g_defcodelabel (labt);
2810 /* Setup the target expression */
2811 lval->Flags = E_MEXPR;
2820 static void opeq (const GenDesc* Gen, ExprDesc *lval, int k)
2821 /* Process "op=" operators. */
2830 Error ("Invalid lvalue in assignment");
2834 /* Determine the type of the lhs */
2835 flags = TypeOf (lval->Type);
2836 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) &&
2837 lval->Type [0] == T_PTR;
2839 /* Get the lhs address on stack (if needed) */
2842 /* Fetch the lhs into the primary register if needed */
2843 exprhs (CF_NONE, k, lval);
2845 /* Bring the lhs on stack */
2846 Mark = GetCodePos ();
2849 /* Evaluate the rhs */
2850 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2851 /* The resulting value is a constant. If the generator has the NOPUSH
2852 * flag set, don't push the lhs.
2854 if (Gen->Flags & GEN_NOPUSH) {
2859 /* lhs is a pointer, scale rhs */
2860 lval2.ConstVal *= CheckedSizeOf (lval->Type+1);
2863 /* If the lhs is character sized, the operation may be later done
2866 if (CheckedSizeOf (lval->Type) == 1) {
2867 flags |= CF_FORCECHAR;
2870 /* Special handling for add and sub - some sort of a hack, but short code */
2871 if (Gen->Func == g_add) {
2872 g_inc (flags | CF_CONST, lval2.ConstVal);
2873 } else if (Gen->Func == g_sub) {
2874 g_dec (flags | CF_CONST, lval2.ConstVal);
2876 Gen->Func (flags | CF_CONST, lval2.ConstVal);
2879 /* rhs is not constant and already in the primary register */
2881 /* lhs is a pointer, scale rhs */
2882 g_scale (TypeOf (lval2.Type), CheckedSizeOf (lval->Type+1));
2885 /* If the lhs is character sized, the operation may be later done
2888 if (CheckedSizeOf (lval->Type) == 1) {
2889 flags |= CF_FORCECHAR;
2892 /* Adjust the types of the operands if needed */
2893 Gen->Func (g_typeadjust (flags, TypeOf (lval2.Type)), 0);
2896 lval->Flags = E_MEXPR;
2901 static void addsubeq (const GenDesc* Gen, ExprDesc *lval, int k)
2902 /* Process the += and -= operators */
2910 /* We must have an lvalue */
2912 Error ("Invalid lvalue in assignment");
2916 /* We're currently only able to handle some adressing modes */
2917 if ((lval->Flags & E_MGLOBAL) == 0 && /* Global address? */
2918 (lval->Flags & E_MLOCAL) == 0 && /* Local address? */
2919 (lval->Flags & E_MCONST) == 0) { /* Constant address? */
2920 /* Use generic routine */
2921 opeq (Gen, lval, k);
2925 /* Skip the operator */
2928 /* Check if we have a pointer expression and must scale rhs */
2929 MustScale = (lval->Type [0] == T_PTR);
2931 /* Initialize the code generator flags */
2935 /* Evaluate the rhs */
2936 if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
2937 /* The resulting value is a constant. */
2939 /* lhs is a pointer, scale rhs */
2940 lval2.ConstVal *= CheckedSizeOf (lval->Type+1);
2945 /* rhs is not constant and already in the primary register */
2947 /* lhs is a pointer, scale rhs */
2948 g_scale (TypeOf (lval2.Type), CheckedSizeOf (lval->Type+1));
2952 /* Setup the code generator flags */
2953 lflags |= TypeOf (lval->Type) | CF_FORCECHAR;
2954 rflags |= TypeOf (lval2.Type);
2956 /* Cast the rhs to the type of the lhs */
2957 g_typecast (lflags, rflags);
2959 /* Output apropriate code */
2960 if (lval->Flags & E_MGLOBAL) {
2961 /* Static variable */
2962 lflags |= GlobalModeFlags (lval->Flags);
2963 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2964 g_addeqstatic (lflags, lval->Name, lval->ConstVal, lval2.ConstVal);
2966 g_subeqstatic (lflags, lval->Name, lval->ConstVal, lval2.ConstVal);
2968 } else if (lval->Flags & E_MLOCAL) {
2969 /* ref to localvar */
2970 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2971 g_addeqlocal (lflags, lval->ConstVal, lval2.ConstVal);
2973 g_subeqlocal (lflags, lval->ConstVal, lval2.ConstVal);
2975 } else if (lval->Flags & E_MCONST) {
2976 /* ref to absolute address */
2977 lflags |= CF_ABSOLUTE;
2978 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2979 g_addeqstatic (lflags, lval->ConstVal, 0, lval2.ConstVal);
2981 g_subeqstatic (lflags, lval->ConstVal, 0, lval2.ConstVal);
2983 } else if (lval->Flags & E_MEXPR) {
2984 /* Address in a/x. */
2985 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2986 g_addeqind (lflags, lval->ConstVal, lval2.ConstVal);
2988 g_subeqind (lflags, lval->ConstVal, lval2.ConstVal);
2991 Internal ("Invalid addressing mode");
2994 /* Expression is in the primary now */
2995 lval->Flags = E_MEXPR;
3000 static void Assignment (ExprDesc* lval)
3001 /* Parse an assignment */
3006 type* ltype = lval->Type;
3008 /* Check for assignment to const */
3009 if (IsQualConst (ltype)) {
3010 Error ("Assignment to const");
3013 /* cc65 does not have full support for handling structs by value. Since
3014 * assigning structs is one of the more useful operations from this
3015 * family, allow it here.
3017 if (IsClassStruct (ltype)) {
3019 /* Bring the address of the lhs into the primary and push it */
3020 exprhs (0, 0, lval);
3021 g_push (CF_PTR | CF_UNSIGNED, 0);
3023 /* Get the expression on the right of the '=' into the primary */
3026 /* Get the address */
3027 exprhs (0, 0, &lval2);
3030 /* Push the address (or whatever is in ax in case of errors) */
3031 g_push (CF_PTR | CF_UNSIGNED, 0);
3033 /* Check for equality of the structs */
3034 if (TypeCmp (ltype, lval2.Type) < TC_STRICT_COMPATIBLE) {
3035 Error ("Incompatible types");
3038 /* Load the size of the struct into the primary */
3039 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, CheckedSizeOf (ltype), 0);
3041 /* Call the memcpy function */
3042 g_call (CF_FIXARGC, "memcpy", 4);
3046 /* Get the address on stack if needed */
3049 /* No struct, setup flags for the load */
3050 flags = CheckedSizeOf (ltype) == 1? CF_FORCECHAR : CF_NONE;
3052 /* Get the expression on the right of the '=' into the primary */
3053 if (evalexpr (flags, hie1, &lval2) == 0) {
3054 /* Constant expression. Adjust the types */
3055 assignadjust (ltype, &lval2);
3056 /* Put the value into the primary register */
3057 lconst (flags, &lval2);
3059 /* Expression is not constant and already in the primary */
3060 assignadjust (ltype, &lval2);
3063 /* Generate a store instruction */
3068 /* Value is still in primary */
3069 lval->Flags = E_MEXPR;
3074 int hie1 (ExprDesc* lval)
3075 /* Parse first level of expression hierarchy. */
3079 k = hieQuest (lval);
3080 switch (CurTok.Tok) {
3089 Error ("Invalid lvalue in assignment");
3095 case TOK_PLUS_ASSIGN:
3096 addsubeq (&GenPASGN, lval, k);
3099 case TOK_MINUS_ASSIGN:
3100 addsubeq (&GenSASGN, lval, k);
3103 case TOK_MUL_ASSIGN:
3104 opeq (&GenMASGN, lval, k);
3107 case TOK_DIV_ASSIGN:
3108 opeq (&GenDASGN, lval, k);
3111 case TOK_MOD_ASSIGN:
3112 opeq (&GenMOASGN, lval, k);
3115 case TOK_SHL_ASSIGN:
3116 opeq (&GenSLASGN, lval, k);
3119 case TOK_SHR_ASSIGN:
3120 opeq (&GenSRASGN, lval, k);
3123 case TOK_AND_ASSIGN:
3124 opeq (&GenAASGN, lval, k);
3127 case TOK_XOR_ASSIGN:
3128 opeq (&GenXOASGN, lval, k);
3132 opeq (&GenOASGN, lval, k);
3143 int hie0 (ExprDesc *lval)
3144 /* Parse comma operator. */
3149 while (CurTok.Tok == TOK_COMMA) {
3158 int evalexpr (unsigned flags, int (*f) (ExprDesc*), ExprDesc* lval)
3159 /* Will evaluate an expression via the given function. If the result is a
3160 * constant, 0 is returned and the value is put in the lval struct. If the
3161 * result is not constant, exprhs is called to bring the value into the
3162 * primary register and 1 is returned.
3169 if (k == 0 && lval->Flags == E_MCONST) {
3170 /* Constant expression */
3173 /* Not constant, load into the primary */
3174 exprhs (flags, k, lval);
3181 int expr (int (*func) (ExprDesc*), ExprDesc *lval)
3182 /* Expression parser; func is either hie0 or hie1. */
3191 /* Do some checks if code generation is still constistent */
3192 if (savsp != oursp) {
3194 fprintf (stderr, "oursp != savesp (%d != %d)\n", oursp, savsp);
3196 Internal ("oursp != savsp (%d != %d)", oursp, savsp);
3204 void expression1 (ExprDesc* lval)
3205 /* Evaluate an expression on level 1 (no comma operator) and put it into
3206 * the primary register
3209 memset (lval, 0, sizeof (*lval));
3210 exprhs (CF_NONE, expr (hie1, lval), lval);
3215 void expression (ExprDesc* lval)
3216 /* Evaluate an expression and put it into the primary register */
3218 memset (lval, 0, sizeof (*lval));
3219 exprhs (CF_NONE, expr (hie0, lval), lval);
3224 void ConstExpr (ExprDesc* lval)
3225 /* Get a constant value */
3227 memset (lval, 0, sizeof (*lval));
3228 if (expr (hie1, lval) != 0 || (lval->Flags & E_MCONST) == 0) {
3229 Error ("Constant expression expected");
3230 /* To avoid any compiler errors, make the expression a valid const */
3231 MakeConstIntExpr (lval, 1);
3237 void ConstIntExpr (ExprDesc* Val)
3238 /* Get a constant int value */
3240 memset (Val, 0, sizeof (*Val));
3241 if (expr (hie1, Val) != 0 ||
3242 (Val->Flags & E_MCONST) == 0 ||
3243 !IsClassInt (Val->Type)) {
3244 Error ("Constant integer expression expected");
3245 /* To avoid any compiler errors, make the expression a valid const */
3246 MakeConstIntExpr (Val, 1);
3252 void intexpr (ExprDesc* lval)
3253 /* Get an integer expression */
3256 if (!IsClassInt (lval->Type)) {
3257 Error ("Integer expression expected");
3258 /* To avoid any compiler errors, make the expression a valid int */
3259 MakeConstIntExpr (lval, 1);
3265 void boolexpr (ExprDesc* lval)
3266 /* Get a boolean expression */
3268 /* Read an expression */
3271 /* If it's an integer, it's ok. If it's not an integer, but a pointer,
3272 * the pointer used in a boolean context is also ok
3274 if (!IsClassInt (lval->Type) && !IsClassPtr (lval->Type)) {
3275 Error ("Boolean expression expected");
3276 /* To avoid any compiler errors, make the expression a valid int */
3277 MakeConstIntExpr (lval, 1);
3283 void test (unsigned label, int cond)
3284 /* Generate code to perform test and jump if false. */
3289 /* Eat the parenthesis */
3292 /* Prepare the expression, setup labels */
3293 memset (&lval, 0, sizeof (lval));
3295 /* Generate code to eval the expr */
3296 k = expr (hie0, &lval);
3297 if (k == 0 && lval.Flags == E_MCONST) {
3298 /* Constant rvalue */
3299 if (cond == 0 && lval.ConstVal == 0) {
3301 Warning ("Unreachable code");
3302 } else if (cond && lval.ConstVal) {
3309 /* If the expr hasn't set condition codes, set the force-test flag */
3310 if ((lval.Test & E_CC) == 0) {
3311 lval.Test |= E_FORCETEST;
3314 /* Load the value into the primary register */
3315 exprhs (CF_FORCECHAR, k, &lval);
3317 /* Generate the jump */
3319 g_truejump (CF_NONE, label);
3321 /* Special case (putting this here is a small hack - but hey, the
3322 * compiler itself is one big hack...): If a semicolon follows, we
3323 * don't have a statement and may omit the jump.
3325 if (CurTok.Tok != TOK_SEMI) {
3326 g_falsejump (CF_NONE, label);
3330 /* Check for the closing brace */