Building a DSL with GraalVM (CodeOne)

B DSLB DSL
G VMG VM
M MM M
D S LD S L
https://pxhere.com/en/photo/1057524
SELECT country,
COUNT(1) AS count
FROM Customer
GROUP BY country;
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
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/
+ + > + + + + + [ < + > - ] + + + + + + + + [ < + + + + + + > - ] < .
B *B *
This program adds the numbers 2 and 5
+ + Store the number 2 in the first slot
> + + + + + Store the number 5 in the second slot
[ Move back and forth between first and second slot
< + > - While "moving" ones from the second to the first slot
]
The first 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 first slot
+ + + + + + + + Store 8 in it
[ Again move back and forth between first and second slot
< + + + + + + while adding 6 to the first slot
> - before reducing the counter in the second slot
]
< . Go back to the first slot and print it
0 1 2 3 4 ... 29 997 29 998 29 999
2 5 3 0 0 0 0 0
↑
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
https://www.thepinkhumanist.com/articles/330-life-of-alan-turing-examined-in-a-new-graphic-novel
https://commons.wikimedia.org/wiki/File:USA_tar_bubble_la_brea_CA.jpg
G VMG VM
“One VM to rule them all
cc-by-sa/2.0 - © Lairich Rig - https://www.geograph.org.uk/photo/3203827
Building a DSL with GraalVM (CodeOne)
BB
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
BB
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
TT
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.
A S TA 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)
https://chrisseaton.com/rubytruffle/pldi17-truffle/pldi17-truffle.pdf
P EP 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
BB
https://pxhere.com/en/photo/493605
BB
@Override
public void execute(final VirtualFrame frame) {
final int currentValue = someCalculation();
doPrint(getContext().getOutput(), (char) currentValue);
}
@TruffleBoundary
private void doPrint(final PrintWriter out, final char value) {
out.print(value);
out.flush();
}
SS
https://www.mammoet.com/cases/Tennet/
SS
@Specialization(guards = "b 0")
public double divide(int a, int b) {
return a / b;
}
SS
@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)
W G VM JVMW 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;
}
}
B TB T
Building a DSL with GraalVM (CodeOne)
LL
Converts a sequence of characters into a sequence of tokens.
PP
Converts a sequence of tokens into (hierarchical) data structure.
P LP 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/
II
G VM UG 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 component-0.1-SNAPSHOT.jar
CC
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 files
UU
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());
TT
“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
DD
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
Building a DSL with GraalVM (CodeOne)
OO
The Truffle framework has an Instrument API to write other tools, e.g.
code coverage measurement, profilers.
1. Source code-related events
2. Allocation events
3. Language runtime and thread creation events
4. Application execution events
WW
Yes, you can run any language with GraalVM.
... but it may take some time.
It's certainly fun
... and it might even be profitable.
TT
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
@mthmulders  Oracle Code One
Q AQ A
Sample code:
Please help conference organisers: rate this talk
in the app or at the panel outside the door!
http://bit.ly/brainfuck-jvm
1 of 41

Recommended

Building a DSL with GraalVM (VoxxedDays Luxembourg) by
Building a DSL with GraalVM (VoxxedDays Luxembourg)Building a DSL with GraalVM (VoxxedDays Luxembourg)
Building a DSL with GraalVM (VoxxedDays Luxembourg)Maarten Mulders
239 views41 slides
Redis as a message queue by
Redis as a message queueRedis as a message queue
Redis as a message queueBrandon Lamb
8.7K views10 slides
Redis & ZeroMQ: How to scale your application by
Redis & ZeroMQ: How to scale your applicationRedis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your applicationrjsmelo
23.4K views33 slides
Rubinius @ RubyAndRails2010 by
Rubinius @ RubyAndRails2010Rubinius @ RubyAndRails2010
Rubinius @ RubyAndRails2010Dirkjan Bussink
928 views46 slides
最近作ったN個のCPANモジュール Yokohama.pm #10 by
最近作ったN個のCPANモジュール Yokohama.pm #10最近作ったN個のCPANモジュール Yokohama.pm #10
最近作ったN個のCPANモジュール Yokohama.pm #10Masahiro Nagano
2.7K views27 slides
RestMQ - HTTP/Redis based Message Queue by
RestMQ - HTTP/Redis based Message QueueRestMQ - HTTP/Redis based Message Queue
RestMQ - HTTP/Redis based Message QueueGleicon Moraes
121.4K views25 slides

More Related Content

What's hot

GoLang & GoatCore by
GoLang & GoatCore GoLang & GoatCore
GoLang & GoatCore Sebastian Pożoga
486 views31 slides
我在豆瓣使用Emacs by
我在豆瓣使用Emacs我在豆瓣使用Emacs
我在豆瓣使用Emacs董 伟明
1.2K views34 slides
PyCon KR 2019 sprint - RustPython by example by
PyCon KR 2019 sprint  - RustPython by examplePyCon KR 2019 sprint  - RustPython by example
PyCon KR 2019 sprint - RustPython by exampleYunWon Jeong
190 views12 slides
BOSH deploys distributed systems, and Diego runs any containers by
BOSH deploys distributed systems, and Diego runs any containersBOSH deploys distributed systems, and Diego runs any containers
BOSH deploys distributed systems, and Diego runs any containersBenjamin Gandon
506 views21 slides
Cluj Big Data Meetup - Big Data in Practice by
Cluj Big Data Meetup - Big Data in PracticeCluj Big Data Meetup - Big Data in Practice
Cluj Big Data Meetup - Big Data in PracticeSteffen Wenz
3.2K views39 slides
37562259 top-consuming-process by
37562259 top-consuming-process37562259 top-consuming-process
37562259 top-consuming-processskumner
159 views3 slides

What's hot(20)

我在豆瓣使用Emacs by 董 伟明
我在豆瓣使用Emacs我在豆瓣使用Emacs
我在豆瓣使用Emacs
董 伟明1.2K views
PyCon KR 2019 sprint - RustPython by example by YunWon Jeong
PyCon KR 2019 sprint  - RustPython by examplePyCon KR 2019 sprint  - RustPython by example
PyCon KR 2019 sprint - RustPython by example
YunWon Jeong190 views
BOSH deploys distributed systems, and Diego runs any containers by Benjamin Gandon
BOSH deploys distributed systems, and Diego runs any containersBOSH deploys distributed systems, and Diego runs any containers
BOSH deploys distributed systems, and Diego runs any containers
Benjamin Gandon506 views
Cluj Big Data Meetup - Big Data in Practice by Steffen Wenz
Cluj Big Data Meetup - Big Data in PracticeCluj Big Data Meetup - Big Data in Practice
Cluj Big Data Meetup - Big Data in Practice
Steffen Wenz3.2K views
37562259 top-consuming-process by skumner
37562259 top-consuming-process37562259 top-consuming-process
37562259 top-consuming-process
skumner159 views
All you need to know about the JavaScript event loop by Saša Tatar
All you need to know about the JavaScript event loopAll you need to know about the JavaScript event loop
All you need to know about the JavaScript event loop
Saša Tatar981 views
Goroutine stack and local variable allocation in Go by Yu-Shuan Hsieh
Goroutine stack and local variable allocation in GoGoroutine stack and local variable allocation in Go
Goroutine stack and local variable allocation in Go
Yu-Shuan Hsieh340 views
Apache Hadoop for System Administrators by Allen Wittenauer
Apache Hadoop for System AdministratorsApache Hadoop for System Administrators
Apache Hadoop for System Administrators
Allen Wittenauer4.3K views
Cluj.py Meetup: Extending Python in C by Steffen Wenz
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
Steffen Wenz1.1K views
From Zero to Application Delivery with NixOS by Susan Potter
From Zero to Application Delivery with NixOSFrom Zero to Application Delivery with NixOS
From Zero to Application Delivery with NixOS
Susan Potter2.9K views
Cocoa勉強会23-識別情報の変換〜文字エンコードとデータタイプ by Masayuki Nii
Cocoa勉強会23-識別情報の変換〜文字エンコードとデータタイプCocoa勉強会23-識別情報の変換〜文字エンコードとデータタイプ
Cocoa勉強会23-識別情報の変換〜文字エンコードとデータタイプ
Masayuki Nii575 views
Go Concurrency by jgrahamc
Go ConcurrencyGo Concurrency
Go Concurrency
jgrahamc14.3K views
HAB Software Woes by jgrahamc
HAB Software WoesHAB Software Woes
HAB Software Woes
jgrahamc4.8K views
Go memory by jgrahamc
Go memoryGo memory
Go memory
jgrahamc11K views

