Is there any example code (using cranelift) which lowers struct declarations to data section + appropriate offset computations (?) Struct types are obviously not provided by cranelift, but I'm curious if there is a good bit of reference code to learn from.
I started poking around https://github.com/bjorn3/rustc_codegen_cranelift -- but I haven't figure out the right places to look yet.
Hi @McCoy -- I think that rustc_codegen_cranelift mainly deals with struct layout questions by way of rustc ABI helpers; e.g. here: https://github.com/bjorn3/rustc_codegen_cranelift/blob/d628444e480c0c62350a34cf8b26370f92238b6a/src/value_and_place.rs#L14
that's probably a bit more complex than you're looking for; but actually struct layout can be pretty simple. What's the context for your question -- are you trying to write a thing that generates C ABI-compatible code? Or are you implementing structs in your own language (for which you have the freedom to decide layout details)?
If you're deciding on your own layout, the basic steps are: iterate through fields, determine the alignment for each, align the current offset to that, and save offset. Then look up the type/field when it's accessed and add the offset to the load/store you generate (these instructions take an Offset32
which is a type wrapper around an i32
)
Last updated: Nov 22 2024 at 17:03 UTC