Why Delphi compiler does not inline assembly functions?
Asked Answered
H

2

7

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

Harmonia answered 10/3, 2010 at 6:33 Comment(10)
You should add the register calling convention after the result (eg ...: Integer; register;) to make sure the function also works when a different calling convention is used while compiling.Trapp
If you wanted to discuss the FUTURE possibility of the compiler to support inline assembler functions, perhaps QC or a message board is the best place to do so. I tried to answer that at the moment is is indeed not possible to inline a function with an asm block as it's implementation. You might think it is a good idea (hey I like assembly and think that it would be nice). But is SO the place to discuss feature requests for compilers?? I figured you wanted the answer about your problem at hand: why can't I inline this.Trapp
@Ritsaert Hornstra: why do you think SO is not the place to discuss the feature requests for the compilers? It is clearly the programming question, and where must be answers how to implement the thing in the best way and [possibly rhetoric question] why the thing is not implemented yet.Harmonia
@Serg: from the FAQ: "Avoid asking questions that are subjective, argumentative, or require extended discussion. This is not a discussion board, this is a place for questions that can be answered!". If you wanted to discuss this you should have stated so in your question. I'm sorry if I am nitpicking but i tried to answer your question to my best knowlegde, not trying to discuss future thingies. And now over and out!Trapp
@Serg: Because SO has no input or effect on the future of the compiler. The people who do (Embarcadero) have Quality Central established as the place to report issues or request new features or enhancements. Posting your comments where they can see them is the logical (and proper) place, rather than here where they may or may not decide to stop by for a visit.Countertenor
@Ken White: I doubt EM people ever visit QC but I am sure they visit SO. To make the question more clear I have added two more tags.Harmonia
@Serg: QC is part of Embarcadero's own internal (and public) web pages. To think they visit more often here than their own internal stuff is ridiculous. Unless, of course, you have no idea of what QC is, in which case you should visit qc.embarcadero.com instead of posting such misleading information.Countertenor
@Ken White: Thank you very much, now I know I was wrong. I still wonder why QC report #9283 is open for 5.5 years without any visible move.Harmonia
@Serg: Because it'ss reported doesn't mean that it will (or should) get fixed. Maybe BORL decided that the issue was only important to a few people (of which you seem to be one <g>) and so not worth the effort, or maybe the change would take a lot of time that could be better spent elsewhere. But that doesn't matter. What matters is that expecting better luck at getting EMBT staff to see your complaint here instead of their own office is just not good sense. It's like your wife leaving word for you on your mom's voice mail instead of your own cell phone. Which will reach you sooner?Countertenor
I wouldn't hope EBMT will ever implement this. They even tend to drop the assembler at all (they don't have x64 asm routines).Tumulus
B
11

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.

Badenpowell answered 10/3, 2010 at 8:21 Comment(0)
T
5

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.

Trapp answered 10/3, 2010 at 7:28 Comment(7)
I can't see any problem with inlining the above assembly function. Actually it looks even more simple than inlining a pure pascal function.Harmonia
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.Trapp
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.Genoa
In Turbo Pascal you could just have a block like this between your pascal code: asm mov ax,13h int 10h end;Triboluminescent
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.Genoa
@MarcovandeVoort So FreePascal can do this?Scarification
No. While possible and not "very difficult" doesn't mean it is economical, since it only works for fragments that don't use a stackframe etc. For FPC or DelphiGenoa

© 2022 - 2024 — McMap. All rights reserved.