为什么Delphi编译器不内联汇编函数?
有时我编写非常短的汇编函数,这样
function SeniorBit(Value: LongWord): Integer;
asm
OR EAX,EAX
JZ @@Done
BSR EAX,EAX
INC EAX
@@Done:
end;
似乎是内联的最佳候选者:
function SeniorBit(Value: LongWord): Integer; inline;
但Delphi编译器不允许这样做。为什么?
更新:
感谢 ldsandon,存在一个 5.5 年前的开放 质量控制报告。该报告包含一些建议(例如扩展 asm 指令)来简化编译器的 asm 内联。我更愿意在过程/函数级别引入“裸”指令,该指令告诉编译器不必为过程创建堆栈帧,并且可以选择保留哪些寄存器(eax、edx 和 ecx 之间)。
如果使用 BASM 代码执行高效内联过程的一般任务很困难(并且可能是不必要的),那么一个好主意是为最重要的情况启用内联(例如具有显式声明的寄存器用法的裸函数)。
Sometimes I write very short assembly functions like
function SeniorBit(Value: LongWord): Integer;
asm
OR EAX,EAX
JZ @@Done
BSR EAX,EAX
INC EAX
@@Done:
end;
that seems to be the best candidates for inlining:
function SeniorBit(Value: LongWord): Integer; inline;
but Delphi compiler does not allow it. Why?
Updated:
Thanks to ldsandon, there exists a 5.5 year old open report on QC. The report containes some proposals (like extending asm directive) to simplify the asm inlining for the compiler. I would prefer to introduce the "naked" directive on the procedure/function level which says to the compiler that it does not have to create a stack frame for the procedure and optionally what registers (among eax, edx and ecx) should be preserved.
If the general task of efficient inlining procedures with BASM code is difficult (and may be unnessessary) a good idea is to enable inlining for the most important cases (like naked function with explicitely declared register usage).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅质量中心报告 #9283(并为其投票)。基本上问题是编译器应该能够理解在内联代码之前要保留哪些寄存器以及在内联代码之后要恢复哪些寄存器。只要编译器处理寄存器就很容易,当使用不受控制时就不是了。
您的示例非常简单,但编译器必须能够处理更复杂的情况。该报告处于开放状态,希望新的编译器也能够内联 BASM 代码。
See Quality Central report #9283 (and vote for it). Basically the problem is the compiler should be able to understand what registers to preserve before the inline code and what to restore after. As long as the compiler handles the register it is easy, when usage is not under is control it is not.
Your example is pretty straightforward, but the compiler must be able to handle more complex cases. The report is in open state, hope the new compiler will be able to inline BASM code as well.
您无法内联手工制作的汇编代码。
允许内联这些汇编程序是非常困难的;正常内联对寄存器使用、局部变量等的各种影响都是编译器无法通过内联汇编实现的。
You cannot inline hand crafted assembly code.
It would be very hard to allow inlining of these pieces of assembler; with normal inlining all kinds of effects on register usage, local variables etc are there that the compiler cannot do with inline assembly.