TheDan64 / inkwell

It's a New Kind of Wrapper for Exposing LLVM (Safely)
https://thedan64.github.io/inkwell/
Apache License 2.0
2.38k stars 229 forks source link

Question: How to get the size of a type during LLVM generation? #437

Closed EricLBuehler closed 1 year ago

EricLBuehler commented 1 year ago

Hello everyone,

I am creating a programming language that will have tagged enums. To do this, I need to find the largest element. The way to do this (to my knowledge) is by finding the width of each variant.

I know how to do this for IntType (using the get_bit_width method), however, I cannot find how to do this with PointerType, VectorType, and FloatType. I realize that getting the width of PointerType is likely out of the scope of LLVM in general and would need to associated with the target. However, it seems strange to me that FloatType has no get_bit_width method?

In addition, how would I do this for VectorType? This type has no static length, so there must be another way?

Thank you!

TheDan64 commented 1 year ago

What about the size_of method? You'd get a runtime value but that's maybe expected?

EricLBuehler commented 1 year ago

The size_of method gets the size at runtime, but I need it at compile time to determine the largest variant. I would appreciate your guidance on how to find the length of an ArrayType (although I could track this), width of FloatType and width of PointerType.

TheDan64 commented 1 year ago

@EricLBuehler How did you solve this? For anyone from the future searching for the same answer

EricLBuehler commented 1 year ago

I solved this by:

They all revolve around tracking various metadata.