Defining/Accessing Structs Dynamically

Sorry for the high number of questions I've been posting lately!

I'm currently facing a design problem. I'm making a JIT for a subset of the
MATLAB language and in that language, functions can return many parameters.
The issue is that they will not necessarily return *all* the parameters they
could return. The actual number of returned parameters is defined at
run-time.

For speed, I have been thinking it might be possible to create a struct
containing entries for all the values a function could return, as well as an
integer telling me how many of those values were actually returned (some of
them may not be written to). The issue is that this struct needs to be
defined separately for each function I will JIT compile.

Thus, I need to know how I can define a struct dynamically without having to
write LLVM assembly (is there an API for doing this?), whether or not LLVM
functions can actually return those on the stack, as well as how I can read
and write values to the struct.

Another issue I have is that I need to interface this with C code. Thus, I
will need to be able to know at what address specific values in the struct
lie.

Thank you for your help.

- Max

Thus, I need to know how I can define a struct dynamically without having to
write LLVM assembly (is there an API for doing this?), whether or not LLVM
functions can actually return those on the stack, as well as how I can read
and write values to the struct.

There's a C++ API for everything you can write in LLVM assembly. In
this case, I think you want llvm::StructType::get().

Another issue I have is that I need to interface this with C code. Thus, I
will need to be able to know at what address specific values in the struct
lie.

This can get a bit tricky. If you're using the right TargetData,
simple structures (without any bit-fields, packed members, or other
unusual stuff like that) should just work. If you're having trouble,
though, you can just force both the C and LLVM structures to be
packed, which should make computing the offsets completely reliable
and straightforward.

-Eli

I have most of the code working but I'm running into two issues:

1. I'm not sure exactly how to return a structure on the stack. Right now,
I'm allocating one with alloca, but it seems I can only return a pointer to
it, not the structure itself. Is there any way to dereference the pointer?

2. On the calling side, I get a structure as a return value. However, it
seems GEP is complaining (I get an assertion failure) that I can't do GEP on
a structure object that isn't a pointer. How do I get a pointer to this
return value?

Right now, as a potential solution, I'm actually considering allocating the
structure on the calling side and passing a pointer to it instead, but
perhaps there's a better way to go about this.

- Max

1. I'm not sure exactly how to return a structure on the stack. Right now,
I'm allocating one with alloca, but it seems I can only return a pointer to
it, not the structure itself. Is there any way to dereference the pointer?

You should be able to do it with the load instruction.

2. On the calling side, I get a structure as a return value. However, it
seems GEP is complaining (I get an assertion failure) that I can't do GEP on
a structure object that isn't a pointer. How do I get a pointer to this
return value?

The extractvalue instruction.

Right now, as a potential solution, I'm actually considering allocating the
structure on the calling side and passing a pointer to it instead, but
perhaps there's a better way to go about this.

If you need to interface with C code, this is the only reliable way to do it.

-Eli