GraalVM is a virtual machine that can run many languages on top of the Java Virtual Machine. It comes with support for JavaScript, Ruby, Python… But what if you're building a DSL, or your language is not listed? Fear not!
In this session we'll discover what it takes to run another language in GraalVM. Using GraalVM, we don't only get a fast runtime, but we'll also get great tool support. With Brainfuck as an example, we'll see how we can run guest languages inside Java applications. It might not bring us profit, but at least it will bring some fun.
10. command meaning
+ increase value
decrease value
. print value to std out
, read one byte from std in
[ if value eq 0, jump after matching ]
] if value neq 0, jump after matching [
< move pointer left
> move pointer right
@mthmulders javaBin
18. A S T
ROOT
INCR_VAL INCR_VAL INCR_VAL INCR_VAL INCR_VAL JUMP INCR_VAL INCR_VAL
DECR_PTR INCR_VAL INCR_PTR DECR_VAL
(part of the program that adds 5 and 2)
@mthmulders javaBin
21. N POJO
public class BFIncrementByteNode extends Node {
/** The slot that holds the data pointer.
private f nal FrameSlot dataPointerSlot;
public BFIncrementByteNode(f nal FrameSlot dataPointerSlot) {
this.dataPointerSlot = dataPointerSlot;
}
@Override public void execute(f nal VirtualFrame frame) {
Find the memory location
f nal int dataPointer = FrameUtil.getIntSafe(frame, dataPointerSlot);
f nal FrameSlot currentByteSlot = frame.getFrameDescriptor().f ndFrameSlot(dataPointer);
Find the current value in that location
f nal byte currentValue = FrameUtil.getByteSafe(frame, currentByteSlot);
Increase the current value
f nal byte newValue = (byte) (1 + currentValue);
Store the current value
frame.setByte(getSlot(frame), newValue);
}
}
@mthmulders javaBin
27. W G VM JVM
function abs (int i)
if ( we saw only positive integers in the input ) {
return i;
} else {
transferToInterpreterAndInvalidate;
return i < 0 ? i : i;
}
}
@mthmulders javaBin
40. G L C
Start the launcher with coverage, and optionally
coverage.Output=detailed
coverage.OutputFile=coverage.txt
------------------------------------------------------------------------------------------------
Code coverage histogram.
Shows what percent of each element was covered during execution
------------------------------------------------------------------------------------------------
Path | Statements | Lines | Roots
------------------------------------------------------------------------------------------------
~/Code/brainfuck jvm/launcher/src/test/resources/sample.bf | 100.00% | 100.00% | 0.00%
------------------------------------------------------------------------------------------------
@mthmulders javaBin
41. W
The Truffle framework has an Instrument API to write other tools:
1. Source code-related events
2. Allocation events
3. Language runtime and thread creation events
4. Application execution events
@mthmulders javaBin