473,693 Members | 3,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2959
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_er ror( "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 misunderstandin g 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 misunderstandin g 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
5324
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
3525
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 circular dependencies of DLL's. Does anyone know how to do this, I have tried everything in my book. I have a C++ function GetP: DllExport void GetP()
3
2804
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 main(). The disassembled code for the function under linux (with gcc) looks like this: pushl %ebp ;push old %ebp movl %esp, %ebp ;make new stack frame
19
872
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
1967
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
407
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 can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters, isn't easy. As far as I can see, there are only two solutions: 1) This one is portable. If...
10
1802
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 different functions are not. below is my code //program start void fun1() {int i;
11
2733
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 problems? Of course, I presume that inside f I don't access i in case it was called via g. int f(int i){ /* ... */ return 0; }
10
3250
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 programming system places on how a procedure is called and how data is passed between a calling program and procedures are called calling conventions"
0
9105
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8812
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7645
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5825
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4332
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4567
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2990
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2248
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1968
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.