我想检查boost::variant在我的代码中应用的程序集输出,以便查看哪些中间调用被优化掉了.
当我编译以下示例(使用GCC 5.3 g++ -O3 -std=c++14 -S)时,似乎编译器优化了所有内容并直接返回100:
(...)
main:
.LFB9320:
.cfi_startproc
movl $100, %eax
ret
.cfi_endproc
(...)
Run Code Online (Sandbox Code Playgroud)
#include <boost/variant.hpp>
struct Foo
{
int get() { return 100; }
};
struct Bar
{
int get() { return 999; }
};
using Variant = boost::variant<Foo, Bar>;
int run(Variant v)
{
return boost::apply_visitor([](auto& x){return x.get();}, v);
}
int main()
{
Foo f;
return run(f);
}
Run Code Online (Sandbox Code Playgroud)
但是,完整的程序集输出包含的内容远远超过上面的摘录,对我而言,它看起来永远不会被调用.有没有办法告诉GCC/clang删除所有"噪音"并输出程序运行时实际调用的内容?
完整装配输出:
.file "main1.cpp"
.section .rodata.str1.8,"aMS",@progbits,1
.align 8
.LC0:
.string "/opt/boost/include/boost/variant/detail/forced_return.hpp"
.section .rodata.str1.1,"aMS",@progbits,1
.LC1: …Run Code Online (Sandbox Code Playgroud) 我试图学习汇编语言作为一种爱好,我经常用它gcc -S来产生汇编输出.这非常简单,但我无法编译汇编输出.我只是好奇这是否可以完成.我尝试使用标准汇编输出和intel语法-masm=intel.两者都无法编译nasm和链接ld.
因此,我想问一下是否可以生成汇编代码,然后可以编译.
更准确地说,我使用了以下C代码.
>> cat csimp.c
int main (void){
int i,j;
for(i=1;i<21;i++)
j= i + 100;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
生成程序集gcc -S -O0 -masm=intel csimp.c并尝试编译nasm -f elf64 csimp.s和链接ld -m elf_x86_64 -s -o test csimp.o.我从nasm得到的输出读取:
csimp.s:1: error: attempt to define a local label before any non-local labels
csimp.s:1: error: parser: instruction expected
csimp.s:2: error: attempt to define a local label before any non-local labels
csimp.s:2: error: parser: …Run Code Online (Sandbox Code Playgroud) 我试图通过gcc将我的c代码转换为汇编(通过输入gcc -S -masm = intel或pg.c或gcc -S prog.c),但它给了我masm代码,但我需要nasm one.我想知道你是否可以帮助我将我的c转换为nasm汇编