1 /*****************************************************************************/
5 /* Parse the << and >> operators */
9 /* (C) 2004 Ullrich von Bassewitz */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
45 #include "shiftexpr.h"
49 /*****************************************************************************/
51 /*****************************************************************************/
55 /*****************************************************************************/
57 /*****************************************************************************/
61 void ShiftExpr (struct ExprDesc* Expr)
62 /* Parse the << and >> operators. */
67 token_t Tok; /* The operator token */
68 type* EffType; /* Effective lhs type */
69 type* ResultType; /* Type of the result */
70 unsigned ExprBits; /* Bits of the lhs operand */
71 unsigned GenFlags; /* Generator flags */
73 int rconst; /* Operand is a constant */
76 /* Evaluate the lhs */
77 ExprWithCheck (hie8, Expr);
79 while (CurTok.Tok == TOK_SHL || CurTok.Tok == TOK_SHR) {
81 /* All operators that call this function expect an int on the lhs */
82 if (!IsClassInt (Expr->Type)) {
83 Error ("Integer expression expected");
84 ED_MakeConstAbsInt (Expr, 1);
87 /* Remember the operator token, then skip it */
91 /* Get the type of the result */
92 ResultType = EffType = IntPromotion (Expr->Type);
94 /* Prepare the code generator flags */
95 GenFlags = TypeOf (ResultType);
97 /* Calculate the number of bits the lhs operand has */
98 ExprBits = SizeOf (ResultType) * 8;
100 /* Get the lhs on stack */
102 ltype = TypeOf (Expr->Type);
103 if (ED_IsConstAbs (Expr)) {
106 g_push (ltype | CF_CONST, Expr->IVal);
108 /* Value not constant */
109 LoadExpr (CF_NONE, Expr);
114 /* Get the right hand side */
115 ExprWithCheck (hie8, &Expr2);
117 /* Check the type of the rhs */
118 if (!IsClassInt (Expr2.Type)) {
119 Error ("Integer expression expected");
120 ED_MakeConstAbsInt (&Expr2, 1);
123 /* Check for a constant right side expression */
124 rconst = ED_IsConstAbs (&Expr2);
127 /* Not constant, load into the primary */
128 LoadExpr (CF_NONE, &Expr2);
132 /* The rhs is a constant numeric value. */
133 GenFlags |= CF_CONST;
135 /* Remove the code that pushes the rhs onto the stack. */
138 /* If the shift count is greater or equal than the bit count of
139 * the operand, the behaviour is undefined according to the
142 if (Expr2.IVal < 0 || Expr2.IVal >= (long) ExprBits) {
144 Warning ("Shift count too large for operand type");
145 Expr2.IVal &= ExprBits - 1;
149 /* If the shift count is zero, nothing happens */
150 if (Expr2.IVal == 0) {
152 /* Result is already in Expr, remove the generated code */
159 /* If the left hand side is a constant, the result is constant */
160 if (ED_IsConstAbs (Expr)) {
162 /* Evaluate the result */
164 case TOK_SHL: Expr->IVal <<= Expr2.IVal; break;
165 case TOK_SHR: Expr->IVal >>= Expr2.IVal; break;
166 default: /* Shutup gcc */ break;
169 /* Both operands are constant, remove the generated code */
176 /* If we're shifting an integer or unsigned to the right, the
177 * lhs has a const address, and the shift count is larger than 8,
178 * we can load just the high byte as a char with the correct
179 * signedness, and reduce the shift count by 8. If the remaining
180 * shift count is zero, we're done.
182 if (Tok == TOK_SHR &&
183 IsTypeInt (Expr->Type) &&
185 (ED_IsLocConst (Expr) || ED_IsLocStack (Expr)) &&
190 /* Increase the address by one and decrease the shift count */
194 /* Replace the type of the expression temporarily by the
195 * corresponding char type.
197 OldType = Expr->Type;
198 if (IsSignUnsigned (Expr->Type)) {
199 Expr->Type = type_uchar;
201 Expr->Type = type_schar;
204 /* Remove the generated load code */
207 /* Generate again code for the load, this time with the new type */
208 LoadExpr (CF_NONE, Expr);
211 Expr->Type = OldType;
213 /* If the shift count is now zero, we're done */
214 if (Expr2.IVal == 0) {
215 /* Be sure to mark the value as in the primary */
224 case TOK_SHL: g_asl (GenFlags, Expr2.IVal); break;
225 case TOK_SHR: g_asr (GenFlags, Expr2.IVal); break;
230 /* We have a rvalue in the primary now */
231 ED_MakeRValExpr (Expr);
234 /* Set the type of the result */
235 Expr->Type = ResultType;