问题:
我在内核代码看汇编的时候,不明白'=&r'和'=r'是什么区别,看了n多资料都一样的,就几句话,还是没看懂
解答:
Unless an output operand has the `&' constraint modifier, GCC may
allocate it in the same register as an unrelated input operand, on the
assumption the inputs are consumed before the outputs are produced.
This assumption may be false if the assembler code actually consists of
more than one instruction. In such a case, use `&' for each output
operand that may not overlap an input. *Note Modifiers::.
`&'
Means (in a particular alternative) that this operand is an
"earlyclobber" operand, which is modified before the instruction is
finished using the input operands. Therefore, this operand may
not lie in a register that is used as an input operand or as part
of any memory address.
`&' applies only to the alternative in which it is written. In
constraints with multiple alternatives, sometimes one alternative
requires `&' while others do not. See, for example, the `movdf'
insn of the 68000.
An input operand can be tied to an earlyclobber operand if its only
use as an input occurs before the early result is written. Adding
alternatives of this form often allows GCC to produce better code
when only some of the inputs can be affected by the earlyclobber.
See, for example, the `mulsi3' insn of the ARM.
`&' does not obviate the need to write `='.
Unless an output operand has the `&' constraint modifier, GCC may
allocate it in the same register as an unrelated input operand, on the
assumption the inputs are consumed before the outputs are produced.
This assumption may be false if the assembler code actually consists of
more than one instruction. In such a case, use `&' for each output
operand that may not overlap an input.
__________________
翻译一下:
&只是用来约束输出部的。 如果不指定&约束符, 则GCC有可能为一个输入操作数和该输出操作数使用同一个寄存器, 这是基于以下的假设: 在产生输出之前(把输出写入到寄存器), 作为输入的寄存器内容已经不再需要了。 有时侯嵌入汇编有很多条指令,这种假设可能是不对的, 有可能过早产生了输出, 从而覆盖了输入。 加上&约束,就会告诉gcc使用另一个寄存器, 不要和输入寄存器使用同一个。
>> 如果寄存器不够用,那么使用了'&'就表示在使用这个寄存器之前需要保存到某个内存中.
(把原来的错误理解删除)
如果不加&符号, 举例来说, 如果寄存器不够用,GCC很可能为 输出操作数"=b"和输入操作数"a"使用同一个寄存器, 因为它假设: 在产生输出之前, 输入操作数就已经用完了,不需要了。
输出约束改成"=&b"能改变这一默认的优化。 "=&b"表示,这是一个"earlyclobber",可能会过早的损坏其他操作数,所以你为我单独分配个寄存器吧。 info手册说, 加上&只可能让gcc产生更优代码,没有副作用。
这个问题比较难在代码中试验,不过理论应该就是这样的。
文章来源CU社区:请教一个扩展汇编的问题