Sourav Kannantha B
Sourav Kannantha B

Reputation: 3299

Can programming languages have their own calling conventions?

Windows and Unix have their own calling-conventions for x86-64. But, if a language requires / benefits from it, can it have its own calling conventions for internal use? For eg, Swift / Python(compiled) may get benefitted from having multiple registers for returning multiple results from a function (since those languages support that). So reserving, say 3 registers (rax, rcx, rdx), for return values in their language-calling-convention, does it break anything? causing any unnoticed bugs, or causing undefined behavior in any case?

Also, if it is not necessary to follow that convention, then why do they have defined calling-conventions for user space?

Upvotes: 3

Views: 237

Answers (2)

old_timer
old_timer

Reputation: 71536

Calling convention is completely the decision of the authors of the compiler. There is no reason whatsoever that they conform to any ISA vendor documented convention or any other. For the ISA vendors that have created conventions, they are often used. But there is no requirement. Just look at x86 in the good old days. The notion of a standard is a relatively new thing (in the time frame of compiled languages).

If you want to create binaries then you can do what you want if you wish to make objects/libraries that can be used with binaries from other toolchains then both parties (who compete and likely do not get along) need to use the same convention. Which usually means there is a dominant toolchain and the others try to conform and keep up with changes.

Shared runtime libraries (.dll, .so, etc) are going to be compiled binaries using a convention, if you want to have your applications to be able to use them you need to call the functions within them using that convention, be it a shim you use to use a different convention or that your compiler natively uses the same convention.

Upvotes: 2

zx485
zx485

Reputation: 29022

Of course, they can. The residual problem will be the interaction with the API/the OS where you have to abide the OS way of doing things.

Overall, the main point will probably be the cost/benefit relation.

But for special purposes, this is possible and may even be superior (why else would you even want to do it?).
Also, take into account possible side effects on OS-specific things like the red-zone.

Upvotes: 5

Related Questions