jkenda / aback

A stack-oriented language that uses Polish notation which can be reversed using the ; operator (previously |>)
0 stars 0 forks source link

BEAM as a target #6

Open jkenda opened 2 months ago

jkenda commented 2 months ago

compilation to BEAM bytecode, BEAM specific features like processes and messages

implement backend yourself and make a library out of it?

jkenda commented 2 months ago

Targeting BEAM through an IR

There are a few approaches to targeting the BEAM VM. One notable approach is seen in the development of the Gleam language. Although the current version of Gleam compiles to regular Erlang source code, a previous version compiled directly to BEAM bytecode via Core Erlang, which is an intermediate representation within the Erlang compiler. Core Erlang serves as a lower-level representation that can be more directly translated to BEAM bytecode.

For your needs, targeting Core Erlang might be a viable strategy to compile to BEAM bytecode, given its closer alignment with the BEAM's operational model compared to higher-level Erlang code. This approach could involve:

  1. Translating Prefix Notation to Core Erlang: Develop a transformation process from your AST to Core Erlang, focusing on translating your language's constructs into their Core Erlang equivalents. Since Core Erlang is more granular and closer to BEAM bytecode, this step would involve detailed mapping of operations, control structures, and data handling.

  2. Compiling Core Erlang to BEAM Bytecode: Leverage the existing Erlang compiler to compile the generated Core Erlang code into BEAM bytecode. This step benefits from the optimizations and stability of the Erlang compiler, ensuring that your code runs efficiently on the BEAM VM.

This strategy allows you to focus on the transformation from your language's abstract representation to Core Erlang, relying on the robust Erlang ecosystem to handle the final step of bytecode generation.

jkenda commented 2 months ago

Building BEAM bytecode directly

Building BEAM bytecode directly, bypassing the step of outputting to Erlang (or Core Erlang) and using the Erlang compiler, involves a deeper understanding of the BEAM VM's internals and bytecode specification. This process requires several key steps and considerations:

1. Understand BEAM Bytecode

2. Design Your Compiler Backend

3. Implement the Bytecode Generator

4. Testing and Compatibility

5. Tooling and Ecosystem Integration

Challenges

Directly compiling to BEAM bytecode offers the potential for tight integration and optimization for the BEAM VM but comes with significant complexity and effort. Leveraging the existing Erlang ecosystem as a starting point can provide a more manageable path to running your language on the BEAM VM, with the option to explore direct bytecode generation as your language and compiler mature.