Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
What to Upload to SlideShare
Next
Download to read offline and view in fullscreen.

Share

Building a DSL with GraalVM (Oracle Groundbreaker APAC Virtual Tour)

Download to read offline

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.

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all
  • Be the first to like this

Building a DSL with GraalVM (Oracle Groundbreaker APAC Virtual Tour)

  1. 1. B 
 
DSL
 G VM 
 
 
 
 
 M 
M @mthmuldersOGB
APAC
Virtual
Tour
  2. 2. D 
S 
L https://pxhere.com/en/photo/1057524 @mthmuldersOGB
APAC
Virtual
Tour
  3. 3. SELECT


country,
 








COUNT(1)
AS
count
 FROM




Customer
 GROUP
BY
country;
 @mthmuldersOGB
APAC
Virtual
Tour
  4. 4. Given
my
account
has
a
balance
of
€
430
 When
I
receive
a
money
transfer
of
€
500
 Then
my
account
should
have
a
balance
of
€
930
 @mthmuldersOGB
APAC
Virtual
Tour
  5. 5. Setup(
 



Boat
on
StartingShore,
 



Wolf
on
StartingShore,
 



Sheep
on
StartingShore,
 



Cabbage
on
StartingShore
 )
execute
(
 



Boat
move
Cabbage
to
StartingShore,
 



Boat
move
Sheep
to
DestinationShore,
 



Boat
move
None
to
StartingShore,
 



Boat
move
Cabbage
to
DestinationShore,
 



Boat
move
Sheep
to
StartingShore,
 



Boat
move
Wolf
to
DestinationShore,
 



Boat
move
None
to
StartingShore,
 



Boat
move
Sheep
to
DestinationShore
 )
 https://github.com/NRBPerdijk/dsl-for-the-dense/ @mthmuldersOGB
APAC
Virtual
Tour
  6. 6. +
+
>
+
+
+
+
+
[
<
+
>
-
]
+
+
+
+
+
+
+
+
[
<
+
+
+
+
+
+
>
-
]
<
.
 @mthmuldersOGB
APAC
Virtual
Tour
  7. 7. B This
program
adds
the
numbers
2
and
5
 
 +
+















Store
the
number
2
in
the
f rst
slot
 >
+
+
+
+
+







Store
the
number
5
in
the
second
slot
 
 [

















Move
back
and
forth
between
f rst
and
second
slot
 
 



<
+
>
-







While
"moving"
ones
from
the
second
to
the
f rst
slot
 ]
 
 The
f rst
slot
now
has
'7'
in
it
but
we
need
to
output
the
ASCII
value
for
that
 The
ASCII
value
is
48
higher
than
the
number
we
have
 
 The
second
slot
is
now
empty
 It
will
act
as
a
counter
so
we
can
add
6
*
8
to
the
f rst
slot
 +
+
+
+
+
+
+
+



Store
8
in
it
 
 [

















Again
move
back
and
forth
between
f rst
and
second
slot
 



<
+
+
+
+
+
+

while
adding
6
to
the
f rst
slot
 



>
-











before
reducing
the
counter
in
the
second
slot
 ]
 
 <
.















Go
back
to
the
f rst
slot
and
print
it
 @mthmuldersOGB
APAC
Virtual
Tour
  8. 8. 0 1 2 3 4 ... 29
997 29
998 29
999 2 5 3 0 0 0 0 0 ↑ @mthmuldersOGB
APAC
Virtual
Tour
  9. 9. 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 @mthmuldersOGB
APAC
Virtual
Tour
  10. 10. https://www.thepinkhumanist.com/articles/330-life-of-alan-turing-examined-in-a-new-graphic-novel @mthmuldersOGB
APAC
Virtual
Tour
  11. 11. https://commons.wikimedia.org/wiki/File:USA_tar_bubble_la_brea_CA.jpg @mthmuldersOGB
APAC
Virtual
Tour
  12. 12. G VM “One
VM
to
rule
them
all cc-by-sa/2.0
-
©
Lairich
Rig
-
https://www.geograph.org.uk/photo/3203827 @mthmuldersOGB
APAC
Virtual
Tour
  13. 13. @mthmuldersOGB
APAC
Virtual
Tour
  14. 14. B yapi.bf
