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