[Mono-dev] Returning small structs from internal calls?

Paolo Molaro lupus at ximian.com
Tue Dec 12 10:48:48 EST 2006

On 12/12/06 Aras Pranckevicius wrote:
> We have some trouble returning small (<= byte) structs from our native
> code to Mono via internal calls on x86 (both PC and OS X). All the
> documentation and real code I could find in Mono does not do this; it
> returns either void or primitive types. To me it looks like when
> invoking icall, Mono is not pushing the arguments correctly, but I'm
> not sure because these days my asm skills are quite rusty :)
> Basically, if we have a struct Vector2 { float x, y; } in C++ code,
> the equivalent struct in C# code and have an internall call such as:
>     // native code
>     static Vector2 Material_GetTextureOffset(
>         Reference<Material> self, MonoString* name)
>     {
>       return ...;
>     }

I hope this function has C linkage and not C++ linkage: C++ has been
designed to be ABI-incompatible, so it can't be used to implment an

> We observe this in Mono 1.2, both built with gcc (OS X) and visual
> studio 2003 (Windows). There is no problem on PPC machines (OS X).
> My first blind guess - does it have to do something with small structs
> being returned in registers by most compilers instead of the stack?
> Maybe Mono JIT always generates code as if all structs would be
> returned on the stack?

I fixed the jit to return small structures on registers on OSX for p/invokes
(the code was already there for windows).
As for icalls: passing and returning structures by value is
discouraged and unsupported. Use a ref StructType in C# and a
StructType* in the C side.


lupus at debian.org                                     debian/rules
lupus at ximian.com                             Monkeys do it better

More information about the Mono-devel-list mailing list