C++ 如何在 Intel 语法中使用 clang 生成汇编代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10990018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:43:01  来源:igfitidea点击:

How to generate assembly code with clang in Intel syntax?

c++assemblyx86clangintel

提问by Jesse Good

As this questionshows, with g++, I can do g++ -S -masm=intel test.cpp. Also, with clang, I can do clang++ -S test.cpp, but -masm=intelis not supported by clang (warning argument unused during compilation: -masm=intel). How do I get intel syntax with clang?

正如这个问题所示,使用 g++,我可以做到g++ -S -masm=intel test.cpp. 另外,使用 clang,我可以做clang++ -S test.cpp,但-masm=intel不支持 clang ( warning argument unused during compilation: -masm=intel)。如何使用 clang 获得英特尔语法?

回答by dcoles

As noted below by @thakis, newer versions of Clang (3.5+) accept the -masm=intelargument.

正如下面@thakis指出的,较新版本的 Clang (3.5+) 接受该-masm=intel参数。



For older versions, this should get clang to emit assembly code with Intel syntax:

对于旧版本,这应该让 clang 使用 Intel 语法发出汇编代码:

clang++ -S -mllvm --x86-asm-syntax=intel test.cpp

You can use -mllvm <arg>to pass in llvm options from the clang command line. Sadly this option doesn't appear to be well documented, and thus I only found it by browsing through the llvm mailing lists.

您可以使用-mllvm <arg>从 clang 命令行传入 llvm 选项。遗憾的是,这个选项似乎没有很好的文档记录,因此我只能通过浏览 llvm 邮件列表找到它。

回答by thakis

As of clang r208683(clang 3.5+), it understands -masm=intel. So if your clang is new enough, you can just use that.

从 clang r208683(clang 3.5+) 开始,它理解-masm=intel. 因此,如果您的 clang 足够新,则可以使用它。

回答by Jerry Coffin

Presuming you can have Clang emit normal LLVM byte codes, you can then use llcto compile to assembly language, and use its --x86-asm-syntax=inteloption to get the result in Intel syntax.

假设您可以让 Clang 发出正常的 LLVM 字节码,然后您可以使用llc编译为汇编语言,并使用其--x86-asm-syntax=intel选项以 Intel 语法获取结果。