The documentation (readable by laypeople at least) is severely missing, best one being the cranelift-jit-example, but it only covers working with primitive types.
Now I need to work with a struct, and I'm not quite sure how to do it. As far as I understand, the way is to pass and return structures larger than usize is by reference. However, I am not sure how to allocate stack space that could then be used as a return slot.
hi Ivan,
cranelift only has scalars (and vector types), it does not have support for structs built in
if you're source language has structs, you'll have to keep them in memory and emit clif loads/stores with the appropriate offsets for the struct's layout
you can optionally do something like a "mem2reg" pass before emitting clif if you are concerned about performance of excessive memory operations
I understand that much, my question is how to do it. What exact instructions do I need to emit to follow a pointer with or without offset, or allocate stack space and take its address to it to pass to a function.
Adding to this question, I see that ArgumentPurpose
has the variants StructArgument
and StructReturn
, but these are poorly documented. What effect do they have?
StructArgument is for when you try to pass a struct argument in C that according to the ABI wouldn't be passed in registers. The argument needs to be a pointer to the actual struct data. Cranelift will then copy the struct to the right place on the stack where the callee would expect to find the struct according to the C ABI.
StructReturn is for the argument that contains the location to store the struct that is returned if it isn't returned in a register. StructReturn is currently a nop, but it should ensure that the right register is used to pass the return value. Currently you need to pass it as first argument for the right register to be used.
@Veverak
Ivan Chinenov has marked this topic as resolved.
Last updated: Dec 23 2024 at 13:07 UTC