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

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
kludg
  • 27,213
  • 5
  • 67
  • 118
  • 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. – Ritsaert Hornstra Mar 10 '10 at 07:30
  • 2
    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. – Ritsaert Hornstra Mar 10 '10 at 10:19
  • @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. – kludg Mar 10 '10 at 10:39
  • 2
    @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! – Ritsaert Hornstra Mar 10 '10 at 11:54
  • 1
    @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. – Ken White Mar 10 '10 at 14:24
  • @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. – kludg Mar 10 '10 at 14:37
  • @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 http://qc.embarcadero.com instead of posting such misleading information. – Ken White Mar 10 '10 at 15:13
  • @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. – kludg Mar 10 '10 at 15:30
  • @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 ) 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? – Ken White Mar 12 '10 at 19:42
  • 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). – Fr0sT Aug 11 '15 at 07:36

2 Answers2

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.

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.

Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
Ritsaert Hornstra
  • 5,013
  • 1
  • 33
  • 51
  • I can't see any problem with inlining the above assembly function. Actually it looks even more simple than inlining a pure pascal function. – kludg Mar 10 '10 at 07:40
  • 4
    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 Mar 10 '10 at 08:05
  • 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 Mar 10 '10 at 23:13
  • 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 Mar 11 '10 at 04:01
  • 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 Mar 11 '10 at 11:16
  • @MarcovandeVoort So `FreePascal` can do this? – John Lewis Feb 18 '15 at 16:56
  • 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 Delphi – Marco van de Voort Feb 19 '15 at 12:13