Why does go's compiler "gc" use a different calling convention than C?
Asked Answered
I

1

7

C uses the cdecl, which I've looked into and called with from assembly. It feels well enough, so why break the compatibility? Why was another convention needed?

Inflammable answered 31/3, 2013 at 22:44 Comment(3)
Go allows multiple return values and gc uses segmented stacks. I'm not entirely sure, but I wouldn't be surprised if these features are related to the calling conventions. Afaik, the only way to return multiple values with cdecl is to return a pointer to a structure.Celanese
C does not use the cdecl calling convention. Some implementations use it, and some use other calling conventions.Maryettamaryjane
AFAIK the Google implementation of Go uses a calling convention that is equal to that of C except in the places where C can't express the concept Go uses (such as when there are multiple return values)Gavan
E
5

Because there's no advantage in having the same calling convention. Go code and C code cannot call each other directly even when the calling convention would be the same because Go uses split stacks.

OTOH, it makes sense in gccgo, as gcc supports C split stacks for some architectures. And, IIRC, there the calling convention is because of that compatible. (More details here.)

Disclaimer: I didn't ever actually used gccgo.

Electromagnetic answered 31/3, 2013 at 22:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.