From 9481f7a4986e5cc62b067734035554dc5b5019c0 Mon Sep 17 00:00:00 2001 From: mrdudz Date: Fri, 1 Sep 2023 23:43:02 +0200 Subject: [PATCH] fix const int + float --- libsrc/float/readme.md | 10 ++++----- src/cc65/expr.c | 30 ++++++++++++++++++------- test/val/float-basic-intconst-var.c | 35 ++++++++++++++++++----------- 3 files changed, 49 insertions(+), 26 deletions(-) diff --git a/libsrc/float/readme.md b/libsrc/float/readme.md index f40cc473a..82c166fd7 100644 --- a/libsrc/float/readme.md +++ b/libsrc/float/readme.md @@ -87,15 +87,15 @@ each other (not necessarily by me :)): NOT WORKING YET: - /test/val/float-basic-var-var.c +=, -= + /test/val/float-basic-var-var.c +=, -= /test/val/float-basic-const-intvar.c - - /test/val/float-basic-var-intvar.c -, +=, -= + /test/val/float-basic-var-intvar.c -, +=, -= /test/val/float-basic-var-intconst.c -, *, /, +=, -=, *=, /= - /test/val/float-basic-intvar-const.c -, +=, -=, *=, /= - /test/val/float-basic-intvar-var.c -, *, /, /=, += - /test/val/float-basic-intconst-var.c +, -, *, / + /test/val/float-basic-intvar-const.c -, +=, -=, *=, /= + /test/val/float-basic-intvar-var.c -, *, /, +=, /= + /test/val/float-basic-intconst-var.c -, *, / /test/val/float-cmp-const-intvar.c /test/val/float-cmp-intvar-const.c diff --git a/src/cc65/expr.c b/src/cc65/expr.c index 412f3eab7..0e34e41c6 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -3341,19 +3341,22 @@ static void parseadd (ExprDesc* Expr, int DoArrayRef) flags |= typeadjust (Expr, &Expr2, 1); } else if (!DoArrayRef && IsClassFloat (lhst) && IsClassFloat (rhst)) { /* Float const + float var addition */ - LOG(("%s:%d float addition (const + var)\n", __FILE__, __LINE__)); + LOG(("%s:%d parseadd float addition (const + var)\n", __FILE__, __LINE__)); flags |= typeadjust (Expr, &Expr2, 1); #if 1 } else if (!DoArrayRef && IsClassFloat (lhst) && IsClassInt (rhst)) { //printf("const 1\n"); /* Float const + int var addition */ - LOG(("%s:%d float addition (float const + int var)\n", __FILE__, __LINE__)); + LOG(("%s:%d parseadd float addition (float const + int var)\n", __FILE__, __LINE__)); flags |= typeadjust (Expr, &Expr2, 1); #endif } else if (!DoArrayRef && IsClassInt (lhst) && IsClassFloat (rhst)) { -//printf("const 2\n"); /* FIXME: int const + Float var addition */ - LOG(("%s:%d float addition (int const + float var)\n", __FILE__, __LINE__)); + LOG(("%s:%d parseadd float addition (int const + float var)\n", __FILE__, __LINE__)); + RemoveCode (&Mark); + LoadExpr (CF_FLOAT, &Expr2); + //flags |= typeadjust (Expr, &Expr2, 0); + flags |= CF_FLOAT; } else { /* OOPS */ LOG(("%s:%d OOPS\n", __FILE__, __LINE__)); @@ -3393,14 +3396,25 @@ static void parseadd (ExprDesc* Expr, int DoArrayRef) } } else if (ED_IsAbs (Expr)) { /* Numeric constant */ + LOG(("%s:%d parseadd lhs is numeric constant\n", __FILE__, __LINE__)); if (TypeOf (Expr->Type) == CF_FLOAT) { - //FP_D_As32bitRaw() - LOG(("%s:%d const float\n", __FILE__, __LINE__)); + // lhs = float + LOG(("%s:%d parseadd lhs const float\n", __FILE__, __LINE__)); Double res = FP_D_Mul(Expr->V.FVal, FP_D_FromInt(lscale)); g_inc (flags, FP_D_As32bitRaw(res)); } else { - LOG(("%s:%d const ival:%d\n", __FILE__, __LINE__, Expr->IVal)); - g_inc (flags, Expr->IVal * lscale); + // lhs = int + LOG(("%s:%d parseadd lhs const int:%d\n", __FILE__, __LINE__, Expr->IVal)); + if (TypeOf (Expr2.Type) == CF_FLOAT) { + // lhs = int, rhs float + LOG(("%s:%d parseadd rhs float:%08x\n", __FILE__, __LINE__, FP_D_As32bitRaw(FP_D_FromInt(Expr->IVal * lscale)))); + g_inc (flags, FP_D_As32bitRaw(FP_D_FromInt(Expr->IVal * lscale))); + Expr->Type = Expr2.Type; // HACK! + } else { + // lhs = int, rhs int + LOG(("%s:%d parseadd rhs int:%d\n", __FILE__, __LINE__, Expr->IVal)); + g_inc (flags, Expr->IVal * lscale); + } } } else if (lscale == 1) { LOG(("%s:%d addr\n", __FILE__, __LINE__)); diff --git a/test/val/float-basic-intconst-var.c b/test/val/float-basic-intconst-var.c index 948fa845c..64d486948 100644 --- a/test/val/float-basic-intconst-var.c +++ b/test/val/float-basic-intconst-var.c @@ -39,6 +39,7 @@ unsigned char var_char; unsigned int var_int; float var_float; +#if 1 // returns 1 if value in f matches the string // the string is a hex value without leading "0x" int compare(float f, char *str) @@ -71,31 +72,30 @@ void intconstvar(void) { var_float = 11.123f; -#if 0 + fp1 = 47; - printf("fp1:0x%08lx [42687df4] %s (47)\n", *((uint32_t*)&fp1), _ftostr(buf, fp1)); + printf("fp1:0x%08lx [?] %s (47)\n", *((uint32_t*)&fp1), _ftostr(buf, fp1)); fp1 = var_float; - printf("fp1:0x%08lx [42687df4] %s (11.123)\n", *((uint32_t*)&fp1), _ftostr(buf, fp1)); -#endif -#if 0 // Invalid operands for binary operator '+' + printf("fp1:0x%08lx [?] %s (11.123)\n", *((uint32_t*)&fp1), _ftostr(buf, fp1)); + /* addition */ fp1 = 47 + var_float; - printf("fp1:0x%08lx [42687df4] %s (58.123)\n", *((uint32_t*)&fp1), _ftostr(buf, fp1)); + printf("fp1:0x%08lx [?] %s (58.123)", *((uint32_t*)&fp1), _ftostr(buf, fp1)); test1(fp1, "42687df4"); -#endif -#if 0 + +#if 0 // Invalid operands for binary operator '-' fp1 = 47 - var_float; - printf("fp1:0x%08lx [42687df4] %s (58.123)\n", *((uint32_t*)&fp1), _ftostr(buf, fp1)); + printf("fp1:0x%08lx [?] %s (58.123)", *((uint32_t*)&fp1), _ftostr(buf, fp1)); test1(fp1, "42687df4"); #endif -#if 0 +#if 0 // compiles, but wrong result fp1 = 47 * var_float; - printf("fp1:0x%08lx [42687df4] %s (58.123)\n", *((uint32_t*)&fp1), _ftostr(buf, fp1)); + printf("fp1:0x%08lx [?] %s (522.781)", *((uint32_t*)&fp1), _ftostr(buf, fp1)); test1(fp1, "42687df4"); #endif -#if 0 +#if 0 // Internal compiler error fp1 = 47 / var_float; - printf("fp1:0x%08lx [42687df4] %s (58.123)\n", *((uint32_t*)&fp1), _ftostr(buf, fp1)); + printf("fp1:0x%08lx [?] %s (58.123)", *((uint32_t*)&fp1), _ftostr(buf, fp1)); test1(fp1, "42687df4"); #endif } @@ -114,3 +114,12 @@ int main(void) printf("\nfloat-basic-intconst-var (res:%d)\n", result); return result; } +#else +int main(void) +{ + var_float = 11.123f; + fp1 = 47 + var_float; + printf("fp1:0x%08lx [42687df4] %s (58.123)\n", *((uint32_t*)&fp1), _ftostr(buf, fp1)); + return result; +} +#endif