Skip to content

Weird C interop behavior, when targeting x86_64-windows-gnu #10560

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

Closed
km19809 opened this issue Jan 10, 2022 · 2 comments
Closed

Weird C interop behavior, when targeting x86_64-windows-gnu #10560

km19809 opened this issue Jan 10, 2022 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@km19809
Copy link

km19809 commented Jan 10, 2022

Zig Version

0.9.0

Steps to Reproduce

  1. To get codes:
git clone https://github.com/km19809/zig-raylib
git submodule init
git submodule update
  1. To run the program:
zig build -Dtarget=x86_64-windows-gnu
zig-out/bin/main.exe

Expected Behavior

In examples/main.zig:52, I got a C struct that represents the current mouse position.
The position's y value should be changed into the correct value, as the mouse moves.

Snippets:

  1. examples/main.zig
        const raylib=@cImport({@cInclude("raylib.h");});
        //...
        const mousePosition=raylib.GetMousePosition(); // It returns raylib.Vector2
        circle.position.x=@floatToInt(i32,mousePosition.x);
        circle.position.y=@floatToInt(i32,mousePosition.y);
  1. GetMousePosition() from raylib/src/rcore.c:3737
Vector2 GetMousePosition(void)
{
    Vector2 position = { 0 };

#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
    position = GetTouchPosition(0);
#else
    position.x = (CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x;
    position.y = (CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y;
#endif

    return position;
}
  1. The definition of raylib.Vector2 in auto-generated cimport.zig
pub const struct_Vector2 = extern struct {
    x: f32,
    y: f32,
};
pub const Vector2 = struct_Vector2;

Actual Behavior

The value of mousePosition.y is always 0.
This does not happen when the target is native x86_64-linux.
It only happens when the target is x86_64-windows-gnu. I tested this in both Linux (cross-compile) and Windows (native) hosts.
I also tried compiling c code(core_input_mouse.c) that uses GetMousePosition(), then it worked correctly.

I used vsdbg for debugging this, but I cannot find any solution.
All the values from the raylib struct, CORE.Input.Mouse(raylib/src/rcore.c:3745) , were correct.
But when the mouse position struct passed to zig, its y value suddenly became 0.

Is it related to alignment or something?

@km19809 km19809 added the bug Observed behavior contradicts documented or intended behavior label Jan 10, 2022
@rohlem
Copy link
Contributor

rohlem commented Jan 10, 2022

I imagine this might be a C ABI *compatibility issue (as in, currently unimplemented / incorrect implementation).
Issue #1481 tracks this for stage1 - the checkmark for "x86_64: struct & union return values <= 16 bytes" is unticked.
You might be able to work around this in the short term by storing the struct somewhere in C code and only passing the pointer over to Zig.

@km19809
Copy link
Author

km19809 commented Jan 11, 2022

I imagine this might be a C ABI *compatibility issue (as in, currently unimplemented/incorrect implementation). Issue #1481 tracks this for stage1 - the checkmark for "x86_64: struct & union return values <= 16 bytes" is unticked. You might be able to work around this in the short term by storing the struct somewhere in C code and only passing the pointer over to Zig.

I changed the function to the other one that does not return a struct. It worked.
As you said, it was a C ABI problem. It seems duplicated, so I'll close this issue.
Thank you for your comment!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants