GCC扩展的Asm-了解Clobbers和暂存器的用法 [英] GCC extended Asm - Understanding clobbers and scratch registers usage

查看:149
本文介绍了GCC扩展的Asm-了解Clobbers和暂存器的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自有关 的GCC文档扩展的ASM-Clobbers和暂存器 我发现很难理解以下解释和示例:

From GCC documentation regarding Extended ASM - Clobbers and Scratch Registers I find it difficult to understand the following explanation and example:

这是一个虚拟的平方和指令,需要两个 指向内存中浮点值的指针,并产生一个浮点数 点寄存器输出.请注意,x和y都在 asm参数,一次指定要访问的内存,一次指定一个 asm使用的基本寄存器.

Here is a fictitious sum of squares instruction, that takes two pointers to floating point values in memory and produces a floating point register output. Notice that x, and y both appear twice in the asm parameters, once to specify memory accessed, and once to specify a base register used by the asm.

好的,第一部分已经理解,现在句子继续:

Ok, first part understood, now the sentence continues:

您通常不会浪费 通过这样做来注册,因为GCC可以将相同的寄存器用于两者 目的.但是,对于x in使用%1和%3都是愚蠢的 这个asm,并期望它们是一样的.实际上,%3可能不是 登记.它可能是对该对象的符号内存引用 x指向.

You won’t normally be wasting a register by doing this as GCC can use the same register for both purposes. However, it would be foolish to use both %1 and %3 for x in this asm and expect them to be the same. In fact, %3 may well not be a register. It might be a symbolic memory reference to the object pointed to by x.

丢了我.

示例:

asm ("sumsq %0, %1, %2"
     : "+f" (result)
     : "r" (x), "r" (y), "m" (*x), "m" (*y));

示例和句子的第二部分告诉我们什么?与其他代码相比,此代码的附加值是什么?此代码是否将导致更有效的内存刷新(如本章前面所述)?

What does the example and the second part of the sentence tells us? whats is the added value of this code in compare to another? does this code will result in a more efficient memory flush (as explained earlier in the chapter)?

推荐答案

与另一个代码相比,此代码的附加值是什么?

Whats is the added value of this code in compare to another?

哪个另一个"?我的看法是,如果您有这种说明,则几乎必须使用此代码(唯一的选择是通用的memory clobber).

Which "another"? The way I see it, if you have this kind of instruction, you pretty much have to use this code (the only alternative being a generic memory clobber).

情况是,您需要将寄存器传递给指令,但实际的操作数在内存中.因此,您需要确保两件分开的事情,即:

The situation is, you need to pass in registers to the instruction but the actual operands are in memory. Hence you need to ensure two separate things, namely:

  1. 您的指令将操作数存储在寄存器中.这就是r约束的作用.
  2. 编译器知道您的asm块正在读取内存,因此必须在该块之前刷新*x*y的值.这就是m约束的用途.
  1. That your instruction gets the operands in registers. This is what the r constraints do.
  2. That the compiler knows your asm block reads memory so the values of *x and *y have to be flushed prior to the block. This is what the m constraints are used for.

不能使用%3代替%1,因为m约束使用内存引用语法.例如.而%1可能类似于%r0%eax,而%3可能是(%r0)(%eax)甚至仅仅是some_symbol.不幸的是,您的假设指令不接受此类操作数.

You can't use %3 instead of %1, because the m constraint uses memory reference syntax. E.g. while %1 may be something like %r0 or %eax, %3 may be (%r0), (%eax) or even just some_symbol. Unfortunately your hypothetical instruction doesn't accept these kinds of operands.

这篇关于GCC扩展的Asm-了解Clobbers和暂存器的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