Similar to Building a DSL with GraalVM (CodeOne)

Cpp tutorial by
Cpp tutorialCpp tutorial
Cpp tutorialVikas Sharma
260 views23 slides
Bytes in the Machine: Inside the CPython interpreter by
Bytes in the Machine: Inside the CPython interpreterBytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreterakaptur
1.8K views51 slides
DEF CON 27 - PATRICK WARDLE - harnessing weapons of Mac destruction by
DEF CON 27 - PATRICK WARDLE - harnessing weapons of Mac destructionDEF CON 27 - PATRICK WARDLE - harnessing weapons of Mac destruction
DEF CON 27 - PATRICK WARDLE - harnessing weapons of Mac destructionFelipe Prado
43 views70 slides
CppTutorial.ppt by
CppTutorial.pptCppTutorial.ppt
CppTutorial.pptHODZoology3
3 views23 slides
DevSecCon London 2017 - MacOS security, hardening and forensics 101 by Ben Hu... by
DevSecCon London 2017 - MacOS security, hardening and forensics 101 by Ben Hu...DevSecCon London 2017 - MacOS security, hardening and forensics 101 by Ben Hu...
DevSecCon London 2017 - MacOS security, hardening and forensics 101 by Ben Hu...DevSecCon
906 views59 slides
Penetration Testing for Easy RM to MP3 Converter Application and Post Exploit by
Penetration Testing for Easy RM to MP3 Converter Application and Post ExploitPenetration Testing for Easy RM to MP3 Converter Application and Post Exploit
Penetration Testing for Easy RM to MP3 Converter Application and Post ExploitJongWon Kim
1.6K views21 slides

Similar to Building a DSL with GraalVM (CodeOne)(20)

Bytes in the Machine: Inside the CPython interpreter by akaptur
Bytes in the Machine: Inside the CPython interpreterBytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreter
akaptur1.8K views
DEF CON 27 - PATRICK WARDLE - harnessing weapons of Mac destruction by Felipe Prado
DEF CON 27 - PATRICK WARDLE - harnessing weapons of Mac destructionDEF CON 27 - PATRICK WARDLE - harnessing weapons of Mac destruction
DEF CON 27 - PATRICK WARDLE - harnessing weapons of Mac destruction
Felipe Prado43 views
DevSecCon London 2017 - MacOS security, hardening and forensics 101 by Ben Hu... by DevSecCon
DevSecCon London 2017 - MacOS security, hardening and forensics 101 by Ben Hu...DevSecCon London 2017 - MacOS security, hardening and forensics 101 by Ben Hu...
DevSecCon London 2017 - MacOS security, hardening and forensics 101 by Ben Hu...
DevSecCon906 views
Penetration Testing for Easy RM to MP3 Converter Application and Post Exploit by JongWon Kim
Penetration Testing for Easy RM to MP3 Converter Application and Post ExploitPenetration Testing for Easy RM to MP3 Converter Application and Post Exploit
Penetration Testing for Easy RM to MP3 Converter Application and Post Exploit
JongWon Kim1.6K views
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015 by Windows Developer
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Windows Developer910 views
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U... by Vincenzo Iozzo
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Vincenzo Iozzo714 views
How to go the extra mile on monitoring by Tiago Simões
How to go the extra mile on monitoringHow to go the extra mile on monitoring
How to go the extra mile on monitoring
Tiago Simões128 views
DevOps(4) : Ansible(2) - (MOSG) by Soshi Nemoto
DevOps(4) : Ansible(2) - (MOSG)DevOps(4) : Ansible(2) - (MOSG)
DevOps(4) : Ansible(2) - (MOSG)
Soshi Nemoto851 views
Bootstrap your Cloud Infrastructure using puppet and hashicorp stack by Bram Vogelaar
Bootstrap your Cloud Infrastructure using puppet and hashicorp stackBootstrap your Cloud Infrastructure using puppet and hashicorp stack
Bootstrap your Cloud Infrastructure using puppet and hashicorp stack
Bram Vogelaar399 views
Drizzle to MySQL, Stress Free Migration by Andrew Hutchings
Drizzle to MySQL, Stress Free MigrationDrizzle to MySQL, Stress Free Migration
Drizzle to MySQL, Stress Free Migration
Andrew Hutchings1.1K views
Dependencies Managers in C/C++. Using stdcpp 2014 by biicode
Dependencies Managers in C/C++. Using stdcpp 2014Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014
biicode4.4K views
Python gis by John Zhou
Python gisPython gis
Python gis
John Zhou643 views
From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016 by Susan Potter
From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016
From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016
Susan Potter2.5K views
Patterns and Tools for Database Versioning, Migration, Data Loading and Test ... by Alan Pinstein
Patterns and Tools for Database Versioning, Migration, Data Loading and Test ...Patterns and Tools for Database Versioning, Migration, Data Loading and Test ...
Patterns and Tools for Database Versioning, Migration, Data Loading and Test ...
Alan Pinstein5.2K views
Basicsof c make and git for a hello qt application by Dinesh Manajipet
Basicsof c make and git for a hello qt applicationBasicsof c make and git for a hello qt application
Basicsof c make and git for a hello qt application
Dinesh Manajipet563 views

