3 * Ullrich von Bassewitz, 21.06.1998
13 #include "debugflag.h"
20 #include "assignment.h"
32 #include "shiftexpr.h"
42 /*****************************************************************************/
44 /*****************************************************************************/
48 /* Generator attributes */
49 #define GEN_NOPUSH 0x01 /* Don't push lhs */
51 /* Map a generator function and its attributes to a token */
53 token_t Tok; /* Token to map to */
54 unsigned Flags; /* Flags for generator function */
55 void (*Func) (unsigned, unsigned long); /* Generator func */
58 /* Descriptors for the operations */
59 static GenDesc GenPASGN = { TOK_PLUS_ASSIGN, GEN_NOPUSH, g_add };
60 static GenDesc GenSASGN = { TOK_MINUS_ASSIGN, GEN_NOPUSH, g_sub };
61 static GenDesc GenMASGN = { TOK_MUL_ASSIGN, GEN_NOPUSH, g_mul };
62 static GenDesc GenDASGN = { TOK_DIV_ASSIGN, GEN_NOPUSH, g_div };
63 static GenDesc GenMOASGN = { TOK_MOD_ASSIGN, GEN_NOPUSH, g_mod };
64 static GenDesc GenSLASGN = { TOK_SHL_ASSIGN, GEN_NOPUSH, g_asl };
65 static GenDesc GenSRASGN = { TOK_SHR_ASSIGN, GEN_NOPUSH, g_asr };
66 static GenDesc GenAASGN = { TOK_AND_ASSIGN, GEN_NOPUSH, g_and };
67 static GenDesc GenXOASGN = { TOK_XOR_ASSIGN, GEN_NOPUSH, g_xor };
68 static GenDesc GenOASGN = { TOK_OR_ASSIGN, GEN_NOPUSH, g_or };
72 /*****************************************************************************/
73 /* Helper functions */
74 /*****************************************************************************/
78 static unsigned GlobalModeFlags (unsigned Flags)
79 /* Return the addressing mode flags for the variable with the given flags */
81 switch (Flags & E_MASK_LOC) {
82 case E_LOC_GLOBAL: return CF_EXTERNAL;
83 case E_LOC_STATIC: return CF_STATIC;
84 case E_LOC_REGISTER: return CF_REGVAR;
86 Internal ("GlobalModeFlags: Invalid flags value: %u", Flags);
94 void ExprWithCheck (void (*Func) (ExprDesc*), ExprDesc *Expr)
95 /* Call an expression function with checks. */
97 /* Remember the stack pointer */
100 /* Call the expression function */
103 /* Do some checks if code generation is still constistent */
104 if (StackPtr != OldSP) {
107 "Code generation messed up!\n"
108 "StackPtr is %d, should be %d",
111 Internal ("StackPtr is %d, should be %d\n", StackPtr, OldSP);
118 static type* promoteint (type* lhst, type* rhst)
119 /* In an expression with two ints, return the type of the result */
121 /* Rules for integer types:
122 * - If one of the values is a long, the result is long.
123 * - If one of the values is unsigned, the result is also unsigned.
124 * - Otherwise the result is an int.
126 if (IsTypeLong (lhst) || IsTypeLong (rhst)) {
127 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
133 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
143 static unsigned typeadjust (ExprDesc* lhs, ExprDesc* rhs, int NoPush)
144 /* Adjust the two values for a binary operation. lhs is expected on stack or
145 * to be constant, rhs is expected to be in the primary register or constant.
146 * The function will put the type of the result into lhs and return the
147 * code generator flags for the operation.
148 * If NoPush is given, it is assumed that the operation does not expect the lhs
149 * to be on stack, and that lhs is in a register instead.
150 * Beware: The function does only accept int types.
153 unsigned ltype, rtype;
156 /* Get the type strings */
157 type* lhst = lhs->Type;
158 type* rhst = rhs->Type;
160 /* Generate type adjustment code if needed */
161 ltype = TypeOf (lhst);
162 if (ED_IsLocAbs (lhs)) {
166 /* Value is in primary register*/
169 rtype = TypeOf (rhst);
170 if (ED_IsLocAbs (rhs)) {
173 flags = g_typeadjust (ltype, rtype);
175 /* Set the type of the result */
176 lhs->Type = promoteint (lhst, rhst);
178 /* Return the code generator flags */
184 void DefineData (ExprDesc* Expr)
185 /* Output a data definition for the given expression */
187 switch (ED_GetLoc (Expr)) {
190 /* Absolute: numeric address or const */
191 g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->IVal, 0);
195 /* Global variable */
196 g_defdata (CF_EXTERNAL, Expr->Name, Expr->IVal);
201 /* Static variable or literal in the literal pool */
202 g_defdata (CF_STATIC, Expr->Name, Expr->IVal);
206 /* Register variable. Taking the address is usually not
209 if (IS_Get (&AllowRegVarAddr) == 0) {
210 Error ("Cannot take the address of a register variable");
212 g_defdata (CF_REGVAR, Expr->Name, Expr->IVal);
216 Internal ("Unknown constant type: 0x%04X", ED_GetLoc (Expr));
222 static int kcalc (token_t tok, long val1, long val2)
223 /* Calculate an operation with left and right operand constant. */
227 return (val1 == val2);
229 return (val1 != val2);
231 return (val1 < val2);
233 return (val1 <= val2);
235 return (val1 >= val2);
237 return (val1 > val2);
239 return (val1 | val2);
241 return (val1 ^ val2);
243 return (val1 & val2);
245 return (val1 * val2);
248 Error ("Division by zero");
251 return (val1 / val2);
254 Error ("Modulo operation with zero");
257 return (val1 % val2);
259 Internal ("kcalc: got token 0x%X\n", tok);
266 static const GenDesc* FindGen (token_t Tok, const GenDesc* Table)
267 /* Find a token in a generator table */
269 while (Table->Tok != TOK_INVALID) {
270 if (Table->Tok == Tok) {
280 static int TypeSpecAhead (void)
281 /* Return true if some sort of type is waiting (helper for cast and sizeof()
287 /* There's a type waiting if:
289 * We have an opening paren, and
290 * a. the next token is a type, or
291 * b. the next token is a type qualifier, or
292 * c. the next token is a typedef'd type
294 return CurTok.Tok == TOK_LPAREN && (
295 TokIsType (&NextTok) ||
296 TokIsTypeQual (&NextTok) ||
297 (NextTok.Tok == TOK_IDENT &&
298 (Entry = FindSym (NextTok.Ident)) != 0 &&
299 SymIsTypeDef (Entry)));
304 void PushAddr (const ExprDesc* Expr)
305 /* If the expression contains an address that was somehow evaluated,
306 * push this address on the stack. This is a helper function for all
307 * sorts of implicit or explicit assignment functions where the lvalue
308 * must be saved if it's not constant, before evaluating the rhs.
311 /* Get the address on stack if needed */
312 if (ED_IsLocExpr (Expr)) {
313 /* Push the address (always a pointer) */
320 /*****************************************************************************/
322 /*****************************************************************************/
326 static unsigned FunctionParamList (FuncDesc* Func)
327 /* Parse a function parameter list and pass the parameters to the called
328 * function. Depending on several criteria this may be done by just pushing
329 * each parameter separately, or creating the parameter frame once and then
330 * storing into this frame.
331 * The function returns the size of the parameters pushed.
336 /* Initialize variables */
337 SymEntry* Param = 0; /* Keep gcc silent */
338 unsigned ParamSize = 0; /* Size of parameters pushed */
339 unsigned ParamCount = 0; /* Number of parameters pushed */
340 unsigned FrameSize = 0; /* Size of parameter frame */
341 unsigned FrameParams = 0; /* Number of params in frame */
342 int FrameOffs = 0; /* Offset into parameter frame */
343 int Ellipsis = 0; /* Function is variadic */
345 /* As an optimization, we may allocate the complete parameter frame at
346 * once instead of pushing each parameter as it comes. We may do that,
349 * - optimizations that increase code size are enabled (allocating the
350 * stack frame at once gives usually larger code).
351 * - we have more than one parameter to push (don't count the last param
352 * for __fastcall__ functions).
354 * The FrameSize variable will contain a value > 0 if storing into a frame
355 * (instead of pushing) is enabled.
358 if (IS_Get (&CodeSizeFactor) >= 200) {
360 /* Calculate the number and size of the parameters */
361 FrameParams = Func->ParamCount;
362 FrameSize = Func->ParamSize;
363 if (FrameParams > 0 && (Func->Flags & FD_FASTCALL) != 0) {
364 /* Last parameter is not pushed */
365 FrameSize -= CheckedSizeOf (Func->LastParam->Type);
369 /* Do we have more than one parameter in the frame? */
370 if (FrameParams > 1) {
371 /* Okeydokey, setup the frame */
372 FrameOffs = StackPtr;
374 StackPtr -= FrameSize;
376 /* Don't use a preallocated frame */
381 /* Parse the actual parameter list */
382 while (CurTok.Tok != TOK_RPAREN) {
386 /* Count arguments */
389 /* Fetch the pointer to the next argument, check for too many args */
390 if (ParamCount <= Func->ParamCount) {
391 /* Beware: If there are parameters with identical names, they
392 * cannot go into the same symbol table, which means that in this
393 * case of errorneous input, the number of nodes in the symbol
394 * table and ParamCount are NOT equal. We have to handle this case
395 * below to avoid segmentation violations. Since we know that this
396 * problem can only occur if there is more than one parameter,
397 * we will just use the last one.
399 if (ParamCount == 1) {
401 Param = Func->SymTab->SymHead;
402 } else if (Param->NextSym != 0) {
404 Param = Param->NextSym;
405 CHECK ((Param->Flags & SC_PARAM) != 0);
407 } else if (!Ellipsis) {
408 /* Too many arguments. Do we have an open param list? */
409 if ((Func->Flags & FD_VARIADIC) == 0) {
410 /* End of param list reached, no ellipsis */
411 Error ("Too many arguments in function call");
413 /* Assume an ellipsis even in case of errors to avoid an error
414 * message for each other argument.
419 /* Evaluate the parameter expression */
422 /* If we don't have an argument spec, accept anything, otherwise
423 * convert the actual argument to the type needed.
428 /* Convert the argument to the parameter type if needed */
429 TypeConversion (&Expr, Param->Type);
431 /* If we have a prototype, chars may be pushed as chars */
432 Flags |= CF_FORCECHAR;
436 /* No prototype available. Convert array to "pointer to first
437 * element", and function to "pointer to function".
439 Param->Type = PtrConversion (Param->Type)
443 /* Load the value into the primary if it is not already there */
444 LoadExpr (Flags, &Expr);
446 /* Use the type of the argument for the push */
447 Flags |= TypeOf (Expr.Type);
449 /* If this is a fastcall function, don't push the last argument */
450 if (ParamCount != Func->ParamCount || (Func->Flags & FD_FASTCALL) == 0) {
451 unsigned ArgSize = sizeofarg (Flags);
453 /* We have the space already allocated, store in the frame.
454 * Because of invalid type conversions (that have produced an
455 * error before), we can end up here with a non aligned stack
456 * frame. Since no output will be generated anyway, handle
457 * these cases gracefully instead of doing a CHECK.
459 if (FrameSize >= ArgSize) {
460 FrameSize -= ArgSize;
464 FrameOffs -= ArgSize;
466 g_putlocal (Flags | CF_NOKEEP, FrameOffs, Expr.IVal);
468 /* Push the argument */
469 g_push (Flags, Expr.IVal);
472 /* Calculate total parameter size */
473 ParamSize += ArgSize;
476 /* Check for end of argument list */
477 if (CurTok.Tok != TOK_COMMA) {
483 /* Check if we had enough parameters */
484 if (ParamCount < Func->ParamCount) {
485 Error ("Too few arguments in function call");
488 /* The function returns the size of all parameters pushed onto the stack.
489 * However, if there are parameters missing (which is an error and was
490 * flagged by the compiler) AND a stack frame was preallocated above,
491 * we would loose track of the stackpointer and generate an internal error
492 * later. So we correct the value by the parameters that should have been
493 * pushed to avoid an internal compiler error. Since an error was
494 * generated before, no code will be output anyway.
496 return ParamSize + FrameSize;
501 static void FunctionCall (ExprDesc* Expr)
502 /* Perform a function call. */
504 FuncDesc* Func; /* Function descriptor */
505 int IsFuncPtr; /* Flag */
506 unsigned ParamSize; /* Number of parameter bytes */
508 int PtrOffs = 0; /* Offset of function pointer on stack */
509 int IsFastCall = 0; /* True if it's a fast call function */
510 int PtrOnStack = 0; /* True if a pointer copy is on stack */
512 /* Skip the left paren */
515 /* Get a pointer to the function descriptor from the type string */
516 Func = GetFuncDesc (Expr->Type);
518 /* Handle function pointers transparently */
519 IsFuncPtr = IsTypeFuncPtr (Expr->Type);
522 /* Check wether it's a fastcall function that has parameters */
523 IsFastCall = IsFastCallFunc (Expr->Type + 1) && (Func->ParamCount > 0);
525 /* Things may be difficult, depending on where the function pointer
526 * resides. If the function pointer is an expression of some sort
527 * (not a local or global variable), we have to evaluate this
528 * expression now and save the result for later. Since calls to
529 * function pointers may be nested, we must save it onto the stack.
530 * For fastcall functions we do also need to place a copy of the
531 * pointer on stack, since we cannot use a/x.
533 PtrOnStack = IsFastCall || !ED_IsConst (Expr);
536 /* Not a global or local variable, or a fastcall function. Load
537 * the pointer into the primary and mark it as an expression.
539 LoadExpr (CF_NONE, Expr);
540 ED_MakeRValExpr (Expr);
542 /* Remember the code position */
545 /* Push the pointer onto the stack and remember the offset */
550 /* Check for known standard functions and inline them */
551 } else if (Expr->Name != 0) {
552 int StdFunc = FindStdFunc ((const char*) Expr->Name);
554 /* Inline this function */
555 HandleStdFunc (StdFunc, Func, Expr);
560 /* Parse the parameter list */
561 ParamSize = FunctionParamList (Func);
563 /* We need the closing paren here */
566 /* Special handling for function pointers */
569 /* If the function is not a fastcall function, load the pointer to
570 * the function into the primary.
574 /* Not a fastcall function - we may use the primary */
576 /* If we have no parameters, the pointer is still in the
577 * primary. Remove the code to push it and correct the
580 if (ParamSize == 0) {
584 /* Load from the saved copy */
585 g_getlocal (CF_PTR, PtrOffs);
588 /* Load from original location */
589 LoadExpr (CF_NONE, Expr);
592 /* Call the function */
593 g_callind (TypeOf (Expr->Type+1), ParamSize, PtrOffs);
597 /* Fastcall function. We cannot use the primary for the function
598 * pointer and must therefore use an offset to the stack location.
599 * Since fastcall functions may never be variadic, we can use the
600 * index register for this purpose.
602 g_callind (CF_LOCAL, ParamSize, PtrOffs);
605 /* If we have a pointer on stack, remove it */
607 g_space (- (int) sizeofarg (CF_PTR));
616 /* Normal function */
617 g_call (TypeOf (Expr->Type), (const char*) Expr->Name, ParamSize);
621 /* The function result is an rvalue in the primary register */
622 ED_MakeRValExpr (Expr);
623 Expr->Type = GetFuncReturn (Expr->Type);
628 static void Primary (ExprDesc* E)
629 /* This is the lowest level of the expression parser. */
633 /* Initialize fields in the expression stucture */
636 /* Character and integer constants. */
637 if (CurTok.Tok == TOK_ICONST || CurTok.Tok == TOK_CCONST) {
638 E->IVal = CurTok.IVal;
639 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
640 E->Type = CurTok.Type;
645 /* Floating point constant */
646 if (CurTok.Tok == TOK_FCONST) {
647 E->FVal = CurTok.FVal;
648 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
649 E->Type = CurTok.Type;
654 /* Process parenthesized subexpression by calling the whole parser
657 if (CurTok.Tok == TOK_LPAREN) {
664 /* If we run into an identifier in preprocessing mode, we assume that this
665 * is an undefined macro and replace it by a constant value of zero.
667 if (Preprocessing && CurTok.Tok == TOK_IDENT) {
668 ED_MakeConstAbsInt (E, 0);
672 /* All others may only be used if the expression evaluation is not called
673 * recursively by the preprocessor.
676 /* Illegal expression in PP mode */
677 Error ("Preprocessor expression expected");
678 ED_MakeConstAbsInt (E, 1);
682 switch (CurTok.Tok) {
685 /* Identifier. Get a pointer to the symbol table entry */
686 Sym = E->Sym = FindSym (CurTok.Ident);
688 /* Is the symbol known? */
691 /* We found the symbol - skip the name token */
694 /* Check for illegal symbol types */
695 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
696 if (Sym->Flags & SC_TYPE) {
697 /* Cannot use type symbols */
698 Error ("Variable identifier expected");
699 /* Assume an int type to make E valid */
700 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
705 /* Mark the symbol as referenced */
706 Sym->Flags |= SC_REF;
708 /* The expression type is the symbol type */
711 /* Check for legal symbol types */
712 if ((Sym->Flags & SC_CONST) == SC_CONST) {
713 /* Enum or some other numeric constant */
714 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
715 E->IVal = Sym->V.ConstVal;
716 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
718 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
719 E->Name = (unsigned long) Sym->Name;
720 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
721 /* Local variable. If this is a parameter for a variadic
722 * function, we have to add some address calculations, and the
723 * address is not const.
725 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
726 /* Variadic parameter */
727 g_leavariadic (Sym->V.Offs - F_GetParamSize (CurrentFunc));
728 E->Flags = E_LOC_EXPR | E_RTYPE_LVAL;
730 /* Normal parameter */
731 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
732 E->IVal = Sym->V.Offs;
734 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
735 /* Register variable, zero page based */
736 E->Flags = E_LOC_REGISTER | E_RTYPE_LVAL;
737 E->Name = Sym->V.R.RegOffs;
738 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
739 /* Static variable */
740 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
741 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
742 E->Name = (unsigned long) Sym->Name;
744 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
745 E->Name = Sym->V.Label;
748 /* Local static variable */
749 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
750 E->Name = Sym->V.Offs;
753 /* We've made all variables lvalues above. However, this is
754 * not always correct: An array is actually the address of its
755 * first element, which is a rvalue, and a function is a
756 * rvalue, too, because we cannot store anything in a function.
757 * So fix the flags depending on the type.
759 if (IsTypeArray (E->Type) || IsTypeFunc (E->Type)) {
765 /* We did not find the symbol. Remember the name, then skip it */
767 strcpy (Ident, CurTok.Ident);
770 /* IDENT is either an auto-declared function or an undefined variable. */
771 if (CurTok.Tok == TOK_LPAREN) {
772 /* Declare a function returning int. For that purpose, prepare a
773 * function signature for a function having an empty param list
776 Warning ("Function call without a prototype");
777 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
779 E->Flags = E_LOC_GLOBAL | E_RTYPE_RVAL;
780 E->Name = (unsigned long) Sym->Name;
782 /* Undeclared Variable */
783 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
784 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
786 Error ("Undefined symbol: `%s'", Ident);
794 E->Type = GetCharArrayType (GetLiteralPoolOffs () - CurTok.IVal);
795 E->Flags = E_LOC_LITERAL | E_RTYPE_RVAL;
796 E->IVal = CurTok.IVal;
797 E->Name = LiteralPoolLabel;
804 E->Flags = E_LOC_EXPR | E_RTYPE_RVAL;
809 /* Register pseudo variable */
810 E->Type = type_uchar;
811 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
816 /* Register pseudo variable */
818 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
823 /* Register pseudo variable */
824 E->Type = type_ulong;
825 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
830 /* Illegal primary. */
831 Error ("Expression expected");
832 ED_MakeConstAbsInt (E, 1);
839 static void ArrayRef (ExprDesc* Expr)
840 /* Handle an array reference */
850 /* Skip the bracket */
853 /* Get the type of left side */
856 /* We can apply a special treatment for arrays that have a const base
857 * address. This is true for most arrays and will produce a lot better
858 * code. Check if this is a const base address.
860 ConstBaseAddr = (ED_IsLocConst (Expr) || ED_IsLocStack (Expr));
862 /* If we have a constant base, we delay the address fetch */
864 if (!ConstBaseAddr) {
865 /* Get a pointer to the array into the primary */
866 LoadExpr (CF_NONE, Expr);
868 /* Get the array pointer on stack. Do not push more than 16
869 * bit, even if this value is greater, since we cannot handle
870 * other than 16bit stuff when doing indexing.
876 /* TOS now contains ptr to array elements. Get the subscript. */
877 ExprWithCheck (hie0, &SubScript);
879 /* Check the types of array and subscript. We can either have a
880 * pointer/array to the left, in which case the subscript must be of an
881 * integer type, or we have an integer to the left, in which case the
882 * subscript must be a pointer/array.
883 * Since we do the necessary checking here, we can rely later on the
886 if (IsClassPtr (Expr->Type)) {
887 if (!IsClassInt (SubScript.Type)) {
888 Error ("Array subscript is not an integer");
889 /* To avoid any compiler errors, make the expression a valid int */
890 ED_MakeConstAbsInt (&SubScript, 0);
892 ElementType = Indirect (Expr->Type);
893 } else if (IsClassInt (Expr->Type)) {
894 if (!IsClassPtr (SubScript.Type)) {
895 Error ("Subscripted value is neither array nor pointer");
896 /* To avoid compiler errors, make the subscript a char[] at
899 ED_MakeConstAbs (&SubScript, 0, GetCharArrayType (1));
901 ElementType = Indirect (SubScript.Type);
903 Error ("Cannot subscript");
904 /* To avoid compiler errors, fake both the array and the subscript, so
905 * we can just proceed.
907 ED_MakeConstAbs (Expr, 0, GetCharArrayType (1));
908 ED_MakeConstAbsInt (&SubScript, 0);
909 ElementType = Indirect (Expr->Type);
912 /* Check if the subscript is constant absolute value */
913 if (ED_IsConstAbs (&SubScript)) {
915 /* The array subscript is a numeric constant. If we had pushed the
916 * array base address onto the stack before, we can remove this value,
917 * since we can generate expression+offset.
919 if (!ConstBaseAddr) {
922 /* Get an array pointer into the primary */
923 LoadExpr (CF_NONE, Expr);
926 if (IsClassPtr (Expr->Type)) {
928 /* Lhs is pointer/array. Scale the subscript value according to
931 SubScript.IVal *= CheckedSizeOf (ElementType);
933 /* Remove the address load code */
936 /* In case of an array, we can adjust the offset of the expression
937 * already in Expr. If the base address was a constant, we can even
938 * remove the code that loaded the address into the primary.
940 if (IsTypeArray (Expr->Type)) {
942 /* Adjust the offset */
943 Expr->IVal += SubScript.IVal;
947 /* It's a pointer, so we do have to load it into the primary
948 * first (if it's not already there).
951 LoadExpr (CF_NONE, Expr);
952 ED_MakeRValExpr (Expr);
956 Expr->IVal = SubScript.IVal;
961 /* Scale the rhs value according to the element type */
962 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
964 /* Add the subscript. Since arrays are indexed by integers,
965 * we will ignore the true type of the subscript here and
966 * use always an int. #### Use offset but beware of LoadExpr!
968 g_inc (CF_INT | CF_CONST, SubScript.IVal);
974 /* Array subscript is not constant. Load it into the primary */
976 LoadExpr (CF_NONE, &SubScript);
979 if (IsClassPtr (Expr->Type)) {
981 /* Indexing is based on unsigneds, so we will just use the integer
982 * portion of the index (which is in (e)ax, so there's no further
985 g_scale (CF_INT, CheckedSizeOf (ElementType));
989 /* Get the int value on top. If we come here, we're sure, both
990 * values are 16 bit (the first one was truncated if necessary
991 * and the second one is a pointer). Note: If ConstBaseAddr is
992 * true, we don't have a value on stack, so to "swap" both, just
993 * push the subscript.
997 LoadExpr (CF_NONE, Expr);
1004 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
1008 /* The offset is now in the primary register. It we didn't have a
1009 * constant base address for the lhs, the lhs address is already
1010 * on stack, and we must add the offset. If the base address was
1011 * constant, we call special functions to add the address to the
1014 if (!ConstBaseAddr) {
1016 /* The array base address is on stack and the subscript is in the
1017 * primary. Add both.
1023 /* The subscript is in the primary, and the array base address is
1024 * in Expr. If the subscript has itself a constant address, it is
1025 * often a better idea to reverse again the order of the
1026 * evaluation. This will generate better code if the subscript is
1027 * a byte sized variable. But beware: This is only possible if the
1028 * subscript was not scaled, that is, if this was a byte array
1031 if ((ED_IsLocConst (&SubScript) || ED_IsLocStack (&SubScript)) &&
1032 CheckedSizeOf (ElementType) == SIZEOF_CHAR) {
1036 /* Reverse the order of evaluation */
1037 if (CheckedSizeOf (SubScript.Type) == SIZEOF_CHAR) {
1042 RemoveCode (&Mark2);
1044 /* Get a pointer to the array into the primary. */
1045 LoadExpr (CF_NONE, Expr);
1047 /* Add the variable */
1048 if (ED_IsLocStack (&SubScript)) {
1049 g_addlocal (Flags, SubScript.IVal);
1051 Flags |= GlobalModeFlags (SubScript.Flags);
1052 g_addstatic (Flags, SubScript.Name, SubScript.IVal);
1055 if (ED_IsLocAbs (Expr)) {
1056 /* Constant numeric address. Just add it */
1057 g_inc (CF_INT, Expr->IVal);
1058 } else if (ED_IsLocStack (Expr)) {
1059 /* Base address is a local variable address */
1060 if (IsTypeArray (Expr->Type)) {
1061 g_addaddr_local (CF_INT, Expr->IVal);
1063 g_addlocal (CF_PTR, Expr->IVal);
1066 /* Base address is a static variable address */
1067 unsigned Flags = CF_INT | GlobalModeFlags (Expr->Flags);
1068 if (IsTypeArray (Expr->Type)) {
1069 g_addaddr_static (Flags, Expr->Name, Expr->IVal);
1071 g_addstatic (Flags, Expr->Name, Expr->IVal);
1079 /* The result is an expression in the primary */
1080 ED_MakeRValExpr (Expr);
1084 /* Result is of element type */
1085 Expr->Type = ElementType;
1087 /* An array element is actually a variable. So the rules for variables
1088 * with respect to the reference type apply: If it's an array, it is
1089 * a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1090 * but an array cannot contain functions).
1092 if (IsTypeArray (Expr->Type)) {
1098 /* Consume the closing bracket */
1104 static void StructRef (ExprDesc* Expr)
1105 /* Process struct field after . or ->. */
1110 /* Skip the token and check for an identifier */
1112 if (CurTok.Tok != TOK_IDENT) {
1113 Error ("Identifier expected");
1114 Expr->Type = type_int;
1118 /* Get the symbol table entry and check for a struct field */
1119 strcpy (Ident, CurTok.Ident);
1121 Field = FindStructField (Expr->Type, Ident);
1123 Error ("Struct/union has no field named `%s'", Ident);
1124 Expr->Type = type_int;
1128 /* If we have a struct pointer that is an lvalue and not already in the
1129 * primary, load it now.
1131 if (ED_IsLVal (Expr) && IsTypePtr (Expr->Type)) {
1133 /* Load into the primary */
1134 LoadExpr (CF_NONE, Expr);
1136 /* Make it an lvalue expression */
1137 ED_MakeLValExpr (Expr);
1140 /* Set the struct field offset */
1141 Expr->IVal += Field->V.Offs;
1143 /* The type is now the type of the field */
1144 Expr->Type = Field->Type;
1146 /* An struct member is actually a variable. So the rules for variables
1147 * with respect to the reference type apply: If it's an array, it is
1148 * a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1149 * but a struct field cannot be a function).
1151 if (IsTypeArray (Expr->Type)) {
1160 static void hie11 (ExprDesc *Expr)
1161 /* Handle compound types (structs and arrays) */
1163 /* Evaluate the lhs */
1166 /* Check for a rhs */
1167 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN ||
1168 CurTok.Tok == TOK_DOT || CurTok.Tok == TOK_PTR_REF) {
1170 switch (CurTok.Tok) {
1173 /* Array reference */
1178 /* Function call. */
1179 if (!IsTypeFunc (Expr->Type) && !IsTypeFuncPtr (Expr->Type)) {
1180 /* Not a function */
1181 Error ("Illegal function call");
1182 /* Force the type to be a implicitly defined function, one
1183 * returning an int and taking any number of arguments.
1184 * Since we don't have a name, place it at absolute address
1187 ED_MakeConstAbs (Expr, 0, GetImplicitFuncType ());
1189 /* Call the function */
1190 FunctionCall (Expr);
1194 if (!IsClassStruct (Expr->Type)) {
1195 Error ("Struct expected");
1201 /* If we have an array, convert it to pointer to first element */
1202 if (IsTypeArray (Expr->Type)) {
1203 Expr->Type = ArrayToPtr (Expr->Type);
1205 if (!IsClassPtr (Expr->Type) || !IsClassStruct (Indirect (Expr->Type))) {
1206 Error ("Struct pointer expected");
1212 Internal ("Invalid token in hie11: %d", CurTok.Tok);
1220 void Store (ExprDesc* Expr, const type* StoreType)
1221 /* Store the primary register into the location denoted by Expr. If StoreType
1222 * is given, use this type when storing instead of Expr->Type. If StoreType
1223 * is NULL, use Expr->Type instead.
1228 /* If StoreType was not given, use Expr->Type instead */
1229 if (StoreType == 0) {
1230 StoreType = Expr->Type;
1233 /* Prepare the code generator flags */
1234 Flags = TypeOf (StoreType);
1236 /* Do the store depending on the location */
1237 switch (ED_GetLoc (Expr)) {
1240 /* Absolute: numeric address or const */
1241 g_putstatic (Flags | CF_ABSOLUTE, Expr->IVal, 0);
1245 /* Global variable */
1246 g_putstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->IVal);
1251 /* Static variable or literal in the literal pool */
1252 g_putstatic (Flags | CF_STATIC, Expr->Name, Expr->IVal);
1255 case E_LOC_REGISTER:
1256 /* Register variable */
1257 g_putstatic (Flags | CF_REGVAR, Expr->Name, Expr->IVal);
1261 /* Value on the stack */
1262 g_putlocal (Flags, Expr->IVal, 0);
1266 /* The primary register (value is already there) */
1267 /* ### Do we need a test here if the flag is set? */
1271 /* An expression in the primary register */
1272 g_putind (Flags, Expr->IVal);
1276 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
1279 /* Assume that each one of the stores will invalidate CC */
1280 ED_MarkAsUntested (Expr);
1285 static void PreInc (ExprDesc* Expr)
1286 /* Handle the preincrement operators */
1291 /* Skip the operator token */
1294 /* Evaluate the expression and check that it is an lvalue */
1296 if (!ED_IsLVal (Expr)) {
1297 Error ("Invalid lvalue");
1301 /* Get the data type */
1302 Flags = TypeOf (Expr->Type) | CF_FORCECHAR | CF_CONST;
1304 /* Get the increment value in bytes */
1305 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1307 /* Check the location of the data */
1308 switch (ED_GetLoc (Expr)) {
1311 /* Absolute: numeric address or const */
1312 g_addeqstatic (Flags | CF_ABSOLUTE, Expr->IVal, 0, Val);
1316 /* Global variable */
1317 g_addeqstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->IVal, Val);
1322 /* Static variable or literal in the literal pool */
1323 g_addeqstatic (Flags | CF_STATIC, Expr->Name, Expr->IVal, Val);
1326 case E_LOC_REGISTER:
1327 /* Register variable */
1328 g_addeqstatic (Flags | CF_REGVAR, Expr->Name, Expr->IVal, Val);
1332 /* Value on the stack */
1333 g_addeqlocal (Flags, Expr->IVal, Val);
1337 /* The primary register */
1342 /* An expression in the primary register */
1343 g_addeqind (Flags, Expr->IVal, Val);
1347 Internal ("Invalid location in PreInc(): 0x%04X", ED_GetLoc (Expr));
1350 /* Result is an expression, no reference */
1351 ED_MakeRValExpr (Expr);
1356 static void PreDec (ExprDesc* Expr)
1357 /* Handle the predecrement operators */
1362 /* Skip the operator token */
1365 /* Evaluate the expression and check that it is an lvalue */
1367 if (!ED_IsLVal (Expr)) {
1368 Error ("Invalid lvalue");
1372 /* Get the data type */
1373 Flags = TypeOf (Expr->Type) | CF_FORCECHAR | CF_CONST;
1375 /* Get the increment value in bytes */
1376 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1378 /* Check the location of the data */
1379 switch (ED_GetLoc (Expr)) {
1382 /* Absolute: numeric address or const */
1383 g_subeqstatic (Flags | CF_ABSOLUTE, Expr->IVal, 0, Val);
1387 /* Global variable */
1388 g_subeqstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->IVal, Val);
1393 /* Static variable or literal in the literal pool */
1394 g_subeqstatic (Flags | CF_STATIC, Expr->Name, Expr->IVal, Val);
1397 case E_LOC_REGISTER:
1398 /* Register variable */
1399 g_subeqstatic (Flags | CF_REGVAR, Expr->Name, Expr->IVal, Val);
1403 /* Value on the stack */
1404 g_subeqlocal (Flags, Expr->IVal, Val);
1408 /* The primary register */
1413 /* An expression in the primary register */
1414 g_subeqind (Flags, Expr->IVal, Val);
1418 Internal ("Invalid location in PreDec(): 0x%04X", ED_GetLoc (Expr));
1421 /* Result is an expression, no reference */
1422 ED_MakeRValExpr (Expr);
1427 static void PostIncDec (ExprDesc* Expr, void (*inc) (unsigned, unsigned long))
1428 /* Handle i-- and i++ */
1434 /* The expression to increment must be an lvalue */
1435 if (!ED_IsLVal (Expr)) {
1436 Error ("Invalid lvalue");
1440 /* Get the data type */
1441 Flags = TypeOf (Expr->Type);
1443 /* Push the address if needed */
1446 /* Fetch the value and save it (since it's the result of the expression) */
1447 LoadExpr (CF_NONE, Expr);
1448 g_save (Flags | CF_FORCECHAR);
1450 /* If we have a pointer expression, increment by the size of the type */
1451 if (IsTypePtr (Expr->Type)) {
1452 inc (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1454 inc (Flags | CF_CONST | CF_FORCECHAR, 1);
1457 /* Store the result back */
1460 /* Restore the original value in the primary register */
1461 g_restore (Flags | CF_FORCECHAR);
1463 /* The result is always an expression, no reference */
1464 ED_MakeRValExpr (Expr);
1469 static void UnaryOp (ExprDesc* Expr)
1470 /* Handle unary -/+ and ~ */
1474 /* Remember the operator token and skip it */
1475 token_t Tok = CurTok.Tok;
1478 /* Get the expression */
1481 /* We can only handle integer types */
1482 if (!IsClassInt (Expr->Type)) {
1483 Error ("Argument must have integer type");
1484 ED_MakeConstAbsInt (Expr, 1);
1487 /* Check for a constant expression */
1488 if (ED_IsConstAbs (Expr)) {
1489 /* Value is constant */
1491 case TOK_MINUS: Expr->IVal = -Expr->IVal; break;
1492 case TOK_PLUS: break;
1493 case TOK_COMP: Expr->IVal = ~Expr->IVal; break;
1494 default: Internal ("Unexpected token: %d", Tok);
1497 /* Value is not constant */
1498 LoadExpr (CF_NONE, Expr);
1500 /* Get the type of the expression */
1501 Flags = TypeOf (Expr->Type);
1503 /* Handle the operation */
1505 case TOK_MINUS: g_neg (Flags); break;
1506 case TOK_PLUS: break;
1507 case TOK_COMP: g_com (Flags); break;
1508 default: Internal ("Unexpected token: %d", Tok);
1511 /* The result is a rvalue in the primary */
1512 ED_MakeRValExpr (Expr);
1518 void hie10 (ExprDesc* Expr)
1519 /* Handle ++, --, !, unary - etc. */
1523 switch (CurTok.Tok) {
1541 if (evalexpr (CF_NONE, hie10, Expr) == 0) {
1542 /* Constant expression */
1543 Expr->IVal = !Expr->IVal;
1545 g_bneg (TypeOf (Expr->Type));
1546 ED_MakeRValExpr (Expr);
1547 ED_TestDone (Expr); /* bneg will set cc */
1553 ExprWithCheck (hie10, Expr);
1554 if (ED_IsLVal (Expr) || !(ED_IsLocConst (Expr) || ED_IsLocStack (Expr))) {
1555 /* Not a const, load it into the primary and make it a
1558 LoadExpr (CF_NONE, Expr);
1559 ED_MakeRValExpr (Expr);
1561 /* If the expression is already a pointer to function, the
1562 * additional dereferencing operator must be ignored.
1564 if (IsTypeFuncPtr (Expr->Type)) {
1565 /* Expression not storable */
1568 if (IsClassPtr (Expr->Type)) {
1569 Expr->Type = Indirect (Expr->Type);
1571 Error ("Illegal indirection");
1579 ExprWithCheck (hie10, Expr);
1580 /* The & operator may be applied to any lvalue, and it may be
1581 * applied to functions, even if they're no lvalues.
1583 if (ED_IsRVal (Expr) && !IsTypeFunc (Expr->Type)) {
1584 /* Allow the & operator with an array */
1585 if (!IsTypeArray (Expr->Type)) {
1586 Error ("Illegal address");
1589 Expr->Type = PointerTo (Expr->Type);
1596 if (TypeSpecAhead ()) {
1597 type Type[MAXTYPELEN];
1599 Size = CheckedSizeOf (ParseType (Type));
1602 /* Remember the output queue pointer */
1606 Size = CheckedSizeOf (Expr->Type);
1607 /* Remove any generated code */
1610 ED_MakeConstAbs (Expr, Size, type_size_t);
1611 ED_MarkAsUntested (Expr);
1615 if (TypeSpecAhead ()) {
1625 /* Handle post increment */
1626 if (CurTok.Tok == TOK_INC) {
1627 PostIncDec (Expr, g_inc);
1628 } else if (CurTok.Tok == TOK_DEC) {
1629 PostIncDec (Expr, g_dec);
1639 static void hie_internal (const GenDesc* Ops, /* List of generators */
1641 void (*hienext) (ExprDesc*),
1643 /* Helper function */
1649 token_t Tok; /* The operator token */
1650 unsigned ltype, type;
1651 int rconst; /* Operand is a constant */
1657 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1659 /* Tell the caller that we handled it's ops */
1662 /* All operators that call this function expect an int on the lhs */
1663 if (!IsClassInt (Expr->Type)) {
1664 Error ("Integer expression expected");
1667 /* Remember the operator token, then skip it */
1671 /* Get the lhs on stack */
1672 GetCodePos (&Mark1);
1673 ltype = TypeOf (Expr->Type);
1674 if (ED_IsConstAbs (Expr)) {
1675 /* Constant value */
1676 GetCodePos (&Mark2);
1677 g_push (ltype | CF_CONST, Expr->IVal);
1679 /* Value not constant */
1680 LoadExpr (CF_NONE, Expr);
1681 GetCodePos (&Mark2);
1685 /* Get the right hand side */
1686 rconst = (evalexpr (CF_NONE, hienext, &Expr2) == 0);
1688 /* Check the type of the rhs */
1689 if (!IsClassInt (Expr2.Type)) {
1690 Error ("Integer expression expected");
1693 /* Check for const operands */
1694 if (ED_IsConstAbs (Expr) && rconst) {
1696 /* Both operands are constant, remove the generated code */
1697 RemoveCode (&Mark1);
1699 /* Evaluate the result */
1700 Expr->IVal = kcalc (Tok, Expr->IVal, Expr2.IVal);
1702 /* Get the type of the result */
1703 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1707 /* If the right hand side is constant, and the generator function
1708 * expects the lhs in the primary, remove the push of the primary
1711 unsigned rtype = TypeOf (Expr2.Type);
1714 /* Second value is constant - check for div */
1717 if (Tok == TOK_DIV && Expr2.IVal == 0) {
1718 Error ("Division by zero");
1719 } else if (Tok == TOK_MOD && Expr2.IVal == 0) {
1720 Error ("Modulo operation with zero");
1722 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1723 RemoveCode (&Mark2);
1724 ltype |= CF_REG; /* Value is in register */
1728 /* Determine the type of the operation result. */
1729 type |= g_typeadjust (ltype, rtype);
1730 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1733 Gen->Func (type, Expr2.IVal);
1735 /* We have a rvalue in the primary now */
1736 ED_MakeRValExpr (Expr);
1743 static void hie_compare (const GenDesc* Ops, /* List of generators */
1745 void (*hienext) (ExprDesc*))
1746 /* Helper function for the compare operators */
1752 token_t tok; /* The operator token */
1754 int rconst; /* Operand is a constant */
1759 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1761 /* Remember the operator token, then skip it */
1765 /* Get the lhs on stack */
1766 GetCodePos (&Mark1);
1767 ltype = TypeOf (Expr->Type);
1768 if (ED_IsConstAbs (Expr)) {
1769 /* Constant value */
1770 GetCodePos (&Mark2);
1771 g_push (ltype | CF_CONST, Expr->IVal);
1773 /* Value not constant */
1774 LoadExpr (CF_NONE, Expr);
1775 GetCodePos (&Mark2);
1779 /* Get the right hand side */
1780 rconst = (evalexpr (CF_NONE, hienext, &Expr2) == 0);
1782 /* Make sure, the types are compatible */
1783 if (IsClassInt (Expr->Type)) {
1784 if (!IsClassInt (Expr2.Type) && !(IsClassPtr(Expr2.Type) && ED_IsNullPtr(Expr))) {
1785 Error ("Incompatible types");
1787 } else if (IsClassPtr (Expr->Type)) {
1788 if (IsClassPtr (Expr2.Type)) {
1789 /* Both pointers are allowed in comparison if they point to
1790 * the same type, or if one of them is a void pointer.
1792 type* left = Indirect (Expr->Type);
1793 type* right = Indirect (Expr2.Type);
1794 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1795 /* Incomatible pointers */
1796 Error ("Incompatible types");
1798 } else if (!ED_IsNullPtr (&Expr2)) {
1799 Error ("Incompatible types");
1803 /* Check for const operands */
1804 if (ED_IsConstAbs (Expr) && rconst) {
1806 /* Both operands are constant, remove the generated code */
1807 RemoveCode (&Mark1);
1809 /* Evaluate the result */
1810 Expr->IVal = kcalc (tok, Expr->IVal, Expr2.IVal);
1814 /* If the right hand side is constant, and the generator function
1815 * expects the lhs in the primary, remove the push of the primary
1821 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1822 RemoveCode (&Mark2);
1823 ltype |= CF_REG; /* Value is in register */
1827 /* Determine the type of the operation result. If the left
1828 * operand is of type char and the right is a constant, or
1829 * if both operands are of type char, we will encode the
1830 * operation as char operation. Otherwise the default
1831 * promotions are used.
1833 if (IsTypeChar (Expr->Type) && (IsTypeChar (Expr2.Type) || rconst)) {
1835 if (IsSignUnsigned (Expr->Type) || IsSignUnsigned (Expr2.Type)) {
1836 flags |= CF_UNSIGNED;
1839 flags |= CF_FORCECHAR;
1842 unsigned rtype = TypeOf (Expr2.Type) | (flags & CF_CONST);
1843 flags |= g_typeadjust (ltype, rtype);
1847 Gen->Func (flags, Expr2.IVal);
1849 /* The result is an rvalue in the primary */
1850 ED_MakeRValExpr (Expr);
1853 /* Result type is always int */
1854 Expr->Type = type_int;
1856 /* Condition codes are set */
1863 static void hie9 (ExprDesc *Expr)
1864 /* Process * and / operators. */
1866 static const GenDesc hie9_ops[] = {
1867 { TOK_STAR, GEN_NOPUSH, g_mul },
1868 { TOK_DIV, GEN_NOPUSH, g_div },
1869 { TOK_MOD, GEN_NOPUSH, g_mod },
1870 { TOK_INVALID, 0, 0 }
1874 hie_internal (hie9_ops, Expr, hie10, &UsedGen);
1879 static void parseadd (ExprDesc* Expr)
1880 /* Parse an expression with the binary plus operator. Expr contains the
1881 * unprocessed left hand side of the expression and will contain the
1882 * result of the expression on return.
1886 unsigned flags; /* Operation flags */
1887 CodeMark Mark; /* Remember code position */
1888 type* lhst; /* Type of left hand side */
1889 type* rhst; /* Type of right hand side */
1892 /* Skip the PLUS token */
1895 /* Get the left hand side type, initialize operation flags */
1899 /* Check for constness on both sides */
1900 if (ED_IsConst (Expr)) {
1902 /* The left hand side is a constant of some sort. Good. Get rhs */
1904 if (ED_IsConstAbs (&Expr2)) {
1906 /* Right hand side is a constant numeric value. Get the rhs type */
1909 /* Both expressions are constants. Check for pointer arithmetic */
1910 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1911 /* Left is pointer, right is int, must scale rhs */
1912 Expr->IVal += Expr2.IVal * CheckedPSizeOf (lhst);
1913 /* Result type is a pointer */
1914 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1915 /* Left is int, right is pointer, must scale lhs */
1916 Expr->IVal = Expr->IVal * CheckedPSizeOf (rhst) + Expr2.IVal;
1917 /* Result type is a pointer */
1918 Expr->Type = Expr2.Type;
1919 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1920 /* Integer addition */
1921 Expr->IVal += Expr2.IVal;
1922 typeadjust (Expr, &Expr2, 1);
1925 Error ("Invalid operands for binary operator `+'");
1930 /* lhs is a constant and rhs is not constant. Load rhs into
1933 LoadExpr (CF_NONE, &Expr2);
1935 /* Beware: The check above (for lhs) lets not only pass numeric
1936 * constants, but also constant addresses (labels), maybe even
1937 * with an offset. We have to check for that here.
1940 /* First, get the rhs type. */
1944 if (ED_IsLocAbs (Expr)) {
1945 /* A numerical constant */
1948 /* Constant address label */
1949 flags |= GlobalModeFlags (Expr->Flags) | CF_CONSTADDR;
1952 /* Check for pointer arithmetic */
1953 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1954 /* Left is pointer, right is int, must scale rhs */
1955 g_scale (CF_INT, CheckedPSizeOf (lhst));
1956 /* Operate on pointers, result type is a pointer */
1958 /* Generate the code for the add */
1959 if (ED_GetLoc (Expr) == E_LOC_ABS) {
1960 /* Numeric constant */
1961 g_inc (flags, Expr->IVal);
1963 /* Constant address */
1964 g_addaddr_static (flags, Expr->Name, Expr->IVal);
1966 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1968 /* Left is int, right is pointer, must scale lhs. */
1969 unsigned ScaleFactor = CheckedPSizeOf (rhst);
1971 /* Operate on pointers, result type is a pointer */
1973 Expr->Type = Expr2.Type;
1975 /* Since we do already have rhs in the primary, if lhs is
1976 * not a numeric constant, and the scale factor is not one
1977 * (no scaling), we must take the long way over the stack.
1979 if (ED_IsLocAbs (Expr)) {
1980 /* Numeric constant, scale lhs */
1981 Expr->IVal *= ScaleFactor;
1982 /* Generate the code for the add */
1983 g_inc (flags, Expr->IVal);
1984 } else if (ScaleFactor == 1) {
1985 /* Constant address but no need to scale */
1986 g_addaddr_static (flags, Expr->Name, Expr->IVal);
1988 /* Constant address that must be scaled */
1989 g_push (TypeOf (Expr2.Type), 0); /* rhs --> stack */
1990 g_getimmed (flags, Expr->Name, Expr->IVal);
1991 g_scale (CF_PTR, ScaleFactor);
1994 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1995 /* Integer addition */
1996 flags |= typeadjust (Expr, &Expr2, 1);
1997 /* Generate the code for the add */
1998 if (ED_IsLocAbs (Expr)) {
1999 /* Numeric constant */
2000 g_inc (flags, Expr->IVal);
2002 /* Constant address */
2003 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2007 Error ("Invalid operands for binary operator `+'");
2010 /* Result is a rvalue in primary register */
2011 ED_MakeRValExpr (Expr);
2016 /* Left hand side is not constant. Get the value onto the stack. */
2017 LoadExpr (CF_NONE, Expr); /* --> primary register */
2019 g_push (TypeOf (Expr->Type), 0); /* --> stack */
2021 /* Evaluate the rhs */
2022 if (evalexpr (CF_NONE, hie9, &Expr2) == 0) {
2024 /* Right hand side is a constant. Get the rhs type */
2027 /* Remove pushed value from stack */
2030 /* Check for pointer arithmetic */
2031 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2032 /* Left is pointer, right is int, must scale rhs */
2033 Expr2.IVal *= CheckedPSizeOf (lhst);
2034 /* Operate on pointers, result type is a pointer */
2036 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2037 /* Left is int, right is pointer, must scale lhs (ptr only) */
2038 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2039 /* Operate on pointers, result type is a pointer */
2041 Expr->Type = Expr2.Type;
2042 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2043 /* Integer addition */
2044 flags = typeadjust (Expr, &Expr2, 1);
2047 Error ("Invalid operands for binary operator `+'");
2050 /* Generate code for the add */
2051 g_inc (flags | CF_CONST, Expr2.IVal);
2055 /* lhs and rhs are not constant. Get the rhs type. */
2058 /* Check for pointer arithmetic */
2059 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2060 /* Left is pointer, right is int, must scale rhs */
2061 g_scale (CF_INT, CheckedPSizeOf (lhst));
2062 /* Operate on pointers, result type is a pointer */
2064 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2065 /* Left is int, right is pointer, must scale lhs */
2066 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2067 g_swap (CF_INT); /* Swap TOS and primary */
2068 g_scale (CF_INT, CheckedPSizeOf (rhst));
2069 /* Operate on pointers, result type is a pointer */
2071 Expr->Type = Expr2.Type;
2072 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2073 /* Integer addition. Note: Result is never constant.
2074 * Problem here is that typeadjust does not know if the
2075 * variable is an rvalue or lvalue, so if both operands
2076 * are dereferenced constant numeric addresses, typeadjust
2077 * thinks the operation works on constants. Removing
2078 * CF_CONST here means handling the symptoms, however, the
2079 * whole parser is such a mess that I fear to break anything
2080 * when trying to apply another solution.
2082 flags = typeadjust (Expr, &Expr2, 0) & ~CF_CONST;
2085 Error ("Invalid operands for binary operator `+'");
2088 /* Generate code for the add */
2093 /* Result is a rvalue in primary register */
2094 ED_MakeRValExpr (Expr);
2097 /* Condition codes not set */
2098 ED_MarkAsUntested (Expr);
2104 static void parsesub (ExprDesc* Expr)
2105 /* Parse an expression with the binary minus operator. Expr contains the
2106 * unprocessed left hand side of the expression and will contain the
2107 * result of the expression on return.
2111 unsigned flags; /* Operation flags */
2112 type* lhst; /* Type of left hand side */
2113 type* rhst; /* Type of right hand side */
2114 CodeMark Mark1; /* Save position of output queue */
2115 CodeMark Mark2; /* Another position in the queue */
2116 int rscale; /* Scale factor for the result */
2119 /* Skip the MINUS token */
2122 /* Get the left hand side type, initialize operation flags */
2125 rscale = 1; /* Scale by 1, that is, don't scale */
2127 /* Remember the output queue position, then bring the value onto the stack */
2128 GetCodePos (&Mark1);
2129 LoadExpr (CF_NONE, Expr); /* --> primary register */
2130 GetCodePos (&Mark2);
2131 g_push (TypeOf (lhst), 0); /* --> stack */
2133 /* Parse the right hand side */
2134 if (evalexpr (CF_NONE, hie9, &Expr2) == 0) {
2136 /* The right hand side is constant. Get the rhs type. */
2139 /* Check left hand side */
2140 if (ED_IsConstAbs (Expr)) {
2142 /* Both sides are constant, remove generated code */
2143 RemoveCode (&Mark1);
2145 /* Check for pointer arithmetic */
2146 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2147 /* Left is pointer, right is int, must scale rhs */
2148 Expr->IVal -= Expr2.IVal * CheckedPSizeOf (lhst);
2149 /* Operate on pointers, result type is a pointer */
2150 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2151 /* Left is pointer, right is pointer, must scale result */
2152 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2153 Error ("Incompatible pointer types");
2155 Expr->IVal = (Expr->IVal - Expr2.IVal) /
2156 CheckedPSizeOf (lhst);
2158 /* Operate on pointers, result type is an integer */
2159 Expr->Type = type_int;
2160 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2161 /* Integer subtraction */
2162 typeadjust (Expr, &Expr2, 1);
2163 Expr->IVal -= Expr2.IVal;
2166 Error ("Invalid operands for binary operator `-'");
2169 /* Result is constant, condition codes not set */
2170 ED_MarkAsUntested (Expr);
2174 /* Left hand side is not constant, right hand side is.
2175 * Remove pushed value from stack.
2177 RemoveCode (&Mark2);
2179 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2180 /* Left is pointer, right is int, must scale rhs */
2181 Expr2.IVal *= CheckedPSizeOf (lhst);
2182 /* Operate on pointers, result type is a pointer */
2184 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2185 /* Left is pointer, right is pointer, must scale result */
2186 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2187 Error ("Incompatible pointer types");
2189 rscale = CheckedPSizeOf (lhst);
2191 /* Operate on pointers, result type is an integer */
2193 Expr->Type = type_int;
2194 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2195 /* Integer subtraction */
2196 flags = typeadjust (Expr, &Expr2, 1);
2199 Error ("Invalid operands for binary operator `-'");
2202 /* Do the subtraction */
2203 g_dec (flags | CF_CONST, Expr2.IVal);
2205 /* If this was a pointer subtraction, we must scale the result */
2207 g_scale (flags, -rscale);
2210 /* Result is a rvalue in the primary register */
2211 ED_MakeRValExpr (Expr);
2212 ED_MarkAsUntested (Expr);
2218 /* Right hand side is not constant. Get the rhs type. */
2221 /* Check for pointer arithmetic */
2222 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2223 /* Left is pointer, right is int, must scale rhs */
2224 g_scale (CF_INT, CheckedPSizeOf (lhst));
2225 /* Operate on pointers, result type is a pointer */
2227 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2228 /* Left is pointer, right is pointer, must scale result */
2229 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2230 Error ("Incompatible pointer types");
2232 rscale = CheckedPSizeOf (lhst);
2234 /* Operate on pointers, result type is an integer */
2236 Expr->Type = type_int;
2237 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2238 /* Integer subtraction. If the left hand side descriptor says that
2239 * the lhs is const, we have to remove this mark, since this is no
2240 * longer true, lhs is on stack instead.
2242 if (ED_IsLocAbs (Expr)) {
2243 ED_MakeRValExpr (Expr);
2245 /* Adjust operand types */
2246 flags = typeadjust (Expr, &Expr2, 0);
2249 Error ("Invalid operands for binary operator `-'");
2252 /* Generate code for the sub (the & is a hack here) */
2253 g_sub (flags & ~CF_CONST, 0);
2255 /* If this was a pointer subtraction, we must scale the result */
2257 g_scale (flags, -rscale);
2260 /* Result is a rvalue in the primary register */
2261 ED_MakeRValExpr (Expr);
2262 ED_MarkAsUntested (Expr);
2268 void hie8 (ExprDesc* Expr)
2269 /* Process + and - binary operators. */
2272 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2273 if (CurTok.Tok == TOK_PLUS) {
2283 static void hie6 (ExprDesc* Expr)
2284 /* Handle greater-than type comparators */
2286 static const GenDesc hie6_ops [] = {
2287 { TOK_LT, GEN_NOPUSH, g_lt },
2288 { TOK_LE, GEN_NOPUSH, g_le },
2289 { TOK_GE, GEN_NOPUSH, g_ge },
2290 { TOK_GT, GEN_NOPUSH, g_gt },
2291 { TOK_INVALID, 0, 0 }
2293 hie_compare (hie6_ops, Expr, ShiftExpr);
2298 static void hie5 (ExprDesc* Expr)
2299 /* Handle == and != */
2301 static const GenDesc hie5_ops[] = {
2302 { TOK_EQ, GEN_NOPUSH, g_eq },
2303 { TOK_NE, GEN_NOPUSH, g_ne },
2304 { TOK_INVALID, 0, 0 }
2306 hie_compare (hie5_ops, Expr, hie6);
2311 static void hie4 (ExprDesc* Expr)
2312 /* Handle & (bitwise and) */
2314 static const GenDesc hie4_ops[] = {
2315 { TOK_AND, GEN_NOPUSH, g_and },
2316 { TOK_INVALID, 0, 0 }
2320 hie_internal (hie4_ops, Expr, hie5, &UsedGen);
2325 static void hie3 (ExprDesc* Expr)
2326 /* Handle ^ (bitwise exclusive or) */
2328 static const GenDesc hie3_ops[] = {
2329 { TOK_XOR, GEN_NOPUSH, g_xor },
2330 { TOK_INVALID, 0, 0 }
2334 hie_internal (hie3_ops, Expr, hie4, &UsedGen);
2339 static void hie2 (ExprDesc* Expr)
2340 /* Handle | (bitwise or) */
2342 static const GenDesc hie2_ops[] = {
2343 { TOK_OR, GEN_NOPUSH, g_or },
2344 { TOK_INVALID, 0, 0 }
2348 hie_internal (hie2_ops, Expr, hie3, &UsedGen);
2353 static void hieAndPP (ExprDesc* Expr)
2354 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2355 * called recursively from the preprocessor.
2360 ConstAbsIntExpr (hie2, Expr);
2361 while (CurTok.Tok == TOK_BOOL_AND) {
2367 ConstAbsIntExpr (hie2, &Expr2);
2369 /* Combine the two */
2370 Expr->IVal = (Expr->IVal && Expr2.IVal);
2376 static void hieOrPP (ExprDesc *Expr)
2377 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2378 * called recursively from the preprocessor.
2383 ConstAbsIntExpr (hieAndPP, Expr);
2384 while (CurTok.Tok == TOK_BOOL_OR) {
2390 ConstAbsIntExpr (hieAndPP, &Expr2);
2392 /* Combine the two */
2393 Expr->IVal = (Expr->IVal || Expr2.IVal);
2399 static void hieAnd (ExprDesc* Expr, unsigned TrueLab, int* BoolOp)
2400 /* Process "exp && exp" */
2406 if (CurTok.Tok == TOK_BOOL_AND) {
2408 /* Tell our caller that we're evaluating a boolean */
2411 /* Get a label that we will use for false expressions */
2412 lab = GetLocalLabel ();
2414 /* If the expr hasn't set condition codes, set the force-test flag */
2415 if (!ED_IsTested (Expr)) {
2416 ED_MarkForTest (Expr);
2419 /* Load the value */
2420 LoadExpr (CF_FORCECHAR, Expr);
2422 /* Generate the jump */
2423 g_falsejump (CF_NONE, lab);
2425 /* Parse more boolean and's */
2426 while (CurTok.Tok == TOK_BOOL_AND) {
2433 if (!ED_IsTested (&Expr2)) {
2434 ED_MarkForTest (&Expr2);
2436 LoadExpr (CF_FORCECHAR, &Expr2);
2438 /* Do short circuit evaluation */
2439 if (CurTok.Tok == TOK_BOOL_AND) {
2440 g_falsejump (CF_NONE, lab);
2442 /* Last expression - will evaluate to true */
2443 g_truejump (CF_NONE, TrueLab);
2447 /* Define the false jump label here */
2448 g_defcodelabel (lab);
2450 /* The result is an rvalue in primary */
2451 ED_MakeRValExpr (Expr);
2452 ED_TestDone (Expr); /* Condition codes are set */
2458 static void hieOr (ExprDesc *Expr)
2459 /* Process "exp || exp". */
2462 int BoolOp = 0; /* Did we have a boolean op? */
2463 int AndOp; /* Did we have a && operation? */
2464 unsigned TrueLab; /* Jump to this label if true */
2468 TrueLab = GetLocalLabel ();
2470 /* Call the next level parser */
2471 hieAnd (Expr, TrueLab, &BoolOp);
2473 /* Any boolean or's? */
2474 if (CurTok.Tok == TOK_BOOL_OR) {
2476 /* If the expr hasn't set condition codes, set the force-test flag */
2477 if (!ED_IsTested (Expr)) {
2478 ED_MarkForTest (Expr);
2481 /* Get first expr */
2482 LoadExpr (CF_FORCECHAR, Expr);
2484 /* For each expression jump to TrueLab if true. Beware: If we
2485 * had && operators, the jump is already in place!
2488 g_truejump (CF_NONE, TrueLab);
2491 /* Remember that we had a boolean op */
2494 /* while there's more expr */
2495 while (CurTok.Tok == TOK_BOOL_OR) {
2502 hieAnd (&Expr2, TrueLab, &AndOp);
2503 if (!ED_IsTested (&Expr2)) {
2504 ED_MarkForTest (&Expr2);
2506 LoadExpr (CF_FORCECHAR, &Expr2);
2508 /* If there is more to come, add shortcut boolean eval. */
2509 g_truejump (CF_NONE, TrueLab);
2513 /* The result is an rvalue in primary */
2514 ED_MakeRValExpr (Expr);
2515 ED_TestDone (Expr); /* Condition codes are set */
2518 /* If we really had boolean ops, generate the end sequence */
2520 DoneLab = GetLocalLabel ();
2521 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2522 g_falsejump (CF_NONE, DoneLab);
2523 g_defcodelabel (TrueLab);
2524 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2525 g_defcodelabel (DoneLab);
2531 static void hieQuest (ExprDesc* Expr)
2532 /* Parse the ternary operator */
2536 ExprDesc Expr2; /* Expression 2 */
2537 ExprDesc Expr3; /* Expression 3 */
2538 int Expr2IsNULL; /* Expression 2 is a NULL pointer */
2539 int Expr3IsNULL; /* Expression 3 is a NULL pointer */
2540 type* ResultType; /* Type of result */
2543 /* Call the lower level eval routine */
2544 if (Preprocessing) {
2550 /* Check if it's a ternary expression */
2551 if (CurTok.Tok == TOK_QUEST) {
2553 if (!ED_IsTested (Expr)) {
2554 /* Condition codes not set, request a test */
2555 ED_MarkForTest (Expr);
2557 LoadExpr (CF_NONE, Expr);
2558 labf = GetLocalLabel ();
2559 g_falsejump (CF_NONE, labf);
2561 /* Parse second expression. Remember for later if it is a NULL pointer
2562 * expression, then load it into the primary.
2564 ExprWithCheck (hie1, &Expr2);
2565 Expr2IsNULL = ED_IsNullPtr (&Expr2);
2566 if (!IsTypeVoid (Expr2.Type)) {
2567 /* Load it into the primary */
2568 LoadExpr (CF_NONE, &Expr2);
2569 ED_MakeRValExpr (&Expr2);
2571 labt = GetLocalLabel ();
2575 /* Jump here if the first expression was false */
2576 g_defcodelabel (labf);
2578 /* Parse second expression. Remember for later if it is a NULL pointer
2579 * expression, then load it into the primary.
2581 ExprWithCheck (hie1, &Expr3);
2582 Expr3IsNULL = ED_IsNullPtr (&Expr3);
2583 if (!IsTypeVoid (Expr3.Type)) {
2584 /* Load it into the primary */
2585 LoadExpr (CF_NONE, &Expr3);
2586 ED_MakeRValExpr (&Expr3);
2589 /* Check if any conversions are needed, if so, do them.
2590 * Conversion rules for ?: expression are:
2591 * - if both expressions are int expressions, default promotion
2592 * rules for ints apply.
2593 * - if both expressions are pointers of the same type, the
2594 * result of the expression is of this type.
2595 * - if one of the expressions is a pointer and the other is
2596 * a zero constant, the resulting type is that of the pointer
2598 * - if both expressions are void expressions, the result is of
2600 * - all other cases are flagged by an error.
2602 if (IsClassInt (Expr2.Type) && IsClassInt (Expr3.Type)) {
2604 /* Get common type */
2605 ResultType = promoteint (Expr2.Type, Expr3.Type);
2607 /* Convert the third expression to this type if needed */
2608 TypeConversion (&Expr3, ResultType);
2610 /* Setup a new label so that the expr3 code will jump around
2611 * the type cast code for expr2.
2613 labf = GetLocalLabel (); /* Get new label */
2614 g_jump (labf); /* Jump around code */
2616 /* The jump for expr2 goes here */
2617 g_defcodelabel (labt);
2619 /* Create the typecast code for expr2 */
2620 TypeConversion (&Expr2, ResultType);
2622 /* Jump here around the typecase code. */
2623 g_defcodelabel (labf);
2624 labt = 0; /* Mark other label as invalid */
2626 } else if (IsClassPtr (Expr2.Type) && IsClassPtr (Expr3.Type)) {
2627 /* Must point to same type */
2628 if (TypeCmp (Indirect (Expr2.Type), Indirect (Expr3.Type)) < TC_EQUAL) {
2629 Error ("Incompatible pointer types");
2631 /* Result has the common type */
2632 ResultType = Expr2.Type;
2633 } else if (IsClassPtr (Expr2.Type) && Expr3IsNULL) {
2634 /* Result type is pointer, no cast needed */
2635 ResultType = Expr2.Type;
2636 } else if (Expr2IsNULL && IsClassPtr (Expr3.Type)) {
2637 /* Result type is pointer, no cast needed */
2638 ResultType = Expr3.Type;
2639 } else if (IsTypeVoid (Expr2.Type) && IsTypeVoid (Expr3.Type)) {
2640 /* Result type is void */
2641 ResultType = Expr3.Type;
2643 Error ("Incompatible types");
2644 ResultType = Expr2.Type; /* Doesn't matter here */
2647 /* If we don't have the label defined until now, do it */
2649 g_defcodelabel (labt);
2652 /* Setup the target expression */
2653 ED_MakeRValExpr (Expr);
2654 Expr->Type = ResultType;
2660 static void opeq (const GenDesc* Gen, ExprDesc* Expr)
2661 /* Process "op=" operators. */
2668 /* op= can only be used with lvalues */
2669 if (!ED_IsLVal (Expr)) {
2670 Error ("Invalid lvalue in assignment");
2674 /* There must be an integer or pointer on the left side */
2675 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
2676 Error ("Invalid left operand type");
2677 /* Continue. Wrong code will be generated, but the compiler won't
2678 * break, so this is the best error recovery.
2682 /* Skip the operator token */
2685 /* Determine the type of the lhs */
2686 flags = TypeOf (Expr->Type);
2687 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) && IsTypePtr (Expr->Type);
2689 /* Get the lhs address on stack (if needed) */
2692 /* Fetch the lhs into the primary register if needed */
2693 LoadExpr (CF_NONE, Expr);
2695 /* Bring the lhs on stack */
2699 /* Evaluate the rhs */
2700 if (evalexpr (CF_NONE, hie1, &Expr2) == 0) {
2701 /* The resulting value is a constant. If the generator has the NOPUSH
2702 * flag set, don't push the lhs.
2704 if (Gen->Flags & GEN_NOPUSH) {
2708 /* lhs is a pointer, scale rhs */
2709 Expr2.IVal *= CheckedSizeOf (Expr->Type+1);
2712 /* If the lhs is character sized, the operation may be later done
2715 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
2716 flags |= CF_FORCECHAR;
2719 /* Special handling for add and sub - some sort of a hack, but short code */
2720 if (Gen->Func == g_add) {
2721 g_inc (flags | CF_CONST, Expr2.IVal);
2722 } else if (Gen->Func == g_sub) {
2723 g_dec (flags | CF_CONST, Expr2.IVal);
2725 Gen->Func (flags | CF_CONST, Expr2.IVal);
2728 /* rhs is not constant and already in the primary register */
2730 /* lhs is a pointer, scale rhs */
2731 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Expr->Type+1));
2734 /* If the lhs is character sized, the operation may be later done
2737 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
2738 flags |= CF_FORCECHAR;
2741 /* Adjust the types of the operands if needed */
2742 Gen->Func (g_typeadjust (flags, TypeOf (Expr2.Type)), 0);
2745 ED_MakeRValExpr (Expr);
2750 static void addsubeq (const GenDesc* Gen, ExprDesc *Expr)
2751 /* Process the += and -= operators */
2759 /* We're currently only able to handle some adressing modes */
2760 if (ED_GetLoc (Expr) == E_LOC_EXPR || ED_GetLoc (Expr) == E_LOC_PRIMARY) {
2761 /* Use generic routine */
2766 /* We must have an lvalue */
2767 if (ED_IsRVal (Expr)) {
2768 Error ("Invalid lvalue in assignment");
2772 /* There must be an integer or pointer on the left side */
2773 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
2774 Error ("Invalid left operand type");
2775 /* Continue. Wrong code will be generated, but the compiler won't
2776 * break, so this is the best error recovery.
2780 /* Skip the operator */
2783 /* Check if we have a pointer expression and must scale rhs */
2784 MustScale = IsTypePtr (Expr->Type);
2786 /* Initialize the code generator flags */
2790 /* Evaluate the rhs */
2792 if (ED_IsConstAbs (&Expr2)) {
2793 /* The resulting value is a constant. Scale it. */
2795 Expr2.IVal *= CheckedSizeOf (Indirect (Expr->Type));
2800 /* Not constant, load into the primary */
2801 LoadExpr (CF_NONE, &Expr2);
2803 /* lhs is a pointer, scale rhs */
2804 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Indirect (Expr->Type)));
2808 /* Setup the code generator flags */
2809 lflags |= TypeOf (Expr->Type) | CF_FORCECHAR;
2810 rflags |= TypeOf (Expr2.Type);
2812 /* Convert the type of the lhs to that of the rhs */
2813 g_typecast (lflags, rflags);
2815 /* Output apropriate code depending on the location */
2816 switch (ED_GetLoc (Expr)) {
2819 /* Absolute: numeric address or const */
2820 lflags |= CF_ABSOLUTE;
2821 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2822 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2824 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2829 /* Global variable */
2830 lflags |= CF_EXTERNAL;
2831 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2832 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2834 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2840 /* Static variable or literal in the literal pool */
2841 lflags |= CF_STATIC;
2842 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2843 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2845 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2849 case E_LOC_REGISTER:
2850 /* Register variable */
2851 lflags |= CF_REGVAR;
2852 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2853 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2855 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2860 /* Value on the stack */
2861 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2862 g_addeqlocal (lflags, Expr->IVal, Expr2.IVal);
2864 g_subeqlocal (lflags, Expr->IVal, Expr2.IVal);
2869 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
2872 /* Expression is a rvalue in the primary now */
2873 ED_MakeRValExpr (Expr);
2878 void hie1 (ExprDesc* Expr)
2879 /* Parse first level of expression hierarchy. */
2882 switch (CurTok.Tok) {
2888 case TOK_PLUS_ASSIGN:
2889 addsubeq (&GenPASGN, Expr);
2892 case TOK_MINUS_ASSIGN:
2893 addsubeq (&GenSASGN, Expr);
2896 case TOK_MUL_ASSIGN:
2897 opeq (&GenMASGN, Expr);
2900 case TOK_DIV_ASSIGN:
2901 opeq (&GenDASGN, Expr);
2904 case TOK_MOD_ASSIGN:
2905 opeq (&GenMOASGN, Expr);
2908 case TOK_SHL_ASSIGN:
2909 opeq (&GenSLASGN, Expr);
2912 case TOK_SHR_ASSIGN:
2913 opeq (&GenSRASGN, Expr);
2916 case TOK_AND_ASSIGN:
2917 opeq (&GenAASGN, Expr);
2920 case TOK_XOR_ASSIGN:
2921 opeq (&GenXOASGN, Expr);
2925 opeq (&GenOASGN, Expr);
2935 void hie0 (ExprDesc *Expr)
2936 /* Parse comma operator. */
2939 while (CurTok.Tok == TOK_COMMA) {
2947 int evalexpr (unsigned Flags, void (*Func) (ExprDesc*), ExprDesc* Expr)
2948 /* Will evaluate an expression via the given function. If the result is a
2949 * constant, 0 is returned and the value is put in the Expr struct. If the
2950 * result is not constant, LoadExpr is called to bring the value into the
2951 * primary register and 1 is returned.
2955 ExprWithCheck (Func, Expr);
2957 /* Check for a constant expression */
2958 if (ED_IsConstAbs (Expr)) {
2959 /* Constant expression */
2962 /* Not constant, load into the primary */
2963 LoadExpr (Flags, Expr);
2970 void Expression0 (ExprDesc* Expr)
2971 /* Evaluate an expression via hie0 and put the result into the primary register */
2973 ExprWithCheck (hie0, Expr);
2974 LoadExpr (CF_NONE, Expr);
2979 void ConstExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
2980 /* Will evaluate an expression via the given function. If the result is not
2981 * a constant of some sort, a diagnostic will be printed, and the value is
2982 * replaced by a constant one to make sure there are no internal errors that
2983 * result from this input error.
2986 ExprWithCheck (Func, Expr);
2987 if (!ED_IsConst (Expr)) {
2988 Error ("Constant expression expected");
2989 /* To avoid any compiler errors, make the expression a valid const */
2990 ED_MakeConstAbsInt (Expr, 1);
2996 void BoolExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
2997 /* Will evaluate an expression via the given function. If the result is not
2998 * something that may be evaluated in a boolean context, a diagnostic will be
2999 * printed, and the value is replaced by a constant one to make sure there
3000 * are no internal errors that result from this input error.
3003 ExprWithCheck (Func, Expr);
3004 if (!ED_IsBool (Expr)) {
3005 Error ("Boolean expression expected");
3006 /* To avoid any compiler errors, make the expression a valid int */
3007 ED_MakeConstAbsInt (Expr, 1);
3013 void ConstAbsIntExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3014 /* Will evaluate an expression via the given function. If the result is not
3015 * a constant numeric integer value, a diagnostic will be printed, and the
3016 * value is replaced by a constant one to make sure there are no internal
3017 * errors that result from this input error.
3020 ExprWithCheck (Func, Expr);
3021 if (!ED_IsConstAbsInt (Expr)) {
3022 Error ("Constant integer expression expected");
3023 /* To avoid any compiler errors, make the expression a valid const */
3024 ED_MakeConstAbsInt (Expr, 1);