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);
92 void ExprWithCheck (void (*Func) (ExprDesc*), ExprDesc *Expr)
93 /* Call an expression function with checks. */
95 /* Remember the stack pointer */
98 /* Call the expression function */
101 /* Do some checks if code generation is still constistent */
102 if (StackPtr != OldSP) {
105 "Code generation messed up!\n"
106 "StackPtr is %d, should be %d",
109 Internal ("StackPtr is %d, should be %d\n", StackPtr, OldSP);
116 static type* promoteint (type* lhst, type* rhst)
117 /* In an expression with two ints, return the type of the result */
119 /* Rules for integer types:
120 * - If one of the values is a long, the result is long.
121 * - If one of the values is unsigned, the result is also unsigned.
122 * - Otherwise the result is an int.
124 if (IsTypeLong (lhst) || IsTypeLong (rhst)) {
125 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
131 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
141 static unsigned typeadjust (ExprDesc* lhs, ExprDesc* rhs, int NoPush)
142 /* Adjust the two values for a binary operation. lhs is expected on stack or
143 * to be constant, rhs is expected to be in the primary register or constant.
144 * The function will put the type of the result into lhs and return the
145 * code generator flags for the operation.
146 * If NoPush is given, it is assumed that the operation does not expect the lhs
147 * to be on stack, and that lhs is in a register instead.
148 * Beware: The function does only accept int types.
151 unsigned ltype, rtype;
154 /* Get the type strings */
155 type* lhst = lhs->Type;
156 type* rhst = rhs->Type;
158 /* Generate type adjustment code if needed */
159 ltype = TypeOf (lhst);
160 if (ED_IsLocAbs (lhs)) {
164 /* Value is in primary register*/
167 rtype = TypeOf (rhst);
168 if (ED_IsLocAbs (rhs)) {
171 flags = g_typeadjust (ltype, rtype);
173 /* Set the type of the result */
174 lhs->Type = promoteint (lhst, rhst);
176 /* Return the code generator flags */
182 void DefineData (ExprDesc* Expr)
183 /* Output a data definition for the given expression */
185 switch (ED_GetLoc (Expr)) {
188 /* Absolute: numeric address or const */
189 g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->IVal, 0);
193 /* Global variable */
194 g_defdata (CF_EXTERNAL, Expr->Name, Expr->IVal);
199 /* Static variable or literal in the literal pool */
200 g_defdata (CF_STATIC, Expr->Name, Expr->IVal);
204 /* Register variable. Taking the address is usually not
207 if (IS_Get (&AllowRegVarAddr) == 0) {
208 Error ("Cannot take the address of a register variable");
210 g_defdata (CF_REGVAR, Expr->Name, Expr->IVal);
214 Internal ("Unknown constant type: 0x%04X", ED_GetLoc (Expr));
220 static int kcalc (token_t tok, long val1, long val2)
221 /* Calculate an operation with left and right operand constant. */
225 return (val1 == val2);
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);
246 Error ("Division by zero");
249 return (val1 / val2);
252 Error ("Modulo operation with zero");
255 return (val1 % val2);
257 Internal ("kcalc: got token 0x%X\n", tok);
264 static const GenDesc* FindGen (token_t Tok, const GenDesc* Table)
265 /* Find a token in a generator table */
267 while (Table->Tok != TOK_INVALID) {
268 if (Table->Tok == Tok) {
278 static int TypeSpecAhead (void)
279 /* Return true if some sort of type is waiting (helper for cast and sizeof()
285 /* There's a type waiting if:
287 * We have an opening paren, and
288 * a. the next token is a type, or
289 * b. the next token is a type qualifier, or
290 * c. the next token is a typedef'd type
292 return CurTok.Tok == TOK_LPAREN && (
293 TokIsType (&NextTok) ||
294 TokIsTypeQual (&NextTok) ||
295 (NextTok.Tok == TOK_IDENT &&
296 (Entry = FindSym (NextTok.Ident)) != 0 &&
297 SymIsTypeDef (Entry)));
302 void PushAddr (const ExprDesc* Expr)
303 /* If the expression contains an address that was somehow evaluated,
304 * push this address on the stack. This is a helper function for all
305 * sorts of implicit or explicit assignment functions where the lvalue
306 * must be saved if it's not constant, before evaluating the rhs.
309 /* Get the address on stack if needed */
310 if (ED_IsLocExpr (Expr)) {
311 /* Push the address (always a pointer) */
318 /*****************************************************************************/
320 /*****************************************************************************/
324 static unsigned FunctionParamList (FuncDesc* Func)
325 /* Parse a function parameter list and pass the parameters to the called
326 * function. Depending on several criteria this may be done by just pushing
327 * each parameter separately, or creating the parameter frame once and then
328 * storing into this frame.
329 * The function returns the size of the parameters pushed.
334 /* Initialize variables */
335 SymEntry* Param = 0; /* Keep gcc silent */
336 unsigned ParamSize = 0; /* Size of parameters pushed */
337 unsigned ParamCount = 0; /* Number of parameters pushed */
338 unsigned FrameSize = 0; /* Size of parameter frame */
339 unsigned FrameParams = 0; /* Number of params in frame */
340 int FrameOffs = 0; /* Offset into parameter frame */
341 int Ellipsis = 0; /* Function is variadic */
343 /* As an optimization, we may allocate the complete parameter frame at
344 * once instead of pushing each parameter as it comes. We may do that,
347 * - optimizations that increase code size are enabled (allocating the
348 * stack frame at once gives usually larger code).
349 * - we have more than one parameter to push (don't count the last param
350 * for __fastcall__ functions).
352 * The FrameSize variable will contain a value > 0 if storing into a frame
353 * (instead of pushing) is enabled.
356 if (IS_Get (&CodeSizeFactor) >= 200) {
358 /* Calculate the number and size of the parameters */
359 FrameParams = Func->ParamCount;
360 FrameSize = Func->ParamSize;
361 if (FrameParams > 0 && (Func->Flags & FD_FASTCALL) != 0) {
362 /* Last parameter is not pushed */
363 FrameSize -= CheckedSizeOf (Func->LastParam->Type);
367 /* Do we have more than one parameter in the frame? */
368 if (FrameParams > 1) {
369 /* Okeydokey, setup the frame */
370 FrameOffs = StackPtr;
372 StackPtr -= FrameSize;
374 /* Don't use a preallocated frame */
379 /* Parse the actual parameter list */
380 while (CurTok.Tok != TOK_RPAREN) {
384 /* Count arguments */
387 /* Fetch the pointer to the next argument, check for too many args */
388 if (ParamCount <= Func->ParamCount) {
389 /* Beware: If there are parameters with identical names, they
390 * cannot go into the same symbol table, which means that in this
391 * case of errorneous input, the number of nodes in the symbol
392 * table and ParamCount are NOT equal. We have to handle this case
393 * below to avoid segmentation violations. Since we know that this
394 * problem can only occur if there is more than one parameter,
395 * we will just use the last one.
397 if (ParamCount == 1) {
399 Param = Func->SymTab->SymHead;
400 } else if (Param->NextSym != 0) {
402 Param = Param->NextSym;
403 CHECK ((Param->Flags & SC_PARAM) != 0);
405 } else if (!Ellipsis) {
406 /* Too many arguments. Do we have an open param list? */
407 if ((Func->Flags & FD_VARIADIC) == 0) {
408 /* End of param list reached, no ellipsis */
409 Error ("Too many arguments in function call");
411 /* Assume an ellipsis even in case of errors to avoid an error
412 * message for each other argument.
417 /* Evaluate the parameter expression */
420 /* If we don't have an argument spec, accept anything, otherwise
421 * convert the actual argument to the type needed.
425 /* Convert the argument to the parameter type if needed */
426 TypeConversion (&Expr, Param->Type);
428 /* If we have a prototype, chars may be pushed as chars */
429 Flags |= CF_FORCECHAR;
432 /* Load the value into the primary if it is not already there */
433 LoadExpr (Flags, &Expr);
435 /* Use the type of the argument for the push */
436 Flags |= TypeOf (Expr.Type);
438 /* If this is a fastcall function, don't push the last argument */
439 if (ParamCount != Func->ParamCount || (Func->Flags & FD_FASTCALL) == 0) {
440 unsigned ArgSize = sizeofarg (Flags);
442 /* We have the space already allocated, store in the frame.
443 * Because of invalid type conversions (that have produced an
444 * error before), we can end up here with a non aligned stack
445 * frame. Since no output will be generated anyway, handle
446 * these cases gracefully instead of doing a CHECK.
448 if (FrameSize >= ArgSize) {
449 FrameSize -= ArgSize;
453 FrameOffs -= ArgSize;
455 g_putlocal (Flags | CF_NOKEEP, FrameOffs, Expr.IVal);
457 /* Push the argument */
458 g_push (Flags, Expr.IVal);
461 /* Calculate total parameter size */
462 ParamSize += ArgSize;
465 /* Check for end of argument list */
466 if (CurTok.Tok != TOK_COMMA) {
472 /* Check if we had enough parameters */
473 if (ParamCount < Func->ParamCount) {
474 Error ("Too few arguments in function call");
477 /* The function returns the size of all parameters pushed onto the stack.
478 * However, if there are parameters missing (which is an error and was
479 * flagged by the compiler) AND a stack frame was preallocated above,
480 * we would loose track of the stackpointer and generate an internal error
481 * later. So we correct the value by the parameters that should have been
482 * pushed to avoid an internal compiler error. Since an error was
483 * generated before, no code will be output anyway.
485 return ParamSize + FrameSize;
490 static void FunctionCall (ExprDesc* Expr)
491 /* Perform a function call. */
493 FuncDesc* Func; /* Function descriptor */
494 int IsFuncPtr; /* Flag */
495 int StdFunc; /* Standard function index */
496 unsigned ParamSize; /* Number of parameter bytes */
497 CodeMark Mark = 0; /* Initialize to keep gcc silent */
498 int PtrOffs = 0; /* Offset of function pointer on stack */
499 int IsFastCall = 0; /* True if it's a fast call function */
500 int PtrOnStack = 0; /* True if a pointer copy is on stack */
502 /* Skip the left paren */
505 /* Get a pointer to the function descriptor from the type string */
506 Func = GetFuncDesc (Expr->Type);
508 /* Handle function pointers transparently */
509 IsFuncPtr = IsTypeFuncPtr (Expr->Type);
512 /* Check wether it's a fastcall function that has parameters */
513 IsFastCall = IsFastCallFunc (Expr->Type + 1) && (Func->ParamCount > 0);
515 /* Things may be difficult, depending on where the function pointer
516 * resides. If the function pointer is an expression of some sort
517 * (not a local or global variable), we have to evaluate this
518 * expression now and save the result for later. Since calls to
519 * function pointers may be nested, we must save it onto the stack.
520 * For fastcall functions we do also need to place a copy of the
521 * pointer on stack, since we cannot use a/x.
523 PtrOnStack = IsFastCall || !ED_IsConst (Expr);
526 /* Not a global or local variable, or a fastcall function. Load
527 * the pointer into the primary and mark it as an expression.
529 LoadExpr (CF_NONE, Expr);
530 ED_MakeRValExpr (Expr);
532 /* Remember the code position */
533 Mark = GetCodePos ();
535 /* Push the pointer onto the stack and remember the offset */
540 /* Check for known standard functions and inline them */
541 } else if ((StdFunc = FindStdFunc ((const char*) Expr->Name)) >= 0) {
543 /* Inline this function */
544 HandleStdFunc (StdFunc, Func, Expr);
549 /* Parse the parameter list */
550 ParamSize = FunctionParamList (Func);
552 /* We need the closing paren here */
555 /* Special handling for function pointers */
558 /* If the function is not a fastcall function, load the pointer to
559 * the function into the primary.
563 /* Not a fastcall function - we may use the primary */
565 /* If we have no parameters, the pointer is still in the
566 * primary. Remove the code to push it and correct the
569 if (ParamSize == 0) {
574 /* Load from the saved copy */
575 g_getlocal (CF_PTR, PtrOffs);
578 /* Load from original location */
579 LoadExpr (CF_NONE, Expr);
582 /* Call the function */
583 g_callind (TypeOf (Expr->Type+1), ParamSize, PtrOffs);
587 /* Fastcall function. We cannot use the primary for the function
588 * pointer and must therefore use an offset to the stack location.
589 * Since fastcall functions may never be variadic, we can use the
590 * index register for this purpose.
592 g_callind (CF_LOCAL, ParamSize, PtrOffs);
595 /* If we have a pointer on stack, remove it */
597 g_space (- (int) sizeofarg (CF_PTR));
606 /* Normal function */
607 g_call (TypeOf (Expr->Type), (const char*) Expr->Name, ParamSize);
611 /* The function result is an rvalue in the primary register */
612 ED_MakeRValExpr (Expr);
613 Expr->Type = GetFuncReturn (Expr->Type);
618 static void Primary (ExprDesc* E)
619 /* This is the lowest level of the expression parser. */
623 /* Initialize fields in the expression stucture */
626 /* Character and integer constants. */
627 if (CurTok.Tok == TOK_ICONST || CurTok.Tok == TOK_CCONST) {
628 E->IVal = CurTok.IVal;
629 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
630 E->Type = CurTok.Type;
635 /* Floating point constant */
636 if (CurTok.Tok == TOK_FCONST) {
637 E->FVal = CurTok.FVal;
638 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
639 E->Type = CurTok.Type;
644 /* Process parenthesized subexpression by calling the whole parser
647 if (CurTok.Tok == TOK_LPAREN) {
654 /* If we run into an identifier in preprocessing mode, we assume that this
655 * is an undefined macro and replace it by a constant value of zero.
657 if (Preprocessing && CurTok.Tok == TOK_IDENT) {
658 ED_MakeConstAbsInt (E, 0);
662 /* All others may only be used if the expression evaluation is not called
663 * recursively by the preprocessor.
666 /* Illegal expression in PP mode */
667 Error ("Preprocessor expression expected");
668 ED_MakeConstAbsInt (E, 1);
672 switch (CurTok.Tok) {
675 /* Identifier. Get a pointer to the symbol table entry */
676 Sym = E->Sym = FindSym (CurTok.Ident);
678 /* Is the symbol known? */
681 /* We found the symbol - skip the name token */
684 /* Check for illegal symbol types */
685 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
686 if (Sym->Flags & SC_TYPE) {
687 /* Cannot use type symbols */
688 Error ("Variable identifier expected");
689 /* Assume an int type to make E valid */
690 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
695 /* Mark the symbol as referenced */
696 Sym->Flags |= SC_REF;
698 /* The expression type is the symbol type */
701 /* Check for legal symbol types */
702 if ((Sym->Flags & SC_CONST) == SC_CONST) {
703 /* Enum or some other numeric constant */
704 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
705 E->IVal = Sym->V.ConstVal;
706 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
708 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
709 E->Name = (unsigned long) Sym->Name;
710 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
711 /* Local variable. If this is a parameter for a variadic
712 * function, we have to add some address calculations, and the
713 * address is not const.
715 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
716 /* Variadic parameter */
717 g_leavariadic (Sym->V.Offs - F_GetParamSize (CurrentFunc));
718 E->Flags = E_LOC_EXPR | E_RTYPE_LVAL;
720 /* Normal parameter */
721 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
722 E->IVal = Sym->V.Offs;
724 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
725 /* Register variable, zero page based */
726 E->Flags = E_LOC_REGISTER | E_RTYPE_LVAL;
727 E->Name = Sym->V.R.RegOffs;
728 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
729 /* Static variable */
730 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
731 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
732 E->Name = (unsigned long) Sym->Name;
734 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
735 E->Name = Sym->V.Label;
738 /* Local static variable */
739 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
740 E->Name = Sym->V.Offs;
743 /* We've made all variables lvalues above. However, this is
744 * not always correct: An array is actually the address of its
745 * first element, which is a rvalue, and a function is a
746 * rvalue, too, because we cannot store anything in a function.
747 * So fix the flags depending on the type.
749 if (IsTypeArray (E->Type) || IsTypeFunc (E->Type)) {
755 /* We did not find the symbol. Remember the name, then skip it */
757 strcpy (Ident, CurTok.Ident);
760 /* IDENT is either an auto-declared function or an undefined variable. */
761 if (CurTok.Tok == TOK_LPAREN) {
762 /* Declare a function returning int. For that purpose, prepare a
763 * function signature for a function having an empty param list
766 Warning ("Function call without a prototype");
767 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
769 E->Flags = E_LOC_GLOBAL | E_RTYPE_RVAL;
770 E->Name = (unsigned long) Sym->Name;
772 /* Undeclared Variable */
773 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
774 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
776 Error ("Undefined symbol: `%s'", Ident);
784 E->Type = GetCharArrayType (GetLiteralPoolOffs () - CurTok.IVal);
785 E->Flags = E_LOC_LITERAL | E_RTYPE_RVAL;
786 E->IVal = CurTok.IVal;
787 E->Name = LiteralPoolLabel;
794 E->Flags = E_LOC_EXPR | E_RTYPE_RVAL;
799 /* Register pseudo variable */
800 E->Type = type_uchar;
801 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
806 /* Register pseudo variable */
808 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
813 /* Register pseudo variable */
814 E->Type = type_ulong;
815 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
820 /* Illegal primary. */
821 Error ("Expression expected");
822 ED_MakeConstAbsInt (E, 1);
829 static void ArrayRef (ExprDesc* Expr)
830 /* Handle an array reference */
840 /* Skip the bracket */
843 /* Get the type of left side */
846 /* We can apply a special treatment for arrays that have a const base
847 * address. This is true for most arrays and will produce a lot better
848 * code. Check if this is a const base address.
850 ConstBaseAddr = (ED_IsLocConst (Expr) || ED_IsLocStack (Expr));
852 /* If we have a constant base, we delay the address fetch */
853 Mark1 = GetCodePos ();
854 Mark2 = 0; /* Silence gcc */
855 if (!ConstBaseAddr) {
856 /* Get a pointer to the array into the primary */
857 LoadExpr (CF_NONE, Expr);
859 /* Get the array pointer on stack. Do not push more than 16
860 * bit, even if this value is greater, since we cannot handle
861 * other than 16bit stuff when doing indexing.
863 Mark2 = GetCodePos ();
867 /* TOS now contains ptr to array elements. Get the subscript. */
868 ExprWithCheck (hie0, &SubScript);
870 /* Check the types of array and subscript. We can either have a
871 * pointer/array to the left, in which case the subscript must be of an
872 * integer type, or we have an integer to the left, in which case the
873 * subscript must be a pointer/array.
874 * Since we do the necessary checking here, we can rely later on the
877 if (IsClassPtr (Expr->Type)) {
878 if (!IsClassInt (SubScript.Type)) {
879 Error ("Array subscript is not an integer");
880 /* To avoid any compiler errors, make the expression a valid int */
881 ED_MakeConstAbsInt (&SubScript, 0);
883 ElementType = Indirect (Expr->Type);
884 } else if (IsClassInt (Expr->Type)) {
885 if (!IsClassPtr (SubScript.Type)) {
886 Error ("Subscripted value is neither array nor pointer");
887 /* To avoid compiler errors, make the subscript a char[] at
890 ED_MakeConstAbs (&SubScript, 0, GetCharArrayType (1));
892 ElementType = Indirect (SubScript.Type);
894 Error ("Cannot subscript");
895 /* To avoid compiler errors, fake both the array and the subscript, so
896 * we can just proceed.
898 ED_MakeConstAbs (Expr, 0, GetCharArrayType (1));
899 ED_MakeConstAbsInt (&SubScript, 0);
900 ElementType = Indirect (Expr->Type);
903 /* Check if the subscript is constant absolute value */
904 if (ED_IsConstAbs (&SubScript)) {
906 /* The array subscript is a numeric constant. If we had pushed the
907 * array base address onto the stack before, we can remove this value,
908 * since we can generate expression+offset.
910 if (!ConstBaseAddr) {
914 /* Get an array pointer into the primary */
915 LoadExpr (CF_NONE, Expr);
918 if (IsClassPtr (Expr->Type)) {
920 /* Lhs is pointer/array. Scale the subscript value according to
923 SubScript.IVal *= CheckedSizeOf (ElementType);
925 /* Remove the address load code */
928 /* In case of an array, we can adjust the offset of the expression
929 * already in Expr. If the base address was a constant, we can even
930 * remove the code that loaded the address into the primary.
932 if (IsTypeArray (Expr->Type)) {
934 /* Adjust the offset */
935 Expr->IVal += SubScript.IVal;
939 /* It's a pointer, so we do have to load it into the primary
940 * first (if it's not already there).
943 LoadExpr (CF_NONE, Expr);
944 ED_MakeRValExpr (Expr);
948 Expr->IVal = SubScript.IVal;
953 /* Scale the rhs value according to the element type */
954 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
956 /* Add the subscript. Since arrays are indexed by integers,
957 * we will ignore the true type of the subscript here and
958 * use always an int. #### Use offset but beware of LoadExpr!
960 g_inc (CF_INT | CF_CONST, SubScript.IVal);
966 /* Array subscript is not constant. Load it into the primary */
967 Mark2 = GetCodePos ();
968 LoadExpr (CF_NONE, &SubScript);
971 if (IsClassPtr (Expr->Type)) {
973 /* Indexing is based on unsigneds, so we will just use the integer
974 * portion of the index (which is in (e)ax, so there's no further
977 g_scale (CF_INT, CheckedSizeOf (ElementType));
981 /* Get the int value on top. If we come here, we're sure, both
982 * values are 16 bit (the first one was truncated if necessary
983 * and the second one is a pointer). Note: If ConstBaseAddr is
984 * true, we don't have a value on stack, so to "swap" both, just
985 * push the subscript.
989 LoadExpr (CF_NONE, Expr);
996 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
1000 /* The offset is now in the primary register. It we didn't have a
1001 * constant base address for the lhs, the lhs address is already
1002 * on stack, and we must add the offset. If the base address was
1003 * constant, we call special functions to add the address to the
1006 if (!ConstBaseAddr) {
1008 /* The array base address is on stack and the subscript is in the
1009 * primary. Add both.
1015 /* The subscript is in the primary, and the array base address is
1016 * in Expr. If the subscript has itself a constant address, it is
1017 * often a better idea to reverse again the order of the
1018 * evaluation. This will generate better code if the subscript is
1019 * a byte sized variable. But beware: This is only possible if the
1020 * subscript was not scaled, that is, if this was a byte array
1023 if ((ED_IsLocConst (&SubScript) || ED_IsLocStack (&SubScript)) &&
1024 CheckedSizeOf (ElementType) == SIZEOF_CHAR) {
1028 /* Reverse the order of evaluation */
1029 if (CheckedSizeOf (SubScript.Type) == SIZEOF_CHAR) {
1036 /* Get a pointer to the array into the primary. */
1037 LoadExpr (CF_NONE, Expr);
1039 /* Add the variable */
1040 if (ED_IsLocStack (&SubScript)) {
1041 g_addlocal (Flags, SubScript.IVal);
1043 Flags |= GlobalModeFlags (SubScript.Flags);
1044 g_addstatic (Flags, SubScript.Name, SubScript.IVal);
1047 if (ED_IsLocAbs (Expr)) {
1048 /* Constant numeric address. Just add it */
1049 g_inc (CF_INT, Expr->IVal);
1050 } else if (ED_IsLocStack (Expr)) {
1051 /* Base address is a local variable address */
1052 if (IsTypeArray (Expr->Type)) {
1053 g_addaddr_local (CF_INT, Expr->IVal);
1055 g_addlocal (CF_PTR, Expr->IVal);
1058 /* Base address is a static variable address */
1059 unsigned Flags = CF_INT | GlobalModeFlags (Expr->Flags);
1060 if (IsTypeArray (Expr->Type)) {
1061 g_addaddr_static (Flags, Expr->Name, Expr->IVal);
1063 g_addstatic (Flags, Expr->Name, Expr->IVal);
1071 /* The result is an expression in the primary */
1072 ED_MakeRValExpr (Expr);
1076 /* Result is of element type */
1077 Expr->Type = ElementType;
1079 /* An array element is actually a variable. So the rules for variables
1080 * with respect to the reference type apply: If it's an array, it is
1081 * a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1082 * but an array cannot contain functions).
1084 if (IsTypeArray (Expr->Type)) {
1090 /* Consume the closing bracket */
1096 static void StructRef (ExprDesc* Expr)
1097 /* Process struct field after . or ->. */
1102 /* Skip the token and check for an identifier */
1104 if (CurTok.Tok != TOK_IDENT) {
1105 Error ("Identifier expected");
1106 Expr->Type = type_int;
1110 /* Get the symbol table entry and check for a struct field */
1111 strcpy (Ident, CurTok.Ident);
1113 Field = FindStructField (Expr->Type, Ident);
1115 Error ("Struct/union has no field named `%s'", Ident);
1116 Expr->Type = type_int;
1120 /* If we have a struct pointer that is an lvalue and not already in the
1121 * primary, load it now.
1123 if (ED_IsLVal (Expr) && IsTypePtr (Expr->Type)) {
1125 /* Load into the primary */
1126 LoadExpr (CF_NONE, Expr);
1128 /* Make it an lvalue expression */
1129 ED_MakeLValExpr (Expr);
1132 /* Set the struct field offset */
1133 Expr->IVal += Field->V.Offs;
1135 /* The type is now the type of the field */
1136 Expr->Type = Field->Type;
1138 /* An struct member is actually a variable. So the rules for variables
1139 * with respect to the reference type apply: If it's an array, it is
1140 * a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1141 * but a struct field cannot be a function).
1143 if (IsTypeArray (Expr->Type)) {
1152 static void hie11 (ExprDesc *Expr)
1153 /* Handle compound types (structs and arrays) */
1155 /* Evaluate the lhs */
1158 /* Check for a rhs */
1159 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN ||
1160 CurTok.Tok == TOK_DOT || CurTok.Tok == TOK_PTR_REF) {
1162 switch (CurTok.Tok) {
1165 /* Array reference */
1170 /* Function call. */
1171 if (!IsTypeFunc (Expr->Type) && !IsTypeFuncPtr (Expr->Type)) {
1172 /* Not a function */
1173 Error ("Illegal function call");
1174 /* Force the type to be a implicitly defined function, one
1175 * returning an int and taking any number of arguments.
1176 * Since we don't have a name, place it at absolute address
1179 ED_MakeConstAbs (Expr, 0, GetImplicitFuncType ());
1181 /* Call the function */
1182 FunctionCall (Expr);
1186 if (!IsClassStruct (Expr->Type)) {
1187 Error ("Struct expected");
1193 /* If we have an array, convert it to pointer to first element */
1194 if (IsTypeArray (Expr->Type)) {
1195 Expr->Type = ArrayToPtr (Expr->Type);
1197 if (!IsClassPtr (Expr->Type) || !IsClassStruct (Indirect (Expr->Type))) {
1198 Error ("Struct pointer expected");
1204 Internal ("Invalid token in hie11: %d", CurTok.Tok);
1212 void Store (ExprDesc* Expr, const type* StoreType)
1213 /* Store the primary register into the location denoted by Expr. If StoreType
1214 * is given, use this type when storing instead of Expr->Type. If StoreType
1215 * is NULL, use Expr->Type instead.
1220 /* If StoreType was not given, use Expr->Type instead */
1221 if (StoreType == 0) {
1222 StoreType = Expr->Type;
1225 /* Prepare the code generator flags */
1226 Flags = TypeOf (StoreType);
1228 /* Do the store depending on the location */
1229 switch (ED_GetLoc (Expr)) {
1232 /* Absolute: numeric address or const */
1233 g_putstatic (Flags | CF_ABSOLUTE, Expr->IVal, 0);
1237 /* Global variable */
1238 g_putstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->IVal);
1243 /* Static variable or literal in the literal pool */
1244 g_putstatic (Flags | CF_STATIC, Expr->Name, Expr->IVal);
1247 case E_LOC_REGISTER:
1248 /* Register variable */
1249 g_putstatic (Flags | CF_REGVAR, Expr->Name, Expr->IVal);
1253 /* Value on the stack */
1254 g_putlocal (Flags, Expr->IVal, 0);
1258 /* The primary register (value is already there) */
1259 /* ### Do we need a test here if the flag is set? */
1263 /* An expression in the primary register */
1264 g_putind (Flags, Expr->IVal);
1268 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
1271 /* Assume that each one of the stores will invalidate CC */
1272 ED_MarkAsUntested (Expr);
1277 static void PreInc (ExprDesc* Expr)
1278 /* Handle the preincrement operators */
1283 /* Skip the operator token */
1286 /* Evaluate the expression and check that it is an lvalue */
1288 if (!ED_IsLVal (Expr)) {
1289 Error ("Invalid lvalue");
1293 /* Get the data type */
1294 Flags = TypeOf (Expr->Type) | CF_FORCECHAR | CF_CONST;
1296 /* Get the increment value in bytes */
1297 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1299 /* Check the location of the data */
1300 switch (ED_GetLoc (Expr)) {
1303 /* Absolute: numeric address or const */
1304 g_addeqstatic (Flags | CF_ABSOLUTE, Expr->IVal, 0, Val);
1308 /* Global variable */
1309 g_addeqstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->IVal, Val);
1314 /* Static variable or literal in the literal pool */
1315 g_addeqstatic (Flags | CF_STATIC, Expr->Name, Expr->IVal, Val);
1318 case E_LOC_REGISTER:
1319 /* Register variable */
1320 g_addeqstatic (Flags | CF_REGVAR, Expr->Name, Expr->IVal, Val);
1324 /* Value on the stack */
1325 g_addeqlocal (Flags, Expr->IVal, Val);
1329 /* The primary register */
1334 /* An expression in the primary register */
1335 g_addeqind (Flags, Expr->IVal, Val);
1339 Internal ("Invalid location in PreInc(): 0x%04X", ED_GetLoc (Expr));
1342 /* Result is an expression, no reference */
1343 ED_MakeRValExpr (Expr);
1348 static void PreDec (ExprDesc* Expr)
1349 /* Handle the predecrement operators */
1354 /* Skip the operator token */
1357 /* Evaluate the expression and check that it is an lvalue */
1359 if (!ED_IsLVal (Expr)) {
1360 Error ("Invalid lvalue");
1364 /* Get the data type */
1365 Flags = TypeOf (Expr->Type) | CF_FORCECHAR | CF_CONST;
1367 /* Get the increment value in bytes */
1368 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1370 /* Check the location of the data */
1371 switch (ED_GetLoc (Expr)) {
1374 /* Absolute: numeric address or const */
1375 g_subeqstatic (Flags | CF_ABSOLUTE, Expr->IVal, 0, Val);
1379 /* Global variable */
1380 g_subeqstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->IVal, Val);
1385 /* Static variable or literal in the literal pool */
1386 g_subeqstatic (Flags | CF_STATIC, Expr->Name, Expr->IVal, Val);
1389 case E_LOC_REGISTER:
1390 /* Register variable */
1391 g_subeqstatic (Flags | CF_REGVAR, Expr->Name, Expr->IVal, Val);
1395 /* Value on the stack */
1396 g_subeqlocal (Flags, Expr->IVal, Val);
1400 /* The primary register */
1405 /* An expression in the primary register */
1406 g_subeqind (Flags, Expr->IVal, Val);
1410 Internal ("Invalid location in PreDec(): 0x%04X", ED_GetLoc (Expr));
1413 /* Result is an expression, no reference */
1414 ED_MakeRValExpr (Expr);
1419 static void PostIncDec (ExprDesc* Expr, void (*inc) (unsigned, unsigned long))
1420 /* Handle i-- and i++ */
1426 /* The expression to increment must be an lvalue */
1427 if (!ED_IsLVal (Expr)) {
1428 Error ("Invalid lvalue");
1432 /* Get the data type */
1433 Flags = TypeOf (Expr->Type);
1435 /* Push the address if needed */
1438 /* Fetch the value and save it (since it's the result of the expression) */
1439 LoadExpr (CF_NONE, Expr);
1440 g_save (Flags | CF_FORCECHAR);
1442 /* If we have a pointer expression, increment by the size of the type */
1443 if (IsTypePtr (Expr->Type)) {
1444 inc (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1446 inc (Flags | CF_CONST | CF_FORCECHAR, 1);
1449 /* Store the result back */
1452 /* Restore the original value in the primary register */
1453 g_restore (Flags | CF_FORCECHAR);
1455 /* The result is always an expression, no reference */
1456 ED_MakeRValExpr (Expr);
1461 static void UnaryOp (ExprDesc* Expr)
1462 /* Handle unary -/+ and ~ */
1466 /* Remember the operator token and skip it */
1467 token_t Tok = CurTok.Tok;
1470 /* Get the expression */
1473 /* We can only handle integer types */
1474 if (!IsClassInt (Expr->Type)) {
1475 Error ("Argument must have integer type");
1476 ED_MakeConstAbsInt (Expr, 1);
1479 /* Check for a constant expression */
1480 if (ED_IsConstAbs (Expr)) {
1481 /* Value is constant */
1483 case TOK_MINUS: Expr->IVal = -Expr->IVal; break;
1484 case TOK_PLUS: break;
1485 case TOK_COMP: Expr->IVal = ~Expr->IVal; break;
1486 default: Internal ("Unexpected token: %d", Tok);
1489 /* Value is not constant */
1490 LoadExpr (CF_NONE, Expr);
1492 /* Get the type of the expression */
1493 Flags = TypeOf (Expr->Type);
1495 /* Handle the operation */
1497 case TOK_MINUS: g_neg (Flags); break;
1498 case TOK_PLUS: break;
1499 case TOK_COMP: g_com (Flags); break;
1500 default: Internal ("Unexpected token: %d", Tok);
1503 /* The result is a rvalue in the primary */
1504 ED_MakeRValExpr (Expr);
1510 void hie10 (ExprDesc* Expr)
1511 /* Handle ++, --, !, unary - etc. */
1515 switch (CurTok.Tok) {
1533 if (evalexpr (CF_NONE, hie10, Expr) == 0) {
1534 /* Constant expression */
1535 Expr->IVal = !Expr->IVal;
1537 g_bneg (TypeOf (Expr->Type));
1538 ED_MakeRValExpr (Expr);
1539 ED_TestDone (Expr); /* bneg will set cc */
1545 ExprWithCheck (hie10, Expr);
1546 if (ED_IsLVal (Expr) || !(ED_IsLocConst (Expr) || ED_IsLocStack (Expr))) {
1547 /* Not a const, load it into the primary and make it a
1550 LoadExpr (CF_NONE, Expr);
1551 ED_MakeRValExpr (Expr);
1553 /* If the expression is already a pointer to function, the
1554 * additional dereferencing operator must be ignored.
1556 if (IsTypeFuncPtr (Expr->Type)) {
1557 /* Expression not storable */
1560 if (IsClassPtr (Expr->Type)) {
1561 Expr->Type = Indirect (Expr->Type);
1563 Error ("Illegal indirection");
1571 ExprWithCheck (hie10, Expr);
1572 /* The & operator may be applied to any lvalue, and it may be
1573 * applied to functions, even if they're no lvalues.
1575 if (ED_IsRVal (Expr) && !IsTypeFunc (Expr->Type)) {
1576 /* Allow the & operator with an array */
1577 if (!IsTypeArray (Expr->Type)) {
1578 Error ("Illegal address");
1581 Expr->Type = PointerTo (Expr->Type);
1588 if (TypeSpecAhead ()) {
1589 type Type[MAXTYPELEN];
1591 Size = CheckedSizeOf (ParseType (Type));
1594 /* Remember the output queue pointer */
1595 CodeMark Mark = GetCodePos ();
1597 Size = CheckedSizeOf (Expr->Type);
1598 /* Remove any generated code */
1601 ED_MakeConstAbs (Expr, Size, type_size_t);
1602 ED_MarkAsUntested (Expr);
1606 if (TypeSpecAhead ()) {
1616 /* Handle post increment */
1617 if (CurTok.Tok == TOK_INC) {
1618 PostIncDec (Expr, g_inc);
1619 } else if (CurTok.Tok == TOK_DEC) {
1620 PostIncDec (Expr, g_dec);
1630 static void hie_internal (const GenDesc* Ops, /* List of generators */
1632 void (*hienext) (ExprDesc*),
1634 /* Helper function */
1640 token_t Tok; /* The operator token */
1641 unsigned ltype, type;
1642 int rconst; /* Operand is a constant */
1648 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1650 /* Tell the caller that we handled it's ops */
1653 /* All operators that call this function expect an int on the lhs */
1654 if (!IsClassInt (Expr->Type)) {
1655 Error ("Integer expression expected");
1658 /* Remember the operator token, then skip it */
1662 /* Get the lhs on stack */
1663 Mark1 = GetCodePos ();
1664 ltype = TypeOf (Expr->Type);
1665 if (ED_IsConstAbs (Expr)) {
1666 /* Constant value */
1667 Mark2 = GetCodePos ();
1668 g_push (ltype | CF_CONST, Expr->IVal);
1670 /* Value not constant */
1671 LoadExpr (CF_NONE, Expr);
1672 Mark2 = GetCodePos ();
1676 /* Get the right hand side */
1677 rconst = (evalexpr (CF_NONE, hienext, &Expr2) == 0);
1679 /* Check the type of the rhs */
1680 if (!IsClassInt (Expr2.Type)) {
1681 Error ("Integer expression expected");
1684 /* Check for const operands */
1685 if (ED_IsConstAbs (Expr) && rconst) {
1687 /* Both operands are constant, remove the generated code */
1691 /* Evaluate the result */
1692 Expr->IVal = kcalc (Tok, Expr->IVal, Expr2.IVal);
1694 /* Get the type of the result */
1695 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1699 /* If the right hand side is constant, and the generator function
1700 * expects the lhs in the primary, remove the push of the primary
1703 unsigned rtype = TypeOf (Expr2.Type);
1706 /* Second value is constant - check for div */
1709 if (Tok == TOK_DIV && Expr2.IVal == 0) {
1710 Error ("Division by zero");
1711 } else if (Tok == TOK_MOD && Expr2.IVal == 0) {
1712 Error ("Modulo operation with zero");
1714 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1717 ltype |= CF_REG; /* Value is in register */
1721 /* Determine the type of the operation result. */
1722 type |= g_typeadjust (ltype, rtype);
1723 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1726 Gen->Func (type, Expr2.IVal);
1728 /* We have a rvalue in the primary now */
1729 ED_MakeRValExpr (Expr);
1736 static void hie_compare (const GenDesc* Ops, /* List of generators */
1738 void (*hienext) (ExprDesc*))
1739 /* Helper function for the compare operators */
1745 token_t tok; /* The operator token */
1747 int rconst; /* Operand is a constant */
1752 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1754 /* Remember the operator token, then skip it */
1758 /* Get the lhs on stack */
1759 Mark1 = GetCodePos ();
1760 ltype = TypeOf (Expr->Type);
1761 if (ED_IsConstAbs (Expr)) {
1762 /* Constant value */
1763 Mark2 = GetCodePos ();
1764 g_push (ltype | CF_CONST, Expr->IVal);
1766 /* Value not constant */
1767 LoadExpr (CF_NONE, Expr);
1768 Mark2 = GetCodePos ();
1772 /* Get the right hand side */
1773 rconst = (evalexpr (CF_NONE, hienext, &Expr2) == 0);
1775 /* Make sure, the types are compatible */
1776 if (IsClassInt (Expr->Type)) {
1777 if (!IsClassInt (Expr2.Type) && !(IsClassPtr(Expr2.Type) && ED_IsNullPtr(Expr))) {
1778 Error ("Incompatible types");
1780 } else if (IsClassPtr (Expr->Type)) {
1781 if (IsClassPtr (Expr2.Type)) {
1782 /* Both pointers are allowed in comparison if they point to
1783 * the same type, or if one of them is a void pointer.
1785 type* left = Indirect (Expr->Type);
1786 type* right = Indirect (Expr2.Type);
1787 if (TypeCmp (left, right) < TC_EQUAL && *left != T_VOID && *right != T_VOID) {
1788 /* Incomatible pointers */
1789 Error ("Incompatible types");
1791 } else if (!ED_IsNullPtr (&Expr2)) {
1792 Error ("Incompatible types");
1796 /* Check for const operands */
1797 if (ED_IsConstAbs (Expr) && rconst) {
1799 /* Both operands are constant, remove the generated code */
1803 /* Evaluate the result */
1804 Expr->IVal = kcalc (tok, Expr->IVal, Expr2.IVal);
1808 /* If the right hand side is constant, and the generator function
1809 * expects the lhs in the primary, remove the push of the primary
1815 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1818 ltype |= CF_REG; /* Value is in register */
1822 /* Determine the type of the operation result. If the left
1823 * operand is of type char and the right is a constant, or
1824 * if both operands are of type char, we will encode the
1825 * operation as char operation. Otherwise the default
1826 * promotions are used.
1828 if (IsTypeChar (Expr->Type) && (IsTypeChar (Expr2.Type) || rconst)) {
1830 if (IsSignUnsigned (Expr->Type) || IsSignUnsigned (Expr2.Type)) {
1831 flags |= CF_UNSIGNED;
1834 flags |= CF_FORCECHAR;
1837 unsigned rtype = TypeOf (Expr2.Type) | (flags & CF_CONST);
1838 flags |= g_typeadjust (ltype, rtype);
1842 Gen->Func (flags, Expr2.IVal);
1844 /* The result is an rvalue in the primary */
1845 ED_MakeRValExpr (Expr);
1848 /* Result type is always int */
1849 Expr->Type = type_int;
1851 /* Condition codes are set */
1858 static void hie9 (ExprDesc *Expr)
1859 /* Process * and / operators. */
1861 static const GenDesc hie9_ops[] = {
1862 { TOK_STAR, GEN_NOPUSH, g_mul },
1863 { TOK_DIV, GEN_NOPUSH, g_div },
1864 { TOK_MOD, GEN_NOPUSH, g_mod },
1865 { TOK_INVALID, 0, 0 }
1869 hie_internal (hie9_ops, Expr, hie10, &UsedGen);
1874 static void parseadd (ExprDesc* Expr)
1875 /* Parse an expression with the binary plus operator. Expr contains the
1876 * unprocessed left hand side of the expression and will contain the
1877 * result of the expression on return.
1881 unsigned flags; /* Operation flags */
1882 CodeMark Mark; /* Remember code position */
1883 type* lhst; /* Type of left hand side */
1884 type* rhst; /* Type of right hand side */
1887 /* Skip the PLUS token */
1890 /* Get the left hand side type, initialize operation flags */
1894 /* Check for constness on both sides */
1895 if (ED_IsConst (Expr)) {
1897 /* The left hand side is a constant of some sort. Good. Get rhs */
1899 if (ED_IsConstAbs (&Expr2)) {
1901 /* Right hand side is a constant numeric value. Get the rhs type */
1904 /* Both expressions are constants. Check for pointer arithmetic */
1905 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1906 /* Left is pointer, right is int, must scale rhs */
1907 Expr->IVal += Expr2.IVal * CheckedPSizeOf (lhst);
1908 /* Result type is a pointer */
1909 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1910 /* Left is int, right is pointer, must scale lhs */
1911 Expr->IVal = Expr->IVal * CheckedPSizeOf (rhst) + Expr2.IVal;
1912 /* Result type is a pointer */
1913 Expr->Type = Expr2.Type;
1914 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1915 /* Integer addition */
1916 Expr->IVal += Expr2.IVal;
1917 typeadjust (Expr, &Expr2, 1);
1920 Error ("Invalid operands for binary operator `+'");
1925 /* lhs is a constant and rhs is not constant. Load rhs into
1928 LoadExpr (CF_NONE, &Expr2);
1930 /* Beware: The check above (for lhs) lets not only pass numeric
1931 * constants, but also constant addresses (labels), maybe even
1932 * with an offset. We have to check for that here.
1935 /* First, get the rhs type. */
1939 if (ED_IsLocAbs (Expr)) {
1940 /* A numerical constant */
1943 /* Constant address label */
1944 flags |= GlobalModeFlags (Expr->Flags) | CF_CONSTADDR;
1947 /* Check for pointer arithmetic */
1948 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
1949 /* Left is pointer, right is int, must scale rhs */
1950 g_scale (CF_INT, CheckedPSizeOf (lhst));
1951 /* Operate on pointers, result type is a pointer */
1953 /* Generate the code for the add */
1954 if (ED_GetLoc (Expr) == E_LOC_ABS) {
1955 /* Numeric constant */
1956 g_inc (flags, Expr->IVal);
1958 /* Constant address */
1959 g_addaddr_static (flags, Expr->Name, Expr->IVal);
1961 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
1963 /* Left is int, right is pointer, must scale lhs. */
1964 unsigned ScaleFactor = CheckedPSizeOf (rhst);
1966 /* Operate on pointers, result type is a pointer */
1968 Expr->Type = Expr2.Type;
1970 /* Since we do already have rhs in the primary, if lhs is
1971 * not a numeric constant, and the scale factor is not one
1972 * (no scaling), we must take the long way over the stack.
1974 if (ED_IsLocAbs (Expr)) {
1975 /* Numeric constant, scale lhs */
1976 Expr->IVal *= ScaleFactor;
1977 /* Generate the code for the add */
1978 g_inc (flags, Expr->IVal);
1979 } else if (ScaleFactor == 1) {
1980 /* Constant address but no need to scale */
1981 g_addaddr_static (flags, Expr->Name, Expr->IVal);
1983 /* Constant address that must be scaled */
1984 g_push (TypeOf (Expr2.Type), 0); /* rhs --> stack */
1985 g_getimmed (flags, Expr->Name, Expr->IVal);
1986 g_scale (CF_PTR, ScaleFactor);
1989 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
1990 /* Integer addition */
1991 flags |= typeadjust (Expr, &Expr2, 1);
1992 /* Generate the code for the add */
1993 if (ED_IsLocAbs (Expr)) {
1994 /* Numeric constant */
1995 g_inc (flags, Expr->IVal);
1997 /* Constant address */
1998 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2002 Error ("Invalid operands for binary operator `+'");
2005 /* Result is a rvalue in primary register */
2006 ED_MakeRValExpr (Expr);
2011 /* Left hand side is not constant. Get the value onto the stack. */
2012 LoadExpr (CF_NONE, Expr); /* --> primary register */
2013 Mark = GetCodePos ();
2014 g_push (TypeOf (Expr->Type), 0); /* --> stack */
2016 /* Evaluate the rhs */
2017 if (evalexpr (CF_NONE, hie9, &Expr2) == 0) {
2019 /* Right hand side is a constant. Get the rhs type */
2022 /* Remove pushed value from stack */
2024 pop (TypeOf (Expr->Type));
2026 /* Check for pointer arithmetic */
2027 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2028 /* Left is pointer, right is int, must scale rhs */
2029 Expr2.IVal *= CheckedPSizeOf (lhst);
2030 /* Operate on pointers, result type is a pointer */
2032 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2033 /* Left is int, right is pointer, must scale lhs (ptr only) */
2034 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2035 /* Operate on pointers, result type is a pointer */
2037 Expr->Type = Expr2.Type;
2038 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2039 /* Integer addition */
2040 flags = typeadjust (Expr, &Expr2, 1);
2043 Error ("Invalid operands for binary operator `+'");
2046 /* Generate code for the add */
2047 g_inc (flags | CF_CONST, Expr2.IVal);
2051 /* lhs and rhs are not constant. Get the rhs type. */
2054 /* Check for pointer arithmetic */
2055 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2056 /* Left is pointer, right is int, must scale rhs */
2057 g_scale (CF_INT, CheckedPSizeOf (lhst));
2058 /* Operate on pointers, result type is a pointer */
2060 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2061 /* Left is int, right is pointer, must scale lhs */
2062 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2063 g_swap (CF_INT); /* Swap TOS and primary */
2064 g_scale (CF_INT, CheckedPSizeOf (rhst));
2065 /* Operate on pointers, result type is a pointer */
2067 Expr->Type = Expr2.Type;
2068 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2069 /* Integer addition. Note: Result is never constant.
2070 * Problem here is that typeadjust does not know if the
2071 * variable is an rvalue or lvalue, so if both operands
2072 * are dereferenced constant numeric addresses, typeadjust
2073 * thinks the operation works on constants. Removing
2074 * CF_CONST here means handling the symptoms, however, the
2075 * whole parser is such a mess that I fear to break anything
2076 * when trying to apply another solution.
2078 flags = typeadjust (Expr, &Expr2, 0) & ~CF_CONST;
2081 Error ("Invalid operands for binary operator `+'");
2084 /* Generate code for the add */
2089 /* Result is a rvalue in primary register */
2090 ED_MakeRValExpr (Expr);
2093 /* Condition codes not set */
2094 ED_MarkAsUntested (Expr);
2100 static void parsesub (ExprDesc* Expr)
2101 /* Parse an expression with the binary minus operator. Expr contains the
2102 * unprocessed left hand side of the expression and will contain the
2103 * result of the expression on return.
2107 unsigned flags; /* Operation flags */
2108 type* lhst; /* Type of left hand side */
2109 type* rhst; /* Type of right hand side */
2110 CodeMark Mark1; /* Save position of output queue */
2111 CodeMark Mark2; /* Another position in the queue */
2112 int rscale; /* Scale factor for the result */
2115 /* Skip the MINUS token */
2118 /* Get the left hand side type, initialize operation flags */
2121 rscale = 1; /* Scale by 1, that is, don't scale */
2123 /* Remember the output queue position, then bring the value onto the stack */
2124 Mark1 = GetCodePos ();
2125 LoadExpr (CF_NONE, Expr); /* --> primary register */
2126 Mark2 = GetCodePos ();
2127 g_push (TypeOf (lhst), 0); /* --> stack */
2129 /* Parse the right hand side */
2130 if (evalexpr (CF_NONE, hie9, &Expr2) == 0) {
2132 /* The right hand side is constant. Get the rhs type. */
2135 /* Check left hand side */
2136 if (ED_IsConstAbs (Expr)) {
2138 /* Both sides are constant, remove generated code */
2140 pop (TypeOf (lhst)); /* Clean up the stack */
2142 /* Check for pointer arithmetic */
2143 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2144 /* Left is pointer, right is int, must scale rhs */
2145 Expr->IVal -= Expr2.IVal * CheckedPSizeOf (lhst);
2146 /* Operate on pointers, result type is a pointer */
2147 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2148 /* Left is pointer, right is pointer, must scale result */
2149 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2150 Error ("Incompatible pointer types");
2152 Expr->IVal = (Expr->IVal - Expr2.IVal) /
2153 CheckedPSizeOf (lhst);
2155 /* Operate on pointers, result type is an integer */
2156 Expr->Type = type_int;
2157 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2158 /* Integer subtraction */
2159 typeadjust (Expr, &Expr2, 1);
2160 Expr->IVal -= Expr2.IVal;
2163 Error ("Invalid operands for binary operator `-'");
2166 /* Result is constant, condition codes not set */
2167 ED_MarkAsUntested (Expr);
2171 /* Left hand side is not constant, right hand side is.
2172 * Remove pushed value from stack.
2175 pop (TypeOf (lhst));
2177 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2178 /* Left is pointer, right is int, must scale rhs */
2179 Expr2.IVal *= CheckedPSizeOf (lhst);
2180 /* Operate on pointers, result type is a pointer */
2182 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2183 /* Left is pointer, right is pointer, must scale result */
2184 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2185 Error ("Incompatible pointer types");
2187 rscale = CheckedPSizeOf (lhst);
2189 /* Operate on pointers, result type is an integer */
2191 Expr->Type = type_int;
2192 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2193 /* Integer subtraction */
2194 flags = typeadjust (Expr, &Expr2, 1);
2197 Error ("Invalid operands for binary operator `-'");
2200 /* Do the subtraction */
2201 g_dec (flags | CF_CONST, Expr2.IVal);
2203 /* If this was a pointer subtraction, we must scale the result */
2205 g_scale (flags, -rscale);
2208 /* Result is a rvalue in the primary register */
2209 ED_MakeRValExpr (Expr);
2210 ED_MarkAsUntested (Expr);
2216 /* Right hand side is not constant. Get the rhs type. */
2219 /* Check for pointer arithmetic */
2220 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2221 /* Left is pointer, right is int, must scale rhs */
2222 g_scale (CF_INT, CheckedPSizeOf (lhst));
2223 /* Operate on pointers, result type is a pointer */
2225 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2226 /* Left is pointer, right is pointer, must scale result */
2227 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2228 Error ("Incompatible pointer types");
2230 rscale = CheckedPSizeOf (lhst);
2232 /* Operate on pointers, result type is an integer */
2234 Expr->Type = type_int;
2235 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2236 /* Integer subtraction. If the left hand side descriptor says that
2237 * the lhs is const, we have to remove this mark, since this is no
2238 * longer true, lhs is on stack instead.
2240 if (ED_IsLocAbs (Expr)) {
2241 ED_MakeRValExpr (Expr);
2243 /* Adjust operand types */
2244 flags = typeadjust (Expr, &Expr2, 0);
2247 Error ("Invalid operands for binary operator `-'");
2250 /* Generate code for the sub (the & is a hack here) */
2251 g_sub (flags & ~CF_CONST, 0);
2253 /* If this was a pointer subtraction, we must scale the result */
2255 g_scale (flags, -rscale);
2258 /* Result is a rvalue in the primary register */
2259 ED_MakeRValExpr (Expr);
2260 ED_MarkAsUntested (Expr);
2266 void hie8 (ExprDesc* Expr)
2267 /* Process + and - binary operators. */
2270 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2271 if (CurTok.Tok == TOK_PLUS) {
2281 static void hie6 (ExprDesc* Expr)
2282 /* Handle greater-than type comparators */
2284 static const GenDesc hie6_ops [] = {
2285 { TOK_LT, GEN_NOPUSH, g_lt },
2286 { TOK_LE, GEN_NOPUSH, g_le },
2287 { TOK_GE, GEN_NOPUSH, g_ge },
2288 { TOK_GT, GEN_NOPUSH, g_gt },
2289 { TOK_INVALID, 0, 0 }
2291 hie_compare (hie6_ops, Expr, ShiftExpr);
2296 static void hie5 (ExprDesc* Expr)
2297 /* Handle == and != */
2299 static const GenDesc hie5_ops[] = {
2300 { TOK_EQ, GEN_NOPUSH, g_eq },
2301 { TOK_NE, GEN_NOPUSH, g_ne },
2302 { TOK_INVALID, 0, 0 }
2304 hie_compare (hie5_ops, Expr, hie6);
2309 static void hie4 (ExprDesc* Expr)
2310 /* Handle & (bitwise and) */
2312 static const GenDesc hie4_ops[] = {
2313 { TOK_AND, GEN_NOPUSH, g_and },
2314 { TOK_INVALID, 0, 0 }
2318 hie_internal (hie4_ops, Expr, hie5, &UsedGen);
2323 static void hie3 (ExprDesc* Expr)
2324 /* Handle ^ (bitwise exclusive or) */
2326 static const GenDesc hie3_ops[] = {
2327 { TOK_XOR, GEN_NOPUSH, g_xor },
2328 { TOK_INVALID, 0, 0 }
2332 hie_internal (hie3_ops, Expr, hie4, &UsedGen);
2337 static void hie2 (ExprDesc* Expr)
2338 /* Handle | (bitwise or) */
2340 static const GenDesc hie2_ops[] = {
2341 { TOK_OR, GEN_NOPUSH, g_or },
2342 { TOK_INVALID, 0, 0 }
2346 hie_internal (hie2_ops, Expr, hie3, &UsedGen);
2351 static void hieAndPP (ExprDesc* Expr)
2352 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2353 * called recursively from the preprocessor.
2358 ConstAbsIntExpr (hie2, Expr);
2359 while (CurTok.Tok == TOK_BOOL_AND) {
2365 ConstAbsIntExpr (hie2, &Expr2);
2367 /* Combine the two */
2368 Expr->IVal = (Expr->IVal && Expr2.IVal);
2374 static void hieOrPP (ExprDesc *Expr)
2375 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2376 * called recursively from the preprocessor.
2381 ConstAbsIntExpr (hieAndPP, Expr);
2382 while (CurTok.Tok == TOK_BOOL_OR) {
2388 ConstAbsIntExpr (hieAndPP, &Expr2);
2390 /* Combine the two */
2391 Expr->IVal = (Expr->IVal || Expr2.IVal);
2397 static void hieAnd (ExprDesc* Expr, unsigned TrueLab, int* BoolOp)
2398 /* Process "exp && exp" */
2404 if (CurTok.Tok == TOK_BOOL_AND) {
2406 /* Tell our caller that we're evaluating a boolean */
2409 /* Get a label that we will use for false expressions */
2410 lab = GetLocalLabel ();
2412 /* If the expr hasn't set condition codes, set the force-test flag */
2413 if (!ED_IsTested (Expr)) {
2414 ED_MarkForTest (Expr);
2417 /* Load the value */
2418 LoadExpr (CF_FORCECHAR, Expr);
2420 /* Generate the jump */
2421 g_falsejump (CF_NONE, lab);
2423 /* Parse more boolean and's */
2424 while (CurTok.Tok == TOK_BOOL_AND) {
2431 if (!ED_IsTested (&Expr2)) {
2432 ED_MarkForTest (&Expr2);
2434 LoadExpr (CF_FORCECHAR, &Expr2);
2436 /* Do short circuit evaluation */
2437 if (CurTok.Tok == TOK_BOOL_AND) {
2438 g_falsejump (CF_NONE, lab);
2440 /* Last expression - will evaluate to true */
2441 g_truejump (CF_NONE, TrueLab);
2445 /* Define the false jump label here */
2446 g_defcodelabel (lab);
2448 /* The result is an rvalue in primary */
2449 ED_MakeRValExpr (Expr);
2450 ED_TestDone (Expr); /* Condition codes are set */
2456 static void hieOr (ExprDesc *Expr)
2457 /* Process "exp || exp". */
2460 int BoolOp = 0; /* Did we have a boolean op? */
2461 int AndOp; /* Did we have a && operation? */
2462 unsigned TrueLab; /* Jump to this label if true */
2466 TrueLab = GetLocalLabel ();
2468 /* Call the next level parser */
2469 hieAnd (Expr, TrueLab, &BoolOp);
2471 /* Any boolean or's? */
2472 if (CurTok.Tok == TOK_BOOL_OR) {
2474 /* If the expr hasn't set condition codes, set the force-test flag */
2475 if (!ED_IsTested (Expr)) {
2476 ED_MarkForTest (Expr);
2479 /* Get first expr */
2480 LoadExpr (CF_FORCECHAR, Expr);
2482 /* For each expression jump to TrueLab if true. Beware: If we
2483 * had && operators, the jump is already in place!
2486 g_truejump (CF_NONE, TrueLab);
2489 /* Remember that we had a boolean op */
2492 /* while there's more expr */
2493 while (CurTok.Tok == TOK_BOOL_OR) {
2500 hieAnd (&Expr2, TrueLab, &AndOp);
2501 if (!ED_IsTested (&Expr2)) {
2502 ED_MarkForTest (&Expr2);
2504 LoadExpr (CF_FORCECHAR, &Expr2);
2506 /* If there is more to come, add shortcut boolean eval. */
2507 g_truejump (CF_NONE, TrueLab);
2511 /* The result is an rvalue in primary */
2512 ED_MakeRValExpr (Expr);
2513 ED_TestDone (Expr); /* Condition codes are set */
2516 /* If we really had boolean ops, generate the end sequence */
2518 DoneLab = GetLocalLabel ();
2519 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2520 g_falsejump (CF_NONE, DoneLab);
2521 g_defcodelabel (TrueLab);
2522 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2523 g_defcodelabel (DoneLab);
2529 static void hieQuest (ExprDesc* Expr)
2530 /* Parse the ternary operator */
2534 ExprDesc Expr2; /* Expression 2 */
2535 ExprDesc Expr3; /* Expression 3 */
2536 int Expr2IsNULL; /* Expression 2 is a NULL pointer */
2537 int Expr3IsNULL; /* Expression 3 is a NULL pointer */
2538 type* ResultType; /* Type of result */
2541 /* Call the lower level eval routine */
2542 if (Preprocessing) {
2548 /* Check if it's a ternary expression */
2549 if (CurTok.Tok == TOK_QUEST) {
2551 if (!ED_IsTested (Expr)) {
2552 /* Condition codes not set, request a test */
2553 ED_MarkForTest (Expr);
2555 LoadExpr (CF_NONE, Expr);
2556 labf = GetLocalLabel ();
2557 g_falsejump (CF_NONE, labf);
2559 /* Parse second expression. Remember for later if it is a NULL pointer
2560 * expression, then load it into the primary.
2562 ExprWithCheck (hie1, &Expr2);
2563 Expr2IsNULL = ED_IsNullPtr (&Expr2);
2564 if (!IsTypeVoid (Expr2.Type)) {
2565 /* Load it into the primary */
2566 LoadExpr (CF_NONE, &Expr2);
2567 ED_MakeRValExpr (&Expr2);
2569 labt = GetLocalLabel ();
2573 /* Jump here if the first expression was false */
2574 g_defcodelabel (labf);
2576 /* Parse second expression. Remember for later if it is a NULL pointer
2577 * expression, then load it into the primary.
2579 ExprWithCheck (hie1, &Expr3);
2580 Expr3IsNULL = ED_IsNullPtr (&Expr3);
2581 if (!IsTypeVoid (Expr3.Type)) {
2582 /* Load it into the primary */
2583 LoadExpr (CF_NONE, &Expr3);
2584 ED_MakeRValExpr (&Expr3);
2587 /* Check if any conversions are needed, if so, do them.
2588 * Conversion rules for ?: expression are:
2589 * - if both expressions are int expressions, default promotion
2590 * rules for ints apply.
2591 * - if both expressions are pointers of the same type, the
2592 * result of the expression is of this type.
2593 * - if one of the expressions is a pointer and the other is
2594 * a zero constant, the resulting type is that of the pointer
2596 * - if both expressions are void expressions, the result is of
2598 * - all other cases are flagged by an error.
2600 if (IsClassInt (Expr2.Type) && IsClassInt (Expr3.Type)) {
2602 /* Get common type */
2603 ResultType = promoteint (Expr2.Type, Expr3.Type);
2605 /* Convert the third expression to this type if needed */
2606 TypeConversion (&Expr3, ResultType);
2608 /* Setup a new label so that the expr3 code will jump around
2609 * the type cast code for expr2.
2611 labf = GetLocalLabel (); /* Get new label */
2612 g_jump (labf); /* Jump around code */
2614 /* The jump for expr2 goes here */
2615 g_defcodelabel (labt);
2617 /* Create the typecast code for expr2 */
2618 TypeConversion (&Expr2, ResultType);
2620 /* Jump here around the typecase code. */
2621 g_defcodelabel (labf);
2622 labt = 0; /* Mark other label as invalid */
2624 } else if (IsClassPtr (Expr2.Type) && IsClassPtr (Expr3.Type)) {
2625 /* Must point to same type */
2626 if (TypeCmp (Indirect (Expr2.Type), Indirect (Expr3.Type)) < TC_EQUAL) {
2627 Error ("Incompatible pointer types");
2629 /* Result has the common type */
2630 ResultType = Expr2.Type;
2631 } else if (IsClassPtr (Expr2.Type) && Expr3IsNULL) {
2632 /* Result type is pointer, no cast needed */
2633 ResultType = Expr2.Type;
2634 } else if (Expr2IsNULL && IsClassPtr (Expr3.Type)) {
2635 /* Result type is pointer, no cast needed */
2636 ResultType = Expr3.Type;
2637 } else if (IsTypeVoid (Expr2.Type) && IsTypeVoid (Expr3.Type)) {
2638 /* Result type is void */
2639 ResultType = Expr3.Type;
2641 Error ("Incompatible types");
2642 ResultType = Expr2.Type; /* Doesn't matter here */
2645 /* If we don't have the label defined until now, do it */
2647 g_defcodelabel (labt);
2650 /* Setup the target expression */
2651 ED_MakeRValExpr (Expr);
2652 Expr->Type = ResultType;
2658 static void opeq (const GenDesc* Gen, ExprDesc* Expr)
2659 /* Process "op=" operators. */
2666 /* op= can only be used with lvalues */
2667 if (!ED_IsLVal (Expr)) {
2668 Error ("Invalid lvalue in assignment");
2672 /* There must be an integer or pointer on the left side */
2673 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
2674 Error ("Invalid left operand type");
2675 /* Continue. Wrong code will be generated, but the compiler won't
2676 * break, so this is the best error recovery.
2680 /* Skip the operator token */
2683 /* Determine the type of the lhs */
2684 flags = TypeOf (Expr->Type);
2685 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) && IsTypePtr (Expr->Type);
2687 /* Get the lhs address on stack (if needed) */
2690 /* Fetch the lhs into the primary register if needed */
2691 LoadExpr (CF_NONE, Expr);
2693 /* Bring the lhs on stack */
2694 Mark = GetCodePos ();
2697 /* Evaluate the rhs */
2698 if (evalexpr (CF_NONE, hie1, &Expr2) == 0) {
2699 /* The resulting value is a constant. If the generator has the NOPUSH
2700 * flag set, don't push the lhs.
2702 if (Gen->Flags & GEN_NOPUSH) {
2707 /* lhs is a pointer, scale rhs */
2708 Expr2.IVal *= CheckedSizeOf (Expr->Type+1);
2711 /* If the lhs is character sized, the operation may be later done
2714 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
2715 flags |= CF_FORCECHAR;
2718 /* Special handling for add and sub - some sort of a hack, but short code */
2719 if (Gen->Func == g_add) {
2720 g_inc (flags | CF_CONST, Expr2.IVal);
2721 } else if (Gen->Func == g_sub) {
2722 g_dec (flags | CF_CONST, Expr2.IVal);
2724 Gen->Func (flags | CF_CONST, Expr2.IVal);
2727 /* rhs is not constant and already in the primary register */
2729 /* lhs is a pointer, scale rhs */
2730 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Expr->Type+1));
2733 /* If the lhs is character sized, the operation may be later done
2736 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
2737 flags |= CF_FORCECHAR;
2740 /* Adjust the types of the operands if needed */
2741 Gen->Func (g_typeadjust (flags, TypeOf (Expr2.Type)), 0);
2744 ED_MakeRValExpr (Expr);
2749 static void addsubeq (const GenDesc* Gen, ExprDesc *Expr)
2750 /* Process the += and -= operators */
2758 /* We're currently only able to handle some adressing modes */
2759 if (ED_GetLoc (Expr) == E_LOC_EXPR || ED_GetLoc (Expr) == E_LOC_PRIMARY) {
2760 /* Use generic routine */
2765 /* We must have an lvalue */
2766 if (ED_IsRVal (Expr)) {
2767 Error ("Invalid lvalue in assignment");
2771 /* There must be an integer or pointer on the left side */
2772 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
2773 Error ("Invalid left operand type");
2774 /* Continue. Wrong code will be generated, but the compiler won't
2775 * break, so this is the best error recovery.
2779 /* Skip the operator */
2782 /* Check if we have a pointer expression and must scale rhs */
2783 MustScale = IsTypePtr (Expr->Type);
2785 /* Initialize the code generator flags */
2789 /* Evaluate the rhs */
2791 if (ED_IsConstAbs (&Expr2)) {
2792 /* The resulting value is a constant. Scale it. */
2794 Expr2.IVal *= CheckedSizeOf (Indirect (Expr->Type));
2799 /* Not constant, load into the primary */
2800 LoadExpr (CF_NONE, &Expr2);
2802 /* lhs is a pointer, scale rhs */
2803 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Indirect (Expr->Type)));
2807 /* Setup the code generator flags */
2808 lflags |= TypeOf (Expr->Type) | CF_FORCECHAR;
2809 rflags |= TypeOf (Expr2.Type);
2811 /* Convert the type of the lhs to that of the rhs */
2812 g_typecast (lflags, rflags);
2814 /* Output apropriate code depending on the location */
2815 switch (ED_GetLoc (Expr)) {
2818 /* Absolute: numeric address or const */
2819 lflags |= CF_ABSOLUTE;
2820 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2821 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2823 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2828 /* Global variable */
2829 lflags |= CF_EXTERNAL;
2830 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2831 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2833 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2839 /* Static variable or literal in the literal pool */
2840 lflags |= CF_STATIC;
2841 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2842 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2844 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2848 case E_LOC_REGISTER:
2849 /* Register variable */
2850 lflags |= CF_REGVAR;
2851 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2852 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2854 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2859 /* Value on the stack */
2860 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2861 g_addeqlocal (lflags, Expr->IVal, Expr2.IVal);
2863 g_subeqlocal (lflags, Expr->IVal, Expr2.IVal);
2868 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
2871 /* Expression is a rvalue in the primary now */
2872 ED_MakeRValExpr (Expr);
2877 void hie1 (ExprDesc* Expr)
2878 /* Parse first level of expression hierarchy. */
2881 switch (CurTok.Tok) {
2887 case TOK_PLUS_ASSIGN:
2888 addsubeq (&GenPASGN, Expr);
2891 case TOK_MINUS_ASSIGN:
2892 addsubeq (&GenSASGN, Expr);
2895 case TOK_MUL_ASSIGN:
2896 opeq (&GenMASGN, Expr);
2899 case TOK_DIV_ASSIGN:
2900 opeq (&GenDASGN, Expr);
2903 case TOK_MOD_ASSIGN:
2904 opeq (&GenMOASGN, Expr);
2907 case TOK_SHL_ASSIGN:
2908 opeq (&GenSLASGN, Expr);
2911 case TOK_SHR_ASSIGN:
2912 opeq (&GenSRASGN, Expr);
2915 case TOK_AND_ASSIGN:
2916 opeq (&GenAASGN, Expr);
2919 case TOK_XOR_ASSIGN:
2920 opeq (&GenXOASGN, Expr);
2924 opeq (&GenOASGN, Expr);
2934 void hie0 (ExprDesc *Expr)
2935 /* Parse comma operator. */
2938 while (CurTok.Tok == TOK_COMMA) {
2946 int evalexpr (unsigned Flags, void (*Func) (ExprDesc*), ExprDesc* Expr)
2947 /* Will evaluate an expression via the given function. If the result is a
2948 * constant, 0 is returned and the value is put in the Expr struct. If the
2949 * result is not constant, LoadExpr is called to bring the value into the
2950 * primary register and 1 is returned.
2954 ExprWithCheck (Func, Expr);
2956 /* Check for a constant expression */
2957 if (ED_IsConstAbs (Expr)) {
2958 /* Constant expression */
2961 /* Not constant, load into the primary */
2962 LoadExpr (Flags, Expr);
2969 void Expression0 (ExprDesc* Expr)
2970 /* Evaluate an expression via hie0 and put the result into the primary register */
2972 ExprWithCheck (hie0, Expr);
2973 LoadExpr (CF_NONE, Expr);
2978 void ConstExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
2979 /* Will evaluate an expression via the given function. If the result is not
2980 * a constant of some sort, a diagnostic will be printed, and the value is
2981 * replaced by a constant one to make sure there are no internal errors that
2982 * result from this input error.
2985 ExprWithCheck (Func, Expr);
2986 if (!ED_IsConst (Expr)) {
2987 Error ("Constant expression expected");
2988 /* To avoid any compiler errors, make the expression a valid const */
2989 ED_MakeConstAbsInt (Expr, 1);
2995 void BoolExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
2996 /* Will evaluate an expression via the given function. If the result is not
2997 * something that may be evaluated in a boolean context, a diagnostic will be
2998 * printed, and the value is replaced by a constant one to make sure there
2999 * are no internal errors that result from this input error.
3002 ExprWithCheck (Func, Expr);
3003 if (!ED_IsBool (Expr)) {
3004 Error ("Boolean expression expected");
3005 /* To avoid any compiler errors, make the expression a valid int */
3006 ED_MakeConstAbsInt (Expr, 1);
3012 void ConstAbsIntExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3013 /* Will evaluate an expression via the given function. If the result is not
3014 * a constant numeric integer value, a diagnostic will be printed, and the
3015 * value is replaced by a constant one to make sure there are no internal
3016 * errors that result from this input error.
3019 ExprWithCheck (Func, Expr);
3020 if (!ED_IsConstAbsInt (Expr)) {
3021 Error ("Constant integer expression expected");
3022 /* To avoid any compiler errors, make the expression a valid const */
3023 ED_MakeConstAbsInt (Expr, 1);