【发布时间】:2019-11-02 05:22:36
【问题描述】:
编译以下代码时sn -p (clang x86-64 -O3)
std::array<int, 5> test()
{
std::array<int, 5> values {{0, 1, 2, 3, 4}};
return values;
}
It produced 我期望的典型程序集
test(): # @test()
mov rax, rdi
mov ecx, dword ptr [rip + .L__const.test().values+16]
mov dword ptr [rdi + 16], ecx
movups xmm0, xmmword ptr [rip + .L__const.test().values]
movups xmmword ptr [rdi], xmm0
ret
.L__const.test().values:
.long 0 # 0x0
.long 1 # 0x1
.long 2 # 0x2
.long 3 # 0x3
.long 4 # 0x4
不过对于小数组,似乎已经想出一招了?
std::array<int, 3> test()
{
std::array<int, 3> values {{0, 1, 2}};
return values;
}
This 是对应的程序集
test(): # @test()
movabs rax, 4294967296
mov edx, 2
ret
那个神奇的数字 (4294967296) 是从哪里来的?这本质上是一个可以将reinterpret_cast 以某种方式返回到int 数组的值吗?
【问题讨论】:
-
在 Godbolt 上,您可以将鼠标悬停在一个数字上以十六进制查看。如果您经常查看 asm,您将习惯
4294967...大约为 2^32,因此您应该查看十六进制以查看高/低 32 位的巨大线索。 (或者对于低于 2^32 的数字,它实际上是一个负的 32 位整数。)
标签: c++ x86-64 compiler-optimization calling-convention abi