Skip to content

C backend: generate GCC-compatible code #13575

@andrewrk

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 GCC.

Here's what happens currently, with GCC 11.3.0:

$ stage3/bin/zig test ../test/behavior.zig -I../test -ofmt=c -femit-bin=test.c
$ gcc -o test test.c
test.c: In function ‘behavior_atomics_testAtomicRmwFloat’:
test.c:19080:2: error: operand type ‘_Atomic zig_f32 *’ {aka ‘_Atomic float *’} is incompatible with argument 1 of ‘__atomic_fetch_add’
19080 |  zig_f32 const t10 = zig_atomicrmw_add((zig_atomic(zig_f32 ) *)&t0, (zig_f32 )zig_as_f32(0x1.4p2, zig_as_i32(0x40a00000)), memory_order_seq_cst);
      |  ^~~~~~~
test.c:19094:2: error: operand type ‘_Atomic zig_f32 *’ {aka ‘_Atomic float *’} is incompatible with argument 1 of ‘__atomic_fetch_sub’
19094 |  zig_f32 const t15 = zig_atomicrmw_sub((zig_atomic(zig_f32 ) *)&t0, (zig_f32 )zig_as_f32(0x1p1, zig_as_i32(0x40000000)), memory_order_seq_cst);
      |  ^~~~~~~
In file included from test.c:1:
test.c: In function ‘behavior_atomics_testAtomicRmwInt’:
zig.h:127:45: warning: implicit declaration of function ‘__atomic_fetch_max’; did you mean ‘__atomic_fetch_add’? [-Wimplicit-function-declaration]
  127 | #define  zig_atomicrmw_max(obj, arg, order) __atomic_fetch_max        (obj, arg, order)
      |                                             ^~~~~~~~~~~~~~~~~~
test.c:19263:21: note: in expansion of macro ‘zig_atomicrmw_max’
19263 |  zig_u8 const t41 = zig_atomicrmw_max((zig_u8  *)&t0, zig_as_u8(1), memory_order_seq_cst);
      |                     ^~~~~~~~~~~~~~~~~
zig.h:126:45: warning: implicit declaration of function ‘__atomic_fetch_min’; did you mean ‘__atomic_fetch_add’? [-Wimplicit-function-declaration]
  126 | #define  zig_atomicrmw_min(obj, arg, order) __atomic_fetch_min        (obj, arg, order)
      |                                             ^~~~~~~~~~~~~~~~~~
test.c:19277:21: note: in expansion of macro ‘zig_atomicrmw_min’
19277 |  zig_u8 const t46 = zig_atomicrmw_min((zig_u8  *)&t0, zig_as_u8(1), memory_order_seq_cst);
      |                     ^~~~~~~~~~~~~~~~~

Metadata

Metadata

Assignees

No one assigned

    Labels

    backend-cThe C backend (CBE) outputs C source code.contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.enhancementSolving this issue will likely involve adding new logic or components to the codebase.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions