LLVM中类型的大小 [英] Size of types in LLVM

查看:124
本文介绍了LLVM中类型的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们当前正在使用Java构建使用LLVM作为中间代码表示形式的编译器.我们使用了几种类型,我们需要为它们分配内存,例如使用malloc.

We are currently building a compiler in Java that uses LLVM as intermediate code representation. We use several types and we need to allocate memory for them, using malloc for instance.

我想知道如何计算元素在内存中所需的大小:指针,结构,...

I would like to know how to compute the size that elements will need in memory: pointers, structures, ...

我知道,如果我们使用LLVM API,就可以使用DataLayout,但是不幸的是我们没有使用它,而是手工"生成LLVM代码(主要是因为我们找不到合适的,易于使用的Java捆绑).

I know that we can use DataLayout if we use the LLVM API, but unfortunately we don't use it and we generate LLVM code "by hand" (mainly because we were not able to find a decent and easy to use Java binding).

因此,我们正在尝试确定如何计算(复杂)类型的大小,具体取决于将在其上执行编译器的体系结构.

So we are trying to determine how to compute the size of (complex) types, depending on the architecture the compiler will be executed on.

现在,我们只是检查我们是否在64位体系结构上,以确定指针的大小(4或8个字节).我们只是通过添加元素的大小来猜测结构的大小,但这似乎是不正确的(我想应该考虑对齐问题).

Right now, we just check if we are on a 64-bit architecture or not, to determine the size of the pointers (4 or 8 bytes). We guess the size of structures by simply adding the sizes of their elements, but it seems that it is not correct (alignment should be taken into account, I guess).

您能帮我找到解决这个问题的方法吗?

Could you help me to find a way to solve this problem?

谢谢!

推荐答案

我完成此操作的一种方法是,使指针指向要尝试确定大小的类型.然后使用getelementpointer获取指向元素1和元素0的指针.从指向元素1的指针中减去指向元素0的指针,这将是大小.当后端将其减小到填充大小时,它应该进行固定折叠并优化为单个常数.

A way I've done this is by making a pointer to the type you're trying to size. Then use getelementpointer to get a pointer to element 1 and element 0. Subtract the pointer to element 0 from the pointer to element 1 and this will be the size. When this gets lowered to the padded sizes by the backend it should get constant folded and optimized to a single constant.

这篇关于LLVM中类型的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