HVM icon indicating copy to clipboard operation
HVM copied to clipboard

C code portability

Open steinerkelvin opened this issue 3 years ago • 9 comments

As of now, compiling the generated code with clang on Linux or with the -pedantic flag gives some warnings, mostly related to constants, e.g.

./main.c:341:10: warning: expression is not an integer constant expression; folding it to a constant is a GNU extension [-Wgnu-folding-constant]
    case DP0: {
         ^~~

Also, compiling with tinycc fails, as per the quoted comment bellow.

Fixing this we can move forward to try embedding a C compiler (clang? tcc? zig cc?).

Please feel free to comment other platforms/compilers that you think should be considered.


  • [x] fix constant related warnings
  • [x] test compilation with clang/gcc/tcc
    • [x] on MacOS
    • [x] on Linux

steinerkelvin avatar Feb 03 '22 17:02 steinerkelvin

I just tried tinycc on the output from compiling bench/TreeSum/main.hvm; C compilation fails due to at least two issues:

  • lack of <stdatomic.h>
  • initializer element is not constant in the declaration of HEAP_SIZE (I'm not sure why this is)

There may be other issues, I didn't investigate further. I think these rule out embedding tinycc as a portable C compiler, though.

Originally posted by @DylanSp in https://github.com/Kindelia/HVM/issues/38#issuecomment-1027505883

steinerkelvin avatar Feb 03 '22 17:02 steinerkelvin

Also, it would be great if we could test in multiple platforms/compilers on CI. @nothingnesses any ideas?

steinerkelvin avatar Feb 03 '22 17:02 steinerkelvin

Testing on multiple platforms would probably involve using this. As for testing C, I have less of an idea as I'm not very familiar with the C ecosystem. This looks like it might be useful though.

nothingnesses avatar Feb 03 '22 17:02 nothingnesses

Hey guys, just wanted to drop a small diff that made it work on m1 mac.

#include <stdint.h>
// etc
typedef uint8_t u8;
typedef uint32_t u32;
typedef uint64_t u64;

cauefcr avatar Feb 03 '22 19:02 cauefcr

I was able to make some progress on the stdatomic.h issue for tinycc solved by vendoring in an implementation of that header (I copied from V, as suggested by @dumblob in https://github.com/Kindelia/HVM/issues/38#issuecomment-1027715103).

The issues with constant expressions appear to be due to tcc not supporting the GNU/GCC extension(s) around constant folding:

$ clang -O2 -pedantic --std=c99 bench/TreeSum/main.c -o main -pthread

bench/TreeSum/main.c:1455:10: warning: expression is not an integer constant expression; folding it to a constant is a GNU extension [-Wgnu-folding-constant]
    case NIL: printf("NIL"); break;

There's one of these warnings whenever a const is used as a case label. I'm not sure why clang isn't issuing a warning on the initialization of HEAP_SIZE, even with -pedantic.

In any case, changing the const declarations to #defines generally seems to work, though I'm still getting some errors in my experiments.

DylanSp avatar Feb 04 '22 03:02 DylanSp

Hi all. I'm afraid I cannot compile the generated C code using GCC on Ubuntu 20.04 on WSL2. I keep receiving these compiler errors about the constant expressions. There's a bunch of info at the end if you want to try reproducing this.

As @DylanSp mentioned, replacing the const _ = ? bindings with #define _ = ? or an enum block should fix this. I started working on #52 today on my fork. I would be happy to work on this issue next, or even simultaneously, since I will have to compare output across different compilers on Windows and Linux while testing anyway.


Info to reproduce my compiler error

  • gcc_version.txt: : the result of gcc --version
  • log.err: the result of gcc test.c -o test 2> log.err
  • test.c: the generated C file
  • test.hvm: the HVM file I compiled
  • I checked out HVM commit c6f6ac1de11e759dba47359e537641dfe415e264

tsnl avatar Feb 08 '22 11:02 tsnl

@tsnl Could you test on my last commit (c0b8a02563a928db2bd364dc0709c0262b964a71) ?

steinerkelvin avatar Feb 08 '22 19:02 steinerkelvin

@tsnl Could you test on my last commit (c0b8a02) ?

That fixed it! Thank you so much. ^_^

tsnl avatar Feb 08 '22 20:02 tsnl

I replaced the consts and fixed all the warnings I could find. The compilation with tcc is also working on my Linux machine on the single thread mode (hvm compile ./main.hvm --single-thread) that now ignores stdatomic.h. The only thing remaining would be to solve the stdatomic.h thing with tcc.

steinerkelvin avatar Feb 08 '22 20:02 steinerkelvin