PowerPC 转向可变 SPR
我正在为 C 程序编写一个汇编宏,并且对此很陌生,我陷入了困境。 我正在尝试编写一个宏,用于将数据从通用寄存器移动到专用寄存器。
我的问题是,我发现将数据从 GPR 移动到 SPR 的语法采用恒定的 SPR 值,而我想使用存储在另一个寄存器中的变量。
# SPR is constant, rA is the value to be written
mtspr SPR, rA
我正在寻找看起来像这样的东西:
# rA contains the number of the SPR, and rB the value to be moved.
AWESOMEmtspr rA, rB
是否有原因没有这样的宏可用,我该如何自己制作它?
非常感谢。
---- 编辑: ---- 现在看来,我的 C 代码中有一个巨大的 switch case,可以跳转到正确的 mtspr 部分。我有二十几个部分用于读取和编写特定的 SPR:s,每个部分看起来完全相同,但有一个常数值不同。
I'm writing an assembly macro to a C-program, and being quite new with this I have gotten stuck on something.
I'm trying to write a macro for moving data from a general purpose register to a special purpose register.
My problem is that the syntax I've found to move data from a GPR to an SPR takes a constant SPR value, while I want to use a variable one stored in another register.
# SPR is constant, rA is the value to be written
mtspr SPR, rA
I'm after something that looks like this:
# rA contains the number of the SPR, and rB the value to be moved.
AWESOMEmtspr rA, rB
Is there a reason there is no such macro available, and how would I make it myself?
Many thanks in advance.
---- Edit: ----
As it looks now I have a giant switch case in my C-code that jumps to the correct mtspr-section. I have twenty-some sections for reading and writing specifit SPR:s that each look exactly the same, but differ by a constant value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不能这样做的原因是指令体系结构不接受寄存器间接作为寄存器参数的寻址模式。老实说,我从未见过这样的机器架构,因为寄存器的数量通常相当小,因此寄存器被编码为指令本身的一部分。如果您确实不喜欢现有的解决方案,您可以尝试自己合成指令(获取基本操作码,查看寄存器说明符的位置以及适当值中的“或”),然后执行它。根据您的操作系统和编译器,这可能是不可能的(自修改代码通常是禁忌)。
如果在汇编中编写跳转表,是否会使代码更清晰?也许传入 SPR 说明符(假设它是一个从零开始的整数,或者可以强制为 1),将其向左移动以获得跳转表中的偏移量,然后跳转到表中,这将是一个
我不知道 的序列我不知道什么对你来说算“更干净”,只是想我会把它扔掉。请注意,您可能必须用 NOP 填充才能使所有内容对齐,我没有 PowerPC 手册,因此我不知道指令大小或对齐要求是什么。
The reason you can't do it is that the instruction architecture doesn't accept register-indirect as an addressing mode for the register parameter. Honestly, I've never seen a machine architecture that does, as the number of registers is usually fairly small, so the register is encoded as part of the instruction itself. If you really don't like the solution you've got, you can try to synthesize the instruction yourself (take the base opcode, see where the register specifier goes and OR in the appropriate value), then execute it. Depending on your OS and compiler, this may not be possible (self-modifying code is often taboo).
Does it make the code cleaner if you write the jump table in assembly? Maybe pass in the SPR specifier (assuming it's a zero-based integer, or can be coerced into one), shift it left to get the offset into the jump table, then jump into the table, which would be a sequence of
I don't know what counts as "cleaner" to you, just thought I'd throw that out. Note that you might have to pad with NOPs to get everything aligned, I don't have a PowerPC manual so I have no idea what the instruction sizes or alignment requirements are.
我使用 stringizer 宏找到了一个相当优雅的解决方案:
这是来自 PowerPC 的 U-Boot 代码。
I found a rather elegant solution to this using the stringizer macro:
This is from the U-Boot code for the PowerPC.
这看起来像是一件奇怪的事情,但如果您确信出于某种原因需要这样做,那么您需要实现一个跳转表或条件序列,通过它来测试 rA 并跳转到适当的硬编码 <代码>mtspr指令。您还需要考虑如何处理无效的 SPR 编号。
This seems like a bizarre thing to do, but if you're convinced you need to do this for some reason then you'll need to implement a jump table or sequence of conditionals whereby you test rA and jump to the appropriate hard-coded
mtspr
instruction. You'll need to think about how you handle invalid SPR numbers too.