473,326 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

function Calling conventions

I've read calling conventions to be the order(reverse or forward) in
which the parameters are being read & understood by compilers.

For ex. the following function.
int Add(int p1, int p2, int p3);

The parameters here can be read either in the forward order from p1
till p3 or reverse order from p3 till p1.

Can anyone explain what is the advantage/disadvantage of either of
these ? What does C++ standard say about this?

Rgds
Muthu
Jul 22 '05 #1
8 2920
Muthu wrote:

I've read calling conventions to be the order(reverse or forward) in
which the parameters are being read & understood by compilers.

For ex. the following function.
int Add(int p1, int p2, int p3);

The parameters here can be read either in the forward order from p1
till p3 or reverse order from p3 till p1.
or p3, p1, p2
or p2, p1, p3
or p2, p3, p1
or p1, p3, p2

Can anyone explain what is the advantage/disadvantage of either of
these ? What does C++ standard say about this?


It is undefined. The compiler writer may choose the way which
is most simple in the implementation he wrotes or best fits the
hardware requirements or ....
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
On Tue, 03 Aug 2004 01:35:59 -0700, Muthu wrote:
I've read calling conventions to be the order(reverse or forward) in which
the parameters are being read & understood by compilers.

For ex. the following function.
int Add(int p1, int p2, int p3);

The parameters here can be read either in the forward order from p1 till
p3 or reverse order from p3 till p1.
Or passed partly (or completely) in registers, or (ObCompSci) written to a
tape for a Turing machine, or...
Can anyone explain what is the advantage/disadvantage of either of these ?
For certain types of function, different calling conventions can be
advantageous. It may be faster to pass parameters through registers, for
functions with small numbers of parameters (one, for most register-based
schemes on x86, but other architectures may not have such tight register
limits), and for varargs functions the calling convention needs to be able
to handle arbitrary amounts of data.

All of this should be explained in your compiler's manual. You _have_
read it, have you?
What does C++ standard say about this?


Nothing at all. Calling conventions are different for each platform;
implementations are free to define their own so long as they can implement
C++ function semantics.

--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

Jul 22 '05 #3
> The parameters here can be read either in the forward order from p1
till p3 or reverse order from p3 till p1.
Can anyone explain what is the advantage/disadvantage of either of
these ?
The advantage of the forward order is that the compiler can generate code in
the same sequence as the source. This was important a long time ago, when
PCs did not have very much RAM.

The advantage of the backward order is that it allows variable number of
parameters. The first parameter is in that case at a fixed offset to the
stack pointer or base pointer and the other parameters can be addressed at
higher addresses.

There are other architectures where there are other issues. Especially CPUs
with a lot of registers will probably pass most of the parameters in
registers instead.
What does C++ standard say about this?
Dont know, but most C++ compilers use the backward order because of the
variable number of parameters. Most compilers can handle other calling
sequences including forward order and register passing.
I've read calling conventions to be the order(reverse or forward)


Calling conventions do also include who is responsible for removing the
parameters from the stack. The caller or the called function. Usual C++ lets
the caller remove the parameters, because if the number of parameters is
variable, then the called function might not know the number of parameters
to remove.

Niels Dybdahl
Jul 22 '05 #4
* Muthu:
I've read calling conventions to be the order(reverse or forward) in
which the parameters are being read & understood by compilers.

For ex. the following function.
int Add(int p1, int p2, int p3);

The parameters here can be read either in the forward order from p1
till p3 or reverse order from p3 till p1.
Generally a calling convention includes

* General mechanism for parameters (in registers, static memory
locations, on the stack and if so in what order).

* Specifics of passing certain types of parameters, e.g. is a source
code pass-by-value struct passed as a pointer? If nested functions
are supported, how are outer function arguments accessed (Dijkstra
display or what)? And so on.

* Who is responsible for clean-up, e.g. with stack-based parameter
passing, who pops the stack on function return?
The calling convention in turn usually determines or constrains
* Transformation of source code names to linkage level names.

Can anyone explain what is the advantage/disadvantage of either of
these?
What is commonly called "C" calling convention is stack-based with
arguments pushed from right to left and caller responsible for popping,
and source code names transformed by prepending a single underscore.

That does not mean that a C compiler necessarily uses that convention,
but most do.

