LLVM:在编译时将所有类型的值存储在 i8 数组中

LLVM : Store Values of all types inside an i8 array at compile time

标题的一个例子是在数组中存储一个符号(例如函数指针),比方说,整数。这将允许链接器将我们在 optimization-time 处不知道的符号的正确地址放置在 i8 数组中。

如何在 [<Sum of sizes> x i8] 数组中存储任何 LLVM 值,无论其类型如何(只要大小合适)?这将在 [N x i8] 数组中存储任何类型的值,这将发生在 LLVM 过程中。 我知道它需要通过指针转换和加载来更改这些值的每次使用;这不是问题。

谢谢。

我不太明白你想用 i8 阵列做什么,但我认为这是你问题的核心:

How can I store any LLVM Value, whichever it's type is (as long as it's sized), inside an [ x i8] array

要将 LLVM 值重新解释为具有不同类型的值,请使用 bitcast. You could obtain a pointer to your data and then bitcast it to i8*, then use llvm.memcpy to write it to your array. For obtaining the runtime size of a type, see this answer

或者,您可以将所有对象放在一个巨大的结构中,然后将其位转换为需要它的 i8 数组。这可能更可取,因为它会更自然地编译成没有运行时开销的东西。