Download presentation
Presentation is loading. Please wait.
Published byChristine Howard Modified over 9 years ago
1
© Oscar Nierstrasz ST — Introduction ST 1.1 The Smalltalk Compiler
2
© Oscar Nierstrasz ST — Introduction ST 1.2 Compiler Fully reified compilation process: —Scanner/Parser (built with SmaCC) – builds AST (from Refactoring Browser) —Semantic Analysis: ASTChecker – annotates the AST (e.g., var bindings) —Translation to IR: ASTTranslator – uses IRBuilder to build IR (Intermediate Representation) —Bytecode generation: IRTranslator – uses BytecodeBuilder to emit bytecodes
3
© Oscar Nierstrasz ST — Introduction ST 1.3 Compiler: Overview code AST Scanner / Parser Scanner / Parser Semantic Analysis Semantic Analysis Bytecode Code Generation Code Generation AST IR Build IR Build IR Bytecode Generation Bytecode Generation ASTTranslator IRBuilder IRTranslator BytecodeBuilder Code generation in detail
4
© Oscar Nierstrasz ST — Introduction ST 1.4 Compiler: Syntax SmaCC: Smalltalk Compiler Compiler —Like Lex/Yacc —Input: – Scanner definition: regular expressions – Parser: BNF-like grammar – Code that builds AST as annotation —SmaCC can build LARL(1) or LR(1) parser —Output: – class for Scanner (subclass SmaCCScanner) – class for Parser (subclass SmaCCParser)
5
© Oscar Nierstrasz ST — Introduction ST 1.5 Scanner
6
© Oscar Nierstrasz ST — Introduction ST 1.6 Parser
7
© Oscar Nierstrasz ST — Introduction ST 1.7 Calling Parser code
8
© Oscar Nierstrasz ST — Introduction ST 1.8 Compiler: AST AST: Abstract Syntax Tree —Encodes the Syntax as a Tree —No semantics yet! —Uses the RB Tree: – visitors – backward pointers in ParseNodes – transformation (replace/add/delete) – pattern directed TreeRewriter – PrettyPrinter RBProgramNode RBDoItNode RBMethodNode RBReturnNode RBSequenceNode RBValueNode RBArrayNode RBAssignmentNode RBBlockNode RBCascadeNode RBLiteralNode RBMessageNode RBOptimizedNode RBVariableNode RBProgramNode RBDoItNode RBMethodNode RBReturnNode RBSequenceNode RBValueNode RBArrayNode RBAssignmentNode RBBlockNode RBCascadeNode RBLiteralNode RBMessageNode RBOptimizedNode RBVariableNode
9
© Oscar Nierstrasz ST — Introduction ST 1.9 Compiler: Semantics We need to analyse the AST —Names need to be linked to the variables according to the scoping rules ASTChecker implemented as a Visitor —Subclass of RBProgramNodeVisitor —Visits the nodes —Grows and shrinks scope chain —Methods/Blocks are linked with the scope —Variable definitions and references are linked with objects describing the variables
10
© Oscar Nierstrasz ST — Introduction ST 1.10 A Simple Tree
11
© Oscar Nierstrasz ST — Introduction ST 1.11 A Simple Visitor RBProgramNodeVisitor new visitNode: tree Does nothing except walk through the tree
12
© Oscar Nierstrasz ST — Introduction ST 1.12 TestVisitor RBProgramNodeVisitor subclass: #TestVisitor instanceVariableNames: 'literals' classVariableNames: '' poolDictionaries: '' category: 'Compiler-AST-Visitors' TestVisitor>>acceptLiteralNode: aLiteralNode literals add: aLiteralNode value. TestVisitor>>initialize literals := Set new. TestVisitor>>literals ^literals RBProgramNodeVisitor subclass: #TestVisitor instanceVariableNames: 'literals' classVariableNames: '' poolDictionaries: '' category: 'Compiler-AST-Visitors' TestVisitor>>acceptLiteralNode: aLiteralNode literals add: aLiteralNode value. TestVisitor>>initialize literals := Set new. TestVisitor>>literals ^literals tree := RBParser parseExpression: '3 + 4'. (TestVisitor new visitNode: tree) literals tree := RBParser parseExpression: '3 + 4'. (TestVisitor new visitNode: tree) literals a Set(3 4)
13
© Oscar Nierstrasz ST — Introduction ST 1.13 Compiler: Intermediate Representation IR: Intermediate Representation —Semantic like Bytecode, but more abstract —Independent of the bytecode set —IR is a tree —IR nodes allow easy transformation —Decompilation to RB AST IR build from AST using ASTTranslator: —AST Visitor —Uses IRBuilder
14
© Oscar Nierstrasz ST — Introduction ST 1.14 Compiler: Bytecode Generation IR needs to be converted to Bytecode —IRTranslator: Visitor for IR tree —Uses BytecodeBuilder to generate Bytecode —Builds a compiledMethod testReturn1 | iRMethod aCompiledMethod | iRMethod := IRBuilder new numRargs: 1; addTemps: #(self);"receiver and args declarations" pushLiteral: 1; returnTop; ir. testReturn1 | iRMethod aCompiledMethod | iRMethod := IRBuilder new numRargs: 1; addTemps: #(self);"receiver and args declarations" pushLiteral: 1; returnTop; ir. aCompiledMethod := iRMethod compiledMethod. self should: [(aCompiledMethod valueWithReceiver: nil arguments: #() ) = 1]. aCompiledMethod := iRMethod compiledMethod. self should: [(aCompiledMethod valueWithReceiver: nil arguments: #() ) = 1]. CHECK the example!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.