你可以这样做。
您可以通过将 C 代码复制并粘贴到位于 http://llvm.org/demo/index.cgi 的 LLVM 在线演示中来了解示例 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 bitcode是:
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
}
调用函数复制结构体,然后传入副本的地址。