Main advantage is that it allows for variable length argument lists,
the "..." notation in C++, since the called function has the named
arguments at known locations, and since the caller has the knowledge
needed to pop the stack no matter how many arguments were pushed.

What is commonly called "Pascal" calling convention is stack-based
with arguments pushed from left to right and called function
responsible for popping (no common convention for name transform).

Pascal supports nested functions but how that aspect is implemented
is not specified when one says "Pascal" calling convention.

Main advantage is reduced machine code size.

What does C++ standard say about this?


It doesn't say much at all, only that 'extern "C"' should give
compatibility with some C compiler.

I think that is one aspect that would be nice to have in upcoming
"version 2" of the standard: support for specifying calling convention
and name transformation.

Another aspect: how can the stack be properly cleaned up in
void f( int x, ... )
{
throw std::runtime_error( "Oops!" );
}

void g()
{
f( 0, "What?", 42 );
}
using a variable length argument list.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #5
Karl Heinz Buchegger wrote:
Can anyone explain what is the advantage/disadvantage of either of
these ? What does C++ standard say about this?


It is undefined.


It is unspecified. When the standard says that the effect of some piece
of code is undefined it means that the standard imposes no constraints
on what that code does. When it says that something is unspecified it
means that the implementation is free to choose among a well-defined set
of alternatives.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #6
Pete Becker wrote:

Karl Heinz Buchegger wrote:
Can anyone explain what is the advantage/disadvantage of either of
these ? What does C++ standard say about this?


It is undefined.


It is unspecified. When the standard says that the effect of some piece
of code is undefined it means that the standard imposes no constraints
on what that code does. When it says that something is unspecified it
means that the implementation is free to choose among a well-defined set
of alternatives.


I left a key point unstated: a program that uses code constructs whose
behavior is undefined are ill-formed; a program that uses code
constructs whose behavior is unspecified are well-formed.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #7
Pete Becker wrote:

Karl Heinz Buchegger wrote:
Can anyone explain what is the advantage/disadvantage of either of
these ? What does C++ standard say about this?


It is undefined.


It is unspecified. When the standard says that the effect of some piece
of code is undefined it means that the standard imposes no constraints
on what that code does. When it says that something is unspecified it
means that the implementation is free to choose among a well-defined set
of alternatives.

I think that neither 'undefined' nor 'unspecified' apply. At least
not in the meaning this words are used throughout the standard.
The standard simply doesn't say anything about how a compiler
should implement it (AFAIK). Or are there any alternatives listed
in the standard.
Or am I misunderstanding you completely?
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8
Karl Heinz Buchegger wrote:

I think that neither 'undefined' nor 'unspecified' apply. At least
not in the meaning this words are used throughout the standard.
The standard simply doesn't say anything about how a compiler
should implement it (AFAIK). Or are there any alternatives listed
in the standard.
Or am I misunderstanding you completely?


No, you're probably right. I was thinking more of order of evaluation of
function arguments, which has nothing to do with calling convention.
Changes in calling conventions don't have visible behavior, so the
wording about unspecified behavior doesn't apply.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Eric Entressangle | last post by:
Hi all, is there any trouble setting an C++ static class method as callback function for a C program or library ? Thanks
6
by: Adrian | last post by:
I am trying to pass the address of a C++ function into a Fortran routine to enable the Fortran routine to call this C++ function. I have to do it this way as our build process does not allow...
3
by: Shi Jin | last post by:
Hi there, I am now having a puzzle regarding what a function call actually does when called. I have a simple code to see what's going on. It is simply calling another simple function from...
19
by: sabarish | last post by:
Hi friend, what is the use of function pointer in c language and where it is useful? tell with simple example...? plz help me.
11
by: j23 | last post by:
I have library (static) testlib.cpp: #include <stdarg.h> void xxx(...) { char buf; va_list args; va_start(args, buf); va_end(args); }
11
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
10
by: ypjofficial | last post by:
Hello All, since the programs' stack is shared among all the function inside the program, I was just doing some R&D to see whether the same stack space is used for the variables inside the...
11
by: Felix Kater | last post by:
Hi, I can compile and run this code (see below) which twice calls the function f, first with too less, second with too much arguments. But is it legal and free of memory leaks and other...
10
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.