例如
int foo(short x);
short s = -1;
foo(s);
是否相同
//int foo(short x); //Updated
int foo(signed x);
short s = -1;
foo((signed)s);//sign-extend and push to stack
或者是一样的
//int foo(short x); //Updated
int foo(unsigned)
short s = -1;
foo((unsigned)(unsigned short)s);//zero-extend and push to stack
或者两者都可以(我们将高位视为脏)?
我可以在特殊的编译器上做一些实验。但我不确定每件事在细节上都是一样的。我只需要一些正式的 promise 。
最佳答案
cdecl 似乎是应用程序二进制接口(interface)的不完整规范。我希望它依赖于 System V 应用程序二进制接口(interface)的完整性。我找不到对此的明确说明。
来自 System V Application Binary Interface: Intel386 Architecture Processor Supplement 第 43 页,“函数将所有整数值参数作为字传递,根据需要扩展或填充有符号或无符号字节和半字。”
这是模糊的,因为它没有指定参数是否应该通过符号扩展、零填充或其他方式来扩展。我将其解释为意味着添加位的内容未指定,因此调用者可以传递任何值,而被调用者不应使用额外的位。
请注意,C 函数调用中的转换表达式不会对参数的传递方式产生任何影响。传递参数的类型由函数声明确定。我将您提供的示例代码解释为传递不同大小整数概念的伪代码,而不是实际的 C 代码。
关于c - 在cdecl调用约定中,signed char和short如何作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20477670/