LLVM malloc 一个指针数组

LLVM malloc an array of pointers

我正在为 LLVM-IR 编写我自己的语言的编译器。我已经定义了一些表示数组的结构类型:

{ i32, [ 0 x i32] }

现在我需要为指向这些结构的实际指针数组分配内存,即

[{ i32, [ 0 x i32] }* x 10]

但是要告诉 malloc 分配内存,我需要指针的大小。我怎样才能找到它?

P.S。我看到每个指针 8 个字节应该没问题,因为不存在任何具有更大指针的体系结构,但我正在寻找更通用的解决方案。

LLVM ModuleDataLayout 指定指针的大小。 DataLayout 绑定到体系结构,目标是每个 LLVM Module 应具有的三倍。

x86_64 架构中的 DataLayout 如下所示:

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

编辑:要显式设置指针大小,您可以添加 p[n]:<size>:<abi>:<pref>,其中 p 表示指向地址 space [n] 的指针(这是可选的,默认为 0)。第二个参数是指针的大小(例如,64 位)。在上面提供的 DataLayout 中,使用了替换规则(引用自 here):

When LLVM is determining the alignment for a given type, it uses the following rules:

  • If the type sought is an exact match for one of the specifications, that specification is used. If no match is found, and the type sought is an integer type, then the smallest integer type that is larger
    than the bitwidth of the sought type is used.
  • If none of the specifications are larger than the bitwidth then the largest integer type is used. For example, given the default specifications above, the i7 type will use the alignment of i8 (next largest) while both i65 and i256 will use the alignment of i64 (largest specified).
  • If no match is found, and the type sought is a vector type, then the largest vector type that is smaller than the sought vector type will be used as a fall back. This happens because <128 x double> can be implemented in terms of 64 <2 x double>, for example.

后端将识别指针大小并接受它(如果有效)。

有了这个,您可以执行以下操作,使用 ModuleDataLayout 和 LLVM C++ API 来获取每个 Type 的分配大小:

Module* M = /*your current module*/;
Type* myType = /*some type*/;
unsigend size = M->getDataLayout()->getTypeAllocSize(myType); 
//size is 8 with the DataLayout defined above