delphiassemblyinliningbasm

Why Delphi compiler does not inline assembly functions?


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).


Solution

  • 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.