
Sigbjorn Finne
"Julian Seward (Intl Vendor)"
writes: | > char fooble ( ... ) | > { | > return 'z'; | > } | > | > on an x86, 'z' will be returned at the lowest 8 bits in %eax. What I | > don't know is, is the C compiler obliged to clear the upper 24 bits of | > %eax, or does that onus fall on the callee?
The upper 24 bits of eax must be cleared in the callee.
The caller can assume that they are zero.
I don't know if this is documented anywhere, but that is the convention
which GNU C follows (on all architectures -- the C front-end internally
promotes the return type from "char" to "int"). I can point you to
the exact line of code in the GNU C front-end if you really want.
I think this is required by traditional K&R C code, which does things
like calling such functions without declaring them.
--
Fergus Henderson

On 08-Aug-2001, Fergus Henderson
Sigbjorn Finne
wrote: "Julian Seward (Intl Vendor)"
writes: | > char fooble ( ... ) | > { | > return 'z'; | > } | > | > on an x86, 'z' will be returned at the lowest 8 bits in %eax. What I | > don't know is, is the C compiler obliged to clear the upper 24 bits of | > %eax, or does that onus fall on the callee?
The upper 24 bits of eax must be cleared in the callee. The caller can assume that they are zero.
I don't know if this is documented anywhere, but that is the convention which GNU C follows (on all architectures -- the C front-end internally promotes the return type from "char" to "int"). I can point you to the exact line of code in the GNU C front-end if you really want.
I think this is required by traditional K&R C code, which does things like calling such functions without declaring them.
On further investigation it appears that although what I wrote is true
for GNU C, it seems to *not* be true for GNU C++, which was being used in
the code in the original bug report.
I'm not sure why that is. The C++ front-end has some code similar to
the code in the GNU C front-end, to promote function return types,
but the code seems to not get used. I don't know whether that is
deliberate or not.
--
Fergus Henderson
participants (1)
-
Fergus Henderson