简体   繁体   English

如何在LLVM中保存一个指针数组?

[英]How do I hold an array of pointers in LLVM?

For some background, I am implementing a compiler using the llvmpy library which is a wrapper around the LLVM IR generation. 对于某些背景,我正在使用llvmpy库实现编译器,该库是LLVM IR生成的包装器。

I have created a character type which represents one or more UTF-8 code points. 我创建了一个代表一个或多个UTF-8代码点的字符类型。 These code points are stored in an array so a character can be one of the following arrays: 这些代码点存储在数组中,因此字符可以是以下数组之一:

[1 x i32], [2 x i32], ..., [6 x i32]

Now, I would like to implement a string type. 现在,我想实现一个字符串类型。 This would be an array of pointers to arrays: 这将是一个指向数组的指针数组:

[n x [1-6 x i32]*] where n is the string length

However, (as far as I know) it seems that LLVM requires me to declare the length of the inner array. 但是,据我所知,LLVM似乎要求我声明内部数组的长度。 So, while I can store this: 因此,尽管我可以存储以下内容:

[[1 x i32], [1 x i32], [1 x i32]]

I cannot store this: 我无法存储:

[[1 x i32], [2 x i32]]

Is there a way to store an array of array pointers if the array pointers lead to arrays of different length? 如果数组指针导致不同长度的数组,是否可以存储数组指针数组?

Much like in C, LLVM IR requires all the elements of an array to be of the same type. 类似于C语言,LLVM IR 要求数组的所有元素都属于同一类型。

I guess the simplest way to work around this is to just store some arbitrary pointer type (eg i32* ), and perform bitcast s whenever you want to access the array - though that of course assumes that you know in advance the size of the internal array at each index. 我猜想解决此问题的最简单方法是只存储一些任意指针类型(例如i32* ),并在每次您要访问数组时执行bitcast s-尽管这当然假定您事先知道内部数组的大小。每个索引处的数组。

If it's only known at run-time, you can make each array element point to some { i32, i32* } struct which holds the size of the internal array as well as a pointer to it, and then switch on that size and bitcast accordingly in each branch target - or just calculate the size at run-time from the i32* pointer, which is easy as this is UTF-8. 如果仅在运行时知道,则可以使每个数组元素指向某个{ i32, i32* }结构,该结构保存内部数组的大小以及指向它的指针,然后switch该大小并bitcast相应的位bitcast在每个分支目标中-或仅通过i32*指针在运行时计算大小,这很容易,因为这是UTF-8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM