haskell / hsc2hs

Pre-processor for .hsc files
http://hackage.haskell.org/package/hsc2hs
Other
38 stars 21 forks source link

hsc2hs doesn't work with floating-point consts in cross-compilation mode #90

Open sgillespie opened 2 months ago

sgillespie commented 2 months ago

If a const is defined as floating point or double, --cross-compile fails with:

MyLib.hsc: In function ‘_hsc2hs_test1’:
MyLib.hsc:5:20: error: storage size of ‘test_array’ isn’t constant
compilation failed

In the example above, MyLib.hsc has the following construct:

#const MY_CONST

and MY_CONST is defined in a header file with #define MY_CONST ((double)-123456789.). This is the function it generates:

#include "/nix/store/sk5if9vmsykj16phb87wv7a8pv08wi62-ghc-9.6.5/lib/ghc-9.6.5/lib/template-hsc.h"
#include "../cbits/cross.h"
void _hsc2hs_test1()
{
#line 4 "MyLib.hsc"
    {
        static int test_array[(MY_CONST) > 0 ? 2 : 1];
        (void)test_array;
    }
}
sgillespie commented 2 months ago

Related:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46028 https://github.com/haskell/text-icu/issues/7

sgillespie commented 2 months ago

I did a few experiments with autoconf to see what it does. I tried passing floats/doubles to ac_fn_c_compute_int, and the results were inconsistent.

If I passed 5.0/5.0f without cross-compiling, I correctly got the integer 5. If I passed 5.1, the result was 0. With cross compiling, I got 0 either way.

sgillespie commented 2 months ago

There are a few options, hsc2hs could completely disallow non-integrals (as it currently does only with --cross-compile), but that would break existing packages, including text-icu. Another option, would be to modify the binary search to work with floats, which would essentially result in a cast to integer.