gpt4 book ai didi

c - 使用 LLVM 按参数传递结构

转载 作者:行者123 更新时间:2023-12-02 17:43:37 27 4
gpt4 key购买 nike

是否可以通过参数传递结构?

它与 C abi 兼容吗?

[编辑]

基本上,我想要一个包含两个成员的 C++ POD(该结构将是一个胖指针,带有一个指针和一个整数),并且能够在调用指令中将此结构作为函数参数传递(甚至当调用 C 代码时)。

我现在没有使用胖指针(指针和整数都在不同的函数参数中),我想知道在开始相当大的重构之前是否可能!

最佳答案

你可以做到这一点。

您可以通过将 C 代码复制并粘贴到 LLVM 的在线演示(地址为 http://llvm.org/demo/index.cgi)来了解示例 C 的 LLVM 代码是什么。 .

如果您复制并粘贴 codepad.org 中的代码,您将看到 LLVM 为 myFunction 生成以下内容:

define void @_Z10myFunction10MyStruct_t(i8* %myStructAsParam.coerce0, i32     %myStructAsParam.coerce1) nounwind uwtable {
%1 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([23 x i8]* @.str, i64 0, i64 0), i8* %myStructAsParam.coerce0, i32 %myStructAsParam.coerce1)
ret void
}

当然,如果您查看该调用,您会发现没有进行任何复制。这由调用函数来完成。如果我们编写一个小的 C 函数:

void myCallingFunction(MyStruct_t *foobar)
{
myFunction(*foobar);
}

我们可以看到为myCallingFunction生成的LLVM位码是:

define void @_Z17myCallingFunctionP10MyStruct_t(%struct.MyStruct_t* nocapture %foobar)   nounwind uwtable {
%foobar.0 = getelementptr inbounds %struct.MyStruct_t* %foobar, i64 0, i32 0
%tmp = load i8** %foobar.0, align 8
%foobar.1 = getelementptr inbounds %struct.MyStruct_t* %foobar, i64 0, i32 1
%tmp1 = load i32* %foobar.1, align 8
%1 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([23 x i8]* @.str, i64 0, i64 0), i8* %tmp, i32 %tmp1) nounwind
ret void
}

调用函数复制该结构,然后传入该副本的地址。

关于c - 使用 LLVM 按参数传递结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12755882/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com