3 * Ullrich von Bassewitz, 21.06.1998
13 #include "debugflag.h"
20 #include "assignment.h"
32 #include "shiftexpr.h"
43 /*****************************************************************************/
45 /*****************************************************************************/
49 /* Generator attributes */
50 #define GEN_NOPUSH 0x01 /* Don't push lhs */
52 /* Map a generator function and its attributes to a token */
54 token_t Tok; /* Token to map to */
55 unsigned Flags; /* Flags for generator function */
56 void (*Func) (unsigned, unsigned long); /* Generator func */
59 /* Descriptors for the operations */
60 static GenDesc GenPASGN = { TOK_PLUS_ASSIGN, GEN_NOPUSH, g_add };
61 static GenDesc GenSASGN = { TOK_MINUS_ASSIGN, GEN_NOPUSH, g_sub };
62 static GenDesc GenMASGN = { TOK_MUL_ASSIGN, GEN_NOPUSH, g_mul };
63 static GenDesc GenDASGN = { TOK_DIV_ASSIGN, GEN_NOPUSH, g_div };
64 static GenDesc GenMOASGN = { TOK_MOD_ASSIGN, GEN_NOPUSH, g_mod };
65 static GenDesc GenSLASGN = { TOK_SHL_ASSIGN, GEN_NOPUSH, g_asl };
66 static GenDesc GenSRASGN = { TOK_SHR_ASSIGN, GEN_NOPUSH, g_asr };
67 static GenDesc GenAASGN = { TOK_AND_ASSIGN, GEN_NOPUSH, g_and };
68 static GenDesc GenXOASGN = { TOK_XOR_ASSIGN, GEN_NOPUSH, g_xor };
69 static GenDesc GenOASGN = { TOK_OR_ASSIGN, GEN_NOPUSH, g_or };
73 /*****************************************************************************/
74 /* Helper functions */
75 /*****************************************************************************/
79 static unsigned GlobalModeFlags (const ExprDesc* Expr)
80 /* Return the addressing mode flags for the given expression */
82 switch (ED_GetLoc (Expr)) {
83 case E_LOC_ABS: return CF_ABSOLUTE;
84 case E_LOC_GLOBAL: return CF_EXTERNAL;
85 case E_LOC_LITERAL: /* Same as static */
86 case E_LOC_STATIC: return CF_STATIC;
87 case E_LOC_REGISTER: return CF_REGVAR;
89 Internal ("GlobalModeFlags: Invalid location flags value: 0x%04X", Expr->Flags);
97 void ExprWithCheck (void (*Func) (ExprDesc*), ExprDesc *Expr)
98 /* Call an expression function with checks. */
100 /* Remember the stack pointer */
101 int OldSP = StackPtr;
103 /* Call the expression function */
106 /* Do some checks if code generation is still constistent */
107 if (StackPtr != OldSP) {
110 "Code generation messed up!\n"
111 "StackPtr is %d, should be %d",
114 Internal ("StackPtr is %d, should be %d\n", StackPtr, OldSP);
121 static Type* promoteint (Type* lhst, Type* rhst)
122 /* In an expression with two ints, return the type of the result */
124 /* Rules for integer types:
125 * - If one of the values is a long, the result is long.
126 * - If one of the values is unsigned, the result is also unsigned.
127 * - Otherwise the result is an int.
129 if (IsTypeLong (lhst) || IsTypeLong (rhst)) {
130 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
136 if (IsSignUnsigned (lhst) || IsSignUnsigned (rhst)) {
146 static unsigned typeadjust (ExprDesc* lhs, ExprDesc* rhs, int NoPush)
147 /* Adjust the two values for a binary operation. lhs is expected on stack or
148 * to be constant, rhs is expected to be in the primary register or constant.
149 * The function will put the type of the result into lhs and return the
150 * code generator flags for the operation.
151 * If NoPush is given, it is assumed that the operation does not expect the lhs
152 * to be on stack, and that lhs is in a register instead.
153 * Beware: The function does only accept int types.
156 unsigned ltype, rtype;
159 /* Get the type strings */
160 Type* lhst = lhs->Type;
161 Type* rhst = rhs->Type;
163 /* Generate type adjustment code if needed */
164 ltype = TypeOf (lhst);
165 if (ED_IsLocAbs (lhs)) {
169 /* Value is in primary register*/
172 rtype = TypeOf (rhst);
173 if (ED_IsLocAbs (rhs)) {
176 flags = g_typeadjust (ltype, rtype);
178 /* Set the type of the result */
179 lhs->Type = promoteint (lhst, rhst);
181 /* Return the code generator flags */
187 static const GenDesc* FindGen (token_t Tok, const GenDesc* Table)
188 /* Find a token in a generator table */
190 while (Table->Tok != TOK_INVALID) {
191 if (Table->Tok == Tok) {
201 static int TypeSpecAhead (void)
202 /* Return true if some sort of type is waiting (helper for cast and sizeof()
208 /* There's a type waiting if:
210 * We have an opening paren, and
211 * a. the next token is a type, or
212 * b. the next token is a type qualifier, or
213 * c. the next token is a typedef'd type
215 return CurTok.Tok == TOK_LPAREN && (
216 TokIsType (&NextTok) ||
217 TokIsTypeQual (&NextTok) ||
218 (NextTok.Tok == TOK_IDENT &&
219 (Entry = FindSym (NextTok.Ident)) != 0 &&
220 SymIsTypeDef (Entry)));
225 void PushAddr (const ExprDesc* Expr)
226 /* If the expression contains an address that was somehow evaluated,
227 * push this address on the stack. This is a helper function for all
228 * sorts of implicit or explicit assignment functions where the lvalue
229 * must be saved if it's not constant, before evaluating the rhs.
232 /* Get the address on stack if needed */
233 if (ED_IsLocExpr (Expr)) {
234 /* Push the address (always a pointer) */
241 /*****************************************************************************/
243 /*****************************************************************************/
247 static unsigned FunctionParamList (FuncDesc* Func)
248 /* Parse a function parameter list and pass the parameters to the called
249 * function. Depending on several criteria this may be done by just pushing
250 * each parameter separately, or creating the parameter frame once and then
251 * storing into this frame.
252 * The function returns the size of the parameters pushed.
257 /* Initialize variables */
258 SymEntry* Param = 0; /* Keep gcc silent */
259 unsigned ParamSize = 0; /* Size of parameters pushed */
260 unsigned ParamCount = 0; /* Number of parameters pushed */
261 unsigned FrameSize = 0; /* Size of parameter frame */
262 unsigned FrameParams = 0; /* Number of params in frame */
263 int FrameOffs = 0; /* Offset into parameter frame */
264 int Ellipsis = 0; /* Function is variadic */
266 /* As an optimization, we may allocate the complete parameter frame at
267 * once instead of pushing each parameter as it comes. We may do that,
270 * - optimizations that increase code size are enabled (allocating the
271 * stack frame at once gives usually larger code).
272 * - we have more than one parameter to push (don't count the last param
273 * for __fastcall__ functions).
275 * The FrameSize variable will contain a value > 0 if storing into a frame
276 * (instead of pushing) is enabled.
279 if (IS_Get (&CodeSizeFactor) >= 200) {
281 /* Calculate the number and size of the parameters */
282 FrameParams = Func->ParamCount;
283 FrameSize = Func->ParamSize;
284 if (FrameParams > 0 && (Func->Flags & FD_FASTCALL) != 0) {
285 /* Last parameter is not pushed */
286 FrameSize -= CheckedSizeOf (Func->LastParam->Type);
290 /* Do we have more than one parameter in the frame? */
291 if (FrameParams > 1) {
292 /* Okeydokey, setup the frame */
293 FrameOffs = StackPtr;
295 StackPtr -= FrameSize;
297 /* Don't use a preallocated frame */
302 /* Parse the actual parameter list */
303 while (CurTok.Tok != TOK_RPAREN) {
307 /* Count arguments */
310 /* Fetch the pointer to the next argument, check for too many args */
311 if (ParamCount <= Func->ParamCount) {
312 /* Beware: If there are parameters with identical names, they
313 * cannot go into the same symbol table, which means that in this
314 * case of errorneous input, the number of nodes in the symbol
315 * table and ParamCount are NOT equal. We have to handle this case
316 * below to avoid segmentation violations. Since we know that this
317 * problem can only occur if there is more than one parameter,
318 * we will just use the last one.
320 if (ParamCount == 1) {
322 Param = Func->SymTab->SymHead;
323 } else if (Param->NextSym != 0) {
325 Param = Param->NextSym;
326 CHECK ((Param->Flags & SC_PARAM) != 0);
328 } else if (!Ellipsis) {
329 /* Too many arguments. Do we have an open param list? */
330 if ((Func->Flags & FD_VARIADIC) == 0) {
331 /* End of param list reached, no ellipsis */
332 Error ("Too many arguments in function call");
334 /* Assume an ellipsis even in case of errors to avoid an error
335 * message for each other argument.
340 /* Evaluate the parameter expression */
343 /* If we don't have an argument spec, accept anything, otherwise
344 * convert the actual argument to the type needed.
349 /* Convert the argument to the parameter type if needed */
350 TypeConversion (&Expr, Param->Type);
352 /* If we have a prototype, chars may be pushed as chars */
353 Flags |= CF_FORCECHAR;
357 /* No prototype available. Convert array to "pointer to first
358 * element", and function to "pointer to function".
360 Expr.Type = PtrConversion (Expr.Type);
364 /* Load the value into the primary if it is not already there */
365 LoadExpr (Flags, &Expr);
367 /* Use the type of the argument for the push */
368 Flags |= TypeOf (Expr.Type);
370 /* If this is a fastcall function, don't push the last argument */
371 if (ParamCount != Func->ParamCount || (Func->Flags & FD_FASTCALL) == 0) {
372 unsigned ArgSize = sizeofarg (Flags);
374 /* We have the space already allocated, store in the frame.
375 * Because of invalid type conversions (that have produced an
376 * error before), we can end up here with a non aligned stack
377 * frame. Since no output will be generated anyway, handle
378 * these cases gracefully instead of doing a CHECK.
380 if (FrameSize >= ArgSize) {
381 FrameSize -= ArgSize;
385 FrameOffs -= ArgSize;
387 g_putlocal (Flags | CF_NOKEEP, FrameOffs, Expr.IVal);
389 /* Push the argument */
390 g_push (Flags, Expr.IVal);
393 /* Calculate total parameter size */
394 ParamSize += ArgSize;
397 /* Check for end of argument list */
398 if (CurTok.Tok != TOK_COMMA) {
404 /* Check if we had enough parameters */
405 if (ParamCount < Func->ParamCount) {
406 Error ("Too few arguments in function call");
409 /* The function returns the size of all parameters pushed onto the stack.
410 * However, if there are parameters missing (which is an error and was
411 * flagged by the compiler) AND a stack frame was preallocated above,
412 * we would loose track of the stackpointer and generate an internal error
413 * later. So we correct the value by the parameters that should have been
414 * pushed to avoid an internal compiler error. Since an error was
415 * generated before, no code will be output anyway.
417 return ParamSize + FrameSize;
422 static void FunctionCall (ExprDesc* Expr)
423 /* Perform a function call. */
425 FuncDesc* Func; /* Function descriptor */
426 int IsFuncPtr; /* Flag */
427 unsigned ParamSize; /* Number of parameter bytes */
429 int PtrOffs = 0; /* Offset of function pointer on stack */
430 int IsFastCall = 0; /* True if it's a fast call function */
431 int PtrOnStack = 0; /* True if a pointer copy is on stack */
433 /* Skip the left paren */
436 /* Get a pointer to the function descriptor from the type string */
437 Func = GetFuncDesc (Expr->Type);
439 /* Handle function pointers transparently */
440 IsFuncPtr = IsTypeFuncPtr (Expr->Type);
443 /* Check wether it's a fastcall function that has parameters */
444 IsFastCall = IsFastCallFunc (Expr->Type + 1) && (Func->ParamCount > 0);
446 /* Things may be difficult, depending on where the function pointer
447 * resides. If the function pointer is an expression of some sort
448 * (not a local or global variable), we have to evaluate this
449 * expression now and save the result for later. Since calls to
450 * function pointers may be nested, we must save it onto the stack.
451 * For fastcall functions we do also need to place a copy of the
452 * pointer on stack, since we cannot use a/x.
454 PtrOnStack = IsFastCall || !ED_IsConst (Expr);
457 /* Not a global or local variable, or a fastcall function. Load
458 * the pointer into the primary and mark it as an expression.
460 LoadExpr (CF_NONE, Expr);
461 ED_MakeRValExpr (Expr);
463 /* Remember the code position */
466 /* Push the pointer onto the stack and remember the offset */
471 /* Check for known standard functions and inline them */
472 } else if (Expr->Name != 0) {
473 int StdFunc = FindStdFunc ((const char*) Expr->Name);
475 /* Inline this function */
476 HandleStdFunc (StdFunc, Func, Expr);
481 /* Parse the parameter list */
482 ParamSize = FunctionParamList (Func);
484 /* We need the closing paren here */
487 /* Special handling for function pointers */
490 /* If the function is not a fastcall function, load the pointer to
491 * the function into the primary.
495 /* Not a fastcall function - we may use the primary */
497 /* If we have no parameters, the pointer is still in the
498 * primary. Remove the code to push it and correct the
501 if (ParamSize == 0) {
505 /* Load from the saved copy */
506 g_getlocal (CF_PTR, PtrOffs);
509 /* Load from original location */
510 LoadExpr (CF_NONE, Expr);
513 /* Call the function */
514 g_callind (TypeOf (Expr->Type+1), ParamSize, PtrOffs);
518 /* Fastcall function. We cannot use the primary for the function
519 * pointer and must therefore use an offset to the stack location.
520 * Since fastcall functions may never be variadic, we can use the
521 * index register for this purpose.
523 g_callind (CF_LOCAL, ParamSize, PtrOffs);
526 /* If we have a pointer on stack, remove it */
528 g_space (- (int) sizeofarg (CF_PTR));
537 /* Normal function */
538 g_call (TypeOf (Expr->Type), (const char*) Expr->Name, ParamSize);
542 /* The function result is an rvalue in the primary register */
543 ED_MakeRValExpr (Expr);
544 Expr->Type = GetFuncReturn (Expr->Type);
549 static void Primary (ExprDesc* E)
550 /* This is the lowest level of the expression parser. */
554 /* Initialize fields in the expression stucture */
557 /* Character and integer constants. */
558 if (CurTok.Tok == TOK_ICONST || CurTok.Tok == TOK_CCONST) {
559 E->IVal = CurTok.IVal;
560 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
561 E->Type = CurTok.Type;
566 /* Floating point constant */
567 if (CurTok.Tok == TOK_FCONST) {
568 E->FVal = CurTok.FVal;
569 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
570 E->Type = CurTok.Type;
575 /* Process parenthesized subexpression by calling the whole parser
578 if (CurTok.Tok == TOK_LPAREN) {
585 /* If we run into an identifier in preprocessing mode, we assume that this
586 * is an undefined macro and replace it by a constant value of zero.
588 if (Preprocessing && CurTok.Tok == TOK_IDENT) {
590 ED_MakeConstAbsInt (E, 0);
594 /* All others may only be used if the expression evaluation is not called
595 * recursively by the preprocessor.
598 /* Illegal expression in PP mode */
599 Error ("Preprocessor expression expected");
600 ED_MakeConstAbsInt (E, 1);
604 switch (CurTok.Tok) {
607 /* Identifier. Get a pointer to the symbol table entry */
608 Sym = E->Sym = FindSym (CurTok.Ident);
610 /* Is the symbol known? */
613 /* We found the symbol - skip the name token */
616 /* Check for illegal symbol types */
617 CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
618 if (Sym->Flags & SC_TYPE) {
619 /* Cannot use type symbols */
620 Error ("Variable identifier expected");
621 /* Assume an int type to make E valid */
622 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
627 /* Mark the symbol as referenced */
628 Sym->Flags |= SC_REF;
630 /* The expression type is the symbol type */
633 /* Check for legal symbol types */
634 if ((Sym->Flags & SC_CONST) == SC_CONST) {
635 /* Enum or some other numeric constant */
636 E->Flags = E_LOC_ABS | E_RTYPE_RVAL;
637 E->IVal = Sym->V.ConstVal;
638 } else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
640 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
641 E->Name = (unsigned long) Sym->Name;
642 } else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
643 /* Local variable. If this is a parameter for a variadic
644 * function, we have to add some address calculations, and the
645 * address is not const.
647 if ((Sym->Flags & SC_PARAM) == SC_PARAM && F_IsVariadic (CurrentFunc)) {
648 /* Variadic parameter */
649 g_leavariadic (Sym->V.Offs - F_GetParamSize (CurrentFunc));
650 E->Flags = E_LOC_EXPR | E_RTYPE_LVAL;
652 /* Normal parameter */
653 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
654 E->IVal = Sym->V.Offs;
656 } else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
657 /* Register variable, zero page based */
658 E->Flags = E_LOC_REGISTER | E_RTYPE_LVAL;
659 E->Name = Sym->V.R.RegOffs;
660 } else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
661 /* Static variable */
662 if (Sym->Flags & (SC_EXTERN | SC_STORAGE)) {
663 E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
664 E->Name = (unsigned long) Sym->Name;
666 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
667 E->Name = Sym->V.Label;
670 /* Local static variable */
671 E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
672 E->Name = Sym->V.Offs;
675 /* We've made all variables lvalues above. However, this is
676 * not always correct: An array is actually the address of its
677 * first element, which is a rvalue, and a function is a
678 * rvalue, too, because we cannot store anything in a function.
679 * So fix the flags depending on the type.
681 if (IsTypeArray (E->Type) || IsTypeFunc (E->Type)) {
687 /* We did not find the symbol. Remember the name, then skip it */
689 strcpy (Ident, CurTok.Ident);
692 /* IDENT is either an auto-declared function or an undefined variable. */
693 if (CurTok.Tok == TOK_LPAREN) {
694 /* C99 doesn't allow calls to undefined functions, so
695 * generate an error and otherwise a warning. Declare a
696 * function returning int. For that purpose, prepare a
697 * function signature for a function having an empty param
698 * list and returning int.
700 if (IS_Get (&Standard) >= STD_C99) {
701 Error ("Call to undefined function `%s'", Ident);
703 Warning ("Call to undefined function `%s'", Ident);
705 Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
707 E->Flags = E_LOC_GLOBAL | E_RTYPE_RVAL;
708 E->Name = (unsigned long) Sym->Name;
710 /* Undeclared Variable */
711 Sym = AddLocalSym (Ident, type_int, SC_AUTO | SC_REF, 0);
712 E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
714 Error ("Undefined symbol: `%s'", Ident);
722 E->Type = GetCharArrayType (GetLiteralPoolOffs () - CurTok.IVal);
723 E->Flags = E_LOC_LITERAL | E_RTYPE_RVAL;
724 E->IVal = CurTok.IVal;
725 E->Name = LiteralPoolLabel;
732 E->Flags = E_LOC_EXPR | E_RTYPE_RVAL;
737 /* Register pseudo variable */
738 E->Type = type_uchar;
739 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
744 /* Register pseudo variable */
746 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
751 /* Register pseudo variable */
752 E->Type = type_ulong;
753 E->Flags = E_LOC_PRIMARY | E_RTYPE_LVAL;
758 /* Illegal primary. */
759 Error ("Expression expected");
760 ED_MakeConstAbsInt (E, 1);
767 static void ArrayRef (ExprDesc* Expr)
768 /* Handle an array reference. This function needs a rewrite. */
778 /* Skip the bracket */
781 /* Get the type of left side */
784 /* We can apply a special treatment for arrays that have a const base
785 * address. This is true for most arrays and will produce a lot better
786 * code. Check if this is a const base address.
788 ConstBaseAddr = ED_IsRVal (Expr) &&
789 (ED_IsLocConst (Expr) || ED_IsLocStack (Expr));
791 /* If we have a constant base, we delay the address fetch */
793 if (!ConstBaseAddr) {
794 /* Get a pointer to the array into the primary */
795 LoadExpr (CF_NONE, Expr);
797 /* Get the array pointer on stack. Do not push more than 16
798 * bit, even if this value is greater, since we cannot handle
799 * other than 16bit stuff when doing indexing.
805 /* TOS now contains ptr to array elements. Get the subscript. */
806 ExprWithCheck (hie0, &SubScript);
808 /* Check the types of array and subscript. We can either have a
809 * pointer/array to the left, in which case the subscript must be of an
810 * integer type, or we have an integer to the left, in which case the
811 * subscript must be a pointer/array.
812 * Since we do the necessary checking here, we can rely later on the
815 if (IsClassPtr (Expr->Type)) {
816 if (!IsClassInt (SubScript.Type)) {
817 Error ("Array subscript is not an integer");
818 /* To avoid any compiler errors, make the expression a valid int */
819 ED_MakeConstAbsInt (&SubScript, 0);
821 ElementType = Indirect (Expr->Type);
822 } else if (IsClassInt (Expr->Type)) {
823 if (!IsClassPtr (SubScript.Type)) {
824 Error ("Subscripted value is neither array nor pointer");
825 /* To avoid compiler errors, make the subscript a char[] at
828 ED_MakeConstAbs (&SubScript, 0, GetCharArrayType (1));
830 ElementType = Indirect (SubScript.Type);
832 Error ("Cannot subscript");
833 /* To avoid compiler errors, fake both the array and the subscript, so
834 * we can just proceed.
836 ED_MakeConstAbs (Expr, 0, GetCharArrayType (1));
837 ED_MakeConstAbsInt (&SubScript, 0);
838 ElementType = Indirect (Expr->Type);
841 /* Check if the subscript is constant absolute value */
842 if (ED_IsConstAbs (&SubScript)) {
844 /* The array subscript is a numeric constant. If we had pushed the
845 * array base address onto the stack before, we can remove this value,
846 * since we can generate expression+offset.
848 if (!ConstBaseAddr) {
851 /* Get an array pointer into the primary */
852 LoadExpr (CF_NONE, Expr);
855 if (IsClassPtr (Expr->Type)) {
857 /* Lhs is pointer/array. Scale the subscript value according to
860 SubScript.IVal *= CheckedSizeOf (ElementType);
862 /* Remove the address load code */
865 /* In case of an array, we can adjust the offset of the expression
866 * already in Expr. If the base address was a constant, we can even
867 * remove the code that loaded the address into the primary.
869 if (IsTypeArray (Expr->Type)) {
871 /* Adjust the offset */
872 Expr->IVal += SubScript.IVal;
876 /* It's a pointer, so we do have to load it into the primary
877 * first (if it's not already there).
879 if (ConstBaseAddr || ED_IsLVal (Expr)) {
880 LoadExpr (CF_NONE, Expr);
881 ED_MakeRValExpr (Expr);
885 Expr->IVal = SubScript.IVal;
890 /* Scale the rhs value according to the element type */
891 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
893 /* Add the subscript. Since arrays are indexed by integers,
894 * we will ignore the true type of the subscript here and
895 * use always an int. #### Use offset but beware of LoadExpr!
897 g_inc (CF_INT | CF_CONST, SubScript.IVal);
903 /* Array subscript is not constant. Load it into the primary */
905 LoadExpr (CF_NONE, &SubScript);
908 if (IsClassPtr (Expr->Type)) {
910 /* Indexing is based on unsigneds, so we will just use the integer
911 * portion of the index (which is in (e)ax, so there's no further
914 g_scale (CF_INT, CheckedSizeOf (ElementType));
918 /* Get the int value on top. If we come here, we're sure, both
919 * values are 16 bit (the first one was truncated if necessary
920 * and the second one is a pointer). Note: If ConstBaseAddr is
921 * true, we don't have a value on stack, so to "swap" both, just
922 * push the subscript.
926 LoadExpr (CF_NONE, Expr);
933 g_scale (TypeOf (tptr1), CheckedSizeOf (ElementType));
937 /* The offset is now in the primary register. It we didn't have a
938 * constant base address for the lhs, the lhs address is already
939 * on stack, and we must add the offset. If the base address was
940 * constant, we call special functions to add the address to the
943 if (!ConstBaseAddr) {
945 /* The array base address is on stack and the subscript is in the
952 /* The subscript is in the primary, and the array base address is
953 * in Expr. If the subscript has itself a constant address, it is
954 * often a better idea to reverse again the order of the
955 * evaluation. This will generate better code if the subscript is
956 * a byte sized variable. But beware: This is only possible if the
957 * subscript was not scaled, that is, if this was a byte array
960 if ((ED_IsLocConst (&SubScript) || ED_IsLocStack (&SubScript)) &&
961 CheckedSizeOf (ElementType) == SIZEOF_CHAR) {
965 /* Reverse the order of evaluation */
966 if (CheckedSizeOf (SubScript.Type) == SIZEOF_CHAR) {
973 /* Get a pointer to the array into the primary. */
974 LoadExpr (CF_NONE, Expr);
976 /* Add the variable */
977 if (ED_IsLocStack (&SubScript)) {
978 g_addlocal (Flags, SubScript.IVal);
980 Flags |= GlobalModeFlags (&SubScript);
981 g_addstatic (Flags, SubScript.Name, SubScript.IVal);
985 if (ED_IsLocAbs (Expr)) {
986 /* Constant numeric address. Just add it */
987 g_inc (CF_INT, Expr->IVal);
988 } else if (ED_IsLocStack (Expr)) {
989 /* Base address is a local variable address */
990 if (IsTypeArray (Expr->Type)) {
991 g_addaddr_local (CF_INT, Expr->IVal);
993 g_addlocal (CF_PTR, Expr->IVal);
996 /* Base address is a static variable address */
997 unsigned Flags = CF_INT | GlobalModeFlags (Expr);
998 if (ED_IsRVal (Expr)) {
999 /* Add the address of the location */
1000 g_addaddr_static (Flags, Expr->Name, Expr->IVal);
1002 /* Add the contents of the location */
1003 g_addstatic (Flags, Expr->Name, Expr->IVal);
1011 /* The result is an expression in the primary */
1012 ED_MakeRValExpr (Expr);
1016 /* Result is of element type */
1017 Expr->Type = ElementType;
1019 /* An array element is actually a variable. So the rules for variables
1020 * with respect to the reference type apply: If it's an array, it is
1021 * a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1022 * but an array cannot contain functions).
1024 if (IsTypeArray (Expr->Type)) {
1030 /* Consume the closing bracket */
1036 static void StructRef (ExprDesc* Expr)
1037 /* Process struct field after . or ->. */
1042 /* Skip the token and check for an identifier */
1044 if (CurTok.Tok != TOK_IDENT) {
1045 Error ("Identifier expected");
1046 Expr->Type = type_int;
1050 /* Get the symbol table entry and check for a struct field */
1051 strcpy (Ident, CurTok.Ident);
1053 Field = FindStructField (Expr->Type, Ident);
1055 Error ("Struct/union has no field named `%s'", Ident);
1056 Expr->Type = type_int;
1060 /* If we have a struct pointer that is an lvalue and not already in the
1061 * primary, load it now.
1063 if (ED_IsLVal (Expr) && IsTypePtr (Expr->Type)) {
1065 /* Load into the primary */
1066 LoadExpr (CF_NONE, Expr);
1068 /* Make it an lvalue expression */
1069 ED_MakeLValExpr (Expr);
1072 /* Set the struct field offset */
1073 Expr->IVal += Field->V.Offs;
1075 /* The type is now the type of the field */
1076 Expr->Type = Field->Type;
1078 /* An struct member is actually a variable. So the rules for variables
1079 * with respect to the reference type apply: If it's an array, it is
1080 * a rvalue, otherwise it's an lvalue. (A function would also be a rvalue,
1081 * but a struct field cannot be a function).
1083 if (IsTypeArray (Expr->Type)) {
1092 static void hie11 (ExprDesc *Expr)
1093 /* Handle compound types (structs and arrays) */
1095 /* Name value used in invalid function calls */
1096 static const char IllegalFunc[] = "illegal_function_call";
1098 /* Evaluate the lhs */
1101 /* Check for a rhs */
1102 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN ||
1103 CurTok.Tok == TOK_DOT || CurTok.Tok == TOK_PTR_REF) {
1105 switch (CurTok.Tok) {
1108 /* Array reference */
1113 /* Function call. */
1114 if (!IsTypeFunc (Expr->Type) && !IsTypeFuncPtr (Expr->Type)) {
1115 /* Not a function */
1116 Error ("Illegal function call");
1117 /* Force the type to be a implicitly defined function, one
1118 * returning an int and taking any number of arguments.
1119 * Since we don't have a name, invent one.
1121 ED_MakeConstAbs (Expr, 0, GetImplicitFuncType ());
1122 Expr->Name = (long) IllegalFunc;
1124 /* Call the function */
1125 FunctionCall (Expr);
1129 if (!IsClassStruct (Expr->Type)) {
1130 Error ("Struct expected");
1136 /* If we have an array, convert it to pointer to first element */
1137 if (IsTypeArray (Expr->Type)) {
1138 Expr->Type = ArrayToPtr (Expr->Type);
1140 if (!IsClassPtr (Expr->Type) || !IsClassStruct (Indirect (Expr->Type))) {
1141 Error ("Struct pointer expected");
1147 Internal ("Invalid token in hie11: %d", CurTok.Tok);
1155 void Store (ExprDesc* Expr, const Type* StoreType)
1156 /* Store the primary register into the location denoted by Expr. If StoreType
1157 * is given, use this type when storing instead of Expr->Type. If StoreType
1158 * is NULL, use Expr->Type instead.
1163 /* If StoreType was not given, use Expr->Type instead */
1164 if (StoreType == 0) {
1165 StoreType = Expr->Type;
1168 /* Prepare the code generator flags */
1169 Flags = TypeOf (StoreType);
1171 /* Do the store depending on the location */
1172 switch (ED_GetLoc (Expr)) {
1175 /* Absolute: numeric address or const */
1176 g_putstatic (Flags | CF_ABSOLUTE, Expr->IVal, 0);
1180 /* Global variable */
1181 g_putstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->IVal);
1186 /* Static variable or literal in the literal pool */
1187 g_putstatic (Flags | CF_STATIC, Expr->Name, Expr->IVal);
1190 case E_LOC_REGISTER:
1191 /* Register variable */
1192 g_putstatic (Flags | CF_REGVAR, Expr->Name, Expr->IVal);
1196 /* Value on the stack */
1197 g_putlocal (Flags, Expr->IVal, 0);
1201 /* The primary register (value is already there) */
1202 /* ### Do we need a test here if the flag is set? */
1206 /* An expression in the primary register */
1207 g_putind (Flags, Expr->IVal);
1211 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
1214 /* Assume that each one of the stores will invalidate CC */
1215 ED_MarkAsUntested (Expr);
1220 static void PreInc (ExprDesc* Expr)
1221 /* Handle the preincrement operators */
1226 /* Skip the operator token */
1229 /* Evaluate the expression and check that it is an lvalue */
1231 if (!ED_IsLVal (Expr)) {
1232 Error ("Invalid lvalue");
1236 /* We cannot modify const values */
1237 if (IsQualConst (Expr->Type)) {
1238 Error ("Increment of read-only variable");
1241 /* Get the data type */
1242 Flags = TypeOf (Expr->Type) | CF_FORCECHAR | CF_CONST;
1244 /* Get the increment value in bytes */
1245 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1247 /* Check the location of the data */
1248 switch (ED_GetLoc (Expr)) {
1251 /* Absolute: numeric address or const */
1252 g_addeqstatic (Flags | CF_ABSOLUTE, Expr->IVal, 0, Val);
1256 /* Global variable */
1257 g_addeqstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->IVal, Val);
1262 /* Static variable or literal in the literal pool */
1263 g_addeqstatic (Flags | CF_STATIC, Expr->Name, Expr->IVal, Val);
1266 case E_LOC_REGISTER:
1267 /* Register variable */
1268 g_addeqstatic (Flags | CF_REGVAR, Expr->Name, Expr->IVal, Val);
1272 /* Value on the stack */
1273 g_addeqlocal (Flags, Expr->IVal, Val);
1277 /* The primary register */
1282 /* An expression in the primary register */
1283 g_addeqind (Flags, Expr->IVal, Val);
1287 Internal ("Invalid location in PreInc(): 0x%04X", ED_GetLoc (Expr));
1290 /* Result is an expression, no reference */
1291 ED_MakeRValExpr (Expr);
1296 static void PreDec (ExprDesc* Expr)
1297 /* Handle the predecrement operators */
1302 /* Skip the operator token */
1305 /* Evaluate the expression and check that it is an lvalue */
1307 if (!ED_IsLVal (Expr)) {
1308 Error ("Invalid lvalue");
1312 /* We cannot modify const values */
1313 if (IsQualConst (Expr->Type)) {
1314 Error ("Decrement of read-only variable");
1317 /* Get the data type */
1318 Flags = TypeOf (Expr->Type) | CF_FORCECHAR | CF_CONST;
1320 /* Get the increment value in bytes */
1321 Val = IsTypePtr (Expr->Type)? CheckedPSizeOf (Expr->Type) : 1;
1323 /* Check the location of the data */
1324 switch (ED_GetLoc (Expr)) {
1327 /* Absolute: numeric address or const */
1328 g_subeqstatic (Flags | CF_ABSOLUTE, Expr->IVal, 0, Val);
1332 /* Global variable */
1333 g_subeqstatic (Flags | CF_EXTERNAL, Expr->Name, Expr->IVal, Val);
1338 /* Static variable or literal in the literal pool */
1339 g_subeqstatic (Flags | CF_STATIC, Expr->Name, Expr->IVal, Val);
1342 case E_LOC_REGISTER:
1343 /* Register variable */
1344 g_subeqstatic (Flags | CF_REGVAR, Expr->Name, Expr->IVal, Val);
1348 /* Value on the stack */
1349 g_subeqlocal (Flags, Expr->IVal, Val);
1353 /* The primary register */
1358 /* An expression in the primary register */
1359 g_subeqind (Flags, Expr->IVal, Val);
1363 Internal ("Invalid location in PreDec(): 0x%04X", ED_GetLoc (Expr));
1366 /* Result is an expression, no reference */
1367 ED_MakeRValExpr (Expr);
1372 static void PostInc (ExprDesc* Expr)
1373 /* Handle the postincrement operator */
1379 /* The expression to increment must be an lvalue */
1380 if (!ED_IsLVal (Expr)) {
1381 Error ("Invalid lvalue");
1385 /* We cannot modify const values */
1386 if (IsQualConst (Expr->Type)) {
1387 Error ("Increment of read-only variable");
1390 /* Get the data type */
1391 Flags = TypeOf (Expr->Type);
1393 /* Push the address if needed */
1396 /* Fetch the value and save it (since it's the result of the expression) */
1397 LoadExpr (CF_NONE, Expr);
1398 g_save (Flags | CF_FORCECHAR);
1400 /* If we have a pointer expression, increment by the size of the type */
1401 if (IsTypePtr (Expr->Type)) {
1402 g_inc (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1404 g_inc (Flags | CF_CONST | CF_FORCECHAR, 1);
1407 /* Store the result back */
1410 /* Restore the original value in the primary register */
1411 g_restore (Flags | CF_FORCECHAR);
1413 /* The result is always an expression, no reference */
1414 ED_MakeRValExpr (Expr);
1419 static void PostDec (ExprDesc* Expr)
1420 /* Handle the postdecrement operator */
1426 /* The expression to increment must be an lvalue */
1427 if (!ED_IsLVal (Expr)) {
1428 Error ("Invalid lvalue");
1432 /* We cannot modify const values */
1433 if (IsQualConst (Expr->Type)) {
1434 Error ("Decrement of read-only variable");
1437 /* Get the data type */
1438 Flags = TypeOf (Expr->Type);
1440 /* Push the address if needed */
1443 /* Fetch the value and save it (since it's the result of the expression) */
1444 LoadExpr (CF_NONE, Expr);
1445 g_save (Flags | CF_FORCECHAR);
1447 /* If we have a pointer expression, increment by the size of the type */
1448 if (IsTypePtr (Expr->Type)) {
1449 g_dec (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1));
1451 g_dec (Flags | CF_CONST | CF_FORCECHAR, 1);
1454 /* Store the result back */
1457 /* Restore the original value in the primary register */
1458 g_restore (Flags | CF_FORCECHAR);
1460 /* The result is always an expression, no reference */
1461 ED_MakeRValExpr (Expr);
1466 static void UnaryOp (ExprDesc* Expr)
1467 /* Handle unary -/+ and ~ */
1471 /* Remember the operator token and skip it */
1472 token_t Tok = CurTok.Tok;
1475 /* Get the expression */
1478 /* We can only handle integer types */
1479 if (!IsClassInt (Expr->Type)) {
1480 Error ("Argument must have integer type");
1481 ED_MakeConstAbsInt (Expr, 1);
1484 /* Check for a constant expression */
1485 if (ED_IsConstAbs (Expr)) {
1486 /* Value is constant */
1488 case TOK_MINUS: Expr->IVal = -Expr->IVal; break;
1489 case TOK_PLUS: break;
1490 case TOK_COMP: Expr->IVal = ~Expr->IVal; break;
1491 default: Internal ("Unexpected token: %d", Tok);
1494 /* Value is not constant */
1495 LoadExpr (CF_NONE, Expr);
1497 /* Get the type of the expression */
1498 Flags = TypeOf (Expr->Type);
1500 /* Handle the operation */
1502 case TOK_MINUS: g_neg (Flags); break;
1503 case TOK_PLUS: break;
1504 case TOK_COMP: g_com (Flags); break;
1505 default: Internal ("Unexpected token: %d", Tok);
1508 /* The result is a rvalue in the primary */
1509 ED_MakeRValExpr (Expr);
1515 void hie10 (ExprDesc* Expr)
1516 /* Handle ++, --, !, unary - etc. */
1520 switch (CurTok.Tok) {
1538 if (evalexpr (CF_NONE, hie10, Expr) == 0) {
1539 /* Constant expression */
1540 Expr->IVal = !Expr->IVal;
1542 g_bneg (TypeOf (Expr->Type));
1543 ED_MakeRValExpr (Expr);
1544 ED_TestDone (Expr); /* bneg will set cc */
1550 ExprWithCheck (hie10, Expr);
1551 if (ED_IsLVal (Expr) || !(ED_IsLocConst (Expr) || ED_IsLocStack (Expr))) {
1552 /* Not a const, load it into the primary and make it a
1555 LoadExpr (CF_NONE, Expr);
1556 ED_MakeRValExpr (Expr);
1558 /* If the expression is already a pointer to function, the
1559 * additional dereferencing operator must be ignored.
1561 if (IsTypeFuncPtr (Expr->Type)) {
1562 /* Expression not storable */
1565 if (IsClassPtr (Expr->Type)) {
1566 Expr->Type = Indirect (Expr->Type);
1568 Error ("Illegal indirection");
1570 /* The * operator yields an lvalue */
1577 ExprWithCheck (hie10, Expr);
1578 /* The & operator may be applied to any lvalue, and it may be
1579 * applied to functions, even if they're no lvalues.
1581 if (ED_IsRVal (Expr) && !IsTypeFunc (Expr->Type) && !IsTypeArray (Expr->Type)) {
1582 Error ("Illegal address");
1584 Expr->Type = PointerTo (Expr->Type);
1585 /* The & operator yields an rvalue */
1592 if (TypeSpecAhead ()) {
1595 Size = CheckedSizeOf (ParseType (T));
1598 /* Remember the output queue pointer */
1602 Size = CheckedSizeOf (Expr->Type);
1603 /* Remove any generated code */
1606 ED_MakeConstAbs (Expr, Size, type_size_t);
1607 ED_MarkAsUntested (Expr);
1611 if (TypeSpecAhead ()) {
1621 /* Handle post increment */
1622 switch (CurTok.Tok) {
1623 case TOK_INC: PostInc (Expr); break;
1624 case TOK_DEC: PostDec (Expr); break;
1635 static void hie_internal (const GenDesc* Ops, /* List of generators */
1637 void (*hienext) (ExprDesc*),
1639 /* Helper function */
1645 token_t Tok; /* The operator token */
1646 unsigned ltype, type;
1647 int rconst; /* Operand is a constant */
1653 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1655 /* Tell the caller that we handled it's ops */
1658 /* All operators that call this function expect an int on the lhs */
1659 if (!IsClassInt (Expr->Type)) {
1660 Error ("Integer expression expected");
1661 /* To avoid further errors, make Expr a valid int expression */
1662 ED_MakeConstAbsInt (Expr, 1);
1665 /* Remember the operator token, then skip it */
1669 /* Get the lhs on stack */
1670 GetCodePos (&Mark1);
1671 ltype = TypeOf (Expr->Type);
1672 if (ED_IsConstAbs (Expr)) {
1673 /* Constant value */
1674 GetCodePos (&Mark2);
1675 g_push (ltype | CF_CONST, Expr->IVal);
1677 /* Value not constant */
1678 LoadExpr (CF_NONE, Expr);
1679 GetCodePos (&Mark2);
1683 /* Get the right hand side */
1684 rconst = (evalexpr (CF_NONE, hienext, &Expr2) == 0);
1686 /* Check the type of the rhs */
1687 if (!IsClassInt (Expr2.Type)) {
1688 Error ("Integer expression expected");
1691 /* Check for const operands */
1692 if (ED_IsConstAbs (Expr) && rconst) {
1694 /* Both operands are constant, remove the generated code */
1695 RemoveCode (&Mark1);
1697 /* Get the type of the result */
1698 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1700 /* Handle the op differently for signed and unsigned types */
1701 if (IsSignSigned (Expr->Type)) {
1703 /* Evaluate the result for signed operands */
1704 signed long Val1 = Expr->IVal;
1705 signed long Val2 = Expr2.IVal;
1708 Expr->IVal = (Val1 | Val2);
1711 Expr->IVal = (Val1 ^ Val2);
1714 Expr->IVal = (Val1 & Val2);
1717 Expr->IVal = (Val1 * Val2);
1721 Error ("Division by zero");
1722 Expr->IVal = 0x7FFFFFFF;
1724 Expr->IVal = (Val1 / Val2);
1729 Error ("Modulo operation with zero");
1732 Expr->IVal = (Val1 % Val2);
1736 Internal ("hie_internal: got token 0x%X\n", Tok);
1740 /* Evaluate the result for unsigned operands */
1741 unsigned long Val1 = Expr->IVal;
1742 unsigned long Val2 = Expr2.IVal;
1745 Expr->IVal = (Val1 | Val2);
1748 Expr->IVal = (Val1 ^ Val2);
1751 Expr->IVal = (Val1 & Val2);
1754 Expr->IVal = (Val1 * Val2);
1758 Error ("Division by zero");
1759 Expr->IVal = 0xFFFFFFFF;
1761 Expr->IVal = (Val1 / Val2);
1766 Error ("Modulo operation with zero");
1769 Expr->IVal = (Val1 % Val2);
1773 Internal ("hie_internal: got token 0x%X\n", Tok);
1779 /* If the right hand side is constant, and the generator function
1780 * expects the lhs in the primary, remove the push of the primary
1783 unsigned rtype = TypeOf (Expr2.Type);
1786 /* Second value is constant - check for div */
1789 if (Tok == TOK_DIV && Expr2.IVal == 0) {
1790 Error ("Division by zero");
1791 } else if (Tok == TOK_MOD && Expr2.IVal == 0) {
1792 Error ("Modulo operation with zero");
1794 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1795 RemoveCode (&Mark2);
1796 ltype |= CF_REG; /* Value is in register */
1800 /* Determine the type of the operation result. */
1801 type |= g_typeadjust (ltype, rtype);
1802 Expr->Type = promoteint (Expr->Type, Expr2.Type);
1805 Gen->Func (type, Expr2.IVal);
1807 /* We have a rvalue in the primary now */
1808 ED_MakeRValExpr (Expr);
1815 static void hie_compare (const GenDesc* Ops, /* List of generators */
1817 void (*hienext) (ExprDesc*))
1818 /* Helper function for the compare operators */
1824 token_t Tok; /* The operator token */
1826 int rconst; /* Operand is a constant */
1831 while ((Gen = FindGen (CurTok.Tok, Ops)) != 0) {
1833 /* Remember the operator token, then skip it */
1837 /* Get the lhs on stack */
1838 GetCodePos (&Mark1);
1839 ltype = TypeOf (Expr->Type);
1840 if (ED_IsConstAbs (Expr)) {
1841 /* Constant value */
1842 GetCodePos (&Mark2);
1843 g_push (ltype | CF_CONST, Expr->IVal);
1845 /* Value not constant */
1846 LoadExpr (CF_NONE, Expr);
1847 GetCodePos (&Mark2);
1851 /* Get the right hand side */
1852 rconst = (evalexpr (CF_NONE, hienext, &Expr2) == 0);
1854 /* Make sure, the types are compatible */
1855 if (IsClassInt (Expr->Type)) {
1856 if (!IsClassInt (Expr2.Type) && !(IsClassPtr(Expr2.Type) && ED_IsNullPtr(Expr))) {
1857 Error ("Incompatible types");
1859 } else if (IsClassPtr (Expr->Type)) {
1860 if (IsClassPtr (Expr2.Type)) {
1861 /* Both pointers are allowed in comparison if they point to
1862 * the same type, or if one of them is a void pointer.
1864 Type* left = Indirect (Expr->Type);
1865 Type* right = Indirect (Expr2.Type);
1866 if (TypeCmp (left, right) < TC_EQUAL && left->C != T_VOID && right->C != T_VOID) {
1867 /* Incomatible pointers */
1868 Error ("Incompatible types");
1870 } else if (!ED_IsNullPtr (&Expr2)) {
1871 Error ("Incompatible types");
1875 /* Check for const operands */
1876 if (ED_IsConstAbs (Expr) && rconst) {
1878 /* Both operands are constant, remove the generated code */
1879 RemoveCode (&Mark1);
1881 /* Determine if this is a signed or unsigned compare */
1882 if (IsClassInt (Expr->Type) && IsSignSigned (Expr->Type) &&
1883 IsClassInt (Expr2.Type) && IsSignSigned (Expr2.Type)) {
1885 /* Evaluate the result for signed operands */
1886 signed long Val1 = Expr->IVal;
1887 signed long Val2 = Expr2.IVal;
1889 case TOK_EQ: Expr->IVal = (Val1 == Val2); break;
1890 case TOK_NE: Expr->IVal = (Val1 != Val2); break;
1891 case TOK_LT: Expr->IVal = (Val1 < Val2); break;
1892 case TOK_LE: Expr->IVal = (Val1 <= Val2); break;
1893 case TOK_GE: Expr->IVal = (Val1 >= Val2); break;
1894 case TOK_GT: Expr->IVal = (Val1 > Val2); break;
1895 default: Internal ("hie_compare: got token 0x%X\n", Tok);
1900 /* Evaluate the result for unsigned operands */
1901 unsigned long Val1 = Expr->IVal;
1902 unsigned long Val2 = Expr2.IVal;
1904 case TOK_EQ: Expr->IVal = (Val1 == Val2); break;
1905 case TOK_NE: Expr->IVal = (Val1 != Val2); break;
1906 case TOK_LT: Expr->IVal = (Val1 < Val2); break;
1907 case TOK_LE: Expr->IVal = (Val1 <= Val2); break;
1908 case TOK_GE: Expr->IVal = (Val1 >= Val2); break;
1909 case TOK_GT: Expr->IVal = (Val1 > Val2); break;
1910 default: Internal ("hie_compare: got token 0x%X\n", Tok);
1916 /* If the right hand side is constant, and the generator function
1917 * expects the lhs in the primary, remove the push of the primary
1923 if ((Gen->Flags & GEN_NOPUSH) != 0) {
1924 RemoveCode (&Mark2);
1925 ltype |= CF_REG; /* Value is in register */
1929 /* Determine the type of the operation result. If the left
1930 * operand is of type char and the right is a constant, or
1931 * if both operands are of type char, we will encode the
1932 * operation as char operation. Otherwise the default
1933 * promotions are used.
1935 if (IsTypeChar (Expr->Type) && (IsTypeChar (Expr2.Type) || rconst)) {
1937 if (IsSignUnsigned (Expr->Type) || IsSignUnsigned (Expr2.Type)) {
1938 flags |= CF_UNSIGNED;
1941 flags |= CF_FORCECHAR;
1944 unsigned rtype = TypeOf (Expr2.Type) | (flags & CF_CONST);
1945 flags |= g_typeadjust (ltype, rtype);
1949 Gen->Func (flags, Expr2.IVal);
1951 /* The result is an rvalue in the primary */
1952 ED_MakeRValExpr (Expr);
1955 /* Result type is always int */
1956 Expr->Type = type_int;
1958 /* Condition codes are set */
1965 static void hie9 (ExprDesc *Expr)
1966 /* Process * and / operators. */
1968 static const GenDesc hie9_ops[] = {
1969 { TOK_STAR, GEN_NOPUSH, g_mul },
1970 { TOK_DIV, GEN_NOPUSH, g_div },
1971 { TOK_MOD, GEN_NOPUSH, g_mod },
1972 { TOK_INVALID, 0, 0 }
1976 hie_internal (hie9_ops, Expr, hie10, &UsedGen);
1981 static void parseadd (ExprDesc* Expr)
1982 /* Parse an expression with the binary plus operator. Expr contains the
1983 * unprocessed left hand side of the expression and will contain the
1984 * result of the expression on return.
1988 unsigned flags; /* Operation flags */
1989 CodeMark Mark; /* Remember code position */
1990 Type* lhst; /* Type of left hand side */
1991 Type* rhst; /* Type of right hand side */
1994 /* Skip the PLUS token */
1997 /* Get the left hand side type, initialize operation flags */
2001 /* Check for constness on both sides */
2002 if (ED_IsConst (Expr)) {
2004 /* The left hand side is a constant of some sort. Good. Get rhs */
2006 if (ED_IsConstAbs (&Expr2)) {
2008 /* Right hand side is a constant numeric value. Get the rhs type */
2011 /* Both expressions are constants. Check for pointer arithmetic */
2012 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2013 /* Left is pointer, right is int, must scale rhs */
2014 Expr->IVal += Expr2.IVal * CheckedPSizeOf (lhst);
2015 /* Result type is a pointer */
2016 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2017 /* Left is int, right is pointer, must scale lhs */
2018 Expr->IVal = Expr->IVal * CheckedPSizeOf (rhst) + Expr2.IVal;
2019 /* Result type is a pointer */
2020 Expr->Type = Expr2.Type;
2021 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2022 /* Integer addition */
2023 Expr->IVal += Expr2.IVal;
2024 typeadjust (Expr, &Expr2, 1);
2027 Error ("Invalid operands for binary operator `+'");
2032 /* lhs is a constant and rhs is not constant. Load rhs into
2035 LoadExpr (CF_NONE, &Expr2);
2037 /* Beware: The check above (for lhs) lets not only pass numeric
2038 * constants, but also constant addresses (labels), maybe even
2039 * with an offset. We have to check for that here.
2042 /* First, get the rhs type. */
2046 if (ED_IsLocAbs (Expr)) {
2047 /* A numerical constant */
2050 /* Constant address label */
2051 flags |= GlobalModeFlags (Expr) | CF_CONSTADDR;
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 /* Generate the code for the add */
2061 if (ED_GetLoc (Expr) == E_LOC_ABS) {
2062 /* Numeric constant */
2063 g_inc (flags, Expr->IVal);
2065 /* Constant address */
2066 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2068 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2070 /* Left is int, right is pointer, must scale lhs. */
2071 unsigned ScaleFactor = CheckedPSizeOf (rhst);
2073 /* Operate on pointers, result type is a pointer */
2075 Expr->Type = Expr2.Type;
2077 /* Since we do already have rhs in the primary, if lhs is
2078 * not a numeric constant, and the scale factor is not one
2079 * (no scaling), we must take the long way over the stack.
2081 if (ED_IsLocAbs (Expr)) {
2082 /* Numeric constant, scale lhs */
2083 Expr->IVal *= ScaleFactor;
2084 /* Generate the code for the add */
2085 g_inc (flags, Expr->IVal);
2086 } else if (ScaleFactor == 1) {
2087 /* Constant address but no need to scale */
2088 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2090 /* Constant address that must be scaled */
2091 g_push (TypeOf (Expr2.Type), 0); /* rhs --> stack */
2092 g_getimmed (flags, Expr->Name, Expr->IVal);
2093 g_scale (CF_PTR, ScaleFactor);
2096 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2097 /* Integer addition */
2098 flags |= typeadjust (Expr, &Expr2, 1);
2099 /* Generate the code for the add */
2100 if (ED_IsLocAbs (Expr)) {
2101 /* Numeric constant */
2102 g_inc (flags, Expr->IVal);
2104 /* Constant address */
2105 g_addaddr_static (flags, Expr->Name, Expr->IVal);
2109 Error ("Invalid operands for binary operator `+'");
2113 /* Result is a rvalue in primary register */
2114 ED_MakeRValExpr (Expr);
2119 /* Left hand side is not constant. Get the value onto the stack. */
2120 LoadExpr (CF_NONE, Expr); /* --> primary register */
2122 g_push (TypeOf (Expr->Type), 0); /* --> stack */
2124 /* Evaluate the rhs */
2125 if (evalexpr (CF_NONE, hie9, &Expr2) == 0) {
2127 /* Right hand side is a constant. Get the rhs type */
2130 /* Remove pushed value from stack */
2133 /* Check for pointer arithmetic */
2134 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2135 /* Left is pointer, right is int, must scale rhs */
2136 Expr2.IVal *= CheckedPSizeOf (lhst);
2137 /* Operate on pointers, result type is a pointer */
2139 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2140 /* Left is int, right is pointer, must scale lhs (ptr only) */
2141 g_scale (CF_INT | CF_CONST, CheckedPSizeOf (rhst));
2142 /* Operate on pointers, result type is a pointer */
2144 Expr->Type = Expr2.Type;
2145 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2146 /* Integer addition */
2147 flags = typeadjust (Expr, &Expr2, 1);
2150 Error ("Invalid operands for binary operator `+'");
2154 /* Generate code for the add */
2155 g_inc (flags | CF_CONST, Expr2.IVal);
2159 /* lhs and rhs are not constant. Get the rhs type. */
2162 /* Check for pointer arithmetic */
2163 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2164 /* Left is pointer, right is int, must scale rhs */
2165 g_scale (CF_INT, CheckedPSizeOf (lhst));
2166 /* Operate on pointers, result type is a pointer */
2168 } else if (IsClassInt (lhst) && IsClassPtr (rhst)) {
2169 /* Left is int, right is pointer, must scale lhs */
2170 g_tosint (TypeOf (rhst)); /* Make sure, TOS is int */
2171 g_swap (CF_INT); /* Swap TOS and primary */
2172 g_scale (CF_INT, CheckedPSizeOf (rhst));
2173 /* Operate on pointers, result type is a pointer */
2175 Expr->Type = Expr2.Type;
2176 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2177 /* Integer addition. Note: Result is never constant.
2178 * Problem here is that typeadjust does not know if the
2179 * variable is an rvalue or lvalue, so if both operands
2180 * are dereferenced constant numeric addresses, typeadjust
2181 * thinks the operation works on constants. Removing
2182 * CF_CONST here means handling the symptoms, however, the
2183 * whole parser is such a mess that I fear to break anything
2184 * when trying to apply another solution.
2186 flags = typeadjust (Expr, &Expr2, 0) & ~CF_CONST;
2189 Error ("Invalid operands for binary operator `+'");
2193 /* Generate code for the add */
2198 /* Result is a rvalue in primary register */
2199 ED_MakeRValExpr (Expr);
2202 /* Condition codes not set */
2203 ED_MarkAsUntested (Expr);
2209 static void parsesub (ExprDesc* Expr)
2210 /* Parse an expression with the binary minus operator. Expr contains the
2211 * unprocessed left hand side of the expression and will contain the
2212 * result of the expression on return.
2216 unsigned flags; /* Operation flags */
2217 Type* lhst; /* Type of left hand side */
2218 Type* rhst; /* Type of right hand side */
2219 CodeMark Mark1; /* Save position of output queue */
2220 CodeMark Mark2; /* Another position in the queue */
2221 int rscale; /* Scale factor for the result */
2224 /* Skip the MINUS token */
2227 /* Get the left hand side type, initialize operation flags */
2229 rscale = 1; /* Scale by 1, that is, don't scale */
2231 /* Remember the output queue position, then bring the value onto the stack */
2232 GetCodePos (&Mark1);
2233 LoadExpr (CF_NONE, Expr); /* --> primary register */
2234 GetCodePos (&Mark2);
2235 g_push (TypeOf (lhst), 0); /* --> stack */
2237 /* Parse the right hand side */
2238 if (evalexpr (CF_NONE, hie9, &Expr2) == 0) {
2240 /* The right hand side is constant. Get the rhs type. */
2243 /* Check left hand side */
2244 if (ED_IsConstAbs (Expr)) {
2246 /* Both sides are constant, remove generated code */
2247 RemoveCode (&Mark1);
2249 /* Check for pointer arithmetic */
2250 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2251 /* Left is pointer, right is int, must scale rhs */
2252 Expr->IVal -= Expr2.IVal * CheckedPSizeOf (lhst);
2253 /* Operate on pointers, result type is a pointer */
2254 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2255 /* Left is pointer, right is pointer, must scale result */
2256 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2257 Error ("Incompatible pointer types");
2259 Expr->IVal = (Expr->IVal - Expr2.IVal) /
2260 CheckedPSizeOf (lhst);
2262 /* Operate on pointers, result type is an integer */
2263 Expr->Type = type_int;
2264 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2265 /* Integer subtraction */
2266 typeadjust (Expr, &Expr2, 1);
2267 Expr->IVal -= Expr2.IVal;
2270 Error ("Invalid operands for binary operator `-'");
2273 /* Result is constant, condition codes not set */
2274 ED_MarkAsUntested (Expr);
2278 /* Left hand side is not constant, right hand side is.
2279 * Remove pushed value from stack.
2281 RemoveCode (&Mark2);
2283 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2284 /* Left is pointer, right is int, must scale rhs */
2285 Expr2.IVal *= CheckedPSizeOf (lhst);
2286 /* Operate on pointers, result type is a pointer */
2288 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2289 /* Left is pointer, right is pointer, must scale result */
2290 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2291 Error ("Incompatible pointer types");
2293 rscale = CheckedPSizeOf (lhst);
2295 /* Operate on pointers, result type is an integer */
2297 Expr->Type = type_int;
2298 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2299 /* Integer subtraction */
2300 flags = typeadjust (Expr, &Expr2, 1);
2303 Error ("Invalid operands for binary operator `-'");
2307 /* Do the subtraction */
2308 g_dec (flags | CF_CONST, Expr2.IVal);
2310 /* If this was a pointer subtraction, we must scale the result */
2312 g_scale (flags, -rscale);
2315 /* Result is a rvalue in the primary register */
2316 ED_MakeRValExpr (Expr);
2317 ED_MarkAsUntested (Expr);
2323 /* Right hand side is not constant. Get the rhs type. */
2326 /* Check for pointer arithmetic */
2327 if (IsClassPtr (lhst) && IsClassInt (rhst)) {
2328 /* Left is pointer, right is int, must scale rhs */
2329 g_scale (CF_INT, CheckedPSizeOf (lhst));
2330 /* Operate on pointers, result type is a pointer */
2332 } else if (IsClassPtr (lhst) && IsClassPtr (rhst)) {
2333 /* Left is pointer, right is pointer, must scale result */
2334 if (TypeCmp (Indirect (lhst), Indirect (rhst)) < TC_QUAL_DIFF) {
2335 Error ("Incompatible pointer types");
2337 rscale = CheckedPSizeOf (lhst);
2339 /* Operate on pointers, result type is an integer */
2341 Expr->Type = type_int;
2342 } else if (IsClassInt (lhst) && IsClassInt (rhst)) {
2343 /* Integer subtraction. If the left hand side descriptor says that
2344 * the lhs is const, we have to remove this mark, since this is no
2345 * longer true, lhs is on stack instead.
2347 if (ED_IsLocAbs (Expr)) {
2348 ED_MakeRValExpr (Expr);
2350 /* Adjust operand types */
2351 flags = typeadjust (Expr, &Expr2, 0);
2354 Error ("Invalid operands for binary operator `-'");
2358 /* Generate code for the sub (the & is a hack here) */
2359 g_sub (flags & ~CF_CONST, 0);
2361 /* If this was a pointer subtraction, we must scale the result */
2363 g_scale (flags, -rscale);
2366 /* Result is a rvalue in the primary register */
2367 ED_MakeRValExpr (Expr);
2368 ED_MarkAsUntested (Expr);
2374 void hie8 (ExprDesc* Expr)
2375 /* Process + and - binary operators. */
2378 while (CurTok.Tok == TOK_PLUS || CurTok.Tok == TOK_MINUS) {
2379 if (CurTok.Tok == TOK_PLUS) {
2389 static void hie6 (ExprDesc* Expr)
2390 /* Handle greater-than type comparators */
2392 static const GenDesc hie6_ops [] = {
2393 { TOK_LT, GEN_NOPUSH, g_lt },
2394 { TOK_LE, GEN_NOPUSH, g_le },
2395 { TOK_GE, GEN_NOPUSH, g_ge },
2396 { TOK_GT, GEN_NOPUSH, g_gt },
2397 { TOK_INVALID, 0, 0 }
2399 hie_compare (hie6_ops, Expr, ShiftExpr);
2404 static void hie5 (ExprDesc* Expr)
2405 /* Handle == and != */
2407 static const GenDesc hie5_ops[] = {
2408 { TOK_EQ, GEN_NOPUSH, g_eq },
2409 { TOK_NE, GEN_NOPUSH, g_ne },
2410 { TOK_INVALID, 0, 0 }
2412 hie_compare (hie5_ops, Expr, hie6);
2417 static void hie4 (ExprDesc* Expr)
2418 /* Handle & (bitwise and) */
2420 static const GenDesc hie4_ops[] = {
2421 { TOK_AND, GEN_NOPUSH, g_and },
2422 { TOK_INVALID, 0, 0 }
2426 hie_internal (hie4_ops, Expr, hie5, &UsedGen);
2431 static void hie3 (ExprDesc* Expr)
2432 /* Handle ^ (bitwise exclusive or) */
2434 static const GenDesc hie3_ops[] = {
2435 { TOK_XOR, GEN_NOPUSH, g_xor },
2436 { TOK_INVALID, 0, 0 }
2440 hie_internal (hie3_ops, Expr, hie4, &UsedGen);
2445 static void hie2 (ExprDesc* Expr)
2446 /* Handle | (bitwise or) */
2448 static const GenDesc hie2_ops[] = {
2449 { TOK_OR, GEN_NOPUSH, g_or },
2450 { TOK_INVALID, 0, 0 }
2454 hie_internal (hie2_ops, Expr, hie3, &UsedGen);
2459 static void hieAndPP (ExprDesc* Expr)
2460 /* Process "exp && exp" in preprocessor mode (that is, when the parser is
2461 * called recursively from the preprocessor.
2466 ConstAbsIntExpr (hie2, Expr);
2467 while (CurTok.Tok == TOK_BOOL_AND) {
2473 ConstAbsIntExpr (hie2, &Expr2);
2475 /* Combine the two */
2476 Expr->IVal = (Expr->IVal && Expr2.IVal);
2482 static void hieOrPP (ExprDesc *Expr)
2483 /* Process "exp || exp" in preprocessor mode (that is, when the parser is
2484 * called recursively from the preprocessor.
2489 ConstAbsIntExpr (hieAndPP, Expr);
2490 while (CurTok.Tok == TOK_BOOL_OR) {
2496 ConstAbsIntExpr (hieAndPP, &Expr2);
2498 /* Combine the two */
2499 Expr->IVal = (Expr->IVal || Expr2.IVal);
2505 static void hieAnd (ExprDesc* Expr, unsigned TrueLab, int* BoolOp)
2506 /* Process "exp && exp" */
2512 if (CurTok.Tok == TOK_BOOL_AND) {
2514 /* Tell our caller that we're evaluating a boolean */
2517 /* Get a label that we will use for false expressions */
2518 lab = GetLocalLabel ();
2520 /* If the expr hasn't set condition codes, set the force-test flag */
2521 if (!ED_IsTested (Expr)) {
2522 ED_MarkForTest (Expr);
2525 /* Load the value */
2526 LoadExpr (CF_FORCECHAR, Expr);
2528 /* Generate the jump */
2529 g_falsejump (CF_NONE, lab);
2531 /* Parse more boolean and's */
2532 while (CurTok.Tok == TOK_BOOL_AND) {
2539 if (!ED_IsTested (&Expr2)) {
2540 ED_MarkForTest (&Expr2);
2542 LoadExpr (CF_FORCECHAR, &Expr2);
2544 /* Do short circuit evaluation */
2545 if (CurTok.Tok == TOK_BOOL_AND) {
2546 g_falsejump (CF_NONE, lab);
2548 /* Last expression - will evaluate to true */
2549 g_truejump (CF_NONE, TrueLab);
2553 /* Define the false jump label here */
2554 g_defcodelabel (lab);
2556 /* The result is an rvalue in primary */
2557 ED_MakeRValExpr (Expr);
2558 ED_TestDone (Expr); /* Condition codes are set */
2564 static void hieOr (ExprDesc *Expr)
2565 /* Process "exp || exp". */
2568 int BoolOp = 0; /* Did we have a boolean op? */
2569 int AndOp; /* Did we have a && operation? */
2570 unsigned TrueLab; /* Jump to this label if true */
2574 TrueLab = GetLocalLabel ();
2576 /* Call the next level parser */
2577 hieAnd (Expr, TrueLab, &BoolOp);
2579 /* Any boolean or's? */
2580 if (CurTok.Tok == TOK_BOOL_OR) {
2582 /* If the expr hasn't set condition codes, set the force-test flag */
2583 if (!ED_IsTested (Expr)) {
2584 ED_MarkForTest (Expr);
2587 /* Get first expr */
2588 LoadExpr (CF_FORCECHAR, Expr);
2590 /* For each expression jump to TrueLab if true. Beware: If we
2591 * had && operators, the jump is already in place!
2594 g_truejump (CF_NONE, TrueLab);
2597 /* Remember that we had a boolean op */
2600 /* while there's more expr */
2601 while (CurTok.Tok == TOK_BOOL_OR) {
2608 hieAnd (&Expr2, TrueLab, &AndOp);
2609 if (!ED_IsTested (&Expr2)) {
2610 ED_MarkForTest (&Expr2);
2612 LoadExpr (CF_FORCECHAR, &Expr2);
2614 /* If there is more to come, add shortcut boolean eval. */
2615 g_truejump (CF_NONE, TrueLab);
2619 /* The result is an rvalue in primary */
2620 ED_MakeRValExpr (Expr);
2621 ED_TestDone (Expr); /* Condition codes are set */
2624 /* If we really had boolean ops, generate the end sequence */
2626 DoneLab = GetLocalLabel ();
2627 g_getimmed (CF_INT | CF_CONST, 0, 0); /* Load FALSE */
2628 g_falsejump (CF_NONE, DoneLab);
2629 g_defcodelabel (TrueLab);
2630 g_getimmed (CF_INT | CF_CONST, 1, 0); /* Load TRUE */
2631 g_defcodelabel (DoneLab);
2637 static void hieQuest (ExprDesc* Expr)
2638 /* Parse the ternary operator */
2642 ExprDesc Expr2; /* Expression 2 */
2643 ExprDesc Expr3; /* Expression 3 */
2644 int Expr2IsNULL; /* Expression 2 is a NULL pointer */
2645 int Expr3IsNULL; /* Expression 3 is a NULL pointer */
2646 Type* ResultType; /* Type of result */
2649 /* Call the lower level eval routine */
2650 if (Preprocessing) {
2656 /* Check if it's a ternary expression */
2657 if (CurTok.Tok == TOK_QUEST) {
2659 if (!ED_IsTested (Expr)) {
2660 /* Condition codes not set, request a test */
2661 ED_MarkForTest (Expr);
2663 LoadExpr (CF_NONE, Expr);
2664 labf = GetLocalLabel ();
2665 g_falsejump (CF_NONE, labf);
2667 /* Parse second expression. Remember for later if it is a NULL pointer
2668 * expression, then load it into the primary.
2670 ExprWithCheck (hie1, &Expr2);
2671 Expr2IsNULL = ED_IsNullPtr (&Expr2);
2672 if (!IsTypeVoid (Expr2.Type)) {
2673 /* Load it into the primary */
2674 LoadExpr (CF_NONE, &Expr2);
2675 ED_MakeRValExpr (&Expr2);
2677 labt = GetLocalLabel ();
2681 /* Jump here if the first expression was false */
2682 g_defcodelabel (labf);
2684 /* Parse second expression. Remember for later if it is a NULL pointer
2685 * expression, then load it into the primary.
2687 ExprWithCheck (hie1, &Expr3);
2688 Expr3IsNULL = ED_IsNullPtr (&Expr3);
2689 if (!IsTypeVoid (Expr3.Type)) {
2690 /* Load it into the primary */
2691 LoadExpr (CF_NONE, &Expr3);
2692 ED_MakeRValExpr (&Expr3);
2695 /* Check if any conversions are needed, if so, do them.
2696 * Conversion rules for ?: expression are:
2697 * - if both expressions are int expressions, default promotion
2698 * rules for ints apply.
2699 * - if both expressions are pointers of the same type, the
2700 * result of the expression is of this type.
2701 * - if one of the expressions is a pointer and the other is
2702 * a zero constant, the resulting type is that of the pointer
2704 * - if both expressions are void expressions, the result is of
2706 * - all other cases are flagged by an error.
2708 if (IsClassInt (Expr2.Type) && IsClassInt (Expr3.Type)) {
2710 /* Get common type */
2711 ResultType = promoteint (Expr2.Type, Expr3.Type);
2713 /* Convert the third expression to this type if needed */
2714 TypeConversion (&Expr3, ResultType);
2716 /* Setup a new label so that the expr3 code will jump around
2717 * the type cast code for expr2.
2719 labf = GetLocalLabel (); /* Get new label */
2720 g_jump (labf); /* Jump around code */
2722 /* The jump for expr2 goes here */
2723 g_defcodelabel (labt);
2725 /* Create the typecast code for expr2 */
2726 TypeConversion (&Expr2, ResultType);
2728 /* Jump here around the typecase code. */
2729 g_defcodelabel (labf);
2730 labt = 0; /* Mark other label as invalid */
2732 } else if (IsClassPtr (Expr2.Type) && IsClassPtr (Expr3.Type)) {
2733 /* Must point to same type */
2734 if (TypeCmp (Indirect (Expr2.Type), Indirect (Expr3.Type)) < TC_EQUAL) {
2735 Error ("Incompatible pointer types");
2737 /* Result has the common type */
2738 ResultType = Expr2.Type;
2739 } else if (IsClassPtr (Expr2.Type) && Expr3IsNULL) {
2740 /* Result type is pointer, no cast needed */
2741 ResultType = Expr2.Type;
2742 } else if (Expr2IsNULL && IsClassPtr (Expr3.Type)) {
2743 /* Result type is pointer, no cast needed */
2744 ResultType = Expr3.Type;
2745 } else if (IsTypeVoid (Expr2.Type) && IsTypeVoid (Expr3.Type)) {
2746 /* Result type is void */
2747 ResultType = Expr3.Type;
2749 Error ("Incompatible types");
2750 ResultType = Expr2.Type; /* Doesn't matter here */
2753 /* If we don't have the label defined until now, do it */
2755 g_defcodelabel (labt);
2758 /* Setup the target expression */
2759 ED_MakeRValExpr (Expr);
2760 Expr->Type = ResultType;
2766 static void opeq (const GenDesc* Gen, ExprDesc* Expr)
2767 /* Process "op=" operators. */
2774 /* op= can only be used with lvalues */
2775 if (!ED_IsLVal (Expr)) {
2776 Error ("Invalid lvalue in assignment");
2780 /* The left side must not be const qualified */
2781 if (IsQualConst (Expr->Type)) {
2782 Error ("Assignment to const");
2785 /* There must be an integer or pointer on the left side */
2786 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
2787 Error ("Invalid left operand type");
2788 /* Continue. Wrong code will be generated, but the compiler won't
2789 * break, so this is the best error recovery.
2793 /* Skip the operator token */
2796 /* Determine the type of the lhs */
2797 flags = TypeOf (Expr->Type);
2798 MustScale = (Gen->Func == g_add || Gen->Func == g_sub) && IsTypePtr (Expr->Type);
2800 /* Get the lhs address on stack (if needed) */
2803 /* Fetch the lhs into the primary register if needed */
2804 LoadExpr (CF_NONE, Expr);
2806 /* Bring the lhs on stack */
2810 /* Evaluate the rhs */
2811 if (evalexpr (CF_NONE, hie1, &Expr2) == 0) {
2812 /* The resulting value is a constant. If the generator has the NOPUSH
2813 * flag set, don't push the lhs.
2815 if (Gen->Flags & GEN_NOPUSH) {
2819 /* lhs is a pointer, scale rhs */
2820 Expr2.IVal *= CheckedSizeOf (Expr->Type+1);
2823 /* If the lhs is character sized, the operation may be later done
2826 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
2827 flags |= CF_FORCECHAR;
2830 /* Special handling for add and sub - some sort of a hack, but short code */
2831 if (Gen->Func == g_add) {
2832 g_inc (flags | CF_CONST, Expr2.IVal);
2833 } else if (Gen->Func == g_sub) {
2834 g_dec (flags | CF_CONST, Expr2.IVal);
2836 Gen->Func (flags | CF_CONST, Expr2.IVal);
2839 /* rhs is not constant and already in the primary register */
2841 /* lhs is a pointer, scale rhs */
2842 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Expr->Type+1));
2845 /* If the lhs is character sized, the operation may be later done
2848 if (CheckedSizeOf (Expr->Type) == SIZEOF_CHAR) {
2849 flags |= CF_FORCECHAR;
2852 /* Adjust the types of the operands if needed */
2853 Gen->Func (g_typeadjust (flags, TypeOf (Expr2.Type)), 0);
2856 ED_MakeRValExpr (Expr);
2861 static void addsubeq (const GenDesc* Gen, ExprDesc *Expr)
2862 /* Process the += and -= operators */
2870 /* We're currently only able to handle some adressing modes */
2871 if (ED_GetLoc (Expr) == E_LOC_EXPR || ED_GetLoc (Expr) == E_LOC_PRIMARY) {
2872 /* Use generic routine */
2877 /* We must have an lvalue */
2878 if (ED_IsRVal (Expr)) {
2879 Error ("Invalid lvalue in assignment");
2883 /* The left side must not be const qualified */
2884 if (IsQualConst (Expr->Type)) {
2885 Error ("Assignment to const");
2888 /* There must be an integer or pointer on the left side */
2889 if (!IsClassInt (Expr->Type) && !IsTypePtr (Expr->Type)) {
2890 Error ("Invalid left operand type");
2891 /* Continue. Wrong code will be generated, but the compiler won't
2892 * break, so this is the best error recovery.
2896 /* Skip the operator */
2899 /* Check if we have a pointer expression and must scale rhs */
2900 MustScale = IsTypePtr (Expr->Type);
2902 /* Initialize the code generator flags */
2906 /* Evaluate the rhs */
2908 if (ED_IsConstAbs (&Expr2)) {
2909 /* The resulting value is a constant. Scale it. */
2911 Expr2.IVal *= CheckedSizeOf (Indirect (Expr->Type));
2916 /* Not constant, load into the primary */
2917 LoadExpr (CF_NONE, &Expr2);
2919 /* lhs is a pointer, scale rhs */
2920 g_scale (TypeOf (Expr2.Type), CheckedSizeOf (Indirect (Expr->Type)));
2924 /* Setup the code generator flags */
2925 lflags |= TypeOf (Expr->Type) | CF_FORCECHAR;
2926 rflags |= TypeOf (Expr2.Type);
2928 /* Convert the type of the lhs to that of the rhs */
2929 g_typecast (lflags, rflags);
2931 /* Output apropriate code depending on the location */
2932 switch (ED_GetLoc (Expr)) {
2935 /* Absolute: numeric address or const */
2936 lflags |= CF_ABSOLUTE;
2937 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2938 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2940 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2945 /* Global variable */
2946 lflags |= CF_EXTERNAL;
2947 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2948 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2950 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2956 /* Static variable or literal in the literal pool */
2957 lflags |= CF_STATIC;
2958 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2959 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2961 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2965 case E_LOC_REGISTER:
2966 /* Register variable */
2967 lflags |= CF_REGVAR;
2968 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2969 g_addeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2971 g_subeqstatic (lflags, Expr->Name, Expr->IVal, Expr2.IVal);
2976 /* Value on the stack */
2977 if (Gen->Tok == TOK_PLUS_ASSIGN) {
2978 g_addeqlocal (lflags, Expr->IVal, Expr2.IVal);
2980 g_subeqlocal (lflags, Expr->IVal, Expr2.IVal);
2985 Internal ("Invalid location in Store(): 0x%04X", ED_GetLoc (Expr));
2988 /* Expression is a rvalue in the primary now */
2989 ED_MakeRValExpr (Expr);
2994 void hie1 (ExprDesc* Expr)
2995 /* Parse first level of expression hierarchy. */
2998 switch (CurTok.Tok) {
3004 case TOK_PLUS_ASSIGN:
3005 addsubeq (&GenPASGN, Expr);
3008 case TOK_MINUS_ASSIGN:
3009 addsubeq (&GenSASGN, Expr);
3012 case TOK_MUL_ASSIGN:
3013 opeq (&GenMASGN, Expr);
3016 case TOK_DIV_ASSIGN:
3017 opeq (&GenDASGN, Expr);
3020 case TOK_MOD_ASSIGN:
3021 opeq (&GenMOASGN, Expr);
3024 case TOK_SHL_ASSIGN:
3025 opeq (&GenSLASGN, Expr);
3028 case TOK_SHR_ASSIGN:
3029 opeq (&GenSRASGN, Expr);
3032 case TOK_AND_ASSIGN:
3033 opeq (&GenAASGN, Expr);
3036 case TOK_XOR_ASSIGN:
3037 opeq (&GenXOASGN, Expr);
3041 opeq (&GenOASGN, Expr);
3051 void hie0 (ExprDesc *Expr)
3052 /* Parse comma operator. */
3055 while (CurTok.Tok == TOK_COMMA) {
3063 int evalexpr (unsigned Flags, void (*Func) (ExprDesc*), ExprDesc* Expr)
3064 /* Will evaluate an expression via the given function. If the result is a
3065 * constant, 0 is returned and the value is put in the Expr struct. If the
3066 * result is not constant, LoadExpr is called to bring the value into the
3067 * primary register and 1 is returned.
3071 ExprWithCheck (Func, Expr);
3073 /* Check for a constant expression */
3074 if (ED_IsConstAbs (Expr)) {
3075 /* Constant expression */
3078 /* Not constant, load into the primary */
3079 LoadExpr (Flags, Expr);
3086 void Expression0 (ExprDesc* Expr)
3087 /* Evaluate an expression via hie0 and put the result into the primary register */
3089 ExprWithCheck (hie0, Expr);
3090 LoadExpr (CF_NONE, Expr);
3095 void ConstExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3096 /* Will evaluate an expression via the given function. If the result is not
3097 * a constant of some sort, a diagnostic will be printed, and the value is
3098 * replaced by a constant one to make sure there are no internal errors that
3099 * result from this input error.
3102 ExprWithCheck (Func, Expr);
3103 if (!ED_IsConst (Expr)) {
3104 Error ("Constant expression expected");
3105 /* To avoid any compiler errors, make the expression a valid const */
3106 ED_MakeConstAbsInt (Expr, 1);
3112 void BoolExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3113 /* Will evaluate an expression via the given function. If the result is not
3114 * something that may be evaluated in a boolean context, a diagnostic will be
3115 * printed, and the value is replaced by a constant one to make sure there
3116 * are no internal errors that result from this input error.
3119 ExprWithCheck (Func, Expr);
3120 if (!ED_IsBool (Expr)) {
3121 Error ("Boolean expression expected");
3122 /* To avoid any compiler errors, make the expression a valid int */
3123 ED_MakeConstAbsInt (Expr, 1);
3129 void ConstAbsIntExpr (void (*Func) (ExprDesc*), ExprDesc* Expr)
3130 /* Will evaluate an expression via the given function. If the result is not
3131 * a constant numeric integer value, a diagnostic will be printed, and the
3132 * value is replaced by a constant one to make sure there are no internal
3133 * errors that result from this input error.
3136 ExprWithCheck (Func, Expr);
3137 if (!ED_IsConstAbsInt (Expr)) {
3138 Error ("Constant integer expression expected");
3139 /* To avoid any compiler errors, make the expression a valid const */
3140 ED_MakeConstAbsInt (Expr, 1);