Under Review
over 1 year ago

Sdk 4.1.7 constant folds floats + strings incorrectly

Given this code:

        var k = 1.0;
        var x = 1.0 + "foo";
        var y = k + "foo";
        System.println(x);
        System.println(y);

The output is:

1.0foo
1.000000foo
with sdk 4.1.7, and -O1 or -O2. With -O0 (or with 4.1.5 and earlier) it prints:
1.000000foo
1.000000foo
In other words, when the addition is performed at runtime, we get 1.000000foo. When its folded at compile time, we get 1.0foo.
  • eclipse, sdk 4.1.7

    const K = 1.0;

    function test

    {
            var x = K;
            var t1 = K + "foo";
            var t2 = x + "foo";
    }

    -O0

    1.000000foo
    1.000000foo

    BUILD: strfoo_101574:
    BUILD:     STRING "foo" ;
    ...
    BUILD:     lgetv 0
    BUILD:     spush K
    BUILD:     getv
    BUILD:     lputv 1
    BUILD:     # vart1=K+"foo";
    BUILD:     lgetv 0
    BUILD:     spush K
    BUILD:     getv
    BUILD:     news @strfoo_101574
    BUILD:     addv
    BUILD:     lputv 2
    BUILD:     # vart2=x+"foo";
    BUILD:     lgetv 1
    BUILD:     news @strfoo_101574
    BUILD:     addv
    BUILD:     lputv 3

    -O1

    1.0foo
    1.000000foo

    BUILD: str1_0foo_1446841907:
    BUILD:   STRING "1.0foo" ;
    ...
    BUILD: strfoo_101574:
    BUILD:   STRING "foo" ;
    ...
    BUILD:       fpush 1.0
    BUILD:     lputv 1
    BUILD:     news @str1_0foo_1446841907
    BUILD:     lputv 2
    BUILD:     lgetv 1
    BUILD:     news @strfoo_101574
    BUILD:     addv
    BUILD:     lputv 3

  • I've literally posted the exact code that is broken, and explained over and over what's wrong with it, and why it's a problem with Garmin's compiler.

    If its not obvious to you that it has nothing to do with my optimizer (except that I had to disable certain optimizations to prevent triggering this bug), I don't know how I can make it any clearer...

    But just in case: All the code I've posted is fed directly into Garmin's compiler as is, without any input from my optimizer. The bugs are *entirely* down to Garmin.

  • If you use the original code, and not the code that is generated by your extension, is it working as expected?

  • Excellent summary @flows, thanks.

    I'll just add that it's not *just* that optimization changes it. It's that optimization breaks it unpredictably. eg

    const K = 1.0;

    function foo() {

      var x = K;

      var a = K + "foo"; // "1.0foo" with optimization on

      var b = x + "foo"; // "1.000000foo" with or without optimization

    }

    (sorry, for some reason if I use any of the formatting features, I get a "you've been blocked from posting" popup, so I've had to post this as text)

  • I've seen similar issues for a long time..

    Yet you push back against so many bug reports. Better to apologize for Garmin then to complain, file bug reports, or offer constructive feedback, I guess.

    It's up to Garmin whether they want to address this or not, but I don't see the need for the community to tell people their bug report isn't valid (unless that's actually the case.)