<div dir="ltr">capi uses the C compiler to make the call, requiring ghc to write out and compile a C stub file for each import. This guarantees C compatibility and also works with cpp-defined symbols.<div><br></div><div>ccall attempts to emulate what the C compiler does with generated assembly language, but may get it wrong on Darwin; I recall Moritz Angermann saying someone would have to dump out the generated assembly from both cases to see what's different and what needs to change for ccall to work on Darwin.</div><div><br></div><div>Possible differences include not zeroing the upper parts of registers with 32-bit values in them and not treating other registers properly (for example, getting caller-saved registers wrong). It's also conceivable that there's a cpp macro involved normally and there's a hidden bug in a fallback libSystem version of a function. Someone will have to examine the generated assembly as above to see what's actually wrong.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Jun 13, 2021 at 5:21 PM Julian Ospald <<a href="mailto:hasufell@hasufell.de">hasufell@hasufell.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br>
<br>
recently during a ghcup release, we stumbled over yet another `ccall`<br>
bug on Darwin: <a href="https://github.com/biegunka/terminal-size/pull/16" rel="noreferrer" target="_blank">https://github.com/biegunka/terminal-size/pull/16</a><br>
<br>
This took Ben some time to debug (thanks again) and it isn't always<br>
clear with such<br>
bugs whether something is wrong with GHC/RTS/GC or a library, so this<br>
can be a time-sink.<br>
<br>
It's also not the first time it happened:<br>
* <a href="https://github.com/haskell/unix/pull/110#issuecomment-613384845" rel="noreferrer" target="_blank">https://github.com/haskell/unix/pull/110#issuecomment-613384845</a><br>
* <a href="https://github.com/hasufell/hpath/commit/74d686547e67da3eeae327c84c63f83833205890" rel="noreferrer" target="_blank">https://github.com/hasufell/hpath/commit/74d686547e67da3eeae327c84c63f83833205890</a><br>
<br>
I can't say that I fully understood what the exact difference here is<br>
and why it seems to matter much more on Darwin than on other systems. So<br>
I think it would be helpful if we could come up with:<br>
<br>
1. a clear description of the differences of ccall vs capi<br>
2. an explanation why ccall can lead to terrible bugs on darwin<br>
3. a guideline of when using one over the other is appropriate<br>
<br>
Not everyone deals with ffi on a regular basis, so I hope the experts<br>
here can shed some light!<br>
<br>
Cheers,<br>
Julian<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>brandon s allbery kf8nh</div><div><a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a></div></div></div></div></div>