3

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?

dawid
  • 663
  • 6
  • 12
  • In other words, is scala-native able to compile itself? – dawid Sep 08 '19 at 01:06
  • 3
    No, Scala compiler only works on JVM and Scala Native resuses a large part (frontend) of the original JVM compiler. – ghik Sep 08 '19 at 23:02

1 Answers1

1

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.

Andreas ZUERCHER
  • 862
  • 1
  • 7
  • 20