Skip to content

Latest commit

 

History

History
131 lines (104 loc) · 6.44 KB

README.md

File metadata and controls

131 lines (104 loc) · 6.44 KB

Substrate VM

Substrate VM is a framework that allows ahead-of-time (AOT) compilation of Java applications under closed-world assumption into executable images or shared objects (ELF-64, 64-bit Mach-O, PE32+). Substrate VM is an internal project name for the technology behind GraalVM Native Image.

Quick start

To get started, install mx. Then point the JAVA_HOME variable to a JDK that supports a compatible version of the JVM Compiler Interface (JVMCI). JVMCI is a privileged low-level interface to the JVM, that can read metadata from the VM such as method bytecode and install machine code into the VM. Obtain JVMCI-enabled:

Prerequisites

For compilation native-image depends on the local toolchain, so make sure: glibc-devel, zlib-devel (header files for the C library and zlib) and gcc are available on your system. Unlike Linux or macOS platforms, building native images on Windows requires meeting certain prerequisites. The required Microsoft Visual C++ (MSVC) version depends on the JDK version that GraalVM is based on. For GraalVM distribution based on JDK 8, you will need MSVC 2010 SP1 version. The recommended installation method is using Microsoft Windows SDK 7.1:

  1. Download the SDK file GRMSDKX_EN_DVD.iso for from Microsoft.
  2. Mount the image by opening F:\Setup\SDKSetup.exe directly. For GraalVM distribution based on JDK 11, you will need MSVC 2017 15.5.5 or later version.

After cloning the repository, run

cd substratevm
mx build

echo "public class HelloWorld { public static void main(String[] args) { System.out.println(\"Hello World\"); } }" > HelloWorld.java
$JAVA_HOME/bin/javac HelloWorld.java
mx native-image HelloWorld
./helloworld

To build Truffle-based images please refer to the documentation in the VM suite.

Build Script

Using Substrate VM requires the mx tool to be installed first, so that it is on your PATH. Visit the MX Homepage for more details.

In the main directory, invoke mx help to see the list of commands. Most of the commands are inherited from the Graal and Truffle code bases. The most important commands for the Substrate VM are listed below. More information on the parameters of a command is available by running mx help <command>

  • build: Compile all Java and native code.
  • clean: Remove all compilation artifacts.
  • ideinit: Create project files for Eclipse and other common IDEs. See the documentation on IDE integration for details.

Building images

After running mx build you can use mx native-image to build native images. You can specify the main entry point, i.e., the application you want to create the image for. For more information run mx native-image --help.

Native image generation is performed by a Java program that runs on a JVMCI-enabled JDK. You can debug it with a regular Java debugger. Use mx native-image --debug-attach to start native image generation so that it waits for a Java debugger to attach first (by default, at port 8000). In Eclipse, use the debugging configuration "substratevm-localhost-8000" to attach to it. This debugging configuration is automatically generated by mx ideinit.

If you find yourself having to debug into the Graal level of SubstrateVM, proceed to the debugging page. You can use Ideal Graph Visualizer (IGV) to view individual compilation steps:

mx igv &>/dev/null &
mx native-image HelloWorld -H:Dump= -H:MethodFilter=HelloWorld.*

Images and Entry Points

An native image can be built as a standalone executable, which is the default, or as a shared library by passing --shared to native-image. For an image to be useful, it needs to have at least one entry point method.

For executables, Substrate VM supports Java main methods with a signature that takes the command line arguments as an array of strings:

public static void main(String[] arg) { /* ... */ }

For shared libraries, SVM provides the @CEntryPoint annotation to specify entry point methods that should be exported and callable from C. Entry point methods must be static and may only have non-object parameters and return types – this includes Java primitives, but also Word types (including pointers). One of the parameters of an entry point method has to be of type IsolateThread or Isolate. This parameter provides the current thread's execution context for the call. For example:

@CEntryPoint static int add(IsolateThread thread, int a, int b) {
    return a + b;
}

Shared library builds generate an additional C header file. This header file contains declarations for the SVM C API, which allows creating isolates and attaching threads from C code, as well as declarations for each entry point in user code. The generated C declaration for the above example is:

int add(graal_isolatethread_t* thread, int a, int b);

Both executable images and shared library images can have an arbitrary number of entry points, for example to implement callbacks or APIs.

Options

More information about options, and the important distinction between hosted and runtime options, is available here.

Project Structure

The list of projects is defined in a custom format in the file mx.substratevm/suite.py. It is never necessary to create new projects in the IDE. Instead, a new project is created by adding it in suite.py and running mx ideinit to generate a corresponding IDE project.

Code Formatting

Style rules and procedures for checking adherence are described in the style guide.

Troubleshooting Eclipse

Sometimes, Eclipse gives strange error messages, especially after pulling a bigger changeset. Also, projects are frequently added or removed, leading to error messages about missing projects if you do not import the new projects. The following should reset everything:

  • Delete all projects in Eclipse
  • mx clean
  • mx ideclean
  • mx fsckprojects
  • mx build
  • mx ideinit
  • Import all projects into Eclipse again

License

The Substrate VM is licensed under the GPL 2 with Classpath Exception.