More from Maarten Mulders

What's cooking in Maven? (Devoxx FR) by
What's cooking in Maven? (Devoxx FR)What's cooking in Maven? (Devoxx FR)
What's cooking in Maven? (Devoxx FR)Maarten Mulders
173 views25 slides
Making Maven Marvellous (Devnexus) by
Making Maven Marvellous (Devnexus)Making Maven Marvellous (Devnexus)
Making Maven Marvellous (Devnexus)Maarten Mulders
149 views13 slides
Making Maven Marvellous (Java.il) by
Making Maven Marvellous (Java.il)Making Maven Marvellous (Java.il)
Making Maven Marvellous (Java.il)Maarten Mulders
146 views13 slides
Making Maven Marvellous (JavaZone) by
Making Maven Marvellous (JavaZone)Making Maven Marvellous (JavaZone)
Making Maven Marvellous (JavaZone)Maarten Mulders
90 views13 slides
Dapr: Dinosaur or Developer's Dream? (v1) by
Dapr: Dinosaur or Developer's Dream? (v1)Dapr: Dinosaur or Developer's Dream? (v1)
Dapr: Dinosaur or Developer's Dream? (v1)Maarten Mulders
132 views42 slides
Dapr: Dinosaur or Developer Dream? (J-Fall) by
Dapr: Dinosaur or Developer Dream? (J-Fall)Dapr: Dinosaur or Developer Dream? (J-Fall)
Dapr: Dinosaur or Developer Dream? (J-Fall)Maarten Mulders
138 views42 slides

More from Maarten Mulders(20)

What's cooking in Maven? (Devoxx FR) by Maarten Mulders
What's cooking in Maven? (Devoxx FR)What's cooking in Maven? (Devoxx FR)
What's cooking in Maven? (Devoxx FR)
Maarten Mulders173 views
Making Maven Marvellous (Devnexus) by Maarten Mulders
Making Maven Marvellous (Devnexus)Making Maven Marvellous (Devnexus)
Making Maven Marvellous (Devnexus)
Maarten Mulders149 views
Making Maven Marvellous (Java.il) by Maarten Mulders
Making Maven Marvellous (Java.il)Making Maven Marvellous (Java.il)
Making Maven Marvellous (Java.il)
Maarten Mulders146 views
Dapr: Dinosaur or Developer's Dream? (v1) by Maarten Mulders
Dapr: Dinosaur or Developer's Dream? (v1)Dapr: Dinosaur or Developer's Dream? (v1)
Dapr: Dinosaur or Developer's Dream? (v1)
Maarten Mulders132 views
Dapr: Dinosaur or Developer Dream? (J-Fall) by Maarten Mulders
Dapr: Dinosaur or Developer Dream? (J-Fall)Dapr: Dinosaur or Developer Dream? (J-Fall)
Dapr: Dinosaur or Developer Dream? (J-Fall)
Maarten Mulders138 views
React in 40 minutes (Voxxed Days Romania) by Maarten Mulders
React in 40 minutes (Voxxed Days Romania) React in 40 minutes (Voxxed Days Romania)
React in 40 minutes (Voxxed Days Romania)
Maarten Mulders93 views
React in 50 minutes (Bucharest Software Craftsmanship Community) by Maarten Mulders
React in 50 minutes (Bucharest Software Craftsmanship Community)React in 50 minutes (Bucharest Software Craftsmanship Community)
React in 50 minutes (Bucharest Software Craftsmanship Community)
Maarten Mulders244 views
React in 50 Minutes (JNation) by Maarten Mulders
 React in 50 Minutes (JNation)  React in 50 Minutes (JNation)
