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