Where to make AST -> AST backend for DMD?

Jacob Carlborg doob at me.com
Sat Oct 3 07:33:41 UTC 2020


On 2020-10-02 21:50, James Lu wrote:
> Context: I'm experimenting with lowering D to a JITed language such as 
> Lua or JavaScript for faster compilation to machine code, potential as a 
> CTFE engine, and other reasons.

IIRC, Adam D. Ruppe has worked on this. Outputting JavaScript code from 
D code.

> I want to cut in after opDispatch, after alias resolution, after alias 
> this, after UFCS is reduced to a normal function call, and probably 
> after template resolution.
> 
> Where (file) and how (methods, example code) do I get D's AST so I can 
> traverse and lower it into another language's AST?

You probably want to starting doing things here [1]. This is after 
semantic analysis but before inlining.

> I imagine using a recursive stringification method to generate the code 
> from the target language's AST as represented in the compiler, and 
> replacing D's AST nodes with the target language's AST nodes, where the 
> target language's nodes are subclasses of D nodes, pretending to be D 
> language nodes.

Inheriting from D's AST nodes might be difficult. You want to use the 
existing visitors and either output the target language directly or 
build up your own AST. You probably want to inherit from this visitor [2].

Alternatively you can use DMD as a library and create a completely 
separate project which uses DMD as a dependency, here's an example [3].

[1] 
https://github.com/dlang/dmd/blob/5fda4ea088f70afb99818482e8482771758136a2/src/dmd/mars.d#L598

[2] 
https://github.com/dlang/dmd/blob/5fda4ea088f70afb99818482e8482771758136a2/src/dmd/visitor.d#L113

[3] https://github.com/jacob-carlborg/dlp

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list