Inline assembler in D and LDC, round 2

Tomas Lindquist Olsen tomas.l.olsen at gmail.com
Thu Feb 5 08:22:13 PST 2009


On Thu, Feb 5, 2009 at 2:42 PM, Frits van Bommel
<fvbommel at remwovexcapss.nl> wrote:
> Don wrote:
>>
>> Frits van Bommel wrote:
>>>
>>> Walter Bright wrote:
>>>>
>>>> Frits van Bommel wrote:
>>>>>
>>>>> Is it really that hard? Can't you just detect this case (non-void
>>>>> function without a 'return' at the end but with inline asm inside)?
>>>>>
>>>>> Since the compiler should know the calling convention[1], the register
>>>>> that will contain the return value of the function should be a simple lookup
>>>>> (based on target architecture, cc and return type).
>>>>> Just add that register as an output of the inline asm and return it...
>>>>
>>>> dmd doesn't attempt to figure out which register is the return value. It
>>>> just assumes that the registers specified by the ABI for the function's
>>>> return type have the proper return value in them.
>>>
>>> That isn't an option for LDC, which is why I suggested another approach.
>>
>> What's the difference? Walter's approach assumes there's a "return EAX;"
>> at the end of every function returning an int, for example; your approach
>> seems to be to add it.
>
> His approach depends on DMD directly emitting x86 machine code, so it can
> just emit 'RET' and be done with it.
>
> LDC on the other hand needs to emit LLVM asm, which requires it to specify
> an explicit return value. My approach is a way to extract that return value
> from the inline asm, allowing it to emulate DMD behavior within the LLVM IR.
>

I had really hoped I didn't have to do something like this, but I
can't come up with a better approach. I just hope it actually works
when I'm done ...
Also I have no idea if code quality is going to be optimal. I imagine
people write code like this for efficiency, if LLVM adds extra
instructions there is little point in writing code like this for LDC,
and we'd want to version things in any case, providing a true naked
version for LDC. In this case I'm not sure it's worth it to actually
do this work in the first place.



More information about the Digitalmars-d mailing list