soul-lang / SOUL

The SOUL programming language and API
Other
1.71k stars 96 forks source link

Generating a Dplug plugin #40

Closed p0nce closed 3 years ago

p0nce commented 3 years ago

Would it be possible to export to a plugin in another langage than C++? (and another framework than JUCE) Curious about what the possibilities of SOULLang are, and what another backend entails.

julianstorer commented 3 years ago

Yes... well... sort of. It's easy for some languages, but very hard for others!

The tricky bit is that the HEART code which gets converted to the target language uses SSA form. That means it needs to be free to jump between syntactic blocks. At the moment we can emit LLVM IR (which is also SSA form) and C++ (which can emulate SSA form using goto for the jumps) but converting SSA to a language without a goto statement, or which has limits on where a goto can jump to is far from a straight conversion. WASM is such a target, and we currently rely on LLVM to do the hard conversion of SSA-form to WASM (but future extensions to the WASM spec will probably add support for SSA-like syntax).

I'm assuming Dplug uses D (?), and I hope D is too sensible to allow random goto statements, so you're have a tough time doing that. It's possible of course - in compiler literature there's lots of papers about how to do this SSA -> non-SSA conversion process, but it's a tough one.

p0nce commented 3 years ago

Thanks for the write-up. In terms of control flow, D has goto and roughly the same construct than C++ has.

D when using the LLVM D compiler (LDC) can also ingest fragments LLVM IR (have to be known at compile-time though) and passthrough it. In practice Dplug uses this compiler for all platforms. https://wiki.dlang.org/LDC_inline_IR

One example of D taking LLVM IR is implementing:

int add(int a, int b)
{
    return inlineIR!(`
        %r = add i32 %0, %1
        ret i32 %r`, int)(a, b);
}

(inlineIR is the template name and the IR code is the template parameter.)

julianstorer commented 3 years ago

Interesting. Well, if D really does allow free gotos then generating it would be a similar task to our C++ generator.

I would say "have a go at it yourself" but actually that might be tricky at the moment, as there are utilities you'd need which we've not yet moved into our open-source code section.. we're aiming to refactor things to make it possible for people to create their own back-ends as soon as possible, we're just not quite settled on the right APIs for it yet.