ST Introduction The Smalltalk Compiler Oscar Nierstrasz 1

  • Slides: 14
Download presentation
ST — Introduction The Smalltalk Compiler © Oscar Nierstrasz 1

ST — Introduction The Smalltalk Compiler © Oscar Nierstrasz 1

ST — Introduction Compiler > Fully reified compilation process: — Scanner/Parser (built with Sma.

ST — Introduction Compiler > Fully reified compilation process: — Scanner/Parser (built with Sma. CC) – 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 – © Oscar Nierstrasz uses Bytecode. Builder to emit bytecodes 2

ST — Introduction Compiler: Overview code Scanner / Parser AST Semantic Analysis AST Code

ST — Introduction Compiler: Overview code Scanner / Parser AST Semantic Analysis AST Code Generation Bytecode Code generation in detail AST Build IR ASTTranslator IRBuilder © Oscar Nierstrasz IR Bytecode Generation Bytecode IRTranslator Bytecode. Builder 3

ST — Introduction Compiler: Syntax > Sma. CC: Smalltalk Compiler — Like Lex/Yacc —

ST — Introduction Compiler: Syntax > Sma. CC: Smalltalk Compiler — Like Lex/Yacc — Input: – – – Scanner definition: regular expressions Parser: BNF-like grammar Code that builds AST as annotation — Sma. CC can build LARL(1) or LR(1) parser — Output: – – © Oscar Nierstrasz class for Scanner (subclass Sma. CCScanner) class for Parser (subclass Sma. CCParser) 4

ST — Introduction Scanner © Oscar Nierstrasz 5

ST — Introduction Scanner © Oscar Nierstrasz 5

ST — Introduction Parser © Oscar Nierstrasz 6

ST — Introduction Parser © Oscar Nierstrasz 6

ST — Introduction Calling Parser code © Oscar Nierstrasz 7

ST — Introduction Calling Parser code © Oscar Nierstrasz 7

ST — Introduction Compiler: AST > AST: Abstract Syntax Tree — Encodes the Syntax

ST — Introduction Compiler: AST > AST: Abstract Syntax Tree — Encodes the Syntax as a Tree — No semantics yet! — Uses the RB Tree: – – – © Oscar Nierstrasz visitors backward pointers in Parse. Nodes transformation (replace/add/delete) pattern directed Tree. Rewriter Pretty. Printer RBProgram. Node RBDo. It. Node RBMethod. Node RBReturn. Node RBSequence. Node RBValue. Node RBArray. Node RBAssignment. Node RBBlock. Node RBCascade. Node RBLiteral. Node RBMessage. Node RBOptimized. Node RBVariable. Node 8

ST — Introduction Compiler: Semantics > We need to analyse the AST — Names

ST — Introduction 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 RBProgram. Node. Visitor — 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 © Oscar Nierstrasz 9

ST — Introduction A Simple Tree © Oscar Nierstrasz 10

ST — Introduction A Simple Tree © Oscar Nierstrasz 10

ST — Introduction A Simple Visitor RBProgram. Node. Visitor new visit. Node: tree Does

ST — Introduction A Simple Visitor RBProgram. Node. Visitor new visit. Node: tree Does nothing except walk through the tree © Oscar Nierstrasz 11

ST — Introduction Test. Visitor RBProgram. Node. Visitor subclass: #Test. Visitor instance. Variable. Names:

ST — Introduction Test. Visitor RBProgram. Node. Visitor subclass: #Test. Visitor instance. Variable. Names: 'literals' class. Variable. Names: '' pool. Dictionaries: '' category: 'Compiler-AST-Visitors' Test. Visitor>>accept. Literal. Node: a. Literal. Node literals add: a. Literal. Node value. Test. Visitor>>initialize literals : = Set new. Test. Visitor>>literals ^literals tree : = RBParser parse. Expression: '3 + 4'. (Test. Visitor new visit. Node: tree) literals a Set(3 4) © Oscar Nierstrasz 12

ST — Introduction Compiler: Intermediate Representation > IR: Intermediate Representation — Semantic like Bytecode,

ST — Introduction 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 © Oscar Nierstrasz 13

ST — Introduction Compiler: Bytecode Generation > IR — — — CHECK the example!

ST — Introduction Compiler: Bytecode Generation > IR — — — CHECK the example! needs to be converted to Bytecode IRTranslator: Visitor for IR tree Uses Bytecode. Builder to generate Bytecode Builds a compiled. Method test. Return 1 | i. RMethod a. Compiled. Method | i. RMethod : = IRBuilder new num. Rargs: 1; add. Temps: #(self); "receiver and args declarations" push. Literal: 1; return. Top; a. Compiled. Method : = i. RMethod compiled. Method. ir. self should: [(a. Compiled. Method value. With. Receiver: nil arguments: #() ) = 1]. © Oscar Nierstrasz 14