calculating
15
digits
of
π Runtime Average
time
(ms/op) Error Java
HotSpot(TM)
64-Bit
Server
VM 53 ±
1 OpenJDK
GraalVM
CE
19.0.0 45 ±
1 All
tests
are
ran
on
an
2018
MacBook
Pro
with
2.6
GHz
Intel
Core
i7
and
16
GB
2400
MHz
DDR4.
The
machine
runs
macOS
Mojave
10.14.4
and
JDK 1.8.0_212.
Tests
measured
with
 .
Each
test
ran
5
times
with
5
warmup
iterations.jmh @mthmuldersOGB
APAC
Virtual
Tour
  15. 15. B yapi.bf
calculating
45
digits
of
π Runtime Average
time
(ms/op) Error Java
HotSpot(TM)
64-Bit
Server
VM 207 ±
2 OpenJDK
GraalVM
CE
19.0.0 185 ±
3 All
tests
are
ran
on
an
2018
MacBook
Pro
with
2.6
GHz
Intel
Core
i7
and
16
GB
2400
MHz
DDR4.
The
machine
runs
macOS
Mojave
10.14.4
and
JDK 1.8.0_212.
Tests
measured
with
 .
Each
test
ran
5
times
with
5
warmup
iterations.jmh @mthmuldersOGB
APAC
Virtual
Tour
  16. 16. T cc-by-sa/2.5
-
©
Darvin
DeShazer
-
https://mushroomobserver.org/2538 “open
source
library
for
building programming
language implementations
as
interpreters
for self-modifying
Abstract
Syntax
Trees. @mthmuldersOGB
APAC
Virtual
Tour
  17. 17. 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) @mthmuldersOGB
APAC
Virtual
Tour
  18. 18. https://chrisseaton.com/rubytruffle/pldi17-truffle/pldi17-truffle.pdf @mthmuldersOGB
APAC
Virtual
Tour
  19. 19. P 
E Calculate
 
for
positive
integers If
we
know
(or
assume)
that
 ,
the
program
becomes
simpler: x n f (x, n) = ⎧ ⎩ ⎨ ⎪ ⎪ 1 ,(f (x, 0.5 ∗ n)) 2 x ∗ f (x, n − 1), if n = 0 if n is
even otherwise n = 5 f (x) = x ∗ ( )x 2 2 @mthmuldersOGB
APAC
Virtual
Tour
  20. 20. 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);
 



}
 }
 @mthmuldersOGB
APAC
Virtual
Tour
  21. 21. B https://pxhere.com/en/photo/493605 @mthmuldersOGB
APAC
Virtual
Tour
  22. 22. B @Override
 public
void
execute(f nal
VirtualFrame
frame)
{
 



f nal
int
currentValue
=
someCalculation();
 



doPrint(getContext().getOutput(),
(char)
currentValue);
 }
 
 @TruffleBoundary
 private
void
doPrint(f nal
PrintWriter
out,
f nal
char
value)
{
 



out.print(value);
 



out.flush();
 }
 @mthmuldersOGB
APAC
Virtual
Tour
  23. 23. S https://www.mammoet.com/cases/Tennet/ @mthmuldersOGB
APAC
Virtual
Tour
  24. 24. S @Specialization(guards
=
"b
 
0")
 public
double
divide(int
a,
int
b)
{
 



return
a
/
b;
 }
 @mthmuldersOGB
APAC
Virtual
Tour
  25. 25. S @Specialization(rewriteOn
=
ArithmeticException.class)
 int
doAddNoOverflow(int
a,
int
b)
{
 



return
Math.addExact(a,
b);
 }
 
 @Specialization
 long
doAddWithOverflow(int
a,
int
b)
{
 



return
a
+
b;
 }
 execute(Integer.MAX_VALUE
-
1,
1)
 
doAddNoOverflow(Integer.MAX_VALUE
-
1,
1)
 
 execute(Integer.MAX_VALUE



,
1)
 
doAddNoOverflow(Integer.MAX_VALUE,
1)
 




































throws
ArithmeticException
 

































 
doAddWithOverflow(Integer.MAX_VALUE,
1)
 
 execute(Integer.MAX_VALUE
-
1,
1)
 
doAddWithOverflow(Integer.MAX_VALUE
-
1,
1)
 @mthmuldersOGB
APAC
Virtual
Tour
  26. 26. 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;
 



}
 }
 @mthmuldersOGB
APAC
Virtual
Tour
  27. 27. B 
 
 
 
