SlideShare une entreprise Scribd logo
1  sur  67
Télécharger pour lire hors ligne
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.
Software engineers 
united we stand 
divided we fall
Patterns for JVM languages 
Jarosław Pałka
about me 
work://chief_architect@lumesse 
owner://symentis.pl 
twitter://j_palka 
blog://geekyprimitives.wordpress.com 
scm:bitbucket://kcrimson 
scm:github://kcrimson
Pet project
Because having pet projects is like solving crosswords 
everyday 
Or going to a gym 
Helps your brain to stay in shape
JVM 
is 
the 
new 
assembler
Translator
source code 
↓ 
target language source code 
↓ 
machine code || interpreter
Interpreter
source code 
↓ 
execution results
Compiler
source code 
↓ 
machine code
So how does it all work?
Diving into the stream
The ALL(*) lexer and parser generator 
Which supports multiple target languages
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 
: 
( 
'_' 
| LETTER 
)( 
'_' 
| LETTER 
| DIGIT 
)* 
; 
COMMENT 
: 
'--' .*? 'n' -> skip 
; 
NL 
: 
'r'? 'n' -> skip 
; 
WS 
: 
[ tf]+ -> skip 
;
[@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>]
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 
;
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 context); 
}
package pl.symentis.lua.grammar; 
Import pl.symentis.lua.runtime.Context; 
public interface Expression<T> { 
T evaluate(Context ctx); 
}
package pl.symentis.lua.runtime; 
public interface Context { 
VariableRef resolveVariable(String name); 
Scope enterScope(); 
Scope exitScope(); 
}
package pl.symentis.lua.runtime; 
public interface Scope { 
Scope getOuterScope(); 
VariableRef resolveVariable(String name); 
void bindVariable(String name, Object value); 
}
Into the forest of syntax tree
x = 1
public class Literal implements ExpressionNode { 
public final LuaType value; 
public Literal(LuaType value) { 
super(); 
this.value = value; 
} 
@Override 
public void accept(ExpressionVisitor visitor) { 
visitor.visit(this); 
} 
}
public class VariableName extends Var { 
public final String name; 
public VariableName(String name) { 
this.name = name; 
} 
@Override 
public void accept(ExpressionVisitor visitor) { 
visitor.visit(this); 
} 
}
public class Assignment implements StatementNode { 
public final Varlist varlist; 
public final Explist explist; 
public Assignment(Varlist varlist, Explist explist) { 
super(); 
this.varlist = varlist; 
this.explist = explist; 
} 
@Override 
public void accept(StatementVisitor visitor) { 
visitor.visit(this); 
} 
}
public void visit(Assignment assignment) { 
InterpreterExpressionVisitor varlist = createExpressionVisitor(); 
assignment.varlist.accept(varlist); 
InterpreterExpressionVisitor explist = createExpressionVisitor(); 
assignment.explist.accept(explist); 
Iterator<ExpressionNode> iterator = ((List<ExpressionNode>) explist.result()).iterator(); 
List<VariableRef> vars = (List<VariableRef>) varlist.result(); 
for (VariableRef variableRef : vars) { 
if (iterator.hasNext()) { 
ExpressionNode node = iterator.next(); 
InterpreterExpressionVisitor exp = createExpressionVisitor(); 
node.accept(exp); 
variableRef.set(exp.result()); 
} else { 
variableRef.set(LuaNil.Nil); 
} 
} 
}
public void visit(Literal literal) { 
result = literal.value; 
} 
public void visit(VariableName variableName) { 
result = context.resolveVariable(variableName.name); 
} 
public void visit(Varlist varlist) { 
List<VariableRef> refs = new ArrayList<>(); 
for (Var var : varlist.vars) { 
var.accept(this); 
refs.add((VariableRef) result()); 
} 
result = refs; 
}
translator 
interpreter 
compiler
compiler → byte code 
org.ow2.asm:asm:jar 
me.qmx.jitescript:jitescript:jar
JiteClass jiteClass = new JiteClass(classname, new String[] { p(Script.class) }); 
jiteClass.defineDefaultConstructor(); 
CodeBlock codeBlock = newCodeBlock(); 
// bind context into current runtime 
codeBlock.aload(1); 
codeBlock.invokestatic(p(RuntimeContext.class), "enterContext", sig(void.class, Context.class)); 
// parse file and start to walk through AST visitor 
StatementNode chunk = parseScript(readFileToString(script)); 
// generate byte code 
chunk.accept(new CompilerStatementVisitor(codeBlock, new HashSymbolTable())); 
codeBlock.voidreturn(); 
jiteClass.defineMethod("execute", JiteClass.ACC_PUBLIC, sig(void.class, new Class[] 
{ Context.class }), 
codeBlock); 
return jiteClass.toBytes(JDKVersion.V1_8); 
}
JiteClass jiteClass = new JiteClass(classname, new String[] { p(Script.class) }); 
jiteClass.defineDefaultConstructor(); 
CodeBlock codeBlock = newCodeBlock(); 
// bind context into current runtime 
codeBlock.aload(1); 
codeBlock.invokestatic(p(RuntimeContext.class), "enterContext", sig(void.class, Context.class)); 
// parse file and start to walk through AST visitor 
StatementNode chunk = parseScript(readFileToString(script)); 
// generate byte code 
chunk.accept(new CompilerStatementVisitor(codeBlock, new HashSymbolTable())); 
codeBlock.voidreturn(); 
jiteClass.defineMethod("execute", JiteClass.ACC_PUBLIC, sig(void.class, new Class[] 
{ Context.class }), 
codeBlock); 
return jiteClass.toBytes(JDKVersion.V1_8); 
}
JiteClass jiteClass = new JiteClass(classname, new String[] { p(Script.class) }); 
jiteClass.defineDefaultConstructor(); 
CodeBlock codeBlock = newCodeBlock(); 
// bind context into current runtime 
codeBlock.aload(1); 
codeBlock.invokestatic(p(RuntimeContext.class), "enterContext", sig(void.class, Context.class)); 
// parse file and start to walk through AST visitor 
StatementNode chunk = parseScript(readFileToString(script)); 
// generate byte code 
chunk.accept(new CompilerStatementVisitor(codeBlock, new HashSymbolTable())); 
codeBlock.voidreturn(); 
jiteClass.defineMethod("execute", JiteClass.ACC_PUBLIC, sig(void.class, new Class[] 
{ Context.class }), 
codeBlock); 
return jiteClass.toBytes(JDKVersion.V1_8); 
}
x = 1
public void visit(Assignment assignment) { 
Explist explist = assignment.explist; 
Varlist varlist = assignment.varlist; 
Iterator<Var> vars = varlist.vars.iterator(); 
for (ExpressionNode exp : explist.expressions) { 
exp.accept(new CompilerExpressionVisitor(codeBlock, symbolTable)); 
CompilerExpressionVisitor visitor = new CompilerExpressionVisitor( 
codeBlock, symbolTable); 
vars.next().accept(visitor); 
String varName = (String) visitor.result(); 
codeBlock.astore(symbolTable.index(varName)); 
} 
}
public void visit(Literal literal) { 
Object object = literal.value.asObject(); 
codeBlock.ldc(object); 
// box object into Lua type 
if (object instanceof Integer) { 
codeBlock.invokestatic(p(Integer.class), "valueOf", sig(Integer.class, int.class)); 
} else if (object instanceof Double) { 
codeBlock.invokestatic(p(Double.class), "valueOf", sig(Double.class, 
double.class)); 
} 
codeBlock.invokestatic(p(LuaTypes.class), "valueOf", sig(LuaType.class, Object.class)); 
} 
public void visit(VariableName variableName) { 
result = variableName.name; 
}
When writing bytecode compiler it helps to think about Java code 
But at the end you need to think like stack machine :)
.method public execute : (Lpl/symentis/lua4j/runtime/Context;)V 
; method code size: 21 bytes 
.limit stack 2 
.limit locals 3 
aload_1 
invokestatic pl/symentis/lua4j/compiler/RuntimeContext 
enterContext (Lpl/symentis/lua4j/runtime/Context;)V 
ldc2_w 1.0 
invokestatic java/lang/Double valueOf (D)Ljava/lang/Double; 
invokestatic pl/symentis/lua4j/types/LuaTypes valueOf [_28] 
astore_2 
aload_2 
return 
.end method
Symbol table 
maps variable local to its symbolic name
0xBA 
invokedynamic
print("Welcome","to","Jokerconf",2014)
Var var = functionCall.var; 
CompilerExpressionVisitor visitor = new CompilerExpressionVisitor(codeBlock, 
symbolTable); 
var.accept(visitor); 
String functionName = (String) visitor.result(); 
Args args = functionCall.args; 
visitor = new CompilerExpressionVisitor(codeBlock, symbolTable); 
args.accept(visitor); 
Integer argNumber = (Integer) visitor.result(); 
Class<?>[] paramTypeArr = new Class[argNumber]; 
fill(paramTypeArr, LuaType.class); 
codeBlock.invokedynamic(functionName, sig(void.class, paramTypeArr), 
Lua4JDynamicBootstrap.HANDLE, 
new Object[] {});
static final Handle HANDLE = new Handle( 
CodeBlock.H_INVOKESTATIC, 
p(Lua4JDynamicBootstrap.class), 
"bootstrap", 
sig(CallSite.class, MethodHandles.Lookup.class, String.class, 
MethodType.class));
public static CallSite bootstrap( 
MethodHandles.Lookup lookup, 
String name, 
MethodType type) throws Throwable { 
Context context = currentContext(); 
MethodHandle target=lookup.findStatic(BasicFunctions.class,name, 
methodType( 
LuaType.class, 
Context.class, 
LuaType[].class)); 
target = MethodHandles.insertArguments(handler, 0, context) 
.asCollector(LuaType[].class, type.parameterCount()); 
return new ConstantCallSite(target.asType(type)); 
}
Things to consider 
Type coercion 
Symbol table and scope 
Function handles and scope 
Global scope
https://github.com/headius/invokebinder 
A Java DSL for binding method handles forward, 
rather than backward 
https://github.com/projectodd/rephract 
Another Java DSL for invokedynamic
What's next? 
graal + truffle 
dynamic compiler + AST interpreter
Books
Thanks, 
stay cool, 
dynamically linked 
and...
Let the eternal stream of bytecode 
flow through your body
Q&A

Contenu connexe

Tendances

Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Rails
elliando dias
 
Building High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low EffortBuilding High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low Effort
Stefan Marr
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developers
Bartosz Kosarzycki
 

Tendances (20)

Apache Thrift
Apache ThriftApache Thrift
Apache Thrift
 
Natural Language Toolkit (NLTK), Basics
Natural Language Toolkit (NLTK), Basics Natural Language Toolkit (NLTK), Basics
Natural Language Toolkit (NLTK), Basics
 
The Ring programming language version 1.10 book - Part 49 of 212
The Ring programming language version 1.10 book - Part 49 of 212The Ring programming language version 1.10 book - Part 49 of 212
The Ring programming language version 1.10 book - Part 49 of 212
 
Inside the JVM - Follow the white rabbit!
Inside the JVM - Follow the white rabbit!Inside the JVM - Follow the white rabbit!
Inside the JVM - Follow the white rabbit!
 
Introduction To Groovy 2005
Introduction To Groovy 2005Introduction To Groovy 2005
Introduction To Groovy 2005
 
Iron Languages - NYC CodeCamp 2/19/2011
Iron Languages - NYC CodeCamp 2/19/2011Iron Languages - NYC CodeCamp 2/19/2011
Iron Languages - NYC CodeCamp 2/19/2011
 
