Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C code portability #62

Closed
4 tasks done
steinerkelvin opened this issue Feb 3, 2022 · 9 comments
Closed
4 tasks done

C code portability #62

steinerkelvin opened this issue Feb 3, 2022 · 9 comments
Assignees
Labels
enhancement New feature or request

Comments

@steinerkelvin
Copy link

steinerkelvin commented Feb 3, 2022

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.


  • fix constant related warnings
  • test compilation with clang/gcc/tcc
    • on MacOS
    • on Linux
@steinerkelvin steinerkelvin added the enhancement New feature or request label Feb 3, 2022
@steinerkelvin steinerkelvin self-assigned this Feb 3, 2022
@steinerkelvin
Copy link
Author

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 #38 (comment)

@steinerkelvin
Copy link
Author

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

@nothingnesses
Copy link

nothingnesses commented Feb 3, 2022

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.

@cauefcr
Copy link

cauefcr commented Feb 3, 2022

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;

@DylanSp
Copy link

DylanSp commented Feb 4, 2022

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 #38 (comment)).

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.

@tsnl
Copy link

tsnl commented Feb 8, 2022

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

@steinerkelvin
Copy link
Author

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

@tsnl
Copy link

tsnl commented Feb 8, 2022

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

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

@steinerkelvin
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants