]> git.sur5r.net Git - cc65/blob - src/cc65/assignment.c
Fixed a bug
[cc65] / src / cc65 / assignment.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                               assignment.c                                */
4 /*                                                                           */
5 /*                             Parse assignments                             */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2002      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
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 "codegen.h"
38 #include "datatype.h"
39 #include "error.h"
40 #include "expr.h"
41 #include "typecmp.h"
42 #include "assignment.h"
43
44
45
46 /*****************************************************************************/
47 /*                                   Code                                    */
48 /*****************************************************************************/
49
50
51
52 void Assignment (ExprDesc* lval)
53 /* Parse an assignment */
54 {
55     int k;
56     ExprDesc lval2;
57     type* ltype = lval->Type;
58
59
60     /* Check for assignment to const */
61     if (IsQualConst (ltype)) {
62         Error ("Assignment to const");
63     }
64
65     /* cc65 does not have full support for handling structs by value. Since
66      * assigning structs is one of the more useful operations from this
67      * family, allow it here.
68      */
69     if (IsClassStruct (ltype)) {
70
71         /* Get the size of the left hand side. */
72         unsigned Size = SizeOf (ltype);
73
74         /* If the size is that of a basic type (char, int, long), we will copy
75          * the struct using the primary register, otherwise we use memcpy. In
76          * the former case, push the address only if really needed.
77          */
78         int UseReg = 1;
79         type* stype;
80         switch (Size) {
81             case SIZEOF_CHAR:   stype = type_uchar;             break;
82             case SIZEOF_INT:    stype = type_uint;              break;
83             case SIZEOF_LONG:   stype = type_ulong;             break;
84             default:            stype = ltype; UseReg = 0;      break;
85         }
86         if (UseReg) {
87             PushAddr (lval);
88         } else {
89             exprhs (0, 0, lval);
90             g_push (CF_PTR | CF_UNSIGNED, 0);
91         }
92
93         /* Get the expression on the right of the '=' into the primary */
94         k = hie1 (&lval2);
95
96         /* Check for equality of the structs */
97         if (TypeCmp (ltype, lval2.Type) < TC_STRICT_COMPATIBLE) {
98             Error ("Incompatible types");
99         }
100
101         /* Check if the right hand side is an lvalue */
102         if (k) {
103             /* We have an lvalue. Do we copy using the primary? */
104             if (UseReg) {
105                 /* Just use the replacement type */
106                 lval2.Type = stype;
107
108                 /* Load the value into the primary */
109                 exprhs (CF_FORCECHAR, k, &lval2);
110
111                 /* Store it into the new location */
112                 Store (lval, stype);
113
114             } else {
115
116                 /* We will use memcpy. Push the address of the rhs */
117                 exprhs (0, 0, &lval2);
118
119                 /* Push the address (or whatever is in ax in case of errors) */
120                 g_push (CF_PTR | CF_UNSIGNED, 0);
121
122                 /* Load the size of the struct into the primary */
123                 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, CheckedSizeOf (ltype), 0);
124
125                 /* Call the memcpy function */
126                 g_call (CF_FIXARGC, "memcpy", 4);
127             }
128
129         } else {
130
131             /* We have an rvalue. This can only happen if a function returns
132              * a struct, since there is no other way to generate an expression
133              * that as a struct as an rvalue result. We allow only 1, 2, and 4
134              * byte sized structs and do direct assignment.
135              */
136             if (UseReg) {
137                 /* Do the store */
138                 Store (lval, stype);
139             } else {
140                 /* Print a diagnostic */
141                 Error ("Structs of this size are not supported");
142                 /* Adjust the stack so we won't run in an internal error later */
143                 pop (CF_PTR);
144             }
145
146         }
147
148     } else {
149
150         /* Get the address on stack if needed */
151         PushAddr (lval);
152
153         /* Get the expression on the right of the '=' into the primary */
154         if (evalexpr (CF_NONE, hie1, &lval2) == 0) {
155             /* Constant expression. Adjust the types */
156             assignadjust (ltype, &lval2);
157             /* Put the value into the primary register */
158             exprhs (CF_NONE, 0, &lval2);
159         } else {
160             /* Expression is not constant and already in the primary */
161             assignadjust (ltype, &lval2);
162         }
163
164         /* Generate a store instruction */
165         Store (lval, 0);
166
167     }
168
169     /* Value is still in primary */
170     lval->Flags = E_MEXPR;
171 }
172
173
174