2002-11-01 21:27:31 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* assignment.c */
|
|
|
|
/* */
|
|
|
|
/* Parse assignments */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
2009-08-29 21:20:13 +00:00
|
|
|
/* (C) 2002-2009, Ullrich von Bassewitz */
|
|
|
|
/* Roemerstrasse 52 */
|
|
|
|
/* D-70794 Filderstadt */
|
|
|
|
/* EMail: uz@cc65.org */
|
2002-11-01 21:27:31 +00:00
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
/* */
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
/* */
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
/* distribution. */
|
|
|
|
/* */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* cc65 */
|
2009-08-31 14:59:49 +00:00
|
|
|
#include "asmcode.h"
|
2004-05-25 20:59:38 +00:00
|
|
|
#include "assignment.h"
|
2002-11-01 21:27:31 +00:00
|
|
|
#include "codegen.h"
|
|
|
|
#include "datatype.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "expr.h"
|
2004-06-09 14:12:57 +00:00
|
|
|
#include "loadexpr.h"
|
2004-05-25 20:59:38 +00:00
|
|
|
#include "scanner.h"
|
2004-05-26 13:42:19 +00:00
|
|
|
#include "stdnames.h"
|
2002-11-01 21:27:31 +00:00
|
|
|
#include "typecmp.h"
|
2003-08-11 20:18:30 +00:00
|
|
|
#include "typeconv.h"
|
2002-11-01 21:27:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Code */
|
2002-11-01 21:27:31 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-18 15:30:09 +00:00
|
|
|
static int CopyStruct (ExprDesc* LExpr, ExprDesc* RExpr)
|
2020-07-19 05:47:48 +00:00
|
|
|
/* Copy the struct/union represented by RExpr to the one represented by LExpr */
|
2020-07-18 15:30:09 +00:00
|
|
|
{
|
|
|
|
/* If the size is that of a basic type (char, int, long), we will copy
|
2020-08-13 01:27:53 +00:00
|
|
|
** the struct using the primary register, otherwise we will use memcpy.
|
2020-07-18 15:30:09 +00:00
|
|
|
*/
|
|
|
|
const Type* ltype = LExpr->Type;
|
2020-07-19 05:47:48 +00:00
|
|
|
const Type* stype = GetStructReplacementType (ltype);
|
|
|
|
int UseReg = (stype != ltype);
|
2020-07-18 15:30:09 +00:00
|
|
|
|
|
|
|
if (UseReg) {
|
2020-08-13 01:27:53 +00:00
|
|
|
/* Back up the address of lhs only if it is in the primary */
|
2020-07-18 15:30:09 +00:00
|
|
|
PushAddr (LExpr);
|
|
|
|
} else {
|
2020-08-13 01:27:53 +00:00
|
|
|
/* Push the address of lhs as the destination of memcpy */
|
|
|
|
ED_AddrExpr (LExpr);
|
2020-07-18 15:30:09 +00:00
|
|
|
LoadExpr (CF_NONE, LExpr);
|
|
|
|
g_push (CF_PTR | CF_UNSIGNED, 0);
|
|
|
|
}
|
|
|
|
|
2020-08-13 01:27:53 +00:00
|
|
|
/* Get the expression on the right of the '=' */
|
2020-07-18 15:30:09 +00:00
|
|
|
hie1 (RExpr);
|
|
|
|
|
2020-08-14 00:32:22 +00:00
|
|
|
/* Check for equality of the structs/unions */
|
2021-03-15 08:59:08 +00:00
|
|
|
if (TypeCmp (ltype, RExpr->Type).C < TC_STRICT_COMPATIBLE) {
|
2020-08-02 13:51:32 +00:00
|
|
|
TypeCompatibilityDiagnostic (ltype, RExpr->Type, 1,
|
|
|
|
"Incompatible types in assignment to '%s' from '%s'");
|
2020-07-18 15:30:09 +00:00
|
|
|
}
|
|
|
|
|
2020-08-13 01:27:53 +00:00
|
|
|
/* Do we copy the value directly using the primary? */
|
2020-07-18 15:30:09 +00:00
|
|
|
if (UseReg) {
|
|
|
|
|
2020-08-13 01:27:53 +00:00
|
|
|
/* Check if the value of the rhs is not in the primary yet */
|
|
|
|
if (!ED_IsLocPrimary (RExpr)) {
|
2020-07-18 15:30:09 +00:00
|
|
|
/* Just load the value into the primary as the replacement type. */
|
|
|
|
LoadExpr (TypeOf (stype) | CF_FORCECHAR, RExpr);
|
|
|
|
}
|
|
|
|
|
2020-08-13 01:27:53 +00:00
|
|
|
/* Store it into the location referred in the primary */
|
2020-07-18 15:30:09 +00:00
|
|
|
Store (LExpr, stype);
|
|
|
|
|
2020-08-19 23:56:11 +00:00
|
|
|
/* Value is in primary as an rvalue */
|
|
|
|
ED_FinalizeRValLoad (LExpr);
|
|
|
|
|
2020-07-18 15:30:09 +00:00
|
|
|
} else {
|
|
|
|
|
2020-08-19 23:56:11 +00:00
|
|
|
/* The rhs cannot happen to be loaded in the primary as it is too big */
|
|
|
|
if (!ED_IsLocExpr (RExpr)) {
|
2020-08-13 01:27:53 +00:00
|
|
|
ED_AddrExpr (RExpr);
|
2020-07-18 15:30:09 +00:00
|
|
|
LoadExpr (CF_NONE, RExpr);
|
|
|
|
}
|
|
|
|
|
2020-08-13 01:27:53 +00:00
|
|
|
/* Push the address of the rhs as the source of memcpy */
|
2020-07-18 15:30:09 +00:00
|
|
|
g_push (CF_PTR | CF_UNSIGNED, 0);
|
|
|
|
|
2020-07-19 05:47:48 +00:00
|
|
|
/* Load the size of the struct or union into the primary */
|
2020-08-07 22:46:22 +00:00
|
|
|
g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, SizeOf (ltype), 0);
|
2020-07-18 15:30:09 +00:00
|
|
|
|
|
|
|
/* Call the memcpy function */
|
|
|
|
g_call (CF_FIXARGC, Func_memcpy, 4);
|
2020-08-13 01:27:53 +00:00
|
|
|
|
2020-08-19 23:56:11 +00:00
|
|
|
/* The result is an rvalue referenced in the primary */
|
|
|
|
ED_FinalizeRValLoad (LExpr);
|
|
|
|
|
2020-08-13 01:27:53 +00:00
|
|
|
/* Restore the indirection level of lhs */
|
|
|
|
ED_IndExpr (LExpr);
|
2020-07-18 15:30:09 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 23:56:11 +00:00
|
|
|
/* Clear the tested flag set during loading. This is not neccessary
|
|
|
|
** currently (and probably ever) as a struct/union cannot be converted
|
|
|
|
** to a boolean in C, but there is no harm to be future-proof.
|
|
|
|
*/
|
|
|
|
ED_MarkAsUntested (LExpr);
|
|
|
|
|
2020-08-07 22:46:22 +00:00
|
|
|
return 1;
|
2020-07-18 15:30:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-05-31 15:27:33 +00:00
|
|
|
void Assignment (ExprDesc* Expr)
|
2002-11-01 21:27:31 +00:00
|
|
|
/* Parse an assignment */
|
|
|
|
{
|
2021-04-05 08:40:32 +00:00
|
|
|
const Type* ltype = Expr->Type;
|
2002-11-01 21:27:31 +00:00
|
|
|
|
2020-08-31 12:33:58 +00:00
|
|
|
ExprDesc Expr2;
|
2020-08-19 23:52:06 +00:00
|
|
|
ED_Init (&Expr2);
|
2020-08-31 12:33:58 +00:00
|
|
|
Expr2.Flags |= Expr->Flags & E_MASK_KEEP_SUBEXPR;
|
2002-11-01 21:27:31 +00:00
|
|
|
|
2004-05-25 20:59:38 +00:00
|
|
|
/* We must have an lvalue for an assignment */
|
2004-05-31 15:27:33 +00:00
|
|
|
if (ED_IsRVal (Expr)) {
|
2020-08-02 17:16:00 +00:00
|
|
|
if (IsTypeArray (Expr->Type)) {
|
|
|
|
Error ("Array type '%s' is not assignable", GetFullTypeName (Expr->Type));
|
|
|
|
} else if (IsTypeFunc (Expr->Type)) {
|
|
|
|
Error ("Function type '%s' is not assignable", GetFullTypeName (Expr->Type));
|
|
|
|
} else {
|
|
|
|
Error ("Assignment to rvalue");
|
|
|
|
}
|
2004-05-25 20:59:38 +00:00
|
|
|
}
|
|
|
|
|
2002-11-01 21:27:31 +00:00
|
|
|
/* Check for assignment to const */
|
|
|
|
if (IsQualConst (ltype)) {
|
2013-05-09 11:56:54 +00:00
|
|
|
Error ("Assignment to const");
|
2002-11-01 21:27:31 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 21:57:12 +00:00
|
|
|
/* Check for assignment to incomplete type */
|
|
|
|
if (IsIncompleteESUType (ltype)) {
|
|
|
|
Error ("Assignment to incomplete type '%s'", GetFullTypeName (ltype));
|
|
|
|
}
|
|
|
|
|
2004-05-25 20:59:38 +00:00
|
|
|
/* Skip the '=' token */
|
|
|
|
NextToken ();
|
|
|
|
|
2020-07-19 05:47:48 +00:00
|
|
|
/* cc65 does not have full support for handling structs or unions. Since
|
|
|
|
** assigning structs is one of the more useful operations from this family,
|
|
|
|
** allow it here.
|
2021-02-15 10:37:01 +00:00
|
|
|
** Note: IsClassStruct() is also true for union types.
|
2014-06-30 09:10:35 +00:00
|
|
|
*/
|
2002-11-01 21:27:31 +00:00
|
|
|
if (IsClassStruct (ltype)) {
|
2020-07-19 05:47:48 +00:00
|
|
|
/* Copy the struct or union by value */
|
2020-07-18 15:30:09 +00:00
|
|
|
CopyStruct (Expr, &Expr2);
|
2002-11-01 21:27:31 +00:00
|
|
|
|
2009-08-29 21:20:13 +00:00
|
|
|
} else if (ED_IsBitField (Expr)) {
|
|
|
|
|
2009-08-31 14:59:49 +00:00
|
|
|
CodeMark AndPos;
|
|
|
|
CodeMark PushPos;
|
|
|
|
|
2009-08-29 21:20:13 +00:00
|
|
|
unsigned Mask;
|
2009-08-30 09:01:04 +00:00
|
|
|
unsigned Flags;
|
|
|
|
|
|
|
|
/* If the bit-field fits within one byte, do the following operations
|
2014-06-30 09:10:35 +00:00
|
|
|
** with bytes.
|
|
|
|
*/
|
2009-08-30 09:01:04 +00:00
|
|
|
if (Expr->BitOffs / CHAR_BITS == (Expr->BitOffs + Expr->BitWidth - 1) / CHAR_BITS) {
|
|
|
|
Expr->Type = type_uchar;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine code generator flags */
|
|
|
|
Flags = TypeOf (Expr->Type);
|
2009-08-29 21:20:13 +00:00
|
|
|
|
|
|
|
/* Assignment to a bit field. Get the address on stack for the store. */
|
|
|
|
PushAddr (Expr);
|
|
|
|
|
2009-08-30 09:01:04 +00:00
|
|
|
/* Load the value from the location */
|
2009-08-29 21:20:13 +00:00
|
|
|
Expr->Flags &= ~E_BITFIELD;
|
|
|
|
LoadExpr (CF_NONE, Expr);
|
|
|
|
|
|
|
|
/* Mask unwanted bits */
|
|
|
|
Mask = (0x0001U << Expr->BitWidth) - 1U;
|
2009-08-31 14:59:49 +00:00
|
|
|
GetCodePos (&AndPos);
|
2009-08-29 21:20:13 +00:00
|
|
|
g_and (Flags | CF_CONST, ~(Mask << Expr->BitOffs));
|
|
|
|
|
|
|
|
/* Push it on stack */
|
2009-08-31 14:59:49 +00:00
|
|
|
GetCodePos (&PushPos);
|
2009-08-29 21:20:13 +00:00
|
|
|
g_push (Flags, 0);
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Read the expression on the right side of the '=' */
|
|
|
|
MarkedExprWithCheck (hie1, &Expr2);
|
2009-08-29 21:20:13 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Do type conversion if necessary. Beware: Do not use char type
|
2014-06-30 09:10:35 +00:00
|
|
|
** here!
|
|
|
|
*/
|
2013-05-09 11:56:54 +00:00
|
|
|
TypeConversion (&Expr2, ltype);
|
2009-08-29 21:20:13 +00:00
|
|
|
|
2009-09-07 14:22:27 +00:00
|
|
|
/* Special treatment if the value is constant. */
|
|
|
|
/* Beware: Expr2 may contain side effects, so there must not be
|
2014-06-30 09:10:35 +00:00
|
|
|
** code generated for Expr2.
|
|
|
|
*/
|
2009-09-08 19:49:30 +00:00
|
|
|
if (ED_IsConstAbsInt (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
|
2009-08-31 14:59:49 +00:00
|
|
|
|
|
|
|
/* Get the value and apply the mask */
|
|
|
|
unsigned Val = (unsigned) (Expr2.IVal & Mask);
|
2009-08-29 21:20:13 +00:00
|
|
|
|
2009-08-31 14:59:49 +00:00
|
|
|
/* Since we will do the OR with a constant, we can remove the push */
|
|
|
|
RemoveCode (&PushPos);
|
|
|
|
|
|
|
|
/* If the value is equal to the mask now, all bits are one, and we
|
2014-06-30 09:10:35 +00:00
|
|
|
** can remove the mask operation from above.
|
|
|
|
*/
|
2009-08-31 14:59:49 +00:00
|
|
|
if (Val == Mask) {
|
|
|
|
RemoveCode (&AndPos);
|
|
|
|
}
|
2009-08-29 21:20:13 +00:00
|
|
|
|
2009-08-31 14:59:49 +00:00
|
|
|
/* Generate the or operation */
|
|
|
|
g_or (Flags | CF_CONST, Val << Expr->BitOffs);
|
2009-08-29 21:20:13 +00:00
|
|
|
|
2009-08-31 14:59:49 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
/* If necessary, load the value into the primary register */
|
|
|
|
LoadExpr (CF_NONE, &Expr2);
|
|
|
|
|
|
|
|
/* Apply the mask */
|
|
|
|
g_and (Flags | CF_CONST, Mask);
|
|
|
|
|
|
|
|
/* Shift it into the right position */
|
|
|
|
g_asl (Flags | CF_CONST, Expr->BitOffs);
|
|
|
|
|
|
|
|
/* Or both values */
|
|
|
|
g_or (Flags, 0);
|
|
|
|
}
|
2009-08-29 21:20:13 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Generate a store instruction */
|
|
|
|
Store (Expr, 0);
|
2009-08-29 21:20:13 +00:00
|
|
|
|
2009-08-30 09:01:04 +00:00
|
|
|
/* Restore the expression type */
|
|
|
|
Expr->Type = ltype;
|
|
|
|
|
2020-08-19 23:56:11 +00:00
|
|
|
/* Value is in primary as an rvalue */
|
|
|
|
ED_FinalizeRValLoad (Expr);
|
|
|
|
|
2002-11-01 21:27:31 +00:00
|
|
|
} else {
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Get the address on stack if needed */
|
|
|
|
PushAddr (Expr);
|
2002-11-01 21:27:31 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Read the expression on the right side of the '=' */
|
|
|
|
hie1 (&Expr2);
|
2003-08-11 20:18:30 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Do type conversion if necessary */
|
|
|
|
TypeConversion (&Expr2, ltype);
|
2003-08-11 20:18:30 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* If necessary, load the value into the primary register */
|
|
|
|
LoadExpr (CF_NONE, &Expr2);
|
2002-11-01 21:27:31 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Generate a store instruction */
|
|
|
|
Store (Expr, 0);
|
2002-11-01 21:27:31 +00:00
|
|
|
|
2020-08-19 23:56:11 +00:00
|
|
|
/* Value is in primary as an rvalue */
|
|
|
|
ED_FinalizeRValLoad (Expr);
|
2002-11-01 21:27:31 +00:00
|
|
|
|
2020-08-19 23:56:11 +00:00
|
|
|
}
|
2002-11-01 21:27:31 +00:00
|
|
|
}
|