Stream: cranelift

Topic: ✔ How to work with structs?


view this post on Zulip Ivan Chinenov (Nov 19 2021 at 22:01):

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.

view this post on Zulip fitzgen (he/him) (Nov 19 2021 at 22:25):

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

view this post on Zulip Ivan Chinenov (Nov 20 2021 at 09:15):

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.

view this post on Zulip Veverak (Nov 23 2021 at 05:11):

Adding to this question, I see that ArgumentPurpose has the variants StructArgument and StructReturn, but these are poorly documented. What effect do they have?

view this post on Zulip bjorn3 (Nov 23 2021 at 06:58):

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.

view this post on Zulip bjorn3 (Nov 23 2021 at 07:00):

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.

view this post on Zulip bjorn3 (Nov 23 2021 at 07:01):

@Veverak

view this post on Zulip Notification Bot (Nov 27 2021 at 20:16):

Ivan Chinenov has marked this topic as resolved.


Last updated: Dec 23 2024 at 13:07 UTC