5. Interpreters under the hood
1. Tokenize
2. Parse
3. AST
Ruby Tokens
Abstract Syntax Tree
nodes
C
Machine
Language
interpret
6. Abstract Syntax Tree
function sum(n) {
var sum = 0;
for (var i = 1; i < n; i++) {
sum += i;
}
return sum;
}
source: T. Würthinger, C. Wimmer, A. Wöß, L. Stadler, G.
Duboscq, C. Humer, G. Richards, D. Simon, M. Wolczko:
One VM to Rule Them All. In Proceedings of Onward!, ACM
Press, 2013.
7. Interpreter on the JVM
Ruby source code compiled to Java bytecode
Operating system
Libraries
JRuby interpreter
Java Runtime Env.
GC Libraries
Why?
Existing solutions
8. The future is here: Truffle and Graal
Goal: Native support for interpreters
Requirements:
● simplicity
● generality
● performance
9. System structure
source: Christian Wimmer and Chris Seaton speak at the JVM Language Summit, July 31, 2013.
Ruby
Javascript Python
R ...
Your language here
TruffleGraal
Language agnostic
dynamic compiler Common API between
language implementation
and optimization system
Substrate VM
Graal VM
10. Truffle
● Java framework for writing AST interpreters
● node specialization with tree rewriting
● define compiler optimizations, directives
○ assumptions
11. Graal
● 100% Java-based JIT framework
○ JIT: Just In Time (compiler)
● dynamic compiler
● directly control code generation
● graph intermediate representation
12. Node rewrite - optimization
source: T. Würthinger, C. Wimmer, A. Wöß, L. Stadler, G. Duboscq, C. Humer, G. Richards, D. Simon,
M. Wolczko: One VM to Rule Them All. In Proceedings of Onward!, ACM Press, 2013.
13. Node rewrite - de- and reoptimization
source: T. Würthinger, C. Wimmer, A. Wöß, L. Stadler, G. Duboscq, C. Humer, G. Richards, D. Simon,
M. Wolczko: One VM to Rule Them All. In Proceedings of Onward!, ACM Press, 2013.
14. Bonus
“Write your own language!”
Before Truffle:
● write a parser and an AST interpreter
● create a runtime system (C/C++), GC
● performance problems: bytecode, JIT, improve GC
● ...???
Easy?
15. Summary
● Truffle, Graal (~ Java 9)
○ “easy” to use AST interpreter framework
○ high performance (compiled to machine code!)
● Bonus: “Write your own language!”
● Contribute!
○ meetup.com: budapest.rb, budapest.js, …
○ https://github.com/jruby/jruby
○ http://openjdk.java.net/