2

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.

HarryLeong
  • 303
  • 1
  • 8
  • What difference does a compiler implementation detail make? You're not supposed to be able to do anything with those high bits, just consider them undefined. – DCoder Dec 09 '13 at 18:23
  • 2
    @DCoder: It matters if you are writing in other languages that need to interoperate with C routines or if you are writing a C implementation that needs to interoperate with other C implementations. – Eric Postpischil Dec 09 '13 at 18:23
  • Are you saying that you think a number larger than `short` is pushed to the stack when the function prototype is `int foo(short x)'`? Why would that be so? – Floris Dec 09 '13 at 18:29
  • 2
    @Floris: An application binary interface would specify a minimum size for arguments pushed to the stack so that the stack would maintain a desired alignment. – Eric Postpischil Dec 09 '13 at 18:33
  • @EricPostpischil - I understand; but that is platform dependent, not implicit in the C standard; right? – Floris Dec 09 '13 at 19:07
  • 2
    @Floris: The question is asking about specific C implementations, not about the C standard. The C tag does not mean the question is about the C standard. “C” means more than just standard C; there are other versions of C besides the standard. Additionally, some people add a tag because the question involves the tagged subject, not because it is directly about the tagged subject. The title specifically indicates the question is about the cdecl calling convention. – Eric Postpischil Dec 09 '13 at 19:15
  • I'm sorry.There are some problems in my example.I have fixed it.I just want to know what value should the parameters(with a type of short) have on stack.I'm writing an Interpreter of script.And I want call a C function in my script. – HarryLeong Dec 10 '13 at 11:18
  • e.g. I have a function pointer(with type of int (*)(short)),a variable (with type of short).I cast function pointer to a pointer with type of int(*)(int),then I extend the value of the variable,then pass it to the function with type of int(*)(int).Why not pass the variable with type of short to the function with type of int(*)(short)?Because I want to pass variables with any type to function with as few as possible types of function pointer.(If there are many parameters in same function, that's important to make your work easier!) – HarryLeong Dec 10 '13 at 11:19

1 Answers1

2

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.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Yeah,I think I have got what I wanted.There are really some problems in my example.The parameter may be cast to short again before being passed.I will update it.Thanks. – HarryLeong Dec 10 '13 at 10:34