The following presentation is provided under DHMB
license (Do Not Hurt My Brain).
Lecturer is not responsible for the fina...
Patterns for JVM languages
Polish JUG, 2014
about me
work://chief_architect@lumesse
owner://symentis.pl
twitter://j_palka
blog://geekyprimitives.wordpress.com
scm:bit...
JVM
is
the
new
assembler
translator
interpreter
compiler
source code
↓
target language source code
↓
machine code || interpreter
source code
↓
execution results
source code
↓
machine code
So how does it all work?
source code
↓lexer
token stream
↓parser
IR
Intermediate Representation
visitor
listener
tree
Syntax tree
local y = 1
x = 1 + y
fragment
DIGIT
:
[0-9]
;
fragment
LETTER
:
[a-zA-Z]
;
String
:
''' .*? '''
| '"' .*? '"'
;
Number
:
DIGIT+
;
Name
:
(
'_'
...
[@0,[0..1)='x',<49>]
[@1,[2..3)='=',<36>]
[@2,[4..5)='1',<48>]
[@3,[6..7)='+',<33>]
[@4,[8..15)='"Hello"',<47>]
[@5,[15..1...
varlist returns [Varlist result]
:
var
{$result = createVarlist($var.result);}
(
',' var
{$result = createVarlist($result,...
beware of left recursion
translator
interpreter
compiler
Interpreter pattern
Non terminal nodes
Terminal nodes
package pl.symentis.lua.grammar;
import pl.symentis.lua.runtime.Context;
public interface Statement {
void execute(Context...
package pl.symentis.lua.grammar;
import pl.symentis.lua.runtime.Context;
public interface Expression<T> {
T evaluate(Conte...
package pl.symentis.lua.runtime;
public interface Context {
VariableRef resolveVariable(String name);
Scope enterScope();
...
package pl.symentis.lua.runtime;
public interface Scope {
Scope getOuterScope();
VariableRef resolveVariable(String name);...
translator
interpreter
compiler
compiler → byte code
org.ow2.asm:asm:jar
me.qmx.jitescript:jitescript:jar
JiteClass jiteClass = new JiteClass(classname,new String[] { p(Statement.class) });
jiteClass.defineDefaultConstructor();
...
When writing bytecode compiler it helps to think
about Java code
But at the end you need to think like stack machine :)
@Override
public void execute(Context context) {
x = valueOf(1).plus(valueOf(2));
}
public void visitExpression(CodeBlock codeBlock) {
codeBlock.ldc(2);
codeBlock.invokestatic(p(Integer.class), "valueOf",si...
Symbol table
maps variable locals to its symbolic name
Pillars of every language design
Visibility
Types
Runtime
What's next?
Just let me know
type analisys and optimizations
invokedynamic
graal + truffle
Or interpreter/compiler hackathon
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Upcoming SlideShare
Loading in...5
×

Patterns for JVM languages

241

Published on

Have you ever wondered what does it take to create new language? Have you ever wanted to contribute to your favourite one? Don't have time to read "the dragon book"? No problem.

During this talk I will gently introduce you to world of token streams, lexers, parsers and compilers. I will try to show you patterns and tools you have at your disposal, at the same time not diving to deep into theory, rather focusing on practical aspects. Of course I will focus on JVM, bytecode and latest changes in a world of JVM, which make life of language developers easier.

Published in: Software, Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
241
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Patterns for JVM languages

  1. 1. The following presentation is provided under DHMB license (Do Not Hurt My Brain). Lecturer is not responsible for the financial and moral damages resulting from taking too seriously the content of the presentation. Including permanent damage to neurons, reduction of neurotransmitter activity at the molecular level and group dismissal.
  2. 2. Patterns for JVM languages Polish JUG, 2014
  3. 3. about me work://chief_architect@lumesse owner://symentis.pl twitter://j_palka blog://geekyprimitives.wordpress.com scm:bitbucket://kcrimson scm:github://kcrimson
  4. 4. JVM is the new assembler
  5. 5. translator interpreter compiler
  6. 6. source code ↓ target language source code ↓ machine code || interpreter
  7. 7. source code ↓ execution results
  8. 8. source code ↓ machine code
  9. 9. So how does it all work?
  10. 10. source code ↓lexer token stream ↓parser IR
  11. 11. Intermediate Representation visitor listener tree
  12. 12. Syntax tree
  13. 13. local y = 1 x = 1 + y
  14. 14. fragment DIGIT : [0-9] ; fragment LETTER : [a-zA-Z] ; String : ''' .*? ''' | '"' .*? '"' ; Number : DIGIT+ ; Name : ( '_' | LETTER ) ( '_' | LETTER | DIGIT )* ; COMMENT : '--' .*? 'n' -> skip ; NL : 'r'? 'n' -> skip ; WS : [ tf]+ -> skip ;
  15. 15. [@0,[0..1)='x',<49>] [@1,[2..3)='=',<36>] [@2,[4..5)='1',<48>] [@3,[6..7)='+',<33>] [@4,[8..15)='"Hello"',<47>] [@5,[15..16)=';',<37>] [@6,[17..17)='',<-1=EOF>]
  16. 16. varlist returns [Varlist result] : var {$result = createVarlist($var.result);} ( ',' var {$result = createVarlist($result,$var.result);} )* ; // var returns [Var result] : Name { $result = createVariableName($Name.text); } | prefixexp '[' exp ']' | prefixexp '.' Name ;
  17. 17. beware of left recursion
  18. 18. translator interpreter compiler
  19. 19. Interpreter pattern Non terminal nodes Terminal nodes
  20. 20. package pl.symentis.lua.grammar; import pl.symentis.lua.runtime.Context; public interface Statement { void execute(Context context); }
  21. 21. package pl.symentis.lua.grammar; import pl.symentis.lua.runtime.Context; public interface Expression<T> { T evaluate(Context ctx); }
  22. 22. package pl.symentis.lua.runtime; public interface Context { VariableRef resolveVariable(String name); Scope enterScope(); Scope exitScope(); }
  23. 23. package pl.symentis.lua.runtime; public interface Scope { Scope getOuterScope(); VariableRef resolveVariable(String name); void bindVariable(String name, Object value); }
  24. 24. translator interpreter compiler
  25. 25. compiler → byte code org.ow2.asm:asm:jar me.qmx.jitescript:jitescript:jar
  26. 26. JiteClass jiteClass = new JiteClass(classname,new String[] { p(Statement.class) }); jiteClass.defineDefaultConstructor(); // parse file and start to walk through AST visitor final CodeBlock codeBlock = newCodeBlock(); chunk.accept(new CompilerVisitor(codeBlock)); codeBlock.voidreturn(); jiteClass.defineMethod("execute", JiteClass.ACC_PUBLIC, sig(void.class, new Class[] { Context.class }), codeBlock); return jiteClass.toBytes(JDKVersion.V1_7);
  27. 27. When writing bytecode compiler it helps to think about Java code But at the end you need to think like stack machine :)
  28. 28. @Override public void execute(Context context) { x = valueOf(1).plus(valueOf(2)); }
  29. 29. public void visitExpression(CodeBlock codeBlock) { codeBlock.ldc(2); codeBlock.invokestatic(p(Integer.class), "valueOf",sig(Integer.class, int.class)); codeBlock.invokestatic(p(LuaTypes.class), "valueOf",sig(LuaType.class,Object.class)); codeBlock.astore(2); codeBlock.ldc(1); codeBlock.invokestatic(p(Integer.class), "valueOf",sig(Integer.class, int.class)); codeBlock.invokestatic(p(LuaTypes.class), "valueOf", sig(LuaType.class, Object.class)); codeBlock.aload(2); codeBlock.invokeinterface(p(LuaType.class), "plus",sig(LuaType.class, LuaType.class)); codeBlock.astore(3); }
  30. 30. Symbol table maps variable locals to its symbolic name
  31. 31. Pillars of every language design Visibility Types Runtime
  32. 32. What's next? Just let me know type analisys and optimizations invokedynamic graal + truffle
  33. 33. Or interpreter/compiler hackathon
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×