]> git.sur5r.net Git - cc65/blob - src/cc65/typeconv.c
Renamed ExprDesc.Val to ExprDesc.IVal. Added an FVal field for a floating
[cc65] / src / cc65 / typeconv.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                typeconv.c                                 */
4 /*                                                                           */
5 /*                          Handle type conversions                          */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2002-2004 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 void DoConversion (ExprDesc* Expr, const 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         ED_MakeRVal (Expr);     /* 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 (ED_IsLVal (Expr)) {
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, Expr);
116
117             /* Emit typecast code */
118             g_typecast (TypeOf (NewType), TypeOf (OldType));
119
120             /* Value is now in primary and an rvalue */
121             ED_MakeRValExpr (Expr);
122         }
123
124     } else if (ED_IsLocAbs (Expr)) {
125
126         /* A cast of a constant numeric value to another type. Be sure
127          * to handle sign extension correctly.
128          */
129
130         /* Get the current and new size of the value */
131         unsigned OldBits = OldSize * 8;
132         unsigned NewBits = NewSize * 8;
133
134         /* Check if the new datatype will have a smaller range. If it
135          * has a larger range, things are ok, since the value is
136          * internally already represented by a long.
137          */
138         if (NewBits <= OldBits) {
139
140             /* Cut the value to the new size */
141             Expr->IVal &= (0xFFFFFFFFUL >> (32 - NewBits));
142
143             /* If the new type is signed, sign extend the value */
144             if (!IsSignUnsigned (NewType)) {
145                 if (Expr->IVal & (0x01UL << (NewBits-1))) {
146                     /* Beware: Use the safe shift routine here. */
147                     Expr->IVal |= shl_l (~0UL, NewBits);
148                 }
149             }
150         }
151
152     } else {
153
154         /* The value is not a constant. If the sizes of the types are
155          * not equal, add conversion code. Be sure to convert chars
156          * correctly.
157          */
158         if (OldSize != NewSize) {
159
160             /* Load the value into the primary */
161             ExprLoad (CF_NONE, Expr);
162
163             /* Emit typecast code. */
164             g_typecast (TypeOf (NewType) | CF_FORCECHAR, TypeOf (OldType));
165
166             /* Value is now a rvalue in the primary */
167             ED_MakeRValExpr (Expr);
168         }
169     }
170
171 ExitPoint:
172     /* The expression has always the new type */
173     ReplaceType (Expr, NewType);
174 }
175
176
177
178 void TypeConversion (ExprDesc* Expr, type* NewType)
179 /* Do an automatic conversion of the given expression to the new type. Output
180  * warnings or errors where this automatic conversion is suspicious or
181  * impossible.
182  */
183 {
184     /* Get the type of the right hand side. Treat function types as
185      * pointer-to-function
186      */
187     DoPtrConversions (Expr);
188
189     /* First, do some type checking */
190     if (IsTypeVoid (NewType) || IsTypeVoid (Expr->Type)) {
191         /* If one of the sides are of type void, output a more apropriate
192          * error message.
193          */
194         Error ("Illegal type");
195     }
196
197     /* Check for conversion problems */
198     if (IsClassInt (NewType)) {
199
200         /* Handle conversions to int type */
201         if (IsClassPtr (Expr->Type)) {
202             /* Pointer -> int conversion */
203             Warning ("Converting pointer to integer without a cast");
204         } else if (!IsClassInt (Expr->Type)) {
205             Error ("Incompatible types");
206         }
207
208     } else if (IsClassPtr (NewType)) {
209
210         /* Handle conversions to pointer type */
211         if (IsClassPtr (Expr->Type)) {
212             /* Pointer to pointer assignment is valid, if:
213              *   - both point to the same types, or
214              *   - the rhs pointer is a void pointer, or
215              *   - the lhs pointer is a void pointer.
216              */
217             if (!IsTypeVoid (Indirect (NewType)) && !IsTypeVoid (Indirect (Expr->Type))) {
218                 /* Compare the types */
219                 switch (TypeCmp (NewType, Expr->Type)) {
220
221                     case TC_INCOMPATIBLE:
222                         Error ("Incompatible pointer types");
223                         break;
224
225                     case TC_QUAL_DIFF:
226                         Error ("Pointer types differ in type qualifiers");
227                         break;
228
229                     default:
230                         /* Ok */
231                         break;
232                 }
233             }
234         } else if (IsClassInt (Expr->Type)) {
235             /* Int to pointer assignment is valid only for constant zero */
236             if (!ED_IsConstAbsInt (Expr) || Expr->IVal != 0) {
237                 Warning ("Converting integer to pointer without a cast");
238             }
239         } else if (IsTypeFuncPtr (NewType) && IsTypeFunc(Expr->Type)) {
240             /* Assignment of function to function pointer is allowed, provided
241              * that both functions have the same parameter list.
242              */
243             if (TypeCmp (Indirect (NewType), Expr->Type) < TC_EQUAL) {
244                 Error ("Incompatible types");
245             }
246         } else {
247             Error ("Incompatible types");
248         }
249
250     } else {
251
252         /* Invalid automatic conversion */
253         Error ("Incompatible types");
254
255     }
256
257     /* Do the actual conversion */
258     DoConversion (Expr, NewType);
259 }
260
261
262
263 void TypeCast (ExprDesc* Expr)
264 /* Handle an explicit cast. The function returns true if the resulting
265  * expression is an lvalue and false if not.
266  */
267 {
268     type    NewType[MAXTYPELEN];
269
270     /* Skip the left paren */
271     NextToken ();
272
273     /* Read the type */
274     ParseType (NewType);
275
276     /* Closing paren */
277     ConsumeRParen ();
278
279     /* Read the expression we have to cast */
280     hie10 (Expr);
281
282     /* Convert functions and arrays to "pointer to" object */
283     DoPtrConversions (Expr);
284
285     /* Convert the value. */
286     DoConversion (Expr, NewType);
287 }
288
289
290
291