Stoop 305-reflective programming5
Stoop 305-reflective programming5Stoop 305-reflective programming5
Stoop 305-reflective programming5
 
Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Rails
 
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
 
Groovy DSLs, from Beginner to Expert - Guillaume Laforge and Paul King - Spri...
Groovy DSLs, from Beginner to Expert - Guillaume Laforge and Paul King - Spri...Groovy DSLs, from Beginner to Expert - Guillaume Laforge and Paul King - Spri...
Groovy DSLs, from Beginner to Expert - Guillaume Laforge and Paul King - Spri...
 
Building High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low EffortBuilding High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low Effort
 
DRYing to Monad in Java8
DRYing to Monad in Java8DRYing to Monad in Java8
DRYing to Monad in Java8
 
Kotlin boost yourproductivity
Kotlin boost yourproductivityKotlin boost yourproductivity
Kotlin boost yourproductivity
 
Groovy!
Groovy!Groovy!
Groovy!
 
Stoop 390-instruction stream
Stoop 390-instruction streamStoop 390-instruction stream
Stoop 390-instruction stream
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developers
 
Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2
 
Introduction to Dart
Introduction to DartIntroduction to Dart
Introduction to Dart
 
Behavioral Reflection
Behavioral ReflectionBehavioral Reflection
Behavioral Reflection
 

Similaire à Patterns for JVM languages JokerConf

Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014
Jaroslaw Palka
 
AST Transformations
AST TransformationsAST Transformations
AST Transformations
HamletDRC
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
Mario Fusco
 

Similaire à Patterns for JVM languages JokerConf (20)

Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014
 
Ast transformations
Ast transformationsAst transformations
Ast transformations
 
Dart structured web apps
Dart   structured web appsDart   structured web apps
Dart structured web apps
 
Dart, unicorns and rainbows
Dart, unicorns and rainbowsDart, unicorns and rainbows
Dart, unicorns and rainbows
 
AST Transformations
AST TransformationsAST Transformations
AST Transformations
 
Dart
DartDart
Dart
 
[Deprecated] Integrating libSyntax into the compiler pipeline
[Deprecated] Integrating libSyntax into the compiler pipeline[Deprecated] Integrating libSyntax into the compiler pipeline
[Deprecated] Integrating libSyntax into the compiler pipeline
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
Dart London hackathon
Dart  London hackathonDart  London hackathon
Dart London hackathon
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and Monoids
 
Interpreter Case Study - Design Patterns
Interpreter Case Study - Design PatternsInterpreter Case Study - Design Patterns
Interpreter Case Study - Design Patterns
 
Cs267 hadoop programming
Cs267 hadoop programmingCs267 hadoop programming
Cs267 hadoop programming
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
Introduction to Spark with Scala
Introduction to Spark with ScalaIntroduction to Spark with Scala
Introduction to Spark with Scala
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
 
Power tools in Java
Power tools in JavaPower tools in Java
Power tools in Java
 
Apache Beam de A à Z
 Apache Beam de A à Z Apache Beam de A à Z
Apache Beam de A à Z
 
Lambda Chops - Recipes for Simpler, More Expressive Code
Lambda Chops - Recipes for Simpler, More Expressive CodeLambda Chops - Recipes for Simpler, More Expressive Code
Lambda Chops - Recipes for Simpler, More Expressive Code
 
Let's Go-lang
Let's Go-langLet's Go-lang
Let's Go-lang
 
Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)
 

Plus de Jaroslaw Palka

Czterej jeźdźcy apokalipsy gdy Armagedon w JVM nadchodzi
Czterej jeźdźcy apokalipsy  gdy Armagedon w JVM nadchodziCzterej jeźdźcy apokalipsy  gdy Armagedon w JVM nadchodzi
Czterej jeźdźcy apokalipsy gdy Armagedon w JVM nadchodzi
Jaroslaw Palka
 
Systematyczny architekt na drodze ku planowanemu postarzaniu
Systematyczny architekt na drodze ku planowanemu postarzaniuSystematyczny architekt na drodze ku planowanemu postarzaniu
Systematyczny architekt na drodze ku planowanemu postarzaniu
Jaroslaw Palka
 
Patterns for organic architecture
Patterns for organic architecturePatterns for organic architecture
Patterns for organic architecture
Jaroslaw Palka
 

Plus de Jaroslaw Palka (7)

We are crowd, we are anonymous
We are crowd, we are anonymousWe are crowd, we are anonymous
We are crowd, we are anonymous
 
Czterej jeźdźcy apokalipsy gdy Armagedon w JVM nadchodzi
Czterej jeźdźcy apokalipsy  gdy Armagedon w JVM nadchodziCzterej jeźdźcy apokalipsy  gdy Armagedon w JVM nadchodzi
Czterej jeźdźcy apokalipsy gdy Armagedon w JVM nadchodzi
 
Systematyczny architekt na drodze ku planowanemu postarzaniu
Systematyczny architekt na drodze ku planowanemu postarzaniuSystematyczny architekt na drodze ku planowanemu postarzaniu
Systematyczny architekt na drodze ku planowanemu postarzaniu
 
Systematyczny architekt na drodze ku planowanemu postarzaniu
Systematyczny architekt na drodze ku planowanemu postarzaniuSystematyczny architekt na drodze ku planowanemu postarzaniu
Systematyczny architekt na drodze ku planowanemu postarzaniu
 
Patterns for organic architecture
Patterns for organic architecturePatterns for organic architecture
Patterns for organic architecture
 
I ty też możesz mieć swoje dane w cache
I ty też możesz mieć swoje dane w cacheI ty też możesz mieć swoje dane w cache
I ty też możesz mieć swoje dane w cache
 
Programming and architecture of NOSQL web at 33degree
Programming and architecture of NOSQL web at 33degreeProgramming and architecture of NOSQL web at 33degree
Programming and architecture of NOSQL web at 33degree
 

Dernier

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

Dernier (20)

LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 

Patterns for JVM languages JokerConf

  • 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. Software engineers united we stand divided we fall
  • 3. Patterns for JVM languages Jarosław Pałka
  • 4. about me work://chief_architect@lumesse owner://symentis.pl twitter://j_palka blog://geekyprimitives.wordpress.com scm:bitbucket://kcrimson scm:github://kcrimson
  • 5.
  • 7. Because having pet projects is like solving crosswords everyday Or going to a gym Helps your brain to stay in shape
  • 8.
  • 9.
  • 10. JVM is the new assembler
  • 12. source code ↓ target language source code ↓ machine code || interpreter
  • 14. source code ↓ execution results
  • 16. source code ↓ machine code
  • 17. So how does it all work?
  • 18.
  • 19. Diving into the stream
  • 20. The ALL(*) lexer and parser generator Which supports multiple target languages
  • 21. source code ↓lexer token stream ↓parser IR
  • 24. local y = 1 x = 1 + y
  • 25.
  • 26.
  • 27. 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 ;
  • 28. [@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>]
  • 29. 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 ;
  • 30. beware of left recursion
  • 32. Interpreter pattern Non terminal nodes Terminal nodes
  • 33.
  • 34. package pl.symentis.lua.grammar; import pl.symentis.lua.runtime.Context; public interface Statement { void execute(Context context); }
  • 35. package pl.symentis.lua.grammar; Import pl.symentis.lua.runtime.Context; public interface Expression<T> { T evaluate(Context ctx); }
  • 36. package pl.symentis.lua.runtime; public interface Context { VariableRef resolveVariable(String name); Scope enterScope(); Scope exitScope(); }
  • 37. package pl.symentis.lua.runtime; public interface Scope { Scope getOuterScope(); VariableRef resolveVariable(String name); void bindVariable(String name, Object value); }
  • 38. Into the forest of syntax tree
  • 39. x = 1
  • 40. public class Literal implements ExpressionNode { public final LuaType value; public Literal(LuaType value) { super(); this.value = value; } @Override public void accept(ExpressionVisitor visitor) { visitor.visit(this); } }
  • 41. public class VariableName extends Var { public final String name; public VariableName(String name) { this.name = name; } @Override public void accept(ExpressionVisitor visitor) { visitor.visit(this); } }
  • 42. public class Assignment implements StatementNode { public final Varlist varlist; public final Explist explist; public Assignment(Varlist varlist, Explist explist) { super(); this.varlist = varlist; this.explist = explist; } @Override public void accept(StatementVisitor visitor) { visitor.visit(this); } }
  • 43. public void visit(Assignment assignment) { InterpreterExpressionVisitor varlist = createExpressionVisitor(); assignment.varlist.accept(varlist); InterpreterExpressionVisitor explist = createExpressionVisitor(); assignment.explist.accept(explist); Iterator<ExpressionNode> iterator = ((List<ExpressionNode>) explist.result()).iterator(); List<VariableRef> vars = (List<VariableRef>) varlist.result(); for (VariableRef variableRef : vars) { if (iterator.hasNext()) { ExpressionNode node = iterator.next(); InterpreterExpressionVisitor exp = createExpressionVisitor(); node.accept(exp); variableRef.set(exp.result()); } else { variableRef.set(LuaNil.Nil); } } }
  • 44. public void visit(Literal literal) { result = literal.value; } public void visit(VariableName variableName) { result = context.resolveVariable(variableName.name); } public void visit(Varlist varlist) { List<VariableRef> refs = new ArrayList<>(); for (Var var : varlist.vars) { var.accept(this); refs.add((VariableRef) result()); } result = refs; }
  • 46. compiler → byte code org.ow2.asm:asm:jar me.qmx.jitescript:jitescript:jar
  • 47. JiteClass jiteClass = new JiteClass(classname, new String[] { p(Script.class) }); jiteClass.defineDefaultConstructor(); CodeBlock codeBlock = newCodeBlock(); // bind context into current runtime codeBlock.aload(1); codeBlock.invokestatic(p(RuntimeContext.class), "enterContext", sig(void.class, Context.class)); // parse file and start to walk through AST visitor StatementNode chunk = parseScript(readFileToString(script)); // generate byte code chunk.accept(new CompilerStatementVisitor(codeBlock, new HashSymbolTable())); codeBlock.voidreturn(); jiteClass.defineMethod("execute", JiteClass.ACC_PUBLIC, sig(void.class, new Class[] { Context.class }), codeBlock); return jiteClass.toBytes(JDKVersion.V1_8); }
  • 48. JiteClass jiteClass = new JiteClass(classname, new String[] { p(Script.class) }); jiteClass.defineDefaultConstructor(); CodeBlock codeBlock = newCodeBlock(); // bind context into current runtime codeBlock.aload(1); codeBlock.invokestatic(p(RuntimeContext.class), "enterContext", sig(void.class, Context.class)); // parse file and start to walk through AST visitor StatementNode chunk = parseScript(readFileToString(script)); // generate byte code chunk.accept(new CompilerStatementVisitor(codeBlock, new HashSymbolTable())); codeBlock.voidreturn(); jiteClass.defineMethod("execute", JiteClass.ACC_PUBLIC, sig(void.class, new Class[] { Context.class }), codeBlock); return jiteClass.toBytes(JDKVersion.V1_8); }
  • 49. JiteClass jiteClass = new JiteClass(classname, new String[] { p(Script.class) }); jiteClass.defineDefaultConstructor(); CodeBlock codeBlock = newCodeBlock(); // bind context into current runtime codeBlock.aload(1); codeBlock.invokestatic(p(RuntimeContext.class), "enterContext", sig(void.class, Context.class)); // parse file and start to walk through AST visitor StatementNode chunk = parseScript(readFileToString(script)); // generate byte code chunk.accept(new CompilerStatementVisitor(codeBlock, new HashSymbolTable())); codeBlock.voidreturn(); jiteClass.defineMethod("execute", JiteClass.ACC_PUBLIC, sig(void.class, new Class[] { Context.class }), codeBlock); return jiteClass.toBytes(JDKVersion.V1_8); }
  • 50. x = 1
  • 51. public void visit(Assignment assignment) { Explist explist = assignment.explist; Varlist varlist = assignment.varlist; Iterator<Var> vars = varlist.vars.iterator(); for (ExpressionNode exp : explist.expressions) { exp.accept(new CompilerExpressionVisitor(codeBlock, symbolTable)); CompilerExpressionVisitor visitor = new CompilerExpressionVisitor( codeBlock, symbolTable); vars.next().accept(visitor); String varName = (String) visitor.result(); codeBlock.astore(symbolTable.index(varName)); } }
  • 52. public void visit(Literal literal) { Object object = literal.value.asObject(); codeBlock.ldc(object); // box object into Lua type if (object instanceof Integer) { codeBlock.invokestatic(p(Integer.class), "valueOf", sig(Integer.class, int.class)); } else if (object instanceof Double) { codeBlock.invokestatic(p(Double.class), "valueOf", sig(Double.class, double.class)); } codeBlock.invokestatic(p(LuaTypes.class), "valueOf", sig(LuaType.class, Object.class)); } public void visit(VariableName variableName) { result = variableName.name; }
  • 53. When writing bytecode compiler it helps to think about Java code But at the end you need to think like stack machine :)
  • 54. .method public execute : (Lpl/symentis/lua4j/runtime/Context;)V ; method code size: 21 bytes .limit stack 2 .limit locals 3 aload_1 invokestatic pl/symentis/lua4j/compiler/RuntimeContext enterContext (Lpl/symentis/lua4j/runtime/Context;)V ldc2_w 1.0 invokestatic java/lang/Double valueOf (D)Ljava/lang/Double; invokestatic pl/symentis/lua4j/types/LuaTypes valueOf [_28] astore_2 aload_2 return .end method
  • 55. Symbol table maps variable local to its symbolic name
  • 58. Var var = functionCall.var; CompilerExpressionVisitor visitor = new CompilerExpressionVisitor(codeBlock, symbolTable); var.accept(visitor); String functionName = (String) visitor.result(); Args args = functionCall.args; visitor = new CompilerExpressionVisitor(codeBlock, symbolTable); args.accept(visitor); Integer argNumber = (Integer) visitor.result(); Class<?>[] paramTypeArr = new Class[argNumber]; fill(paramTypeArr, LuaType.class); codeBlock.invokedynamic(functionName, sig(void.class, paramTypeArr), Lua4JDynamicBootstrap.HANDLE, new Object[] {});
  • 59. static final Handle HANDLE = new Handle( CodeBlock.H_INVOKESTATIC, p(Lua4JDynamicBootstrap.class), "bootstrap", sig(CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class));
  • 60. public static CallSite bootstrap( MethodHandles.Lookup lookup, String name, MethodType type) throws Throwable { Context context = currentContext(); MethodHandle target=lookup.findStatic(BasicFunctions.class,name, methodType( LuaType.class, Context.class, LuaType[].class)); target = MethodHandles.insertArguments(handler, 0, context) .asCollector(LuaType[].class, type.parameterCount()); return new ConstantCallSite(target.asType(type)); }
  • 61. Things to consider Type coercion Symbol table and scope Function handles and scope Global scope
  • 62. https://github.com/headius/invokebinder A Java DSL for binding method handles forward, rather than backward https://github.com/projectodd/rephract Another Java DSL for invokedynamic
  • 63. What's next? graal + truffle dynamic compiler + AST interpreter
  • 64. Books
  • 65. Thanks, stay cool, dynamically linked and...
  • 66. Let the eternal stream of bytecode flow through your body
  • 67. Q&A