在学习编译器优化时,我在 C 中C使用Linux版本GCCTounderstant gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5.1)
( not a statementnop) 编写代码。我首先编写了两个代码y.c并x.c使用generate their compiled assembly code选项gcc -S。
拳头代码yc
desktop:~$ cat y.c
main()
{
int i=0;
}
desktop:~$ gcc -S y.c
desktop:~$
第二个代码xc
desktop:~$ cat x.c
main()
{
int i=0;
/* Loops and if*/
while(0);
for(;0;);
if(0);
/* Arithmetic Operations */
i * i;
i / i;
i - i;
i + i;
i % i;
+i;
-i;
/* Comparison operators */
i == i;
i != i;
i < i;
i > i;
i >= i;
i <= i;
/* Bitwise Operations*/
i ^ i;
i | i;
i & i;
~i;
i << i;
i >> i;
/* Address Operatins*/
&i;
*(&i);
(&i)[i];
/* Ternary conditional operation*/
i? i : i;
/* Other Operations*/
sizeof(i);
(char)i;
/* Not-Logical Operation*/
!i;
/* Logical AND , OR Operators */
// i && i; // Commented && operation
// i || i; // Commented || operation
}
desktop:~$ gcc -S x.c
注意:这次最后两行x.c被注释掉了。
正如我所期待的那样。它们生成的汇编代码没有区别。我比较x.s并y.s使用diff命令。
desktop:~$ diff x.s y.s
1c1
< .file "x.c"
---
> .file "y.c"
desktop:~$
但是当我在x.c.
i && i;
i || i;
再次使用 -S 选项编译 xc 并与 ys 进行比较
desktop:~$ tail x.c
sizeof(i);
(char)i;
/* Not-Logical Operation*/
!i;
/* Logical AND , OR Operators */
i && i; // unCommented && operation
i || i; // unCommented || operation
}
desktop:~$ gcc -S x.c
desktop:~$ diff x.s y.s
1c1
< .file "x.c"
---
> .file "y.c"
10,21d9
< movl -4(%ebp), %eax
< testl %eax, %eax
< je .L3
< movl -4(%ebp), %eax
< testl %eax, %eax
< .L3:
< movl -4(%ebp), %eax
< testl %eax, %eax
< jne .L8
< movl -4(%ebp), %eax
< testl %eax, %eax
< .L8:
desktop:~$
问题:
我就是不明白为什么表达式i || i和i && i不等价'not a statement'?
为什么编译器将这两个语句转换为可执行文件(我们可以反汇编objdump得到相同的代码)。这两个表达式有什么特别之处。它们不包括=操作。
他们是否更改(设置/重置)CPU 标志寄存器?我觉得不是!
甚至/丢弃可能导致除以零错误的除法运算。
编辑:添加答案
i || i和i && i表达式没有什么特别之处。两者都等同于 NOT A STATEMENT。并且可以通过GCC compiler 一些额外的努力来移除。
要删除这个: 标志很有用:-o2这
是我的尝试! -o3
desktop:~$ gcc -o2 -S y.c
desktop:~$ gcc -o2 -S x.c
desktop:~$ diff x.s y.s -y
.file "x.c" | .file "y.c"
.text .text
.p2align 4,,15 <
.globl main .globl main
.type main, @function .type main, @function
main: main:
pushl %ebp pushl %ebp
movl %esp, %ebp movl %esp, %ebp
popl %ebp | subl $16, %esp
> movl $0, -4(%ebp)
> leave
RHS 中的额外行是由于文件之间未对齐。
我还喜欢添加信息,JAVA编译器会C#在没有任何标志的情况下丢弃此表达式。