From 6f004dadf76d882e4d74dbf05739c5c448b6cf84 Mon Sep 17 00:00:00 2001 From: mrdudz Date: Fri, 1 Sep 2023 22:15:52 +0200 Subject: [PATCH] fix float const -/ int const --- src/cc65/expr.c | 72 ++++++++++++++++++++++++--- test/val/float-basic-const-const.c | 8 +-- test/val/float-basic-const-intconst.c | 45 ++++++++--------- test/val/float-basic-intconst-const.c | 23 ++++----- 4 files changed, 102 insertions(+), 46 deletions(-) diff --git a/src/cc65/expr.c b/src/cc65/expr.c index 2fb4d788f..412f3eab7 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -3137,6 +3137,7 @@ static void parseadd (ExprDesc* Expr, int DoArrayRef) ** - (integer)(base + offset) * 1 + (pointer)numeric */ if (ED_IsQuasiConst (Expr)) { + LOG(("parseadd lhs is const\n")); /* The left hand side is a constant of some sort. Good. Get rhs */ ExprWithCheck (DoArrayRef ? hie0 : hie9, &Expr2); @@ -3144,6 +3145,7 @@ static void parseadd (ExprDesc* Expr, int DoArrayRef) /* Right hand side is constant. Get the rhs type */ rhst = Expr2.Type; if (ED_IsQuasiConst (&Expr2)) { + LOG(("parseadd lhs and rhs are const\n")); /* Both expressions are constants. Check for pointer arithmetic */ if (IsClassPtr (lhst) && IsClassInt (rhst)) { @@ -3161,9 +3163,23 @@ static void parseadd (ExprDesc* Expr, int DoArrayRef) flags = CF_INT; } else if (!DoArrayRef && IsClassFloat (lhst) && IsClassFloat (rhst)) { /* FIXME: float addition (const + const) */ - LOG(("%s:%d float addition (const + const)\n", __FILE__, __LINE__)); + LOG(("%s:%d float addition (float const + float const)\n", __FILE__, __LINE__)); /* Integer addition */ flags = CF_FLOAT; +#if 1 + } else if (!DoArrayRef && IsClassFloat (lhst) && IsClassInt (rhst)) { + /* FIXME: float addition (const + const int) */ + LOG(("%s:%d float addition (float const + const int)\n", __FILE__, __LINE__)); + /* Integer addition */ + flags = CF_FLOAT; +#endif +#if 1 + } else if (!DoArrayRef && IsClassInt (lhst) && IsClassFloat (rhst)) { + /* FIXME: float addition (const int + const) */ + LOG(("%s:%d float addition (const int + float const)\n", __FILE__, __LINE__)); + /* Integer addition */ + flags = CF_FLOAT; +#endif } else { LOG(("%s:%d OOPS\n", __FILE__, __LINE__)); /* OOPS */ @@ -3181,7 +3197,27 @@ static void parseadd (ExprDesc* Expr, int DoArrayRef) Expr->Type = type_float; //Expr->Type = ArithmeticConvert (Expr->Type, Expr2.Type); AddDone = 1; - LOG(("%s:%d float addition (const + const) res:%f\n", __FILE__, __LINE__, Expr->V.FVal.V)); + LOG(("%s:%d parseadd (float const + float const) res:%f\n", __FILE__, __LINE__, Expr->V.FVal.V)); +#if 1 + } else if (!DoArrayRef && IsClassFloat (lhst) && IsClassInt (rhst)) { + /* float + int */ + /* FIXME: float - this is probably completely wrong */ + Expr->V.FVal.V = Expr->V.FVal.V + Expr2.IVal; + Expr->Type = type_float; + //Expr->Type = ArithmeticConvert (Expr->Type, Expr2.Type); + AddDone = 1; + LOG(("%s:%d parseadd (float const + int const) res:%f\n", __FILE__, __LINE__, Expr->V.FVal.V)); +#endif +#if 1 + } else if (!DoArrayRef && IsClassInt (lhst) && IsClassFloat (rhst)) { + /* int + float */ + /* FIXME: float - this is probably completely wrong */ + Expr->V.FVal.V = Expr->IVal + Expr2.V.FVal.V; + Expr->Type = type_float; + //Expr->Type = ArithmeticConvert (Expr->Type, Expr2.Type); + AddDone = 1; + LOG(("%s:%d parseadd (int const + float const) res:%f\n", __FILE__, __LINE__, Expr->V.FVal.V)); +#endif } else { /* integer + integer */ if (ED_IsAbs (&Expr2) && @@ -3764,9 +3800,13 @@ static void parsesub (ExprDesc* Expr) } else if (ED_IsQuasiConst (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) { + LOG(("parsesub: rhs is const\n")); + /* Right hand side is constant. Check left hand side. */ if (ED_IsQuasiConst (Expr)) { + LOG(("parsesub: lhs and rhs are const\n")); + /* Both sides are constant. Check for pointer arithmetic */ if (IsClassPtr (lhst) && IsClassInt (rhst)) { /* Pointer subtraction. We've got the scale factor and flags above */ @@ -3784,10 +3824,9 @@ static void parsesub (ExprDesc* Expr) /* OOPS */ Error ("Invalid operands for binary operator '-'"); } -#if 1 - if (!IsClassFloat (lhst) && !IsClassFloat (rhst)) -#endif - { + + if (!IsClassFloat (lhst) && !IsClassFloat (rhst)) { + LOG(("parsesub const int - const int\n")); /* We can't make all subtraction expressions constant */ if (ED_IsConstAbs (&Expr2)) { Expr->IVal -= Expr2.IVal * rscale; @@ -3801,6 +3840,27 @@ static void parsesub (ExprDesc* Expr) /* No runtime code */ SubDone = 1; } + } else if (IsClassFloat (lhst) && IsClassInt (rhst)) { + LOG(("parsesub const float - const int\n")); + if (ED_IsConstAbs (&Expr2)) { + Expr->V.FVal = FP_D_Sub(Expr->V.FVal, FP_D_FromInt(Expr2.IVal)); + /* No runtime code */ + SubDone = 1; + } + } else if (IsClassInt (lhst) && IsClassFloat (rhst)) { + LOG(("parsesub const int - const float\n")); + if (ED_IsConstAbs (&Expr2)) { + Expr->V.FVal = FP_D_Sub(FP_D_FromInt(Expr->IVal), Expr2.V.FVal); + /* No runtime code */ + SubDone = 1; + } + } else if (IsClassFloat (lhst) && IsClassFloat (rhst)) { + LOG(("FIXME: parsesub const float - const float\n")); + if (ED_IsConstAbs (&Expr2)) { + Expr->V.FVal = FP_D_Sub(Expr->V.FVal, Expr2.V.FVal); + /* No runtime code */ + SubDone = 1; + } } if (SubDone) { diff --git a/test/val/float-basic-const-const.c b/test/val/float-basic-const-const.c index f7d641f4d..8bcb9f610 100644 --- a/test/val/float-basic-const-const.c +++ b/test/val/float-basic-const-const.c @@ -81,11 +81,11 @@ void constconst(void) test1(fp3, "bdcccccd"); fp3 = 0.3f - 0.1f; - printf("fp3:0x%08lx [0xbdcccccd] %s (0.2)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); - test1(fp3, "3e4cccce"); + printf("fp3:0x%08lx [0x3e4ccccd] %s (0.2)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); + test1(fp3, "3e4ccccd"); fp3 = 0.1f - 0.3f; - printf("fp3:0x%08lx [0xbdcccccd] %s (-0.2)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); - test1(fp3, "be4cccce"); + printf("fp3:0x%08lx [0xbe4ccccd] %s (-0.2)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); + test1(fp3, "be4ccccd"); // multiplication printf("\nconstant * constant\n\n"); diff --git a/test/val/float-basic-const-intconst.c b/test/val/float-basic-const-intconst.c index 145870d32..4eceb3b28 100644 --- a/test/val/float-basic-const-intconst.c +++ b/test/val/float-basic-const-intconst.c @@ -34,6 +34,7 @@ unsigned long var_ulong; int result = 0; +#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) @@ -59,47 +60,35 @@ void constintconst(void) printf("\n*** float constant vs int constant\n\n"); // addition -#if 0 // Error: Invalid operands for binary operator '+' printf("\nconstant + constant\n\n"); fp1 = 0.1f; fp2 = 0.2f; - fp3 = 0.1f + 2; + fp3 = 0.2f + 2; printf(" 0x%08lx [0x3dcccccd] %s (0.1)\n", *((uint32_t*)&fp1), _ftostr(buf, fp1)); printf(" 0x%08lx [0x3e4ccccd] %s (0.2)\n", *((uint32_t*)&fp2), _ftostr(buf, fp2)); - printf("fp3:0x%08lx [0x3e99999a] %s (0.3)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); - test1(fp3, "3e99999a"); -#endif + printf("fp3:0x%08lx [0x400ccccd] %s (2.2)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); + test1(fp3, "400ccccd"); -#if 0 // compiles, but gives wrong results // substraction printf("\nconstant - constant\n\n"); fp1 = 0.1f; fp2 = 0.2f; fp3 = 0.1f - 2; - // printf(" 0x%08lx [0x3dcccccd] %s (0.1)\n", *((uint32_t*)&fp1), _ftostr(buf, fp1)); - // printf(" 0x%08lx [0x3e4ccccd] %s (0.2)\n", *((uint32_t*)&fp2), _ftostr(buf, fp2)); - - printf("fp3:0x%08lx [0xbdcccccd] %s (-1.9)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); - test1(fp3, "bdcccccd"); + printf("fp3:0x%08lx [0xbff33333] %s (-1.9)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); + test1(fp3, "bff33333"); fp3 = 0.3f - 1; - printf("fp3:0x%08lx [0xbdcccccd] %s (-0.7)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); - test1(fp3, "3e4cccce"); - fp3 = 0.1f - 3; - printf("fp3:0x%08lx [0xbdcccccd] %s (-2.7)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); - test1(fp3, "be4cccce"); -#endif + printf("fp3:0x%08lx [0xbf333333] %s (-0.7)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); + test1(fp3, "bf333333"); + // multiplication printf("\nconstant * constant\n\n"); fp1 = 0.1f; fp2 = 0.2f; fp3 = 0.1f * 5; - // printf(" 0x%08lx [0x3dcccccd] %s (0.1)\n", *((uint32_t*)&fp1), _ftostr(buf, fp1)); - // printf(" 0x%08lx [0x3e4ccccd] %s (0.2)\n", *((uint32_t*)&fp2), _ftostr(buf, fp2)); - printf("fp3:0x%08lx [0x3f000000] %s (0.5)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); test1(fp3, "3f000000"); @@ -109,9 +98,6 @@ void constintconst(void) fp2 = 0.2f; fp3 = 0.1f / 2; - // printf(" 0x%08lx [0x3dcccccd] %s (0.1)\n", *((uint32_t*)&fp1), _ftostr(buf, fp1)); - // printf(" 0x%08lx [0x3e4ccccd] %s (0.2)\n", *((uint32_t*)&fp2), _ftostr(buf, fp2)); - printf("fp3:0x%08lx [0x3d4ccccd] %s (0.05)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); test1(fp3, "3d4ccccd"); } @@ -130,3 +116,16 @@ int main(void) printf("\nfloat-basic-const-intconst (res:%d)\n", result); return result; } +#else +int main(void) +{ + fp1 = 0.1f; + fp2 = 0.2f; + fp3 = 0.1f - 2; // 2.1 + + printf(" 0x%08lx [0x3dcccccd] %s (0.1)\n", *((uint32_t*)&fp1), _ftostr(buf, fp1)); + printf(" 0x%08lx [0x3e4ccccd] %s (0.2)\n", *((uint32_t*)&fp2), _ftostr(buf, fp2)); + printf("fp3:0x%08lx [?] %s (2.1)\n", *((uint32_t*)&fp3), _ftostr(buf, fp3)); + return result; +} +#endif diff --git a/test/val/float-basic-intconst-const.c b/test/val/float-basic-intconst-const.c index f6af2cd0f..83ed36190 100644 --- a/test/val/float-basic-intconst-const.c +++ b/test/val/float-basic-intconst-const.c @@ -57,20 +57,18 @@ void test1(float f, char *str) void intconstconst(void) { printf("\n*** int constant vs constant\n\n"); -#if 0 + // addition printf("\nconstant + constant\n\n"); fp1 = 0.1f; fp2 = 5; - fp3 = 5 + 0.2f; + fp3 = 5 + 0.5f; printf(" 0x%08lx [0x3dcccccd] %s (0.1)\n", *((uint32_t*)&fp1), _ftostr(buf, fp1)); printf(" 0x%08lx [0x3e4ccccd] %s (5.0)\n", *((uint32_t*)&fp2), _ftostr(buf, fp2)); - printf("fp3:0x%08lx [0x3e99999a] %s (0.3)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); - test1(fp3, "3e99999a"); -#endif + printf("fp3:0x%08lx [0x40b00000] %s (5.5)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); + test1(fp3, "40b00000"); -#if 0 // work but produce wrong values // substraction printf("\nconstant - constant\n\n"); fp1 = 0.1f; @@ -79,16 +77,15 @@ void intconstconst(void) printf(" 0x%08lx [0x3dcccccd] %s (0.1)\n", *((uint32_t*)&fp1), _ftostr(buf, fp1)); printf(" 0x%08lx [0x3e4ccccd] %s (5.0)\n", *((uint32_t*)&fp2), _ftostr(buf, fp2)); - printf("fp3:0x%08lx [0xbdcccccd] %s (4.8)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); - test1(fp3, "bdcccccd"); + printf("fp3:0x%08lx [0x4099999a] %s (4.8)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); + test1(fp3, "4099999a"); fp3 = 5 - 1.1f; - printf("fp3:0x%08lx [0xbdcccccd] %s (3.9)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); - test1(fp3, "3e4cccce"); + printf("fp3:0x%08lx [0x4079999a] %s (3.9)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); + test1(fp3, "4079999a"); fp3 = 5 - 2.3f; - printf("fp3:0x%08lx [0xbdcccccd] %s (2.7)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); - test1(fp3, "be4cccce"); -#endif + printf("fp3:0x%08lx [0x402ccccd] %s (2.7)", *((uint32_t*)&fp3), _ftostr(buf, fp3)); + test1(fp3, "402ccccd"); // multiplication printf("\nconstant * constant\n\n");