Introduction to SimpleLanguage

We found the easiest way to get started with implementing your own language by extending an existing SimpleLanguage. SimpleLanguage is a demonstration language built using the Truffle API. The SimpleLanguage project provides a showcase on how to use the Truffle APIs for writing your own language. It aims to use most of the available Truffle framework features and documents their use extensively with inline source documentation.

To start, please ensure maven3 and GraalVM are available in your system.

  1. Clone the SimpleLanguage repository using
    git clone
  2. Set the JAVA_HOME and PATH environment variables to the GraalVM home and bin folders using a command-line shell for Linux:
    export JAVA_HOME=/path/to/graalvm
    export PATH=/path/to/graalvm/bin:$PATH

    and for macOS X:

    export JAVA_HOME=/path/to/graalvm/Contents/Home
    export PATH=/path/to/graalvm/Contents/Home/bin:$PATH
  3. Execute mvn package from SimpleLanguage folder to build the language. The command also builds a slnative executable in the simplelanguage/native directory and sl-component.jar language component which later can be installed into GraalVM using the GraalVM Updater tool. Please verify if native-image plugin is available in your GraalVM distribution to avoid the build failure:
    $ gu list
    $ gu install native-image

    You can disable the SimpleLanguage native image build during the packaging phase by running:

    $ export SL_BUILD_NATIVE=false
    $ mvn package
  4. Run in the simplelanguage root folder:
    $ ./sl ./language/tests/

The SimpleLanguage demonstration language is licensed under the Universal Permissive License (UPL).

IDE Setup #

The Truffle Language Implementation framework provides language-agnostic infrastructure to realize standard IDE features by providing additional APIs. If you would like to experiment with your language and get the benefits of an IDE, consider the import of SimpleLanguage as an example.

Eclipse #

The SimpleLanguage teaching project has been tested with Eclipse Neon.2 Release 4.6.2 and Eclipse Oxygen.3A. To import the project folder to the desirable Eclipse environment:

  1. Open Eclipse with a new workspace;
  2. Install m2e and m2e-apt plugins from the Eclipse marketplace (Help -> Eclipse Marketplace);
  3. Finally, import SimpleLanguage project from File -> Import -> Maven -> Existing Maven Projects -> browse to the simplelanguage folder -> Finish.

Netbeans #

NetBeans provides GUI support for debugging arbitrary languages. In order to upload SimpleLanguage to NetBeans interface, proceed to File -> Open Project -> select simplelanguage folder -> check “Open Required Projects” -> open Project.

IntelliJ IDEA #

SimpleLanguage project has been tested with IntelliJ IDEA 2018.3. Open IntelliJ IDEA and, from the main menu bar, select File -> Open -> Navigate to and select the simplelanguage folder -> Press “OK”. All dependencies will be included automatically.

Run SimpleLanguage #

To run a SimpleLanguage source file, execute

$ ./sl language/tests/

To see assembly code for Truffle compiled functions, run

$ ./sl -disassemble language/tests/

Dump Graphs #

To investigate performance issues, we recommend the Ideal Graph Visualizer (IGV) – an essential tool for any language implementer building on top of GraalVM Enterprise Edition. It is available as a separate download on Oracle Technology Network Downloads page and requires accepting the Oracle Technology Network Developer License.

1.Unzip the downloaded package, enter the bin directory and start IGV:

$ cd idealgraphvisualizer/bin
$ idealgraphvisualizer

2.Execute the following from simplelanguage root folder to dump graphs to IGV:

$ ./sl -dump language/tests/

Debug #

To start debugging the SimpleLanguage implementation with a Java debugger, pass the -debug option to the command-line launcher of your program:

$ ./sl -debug language/tests/

Then attach a Java remote debugger (like Eclipse) on port 8000.

SimpleLanguage Component for GraalVM #

Languages implemented with Truffle Language Implementation framework can be packaged as Components which later can be installed into GraalVM using the GraalVM Updater. Running mvn package in the simplelanguage folder also builds a sl-component.jar. This file is the SimpleLanguage component for GraalVM and can be installed by running:

$ gu -L install /path/to/sl-component.jar

SimpleLanguage Native Image #

A language implementation built with the Truffle Language Implementation framework can be AOT compiled using the GraalVM Native Image. Running mvn package in the simplelanguage folder also builds a slnative executable in the native directory. This executable is the full SimpleLanguage implementation as a single native application, and has no need for a GraalVM in order to execute SimpleLanguage code. Besides this, a big advantage of using the native image when compared to running on the GraalVM is the greatly faster startup time as shown bellow.

$ time ./sl language/tests/
== running on org.graalvm.polyglot.Engine@2db0f6b2
Hello World!

real    0m0.405s
user    0m0.660s
sys     0m0.108s

$ time ./native/slnative
== running on org.graalvm.polyglot.Engine@7fd046f06898
Hello World!

real    0m0.004s
user    0m0.000s
sys     0m0.000s

This snipped shows a timed execution of a “Hello World” program using the sl launcher script, which runs SimpleLanguage on GraalVM, and using the native image. We can see that when running on GraalVM the execution takes 405ms. Since our SimpleLanguage program does just one print statement, we can conclude that almost all of this time is spent starting up GraalVM and initializing the language itself. When using the native image we see that the execution takes only 4ms, showing two orders of magnitude faster startup than running on the GraalVM.

For more information on the native-image tool consider reading the reference manual.

Disable SimpleLanguage Native Image Build #

Building the native image through maven is attached to the maven package phase. Since the native image build can take a bit of time, we provide the option to skip this build by setting the SL_BUILD_NATIVE environment variable to false like so:

$ export SL_BUILD_NATIVE=false
$ mvn package
[INFO] ------------------------------------------------------------------------
[INFO] Building simplelanguage-graalvm-native
[INFO] ------------------------------------------------------------------------
[INFO] --- exec-maven-plugin:1.6.0:exec (make_native) @ simplelanguage-graalvm-native ---
Skipping the native image build because SL_BUILD_NATIVE is set to false.
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------

Run SimpleLanguage with the Newest Compiler Version #

In the outstanding case that you need to execute SimpleLanguage with the newest version of the GraalVM compiler, please follow these instructions:

  1. Download the latest JVMCI JDK 8 and point JAVA_HOME at it:
    $ export JAVA_HOME=/path/to/openjdk1.8.0_242-jvmci-20.1-b01
  2. Clone the “Graal” repository from SimpleLanguage folder:
    $ cd /path/to/simplelanguage
    $ git clone
  3. Clone the mx repository:
    $ git clone
  4. Add mx to your path:
    $ export PATH=/path/to/mx:$PATH
  5. Navigate to the compiler folder:
    $ cd /path/to/graal/compiler
  6. Build the GraalVM compiler:
    $ mx build
  7. Run SimpleLanguage using the mx command:
    $ mx -v --jdk=jvmci vm -cp /path/to/simplelanguage/launcher/target/launcher-20.1.0-SNAPSHOT.jar:/path/to/simplelanguage/language/target/simplelanguage.jar  /path/to/simplelanguage/language/tests/

Run SimpleLanguage Using Command Line #

Executing SimpleLanguage code is normally done with the sl script which sets up the necessary command line depending on whether JAVA_HOME points to the GraalVM or another JVM installation. The following subsections describe the command line for both cases.

Run SimpleLanguage with GraalVM as JAVA_HOME #

Assuming JAVA_HOME points to the GraalVM installation and that the current working directory is the simplelanguage directory, to run SimpleLanguage one should execute the following command:

$ $JAVA_HOME/bin/java \
    -cp launcher/target/launcher-20.1.0-SNAPSHOT.jar \
    -Dtruffle.class.path.append=language/target/simplelanguage.jar \ language/tests/

In short, we place the launcher jar on the class path and execute its main class, but we inform GraalVM of the presence of SimpleLanguage by using the -Dtruffle.class.path.append option and providing it the path to the fat language jar. Having the language on a separate class path ensures a strong separation between the language implementation and its embedding context (in this case, the launcher).

Disable Class Path Separation

NOTE! This should only be used during development.

For development purposes it is useful to disable the class path separation and enable having the language implementation on the application class path (for example, for testing the internals of the language).

For GraalVM distribution based on JDK 8, you can add the -XX:-UseJVMCIClassLoader option. This disables the class path isolation, enabling the language implementation to be placed on the application class path. The command line can be as the following:

$JAVA_HOME/bin/java \
    -XX:-UseJVMCIClassLoader -Dgraalvm.locatorDisabled=true \
    -cp launcher/target/launcher-20.1.0-SNAPSHOT.jar:language/target/simplelanguage.jar \ language/tests/

For JDK 11 based distribution of GraalVM, the -XX:-UseJVMCIClassLoader option is not valid. The Java Module System isolation is used. You can achieve the same behaviour using --add-exports or --upgrade-module-path. The later is preferable.

The Truffle API jar on the Maven Central exports all API packages in its module-info. Apply --upgrade-module-path option together with -Dgraalvm.locatorDisabled=true and this jar to export Truffle API packages:

-Dgraalvm.locatorDisabled=true --module-path=<yourModulePath>:${truffle.dir} --upgrade-module-path=${truffle.dir}/truffle-api.jar

A sample POM using the --upgrade-module-path to export Truffle API packages can be found in the Simple Language POM.xml file.

Other JVM Implementations #

Unlike GraalVM, which includes all the dependencies needed to run a language implemented with Truffle framework, other JVM implementations need additional jars to be present on the class path. These are the Truffle API and GraalVM SDK jars available from Maven Central.

Assuming JAVA_HOME points to a Stock JDK installation, that the current working directory is the simplelanguage directory and the Truffle API and GraalVM SDK jars are present in that directory, one can execute SimpleLanguage with the following command:

$ $JAVA_HOME/bin/java \
    -cp graal-sdk-20.1.0.jar:truffle-api-20.1.0.jar:launcher/target/launcher-20.1.0-SNAPSHOT.jar:language/target/simplelanguage.jar \ language/tests/