10th April, 2023
Calling Conventions - The why and the Need
Functions, Procedures and Methods
The exception is that when talking explicitly of programs in languages like C that have only functions, we shall refer to them as \functions." Or, if we are discussing a language like Java that has only methods, we shall use that term instead. A function generally returns a value of some type (the \return type"), while a procedure does not return any value. C and similar languages, which have only functions, treat procedures as functions that have a special return type \void," to signify no return value. Ob ject-oriented languages like Java and C++ use the term \methods." These can behave like either functions or procedures, but are associated with a particular class.
Calling Conventions
Calling conventions are low level implementations that tell functions how to receive arguments from their calling functions and how to return results. A calling function is a function that has a function call within its scope.
A calling function(caller)
in the above code refers to
main()
and its
callee
(the function that it calls) is
print_specimen
which in turn is a
calling function to print_age
. A
calling function can be a callee to another function.
Calling conventions in this case basically are the low level
implementations of how
print_age
receives the
age
argument from
print_specimen
.
While writing programs, it is commonplace to see functions being called here and there. Some passing arguments from one function to another like the example above and other return values that will be used to feed into other functions as arguments. At the lowest level of what makes this feature of languages tick, lies calling conventions. Ah! almighty calling conventions.
Calling conventions are usually said to be part of the ABI. They are not part of the programming language. They give instructions to a programming language compiler. Also adherence to the calling convention falls onto the operating system or library writer. Systems engineers might find themselves dealing with calling conventions if they are writing programs with a mix of languages or having a language their application is written in having to talk to the underlying platform where there is no interface to make such communication possible.
Below is an example of Rust functions that use the C and x86_interrupt calling conventions respectively.
Okay, How many are there?
There are several calling conventions more than we would like to count. They can differ in several subtle places like where parameters are stored in memory. Parameters passed to functions following one or another calling conventions could stored in
- A. Registers
- B. Stack
- C. Heap
- D. A mixture of any of the above.
Languages like Python, JavaScript, Ruby are said to be call-by-sharing. It should be noted that this term is inconsistent with other sources that refers to call-by-sharing with other names.
Rust supports a handful of calling conventions. Rust allows the programmer to write programs that use functions from other languages where there is the need to use calling conventions to get the underlying platform to do some task.
The sample code above already shows us using two distinct calling conventions in our rust code. The code is actually from Oubre OS, an Operating System that I am building in Rust.