1 /*****************************************************************************/
5 /* Parse assignments */
9 /* (C) 2002-2006 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
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 /*****************************************************************************/
37 #include "assignment.h"
50 /*****************************************************************************/
52 /*****************************************************************************/
56 void Assignment (ExprDesc* Expr)
57 /* Parse an assignment */
60 Type* ltype = Expr->Type;
63 /* We must have an lvalue for an assignment */
64 if (ED_IsRVal (Expr)) {
65 Error ("Invalid lvalue in assignment");
68 /* Check for assignment to const */
69 if (IsQualConst (ltype)) {
70 Error ("Assignment to const");
73 /* Skip the '=' token */
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.
80 if (IsClassStruct (ltype)) {
82 /* Get the size of the left hand side. */
83 unsigned Size = SizeOf (ltype);
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.
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;
100 LoadExpr (CF_NONE, Expr);
101 g_push (CF_PTR | CF_UNSIGNED, 0);
104 /* Get the expression on the right of the '=' into the primary */
107 /* Check for equality of the structs */
108 if (TypeCmp (ltype, Expr2.Type) < TC_STRICT_COMPATIBLE) {
109 Error ("Incompatible types");
112 /* Check if the right hand side is an lvalue */
113 if (ED_IsLVal (&Expr2)) {
114 /* We have an lvalue. Do we copy using the primary? */
116 /* Just use the replacement type */
119 /* Load the value into the primary */
120 LoadExpr (CF_FORCECHAR, &Expr2);
122 /* Store it into the new location */
127 /* We will use memcpy. Push the address of the rhs */
128 ED_MakeRVal (&Expr2);
129 LoadExpr (CF_NONE, &Expr2);
131 /* Push the address (or whatever is in ax in case of errors) */
132 g_push (CF_PTR | CF_UNSIGNED, 0);
134 /* Load the size of the struct into the primary */
135 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, CheckedSizeOf (ltype), 0);
137 /* Call the memcpy function */
138 g_call (CF_FIXARGC, Func_memcpy, 4);
143 /* We have an rvalue. This can only happen if a function returns
144 * a struct, since there is no other way to generate an expression
145 * that as a struct as an rvalue result. We allow only 1, 2, and 4
146 * byte sized structs and do direct assignment.
152 /* Print a diagnostic */
153 Error ("Structs of this size are not supported");
154 /* Adjust the stack so we won't run in an internal error later */
162 /* Get the address on stack if needed */
165 /* Read the expression on the right side of the '=' */
168 /* Do type conversion if necessary */
169 TypeConversion (&Expr2, ltype);
171 /* If necessary, load the value into the primary register */
172 LoadExpr (CF_NONE, &Expr2);
174 /* Generate a store instruction */
179 /* Value is still in primary and not an lvalue */
180 ED_MakeRValExpr (Expr);