可以通過參數傳遞結構嗎?通過LLVM參數傳遞結構
它與C abi兼容嗎?
[編輯]
基本上,我想有一個C++ POD其中將包括兩個構件(結構將是一個脂肪指針,用指針和一個整數),並能夠通過這個結構作爲調用指令中的函數參數(即使調用C代碼時)。
我現在沒有使用胖指針(指針和整數都在不同的函數參數中),我想知道在啓動一個相當大的重構之前是否有可能!
可以通過參數傳遞結構嗎?通過LLVM參數傳遞結構
它與C abi兼容嗎?
[編輯]
基本上,我想有一個C++ POD其中將包括兩個構件(結構將是一個脂肪指針,用指針和一個整數),並能夠通過這個結構作爲調用指令中的函數參數(即使調用C代碼時)。
我現在沒有使用胖指針(指針和整數都在不同的函數參數中),我想知道在啓動一個相當大的重構之前是否有可能!
絕對可以。這裏有一個例子:
struct MyStruct_t {
char *charPointer;
int number;
};
void myFunction(MyStruct_t myStructAsParam) {
printf("String: %s, Number: %i", myStructAsParam.charPointer, myStructAsParam.number);
// Your stuff here.
}
您的myStructAsParam是一個指針,但您使用'。'運營商,什麼是正確的事情?要清楚,我正在尋找傳遞myStructAsParam作爲值,而不是指針(http://codepad.org/H4Az2uKK)。 –
哎呀,習慣的力量:P –
你可以這樣做。
你可以找出LLVM代碼是什麼樣品C通過複製和粘貼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
}
調用功能,使該結構的一個副本,然後通過在地址的副本。
請詳細說明。你有什麼嘗試?你可以包含一些代碼嗎? –
@SimonGermain我添加了一些關於我的用例的上下文。目前沒有代碼。 –