Skip to content

C backend: generate tcc-compatible code #13576

Open

Description

@andrewrk

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

Activity

  1. added
    enhancement
    contributor friendly
    backend-c
    on Nov 17, 2022
  2. added this to the 0.11.0 milestone on Nov 17, 2022
  3. hryx commented on Nov 26, 2022

    @hryx
    SponsorContributor

    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?

  4. hryx commented on Nov 26, 2022

    @hryx
    SponsorContributor

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

  5. andrewrk commented on Nov 27, 2022

    @andrewrk
    MemberAuthor

    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.

  6. hryx commented on Nov 27, 2022

    @hryx
    SponsorContributor

    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.

  7. andrewrk commented on Nov 27, 2022

    @andrewrk
    MemberAuthor

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

  8. nektro commented on Nov 27, 2022

    @nektro
    Contributor

    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:

    • tcc-0.9.27+date=2022-01-11 on nix
    • 0.9.27+git20200814.62c30a4a on debian
    • 0.9.27_git20220323 on alpine
  9. hryx commented on Nov 27, 2022

    @hryx
    SponsorContributor

    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.

  10. hryx commented on Nov 27, 2022

    @hryx
    SponsorContributor

    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.

  11. modified the milestones: 0.11.0, 1.1.0 on Nov 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backend-ccontributor friendlyenhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions