towards a flexible pharo compiler
DESCRIPTION
ESUG IWST 2013TRANSCRIPT
![Page 1: Towards a flexible Pharo Compiler](https://reader037.vdocument.in/reader037/viewer/2022100600/554f98adb4c90586258b46c7/html5/thumbnails/1.jpg)
Towards a flexible Pharo Compiler
Clement Bera and Marcus Denker
![Page 2: Towards a flexible Pharo Compiler](https://reader037.vdocument.in/reader037/viewer/2022100600/554f98adb4c90586258b46c7/html5/thumbnails/2.jpg)
Three Problems
• Architecture is not reusable
• Compiler can not be parametrized
• The mapping between source code and bytecode is overly complex.
![Page 3: Towards a flexible Pharo Compiler](https://reader037.vdocument.in/reader037/viewer/2022100600/554f98adb4c90586258b46c7/html5/thumbnails/3.jpg)
Reusability
• AST is special for the Compiler
• Tools use own AST (RB)
• AST is destroyed when compiling
• No reusable backend/parser...
![Page 4: Towards a flexible Pharo Compiler](https://reader037.vdocument.in/reader037/viewer/2022100600/554f98adb4c90586258b46c7/html5/thumbnails/4.jpg)
No Parametrization
• No pluggable archicture
• Parser, code generator fixed
• No infrastructure for compiler options
![Page 5: Towards a flexible Pharo Compiler](https://reader037.vdocument.in/reader037/viewer/2022100600/554f98adb4c90586258b46c7/html5/thumbnails/5.jpg)
Mapping bc2source
• For the Debugger, we need to map bytecode to source offsets
• With closures, we need to map temp offsets to real temps.
Old Compiler: Encoder builds complex table structure
![Page 6: Towards a flexible Pharo Compiler](https://reader037.vdocument.in/reader037/viewer/2022100600/554f98adb4c90586258b46c7/html5/thumbnails/6.jpg)
Solution: OPAL
• New compiler framework for Pharo
• Default compiler in Pharo3
• Old Compiler will be a loadable package
![Page 7: Towards a flexible Pharo Compiler](https://reader037.vdocument.in/reader037/viewer/2022100600/554f98adb4c90586258b46c7/html5/thumbnails/7.jpg)
Design
• RB AST
• Visitors
• Bytecode level IR
Smalltalk source code
Refactoring browser abstract syntax tree
Intermediate representation
Bytecode
RBParser
ASTTranslator + IRBuilder
IRByteCodeGenerator
OCSemanticAnnotator
![Page 8: Towards a flexible Pharo Compiler](https://reader037.vdocument.in/reader037/viewer/2022100600/554f98adb4c90586258b46c7/html5/thumbnails/8.jpg)
Reusability
• AST is unchanged
• Backend independent
AST InterpreterOz/Hazelnut
ReflectivityMetalinks
Class Builder
Smart suggestionsNode navigation
![Page 9: Towards a flexible Pharo Compiler](https://reader037.vdocument.in/reader037/viewer/2022100600/554f98adb4c90586258b46c7/html5/thumbnails/9.jpg)
Parametrization
• Explicit compiler context
• All visitors are pluggable
Smalltalk source code
Refactoring browser abstract syntax tree
Intermediate representation
Bytecode
Compilation context
![Page 10: Towards a flexible Pharo Compiler](https://reader037.vdocument.in/reader037/viewer/2022100600/554f98adb4c90586258b46c7/html5/thumbnails/10.jpg)
Compiler Options
• Turn off inlining of ifTrue: and friends
MyClass>>foo<compilerOptions: - optionInlineIf>
^ #myNonBooleanObject ifTrue: [ 1 ] ifFalse: [ 0 ]
![Page 11: Towards a flexible Pharo Compiler](https://reader037.vdocument.in/reader037/viewer/2022100600/554f98adb4c90586258b46c7/html5/thumbnails/11.jpg)
Mapping
• Mapping uses AST directly
source codeBytecode
foo ^ 1 + 2 + 3
76 77 B0 20 B0 7C
IRMethod RBMethodNode
RBSequenceNode
RBReturnNode
RBMessageNode 1
RBLiteralValueNode 1
IRPushLiteral
IRSequence
RBLiteral ValueNode 1
IRPushLiteralRBLiteral
ValueNode 2
IRReturnRBReturnNode
IRSendRBMessageNode 1
IRPushLiteralRBLiteral
ValueNode 3RBMessageNode 2 RBLiteral
ValueNode 3
IRSendRBMessageNode 2
B0+ 2
1 2
3+ 2
+ 3
^ 1 + 2 + 3
^ 1 + 2 + 3
foo ^ 1 + 2 + 3
RBLiteralValueNode 2
![Page 12: Towards a flexible Pharo Compiler](https://reader037.vdocument.in/reader037/viewer/2022100600/554f98adb4c90586258b46c7/html5/thumbnails/12.jpg)
Performance
• Visitors and IR do cost a bit of speed
• But not much
![Page 13: Towards a flexible Pharo Compiler](https://reader037.vdocument.in/reader037/viewer/2022100600/554f98adb4c90586258b46c7/html5/thumbnails/13.jpg)
Conclusion
• Opal solves the problems of the old compiler
• Important basis for many features you will see in Pharo3 and Pharo4
![Page 14: Towards a flexible Pharo Compiler](https://reader037.vdocument.in/reader037/viewer/2022100600/554f98adb4c90586258b46c7/html5/thumbnails/14.jpg)
Conclusion
• Opal solves the problems of the old compiler
• Important basis for many features you will see in Pharo3 and Pharo4
Questions?