www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Compiler plugins?

reply Rel <relmail rambler.ru> writes:
I want to try out making some code obfuscation and protection 
stuff for my D code base. Ideally I'd like to take the AST 
(abstract syntax tree) or some other intermediate representation 
of a module and transform it at compile time.

Do we have any option to hook into one of D's compilers pipe line 
and transform the AST or some other intermediate representation? 
Or is my task somehow can be solved with metaprogramming?

As of now one thing I can think of is emmiting LLVM IR with LDC, 
transform it and then compile to native code. This may work, but 
having a way to modify the AST for example looks more appealing, 
as the AST is kinda a much more higher level representation and D 
specific.
Mar 08 2020
next sibling parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Sunday, 8 March 2020 at 18:37:49 UTC, Rel wrote:
 Do we have any option to hook into one of D's compilers pipe 
 line and transform the AST or some other intermediate 
 representation? Or is my task somehow can be solved with 
 metaprogramming?
Sounds like a case for dmd as a library: https://dlang.org/blog/2017/08/01/a-dub-case-study-compiling-dmd-as-a-library/ https://forum.dlang.org/thread/okktlu$2bin$1 digitalmars.com Bastiaan
Mar 08 2020
prev sibling parent Johan <j j.nl> writes:
On Sunday, 8 March 2020 at 18:37:49 UTC, Rel wrote:
 As of now one thing I can think of is emmiting LLVM IR with 
 LDC, transform it and then compile to native code.
You can pass plugins to LDC, which operate on the LLVM IR. There is for example a fuzzing plugin that works for Clang and for LDC. A very simple example can be found in LDC's testsuite: https://github.com/ldc-developers/ldc/tree/master/tests/plugins/addFuncEntryCall This example inserts a function call to "__test_funcentrycall()" at the start of every function. The fuzz plugin is here: https://github.com/google/AFL/blob/master/llvm_mode/afl-llvm-pass.so.cc cheers, Johan
Mar 08 2020