React in 50 Minutes (JNation)
Maarten Mulders143 views
Building a DSL with GraalVM (Oracle Groundbreaker APAC Virtual Tour) by Maarten Mulders
Building a DSL with GraalVM (Oracle Groundbreaker APAC Virtual Tour)Building a DSL with GraalVM (Oracle Groundbreaker APAC Virtual Tour)
Building a DSL with GraalVM (Oracle Groundbreaker APAC Virtual Tour)
Maarten Mulders128 views
SSL/TLS for Mortals (Oracle Groundbreaker EMEA Virtual Tour) by Maarten Mulders
SSL/TLS for Mortals (Oracle Groundbreaker EMEA Virtual Tour)SSL/TLS for Mortals (Oracle Groundbreaker EMEA Virtual Tour)
SSL/TLS for Mortals (Oracle Groundbreaker EMEA Virtual Tour)
Maarten Mulders114 views
SSL/TLS for Mortals (UtrechtJUG) by Maarten Mulders
SSL/TLS for Mortals (UtrechtJUG)SSL/TLS for Mortals (UtrechtJUG)
SSL/TLS for Mortals (UtrechtJUG)
Maarten Mulders202 views
Building a DSL with GraalVM (javaBin online) by Maarten Mulders
Building a DSL with GraalVM (javaBin online)Building a DSL with GraalVM (javaBin online)
Building a DSL with GraalVM (javaBin online)
Maarten Mulders221 views
SSL/TLS for Mortals (Lockdown Lecture) by Maarten Mulders
SSL/TLS for Mortals (Lockdown Lecture)SSL/TLS for Mortals (Lockdown Lecture)
SSL/TLS for Mortals (Lockdown Lecture)
Maarten Mulders122 views
React in 50 Minutes (OpenValue) by Maarten Mulders
React in 50 Minutes (OpenValue) React in 50 Minutes (OpenValue)
React in 50 Minutes (OpenValue)
Maarten Mulders162 views
React in 50 Minutes (DevNexus) by Maarten Mulders
React in 50 Minutes (DevNexus) React in 50 Minutes (DevNexus)
React in 50 Minutes (DevNexus)
Maarten Mulders114 views

Recently uploaded

Using Qt under LGPL-3.0 by
Using Qt under LGPL-3.0Using Qt under LGPL-3.0
Using Qt under LGPL-3.0Burkhard Stubert
12 views11 slides
WebAssembly by
WebAssemblyWebAssembly
WebAssemblyJens Siebert
52 views18 slides
Agile 101 by
Agile 101Agile 101
Agile 101John Valentino
9 views20 slides
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium... by
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Lisi Hocke
35 views124 slides
Gen Apps on Google Cloud PaLM2 and Codey APIs in Action by
Gen Apps on Google Cloud PaLM2 and Codey APIs in ActionGen Apps on Google Cloud PaLM2 and Codey APIs in Action
Gen Apps on Google Cloud PaLM2 and Codey APIs in ActionMárton Kodok
11 views55 slides
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated... by
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...TomHalpin9
6 views29 slides

