Question

For example

int foo(short x);
short s = -1; 
foo(s);

Is it same as

//int foo(short x); //Updated
int foo(signed x);
short s = -1; 
foo((signed)s);//sign-extend and push to stack

Or is it same as

//int foo(short x); //Updated
int foo(unsigned)
short s = -1; 
foo((unsigned)(unsigned short)s);//zero-extend and push to stack

Or both is OK(we treat the high bits as dirty)?

I can do some experiment on special compiler.But I'm not sure every thing is same in great detail.I just need some formal promise.

Was it helpful?

Solution

cdecl seems to be an incomplete specification of the application binary interface. I expect it relies on the System V Application Binary Interface for completeness. I cannot find an explicit statement of this.

From page 43 of the System V Application Binary Interface: Intel386 Architecture Processor Supplement, “Functions pass all integer-valued arguments as words, expanding or padding signed or unsigned bytes and halfwords as needed.”

This is vague, since it does not specify whether the arguments should be expanded by sign extension, zero-padding, or otherwise. I would interpret it as meaning the contents of the added bits are unspecified, so callers may pass any values and callees should not use the extra bits.

Note that casting expressions in C function calls will not have any effect on how the arguments are passed. The types of the passed arguments are determined by the function declaration. I interpreted the sample code you provided as pseudo-code for concepts of passing different sizes of integers, not as actual C code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top