views:

461

answers:

2

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

+4  A: 

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.

Ritsaert Hornstra
I can't see any problem with inlining the above assembly function. Actually it looks even more simple than inlining a pure pascal function.
Serg
If you think it is simple, EMbarcadero should hire you as their new compiler guru :-). Not kidding: it is difficult. A compiler performs different phases (do not know the Delphi compiler from the inside though) text (lexing) tokens (parsing) syntax tree (optimizations) syntax tree -> ... -> (codegen) machine code. Now a piece of inline assemply is very difficult to analyse at the optimizations phase where you work with some abstract syntax tree.
Ritsaert Hornstra
I don't see why? It is a simple linear scan from front to back to find allocated/modified registers. Some heuristics (like setting up a stackframe if BP is used). Note that if odd constructs (like adressing %esp) are detected, inlining can be disabled. It would be already great to have the basis work.
Marco van de Voort
In Turbo Pascal you could just have a block like this between your pascal code: asm mov ax,13h int 10h end;
Wouter van Nifterick
TP was effectively a non-optimizing compiler, which afaik didn't keep values in registers between statements. Delphi supports inline assembler too, but doesn't have to morph a stack frame to do that.
Marco van de Voort
+5  A: 

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.

ldsandon