]> git.sur5r.net Git - cc65/blob - src/cc65/typeconv.c
Fixed a bug
[cc65] / src / cc65 / typeconv.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                typeconv.c                                 */
4 /*                                                                           */
5 /*                          Handle type conversions                          */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2002-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 /* common */
37 #include "shift.h"
38
39 /* cc65 */
40 #include "codegen.h"
41 #include "datatype.h"
42 #include "declare.h"
43 #include "error.h"
44 #include "expr.h"
45 #include "scanner.h"
46 #include "typecmp.h"
47 #include "typeconv.h"
48
49
50
51 /*****************************************************************************/
52 /*                                   Code                                    */
53 /*****************************************************************************/
54
55
56
57 static void DoPtrConversions (ExprDesc* Expr)
58 /* If the expression is a function, convert it to pointer to function.
59  * If the expression is an array, convert it to pointer to first element.
60  */
61 {
62     if (IsTypeFunc (Expr->Type)) {
63         Expr->Type = PointerTo (Expr->Type);
64     } else if (IsTypeArray (Expr->Type)) {
65         Expr->Type = ArrayToPtr (Expr->Type);
66     }
67 }
68
69
70
71 static int DoConversion (ExprDesc* Expr, int k, type* NewType)
72 /* Emit code to convert the given expression to a new type. */
73 {
74     type*    OldType;
75     unsigned OldSize;
76     unsigned NewSize;
77
78
79     /* Remember the old type */
80     OldType = Expr->Type;
81
82     /* If we're converting to void, we're done. Note: This does also cover a
83      * conversion void -> void.
84      */
85     if (IsTypeVoid (NewType)) {
86         k = 0;          /* Never an lvalue */
87         goto ExitPoint;
88     }
89
90     /* Don't allow casts from void to something else. */
91     if (IsTypeVoid (OldType)) {
92         Error ("Cannot convert from `void' to something else");
93         goto ExitPoint;
94     }
95
96     /* Get the sizes of the types. Since we've excluded void types, checking
97      * for known sizes makes sense here.
98      */
99     OldSize = CheckedSizeOf (OldType);
100     NewSize = CheckedSizeOf (NewType);
101
102     /* lvalue? */
103     if (k != 0) {
104
105         /* We have an lvalue. If the new size is smaller than the new one,
106          * we don't need to do anything. The compiler will generate code
107          * to load only the portion of the value that is actually needed.
108          * This works only on a little endian architecture, but that's
109          * what we support.
110          * If both sizes are equal, do also leave the value alone.
111          * If the new size is larger, we must convert the value.
112          */
113         if (NewSize > OldSize) {
114             /* Load the value into the primary */
115             ExprLoad (CF_NONE, k, Expr);
116
117             /* Emit typecast code */
118             g_typecast (TypeOf (NewType), TypeOf (OldType));
119
120             /* Value is now in primary */
121             Expr->Flags = E_MEXPR;
122             k = 0;
123         }
124
125     } else {
126
127         /* We have an rvalue. Check for a constant. */
128         if (Expr->Flags == E_MCONST) {
129
130             /* A cast of a constant to an integer. Be sure to handle sign
131              * extension correctly.
132              */
133
134             /* Get the current and new size of the value */
135             unsigned OldBits = OldSize * 8;
136             unsigned NewBits = NewSize * 8;
137
138             /* Check if the new datatype will have a smaller range. If it
139              * has a larger range, things are ok, since the value is
140              * internally already represented by a long.
141              */
142             if (NewBits <= OldBits) {
143
144                 /* Cut the value to the new size */
145                 Expr->ConstVal &= (0xFFFFFFFFUL >> (32 - NewBits));
146
147                 /* If the new type is signed, sign extend the value */
148                 if (!IsSignUnsigned (NewType)) {
149                     if (Expr->ConstVal & (0x01UL << (NewBits-1))) {
150                         /* Beware: Use the safe shift routine here. */
151                         Expr->ConstVal |= shl_l (~0UL, NewBits);
152                     }
153                 }
154             }
155
156         } else {
157
158             /* The value is not a constant. If the sizes of the types are
159              * not equal, add conversion code. Be sure to convert chars
160              * correctly.
161              */
162             if (OldSize != NewSize) {
163
164                 /* Load the value into the primary */
165                 ExprLoad (CF_NONE, k, Expr);
166
167                 /* Emit typecast code. */
168                 g_typecast (TypeOf (NewType) | CF_FORCECHAR, TypeOf (OldType));
169
170                 /* Value is now in primary */
171                 Expr->Flags = E_MEXPR;
172                 k = 0;
173             }
174         }
175     }
176
177 ExitPoint:
178     /* The expression has always the new type */
179     ReplaceType (Expr, NewType);
180
181     /* Done */
182     return k;
183 }
184
185
186
187 int TypeConversion (ExprDesc* Expr, int k, type* NewType)
188 /* Do an automatic conversion of the given expression to the new type. Output
189  * warnings or errors where this automatic conversion is suspicious or
190  * impossible.
191  */
192 {
193     /* Get the type of the right hand side. Treat function types as
194      * pointer-to-function
195      */
196     DoPtrConversions (Expr);
197
198     /* First, do some type checking */
199     if (IsTypeVoid (NewType) || IsTypeVoid (Expr->Type)) {
200         /* If one of the sides are of type void, output a more apropriate
201          * error message.
202          */
203         Error ("Illegal type");
204         return k;
205     }
206
207     /* Handle conversions to int type */
208     if (IsClassInt (NewType)) {
209         if (IsClassPtr (Expr->Type)) {
210             /* Pointer -> int conversion */
211             Warning ("Converting pointer to integer without a cast");
212         } else if (!IsClassInt (Expr->Type)) {
213             Error ("Incompatible types");
214         }
215
216         /* Do a conversion regardless of errors and return the result. */
217         return DoConversion (Expr, k, NewType);
218     }
219
220     /* Handle conversions to pointer type */
221     if (IsClassPtr (NewType)) {
222         if (IsClassPtr (Expr->Type)) {
223             /* Pointer to pointer assignment is valid, if:
224              *   - both point to the same types, or
225              *   - the rhs pointer is a void pointer, or
226              *   - the lhs pointer is a void pointer.
227              */
228             if (!IsTypeVoid (Indirect (NewType)) && !IsTypeVoid (Indirect (Expr->Type))) {
229                 /* Compare the types */
230                 switch (TypeCmp (NewType, Expr->Type)) {
231
232                     case TC_INCOMPATIBLE:
233                         Error ("Incompatible pointer types");
234                         break;
235
236                     case TC_QUAL_DIFF:
237                         Error ("Pointer types differ in type qualifiers");
238                         break;
239
240                     default:
241                         /* Ok */
242                         break;
243                 }
244             }
245         } else if (IsClassInt (Expr->Type)) {
246             /* Int to pointer assignment is valid only for constant zero */
247             if (Expr->Flags != E_MCONST || Expr->ConstVal != 0) {
248                 Warning ("Converting integer to pointer without a cast");
249             }
250         } else if (IsTypeFuncPtr (NewType) && IsTypeFunc(Expr->Type)) {
251             /* Assignment of function to function pointer is allowed, provided
252              * that both functions have the same parameter list.
253              */
254             if (TypeCmp (Indirect (NewType), Expr->Type) < TC_EQUAL) {
255                 Error ("Incompatible types");
256             }
257         } else {
258             Error ("Incompatible types");
259         }
260
261         /* Do the conversion even in case of errors */
262         return DoConversion (Expr, k, NewType);
263
264     }
265
266     /* Invalid automatic conversion */
267     Error ("Incompatible types");
268     return DoConversion (Expr, k, NewType);
269 }
270
271
272
273 int TypeCast (ExprDesc* Expr)
274 /* Handle an explicit cast. The function returns true if the resulting
275  * expression is an lvalue and false if not.
276  */
277 {
278     int     k;
279     type    NewType[MAXTYPELEN];
280
281     /* Skip the left paren */
282     NextToken ();
283
284     /* Read the type */
285     ParseType (NewType);
286
287     /* Closing paren */
288     ConsumeRParen ();
289
290     /* Read the expression we have to cast */
291     k = hie10 (Expr);
292
293     /* Convert functions and arrays to "pointer to" object */
294     DoPtrConversions (Expr);
295
296     /* Convert the value and return the result. */
297     return DoConversion (Expr, k, NewType);
298 }
299
300
301
302