* Function returning uint16
@ 2016-06-16 15:04 Stéphane Glondu
2016-06-16 19:15 ` Richard Henderson
0 siblings, 1 reply; 2+ messages in thread
From: Stéphane Glondu @ 2016-06-16 15:04 UTC (permalink / raw)
To: libffi-discuss
Hello,
I am trying to debug
https://github.com/ocamllabs/ocaml-ctypes/issues/404
and I realized that the following code (on amd64):
#include <stdio.h>
#include <stdint.h>
#include <ffi.h>
uint16_t retrieve() {
return 0x4242;
}
int main() {
uint16_t r[2] = { 0xdead, 0xbeef };
ffi_cif cif;
printf("r = {%x, %x}\n", r[0], r[1]);
ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 0, &ffi_type_uint16, NULL);
ffi_call(&cif, FFI_FN(retrieve), &r[0], NULL);
printf("r = {%x, %x}\n", r[0], r[1]);
return 0;
}
returns:
r = {dead, beef}
r = {4242, 0}
Is that expected? I don't expect r[1] to be overwritten...
Cheers,
--
Stéphane
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Function returning uint16
2016-06-16 15:04 Function returning uint16 Stéphane Glondu
@ 2016-06-16 19:15 ` Richard Henderson
0 siblings, 0 replies; 2+ messages in thread
From: Richard Henderson @ 2016-06-16 19:15 UTC (permalink / raw)
To: Stéphane Glondu, libffi-discuss
On 06/16/2016 08:03 AM, Stéphane Glondu wrote:
> Hello,
>
> I am trying to debug
>
> https://github.com/ocamllabs/ocaml-ctypes/issues/404
>
> and I realized that the following code (on amd64):
>
> #include <stdio.h>
> #include <stdint.h>
> #include <ffi.h>
>
> uint16_t retrieve() {
> return 0x4242;
> }
>
> int main() {
> uint16_t r[2] = { 0xdead, 0xbeef };
> ffi_cif cif;
> printf("r = {%x, %x}\n", r[0], r[1]);
> ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 0, &ffi_type_uint16, NULL);
> ffi_call(&cif, FFI_FN(retrieve), &r[0], NULL);
> printf("r = {%x, %x}\n", r[0], r[1]);
> return 0;
> }
>
> returns:
>
> r = {dead, beef}
> r = {4242, 0}
>
> Is that expected? I don't expect r[1] to be overwritten...
It is expected. In the documentation,
In most situations, @samp{libffi} will handle promotion according to
the ABI. However, for historical reasons, there is a special case
with return values that must be handled by your code. In particular,
for integral (not @code{struct}) types that are narrower than the
system register size, the return value will be widened by
@samp{libffi}. @samp{libffi} provides a type, @code{ffi_arg}, that
can be used as the return type. For example, if the CIF was defined
with a return type of @code{char}, @samp{libffi} will try to store a
full @code{ffi_arg} into the return value.
r~
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2016-06-16 19:15 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-16 15:04 Function returning uint16 Stéphane Glondu
2016-06-16 19:15 ` Richard Henderson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).