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

stage1 C ABI compatibility #1481

Open
andrewrk opened this Issue Sep 6, 2018 · 4 comments

Comments

Projects
None yet
3 participants
@andrewrk
Member

andrewrk commented Sep 6, 2018

This issue is to track C ABI compatibility support in the stage1 compiler.

Here's what support looks like currently:

  • integers, floats, pointers, enums, bools work on every target, as parameters and return values
  • x86_64: struct & union parameters & return values larger than 16 bytes
  • x86_64: struct & union parameters that have at least 1 integer in them
  • x86_64: struct & union parameters <= 16 bytes which break structs into parameters
  • x86_64: struct & union parameters <= 16 bytes which are all floats
  • x86_64: struct & union return values <= 16 bytes
  • ARM: struct & union return values
  • ARM: struct & union parameters
  • C ABI for parameters that is unsupported gives a compile error linking to this issue
  • C ABI for return values that is unsupported gives a compile error linking to this issue
  • other architectures structs & unions parameters & return values

For those who find this issue from the compile error, leave a comment detailing your specific needs and I'll see if I can code those up for you to unblock you, so you don't have to wait for this issue to be 100% solved.

@andrewrk andrewrk added this to the 0.3.0 milestone Sep 6, 2018

andrewrk added a commit that referenced this issue Sep 7, 2018

stage1: compile error instead of incorrect code
for unimplemented C ABI

See #1411
See #1481
@andrewrk

This comment has been minimized.

Member

andrewrk commented Sep 7, 2018

Update:

I just merged a branch into master which does the following things:

  • Unsupported C ABI gives a compile error, like this:
const ASmallUnion = extern union {
    r: u8,
};
extern fn foo(x: ASmallUnion) void;

pub fn main() void {
    const x = ASmallUnion{ .r = 0x12 };

    foo(col);
}
/home/andy/downloads/zig/build/test.zig:4:8: error: TODO: support C ABI for more targets. https://github.com/ziglang/zig/issues/1481
extern fn foo(x: ASmallUnion) void;
       ^
/home/andy/downloads/zig/build/test.zig:4:8: note: pointers, integers, floats, bools, and enums work on all targets
extern fn foo(x: ASmallUnion) void;
       ^
  • There is a new type of test, C ABI tests, which covers the C ABI that we do suport. See test/stage1/c_abi/* for the source.
Test 1/9 C importing Zig ABI Tests...OK
Test 2/9 C ABI integers...OK
Test 3/9 C ABI floats...OK
Test 4/9 C ABI pointer...OK
Test 5/9 C ABI bool...OK
Test 6/9 C ABI array...OK
Test 7/9 C ABI big struct...OK
Test 8/9 C ABI big union...OK
Test 9/9 C ABI small struct of ints...OK
  • Added all targets C ABI support for integers, floats, bools, enums, and pointers
  • Added x86_64 C ABI support for big structs and unions (which are passed by memory)
  • Added x86_64 C ABI support for small structs which contain at least one integer

andrewrk added a commit that referenced this issue Sep 7, 2018

andrewrk added a commit that referenced this issue Sep 8, 2018

C ABI: support returning large structs on x86_64
also panic instead of emitting bad code for returning small structs

See #1481
@andrewrk

This comment has been minimized.

Member

andrewrk commented Sep 8, 2018

Zig will now either panic or emit a compile error when C ABI compatibility is required that it doesn't support yet. Therefore this issue is now an enhancement in order to provide additional C ABI compatibility and not a bug.

For those who find this issue from the compile error, leave a comment detailing your specific needs and I'll see if I can code those up for you to unblock you, so you don't have to wait for this issue to be 100% solved.

@bheads

This comment has been minimized.

bheads commented Oct 5, 2018

Running into this issue when trying to work with the godot native headers.
"TODO implement C ABI for x86_64 return types. type 'godot_string'"

https://github.com/GodotNativeTools/godot_headers

andrewrk added a commit that referenced this issue Oct 13, 2018

C ABI and compiler rt improvements for ARM
* add __multi3 compiler rt function. See #1290
* compiler rt includes ARM functions for thumb and aarch64 and
  other sub-arches left out. See #1526
* support C ABI for returning structs on ARM. see #1481
@DavidYKay

This comment has been minimized.

DavidYKay commented Oct 21, 2018

Running into this when bringing up the BGFX rendering library on 64-bit Linux.

My code calls the following function:

bgfx_create_vertex_buffer()

Error message:

TODO implement C ABI for x86_64 return types. type 'struct_bgfx_vertex_buffer_handle_s'

bgfx_vertex_buffer_handle_s is defined in this header, using a macro.

The macro evaluates to the following struct definition:

typedef struct bgfx_vertex_buffer_handle_s { 
    uint16_t idx; 
} bgfx_vertex_buffer_handle_t

This seems to correspond to "x86_64: struct & union return values <= 16 bytes," above.

Please advise on how to get unblocked. 😃

Thanks much for your hard work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment