[LLVMdev] Returning a structure
Duncan Sands
baldrick at free.fr
Wed Jan 27 05:26:11 PST 2010
Hi Rob,
> Apologies in advance if this has been hashed over already—and of course, one should be aware that I know _absolutely nothing_ in this regard ( (: )—but perhaps at least some platform ABI details could be handled by an IR-to-IR transformation (a la one of the function passes), taking a function that returns some arbitrary structure via a return mechanism and changing it to use the platform ABI (returning via reference on the stack) instead?
yes, this has be discussed many times already on the mailing list, and in
general it is not possible, sorry.
> Is that a total non-starter? My needs are relatively simple, as I’m not interfacing with arbitrary C functions, no var-args, or anything at all out of the ordinary. Literally the most complicated thing I need to do is return a structure.
If this routine is only going to be called from code you generate, then you can
use whatever calling convention you like, for example you can just return your
struct in registers. Otherwise I'm afraid you are mistaken in thinking that
returning a struct is simple - in fact the ABI logic on (eg) x86-64 is really
horribly complicated for this.
> In this case, I actually meant building the Value of the appropriate struct type. Is there a simpler way to build a structure than to alloca a struct and store things into its fields? Or is the “{ i32 1, i32 2 }” thing only possible for 100% constant structs (I expect the answer is yes, just looking for verification (: )?
Look in the docs for "first class aggregates". Check out this example:
struct R {
long a;
long b;
};
struct R f(long a, long b) {
struct R A;
A.a = a;
A.b = b;
return A;
}
->
%"struct R" = type { i64, i64 }
define %"struct R" @f(i64 %a, i64 %b) nounwind readnone {
entry:
%mrv5 = insertvalue %"struct R" undef, i64 %a, 0 ; <%"struct R"> [#uses=1]
%mrv6 = insertvalue %"struct R" %mrv5, i64 %b, 1 ; <%"struct R"> [#uses=1]
ret %"struct R" %mrv6
}
More information about the llvm-dev
mailing list