ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.84k stars 2.55k forks source link

C backend: generate tcc-compatible code #13576

Open andrewrk opened 1 year ago

andrewrk commented 1 year ago

This issue can be closed when we add CI test coverage that compiles the behavior tests with -ofmt=c and then compiles & runs them with TCC.

This command can be used to produce a test.c file for testing with tcc:

$ stage3/bin/zig test ../test/behavior.zig -I../test -ofmt=c -femit-bin=test.c
hryx commented 1 year ago

C codegen outputs functions taking arrays qualified inside [...] like so:

static zig_u8 fn_name(zig_u8 const a0[static const 2])
//                                    ^      ^

TCC added support for this syntax in 2018 but there have been no new releases since 2017.

Would you prefer to change the generated C (maybe for TCC only, since these qualifiers are ignored anyway) or only support TCC built from git?

hryx commented 1 year ago

It also looks like TCC doesn't support thread-local variables, unless built with gcc on Linux i386/x86_64.

andrewrk commented 1 year ago

I would prefer to change the generated C to not use these qualifiers so that the code generates the lowest common denominator compatibility. There should not be any TCC specific logic.

Thread local variables should lower to something like zig_threadlocal and have that be an undeclared identifier so that TCC generates a compile error when thread local variables are used.

hryx commented 1 year ago

change the generated C to not use these qualifiers

Sounds good, as I understand these qualifiers only add safety/optimization hints to compilers that support them, no change in functionality.

The undeclared identifier thing is already in place (generates an undefined zig_threadlocal_unavailable), but I thought I should point it out because there are a couple instances of thread-local vars in the behavior tests, which blocks this issue's criterion.

andrewrk commented 1 year ago

We can pass -fsingle-threaded in this case to disable dependency on thread local variables

nektro commented 1 year ago

TCC added support for this syntax in 2018 but there have been no new releases since 2017.

many systems now test and pull the master branch from various points, for example:

hryx commented 1 year ago

Aye. More data bits: Arch and Homebrew provide the tagged release by default, but latest is still available with an AUR and --HEAD respectively. Small point perhaps, but I'm not sure what the expectation is for TCC since it's more niche and the original author disclaims detachment from the project.

Assuming there are other compilers in use without full C99 support out there, "lowest common denominator" might still suggest foregoing the array param qualifiers.

hryx commented 1 year ago

I must be missing something because adding -fsingle-threaded to the original command still generates a C file with the (undefined) thread-local attribute. I'll comb the community and issues for more info.