]> git.sur5r.net Git - cc65/blob - src/cc65/assignment.c
af414ff50e76e2902c7b4c40e567a462125271c9
[cc65] / src / cc65 / assignment.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                               assignment.c                                */
4 /*                                                                           */
5 /*                             Parse assignments                             */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2002-2009, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 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 /* cc65 */
37 #include "assignment.h"
38 #include "codegen.h"
39 #include "datatype.h"
40 #include "error.h"
41 #include "expr.h"
42 #include "loadexpr.h"
43 #include "scanner.h"
44 #include "stdnames.h"
45 #include "typecmp.h"
46 #include "typeconv.h"
47
48
49
50 /*****************************************************************************/
51 /*                                   Code                                    */
52 /*****************************************************************************/
53
54
55
56 void Assignment (ExprDesc* Expr)
57 /* Parse an assignment */
58 {
59     ExprDesc Expr2;
60     Type* ltype = Expr->Type;
61
62
63     /* We must have an lvalue for an assignment */
64     if (ED_IsRVal (Expr)) {
65         Error ("Invalid lvalue in assignment");
66     }
67
68     /* Check for assignment to const */
69     if (IsQualConst (ltype)) {
70         Error ("Assignment to const");
71     }
72
73     /* Skip the '=' token */
74     NextToken ();
75
76     /* cc65 does not have full support for handling structs by value. Since
77      * assigning structs is one of the more useful operations from this
78      * family, allow it here.
79      */
80     if (IsClassStruct (ltype)) {
81
82         /* Get the size of the left hand side. */
83         unsigned Size = SizeOf (ltype);
84
85         /* If the size is that of a basic type (char, int, long), we will copy
86          * the struct using the primary register, otherwise we use memcpy. In
87          * the former case, push the address only if really needed.
88          */
89         int UseReg = 1;
90         Type* stype;
91         switch (Size) {
92             case SIZEOF_CHAR:   stype = type_uchar;             break;
93             case SIZEOF_INT:    stype = type_uint;              break;
94             case SIZEOF_LONG:   stype = type_ulong;             break;
95             default:            stype = ltype; UseReg = 0;      break;
96         }
97         if (UseReg) {
98             PushAddr (Expr);
99         } else {
100             ED_MakeRVal (Expr);
101             LoadExpr (CF_NONE, Expr);
102             g_push (CF_PTR | CF_UNSIGNED, 0);
103         }
104
105         /* Get the expression on the right of the '=' into the primary */
106         hie1 (&Expr2);
107
108         /* Check for equality of the structs */
109         if (TypeCmp (ltype, Expr2.Type) < TC_STRICT_COMPATIBLE) {
110             Error ("Incompatible types");
111         }
112
113         /* Check if the right hand side is an lvalue */
114         if (ED_IsLVal (&Expr2)) {
115             /* We have an lvalue. Do we copy using the primary? */
116             if (UseReg) {
117                 /* Just use the replacement type */
118                 Expr2.Type = stype;
119
120                 /* Load the value into the primary */
121                 LoadExpr (CF_FORCECHAR, &Expr2);
122
123                 /* Store it into the new location */
124                 Store (Expr, stype);
125
126             } else {
127
128                 /* We will use memcpy. Push the address of the rhs */
129                 ED_MakeRVal (&Expr2);
130                 LoadExpr (CF_NONE, &Expr2);
131
132                 /* Push the address (or whatever is in ax in case of errors) */
133                 g_push (CF_PTR | CF_UNSIGNED, 0);
134
135                 /* Load the size of the struct into the primary */
136                 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, CheckedSizeOf (ltype), 0);
137
138                 /* Call the memcpy function */
139                 g_call (CF_FIXARGC, Func_memcpy, 4);
140             }
141
142         } else {
143
144             /* We have an rvalue. This can only happen if a function returns
145              * a struct, since there is no other way to generate an expression
146              * that as a struct as an rvalue result. We allow only 1, 2, and 4
147              * byte sized structs and do direct assignment.
148              */
149             if (UseReg) {
150                 /* Do the store */
151                 Store (Expr, stype);
152             } else {
153                 /* Print a diagnostic */
154                 Error ("Structs of this size are not supported");
155                 /* Adjust the stack so we won't run in an internal error later */
156                 pop (CF_PTR);
157             }
158
159         }
160
161     } else if (ED_IsBitField (Expr)) {
162
163         unsigned Mask;
164         unsigned Flags;
165
166         /* If the bit-field fits within one byte, do the following operations
167          * with bytes.
168          */
169         if (Expr->BitOffs / CHAR_BITS == (Expr->BitOffs + Expr->BitWidth - 1) / CHAR_BITS) {
170             Expr->Type = type_uchar;
171         }
172
173         /* Determine code generator flags */
174         Flags = TypeOf (Expr->Type);
175
176         /* Assignment to a bit field. Get the address on stack for the store. */
177         PushAddr (Expr);
178
179         /* Load the value from the location */
180         Expr->Flags &= ~E_BITFIELD;
181         LoadExpr (CF_NONE, Expr);
182
183         /* Mask unwanted bits */
184         Mask = (0x0001U << Expr->BitWidth) - 1U;
185         g_and (Flags | CF_CONST, ~(Mask << Expr->BitOffs));
186
187         /* Push it on stack */
188         g_push (Flags, 0);
189
190         /* Read the expression on the right side of the '=' */
191         hie1 (&Expr2);
192
193         /* Do type conversion if necessary. Beware: Do not use char type 
194          * here!
195          */
196         TypeConversion (&Expr2, ltype);
197
198         /* If necessary, load the value into the primary register */
199         LoadExpr (CF_NONE, &Expr2);
200
201         /* Apply the mask */
202         g_and (Flags | CF_CONST, Mask);
203
204         /* Shift it into the right position */
205         g_asl (Flags | CF_CONST, Expr->BitOffs);
206
207         /* Or both values */
208         g_or (Flags, 0);
209
210         /* Generate a store instruction */
211         Store (Expr, 0);
212
213         /* Restore the expression type */
214         Expr->Type = ltype;
215
216     } else {
217
218         /* Get the address on stack if needed */
219         PushAddr (Expr);
220
221         /* Read the expression on the right side of the '=' */
222         hie1 (&Expr2);
223
224         /* Do type conversion if necessary */
225         TypeConversion (&Expr2, ltype);
226
227         /* If necessary, load the value into the primary register */
228         LoadExpr (CF_NONE, &Expr2);
229
230         /* Generate a store instruction */
231         Store (Expr, 0);
232
233     }
234
235     /* Value is still in primary and not an lvalue */
236     ED_MakeRValExpr (Expr);
237 }
238
239
240