Stream: cranelift

Topic: example of struct implementation


view this post on Zulip McCoy (Jun 06 2022 at 02:43):

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.

view this post on Zulip McCoy (Jun 06 2022 at 02:45):

I started poking around https://github.com/bjorn3/rustc_codegen_cranelift -- but I haven't figure out the right places to look yet.

Cranelift based backend for rustc. Contribute to bjorn3/rustc_codegen_cranelift development by creating an account on GitHub.

view this post on Zulip Chris Fallin (Jun 06 2022 at 05:27):

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)

Cranelift based backend for rustc. Contribute to bjorn3/rustc_codegen_cranelift development by creating an account on GitHub.

Last updated: Nov 22 2024 at 17:03 UTC