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
|
|
|
|
** the struct using the primary register, otherwise we use memcpy. In
|
|
|
|
** the former case, push the address only if really needed.
|
|
|
|
*/
|
|
|
|
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) {
|
|
|
|
PushAddr (LExpr);
|
|
|
|
} else {
|
|
|
|
ED_MarkExprAsRVal (LExpr);
|
|
|
|
LoadExpr (CF_NONE, LExpr);
|
|
|
|
g_push (CF_PTR | CF_UNSIGNED, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the expression on the right of the '=' into the primary */
|
|
|
|
hie1 (RExpr);
|
|
|
|
|
|
|
|
/* Check for equality of the structs */
|
|
|
|
if (TypeCmp (ltype, RExpr->Type) < TC_STRICT_COMPATIBLE) {
|
|
|
|
Error ("Incompatible types");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do we copy using the primary? */
|
|
|
|
if (UseReg) {
|
|
|
|
|
|
|
|
/* Check if the right hand side is an lvalue */
|
|
|
|
if (ED_IsLVal (RExpr)) {
|
|
|
|
/* Just load the value into the primary as the replacement type. */
|
|
|
|
LoadExpr (TypeOf (stype) | CF_FORCECHAR, RExpr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store it into the new location */
|
|
|
|
Store (LExpr, stype);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* Check if the right hand side is an lvalue */
|
|
|
|
if (ED_IsLVal (RExpr)) {
|
|
|
|
/* We will use memcpy. Push the address of the rhs */
|
|
|
|
ED_MarkExprAsRVal (RExpr);
|
|
|
|
LoadExpr (CF_NONE, RExpr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Push the address (or whatever is in ax in case of errors) */
|
|
|
|
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-07-18 15:30:09 +00:00
|
|
|
g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, CheckedSizeOf (ltype), 0);
|
|
|
|
|
|
|
|
/* Call the memcpy function */
|
|
|
|
g_call (CF_FIXARGC, Func_memcpy, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-05-31 15:27:33 +00:00
|
|
|
void Assignment (ExprDesc* Expr)
|
2002-11-01 21:27:31 +00:00
|
|
|
/* Parse an assignment */
|
|
|
|
{
|
2004-06-09 14:12:57 +00:00
|
|
|
ExprDesc Expr2;
|
2006-02-19 12:29:37 +00:00
|
|
|
Type* ltype = Expr->Type;
|
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)) {
|
2004-05-25 20:59:38 +00:00
|
|
|
Error ("Invalid lvalue in assignment");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
** 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;
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
2003-08-11 20:18:30 +00:00
|
|
|
/* Value is still in primary and not an lvalue */
|
2020-07-15 12:22:28 +00:00
|
|
|
ED_FinalizeRValLoad (Expr);
|
2002-11-01 21:27:31 +00:00
|
|
|
}
|