当在函数中使用结构参数,铛会改变函数签名。而不是使用一个结构类型,签名将是一个相同大小的强制int。在我的编译器项目中,我使用llvm结构类型作为方法签名(这看起来更合理)。为什么锵强迫结构参数整数
这不会是一个问题,除了通过LLVM使用该结构时,或得到的组件产生的强制类型是不同的并且不呼叫兼容这一事实。这导致我的编译器不能与具有struct的C函数ABI兼容。
为什么铛做到这一点?这是在C ABI中指定的东西吗?
这里有一个简单的例子C源文件:
struct TwoInt { int a, b; };
struct EightChar { char a, b, c, d, e, f, g, h; };
void doTwoInt(struct TwoInt a) {}
void doEightChar(struct EightChar a) {}
int main()
{
struct TwoInt ti;
struct EightChar fc;
doTwoInt(ti);
doEightChar(fc);
return 0;
}
从锵
%struct.TwoInt = type { i32, i32 }
%struct.EightChar = type { i8, i8, i8, i8, i8, i8, i8, i8 }
define void @doTwoInt(i64 %a.coerce) nounwind uwtable {
%a = alloca %struct.TwoInt, align 8
%1 = bitcast %struct.TwoInt* %a to i64*
store i64 %a.coerce, i64* %1, align 1
ret void
}
define void @doEightChar(i64 %a.coerce) nounwind uwtable {
%a = alloca %struct.EightChar, align 8
%1 = bitcast %struct.EightChar* %a to i64*
store i64 %a.coerce, i64* %1, align 1
ret void
}
define i32 @main() nounwind uwtable {
%1 = alloca i32, align 4
%ti = alloca %struct.TwoInt, align 4
%fc = alloca %struct.EightChar, align 1
store i32 0, i32* %1
%2 = bitcast %struct.TwoInt* %ti to i64*
%3 = load i64* %2, align 1
call void @doTwoInt(i64 %3)
%4 = bitcast %struct.EightChar* %fc to i64*
%5 = load i64* %4, align 1
call void @doEightChar(i64 %5)
ret i32 0
}
所得LLVM-IR我会一直期待(什么我的编译器输出):
%TwoInt = type { i32, i32 }
%EightChar = type { i8, i8, i8, i8, i8, i8, i8, i8 }
define void @doTwoInt(%TwoInt %a) {
%1 = alloca i32
%2 = alloca %TwoInt
store %TwoInt %a, %TwoInt* %2
ret void
}
define void @doEightChar(%EightChar %a) {
%1 = alloca i32
%2 = alloca %EightChar
store %EightChar %a, %EightChar* %2
ret void
}
define i32 @main() {
%1 = alloca i32
%ti = alloca %TwoInt
%fc = alloca %EightChar
%2 = load %TwoInt* %ti
call void @doTwoInt(%TwoInt %2)
%3 = load %EightChar* %fc
call void @doEightChar(%EightChar %3)
ret i32 0
}
铛如何开始?你的操作系统和ABI是什么? ABI不是在C/C++标准中定义的,而是由操作系统及其库文件定义的,并且有可能您的编译器不按照ABI工作。 – osgx
链接到更详细的网页很好的概述:什么是应用程序二进制接口(ABI)?(https://stackoverflow.com/questions/2171177/what-is-an-application-binary-interface-abi) –