在预处理阶段之后但在汇编阶段之前进行编译
想象一下,我在程序(macrotest.c)中有一组宏,如下所示:
#include <stdio.h>
#include <stdlib.h>
#define n1 75
#define n2 90
#define mac(x,y) ((x > y) ? (12) : (15))
int main(){
printf("%d",mac(n1,n2));
exit(0);
}
当您编译为程序集(EM64T)时,您会得到以下内容:
gcc -S -o Macrotest.asm Macrotest.c
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $.LC0, %eax
movl $15, %esi <------ This
movq %rax, %rdi
movl $0, %eax
call printf
movl $0, %edi
call exit
.cfi_endproc
您可以看到它立即将 mac(n1,n2) 转换为 15。 但是,当您编译到预处理器阶段时,它只是扩展宏:
gcc -E -o Macrotest.prp Macrotest.c
int main(){
printf("%d",((75 > 90) ? (12) : (15)));
exit(0);
}
有什么方法可以进一步分解 C 代码无需进入汇编?
Imagine I have a set of macros in a program (macrotest.c) like so:
#include <stdio.h>
#include <stdlib.h>
#define n1 75
#define n2 90
#define mac(x,y) ((x > y) ? (12) : (15))
int main(){
printf("%d",mac(n1,n2));
exit(0);
}
When you compile down to assembly (EM64T), you get the following:
gcc -S -o macrotest.asm macrotest.c
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $.LC0, %eax
movl $15, %esi <------ This
movq %rax, %rdi
movl $0, %eax
call printf
movl $0, %edi
call exit
.cfi_endproc
You can see that it immediately converts the mac(n1,n2) to 15.
However, when you compile to the pre-processor stage, it merely expands out the macros:
gcc -E -o macrotest.prp macrotest.c
int main(){
printf("%d",((75 > 90) ? (12) : (15)));
exit(0);
}
Is there some way I can further break down the C code without going into assembly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用选项
-fdump-tree-all
并看看它是否告诉您需要什么?这会转储编译器的内部表示。它不使用 C 代码(您无法获取并编译它),但它看起来确实有点像 C。如果您想更多地了解它,请阅读“单一静态分配”(SSA)。
如果您想进一步发展汇编程序,请添加
-fdump-rtl-all
。在该过程的后半部分,GCC 从 SSA 形式(编译器尝试优化程序)切换,并使用所谓的“寄存器传输语言”。此阶段的目的是优化低级机器指令的使用。这更难理解。尝试阅读 GCC 内部手册。Try the option
-fdump-tree-all
and see if that tells you what you need?This dumps the internal representation of the compiler. It does not use C code (you couldn't take it and compile it), but it does look a bit like C. If you want to understand it more, read up on 'Single Static Assignment' (SSA).
If you want to take this even further towards assembler, then add
-fdump-rtl-all
. For the second half of the process GCC switches from SSA form (in which the compiler attempts to optimize the program), and uses what it calls "Register Transfer Language". The purpose of this phase is to optimize the low-level machine instruction usage. This is even harder to understand. Try reading the GCC internals manual.