SlideShare ist ein Scribd-Unternehmen logo
1 von 77
Downloaden Sie, um offline zu lesen
Compiler Case Study -
Design Patterns in C#
Ganesh Samarthyam
ganesh@codeops.tech
Design Patterns - Introduction
“Applying design principles is the key to creating
high-quality software!”
Architectural principles:
Axis, symmetry, rhythm, datum, hierarchy, transformation
How to Apply Principles in Practice?
Design principles
Code
How to bridge
the gap?
What are Smells?
“Smells'are'certain'structures'
in'the'code'that'suggest'
(some4mes'they'scream'for)'
the'possibility'of'refactoring.”''
What is Refactoring?
Refactoring (noun): a change
made to the internal structure of
software to make it easier to
understand and cheaper to
modify without changing its
observable behavior
Refactor (verb): to restructure
software by applying a series
of refactorings without
changing its observable
behavior
What are Design Patterns?
recurrent solutions
to common
design problems
Pattern Name
Problem
Solution
Consequences
Principles to Patterns
Expr expr = Addition.Make(
Multiplication.Make(
Constant.Make(10),
Constant.Make(20)),
Constant.Make(10));
Expr expr = new ExprBuilder().Const(10).Mult(20).Plus(30).Build();
Encapsulate object creation
Builder pattern
Why Care About Patterns?
❖ Patterns capture expert
knowledge in the form of
proven reusable solutions
❖ Better to reuse proven
solutions than to “re-invent”
the wheel
❖ When used correctly, patterns
positively inïŹ‚uence software
quality
❖ Creates maintainable,
extensible, and reusable code
Design Patterns - Catalog
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Design Patterns - Catalog
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Creational
Deals with controlled
object creation
Factory method, for
example
Structural
Deals with
composition of classes
or objects
Composite, for example
Behavioral
Deals with interaction
between objects/
classes and
distribution of
responsibility
Strategy, for example
3 Principles Behind Patterns
Design'principles'
behind'pa0erns'
Program'to'an'
interface,'not'to'an'
implementa7on''
Favor'object'
composi7on''
over'inheritance'
Encapsulate'what'
varies'
Compiler Case Study
Case Study Overview
❖ Smelly code segments (specially if-else based on types
and switch statements) used ïŹrst
❖ They are “refactored” using design patterns
❖ The case study shows code-snippets; compilable, self-
contained source code ïŹles shared with you
What You Need to Know
❖ Understand essential object oriented constructs such as runtime
polymorphism (virtual functions), and principles (such as
encapsulation)
❖ Know essential C# language features - classes, inheritance, etc
❖ Data structures and algorithms (especially binary trees, tree
traversals, and stacks)
❖ NO background in compilers needed
❖ Extremely simpliïŹed compiler code example; purposefully
done to make best use of this case study
Simple Expression Tree Example
+
* 30
10 20
Expression: ((10 * 20) + 30)
Post-order Traversal: Simulating Code Gen
10 20 * 30 +
post-order
traversal
result
+
* 30
10 20
Front-end
Intermediate code (CIL, Java bytecodes, 
)
Interpreter (Virtual
Machine)
Using Stack for Evaluation
Initial
empty
10
push 10
10
push 20
20
200
perform
10 * 20
push
200
200
push 30
30
230
perform
200 +
30
push
230
End
result
= 230
10 20 * 30 +
Mnemonic Names in Java and .NET
10 20 * 30 +
push 10
push 20
multiply
push 30
add
.NET CIL Instructions Java Bytecode Instructions
ldc.i4.s 0x0a
ldc.i4.s 0x14
mul
ldc.i4.s 0x1e
add
(ldc => load constant ;
i4 => integer of 32 bits;
s => signed)
bipush 10
bipush 20
imul
bipush 30
iadd
(bipush stands for “byte integer push”)
Hands-on Exercise
Use the ildasm tool to disassemble the Expr.cs code
// Expr.cs
using System;
class Test {
    public static void Main() {
        int a = 10, b = 20, c = 30;
        int r = ((a * b) + c);
        Console.WriteLine(r);
    }
}
((10	*	20)	+	30)Source Code
C#
Compiler
.NET CLR
(Common
Language
Runtime)
ldc.i4.s 0x0a
ldc.i4.s 0x14
mul
ldc.i4.s 0x1e
add
+
* 30
10 20
Facade Pattern
Provide a uniïŹed interface to a set of interfaces in a subsystem. Facade deïŹnes a
higher-level interface that makes the subsystem easier to use.
Facade Pattern in Compilers
“The Compiler class acts as a facade: It offers clients a single,
simple interface to the compiler subsystem.”
Facade Pattern in Compilers
public virtual void Compile(StreamReader input, BytecodeStream output) {
Scanner scanner = new Scanner(input);
var builder = new ProgramNodeBuilder();
Parser parser = new Parser();
parser.Parse(scanner, builder);
RISCCodeGenerator generator = new RISCCodeGenerator(output);
ProgramNode parseTree = builder.GetRootNode();
parseTree.Traverse(generator);
}
Procedural (conditional) Code
How to get rid of
conditional statements?
public void GenCode()
{
if (_left == null && _right == null)
{
Console.WriteLine("ldc.i4.s " + _value);
}
else
{
// its an intermediate node
_left.GenCode();
_right.GenCode();
switch (_value)
{
case "+":
Console.WriteLine("add");
break;
case "-":
Console.WriteLine("sub");
break;
case "*":
Console.WriteLine("mul");
break;
case "/":
Console.WriteLine("div");
break;
default:
Console.WriteLine("Not implemented yet!");
break;
}
}
}
Hands-on Exercise
Refactor the “../Composite/Before/Compiler.cs” to
get rid of conditional statements and use runtime
polymorphism instead
Hands-on Exercise
internal abstract class Expr
{
public abstract void GenCode();
}
internal class Constant : Expr
{
private readonly int _val;
public Constant(int val)
{
_val = val;
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
internal class BinaryExpr : Expr
{
private readonly Expr _left;
private readonly Expr _right;
public BinaryExpr(Expr arg1, Expr arg2)
{
_left = arg1;
_right = arg2;
}
public override void GenCode()
{
_left.GenCode();
_right.GenCode();
}
}
internal class Plus : BinaryExpr
{
public Plus(Expr arg1, Expr arg2) : base(arg1, arg2)
{
}
public override void GenCode()
{
base.GenCode();
Console.WriteLine("add");
}
}
Composite Pattern
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Composite Pattern: Discussion
❖ There are many situations where
a group of components form
larger components
❖ Simplistic approach: Make
container component contain
primitive ones
❖ Problem: Code has to treat
container and primitive
components differently
Compose objects into tree structures to represent part-whole hierarchies. Composite
lets client treat individual objects and compositions of objects uniformly.
❖ Perform recursive
composition of
components
❖ Clients don’t have to
treat container and
primitive components
differently
BTW, Did You Observe?
// ((10 * 20) + 30)
var expr = new Expr(
new Expr(
new Expr(null, "10", null), "*", new Expr(null, "20", null)),
"+",
new Expr(null, "30", null));
expr.GenCode();
// ((10 * 20) + 10)
Expr expr = new Plus(
new Multiply(
new Constant(10),
new Constant(20)),
new Constant(30));
expr.GenCode();
Hands-on Exercise
function interpret(node: binary expression tree)
| if node is a leaf then
return (node.value)
| if node is binary operator op then
return interpret(node.left) op interpret(node.right)
Rewrite the code to “evaluate” the expression instead
of generating the code - here is the “pseudo-code”
Solution
internal class Constant : Expr
{
private readonly int _val;
public Constant(int arg)
{
_val = arg;
}
public override int Interpret()
{
return _val;
}
}
internal class Plus : Expr
{
private readonly Expr _left;
private readonly Expr _right;
public Plus(Expr arg1, Expr arg2)
{
_left = arg1;
_right = arg2;
}
public override int Interpret()
{
return _left.Interpret() + _right.Interpret();
}
}
Register-Based Instructions (x86)
movl $10, -8(%rbp)
movl $20, -12(%rbp)
movl $30, -16(%rbp)
movl -8(%rbp), %eax
imull -12(%rbp), %eax
addl -16(%rbp), %eax
popq %rbp
retq
int main() {
int a = 10;
int b = 20;
int c = 30;
return ((a * b) + c);
}
cc -S expr.c
Register-Based Instructions (Android)
((a * b) + c);
javac -source 1.7 -target 1.7 expr.java
dx --dex --output=expr.dex expr.class
dexdump -d expr.dex
const/16 v1, #int 10 // #a
const/16 v2, #int 20 // #14
const/16 v3, #int 30 // #1e
mul-int v0, v1, v2
add-int/2addr v0, v3
return v0
Hands-on Exercise
Generate register-based instructions for the given
expression ((10 * 20) + 30)
You can generate for an imaginary machine (i.e., a
virtual machine like Dalvik/Android) or a real-
machine (like x86-64)
Solution
internal class Register
{
private readonly int _index;
private Register(int index)
{
_index = index;
}
public static Register GetRegister(int index)
{
return new Register(index);
}
// FreeRegister to be implemented
// not implemented for now for the sake of simplicity
public int GetIndex()
{
return _index;
}
}
Solution
abstract class Expr {
public abstract Register GenCode();
}
class Constant : Expr {
int val;
public Constant(int arg) {
val = arg;
}
public override Register GenCode() {
Register targetRegister = RegisterAllocator.GetNextRegister();
Console.WriteLine("const/16 v{0}, #int {1}", targetRegister.GetIndex(), val);
return targetRegister;
}
}
class Plus : Expr {
private Expr left, right;
public Plus(Expr arg1, Expr arg2) {
left = arg1;
right = arg2;
}
public override Register GenCode() {
Register firstRegister = left.GenCode();
Register secondRegister = right.GenCode();
Register targetRegister = RegisterAllocator.GetNextRegister();
Console.WriteLine("add-int v{0}, v{1}, v{2}", targetRegister.GetIndex(), firstRegister.GetIndex(),
secondRegister.GetIndex());
return targetRegister;
}
}
Solution
((10 + 20) + 30);
Assuming inïŹnite registers from v0,
with target register as the ïŹrst argument,
here is the mnemonic code generated for our
imaginary (i.e., virtual) machine
const/16 v0, #int 10
const/16 v1, #int 20
add-int v2, v0, v1
const/16 v3, #int 30
add-int v4, v2, v3
How to Improve the Tree Creation?
var expr = Addition.Make(
Multiplication.Make(
Constant.Make(10),
Constant.Make(20)),
Constant.Make(10));
Expr expr = new Plus(
new Multiply(
new Constant(10),
new Constant(20)),
new Constant(30));
Hands-on Exercise: Solution
internal class Constant : Expr
{
private readonly int _val;
private Constant(int val)
{
_val = val;
}
public static Constant Make(int arg)
{
return new Constant(arg);
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
internal class Constant : Expr
{
private readonly int _val;
public Constant(int val)
{
_val = val;
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
Factory Method Pattern: Structure
Factory Method Pattern: Discussion
❖ A class cannot anticipate the
class of objects it must create
❖ A class wants its subclasses to
specify the objects it creates
DeïŹne an interface for creating an object, but let subclasses decide which class to
instantiate. Factory method lets a class defer instantiation to subclasses.
❖ Delegate the responsibility
to one of the several helper
subclasses
❖ Also, localize the
knowledge of which
subclass is the delegate
Scenario
internal class Constant : Expr
{
private readonly int _val;
private Constant(int val)
{
_val = val;
}
public static Constant Make(int arg)
{
return new Constant(arg);
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
For expressions like ((10 * 20)
+ 10), why should we create
different Constant objects?
Can we reuse them?
Hands-on Exercise
Improve this code to reuse immutable objects (for the
ones with the same values) instead of creating
duplicate objects.
public static Constant Make(int arg)
{
return new Constant(arg);
}
Hands-on Exercise: Solution
internal class Constant : Expr
{
private static readonly
Dictionary<int, Constant> Pool = new Dictionary<int, Constant>();
private readonly int _val;
private Constant(int val)
{
_val = val;
}
public static Constant Make(int arg)
{
if (!Pool.ContainsKey(arg)) Pool[arg] = new Constant(arg);
return Pool[arg];
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
Flyweight Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Flyweight Pattern: Discussion
❖ When an application uses a
large number of small objects,
it can be expensive
❖ How to share objects at
granular level without
prohibitive cost?
Use sharing to support large numbers of ïŹne-grained objects efïŹciently
❖ When it is possible to share
objects (i.e., objects don't
depend on identity)
❖ When object’s value
remain the same
irrespective of the contexts
- they can be shared
❖ Share the commonly used
objects in a pool
Scenario
// ((10 * 20) + 10)
var expr = Addition.Make(
Multiplication.Make(
Constant.Make(10),
Constant.Make(20)),
Constant.Make(10));
expr.GenCode();
This code still sucks - if the
expression becomes more
complex, can you really read
it? Can we simplify this?
Hands-on Exercise
Improve this code to use “builder pattern”.
// ((10 * 20) + 10)
var expr = Addition.Make(
Multiplication.Make(
Constant.Make(10),
Constant.Make(20)),
Constant.Make(10));
expr.GenCode();
var expr = new ExprBuilder().Const(10).Mult(20).Plus(30).Build();
Hands-on Exercise: Solution
internal class ExprBuilder
{
private Expr _expr;
public ExprBuilder Const(int arg)
{
_expr = Constant.Make(arg);
return this;
}
public ExprBuilder Plus(int arg)
{
_expr = new Addition(_expr, Constant.Make(arg));
return this;
}
public ExprBuilder Mult(int arg)
{
_expr = new Multiplication(_expr, Constant.Make(arg));
return this;
}
public Expr Build()
{
return _expr;
}
}
Builder Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Builder Pattern: Discussion
❖ Creating or assembling a
complex object can be tedious
Separate the construction of a complex object from its representation so that the same
construction process can create different representations.
❖ Make the algorithm for
creating a complex object
independent of parts that
make up the object and
how they are assembled
❖ The construction process
allows different
representations for the
object that is constructed
Scenario
Provide the ability to traverse
the aggregate structure
without exposing
implementation details (also
use iteration instead of
recursion)
internal class Constant : Expr
{
private readonly int _val;
public Constant(int val)
{
_val = val;
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
internal class Plus : BinaryExpr
{
public Plus(Expr arg1, Expr arg2) : base(arg1, arg2)
{
}
public override void GenCode()
{
base.GenCode();
Console.WriteLine("add");
}
}
Hands-on Exercise
Traverse the expression tree using “external” iteration
public override void GenCode()
{
base.GenCode();
Console.WriteLine("add");
}
Hands-on Exercise: Solution
internal class ExprIterator
{
public static IEnumerable<Expr> Traverse(Expr node)
{
if (node != null)
{
foreach (var left in Traverse(node.GetLeft())) yield return left;
foreach (var right in Traverse(node.GetRight())) yield return right;
yield return node;
}
}
}
foreach (var node in ExprIterator.Traverse(expr))
node.GenCode();
Iterator Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Iterator Pattern: Discussion
❖ How to provide a way to
traverse an aggregate structure
in different ways, but without
exposing its implementation
details?
Provide a way to access the elements of an aggregate object sequentially without
exposing its underlying representation.
❖ Take the responsibility for
access and traversal out of
the list object and put it
into an iterator object
❖ Let the iterator keep track
of the element visited,
traversal order, etc
Scenario
How to separate:
a) code generation logic
from node types?
b) how to support different
target types?
class Plus extends Expr {
private Expr left, right;
public Plus(Expr arg1, Expr arg2) {
left = arg1;
right = arg2;
}
public void genCode() {
left.genCode();
right.genCode();
if(t == Target.JVM) {
System.out.println("iadd");
}
else { // DOTNET
System.out.println("add");
}
}
}
Hands-on Exercise
Refactor this code to remove the if-else condition check
(explicit type-checking code) and use runtime
polymorphism instead
if(t == Target.JVM) {
System.out.println("iadd");
}
else { // DOTNET
System.out.println("add");
}
Hands-on Exercise
internal abstract class Target
{
public abstract void GenCode(Constant constant);
public abstract void GenCode(Plus plus);
public abstract void GenCode(Mult mult);
}
internal class JvmTarget : Target
{
public override void GenCode(Constant constant)
{
Console.WriteLine("bipush " + constant.GetValue());
}
public override void GenCode(Plus plus)
{
Console.WriteLine("iadd");
}
public override void GenCode(Mult mult)
{
Console.WriteLine("imul");
}
}
Strategy Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Strategy Pattern: Discussion
❖ Useful when there is a set of
related algorithms and a client
object needs to be able to
dynamically pick and choose
an algorithm that suits its
current need
DeïŹnes a family of algorithms, encapsulates each one, and makes
them interchangeable. Strategy lets the algorithm vary
independently from clients that use it
❖ The implementation of each of the
algorithms is kept in a separate
class referred to as a strategy.
❖ An object that uses a Strategy
object is referred to as a context
object.
❖ Changing the behavior of a
Context object is a matter of
changing its Strategy object to the
one that implements the required
algorithm
Solution Using Visitor pattern
class Plus extends Expr {
private Expr left, right;
public Plus(Expr arg1, Expr arg2) {
left = arg1;
right = arg2;
}
public Expr getLeft() {
return left;
}
public Expr getRight() {
return right;
}
public void accept(Visitor v) {
v.visit(this);
}
}
class DOTNETVisitor extends Visitor {
public void visit(Constant arg) {
System.out.println("ldarg " + arg.getVal());
}
public void visit(Plus plus) {
genCode(plus.getLeft());
genCode(plus.getRight());
System.out.println("add");
}
public void visit(Sub sub) {
genCode(sub.getLeft());
genCode(sub.getRight());
System.out.println("sub");
}
public void genCode(Expr expr) {
expr.accept(this);
}
}
Visitor Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Visitor Pattern: Call Sequence
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Visitor Pattern: Discussion
❖ Many distinct and unrelated
operations need to be
performed on objects in an
object structure, and you want
to avoid “polluting” their
classes with these operations
Represent an operation to be performed on the elements of an object structure.
Visitor lets you deïŹne a new operation without changing the classes of the
elements on which it operations
❖ Create two class
hierarchies:
❖ One for the elements
being operated on
❖ One for the visitors that
deïŹne operations on the
elements
Procedural (conditional) Code
How to get rid of
conditional statements?
internal class Interpreter
{
private static readonly Stack<int> ExecutionStack = new Stack<int>();
private static int Interpret(byte[] byteCodes)
{
var pc = 0;
while (pc < byteCodes.Length)
switch (byteCodes[pc++])
{
case (byte) BYTECODE.ADD:
ExecutionStack.Push(ExecutionStack.Pop() + ExecutionStack.Pop());
break;
case (byte) BYTECODE.LDCI4S:
ExecutionStack.Push(byteCodes[pc++]);
break;
}
return ExecutionStack.Pop();
}
// rest of the code elided ...
}
Hands-on Exercise
Refactor this code to remove the switch statements and
use runtime polymorphism instead
switch (byteCodes[pc++])
{
case (byte) BYTECODE.ADD:
ExecutionStack.Push(ExecutionStack.Pop() + ExecutionStack.Pop());
break;
case (byte) BYTECODE.LDCI4S:
ExecutionStack.Push(byteCodes[pc++]);
break;
}
Command Pattern: Solution
internal class Interpreter
{
private readonly Stack<int> _evalStack = new Stack<int>();
public int Interpret(ByteCode[] byteCodes)
{
foreach (var byteCode in byteCodes) byteCode.Exec(_evalStack);
return _evalStack.Pop();
}
}
internal abstract class ByteCode
{
public abstract void Exec(Stack<int> evalStack);
}
internal class ADD : ByteCode
{
public override void Exec(Stack<int> evalStack)
{
var lval = evalStack.Pop();
var rval = evalStack.Pop();
evalStack.Push(rval + lval);
}
}
Command Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Command Pattern: Discussion
❖ Want to issue requests or
execute commands without
knowing anything about the
operation being requested or
the receiver of the request
Encapsulate a request as an object, thereby letting you parameterize clients with
different requests, queue or log requests, and support undoable operations.
❖ Parameterize objects by an
action to perform, with a
common method such as
Execute
❖ Can be useful to specify,
queue, and execute
requests at different times;
also to support undo or
logging
Patterns Discussed in this Case-Study
✓ Facade
✓ Composite
✓ Factory method
✓ Flyweight
✓ Builder
✓ Iterator pattern
✓ Strategy
✓ Visitor
✓ Command
BTW, What Architecture Style is it?
Lexical Analysis
Parsing
Semantic Analysis
Program ïŹle
Intermediate Code
Generation
Optimization
Native Code
Generation
Native assembly/machine code
Token stream
AST (Abstract Syntax Tree)
Attributed tree/
augmented syntax tree
Intermediate code
Optimized
intermediate code
Its “pipe-and-filter” Style!
sediment
pre-
carbon
ultra-ïŹlter
post-
carbon
Filtered
water
Real-world “pipes and ïŹlters”
Pattern Oriented Software Architecture (POSA)
ganesh@codeops.tech @GSamarthyam
www.codeops.tech slideshare.net/sgganesh
+91 98801 64463 bit.ly/sgganesh

Weitere Àhnliche Inhalte

Was ist angesagt?

Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9
koolkampus
 

Was ist angesagt? (20)

Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Design Patterns & JDK Examples
Design Patterns & JDK ExamplesDesign Patterns & JDK Examples
Design Patterns & JDK Examples
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp Presentation
 
Data Persistence in Android with Room Library
Data Persistence in Android with Room LibraryData Persistence in Android with Room Library
Data Persistence in Android with Room Library
 
Software design
Software designSoftware design
Software design
 
Svelte JS introduction
Svelte JS introductionSvelte JS introduction
Svelte JS introduction
 
JavaScript Data Types
JavaScript Data TypesJavaScript Data Types
JavaScript Data Types
 
Regular Expressions in Java
Regular Expressions in JavaRegular Expressions in Java
Regular Expressions in Java
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Software Engineering Fundamentals
Software Engineering FundamentalsSoftware Engineering Fundamentals
Software Engineering Fundamentals
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
 
Windows form application - C# Training
Windows form application - C# Training Windows form application - C# Training
Windows form application - C# Training
 
Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9
 
Android activity lifecycle
Android activity lifecycleAndroid activity lifecycle
Android activity lifecycle
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptx
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UML
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Cohesion and coupling
Cohesion and couplingCohesion and coupling
Cohesion and coupling
 

Ähnlich wie Design Patterns - Compiler Case Study - Hands-on Examples

Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
Romain Francois
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
Sudharsan S
 

Ähnlich wie Design Patterns - Compiler Case Study - Hands-on Examples (20)

Compiler Case Study - Design Patterns in C#
Compiler Case Study - Design Patterns in C#Compiler Case Study - Design Patterns in C#
Compiler Case Study - Design Patterns in C#
 
Applied Design Patterns - A Compiler Case Study
Applied Design Patterns - A Compiler Case Study Applied Design Patterns - A Compiler Case Study
Applied Design Patterns - A Compiler Case Study
 
Effective Object Oriented Design in Cpp
Effective Object Oriented Design in CppEffective Object Oriented Design in Cpp
Effective Object Oriented Design in Cpp
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and Monoids
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”
 
Novidades do c# 7 e 8
Novidades do c# 7 e 8Novidades do c# 7 e 8
Novidades do c# 7 e 8
 
Roslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
Roslyn: el futuro de C# y VB.NET by Rodolfo FinochiettiRoslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
Roslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
 
Roslyn: el futuro de C#
Roslyn: el futuro de C#Roslyn: el futuro de C#
Roslyn: el futuro de C#
 
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
sonam Kumari python.ppt
sonam Kumari python.pptsonam Kumari python.ppt
sonam Kumari python.ppt
 
PVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications developmentPVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications development
 
Interpreter Case Study - Design Patterns
Interpreter Case Study - Design PatternsInterpreter Case Study - Design Patterns
Interpreter Case Study - Design Patterns
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
 

Mehr von Ganesh Samarthyam

Mehr von Ganesh Samarthyam (20)

Wonders of the Sea
Wonders of the SeaWonders of the Sea
Wonders of the Sea
 
Animals - for kids
Animals - for kids Animals - for kids
Animals - for kids
 
Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in Practice
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't Enough
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - Description
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief Presentation
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - Poster
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship Deck
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming Language
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz Questions
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz Questions
 
Docker by Example - Quiz
Docker by Example - QuizDocker by Example - Quiz
Docker by Example - Quiz
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quiz
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
 

KĂŒrzlich hochgeladen

Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
Abortion Pill Prices Tembisa [(+27832195400*)] đŸ„ Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] đŸ„ Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] đŸ„ Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] đŸ„ Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

KĂŒrzlich hochgeladen (20)

Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
Abortion Pill Prices Tembisa [(+27832195400*)] đŸ„ Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] đŸ„ Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] đŸ„ Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] đŸ„ Women's Abortion Clinic in T...
 
%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
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%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
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 

Design Patterns - Compiler Case Study - Hands-on Examples

  • 1. Compiler Case Study - Design Patterns in C# Ganesh Samarthyam ganesh@codeops.tech
  • 2. Design Patterns - Introduction
  • 3. “Applying design principles is the key to creating high-quality software!” Architectural principles: Axis, symmetry, rhythm, datum, hierarchy, transformation
  • 4. How to Apply Principles in Practice? Design principles Code How to bridge the gap?
  • 6. What is Refactoring? Refactoring (noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior Refactor (verb): to restructure software by applying a series of refactorings without changing its observable behavior
  • 7. What are Design Patterns? recurrent solutions to common design problems Pattern Name Problem Solution Consequences
  • 8. Principles to Patterns Expr expr = Addition.Make( Multiplication.Make( Constant.Make(10), Constant.Make(20)), Constant.Make(10)); Expr expr = new ExprBuilder().Const(10).Mult(20).Plus(30).Build(); Encapsulate object creation Builder pattern
  • 9. Why Care About Patterns? ❖ Patterns capture expert knowledge in the form of proven reusable solutions ❖ Better to reuse proven solutions than to “re-invent” the wheel ❖ When used correctly, patterns positively inïŹ‚uence software quality ❖ Creates maintainable, extensible, and reusable code
  • 10. Design Patterns - Catalog Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 11. Design Patterns - Catalog Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994 Creational Deals with controlled object creation Factory method, for example Structural Deals with composition of classes or objects Composite, for example Behavioral Deals with interaction between objects/ classes and distribution of responsibility Strategy, for example
  • 12. 3 Principles Behind Patterns Design'principles' behind'pa0erns' Program'to'an' interface,'not'to'an' implementa7on'' Favor'object' composi7on'' over'inheritance' Encapsulate'what' varies'
  • 14. Case Study Overview ❖ Smelly code segments (specially if-else based on types and switch statements) used ïŹrst ❖ They are “refactored” using design patterns ❖ The case study shows code-snippets; compilable, self- contained source code ïŹles shared with you
  • 15. What You Need to Know ❖ Understand essential object oriented constructs such as runtime polymorphism (virtual functions), and principles (such as encapsulation) ❖ Know essential C# language features - classes, inheritance, etc ❖ Data structures and algorithms (especially binary trees, tree traversals, and stacks) ❖ NO background in compilers needed ❖ Extremely simpliïŹed compiler code example; purposefully done to make best use of this case study
  • 16. Simple Expression Tree Example + * 30 10 20 Expression: ((10 * 20) + 30)
  • 17. Post-order Traversal: Simulating Code Gen 10 20 * 30 + post-order traversal result + * 30 10 20
  • 18. Front-end Intermediate code (CIL, Java bytecodes, 
) Interpreter (Virtual Machine)
  • 19. Using Stack for Evaluation Initial empty 10 push 10 10 push 20 20 200 perform 10 * 20 push 200 200 push 30 30 230 perform 200 + 30 push 230 End result = 230 10 20 * 30 +
  • 20. Mnemonic Names in Java and .NET 10 20 * 30 + push 10 push 20 multiply push 30 add .NET CIL Instructions Java Bytecode Instructions ldc.i4.s 0x0a ldc.i4.s 0x14 mul ldc.i4.s 0x1e add (ldc => load constant ; i4 => integer of 32 bits; s => signed) bipush 10 bipush 20 imul bipush 30 iadd (bipush stands for “byte integer push”)
  • 21. Hands-on Exercise Use the ildasm tool to disassemble the Expr.cs code // Expr.cs using System; class Test {     public static void Main() {         int a = 10, b = 20, c = 30;         int r = ((a * b) + c);         Console.WriteLine(r);     } }
  • 22. ((10 * 20) + 30)Source Code C# Compiler .NET CLR (Common Language Runtime) ldc.i4.s 0x0a ldc.i4.s 0x14 mul ldc.i4.s 0x1e add + * 30 10 20
  • 23. Facade Pattern Provide a uniïŹed interface to a set of interfaces in a subsystem. Facade deïŹnes a higher-level interface that makes the subsystem easier to use.
  • 24. Facade Pattern in Compilers “The Compiler class acts as a facade: It offers clients a single, simple interface to the compiler subsystem.”
  • 25. Facade Pattern in Compilers public virtual void Compile(StreamReader input, BytecodeStream output) { Scanner scanner = new Scanner(input); var builder = new ProgramNodeBuilder(); Parser parser = new Parser(); parser.Parse(scanner, builder); RISCCodeGenerator generator = new RISCCodeGenerator(output); ProgramNode parseTree = builder.GetRootNode(); parseTree.Traverse(generator); }
  • 26. Procedural (conditional) Code How to get rid of conditional statements? public void GenCode() { if (_left == null && _right == null) { Console.WriteLine("ldc.i4.s " + _value); } else { // its an intermediate node _left.GenCode(); _right.GenCode(); switch (_value) { case "+": Console.WriteLine("add"); break; case "-": Console.WriteLine("sub"); break; case "*": Console.WriteLine("mul"); break; case "/": Console.WriteLine("div"); break; default: Console.WriteLine("Not implemented yet!"); break; } } }
  • 27. Hands-on Exercise Refactor the “../Composite/Before/Compiler.cs” to get rid of conditional statements and use runtime polymorphism instead
  • 28. Hands-on Exercise internal abstract class Expr { public abstract void GenCode(); } internal class Constant : Expr { private readonly int _val; public Constant(int val) { _val = val; } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } } internal class BinaryExpr : Expr { private readonly Expr _left; private readonly Expr _right; public BinaryExpr(Expr arg1, Expr arg2) { _left = arg1; _right = arg2; } public override void GenCode() { _left.GenCode(); _right.GenCode(); } } internal class Plus : BinaryExpr { public Plus(Expr arg1, Expr arg2) : base(arg1, arg2) { } public override void GenCode() { base.GenCode(); Console.WriteLine("add"); } }
  • 29. Composite Pattern Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 30. Composite Pattern: Discussion ❖ There are many situations where a group of components form larger components ❖ Simplistic approach: Make container component contain primitive ones ❖ Problem: Code has to treat container and primitive components differently Compose objects into tree structures to represent part-whole hierarchies. Composite lets client treat individual objects and compositions of objects uniformly. ❖ Perform recursive composition of components ❖ Clients don’t have to treat container and primitive components differently
  • 31. BTW, Did You Observe? // ((10 * 20) + 30) var expr = new Expr( new Expr( new Expr(null, "10", null), "*", new Expr(null, "20", null)), "+", new Expr(null, "30", null)); expr.GenCode(); // ((10 * 20) + 10) Expr expr = new Plus( new Multiply( new Constant(10), new Constant(20)), new Constant(30)); expr.GenCode();
  • 32. Hands-on Exercise function interpret(node: binary expression tree) | if node is a leaf then return (node.value) | if node is binary operator op then return interpret(node.left) op interpret(node.right) Rewrite the code to “evaluate” the expression instead of generating the code - here is the “pseudo-code”
  • 33. Solution internal class Constant : Expr { private readonly int _val; public Constant(int arg) { _val = arg; } public override int Interpret() { return _val; } } internal class Plus : Expr { private readonly Expr _left; private readonly Expr _right; public Plus(Expr arg1, Expr arg2) { _left = arg1; _right = arg2; } public override int Interpret() { return _left.Interpret() + _right.Interpret(); } }
  • 34. Register-Based Instructions (x86) movl $10, -8(%rbp) movl $20, -12(%rbp) movl $30, -16(%rbp) movl -8(%rbp), %eax imull -12(%rbp), %eax addl -16(%rbp), %eax popq %rbp retq int main() { int a = 10; int b = 20; int c = 30; return ((a * b) + c); } cc -S expr.c
  • 35. Register-Based Instructions (Android) ((a * b) + c); javac -source 1.7 -target 1.7 expr.java dx --dex --output=expr.dex expr.class dexdump -d expr.dex const/16 v1, #int 10 // #a const/16 v2, #int 20 // #14 const/16 v3, #int 30 // #1e mul-int v0, v1, v2 add-int/2addr v0, v3 return v0
  • 36. Hands-on Exercise Generate register-based instructions for the given expression ((10 * 20) + 30) You can generate for an imaginary machine (i.e., a virtual machine like Dalvik/Android) or a real- machine (like x86-64)
  • 37. Solution internal class Register { private readonly int _index; private Register(int index) { _index = index; } public static Register GetRegister(int index) { return new Register(index); } // FreeRegister to be implemented // not implemented for now for the sake of simplicity public int GetIndex() { return _index; } }
  • 38. Solution abstract class Expr { public abstract Register GenCode(); } class Constant : Expr { int val; public Constant(int arg) { val = arg; } public override Register GenCode() { Register targetRegister = RegisterAllocator.GetNextRegister(); Console.WriteLine("const/16 v{0}, #int {1}", targetRegister.GetIndex(), val); return targetRegister; } } class Plus : Expr { private Expr left, right; public Plus(Expr arg1, Expr arg2) { left = arg1; right = arg2; } public override Register GenCode() { Register firstRegister = left.GenCode(); Register secondRegister = right.GenCode(); Register targetRegister = RegisterAllocator.GetNextRegister(); Console.WriteLine("add-int v{0}, v{1}, v{2}", targetRegister.GetIndex(), firstRegister.GetIndex(), secondRegister.GetIndex()); return targetRegister; } }
  • 39. Solution ((10 + 20) + 30); Assuming inïŹnite registers from v0, with target register as the ïŹrst argument, here is the mnemonic code generated for our imaginary (i.e., virtual) machine const/16 v0, #int 10 const/16 v1, #int 20 add-int v2, v0, v1 const/16 v3, #int 30 add-int v4, v2, v3
  • 40. How to Improve the Tree Creation? var expr = Addition.Make( Multiplication.Make( Constant.Make(10), Constant.Make(20)), Constant.Make(10)); Expr expr = new Plus( new Multiply( new Constant(10), new Constant(20)), new Constant(30));
  • 41. Hands-on Exercise: Solution internal class Constant : Expr { private readonly int _val; private Constant(int val) { _val = val; } public static Constant Make(int arg) { return new Constant(arg); } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } } internal class Constant : Expr { private readonly int _val; public Constant(int val) { _val = val; } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } }
  • 43. Factory Method Pattern: Discussion ❖ A class cannot anticipate the class of objects it must create ❖ A class wants its subclasses to specify the objects it creates DeïŹne an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses. ❖ Delegate the responsibility to one of the several helper subclasses ❖ Also, localize the knowledge of which subclass is the delegate
  • 44. Scenario internal class Constant : Expr { private readonly int _val; private Constant(int val) { _val = val; } public static Constant Make(int arg) { return new Constant(arg); } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } } For expressions like ((10 * 20) + 10), why should we create different Constant objects? Can we reuse them?
  • 45. Hands-on Exercise Improve this code to reuse immutable objects (for the ones with the same values) instead of creating duplicate objects. public static Constant Make(int arg) { return new Constant(arg); }
  • 46. Hands-on Exercise: Solution internal class Constant : Expr { private static readonly Dictionary<int, Constant> Pool = new Dictionary<int, Constant>(); private readonly int _val; private Constant(int val) { _val = val; } public static Constant Make(int arg) { if (!Pool.ContainsKey(arg)) Pool[arg] = new Constant(arg); return Pool[arg]; } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } }
  • 47. Flyweight Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 48. Flyweight Pattern: Discussion ❖ When an application uses a large number of small objects, it can be expensive ❖ How to share objects at granular level without prohibitive cost? Use sharing to support large numbers of ïŹne-grained objects efïŹciently ❖ When it is possible to share objects (i.e., objects don't depend on identity) ❖ When object’s value remain the same irrespective of the contexts - they can be shared ❖ Share the commonly used objects in a pool
  • 49. Scenario // ((10 * 20) + 10) var expr = Addition.Make( Multiplication.Make( Constant.Make(10), Constant.Make(20)), Constant.Make(10)); expr.GenCode(); This code still sucks - if the expression becomes more complex, can you really read it? Can we simplify this?
  • 50. Hands-on Exercise Improve this code to use “builder pattern”. // ((10 * 20) + 10) var expr = Addition.Make( Multiplication.Make( Constant.Make(10), Constant.Make(20)), Constant.Make(10)); expr.GenCode(); var expr = new ExprBuilder().Const(10).Mult(20).Plus(30).Build();
  • 51. Hands-on Exercise: Solution internal class ExprBuilder { private Expr _expr; public ExprBuilder Const(int arg) { _expr = Constant.Make(arg); return this; } public ExprBuilder Plus(int arg) { _expr = new Addition(_expr, Constant.Make(arg)); return this; } public ExprBuilder Mult(int arg) { _expr = new Multiplication(_expr, Constant.Make(arg)); return this; } public Expr Build() { return _expr; } }
  • 52. Builder Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 53. Builder Pattern: Discussion ❖ Creating or assembling a complex object can be tedious Separate the construction of a complex object from its representation so that the same construction process can create different representations. ❖ Make the algorithm for creating a complex object independent of parts that make up the object and how they are assembled ❖ The construction process allows different representations for the object that is constructed
  • 54. Scenario Provide the ability to traverse the aggregate structure without exposing implementation details (also use iteration instead of recursion) internal class Constant : Expr { private readonly int _val; public Constant(int val) { _val = val; } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } } internal class Plus : BinaryExpr { public Plus(Expr arg1, Expr arg2) : base(arg1, arg2) { } public override void GenCode() { base.GenCode(); Console.WriteLine("add"); } }
  • 55. Hands-on Exercise Traverse the expression tree using “external” iteration public override void GenCode() { base.GenCode(); Console.WriteLine("add"); }
  • 56. Hands-on Exercise: Solution internal class ExprIterator { public static IEnumerable<Expr> Traverse(Expr node) { if (node != null) { foreach (var left in Traverse(node.GetLeft())) yield return left; foreach (var right in Traverse(node.GetRight())) yield return right; yield return node; } } } foreach (var node in ExprIterator.Traverse(expr)) node.GenCode();
  • 57. Iterator Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 58. Iterator Pattern: Discussion ❖ How to provide a way to traverse an aggregate structure in different ways, but without exposing its implementation details? Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. ❖ Take the responsibility for access and traversal out of the list object and put it into an iterator object ❖ Let the iterator keep track of the element visited, traversal order, etc
  • 59. Scenario How to separate: a) code generation logic from node types? b) how to support different target types? class Plus extends Expr { private Expr left, right; public Plus(Expr arg1, Expr arg2) { left = arg1; right = arg2; } public void genCode() { left.genCode(); right.genCode(); if(t == Target.JVM) { System.out.println("iadd"); } else { // DOTNET System.out.println("add"); } } }
  • 60. Hands-on Exercise Refactor this code to remove the if-else condition check (explicit type-checking code) and use runtime polymorphism instead if(t == Target.JVM) { System.out.println("iadd"); } else { // DOTNET System.out.println("add"); }
  • 61. Hands-on Exercise internal abstract class Target { public abstract void GenCode(Constant constant); public abstract void GenCode(Plus plus); public abstract void GenCode(Mult mult); } internal class JvmTarget : Target { public override void GenCode(Constant constant) { Console.WriteLine("bipush " + constant.GetValue()); } public override void GenCode(Plus plus) { Console.WriteLine("iadd"); } public override void GenCode(Mult mult) { Console.WriteLine("imul"); } }
  • 62. Strategy Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 63. Strategy Pattern: Discussion ❖ Useful when there is a set of related algorithms and a client object needs to be able to dynamically pick and choose an algorithm that suits its current need DeïŹnes a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it ❖ The implementation of each of the algorithms is kept in a separate class referred to as a strategy. ❖ An object that uses a Strategy object is referred to as a context object. ❖ Changing the behavior of a Context object is a matter of changing its Strategy object to the one that implements the required algorithm
  • 64. Solution Using Visitor pattern class Plus extends Expr { private Expr left, right; public Plus(Expr arg1, Expr arg2) { left = arg1; right = arg2; } public Expr getLeft() { return left; } public Expr getRight() { return right; } public void accept(Visitor v) { v.visit(this); } } class DOTNETVisitor extends Visitor { public void visit(Constant arg) { System.out.println("ldarg " + arg.getVal()); } public void visit(Plus plus) { genCode(plus.getLeft()); genCode(plus.getRight()); System.out.println("add"); } public void visit(Sub sub) { genCode(sub.getLeft()); genCode(sub.getRight()); System.out.println("sub"); } public void genCode(Expr expr) { expr.accept(this); } }
  • 65. Visitor Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 66. Visitor Pattern: Call Sequence Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 67. Visitor Pattern: Discussion ❖ Many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid “polluting” their classes with these operations Represent an operation to be performed on the elements of an object structure. Visitor lets you deïŹne a new operation without changing the classes of the elements on which it operations ❖ Create two class hierarchies: ❖ One for the elements being operated on ❖ One for the visitors that deïŹne operations on the elements
  • 68. Procedural (conditional) Code How to get rid of conditional statements? internal class Interpreter { private static readonly Stack<int> ExecutionStack = new Stack<int>(); private static int Interpret(byte[] byteCodes) { var pc = 0; while (pc < byteCodes.Length) switch (byteCodes[pc++]) { case (byte) BYTECODE.ADD: ExecutionStack.Push(ExecutionStack.Pop() + ExecutionStack.Pop()); break; case (byte) BYTECODE.LDCI4S: ExecutionStack.Push(byteCodes[pc++]); break; } return ExecutionStack.Pop(); } // rest of the code elided ... }
  • 69. Hands-on Exercise Refactor this code to remove the switch statements and use runtime polymorphism instead switch (byteCodes[pc++]) { case (byte) BYTECODE.ADD: ExecutionStack.Push(ExecutionStack.Pop() + ExecutionStack.Pop()); break; case (byte) BYTECODE.LDCI4S: ExecutionStack.Push(byteCodes[pc++]); break; }
  • 70. Command Pattern: Solution internal class Interpreter { private readonly Stack<int> _evalStack = new Stack<int>(); public int Interpret(ByteCode[] byteCodes) { foreach (var byteCode in byteCodes) byteCode.Exec(_evalStack); return _evalStack.Pop(); } } internal abstract class ByteCode { public abstract void Exec(Stack<int> evalStack); } internal class ADD : ByteCode { public override void Exec(Stack<int> evalStack) { var lval = evalStack.Pop(); var rval = evalStack.Pop(); evalStack.Push(rval + lval); } }
  • 71. Command Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 72. Command Pattern: Discussion ❖ Want to issue requests or execute commands without knowing anything about the operation being requested or the receiver of the request Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. ❖ Parameterize objects by an action to perform, with a common method such as Execute ❖ Can be useful to specify, queue, and execute requests at different times; also to support undo or logging
  • 73. Patterns Discussed in this Case-Study ✓ Facade ✓ Composite ✓ Factory method ✓ Flyweight ✓ Builder ✓ Iterator pattern ✓ Strategy ✓ Visitor ✓ Command
  • 74. BTW, What Architecture Style is it? Lexical Analysis Parsing Semantic Analysis Program ïŹle Intermediate Code Generation Optimization Native Code Generation Native assembly/machine code Token stream AST (Abstract Syntax Tree) Attributed tree/ augmented syntax tree Intermediate code Optimized intermediate code
  • 76. Pattern Oriented Software Architecture (POSA)