T @mthmuldersOGB
APAC
Virtual
Tour
  28. 28. @mthmuldersOGB
APAC
Virtual
Tour
  29. 29. L Converts
a
sequence
of
characters
into
a
sequence
of
tokens. 
 
 
 
 @mthmuldersOGB
APAC
Virtual
Tour
  30. 30. P Converts
a
sequence
of
tokens
into
(hierarchical)
data
structure. 
 
 
 
 @mthmuldersOGB
APAC
Virtual
Tour
  31. 31. P 
 
L 
 1.
Write
some
regular
expressions 2.
Use
a
parser
generator
(like
 )ANTLR cc-by-nc/2.5
-
©
Randall
Munroe
-
https://www.xkcd.com/1171/ @mthmuldersOGB
APAC
Virtual
Tour
  32. 32. I 
 
 @mthmuldersOGB
APAC
Virtual
Tour
  33. 33. G VM
U GraalVM
comes
with
the
GraalVM
Updater
(gu) Use
gu
to
install
components,
such
as
language
packs
or
tools. e.g.
gu
install
native­image gu
-L
install
brainfuck-0.1-SNAPSHOT-component.jar
 @mthmuldersOGB
APAC
Virtual
Tour
  34. 34. C Distribute
your
language
implementation
as
a
component: $
tree
 .
 ├──
META-INF
 │


├──
MANIFEST.MF
 │


├──
permissions
 │


└──
symlinks
 └──
jre
 



└──
languages
 







└──
bf
 











├──
bin
 











│


└──
bf
 











├──
brainfuck.jar
 











└──
launcher
 















└──
bf launcher.jar
 
 6
directories,
6
f les
 @mthmuldersOGB
APAC
Virtual
Tour
  35. 35. U 
 
 
 1.
Prepare
source
code 2.
Prepare
GraalVM
polyglot
context 3.
Evaluate
the
source
code input
=
"+
+
>
+
+
+
+
+
[
<
+
>
-
]
+
+
+
+
+
+
+
+
[
<
+
+
+
+
+
+
>
-
]
<
.";
 source
=
Source.newBuilder("bf",
input,
"user
input").build();
 output
=
new
ByteArrayOutputStream();
 context
=
Context.newBuilder("bf").out(output).build();
 context.eval(source);
 System.out.println(output.toString());
 @mthmuldersOGB
APAC
Virtual
Tour
  36. 36. T “Implementing
your
own
language using
GraalVM
will
not
only
give
you high
performance.
More
importantly,
it allows
your
language
to
connect
with the
rich
tooling
provided
by
the GraalVM
ecosystem. https://www.graalvm.org/docs/graalvm-as-a-platform/ https://pxhere.com/en/photo/1067853 @mthmuldersOGB
APAC
Virtual
Tour
  37. 37. G 
L 
D Start
the
launcher
with
­­inspect Debugger
listening
on
port
9229.
 To
start
debugging,
open
the
following
URL
in
Chrome:
 



chrome­devtools://devtools/bundled/js_app.html?ws=127.0.0.1:9229/77b52d12­53f20cb0ab35
 @mthmuldersOGB
APAC
Virtual
Tour
  38. 38. @mthmuldersOGB
APAC
Virtual
Tour
  39. 39. 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%

 
 @mthmuldersOGB
APAC
Virtual
Tour
  40. 40. 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 @mthmuldersOGB
APAC
Virtual
Tour
  41. 41. W 
 Yes,
you
can
run
any
language
with
GraalVM.
 ...
but
it
may
take
some
time. It's
certainly
fun
 ...
and
it
might
even
be
profitable. @mthmuldersOGB
APAC
Virtual
Tour
  42. 42. T 
 You
don't
need
to
write
a
parser
yourself
 (and
maybe
you
don't
want
to,
either) Take
time
to
think
about
the
AST using
a
wrong
structure
leads
to
hard-to-track
bugs refactoring
it
later
is
very
hard
and
time-consuming @mthmuldersOGB
APAC
Virtual
Tour
  43. 43. Q
 
A 
 
 
 
 Sample
code:
 Follow-up
project:
 http://bit.ly/brainfuck-jvm https://bit.ly/scheme4graalvm @mthmuldersOGB
APAC
Virtual
Tour

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.

Views

Total views

63

On Slideshare

0

From embeds

0

Number of embeds

0

Actions

Downloads

1

Shares

0

Comments

0

Likes

0

×