www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Where to make AST -> AST backend for DMD?

reply James Lu <jamtlu gmail.com> writes:
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.

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?

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.
Oct 02 2020
parent reply Jacob Carlborg <doob me.com> writes:
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
Oct 03 2020
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 3 October 2020 at 07:33:41 UTC, Jacob Carlborg wrote:
 IIRC, Adam D. Ruppe has worked on this. Outputting JavaScript 
 code from D code.
Yea, I played with it in 2011, but the compiler has changed since then, so I don't know how it is done anymore. One of the post-semantic visitor classes surely though. None of those existed back then, but they actually should make it easier than it used to be. This is after compiler lowerings and error checks.
Oct 03 2020