Thread overview
Testers required for tool for LDC - asm_parse
Jul 06, 2023
Cecil Ward
Jul 06, 2023
kinke
Jul 06, 2023
Cecil Ward
Jul 07, 2023
Cecil Ward
July 06, 2023
I have written a tool that transforms a GDC-style (advanced-type ?) inline asm fragment of D, an "asm { }" statement, into a form that LDC can understand even when it contains named variables in the asm.

This could either be used with LDC to transform your advanced friendly asm so LDC will accept it, or if anyone is really interested I suppose that it could be integrated into LDC if desired, as that would save the LDC developers some work.

It’s a hand-written recursive descent parser for a partial D grammar, only the minimal bits that are necessary to get the job done and I’ll admit that it’s a bit of a hack and the code is still untidy.

It has only received minimal testing, and needs other test input, that is little asm statements’ text. It also needs ill-formed input, just to make sure that the parser doesn’t go into an infinite loop anywhere. It doesn’t do diagnostic error messages, intentionally omitted for several good reasons, if faced with syntactically incorrect ie. ill-formed input.

It is a single D file of about 80k, 2k loc. Internally it uses my ‘likely’/‘unlikely’ technology taken from GCC to boost performance of conditional jumps. This may not work under LDC, hasn’t been tested enough, but not working means not giving the expected performance improvement, not that it will make the program’s functioning incorrect.
July 06, 2023
I think adding symbol-names support in LDC directly would be quite straight-forward - by replacing the symbol names in the assembly template string with the operand's underlying numeric ID. See https://github.com/ldc-developers/ldc/blob/d4f2bed3a45688b32434d429e39c22dea5a9ffda/gen/asm-gcc.cpp#L186-L196. The raw string would be `insn`.
July 06, 2023
On Thursday, 6 July 2023 at 21:43:18 UTC, kinke wrote:
> I think adding symbol-names support in LDC directly would be quite straight-forward - by replacing the symbol names in the assembly template string with the operand's underlying numeric ID. See https://github.com/ldc-developers/ldc/blob/d4f2bed3a45688b32434d429e39c22dea5a9ffda/gen/asm-gcc.cpp#L186-L196. The raw string would be `insn`.

Indeed, that’s what I did, that and removing the name definitions from inside the constraint blocks. It turned out to be a pain to parse the whole thing properly though, and as I’m both very rusty and very unwell it took me quite a while. The forum was extremely helpful.
July 07, 2023
On Thursday, 6 July 2023 at 23:01:19 UTC, Cecil Ward wrote:
> On Thursday, 6 July 2023 at 21:43:18 UTC, kinke wrote:
>> I think adding symbol-names support in LDC directly would be quite straight-forward - by replacing the symbol names in the assembly template string with the operand's underlying numeric ID. See https://github.com/ldc-developers/ldc/blob/d4f2bed3a45688b32434d429e39c22dea5a9ffda/gen/asm-gcc.cpp#L186-L196. The raw string would be `insn`.
>
> Indeed, that’s what I did, that and removing the name definitions from inside the constraint blocks. It turned out to be a pain to parse the whole thing properly though, and as I’m both very rusty and very unwell it took me quite a while. The forum was extremely helpful.

I hope I have the transformed syntax correct. I always write with GDC and always use variable names, but I don’t have much experience at all.

Here’s a test run:

0237: Module Test::
source:
asm=[asm @safe pure nothrow nogc
                                {
                                .intel_syntax

                                add %[ p1 ], %[ p2 ]
                                mov %[ p3 ], %[ p1 ]

                                .att_syntax

                        : /* output */
                                [ p1 ]  "+r"    ( sum ),
                                [ p3 ]  "=r"    ( dest )
                        : /* input */
                                [ p2 ]  "rm"    ( plus )
                        : /* clobbers */
                        ;
                        }
                        ]
result=[asm @safe pure nothrow nogc
                                {
                                .intel_syntax

                                add %0, %2
                                mov %1, %0

                                .att_syntax

                        : /* output */
                                        "+r"    ( sum ),
                                        "=r"    ( dest )
                        : /* input */
                                        "rm"    ( plus )
                        : /* clobbers */
                        ;
                        }
                        ];