Can I compile a string containing Scala code to machine code using Scala Native as a library of my program?

165 views Asked by At

I succeed compiling a scala project to machine code using Scala Native. But I want to generate some executable code at runtime (I plan to implement a standalone compiler from a scala-like language to machine code). The goal is to have a self-hosted language, independent of JVM. Is it possible to somehow embed the Scala Native compiler in my project?

1

There are 1 answers

0
Andreas ZUERCHER On

As described in https://www.scala-native.org/en/v0.4.0/contrib/build.html,

  • The build of Scala Native contains the following JVM-based portions of which the 1st, 3rd, and 4th seem like they would be necessary for a Scala Native compiler embedded in your own compiler:

The Scala Native sbt plugin and its dependencies (directory names are in parentheses). These are JVM projects.

  • sbtScalaNative (sbt-scala-native)
  • tools
  • nir, util
  • nirparser
  • testRunner (test-runner)

So Scala Native is not independent of JVM as OP's question seeks. Conversely, studying the NIR (scala-Native Intermediate Representation) portions of the Scala Native codebase might indicate a point (somewhere near the emission of NIR onward) to factor out a nonJVM NIR-to-LLVM backend. Then OP's “self-hosted language” that compiles NIR to LLVM IR to machine code “from a scala-like language to machine code” as OP's question seeks might be possible, as derived from some backend extract/fragment of Scala Native's codebase after the parser, perhaps after the AST, which is dependent on Scala(-proper)'s JVM-based parser, whereas from NIR onward is in the JVM simply because the parser and AST were already within the JVM.