Recently uploaded(20)

Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium... by Lisi Hocke
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Lisi Hocke35 views
Gen Apps on Google Cloud PaLM2 and Codey APIs in Action by Márton Kodok
Gen Apps on Google Cloud PaLM2 and Codey APIs in ActionGen Apps on Google Cloud PaLM2 and Codey APIs in Action
Gen Apps on Google Cloud PaLM2 and Codey APIs in Action
Márton Kodok11 views
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated... by TomHalpin9
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
TomHalpin96 views
FIMA 2023 Neo4j & FS - Entity Resolution.pptx by Neo4j
FIMA 2023 Neo4j & FS - Entity Resolution.pptxFIMA 2023 Neo4j & FS - Entity Resolution.pptx
FIMA 2023 Neo4j & FS - Entity Resolution.pptx
Neo4j12 views
FOSSLight Community Day 2023-11-30 by Shane Coughlan
FOSSLight Community Day 2023-11-30FOSSLight Community Day 2023-11-30
FOSSLight Community Day 2023-11-30
Shane Coughlan5 views
Sprint 226 by ManageIQ
Sprint 226Sprint 226
Sprint 226
ManageIQ8 views
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports by Ra'Fat Al-Msie'deen
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug ReportsBushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
Generic or specific? Making sensible software design decisions by Bert Jan Schrijver
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
JioEngage_Presentation.pptx by admin125455
JioEngage_Presentation.pptxJioEngage_Presentation.pptx
JioEngage_Presentation.pptx
admin1254556 views
Myths and Facts About Hospice Care: Busting Common Misconceptions by Care Coordinations
Myths and Facts About Hospice Care: Busting Common MisconceptionsMyths and Facts About Hospice Care: Busting Common Misconceptions
Myths and Facts About Hospice Care: Busting Common Misconceptions
360 graden fabriek by info33492
360 graden fabriek360 graden fabriek
360 graden fabriek
info33492138 views
Dapr Unleashed: Accelerating Microservice Development by Miroslav Janeski
Dapr Unleashed: Accelerating Microservice DevelopmentDapr Unleashed: Accelerating Microservice Development
Dapr Unleashed: Accelerating Microservice Development
Miroslav Janeski12 views
Copilot Prompting Toolkit_All Resources.pdf by Riccardo Zamana
Copilot Prompting Toolkit_All Resources.pdfCopilot Prompting Toolkit_All Resources.pdf
Copilot Prompting Toolkit_All Resources.pdf
Riccardo Zamana11 views
Software evolution understanding: Automatic extraction of software identifier... by Ra'Fat Al-Msie'deen
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...
AI and Ml presentation .pptx by FayazAli87
AI and Ml presentation .pptxAI and Ml presentation .pptx
AI and Ml presentation .pptx
FayazAli8712 views

Building a DSL with GraalVM (CodeOne)

  • 1. B DSLB DSL G VMG VM M MM M
  • 2. D S LD S L https://pxhere.com/en/photo/1057524
  • 3. SELECT country, COUNT(1) AS count FROM Customer GROUP BY country;
  • 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
  • 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/
  • 6. + + > + + + + + [ < + > - ] + + + + + + + + [ < + + + + + + > - ] < .
  • 7. B *B * This program adds the numbers 2 and 5 + + Store the number 2 in the first slot > + + + + + Store the number 5 in the second slot [ Move back and forth between first and second slot < + > - While "moving" ones from the second to the first slot ] The first 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 first slot + + + + + + + + Store 8 in it [ Again move back and forth between first and second slot < + + + + + + while adding 6 to the first slot > - before reducing the counter in the second slot ] < . Go back to the first slot and print it
  • 8. 0 1 2 3 4 ... 29 997 29 998 29 999 2 5 3 0 0 0 0 0 ↑
  • 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
  • 12. G VMG VM “One VM to rule them all cc-by-sa/2.0 - © Lairich Rig - https://www.geograph.org.uk/photo/3203827
  • 14. BB 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
  • 15. BB 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
  • 16. TT 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.
  • 17. A S TA 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)
  • 19. P EP 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
  • 21. BB @Override public void execute(final VirtualFrame frame) { final int currentValue = someCalculation(); doPrint(getContext().getOutput(), (char) currentValue); } @TruffleBoundary private void doPrint(final PrintWriter out, final char value) { out.print(value); out.flush(); }
  • 23. SS @Specialization(guards = "b 0") public double divide(int a, int b) { return a / b; }
  • 24. SS @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)
  • 25. W G VM JVMW 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; } }
  • 28. LL Converts a sequence of characters into a sequence of tokens.
  • 29. PP Converts a sequence of tokens into (hierarchical) data structure.
  • 30. P LP 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/
  • 31. II
  • 32. G VM UG 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 component-0.1-SNAPSHOT.jar
  • 33. CC 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 files
  • 34. UU 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());
  • 35. TT “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
  • 36. DD 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
  • 38. OO The Truffle framework has an Instrument API to write other tools, e.g. code coverage measurement, profilers. 1. Source code-related events 2. Allocation events 3. Language runtime and thread creation events 4. Application execution events
  • 39. WW Yes, you can run any language with GraalVM. ... but it may take some time. It's certainly fun ... and it might even be profitable.
  • 40. TT 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
  • 41. @mthmulders  Oracle Code One Q AQ A Sample code: Please help conference organisers: rate this talk in the app or at the panel outside the door! http://bit.ly/brainfuck-jvm